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 "ResourceParser.h"
18
19 #include <sstream>
20 #include <string>
21
22 #include "ResourceTable.h"
23 #include "ResourceUtils.h"
24 #include "ResourceValues.h"
25 #include "io/StringStream.h"
26 #include "test/Test.h"
27 #include "xml/XmlPullParser.h"
28
29 using ::aapt::io::StringInputStream;
30 using ::aapt::test::StrValueEq;
31 using ::aapt::test::ValueEq;
32 using ::android::ConfigDescription;
33 using ::android::Res_value;
34 using ::android::ResTable_map;
35 using ::android::StringPiece;
36 using ::testing::Eq;
37 using ::testing::IsEmpty;
38 using ::testing::IsNull;
39 using ::testing::NotNull;
40 using ::testing::Pointee;
41 using ::testing::SizeIs;
42 using ::testing::StrEq;
43
44 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
45
46 namespace aapt {
47
48 constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
49
TEST(ResourceParserSingleTest,FailToParseWithNoRootResourcesElement)50 TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
51 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
52 ResourceTable table;
53 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
54
55 std::string input = kXmlPreamble;
56 input += R"(<attr name="foo"/>)";
57 StringInputStream in(input);
58 xml::XmlPullParser xml_parser(&in);
59 ASSERT_FALSE(parser.Parse(&xml_parser));
60 }
61
62 class ResourceParserTest : public ::testing::Test {
63 public:
SetUp()64 void SetUp() override {
65 context_ = test::ContextBuilder().Build();
66 }
67
TestParse(const StringPiece & str)68 ::testing::AssertionResult TestParse(const StringPiece& str) {
69 return TestParse(str, ConfigDescription{});
70 }
71
TestParse(const StringPiece & str,const ConfigDescription & config)72 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
73 ResourceParserOptions parserOptions;
74 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
75 parserOptions);
76
77 std::string input = kXmlPreamble;
78 input += "<resources>\n";
79 input.append(str.data(), str.size());
80 input += "\n</resources>";
81 StringInputStream in(input);
82 xml::XmlPullParser xmlParser(&in);
83 if (parser.Parse(&xmlParser)) {
84 return ::testing::AssertionSuccess();
85 }
86 return ::testing::AssertionFailure();
87 }
88
89 protected:
90 ResourceTable table_;
91 std::unique_ptr<IAaptContext> context_;
92 };
93
TEST_F(ResourceParserTest,ParseQuotedString)94 TEST_F(ResourceParserTest, ParseQuotedString) {
95 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
96
97 String* str = test::GetValue<String>(&table_, "string/foo");
98 ASSERT_THAT(str, NotNull());
99 EXPECT_THAT(*str, StrValueEq(" hey there "));
100 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
101
102 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
103 str = test::GetValue<String>(&table_, "string/bar");
104 ASSERT_THAT(str, NotNull());
105 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
106
107 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
108 str = test::GetValue<String>(&table_, "string/baz");
109 ASSERT_THAT(str, NotNull());
110 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
111 }
112
113 TEST_F(ResourceParserTest, ParseEscapedString) {
114 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
115
116 String* str = test::GetValue<String>(&table_, "string/foo");
117 ASSERT_THAT(str, NotNull());
118 EXPECT_THAT(*str, StrValueEq("?123"));
119 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
120
121 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
122 str = test::GetValue<String>(&table_, "string/bar");
123 ASSERT_THAT(str, NotNull());
124 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
125 }
126
127 TEST_F(ResourceParserTest, ParseFormattedString) {
128 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
129 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
130 }
131
132 TEST_F(ResourceParserTest, ParseStyledString) {
133 // Use a surrogate pair unicode point so that we can verify that the span
134 // indices use UTF-16 length and not UTF-8 length.
135 std::string input =
136 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
137 ASSERT_TRUE(TestParse(input));
138
139 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
140 ASSERT_THAT(str, NotNull());
141
142 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
143 EXPECT_THAT(str->value->spans, SizeIs(2));
144 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
145
146 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
147 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
148 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
149
150 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
151 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
152 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
153 }
154
TEST_F(ResourceParserTest,ParseStringWithWhitespace)155 TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
156 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
157
158 String* str = test::GetValue<String>(&table_, "string/foo");
159 ASSERT_THAT(str, NotNull());
160 EXPECT_THAT(*str->value, StrEq("This is what I think"));
161 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
162
163 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
164
165 str = test::GetValue<String>(&table_, "string/foo2");
166 ASSERT_THAT(str, NotNull());
167 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
168 }
169
TEST_F(ResourceParserTest,ParseStringTruncateASCII)170 TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
171 // Tuncate leading and trailing whitespace
172 EXPECT_TRUE(TestParse(R"(<string name="foo"> Hello </string>)"));
173
174 String* str = test::GetValue<String>(&table_, "string/foo");
175 ASSERT_THAT(str, NotNull());
176 EXPECT_THAT(*str->value, StrEq("Hello"));
177 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
178
179 // AAPT does not truncate unicode whitespace
180 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
181
182 str = test::GetValue<String>(&table_, "string/foo2");
183 ASSERT_THAT(str, NotNull());
184 EXPECT_THAT(*str->value, StrEq(" Hello "));
185 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
186
187 // Preserve non-ASCII whitespace including extended ASCII characters
188 EXPECT_TRUE(TestParse(R"(<string name="foo3"> Hello World </string>)"));
189
190 str = test::GetValue<String>(&table_, "string/foo3");
191 ASSERT_THAT(str, NotNull());
192 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0"));
193 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
194
195 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
196
197 str = test::GetValue<String>(&table_, "string/foo4");
198 ASSERT_THAT(str, NotNull());
199 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
200 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
201 }
202
TEST_F(ResourceParserTest,ParseStyledStringWithWhitespace)203 TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
204 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
205 ASSERT_TRUE(TestParse(input));
206
207 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
208 ASSERT_THAT(str, NotNull());
209 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
210 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
211
212 ASSERT_THAT(str->value->spans, SizeIs(2u));
213 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
214 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
215 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
216
217 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
218 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
219 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
220 }
221
TEST_F(ResourceParserTest,ParseStringTranslatableAttribute)222 TEST_F(ResourceParserTest, ParseStringTranslatableAttribute) {
223 // If there is no translate attribute the default is 'true'
224 EXPECT_TRUE(TestParse(R"(<string name="foo1">Translate</string>)"));
225 String* str = test::GetValue<String>(&table_, "string/foo1");
226 ASSERT_THAT(str, NotNull());
227 ASSERT_TRUE(str->IsTranslatable());
228
229 // Explicit 'true' translate attribute
230 EXPECT_TRUE(TestParse(R"(<string name="foo2" translatable="true">Translate</string>)"));
231 str = test::GetValue<String>(&table_, "string/foo2");
232 ASSERT_THAT(str, NotNull());
233 ASSERT_TRUE(str->IsTranslatable());
234
235 // Explicit 'false' translate attribute
236 EXPECT_TRUE(TestParse(R"(<string name="foo3" translatable="false">Do not translate</string>)"));
237 str = test::GetValue<String>(&table_, "string/foo3");
238 ASSERT_THAT(str, NotNull());
239 ASSERT_FALSE(str->IsTranslatable());
240
241 // Invalid value for the translate attribute, should be boolean ('true' or 'false')
242 EXPECT_FALSE(TestParse(R"(<string name="foo4" translatable="yes">Translate</string>)"));
243 }
244
TEST_F(ResourceParserTest,IgnoreXliffTagsOtherThanG)245 TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
246 std::string input = R"(
247 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
248 There are <xliff:source>no</xliff:source> apples</string>)";
249 ASSERT_TRUE(TestParse(input));
250
251 String* str = test::GetValue<String>(&table_, "string/foo");
252 ASSERT_THAT(str, NotNull());
253 EXPECT_THAT(*str, StrValueEq("There are no apples"));
254 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
255 }
256
TEST_F(ResourceParserTest,NestedXliffGTagsAreIllegal)257 TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
258 std::string input = R"(
259 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
260 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
261 EXPECT_FALSE(TestParse(input));
262 }
263
TEST_F(ResourceParserTest,RecordUntranslateableXliffSectionsInString)264 TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
265 std::string input = R"(
266 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
267 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
268 ASSERT_TRUE(TestParse(input));
269
270 String* str = test::GetValue<String>(&table_, "string/foo");
271 ASSERT_THAT(str, NotNull());
272 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
273
274 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
275 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
276 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
277 }
278
TEST_F(ResourceParserTest,RecordUntranslateableXliffSectionsInStyledString)279 TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
280 std::string input = R"(
281 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
282 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
283 ASSERT_TRUE(TestParse(input));
284
285 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
286 ASSERT_THAT(str, NotNull());
287 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
288
289 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
290 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
291 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
292
293 ASSERT_THAT(str->value->spans, SizeIs(1u));
294 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
295 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
296 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
297 }
298
TEST_F(ResourceParserTest,ParseNull)299 TEST_F(ResourceParserTest, ParseNull) {
300 std::string input = R"(<integer name="foo">@null</integer>)";
301 ASSERT_TRUE(TestParse(input));
302
303 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
304 // a non-existing value, and this causes problems in styles when trying to
305 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
306 // with a data value of 0.
307 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
308 ASSERT_THAT(null_ref, NotNull());
309 EXPECT_FALSE(null_ref->name);
310 EXPECT_FALSE(null_ref->id);
311 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
312 }
313
TEST_F(ResourceParserTest,ParseEmpty)314 TEST_F(ResourceParserTest, ParseEmpty) {
315 std::string input = R"(<integer name="foo">@empty</integer>)";
316 ASSERT_TRUE(TestParse(input));
317
318 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
319 ASSERT_THAT(integer, NotNull());
320 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
321 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
322 }
323
TEST_F(ResourceParserTest,ParseAttr)324 TEST_F(ResourceParserTest, ParseAttr) {
325 std::string input = R"(
326 <attr name="foo" format="string"/>
327 <attr name="bar"/>)";
328 ASSERT_TRUE(TestParse(input));
329
330 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
331 ASSERT_THAT(attr, NotNull());
332 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
333
334 attr = test::GetValue<Attribute>(&table_, "attr/bar");
335 ASSERT_THAT(attr, NotNull());
336 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
337 }
338
339 // Old AAPT allowed attributes to be defined under different configurations, but ultimately
340 // stored them with the default configuration. Check that we have the same behavior.
TEST_F(ResourceParserTest,ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig)341 TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
342 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
343 std::string input = R"(
344 <attr name="foo" />
345 <declare-styleable name="bar">
346 <attr name="baz" format="reference"/>
347 </declare-styleable>)";
348 ASSERT_TRUE(TestParse(input, watch_config));
349
350 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
351 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
352 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
353
354 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
355 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
356 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
357 }
358
TEST_F(ResourceParserTest,ParseAttrWithMinMax)359 TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
360 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
361 ASSERT_TRUE(TestParse(input));
362
363 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
364 ASSERT_THAT(attr, NotNull());
365 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
366 EXPECT_THAT(attr->min_int, Eq(10));
367 EXPECT_THAT(attr->max_int, Eq(23));
368 }
369
TEST_F(ResourceParserTest,FailParseAttrWithMinMaxButNotInteger)370 TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
371 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
372 }
373
TEST_F(ResourceParserTest,ParseUseAndDeclOfAttr)374 TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
375 std::string input = R"(
376 <declare-styleable name="Styleable">
377 <attr name="foo" />
378 </declare-styleable>
379 <attr name="foo" format="string"/>)";
380 ASSERT_TRUE(TestParse(input));
381
382 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
383 ASSERT_THAT(attr, NotNull());
384 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
385 }
386
TEST_F(ResourceParserTest,ParseDoubleUseOfAttr)387 TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
388 std::string input = R"(
389 <declare-styleable name="Theme">
390 <attr name="foo" />
391 </declare-styleable>
392 <declare-styleable name="Window">
393 <attr name="foo" format="boolean"/>
394 </declare-styleable>)";
395 ASSERT_TRUE(TestParse(input));
396
397 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
398 ASSERT_THAT(attr, NotNull());
399 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
400 }
401
TEST_F(ResourceParserTest,ParseEnumAttr)402 TEST_F(ResourceParserTest, ParseEnumAttr) {
403 std::string input = R"(
404 <attr name="foo">
405 <enum name="bar" value="0"/>
406 <enum name="bat" value="0x1"/>
407 <enum name="baz" value="2"/>
408 </attr>)";
409 ASSERT_TRUE(TestParse(input));
410
411 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
412 ASSERT_THAT(enum_attr, NotNull());
413 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
414 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
415
416 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
417 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
418 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
419 EXPECT_THAT(enum_attr->symbols[0].type, Eq(Res_value::TYPE_INT_DEC));
420
421 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
422 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
423 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
424 EXPECT_THAT(enum_attr->symbols[1].type, Eq(Res_value::TYPE_INT_HEX));
425
426 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
427 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
428 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
429 EXPECT_THAT(enum_attr->symbols[2].type, Eq(Res_value::TYPE_INT_DEC));
430 }
431
TEST_F(ResourceParserTest,ParseFlagAttr)432 TEST_F(ResourceParserTest, ParseFlagAttr) {
433 std::string input = R"(
434 <attr name="foo">
435 <flag name="bar" value="0"/>
436 <flag name="bat" value="1"/>
437 <flag name="baz" value="2"/>
438 </attr>)";
439 ASSERT_TRUE(TestParse(input));
440
441 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
442 ASSERT_THAT(flag_attr, NotNull());
443 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
444 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
445
446 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
447 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
448 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
449
450 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
451 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
452 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
453
454 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
455 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
456 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
457
458 std::unique_ptr<BinaryPrimitive> flag_value =
459 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
460 ASSERT_THAT(flag_value, NotNull());
461 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
462 }
463
TEST_F(ResourceParserTest,FailToParseEnumAttrWithNonUniqueKeys)464 TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
465 std::string input = R"(
466 <attr name="foo">
467 <enum name="bar" value="0"/>
468 <enum name="bat" value="1"/>
469 <enum name="bat" value="2"/>
470 </attr>)";
471 ASSERT_FALSE(TestParse(input));
472 }
473
TEST_F(ResourceParserTest,ParseStyle)474 TEST_F(ResourceParserTest, ParseStyle) {
475 std::string input = R"(
476 <style name="foo" parent="@style/fu">
477 <item name="bar">#ffffffff</item>
478 <item name="bat">@string/hey</item>
479 <item name="baz"><b>hey</b></item>
480 </style>)";
481 ASSERT_TRUE(TestParse(input));
482
483 Style* style = test::GetValue<Style>(&table_, "style/foo");
484 ASSERT_THAT(style, NotNull());
485 ASSERT_TRUE(style->parent);
486 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
487 ASSERT_THAT(style->entries, SizeIs(3));
488
489 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
490 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
491 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
492 }
493
TEST_F(ResourceParserTest,ParseStyleWithShorthandParent)494 TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
495 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
496
497 Style* style = test::GetValue<Style>(&table_, "style/foo");
498 ASSERT_THAT(style, NotNull());
499 ASSERT_TRUE(style->parent);
500 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
501 }
502
TEST_F(ResourceParserTest,ParseStyleWithPackageAliasedParent)503 TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
504 std::string input = R"(
505 <style xmlns:app="http://schemas.android.com/apk/res/android"
506 name="foo" parent="app:Theme"/>)";
507 ASSERT_TRUE(TestParse(input));
508
509 Style* style = test::GetValue<Style>(&table_, "style/foo");
510 ASSERT_THAT(style, NotNull());
511 ASSERT_TRUE(style->parent);
512 ASSERT_TRUE(style->parent.value().name);
513 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
514 }
515
516 TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
517 std::string input = R"(
518 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
519 <item name="app:bar">0</item>
520 </style>)";
521 ASSERT_TRUE(TestParse(input));
522
523 Style* style = test::GetValue<Style>(&table_, "style/foo");
524 ASSERT_THAT(style, NotNull());
525 ASSERT_THAT(style->entries, SizeIs(1));
526 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
527 }
528
529 TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) {
530 std::string input = R"(
531 <style name="foo">
532 <item name="bar">
533 com.helloworld.AppClass
534 </item>
535 </style>)";
536 ASSERT_TRUE(TestParse(input));
537
538 Style* style = test::GetValue<Style>(&table_, "style/foo");
539 ASSERT_THAT(style, NotNull());
540 EXPECT_THAT(style->entries[0].value, NotNull());
541 RawString* value = ValueCast<RawString>(style->entries[0].value.get());
542 EXPECT_THAT(value, NotNull());
543 EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)"));
544 }
545
546
547 TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
548 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
549
550 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
551 ASSERT_THAT(style, NotNull());
552 ASSERT_TRUE(style->parent);
553 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
554 EXPECT_TRUE(style->parent_inferred);
555 }
556
557 TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
558 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
559
560 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
561 ASSERT_THAT(style, NotNull());
562 EXPECT_FALSE(style->parent);
563 EXPECT_FALSE(style->parent_inferred);
564 }
565
566 TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
567 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
568
569 Style* style = test::GetValue<Style>(&table_, "style/foo");
570 ASSERT_THAT(style, NotNull());
571 ASSERT_TRUE(style->parent);
572 EXPECT_TRUE(style->parent.value().private_reference);
573 }
574
575 TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
576 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
577 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
578 }
579
580 TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
581 std::string input = R"(
582 <declare-styleable name="foo">
583 <attr name="bar" />
584 <attr name="bat" format="string|reference"/>
585 <attr name="baz">
586 <enum name="foo" value="1"/>
587 </attr>
588 </declare-styleable>)";
589 ASSERT_TRUE(TestParse(input));
590
591 Maybe<ResourceTable::SearchResult> result =
592 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
593 ASSERT_TRUE(result);
594 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
595
596 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
597 ASSERT_THAT(attr, IsNull());
598
599 attr = test::GetValue<Attribute>(&table_, "attr/bat");
600 ASSERT_THAT(attr, NotNull());
601 EXPECT_TRUE(attr->IsWeak());
602
603 attr = test::GetValue<Attribute>(&table_, "attr/baz");
604 ASSERT_THAT(attr, NotNull());
605 EXPECT_TRUE(attr->IsWeak());
606 EXPECT_THAT(attr->symbols, SizeIs(1));
607
608 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
609
610 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
611 ASSERT_THAT(styleable, NotNull());
612 ASSERT_THAT(styleable->entries, SizeIs(3));
613
614 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
615 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
616 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
617 }
618
619 TEST_F(ResourceParserTest, ParseDeclareStyleablePreservingVisibility) {
620 StringInputStream input(R"(
621 <resources>
622 <declare-styleable name="foo">
623 <attr name="myattr" />
624 </declare-styleable>
625 <declare-styleable name="bar">
626 <attr name="myattr" />
627 </declare-styleable>
628 <public type="styleable" name="bar" />
629 </resources>)");
630 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
631 ConfigDescription::DefaultConfig(),
632 ResourceParserOptions{.preserve_visibility_of_styleables = true});
633
634 xml::XmlPullParser xml_parser(&input);
635 ASSERT_TRUE(parser.Parse(&xml_parser));
636
637 EXPECT_EQ(
638 table_.FindResource(test::ParseNameOrDie("styleable/foo")).value().entry->visibility.level,
639 Visibility::Level::kUndefined);
640 EXPECT_EQ(
641 table_.FindResource(test::ParseNameOrDie("styleable/bar")).value().entry->visibility.level,
642 Visibility::Level::kPublic);
643 }
644
645 TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
646 std::string input = R"(
647 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
648 name="foo">
649 <attr name="*android:bar" />
650 <attr name="privAndroid:bat" />
651 </declare-styleable>)";
652 ASSERT_TRUE(TestParse(input));
653 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
654 ASSERT_THAT(styleable, NotNull());
655 ASSERT_THAT(styleable->entries, SizeIs(2));
656
657 EXPECT_TRUE(styleable->entries[0].private_reference);
658 ASSERT_TRUE(styleable->entries[0].name);
659 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
660
661 EXPECT_TRUE(styleable->entries[1].private_reference);
662 ASSERT_TRUE(styleable->entries[1].name);
663 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
664 }
665
666 TEST_F(ResourceParserTest, ParseArray) {
667 std::string input = R"(
668 <array name="foo">
669 <item>@string/ref</item>
670 <item>hey</item>
671 <item>23</item>
672 </array>)";
673 ASSERT_TRUE(TestParse(input));
674
675 Array* array = test::GetValue<Array>(&table_, "array/foo");
676 ASSERT_THAT(array, NotNull());
677 ASSERT_THAT(array->elements, SizeIs(3));
678
679 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
680 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
681 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
682 }
683
684 TEST_F(ResourceParserTest, ParseStringArray) {
685 std::string input = R"(
686 <string-array name="foo">
687 <item>"Werk"</item>"
688 </string-array>)";
689 ASSERT_TRUE(TestParse(input));
690 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
691 }
692
TEST_F(ResourceParserTest,ParseArrayWithFormat)693 TEST_F(ResourceParserTest, ParseArrayWithFormat) {
694 std::string input = R"(
695 <array name="foo" format="string">
696 <item>100</item>
697 </array>)";
698 ASSERT_TRUE(TestParse(input));
699
700 Array* array = test::GetValue<Array>(&table_, "array/foo");
701 ASSERT_THAT(array, NotNull());
702 ASSERT_THAT(array->elements, SizeIs(1));
703
704 String* str = ValueCast<String>(array->elements[0].get());
705 ASSERT_THAT(str, NotNull());
706 EXPECT_THAT(*str, StrValueEq("100"));
707 }
708
TEST_F(ResourceParserTest,ParseArrayWithBadFormat)709 TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
710 std::string input = R"(
711 <array name="foo" format="integer">
712 <item>Hi</item>
713 </array>)";
714 ASSERT_FALSE(TestParse(input));
715 }
716
TEST_F(ResourceParserTest,ParsePlural)717 TEST_F(ResourceParserTest, ParsePlural) {
718 std::string input = R"(
719 <plurals name="foo">
720 <item quantity="other">apples</item>
721 <item quantity="one">apple</item>
722 </plurals>)";
723 ASSERT_TRUE(TestParse(input));
724
725 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
726 ASSERT_THAT(plural, NotNull());
727 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
728 EXPECT_THAT(plural->values[Plural::Two], IsNull());
729 EXPECT_THAT(plural->values[Plural::Few], IsNull());
730 EXPECT_THAT(plural->values[Plural::Many], IsNull());
731
732 EXPECT_THAT(plural->values[Plural::One], NotNull());
733 EXPECT_THAT(plural->values[Plural::Other], NotNull());
734 }
735
TEST_F(ResourceParserTest,ParseCommentsWithResource)736 TEST_F(ResourceParserTest, ParseCommentsWithResource) {
737 std::string input = R"(
738 <!--This is a comment-->
739 <string name="foo">Hi</string>)";
740 ASSERT_TRUE(TestParse(input));
741
742 String* value = test::GetValue<String>(&table_, "string/foo");
743 ASSERT_THAT(value, NotNull());
744 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
745 }
746
TEST_F(ResourceParserTest,DoNotCombineMultipleComments)747 TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
748 std::string input = R"(
749 <!--One-->
750 <!--Two-->
751 <string name="foo">Hi</string>)";
752
753 ASSERT_TRUE(TestParse(input));
754
755 String* value = test::GetValue<String>(&table_, "string/foo");
756 ASSERT_THAT(value, NotNull());
757 EXPECT_THAT(value->GetComment(), Eq("Two"));
758 }
759
TEST_F(ResourceParserTest,IgnoreCommentBeforeEndTag)760 TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
761 std::string input = R"(
762 <!--One-->
763 <string name="foo">
764 Hi
765 <!--Two-->
766 </string>)";
767 ASSERT_TRUE(TestParse(input));
768
769 String* value = test::GetValue<String>(&table_, "string/foo");
770 ASSERT_THAT(value, NotNull());
771 EXPECT_THAT(value->GetComment(), Eq("One"));
772 }
773
TEST_F(ResourceParserTest,ParseNestedComments)774 TEST_F(ResourceParserTest, ParseNestedComments) {
775 // We only care about declare-styleable and enum/flag attributes because
776 // comments from those end up in R.java
777 std::string input = R"(
778 <declare-styleable name="foo">
779 <!-- The name of the bar -->
780 <attr name="barName" format="string|reference" />
781 </declare-styleable>
782
783 <attr name="foo">
784 <!-- The very first -->
785 <enum name="one" value="1" />
786 </attr>)";
787 ASSERT_TRUE(TestParse(input));
788
789 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
790 ASSERT_THAT(styleable, NotNull());
791 ASSERT_THAT(styleable->entries, SizeIs(1));
792 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
793
794 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
795 ASSERT_THAT(attr, NotNull());
796 ASSERT_THAT(attr->symbols, SizeIs(1));
797 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
798 }
799
800 // Declaring an ID as public should not require a separate definition (as an ID has no value).
TEST_F(ResourceParserTest,ParsePublicIdAsDefinition)801 TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
802 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
803 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
804 }
805
TEST_F(ResourceParserTest,KeepAllProducts)806 TEST_F(ResourceParserTest, KeepAllProducts) {
807 std::string input = R"(
808 <string name="foo" product="phone">hi</string>
809 <string name="foo" product="no-sdcard">ho</string>
810 <string name="bar" product="">wee</string>
811 <string name="baz">woo</string>
812 <string name="bit" product="phablet">hoot</string>
813 <string name="bot" product="default">yes</string>)";
814 ASSERT_TRUE(TestParse(input));
815
816 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
817 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
818 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
819 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
820 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
821 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
822 }
823
TEST_F(ResourceParserTest,AutoIncrementIdsInPublicGroup)824 TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
825 std::string input = R"(
826 <public-group type="attr" first-id="0x01010040">
827 <public name="foo" />
828 <public name="bar" />
829 </public-group>)";
830 ASSERT_TRUE(TestParse(input));
831
832 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
833 ASSERT_TRUE(result);
834
835 ASSERT_TRUE(result.value().package->id);
836 ASSERT_TRUE(result.value().type->id);
837 ASSERT_TRUE(result.value().entry->id);
838 ResourceId actual_id(result.value().package->id.value(),
839 result.value().type->id.value(),
840 result.value().entry->id.value());
841 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
842
843 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
844 ASSERT_TRUE(result);
845
846 ASSERT_TRUE(result.value().package->id);
847 ASSERT_TRUE(result.value().type->id);
848 ASSERT_TRUE(result.value().entry->id);
849 actual_id = ResourceId(result.value().package->id.value(),
850 result.value().type->id.value(),
851 result.value().entry->id.value());
852 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
853 }
854
TEST_F(ResourceParserTest,StrongestSymbolVisibilityWins)855 TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
856 std::string input = R"(
857 <!-- private -->
858 <java-symbol type="string" name="foo" />
859 <!-- public -->
860 <public type="string" name="foo" id="0x01020000" />
861 <!-- private2 -->
862 <java-symbol type="string" name="foo" />)";
863 ASSERT_TRUE(TestParse(input));
864
865 Maybe<ResourceTable::SearchResult> result =
866 table_.FindResource(test::ParseNameOrDie("string/foo"));
867 ASSERT_TRUE(result);
868
869 ResourceEntry* entry = result.value().entry;
870 ASSERT_THAT(entry, NotNull());
871 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
872 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
873 }
874
TEST_F(ResourceParserTest,ExternalTypesShouldOnlyBeReferences)875 TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
876 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
877 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
878 }
879
TEST_F(ResourceParserTest,AddResourcesElementShouldAddEntryWithUndefinedSymbol)880 TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
881 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
882
883 Maybe<ResourceTable::SearchResult> result =
884 table_.FindResource(test::ParseNameOrDie("string/bar"));
885 ASSERT_TRUE(result);
886 const ResourceEntry* entry = result.value().entry;
887 ASSERT_THAT(entry, NotNull());
888 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
889 EXPECT_TRUE(entry->allow_new);
890 }
891
TEST_F(ResourceParserTest,ParseItemElementWithFormat)892 TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
893 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
894
895 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
896 ASSERT_THAT(val, NotNull());
897 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
898
899 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
900 }
901
902 // An <item> without a format specifier accepts all types of values.
TEST_F(ResourceParserTest,ParseItemElementWithoutFormat)903 TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
904 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
905
906 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
907 ASSERT_THAT(val, NotNull());
908 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
909 }
910
TEST_F(ResourceParserTest,ParseConfigVaryingItem)911 TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
912 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
913 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
914 }
915
TEST_F(ResourceParserTest,ParseBagElement)916 TEST_F(ResourceParserTest, ParseBagElement) {
917 std::string input = R"(
918 <bag name="bag" type="configVarying">
919 <item name="test">Hello!</item>
920 </bag>)";
921 ASSERT_TRUE(TestParse(input));
922
923 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
924 ASSERT_THAT(val, NotNull());
925 ASSERT_THAT(val->entries, SizeIs(1));
926
927 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
928 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
929 }
930
TEST_F(ResourceParserTest,ParseElementWithNoValue)931 TEST_F(ResourceParserTest, ParseElementWithNoValue) {
932 std::string input = R"(
933 <item type="drawable" format="reference" name="foo" />
934 <string name="foo" />)";
935 ASSERT_TRUE(TestParse(input));
936 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
937
938 String* str = test::GetValue<String>(&table_, "string/foo");
939 ASSERT_THAT(str, NotNull());
940 EXPECT_THAT(*str, StrValueEq(""));
941 }
942
TEST_F(ResourceParserTest,ParsePlatformIndependentNewline)943 TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
944 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
945 }
946
TEST_F(ResourceParserTest,ParseOverlayable)947 TEST_F(ResourceParserTest, ParseOverlayable) {
948 std::string input = R"(
949 <overlayable name="Name" actor="overlay://theme">
950 <policy type="signature">
951 <item type="string" name="foo" />
952 <item type="drawable" name="bar" />
953 </policy>
954 </overlayable>)";
955 ASSERT_TRUE(TestParse(input));
956
957 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
958 ASSERT_TRUE(search_result);
959 ASSERT_THAT(search_result.value().entry, NotNull());
960 ASSERT_TRUE(search_result.value().entry->overlayable_item);
961 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
962 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
963 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
964 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
965
966 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
967 ASSERT_TRUE(search_result);
968 ASSERT_THAT(search_result.value().entry, NotNull());
969 ASSERT_TRUE(search_result.value().entry->overlayable_item);
970 result_overlayable_item = search_result.value().entry->overlayable_item.value();
971 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
972 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
973 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
974 }
975
TEST_F(ResourceParserTest,ParseOverlayableRequiresName)976 TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
977 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
978 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
979 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
980 }
981
TEST_F(ResourceParserTest,ParseOverlayableBadActorFail)982 TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
983 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
984 }
985
TEST_F(ResourceParserTest,ParseOverlayablePolicy)986 TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
987 std::string input = R"(
988 <overlayable name="Name">
989 <policy type="product">
990 <item type="string" name="bar" />
991 </policy>
992 <policy type="system">
993 <item type="string" name="fiz" />
994 </policy>
995 <policy type="vendor">
996 <item type="string" name="fuz" />
997 </policy>
998 <policy type="public">
999 <item type="string" name="faz" />
1000 </policy>
1001 <policy type="signature">
1002 <item type="string" name="foz" />
1003 </policy>
1004 <policy type="odm">
1005 <item type="string" name="biz" />
1006 </policy>
1007 <policy type="oem">
1008 <item type="string" name="buz" />
1009 </policy>
1010 <policy type="actor">
1011 <item type="string" name="actor" />
1012 </policy>
1013 </overlayable>)";
1014 ASSERT_TRUE(TestParse(input));
1015
1016 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1017 ASSERT_TRUE(search_result);
1018 ASSERT_THAT(search_result.value().entry, NotNull());
1019 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1020 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1021 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1022 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION));
1023
1024 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
1025 ASSERT_TRUE(search_result);
1026 ASSERT_THAT(search_result.value().entry, NotNull());
1027 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1028 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1029 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1030 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SYSTEM_PARTITION));
1031
1032 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
1033 ASSERT_TRUE(search_result);
1034 ASSERT_THAT(search_result.value().entry, NotNull());
1035 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1036 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1037 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1038 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION));
1039
1040 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
1041 ASSERT_TRUE(search_result);
1042 ASSERT_THAT(search_result.value().entry, NotNull());
1043 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1044 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1045 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1046 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PUBLIC));
1047
1048 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
1049 ASSERT_TRUE(search_result);
1050 ASSERT_THAT(search_result.value().entry, NotNull());
1051 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1052 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1053 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1054 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
1055
1056 search_result = table_.FindResource(test::ParseNameOrDie("string/biz"));
1057 ASSERT_TRUE(search_result);
1058 ASSERT_THAT(search_result.value().entry, NotNull());
1059 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1060 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1061 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1062 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ODM_PARTITION));
1063
1064 search_result = table_.FindResource(test::ParseNameOrDie("string/buz"));
1065 ASSERT_TRUE(search_result);
1066 ASSERT_THAT(search_result.value().entry, NotNull());
1067 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1068 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1069 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1070 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::OEM_PARTITION));
1071
1072 search_result = table_.FindResource(test::ParseNameOrDie("string/actor"));
1073 ASSERT_TRUE(search_result);
1074 ASSERT_THAT(search_result.value().entry, NotNull());
1075 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1076 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1077 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1078 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ACTOR_SIGNATURE));
1079 }
1080
TEST_F(ResourceParserTest,ParseOverlayableNoPolicyError)1081 TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
1082 std::string input = R"(
1083 <overlayable name="Name">
1084 <item type="string" name="foo" />
1085 </overlayable>)";
1086 EXPECT_FALSE(TestParse(input));
1087
1088 input = R"(
1089 <overlayable name="Name">
1090 <policy>
1091 <item name="foo" />
1092 </policy>
1093 </overlayable>)";
1094 EXPECT_FALSE(TestParse(input));
1095 }
1096
TEST_F(ResourceParserTest,ParseOverlayableBadPolicyError)1097 TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1098 std::string input = R"(
1099 <overlayable name="Name">
1100 <policy type="illegal_policy">
1101 <item type="string" name="foo" />
1102 </policy>
1103 </overlayable>)";
1104 EXPECT_FALSE(TestParse(input));
1105
1106 input = R"(
1107 <overlayable name="Name">
1108 <policy type="product">
1109 <item name="foo" />
1110 </policy>
1111 </overlayable>)";
1112 EXPECT_FALSE(TestParse(input));
1113
1114 input = R"(
1115 <overlayable name="Name">
1116 <policy type="vendor">
1117 <item type="string" />
1118 </policy>
1119 </overlayable>)";
1120 EXPECT_FALSE(TestParse(input));
1121 }
1122
TEST_F(ResourceParserTest,ParseOverlayableMultiplePolicy)1123 TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1124 std::string input = R"(
1125 <overlayable name="Name">
1126 <policy type="vendor|public">
1127 <item type="string" name="foo" />
1128 </policy>
1129 <policy type="product|system">
1130 <item type="string" name="bar" />
1131 </policy>
1132 </overlayable>)";
1133 ASSERT_TRUE(TestParse(input));
1134
1135 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1136 ASSERT_TRUE(search_result);
1137 ASSERT_THAT(search_result.value().entry, NotNull());
1138 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1139 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1140 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1141 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION
1142 | PolicyFlags::PUBLIC));
1143
1144 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1145 ASSERT_TRUE(search_result);
1146 ASSERT_THAT(search_result.value().entry, NotNull());
1147 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1148 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1149 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1150 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
1151 | PolicyFlags::SYSTEM_PARTITION));
1152 }
1153
TEST_F(ResourceParserTest,DuplicateOverlayableIsError)1154 TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1155 std::string input = R"(
1156 <overlayable name="Name">
1157 <item type="string" name="foo" />
1158 <item type="string" name="foo" />
1159 </overlayable>)";
1160 EXPECT_FALSE(TestParse(input));
1161
1162 input = R"(
1163 <overlayable name="Name">
1164 <item type="string" name="foo" />
1165 </overlayable>
1166 <overlayable name="Name">
1167 <item type="string" name="foo" />
1168 </overlayable>)";
1169 EXPECT_FALSE(TestParse(input));
1170
1171 input = R"(
1172 <overlayable name="Name">
1173 <item type="string" name="foo" />
1174 </overlayable>
1175 <overlayable name="Other">
1176 <item type="string" name="foo" />
1177 </overlayable>)";
1178 EXPECT_FALSE(TestParse(input));
1179
1180 input = R"(
1181 <overlayable name="Name" actor="overlay://my.actor.one">
1182 <item type="string" name="foo" />
1183 </overlayable>
1184 <overlayable name="Other" actor="overlay://my.actor.two">
1185 <item type="string" name="foo" />
1186 </overlayable>)";
1187 EXPECT_FALSE(TestParse(input));
1188
1189 input = R"(
1190 <overlayable name="Name">
1191 <policy type="product">
1192 <item type="string" name="foo" />
1193 <item type="string" name="foo" />
1194 </policy>
1195 </overlayable>)";
1196 EXPECT_FALSE(TestParse(input));
1197
1198 input = R"(
1199 <overlayable name="Name">
1200 <policy type="product">
1201 <item type="string" name="foo" />
1202 </policy>
1203 <item type="string" name="foo" />
1204 </overlayable>)";
1205 EXPECT_FALSE(TestParse(input));
1206
1207 input = R"(
1208 <overlayable name="Name">
1209 <policy type="product">
1210 <item type="string" name="foo" />
1211 </policy>
1212 <policy type="vendor">
1213 <item type="string" name="foo" />
1214 </policy>
1215 </overlayable>)";
1216 EXPECT_FALSE(TestParse(input));
1217
1218 input = R"(
1219 <overlayable name="Name">
1220 <policy type="product">
1221 <item type="string" name="foo" />
1222 </policy>
1223 </overlayable>
1224
1225 <overlayable name="Name">
1226 <policy type="product">
1227 <item type="string" name="foo" />
1228 </policy>
1229 </overlayable>)";
1230 EXPECT_FALSE(TestParse(input));
1231 }
1232
1233 TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1234 std::string input = R"(
1235 <overlayable name="Name">
1236 <policy type="vendor|product">
1237 <policy type="public">
1238 <item type="string" name="foo" />
1239 </policy>
1240 </policy>
1241 </overlayable>)";
1242 EXPECT_FALSE(TestParse(input));
1243 }
1244
1245 TEST_F(ResourceParserTest, ParseIdItem) {
1246 std::string input = R"(
1247 <item name="foo" type="id">@id/bar</item>
1248 <item name="bar" type="id"/>
1249 <item name="baz" type="id"></item>)";
1250 ASSERT_TRUE(TestParse(input));
1251
1252 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1253 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1254 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1255
1256 input = R"(
1257 <id name="foo2">@id/bar</id>
1258 <id name="bar2"/>
1259 <id name="baz2"></id>)";
1260 ASSERT_TRUE(TestParse(input));
1261
1262 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1263 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1264 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1265
1266 // Reject attribute references
1267 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
1268 ASSERT_FALSE(TestParse(input));
1269
1270 // Reject non-references
1271 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
1272 ASSERT_FALSE(TestParse(input));
1273 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
1274 ASSERT_FALSE(TestParse(input));
1275 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
1276 ASSERT_FALSE(TestParse(input));
1277
1278 // Ids that reference other resource ids cannot be public
1279 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
1280 ASSERT_FALSE(TestParse(input));
1281 }
1282
TEST_F(ResourceParserTest,ParseCData)1283 TEST_F(ResourceParserTest, ParseCData) {
1284 // Double quotes should still change the state of whitespace processing
1285 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
1286 ASSERT_TRUE(TestParse(input));
1287 auto output = test::GetValue<String>(&table_, "string/foo");
1288 ASSERT_THAT(output, NotNull());
1289 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
1290
1291 input = R"(<string name="foo2"><![CDATA[Hello
1292 World]]></string>)";
1293 ASSERT_TRUE(TestParse(input));
1294 output = test::GetValue<String>(&table_, "string/foo2");
1295 ASSERT_THAT(output, NotNull());
1296 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
1297
1298 // Cdata blocks should have their whitespace trimmed
1299 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1300 ASSERT_TRUE(TestParse(input));
1301 output = test::GetValue<String>(&table_, "string/foo3");
1302 ASSERT_THAT(output, NotNull());
1303 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
1304
1305 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1306 ASSERT_TRUE(TestParse(input));
1307 output = test::GetValue<String>(&table_, "string/foo4");
1308 ASSERT_THAT(output, NotNull());
1309 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1310
1311 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1312 ASSERT_TRUE(TestParse(input));
1313 output = test::GetValue<String>(&table_, "string/foo5");
1314 ASSERT_THAT(output, NotNull());
1315 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1316
1317 // Single quotes must still be escaped
1318 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1319 ASSERT_FALSE(TestParse(input));
1320 }
1321
1322 } // namespace aapt
1323