ShortcutManagerMultiuser: wait until user started

When starting a user, wait until the user has actually started before
continuing. This is necessary because "am start-user" is async, so it
may not have finished starting the user before the test continues.

Test: mmm cts/hostsidetests/shortcuts && cts-tradefed run cts-dev -m CtsShortcutHostTestCases -t android.content.pm.cts.shortcuthost.ShortcutManagerMultiuserTest#testManagedUser
Bug: 123038606
Change-Id: I3a4ed3e88de1b9e0e3a10273453d1c251963167a
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 bc33abf..5354b02 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
@@ -214,6 +214,29 @@
         throw new IllegalStateException();
     }
 
+    /** Starts user {@code userId} and waits until it is in state RUNNING_UNLOCKED. */
+    protected void startUserAndWait(int userId) throws Exception {
+        getDevice().startUser(userId);
+
+        final String desiredState = "RUNNING_UNLOCKED";
+        final long USER_STATE_TIMEOUT_MS = 60_0000; // 1 minute
+        final long timeout = System.currentTimeMillis() + USER_STATE_TIMEOUT_MS;
+        final String command = String.format("am get-started-user-state %d", userId);
+        String output = "";
+        while (System.currentTimeMillis() <= timeout) {
+            output = getDevice().executeShellCommand(command);
+            if (output.contains(desiredState)) {
+                return;
+            }
+            try {
+                Thread.sleep(100);
+            } catch (InterruptedException e) {
+                // Do nothing.
+            }
+        }
+        fail("User state of " + userId + " was '" + output + "' rather than " + desiredState);
+    }
+
     /**
      * Variant of {@link #assertContainsRegex(String,String,String)} using a
      * generic message.
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 3c265ba..72c6a44 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
@@ -45,7 +45,7 @@
         runDeviceTestsAsUser(TARGET_PKG, ".ShortcutManagerManagedUserTest",
                 "test01_managedProfileNotStarted", getPrimaryUserId());
 
-        getDevice().startUser(profileId);
+        startUserAndWait(profileId);
 
         runDeviceTestsAsUser(TARGET_PKG, ".ShortcutManagerManagedUserTest",
                 "test02_createShortuctsOnPrimaryUser", getPrimaryUserId());