summaryrefslogtreecommitdiff
path: root/hostsidetests/install/src/android/cts/install/host/UpgradeTest.java
blob: 21067b5792a7ffbea2cc306a2741d7ac8fa08f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2020 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 android.cts.install.host;

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

import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import android.cts.install.INSTALL_TYPE;
import android.platform.test.annotations.LargeTest;

import com.android.compatibility.common.util.CpuFeatures;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RunWith(DeviceParameterized.class)
@UseParametersRunnerFactory(DeviceParameterized.RunnerFactory.class)
public final class UpgradeTest extends BaseHostJUnit4Test {
    private static final String PACKAGE_NAME = "android.cts.install";
    private static final String PHASE_FORMAT_SUFFIX = "[%s_Staged%b_Rollback%b]";
    private static final String ARRANGE_PHASE = "arrange_phase";
    private static final String ASSERT_POST_ARRANGE_PHASE = "assert_postArrange_phase";
    private static final String ACTION_PHASE = "action_phase";
    private static final String ASSERT_PHASE = "assert_phase";
    private static final String ASSERT_PRE_REBOOT_PHASE = "assert_preReboot_phase";
    private static final String ASSERT_POST_REBOOT_PHASE = "assert_postReboot_phase";
    private static final String CLEAN_UP_PHASE = "cleanUp_phase";

    @Rule
    public ShimApexRule mShimApexRule = new ShimApexRule(this);

    @Parameter(0)
    public INSTALL_TYPE mInstallType;

    @Parameter(1)
    public boolean mEnableRollback;

    @Parameters(name = "{0}_Rollback{1}")
    public static Collection<Object[]> combinations() {
        boolean[] booleanValues = new boolean[]{true, false};
        List<Object[]> temp = new ArrayList<>();
        for (INSTALL_TYPE installType : INSTALL_TYPE.values()) {
            for (boolean enableRollback : booleanValues) {
                temp.add(new Object[]{installType, enableRollback});
            }
        }
        return temp;
    }

    @Before
    @After
    public void cleanUp() throws Exception {
        runPhase(CLEAN_UP_PHASE);
    }

    @Before
    public void assumeNotNativeBridgeWithApex() throws Exception {
        if (!CpuFeatures.isNativeAbi(getDevice(), getAbi().getName())) {
            assumeFalse("APEX packages do not work with native bridge",
                    mInstallType.containsApex());
        }
    }

    @Before
    public void assumeApexSupported() throws DeviceNotAvailableException {
        if (mInstallType.containsApex()) {
            assumeTrue("Device does not support updating APEX",
                    mShimApexRule.isUpdatingApexSupported());
        }
    }

    @Test
    public void testNonStagedUpgrade() throws Exception {
        // Apex should not be committed in non-staged install, such logic covered in InstallTest.
        assumeFalse(mInstallType.containsApex());
        runPhase(ARRANGE_PHASE);
        runPhase(ASSERT_POST_ARRANGE_PHASE);

        runPhase(ACTION_PHASE);

        runPhase(ASSERT_PHASE);
    }

    @Test
    @LargeTest
    public void testStagedUpgrade() throws Exception {
        assumeTrue(mInstallType.containsApex());
        runStagedPhase(ARRANGE_PHASE);
        getDevice().reboot();
        runStagedPhase(ASSERT_POST_ARRANGE_PHASE);

        runStagedPhase(ACTION_PHASE);

        runStagedPhase(ASSERT_PRE_REBOOT_PHASE);
        getDevice().reboot();
        runStagedPhase(ASSERT_POST_REBOOT_PHASE);
    }

    private void runPhase(String phase) throws DeviceNotAvailableException {
        runPhase(phase, false /* staged */);
    }

    private void runStagedPhase(String phase) throws DeviceNotAvailableException {
        runPhase(phase, true /* staged */);
    }

    /**
     * Runs the given phase of a test with parameters by calling into the device.
     * Throws an exception if the test phase fails.
     * <p>
     * For example, <code>runPhase("action_phase", true);</code>
     */
    private void runPhase(String phase, boolean staged) throws DeviceNotAvailableException {
        assertThat(runDeviceTests(PACKAGE_NAME,
                String.format("%s.%s", PACKAGE_NAME, this.getClass().getSimpleName()),
                String.format(phase + PHASE_FORMAT_SUFFIX, mInstallType, staged, mEnableRollback)))
                .isTrue();
    }
}