blob: 3d93cb1dd43bd4c0c4b9d7525bf80c344f21b177 [file] [log] [blame]
Adam Lesinskif762df22017-06-26 16:39:03 -07001/*
2 * Copyright (C) 2017 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#include "java/ProguardRules.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070018#include "link/Linkers.h"
Adam Lesinskif762df22017-06-26 16:39:03 -070019
Adam Lesinskia693c4a2017-11-09 11:29:39 -080020#include "io/StringStream.h"
Adam Lesinskif762df22017-06-26 16:39:03 -070021#include "test/Test.h"
22
Adam Lesinskia693c4a2017-11-09 11:29:39 -080023using ::aapt::io::StringOutputStream;
Adam Lesinskif762df22017-06-26 16:39:03 -070024using ::testing::HasSubstr;
25using ::testing::Not;
26
27namespace aapt {
28
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070029std::string GetKeepSetString(const proguard::KeepSet& set, bool minimal_rules) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080030 std::string out;
31 StringOutputStream sout(&out);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070032 proguard::WriteKeepSet(set, &sout, minimal_rules);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080033 sout.Flush();
34 return out;
35}
36
Jake Whartonab660a72018-06-08 17:56:55 -040037TEST(ProguardRulesTest, ManifestRuleDefaultConstructorOnly) {
38 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"(
39 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
Jake Whartoncfbc7672018-06-12 09:26:13 -040040 <application
Jake Whartone4bd1602018-06-12 09:39:14 -040041 android:appComponentFactory="com.foo.BarAppComponentFactory"
Jake Whartoncfbc7672018-06-12 09:26:13 -040042 android:backupAgent="com.foo.BarBackupAgent"
43 android:name="com.foo.BarApplication"
44 >
Jake Whartonab660a72018-06-08 17:56:55 -040045 <activity android:name="com.foo.BarActivity"/>
46 <service android:name="com.foo.BarService"/>
47 <receiver android:name="com.foo.BarReceiver"/>
48 <provider android:name="com.foo.BarProvider"/>
49 </application>
50 <instrumentation android:name="com.foo.BarInstrumentation"/>
51 </manifest>)");
52
53 proguard::KeepSet set;
54 ASSERT_TRUE(proguard::CollectProguardRulesForManifest(manifest.get(), &set, false));
55
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070056 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
57 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }"));
58 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
59 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }"));
60 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { <init>(); }"));
61 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { <init>(); }"));
62 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { <init>(); }"));
63 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { <init>(); }"));
64 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { <init>(); }"));
Jake Whartonab660a72018-06-08 17:56:55 -040065
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070066 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Whartone4bd1602018-06-12 09:39:14 -040067 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }"));
Jake Whartonab660a72018-06-08 17:56:55 -040068 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
Jake Whartoncfbc7672018-06-12 09:26:13 -040069 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }"));
Jake Whartonab660a72018-06-08 17:56:55 -040070 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { <init>(); }"));
71 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { <init>(); }"));
72 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { <init>(); }"));
73 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { <init>(); }"));
74 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { <init>(); }"));
75}
76
Adam Lesinskif762df22017-06-26 16:39:03 -070077TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) {
78 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
79 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
80 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
81 android:name="com.foo.Bar"/>)");
82 layout->file.name = test::ParseNameOrDie("layout/foo");
83
84 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -070085 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -070086
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070087 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
88 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -070089
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070090 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -040091 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -070092}
93
94TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) {
95 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
96 std::unique_ptr<xml::XmlResource> layout =
97 test::BuildXmlDom(R"(<fragment class="com.foo.Bar"/>)");
98 layout->file.name = test::ParseNameOrDie("layout/foo");
99
100 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700101 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700102
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700103 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
104 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700105
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700106 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400107 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700108}
109
110TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) {
111 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
112 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
113 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
114 android:name="com.foo.Baz"
115 class="com.foo.Bar"/>)");
116 layout->file.name = test::ParseNameOrDie("layout/foo");
117
118 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700119 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700120
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700121 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
122 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
123 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700124
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700125 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400126 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
127 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700128}
129
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700130TEST(ProguardRulesTest, NavigationFragmentNameAndClassRulesAreEmitted) {
131 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
132 .SetCompilationPackage("com.base").Build();
133 std::unique_ptr<xml::XmlResource> navigation = test::BuildXmlDom(R"(
134 <navigation
135 xmlns:android="http://schemas.android.com/apk/res/android"
136 xmlns:app="http://schemas.android.com/apk/res-auto">
137 <custom android:id="@id/foo"
138 android:name="com.package.Foo"/>
139 <fragment android:id="@id/bar"
140 android:name="com.package.Bar">
141 <nested android:id="@id/nested"
142 android:name=".Nested"/>
143 </fragment>
144 </navigation>
145 )");
146
147 navigation->file.name = test::ParseNameOrDie("navigation/graph.xml");
148
149 proguard::KeepSet set;
150 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), navigation.get(), &set));
151
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700152 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
153 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
154 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
155 EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));
156
157 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton420785e2018-06-11 15:40:48 -0400158 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
159 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
160 EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700161}
162
Adam Koskidc21dea2017-07-21 10:55:27 -0700163TEST(ProguardRulesTest, CustomViewRulesAreEmitted) {
164 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
165 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
166 <View xmlns:android="http://schemas.android.com/apk/res/android">
167 <com.foo.Bar />
168 </View>)");
169 layout->file.name = test::ParseNameOrDie("layout/foo");
170
171 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700172 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700173
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700174 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
175 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700176
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700177 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400178 EXPECT_THAT(actual, HasSubstr(
179 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700180}
181
182TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) {
183 std::unique_ptr<xml::XmlResource> bar_layout = test::BuildXmlDom(R"(
184 <View xmlns:android="http://schemas.android.com/apk/res/android">
185 <com.foo.Bar />
186 </View>)");
187 bar_layout->file.name = test::ParseNameOrDie("com.foo:layout/bar");
188
189 ResourceTable table;
190 StdErrDiagnostics errDiagnostics;
191 table.AddResource(bar_layout->file.name, ConfigDescription::DefaultConfig(), "",
192 util::make_unique<FileReference>(), &errDiagnostics);
193
194 std::unique_ptr<IAaptContext> context =
195 test::ContextBuilder()
196 .SetCompilationPackage("com.foo")
197 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(&table))
198 .Build();
199
200 std::unique_ptr<xml::XmlResource> foo_layout = test::BuildXmlDom(R"(
201 <View xmlns:android="http://schemas.android.com/apk/res/android">
202 <include layout="@layout/bar" />
203 </View>)");
204 foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo");
205
206 XmlReferenceLinker xml_linker;
207 ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get()));
208 ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get()));
209
210 proguard::KeepSet set = proguard::KeepSet(true);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700211 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), bar_layout.get(), &set));
212 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), foo_layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700213
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700214 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
215 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
216 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
217 EXPECT_THAT(actual, HasSubstr("int foo"));
218 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700219
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700220 actual = GetKeepSetString(set, /** minimal_rules */ true);
Adam Koski09ef94e2017-11-10 11:15:55 -0800221 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400222 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700223 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700224 EXPECT_THAT(actual, HasSubstr("int foo"));
225 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700226}
227
228TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) {
229 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
230 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
231 <View xmlns:android="http://schemas.android.com/apk/res/android">
232 <com.foo.Bar />
233 </View>)");
234 layout->file.name = test::ParseNameOrDie("layout/foo");
235
236 proguard::KeepSet set = proguard::KeepSet(true);
237 set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700238 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700239
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700240 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400241 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700242 "-keep class com.foo.Bar { <init>(...); }"));
243 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
244 EXPECT_THAT(actual, HasSubstr("int foo"));
245 EXPECT_THAT(actual, HasSubstr("int bar"));
246
247 actual = GetKeepSetString(set, /** minimal_rules */ true);
248 EXPECT_THAT(actual, HasSubstr(
249 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koski09ef94e2017-11-10 11:15:55 -0800250 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700251 EXPECT_THAT(actual, HasSubstr("int foo"));
252 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700253}
254
255TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) {
256 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
257 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
258 <View xmlns:android="http://schemas.android.com/apk/res/android">
259 <com.foo.Bar />
260 </View>)");
261 layout->file.name = test::ParseNameOrDie("layout/foo");
262
263 proguard::KeepSet set = proguard::KeepSet(true);
264 set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700265 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700266
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700267 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
268 EXPECT_THAT(actual, Not(HasSubstr("-if")));
269 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700270
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700271 actual = GetKeepSetString(set, /** minimal_rules */ true);
Adam Koski09ef94e2017-11-10 11:15:55 -0800272 EXPECT_THAT(actual, Not(HasSubstr("-if")));
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400273 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700274 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700275}
276
Adam Lesinskif762df22017-06-26 16:39:03 -0700277TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
278 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
279 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
280 <View xmlns:android="http://schemas.android.com/apk/res/android"
281 android:onClick="bar_method" />)");
282 layout->file.name = test::ParseNameOrDie("layout/foo");
283
284 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700285 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700286
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700287 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
Jake Wharton3001f032018-06-11 12:24:11 -0400288 EXPECT_THAT(actual, HasSubstr(
289 "-keepclassmembers class * { *** bar_method(android.view.View); }"));
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700290
291 actual = GetKeepSetString(set, /** minimal_rules */ true);
292 EXPECT_THAT(actual, HasSubstr(
293 "-keepclassmembers class * { *** bar_method(android.view.View); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700294}
295
296TEST(ProguardRulesTest, MenuRulesAreEmitted) {
297 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
298 std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"(
299 <menu xmlns:android="http://schemas.android.com/apk/res/android">
300 <item android:onClick="on_click"
301 android:actionViewClass="com.foo.Bar"
302 android:actionProviderClass="com.foo.Baz"
303 android:name="com.foo.Bat" />
304 </menu>)");
305 menu->file.name = test::ParseNameOrDie("menu/foo");
306
307 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700308 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700309
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700310 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
Jake Wharton3001f032018-06-11 12:24:11 -0400311 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700312 "-keepclassmembers class * { *** on_click(android.view.MenuItem); }"));
313 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
314 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
315 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
316
317 actual = GetKeepSetString(set, /** minimal_rules */ true);
318 EXPECT_THAT(actual, HasSubstr(
319 "-keepclassmembers class * { *** on_click(android.view.MenuItem); }"));
Jake Wharton98100c32018-06-11 15:46:03 -0400320 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(android.content.Context); }"));
321 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(android.content.Context); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700322 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
323}
324
Jake Wharton420785e2018-06-11 15:40:48 -0400325TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) {
326 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
327 std::unique_ptr<xml::XmlResource> transition = test::BuildXmlDom(R"(
328 <changeBounds>
329 <pathMotion class="com.foo.Bar"/>
330 </changeBounds>)");
331 transition->file.name = test::ParseNameOrDie("transition/foo");
332
333 proguard::KeepSet set;
334 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transition.get(), &set));
335
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700336 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
337 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400338
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700339 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400340 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700341 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400342}
343
344TEST(ProguardRulesTest, TransitionRulesAreEmitted) {
345 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
346 std::unique_ptr<xml::XmlResource> transitionSet = test::BuildXmlDom(R"(
347 <transitionSet>
348 <transition class="com.foo.Bar"/>
349 </transitionSet>)");
350 transitionSet->file.name = test::ParseNameOrDie("transition/foo");
351
352 proguard::KeepSet set;
353 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transitionSet.get(), &set));
354
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700355 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
356 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400357
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700358 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400359 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700360 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400361}
362
Adam Lesinskif762df22017-06-26 16:39:03 -0700363} // namespace aapt