Modified ShortcutManagerMultiuserTest.testSecondaryUser to use existing
secondary user if possible and switch to initial user at tearDown

Devices supporting multiuser may run CTS on secondary user and creating
a new secondary user is not necessary for this test. Also the test was
switching to user 0 on tearDown, which may not be the initial user
before running the test.

Bug: 137817896
Bug: 130183499
Test: atest android.content.pm.cts.shortcuthost.ShortcutManagerMultiuserTest#testSecondaryUser
Change-Id: Ie3437657a6ecd0be9b9fb11b5acbd440adf0b934
Merged-In: Ie3437657a6ecd0be9b9fb11b5acbd440adf0b934
diff --git a/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/BaseShortcutManagerHostTest.java b/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/BaseShortcutManagerHostTest.java
index 863f51b..0931792 100644
--- a/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/BaseShortcutManagerHostTest.java
+++ b/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/BaseShortcutManagerHostTest.java
@@ -50,6 +50,7 @@
     protected boolean mIsMultiuserSupported;
     protected boolean mIsManagedUserSupported;
 
+    private int mInitialUserId;
     private ArrayList<Integer> mOriginalUsers;
 
     @Override
@@ -72,6 +73,7 @@
         }
 
         if (mIsMultiuserSupported) {
+            mInitialUserId = getDevice().getCurrentUser();
             mOriginalUsers = new ArrayList<>(getDevice().listUsers());
         }
     }
@@ -183,7 +185,7 @@
         if (!mIsMultiuserSupported) {
             return;
         }
-        getDevice().switchUser(getPrimaryUserId());
+        getDevice().switchUser(mInitialUserId);
         for (int userId : getDevice().listUsers()) {
             if (!mOriginalUsers.contains(userId)) {
                 getDevice().removeUser(userId);
@@ -191,6 +193,18 @@
         }
     }
 
+    protected int getOrCreateSecondaryUser() throws Exception {
+        if (getDevice().isUserSecondary(mInitialUserId)) {
+            return mInitialUserId;
+        }
+        for (int userId : getDevice().listUsers()) {
+            if (getDevice().isUserSecondary(userId)) {
+                return userId;
+            }
+        }
+        return createUser();
+    }
+
     protected int createUser() throws Exception{
         return getDevice().createUser("TestUser_" + System.currentTimeMillis());
     }
diff --git a/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/ShortcutManagerMultiuserTest.java b/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/ShortcutManagerMultiuserTest.java
index 72c6a44..9549eb0 100644
--- a/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/ShortcutManagerMultiuserTest.java
+++ b/hostsidetests/shortcuts/hostside/src/android/content/pm/cts/shortcuthost/ShortcutManagerMultiuserTest.java
@@ -62,7 +62,7 @@
         if (!mIsMultiuserSupported) {
             return;
         }
-        final int secondUserID = createUser();
+        final int secondUserID = getOrCreateSecondaryUser();
 
         getDevice().startUser(secondUserID);
         getDevice().switchUser(secondUserID);