Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c3cf7c2c authored by João Victor Mendes Freire's avatar João Victor Mendes Freire Committed by Android (Google) Code Review
Browse files

Merge "Add test with static wallpaper for switchUser"

parents b04bfc76 d9938b87
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.perftests.multiuser">


    <uses-permission android:name="android.permission.CONTROL_KEYGUARD" />
    <uses-permission android:name="android.permission.DEVICE_POWER" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
@@ -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>
+77 −1
Original line number Diff line number Diff line
@@ -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());