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_MethodSelectorWithReturnType {
22
23    @Test fun proGuard_methodReturnTypeSelector() {
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                "  void get*(); \n" +
35                "  void get*(...); \n" +
36                "  void get*(*); \n" +
37                "  void get*(support.Activity); \n" +
38                "  void get?(support.Activity); \n" +
39                "  void get(support.Activity); \n" +
40                "  void *(support.Activity, support.Fragment, keep.Please); \n" +
41                "}"
42            )
43            .rewritesTo(
44                "-keep public class * { \n" +
45                "  void get*(); \n" +
46                "  void get*(...); \n" +
47                "  void get*(*); \n" +
48                "  void get*(test.Activity); \n" +
49                "  void get?(test.Activity); \n" +
50                "  void get(test.Activity); \n" +
51                "  void *(test.Activity, test.Fragment, keep.Please); \n" +
52                "}"
53            )
54    }
55
56    @Test fun proGuard_methodReturnTypeSelector_voidResult() {
57        ProGuardTester()
58            .forGivenPrefixes(
59                "support/"
60            )
61            .forGivenTypesMap(
62                "support/Activity" to "test/Activity",
63                "support/Fragment" to "test/Fragment"
64            )
65            .testThatGivenProGuard(
66                "-keep public class * { \n" +
67                "  void get(); \n" +
68                "  void get(...); \n" +
69                "  void get(*); \n" +
70                "  void get(support.Activity); \n" +
71                "  void get(support.Activity, support.Fragment, keep.Please); \n" +
72                "}"
73            )
74            .rewritesTo(
75                "-keep public class * { \n" +
76                "  void get(); \n" +
77                "  void get(...); \n" +
78                "  void get(*); \n" +
79                "  void get(test.Activity); \n" +
80                "  void get(test.Activity, test.Fragment, keep.Please); \n" +
81                "}"
82            )
83    }
84
85    @Test fun proGuard_methodReturnTypeSelector_starResult() {
86        ProGuardTester()
87            .forGivenPrefixes(
88                "support/"
89            )
90            .forGivenTypesMap(
91                "support/Activity" to "test/Activity",
92                "support/Fragment" to "test/Fragment"
93            )
94            .testThatGivenProGuard(
95                "-keep public class * { \n" +
96                "  * get(); \n" +
97                "  * get(...); \n" +
98                "  * get(*); \n" +
99                "  * get(support.Activity); \n" +
100                "  * get(support.Activity, support.Fragment, keep.Please); \n" +
101                "}"
102            )
103            .rewritesTo(
104                "-keep public class * { \n" +
105                "  * get(); \n" +
106                "  * get(...); \n" +
107                "  * get(*); \n" +
108                "  * get(test.Activity); \n" +
109                "  * get(test.Activity, test.Fragment, keep.Please); \n" +
110                "}"
111            )
112    }
113
114    @Test fun proGuard_methodReturnTypeSelector_typeResult() {
115        ProGuardTester()
116            .forGivenPrefixes(
117                "support/"
118            )
119            .forGivenTypesMap(
120                "support/Activity" to "test/Activity",
121                "support/Fragment" to "test/Fragment"
122            )
123            .testThatGivenProGuard(
124                "-keep public class * { \n" +
125                "  support.Fragment get(); \n" +
126                "  support.Fragment get(...); \n" +
127                "  support.Fragment get(*); \n" +
128                "  support.Fragment get(support.Activity); \n" +
129                "  support.Fragment get(support.Activity, support.Fragment, keep.Please); \n" +
130                "}"
131            )
132            .rewritesTo(
133                "-keep public class * { \n" +
134                "  test.Fragment get(); \n" +
135                "  test.Fragment get(...); \n" +
136                "  test.Fragment get(*); \n" +
137                "  test.Fragment get(test.Activity); \n" +
138                "  test.Fragment get(test.Activity, test.Fragment, keep.Please); \n" +
139                "}"
140            )
141    }
142
143    @Test fun proGuard_methodReturnTypeSelector_typeResult_wildcards() {
144        ProGuardTester()
145            .forGivenPrefixes(
146                "support/"
147            )
148            .forGivenTypesMap(
149                "support/Activity" to "test/Activity",
150                "support/Fragment" to "test/Fragment"
151            )
152            .testThatGivenProGuard(
153                "-keep public class * { \n" +
154                "  support.Fragment get*(); \n" +
155                "  support.Fragment get?(...); \n" +
156                "  support.Fragment *(*); \n" +
157                "  support.Fragment *(support.Activity); \n" +
158                "  support.Fragment *(support.Activity, support.Fragment, keep.Please); \n" +
159                "}"
160            )
161            .rewritesTo(
162                "-keep public class * { \n" +
163                "  test.Fragment get*(); \n" +
164                "  test.Fragment get?(...); \n" +
165                "  test.Fragment *(*); \n" +
166                "  test.Fragment *(test.Activity); \n" +
167                "  test.Fragment *(test.Activity, test.Fragment, keep.Please); \n" +
168                "}"
169            )
170    }
171
172    @Test fun proGuard_methodReturnTypeSelector_typeResult_modifiers() {
173        ProGuardTester()
174            .forGivenPrefixes(
175                "support/"
176            )
177            .forGivenTypesMap(
178                "support/Activity" to "test/Activity",
179                "support/Fragment" to "test/Fragment"
180            )
181            .testThatGivenProGuard(
182                "-keep public class * { \n" +
183                "  public support.Fragment get(); \n" +
184                "  public static support.Fragment get(...); \n" +
185                "  !public !static support.Fragment get(*); \n" +
186                "  private support.Fragment get(support.Activity); \n" +
187                "  public abstract support.Fragment get(support.Activity, support.Fragment, " +
188                "keep.Please); \n" +
189                "}"
190            )
191            .rewritesTo(
192                "-keep public class * { \n" +
193                "  public test.Fragment get(); \n" +
194                "  public static test.Fragment get(...); \n" +
195                "  !public !static test.Fragment get(*); \n" +
196                "  private test.Fragment get(test.Activity); \n" +
197                "  public abstract test.Fragment get(test.Activity, test.Fragment, keep.Please); " +
198                "\n" +
199                "}"
200            )
201    }
202
203    @Test fun proGuard_methodReturnTypeSelector_typeResult_annotation() {
204        ProGuardTester()
205            .forGivenPrefixes(
206                "support/"
207            )
208            .forGivenTypesMap(
209                "support/Activity" to "test/Activity",
210                "support/Fragment" to "test/Fragment",
211                "support/Annotation" to "test/Annotation"
212            )
213            .testThatGivenProGuard(
214                "-keep public class * { \n" +
215                "  @support.Annotation support.Fragment get(); \n" +
216                "  @support.Annotation support.Fragment get(...); \n" +
217                "  @support.Annotation support.Fragment get(*); \n" +
218                "  @keep.Me support.Fragment get(support.Activity); \n" +
219                "  @support.Annotation support.Fragment get(support.Activity, support.Fragment, " +
220                "keep.Please); \n" +
221                "}"
222            )
223            .rewritesTo(
224                "-keep public class * { \n" +
225                "  @test.Annotation test.Fragment get(); \n" +
226                "  @test.Annotation test.Fragment get(...); \n" +
227                "  @test.Annotation test.Fragment get(*); \n" +
228                "  @keep.Me test.Fragment get(test.Activity); \n" +
229                "  @test.Annotation test.Fragment get(test.Activity, test.Fragment, keep.Please" +
230                "); \n" +
231                "}"
232            )
233    }
234
235    @Test fun proGuard_methodReturnTypeSelector_typeResult_modifiers_annotation() {
236        ProGuardTester()
237            .forGivenPrefixes(
238                "support/"
239            )
240            .forGivenTypesMap(
241                "support/Activity" to "test/Activity",
242                "support/Fragment" to "test/Fragment",
243                "support/Annotation" to "test/Annotation"
244            )
245            .testThatGivenProGuard(
246                "-keep public class * { \n" +
247                "  @support.Annotation public support.Fragment get(); \n" +
248                "  @support.Annotation public static support.Fragment get(...); \n" +
249                "  @support.Annotation !public !static support.Fragment get(*); \n" +
250                "  @support.Annotation private support.Fragment get(support.Activity); \n" +
251                "  @support.Annotation public abstract support.Fragment get(support.Activity, " +
252                "support.Fragment,  keep.Please); \n" +
253                "}"
254            )
255            .rewritesTo(
256                "-keep public class * { \n" +
257                "  @test.Annotation public test.Fragment get(); \n" +
258                "  @test.Annotation public static test.Fragment get(...); \n" +
259                "  @test.Annotation !public !static test.Fragment get(*); \n" +
260                "  @test.Annotation private test.Fragment get(test.Activity); \n" +
261                "  @test.Annotation public abstract test.Fragment get(test.Activity, " +
262                "test.Fragment, keep.Please); \n" +
263                "}"
264            )
265    }
266
267    @Test fun proGuard_methodReturnTypeSelector_typeResult_modifiers_annotation_spaces() {
268        ProGuardTester()
269            .forGivenPrefixes(
270                "support/"
271            )
272            .forGivenTypesMap(
273                "support/Activity" to "test/Activity",
274                "support/Fragment" to "test/Fragment",
275                "support/Annotation" to "test/Annotation"
276            )
277            .testThatGivenProGuard(
278                "-keep public class * { \n" +
279                "  @support.Annotation  support.Fragment \t get(support.Activity ,  " +
280                "support.Fragment ,  keep.Please) ; \n" +
281                "}"
282            )
283            .rewritesTo(
284                "-keep public class * { \n" +
285                "  @test.Annotation  test.Fragment \t get(test.Activity, test.Fragment, " +
286                "keep.Please) ; \n" +
287                "}"
288            )
289    }
290
291    @Test fun proGuard_methodReturnTypeSelector_multiple() {
292        ProGuardTester()
293            .forGivenPrefixes(
294                "support/"
295            )
296            .forGivenProGuardMapSet("support.**" to setOf("support.**", "androidx.**"))
297            .testThatGivenProGuard(
298                "-keep public class * { \n" +
299                "  support.** get(support.**); \n" +
300                "}"
301            )
302            .rewritesTo(
303                "-keep public class * { \n" +
304                "  support.** get(support.**); \n" +
305                "}\n" +
306                "-keep public class * { \n" +
307                "  androidx.** get(support.**); \n" +
308                "}\n" +
309                "-keep public class * { \n" +
310                "  support.** get(androidx.**); \n" +
311                "}\n" +
312                "-keep public class * { \n" +
313                "  androidx.** get(androidx.**); \n" +
314                "}"
315            )
316    }
317}