Add a CTS for parceling index8 Bitmaps

When writing to a parcel and then reading back we should get the same
exact image. We had a bug where the color table was not being written
to the parcel, resulting in the unparceled Bitmap missing one.
Attempting to use this Bitmap results in crashes/reading uninitialized
memory.

BUG:26527976
Change-Id: Ib2183092639e38cc3e97fa5bbfb921d2adf511e1
diff --git a/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java b/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java
index a93fd27..6e0f277 100644
--- a/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java
+++ b/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java
@@ -27,6 +27,7 @@
 import android.graphics.Bitmap.CompressFormat;
 import android.graphics.Bitmap.Config;
 import android.graphics.BitmapFactory.Options;
+import android.os.Parcel;
 import android.os.ParcelFileDescriptor;
 import android.test.InstrumentationTestCase;
 import android.util.DisplayMetrics;
@@ -571,6 +572,32 @@
         verifyScaled(BitmapFactory.decodeStream(obtainInputStream(), null, scaledOpt));
     }
 
+    // Test that writing an index8 bitmap to a Parcel succeeds.
+    public void testParcel() {
+        // Turn off scaling, which would convert to an 8888 bitmap, which does not expose
+        // the bug.
+        BitmapFactory.Options opts = new BitmapFactory.Options();
+        opts.inScaled = false;
+        Bitmap b = BitmapFactory.decodeResource(mRes, R.drawable.gif_test, opts);
+        assertNotNull(b);
+
+        // index8 has no Java equivalent, so the Config will be null.
+        assertNull(b.getConfig());
+
+        Parcel p = Parcel.obtain();
+        b.writeToParcel(p, 0);
+
+        p.setDataPosition(0);
+        Bitmap b2 = Bitmap.CREATOR.createFromParcel(p);
+        compareBitmaps(b, b2, 0, true, true);
+
+        // When this failed previously, the bitmap was missing a colortable, resulting in a crash
+        // attempting to compress by dereferencing a null pointer. Compress to verify that we do
+        // not crash, but succeed instead.
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        assertTrue(b2.compress(Bitmap.CompressFormat.JPEG, 50, baos));
+    }
+
     public void testConfigs() {
         // The output Config of a BitmapFactory decode depends on the request from the
         // client and the properties of the image to be decoded.