blob: f18f20973ac8d69dec967aa09e31ab54f3d01838 [file] [log] [blame]
Alan Viverettee48ddab2021-05-25 14:08:49 -04001/*
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
19package androidx.build.lint
20
21import com.android.tools.lint.checks.infrastructure.LintDetectorTest
22import com.android.tools.lint.checks.infrastructure.TestFile
Tiem Song91a2d702023-01-19 11:14:42 -080023import com.android.tools.lint.checks.infrastructure.TestLintTask
Alan Viverettee48ddab2021-05-25 14:08:49 -040024import com.android.tools.lint.detector.api.Detector
25import com.android.tools.lint.detector.api.Issue
Alan Viverettee48ddab2021-05-25 14:08:49 -040026import org.junit.Test
27
28class TargetApiAnnotationUsageDetectorTest : LintDetectorTest() {
29 override fun getDetector(): Detector = TargetApiAnnotationUsageDetector()
30
Omar Ismailedf734f2024-05-28 12:34:57 +010031 override fun getIssues(): List<Issue> = listOf(TargetApiAnnotationUsageDetector.ISSUE)
Alan Viverettee48ddab2021-05-25 14:08:49 -040032
Tiem Song91a2d702023-01-19 11:14:42 -080033 private fun checkTask(testFile: TestFile): TestLintTask {
Omar Ismailedf734f2024-05-28 12:34:57 +010034 return lint().files(java(annotationSource), testFile)
Alan Viverettee48ddab2021-05-25 14:08:49 -040035 }
36
Omar Ismailedf734f2024-05-28 12:34:57 +010037 private val annotationSource =
38 """
Alan Viverettee48ddab2021-05-25 14:08:49 -040039package android.annotation;
40
41import static java.lang.annotation.ElementType.CONSTRUCTOR;
42import static java.lang.annotation.ElementType.METHOD;
43import static java.lang.annotation.ElementType.TYPE;
44
45import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
47import java.lang.annotation.Target;
48
49@Target({TYPE, METHOD, CONSTRUCTOR})
50@Retention(RetentionPolicy.CLASS)
51public @interface TargetApi {
52 int value();
53}
Omar Ismailedf734f2024-05-28 12:34:57 +010054 """
55 .trimIndent()
Alan Viverettee48ddab2021-05-25 14:08:49 -040056
57 @Test
58 fun testAnnotationUsageJava() {
Omar Ismailedf734f2024-05-28 12:34:57 +010059 val input =
60 java(
61 """
Alan Viverettee48ddab2021-05-25 14:08:49 -040062package androidx.sample;
63
64import android.annotation.TargetApi;
65
66@TargetApi(24)
67public class SampleClass {
68 @TargetApi(15)
69 public void method() {
70 // Stub
71 }
72}
Omar Ismailedf734f2024-05-28 12:34:57 +010073 """
74 .trimIndent()
75 )
Alan Viverettee48ddab2021-05-25 14:08:49 -040076
Omar Ismailedf734f2024-05-28 12:34:57 +010077 val expected =
78 """
Alan Viverettee48ddab2021-05-25 14:08:49 -040079src/androidx/sample/SampleClass.java:5: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
80@TargetApi(24)
81~~~~~~~~~~~~~~
82src/androidx/sample/SampleClass.java:7: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
83 @TargetApi(15)
84 ~~~~~~~~~~~~~~
852 errors, 0 warnings
Omar Ismailedf734f2024-05-28 12:34:57 +010086 """
87 .trimIndent()
Alan Viverettee48ddab2021-05-25 14:08:49 -040088
Omar Ismailedf734f2024-05-28 12:34:57 +010089 val expectFixDiffs =
90 """
Alan Viverettee48ddab2021-05-25 14:08:49 -040091Fix for src/androidx/sample/SampleClass.java line 5: Replace with `@RequiresApi`:
92@@ -5 +5
93- @TargetApi(24)
94+ @androidx.annotation.RequiresApi(24)
95Fix for src/androidx/sample/SampleClass.java line 7: Replace with `@RequiresApi`:
96@@ -7 +7
97- @TargetApi(15)
98+ @androidx.annotation.RequiresApi(15)
Omar Ismailedf734f2024-05-28 12:34:57 +010099 """
100 .trimIndent()
Alan Viverettee48ddab2021-05-25 14:08:49 -0400101
Omar Ismailedf734f2024-05-28 12:34:57 +0100102 checkTask(input).run().expect(expected).expectFixDiffs(expectFixDiffs)
Alan Viverettee48ddab2021-05-25 14:08:49 -0400103 }
104
105 @Test
106 fun testAnnotationUsageKt() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100107 val input =
108 kotlin(
109 """
Alan Viverettee48ddab2021-05-25 14:08:49 -0400110package androidx.sample
111
112import android.annotation.TargetApi
113
114@TargetApi(24)
115class SampleClass {
116 @TargetApi(15)
117 fun method() {
118 // Stub
119 }
120}
Omar Ismailedf734f2024-05-28 12:34:57 +0100121 """
122 .trimIndent()
123 )
Alan Viverettee48ddab2021-05-25 14:08:49 -0400124
Omar Ismailedf734f2024-05-28 12:34:57 +0100125 val expected =
126 """
Alan Viverettee48ddab2021-05-25 14:08:49 -0400127src/androidx/sample/SampleClass.kt:5: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
128@TargetApi(24)
129~~~~~~~~~~~~~~
130src/androidx/sample/SampleClass.kt:7: Error: Use @RequiresApi instead of @TargetApi [BanTargetApiAnnotation]
131 @TargetApi(15)
132 ~~~~~~~~~~~~~~
1332 errors, 0 warnings
Omar Ismailedf734f2024-05-28 12:34:57 +0100134 """
135 .trimIndent()
Alan Viverettee48ddab2021-05-25 14:08:49 -0400136
Omar Ismailedf734f2024-05-28 12:34:57 +0100137 val expectFixDiffs =
138 """
Alan Viverettee48ddab2021-05-25 14:08:49 -0400139Fix for src/androidx/sample/SampleClass.kt line 5: Replace with `@RequiresApi`:
140@@ -5 +5
141- @TargetApi(24)
142+ @androidx.annotation.RequiresApi(24)
143Fix for src/androidx/sample/SampleClass.kt line 7: Replace with `@RequiresApi`:
144@@ -7 +7
145- @TargetApi(15)
146+ @androidx.annotation.RequiresApi(15)
Omar Ismailedf734f2024-05-28 12:34:57 +0100147 """
148 .trimIndent()
Alan Viverettee48ddab2021-05-25 14:08:49 -0400149
Omar Ismailedf734f2024-05-28 12:34:57 +0100150 checkTask(input).run().expect(expected).expectFixDiffs(expectFixDiffs)
Alan Viverettee48ddab2021-05-25 14:08:49 -0400151 }
152}