Merge "Enables module to run in secondary users if enterprise features are off." into pie-cts-dev am: dffe3e0fb2
am: d59a944ea9

Change-Id: Iacc2984dfca4a71be45ec042ab1ada956738f617
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
index ef51156..6589b22 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
@@ -150,7 +150,18 @@
         if (mPrimaryUserId != USER_SYSTEM) {
             mFixedUsers.add(USER_SYSTEM);
         }
-        switchUser(mPrimaryUserId);
+
+        if (mHasFeature) {
+            // Switching to primary is only needed when we're testing device admin features.
+            switchUser(mPrimaryUserId);
+        } else {
+            // Otherwise, all the tests can be executed in any of the Android users, so remain in
+            // current user, and don't delete it. This enables testing in secondary users.
+            if (getDevice().getCurrentUser() != mPrimaryUserId) {
+                mFixedUsers.add(getDevice().getCurrentUser());
+            }
+        }
+
         removeOwners();
         removeTestUsers();
         // Unlock keyguard before test
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseLauncherAppsTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseLauncherAppsTest.java
index ed885c5..53db413 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseLauncherAppsTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseLauncherAppsTest.java
@@ -35,10 +35,9 @@
             "com.android.cts.launchertests.support";
     protected static final String LAUNCHER_TESTS_SUPPORT_APK = "CtsLauncherAppsTestsSupport.apk";
 
-    protected void installTestApps() throws Exception {
-        uninstallTestApps();
-        installAppAsUser(LAUNCHER_TESTS_APK, mPrimaryUserId);
-        installAppAsUser(LAUNCHER_TESTS_SUPPORT_APK, mPrimaryUserId);
+    protected void installTestApps(int userId) throws Exception {
+        installAppAsUser(LAUNCHER_TESTS_APK, userId);
+        installAppAsUser(LAUNCHER_TESTS_SUPPORT_APK, userId);
     }
 
     protected void uninstallTestApps() throws Exception {
@@ -47,8 +46,8 @@
         getDevice().uninstallPackage(SIMPLE_APP_PKG);
     }
 
-    protected void startCallbackService() throws Exception {
-        String command = "am startservice --user " + mPrimaryUserId
+    protected void startCallbackService(int userId) throws Exception {
+        String command = "am startservice --user " + userId
                 + " -a " + LAUNCHER_TESTS_SUPPORT_PKG + ".REGISTER_CALLBACK "
                 + LAUNCHER_TESTS_SUPPORT_PKG + "/.LauncherCallbackTestsService";
         CLog.d("Output for command " + command + ": " + getDevice().executeShellCommand(command));
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsMultiUserTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsMultiUserTest.java
index 9205ff7..8ae7184 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsMultiUserTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsMultiUserTest.java
@@ -42,7 +42,8 @@
 
         if (mMultiUserSupported) {
             removeTestUsers();
-            installTestApps();
+            uninstallTestApps();
+            installTestApps(mPrimaryUserId);
             // Create a secondary user.
             mSecondaryUserId = createUser();
             mSecondaryUserSerialNumber = Integer.toString(getUserSerialNumber(mSecondaryUserId));
@@ -75,7 +76,7 @@
         if (!mMultiUserSupported) {
             return;
         }
-        startCallbackService();
+        startCallbackService(mPrimaryUserId);
         installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java
index 1596b3c..7df242d 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java
@@ -53,12 +53,9 @@
             mMainUserSerialNumber = Integer.toString(getUserSerialNumber(mParentUserId));
             startUser(mProfileUserId);
 
-            // Install test APK.
-            installTestApps();
-
-            // Also install on the managed profile too.
-            installAppAsUser(LAUNCHER_TESTS_APK, mProfileUserId);
-            installAppAsUser(LAUNCHER_TESTS_SUPPORT_APK, mProfileUserId);
+            // Install test APK on primary user and the managed profile.
+            installTestApps(mPrimaryUserId);
+            installTestApps(mProfileUserId);
         }
     }
 
@@ -110,7 +107,7 @@
         if (!mHasFeature) {
             return;
         }
-        startCallbackService();
+        startCallbackService(mPrimaryUserId);
         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
@@ -123,7 +120,7 @@
             return;
         }
         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
-        startCallbackService();
+        startCallbackService(mPrimaryUserId);
         getDevice().uninstallPackage(SIMPLE_APP_PKG);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
@@ -136,7 +133,7 @@
             return;
         }
         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
-        startCallbackService();
+        startCallbackService(mPrimaryUserId);
         installAppAsUser(SIMPLE_APP_APK, mProfileUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsSingleUserTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsSingleUserTest.java
index 0481777..5d67a46 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsSingleUserTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsSingleUserTest.java
@@ -29,6 +29,7 @@
 
     private boolean mHasLauncherApps;
     private String mSerialNumber;
+    private int mCurrentUserId;
 
     @Override
     protected void setUp() throws Exception {
@@ -36,8 +37,10 @@
         mHasLauncherApps = getDevice().getApiLevel() >= 21;
 
         if (mHasLauncherApps) {
-            mSerialNumber = Integer.toString(getUserSerialNumber(USER_SYSTEM));
-            installTestApps();
+            mCurrentUserId = getDevice().getCurrentUser();
+            mSerialNumber = Integer.toString(getUserSerialNumber(mCurrentUserId));
+            uninstallTestApps();
+            installTestApps(mCurrentUserId);
         }
     }
 
@@ -53,78 +56,78 @@
         if (!mHasLauncherApps) {
             return;
         }
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS, "testSimpleAppInstalledForUser",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 
     public void testLauncherCallbackPackageAddedMainUser() throws Exception {
         if (!mHasLauncherApps) {
             return;
         }
-        startCallbackService();
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
+        startCallbackService(mCurrentUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
 
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
                 "testPackageAddedCallbackForUser",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 
     public void testLauncherCallbackPackageRemovedMainUser() throws Exception {
         if (!mHasLauncherApps) {
             return;
         }
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
-        startCallbackService();
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
+        startCallbackService(mCurrentUserId);
         getDevice().uninstallPackage(SIMPLE_APP_PKG);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
                 "testPackageRemovedCallbackForUser",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 
     public void testLauncherCallbackPackageChangedMainUser() throws Exception {
         if (!mHasLauncherApps) {
             return;
         }
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
-        startCallbackService();
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
+        startCallbackService(mCurrentUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS,
                 "testPackageChangedCallbackForUser",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 
     public void testLauncherNonExportedAppFails() throws Exception {
         if (!mHasLauncherApps) {
             return;
         }
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS, "testLaunchNonExportActivityFails",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 
     public void testLaunchNonExportActivityFails() throws Exception {
         if (!mHasLauncherApps) {
             return;
         }
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS, "testLaunchNonExportLauncherFails",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 
     public void testLaunchMainActivity() throws Exception {
         if (!mHasLauncherApps) {
             return;
         }
-        installAppAsUser(SIMPLE_APP_APK, mPrimaryUserId);
+        installAppAsUser(SIMPLE_APP_APK, mCurrentUserId);
         runDeviceTestsAsUser(LAUNCHER_TESTS_PKG,
                 LAUNCHER_TESTS_CLASS, "testLaunchMainActivity",
-                mPrimaryUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
+                mCurrentUserId, Collections.singletonMap(PARAM_TEST_USER, mSerialNumber));
     }
 }