1 /*
2 * Copyright (C) 2015 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 "ResourceUtils.h"
18
19 #include "SdkConstants.h"
20 #include "Resource.h"
21 #include "test/Test.h"
22
23 using ::aapt::test::ValueEq;
24 using ::android::Res_value;
25 using ::android::ResTable_map;
26 using ::testing::Eq;
27 using ::testing::NotNull;
28 using ::testing::Pointee;
29
30 namespace aapt {
31
TEST(ResourceUtilsTest,ParseBool)32 TEST(ResourceUtilsTest, ParseBool) {
33 EXPECT_THAT(ResourceUtils::ParseBool("true"), Eq(Maybe<bool>(true)));
34 EXPECT_THAT(ResourceUtils::ParseBool("TRUE"), Eq(Maybe<bool>(true)));
35 EXPECT_THAT(ResourceUtils::ParseBool("True"), Eq(Maybe<bool>(true)));
36
37 EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(Maybe<bool>(false)));
38 EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(Maybe<bool>(false)));
39 EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(Maybe<bool>(false)));
40
41 EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(Maybe<bool>(false)));
42 }
43
TEST(ResourceUtilsTest,ParseResourceName)44 TEST(ResourceUtilsTest, ParseResourceName) {
45 ResourceNameRef actual;
46 bool actual_priv = false;
47 EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, &actual_priv));
48 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
49 EXPECT_FALSE(actual_priv);
50
51 EXPECT_TRUE(ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
52 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
53 EXPECT_FALSE(actual_priv);
54
55 EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, &actual_priv));
56 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
57 EXPECT_TRUE(actual_priv);
58
59 EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
60 }
61
TEST(ResourceUtilsTest,ParseReferenceWithNoPackage)62 TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
63 ResourceNameRef actual;
64 bool create = false;
65 bool private_ref = false;
66 EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, &private_ref));
67 EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
68 EXPECT_FALSE(create);
69 EXPECT_FALSE(private_ref);
70 }
71
TEST(ResourceUtilsTest,ParseReferenceWithPackage)72 TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
73 ResourceNameRef actual;
74 bool create = false;
75 bool private_ref = false;
76 EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, &create, &private_ref));
77 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
78 EXPECT_FALSE(create);
79 EXPECT_FALSE(private_ref);
80 }
81
TEST(ResourceUtilsTest,ParseReferenceWithSurroundingWhitespace)82 TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
83 ResourceNameRef actual;
84 bool create = false;
85 bool private_ref = false;
86 EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", &actual, &create, &private_ref));
87 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
88 EXPECT_FALSE(create);
89 EXPECT_FALSE(private_ref);
90 }
91
TEST(ResourceUtilsTest,ParseAutoCreateIdReference)92 TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
93 ResourceNameRef actual;
94 bool create = false;
95 bool private_ref = false;
96 EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, &create, &private_ref));
97 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
98 EXPECT_TRUE(create);
99 EXPECT_FALSE(private_ref);
100 }
101
TEST(ResourceUtilsTest,ParsePrivateReference)102 TEST(ResourceUtilsTest, ParsePrivateReference) {
103 ResourceNameRef actual;
104 bool create = false;
105 bool private_ref = false;
106 EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, &create, &private_ref));
107 EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
108 EXPECT_FALSE(create);
109 EXPECT_TRUE(private_ref);
110 }
111
TEST(ResourceUtilsTest,ParseBinaryDynamicReference)112 TEST(ResourceUtilsTest, ParseBinaryDynamicReference) {
113 android::Res_value value = {};
114 value.data = util::HostToDevice32(0x01);
115 value.dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
116 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(ResourceType::kId,
117 android::ConfigDescription(),
118 android::ResStringPool(), value,
119 nullptr);
120
121 Reference* ref = ValueCast<Reference>(item.get());
122 EXPECT_TRUE(ref->is_dynamic);
123 EXPECT_EQ(ref->id.value().id, 0x01);
124 }
125
TEST(ResourceUtilsTest,FailToParseAutoCreateNonIdReference)126 TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
127 bool create = false;
128 bool private_ref = false;
129 ResourceNameRef actual;
130 EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref));
131 }
132
TEST(ResourceUtilsTest,ParseAttributeReferences)133 TEST(ResourceUtilsTest, ParseAttributeReferences) {
134 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
135 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
136 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
137 EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
138 }
139
TEST(ResourceUtilsTest,FailParseIncompleteReference)140 TEST(ResourceUtilsTest, FailParseIncompleteReference) {
141 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
142 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
143 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
144 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
145 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
146 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
147 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
148 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
149 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
150 EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
151 }
152
TEST(ResourceUtilsTest,ParseStyleParentReference)153 TEST(ResourceUtilsTest, ParseStyleParentReference) {
154 const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
155 const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
156
157 std::string err_str;
158 Maybe<Reference> ref = ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
159 ASSERT_TRUE(ref);
160 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
161
162 ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
163 ASSERT_TRUE(ref);
164 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
165
166 ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
167 ASSERT_TRUE(ref);
168 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
169
170 ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
171 ASSERT_TRUE(ref);
172 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
173
174 ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
175 ASSERT_TRUE(ref);
176 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
177
178 ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
179 ASSERT_TRUE(ref);
180 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
181
182 ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
183 ASSERT_TRUE(ref);
184 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
185
186 ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
187 ASSERT_TRUE(ref);
188 EXPECT_THAT(ref.value().name, Eq(make_value(kStyleFooName)));
189
190 ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
191 ASSERT_TRUE(ref);
192 EXPECT_THAT(ref.value().name, Eq(make_value(kAndroidStyleFooName)));
193 EXPECT_TRUE(ref.value().private_reference);
194 }
195
TEST(ResourceUtilsTest,ParseEmptyFlag)196 TEST(ResourceUtilsTest, ParseEmptyFlag) {
197 std::unique_ptr<Attribute> attr = test::AttributeBuilder()
198 .SetTypeMask(ResTable_map::TYPE_FLAGS)
199 .AddItem("one", 0x01)
200 .AddItem("two", 0x02)
201 .Build();
202
203 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
204 ASSERT_THAT(result, NotNull());
205 EXPECT_THAT(result->value.data, Eq(0u));
206 }
207
TEST(ResourceUtilsTest,NullIsEmptyReference)208 TEST(ResourceUtilsTest, NullIsEmptyReference) {
209 ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
210 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
211 }
212
TEST(ResourceUtilsTest,EmptyIsBinaryPrimitive)213 TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
214 ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
215 ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
216 }
217
TEST(ResourceUtilsTest,ItemsWithWhitespaceAreParsedCorrectly)218 TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
219 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER),
220 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
221 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN),
222 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
223
224 const float expected_float = 12.0f;
225 const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
226 EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT),
227 Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
228 }
229
TEST(ResourceUtilsTest,ParseSdkVersionWithCodename)230 TEST(ResourceUtilsTest, ParseSdkVersionWithCodename) {
231 EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q"), Eq(Maybe<int>(10000)));
232 EXPECT_THAT(
233 ResourceUtils::ParseSdkVersion("Q.fingerprint"),
234 Eq(Maybe<int>(10000)));
235
236 EXPECT_THAT(ResourceUtils::ParseSdkVersion("R"), Eq(Maybe<int>(10000)));
237 EXPECT_THAT(
238 ResourceUtils::ParseSdkVersion("R.fingerprint"),
239 Eq(Maybe<int>(10000)));
240 }
241
TEST(ResourceUtilsTest,StringBuilderWhitespaceRemoval)242 TEST(ResourceUtilsTest, StringBuilderWhitespaceRemoval) {
243 EXPECT_THAT(ResourceUtils::StringBuilder()
244 .AppendText(" hey guys ")
245 .AppendText(" this is so cool ")
246 .to_string(),
247 Eq(" hey guys this is so cool "));
248 EXPECT_THAT(ResourceUtils::StringBuilder()
249 .AppendText(" \" wow, so many \t ")
250 .AppendText("spaces. \"what? ")
251 .to_string(),
252 Eq(" wow, so many \t spaces. what? "));
253 EXPECT_THAT(ResourceUtils::StringBuilder()
254 .AppendText(" where \t ")
255 .AppendText(" \nis the pie?")
256 .to_string(),
257 Eq(" where is the pie?"));
258 }
259
TEST(ResourceUtilsTest,StringBuilderEscaping)260 TEST(ResourceUtilsTest, StringBuilderEscaping) {
261 EXPECT_THAT(ResourceUtils::StringBuilder()
262 .AppendText("hey guys\\n ")
263 .AppendText(" this \\t is so\\\\ cool")
264 .to_string(),
265 Eq("hey guys\n this \t is so\\ cool"));
266 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\@\\?\\#\\\\\\'").to_string(),
267 Eq("@?#\\\'"));
268 }
269
TEST(ResourceUtilsTest,StringBuilderMisplacedQuote)270 TEST(ResourceUtilsTest, StringBuilderMisplacedQuote) {
271 ResourceUtils::StringBuilder builder;
272 EXPECT_FALSE(builder.AppendText("they're coming!"));
273 }
274
TEST(ResourceUtilsTest,StringBuilderUnicodeCodes)275 TEST(ResourceUtilsTest, StringBuilderUnicodeCodes) {
276 EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\u00AF\\u0AF0 woah").to_string(),
277 Eq("\u00AF\u0AF0 woah"));
278 EXPECT_FALSE(ResourceUtils::StringBuilder().AppendText("\\u00 yo"));
279 }
280
TEST(ResourceUtilsTest,StringBuilderPreserveSpaces)281 TEST(ResourceUtilsTest, StringBuilderPreserveSpaces) {
282 EXPECT_THAT(ResourceUtils::StringBuilder(true /*preserve_spaces*/).AppendText("\"").to_string(),
283 Eq("\""));
284 }
285
286 } // namespace aapt
287