Merge "Add test with static wallpaper for switchUser"

This commit is contained in:
João Victor Mendes Freire
2023-01-16 20:54:43 +00:00
committed by Android (Google) Code Review
2 changed files with 79 additions and 4 deletions

View File

@@ -15,8 +15,7 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.perftests.multiuser">
package="com.android.perftests.multiuser">
<uses-permission android:name="android.permission.CONTROL_KEYGUARD" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
@@ -27,6 +26,7 @@
<uses-permission android:name="android.permission.REAL_GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application>
<uses-library android:name="android.test.runner" />
@@ -38,5 +38,4 @@
<queries>
<package android:name="perftests.multiuser.apps.dummyapp" />
</queries>
</manifest>

View File

@@ -15,6 +15,9 @@
*/
package android.multiuser;
import static android.app.WallpaperManager.FLAG_LOCK;
import static android.app.WallpaperManager.FLAG_SYSTEM;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
@@ -26,6 +29,7 @@ import android.app.AppGlobals;
import android.app.IActivityManager;
import android.app.IStopUserCallback;
import android.app.WaitResult;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IIntentReceiver;
@@ -35,6 +39,7 @@ import android.content.IntentSender;
import android.content.pm.IPackageInstaller;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.IBinder;
import android.os.IProgressListener;
@@ -60,6 +65,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -115,6 +121,7 @@ public class UserLifecycleTests {
private ActivityManager mAm;
private IActivityManager mIam;
private PackageManager mPm;
private WallpaperManager mWm;
private ArrayList<Integer> mUsersToRemove;
private boolean mHasManagedUserFeature;
private BroadcastWaiter mBroadcastWaiter;
@@ -133,6 +140,7 @@ public class UserLifecycleTests {
mIam = ActivityManager.getService();
mUsersToRemove = new ArrayList<>();
mPm = context.getPackageManager();
mWm = WallpaperManager.getInstance(context);
mHasManagedUserFeature = mPm.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS);
mBroadcastWaiter = new BroadcastWaiter(context, TAG, TIMEOUT_IN_SECOND,
Intent.ACTION_USER_STARTED,
@@ -376,6 +384,32 @@ public class UserLifecycleTests {
removeUser(testUser);
}
/** Tests switching to a previously-started, but no-longer-running, user with wait
* times between iterations and using a static wallpaper */
@Test(timeout = TIMEOUT_MAX_TEST_TIME_MS)
public void switchUser_stopped_staticWallpaper() throws RemoteException {
assumeTrue(mWm.isWallpaperSupported() && mWm.isSetWallpaperAllowed());
final int startUser = ActivityManager.getCurrentUser();
final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ true,
/* useStaticWallpaper */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 {
@@ -419,6 +453,31 @@ public class UserLifecycleTests {
removeUser(testUser);
}
/** Tests switching to an already-created already-running non-owner background user, with wait
* times between iterations and using a default static wallpaper */
@Test(timeout = TIMEOUT_MAX_TEST_TIME_MS)
public void switchUser_running_staticWallpaper() throws RemoteException {
assumeTrue(mWm.isWallpaperSupported() && mWm.isSetWallpaperAllowed());
final int startUser = ActivityManager.getCurrentUser();
final int testUser = initializeNewUserAndSwitchBack(/* stopNewUser */ false,
/* useStaticWallpaper */ true);
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 {
@@ -847,14 +906,20 @@ public class UserLifecycleTests {
waitForLatch("Failed to properly stop user " + userId, latch);
}
private int initializeNewUserAndSwitchBack(boolean stopNewUser) throws RemoteException {
return initializeNewUserAndSwitchBack(stopNewUser, /* useStaticWallpaper */ false);
}
/**
* 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.
* @param useStaticWallpaper whether to switch the wallpaper of the default user to a static.
* @return userId of the newly created user.
*/
private int initializeNewUserAndSwitchBack(boolean stopNewUser) throws RemoteException {
private int initializeNewUserAndSwitchBack(boolean stopNewUser, boolean useStaticWallpaper)
throws RemoteException {
final int origUser = mAm.getCurrentUser();
// First, create and switch to testUser, waiting for its ACTION_USER_UNLOCKED
final int testUser = createUserNoFlags();
@@ -862,6 +927,17 @@ public class UserLifecycleTests {
mAm.switchUser(testUser);
}, Intent.ACTION_USER_UNLOCKED, Intent.ACTION_MEDIA_MOUNTED);
if (useStaticWallpaper) {
assertTrue(mWm.isWallpaperSupported() && mWm.isSetWallpaperAllowed());
try {
Bitmap blank = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
mWm.setBitmap(blank, /* visibleCropHint */ null, /* allowBackup */ true,
/* which */ FLAG_SYSTEM | FLAG_LOCK, testUser);
} catch (IOException exception) {
fail("Unable to set static wallpaper.");
}
}
// Second, switch back to origUser, waiting merely for switchUser() to finish
switchUser(origUser);
attestTrue("Didn't switch back to user, " + origUser, origUser == mAm.getCurrentUser());