diff options
author | Bookatz <bookatz@google.com> | 2019-02-28 17:29:26 -0800 |
---|---|---|
committer | Bookatz <bookatz@google.com> | 2019-03-29 10:46:09 -0700 |
commit | 52b12ac7a13418e69008b7758f4bb40f137bd0dc (patch) | |
tree | 7ac614a36c2b9490eb450c696a4c408b8ca57313 | |
parent | b0ee8019bd7f55895fb859486d1f9dbbf31b4b0f (diff) | |
download | android_frameworks_base-52b12ac7a13418e69008b7758f4bb40f137bd0dc.tar.gz android_frameworks_base-52b12ac7a13418e69008b7758f4bb40f137bd0dc.tar.bz2 android_frameworks_base-52b12ac7a13418e69008b7758f4bb40f137bd0dc.zip |
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
-rw-r--r-- | apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java index 2fdba0af2c1..7e7b87137ff 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.content.pm.UserInfo; 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 @@ public class UserLifecycleTests { } } + /** 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 @@ public class UserLifecycleTests { 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 @@ public class UserLifecycleTests { 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); + } } |