cts: Add test to enforce max aspect ratios match.

Bug: 16737746

Update CTS with backward compatibility requirements for Camera2 API
to ensure:
 - The largest preview and jpeg outputs available on the device
   have the same aspect ratio as the camera sensor.
 - These sizes are appropriately reported in
   getSupportedPreviewSizes() and getSupportedPictureSizes().

Change-Id: Ic3d28b29cd59fda4c7445c4466bc859ac287344e
diff --git a/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java b/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java
index 2e072d5..f6fc86f 100644
--- a/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java
+++ b/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java
@@ -31,6 +31,7 @@
 import android.hardware.camera2.CaptureRequest;
 import android.hardware.camera2.CaptureResult;
 import android.hardware.camera2.TotalCaptureResult;
+import android.hardware.cts.helpers.CameraUtils;
 import android.util.Size;
 import android.hardware.camera2.params.MeteringRectangle;
 import android.hardware.camera2.params.StreamConfigurationMap;
@@ -522,13 +523,8 @@
     public static class SizeComparator implements Comparator<Size> {
         @Override
         public int compare(Size lhs, Size rhs) {
-            long left = lhs.getWidth() * lhs.getHeight();
-            long right = rhs.getWidth() * rhs.getHeight();
-            if (left == right) {
-                left = lhs.getWidth();
-                right = rhs.getWidth();
-            }
-            return (left < right) ? -1 : (left > right ? 1 : 0);
+            return CameraUtils
+                    .compareSizes(lhs.getWidth(), lhs.getHeight(), rhs.getWidth(), rhs.getHeight());
         }
     }
 
diff --git a/tests/tests/hardware/src/android/hardware/cts/Camera_SizeTest.java b/tests/tests/hardware/src/android/hardware/cts/Camera_SizeTest.java
index 72d4ba3..355675e 100644
--- a/tests/tests/hardware/src/android/hardware/cts/Camera_SizeTest.java
+++ b/tests/tests/hardware/src/android/hardware/cts/Camera_SizeTest.java
@@ -17,14 +17,20 @@
 package android.hardware.cts;
 
 
+import android.cts.util.CtsAndroidTestCase;
 import android.hardware.Camera;
 import android.hardware.Camera.Parameters;
+import android.hardware.cts.helpers.CameraUtils;
 import android.test.suitebuilder.annotation.LargeTest;
 
-import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import java.util.Collections;
+import java.util.List;
 
 @LargeTest
-public class Camera_SizeTest extends TestCase {
+public class Camera_SizeTest extends CtsAndroidTestCase {
 
     private final int HEIGHT1 = 320;
     private final int WIDTH1 = 240;
@@ -33,6 +39,8 @@
     private final int HEIGHT3 = 640;
     private final int WIDTH3 = 480;
 
+    private static final float ASPECT_RATIO_TOLERANCE = 0.05f;
+
     public void testConstructor() {
         if (Camera.getNumberOfCameras() < 1) {
             return;
@@ -48,10 +56,54 @@
         camera.release();
     }
 
+    /**
+     * Check that the largest available preview and jpeg outputs have the same aspect ratio.  This
+     * aspect ratio must be the same as the physical camera sensor, and the FOV for these outputs
+     * must not be cropped.
+     *
+     * This is required for backward compatibility of the Camera2 API.
+     */
+    public void testMaxAspectRatios() {
+        if (Camera.getNumberOfCameras() < 1) {
+            return;
+        }
+
+        Camera camera = Camera.open(0);
+        Parameters parameters = camera.getParameters();
+
+        List<Camera.Size> supportedJpegDimens = parameters.getSupportedPictureSizes();
+        List<Camera.Size> supportedPreviewDimens = parameters.getSupportedPreviewSizes();
+
+        Collections.sort(supportedJpegDimens, new CameraUtils.LegacySizeComparator());
+        Collections.sort(supportedPreviewDimens, new CameraUtils.LegacySizeComparator());
+
+        Camera.Size largestJpegDimen = supportedJpegDimens.get(supportedJpegDimens.size() - 1);
+        Camera.Size largestPreviewDimen =
+                supportedPreviewDimens.get(supportedPreviewDimens.size() - 1);
+
+        float jpegAspect = largestJpegDimen.width / (float) largestJpegDimen.height;
+        float previewAspect = largestPreviewDimen.width / (float) largestPreviewDimen.height;
+
+        assertTrue("Largest preview dimension (w=" + largestPreviewDimen.width + ", h=" +
+                        largestPreviewDimen.height +
+                        ") must have the same aspect ratio as the largest Jpeg dimension (w=" +
+                        largestJpegDimen.width + ", h=" + largestJpegDimen.height + ")",
+                Math.abs(jpegAspect - previewAspect) < ASPECT_RATIO_TOLERANCE);
+
+
+        camera.release();
+    }
+
     private void checkSize(Parameters parameters, int width, int height) {
         parameters.setPictureSize(width, height);
         assertEquals(width, parameters.getPictureSize().width);
         assertEquals(height, parameters.getPictureSize().height);
     }
+
+    private static void addTestToSuite(TestSuite testSuite, String testName) {
+        Camera_SizeTest test = new Camera_SizeTest();
+        test.setName(testName);
+        testSuite.addTest(test);
+    }
 }
 
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/CameraUtils.java b/tests/tests/hardware/src/android/hardware/cts/helpers/CameraUtils.java
new file mode 100644
index 0000000..bba726a
--- /dev/null
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/CameraUtils.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.cts.helpers;
+
+import android.hardware.Camera;
+
+import java.util.Comparator;
+
+/**
+ * Utility class containing helper functions for the Camera CTS tests.
+ */
+public class CameraUtils {
+
+    /**
+     * Shared size comparison method used by size comparators.
+     *
+     * <p>Compares the number of pixels it covers.If two the areas of two sizes are same, compare
+     * the widths.</p>
+     */
+     public static int compareSizes(int widthA, int heightA, int widthB, int heightB) {
+        long left = widthA * (long) heightA;
+        long right = widthB * (long) heightB;
+        if (left == right) {
+            left = widthA;
+            right = widthB;
+        }
+        return (left < right) ? -1 : (left > right ? 1 : 0);
+    }
+
+    /**
+     * Size comparator that compares the number of pixels it covers.
+     *
+     * <p>If two the areas of two sizes are same, compare the widths.</p>
+     */
+    public static class LegacySizeComparator implements Comparator<Camera.Size> {
+        @Override
+        public int compare(Camera.Size lhs, Camera.Size rhs) {
+            return compareSizes(lhs.width, lhs.height, rhs.width, rhs.height);
+        }
+    }
+
+}