1 /*
2  * Copyright (C) 2021 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 com.android.modules.utils.build;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.util.SparseArray;
24 
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.util.Set;
31 
32 @RunWith(AndroidJUnit4.class)
33 public class UnboundedSdkLevelTest {
34     private static final SparseArray<Set<String>> PREVIOUS_CODENAMES = new SparseArray<>();
35 
36     static {
37         PREVIOUS_CODENAMES.put(29, Set.of("Q"));
38         PREVIOUS_CODENAMES.put(30, Set.of("Q", "R"));
39         PREVIOUS_CODENAMES.put(31, Set.of("Q", "R", "S"));
40         PREVIOUS_CODENAMES.put(32, Set.of("Q", "R", "S", "Sv2"));
41     }
42 
43     @Test
testFinalizedSdk_S()44     public void testFinalizedSdk_S() {
45         // Test against finalized S / 31 release
46         UnboundedSdkLevel sdkLevel = new UnboundedSdkLevel(31, "REL", PREVIOUS_CODENAMES.get(31));
47 
48         assertThat(sdkLevel.isAtLeastInternal("30")).isTrue();
49         assertThat(sdkLevel.isAtLeastInternal("31")).isTrue();
50         assertThat(sdkLevel.isAtLeastInternal("32")).isFalse();
51 
52         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal("R"));
53         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal("S"));
54         assertThat(sdkLevel.isAtLeastInternal("Sv2")).isFalse();
55         assertThat(sdkLevel.isAtLeastInternal("Tiramisu")).isFalse();
56         assertThat(sdkLevel.isAtLeastInternal("U")).isFalse();
57 
58         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal(""));
59         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal("current"));
60 
61         assertThat(sdkLevel.isAtMostInternal("30")).isFalse();
62         assertThat(sdkLevel.isAtMostInternal("31")).isTrue();
63         assertThat(sdkLevel.isAtMostInternal("32")).isTrue();
64 
65         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal("R"));
66         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal("S"));
67         assertThat(sdkLevel.isAtMostInternal("Sv2")).isTrue();
68         assertThat(sdkLevel.isAtMostInternal("Tiramisu")).isTrue();
69         assertThat(sdkLevel.isAtMostInternal("U")).isTrue();
70 
71         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal(""));
72         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal("current"));
73     }
74 
75     @Test
testNonFinalizedSdk_Sv2()76     public void testNonFinalizedSdk_Sv2() {
77         UnboundedSdkLevel sdkLevel = new UnboundedSdkLevel(31, "Sv2", Set.of("Q", "R", "S", "Sv2"));
78 
79         assertThat(sdkLevel.isAtLeastInternal("30")).isTrue();
80         assertThat(sdkLevel.isAtLeastInternal("31")).isTrue();
81         assertThat(sdkLevel.isAtLeastInternal("32")).isFalse();
82 
83         assertThat(sdkLevel.isAtLeastInternal("R")).isTrue();
84         assertThat(sdkLevel.isAtLeastInternal("S")).isTrue();
85         assertThat(sdkLevel.isAtLeastInternal("Sv2")).isTrue();
86         assertThat(sdkLevel.isAtLeastInternal("Tiramisu")).isFalse();
87         assertThat(sdkLevel.isAtLeastInternal("U")).isFalse();
88 
89         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal(""));
90         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal("current"));
91 
92         assertThat(sdkLevel.isAtMostInternal("30")).isFalse();
93         assertThat(sdkLevel.isAtMostInternal("31")).isFalse();
94         assertThat(sdkLevel.isAtMostInternal("32")).isTrue();
95 
96         assertThat(sdkLevel.isAtMostInternal("R")).isFalse();
97         assertThat(sdkLevel.isAtMostInternal("S")).isFalse();
98         assertThat(sdkLevel.isAtMostInternal("Sv2")).isTrue();
99         assertThat(sdkLevel.isAtMostInternal("Tiramisu")).isTrue();
100         assertThat(sdkLevel.isAtMostInternal("U")).isTrue();
101 
102         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal(""));
103         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal("current"));
104     }
105 
106     @Test
testNonFinalizedSdk_Tiramisu()107     public void testNonFinalizedSdk_Tiramisu() {
108         UnboundedSdkLevel sdkLevel = new UnboundedSdkLevel(31, "Tiramisu",
109                 Set.of("Q", "R", "S", "Sv2", "Tiramisu"));
110 
111         assertThat(sdkLevel.isAtLeastInternal("30")).isTrue();
112         assertThat(sdkLevel.isAtLeastInternal("31")).isTrue();
113         assertThat(sdkLevel.isAtLeastInternal("32")).isFalse();
114 
115         assertThat(sdkLevel.isAtLeastInternal("R")).isTrue();
116         assertThat(sdkLevel.isAtLeastInternal("S")).isTrue();
117         assertThat(sdkLevel.isAtLeastInternal("Sv2")).isTrue();
118         assertThat(sdkLevel.isAtLeastInternal("Tiramisu")).isTrue();
119         assertThat(sdkLevel.isAtLeastInternal("U")).isFalse();
120 
121         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal(""));
122         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtLeastInternal("current"));
123 
124         assertThat(sdkLevel.isAtMostInternal("30")).isFalse();
125         assertThat(sdkLevel.isAtMostInternal("31")).isFalse();
126         assertThat(sdkLevel.isAtMostInternal("32")).isTrue();
127 
128         assertThat(sdkLevel.isAtMostInternal("R")).isFalse();
129         assertThat(sdkLevel.isAtMostInternal("S")).isFalse();
130         assertThat(sdkLevel.isAtMostInternal("Sv2")).isFalse();
131         assertThat(sdkLevel.isAtMostInternal("Tiramisu")).isTrue();
132         assertThat(sdkLevel.isAtMostInternal("U")).isTrue();
133 
134         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal(""));
135         assertThrows(IllegalArgumentException.class, () -> sdkLevel.isAtMostInternal("current"));
136     }
137 
138     @Test
testLegacySdkR()139     public void testLegacySdkR() {
140         UnboundedSdkLevel sdkLevelR = new UnboundedSdkLevel(30, "REL",
141                 PREVIOUS_CODENAMES.get(30));
142         assertThat(sdkLevelR.isAtLeastInternal("29")).isTrue();
143         assertThat(sdkLevelR.isAtLeastInternal("30")).isTrue();
144         assertThat(sdkLevelR.isAtLeastInternal("31")).isFalse();
145 
146         assertThrows(IllegalArgumentException.class,
147                 () -> assertThat(sdkLevelR.isAtLeastInternal("Q")));
148         assertThrows(IllegalArgumentException.class,
149                 () -> assertThat(sdkLevelR.isAtLeastInternal("R")));
150         assertThat(sdkLevelR.isAtLeastInternal("S")).isFalse();
151         assertThat(sdkLevelR.isAtLeastInternal("Sv2")).isFalse();
152         assertThat(sdkLevelR.isAtLeastInternal("Tiramisu")).isFalse();
153 
154         assertThat(sdkLevelR.isAtMostInternal("29")).isFalse();
155         assertThat(sdkLevelR.isAtMostInternal("30")).isTrue();
156         assertThat(sdkLevelR.isAtMostInternal("31")).isTrue();
157 
158         assertThrows(IllegalArgumentException.class,
159                 () -> assertThat(sdkLevelR.isAtMostInternal("Q")));
160         assertThrows(IllegalArgumentException.class,
161                 () -> assertThat(sdkLevelR.isAtMostInternal("R")));
162         assertThat(sdkLevelR.isAtMostInternal("S")).isTrue();
163         assertThat(sdkLevelR.isAtMostInternal("Sv2")).isTrue();
164         assertThat(sdkLevelR.isAtMostInternal("Tiramisu")).isTrue();
165 
166         assertThrows(IllegalArgumentException.class, () -> sdkLevelR.isAtMostInternal(""));
167         assertThrows(IllegalArgumentException.class, () -> sdkLevelR.isAtMostInternal("current"));
168     }
169 
170     @Test
testLegacySdkQ()171     public void testLegacySdkQ() {
172         UnboundedSdkLevel sdkLevelQ = new UnboundedSdkLevel(29, "REL",
173                 PREVIOUS_CODENAMES.get(29));
174 
175         assertThat(sdkLevelQ.isAtLeastInternal("28")).isTrue();
176         assertThat(sdkLevelQ.isAtLeastInternal("29")).isTrue();
177         assertThat(sdkLevelQ.isAtLeastInternal("30")).isFalse();
178         assertThat(sdkLevelQ.isAtLeastInternal("31")).isFalse();
179 
180         assertThrows(IllegalArgumentException.class,
181                 () -> assertThat(sdkLevelQ.isAtLeastInternal("Q")));
182         assertThat(sdkLevelQ.isAtLeastInternal("R")).isFalse();
183         assertThat(sdkLevelQ.isAtLeastInternal("S")).isFalse();
184         assertThat(sdkLevelQ.isAtLeastInternal("Sv2")).isFalse();
185         assertThat(sdkLevelQ.isAtLeastInternal("Tiramisu")).isFalse();
186 
187         assertThat(sdkLevelQ.isAtMostInternal("28")).isFalse();
188         assertThat(sdkLevelQ.isAtMostInternal("29")).isTrue();
189         assertThat(sdkLevelQ.isAtMostInternal("30")).isTrue();
190         assertThat(sdkLevelQ.isAtMostInternal("31")).isTrue();
191 
192         assertThrows(IllegalArgumentException.class,
193                 () -> assertThat(sdkLevelQ.isAtMostInternal("Q")));
194         assertThat(sdkLevelQ.isAtMostInternal("R")).isTrue();
195         assertThat(sdkLevelQ.isAtMostInternal("S")).isTrue();
196         assertThat(sdkLevelQ.isAtMostInternal("Sv2")).isTrue();
197         assertThat(sdkLevelQ.isAtMostInternal("Tiramisu")).isTrue();
198 
199         assertThrows(IllegalArgumentException.class, () -> sdkLevelQ.isAtMostInternal(""));
200         assertThrows(IllegalArgumentException.class, () -> sdkLevelQ.isAtMostInternal("current"));
201     }
202 
203     @Test
testCodenamesWithFingerprint()204     public void testCodenamesWithFingerprint() {
205         UnboundedSdkLevel sdkLevel = new UnboundedSdkLevel(30, "R",
206             PREVIOUS_CODENAMES.get(30));
207 
208         assertThat(sdkLevel.isAtMostInternal("Q")).isFalse();
209         assertThat(sdkLevel.isAtMostInternal("Tiramisu")).isTrue();
210         assertThat(sdkLevel.isAtLeastInternal("Q")).isTrue();
211         assertThat(sdkLevel.isAtLeastInternal("Tiramisu")).isFalse();
212         assertThat(sdkLevel.isAtMostInternal("Q.fingerprint")).isFalse();
213         assertThat(sdkLevel.isAtMostInternal("Tiramisu.fingerprint")).isTrue();
214         assertThat(sdkLevel.isAtLeastInternal("Q.fingerprint")).isTrue();
215         assertThat(sdkLevel.isAtLeastInternal("Tiramisu.fingerprint")).isFalse();
216     }
217 }
218