blob: 8062c2e6afeac3459e97435357c0c68f92c86633 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <sstream>
20#include <string>
21
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070025#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080026#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskid5083f62017-01-16 15:07:21 -080028using android::StringPiece;
29
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032constexpr const char* kXmlPreamble =
33 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 std::stringstream input(kXmlPreamble);
38 input << "<attr name=\"foo\"/>" << std::endl;
39 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
41 xml::XmlPullParser xml_parser(input);
42 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070043}
44
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045class ResourceParserTest : public ::testing::Test {
46 public:
47 void SetUp() override { context_ = test::ContextBuilder().Build(); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 ::testing::AssertionResult TestParse(const StringPiece& str) {
50 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 }
Adam Lesinski52364f72016-01-11 13:10:24 -080052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 ::testing::AssertionResult TestParse(const StringPiece& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 const ConfigDescription& config) {
55 std::stringstream input(kXmlPreamble);
56 input << "<resources>\n" << str << "\n</resources>" << std::endl;
57 ResourceParserOptions parserOptions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 config, parserOptions);
60 xml::XmlPullParser xmlParser(input);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return ::testing::AssertionFailure();
65 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066
67 protected:
68 ResourceTable table_;
69 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070};
71
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 ASSERT_NE(nullptr, str);
78 EXPECT_EQ(std::string(" hey there "), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -080079 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080}
81
82TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 ASSERT_NE(nullptr, str);
88 EXPECT_EQ(std::string("?123"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -080089 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090}
91
Adam Lesinski9f222042015-11-04 13:51:45 -080092TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 std::string input = "<string name=\"foo\">%d %s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 ASSERT_FALSE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 input = "<string name=\"foo\">%1$d %2$s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 ASSERT_TRUE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080098}
99
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700100TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800102 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700104 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 ASSERT_NE(nullptr, str);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700109
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700110 const std::string expected_str = "This is my aunt\u2019s fickle string";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 EXPECT_EQ(expected_str, *str->value->str);
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700112 EXPECT_EQ(2u, str->value->spans.size());
Adam Lesinski75421622017-01-06 15:20:04 -0800113 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 EXPECT_EQ(17u, str->value->spans[0].first_char);
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700117 EXPECT_EQ(30u, str->value->spans[0].last_char);
118
119 EXPECT_EQ(std::string("small"), *str->value->spans[1].name);
120 EXPECT_EQ(24u, str->value->spans[1].first_char);
121 EXPECT_EQ(30u, str->value->spans[1].last_char);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700122}
123
124TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 std::string input = "<string name=\"foo\"> This is what I think </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 ASSERT_NE(nullptr, str);
130 EXPECT_EQ(std::string("This is what I think"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -0800131 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 input = "<string name=\"foo2\">\" This is what I think \"</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 ASSERT_NE(nullptr, str);
138 EXPECT_EQ(std::string(" This is what I think "), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700139}
140
Adam Lesinski75421622017-01-06 15:20:04 -0800141TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
142 std::string input = R"EOF(
143 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
144 There are <xliff:source>no</xliff:source> apples</string>)EOF";
145 ASSERT_TRUE(TestParse(input));
146
147 String* str = test::GetValue<String>(&table_, "string/foo");
148 ASSERT_NE(nullptr, str);
149 EXPECT_EQ(StringPiece("There are no apples"), StringPiece(*str->value));
150 EXPECT_TRUE(str->untranslatable_sections.empty());
151}
152
153TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
154 std::string input = R"EOF(
155 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
156 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)EOF";
157 EXPECT_FALSE(TestParse(input));
158}
159
160TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
161 std::string input = R"EOF(
162 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
163 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 ASSERT_NE(nullptr, str);
168 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski75421622017-01-06 15:20:04 -0800169
170 ASSERT_EQ(1u, str->untranslatable_sections.size());
171
172 // We expect indices and lengths that span to include the whitespace
173 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
174 // needed (to deal with line breaks, etc.).
175 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
176 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
177}
178
179TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
180 std::string input = R"EOF(
181 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
182 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)EOF";
183 ASSERT_TRUE(TestParse(input));
184
185 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
186 ASSERT_NE(nullptr, str);
187 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value->str));
188
189 ASSERT_EQ(1u, str->untranslatable_sections.size());
190
191 // We expect indices and lengths that span to include the whitespace
192 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
193 // needed (to deal with line breaks, etc.).
194 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
195 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196}
197
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700198TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 std::string input = "<integer name=\"foo\">@null</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
203 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800204 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 // with a data value of 0.
Adam Lesinski75421622017-01-06 15:20:04 -0800206 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 ASSERT_NE(nullptr, integer);
Adam Lesinski75421622017-01-06 15:20:04 -0800208 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 EXPECT_EQ(0u, integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700210}
211
212TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 std::string input = "<integer name=\"foo\">@empty</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700215
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 BinaryPrimitive* integer =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 ASSERT_NE(nullptr, integer);
219 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
220 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700221}
222
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 std::string input =
225 "<attr name=\"foo\" format=\"string\"/>\n"
226 "<attr name=\"bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236}
237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238// Old AAPT allowed attributes to be defined under different configurations, but
239// ultimately
240// stored them with the default configuration. Check that we have the same
241// behavior.
242TEST_F(ResourceParserTest,
243 ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 std::string input = R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800246 <attr name="foo" />
247 <declare-styleable name="bar">
248 <attr name="baz" />
249 </declare-styleable>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800251
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/foo",
253 watch_config));
254 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/baz",
255 watch_config));
256 EXPECT_EQ(nullptr, test::GetValueForConfig<Styleable>(
257 &table_, "styleable/bar", watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/foo"));
260 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/baz"));
261 EXPECT_NE(nullptr, test::GetValue<Styleable>(&table_, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800262}
263
Adam Lesinskia5870652015-11-20 15:32:30 -0800264TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 std::string input =
266 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask);
272 EXPECT_EQ(10, attr->min_int);
273 EXPECT_EQ(23, attr->max_int);
Adam Lesinskia5870652015-11-20 15:32:30 -0800274}
275
276TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 std::string input =
278 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 ASSERT_FALSE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800280}
281
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 std::string input =
284 "<declare-styleable name=\"Styleable\">\n"
285 " <attr name=\"foo\" />\n"
286 "</declare-styleable>\n"
287 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
295TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 std::string input =
297 "<declare-styleable name=\"Theme\">"
298 " <attr name=\"foo\" />\n"
299 "</declare-styleable>\n"
300 "<declare-styleable name=\"Window\">\n"
301 " <attr name=\"foo\" format=\"boolean\"/>\n"
302 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308}
309
310TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 std::string input =
312 "<attr name=\"foo\">\n"
313 " <enum name=\"bar\" value=\"0\"/>\n"
314 " <enum name=\"bat\" value=\"1\"/>\n"
315 " <enum name=\"baz\" value=\"2\"/>\n"
316 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
320 ASSERT_NE(enum_attr, nullptr);
321 EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM);
322 ASSERT_EQ(enum_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
325 EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar");
326 EXPECT_EQ(enum_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
329 EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat");
330 EXPECT_EQ(enum_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
333 EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz");
334 EXPECT_EQ(enum_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335}
336
337TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 std::string input =
339 "<attr name=\"foo\">\n"
340 " <flag name=\"bar\" value=\"0\"/>\n"
341 " <flag name=\"bat\" value=\"1\"/>\n"
342 " <flag name=\"baz\" value=\"2\"/>\n"
343 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
347 ASSERT_NE(nullptr, flag_attr);
348 EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS);
349 ASSERT_EQ(flag_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
352 EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar");
353 EXPECT_EQ(flag_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
356 EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat");
357 EXPECT_EQ(flag_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
360 EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz");
361 EXPECT_EQ(flag_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 std::unique_ptr<BinaryPrimitive> flag_value =
364 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
365 ASSERT_NE(nullptr, flag_value);
366 EXPECT_EQ(flag_value->value.data, 1u | 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367}
368
369TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 std::string input =
371 "<attr name=\"foo\">\n"
372 " <enum name=\"bar\" value=\"0\"/>\n"
373 " <enum name=\"bat\" value=\"1\"/>\n"
374 " <enum name=\"bat\" value=\"2\"/>\n"
375 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800377}
378
379TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 std::string input =
381 "<style name=\"foo\" parent=\"@style/fu\">\n"
382 " <item name=\"bar\">#ffffffff</item>\n"
383 " <item name=\"bat\">@string/hey</item>\n"
384 " <item name=\"baz\"><b>hey</b></item>\n"
385 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800387
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 ASSERT_NE(nullptr, style);
390 AAPT_ASSERT_TRUE(style->parent);
391 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 EXPECT_EQ(test::ParseNameOrDie("style/fu"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 style->parent.value().name.value());
394 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 EXPECT_EQ(test::ParseNameOrDie("attr/baz"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407}
408
Adam Lesinski769de982015-04-10 19:43:55 -0700409TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 ASSERT_TRUE(TestParse(input));
Adam Lesinski769de982015-04-10 19:43:55 -0700412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 ASSERT_NE(nullptr, style);
415 AAPT_ASSERT_TRUE(style->parent);
416 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 EXPECT_EQ(test::ParseNameOrDie("com.app:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700419}
420
Adam Lesinski24aad162015-04-24 19:19:30 -0700421TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 std::string input =
423 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
424 " name=\"foo\" parent=\"app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 ASSERT_NE(nullptr, style);
429 AAPT_ASSERT_TRUE(style->parent);
430 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 EXPECT_EQ(test::ParseNameOrDie("android:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700433}
434
435TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 std::string input =
437 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" "
438 "name=\"foo\">\n"
439 " <item name=\"app:bar\">0</item>\n"
440 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 ASSERT_NE(nullptr, style);
445 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 EXPECT_EQ(test::ParseNameOrDie("android:attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700448}
449
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700450TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 std::string input = "<style name=\"foo.bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 ASSERT_NE(nullptr, style);
456 AAPT_ASSERT_TRUE(style->parent);
457 AAPT_ASSERT_TRUE(style->parent.value().name);
458 EXPECT_EQ(style->parent.value().name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 test::ParseNameOrDie("style/foo"));
460 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700461}
462
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463TEST_F(ResourceParserTest,
464 ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
465 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 ASSERT_NE(nullptr, style);
470 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700472}
473
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800474TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 std::string input =
476 R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 ASSERT_TRUE(TestParse(input));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 ASSERT_NE(nullptr, style);
481 AAPT_ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800483}
484
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 Id* id = test::GetValue<Id>(&table_, "id/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 ASSERT_NE(id, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491}
492
493TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 std::string input =
495 "<declare-styleable name=\"foo\">\n"
496 " <attr name=\"bar\" />\n"
497 " <attr name=\"bat\" format=\"string|reference\"/>\n"
498 " <attr name=\"baz\">\n"
499 " <enum name=\"foo\" value=\"1\"/>\n"
500 " </attr>\n"
501 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 AAPT_ASSERT_TRUE(result);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state);
Adam Lesinski9f222042015-11-04 13:51:45 -0800508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800516
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 EXPECT_EQ(1u, attr->symbols.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 EXPECT_NE(nullptr, test::GetValue<Id>(&table_, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 ASSERT_NE(styleable, nullptr);
526 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 styleable->entries[0].name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532}
533
Adam Lesinski467f1712015-11-16 17:35:44 -0800534TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 std::string input =
536 "<declare-styleable name=\"foo\" "
537 "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
538 " <attr name=\"*android:bar\" />\n"
539 " <attr name=\"privAndroid:bat\" />\n"
540 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 ASSERT_TRUE(TestParse(input));
542 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 ASSERT_NE(nullptr, styleable);
544 ASSERT_EQ(2u, styleable->entries.size());
Adam Lesinski467f1712015-11-16 17:35:44 -0800545
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 AAPT_ASSERT_TRUE(styleable->entries[0].name);
548 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800549
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 AAPT_ASSERT_TRUE(styleable->entries[1].name);
552 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800553}
554
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800555TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 std::string input =
557 "<array name=\"foo\">\n"
558 " <item>@string/ref</item>\n"
559 " <item>hey</item>\n"
560 " <item>23</item>\n"
561 "</array>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 ASSERT_NE(array, nullptr);
566 ASSERT_EQ(3u, array->items.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 EXPECT_NE(nullptr, ValueCast<Reference>(array->items[0].get()));
569 EXPECT_NE(nullptr, ValueCast<String>(array->items[1].get()));
570 EXPECT_NE(nullptr, ValueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571}
572
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700573TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 std::string input =
575 "<string-array name=\"foo\">\n"
576 " <item>\"Werk\"</item>\n"
577 "</string-array>\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 ASSERT_TRUE(TestParse(input));
579 EXPECT_NE(nullptr, test::GetValue<Array>(&table_, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700580}
581
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800582TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 std::string input =
584 "<plurals name=\"foo\">\n"
585 " <item quantity=\"other\">apples</item>\n"
586 " <item quantity=\"one\">apple</item>\n"
587 "</plurals>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800589
590 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
591 ASSERT_NE(nullptr, plural);
592 EXPECT_EQ(nullptr, plural->values[Plural::Zero]);
593 EXPECT_EQ(nullptr, plural->values[Plural::Two]);
594 EXPECT_EQ(nullptr, plural->values[Plural::Few]);
595 EXPECT_EQ(nullptr, plural->values[Plural::Many]);
596
597 EXPECT_NE(nullptr, plural->values[Plural::One]);
598 EXPECT_NE(nullptr, plural->values[Plural::Other]);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800599}
600
601TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 std::string input =
603 "<!--This is a comment-->\n"
604 "<string name=\"foo\">Hi</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 EXPECT_EQ(value->GetComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700610}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700611
Adam Lesinskie78fd612015-10-22 12:48:43 -0700612TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 std::string input =
614 "<!--One-->\n"
615 "<!--Two-->\n"
616 "<string name=\"foo\">Hi</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700619
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 EXPECT_EQ(value->GetComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700623}
624
625TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 std::string input =
627 "<!--One-->\n"
628 "<string name=\"foo\">\n"
629 " Hi\n"
630 "<!--Two-->\n"
631 "</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700632
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700634
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 EXPECT_EQ(value->GetComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800638}
639
Adam Lesinskica5638f2015-10-21 14:42:43 -0700640TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 // We only care about declare-styleable and enum/flag attributes because
642 // comments
643 // from those end up in R.java
644 std::string input = R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -0700645 <declare-styleable name="foo">
646 <!-- The name of the bar -->
647 <attr name="barName" format="string|reference" />
648 </declare-styleable>
649
650 <attr name="foo">
651 <!-- The very first -->
652 <enum name="one" value="1" />
653 </attr>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 ASSERT_NE(nullptr, styleable);
658 ASSERT_EQ(1u, styleable->entries.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700659
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 EXPECT_EQ(StringPiece("The name of the bar"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 styleable->entries.front().GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700662
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 ASSERT_NE(nullptr, attr);
665 ASSERT_EQ(1u, attr->symbols.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700666
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 EXPECT_EQ(StringPiece("The very first"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 attr->symbols.front().symbol.GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700669}
670
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800671/*
672 * Declaring an ID as public should not require a separate definition
673 * (as an ID has no value).
674 */
675TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 Id* id = test::GetValue<Id>(&table_, "id/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 ASSERT_NE(nullptr, id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800681}
682
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800683TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 std::string input = R"EOF(
Adam Lesinski7751afc2016-01-06 15:45:28 -0800685 <string name="foo" product="phone">hi</string>
686 <string name="foo" product="no-sdcard">ho</string>
687 <string name="bar" product="">wee</string>
688 <string name="baz">woo</string>
689 <string name="bit" product="phablet">hoot</string>
690 <string name="bot" product="default">yes</string>
691 )EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
695 &table_, "string/foo",
696 ConfigDescription::DefaultConfig(), "phone"));
697 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
698 &table_, "string/foo",
699 ConfigDescription::DefaultConfig(), "no-sdcard"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 test::GetValueForConfigAndProduct<String>(
702 &table_, "string/bar", ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 test::GetValueForConfigAndProduct<String>(
705 &table_, "string/baz", ConfigDescription::DefaultConfig(), ""));
706 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
707 &table_, "string/bit",
708 ConfigDescription::DefaultConfig(), "phablet"));
709 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
710 &table_, "string/bot",
711 ConfigDescription::DefaultConfig(), "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700712}
713
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800714TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 std::string input = R"EOF(
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800716 <public-group type="attr" first-id="0x01010040">
717 <public name="foo" />
718 <public name="bar" />
719 </public-group>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800721
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 AAPT_ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800725
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 AAPT_ASSERT_TRUE(result.value().package->id);
727 AAPT_ASSERT_TRUE(result.value().type->id);
728 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 ResourceId actual_id(result.value().package->id.value(),
730 result.value().type->id.value(),
731 result.value().entry->id.value());
732 EXPECT_EQ(ResourceId(0x01010040), actual_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 AAPT_ASSERT_TRUE(result);
736
737 AAPT_ASSERT_TRUE(result.value().package->id);
738 AAPT_ASSERT_TRUE(result.value().type->id);
739 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 actual_id = ResourceId(result.value().package->id.value(),
741 result.value().type->id.value(),
742 result.value().entry->id.value());
743 EXPECT_EQ(ResourceId(0x01010041), actual_id);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800744}
745
Adam Lesinskifa105052015-11-07 13:34:39 -0800746TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 std::string input =
748 R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 ASSERT_TRUE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800750
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 ASSERT_FALSE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800753}
754
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755TEST_F(ResourceParserTest,
756 AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
757 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 ASSERT_TRUE(TestParse(input));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800759
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 AAPT_ASSERT_TRUE(result);
763 const ResourceEntry* entry = result.value().entry;
764 ASSERT_NE(nullptr, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 EXPECT_EQ(SymbolState::kUndefined, entry->symbol_status.state);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800766}
767
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800768TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 std::string input =
770 R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700771 ASSERT_TRUE(TestParse(input));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800772
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 BinaryPrimitive* val =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 ASSERT_NE(nullptr, val);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800776
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800778}
779
Adam Lesinski86d67df2017-01-31 13:47:27 -0800780TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
781 std::string input = R"EOF(<item name="foo" type="configVarying">Hey</item>)EOF";
782 ASSERT_TRUE(TestParse(input));
783 ASSERT_NE(nullptr, test::GetValue<String>(&table_, "configVarying/foo"));
784}
785
786TEST_F(ResourceParserTest, ParseBagElement) {
787 std::string input =
788 R"EOF(<bag name="bag" type="configVarying"><item name="test">Hello!</item></bag>)EOF";
789 ASSERT_TRUE(TestParse(input));
790
791 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
792 ASSERT_NE(nullptr, val);
793
794 ASSERT_EQ(1u, val->entries.size());
795 EXPECT_EQ(Reference(test::ParseNameOrDie("attr/test")), val->entries[0].key);
796 EXPECT_NE(nullptr, ValueCast<RawString>(val->entries[0].value.get()));
797}
798
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799} // namespace aapt