blob: 06c3404561de8a6467e19c52a4ac13f23f262efe [file] [log] [blame]
Adam Lesinski8f7c5502017-03-02 17:45:01 -08001/*
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
21namespace aapt {
22
23TEST(ResourceValuesTest, PluralEquals) {
24 StringPool pool;
25
26 Plural a;
27 a.values[Plural::One] = util::make_unique<String>(pool.MakeRef("one"));
28 a.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("other"));
29
30 Plural b;
31 b.values[Plural::One] = util::make_unique<String>(pool.MakeRef("une"));
32 b.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("autre"));
33
34 Plural c;
35 c.values[Plural::One] = util::make_unique<String>(pool.MakeRef("one"));
36 c.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("other"));
37
38 EXPECT_FALSE(a.Equals(&b));
39 EXPECT_TRUE(a.Equals(&c));
40}
41
42TEST(ResourceValuesTest, PluralClone) {
43 StringPool pool;
44
45 Plural a;
46 a.values[Plural::One] = util::make_unique<String>(pool.MakeRef("one"));
47 a.values[Plural::Other] = util::make_unique<String>(pool.MakeRef("other"));
48
49 std::unique_ptr<Plural> b(a.Clone(&pool));
50 EXPECT_TRUE(a.Equals(b.get()));
51}
52
53TEST(ResourceValuesTest, ArrayEquals) {
54 StringPool pool;
55
56 Array a;
57 a.items.push_back(util::make_unique<String>(pool.MakeRef("one")));
58 a.items.push_back(util::make_unique<String>(pool.MakeRef("two")));
59
60 Array b;
61 b.items.push_back(util::make_unique<String>(pool.MakeRef("une")));
62 b.items.push_back(util::make_unique<String>(pool.MakeRef("deux")));
63
64 Array c;
65 c.items.push_back(util::make_unique<String>(pool.MakeRef("uno")));
66
67 Array d;
68 d.items.push_back(util::make_unique<String>(pool.MakeRef("one")));
69 d.items.push_back(util::make_unique<String>(pool.MakeRef("two")));
70
71 EXPECT_FALSE(a.Equals(&b));
72 EXPECT_FALSE(a.Equals(&c));
73 EXPECT_FALSE(b.Equals(&c));
74 EXPECT_TRUE(a.Equals(&d));
75}
76
77TEST(ResourceValuesTest, ArrayClone) {
78 StringPool pool;
79
80 Array a;
81 a.items.push_back(util::make_unique<String>(pool.MakeRef("one")));
82 a.items.push_back(util::make_unique<String>(pool.MakeRef("two")));
83
84 std::unique_ptr<Array> b(a.Clone(&pool));
85 EXPECT_TRUE(a.Equals(b.get()));
86}
87
88TEST(ResourceValuesTest, StyleEquals) {
89 StringPool pool;
90
91 std::unique_ptr<Style> a = test::StyleBuilder()
92 .SetParent("android:style/Parent")
93 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
94 .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
95 .Build();
96
97 std::unique_ptr<Style> b = test::StyleBuilder()
98 .SetParent("android:style/Parent")
99 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
100 .AddItem("android:attr/bar", ResourceUtils::TryParseInt("3"))
101 .Build();
102
103 std::unique_ptr<Style> c = test::StyleBuilder()
104 .SetParent("android:style/NoParent")
105 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
106 .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
107 .Build();
108
109 std::unique_ptr<Style> d = test::StyleBuilder()
110 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
111 .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
112 .Build();
113
114 std::unique_ptr<Style> e = test::StyleBuilder()
115 .SetParent("android:style/Parent")
116 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
117 .AddItem("android:attr/bat", ResourceUtils::TryParseInt("2"))
118 .Build();
119
120 std::unique_ptr<Style> f = test::StyleBuilder()
121 .SetParent("android:style/Parent")
122 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
123 .Build();
124
125 std::unique_ptr<Style> g = test::StyleBuilder()
126 .SetParent("android:style/Parent")
127 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
128 .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
129 .Build();
130
131 EXPECT_FALSE(a->Equals(b.get()));
132 EXPECT_FALSE(a->Equals(c.get()));
133 EXPECT_FALSE(a->Equals(d.get()));
134 EXPECT_FALSE(a->Equals(e.get()));
135 EXPECT_FALSE(a->Equals(f.get()));
136
137 EXPECT_TRUE(a->Equals(g.get()));
138}
139
140TEST(ResourceValuesTest, StyleClone) {
141 std::unique_ptr<Style> a = test::StyleBuilder()
142 .SetParent("android:style/Parent")
143 .AddItem("android:attr/foo", ResourceUtils::TryParseInt("1"))
144 .AddItem("android:attr/bar", ResourceUtils::TryParseInt("2"))
145 .Build();
146
147 std::unique_ptr<Style> b(a->Clone(nullptr));
148 EXPECT_TRUE(a->Equals(b.get()));
149}
150
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700151TEST(ResourceValuesTest, StyleMerges) {
152 StringPool pool_a;
153 StringPool pool_b;
154
155 std::unique_ptr<Style> a =
156 test::StyleBuilder()
157 .SetParent("android:style/Parent")
158 .AddItem("android:attr/a", util::make_unique<String>(pool_a.MakeRef("FooA")))
159 .AddItem("android:attr/b", util::make_unique<String>(pool_a.MakeRef("FooB")))
160 .Build();
161
162 std::unique_ptr<Style> b =
163 test::StyleBuilder()
164 .SetParent("android:style/OverlayParent")
165 .AddItem("android:attr/c", util::make_unique<String>(pool_b.MakeRef("OverlayFooC")))
166 .AddItem("android:attr/a", util::make_unique<String>(pool_b.MakeRef("OverlayFooA")))
167 .Build();
168
169 a->MergeWith(b.get(), &pool_a);
170
171 StringPool pool;
172 std::unique_ptr<Style> expected =
173 test::StyleBuilder()
174 .SetParent("android:style/OverlayParent")
175 .AddItem("android:attr/a", util::make_unique<String>(pool.MakeRef("OverlayFooA")))
176 .AddItem("android:attr/b", util::make_unique<String>(pool.MakeRef("FooB")))
177 .AddItem("android:attr/c", util::make_unique<String>(pool.MakeRef("OverlayFooC")))
178 .Build();
179
180 EXPECT_TRUE(a->Equals(expected.get()));
181}
182
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700183// TYPE_NULL is encoded as TYPE_REFERENCE with a value of 0. This is represented in AAPT2
184// by a default constructed Reference value.
185TEST(ResourcesValuesTest, EmptyReferenceFlattens) {
186 android::Res_value value = {};
187 ASSERT_TRUE(Reference().Flatten(&value));
188
189 EXPECT_EQ(android::Res_value::TYPE_REFERENCE, value.dataType);
190 EXPECT_EQ(0x0u, value.data);
191}
192
Adam Lesinski3124e7c2017-06-13 16:03:55 -0700193TEST(ResourcesValuesTest, AttributeMatches) {
194 constexpr const uint32_t TYPE_DIMENSION = android::ResTable_map::TYPE_DIMENSION;
195 constexpr const uint32_t TYPE_ENUM = android::ResTable_map::TYPE_ENUM;
196 constexpr const uint32_t TYPE_FLAGS = android::ResTable_map::TYPE_FLAGS;
197 constexpr const uint32_t TYPE_INTEGER = android::ResTable_map::TYPE_INTEGER;
198 constexpr const uint8_t TYPE_INT_DEC = android::Res_value::TYPE_INT_DEC;
199
200 Attribute attr1(false /*weak*/, TYPE_DIMENSION);
201 EXPECT_FALSE(attr1.Matches(*ResourceUtils::TryParseColor("#7fff00")));
202 EXPECT_TRUE(attr1.Matches(*ResourceUtils::TryParseFloat("23dp")));
203 EXPECT_TRUE(attr1.Matches(*ResourceUtils::TryParseReference("@android:string/foo")));
204
205 Attribute attr2(false /*weak*/, TYPE_INTEGER | TYPE_ENUM);
206 attr2.min_int = 0;
207 attr2.symbols.push_back(Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")),
208 static_cast<uint32_t>(-1)});
209 EXPECT_FALSE(attr2.Matches(*ResourceUtils::TryParseColor("#7fff00")));
210 EXPECT_TRUE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, static_cast<uint32_t>(-1))));
211 EXPECT_TRUE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, 1u)));
212 EXPECT_FALSE(attr2.Matches(BinaryPrimitive(TYPE_INT_DEC, static_cast<uint32_t>(-2))));
213
214 Attribute attr3(false /*weak*/, TYPE_INTEGER | TYPE_FLAGS);
215 attr3.max_int = 100;
216 attr3.symbols.push_back(
217 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")), 0x01u});
218 attr3.symbols.push_back(
219 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/bar")), 0x02u});
220 attr3.symbols.push_back(
221 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/baz")), 0x04u});
222 attr3.symbols.push_back(
223 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/bat")), 0x80u});
224 EXPECT_FALSE(attr3.Matches(*ResourceUtils::TryParseColor("#7fff00")));
225 EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u | 0x02u)));
226 EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u | 0x02u | 0x80u)));
227
228 // Not a flag, but a value less than max_int.
229 EXPECT_TRUE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x08u)));
230
231 // Not a flag and greater than max_int.
232 EXPECT_FALSE(attr3.Matches(BinaryPrimitive(TYPE_INT_DEC, 127u)));
233
234 Attribute attr4(false /*weak*/, TYPE_ENUM);
235 attr4.symbols.push_back(
236 Attribute::Symbol{Reference(test::ParseNameOrDie("android:id/foo")), 0x01u});
237 EXPECT_TRUE(attr4.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x01u)));
238 EXPECT_FALSE(attr4.Matches(BinaryPrimitive(TYPE_INT_DEC, 0x02u)));
239}
240
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800241} // namespace aapt