Add more comprehensive unit tests for WebP Image.

Added test for end-to-end WebP encoding and decoding, by encoding a PNG
image to WebP and decode WebP image back to Bitmap and compare against
the PNG BitMap.
Note for integrator: The file 'res/drawable/webp_test.webp' is already
present in the internal Android-git repo, but not present in AOSP (as
AOSP is somehow not in sync with internal Android-git repo w.r.t
'platform/cts.git' project). I had to add this file to make this test
complete w.r.t it's dependencies.

Change-Id: I68cfa71290797aee1d5645be0deec0a3bc1fe727
diff --git a/tests/res/drawable/webp_test.webp b/tests/res/drawable/webp_test.webp
new file mode 100644
index 0000000..7b1009f
--- /dev/null
+++ b/tests/res/drawable/webp_test.webp
Binary files differ
diff --git a/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java b/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java
index 4c6259c..eb73f39 100644
--- a/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java
+++ b/tests/tests/graphics/src/android/graphics/cts/BitmapFactoryTest.java
@@ -25,18 +25,23 @@
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.Color;
 import android.graphics.Rect;
+import android.graphics.Bitmap.CompressFormat;
+import android.graphics.Bitmap.Config;
 import android.graphics.BitmapFactory.Options;
 import android.os.ParcelFileDescriptor;
 import android.test.InstrumentationTestCase;
 import android.util.DisplayMetrics;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 
 @TestTargetClass(BitmapFactory.class)
 public class BitmapFactoryTest extends InstrumentationTestCase {
@@ -51,6 +56,20 @@
     private int mDefaultDensity;
     private int mTargetDensity;
 
+    // The test images, including baseline JPEG, a PNG, a GIF, a BMP AND a WEBP.
+    private static int[] RES_IDS = new int[] {
+            R.drawable.baseline_jpeg, R.drawable.png_test, R.drawable.gif_test,
+            R.drawable.bmp_test, R.drawable.webp_test
+    };
+    private static String[] NAMES_TEMP_FILES = new String[] {
+        "baseline_temp.jpg", "png_temp.png", "gif_temp.gif",
+        "bmp_temp.bmp", "webp_temp.webp"
+    };
+
+    // The width and height of the above image.
+    private static int WIDTHS[] = new int[] { 1280, 640, 320, 320, 640 };
+    private static int HEIGHTS[] = new int[] { 960, 480, 240, 240, 480 };
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -169,6 +188,53 @@
 
     @TestTargetNew(
         level = TestLevel.COMPLETE,
+        method = "decodeStream",
+        args = {java.io.InputStream.class}
+    )
+    public void testDecodeStream3() throws IOException {
+        for (int i = 0; i < RES_IDS.length; ++i) {
+            InputStream is = obtainInputStream(RES_IDS[i]);
+            Bitmap b = BitmapFactory.decodeStream(is);
+            assertNotNull(b);
+            // Test the bitmap size
+            assertEquals(HEIGHTS[i], b.getHeight());
+            assertEquals(WIDTHS[i], b.getWidth());
+        }
+    }
+
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "decodeStream",
+        args = {java.io.InputStream.class}
+    )
+    public void testDecodeStream4() throws IOException {
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inPreferredConfig = Config.ARGB_8888;
+
+        // Decode the PNG & WebP test images. The WebP test image has been encoded from PNG test
+        // image and should have same similar (within some error-tolerance) Bitmap data.
+        InputStream iStreamPng = obtainInputStream(R.drawable.png_test);
+        Bitmap bPng = BitmapFactory.decodeStream(iStreamPng, null, options);
+        assertNotNull(bPng);
+        assertEquals(bPng.getConfig(), Config.ARGB_8888);
+
+        InputStream iStreamWebp = obtainInputStream(R.drawable.webp_test);
+        Bitmap bWebp = BitmapFactory.decodeStream(iStreamWebp, null, options);
+        assertNotNull(bWebp);
+        compareBitmaps(bPng, bWebp, 16, true);
+
+        // Compress the PNG image to WebP format (Quality=90) and decode it back.
+        // This will test end-to-end WebP encoding and decoding.
+        ByteArrayOutputStream oStreamWebp = new ByteArrayOutputStream();
+        assertTrue(bPng.compress(CompressFormat.WEBP, 90, oStreamWebp));
+        iStreamWebp = new ByteArrayInputStream(oStreamWebp.toByteArray());
+        bWebp = BitmapFactory.decodeStream(iStreamWebp, null, options);
+        assertNotNull(bWebp);
+        compareBitmaps(bPng, bWebp, 16, true);
+    }
+
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
         method = "decodeFileDescriptor",
         args = {java.io.FileDescriptor.class, android.graphics.Rect.class,
                 android.graphics.BitmapFactory.Options.class}
@@ -240,6 +306,10 @@
         return mRes.openRawResource(R.drawable.start);
     }
 
+    private InputStream obtainInputStream(int resId) {
+        return mRes.openRawResource(resId);
+    }
+
     private FileDescriptor obtainDescriptor(String path) throws IOException {
       File file = new File(path);
       return(ParcelFileDescriptor.open(file,
@@ -266,4 +336,49 @@
         fOutput.close();
         return (file.getPath());
     }
+
+    // Compare expected to actual to see if their diff is less then mseMargin.
+    // lessThanMargin is to indicate whether we expect the diff to be
+    // "less than" or "no less than".
+    private void compareBitmaps(Bitmap expected, Bitmap actual,
+            int mseMargin, boolean lessThanMargin) {
+        assertEquals("mismatching widths", expected.getWidth(),
+                actual.getWidth());
+        assertEquals("mismatching heights", expected.getHeight(),
+                actual.getHeight());
+        assertEquals("mismatching configs", expected.getConfig(),
+                actual.getConfig());
+
+        double mse = 0;
+        int width = expected.getWidth();
+        int height = expected.getHeight();
+        int[] expectedColors = new int [width * height];
+        int[] actualColors = new int [width * height];
+
+        expected.getPixels(expectedColors, 0, width, 0, 0, width, height);
+        actual.getPixels(actualColors, 0, width, 0, 0, width, height);
+
+        for (int row = 0; row < height; ++row) {
+            for (int col = 0; col < width; ++col) {
+                int idx = row * width + col;
+                mse += distance(expectedColors[idx], actualColors[idx]);
+            }
+        }
+        mse /= width * height;
+
+        if (lessThanMargin) {
+            assertTrue("MSE too large for normal case: " + mse,
+                    mse <= mseMargin);
+        } else {
+            assertFalse("MSE too small for abnormal case: " + mse,
+                    mse <= mseMargin);
+        }
+    }
+
+    private double distance(int exp, int actual) {
+        int r = Color.red(actual) - Color.red(exp);
+        int g = Color.green(actual) - Color.green(exp);
+        int b = Color.blue(actual) - Color.blue(exp);
+        return r * r + g * g + b * b;
+    }
 }