Test Aspect Ratio in Different Orientations

Bug 5786617

Check that one of the aspect ratios without the system bar
is within the CDD range.

Change-Id: I32b3d7e47b35f180d8413baa7b930ff40d915356
diff --git a/tests/tests/dpi/AndroidManifest.xml b/tests/tests/dpi/AndroidManifest.xml
index 562579a..6e141ac 100644
--- a/tests/tests/dpi/AndroidManifest.xml
+++ b/tests/tests/dpi/AndroidManifest.xml
@@ -21,7 +21,7 @@
     <application>
         <uses-library android:name="android.test.runner" />
 
-        <activity android:name="android.dpi.cts.ConfigurationScreenLayoutActivity"
+        <activity android:name="android.dpi.cts.OrientationActivity"
                 android:configChanges="orientation" />
     </application>
 
diff --git a/tests/tests/dpi/src/android/dpi/cts/AspectRatioTest.java b/tests/tests/dpi/src/android/dpi/cts/AspectRatioTest.java
new file mode 100644
index 0000000..74a9f22
--- /dev/null
+++ b/tests/tests/dpi/src/android/dpi/cts/AspectRatioTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2011 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.dpi.cts;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.test.ActivityInstrumentationTestCase2;
+import android.util.DisplayMetrics;
+import android.view.Display;
+import android.view.WindowManager;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AspectRatioTest extends ActivityInstrumentationTestCase2<OrientationActivity> {
+
+    private static final int[] ORIENTATIONS = new int[] {
+        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
+        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
+    };
+
+    public AspectRatioTest() {
+        super(OrientationActivity.class);
+    }
+
+    /**
+     * Get all the aspect rations in different orientations. They could be
+     * different due to the system bar being different sizes. Test that
+     * one of the aspect ratios is within the range.
+     */
+    public void testAspectRatio() throws Exception {
+        List<Double> aspectRatios = getAllAspectRatios();
+        for (double aspectRatio : aspectRatios) {
+            if (aspectRatio >= 1.333 && aspectRatio <= 1.86) {
+                return;
+            }
+        }
+        fail("Aspect ratios were not between 1.333 and 1.86: " + aspectRatios);
+    }
+
+    private List<Double> getAllAspectRatios() throws Exception {
+        List<Double> aspectRatios = new ArrayList<Double>();
+        for (int i = 0; i < ORIENTATIONS.length; i++) {
+            Activity activity = startOrientationActivity(ORIENTATIONS[i]);
+            aspectRatios.add(getAspectRatio(activity));
+            tearDown();
+        }
+        return aspectRatios;
+    }
+
+    private double getAspectRatio(Context context) {
+        WindowManager windowManager =
+                (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
+        Display display = windowManager.getDefaultDisplay();
+        DisplayMetrics metrics = new DisplayMetrics();
+        display.getMetrics(metrics);
+
+        int max = Math.max(metrics.widthPixels, metrics.heightPixels);
+        int min = Math.min(metrics.widthPixels, metrics.heightPixels);
+        return (double) max / min;
+    }
+
+    private Activity startOrientationActivity(int orientation) {
+        Intent intent = new Intent();
+        intent.putExtra(OrientationActivity.EXTRA_ORIENTATION, orientation);
+        setActivityIntent(intent);
+        return getActivity();
+    }
+}
diff --git a/tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutTest.java b/tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutTest.java
index 9fce9f1..d4c3611 100644
--- a/tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutTest.java
+++ b/tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutTest.java
@@ -27,7 +27,7 @@
 import android.view.WindowManager;
 
 public class ConfigurationScreenLayoutTest
-        extends ActivityInstrumentationTestCase2<ConfigurationScreenLayoutActivity> {
+        extends ActivityInstrumentationTestCase2<OrientationActivity> {
 
     private static final int[] ORIENTATIONS = new int[] {
             ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
@@ -37,7 +37,7 @@
     };
 
     public ConfigurationScreenLayoutTest() {
-        super(ConfigurationScreenLayoutActivity.class);
+        super(OrientationActivity.class);
     }
 
     public void testScreenLayout() throws Exception {
@@ -82,7 +82,7 @@
 
     private Activity startOrientationActivity(int orientation) {
         Intent intent = new Intent();
-        intent.putExtra(ConfigurationScreenLayoutActivity.EXTRA_ORIENTATION, orientation);
+        intent.putExtra(OrientationActivity.EXTRA_ORIENTATION, orientation);
         setActivityIntent(intent);
         return getActivity();
     }
diff --git a/tests/tests/dpi/src/android/dpi/cts/ConfigurationTest.java b/tests/tests/dpi/src/android/dpi/cts/ConfigurationTest.java
index df254d2..937d8f8 100644
--- a/tests/tests/dpi/src/android/dpi/cts/ConfigurationTest.java
+++ b/tests/tests/dpi/src/android/dpi/cts/ConfigurationTest.java
@@ -46,12 +46,6 @@
         double density = 160.0d * metrics.density;
         assertTrue("Screen density must be at least 100 dpi: " + density, density >= 100.0d);
 
-        int max = Math.max(metrics.widthPixels, metrics.heightPixels);
-        int min = Math.min(metrics.widthPixels, metrics.heightPixels);
-        double aspectRatio = (double) max / min;
-        assertTrue("Aspect ratio must be between 1.333 and 1.86. It was " + aspectRatio,
-                aspectRatio >= 1.333 && aspectRatio <= 1.86);
-
         Set<Integer> allowedDensities = new HashSet<Integer>();
         allowedDensities.add(DisplayMetrics.DENSITY_LOW);
         allowedDensities.add(DisplayMetrics.DENSITY_MEDIUM);
diff --git a/tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutActivity.java b/tests/tests/dpi/src/android/dpi/cts/OrientationActivity.java
similarity index 94%
rename from tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutActivity.java
rename to tests/tests/dpi/src/android/dpi/cts/OrientationActivity.java
index d4d268d..3bd76b6 100644
--- a/tests/tests/dpi/src/android/dpi/cts/ConfigurationScreenLayoutActivity.java
+++ b/tests/tests/dpi/src/android/dpi/cts/OrientationActivity.java
@@ -22,7 +22,7 @@
 import android.view.WindowManager;
 
 /** {@link Activity} that calls {@link #setRequestedOrientation(int)} with the extra value. */
-public class ConfigurationScreenLayoutActivity extends Activity {
+public class OrientationActivity extends Activity {
 
     static final String EXTRA_ORIENTATION = "orientation";