blob: 2b077f9f44f0474d0198b2837094b7e15772be6e [file] [log] [blame]
Jake Whartona0576c32018-07-27 13:09:42 -04001/*
2 * Copyright 2018 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
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040017@file:Suppress("UnstableApiUsage")
Jake Whartona0576c32018-07-27 13:09:42 -040018
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040019package androidx.build.lint
Jeff Gaston8fd9fc82019-07-26 14:26:10 -040020
Jake Whartona0576c32018-07-27 13:09:42 -040021import org.junit.Ignore
22import org.junit.Test
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040023import org.junit.runner.RunWith
24import org.junit.runners.JUnit4
Jake Whartona0576c32018-07-27 13:09:42 -040025
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040026@RunWith(JUnit4::class)
Omar Ismailedf734f2024-05-28 12:34:57 +010027class ObsoleteBuildCompatUsageDetectorTest :
28 AbstractLintDetectorTest(
29 useDetector = ObsoleteBuildCompatUsageDetector(),
30 useIssues = listOf(ObsoleteBuildCompatUsageDetector.ISSUE),
31 stubs = arrayOf(BuildCompat),
32 ) {
Jake Whartona0576c32018-07-27 13:09:42 -040033
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040034 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -040035 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040036 fun isAtLeastN() {
Omar Ismailedf734f2024-05-28 12:34:57 +010037 val input =
38 java(
39 """
Jake Whartona0576c32018-07-27 13:09:42 -040040 package foo;
41 import androidx.core.os.BuildCompat;
42 public class Example {
43 public static void main(String... args) {
44 if (BuildCompat.isAtLeastN()) {
45 System.out.println("Hey");
46 }
47 }
48 }
Omar Ismailedf734f2024-05-28 12:34:57 +010049 """
50 .trimIndent()
51 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040052
Omar Ismailedf734f2024-05-28 12:34:57 +010053 val expected =
54 """
Jake Whartona0576c32018-07-27 13:09:42 -040055 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
56 if (BuildCompat.isAtLeastN()) {
57 ~~~~~~~~~~~~~~~~~~~~~~~~
58 1 errors, 0 warnings
59 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040060
Omar Ismailedf734f2024-05-28 12:34:57 +010061 val expectedDiff =
62 """
Jake Whartona0576c32018-07-27 13:09:42 -040063 Fix for src/foo/Example.java line 5: Use SDK_INT >= 24:
64 @@ -5 +5
65 - if (BuildCompat.isAtLeastN()) {
66 + if (Build.VERSION.SDK_INT >= 24) {
67 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040068
Omar Ismailedf734f2024-05-28 12:34:57 +010069 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -040070 }
71
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040072 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -040073 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040074 fun isAtLeastNStaticImport() {
Omar Ismailedf734f2024-05-28 12:34:57 +010075 val input =
76 java(
77 """
Jake Whartona0576c32018-07-27 13:09:42 -040078 package foo;
79 import static androidx.core.os.BuildCompat.isAtLeastN;
80 public class Example {
81 public static void main(String... args) {
82 if (isAtLeastN()) {
83 System.out.println("Hey");
84 }
85 }
86 }
Omar Ismailedf734f2024-05-28 12:34:57 +010087 """
88 .trimIndent()
89 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040090
Omar Ismailedf734f2024-05-28 12:34:57 +010091 val expected =
92 """
Jake Whartona0576c32018-07-27 13:09:42 -040093 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
94 if (isAtLeastN()) {
95 ~~~~~~~~~~~~
96 1 errors, 0 warnings
97 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -040098
Omar Ismailedf734f2024-05-28 12:34:57 +010099 val expectedDiff =
100 """
Jake Whartona0576c32018-07-27 13:09:42 -0400101 Fix for src/foo/Example.java line 5: Use SDK_INT >= 24:
102 @@ -5 +5
103 - if (isAtLeastN()) {
104 + if (Build.VERSION.SDK_INT >= 24) {
105 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400106
Omar Ismailedf734f2024-05-28 12:34:57 +0100107 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -0400108 }
109
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400110 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -0400111 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400112 fun isAtLeastNMR1() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100113 val input =
114 java(
115 """
Jake Whartona0576c32018-07-27 13:09:42 -0400116 package foo;
117 import androidx.core.os.BuildCompat;
118 public class Example {
119 public static void main(String... args) {
120 if (BuildCompat.isAtLeastNMR1()) {
121 System.out.println("Hey");
122 }
123 }
124 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100125 """
126 .trimIndent()
127 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400128
Omar Ismailedf734f2024-05-28 12:34:57 +0100129 val expected =
130 """
Jake Whartona0576c32018-07-27 13:09:42 -0400131 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
132 if (BuildCompat.isAtLeastNMR1()) {
133 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
134 1 errors, 0 warnings
135 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400136
Omar Ismailedf734f2024-05-28 12:34:57 +0100137 val expectedDiff =
138 """
Jake Whartona0576c32018-07-27 13:09:42 -0400139 Fix for src/foo/Example.java line 5: Use SDK_INT >= 25:
140 @@ -5 +5
141 - if (BuildCompat.isAtLeastNMR1()) {
142 + if (Build.VERSION.SDK_INT >= 25) {
143 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400144
Omar Ismailedf734f2024-05-28 12:34:57 +0100145 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -0400146 }
147
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400148 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -0400149 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400150 fun isAtLeastO() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100151 val input =
152 java(
153 """
Jake Whartona0576c32018-07-27 13:09:42 -0400154 package foo;
155 import androidx.core.os.BuildCompat;
156 public class Example {
157 public static void main(String... args) {
158 if (BuildCompat.isAtLeastO()) {
159 System.out.println("Hey");
160 }
161 }
162 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100163 """
164 .trimIndent()
165 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400166
Omar Ismailedf734f2024-05-28 12:34:57 +0100167 val expected =
168 """
Jake Whartona0576c32018-07-27 13:09:42 -0400169 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
170 if (BuildCompat.isAtLeastO()) {
171 ~~~~~~~~~~~~~~~~~~~~~~~~
172 1 errors, 0 warnings
173 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400174
Omar Ismailedf734f2024-05-28 12:34:57 +0100175 val expectedDiff =
176 """
Jake Whartona0576c32018-07-27 13:09:42 -0400177 Fix for src/foo/Example.java line 5: Use SDK_INT >= 26:
178 @@ -5 +5
179 - if (BuildCompat.isAtLeastO()) {
180 + if (Build.VERSION.SDK_INT >= 26) {
181 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400182
Omar Ismailedf734f2024-05-28 12:34:57 +0100183 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -0400184 }
185
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400186 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -0400187 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400188 fun isAtLeastOMR1() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100189 val input =
190 java(
191 """
Jake Whartona0576c32018-07-27 13:09:42 -0400192 package foo;
193 import androidx.core.os.BuildCompat;
194 public class Example {
195 public static void main(String... args) {
196 if (BuildCompat.isAtLeastOMR1()) {
197 System.out.println("Hey");
198 }
199 }
200 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100201 """
202 .trimIndent()
203 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400204
Omar Ismailedf734f2024-05-28 12:34:57 +0100205 val expected =
206 """
Jake Whartona0576c32018-07-27 13:09:42 -0400207 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
208 if (BuildCompat.isAtLeastOMR1()) {
209 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
210 1 errors, 0 warnings
211 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400212
Omar Ismailedf734f2024-05-28 12:34:57 +0100213 val expectedDiff =
214 """
Jake Whartona0576c32018-07-27 13:09:42 -0400215 Fix for src/foo/Example.java line 5: Use SDK_INT >= 27:
216 @@ -5 +5
217 - if (BuildCompat.isAtLeastOMR1()) {
218 + if (Build.VERSION.SDK_INT >= 27) {
219 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400220
Omar Ismailedf734f2024-05-28 12:34:57 +0100221 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -0400222 }
223
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400224 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -0400225 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400226 fun isAtLeastP() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100227 val input =
228 java(
229 """
Jake Whartona0576c32018-07-27 13:09:42 -0400230 package foo;
231 import androidx.core.os.BuildCompat;
232 public class Example {
233 public static void main(String... args) {
234 if (BuildCompat.isAtLeastP()) {
235 System.out.println("Hey");
236 }
237 }
238 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100239 """
240 .trimIndent()
241 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400242
Omar Ismailedf734f2024-05-28 12:34:57 +0100243 val expected =
244 """
Jake Whartona0576c32018-07-27 13:09:42 -0400245 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
246 if (BuildCompat.isAtLeastP()) {
247 ~~~~~~~~~~~~~~~~~~~~~~~~
248 1 errors, 0 warnings
249 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400250
Omar Ismailedf734f2024-05-28 12:34:57 +0100251 val expectedDiff =
252 """
Jake Whartona0576c32018-07-27 13:09:42 -0400253 Fix for src/foo/Example.java line 5: Use SDK_INT >= 28:
254 @@ -5 +5
255 - if (BuildCompat.isAtLeastP()) {
256 + if (Build.VERSION.SDK_INT >= 28) {
257 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400258
Omar Ismailedf734f2024-05-28 12:34:57 +0100259 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -0400260 }
261
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400262 @Test
Julia McClellanf2f07aa2022-06-09 10:08:09 -0400263 @Ignore("ANDROID_HOME not available on CI")
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400264 fun isAtLeastQ() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100265 val input =
266 java(
267 """
Jake Whartona0576c32018-07-27 13:09:42 -0400268 package foo;
269 import androidx.core.os.BuildCompat;
270 public class Example {
271 public static void main(String... args) {
272 if (BuildCompat.isAtLeastQ()) {
273 System.out.println("Hey");
274 }
275 }
276 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100277 """
278 .trimIndent()
279 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400280
Omar Ismailedf734f2024-05-28 12:34:57 +0100281 val expected =
282 """
Clara Bayarrid20f2c12019-04-29 15:04:04 +0100283 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat]
284 if (BuildCompat.isAtLeastQ()) {
285 ~~~~~~~~~~~~~~~~~~~~~~~~
286 1 errors, 0 warnings
287 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400288
Omar Ismailedf734f2024-05-28 12:34:57 +0100289 val expectedDiff =
290 """
Clara Bayarrid20f2c12019-04-29 15:04:04 +0100291 Fix for src/foo/Example.java line 5: Use SDK_INT >= 29:
292 @@ -5 +5
293 - if (BuildCompat.isAtLeastQ()) {
294 + if (Build.VERSION.SDK_INT >= 29) {
295 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400296
Omar Ismailedf734f2024-05-28 12:34:57 +0100297 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent())
Jake Whartona0576c32018-07-27 13:09:42 -0400298 }
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400299
300 companion object {
Omar Ismailedf734f2024-05-28 12:34:57 +0100301 private val BuildCompat =
302 java(
303 """
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400304 package androidx.core.os;
305 public class BuildCompat {
306 public static boolean isAtLeastN() { return false; }
307 public static boolean isAtLeastNMR1() { return false; }
308 public static boolean isAtLeastO() { return false; }
309 public static boolean isAtLeastOMR1() { return false; }
310 public static boolean isAtLeastP() { return false; }
311 public static boolean isAtLeastQ() { return false; }
312 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100313 """
314 .trimIndent()
315 )
Alan Viveretteb71ec7b2021-05-13 17:37:57 -0400316 }
Jake Whartona0576c32018-07-27 13:09:42 -0400317}