Extend screen capture disabled test

Add another test that ensures that disabling screen capture on a
profile owner does not block screen shots for primary user activities.

Bug: 17482450
Change-Id: Id04093d8afeff4b10cf06bfce0686158cc352f73
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ScreenCaptureDisabledTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ScreenCaptureDisabledTest.java
index 31423c9..e2deaa4 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ScreenCaptureDisabledTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ScreenCaptureDisabledTest.java
@@ -44,7 +44,6 @@
 
     protected void tearDown() throws Exception {
         mContext.unregisterReceiver(mReceiver);
-        mDevicePolicyManager.setScreenCaptureDisabled(ADMIN_RECEIVER_COMPONENT, false);
         super.tearDown();
     }
 
@@ -64,6 +63,13 @@
         assertNull(getInstrumentation().getUiAutomation().takeScreenshot());
     }
 
+    public void testScreenCapturePossible() throws Exception {
+        assertNotNull(getInstrumentation().getUiAutomation().takeScreenshot());
+    }
+
+    // We need to launch an activity before trying to take a screen shot, because screenshots are
+    // only blocked on a per-user basis in the profile owner case depending on the owner of the
+    // foreground activity.
     private void startTestActivity() throws Exception {
         Intent launchIntent = new Intent();
         launchIntent.setComponent(new ComponentName(PACKAGE_NAME,
@@ -71,6 +77,7 @@
         launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         mContext.startActivity(launchIntent);
         assertTrue(mReceiver.waitForBroadcast());
+        Thread.sleep(1000);
     }
 
     private class ScreenCaptureBroadcastReceiver extends BroadcastReceiver {
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
index 02f42ba..fed3c6a 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
@@ -40,12 +40,15 @@
     private static final String SIMPLE_PRE_M_APP_PKG = "com.android.cts.launcherapps.simplepremapp";
     private static final String SIMPLE_PRE_M_APP_APK = "CtsSimplePreMApp.apk";
 
+    protected static final int USER_OWNER = 0;
+
     // ID of the user all tests are run as. For device owner this will be 0, for profile owner it
     // is the user id of the created profile.
     protected int mUserId;
 
     protected void tearDown() throws Exception {
         if (mHasFeature) {
+            getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
             getDevice().uninstallPackage(PERMISSIONS_APP_PKG);
             getDevice().uninstallPackage(SIMPLE_PRE_M_APP_PKG);
         }
@@ -142,14 +145,22 @@
         if (!mHasFeature) {
             return;
         }
-        executeDeviceTestClass(".ScreenCaptureDisabledTest");
+        // We need to ensure that the policy is deactivated for the device owner case, so making
+        // sure the second test is run even if the first one fails
+        try {
+            executeDeviceTestMethod(".ScreenCaptureDisabledTest",
+                    "testSetScreenCaptureDisabled_true");
+        } finally {
+            executeDeviceTestMethod(".ScreenCaptureDisabledTest",
+                    "testSetScreenCaptureDisabled_false");
+        }
     }
 
-    private void executeDeviceTestClass(String className) throws Exception {
+    protected void executeDeviceTestClass(String className) throws Exception {
         assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, mUserId));
     }
 
-    private void executeDeviceTestMethod(String className, String testName) throws Exception {
+    protected void executeDeviceTestMethod(String className, String testName) throws Exception {
         assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, testName, mUserId));
     }
 }
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java
index 97851d4..81ed31a 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java
@@ -36,7 +36,7 @@
         super.setUp();
 
         if (mHasFeature) {
-            mUserId = 0;
+            mUserId = USER_OWNER;
 
             installApp(DEVICE_ADMIN_APK);
             setDeviceOwner(DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS);
@@ -48,7 +48,6 @@
         if (mHasFeature) {
             assertTrue("Failed to remove device owner.",
                     runDeviceTests(DEVICE_ADMIN_PKG, CLEAR_DEVICE_OWNER_TEST_CLASS));
-            getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
         }
         super.tearDown();
     }
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java
index 7f47227..0dc2496 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java
@@ -39,7 +39,7 @@
             removeTestUsers();
             mUserId = createManagedProfile();
 
-            installApp(DEVICE_ADMIN_APK);
+            installAppAsUser(DEVICE_ADMIN_APK, mUserId);
             setProfileOwner(DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mUserId);
             startUser(mUserId);
         }
@@ -49,10 +49,26 @@
     protected void tearDown() throws Exception {
         if (mHasFeature) {
             removeUser(mUserId);
-            getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
         }
         super.tearDown();
     }
 
-    // All tests for this class are defined in DeviceAndProfileOwnerTest
+    // Most tests for this class are defined in DeviceAndProfileOwnerTest
+
+    /**
+     * Verify that screenshots are still possible for activities in the primary user when the policy
+     * is set on the profile owner.
+     */
+    public void testScreenCaptureDisabled_allowedPrimaryUser() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        executeDeviceTestMethod(".ScreenCaptureDisabledTest", "testSetScreenCaptureDisabled_true");
+        // start the ScreenCaptureDisabledActivity in the primary user
+        installAppAsUser(DEVICE_ADMIN_APK, USER_OWNER);
+        String command = "am start -W --user 0 " + DEVICE_ADMIN_PKG + "/"
+                + DEVICE_ADMIN_PKG + ".ScreenCaptureDisabledActivity";
+        getDevice().executeShellCommand(command);
+        executeDeviceTestMethod(".ScreenCaptureDisabledTest", "testScreenCapturePossible");
+    }
 }