blob: 3179fefc3fd0a60a1bef6c7dd16538ba7aedf42b [file] [log] [blame]
Adam Lesinski34a16872018-02-23 16:18:10 -08001/*
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
21namespace aapt {
22
23TEST(NoDefaultResourceRemoverTest, RemoveEntryWithNoDefaultAndOnlyLocales) {
Ryan Mitchell1d008d12021-03-19 14:54:17 -070024 std::unique_ptr<IAaptContext> context = test::ContextBuilder().SetPackageId(0x01).Build();
Adam Lesinski34a16872018-02-23 16:18:10 -080025 std::unique_ptr<ResourceTable> table =
26 test::ResourceTableBuilder()
Adam Lesinski34a16872018-02-23 16:18:10 -080027 .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
Ryan Mitchella5936052018-05-23 13:31:28 -070048TEST(NoDefaultResourceRemoverTest, KeepEntryWithLocalesAndDensities) {
Ryan Mitchell1d008d12021-03-19 14:54:17 -070049 std::unique_ptr<IAaptContext> context =
50 test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(26).Build();
Ryan Mitchella5936052018-05-23 13:31:28 -070051 std::unique_ptr<ResourceTable> table =
52 test::ResourceTableBuilder()
Ryan Mitchella5936052018-05-23 13:31:28 -070053 .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
72TEST(NoDefaultResourceRemoverTest, RemoveEntryWithLocalesAndDensitiesLowVersion) {
Ryan Mitchell1d008d12021-03-19 14:54:17 -070073 std::unique_ptr<IAaptContext> context =
74 test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(3).Build();
Ryan Mitchella5936052018-05-23 13:31:28 -070075 std::unique_ptr<ResourceTable> table =
76 test::ResourceTableBuilder()
Ryan Mitchella5936052018-05-23 13:31:28 -070077 .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
88TEST(NoDefaultResourceRemoverTest, KeepEntryWithVersion) {
Ryan Mitchell1d008d12021-03-19 14:54:17 -070089 std::unique_ptr<IAaptContext> context =
90 test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(8).Build();
Ryan Mitchella5936052018-05-23 13:31:28 -070091 std::unique_ptr<ResourceTable> table =
92 test::ResourceTableBuilder()
Ryan Mitchella5936052018-05-23 13:31:28 -070093 .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
Adam Lesinski34a16872018-02-23 16:18:10 -0800108} // namespace aapt