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 ProguardSamplesTest {
22
23    @Test fun proGuard_sample() {
24        ProGuardTester()
25            .forGivenPrefixes(
26                "android/app/",
27                "android/view/",
28                "android/content/",
29                "android/os/",
30                "android/webkit/"
31            )
32            .forGivenTypesMap(
33                "android/app/Activity" to "test/app/Activity",
34                "android/app/Application" to "test/app/Application",
35                "android/view/View" to "test/view/View",
36                "android/view/MenuItem" to "test/view/MenuItem",
37                "android/content/Context" to "test/content/Context",
38                "android/os/Parcelable" to "test/os/Parcelable",
39                "android/webkit/JavascriptInterface" to "test/webkit/JavascriptInterface"
40            )
41            .testThatGivenProGuard(
42                "-injars      bin/classes \n" +
43                "-injars      libs \n" +
44                "-outjars     bin/classes-processed.jar \n" +
45                "-libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar \n" +
46                "\n" +
47                "-dontpreverify \n" +
48                "-repackageclasses '' \n" +
49                "-allowaccessmodification \n" +
50                "-optimizations !code/simplification/arithmetic \n" +
51                "-keepattributes *Annotation* \n" +
52                "\n" +
53                "-keep public class * extends android.app.Activity \n" +
54                "-keep public class * extends android.app.Application \n" +
55                " \n" +
56                "-keep public class * extends android.view.View { \n" +
57                "      public <init>(android.content.Context); \n" +
58                "      public <init>(android.content.Context, android.util.AttributeSet); \n" +
59                "      public <init>(android.content.Context, android.util.AttributeSet, int); \n" +
60                "      public void set*(...); \n" +
61                "} \n" +
62                "\n" +
63                "-keepclasseswithmembers class * { \n" +
64                "    public <init>(android.content.Context, android.util.AttributeSet); \n" +
65                "} \n" +
66                "\n" +
67                "-keepclasseswithmembers class * { \n" +
68                "    public <init>(android.content.Context, android.util.AttributeSet, int); \n" +
69                "} \n" +
70                "\n" +
71                "-keepclassmembers class * extends android.content.Context { \n" +
72                "    public void *(android.view.View); \n" +
73                "    public void *(android.view.MenuItem); \n" +
74                "} \n" +
75                "\n" +
76                "-keepclassmembers class * implements android.os.Parcelable { \n" +
77                "    static ** CREATOR; \n" +
78                "} \n" +
79                "\n" +
80                "-keepclassmembers class **.R\$* { \n" +
81                "    public static <fields>; \n" +
82                "} \n" +
83                "\n" +
84                "-keepclassmembers class * { \n" +
85                "    @android.webkit.JavascriptInterface <methods>; \n" +
86                "} "
87            )
88            .rewritesTo(
89                "-injars      bin/classes \n" +
90                "-injars      libs \n" +
91                "-outjars     bin/classes-processed.jar \n" +
92                "-libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar \n" +
93                "\n" +
94                "-dontpreverify \n" +
95                "-repackageclasses '' \n" +
96                "-allowaccessmodification \n" +
97                "-optimizations !code/simplification/arithmetic \n" +
98                "-keepattributes *Annotation* \n" +
99                "\n" +
100                "-keep public class * extends test.app.Activity \n" +
101                "-keep public class * extends test.app.Application \n" +
102                " \n" +
103                "-keep public class * extends test.view.View { \n" +
104                "      public <init>(test.content.Context); \n" +
105                "      public <init>(test.content.Context, android.util.AttributeSet); \n" +
106                "      public <init>(test.content.Context, android.util.AttributeSet, int); \n" +
107                "      public void set*(...); \n" +
108                "} \n" +
109                "\n" +
110                "-keepclasseswithmembers class * { \n" +
111                "    public <init>(test.content.Context, android.util.AttributeSet); \n" +
112                "} \n" +
113                "\n" +
114                "-keepclasseswithmembers class * { \n" +
115                "    public <init>(test.content.Context, android.util.AttributeSet, int); \n" +
116                "} \n" +
117                "\n" +
118                "-keepclassmembers class * extends test.content.Context { \n" +
119                "    public void *(test.view.View); \n" +
120                "    public void *(test.view.MenuItem); \n" +
121                "} \n" +
122                "\n" +
123                "-keepclassmembers class * implements test.os.Parcelable { \n" +
124                "    static ** CREATOR; \n" +
125                "} \n" +
126                "\n" +
127                "-keepclassmembers class **.R\$* { \n" +
128                "    public static <fields>; \n" +
129                "} \n" +
130                "\n" +
131                "-keepclassmembers class * { \n" +
132                "    @test.webkit.JavascriptInterface <methods>; \n" +
133                "} "
134            )
135    }
136
137    @Test fun proGuard_sample2() {
138        ProGuardTester()
139            .forGivenPrefixes(
140                "android/support/v7/"
141            )
142            .forGivenTypesMap(
143                "android/support/v7/preference/Preference" to "test/Preference"
144            )
145            .testThatGivenProGuard(
146                "-keep public class android.support.v7.preference.Preference {\n" +
147                "  public <init>(android.content.Context, android.util.AttributeSet);\n" +
148                "}\n" +
149                "-keep public class * extends android.support.v7.preference.Preference {\n" +
150                "  public <init>(android.content.Context, android.util.AttributeSet);\n" +
151                "}"
152            )
153            .rewritesTo(
154                "-keep public class test.Preference {\n" +
155                "  public <init>(android.content.Context, android.util.AttributeSet);\n" +
156                "}\n" +
157                "-keep public class * extends test.Preference {\n" +
158                "  public <init>(android.content.Context, android.util.AttributeSet);\n" +
159                "}"
160            )
161    }
162
163    @Test fun proGuard_sample3() {
164        ProGuardTester()
165            .forGivenPrefixes(
166                "android/support/design/",
167                "android/support/v7/"
168            )
169            .forGivenTypesMap(
170                "support/Fragment" to "test/Fragment",
171                "android/support/v7/widget/RoundRectDrawable" to "test/RoundRectDrawable"
172            )
173            .forGivenProGuardMap(
174                "android/support/design.**" to "test/design.**",
175                "android/support/design/R\$*" to "test/design/R\$*"
176            )
177            .testThatGivenProGuard(
178                "-dontwarn android.support.design.**\n" +
179                "-keep class android.support.design.** { *; }\n" +
180                "-keep interface android.support.design.** { *; }\n" +
181                "-keep public class android.support.design.R\$* { *; }\n" +
182                "-keep class android.support.v7.widget.RoundRectDrawable { *; }"
183            )
184            .rewritesTo(
185                "-dontwarn test.design.**\n" +
186                "-keep class test.design.** { *; }\n" +
187                "-keep interface test.design.** { *; }\n" +
188                "-keep public class test.design.R\$* { *; }\n" +
189                "-keep class test.RoundRectDrawable { *; }"
190            )
191    }
192
193    @Test fun proGuard_sample4() {
194        ProGuardTester()
195            .forGivenPrefixes(
196                "android/support/design/",
197                "android/support/v7/",
198                "android/support/v4/"
199            )
200            .forGivenTypesMap(
201                "android/support/v7/widget/LinearLayoutManager" to "test/LinearLayoutManager",
202                "android/support/v4/view/ActionProvider" to "test/ActionProvider"
203            )
204            .forGivenProGuardMap(
205                "android/support/v7/**" to "test/v7/**",
206                "android/support/v7/widget/**" to "test/v7/widget/**",
207                "android/support/v7/internal/widget/**" to "test/v7/internal/widget/**",
208                "android/support/v7/internal/**" to "test/v7/internal/**"
209            )
210            .testThatGivenProGuard(
211                "-dontwarn android.support.v7.**\n" +
212                "-keep public class android.support.v7.widget.** { *; }\n" +
213                "-keep public class android.support.v7.internal.widget.** { *; }\n" +
214                "-keep class android.support.v7.widget.LinearLayoutManager { *; }\n" +
215                "-keep class android.support.v7.internal.** { *; }\n" +
216                "-keep interface android.support.v7.internal.** { *; }\n" +
217                "\n" +
218                "-keep class android.support.v7.** { *; }\n" +
219                "-keep interface android.support.v7.** { *; }\n" +
220                "\n" +
221                "-keep public class * extends android.support.v4.view.ActionProvider {\n" +
222                "    public <init>(android.content.Context);"
223            )
224            .rewritesTo(
225                "-dontwarn test.v7.**\n" +
226                "-keep public class test.v7.widget.** { *; }\n" +
227                "-keep public class test.v7.internal.widget.** { *; }\n" +
228                "-keep class test.LinearLayoutManager { *; }\n" +
229                "-keep class test.v7.internal.** { *; }\n" +
230                "-keep interface test.v7.internal.** { *; }\n" +
231                "\n" +
232                "-keep class test.v7.** { *; }\n" +
233                "-keep interface test.v7.** { *; }\n" +
234                "\n" +
235                "-keep public class * extends test.ActionProvider {\n" +
236                "    public <init>(android.content.Context);"
237            )
238    }
239
240    @Test fun proGuard_sample5() {
241        ProGuardTester()
242            .forGivenPrefixes(
243                "support/"
244            )
245            .forGivenTypesMap(
246                "support/Activity" to "test/Activity",
247                "support/Fragment" to "test/Fragment",
248                "support/Annotation" to "test/Annotation"
249            )
250            .testThatGivenProGuard(
251                "-keep public class * extends support.Activity { \n" +
252                "  public static <fields>; \n" +
253                "  public !static <methods>; \n" +
254                "  public support.Fragment height; \n" +
255                "  public static <fields>; \n" +
256                "  public not.related.Type width; public support.Fragment width; \n" +
257                "  ignoreMe; \n" +
258                "  @support.Annotation public support.Fragment get(); \n" +
259                "}\n" +
260                "-dontwarn support.**"
261            )
262            .rewritesTo(
263                "-keep public class * extends test.Activity { \n" +
264                "  public static <fields>; \n" +
265                "  public !static <methods>; \n" +
266                "  public test.Fragment height; \n" +
267                "  public static <fields>; \n" +
268                "  public not.related.Type width; public test.Fragment width; \n" +
269                "  ignoreMe; \n" +
270                "  @test.Annotation public test.Fragment get(); \n" +
271                "}\n" +
272                "-dontwarn test.**"
273            )
274    }
275}