MultiUserPerfTest test switch to pre-existing user
Creates two new MultiUserPerfTests, timing how long it takes to switch
to a pre-existing user, one for when the user is already-started, and
the other for when it is stopped.
Bug: 126745587
Test: make MultiUserPerfTests && adb install -r ${ANDROID_PRODUCT_OUT}/data/app/MultiUserPerfTests/MultiUserPerfTests.apk && adb shell am instrument -e class android.multiuser.UserLifecycleTests -w com.android.perftests.multiuser/androidx.test.runner.AndroidJUnitRunner
Change-Id: I734f4971ad992e906a0090f4ef328255ab2a0349
diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java
index 2fdba0a..7e7b8713 100644
--- a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java
+++ b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java
@@ -27,6 +27,7 @@
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
+import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
@@ -130,6 +131,47 @@
}
}
+ /** Tests switching to an already-created, but no-longer-running, user. */
+ @Test
+ public void switchUser_stopped() throws Exception {
+ while (mRunner.keepRunning()) {
+ mRunner.pauseTiming();
+ final int startUser = mAm.getCurrentUser();
+ final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ true);
+ final CountDownLatch latch = new CountDownLatch(1);
+ registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch, testUser);
+ mRunner.resumeTiming();
+
+ mAm.switchUser(testUser);
+ boolean success = latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS);
+
+ mRunner.pauseTiming();
+ attestTrue("Failed to achieve 2nd ACTION_USER_UNLOCKED for user " + testUser, success);
+ switchUser(startUser);
+ removeUser(testUser);
+ mRunner.resumeTiming();
+ }
+ }
+
+ /** Tests switching to an already-created already-running non-owner user. */
+ @Test
+ public void switchUser_running() throws Exception {
+ while (mRunner.keepRunning()) {
+ mRunner.pauseTiming();
+ final int startUser = mAm.getCurrentUser();
+ final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ false);
+ mRunner.resumeTiming();
+
+ switchUser(testUser);
+
+ mRunner.pauseTiming();
+ attestTrue("Failed to switch to user " + testUser, mAm.isUserRunning(testUser));
+ switchUser(startUser);
+ removeUser(testUser);
+ mRunner.resumeTiming();
+ }
+ }
+
@Test
public void stopUser() throws Exception {
while (mRunner.keepRunning()) {
@@ -262,6 +304,35 @@
latch.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS);
}
+ /**
+ * Creates a user and waits for its ACTION_USER_UNLOCKED.
+ * Then switches to back to the original user and waits for its switchUser() to finish.
+ *
+ * @param stopNewUser whether to stop the new user after switching to otherUser.
+ * @return userId of the newly created user.
+ */
+ private int initializeNewUserAndSwitchBack(boolean stopNewUser) throws Exception {
+ final int origUser = mAm.getCurrentUser();
+ // First, create and switch to testUser, waiting for its ACTION_USER_UNLOCKED
+ final int testUser = mUm.createUser("TestUser", 0).id;
+ final CountDownLatch latch1 = new CountDownLatch(1);
+ registerBroadcastReceiver(Intent.ACTION_USER_UNLOCKED, latch1, testUser);
+ mAm.switchUser(testUser);
+ attestTrue("Failed to achieve initial ACTION_USER_UNLOCKED for user " + testUser,
+ latch1.await(TIMEOUT_IN_SECOND, TimeUnit.SECONDS));
+
+ // Second, switch back to origUser, waiting merely for switchUser() to finish
+ switchUser(origUser);
+ attestTrue("Didn't switch back to user, " + origUser, origUser == mAm.getCurrentUser());
+
+ if (stopNewUser) {
+ stopUser(testUser, true);
+ attestFalse("Failed to stop user " + testUser, mAm.isUserRunning(testUser));
+ }
+
+ return testUser;
+ }
+
private void registerUserSwitchObserver(final CountDownLatch switchLatch,
final CountDownLatch bootCompleteLatch, final int userId) throws Exception {
ActivityManager.getService().registerUserSwitchObserver(
@@ -313,4 +384,14 @@
mUsersToRemove.add(userId);
}
}
+
+ private void attestTrue(String message, boolean attestion) {
+ if (!attestion) {
+ Log.w(TAG, message);
+ }
+ }
+
+ private void attestFalse(String message, boolean attestion) {
+ attestTrue(message, !attestion);
+ }
}