CTS test for notifying user about unsupported display zoom

Bug: 29322470
Change-Id: I3e5a64ae8bdbd66545d8ce9721f96b08147e059f
diff --git a/hostsidetests/services/activitymanager/AndroidTest.xml b/hostsidetests/services/activitymanager/AndroidTest.xml
index 641fa09..3a2f575 100644
--- a/hostsidetests/services/activitymanager/AndroidTest.xml
+++ b/hostsidetests/services/activitymanager/AndroidTest.xml
@@ -17,6 +17,7 @@
     <target_preparer class="com.android.compatibility.common.tradefed.targetprep.ApkInstaller">
         <option name="cleanup-apks" value="true" />
         <option name="test-file-name" value="CtsDeviceServicesTestApp.apk" />
+        <option name="test-file-name" value="CtsDeviceDisplaySizeApp.apk" />
     </target_preparer>
     <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
         <option name="jar" value="CtsServicesHostTestCases.jar" />
diff --git a/hostsidetests/services/activitymanager/appDisplaySize/Android.mk b/hostsidetests/services/activitymanager/appDisplaySize/Android.mk
new file mode 100644
index 0000000..7f7abd2
--- /dev/null
+++ b/hostsidetests/services/activitymanager/appDisplaySize/Android.mk
@@ -0,0 +1,31 @@
+# Copyright (C) 2016 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# don't include this package in any target
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_SDK_VERSION := test_current
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+LOCAL_PACKAGE_NAME := CtsDeviceDisplaySizeApp
+
+include $(BUILD_CTS_SUPPORT_PACKAGE)
diff --git a/hostsidetests/services/activitymanager/appDisplaySize/AndroidManifest.xml b/hostsidetests/services/activitymanager/appDisplaySize/AndroidManifest.xml
new file mode 100644
index 0000000..49649b6
--- /dev/null
+++ b/hostsidetests/services/activitymanager/appDisplaySize/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2016 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="android.displaysize.app">
+
+    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
+
+    <!-- Set a ridiculously high value for required smallest width. Hopefully
+         we never have to run CTS on Times Square display. -->
+    <supports-screens android:requiresSmallestWidthDp="100000" />
+
+    <application>
+        <activity android:name=".SmallestWidthActivity" />
+    </application>
+
+</manifest>
diff --git a/hostsidetests/services/activitymanager/appDisplaySize/src/android/displaysize/app/SmallestWidthActivity.java b/hostsidetests/services/activitymanager/appDisplaySize/src/android/displaysize/app/SmallestWidthActivity.java
new file mode 100644
index 0000000..c29c0c5
--- /dev/null
+++ b/hostsidetests/services/activitymanager/appDisplaySize/src/android/displaysize/app/SmallestWidthActivity.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016 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.displaysize.app;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+public class SmallestWidthActivity extends Activity {
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        // Nothing to see here.
+    }
+}
diff --git a/hostsidetests/services/activitymanager/src/android/server/cts/DisplaySizeTest.java b/hostsidetests/services/activitymanager/src/android/server/cts/DisplaySizeTest.java
new file mode 100644
index 0000000..a6a6b5c
--- /dev/null
+++ b/hostsidetests/services/activitymanager/src/android/server/cts/DisplaySizeTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2016 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.server.cts;
+
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.testtype.DeviceTestCase;
+
+/**
+ * Ensure that compatibility dialog is shown when launching an application with
+ * an unsupported smallest width.
+ */
+public class DisplaySizeTest extends DeviceTestCase {
+    private static final String DENSITY_PROP_DEVICE = "ro.sf.lcd_density";
+    private static final String DENSITY_PROP_EMULATOR = "qemu.sf.lcd_density";
+
+    private static final String AM_START_COMMAND = "am start -n %s/%s.%s";
+    private static final String AM_FORCE_STOP = "am force-stop %s";
+
+    private static final int WINDOW_TIMEOUT_MILLIS = 1000;
+
+    private ITestDevice mDevice;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mDevice = getDevice();
+
+        // Set device to 0.85 zoom. It doesn't matter that we're zooming out
+        // since the feature verifies that we're in a non-default density.
+        final int stableDensity = getStableDensity();
+        final int targetDensity = (int) (stableDensity * 0.85);
+        mDevice.executeShellCommand("wm density " + targetDensity);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+
+        try {
+            // Restore default density.
+            mDevice.executeShellCommand("wm density reset");
+
+            // Ensure app process is stopped.
+            forceStopPackage("android.displaysize.app");
+        } catch (DeviceNotAvailableException e) {
+            // Do nothing.
+        }
+    }
+
+    public void testCompatibilityDialog() throws Exception {
+        startActivity("android.displaysize.app", "SmallestWidthActivity");
+        verifyWindowDisplayed("UnsupportedDisplaySizeDialog", WINDOW_TIMEOUT_MILLIS);
+    }
+
+    private int getStableDensity() {
+        try {
+            final String densityProp;
+            if (mDevice.getSerialNumber().startsWith("emulator-")) {
+                densityProp = DENSITY_PROP_EMULATOR;
+            } else {
+                densityProp = DENSITY_PROP_DEVICE;
+            }
+
+            return Integer.parseInt(mDevice.getProperty(densityProp));
+        } catch (DeviceNotAvailableException e) {
+            return 0;
+        }
+    }
+
+    private void forceStopPackage(String packageName) throws DeviceNotAvailableException {
+        final String forceStopCmd = String.format(AM_FORCE_STOP, packageName);
+        mDevice.executeShellCommand(forceStopCmd);
+    }
+
+    private void startActivity(String packageName, String activityName)
+            throws DeviceNotAvailableException {
+        final String startCmd = String.format(
+                AM_START_COMMAND, packageName, packageName, activityName);
+        mDevice.executeShellCommand(startCmd);
+    }
+
+    private void verifyWindowDisplayed(String windowName, long timeoutMillis)
+            throws DeviceNotAvailableException {
+        boolean success = false;
+
+        // Verify that compatibility dialog is shown within 1000ms.
+        final long timeoutTimeMillis = System.currentTimeMillis() + timeoutMillis;
+        while (!success && System.currentTimeMillis() < timeoutTimeMillis) {
+            final String output = mDevice.executeShellCommand("dumpsys window");
+            success = output.contains(windowName);
+        }
+
+        assertTrue(windowName + " was not displayed", success);
+    }
+}