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

Commit 80f0b901 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Run UserLifecycleStressTest.stopManagedProfileStressTest on main user." into main

parents de9410e5 44dd5c22
Loading
Loading
Loading
Loading
+17 −8
Original line number Original line Diff line number Diff line
@@ -16,17 +16,19 @@
package com.android.server.pm;
package com.android.server.pm;


import static android.os.UserHandle.USER_NULL;
import static android.os.UserHandle.USER_NULL;
import static android.os.UserHandle.USER_SYSTEM;


import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.Truth.assertWithMessage;


import static org.junit.Assume.assumeFalse;

import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.IStopUserCallback;
import android.app.IStopUserCallback;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.UserManager;
import android.platform.test.annotations.Postsubmit;
import android.platform.test.annotations.Postsubmit;
import android.provider.Settings;
import android.provider.Settings;
@@ -62,7 +64,7 @@ public class UserLifecycleStressTest {
    private static final String TAG = "UserLifecycleStressTest";
    private static final String TAG = "UserLifecycleStressTest";
    // TODO: Make this smaller once we have improved it.
    // TODO: Make this smaller once we have improved it.
    private static final int TIMEOUT_IN_SECOND = 40;
    private static final int TIMEOUT_IN_SECOND = 40;
    private static final int CHECK_USER_REMOVED_INTERVAL_MS = 200;
    private static final int CHECK_USER_REMOVED_INTERVAL_MS = 500;


    private static final int NUM_ITERATIONS = 8;
    private static final int NUM_ITERATIONS = 8;
    private static final int WAIT_BEFORE_STOP_USER_IN_SECOND = 3;
    private static final int WAIT_BEFORE_STOP_USER_IN_SECOND = 3;
@@ -75,6 +77,7 @@ public class UserLifecycleStressTest {
    private ActivityManager mActivityManager;
    private ActivityManager mActivityManager;
    private UserSwitchWaiter mUserSwitchWaiter;
    private UserSwitchWaiter mUserSwitchWaiter;
    private String mRemoveGuestOnExitOriginalValue;
    private String mRemoveGuestOnExitOriginalValue;
    private int mOriginalCurrentUserId;


    @Before
    @Before
    public void setup() throws RemoteException {
    public void setup() throws RemoteException {
@@ -85,10 +88,12 @@ public class UserLifecycleStressTest {
        mRemoveGuestOnExitOriginalValue = Settings.Global.getString(mContext.getContentResolver(),
        mRemoveGuestOnExitOriginalValue = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.REMOVE_GUEST_ON_EXIT);
                Settings.Global.REMOVE_GUEST_ON_EXIT);
        waitForBroadcastBarrier(); // isolate tests from each other
        waitForBroadcastBarrier(); // isolate tests from each other
        mOriginalCurrentUserId = ActivityManager.getCurrentUser();
    }
    }


    @After
    @After
    public void tearDown() throws IOException {
    public void tearDown() throws IOException {
        switchUser(mOriginalCurrentUserId);
        mUserSwitchWaiter.close();
        mUserSwitchWaiter.close();
        Settings.Global.putString(mContext.getContentResolver(),
        Settings.Global.putString(mContext.getContentResolver(),
                Settings.Global.REMOVE_GUEST_ON_EXIT, mRemoveGuestOnExitOriginalValue);
                Settings.Global.REMOVE_GUEST_ON_EXIT, mRemoveGuestOnExitOriginalValue);
@@ -101,6 +106,10 @@ public class UserLifecycleStressTest {
     */
     */
    @Test
    @Test
    public void stopManagedProfileStressTest() throws RemoteException, InterruptedException {
    public void stopManagedProfileStressTest() throws RemoteException, InterruptedException {
        UserHandle mainUser = mUserManager.getMainUser();
        assumeFalse("There is no main user", mainUser == null);
        switchUser(mainUser.getIdentifier());

        for (int i = 0; i < NUM_ITERATIONS; i++) {
        for (int i = 0; i < NUM_ITERATIONS; i++) {
            logIteration(i, "stopManagedProfileStressTest");
            logIteration(i, "stopManagedProfileStressTest");


@@ -164,10 +173,6 @@ public class UserLifecycleStressTest {
        Settings.Global.putString(mContext.getContentResolver(),
        Settings.Global.putString(mContext.getContentResolver(),
                Settings.Global.REMOVE_GUEST_ON_EXIT, "0");
                Settings.Global.REMOVE_GUEST_ON_EXIT, "0");


        if (ActivityManager.getCurrentUser() != USER_SYSTEM) {
            switchUser(USER_SYSTEM);
        }

        final List<UserInfo> guestUsers = mUserManager.getGuestUsers();
        final List<UserInfo> guestUsers = mUserManager.getGuestUsers();
        int nextGuestId = guestUsers.isEmpty() ? USER_NULL : guestUsers.get(0).id;
        int nextGuestId = guestUsers.isEmpty() ? USER_NULL : guestUsers.get(0).id;


@@ -202,8 +207,8 @@ public class UserLifecycleStressTest {
                        .isTrue();
                        .isTrue();
            }
            }


            Log.d(TAG, "Switching back to the system user");
            Log.d(TAG, "Switching back to the initial user");
            switchUser(USER_SYSTEM);
            switchUser(mOriginalCurrentUserId);


            nextGuestId = newGuest.id;
            nextGuestId = newGuest.id;
        }
        }
@@ -253,6 +258,10 @@ public class UserLifecycleStressTest {


    /** Starts the given user in the foreground and waits for the switch to finish. */
    /** Starts the given user in the foreground and waits for the switch to finish. */
    private void switchUser(int userId) {
    private void switchUser(int userId) {
        if (ActivityManager.getCurrentUser() == userId) {
            Log.d(TAG, "No need to switch, current user is already user " + userId);
            return;
        }
        Log.d(TAG, "Switching to user " + userId);
        Log.d(TAG, "Switching to user " + userId);


        mUserSwitchWaiter.runThenWaitUntilSwitchCompleted(userId, () -> {
        mUserSwitchWaiter.runThenWaitUntilSwitchCompleted(userId, () -> {