Fix 3106858, where some CTS tests fail in BitmapFactory's decode
operations.

When BitmapFactory.Options is not set, e.g.: in the case of BitmapFactory.decodeFile(String pathName),
we generate the options settings in the native codes based on the device configuration and image
format. This resulting options may be different from the options we set to get the testing bitmap.
That's why we failed in these testing cases. Also, the failures happen in Gingerbread
because we modified the default value of BitmapFactory.Options in 288471d8a,
specifically the values of inDither and inPreferredConfig.

To fix it, we set Options for the testing bitmap based on the config of the bitmap we get from
ImageView.setImageURI() and the like.

Bug: 3106858

Change-Id: I24a04c6ee3f93e01605c48b174a7e90b91169c2d
diff --git a/tests/src/android/widget/cts/WidgetTestUtils.java b/tests/src/android/widget/cts/WidgetTestUtils.java
index 0a8f6dc..2df6629 100644
--- a/tests/src/android/widget/cts/WidgetTestUtils.java
+++ b/tests/src/android/widget/cts/WidgetTestUtils.java
@@ -48,7 +48,8 @@
         }
 
         // b1 and b2 are all not null.
-        if (b1.getWidth() != b2.getWidth() || b1.getHeight() != b2.getHeight()) {
+        if (b1.getWidth() != b2.getWidth() || b1.getHeight() != b2.getHeight()
+            || b1.getConfig() != b2.getConfig()) {
             Assert.fail("the bitmaps are not equal");
         }
 
@@ -119,4 +120,19 @@
         options.inScaled = false;
         return BitmapFactory.decodeResource(resources, resId, options);
     }
+
+    /**
+     * Retrieve a dithered bitmap that can be used for comparison on any density
+     * @param resources
+     * @param config the preferred config for the returning bitmap
+     * @return the {@link Bitmap} or <code>null</code>
+     */
+    public static Bitmap getUnscaledAndDitheredBitmap(Resources resources,
+            int resId, Bitmap.Config config) {
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inDither = true;
+        options.inScaled = false;
+        options.inPreferredConfig = config;
+        return BitmapFactory.decodeResource(resources, resId, options);
+    }
 }
diff --git a/tests/tests/widget/src/android/widget/cts/ImageSwitcherTest.java b/tests/tests/widget/src/android/widget/cts/ImageSwitcherTest.java
index 9e32748..a7667ba 100644
--- a/tests/tests/widget/src/android/widget/cts/ImageSwitcherTest.java
+++ b/tests/tests/widget/src/android/widget/cts/ImageSwitcherTest.java
@@ -136,18 +136,20 @@
         imageSwitcher.setImageURI(uri);
         assertSame(iv1, imageSwitcher.getCurrentView());
 
-        Bitmap testImageBitmap = WidgetTestUtils.getUnscaledBitmap(getContext().getResources(),
-                R.raw.testimage);
         BitmapDrawable currViewBitmap =
             (BitmapDrawable) ((ImageView) imageSwitcher.getCurrentView()).getDrawable();
+        Bitmap testImageBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(
+                getContext().getResources(), R.raw.testimage,
+                currViewBitmap.getBitmap().getConfig());
         WidgetTestUtils.assertEquals(testImageBitmap, currViewBitmap.getBitmap());
 
         createSampleImage(imagefile, R.raw.scenery);
         uri = Uri.parse(imagefile.getPath());
         imageSwitcher.setImageURI(uri);
         assertSame(iv, imageSwitcher.getCurrentView());
-        Bitmap sceneryImageBitmap = WidgetTestUtils.getUnscaledBitmap(getContext().getResources(),
-                R.raw.scenery);
+        Bitmap sceneryImageBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(
+                getContext().getResources(), R.raw.scenery,
+                currViewBitmap.getBitmap().getConfig());
         currViewBitmap =
             (BitmapDrawable) ((ImageView) imageSwitcher.getCurrentView()).getDrawable();
         WidgetTestUtils.assertEquals(sceneryImageBitmap, currViewBitmap.getBitmap());
diff --git a/tests/tests/widget/src/android/widget/cts/ImageViewTest.java b/tests/tests/widget/src/android/widget/cts/ImageViewTest.java
index 5214b50..0777259 100644
--- a/tests/tests/widget/src/android/widget/cts/ImageViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/ImageViewTest.java
@@ -276,10 +276,12 @@
         assertTrue(mImageView.isLayoutRequested());
         assertNotNull(mImageView.getDrawable());
 
-        Bitmap testimageBitmap = WidgetTestUtils.getUnscaledBitmap(mActivity.getResources(),
-                R.raw.testimage);
         Drawable imageViewDrawable = mImageView.getDrawable();
         BitmapDrawable imageViewBitmap = (BitmapDrawable) imageViewDrawable;
+        Bitmap.Config viewConfig = imageViewBitmap.getBitmap().getConfig();
+        Bitmap testimageBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(
+                mActivity.getResources(), R.raw.testimage, viewConfig);
+
         WidgetTestUtils.assertEquals(testimageBitmap, imageViewBitmap.getBitmap());
     }
 
diff --git a/tests/tests/widget/src/android/widget/cts/SimpleAdapterTest.java b/tests/tests/widget/src/android/widget/cts/SimpleAdapterTest.java
index 6fb7063..d20582b 100644
--- a/tests/tests/widget/src/android/widget/cts/SimpleAdapterTest.java
+++ b/tests/tests/widget/src/android/widget/cts/SimpleAdapterTest.java
@@ -410,10 +410,10 @@
             mSimpleAdapter.setViewImage(view, SimpleCursorAdapterTest.createTestImage(mContext,
                     "testimage", com.android.cts.stub.R.raw.testimage));
             assertNotNull(view.getDrawable());
-            Bitmap testBitmap = WidgetTestUtils.getUnscaledBitmap(mContext.getResources(),
-                    com.android.cts.stub.R.raw.testimage);
-            WidgetTestUtils.assertEquals(testBitmap,
-                    ((BitmapDrawable) view.getDrawable()).getBitmap());
+            Bitmap actualBitmap = ((BitmapDrawable) view.getDrawable()).getBitmap();
+            Bitmap testBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(mContext.getResources(),
+                    com.android.cts.stub.R.raw.testimage, actualBitmap.getConfig());
+            WidgetTestUtils.assertEquals(testBitmap, actualBitmap);
         } finally {
             SimpleCursorAdapterTest.destroyTestImage(mContext,"testimage");
         }
diff --git a/tests/tests/widget/src/android/widget/cts/SimpleCursorAdapterTest.java b/tests/tests/widget/src/android/widget/cts/SimpleCursorAdapterTest.java
index 8f32228..08e425a 100644
--- a/tests/tests/widget/src/android/widget/cts/SimpleCursorAdapterTest.java
+++ b/tests/tests/widget/src/android/widget/cts/SimpleCursorAdapterTest.java
@@ -256,10 +256,10 @@
             mSimpleCursorAdapter.setViewImage(view,
                     createTestImage(mContext, SAMPLE_IMAGE_NAME, testimgRawId));
             assertNotNull(view.getDrawable());
-            Bitmap testBitmap = WidgetTestUtils.getUnscaledBitmap(mContext.getResources(),
-                    testimgRawId);
-            WidgetTestUtils.assertEquals(testBitmap,
-                    ((BitmapDrawable) view.getDrawable()).getBitmap());
+            Bitmap actualBitmap = ((BitmapDrawable) view.getDrawable()).getBitmap();
+            Bitmap testBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(mContext.getResources(),
+                    testimgRawId, actualBitmap.getConfig());
+            WidgetTestUtils.assertEquals(testBitmap, actualBitmap);
         } finally {
             destroyTestImage(mContext, SAMPLE_IMAGE_NAME);
         }
diff --git a/tests/tests/widget/src/android/widget/cts/SimpleCursorTreeAdapterTest.java b/tests/tests/widget/src/android/widget/cts/SimpleCursorTreeAdapterTest.java
index 7295e75..8e6f0f3 100644
--- a/tests/tests/widget/src/android/widget/cts/SimpleCursorTreeAdapterTest.java
+++ b/tests/tests/widget/src/android/widget/cts/SimpleCursorTreeAdapterTest.java
@@ -83,24 +83,24 @@
             level = TestLevel.COMPLETE,
             notes = "Test constructors",
             method = "SimpleCursorTreeAdapter",
-            args = {android.content.Context.class, android.database.Cursor.class, int.class, 
-                    int.class, java.lang.String[].class, int[].class, int.class, int.class, 
+            args = {android.content.Context.class, android.database.Cursor.class, int.class,
+                    int.class, java.lang.String[].class, int[].class, int.class, int.class,
                     java.lang.String[].class, int[].class}
         ),
         @TestTargetNew(
             level = TestLevel.COMPLETE,
             notes = "Test constructors",
             method = "SimpleCursorTreeAdapter",
-            args = {android.content.Context.class, android.database.Cursor.class, int.class, 
-                    int.class, java.lang.String[].class, int[].class, int.class, 
+            args = {android.content.Context.class, android.database.Cursor.class, int.class,
+                    int.class, java.lang.String[].class, int[].class, int.class,
                     java.lang.String[].class, int[].class}
         ),
         @TestTargetNew(
             level = TestLevel.COMPLETE,
             notes = "Test constructors",
             method = "SimpleCursorTreeAdapter",
-            args = {android.content.Context.class, android.database.Cursor.class, int.class, 
-                    java.lang.String[].class, int[].class, int.class, java.lang.String[].class, 
+            args = {android.content.Context.class, android.database.Cursor.class, int.class,
+                    java.lang.String[].class, int[].class, int.class, java.lang.String[].class,
                     int[].class}
         )
     })
@@ -122,7 +122,7 @@
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         method = "bindChildView",
-        args = {android.view.View.class, android.content.Context.class, 
+        args = {android.view.View.class, android.content.Context.class,
                 android.database.Cursor.class, boolean.class}
     )
     public void testBindChildView() {
@@ -146,7 +146,7 @@
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         method = "bindGroupView",
-        args = {android.view.View.class, android.content.Context.class, 
+        args = {android.view.View.class, android.content.Context.class,
                 android.database.Cursor.class, boolean.class}
     )
     // The param context and isExpanded is never readed.
@@ -183,7 +183,7 @@
         // color drawable
         ImageView view = new ImageView(mContext);
         assertNull(view.getDrawable());
-        mSimpleCursorTreeAdapter.setViewImage(view, 
+        mSimpleCursorTreeAdapter.setViewImage(view,
                 String.valueOf(com.android.cts.stub.R.drawable.scenery));
         BitmapDrawable d = (BitmapDrawable) mContext.getResources().getDrawable(
                 com.android.cts.stub.R.drawable.scenery);
@@ -213,10 +213,10 @@
             mSimpleCursorTreeAdapter.setViewImage(view,
                     SimpleCursorAdapterTest.createTestImage(mContext, SAMPLE_IMAGE_NAME,
                             com.android.cts.stub.R.raw.testimage));
-            Bitmap test = WidgetTestUtils.getUnscaledBitmap(mContext.getResources(),
-                    com.android.cts.stub.R.raw.testimage);
-            WidgetTestUtils.assertEquals(test,
-                    ((BitmapDrawable) view.getDrawable()).getBitmap());
+            Bitmap actualBitmap = ((BitmapDrawable) view.getDrawable()).getBitmap();
+            Bitmap test = WidgetTestUtils.getUnscaledAndDitheredBitmap(mContext.getResources(),
+                    com.android.cts.stub.R.raw.testimage, actualBitmap.getConfig());
+            WidgetTestUtils.assertEquals(test, actualBitmap);
         } finally {
             SimpleCursorAdapterTest.destroyTestImage(mContext, SAMPLE_IMAGE_NAME);
         }