blob: 900b07339715efa3ca9c130c384d9743b157388c [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"
18
19#include "test/Test.h"
20
21using ::testing::HasSubstr;
22using ::testing::Not;
23
24namespace aapt {
25
26TEST(ProguardRulesTest, FragmentNameRuleIsEmitted) {
27 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
28 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
29 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
30 android:name="com.foo.Bar"/>)");
31 layout->file.name = test::ParseNameOrDie("layout/foo");
32
33 proguard::KeepSet set;
34 ASSERT_TRUE(proguard::CollectProguardRules({}, layout.get(), &set));
35
36 std::stringstream out;
37 ASSERT_TRUE(proguard::WriteKeepSet(&out, set));
38
39 std::string actual = out.str();
40 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
41}
42
43TEST(ProguardRulesTest, FragmentClassRuleIsEmitted) {
44 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
45 std::unique_ptr<xml::XmlResource> layout =
46 test::BuildXmlDom(R"(<fragment class="com.foo.Bar"/>)");
47 layout->file.name = test::ParseNameOrDie("layout/foo");
48
49 proguard::KeepSet set;
50 ASSERT_TRUE(proguard::CollectProguardRules({}, layout.get(), &set));
51
52 std::stringstream out;
53 ASSERT_TRUE(proguard::WriteKeepSet(&out, set));
54
55 std::string actual = out.str();
56 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
57}
58
59TEST(ProguardRulesTest, FragmentNameAndClassRulesAreEmitted) {
60 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
61 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
62 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
63 android:name="com.foo.Baz"
64 class="com.foo.Bar"/>)");
65 layout->file.name = test::ParseNameOrDie("layout/foo");
66
67 proguard::KeepSet set;
68 ASSERT_TRUE(proguard::CollectProguardRules({}, layout.get(), &set));
69
70 std::stringstream out;
71 ASSERT_TRUE(proguard::WriteKeepSet(&out, set));
72
73 std::string actual = out.str();
74 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
75 EXPECT_THAT(actual, HasSubstr("com.foo.Baz"));
76}
77
78TEST(ProguardRulesTest, ViewOnClickRuleIsEmitted) {
79 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
80 std::unique_ptr<xml::XmlResource> layout = test::BuildXmlDom(R"(
81 <View xmlns:android="http://schemas.android.com/apk/res/android"
82 android:onClick="bar_method" />)");
83 layout->file.name = test::ParseNameOrDie("layout/foo");
84
85 proguard::KeepSet set;
86 ASSERT_TRUE(proguard::CollectProguardRules({}, layout.get(), &set));
87
88 std::stringstream out;
89 ASSERT_TRUE(proguard::WriteKeepSet(&out, set));
90
91 std::string actual = out.str();
92 EXPECT_THAT(actual, HasSubstr("bar_method"));
93}
94
95TEST(ProguardRulesTest, MenuRulesAreEmitted) {
96 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
97 std::unique_ptr<xml::XmlResource> menu = test::BuildXmlDom(R"(
98 <menu xmlns:android="http://schemas.android.com/apk/res/android">
99 <item android:onClick="on_click"
100 android:actionViewClass="com.foo.Bar"
101 android:actionProviderClass="com.foo.Baz"
102 android:name="com.foo.Bat" />
103 </menu>)");
104 menu->file.name = test::ParseNameOrDie("menu/foo");
105
106 proguard::KeepSet set;
107 ASSERT_TRUE(proguard::CollectProguardRules({}, menu.get(), &set));
108
109 std::stringstream out;
110 ASSERT_TRUE(proguard::WriteKeepSet(&out, set));
111
112 std::string actual = out.str();
113 EXPECT_THAT(actual, HasSubstr("on_click"));
114 EXPECT_THAT(actual, HasSubstr("com.foo.Bar"));
115 EXPECT_THAT(actual, HasSubstr("com.foo.Baz"));
116 EXPECT_THAT(actual, Not(HasSubstr("com.foo.Bat")));
117}
118
119} // namespace aapt