1 /*
2  * Copyright (C) 2016 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.content.pm;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 
23 import android.os.Build;
24 import android.support.test.runner.AndroidJUnit4;
25 import android.test.suitebuilder.annotation.SmallTest;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 @SmallTest
31 @RunWith(AndroidJUnit4.class)
32 public class PackageParserTest {
33     private static final String RELEASED = null;
34     private static final String OLDER_PRE_RELEASE = "A";
35     private static final String PRE_RELEASE = "B";
36     private static final String NEWER_PRE_RELEASE = "C";
37 
38     private static final String[] CODENAMES_RELEASED = { /* empty */ };
39     private static final String[] CODENAMES_PRE_RELEASE = { PRE_RELEASE };
40 
41     private static final int OLDER_VERSION = 10;
42     private static final int PLATFORM_VERSION = 20;
43     private static final int NEWER_VERSION = 30;
44 
verifyComputeMinSdkVersion(int minSdkVersion, String minSdkCodename, boolean isPlatformReleased, int expectedMinSdk)45     private void verifyComputeMinSdkVersion(int minSdkVersion, String minSdkCodename,
46             boolean isPlatformReleased, int expectedMinSdk) {
47         final String[] outError = new String[1];
48         final int result = PackageParser.computeMinSdkVersion(
49                 minSdkVersion,
50                 minSdkCodename,
51                 PLATFORM_VERSION,
52                 isPlatformReleased ? CODENAMES_RELEASED : CODENAMES_PRE_RELEASE,
53                 outError);
54 
55         assertEquals(result, expectedMinSdk);
56 
57         if (expectedMinSdk == -1) {
58             assertNotNull(outError[0]);
59         } else {
60             assertNull(outError[0]);
61         }
62     }
63 
64     @Test
testComputeMinSdkVersion_preReleasePlatform()65     public void testComputeMinSdkVersion_preReleasePlatform() {
66         // Do allow older release minSdkVersion on pre-release platform.
67         // APP: Released API 10
68         // DEV: Pre-release API 20
69         verifyComputeMinSdkVersion(OLDER_VERSION, RELEASED, false, OLDER_VERSION);
70 
71         // Do allow same release minSdkVersion on pre-release platform.
72         // APP: Released API 20
73         // DEV: Pre-release API 20
74         verifyComputeMinSdkVersion(PLATFORM_VERSION, RELEASED, false, PLATFORM_VERSION);
75 
76         // Don't allow newer release minSdkVersion on pre-release platform.
77         // APP: Released API 30
78         // DEV: Pre-release API 20
79         verifyComputeMinSdkVersion(NEWER_VERSION, RELEASED, false, -1);
80 
81         // Don't allow older pre-release minSdkVersion on pre-release platform.
82         // APP: Pre-release API 10
83         // DEV: Pre-release API 20
84         verifyComputeMinSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, false, -1);
85 
86         // Do allow same pre-release minSdkVersion on pre-release platform,
87         // but overwrite the specified version with CUR_DEVELOPMENT.
88         // APP: Pre-release API 20
89         // DEV: Pre-release API 20
90         verifyComputeMinSdkVersion(PLATFORM_VERSION, PRE_RELEASE, false,
91                 Build.VERSION_CODES.CUR_DEVELOPMENT);
92 
93         // Don't allow newer pre-release minSdkVersion on pre-release platform.
94         // APP: Pre-release API 30
95         // DEV: Pre-release API 20
96         verifyComputeMinSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, false, -1);
97     }
98 
99     @Test
testComputeMinSdkVersion_releasedPlatform()100     public void testComputeMinSdkVersion_releasedPlatform() {
101         // Do allow older release minSdkVersion on released platform.
102         // APP: Released API 10
103         // DEV: Released API 20
104         verifyComputeMinSdkVersion(OLDER_VERSION, RELEASED, true, OLDER_VERSION);
105 
106         // Do allow same release minSdkVersion on released platform.
107         // APP: Released API 20
108         // DEV: Released API 20
109         verifyComputeMinSdkVersion(PLATFORM_VERSION, RELEASED, true, PLATFORM_VERSION);
110 
111         // Don't allow newer release minSdkVersion on released platform.
112         // APP: Released API 30
113         // DEV: Released API 20
114         verifyComputeMinSdkVersion(NEWER_VERSION, RELEASED, true, -1);
115 
116         // Don't allow older pre-release minSdkVersion on released platform.
117         // APP: Pre-release API 10
118         // DEV: Released API 20
119         verifyComputeMinSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, true, -1);
120 
121         // Don't allow same pre-release minSdkVersion on released platform.
122         // APP: Pre-release API 20
123         // DEV: Released API 20
124         verifyComputeMinSdkVersion(PLATFORM_VERSION, PRE_RELEASE, true, -1);
125 
126         // Don't allow newer pre-release minSdkVersion on released platform.
127         // APP: Pre-release API 30
128         // DEV: Released API 20
129         verifyComputeMinSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, true, -1);
130     }
131 
verifyComputeTargetSdkVersion(int targetSdkVersion, String targetSdkCodename, boolean isPlatformReleased, int expectedTargetSdk)132     private void verifyComputeTargetSdkVersion(int targetSdkVersion, String targetSdkCodename,
133             boolean isPlatformReleased, int expectedTargetSdk) {
134         final String[] outError = new String[1];
135         final int result = PackageParser.computeTargetSdkVersion(
136                 targetSdkVersion,
137                 targetSdkCodename,
138                 PLATFORM_VERSION,
139                 isPlatformReleased ? CODENAMES_RELEASED : CODENAMES_PRE_RELEASE,
140                 outError);
141 
142         assertEquals(result, expectedTargetSdk);
143 
144         if (expectedTargetSdk == -1) {
145             assertNotNull(outError[0]);
146         } else {
147             assertNull(outError[0]);
148         }
149     }
150 
151     @Test
testComputeTargetSdkVersion_preReleasePlatform()152     public void testComputeTargetSdkVersion_preReleasePlatform() {
153         // Do allow older release targetSdkVersion on pre-release platform.
154         // APP: Released API 10
155         // DEV: Pre-release API 20
156         verifyComputeTargetSdkVersion(OLDER_VERSION, RELEASED, false, OLDER_VERSION);
157 
158         // Do allow same release targetSdkVersion on pre-release platform.
159         // APP: Released API 20
160         // DEV: Pre-release API 20
161         verifyComputeTargetSdkVersion(PLATFORM_VERSION, RELEASED, false, PLATFORM_VERSION);
162 
163         // Do allow newer release targetSdkVersion on pre-release platform.
164         // APP: Released API 30
165         // DEV: Pre-release API 20
166         verifyComputeTargetSdkVersion(NEWER_VERSION, RELEASED, false, NEWER_VERSION);
167 
168         // Don't allow older pre-release targetSdkVersion on pre-release platform.
169         // APP: Pre-release API 10
170         // DEV: Pre-release API 20
171         verifyComputeTargetSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, false, -1);
172 
173         // Do allow same pre-release targetSdkVersion on pre-release platform,
174         // but overwrite the specified version with CUR_DEVELOPMENT.
175         // APP: Pre-release API 20
176         // DEV: Pre-release API 20
177         verifyComputeTargetSdkVersion(PLATFORM_VERSION, PRE_RELEASE, false,
178                 Build.VERSION_CODES.CUR_DEVELOPMENT);
179 
180         // Don't allow newer pre-release targetSdkVersion on pre-release platform.
181         // APP: Pre-release API 30
182         // DEV: Pre-release API 20
183         verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, false, -1);
184     }
185 
186     @Test
testComputeTargetSdkVersion_releasedPlatform()187     public void testComputeTargetSdkVersion_releasedPlatform() {
188         // Do allow older release targetSdkVersion on released platform.
189         // APP: Released API 10
190         // DEV: Released API 20
191         verifyComputeTargetSdkVersion(OLDER_VERSION, RELEASED, true, OLDER_VERSION);
192 
193         // Do allow same release targetSdkVersion on released platform.
194         // APP: Released API 20
195         // DEV: Released API 20
196         verifyComputeTargetSdkVersion(PLATFORM_VERSION, RELEASED, true, PLATFORM_VERSION);
197 
198         // Do allow newer release targetSdkVersion on released platform.
199         // APP: Released API 30
200         // DEV: Released API 20
201         verifyComputeTargetSdkVersion(NEWER_VERSION, RELEASED, true, NEWER_VERSION);
202 
203         // Don't allow older pre-release targetSdkVersion on released platform.
204         // APP: Pre-release API 10
205         // DEV: Released API 20
206         verifyComputeTargetSdkVersion(OLDER_VERSION, OLDER_PRE_RELEASE, true, -1);
207 
208         // Don't allow same pre-release targetSdkVersion on released platform.
209         // APP: Pre-release API 20
210         // DEV: Released API 20
211         verifyComputeTargetSdkVersion(PLATFORM_VERSION, PRE_RELEASE, true, -1);
212 
213         // Don't allow newer pre-release targetSdkVersion on released platform.
214         // APP: Pre-release API 30
215         // DEV: Released API 20
216         verifyComputeTargetSdkVersion(NEWER_VERSION, NEWER_PRE_RELEASE, true, -1);
217     }
218 
219     /**
220      * Unit test for PackageParser.getActivityConfigChanges().
221      * If the bit is 1 in the original configChanges, it is still 1 in the final configChanges.
222      * If the bit is 0 in the original configChanges and the bit is not set to 1 in
223      * recreateOnConfigChanges, the bit is changed to 1 in the final configChanges by default.
224      */
225     @Test
testGetActivityConfigChanges()226     public void testGetActivityConfigChanges() {
227         // Not set in either configChanges or recreateOnConfigChanges.
228         int configChanges = 0x0000; // 00000000.
229         int recreateOnConfigChanges = 0x0000; // 00000000.
230         int finalConfigChanges =
231                 PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges);
232         assertEquals(0x0003, finalConfigChanges); // Should be 00000011.
233 
234         // Not set in configChanges, but set in recreateOnConfigChanges.
235         configChanges = 0x0000; // 00000000.
236         recreateOnConfigChanges = 0x0003; // 00000011.
237         finalConfigChanges =
238                 PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges);
239         assertEquals(0x0000, finalConfigChanges); // Should be 00000000.
240 
241         // Set in configChanges.
242         configChanges = 0x0003; // 00000011.
243         recreateOnConfigChanges = 0X0000; // 00000000.
244         finalConfigChanges =
245                 PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges);
246         assertEquals(0x0003, finalConfigChanges); // Should be 00000011.
247 
248         recreateOnConfigChanges = 0x0003; // 00000011.
249         finalConfigChanges =
250                 PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges);
251         assertEquals(0x0003, finalConfigChanges); // Should still be 00000011.
252 
253         // Other bit set in configChanges.
254         configChanges = 0x0080; // 10000000, orientation.
255         recreateOnConfigChanges = 0x0000; // 00000000.
256         finalConfigChanges =
257                 PackageParser.getActivityConfigChanges(configChanges, recreateOnConfigChanges);
258         assertEquals(0x0083, finalConfigChanges); // Should be 10000011.
259     }
260 }
261