blob: da24907417fa63feb2e9b4ea60cf7b6123a817f1 [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;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020024using ::android::ConfigDescription;
Adam Lesinskif762df22017-06-26 16:39:03 -070025using ::testing::HasSubstr;
26using ::testing::Not;
27
28namespace aapt {
29
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070030std::string GetKeepSetString(const proguard::KeepSet& set, bool minimal_rules) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -080031 std::string out;
32 StringOutputStream sout(&out);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070033 proguard::WriteKeepSet(set, &sout, minimal_rules);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080034 sout.Flush();
35 return out;
36}
37
Jake Whartonab660a72018-06-08 17:56:55 -040038TEST(ProguardRulesTest, ManifestRuleDefaultConstructorOnly) {
39 std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"(
40 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
Jake Whartoncfbc7672018-06-12 09:26:13 -040041 <application
Jake Whartone4bd1602018-06-12 09:39:14 -040042 android:appComponentFactory="com.foo.BarAppComponentFactory"
Jake Whartoncfbc7672018-06-12 09:26:13 -040043 android:backupAgent="com.foo.BarBackupAgent"
44 android:name="com.foo.BarApplication"
45 >
Jake Whartonab660a72018-06-08 17:56:55 -040046 <activity android:name="com.foo.BarActivity"/>
47 <service android:name="com.foo.BarService"/>
48 <receiver android:name="com.foo.BarReceiver"/>
49 <provider android:name="com.foo.BarProvider"/>
50 </application>
51 <instrumentation android:name="com.foo.BarInstrumentation"/>
52 </manifest>)");
53
54 proguard::KeepSet set;
55 ASSERT_TRUE(proguard::CollectProguardRulesForManifest(manifest.get(), &set, false));
56
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070057 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
58 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }"));
59 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
60 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }"));
61 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { <init>(); }"));
62 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { <init>(); }"));
63 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { <init>(); }"));
64 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { <init>(); }"));
65 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { <init>(); }"));
Jake Whartonab660a72018-06-08 17:56:55 -040066
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070067 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Whartone4bd1602018-06-12 09:39:14 -040068 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }"));
Jake Whartonab660a72018-06-08 17:56:55 -040069 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }"));
Jake Whartoncfbc7672018-06-12 09:26:13 -040070 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }"));
Jake Whartonab660a72018-06-08 17:56:55 -040071 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarActivity { <init>(); }"));
72 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarService { <init>(); }"));
73 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarReceiver { <init>(); }"));
74 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarProvider { <init>(); }"));
75 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarInstrumentation { <init>(); }"));
76}
77
Adam Lesinskif762df22017-06-26 16:39:03 -070078TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) {
79 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
80 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
81 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
82 android:name="com.foo.Bar"/>)");
83 layout->file.name = test::ParseNameOrDie("layout/foo");
84
85 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -070086 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -070087
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070088 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
89 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -070090
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070091 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -040092 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -070093}
94
95TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) {
96 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
97 std::unique_ptr<xml::XmlResource> layout =
98 test::BuildXmlDom(R"(<fragment class="com.foo.Bar"/>)");
99 layout->file.name = test::ParseNameOrDie("layout/foo");
100
101 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700102 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700103
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700104 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
105 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700106
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700107 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400108 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700109}
110
111TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) {
112 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
113 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
114 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
115 android:name="com.foo.Baz"
116 class="com.foo.Bar"/>)");
117 layout->file.name = test::ParseNameOrDie("layout/foo");
118
119 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700120 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700121
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700122 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
123 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
124 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700125
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700126 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400127 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }"));
128 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700129}
130
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700131TEST(ProguardRulesTest, NavigationFragmentNameAndClassRulesAreEmitted) {
132 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
133 .SetCompilationPackage("com.base").Build();
134 std::unique_ptr<xml::XmlResource> navigation = test::BuildXmlDom(R"(
135 <navigation
136 xmlns:android="http://schemas.android.com/apk/res/android"
137 xmlns:app="http://schemas.android.com/apk/res-auto">
138 <custom android:id="@id/foo"
139 android:name="com.package.Foo"/>
140 <fragment android:id="@id/bar"
141 android:name="com.package.Bar">
142 <nested android:id="@id/nested"
143 android:name=".Nested"/>
144 </fragment>
145 </navigation>
146 )");
147
148 navigation->file.name = test::ParseNameOrDie("navigation/graph.xml");
149
150 proguard::KeepSet set;
151 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), navigation.get(), &set));
152
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700153 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
154 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
155 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
156 EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));
157
158 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton420785e2018-06-11 15:40:48 -0400159 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }"));
160 EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }"));
161 EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }"));
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700162}
163
Adam Koskidc21dea2017-07-21 10:55:27 -0700164TEST(ProguardRulesTest, CustomViewRulesAreEmitted) {
165 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
166 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
167 <View xmlns:android="http://schemas.android.com/apk/res/android">
168 <com.foo.Bar />
169 </View>)");
170 layout->file.name = test::ParseNameOrDie("layout/foo");
171
172 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700173 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700174
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700175 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
176 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700177
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700178 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400179 EXPECT_THAT(actual, HasSubstr(
180 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700181}
182
183TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) {
184 std::unique_ptr<xml::XmlResource> bar_layout = test::BuildXmlDom(R"(
185 <View xmlns:android="http://schemas.android.com/apk/res/android">
186 <com.foo.Bar />
187 </View>)");
188 bar_layout->file.name = test::ParseNameOrDie("com.foo:layout/bar");
189
190 ResourceTable table;
191 StdErrDiagnostics errDiagnostics;
192 table.AddResource(bar_layout->file.name, ConfigDescription::DefaultConfig(), "",
193 util::make_unique<FileReference>(), &errDiagnostics);
194
195 std::unique_ptr<IAaptContext> context =
196 test::ContextBuilder()
197 .SetCompilationPackage("com.foo")
198 .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(&table))
199 .Build();
200
201 std::unique_ptr<xml::XmlResource> foo_layout = test::BuildXmlDom(R"(
202 <View xmlns:android="http://schemas.android.com/apk/res/android">
203 <include layout="@layout/bar" />
204 </View>)");
205 foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo");
206
207 XmlReferenceLinker xml_linker;
208 ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get()));
209 ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get()));
210
211 proguard::KeepSet set = proguard::KeepSet(true);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700212 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), bar_layout.get(), &set));
213 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), foo_layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700214
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700215 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
216 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
217 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
218 EXPECT_THAT(actual, HasSubstr("int foo"));
219 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700220
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700221 actual = GetKeepSetString(set, /** minimal_rules */ true);
Adam Koski09ef94e2017-11-10 11:15:55 -0800222 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400223 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700224 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700225 EXPECT_THAT(actual, HasSubstr("int foo"));
226 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700227}
228
229TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) {
230 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
231 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
232 <View xmlns:android="http://schemas.android.com/apk/res/android">
233 <com.foo.Bar />
234 </View>)");
235 layout->file.name = test::ParseNameOrDie("layout/foo");
236
237 proguard::KeepSet set = proguard::KeepSet(true);
238 set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700239 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700240
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700241 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400242 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700243 "-keep class com.foo.Bar { <init>(...); }"));
244 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
245 EXPECT_THAT(actual, HasSubstr("int foo"));
246 EXPECT_THAT(actual, HasSubstr("int bar"));
247
248 actual = GetKeepSetString(set, /** minimal_rules */ true);
249 EXPECT_THAT(actual, HasSubstr(
250 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koski09ef94e2017-11-10 11:15:55 -0800251 EXPECT_THAT(actual, HasSubstr("-if class **.R$layout"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700252 EXPECT_THAT(actual, HasSubstr("int foo"));
253 EXPECT_THAT(actual, HasSubstr("int bar"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700254}
255
256TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) {
257 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
258 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
259 <View xmlns:android="http://schemas.android.com/apk/res/android">
260 <com.foo.Bar />
261 </View>)");
262 layout->file.name = test::ParseNameOrDie("layout/foo");
263
264 proguard::KeepSet set = proguard::KeepSet(true);
265 set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name);
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700266 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Koskidc21dea2017-07-21 10:55:27 -0700267
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700268 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
269 EXPECT_THAT(actual, Not(HasSubstr("-if")));
270 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700271
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700272 actual = GetKeepSetString(set, /** minimal_rules */ true);
Adam Koski09ef94e2017-11-10 11:15:55 -0800273 EXPECT_THAT(actual, Not(HasSubstr("-if")));
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400274 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700275 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Adam Koskidc21dea2017-07-21 10:55:27 -0700276}
277
Adam Lesinskif762df22017-06-26 16:39:03 -0700278TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
279 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
280 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
281 <View xmlns:android="http://schemas.android.com/apk/res/android"
282 android:onClick="bar_method" />)");
283 layout->file.name = test::ParseNameOrDie("layout/foo");
284
285 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700286 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700287
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700288 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
Jake Wharton3001f032018-06-11 12:24:11 -0400289 EXPECT_THAT(actual, HasSubstr(
290 "-keepclassmembers class * { *** bar_method(android.view.View); }"));
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700291
292 actual = GetKeepSetString(set, /** minimal_rules */ true);
293 EXPECT_THAT(actual, HasSubstr(
294 "-keepclassmembers class * { *** bar_method(android.view.View); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700295}
296
297TEST(ProguardRulesTest, MenuRulesAreEmitted) {
298 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
299 std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"(
300 <menu xmlns:android="http://schemas.android.com/apk/res/android">
301 <item android:onClick="on_click"
302 android:actionViewClass="com.foo.Bar"
303 android:actionProviderClass="com.foo.Baz"
304 android:name="com.foo.Bat" />
305 </menu>)");
306 menu->file.name = test::ParseNameOrDie("menu/foo");
307
308 proguard::KeepSet set;
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700309 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set));
Adam Lesinskif762df22017-06-26 16:39:03 -0700310
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700311 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
Jake Wharton3001f032018-06-11 12:24:11 -0400312 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700313 "-keepclassmembers class * { *** on_click(android.view.MenuItem); }"));
314 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
315 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(...); }"));
316 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
317
318 actual = GetKeepSetString(set, /** minimal_rules */ true);
319 EXPECT_THAT(actual, HasSubstr(
320 "-keepclassmembers class * { *** on_click(android.view.MenuItem); }"));
Jake Wharton98100c32018-06-11 15:46:03 -0400321 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(android.content.Context); }"));
322 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(android.content.Context); }"));
Adam Lesinskif762df22017-06-26 16:39:03 -0700323 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
324}
325
Jake Wharton420785e2018-06-11 15:40:48 -0400326TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) {
327 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
328 std::unique_ptr<xml::XmlResource> transition = test::BuildXmlDom(R"(
329 <changeBounds>
330 <pathMotion class="com.foo.Bar"/>
331 </changeBounds>)");
332 transition->file.name = test::ParseNameOrDie("transition/foo");
333
334 proguard::KeepSet set;
335 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transition.get(), &set));
336
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700337 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
338 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400339
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700340 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400341 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700342 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400343}
344
345TEST(ProguardRulesTest, TransitionRulesAreEmitted) {
346 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
347 std::unique_ptr<xml::XmlResource> transitionSet = test::BuildXmlDom(R"(
348 <transitionSet>
349 <transition class="com.foo.Bar"/>
350 </transitionSet>)");
351 transitionSet->file.name = test::ParseNameOrDie("transition/foo");
352
353 proguard::KeepSet set;
354 ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transitionSet.get(), &set));
355
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700356 std::string actual = GetKeepSetString(set, /** minimal_rules */ false);
357 EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(...); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400358
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700359 actual = GetKeepSetString(set, /** minimal_rules */ true);
Jake Wharton98100c32018-06-11 15:46:03 -0400360 EXPECT_THAT(actual, HasSubstr(
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700361 "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }"));
Jake Wharton420785e2018-06-11 15:40:48 -0400362}
363
Adam Lesinskif762df22017-06-26 16:39:03 -0700364} // namespace aapt