Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 18 | #include "link/Linkers.h" |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 19 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 20 | #include "io/StringStream.h" |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 21 | #include "test/Test.h" |
| 22 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 23 | using ::aapt::io::StringOutputStream; |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 24 | using ::testing::HasSubstr; |
| 25 | using ::testing::Not; |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 29 | std::string GetKeepSetString(const proguard::KeepSet& set) { |
| 30 | std::string out; |
| 31 | StringOutputStream sout(&out); |
| 32 | proguard::WriteKeepSet(set, &sout); |
| 33 | sout.Flush(); |
| 34 | return out; |
| 35 | } |
| 36 | |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 37 | TEST(ProguardRulesTest, ManifestRuleDefaultConstructorOnly) { |
| 38 | std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"( |
| 39 | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
Jake Wharton | cfbc767 | 2018-06-12 09:26:13 -0400 | [diff] [blame] | 40 | <application |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 41 | android:appComponentFactory="com.foo.BarAppComponentFactory" |
Jake Wharton | cfbc767 | 2018-06-12 09:26:13 -0400 | [diff] [blame] | 42 | android:backupAgent="com.foo.BarBackupAgent" |
| 43 | android:name="com.foo.BarApplication" |
| 44 | > |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 45 | <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 | |
| 56 | std::string actual = GetKeepSetString(set); |
| 57 | |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 58 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarAppComponentFactory { <init>(); }")); |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 59 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarBackupAgent { <init>(); }")); |
Jake Wharton | cfbc767 | 2018-06-12 09:26:13 -0400 | [diff] [blame] | 60 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.BarApplication { <init>(); }")); |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 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>(); }")); |
| 66 | } |
| 67 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 68 | TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) { |
| 69 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 70 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 71 | <fragment xmlns:android="http://schemas.android.com/apk/res/android" |
| 72 | android:name="com.foo.Bar"/>)"); |
| 73 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 74 | |
| 75 | proguard::KeepSet set; |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 76 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 78 | std::string actual = GetKeepSetString(set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 79 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 80 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }")); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) { |
| 84 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 85 | std::unique_ptr<xml::XmlResource> layout = |
| 86 | test::BuildXmlDom(R"(<fragment class="com.foo.Bar"/>)"); |
| 87 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 88 | |
| 89 | proguard::KeepSet set; |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 90 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 92 | std::string actual = GetKeepSetString(set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 93 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 94 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }")); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) { |
| 98 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 99 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 100 | <fragment xmlns:android="http://schemas.android.com/apk/res/android" |
| 101 | android:name="com.foo.Baz" |
| 102 | class="com.foo.Bar"/>)"); |
| 103 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 104 | |
| 105 | proguard::KeepSet set; |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 106 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 108 | std::string actual = GetKeepSetString(set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 109 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 110 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(); }")); |
| 111 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(); }")); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 114 | TEST(ProguardRulesTest, NavigationFragmentNameAndClassRulesAreEmitted) { |
| 115 | std::unique_ptr<IAaptContext> context = test::ContextBuilder() |
| 116 | .SetCompilationPackage("com.base").Build(); |
| 117 | std::unique_ptr<xml::XmlResource> navigation = test::BuildXmlDom(R"( |
| 118 | <navigation |
| 119 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 120 | xmlns:app="http://schemas.android.com/apk/res-auto"> |
| 121 | <custom android:id="@id/foo" |
| 122 | android:name="com.package.Foo"/> |
| 123 | <fragment android:id="@id/bar" |
| 124 | android:name="com.package.Bar"> |
| 125 | <nested android:id="@id/nested" |
| 126 | android:name=".Nested"/> |
| 127 | </fragment> |
| 128 | </navigation> |
| 129 | )"); |
| 130 | |
| 131 | navigation->file.name = test::ParseNameOrDie("navigation/graph.xml"); |
| 132 | |
| 133 | proguard::KeepSet set; |
| 134 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), navigation.get(), &set)); |
| 135 | |
| 136 | std::string actual = GetKeepSetString(set); |
Jake Wharton | 420785e | 2018-06-11 15:40:48 -0400 | [diff] [blame] | 137 | EXPECT_THAT(actual, HasSubstr("-keep class com.package.Foo { <init>(...); }")); |
| 138 | EXPECT_THAT(actual, HasSubstr("-keep class com.package.Bar { <init>(...); }")); |
| 139 | EXPECT_THAT(actual, HasSubstr("-keep class com.base.Nested { <init>(...); }")); |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 142 | TEST(ProguardRulesTest, CustomViewRulesAreEmitted) { |
| 143 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 144 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 145 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 146 | <com.foo.Bar /> |
| 147 | </View>)"); |
| 148 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 149 | |
| 150 | proguard::KeepSet set; |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 151 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 152 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 153 | std::string actual = GetKeepSetString(set); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 154 | |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 155 | EXPECT_THAT(actual, HasSubstr( |
| 156 | "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) { |
| 160 | std::unique_ptr<xml::XmlResource> bar_layout = test::BuildXmlDom(R"( |
| 161 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 162 | <com.foo.Bar /> |
| 163 | </View>)"); |
| 164 | bar_layout->file.name = test::ParseNameOrDie("com.foo:layout/bar"); |
| 165 | |
| 166 | ResourceTable table; |
| 167 | StdErrDiagnostics errDiagnostics; |
| 168 | table.AddResource(bar_layout->file.name, ConfigDescription::DefaultConfig(), "", |
| 169 | util::make_unique<FileReference>(), &errDiagnostics); |
| 170 | |
| 171 | std::unique_ptr<IAaptContext> context = |
| 172 | test::ContextBuilder() |
| 173 | .SetCompilationPackage("com.foo") |
| 174 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(&table)) |
| 175 | .Build(); |
| 176 | |
| 177 | std::unique_ptr<xml::XmlResource> foo_layout = test::BuildXmlDom(R"( |
| 178 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 179 | <include layout="@layout/bar" /> |
| 180 | </View>)"); |
| 181 | foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo"); |
| 182 | |
| 183 | XmlReferenceLinker xml_linker; |
| 184 | ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get())); |
| 185 | ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get())); |
| 186 | |
| 187 | proguard::KeepSet set = proguard::KeepSet(true); |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), bar_layout.get(), &set)); |
| 189 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), foo_layout.get(), &set)); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 191 | std::string actual = GetKeepSetString(set); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 192 | |
Adam Koski | 09ef94e | 2017-11-10 11:15:55 -0800 | [diff] [blame] | 193 | EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 194 | EXPECT_THAT(actual, HasSubstr( |
| 195 | "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 196 | EXPECT_THAT(actual, HasSubstr("int foo")); |
| 197 | EXPECT_THAT(actual, HasSubstr("int bar")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) { |
| 201 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 202 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 203 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 204 | <com.foo.Bar /> |
| 205 | </View>)"); |
| 206 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 207 | |
| 208 | proguard::KeepSet set = proguard::KeepSet(true); |
| 209 | set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name); |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 210 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 211 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 212 | std::string actual = GetKeepSetString(set); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 213 | |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 214 | EXPECT_THAT(actual, HasSubstr( |
| 215 | "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }")); |
Adam Koski | 09ef94e | 2017-11-10 11:15:55 -0800 | [diff] [blame] | 216 | EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 217 | EXPECT_THAT(actual, HasSubstr("int foo")); |
| 218 | EXPECT_THAT(actual, HasSubstr("int bar")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) { |
| 222 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 223 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 224 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 225 | <com.foo.Bar /> |
| 226 | </View>)"); |
| 227 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 228 | |
| 229 | proguard::KeepSet set = proguard::KeepSet(true); |
| 230 | set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name); |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 231 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 232 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 233 | std::string actual = GetKeepSetString(set); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 234 | |
Adam Koski | 09ef94e | 2017-11-10 11:15:55 -0800 | [diff] [blame] | 235 | EXPECT_THAT(actual, Not(HasSubstr("-if"))); |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 236 | EXPECT_THAT(actual, HasSubstr( |
| 237 | "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 240 | TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) { |
| 241 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 242 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 243 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 244 | android:onClick="bar_method" />)"); |
| 245 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 246 | |
| 247 | proguard::KeepSet set; |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 248 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 249 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 250 | std::string actual = GetKeepSetString(set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 251 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 252 | EXPECT_THAT(actual, HasSubstr( |
| 253 | "-keepclassmembers class * { *** bar_method(android.view.View); }")); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST(ProguardRulesTest, MenuRulesAreEmitted) { |
| 257 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 258 | std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"( |
| 259 | <menu xmlns:android="http://schemas.android.com/apk/res/android"> |
| 260 | <item android:onClick="on_click" |
| 261 | android:actionViewClass="com.foo.Bar" |
| 262 | android:actionProviderClass="com.foo.Baz" |
| 263 | android:name="com.foo.Bat" /> |
| 264 | </menu>)"); |
| 265 | menu->file.name = test::ParseNameOrDie("menu/foo"); |
| 266 | |
| 267 | proguard::KeepSet set; |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 268 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), menu.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 269 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 270 | std::string actual = GetKeepSetString(set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 271 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 272 | EXPECT_THAT(actual, HasSubstr( |
| 273 | "-keepclassmembers class * { *** on_click(android.view.MenuItem); }")); |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 274 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Bar { <init>(android.content.Context); }")); |
| 275 | EXPECT_THAT(actual, HasSubstr("-keep class com.foo.Baz { <init>(android.content.Context); }")); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 276 | EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat"))); |
| 277 | } |
| 278 | |
Jake Wharton | 420785e | 2018-06-11 15:40:48 -0400 | [diff] [blame] | 279 | TEST(ProguardRulesTest, TransitionPathMotionRulesAreEmitted) { |
| 280 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 281 | std::unique_ptr<xml::XmlResource> transition = test::BuildXmlDom(R"( |
| 282 | <changeBounds> |
| 283 | <pathMotion class="com.foo.Bar"/> |
| 284 | </changeBounds>)"); |
| 285 | transition->file.name = test::ParseNameOrDie("transition/foo"); |
| 286 | |
| 287 | proguard::KeepSet set; |
| 288 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transition.get(), &set)); |
| 289 | |
| 290 | std::string actual = GetKeepSetString(set); |
| 291 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 292 | EXPECT_THAT(actual, HasSubstr( |
| 293 | "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }")); |
Jake Wharton | 420785e | 2018-06-11 15:40:48 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST(ProguardRulesTest, TransitionRulesAreEmitted) { |
| 297 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 298 | std::unique_ptr<xml::XmlResource> transitionSet = test::BuildXmlDom(R"( |
| 299 | <transitionSet> |
| 300 | <transition class="com.foo.Bar"/> |
| 301 | </transitionSet>)"); |
| 302 | transitionSet->file.name = test::ParseNameOrDie("transition/foo"); |
| 303 | |
| 304 | proguard::KeepSet set; |
| 305 | ASSERT_TRUE(proguard::CollectProguardRules(context.get(), transitionSet.get(), &set)); |
| 306 | |
| 307 | std::string actual = GetKeepSetString(set); |
| 308 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 309 | EXPECT_THAT(actual, HasSubstr( |
| 310 | "-keep class com.foo.Bar { <init>(android.content.Context, android.util.AttributeSet); }")); |
Jake Wharton | 420785e | 2018-06-11 15:40:48 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 313 | } // namespace aapt |