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

Commit f2a75879 authored by JW Wang's avatar JW Wang
Browse files

No need to switch users before running tests (2/n)

My local test shows that switch-user doesn't work on my physical device.
In fact there are APIs to run tests against a particular user without
switching. It also speeds up tests without switching users from time to
time.

Bug: 149876119
Test: atest MultiUserRollbackTest
Change-Id: I34d26ddcb6a6e9cdc39228310830a3cd83212e4a
parent eb908a9e
Loading
Loading
Loading
Loading
+26 −6
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;

/**
 * Runs rollback tests for multiple users.
 */
@@ -41,7 +43,6 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {

    @After
    public void tearDown() throws Exception {
        getDevice().switchUser(mOriginalUserId);
        getDevice().executeShellCommand("pm uninstall com.android.cts.install.lib.testapp.A");
        removeSecondaryUserIfNecessary();
    }
@@ -49,8 +50,8 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {
    @Before
    public void setup() throws Exception {
        mOriginalUserId = getDevice().getCurrentUser();
        createAndStartSecondaryUser();
        installPackageAsUser("RollbackTest.apk", true, mOriginalUserId);
        createAndSwitchToSecondaryUserIfNecessary();
        installPackageAsUser("RollbackTest.apk", true, mSecondaryUserId);
    }

@@ -64,7 +65,6 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {
        runPhaseForUsers("testMultipleUsersInstallV1", mOriginalUserId, mSecondaryUserId);
        runPhaseForUsers("testMultipleUsersUpgradeToV2", mOriginalUserId);
        runPhaseForUsers("testMultipleUsersUpdateUserData", mOriginalUserId, mSecondaryUserId);
        switchToUser(mOriginalUserId);
        getDevice().executeShellCommand("pm rollback-app com.android.cts.install.lib.testapp.A");
        runPhaseForUsers("testMultipleUsersVerifyUserdataRollback", mOriginalUserId,
                mSecondaryUserId);
@@ -74,11 +74,11 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {
     * Run the phase for the given user ids, in the order they are given.
     */
    private void runPhaseForUsers(String phase, int... userIds) throws Exception {
        final long timeout = TimeUnit.MINUTES.toMillis(10);
        for (int userId: userIds) {
            switchToUser(userId);
            assertTrue(runDeviceTests("com.android.tests.rollback",
            assertTrue(runDeviceTests(getDevice(), "com.android.tests.rollback",
                    "com.android.tests.rollback.MultiUserRollbackTest",
                    phase));
                    phase, userId, timeout));
        }
    }

@@ -89,6 +89,26 @@ public class MultiUserRollbackTest extends BaseHostJUnit4Test {
        }
    }

    private void awaitUserUnlocked(int userId) throws Exception {
        for (int i = 0; i < SWITCH_USER_COMPLETED_NUMBER_OF_POLLS; ++i) {
            String userState = getDevice().executeShellCommand("am get-started-user-state "
                    + userId);
            if (userState.contains("RUNNING_UNLOCKED")) {
                return;
            }
            Thread.sleep(SWITCH_USER_COMPLETED_POLL_INTERVAL_IN_MILLIS);
        }
        fail("Timed out in unlocking user: " + userId);
    }

    private void createAndStartSecondaryUser() throws Exception {
        String name = "MultiUserRollbackTest_User" + System.currentTimeMillis();
        mSecondaryUserId = getDevice().createUser(name);
        getDevice().startUser(mSecondaryUserId);
        // Note we can't install apps on a locked user
        awaitUserUnlocked(mSecondaryUserId);
    }

    private void createAndSwitchToSecondaryUserIfNecessary() throws Exception {
        if (mSecondaryUserId == -1) {
            mOriginalUserId = getDevice().getCurrentUser();