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, HasValue("android:string/dedupe", default_config));
56   EXPECT_THAT(table, Not(HasValue("android:string/dedupe", ldrtl_config)));
57   EXPECT_THAT(table, Not(HasValue("android:string/dedupe", land_config)));
58 
59   EXPECT_THAT(table, HasValue("android:string/dedupe2", default_config));
60   EXPECT_THAT(table, HasValue("android:string/dedupe2", ldrtl_v21_config));
61   EXPECT_THAT(table, Not(HasValue("android:string/dedupe2", ldrtl_config)));
62 
63   EXPECT_THAT(table, HasValue("android:string/dedupe3", default_config));
64   EXPECT_THAT(table, HasValue("android:string/dedupe3", en_config));
65   EXPECT_THAT(table, Not(HasValue("android:string/dedupe3", en_v21_config)));
66 }
67 
TEST(ResourceDeduperTest,DifferentValuesAreKept)68 TEST(ResourceDeduperTest, DifferentValuesAreKept) {
69   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
70   const ConfigDescription default_config = {};
71   const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
72   const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
73   // Chosen because this configuration is compatible with ldrtl.
74   const ConfigDescription land_config = test::ParseConfigOrDie("land");
75 
76   std::unique_ptr<ResourceTable> table =
77       test::ResourceTableBuilder()
78           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
79           .AddString("android:string/keep", ResourceId{}, ldrtl_config, "keep")
80           .AddString("android:string/keep", ResourceId{}, ldrtl_v21_config, "keep2")
81           .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
82           .Build();
83 
84   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
85   EXPECT_THAT(table, HasValue("android:string/keep", default_config));
86   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
87   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_v21_config));
88   EXPECT_THAT(table, HasValue("android:string/keep", land_config));
89 }
90 
TEST(ResourceDeduperTest,SameValuesAreDedupedIncompatibleSiblings)91 TEST(ResourceDeduperTest, SameValuesAreDedupedIncompatibleSiblings) {
92   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
93   const ConfigDescription default_config = {};
94   const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
95   const ConfigDescription ldrtl_night_config = test::ParseConfigOrDie("ldrtl-night");
96   // Chosen because this configuration is not compatible with ldrtl-night.
97   const ConfigDescription ldrtl_notnight_config = test::ParseConfigOrDie("ldrtl-notnight");
98 
99   std::unique_ptr<ResourceTable> table =
100       test::ResourceTableBuilder()
101           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
102           .AddString("android:string/keep", ResourceId{}, ldrtl_config, "dedupe")
103           .AddString("android:string/keep", ResourceId{}, ldrtl_night_config, "dedupe")
104           .AddString("android:string/keep", ResourceId{}, ldrtl_notnight_config, "keep2")
105           .Build();
106 
107   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
108   EXPECT_THAT(table, HasValue("android:string/keep", default_config));
109   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
110   EXPECT_THAT(table, Not(HasValue("android:string/keep", ldrtl_night_config)));
111   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_notnight_config));
112 }
113 
TEST(ResourceDeduperTest,SameValuesAreDedupedCompatibleNonSiblings)114 TEST(ResourceDeduperTest, SameValuesAreDedupedCompatibleNonSiblings) {
115   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
116   const ConfigDescription default_config = {};
117   const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
118   const ConfigDescription ldrtl_night_config = test::ParseConfigOrDie("ldrtl-night");
119   // Chosen because this configuration is compatible with ldrtl.
120   const ConfigDescription land_config = test::ParseConfigOrDie("land");
121 
122   std::unique_ptr<ResourceTable> table =
123       test::ResourceTableBuilder()
124           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
125           .AddString("android:string/keep", ResourceId{}, ldrtl_config, "dedupe")
126           .AddString("android:string/keep", ResourceId{}, ldrtl_night_config, "dedupe")
127           .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
128           .Build();
129 
130   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
131   EXPECT_THAT(table, HasValue("android:string/keep", default_config));
132   EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
133   EXPECT_THAT(table, Not(HasValue("android:string/keep", ldrtl_night_config)));
134   EXPECT_THAT(table, HasValue("android:string/keep", land_config));
135 }
136 
TEST(ResourceDeduperTest,LocalesValuesAreKept)137 TEST(ResourceDeduperTest, LocalesValuesAreKept) {
138   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
139   const ConfigDescription default_config = {};
140   const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
141   const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
142 
143   std::unique_ptr<ResourceTable> table =
144       test::ResourceTableBuilder()
145           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
146           .AddString("android:string/keep", ResourceId{}, fr_config, "keep")
147           .AddString("android:string/keep", ResourceId{}, fr_rCA_config, "keep")
148           .Build();
149 
150   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
151   EXPECT_THAT(table, HasValue("android:string/keep", default_config));
152   EXPECT_THAT(table, HasValue("android:string/keep", fr_config));
153   EXPECT_THAT(table, HasValue("android:string/keep", fr_rCA_config));
154 }
155 
TEST(ResourceDeduperTest,MccMncValuesAreKept)156 TEST(ResourceDeduperTest, MccMncValuesAreKept) {
157   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
158   const ConfigDescription default_config = {};
159   const ConfigDescription mcc_config = test::ParseConfigOrDie("mcc262");
160   const ConfigDescription mnc_config = test::ParseConfigOrDie("mnc2");
161 
162   std::unique_ptr<ResourceTable> table =
163       test::ResourceTableBuilder()
164           .AddString("android:string/keep", ResourceId{}, default_config, "keep")
165           .AddString("android:string/keep", ResourceId{}, mcc_config, "keep")
166           .AddString("android:string/keep", ResourceId{}, mnc_config, "keep")
167           .Build();
168 
169   ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
170   EXPECT_THAT(table, HasValue("android:string/keep", default_config));
171   EXPECT_THAT(table, HasValue("android:string/keep", mcc_config));
172   EXPECT_THAT(table, HasValue("android:string/keep", mnc_config));
173 }
174 
175 
176 }  // namespace aapt
177