• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.cts.install.host;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assume.assumeFalse;
22 import static org.junit.Assume.assumeTrue;
23 
24 import android.cts.install.INSTALL_TYPE;
25 import android.platform.test.annotations.LargeTest;
26 
27 import com.android.compatibility.common.util.CpuFeatures;
28 import com.android.tradefed.device.DeviceNotAvailableException;
29 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.Parameterized.Parameter;
37 import org.junit.runners.Parameterized.Parameters;
38 import org.junit.runners.Parameterized.UseParametersRunnerFactory;
39 
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.List;
43 
44 @RunWith(DeviceParameterized.class)
45 @UseParametersRunnerFactory(DeviceParameterized.RunnerFactory.class)
46 public final class InstallTest extends BaseHostJUnit4Test {
47     private static final String PACKAGE_NAME = "android.cts.install";
48     private static final String CUSTOMIZED_LAUNCHER_COMPONENT =
49             PACKAGE_NAME + "/" + PACKAGE_NAME + ".LauncherActivity";
50     private static final String PHASE_FORMAT_SUFFIX = "[%s_Staged%b_Rollback%b]";
51 
52     private static final String CLEAN_UP_PHASE = "cleanUp_phase";
53     private static final String ACTION_PHASE = "action_phase";
54     private static final String ACTION_ABANDON_SESSION_PHASE = "action_abandonSession_phase";
55     private static final String ASSERT_PHASE = "assert_phase";
56     private static final String ASSERT_COMMIT_FAILURE_PHASE = "assert_commitFailure_phase";
57     private static final String ASSERT_PRE_REBOOT_PHASE = "assert_preReboot_phase";
58     private static final String ASSERT_POST_REBOOT_PHASE = "assert_postReboot_phase";
59     private static final String ASSERT_ABANDON_SESSION = "assert_abandonSession_phase";
60 
61     @Rule
62     public ShimApexRule mShimApexRule = new ShimApexRule(this);
63 
64     @Rule
65     public LauncherRule mLauncherRule = new LauncherRule(this, CUSTOMIZED_LAUNCHER_COMPONENT);
66 
67     @Parameter(0)
68     public INSTALL_TYPE mInstallType;
69 
70     @Parameter(1)
71     public boolean mEnableRollback;
72 
73     @Parameters(name = "{0}_Rollback{1}")
combinations()74     public static Collection<Object[]> combinations() {
75         boolean[] booleanValues = new boolean[]{true, false};
76         List<Object[]> temp = new ArrayList<>();
77         for (INSTALL_TYPE installType: INSTALL_TYPE.values()) {
78             for (boolean enableRollback: booleanValues) {
79                 temp.add(new Object[]{installType, enableRollback});
80             }
81         }
82         return temp;
83     }
84 
85     private boolean mStaged;
86 
87     @Before
88     @After
cleanUp()89     public void cleanUp() throws Exception {
90         runPhase(CLEAN_UP_PHASE);
91     }
92 
93     @Before
assumeApexSupported()94     public void assumeApexSupported() throws DeviceNotAvailableException {
95         if (mInstallType.containsApex()) {
96             assumeTrue("Device does not support updating APEX",
97                     mShimApexRule.isUpdatingApexSupported());
98         }
99     }
100 
101     @Before
assumeNotNativeBridgeWithApex()102     public void assumeNotNativeBridgeWithApex() throws Exception {
103         if (!CpuFeatures.isNativeAbi(getDevice(), getAbi().getName())) {
104             assumeFalse("APEX packages do not work with native bridge",
105                     mInstallType.containsApex());
106         }
107     }
108 
109     @Test
testInstall()110     public void testInstall() throws Exception {
111         mStaged = false;
112         if (mInstallType.containsApex()) {
113             runPhase(ASSERT_COMMIT_FAILURE_PHASE);
114             return;
115         }
116         runPhase(ACTION_PHASE);
117         runPhase(ASSERT_PHASE);
118     }
119 
120     @Test
121     @LargeTest
testStagedInstall()122     public void testStagedInstall() throws Exception {
123         mStaged = true;
124         runPhase(ACTION_PHASE);
125         runPhase(ASSERT_PRE_REBOOT_PHASE);
126         getDevice().reboot();
127         runPhase(ASSERT_POST_REBOOT_PHASE);
128     }
129 
130     @Test
testAbandonStagedSessionBeforeReboot()131     public void testAbandonStagedSessionBeforeReboot() throws Exception {
132         mStaged = true;
133         runPhase(ACTION_PHASE);
134         runPhase(ASSERT_PRE_REBOOT_PHASE);
135         runPhase(ACTION_ABANDON_SESSION_PHASE);
136         runPhase(ASSERT_ABANDON_SESSION);
137     }
138 
139     @Test
140     @LargeTest
testAbandonStagedSessionAfterReboot()141     public void testAbandonStagedSessionAfterReboot() throws Exception {
142         mStaged = true;
143         runPhase(ACTION_PHASE);
144         getDevice().reboot();
145         runPhase(ACTION_ABANDON_SESSION_PHASE);
146         runPhase(ASSERT_POST_REBOOT_PHASE);
147     }
148 
149     /**
150      * Runs the given phase of a test with parameters by calling into the device.
151      * Throws an exception if the test phase fails.
152      * <p>
153      * For example, <code>runPhase("action_phase");</code>
154      */
runPhase(String phase)155     private void runPhase(String phase) throws DeviceNotAvailableException {
156         assertThat(runDeviceTests(PACKAGE_NAME,
157                 String.format("%s.%s", PACKAGE_NAME, this.getClass().getSimpleName()),
158                 String.format(phase + PHASE_FORMAT_SUFFIX, mInstallType, mStaged, mEnableRollback)))
159                 .isTrue();
160     }
161 }
162