1 /*
2 * Copyright (C) 2018 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 "link/NoDefaultResourceRemover.h"
18
19 #include "test/Test.h"
20
21 namespace aapt {
22
TEST(NoDefaultResourceRemoverTest,RemoveEntryWithNoDefaultAndOnlyLocales)23 TEST(NoDefaultResourceRemoverTest, RemoveEntryWithNoDefaultAndOnlyLocales) {
24 std::unique_ptr<IAaptContext> context = test::ContextBuilder().SetPackageId(0x01).Build();
25 std::unique_ptr<ResourceTable> table =
26 test::ResourceTableBuilder()
27 .AddSimple("android:string/foo")
28 .AddSimple("android:string/foo", test::ParseConfigOrDie("en-rGB"))
29 .AddSimple("android:string/foo", test::ParseConfigOrDie("fr-rFR"))
30 .AddSimple("android:string/bar", test::ParseConfigOrDie("en-rGB"))
31 .AddSimple("android:string/bar", test::ParseConfigOrDie("fr-rFR"))
32 .AddSimple("android:string/bat", test::ParseConfigOrDie("en-rGB-xhdpi"))
33 .AddSimple("android:string/bat", test::ParseConfigOrDie("fr-rFR-hdpi"))
34 .AddSimple("android:string/baz", test::ParseConfigOrDie("en-rGB"))
35 .AddSimple("android:string/baz", test::ParseConfigOrDie("fr-rFR"))
36 .SetSymbolState("android:string/baz", ResourceId(0x01020002), Visibility::Level::kPublic)
37 .Build();
38
39 NoDefaultResourceRemover remover;
40 ASSERT_TRUE(remover.Consume(context.get(), table.get()));
41
42 EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/foo")));
43 EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:string/bar")));
44 EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/bat")));
45 EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/baz")));
46 }
47
TEST(NoDefaultResourceRemoverTest,KeepEntryWithLocalesAndDensities)48 TEST(NoDefaultResourceRemoverTest, KeepEntryWithLocalesAndDensities) {
49 std::unique_ptr<IAaptContext> context =
50 test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(26).Build();
51 std::unique_ptr<ResourceTable> table =
52 test::ResourceTableBuilder()
53 .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("mdpi")) // v4
54 .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("en-rGB"))
55 .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("fr-rFR"))
56 .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("fr-rFR"))
57 .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("en-rGB"))
58 .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("xxxhdpi")) // v4
59 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR"))
60 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB"))
61 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("w600dp-xhdpi")) // v13
62 .Build();
63
64 NoDefaultResourceRemover remover;
65 ASSERT_TRUE(remover.Consume(context.get(), table.get()));
66
67 EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep1")));
68 EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep2")));
69 EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1")));
70 }
71
TEST(NoDefaultResourceRemoverTest,RemoveEntryWithLocalesAndDensitiesLowVersion)72 TEST(NoDefaultResourceRemoverTest, RemoveEntryWithLocalesAndDensitiesLowVersion) {
73 std::unique_ptr<IAaptContext> context =
74 test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(3).Build();
75 std::unique_ptr<ResourceTable> table =
76 test::ResourceTableBuilder()
77 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("mdpi")) // v4
78 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB"))
79 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR"))
80 .Build();
81
82 NoDefaultResourceRemover remover;
83 ASSERT_TRUE(remover.Consume(context.get(), table.get()));
84
85 EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1")));
86 }
87
TEST(NoDefaultResourceRemoverTest,KeepEntryWithVersion)88 TEST(NoDefaultResourceRemoverTest, KeepEntryWithVersion) {
89 std::unique_ptr<IAaptContext> context =
90 test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(8).Build();
91 std::unique_ptr<ResourceTable> table =
92 test::ResourceTableBuilder()
93 .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("v8"))
94 .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("en-rGB"))
95 .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("fr-rFR"))
96 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("v9"))
97 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB"))
98 .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR"))
99 .Build();
100
101 NoDefaultResourceRemover remover;
102 ASSERT_TRUE(remover.Consume(context.get(), table.get()));
103
104 EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep1")));
105 EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1")));
106 }
107
108 } // namespace aapt
109