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(ResourceValuesTest,StyleMerges)187 TEST(ResourceValuesTest, StyleMerges) {
188 android::StringPool pool_a;
189 android::StringPool pool_b;
190
191 std::unique_ptr<Style> a =
192 test::StyleBuilder()
193 .SetParent("android:style/Parent")
194 .AddItem("android:attr/a", util::make_unique<String>(pool_a.MakeRef("FooA")))
195 .AddItem("android:attr/b", util::make_unique<String>(pool_a.MakeRef("FooB")))
196 .Build();
197
198 std::unique_ptr<Style> b =
199 test::StyleBuilder()
200 .SetParent("android:style/OverlayParent")
201 .AddItem("android:attr/c", util::make_unique<String>(pool_b.MakeRef("OverlayFooC")))
202 .AddItem("android:attr/a", util::make_unique<String>(pool_b.MakeRef("OverlayFooA")))
203 .Build();
204
205 a->MergeWith(b.get(), &pool_a);
206
207 android::StringPool pool;
208 std::unique_ptr<Style> expected =
209 test::StyleBuilder()
210 .SetParent("android:style/OverlayParent")
211 .AddItem("android:attr/a", util::make_unique<String>(pool.MakeRef("OverlayFooA")))
212 .AddItem("android:attr/b", util::make_unique<String>(pool.MakeRef("FooB")))
213 .AddItem("android:attr/c", util::make_unique<String>(pool.MakeRef("OverlayFooC")))
214 .Build();
215
216 EXPECT_TRUE(a->Equals(expected.get()));
217 }
218
219 // TYPE_NULL is encoded as TYPE_REFERENCE with a value of 0. This is represented in AAPT2
220 // by a default constructed Reference value.
TEST(ResourcesValuesTest,EmptyReferenceFlattens)221 TEST(ResourcesValuesTest, EmptyReferenceFlattens) {
222 android::Res_value value = {};
223 ASSERT_TRUE(Reference().Flatten(&value));
224
225 EXPECT_THAT(value.dataType, Eq(android::Res_value::TYPE_REFERENCE));
226 EXPECT_THAT(value.data, Eq(0u));
227 }
228
TEST(ResourcesValuesTest,AttributeMatches)229 TEST(ResourcesValuesTest, AttributeMatches) {
230 constexpr const uint8_t TYPE_INT_DEC = android::Res_value::TYPE_INT_DEC;
231
232 Attribute attr1(TYPE_DIMENSION);
233 EXPECT_FALSE(attr1.Matches(*ResourceUtils::TryParseColor("#7fff00")));
234 EXPECT_TRUE(attr1.Matches(*ResourceUtils::TryParseFloat("23dp")));
235 EXPECT_TRUE(attr1.Matches(*ResourceUtils::TryParseReference("@android:string/foo")));
236
237 Attribute attr2(TYPE_INTEGER | TYPE_ENUM);
238 attr2.min_int = 0;
239 attr2.symbols.push_back(Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")),
240 static_cast<uint32_t>(-1)});
241 EXPECT_FALSE(attr2.Matches(*ResourceUtils::TryParseColor("#7fff00")));
242 EXPECT_TRUE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, static_cast<uint32_t>(-1))));
243 EXPECT_TRUE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, 1u)));
244 EXPECT_FALSE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, static_cast<uint32_t>(-2))));
245
246 Attribute attr3(TYPE_INTEGER | TYPE_FLAGS);
247 attr3.max_int = 100;
248 attr3.symbols.push_back(
249 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")), 0x01u});
250 attr3.symbols.push_back(
251 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/bar")), 0x02u});
252 attr3.symbols.push_back(
253 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/baz")), 0x04u});
254 attr3.symbols.push_back(
255 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/bat")), 0x80u});
256 EXPECT_FALSE(attr3.Matches(*ResourceUtils::TryParseColor("#7fff00")));
257 EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u | 0x02u)));
258 EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u | 0x02u | 0x80u)));
259
260 // Not a flag, but a value less than max_int.
261 EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x08u)));
262
263 // Not a flag and greater than max_int.
264 EXPECT_FALSE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 127u)));
265
266 Attribute attr4(TYPE_ENUM);
267 attr4.symbols.push_back(
268 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")), 0x01u});
269 EXPECT_TRUE(attr4.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u)));
270 EXPECT_FALSE(attr4.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x02u)));
271 }
272
TEST(ResourcesValuesTest,AttributeIsCompatible)273 TEST(ResourcesValuesTest, AttributeIsCompatible) {
274 Attribute attr_one(TYPE_STRING | TYPE_REFERENCE);
275 Attribute attr_two(TYPE_STRING);
276 Attribute attr_three(TYPE_ENUM);
277 Attribute attr_four(TYPE_REFERENCE);
278
279 EXPECT_TRUE(attr_one.IsCompatibleWith(attr_one));
280 EXPECT_TRUE(attr_one.IsCompatibleWith(attr_two));
281 EXPECT_FALSE(attr_one.IsCompatibleWith(attr_three));
282 EXPECT_FALSE(attr_one.IsCompatibleWith(attr_four));
283
284 EXPECT_TRUE(attr_two.IsCompatibleWith(attr_one));
285 EXPECT_TRUE(attr_two.IsCompatibleWith(attr_two));
286 EXPECT_FALSE(attr_two.IsCompatibleWith(attr_three));
287 EXPECT_FALSE(attr_two.IsCompatibleWith(attr_four));
288
289 EXPECT_FALSE(attr_three.IsCompatibleWith(attr_one));
290 EXPECT_FALSE(attr_three.IsCompatibleWith(attr_two));
291 EXPECT_FALSE(attr_three.IsCompatibleWith(attr_three));
292 EXPECT_FALSE(attr_three.IsCompatibleWith(attr_four));
293 }
294
295 } // namespace aapt
296