1 /*
2 * Copyright (C) 2016 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 "optimize/ResourceDeduper.h"
18
19 #include "ResourceTable.h"
20 #include "test/Test.h"
21
22 using ::aapt::test::HasValue;
23 using ::android::ConfigDescription;
24 using ::testing::Not;
25
26 namespace aapt {
27
TEST(ResourceDeduperTest,SameValuesAreDeduped)28 TEST(ResourceDeduperTest, SameValuesAreDeduped) {
29 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
30 const ConfigDescription default_config = {};
31 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
32 const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
33 const ConfigDescription en_config = test::ParseConfigOrDie("en");
34 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
35 // Chosen because this configuration is compatible with ldrtl/en.
36 const ConfigDescription land_config = test::ParseConfigOrDie("land");
37
38 std::unique_ptr<ResourceTable> table =
39 test::ResourceTableBuilder()
40 .AddString("android:string/dedupe", ResourceId{}, default_config, "dedupe")
41 .AddString("android:string/dedupe", ResourceId{}, ldrtl_config, "dedupe")
42 .AddString("android:string/dedupe", ResourceId{}, land_config, "dedupe")
43
44 .AddString("android:string/dedupe2", ResourceId{}, default_config, "dedupe")
45 .AddString("android:string/dedupe2", ResourceId{}, ldrtl_config, "dedupe")
46 .AddString("android:string/dedupe2", ResourceId{}, ldrtl_v21_config, "keep")
47 .AddString("android:string/dedupe2", ResourceId{}, land_config, "dedupe")
48
49 .AddString("android:string/dedupe3", ResourceId{}, default_config, "dedupe")
50 .AddString("android:string/dedupe3", ResourceId{}, en_config, "dedupe")
51 .AddString("android:string/dedupe3", ResourceId{}, en_v21_config, "dedupe")
52 .Build();
53
54 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
55 EXPECT_THAT(table, Not(HasValue("android:string/dedupe", ldrtl_config)));
56 EXPECT_THAT(table, Not(HasValue("android:string/dedupe", land_config)));
57
58 EXPECT_THAT(table, HasValue("android:string/dedupe2", ldrtl_v21_config));
59 EXPECT_THAT(table, Not(HasValue("android:string/dedupe2", ldrtl_config)));
60
61 EXPECT_THAT(table, HasValue("android:string/dedupe3", default_config));
62 EXPECT_THAT(table, HasValue("android:string/dedupe3", en_config));
63 EXPECT_THAT(table, Not(HasValue("android:string/dedupe3", en_v21_config)));
64 }
65
TEST(ResourceDeduperTest,DifferentValuesAreKept)66 TEST(ResourceDeduperTest, DifferentValuesAreKept) {
67 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
68 const ConfigDescription default_config = {};
69 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
70 const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
71 // Chosen because this configuration is compatible with ldrtl.
72 const ConfigDescription land_config = test::ParseConfigOrDie("land");
73
74 std::unique_ptr<ResourceTable> table =
75 test::ResourceTableBuilder()
76 .AddString("android:string/keep", ResourceId{}, default_config, "keep")
77 .AddString("android:string/keep", ResourceId{}, ldrtl_config, "keep")
78 .AddString("android:string/keep", ResourceId{}, ldrtl_v21_config, "keep2")
79 .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
80 .Build();
81
82 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
83 EXPECT_THAT(table, HasValue("android:string/keep", default_config));
84 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
85 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_v21_config));
86 EXPECT_THAT(table, HasValue("android:string/keep", land_config));
87 }
88
TEST(ResourceDeduperTest,SameValuesAreDedupedIncompatibleSiblings)89 TEST(ResourceDeduperTest, SameValuesAreDedupedIncompatibleSiblings) {
90 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
91 const ConfigDescription default_config = {};
92 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
93 const ConfigDescription ldrtl_night_config = test::ParseConfigOrDie("ldrtl-night");
94 // Chosen because this configuration is not compatible with ldrtl-night.
95 const ConfigDescription ldrtl_notnight_config = test::ParseConfigOrDie("ldrtl-notnight");
96
97 std::unique_ptr<ResourceTable> table =
98 test::ResourceTableBuilder()
99 .AddString("android:string/keep", ResourceId{}, default_config, "keep")
100 .AddString("android:string/keep", ResourceId{}, ldrtl_config, "dedupe")
101 .AddString("android:string/keep", ResourceId{}, ldrtl_night_config, "dedupe")
102 .AddString("android:string/keep", ResourceId{}, ldrtl_notnight_config, "keep2")
103 .Build();
104
105 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
106 EXPECT_THAT(table, HasValue("android:string/keep", default_config));
107 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
108 EXPECT_THAT(table, Not(HasValue("android:string/keep", ldrtl_night_config)));
109 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_notnight_config));
110 }
111
TEST(ResourceDeduperTest,SameValuesAreDedupedCompatibleNonSiblings)112 TEST(ResourceDeduperTest, SameValuesAreDedupedCompatibleNonSiblings) {
113 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
114 const ConfigDescription default_config = {};
115 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
116 const ConfigDescription ldrtl_night_config = test::ParseConfigOrDie("ldrtl-night");
117 // Chosen because this configuration is compatible with ldrtl.
118 const ConfigDescription land_config = test::ParseConfigOrDie("land");
119
120 std::unique_ptr<ResourceTable> table =
121 test::ResourceTableBuilder()
122 .AddString("android:string/keep", ResourceId{}, default_config, "keep")
123 .AddString("android:string/keep", ResourceId{}, ldrtl_config, "dedupe")
124 .AddString("android:string/keep", ResourceId{}, ldrtl_night_config, "dedupe")
125 .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
126 .Build();
127
128 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
129 EXPECT_THAT(table, HasValue("android:string/keep", default_config));
130 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
131 EXPECT_THAT(table, Not(HasValue("android:string/keep", ldrtl_night_config)));
132 EXPECT_THAT(table, HasValue("android:string/keep", land_config));
133 }
134
TEST(ResourceDeduperTest,LocalesValuesAreKept)135 TEST(ResourceDeduperTest, LocalesValuesAreKept) {
136 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
137 const ConfigDescription default_config = {};
138 const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
139 const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
140
141 std::unique_ptr<ResourceTable> table =
142 test::ResourceTableBuilder()
143 .AddString("android:string/keep", ResourceId{}, default_config, "keep")
144 .AddString("android:string/keep", ResourceId{}, fr_config, "keep")
145 .AddString("android:string/keep", ResourceId{}, fr_rCA_config, "keep")
146 .Build();
147
148 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
149 EXPECT_THAT(table, HasValue("android:string/keep", default_config));
150 EXPECT_THAT(table, HasValue("android:string/keep", fr_config));
151 EXPECT_THAT(table, HasValue("android:string/keep", fr_rCA_config));
152 }
153
154 } // namespace aapt
155