blob: 2463911445e7f589d7b2c67ddd93b662ef106d9b [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 Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030constexpr const char* kXmlPreamble =
31 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 std::stringstream input(kXmlPreamble);
36 input << "<attr name=\"foo\"/>" << std::endl;
37 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
39 xml::XmlPullParser xml_parser(input);
40 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070041}
42
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043class ResourceParserTest : public ::testing::Test {
44 public:
45 void SetUp() override { context_ = test::ContextBuilder().Build(); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 ::testing::AssertionResult TestParse(const StringPiece& str) {
48 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 }
Adam Lesinski52364f72016-01-11 13:10:24 -080050
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 ::testing::AssertionResult TestParse(const StringPiece& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 const ConfigDescription& config) {
53 std::stringstream input(kXmlPreamble);
54 input << "<resources>\n" << str << "\n</resources>" << std::endl;
55 ResourceParserOptions parserOptions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 config, parserOptions);
58 xml::XmlPullParser xmlParser(input);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return ::testing::AssertionFailure();
63 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064
65 protected:
66 ResourceTable table_;
67 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068};
69
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 ASSERT_NE(nullptr, str);
76 EXPECT_EQ(std::string(" hey there "), *str->value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077}
78
79TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 ASSERT_NE(nullptr, str);
85 EXPECT_EQ(std::string("?123"), *str->value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086}
87
Adam Lesinski9f222042015-11-04 13:51:45 -080088TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 std::string input = "<string name=\"foo\">%d %s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 ASSERT_FALSE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 input = "<string name=\"foo\">%1$d %2$s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 ASSERT_TRUE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080094}
95
Adam Lesinski8c3f31f2016-09-07 13:45:13 -070096TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 // Use a surrogate pair unicode point so that we can verify that the span
98 // indices
99 // use UTF-16 length and not UTF-18 length.
100 std::string input =
101 "<string name=\"foo\">This is my aunt\u2019s <b>string</b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 ASSERT_NE(nullptr, str);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 const std::string expected_str = "This is my aunt\u2019s string";
108 EXPECT_EQ(expected_str, *str->value->str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 EXPECT_EQ(1u, str->value->spans.size());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 EXPECT_EQ(17u, str->value->spans[0].first_char);
113 EXPECT_EQ(23u, str->value->spans[0].last_char);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700114}
115
116TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 std::string input = "<string name=\"foo\"> This is what I think </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 ASSERT_NE(nullptr, str);
122 EXPECT_EQ(std::string("This is what I think"), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 input = "<string name=\"foo2\">\" This is what I think \"</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 ASSERT_NE(nullptr, str);
129 EXPECT_EQ(std::string(" This is what I think "), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700130}
131
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132TEST_F(ResourceParserTest, IgnoreXliffTags) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 std::string input =
134 "<string name=\"foo\" \n"
135 " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"
136 " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 ASSERT_NE(nullptr, str);
141 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142}
143
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700144TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 std::string input = "<integer name=\"foo\">@null</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700147
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
149 // a non-existing value, and this causes problems in styles when trying to
150 // resolve
151 // an attribute. Null values must be encoded as
152 // android::Res_value::TYPE_REFERENCE
153 // with a data value of 0.
154 BinaryPrimitive* integer =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 ASSERT_NE(nullptr, integer);
157 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE),
158 integer->value.dataType);
159 EXPECT_EQ(0u, integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700160}
161
162TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 std::string input = "<integer name=\"foo\">@empty</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 BinaryPrimitive* integer =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 ASSERT_NE(nullptr, integer);
169 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
170 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700171}
172
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800173TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 std::string input =
175 "<attr name=\"foo\" format=\"string\"/>\n"
176 "<attr name=\"bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186}
187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188// Old AAPT allowed attributes to be defined under different configurations, but
189// ultimately
190// stored them with the default configuration. Check that we have the same
191// behavior.
192TEST_F(ResourceParserTest,
193 ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 std::string input = R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800196 <attr name="foo" />
197 <declare-styleable name="bar">
198 <attr name="baz" />
199 </declare-styleable>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/foo",
203 watch_config));
204 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/baz",
205 watch_config));
206 EXPECT_EQ(nullptr, test::GetValueForConfig<Styleable>(
207 &table_, "styleable/bar", watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/foo"));
210 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/baz"));
211 EXPECT_NE(nullptr, test::GetValue<Styleable>(&table_, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800212}
213
Adam Lesinskia5870652015-11-20 15:32:30 -0800214TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 std::string input =
216 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask);
222 EXPECT_EQ(10, attr->min_int);
223 EXPECT_EQ(23, attr->max_int);
Adam Lesinskia5870652015-11-20 15:32:30 -0800224}
225
226TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 std::string input =
228 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 ASSERT_FALSE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800230}
231
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 std::string input =
234 "<declare-styleable name=\"Styleable\">\n"
235 " <attr name=\"foo\" />\n"
236 "</declare-styleable>\n"
237 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243}
244
245TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 std::string input =
247 "<declare-styleable name=\"Theme\">"
248 " <attr name=\"foo\" />\n"
249 "</declare-styleable>\n"
250 "<declare-styleable name=\"Window\">\n"
251 " <attr name=\"foo\" format=\"boolean\"/>\n"
252 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258}
259
260TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 std::string input =
262 "<attr name=\"foo\">\n"
263 " <enum name=\"bar\" value=\"0\"/>\n"
264 " <enum name=\"bat\" value=\"1\"/>\n"
265 " <enum name=\"baz\" value=\"2\"/>\n"
266 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
270 ASSERT_NE(enum_attr, nullptr);
271 EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM);
272 ASSERT_EQ(enum_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
275 EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar");
276 EXPECT_EQ(enum_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
279 EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat");
280 EXPECT_EQ(enum_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
283 EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz");
284 EXPECT_EQ(enum_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285}
286
287TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 std::string input =
289 "<attr name=\"foo\">\n"
290 " <flag name=\"bar\" value=\"0\"/>\n"
291 " <flag name=\"bat\" value=\"1\"/>\n"
292 " <flag name=\"baz\" value=\"2\"/>\n"
293 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
297 ASSERT_NE(nullptr, flag_attr);
298 EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS);
299 ASSERT_EQ(flag_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
302 EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar");
303 EXPECT_EQ(flag_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
306 EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat");
307 EXPECT_EQ(flag_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
310 EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz");
311 EXPECT_EQ(flag_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 std::unique_ptr<BinaryPrimitive> flag_value =
314 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
315 ASSERT_NE(nullptr, flag_value);
316 EXPECT_EQ(flag_value->value.data, 1u | 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317}
318
319TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 std::string input =
321 "<attr name=\"foo\">\n"
322 " <enum name=\"bar\" value=\"0\"/>\n"
323 " <enum name=\"bat\" value=\"1\"/>\n"
324 " <enum name=\"bat\" value=\"2\"/>\n"
325 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
329TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 std::string input =
331 "<style name=\"foo\" parent=\"@style/fu\">\n"
332 " <item name=\"bar\">#ffffffff</item>\n"
333 " <item name=\"bat\">@string/hey</item>\n"
334 " <item name=\"baz\"><b>hey</b></item>\n"
335 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 ASSERT_NE(nullptr, style);
340 AAPT_ASSERT_TRUE(style->parent);
341 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 EXPECT_EQ(test::ParseNameOrDie("style/fu"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 style->parent.value().name.value());
344 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800345
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700349
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700353
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 EXPECT_EQ(test::ParseNameOrDie("attr/baz"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800357}
358
Adam Lesinski769de982015-04-10 19:43:55 -0700359TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 ASSERT_TRUE(TestParse(input));
Adam Lesinski769de982015-04-10 19:43:55 -0700362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 ASSERT_NE(nullptr, style);
365 AAPT_ASSERT_TRUE(style->parent);
366 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 EXPECT_EQ(test::ParseNameOrDie("com.app:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700369}
370
Adam Lesinski24aad162015-04-24 19:19:30 -0700371TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 std::string input =
373 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
374 " name=\"foo\" parent=\"app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 ASSERT_NE(nullptr, style);
379 AAPT_ASSERT_TRUE(style->parent);
380 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 EXPECT_EQ(test::ParseNameOrDie("android:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700383}
384
385TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 std::string input =
387 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" "
388 "name=\"foo\">\n"
389 " <item name=\"app:bar\">0</item>\n"
390 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 ASSERT_NE(nullptr, style);
395 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 EXPECT_EQ(test::ParseNameOrDie("android:attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700398}
399
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700400TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 std::string input = "<style name=\"foo.bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 ASSERT_NE(nullptr, style);
406 AAPT_ASSERT_TRUE(style->parent);
407 AAPT_ASSERT_TRUE(style->parent.value().name);
408 EXPECT_EQ(style->parent.value().name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 test::ParseNameOrDie("style/foo"));
410 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700411}
412
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413TEST_F(ResourceParserTest,
414 ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
415 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 ASSERT_NE(nullptr, style);
420 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700422}
423
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800424TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 std::string input =
426 R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 ASSERT_TRUE(TestParse(input));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 ASSERT_NE(nullptr, style);
431 AAPT_ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800433}
434
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 Id* id = test::GetValue<Id>(&table_, "id/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 ASSERT_NE(id, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441}
442
443TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 std::string input =
445 "<declare-styleable name=\"foo\">\n"
446 " <attr name=\"bar\" />\n"
447 " <attr name=\"bat\" format=\"string|reference\"/>\n"
448 " <attr name=\"baz\">\n"
449 " <enum name=\"foo\" value=\"1\"/>\n"
450 " </attr>\n"
451 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 AAPT_ASSERT_TRUE(result);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state);
Adam Lesinski9f222042015-11-04 13:51:45 -0800458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 EXPECT_EQ(1u, attr->symbols.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700471
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 EXPECT_NE(nullptr, test::GetValue<Id>(&table_, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700473
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 ASSERT_NE(styleable, nullptr);
476 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 styleable->entries[0].name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482}
483
Adam Lesinski467f1712015-11-16 17:35:44 -0800484TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 std::string input =
486 "<declare-styleable name=\"foo\" "
487 "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
488 " <attr name=\"*android:bar\" />\n"
489 " <attr name=\"privAndroid:bat\" />\n"
490 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 ASSERT_TRUE(TestParse(input));
492 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 ASSERT_NE(nullptr, styleable);
494 ASSERT_EQ(2u, styleable->entries.size());
Adam Lesinski467f1712015-11-16 17:35:44 -0800495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 AAPT_ASSERT_TRUE(styleable->entries[0].name);
498 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800499
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 AAPT_ASSERT_TRUE(styleable->entries[1].name);
502 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800503}
504
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 std::string input =
507 "<array name=\"foo\">\n"
508 " <item>@string/ref</item>\n"
509 " <item>hey</item>\n"
510 " <item>23</item>\n"
511 "</array>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 ASSERT_NE(array, nullptr);
516 ASSERT_EQ(3u, array->items.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 EXPECT_NE(nullptr, ValueCast<Reference>(array->items[0].get()));
519 EXPECT_NE(nullptr, ValueCast<String>(array->items[1].get()));
520 EXPECT_NE(nullptr, ValueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521}
522
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700523TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 std::string input =
525 "<string-array name=\"foo\">\n"
526 " <item>\"Werk\"</item>\n"
527 "</string-array>\n";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 ASSERT_TRUE(TestParse(input));
529 EXPECT_NE(nullptr, test::GetValue<Array>(&table_, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700530}
531
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 std::string input =
534 "<plurals name=\"foo\">\n"
535 " <item quantity=\"other\">apples</item>\n"
536 " <item quantity=\"one\">apple</item>\n"
537 "</plurals>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800539}
540
541TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 std::string input =
543 "<!--This is a comment-->\n"
544 "<string name=\"foo\">Hi</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 EXPECT_EQ(value->GetComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700550}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700551
Adam Lesinskie78fd612015-10-22 12:48:43 -0700552TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 std::string input =
554 "<!--One-->\n"
555 "<!--Two-->\n"
556 "<string name=\"foo\">Hi</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 EXPECT_EQ(value->GetComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700563}
564
565TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 std::string input =
567 "<!--One-->\n"
568 "<string name=\"foo\">\n"
569 " Hi\n"
570 "<!--Two-->\n"
571 "</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700572
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700574
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 EXPECT_EQ(value->GetComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578}
579
Adam Lesinskica5638f2015-10-21 14:42:43 -0700580TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 // We only care about declare-styleable and enum/flag attributes because
582 // comments
583 // from those end up in R.java
584 std::string input = R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -0700585 <declare-styleable name="foo">
586 <!-- The name of the bar -->
587 <attr name="barName" format="string|reference" />
588 </declare-styleable>
589
590 <attr name="foo">
591 <!-- The very first -->
592 <enum name="one" value="1" />
593 </attr>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700595
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 ASSERT_NE(nullptr, styleable);
598 ASSERT_EQ(1u, styleable->entries.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700599
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 EXPECT_EQ(StringPiece("The name of the bar"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 styleable->entries.front().GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700602
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 ASSERT_NE(nullptr, attr);
605 ASSERT_EQ(1u, attr->symbols.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700606
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 EXPECT_EQ(StringPiece("The very first"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 attr->symbols.front().symbol.GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700609}
610
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800611/*
612 * Declaring an ID as public should not require a separate definition
613 * (as an ID has no value).
614 */
615TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 Id* id = test::GetValue<Id>(&table_, "id/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 ASSERT_NE(nullptr, id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800621}
622
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800623TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 std::string input = R"EOF(
Adam Lesinski7751afc2016-01-06 15:45:28 -0800625 <string name="foo" product="phone">hi</string>
626 <string name="foo" product="no-sdcard">ho</string>
627 <string name="bar" product="">wee</string>
628 <string name="baz">woo</string>
629 <string name="bit" product="phablet">hoot</string>
630 <string name="bot" product="default">yes</string>
631 )EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
635 &table_, "string/foo",
636 ConfigDescription::DefaultConfig(), "phone"));
637 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
638 &table_, "string/foo",
639 ConfigDescription::DefaultConfig(), "no-sdcard"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641 test::GetValueForConfigAndProduct<String>(
642 &table_, "string/bar", ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 test::GetValueForConfigAndProduct<String>(
645 &table_, "string/baz", ConfigDescription::DefaultConfig(), ""));
646 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
647 &table_, "string/bit",
648 ConfigDescription::DefaultConfig(), "phablet"));
649 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
650 &table_, "string/bot",
651 ConfigDescription::DefaultConfig(), "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700652}
653
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800654TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 std::string input = R"EOF(
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800656 <public-group type="attr" first-id="0x01010040">
657 <public name="foo" />
658 <public name="bar" />
659 </public-group>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800661
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 AAPT_ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800665
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 AAPT_ASSERT_TRUE(result.value().package->id);
667 AAPT_ASSERT_TRUE(result.value().type->id);
668 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 ResourceId actual_id(result.value().package->id.value(),
670 result.value().type->id.value(),
671 result.value().entry->id.value());
672 EXPECT_EQ(ResourceId(0x01010040), actual_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 AAPT_ASSERT_TRUE(result);
676
677 AAPT_ASSERT_TRUE(result.value().package->id);
678 AAPT_ASSERT_TRUE(result.value().type->id);
679 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 actual_id = ResourceId(result.value().package->id.value(),
681 result.value().type->id.value(),
682 result.value().entry->id.value());
683 EXPECT_EQ(ResourceId(0x01010041), actual_id);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800684}
685
Adam Lesinskifa105052015-11-07 13:34:39 -0800686TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 std::string input =
688 R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 ASSERT_TRUE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800690
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 ASSERT_FALSE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800693}
694
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695TEST_F(ResourceParserTest,
696 AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
697 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 ASSERT_TRUE(TestParse(input));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800699
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 AAPT_ASSERT_TRUE(result);
703 const ResourceEntry* entry = result.value().entry;
704 ASSERT_NE(nullptr, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 EXPECT_EQ(SymbolState::kUndefined, entry->symbol_status.state);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800706}
707
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800708TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 std::string input =
710 R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 ASSERT_TRUE(TestParse(input));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800712
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 BinaryPrimitive* val =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 ASSERT_NE(nullptr, val);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800716
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800718}
719
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720} // namespace aapt