blob: eefa320a44184895bda0213e55b9ea81de0e1c9f [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 =
104 "<string name=\"foo\">This is my aunt\u2019s <b>string</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 Lesinskice5e56e2016-10-21 17:56:45 -0700110 const std::string expected_str = "This is my aunt\u2019s string";
111 EXPECT_EQ(expected_str, *str->value->str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 EXPECT_EQ(1u, 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);
117 EXPECT_EQ(23u, str->value->spans[0].last_char);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700118}
119
120TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 std::string input = "<string name=\"foo\"> This is what I think </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 ASSERT_NE(nullptr, str);
126 EXPECT_EQ(std::string("This is what I think"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -0800127 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700128
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 input = "<string name=\"foo2\">\" This is what I think \"</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 ASSERT_NE(nullptr, str);
134 EXPECT_EQ(std::string(" This is what I think "), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700135}
136
Adam Lesinski75421622017-01-06 15:20:04 -0800137TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
138 std::string input = R"EOF(
139 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
140 There are <xliff:source>no</xliff:source> apples</string>)EOF";
141 ASSERT_TRUE(TestParse(input));
142
143 String* str = test::GetValue<String>(&table_, "string/foo");
144 ASSERT_NE(nullptr, str);
145 EXPECT_EQ(StringPiece("There are no apples"), StringPiece(*str->value));
146 EXPECT_TRUE(str->untranslatable_sections.empty());
147}
148
149TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
150 std::string input = R"EOF(
151 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
152 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)EOF";
153 EXPECT_FALSE(TestParse(input));
154}
155
156TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
157 std::string input = R"EOF(
158 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
159 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 ASSERT_NE(nullptr, str);
164 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski75421622017-01-06 15:20:04 -0800165
166 ASSERT_EQ(1u, str->untranslatable_sections.size());
167
168 // We expect indices and lengths that span to include the whitespace
169 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
170 // needed (to deal with line breaks, etc.).
171 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
172 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
173}
174
175TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
176 std::string input = R"EOF(
177 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
178 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)EOF";
179 ASSERT_TRUE(TestParse(input));
180
181 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
182 ASSERT_NE(nullptr, str);
183 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value->str));
184
185 ASSERT_EQ(1u, str->untranslatable_sections.size());
186
187 // We expect indices and lengths that span to include the whitespace
188 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
189 // needed (to deal with line breaks, etc.).
190 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
191 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192}
193
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700194TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 std::string input = "<integer name=\"foo\">@null</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
199 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800200 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 // with a data value of 0.
Adam Lesinski75421622017-01-06 15:20:04 -0800202 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 ASSERT_NE(nullptr, integer);
Adam Lesinski75421622017-01-06 15:20:04 -0800204 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 EXPECT_EQ(0u, integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700206}
207
208TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 std::string input = "<integer name=\"foo\">@empty</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700211
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 BinaryPrimitive* integer =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 ASSERT_NE(nullptr, integer);
215 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
216 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700217}
218
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 std::string input =
221 "<attr name=\"foo\" format=\"string\"/>\n"
222 "<attr name=\"bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 attr = test::GetValue<Attribute>(&table_, "attr/bar");
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_ANY), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232}
233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234// Old AAPT allowed attributes to be defined under different configurations, but
235// ultimately
236// stored them with the default configuration. Check that we have the same
237// behavior.
238TEST_F(ResourceParserTest,
239 ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 std::string input = R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800242 <attr name="foo" />
243 <declare-styleable name="bar">
244 <attr name="baz" />
245 </declare-styleable>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800247
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/foo",
249 watch_config));
250 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/baz",
251 watch_config));
252 EXPECT_EQ(nullptr, test::GetValueForConfig<Styleable>(
253 &table_, "styleable/bar", watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/foo"));
256 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/baz"));
257 EXPECT_NE(nullptr, test::GetValue<Styleable>(&table_, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800258}
259
Adam Lesinskia5870652015-11-20 15:32:30 -0800260TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 std::string input =
262 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask);
268 EXPECT_EQ(10, attr->min_int);
269 EXPECT_EQ(23, attr->max_int);
Adam Lesinskia5870652015-11-20 15:32:30 -0800270}
271
272TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 std::string input =
274 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 ASSERT_FALSE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800276}
277
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 std::string input =
280 "<declare-styleable name=\"Styleable\">\n"
281 " <attr name=\"foo\" />\n"
282 "</declare-styleable>\n"
283 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289}
290
291TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 std::string input =
293 "<declare-styleable name=\"Theme\">"
294 " <attr name=\"foo\" />\n"
295 "</declare-styleable>\n"
296 "<declare-styleable name=\"Window\">\n"
297 " <attr name=\"foo\" format=\"boolean\"/>\n"
298 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304}
305
306TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 std::string input =
308 "<attr name=\"foo\">\n"
309 " <enum name=\"bar\" value=\"0\"/>\n"
310 " <enum name=\"bat\" value=\"1\"/>\n"
311 " <enum name=\"baz\" value=\"2\"/>\n"
312 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
316 ASSERT_NE(enum_attr, nullptr);
317 EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM);
318 ASSERT_EQ(enum_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
321 EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar");
322 EXPECT_EQ(enum_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
325 EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat");
326 EXPECT_EQ(enum_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
329 EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz");
330 EXPECT_EQ(enum_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331}
332
333TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 std::string input =
335 "<attr name=\"foo\">\n"
336 " <flag name=\"bar\" value=\"0\"/>\n"
337 " <flag name=\"bat\" value=\"1\"/>\n"
338 " <flag name=\"baz\" value=\"2\"/>\n"
339 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
343 ASSERT_NE(nullptr, flag_attr);
344 EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS);
345 ASSERT_EQ(flag_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800346
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
348 EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar");
349 EXPECT_EQ(flag_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
352 EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat");
353 EXPECT_EQ(flag_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
356 EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz");
357 EXPECT_EQ(flag_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 std::unique_ptr<BinaryPrimitive> flag_value =
360 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
361 ASSERT_NE(nullptr, flag_value);
362 EXPECT_EQ(flag_value->value.data, 1u | 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363}
364
365TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 std::string input =
367 "<attr name=\"foo\">\n"
368 " <enum name=\"bar\" value=\"0\"/>\n"
369 " <enum name=\"bat\" value=\"1\"/>\n"
370 " <enum name=\"bat\" value=\"2\"/>\n"
371 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800373}
374
375TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 std::string input =
377 "<style name=\"foo\" parent=\"@style/fu\">\n"
378 " <item name=\"bar\">#ffffffff</item>\n"
379 " <item name=\"bat\">@string/hey</item>\n"
380 " <item name=\"baz\"><b>hey</b></item>\n"
381 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 ASSERT_NE(nullptr, style);
386 AAPT_ASSERT_TRUE(style->parent);
387 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 EXPECT_EQ(test::ParseNameOrDie("style/fu"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 style->parent.value().name.value());
390 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 EXPECT_EQ(test::ParseNameOrDie("attr/baz"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403}
404
Adam Lesinski769de982015-04-10 19:43:55 -0700405TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 ASSERT_TRUE(TestParse(input));
Adam Lesinski769de982015-04-10 19:43:55 -0700408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 ASSERT_NE(nullptr, style);
411 AAPT_ASSERT_TRUE(style->parent);
412 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 EXPECT_EQ(test::ParseNameOrDie("com.app:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700415}
416
Adam Lesinski24aad162015-04-24 19:19:30 -0700417TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 std::string input =
419 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
420 " name=\"foo\" parent=\"app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 ASSERT_NE(nullptr, style);
425 AAPT_ASSERT_TRUE(style->parent);
426 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 EXPECT_EQ(test::ParseNameOrDie("android:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700429}
430
431TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 std::string input =
433 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" "
434 "name=\"foo\">\n"
435 " <item name=\"app:bar\">0</item>\n"
436 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 ASSERT_NE(nullptr, style);
441 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 EXPECT_EQ(test::ParseNameOrDie("android:attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700444}
445
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700446TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 std::string input = "<style name=\"foo.bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 ASSERT_NE(nullptr, style);
452 AAPT_ASSERT_TRUE(style->parent);
453 AAPT_ASSERT_TRUE(style->parent.value().name);
454 EXPECT_EQ(style->parent.value().name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 test::ParseNameOrDie("style/foo"));
456 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700457}
458
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459TEST_F(ResourceParserTest,
460 ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
461 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 ASSERT_NE(nullptr, style);
466 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700468}
469
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800470TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 std::string input =
472 R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 ASSERT_TRUE(TestParse(input));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 ASSERT_NE(nullptr, style);
477 AAPT_ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800479}
480
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 Id* id = test::GetValue<Id>(&table_, "id/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 ASSERT_NE(id, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487}
488
489TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 std::string input =
491 "<declare-styleable name=\"foo\">\n"
492 " <attr name=\"bar\" />\n"
493 " <attr name=\"bat\" format=\"string|reference\"/>\n"
494 " <attr name=\"baz\">\n"
495 " <enum name=\"foo\" value=\"1\"/>\n"
496 " </attr>\n"
497 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 AAPT_ASSERT_TRUE(result);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state);
Adam Lesinski9f222042015-11-04 13:51:45 -0800504
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 attr = test::GetValue<Attribute>(&table_, "attr/bat");
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/baz");
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 Lesinskicacb28f2016-10-19 12:18:14 -0700516 EXPECT_EQ(1u, attr->symbols.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700517
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 EXPECT_NE(nullptr, test::GetValue<Id>(&table_, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 ASSERT_NE(styleable, nullptr);
522 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 styleable->entries[0].name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528}
529
Adam Lesinski467f1712015-11-16 17:35:44 -0800530TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 std::string input =
532 "<declare-styleable name=\"foo\" "
533 "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
534 " <attr name=\"*android:bar\" />\n"
535 " <attr name=\"privAndroid:bat\" />\n"
536 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 ASSERT_TRUE(TestParse(input));
538 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 ASSERT_NE(nullptr, styleable);
540 ASSERT_EQ(2u, styleable->entries.size());
Adam Lesinski467f1712015-11-16 17:35:44 -0800541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 AAPT_ASSERT_TRUE(styleable->entries[0].name);
544 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800545
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 AAPT_ASSERT_TRUE(styleable->entries[1].name);
548 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800549}
550
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800551TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 std::string input =
553 "<array name=\"foo\">\n"
554 " <item>@string/ref</item>\n"
555 " <item>hey</item>\n"
556 " <item>23</item>\n"
557 "</array>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 ASSERT_NE(array, nullptr);
562 ASSERT_EQ(3u, array->items.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 EXPECT_NE(nullptr, ValueCast<Reference>(array->items[0].get()));
565 EXPECT_NE(nullptr, ValueCast<String>(array->items[1].get()));
566 EXPECT_NE(nullptr, ValueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567}
568
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700569TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 std::string input =
571 "<string-array name=\"foo\">\n"
572 " <item>\"Werk\"</item>\n"
573 "</string-array>\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 ASSERT_TRUE(TestParse(input));
575 EXPECT_NE(nullptr, test::GetValue<Array>(&table_, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700576}
577
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 std::string input =
580 "<plurals name=\"foo\">\n"
581 " <item quantity=\"other\">apples</item>\n"
582 " <item quantity=\"one\">apple</item>\n"
583 "</plurals>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800585
586 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
587 ASSERT_NE(nullptr, plural);
588 EXPECT_EQ(nullptr, plural->values[Plural::Zero]);
589 EXPECT_EQ(nullptr, plural->values[Plural::Two]);
590 EXPECT_EQ(nullptr, plural->values[Plural::Few]);
591 EXPECT_EQ(nullptr, plural->values[Plural::Many]);
592
593 EXPECT_NE(nullptr, plural->values[Plural::One]);
594 EXPECT_NE(nullptr, plural->values[Plural::Other]);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800595}
596
597TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 std::string input =
599 "<!--This is a comment-->\n"
600 "<string name=\"foo\">Hi</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 EXPECT_EQ(value->GetComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700606}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700607
Adam Lesinskie78fd612015-10-22 12:48:43 -0700608TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 std::string input =
610 "<!--One-->\n"
611 "<!--Two-->\n"
612 "<string name=\"foo\">Hi</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700613
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700615
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 EXPECT_EQ(value->GetComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700619}
620
621TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 std::string input =
623 "<!--One-->\n"
624 "<string name=\"foo\">\n"
625 " Hi\n"
626 "<!--Two-->\n"
627 "</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700630
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 EXPECT_EQ(value->GetComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800634}
635
Adam Lesinskica5638f2015-10-21 14:42:43 -0700636TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 // We only care about declare-styleable and enum/flag attributes because
638 // comments
639 // from those end up in R.java
640 std::string input = R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -0700641 <declare-styleable name="foo">
642 <!-- The name of the bar -->
643 <attr name="barName" format="string|reference" />
644 </declare-styleable>
645
646 <attr name="foo">
647 <!-- The very first -->
648 <enum name="one" value="1" />
649 </attr>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 ASSERT_NE(nullptr, styleable);
654 ASSERT_EQ(1u, styleable->entries.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700655
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 EXPECT_EQ(StringPiece("The name of the bar"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 styleable->entries.front().GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700658
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 ASSERT_NE(nullptr, attr);
661 ASSERT_EQ(1u, attr->symbols.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700662
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 EXPECT_EQ(StringPiece("The very first"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 attr->symbols.front().symbol.GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700665}
666
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800667/*
668 * Declaring an ID as public should not require a separate definition
669 * (as an ID has no value).
670 */
671TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 Id* id = test::GetValue<Id>(&table_, "id/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 ASSERT_NE(nullptr, id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800677}
678
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800679TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 std::string input = R"EOF(
Adam Lesinski7751afc2016-01-06 15:45:28 -0800681 <string name="foo" product="phone">hi</string>
682 <string name="foo" product="no-sdcard">ho</string>
683 <string name="bar" product="">wee</string>
684 <string name="baz">woo</string>
685 <string name="bit" product="phablet">hoot</string>
686 <string name="bot" product="default">yes</string>
687 )EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700689
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
691 &table_, "string/foo",
692 ConfigDescription::DefaultConfig(), "phone"));
693 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
694 &table_, "string/foo",
695 ConfigDescription::DefaultConfig(), "no-sdcard"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 test::GetValueForConfigAndProduct<String>(
698 &table_, "string/bar", ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 test::GetValueForConfigAndProduct<String>(
701 &table_, "string/baz", ConfigDescription::DefaultConfig(), ""));
702 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
703 &table_, "string/bit",
704 ConfigDescription::DefaultConfig(), "phablet"));
705 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
706 &table_, "string/bot",
707 ConfigDescription::DefaultConfig(), "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700708}
709
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800710TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 std::string input = R"EOF(
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800712 <public-group type="attr" first-id="0x01010040">
713 <public name="foo" />
714 <public name="bar" />
715 </public-group>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800717
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 AAPT_ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800721
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 AAPT_ASSERT_TRUE(result.value().package->id);
723 AAPT_ASSERT_TRUE(result.value().type->id);
724 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 ResourceId actual_id(result.value().package->id.value(),
726 result.value().type->id.value(),
727 result.value().entry->id.value());
728 EXPECT_EQ(ResourceId(0x01010040), actual_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 AAPT_ASSERT_TRUE(result);
732
733 AAPT_ASSERT_TRUE(result.value().package->id);
734 AAPT_ASSERT_TRUE(result.value().type->id);
735 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 actual_id = ResourceId(result.value().package->id.value(),
737 result.value().type->id.value(),
738 result.value().entry->id.value());
739 EXPECT_EQ(ResourceId(0x01010041), actual_id);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800740}
741
Adam Lesinskifa105052015-11-07 13:34:39 -0800742TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 std::string input =
744 R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 ASSERT_TRUE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800746
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 ASSERT_FALSE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800749}
750
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751TEST_F(ResourceParserTest,
752 AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
753 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 ASSERT_TRUE(TestParse(input));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800755
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 AAPT_ASSERT_TRUE(result);
759 const ResourceEntry* entry = result.value().entry;
760 ASSERT_NE(nullptr, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 EXPECT_EQ(SymbolState::kUndefined, entry->symbol_status.state);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800762}
763
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800764TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 std::string input =
766 R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 ASSERT_TRUE(TestParse(input));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800768
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 BinaryPrimitive* val =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 ASSERT_NE(nullptr, val);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800772
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800774}
775
Adam Lesinski86d67df2017-01-31 13:47:27 -0800776TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
777 std::string input = R"EOF(<item name="foo" type="configVarying">Hey</item>)EOF";
778 ASSERT_TRUE(TestParse(input));
779 ASSERT_NE(nullptr, test::GetValue<String>(&table_, "configVarying/foo"));
780}
781
782TEST_F(ResourceParserTest, ParseBagElement) {
783 std::string input =
784 R"EOF(<bag name="bag" type="configVarying"><item name="test">Hello!</item></bag>)EOF";
785 ASSERT_TRUE(TestParse(input));
786
787 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
788 ASSERT_NE(nullptr, val);
789
790 ASSERT_EQ(1u, val->entries.size());
791 EXPECT_EQ(Reference(test::ParseNameOrDie("attr/test")), val->entries[0].key);
792 EXPECT_NE(nullptr, ValueCast<RawString>(val->entries[0].value.get()));
793}
794
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795} // namespace aapt