1 /*
2  * Copyright 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 @file:Suppress("UnstableApiUsage")
18 
19 package androidx.build.lint
20 
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.junit.runners.JUnit4
24 
25 @RunWith(JUnit4::class)
26 class TargetApiAnnotationDetectorTest :
27     AbstractLintDetectorTest(
28         useDetector = TargetApiAnnotationUsageDetector(),
29         useIssues = listOf(TargetApiAnnotationUsageDetector.ISSUE),
30     ) {
31 
32     @Test
Detection of TargetApi usage in Java sourcesnull33     fun `Detection of TargetApi usage in Java sources`() {
34         val input =
35             arrayOf(
36                 javaSample("androidx.TargetApiUsageJava"),
37             )
38 
39         val expected =
40             """
41 src/androidx/TargetApiUsageJava.java:22: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
42 @TargetApi(29)
43 ~~~~~~~~~~~~~~
44 src/androidx/TargetApiUsageJava.java:25: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
45     @TargetApi(30)
46     ~~~~~~~~~~~~~~
47 2 errors, 0 warnings
48         """
49                 .trimIndent()
50 
51         check(*input).expect(expected)
52     }
53 
54     @Test
Detection of TargetApi usage in Kotlin sourcesnull55     fun `Detection of TargetApi usage in Kotlin sources`() {
56         val input =
57             arrayOf(
58                 ktSample("androidx.TargetApiUsageKotlin"),
59             )
60 
61         val expected =
62             """
63 src/androidx/TargetApiUsageKotlin.kt:22: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
64 @TargetApi(29)
65 ~~~~~~~~~~~~~~
66 src/androidx/TargetApiUsageKotlin.kt:25: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
67     @TargetApi(30) fun someMethod() {}
68     ~~~~~~~~~~~~~~
69 2 errors, 0 warnings
70         """
71                 .trimIndent()
72 
73         check(*input).expect(expected)
74     }
75 }
76