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

Commit 00190cf8 authored by Bill Lin's avatar Bill Lin Committed by android-build-merger
Browse files

Merge changes from topic "migrate_CtsStagedInstallTest_RollbackTest"

am: 63e6d6af

Change-Id: I362c1a03a1f8c106f684cd6a28fe63fbd5532d9c
parents 5a6df151 63e6d6af
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -31,9 +31,9 @@ java_test_host {
}

java_test_host {
    name: "SecondaryUserRollbackTest",
    srcs: ["SecondaryUserRollbackTest/src/**/*.java"],
    name: "MultiUserRollbackTest",
    srcs: ["MultiUserRollbackTest/src/**/*.java"],
    libs: ["tradefed"],
    test_suites: ["general-tests"],
    test_config: "SecondaryUserRollbackTest.xml",
    test_config: "MultiUserRollbackTest.xml",
}
+3 −8
Original line number Diff line number Diff line
@@ -13,17 +13,12 @@
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<configuration description="Runs the rollback test from a secondary user">
    <option name="test-suite-tag" value="SecondaryUserRollbackTest" />
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="RollbackTest.apk" />
    </target_preparer>
<configuration description="Runs rollback tests for multiple users">
    <option name="test-suite-tag" value="MultiUserRollbackTest" />
    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
        <option name="run-command" value="pm uninstall com.android.cts.install.lib.testapp.A" />
        <option name="run-command" value="pm uninstall com.android.cts.install.lib.testapp.B" />
    </target_preparer>
    <test class="com.android.tradefed.testtype.HostTest" >
        <option name="class" value="com.android.tests.rollback.host.SecondaryUserRollbackTest" />
        <option name="class" value="com.android.tests.rollback.host.MultiUserRollbackTest" />
    </test>
</configuration>
+45 −20
Original line number Diff line number Diff line
@@ -28,13 +28,12 @@ import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Runs rollback tests from a secondary user.
 * Runs rollback tests for multiple users.
 */
@RunWith(DeviceJUnit4ClassRunner.class)
public class SecondaryUserRollbackTest extends BaseHostJUnit4Test {
    private static final int SYSTEM_USER_ID = 0;
public class MultiUserRollbackTest extends BaseHostJUnit4Test {
    // The user that was running originally when the test starts.
    private int mOriginalUser = SYSTEM_USER_ID;
    private int mOriginalUserId;
    private int mSecondaryUserId = -1;
    private static final long SWITCH_USER_COMPLETED_NUMBER_OF_POLLS = 60;
    private static final long SWITCH_USER_COMPLETED_POLL_INTERVAL_IN_MILLIS = 1000;
@@ -42,23 +41,45 @@ public class SecondaryUserRollbackTest extends BaseHostJUnit4Test {

    @After
    public void tearDown() throws Exception {
        getDevice().switchUser(mOriginalUser);
        getDevice().switchUser(mOriginalUserId);
        getDevice().executeShellCommand("pm uninstall com.android.cts.install.lib.testapp.A");
        getDevice().executeShellCommand("pm uninstall com.android.cts.install.lib.testapp.B");
        removeSecondaryUserIfNecessary();
    }

    @Before
    public void setup() throws Exception {
        mOriginalUserId = getDevice().getCurrentUser();
        installPackageAsUser("RollbackTest.apk", true, mOriginalUserId);
        createAndSwitchToSecondaryUserIfNecessary();
        installPackageAsUser("RollbackTest.apk", true, mSecondaryUserId, "--user current");
        installPackageAsUser("RollbackTest.apk", true, mSecondaryUserId);
    }

    @Test
    public void testBasic() throws Exception {
    public void testBasicForSecondaryUser() throws Exception {
        runPhaseForUsers("testBasic", mSecondaryUserId);
    }

    @Test
    public void testMultipleUsers() throws Exception {
        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);
    }

    /**
     * Run the phase for the given user ids, in the order they are given.
     */
    private void runPhaseForUsers(String phase, int... userIds) throws Exception {
        for (int userId: userIds) {
            switchToUser(userId);
            assertTrue(runDeviceTests("com.android.tests.rollback",
                "com.android.tests.rollback.RollbackTest",
                "testBasic"));
                    "com.android.tests.rollback.MultiUserRollbackTest",
                    phase));
        }
    }

    private void removeSecondaryUserIfNecessary() throws Exception {
@@ -70,19 +91,23 @@ public class SecondaryUserRollbackTest extends BaseHostJUnit4Test {

    private void createAndSwitchToSecondaryUserIfNecessary() throws Exception {
        if (mSecondaryUserId == -1) {
            mOriginalUser = getDevice().getCurrentUser();
            mSecondaryUserId = getDevice().createUser("SecondaryUserRollbackTest_User");
            assertTrue(getDevice().switchUser(mSecondaryUserId));
            // give time for user to be switched
            waitForSwitchUserCompleted(mSecondaryUserId);
            mOriginalUserId = getDevice().getCurrentUser();
            mSecondaryUserId = getDevice().createUser("MultiUserRollbackTest_User"
                    + System.currentTimeMillis());
            switchToUser(mSecondaryUserId);
        }
    }

    private void switchToUser(int userId) throws Exception {
        if (getDevice().getCurrentUser() == userId) {
            return;
        }

    private void waitForSwitchUserCompleted(int userId) throws Exception {
        assertTrue(getDevice().switchUser(userId));
        for (int i = 0; i < SWITCH_USER_COMPLETED_NUMBER_OF_POLLS; ++i) {
            String logs = getDevice().executeAdbCommand("logcat", "-v", "brief", "-d",
                    "ActivityManager:D");
            if (logs.contains("Posting BOOT_COMPLETED user #" + userId)) {
            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);
+3 −2
Original line number Diff line number Diff line
@@ -22,8 +22,9 @@
        <option name="package" value="com.android.tests.rollback" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />

        <!-- Exclude the StagedRollbackTest tests, which needs to be specially
             driven from the StagedRollbackTest host test -->
        <!-- Exclude the StagedRollbackTest and MultiUserRollbackTest tests, which need to be
             specially driven from the StagedRollbackTest and MultiUserRollbackTest host test -->
        <option name="exclude-filter" value="com.android.tests.rollback.StagedRollbackTest" />
        <option name="exclude-filter" value="com.android.tests.rollback.MultiUserRollbackTest" />
    </test>
</configuration>
+111 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tests.rollback;

import static com.android.cts.rollback.lib.RollbackInfoSubject.assertThat;
import static com.android.cts.rollback.lib.RollbackUtils.getUniqueRollbackInfoForPackage;

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

import android.Manifest;
import android.content.rollback.RollbackInfo;
import android.content.rollback.RollbackManager;

import com.android.cts.install.lib.Install;
import com.android.cts.install.lib.InstallUtils;
import com.android.cts.install.lib.TestApp;
import com.android.cts.rollback.lib.Rollback;
import com.android.cts.rollback.lib.RollbackUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;


@RunWith(JUnit4.class)
public class MultiUserRollbackTest {

    @Before
    public void adoptShellPermissions() {
        InstallUtils.adoptShellPermissionIdentity(
                Manifest.permission.INSTALL_PACKAGES,
                Manifest.permission.DELETE_PACKAGES,
                Manifest.permission.TEST_MANAGE_ROLLBACKS,
                Manifest.permission.MANAGE_ROLLBACKS);
    }

    @After
    public void dropShellPermissions() {
        InstallUtils.dropShellPermissionIdentity();
    }

    @Test
    public void testBasic() throws Exception {
        new RollbackTest().testBasic();
    }

    /**
     * Install version 1 of the test app. This method is run for both users.
     */
    @Test
    public void testMultipleUsersInstallV1() throws Exception {
        assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(-1);
        Install.single(TestApp.A1).commit();
        assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(1);
        InstallUtils.processUserData(TestApp.A);
    }

    /**
     * Upgrade the test app to version 2. This method should only run once as the system user,
     * and will update the app for both users.
     */
    @Test
    public void testMultipleUsersUpgradeToV2() throws Exception {
        RollbackManager rm = RollbackUtils.getRollbackManager();
        assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(1);
        Install.single(TestApp.A2).setEnableRollback().commit();
        assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(2);
        RollbackInfo rollback = getUniqueRollbackInfoForPackage(
                rm.getAvailableRollbacks(), TestApp.A);
        assertThat(rollback).isNotNull();
        assertThat(rollback).packagesContainsExactly(
                Rollback.from(TestApp.A2).to(TestApp.A1));
    }

    /**
     * This method is run for both users. Assert that the test app has upgraded for both users, and
     * update their userdata to reflect this new version.
     */
    @Test
    public void testMultipleUsersUpdateUserData() {
        assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(2);
        InstallUtils.processUserData(TestApp.A);
    }

    /**
     * The system will have rolled back the test app at this stage. Verify that the rollback has
     * taken place, and that the userdata has been correctly rolled back. This method is run for
     * both users.
     */
    @Test
    public void testMultipleUsersVerifyUserdataRollback() {
        assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(1);
        InstallUtils.processUserData(TestApp.A);
    }
}
Loading