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 | |
| 20 | #include "test/Test.h" |
| 21 | |
| 22 | using ::testing::HasSubstr; |
| 23 | using ::testing::Not; |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) { |
| 28 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 29 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 30 | <fragment xmlns:android="http://schemas.android.com/apk/res/android" |
| 31 | android:name="com.foo.Bar"/>)"); |
| 32 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 33 | |
| 34 | proguard::KeepSet set; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 36 | |
| 37 | std::stringstream out; |
| 38 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 39 | |
| 40 | std::string actual = out.str(); |
| 41 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 42 | } |
| 43 | |
| 44 | TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) { |
| 45 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 46 | std::unique_ptr<xml::XmlResource> layout = |
| 47 | test::BuildXmlDom(R"(<fragment class="com.foo.Bar"/>)"); |
| 48 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 49 | |
| 50 | proguard::KeepSet set; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 52 | |
| 53 | std::stringstream out; |
| 54 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 55 | |
| 56 | std::string actual = out.str(); |
| 57 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 58 | } |
| 59 | |
| 60 | TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) { |
| 61 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 62 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 63 | <fragment xmlns:android="http://schemas.android.com/apk/res/android" |
| 64 | android:name="com.foo.Baz" |
| 65 | class="com.foo.Bar"/>)"); |
| 66 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 67 | |
| 68 | proguard::KeepSet set; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 69 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 70 | |
| 71 | std::stringstream out; |
| 72 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 73 | |
| 74 | std::string actual = out.str(); |
| 75 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 76 | EXPECT_THAT(actual, HasSubstr("com.foo.Baz")); |
| 77 | } |
| 78 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 79 | TEST(ProguardRulesTest, CustomViewRulesAreEmitted) { |
| 80 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 81 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 82 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 83 | <com.foo.Bar /> |
| 84 | </View>)"); |
| 85 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 86 | |
| 87 | proguard::KeepSet set; |
| 88 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
| 89 | |
| 90 | std::stringstream out; |
| 91 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 92 | |
| 93 | std::string actual = out.str(); |
| 94 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 95 | } |
| 96 | |
| 97 | TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) { |
| 98 | std::unique_ptr<xml::XmlResource> bar_layout = test::BuildXmlDom(R"( |
| 99 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 100 | <com.foo.Bar /> |
| 101 | </View>)"); |
| 102 | bar_layout->file.name = test::ParseNameOrDie("com.foo:layout/bar"); |
| 103 | |
| 104 | ResourceTable table; |
| 105 | StdErrDiagnostics errDiagnostics; |
| 106 | table.AddResource(bar_layout->file.name, ConfigDescription::DefaultConfig(), "", |
| 107 | util::make_unique<FileReference>(), &errDiagnostics); |
| 108 | |
| 109 | std::unique_ptr<IAaptContext> context = |
| 110 | test::ContextBuilder() |
| 111 | .SetCompilationPackage("com.foo") |
| 112 | .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(&table)) |
| 113 | .Build(); |
| 114 | |
| 115 | std::unique_ptr<xml::XmlResource> foo_layout = test::BuildXmlDom(R"( |
| 116 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 117 | <include layout="@layout/bar" /> |
| 118 | </View>)"); |
| 119 | foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo"); |
| 120 | |
| 121 | XmlReferenceLinker xml_linker; |
| 122 | ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get())); |
| 123 | ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get())); |
| 124 | |
| 125 | proguard::KeepSet set = proguard::KeepSet(true); |
| 126 | ASSERT_TRUE(proguard::CollectProguardRules(bar_layout.get(), &set)); |
| 127 | ASSERT_TRUE(proguard::CollectProguardRules(foo_layout.get(), &set)); |
| 128 | |
| 129 | std::stringstream out; |
| 130 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 131 | |
| 132 | std::string actual = out.str(); |
Adam Koski | 09ef94e | 2017-11-10 11:15:55 -0800 | [diff] [blame^] | 133 | EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 134 | EXPECT_THAT(actual, HasSubstr("int foo")); |
| 135 | EXPECT_THAT(actual, HasSubstr("int bar")); |
| 136 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 137 | } |
| 138 | |
| 139 | TEST(ProguardRulesTest, AliasedLayoutRulesAreConditional) { |
| 140 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 141 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 142 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 143 | <com.foo.Bar /> |
| 144 | </View>)"); |
| 145 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 146 | |
| 147 | proguard::KeepSet set = proguard::KeepSet(true); |
| 148 | set.AddReference({test::ParseNameOrDie("layout/bar"), {}}, layout->file.name); |
| 149 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
| 150 | |
| 151 | std::stringstream out; |
| 152 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 153 | |
| 154 | std::string actual = out.str(); |
Adam Koski | 09ef94e | 2017-11-10 11:15:55 -0800 | [diff] [blame^] | 155 | EXPECT_THAT(actual, HasSubstr("-if class **.R$layout")); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 156 | EXPECT_THAT(actual, HasSubstr("int foo")); |
| 157 | EXPECT_THAT(actual, HasSubstr("int bar")); |
| 158 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 159 | } |
| 160 | |
| 161 | TEST(ProguardRulesTest, NonLayoutReferencesAreUnconditional) { |
| 162 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 163 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 164 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 165 | <com.foo.Bar /> |
| 166 | </View>)"); |
| 167 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 168 | |
| 169 | proguard::KeepSet set = proguard::KeepSet(true); |
| 170 | set.AddReference({test::ParseNameOrDie("style/MyStyle"), {}}, layout->file.name); |
| 171 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
| 172 | |
| 173 | std::stringstream out; |
| 174 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 175 | |
| 176 | std::string actual = out.str(); |
Adam Koski | 09ef94e | 2017-11-10 11:15:55 -0800 | [diff] [blame^] | 177 | EXPECT_THAT(actual, Not(HasSubstr("-if"))); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 180 | TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) { |
| 181 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 182 | std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"( |
| 183 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 184 | android:onClick="bar_method" />)"); |
| 185 | layout->file.name = test::ParseNameOrDie("layout/foo"); |
| 186 | |
| 187 | proguard::KeepSet set; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(proguard::CollectProguardRules(layout.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 189 | |
| 190 | std::stringstream out; |
| 191 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 192 | |
| 193 | std::string actual = out.str(); |
| 194 | EXPECT_THAT(actual, HasSubstr("bar_method")); |
| 195 | } |
| 196 | |
| 197 | TEST(ProguardRulesTest, MenuRulesAreEmitted) { |
| 198 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 199 | std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"( |
| 200 | <menu xmlns:android="http://schemas.android.com/apk/res/android"> |
| 201 | <item android:onClick="on_click" |
| 202 | android:actionViewClass="com.foo.Bar" |
| 203 | android:actionProviderClass="com.foo.Baz" |
| 204 | android:name="com.foo.Bat" /> |
| 205 | </menu>)"); |
| 206 | menu->file.name = test::ParseNameOrDie("menu/foo"); |
| 207 | |
| 208 | proguard::KeepSet set; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 209 | ASSERT_TRUE(proguard::CollectProguardRules(menu.get(), &set)); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 210 | |
| 211 | std::stringstream out; |
| 212 | ASSERT_TRUE(proguard::WriteKeepSet(&out, set)); |
| 213 | |
| 214 | std::string actual = out.str(); |
| 215 | EXPECT_THAT(actual, HasSubstr("on_click")); |
| 216 | EXPECT_THAT(actual, HasSubstr("com.foo.Bar")); |
| 217 | EXPECT_THAT(actual, HasSubstr("com.foo.Baz")); |
| 218 | EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat"))); |
| 219 | } |
| 220 | |
| 221 | } // namespace aapt |