1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 package android.appsecurity.cts;
17 
18 import android.platform.test.annotations.AppModeFull;
19 import android.platform.test.annotations.AppModeInstant;
20 
21 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
22 
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 
28 @RunWith(DeviceJUnit4ClassRunner.class)
29 public class IsolatedSplitsTests extends BaseAppSecurityTest {
30     private static final String PKG = "com.android.cts.isolatedsplitapp";
31     private static final String TEST_CLASS = PKG + ".SplitAppTest";
32 
33     /* The feature hierarchy looks like this:
34 
35         APK_BASE <- APK_FEATURE_A <- APK_FEATURE_B
36             ^------ APK_FEATURE_C
37 
38      */
39     private static final String APK_BASE = "CtsIsolatedSplitApp.apk";
40     private static final String APK_BASE_pl = "CtsIsolatedSplitApp_pl.apk";
41     private static final String APK_FEATURE_A = "CtsIsolatedSplitAppFeatureA.apk";
42     private static final String APK_FEATURE_A_pl = "CtsIsolatedSplitAppFeatureA_pl.apk";
43     private static final String APK_FEATURE_B = "CtsIsolatedSplitAppFeatureB.apk";
44     private static final String APK_FEATURE_B_pl = "CtsIsolatedSplitAppFeatureB_pl.apk";
45     private static final String APK_FEATURE_C = "CtsIsolatedSplitAppFeatureC.apk";
46     private static final String APK_FEATURE_C_pl = "CtsIsolatedSplitAppFeatureC_pl.apk";
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         Utils.prepareSingleUser(getDevice());
51         getDevice().uninstallPackage(PKG);
52     }
53 
54     @After
tearDown()55     public void tearDown() throws Exception {
56         getDevice().uninstallPackage(PKG);
57     }
58 
59     @Test
60     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallBase_full()61     public void testInstallBase_full() throws Exception {
62         testInstallBase(false);
63     }
64 
65     @Test
66     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallBase_instant()67     public void testInstallBase_instant() throws Exception {
68         testInstallBase(true);
69     }
70 
testInstallBase(boolean instant)71     private void testInstallBase(boolean instant) throws Exception {
72         new InstallMultiple(instant).addApk(APK_BASE).run();
73         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadDefault");
74     }
75 
76     @Test
77     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallBaseAndConfigSplit_full()78     public void testInstallBaseAndConfigSplit_full() throws Exception {
79         testInstallBaseAndConfigSplit(false);
80     }
81 
82     @Test
83     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallBaseAndConfigSplit_instant()84     public void testInstallBaseAndConfigSplit_instant() throws Exception {
85         testInstallBaseAndConfigSplit(true);
86     }
87 
testInstallBaseAndConfigSplit(boolean instant)88     private void testInstallBaseAndConfigSplit(boolean instant) throws Exception {
89         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_BASE_pl).run();
90         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadPolishLocale");
91     }
92 
93     @Test
94     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallMissingDependency_full()95     public void testInstallMissingDependency_full() throws Exception {
96         testInstallMissingDependency(false);
97     }
98 
99     @Test
100     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallMissingDependency_instant()101     public void testInstallMissingDependency_instant() throws Exception {
102         testInstallMissingDependency(true);
103     }
104 
testInstallMissingDependency(boolean instant)105     private void testInstallMissingDependency(boolean instant) throws Exception {
106         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_FEATURE_B).runExpectingFailure();
107     }
108 
109     @Test
110     @AppModeFull(reason = "b/109878606; instant applications can't send broadcasts to manifest "
111             + "receivers")
testInstallOneFeatureSplit_full()112     public void testInstallOneFeatureSplit_full() throws Exception {
113         testInstallOneFeatureSplit(false);
114     }
115 
testInstallOneFeatureSplit(boolean instant)116     private void testInstallOneFeatureSplit(boolean instant) throws Exception {
117         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_FEATURE_A).run();
118         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadDefault");
119         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
120                 "shouldLoadFeatureADefault");
121         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
122                 "shouldLoadFeatureAReceivers");
123     }
124 
125     @Test
126     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallOneFeatureSplitAndConfigSplits_full()127     public void testInstallOneFeatureSplitAndConfigSplits_full() throws Exception {
128         testInstallOneFeatureSplitAndConfigSplits(false);
129     }
130 
131     @Test
132     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallOneFeatureSplitAndConfigSplits_instant()133     public void testInstallOneFeatureSplitAndConfigSplits_instant() throws Exception {
134         testInstallOneFeatureSplitAndConfigSplits(true);
135     }
136 
testInstallOneFeatureSplitAndConfigSplits(boolean instant)137     private void testInstallOneFeatureSplitAndConfigSplits(boolean instant) throws Exception {
138         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_FEATURE_A).addApk(APK_BASE_pl)
139                 .addApk(APK_FEATURE_A_pl).run();
140         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadPolishLocale");
141         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
142                 "shouldLoadFeatureAPolishLocale");
143     }
144 
145     @Test
146     @AppModeFull(reason = "b/109878606; instant applications can't send broadcasts to manifest "
147             + "receivers")
testInstallDependentFeatureSplits_full()148     public void testInstallDependentFeatureSplits_full() throws Exception {
149         testInstallDependentFeatureSplits(false);
150     }
151 
testInstallDependentFeatureSplits(boolean instant)152     private void testInstallDependentFeatureSplits(boolean instant) throws Exception {
153         new InstallMultiple(instant)
154                 .addApk(APK_BASE).addApk(APK_FEATURE_A).addApk(APK_FEATURE_B).run();
155         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadDefault");
156         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
157                 "shouldLoadFeatureADefault");
158         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
159                 "shouldLoadFeatureBDefault");
160         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
161                 "shouldLoadFeatureAAndBReceivers");
162     }
163 
164     @Test
165     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallDependentFeatureSplitsAndConfigSplits_full()166     public void testInstallDependentFeatureSplitsAndConfigSplits_full() throws Exception {
167         testInstallDependentFeatureSplitsAndConfigSplits(false);
168     }
169 
170     @Test
171     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallDependentFeatureSplitsAndConfigSplits_instant()172     public void testInstallDependentFeatureSplitsAndConfigSplits_instant() throws Exception {
173         testInstallDependentFeatureSplitsAndConfigSplits(true);
174     }
175 
testInstallDependentFeatureSplitsAndConfigSplits(boolean instant)176     private void testInstallDependentFeatureSplitsAndConfigSplits(boolean instant)
177             throws Exception {
178         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_FEATURE_A).addApk(APK_FEATURE_B)
179                 .addApk(APK_BASE_pl).addApk(APK_FEATURE_A_pl).addApk(APK_FEATURE_B_pl).run();
180         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadPolishLocale");
181         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
182                 "shouldLoadFeatureAPolishLocale");
183         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
184                 "shouldLoadFeatureBPolishLocale");
185     }
186 
187     @Test
188     @AppModeFull(reason = "b/109878606; instant applications can't send broadcasts to manifest "
189             + "receivers")
testInstallAllFeatureSplits_full()190     public void testInstallAllFeatureSplits_full() throws Exception {
191         testInstallAllFeatureSplits(false);
192     }
193 
testInstallAllFeatureSplits(boolean instant)194     private void testInstallAllFeatureSplits(boolean instant) throws Exception {
195         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_FEATURE_A).addApk(APK_FEATURE_B)
196                 .addApk(APK_FEATURE_C).run();
197         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadDefault");
198         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
199                 "shouldLoadFeatureADefault");
200         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
201                 "shouldLoadFeatureBDefault");
202         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
203                 "shouldLoadFeatureCDefault");
204         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
205                 "shouldLoadFeatureAAndBAndCReceivers");
206     }
207 
208     @Test
209     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallAllFeatureSplitsAndConfigSplits_full()210     public void testInstallAllFeatureSplitsAndConfigSplits_full() throws Exception {
211         testInstallAllFeatureSplitsAndConfigSplits(false);
212     }
213 
214     @Test
215     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallAllFeatureSplitsAndConfigSplits_instant()216     public void testInstallAllFeatureSplitsAndConfigSplits_instant() throws Exception {
217         testInstallAllFeatureSplitsAndConfigSplits(true);
218     }
219 
testInstallAllFeatureSplitsAndConfigSplits(boolean instant)220     private void testInstallAllFeatureSplitsAndConfigSplits(boolean instant) throws Exception {
221         new InstallMultiple(instant).addApk(APK_BASE).addApk(APK_FEATURE_A).addApk(APK_FEATURE_B)
222                 .addApk(APK_FEATURE_C).addApk(APK_BASE_pl).addApk(APK_FEATURE_A_pl)
223                 .addApk(APK_FEATURE_C_pl).run();
224         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS, "shouldLoadDefault");
225         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
226                 "shouldLoadFeatureADefault");
227         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
228                 "shouldLoadFeatureBDefault");
229         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, TEST_CLASS,
230                 "shouldLoadFeatureCDefault");
231     }
232 }
233