1 /* 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 17 @file:Suppress("UnstableApiUsage") 18 19 package androidx.build.lint 20 21 import org.junit.Ignore 22 import org.junit.Test 23 import org.junit.runner.RunWith 24 import org.junit.runners.JUnit4 25 26 @RunWith(JUnit4::class) 27 class ObsoleteBuildCompatUsageDetectorTest : 28 AbstractLintDetectorTest( 29 useDetector = ObsoleteBuildCompatUsageDetector(), 30 useIssues = listOf(ObsoleteBuildCompatUsageDetector.ISSUE), 31 stubs = arrayOf(BuildCompat), 32 ) { 33 34 @Test 35 @Ignore("ANDROID_HOME not available on CI") isAtLeastNnull36 fun isAtLeastN() { 37 val input = 38 java( 39 """ 40 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 } 49 """ 50 .trimIndent() 51 ) 52 53 val expected = 54 """ 55 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 56 if (BuildCompat.isAtLeastN()) { 57 ~~~~~~~~~~~~~~~~~~~~~~~~ 58 1 errors, 0 warnings 59 """ 60 61 val expectedDiff = 62 """ 63 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 """ 68 69 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 70 } 71 72 @Test 73 @Ignore("ANDROID_HOME not available on CI") isAtLeastNStaticImportnull74 fun isAtLeastNStaticImport() { 75 val input = 76 java( 77 """ 78 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 } 87 """ 88 .trimIndent() 89 ) 90 91 val expected = 92 """ 93 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 94 if (isAtLeastN()) { 95 ~~~~~~~~~~~~ 96 1 errors, 0 warnings 97 """ 98 99 val expectedDiff = 100 """ 101 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 """ 106 107 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 108 } 109 110 @Test 111 @Ignore("ANDROID_HOME not available on CI") isAtLeastNMR1null112 fun isAtLeastNMR1() { 113 val input = 114 java( 115 """ 116 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 } 125 """ 126 .trimIndent() 127 ) 128 129 val expected = 130 """ 131 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 132 if (BuildCompat.isAtLeastNMR1()) { 133 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 134 1 errors, 0 warnings 135 """ 136 137 val expectedDiff = 138 """ 139 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 """ 144 145 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 146 } 147 148 @Test 149 @Ignore("ANDROID_HOME not available on CI") isAtLeastOnull150 fun isAtLeastO() { 151 val input = 152 java( 153 """ 154 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 } 163 """ 164 .trimIndent() 165 ) 166 167 val expected = 168 """ 169 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 170 if (BuildCompat.isAtLeastO()) { 171 ~~~~~~~~~~~~~~~~~~~~~~~~ 172 1 errors, 0 warnings 173 """ 174 175 val expectedDiff = 176 """ 177 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 """ 182 183 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 184 } 185 186 @Test 187 @Ignore("ANDROID_HOME not available on CI") isAtLeastOMR1null188 fun isAtLeastOMR1() { 189 val input = 190 java( 191 """ 192 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 } 201 """ 202 .trimIndent() 203 ) 204 205 val expected = 206 """ 207 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 208 if (BuildCompat.isAtLeastOMR1()) { 209 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 210 1 errors, 0 warnings 211 """ 212 213 val expectedDiff = 214 """ 215 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 """ 220 221 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 222 } 223 224 @Test 225 @Ignore("ANDROID_HOME not available on CI") isAtLeastPnull226 fun isAtLeastP() { 227 val input = 228 java( 229 """ 230 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 } 239 """ 240 .trimIndent() 241 ) 242 243 val expected = 244 """ 245 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 246 if (BuildCompat.isAtLeastP()) { 247 ~~~~~~~~~~~~~~~~~~~~~~~~ 248 1 errors, 0 warnings 249 """ 250 251 val expectedDiff = 252 """ 253 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 """ 258 259 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 260 } 261 262 @Test 263 @Ignore("ANDROID_HOME not available on CI") isAtLeastQnull264 fun isAtLeastQ() { 265 val input = 266 java( 267 """ 268 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 } 277 """ 278 .trimIndent() 279 ) 280 281 val expected = 282 """ 283 src/foo/Example.java:5: Error: Using deprecated BuildCompat methods [ObsoleteBuildCompat] 284 if (BuildCompat.isAtLeastQ()) { 285 ~~~~~~~~~~~~~~~~~~~~~~~~ 286 1 errors, 0 warnings 287 """ 288 289 val expectedDiff = 290 """ 291 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 """ 296 297 check(input).expect(expected.trimIndent()).expectFixDiffs(expectedDiff.trimIndent()) 298 } 299 300 companion object { 301 private val BuildCompat = 302 java( 303 """ 304 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 } 313 """ 314 .trimIndent() 315 ) 316 } 317 } 318