diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2019-04-03 21:34:44 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-04-03 21:34:44 +0000 |
commit | 12294a48ad38a3fd3cf138e6955b061a434e7719 (patch) | |
tree | 7866705b8ed0a9b489e15b7497ac549f08f0665c | |
parent | 38d786f0828eb8379a63768d63413786d85e7e84 (diff) | |
parent | 52b12ac7a13418e69008b7758f4bb40f137bd0dc (diff) |
Merge "MultiUserPerfTest test switch to pre-existing user"
-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 2fdba0af2c1b..7e7b87137ff8 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); + } } |