xref: /aosp_15_r20/frameworks/base/tools/aapt2/ResourceValues_test.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
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 "ResourceValues.h"
18 
19 #include "test/Test.h"
20 
21 using ::testing::Eq;
22 using ::testing::SizeIs;
23 using ::testing::StrEq;
24 
25 namespace aapt {
26 
27 namespace {
28 
29 // Attribute types.
30 constexpr const uint32_t TYPE_DIMENSION = android::ResTable_map::TYPE_DIMENSION;
31 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
32 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
33 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
34 constexpr const uint32_t TYPE_REFERENCE = android::Res_value::TYPE_REFERENCE;
35 constexpr const uint32_t TYPE_STRING = android::ResTable_map::TYPE_STRING;
36 
37 }  // namespace
38 
TEST(ResourceValuesTest,PluralEquals)39 TEST(ResourceValuesTest, PluralEquals) {
40   android::StringPool pool;
41 
42   Plural a;
43   a.values[Plural::One] = util::make_unique<String>(pool.MakeRef("one"));
44   a.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("other"));
45 
46   Plural b;
47   b.values[Plural::One] = util::make_unique<String>(pool.MakeRef("une"));
48   b.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("autre"));
49 
50   Plural c;
51   c.values[Plural::One] = util::make_unique<String>(pool.MakeRef("one"));
52   c.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("other"));
53 
54   EXPECT_FALSE(a.Equals(&b));
55   EXPECT_TRUE(a.Equals(&c));
56 }
57 
TEST(ResourceValuesTest,PluralClone)58 TEST(ResourceValuesTest, PluralClone) {
59   android::StringPool pool;
60 
61   Plural a;
62   a.values[Plural::One] = util::make_unique<String>(pool.MakeRef("one"));
63   a.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("other"));
64 
65   CloningValueTransformer cloner(&pool);
66   std::unique_ptr<Plural> b(a.Transform(cloner));
67   EXPECT_TRUE(a.Equals(b.get()));
68 }
69 
TEST(ResourceValuesTest,ArrayEquals)70 TEST(ResourceValuesTest, ArrayEquals) {
71   android::StringPool pool;
72 
73   Array a;
74   a.elements.push_back(util::make_unique<String>(pool.MakeRef("one")));
75   a.elements.push_back(util::make_unique<String>(pool.MakeRef("two")));
76 
77   Array b;
78   b.elements.push_back(util::make_unique<String>(pool.MakeRef("une")));
79   b.elements.push_back(util::make_unique<String>(pool.MakeRef("deux")));
80 
81   Array c;
82   c.elements.push_back(util::make_unique<String>(pool.MakeRef("uno")));
83 
84   Array d;
85   d.elements.push_back(util::make_unique<String>(pool.MakeRef("one")));
86   d.elements.push_back(util::make_unique<String>(pool.MakeRef("two")));
87 
88   EXPECT_FALSE(a.Equals(&b));
89   EXPECT_FALSE(a.Equals(&c));
90   EXPECT_FALSE(b.Equals(&c));
91   EXPECT_TRUE(a.Equals(&d));
92 }
93 
TEST(ResourceValuesTest,ArrayClone)94 TEST(ResourceValuesTest, ArrayClone) {
95   android::StringPool pool;
96 
97   Array a;
98   a.elements.push_back(util::make_unique<String>(pool.MakeRef("one")));
99   a.elements.push_back(util::make_unique<String>(pool.MakeRef("two")));
100 
101   CloningValueTransformer cloner(&pool);
102   std::unique_ptr<Array> b(a.Transform(cloner));
103   EXPECT_TRUE(a.Equals(b.get()));
104 }
105 
TEST(ResourceValuesTest,StyleEquals)106 TEST(ResourceValuesTest, StyleEquals) {
107   android::StringPool pool;
108 
109   std::unique_ptr<Style> a = test::StyleBuilder()
110       .SetParent("android:style/Parent")
111       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
112       .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
113       .Build();
114 
115   std::unique_ptr<Style> b = test::StyleBuilder()
116       .SetParent("android:style/Parent")
117       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
118       .AddItem("android:attr/bar", ResourceUtils::TryParseInt("3"))
119       .Build();
120 
121   std::unique_ptr<Style> c = test::StyleBuilder()
122       .SetParent("android:style/NoParent")
123       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
124       .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
125       .Build();
126 
127   std::unique_ptr<Style> d = test::StyleBuilder()
128       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
129       .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
130       .Build();
131 
132   std::unique_ptr<Style> e = test::StyleBuilder()
133       .SetParent("android:style/Parent")
134       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
135       .AddItem("android:attr/bat", ResourceUtils::TryParseInt("2"))
136       .Build();
137 
138   std::unique_ptr<Style> f = test::StyleBuilder()
139       .SetParent("android:style/Parent")
140       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
141       .Build();
142 
143   std::unique_ptr<Style> g = test::StyleBuilder()
144       .SetParent("android:style/Parent")
145       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
146       .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
147       .Build();
148 
149   EXPECT_FALSE(a->Equals(b.get()));
150   EXPECT_FALSE(a->Equals(c.get()));
151   EXPECT_FALSE(a->Equals(d.get()));
152   EXPECT_FALSE(a->Equals(e.get()));
153   EXPECT_FALSE(a->Equals(f.get()));
154 
155   EXPECT_TRUE(a->Equals(g.get()));
156 }
157 
TEST(ResourceValuesTest,StyleClone)158 TEST(ResourceValuesTest, StyleClone) {
159   std::unique_ptr<Style> a = test::StyleBuilder()
160       .SetParent("android:style/Parent")
161       .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
162       .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
163       .Build();
164 
165   CloningValueTransformer cloner(nullptr);
166   std::unique_ptr<Style> b(a->Transform(cloner));
167   EXPECT_TRUE(a->Equals(b.get()));
168 }
169 
TEST(ResourcesValuesTest,StringClones)170 TEST(ResourcesValuesTest, StringClones) {
171   android::StringPool pool_a;
172   android::StringPool pool_b;
173 
174   String str_a(pool_a.MakeRef("hello", android::StringPool::Context(test::ParseConfigOrDie("en"))));
175 
176   ASSERT_THAT(pool_a, SizeIs(1u));
177   EXPECT_THAT(pool_a.strings()[0]->context.config, Eq(test::ParseConfigOrDie("en")));
178   EXPECT_THAT(pool_a.strings()[0]->value, StrEq("hello"));
179 
180   CloningValueTransformer cloner(&pool_b);
181   str_a.Transform(cloner);
182   ASSERT_THAT(pool_b, SizeIs(1u));
183   EXPECT_THAT(pool_b.strings()[0]->context.config, Eq(test::ParseConfigOrDie("en")));
184   EXPECT_THAT(pool_b.strings()[0]->value, StrEq("hello"));
185 }
186 
TEST(ResourcesValuesTest,StringEquals)187 TEST(ResourcesValuesTest, StringEquals) {
188   android::StringPool pool;
189 
190   String str(pool.MakeRef("hello", android::StringPool::Context(test::ParseConfigOrDie("en"))));
191   String str2(pool.MakeRef("hello"));
192   EXPECT_TRUE(str.Equals(&str2));
193   EXPECT_TRUE(str2.Equals(&str));
194 
195   String str3(pool.MakeRef("how are you"));
196   EXPECT_FALSE(str.Equals(&str3));
197 }
198 
TEST(ResourcesValuesTest,StyledStringEquals)199 TEST(ResourcesValuesTest, StyledStringEquals) {
200   android::StringPool pool;
201 
202   StyledString ss(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"u", 2, 4}}}));
203   StyledString ss2(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"u", 2, 4}}}));
204   StyledString ss3(pool.MakeRef(android::StyleString{"hi", {{"b", 0, 1}, {"u", 2, 4}}}));
205   StyledString ss4(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}}}));
206   StyledString ss5(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"u", 3, 4}}}));
207   StyledString ss6(pool.MakeRef(android::StyleString{"hello", {{"b", 0, 1}, {"s", 2, 4}}}));
208   EXPECT_TRUE(ss.Equals(&ss2));
209   EXPECT_TRUE(ss2.Equals(&ss));
210   EXPECT_FALSE(ss.Equals(&ss3));
211   EXPECT_FALSE(ss.Equals(&ss4));
212   EXPECT_FALSE(ss.Equals(&ss5));
213   EXPECT_FALSE(ss.Equals(&ss6));
214 }
215 
TEST(ResourceValuesTest,StyleMerges)216 TEST(ResourceValuesTest, StyleMerges) {
217   android::StringPool pool_a;
218   android::StringPool pool_b;
219 
220   std::unique_ptr<Style> a =
221       test::StyleBuilder()
222           .SetParent("android:style/Parent")
223           .AddItem("android:attr/a", util::make_unique<String>(pool_a.MakeRef("FooA")))
224           .AddItem("android:attr/b", util::make_unique<String>(pool_a.MakeRef("FooB")))
225           .Build();
226 
227   std::unique_ptr<Style> b =
228       test::StyleBuilder()
229           .SetParent("android:style/OverlayParent")
230           .AddItem("android:attr/c", util::make_unique<String>(pool_b.MakeRef("OverlayFooC")))
231           .AddItem("android:attr/a", util::make_unique<String>(pool_b.MakeRef("OverlayFooA")))
232           .Build();
233 
234   a->MergeWith(b.get(), &pool_a);
235 
236   android::StringPool pool;
237   std::unique_ptr<Style> expected =
238       test::StyleBuilder()
239           .SetParent("android:style/OverlayParent")
240           .AddItem("android:attr/a", util::make_unique<String>(pool.MakeRef("OverlayFooA")))
241           .AddItem("android:attr/b", util::make_unique<String>(pool.MakeRef("FooB")))
242           .AddItem("android:attr/c", util::make_unique<String>(pool.MakeRef("OverlayFooC")))
243           .Build();
244 
245   EXPECT_TRUE(a->Equals(expected.get()));
246 }
247 
248 // TYPE_NULL is encoded as TYPE_REFERENCE with a value of 0. This is represented in AAPT2
249 // by a default constructed Reference value.
TEST(ResourcesValuesTest,EmptyReferenceFlattens)250 TEST(ResourcesValuesTest, EmptyReferenceFlattens) {
251   android::Res_value value = {};
252   ASSERT_TRUE(Reference().Flatten(&value));
253 
254   EXPECT_THAT(value.dataType, Eq(android::Res_value::TYPE_REFERENCE));
255   EXPECT_THAT(value.data, Eq(0u));
256 }
257 
TEST(ResourcesValuesTest,AttributeMatches)258 TEST(ResourcesValuesTest, AttributeMatches) {
259   constexpr const uint8_t TYPE_INT_DEC = android::Res_value::TYPE_INT_DEC;
260 
261   Attribute attr1(TYPE_DIMENSION);
262   EXPECT_FALSE(attr1.Matches(*ResourceUtils::TryParseColor("#7fff00")));
263   EXPECT_TRUE(attr1.Matches(*ResourceUtils::TryParseFloat("23dp")));
264   EXPECT_TRUE(attr1.Matches(*ResourceUtils::TryParseReference("@android:string/foo")));
265 
266   Attribute attr2(TYPE_INTEGER | TYPE_ENUM);
267   attr2.min_int = 0;
268   attr2.symbols.push_back(Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")),
269                                             static_cast<uint32_t>(-1)});
270   EXPECT_FALSE(attr2.Matches(*ResourceUtils::TryParseColor("#7fff00")));
271   EXPECT_TRUE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, static_cast<uint32_t>(-1))));
272   EXPECT_TRUE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, 1u)));
273   EXPECT_FALSE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, static_cast<uint32_t>(-2))));
274 
275   Attribute attr3(TYPE_INTEGER | TYPE_FLAGS);
276   attr3.max_int = 100;
277   attr3.symbols.push_back(
278       Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")), 0x01u});
279   attr3.symbols.push_back(
280       Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/bar")), 0x02u});
281   attr3.symbols.push_back(
282       Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/baz")), 0x04u});
283   attr3.symbols.push_back(
284       Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/bat")), 0x80u});
285   EXPECT_FALSE(attr3.Matches(*ResourceUtils::TryParseColor("#7fff00")));
286   EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u | 0x02u)));
287   EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u | 0x02u | 0x80u)));
288 
289   // Not a flag, but a value less than max_int.
290   EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x08u)));
291 
292   // Not a flag and greater than max_int.
293   EXPECT_FALSE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 127u)));
294 
295   Attribute attr4(TYPE_ENUM);
296   attr4.symbols.push_back(
297       Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")), 0x01u});
298   EXPECT_TRUE(attr4.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u)));
299   EXPECT_FALSE(attr4.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x02u)));
300 }
301 
TEST(ResourcesValuesTest,AttributeIsCompatible)302 TEST(ResourcesValuesTest, AttributeIsCompatible) {
303   Attribute attr_one(TYPE_STRING | TYPE_REFERENCE);
304   Attribute attr_two(TYPE_STRING);
305   Attribute attr_three(TYPE_ENUM);
306   Attribute attr_four(TYPE_REFERENCE);
307 
308   EXPECT_TRUE(attr_one.IsCompatibleWith(attr_one));
309   EXPECT_TRUE(attr_one.IsCompatibleWith(attr_two));
310   EXPECT_FALSE(attr_one.IsCompatibleWith(attr_three));
311   EXPECT_FALSE(attr_one.IsCompatibleWith(attr_four));
312 
313   EXPECT_TRUE(attr_two.IsCompatibleWith(attr_one));
314   EXPECT_TRUE(attr_two.IsCompatibleWith(attr_two));
315   EXPECT_FALSE(attr_two.IsCompatibleWith(attr_three));
316   EXPECT_FALSE(attr_two.IsCompatibleWith(attr_four));
317 
318   EXPECT_FALSE(attr_three.IsCompatibleWith(attr_one));
319   EXPECT_FALSE(attr_three.IsCompatibleWith(attr_two));
320   EXPECT_FALSE(attr_three.IsCompatibleWith(attr_three));
321   EXPECT_FALSE(attr_three.IsCompatibleWith(attr_four));
322 }
323 
324 } // namespace aapt
325