From b71ff68fe50e438b8e8dffe9f1ee60a3687d673f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Mendes=20Freire?= Date: Fri, 16 Dec 2022 12:49:20 +0000 Subject: [PATCH] Fork UserLifecycleTests to make tests realistic This commit creates forks of the UserLifecycleTests, more specifically of the userSwitch tests. The main change is that we avoid creating a new user at each iteration (to reduce post user creation device buzyness) and increase the wait period between user switches. Bug: 261621873 Test: atest UserLifecycleTests Change-Id: Iad57848cc313b07275df2927be2feb882a10f2ba --- .../android/multiuser/UserLifecycleTests.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java index aec60f250f6ff..b2bd8d7f5d5db 100644 --- a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java +++ b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java @@ -266,6 +266,27 @@ public class UserLifecycleTests { } } + /** Tests switching to an uninitialized user with wait times between iterations. */ + @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) + public void switchUser_realistic() throws Exception { + while (mRunner.keepRunning()) { + mRunner.pauseTiming(); + final int startUser = ActivityManager.getCurrentUser(); + final int userId = createUserNoFlags(); + waitCoolDownPeriod(); + Log.d(TAG, "Starting timer"); + mRunner.resumeTiming(); + + switchUser(userId); + + mRunner.pauseTiming(); + Log.d(TAG, "Stopping timer"); + switchUserNoCheck(startUser); + removeUser(userId); + mRunner.resumeTimingForNextIteration(); + } + } + /** Tests switching to a previously-started, but no-longer-running, user. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) public void switchUser_stopped() throws RemoteException { @@ -286,6 +307,30 @@ public class UserLifecycleTests { } } + /** Tests switching to a previously-started, but no-longer-running, user with wait + * times between iterations */ + @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) + public void switchUser_stopped_realistic() throws RemoteException { + final int startUser = ActivityManager.getCurrentUser(); + final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ true); + while (mRunner.keepRunning()) { + mRunner.pauseTiming(); + waitCoolDownPeriod(); + Log.d(TAG, "Starting timer"); + mRunner.resumeTiming(); + + switchUser(testUser); + + mRunner.pauseTiming(); + Log.d(TAG, "Stopping timer"); + switchUserNoCheck(startUser); + stopUserAfterWaitingForBroadcastIdle(testUser, true); + attestFalse("Failed to stop user " + testUser, mAm.isUserRunning(testUser)); + mRunner.resumeTimingForNextIteration(); + } + removeUser(testUser); + } + /** Tests switching to an already-created already-running non-owner background user. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) public void switchUser_running() throws RemoteException { @@ -306,6 +351,29 @@ public class UserLifecycleTests { } } + /** Tests switching to an already-created already-running non-owner background user, with wait + * times between iterations */ + @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) + public void switchUser_running_realistic() throws RemoteException { + final int startUser = ActivityManager.getCurrentUser(); + final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ false); + while (mRunner.keepRunning()) { + mRunner.pauseTiming(); + waitCoolDownPeriod(); + Log.d(TAG, "Starting timer"); + mRunner.resumeTiming(); + + switchUser(testUser); + + mRunner.pauseTiming(); + Log.d(TAG, "Stopping timer"); + waitForBroadcastIdle(); + switchUserNoCheck(startUser); + mRunner.resumeTimingForNextIteration(); + } + removeUser(testUser); + } + /** Tests stopping a background user. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) public void stopUser() throws RemoteException { @@ -902,4 +970,18 @@ public class UserLifecycleTests { private void waitForBroadcastIdle() { ShellHelper.runShellCommand("am wait-for-broadcast-idle"); } + + private void sleep(long ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + // Ignore + } + } + + private void waitCoolDownPeriod() { + final int tenSeconds = 1000 * 10; + waitForBroadcastIdle(); + sleep(tenSeconds); + } }