From 84ff04d3c285c98d1048f93b975a13210bd7c5d9 Mon Sep 17 00:00:00 2001
From: Yasin Kilicdere <tyk@google.com>
Date: Wed, 26 Apr 2023 12:51:13 +0100
Subject: [PATCH] Start user in background via adb command instead of calling
 the API.

If the user to be started is a profile of a current foreground user,
it should be started via IActivityManager.startProfileWithListener API
instead of IActivityManager.startUserInBackgroundWithListener. This
logic is already implemented in ActivityManagerShellCommand, so rather
than duplicating it, we'll be doing it by running the shell command.

Bug: 279403644
Test: atest UserLifecycleTests
Change-Id: I4b9e6f1e565187e93e13ad94f41d51ee5f7e3e42
---
 .../src/android/multiuser/UserLifecycleTests.java    | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java
index 91f0681c9d7b..e5d4a2087fd4 100644
--- a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java
+++ b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java
@@ -1261,15 +1261,13 @@ public class UserLifecycleTests {
      * <p> This should always be used for profiles since profiles cannot be started in foreground.
      */
     private void startUserInBackgroundAndWaitForUnlock(int userId) {
-        final ProgressWaiter waiter = new ProgressWaiter();
-        boolean success = false;
         try {
-            mIam.startUserInBackgroundWithListener(userId, waiter);
-            success = waiter.waitForFinish(TIMEOUT_IN_SECOND);
-        } catch (RemoteException e) {
-            Log.e(TAG, "startUserInBackgroundAndWaitForUnlock failed", e);
+            attestTrue("Failed to start user " + userId + " in background.",
+                    ShellHelper.runShellCommandWithTimeout("am start-user -w " + userId,
+                            TIMEOUT_IN_SECOND).startsWith("Success:"));
+        } catch (TimeoutException e) {
+            fail("Could not start user " + userId + " in " + TIMEOUT_IN_SECOND + " seconds");
         }
-        attestTrue("Failed to start user " + userId + " in background.", success);
     }
 
     /** Starts the given user in the foreground. */
-- 
GitLab