Fixed SecondaryUsersTest:

- Properly handle handle exceptions thrown while the device is not
  ready after reboot.
- Increased timeout for user switch.
- Added more logging.

Fixes: 242017723
Bug: 183116089
Test: atest android.host.multiuser.SecondaryUsersTest

Change-Id: I00ea794c079ec326830f2a692fea2b8f969597da
(cherry picked from commit 899ae9ab14e17dbb3d8adbebfa0f402924582bb5)
diff --git a/hostsidetests/multiuser/src/android/host/multiuser/SecondaryUsersTest.java b/hostsidetests/multiuser/src/android/host/multiuser/SecondaryUsersTest.java
index 139a35c..7f3a817 100644
--- a/hostsidetests/multiuser/src/android/host/multiuser/SecondaryUsersTest.java
+++ b/hostsidetests/multiuser/src/android/host/multiuser/SecondaryUsersTest.java
@@ -20,6 +20,7 @@
 import android.host.multiuser.BaseMultiUserTest.SupportsMultiUserRule;
 
 import com.android.compatibility.common.util.CddTest;
+import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 
 import org.junit.Rule;
@@ -35,9 +36,11 @@
 public final class SecondaryUsersTest extends BaseMultiUserTest {
 
     // Extra time to give the system to switch into secondary user after boot complete.
-    private static final long SECONDARY_USER_BOOT_COMPLETE_TIMEOUT_MS = 30000;
+    private static final long SECONDARY_USER_BOOT_COMPLETE_TIMEOUT_MS = 100_000;
 
-    private static final long POLL_INTERVAL_MS = 100;
+    private static final long POLL_INTERVAL_MS = 1_000;
+    private static final long WAIT_FOR_DEVICE_READY_INTERVAL_MS = 10_000;
+    private static final long WAIT_FOR_BOOT_COMPLETE_INTERVAL_MINUTES = 2;
 
     @Rule
     public final SupportsMultiUserRule mSupportsMultiUserRule = new SupportsMultiUserRule(this);
@@ -47,26 +50,42 @@
     public void testSwitchToSecondaryUserBeforeBootComplete() throws Exception {
         assumeIsAutomotive();
 
+        CLog.d("Rebooting");
         getDevice().nonBlockingReboot();
+        CLog.d("Waiting " + WAIT_FOR_BOOT_COMPLETE_INTERVAL_MINUTES + " minutes for boot complete");
         getDevice().waitForBootComplete(TimeUnit.MINUTES.toMillis(2));
+        CLog.d("Boot completed; waiting until current user is a secondary user");
 
+        int currentUser = -10000; // UserHandle.USER_NULL;
         boolean isUserSecondary = false;
         long ti = System.currentTimeMillis();
 
-        // TODO(b/138944230): Verify if current user is secondary when the UI is ready for user
+        // TODO(b/208518721): Verify if current user is secondary when the UI is ready for user
         // interaction. A possibility is to check if the CarLauncher is started in the
         // Activity Stack, but this becomes tricky in OEM implementation, where CarLauncher is
         // replaced with another launcher. Launcher can usually identify by
         // android.intent.category.HOME (type=home) and priority = -1000. But there is no clear way
         // to determine this via adb.
-        while (System.currentTimeMillis() - ti < SECONDARY_USER_BOOT_COMPLETE_TIMEOUT_MS) {
-            isUserSecondary = getDevice().isUserSecondary(getDevice().getCurrentUser());
-            if (isUserSecondary) {
-                break;
+        while (!isUserSecondary
+                && System.currentTimeMillis() - ti < SECONDARY_USER_BOOT_COMPLETE_TIMEOUT_MS) {
+            try {
+                currentUser = getDevice().getCurrentUser();
+                isUserSecondary = getDevice().isUserSecondary(currentUser);
+                CLog.d("Current user: %d isSecondary: %b", currentUser, isUserSecondary);
+                if (isUserSecondary) {
+                    CLog.d("Saul Goodman!");
+                    break;
+                }
+                CLog.v("Sleeping for %d ms as user %d is not a secondary user yet",
+                        POLL_INTERVAL_MS, currentUser);
+                Thread.sleep(POLL_INTERVAL_MS);
+            } catch (Exception e) {
+                CLog.d("Device not available yet (%s); sleeping for %d ms", e,
+                        WAIT_FOR_DEVICE_READY_INTERVAL_MS);
+                Thread.sleep(WAIT_FOR_DEVICE_READY_INTERVAL_MS);
             }
-            Thread.sleep(POLL_INTERVAL_MS);
         }
-        assertWithMessage("Must switch to secondary user before boot complete")
+        assertWithMessage("Current user (%s) is a secondary user after boot", currentUser)
                 .that(isUserSecondary).isTrue();
     }
 }