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.tradefed.device.DeviceNotAvailableException;
28 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized.Parameter;
36 import org.junit.runners.Parameterized.Parameters;
37 import org.junit.runners.Parameterized.UseParametersRunnerFactory;
38 
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
42 
43 @RunWith(DeviceParameterized.class)
44 @UseParametersRunnerFactory(DeviceParameterized.RunnerFactory.class)
45 public final class SamegradeTest extends BaseHostJUnit4Test {
46     private static final String PACKAGE_NAME = "android.cts.install";
47     private static final String PHASE_FORMAT_SUFFIX = "[%s_Staged%b_Rollback%b]";
48     private static final String ARRANGE_PHASE = "arrange_phase";
49     private static final String ASSERT_POST_ARRANGE_PHASE = "assert_postArrange_phase";
50     private static final String ACTION_PHASE = "action_phase";
51     private static final String ACTION_SYSTEMAPEX_PHASE = "action_systemApex_phase";
52     private static final String ASSERT_PRE_REBOOT_PHASE = "assert_preReboot_phase";
53     private static final String ASSERT_POST_REBOOT_PHASE = "assert_postReboot_phase";
54     private static final String ASSERT_SYSTEMAPEX_REBOOT_PHASE =
55             "assert_systemApex_postReboot_phase";
56     private static final String ASSERT_PHASE = "assert_phase";
57     private static final String CLEAN_UP_PHASE = "cleanUp_phase";
58 
59     @Rule
60     public ShimApexRule mShimApexRule = new ShimApexRule(this);
61 
62     @Parameter(0)
63     public INSTALL_TYPE mInstallType;
64 
65     @Parameter(1)
66     public boolean mEnableRollback;
67 
68     @Parameters(name = "{0}_Rollback{1}")
combinations()69     public static Collection<Object[]> combinations() {
70         boolean[] booleanValues = new boolean[]{true, false};
71         List<Object[]> temp = new ArrayList<>();
72         for (INSTALL_TYPE installType : INSTALL_TYPE.values()) {
73             for (boolean enableRollback : booleanValues) {
74                 temp.add(new Object[]{installType, enableRollback});
75             }
76         }
77         return temp;
78     }
79 
80     @Before
81     @After
cleanUp()82     public void cleanUp() throws Exception {
83         runPhase(CLEAN_UP_PHASE);
84     }
85 
86     @Before
assumeApexSupported()87     public void assumeApexSupported() throws DeviceNotAvailableException {
88         if (mInstallType.containsApex()) {
89             assumeTrue("Device does not support updating APEX",
90                     mShimApexRule.isUpdatingApexSupported());
91         }
92     }
93 
94     /**
95      * Samegrading on a non-APEX install type should be success.
96      */
97     @Test
testNonStagedSamegrade()98     public void testNonStagedSamegrade() throws Exception {
99         // Apex should not be committed in non-staged install, such logic covered in InstallTest.
100         assumeFalse(mInstallType.containsApex());
101 
102         runPhase(ARRANGE_PHASE);
103         runPhase(ASSERT_POST_ARRANGE_PHASE);
104 
105         runPhase(ACTION_PHASE);
106 
107         runPhase(ASSERT_PHASE);
108     }
109 
110     @Test
111     @LargeTest
testStagedSameGrade()112     public void testStagedSameGrade() throws Exception {
113         assumeTrue(mInstallType.containsApex());
114         runStagedPhase(ARRANGE_PHASE);
115         getDevice().reboot();
116         runStagedPhase(ASSERT_POST_ARRANGE_PHASE);
117 
118         runStagedPhase(ACTION_PHASE);
119 
120         runStagedPhase(ASSERT_PRE_REBOOT_PHASE);
121         getDevice().reboot();
122         runStagedPhase(ASSERT_POST_REBOOT_PHASE);
123     }
124 
125     @Test
126     @LargeTest
testStagedSamegradeSystemApex()127     public void testStagedSamegradeSystemApex() throws Exception {
128         assumeTrue(mInstallType.containsApex());
129 
130         runStagedPhase(ACTION_SYSTEMAPEX_PHASE);
131         getDevice().reboot();
132 
133         runStagedPhase(ASSERT_SYSTEMAPEX_REBOOT_PHASE);
134     }
135 
runPhase(String phase)136     private void runPhase(String phase) throws DeviceNotAvailableException {
137         runPhase(phase, false /* staged */);
138     }
139 
runStagedPhase(String phase)140     private void runStagedPhase(String phase) throws DeviceNotAvailableException {
141         runPhase(phase, true /* staged */);
142     }
143 
144     /**
145      * Runs the given phase of a test with parameters by calling into the device.
146      * Throws an exception if the test phase fails.
147      * <p>
148      * For example, <code>runPhase("action_phase", true);</code>
149      */
runPhase(String phase, boolean staged)150     private void runPhase(String phase, boolean staged) throws DeviceNotAvailableException {
151         assertThat(runDeviceTests(PACKAGE_NAME,
152                 String.format("%s.%s", PACKAGE_NAME, this.getClass().getSimpleName()),
153                 String.format(phase + PHASE_FORMAT_SUFFIX, mInstallType, staged, mEnableRollback)))
154                 .isTrue();
155     }
156 }
157