Add test for ImageDecoder scale + crop

Bug: 141735646
Test: This

ImageDecoder had a bug where combining setTargetSize and setCrop did not
work properly. Test scaling and cropping to the bottom right. This
results in a translate in ImageDecoder's internal code, so this verifies
that it is done correctly.

Change-Id: I9ab9115ee4cbc74549f7b914d99b667c700199d5
diff --git a/tests/tests/graphics/src/android/graphics/cts/ImageDecoderTest.java b/tests/tests/graphics/src/android/graphics/cts/ImageDecoderTest.java
index 4591268..05dd8dd 100644
--- a/tests/tests/graphics/src/android/graphics/cts/ImageDecoderTest.java
+++ b/tests/tests/graphics/src/android/graphics/cts/ImageDecoderTest.java
@@ -1388,6 +1388,73 @@
         }
     }
 
+    @Test
+    public void testScaleAndCrop() {
+        class CropListener implements ImageDecoder.OnHeaderDecodedListener {
+            public boolean doCrop = true;
+            public Rect outScaledRect = null;
+            public Rect outCropRect = null;
+
+            @Override
+            public void onHeaderDecoded(ImageDecoder decoder, ImageDecoder.ImageInfo info,
+                                        ImageDecoder.Source src) {
+                // Use software for pixel comparison.
+                decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE);
+
+                // Scale to a size that is not directly supported by sampling.
+                Size originalSize = info.getSize();
+                int scaledWidth = originalSize.getWidth() * 2 / 3;
+                int scaledHeight = originalSize.getHeight() * 2 / 3;
+                decoder.setTargetSize(scaledWidth, scaledHeight);
+
+                outScaledRect = new Rect(0, 0, scaledWidth, scaledHeight);
+
+                if (doCrop) {
+                    outCropRect = new Rect(scaledWidth / 2, scaledHeight / 2,
+                            scaledWidth, scaledHeight);
+                    decoder.setCrop(outCropRect);
+                }
+            }
+        }
+        CropListener l = new CropListener();
+        ImageDecoder.Source src = mCreators[0].apply(R.drawable.png_test);
+
+        // Scale and crop in a single step.
+        Bitmap oneStepBm = null;
+        try {
+            oneStepBm = ImageDecoder.decodeBitmap(src, l);
+        } catch (IOException e) {
+            fail("Failed with exception " + e);
+        }
+        assertNotNull(oneStepBm);
+        assertNotNull(l.outCropRect);
+        assertEquals(l.outCropRect.width(), oneStepBm.getWidth());
+        assertEquals(l.outCropRect.height(), oneStepBm.getHeight());
+        Rect cropRect = new Rect(l.outCropRect);
+
+        assertNotNull(l.outScaledRect);
+        Rect scaledRect = new Rect(l.outScaledRect);
+
+        // Now just scale with ImageDecoder, and crop afterwards.
+        l.doCrop = false;
+        Bitmap twoStepBm = null;
+        try {
+            twoStepBm = ImageDecoder.decodeBitmap(src, l);
+        } catch (IOException e) {
+            fail("Failed with exception " + e);
+        }
+        assertNotNull(twoStepBm);
+        assertEquals(scaledRect.width(), twoStepBm.getWidth());
+        assertEquals(scaledRect.height(), twoStepBm.getHeight());
+
+        Bitmap cropped = Bitmap.createBitmap(twoStepBm, cropRect.left, cropRect.top,
+                cropRect.width(), cropRect.height());
+        assertNotNull(cropped);
+
+        // The two should look the same.
+        assertTrue(BitmapUtils.compareBitmaps(cropped, oneStepBm, .99));
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void testResizeZeroX() {
         ImageDecoder.Source src = mCreators[0].apply(R.drawable.png_test);