1 /*
2  * Copyright 2019 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 androidx.build.lint.replacewith
18 
19 import org.junit.Test
20 import org.junit.runner.RunWith
21 import org.junit.runners.JUnit4
22 
23 @RunWith(JUnit4::class)
24 class ReplaceWithDetectorFieldTest {
25 
26     @Test
staticFieldExplicitClassnull27     fun staticFieldExplicitClass() {
28         val input =
29             arrayOf(
30                 javaSample("replacewith.ReplaceWithUsageJava"),
31                 javaSample("replacewith.StaticFieldExplicitClass")
32             )
33 
34         val expected =
35             """
36 src/replacewith/StaticFieldExplicitClass.java:25: Hint: Replacement available [ReplaceWith]
37         System.out.println(ReplaceWithUsageJava.AUTOFILL_HINT_NAME);
38                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39 0 errors, 0 warnings, 1 hint
40         """
41                 .trimIndent()
42 
43         val expectedFixDiffs =
44             """
45 Fix for src/replacewith/StaticFieldExplicitClass.java line 25: Replace with `View.AUTOFILL_HINT_NAME`:
46 @@ -25 +25
47 -         System.out.println(ReplaceWithUsageJava.AUTOFILL_HINT_NAME);
48 +         System.out.println(View.AUTOFILL_HINT_NAME);
49         """
50                 .trimIndent()
51 
52         check(*input).expect(expected).expectFixDiffs(expectedFixDiffs)
53     }
54 
55     @Test
staticFieldImplicitClassnull56     fun staticFieldImplicitClass() {
57         val input =
58             arrayOf(
59                 javaSample("replacewith.ReplaceWithUsageJava"),
60                 javaSample("replacewith.StaticFieldImplicitClass")
61             )
62 
63         val expected =
64             """
65 src/replacewith/StaticFieldImplicitClass.java:27: Hint: Replacement available [ReplaceWith]
66         System.out.println(AUTOFILL_HINT_NAME);
67                            ~~~~~~~~~~~~~~~~~~
68 0 errors, 0 warnings, 1 hint
69         """
70                 .trimIndent()
71 
72         val expectedFixDiffs =
73             """
74 Fix for src/replacewith/StaticFieldImplicitClass.java line 27: Replace with `View.AUTOFILL_HINT_NAME`:
75 @@ -27 +27
76 -         System.out.println(AUTOFILL_HINT_NAME);
77 +         System.out.println(View.AUTOFILL_HINT_NAME);
78         """
79                 .trimIndent()
80 
81         check(*input).expect(expected).expectFixDiffs(expectedFixDiffs)
82     }
83 }
84