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
17package com.android.tools.build.jetifier.processor.transform.proguard
18
19import org.junit.Test
20
21class ClassSpecTest_FieldTypeSelector {
22
23    @Test fun proGuard_fieldTypeSelector() {
24        ProGuardTester()
25            .forGivenPrefixes(
26                "support/"
27            )
28            .forGivenTypesMap(
29                "support/Activity" to "test/Activity",
30                "support/Fragment" to "test/Fragment"
31            )
32            .testThatGivenProGuard(
33                "-keep public class * { \n" +
34                "  support.Activity height; \n" +
35                "  support.Fragment *; \n" +
36                "  keep.Me width; \n" +
37                "}"
38            )
39            .rewritesTo(
40                "-keep public class * { \n" +
41                "  test.Activity height; \n" +
42                "  test.Fragment *; \n" +
43                "  keep.Me width; \n" +
44                "}"
45            )
46    }
47
48    @Test fun proGuard_fieldTypeSelector_modifiers() {
49        ProGuardTester()
50            .forGivenPrefixes(
51                "support/"
52            )
53            .forGivenTypesMap(
54                "support/Activity" to "test/Activity",
55                "support/Fragment" to "test/Fragment"
56            )
57            .testThatGivenProGuard(
58                "-keep public class * { \n" +
59                "  public support.Fragment height; \n" +
60                "  !public !static support.Fragment height; \n" +
61                "  !protected support.Fragment height; \n" +
62                "}"
63            )
64            .rewritesTo(
65                "-keep public class * { \n" +
66                "  public test.Fragment height; \n" +
67                "  !public !static test.Fragment height; \n" +
68                "  !protected test.Fragment height; \n" +
69                "}"
70            )
71    }
72
73    @Test fun proGuard_fieldTypeSelector_annotation() {
74        ProGuardTester()
75            .forGivenPrefixes(
76                "support/"
77            )
78            .forGivenTypesMap(
79                "support/Activity" to "test/Activity",
80                "support/Fragment" to "test/Fragment",
81                "support/Annotation" to "test/Annotation"
82            )
83            .testThatGivenProGuard(
84                "-keep public class * { \n" +
85                "  @support.Annotation support.Fragment height; \n" +
86                "  @some.Annotation support.Fragment height; \n" +
87                "}"
88            )
89            .rewritesTo(
90                "-keep public class * { \n" +
91                "  @test.Annotation test.Fragment height; \n" +
92                "  @some.Annotation test.Fragment height; \n" +
93                "}"
94            )
95    }
96
97    @Test fun proGuard_fieldTypeSelector_modifiers_annotation() {
98        ProGuardTester()
99            .forGivenPrefixes(
100                "support/"
101            )
102            .forGivenTypesMap(
103                "support/Activity" to "test/Activity",
104                "support/Fragment" to "test/Fragment",
105                "support/Annotation" to "test/Annotation"
106            )
107            .testThatGivenProGuard(
108                "-keep public class * { \n" +
109                "  @support.Annotation public support.Fragment height; \n" +
110                "  @support.Annotation !public !static support.Fragment height; \n" +
111                "  @support.Annotation !protected volatile support.Fragment height; \n" +
112                "}"
113            )
114            .rewritesTo(
115                "-keep public class * { \n" +
116                "  @test.Annotation public test.Fragment height; \n" +
117                "  @test.Annotation !public !static test.Fragment height; \n" +
118                "  @test.Annotation !protected volatile test.Fragment height; \n" +
119                "}"
120            )
121    }
122
123    @Test fun proGuard_fieldTypeSelector_modifiers_annotation_spaces() {
124        ProGuardTester()
125            .forGivenPrefixes(
126                "support/"
127            )
128            .forGivenTypesMap(
129                "support/Activity" to "test/Activity",
130                "support/Fragment" to "test/Fragment",
131                "support/Annotation" to "test/Annotation"
132            )
133            .testThatGivenProGuard(
134                "-keep public class * { \n" +
135                "  @support.Annotation  public  static \t support.Fragment  height ; \n" +
136                "}"
137            )
138            .rewritesTo(
139                "-keep public class * { \n" +
140                "  @test.Annotation  public  static \t test.Fragment  height ; \n" +
141                "}"
142            )
143    }
144
145    @Test fun proGuard_fieldTypeSelector_multiple() {
146        ProGuardTester()
147            .forGivenPrefixes(
148                "support/"
149            )
150            .forGivenProGuardMapSet("support.**" to setOf("support.**", "androidx.**"))
151            .testThatGivenProGuard(
152                "-keep public class support.** { \n" +
153                "  public support.** height; \n" +
154                "}"
155            )
156            .rewritesTo(
157                "-keep public class support.** { \n" +
158                "  public support.** height; \n" +
159                "}\n" +
160                "-keep public class androidx.** { \n" +
161                "  public support.** height; \n" +
162                "}\n" +
163                "-keep public class support.** { \n" +
164                "  public androidx.** height; \n" +
165                "}\n" +
166                "-keep public class androidx.** { \n" +
167                "  public androidx.** height; \n" +
168                "}"
169            )
170    }
171}