blob: b6d57c0f7b9bfc2b74666563a406c25441b4cbed [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"
18#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070019#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070021#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <sstream>
25#include <string>
26
27namespace aapt {
28
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029constexpr const char* kXmlPreamble =
30 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
34 std::stringstream input(kXmlPreamble);
35 input << "<attr name=\"foo\"/>" << std::endl;
36 ResourceTable table;
37 ResourceParser parser(context->getDiagnostics(), &table, Source{"test"}, {});
38 xml::XmlPullParser xmlParser(input);
39 ASSERT_FALSE(parser.parse(&xmlParser));
Adam Lesinski769de982015-04-10 19:43:55 -070040}
41
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042struct ResourceParserTest : public ::testing::Test {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 ResourceTable mTable;
44 std::unique_ptr<IAaptContext> mContext;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 void SetUp() override { mContext = test::ContextBuilder().build(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 ::testing::AssertionResult testParse(const StringPiece& str) {
49 return testParse(str, ConfigDescription{});
50 }
Adam Lesinski52364f72016-01-11 13:10:24 -080051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 ::testing::AssertionResult testParse(const StringPiece& str,
53 const ConfigDescription& config) {
54 std::stringstream input(kXmlPreamble);
55 input << "<resources>\n" << str << "\n</resources>" << std::endl;
56 ResourceParserOptions parserOptions;
57 ResourceParser parser(mContext->getDiagnostics(), &mTable, Source{"test"},
58 config, parserOptions);
59 xml::XmlPullParser xmlParser(input);
60 if (parser.parse(&xmlParser)) {
61 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return ::testing::AssertionFailure();
64 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065};
66
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
69 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 String* str = test::getValue<String>(&mTable, "string/foo");
72 ASSERT_NE(nullptr, str);
73 EXPECT_EQ(std::string(" hey there "), *str->value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074}
75
76TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 std::string input = "<string name=\"foo\">\\?123</string>";
78 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 String* str = test::getValue<String>(&mTable, "string/foo");
81 ASSERT_NE(nullptr, str);
82 EXPECT_EQ(std::string("?123"), *str->value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083}
84
Adam Lesinski9f222042015-11-04 13:51:45 -080085TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 std::string input = "<string name=\"foo\">%d %s</string>";
87 ASSERT_FALSE(testParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 input = "<string name=\"foo\">%1$d %2$s</string>";
90 ASSERT_TRUE(testParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080091}
92
Adam Lesinski8c3f31f2016-09-07 13:45:13 -070093TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 // Use a surrogate pair unicode point so that we can verify that the span
95 // indices
96 // use UTF-16 length and not UTF-18 length.
97 std::string input =
98 "<string name=\"foo\">This is my aunt\u2019s <b>string</b></string>";
99 ASSERT_TRUE(testParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 StyledString* str = test::getValue<StyledString>(&mTable, "string/foo");
102 ASSERT_NE(nullptr, str);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 const std::string expectedStr = "This is my aunt\u2019s string";
105 EXPECT_EQ(expectedStr, *str->value->str);
106 EXPECT_EQ(1u, str->value->spans.size());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
109 EXPECT_EQ(17u, str->value->spans[0].firstChar);
110 EXPECT_EQ(23u, str->value->spans[0].lastChar);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700111}
112
113TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 std::string input = "<string name=\"foo\"> This is what I think </string>";
115 ASSERT_TRUE(testParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 String* str = test::getValue<String>(&mTable, "string/foo");
118 ASSERT_NE(nullptr, str);
119 EXPECT_EQ(std::string("This is what I think"), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 input = "<string name=\"foo2\">\" This is what I think \"</string>";
122 ASSERT_TRUE(testParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 str = test::getValue<String>(&mTable, "string/foo2");
125 ASSERT_NE(nullptr, str);
126 EXPECT_EQ(std::string(" This is what I think "), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700127}
128
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129TEST_F(ResourceParserTest, IgnoreXliffTags) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 std::string input =
131 "<string name=\"foo\" \n"
132 " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"
133 " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>";
134 ASSERT_TRUE(testParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 String* str = test::getValue<String>(&mTable, "string/foo");
137 ASSERT_NE(nullptr, str);
138 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139}
140
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700141TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 std::string input = "<integer name=\"foo\">@null</integer>";
143 ASSERT_TRUE(testParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
146 // a non-existing value, and this causes problems in styles when trying to
147 // resolve
148 // an attribute. Null values must be encoded as
149 // android::Res_value::TYPE_REFERENCE
150 // with a data value of 0.
151 BinaryPrimitive* integer =
152 test::getValue<BinaryPrimitive>(&mTable, "integer/foo");
153 ASSERT_NE(nullptr, integer);
154 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE),
155 integer->value.dataType);
156 EXPECT_EQ(0u, integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700157}
158
159TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 std::string input = "<integer name=\"foo\">@empty</integer>";
161 ASSERT_TRUE(testParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 BinaryPrimitive* integer =
164 test::getValue<BinaryPrimitive>(&mTable, "integer/foo");
165 ASSERT_NE(nullptr, integer);
166 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
167 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700168}
169
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 std::string input =
172 "<attr name=\"foo\" format=\"string\"/>\n"
173 "<attr name=\"bar\"/>";
174 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
177 ASSERT_NE(nullptr, attr);
178 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 attr = test::getValue<Attribute>(&mTable, "attr/bar");
181 ASSERT_NE(nullptr, attr);
182 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->typeMask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183}
184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185// Old AAPT allowed attributes to be defined under different configurations, but
186// ultimately
187// stored them with the default configuration. Check that we have the same
188// behavior.
189TEST_F(ResourceParserTest,
190 ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
191 const ConfigDescription watchConfig = test::parseConfigOrDie("watch");
192 std::string input = R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800193 <attr name="foo" />
194 <declare-styleable name="bar">
195 <attr name="baz" />
196 </declare-styleable>)EOF";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 ASSERT_TRUE(testParse(input, watchConfig));
Adam Lesinski52364f72016-01-11 13:10:24 -0800198
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, "attr/foo",
200 watchConfig));
201 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, "attr/baz",
202 watchConfig));
203 EXPECT_EQ(nullptr, test::getValueForConfig<Styleable>(
204 &mTable, "styleable/bar", watchConfig));
Adam Lesinski52364f72016-01-11 13:10:24 -0800205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, "attr/foo"));
207 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, "attr/baz"));
208 EXPECT_NE(nullptr, test::getValue<Styleable>(&mTable, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800209}
210
Adam Lesinskia5870652015-11-20 15:32:30 -0800211TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 std::string input =
213 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
214 ASSERT_TRUE(testParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800215
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
217 ASSERT_NE(nullptr, attr);
218 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->typeMask);
219 EXPECT_EQ(10, attr->minInt);
220 EXPECT_EQ(23, attr->maxInt);
Adam Lesinskia5870652015-11-20 15:32:30 -0800221}
222
223TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 std::string input =
225 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
226 ASSERT_FALSE(testParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800227}
228
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 std::string input =
231 "<declare-styleable name=\"Styleable\">\n"
232 " <attr name=\"foo\" />\n"
233 "</declare-styleable>\n"
234 "<attr name=\"foo\" format=\"string\"/>";
235 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
238 ASSERT_NE(nullptr, attr);
239 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240}
241
242TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 std::string input =
244 "<declare-styleable name=\"Theme\">"
245 " <attr name=\"foo\" />\n"
246 "</declare-styleable>\n"
247 "<declare-styleable name=\"Window\">\n"
248 " <attr name=\"foo\" format=\"boolean\"/>\n"
249 "</declare-styleable>";
250 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
253 ASSERT_NE(nullptr, attr);
254 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->typeMask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255}
256
257TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 std::string input =
259 "<attr name=\"foo\">\n"
260 " <enum name=\"bar\" value=\"0\"/>\n"
261 " <enum name=\"bat\" value=\"1\"/>\n"
262 " <enum name=\"baz\" value=\"2\"/>\n"
263 "</attr>";
264 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 Attribute* enumAttr = test::getValue<Attribute>(&mTable, "attr/foo");
267 ASSERT_NE(enumAttr, nullptr);
268 EXPECT_EQ(enumAttr->typeMask, android::ResTable_map::TYPE_ENUM);
269 ASSERT_EQ(enumAttr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 AAPT_ASSERT_TRUE(enumAttr->symbols[0].symbol.name);
272 EXPECT_EQ(enumAttr->symbols[0].symbol.name.value().entry, "bar");
273 EXPECT_EQ(enumAttr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 AAPT_ASSERT_TRUE(enumAttr->symbols[1].symbol.name);
276 EXPECT_EQ(enumAttr->symbols[1].symbol.name.value().entry, "bat");
277 EXPECT_EQ(enumAttr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 AAPT_ASSERT_TRUE(enumAttr->symbols[2].symbol.name);
280 EXPECT_EQ(enumAttr->symbols[2].symbol.name.value().entry, "baz");
281 EXPECT_EQ(enumAttr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282}
283
284TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 std::string input =
286 "<attr name=\"foo\">\n"
287 " <flag name=\"bar\" value=\"0\"/>\n"
288 " <flag name=\"bat\" value=\"1\"/>\n"
289 " <flag name=\"baz\" value=\"2\"/>\n"
290 "</attr>";
291 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 Attribute* flagAttr = test::getValue<Attribute>(&mTable, "attr/foo");
294 ASSERT_NE(nullptr, flagAttr);
295 EXPECT_EQ(flagAttr->typeMask, android::ResTable_map::TYPE_FLAGS);
296 ASSERT_EQ(flagAttr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 AAPT_ASSERT_TRUE(flagAttr->symbols[0].symbol.name);
299 EXPECT_EQ(flagAttr->symbols[0].symbol.name.value().entry, "bar");
300 EXPECT_EQ(flagAttr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 AAPT_ASSERT_TRUE(flagAttr->symbols[1].symbol.name);
303 EXPECT_EQ(flagAttr->symbols[1].symbol.name.value().entry, "bat");
304 EXPECT_EQ(flagAttr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 AAPT_ASSERT_TRUE(flagAttr->symbols[2].symbol.name);
307 EXPECT_EQ(flagAttr->symbols[2].symbol.name.value().entry, "baz");
308 EXPECT_EQ(flagAttr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800309
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 std::unique_ptr<BinaryPrimitive> flagValue =
311 ResourceUtils::tryParseFlagSymbol(flagAttr, "baz|bat");
312 ASSERT_NE(nullptr, flagValue);
313 EXPECT_EQ(flagValue->value.data, 1u | 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314}
315
316TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 std::string input =
318 "<attr name=\"foo\">\n"
319 " <enum name=\"bar\" value=\"0\"/>\n"
320 " <enum name=\"bat\" value=\"1\"/>\n"
321 " <enum name=\"bat\" value=\"2\"/>\n"
322 "</attr>";
323 ASSERT_FALSE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324}
325
326TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 std::string input =
328 "<style name=\"foo\" parent=\"@style/fu\">\n"
329 " <item name=\"bar\">#ffffffff</item>\n"
330 " <item name=\"bat\">@string/hey</item>\n"
331 " <item name=\"baz\"><b>hey</b></item>\n"
332 "</style>";
333 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 Style* style = test::getValue<Style>(&mTable, "style/foo");
336 ASSERT_NE(nullptr, style);
337 AAPT_ASSERT_TRUE(style->parent);
338 AAPT_ASSERT_TRUE(style->parent.value().name);
339 EXPECT_EQ(test::parseNameOrDie("style/fu"),
340 style->parent.value().name.value());
341 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 AAPT_ASSERT_TRUE(style->entries[0].key.name);
344 EXPECT_EQ(test::parseNameOrDie("attr/bar"),
345 style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 AAPT_ASSERT_TRUE(style->entries[1].key.name);
348 EXPECT_EQ(test::parseNameOrDie("attr/bat"),
349 style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 AAPT_ASSERT_TRUE(style->entries[2].key.name);
352 EXPECT_EQ(test::parseNameOrDie("attr/baz"),
353 style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354}
355
Adam Lesinski769de982015-04-10 19:43:55 -0700356TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
358 ASSERT_TRUE(testParse(input));
Adam Lesinski769de982015-04-10 19:43:55 -0700359
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 Style* style = test::getValue<Style>(&mTable, "style/foo");
361 ASSERT_NE(nullptr, style);
362 AAPT_ASSERT_TRUE(style->parent);
363 AAPT_ASSERT_TRUE(style->parent.value().name);
364 EXPECT_EQ(test::parseNameOrDie("com.app:style/Theme"),
365 style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700366}
367
Adam Lesinski24aad162015-04-24 19:19:30 -0700368TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 std::string input =
370 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
371 " name=\"foo\" parent=\"app:Theme\"/>";
372 ASSERT_TRUE(testParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700373
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 Style* style = test::getValue<Style>(&mTable, "style/foo");
375 ASSERT_NE(nullptr, style);
376 AAPT_ASSERT_TRUE(style->parent);
377 AAPT_ASSERT_TRUE(style->parent.value().name);
378 EXPECT_EQ(test::parseNameOrDie("android:style/Theme"),
379 style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700380}
381
382TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 std::string input =
384 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" "
385 "name=\"foo\">\n"
386 " <item name=\"app:bar\">0</item>\n"
387 "</style>";
388 ASSERT_TRUE(testParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700389
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 Style* style = test::getValue<Style>(&mTable, "style/foo");
391 ASSERT_NE(nullptr, style);
392 ASSERT_EQ(1u, style->entries.size());
393 EXPECT_EQ(test::parseNameOrDie("android:attr/bar"),
394 style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700395}
396
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700397TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 std::string input = "<style name=\"foo.bar\"/>";
399 ASSERT_TRUE(testParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700400
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 Style* style = test::getValue<Style>(&mTable, "style/foo.bar");
402 ASSERT_NE(nullptr, style);
403 AAPT_ASSERT_TRUE(style->parent);
404 AAPT_ASSERT_TRUE(style->parent.value().name);
405 EXPECT_EQ(style->parent.value().name.value(),
406 test::parseNameOrDie("style/foo"));
407 EXPECT_TRUE(style->parentInferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700408}
409
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410TEST_F(ResourceParserTest,
411 ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
412 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
413 ASSERT_TRUE(testParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700414
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 Style* style = test::getValue<Style>(&mTable, "style/foo.bar");
416 ASSERT_NE(nullptr, style);
417 AAPT_EXPECT_FALSE(style->parent);
418 EXPECT_FALSE(style->parentInferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700419}
420
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800421TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 std::string input =
423 R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
424 ASSERT_TRUE(testParse(input));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800425
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 Style* style = test::getValue<Style>(&mTable, "style/foo");
427 ASSERT_NE(nullptr, style);
428 AAPT_ASSERT_TRUE(style->parent);
429 EXPECT_TRUE(style->parent.value().privateReference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800430}
431
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800432TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 std::string input = "<string name=\"foo\">@+id/bar</string>";
434 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 Id* id = test::getValue<Id>(&mTable, "id/bar");
437 ASSERT_NE(id, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438}
439
440TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 std::string input =
442 "<declare-styleable name=\"foo\">\n"
443 " <attr name=\"bar\" />\n"
444 " <attr name=\"bat\" format=\"string|reference\"/>\n"
445 " <attr name=\"baz\">\n"
446 " <enum name=\"foo\" value=\"1\"/>\n"
447 " </attr>\n"
448 "</declare-styleable>";
449 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800450
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 Maybe<ResourceTable::SearchResult> result =
452 mTable.findResource(test::parseNameOrDie("styleable/foo"));
453 AAPT_ASSERT_TRUE(result);
454 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state);
Adam Lesinski9f222042015-11-04 13:51:45 -0800455
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/bar");
457 ASSERT_NE(attr, nullptr);
458 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 attr = test::getValue<Attribute>(&mTable, "attr/bat");
461 ASSERT_NE(attr, nullptr);
462 EXPECT_TRUE(attr->isWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 attr = test::getValue<Attribute>(&mTable, "attr/baz");
465 ASSERT_NE(attr, nullptr);
466 EXPECT_TRUE(attr->isWeak());
467 EXPECT_EQ(1u, attr->symbols.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700468
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 EXPECT_NE(nullptr, test::getValue<Id>(&mTable, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700470
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 Styleable* styleable = test::getValue<Styleable>(&mTable, "styleable/foo");
472 ASSERT_NE(styleable, nullptr);
473 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800474
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 EXPECT_EQ(test::parseNameOrDie("attr/bar"),
476 styleable->entries[0].name.value());
477 EXPECT_EQ(test::parseNameOrDie("attr/bat"),
478 styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800479}
480
Adam Lesinski467f1712015-11-16 17:35:44 -0800481TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 std::string input =
483 "<declare-styleable name=\"foo\" "
484 "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
485 " <attr name=\"*android:bar\" />\n"
486 " <attr name=\"privAndroid:bat\" />\n"
487 "</declare-styleable>";
488 ASSERT_TRUE(testParse(input));
489 Styleable* styleable = test::getValue<Styleable>(&mTable, "styleable/foo");
490 ASSERT_NE(nullptr, styleable);
491 ASSERT_EQ(2u, styleable->entries.size());
Adam Lesinski467f1712015-11-16 17:35:44 -0800492
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 EXPECT_TRUE(styleable->entries[0].privateReference);
494 AAPT_ASSERT_TRUE(styleable->entries[0].name);
495 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800496
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 EXPECT_TRUE(styleable->entries[1].privateReference);
498 AAPT_ASSERT_TRUE(styleable->entries[1].name);
499 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800500}
501
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 std::string input =
504 "<array name=\"foo\">\n"
505 " <item>@string/ref</item>\n"
506 " <item>hey</item>\n"
507 " <item>23</item>\n"
508 "</array>";
509 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 Array* array = test::getValue<Array>(&mTable, "array/foo");
512 ASSERT_NE(array, nullptr);
513 ASSERT_EQ(3u, array->items.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 EXPECT_NE(nullptr, valueCast<Reference>(array->items[0].get()));
516 EXPECT_NE(nullptr, valueCast<String>(array->items[1].get()));
517 EXPECT_NE(nullptr, valueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518}
519
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700520TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 std::string input =
522 "<string-array name=\"foo\">\n"
523 " <item>\"Werk\"</item>\n"
524 "</string-array>\n";
525 ASSERT_TRUE(testParse(input));
526 EXPECT_NE(nullptr, test::getValue<Array>(&mTable, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700527}
528
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 std::string input =
531 "<plurals name=\"foo\">\n"
532 " <item quantity=\"other\">apples</item>\n"
533 " <item quantity=\"one\">apple</item>\n"
534 "</plurals>";
535 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800536}
537
538TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 std::string input =
540 "<!--This is a comment-->\n"
541 "<string name=\"foo\">Hi</string>";
542 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800543
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 String* value = test::getValue<String>(&mTable, "string/foo");
545 ASSERT_NE(nullptr, value);
546 EXPECT_EQ(value->getComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700547}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700548
Adam Lesinskie78fd612015-10-22 12:48:43 -0700549TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 std::string input =
551 "<!--One-->\n"
552 "<!--Two-->\n"
553 "<string name=\"foo\">Hi</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700554
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 ASSERT_TRUE(testParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700556
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 String* value = test::getValue<String>(&mTable, "string/foo");
558 ASSERT_NE(nullptr, value);
559 EXPECT_EQ(value->getComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700560}
561
562TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 std::string input =
564 "<!--One-->\n"
565 "<string name=\"foo\">\n"
566 " Hi\n"
567 "<!--Two-->\n"
568 "</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 ASSERT_TRUE(testParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700571
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 String* value = test::getValue<String>(&mTable, "string/foo");
573 ASSERT_NE(nullptr, value);
574 EXPECT_EQ(value->getComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575}
576
Adam Lesinskica5638f2015-10-21 14:42:43 -0700577TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 // We only care about declare-styleable and enum/flag attributes because
579 // comments
580 // from those end up in R.java
581 std::string input = R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -0700582 <declare-styleable name="foo">
583 <!-- The name of the bar -->
584 <attr name="barName" format="string|reference" />
585 </declare-styleable>
586
587 <attr name="foo">
588 <!-- The very first -->
589 <enum name="one" value="1" />
590 </attr>)EOF";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 ASSERT_TRUE(testParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700592
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 Styleable* styleable = test::getValue<Styleable>(&mTable, "styleable/foo");
594 ASSERT_NE(nullptr, styleable);
595 ASSERT_EQ(1u, styleable->entries.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700596
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 EXPECT_EQ(StringPiece("The name of the bar"),
598 styleable->entries.front().getComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700599
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
601 ASSERT_NE(nullptr, attr);
602 ASSERT_EQ(1u, attr->symbols.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700603
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 EXPECT_EQ(StringPiece("The very first"),
605 attr->symbols.front().symbol.getComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700606}
607
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608/*
609 * Declaring an ID as public should not require a separate definition
610 * (as an ID has no value).
611 */
612TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 std::string input = "<public type=\"id\" name=\"foo\"/>";
614 ASSERT_TRUE(testParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 Id* id = test::getValue<Id>(&mTable, "id/foo");
617 ASSERT_NE(nullptr, id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800618}
619
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800620TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 std::string input = R"EOF(
Adam Lesinski7751afc2016-01-06 15:45:28 -0800622 <string name="foo" product="phone">hi</string>
623 <string name="foo" product="no-sdcard">ho</string>
624 <string name="bar" product="">wee</string>
625 <string name="baz">woo</string>
626 <string name="bit" product="phablet">hoot</string>
627 <string name="bot" product="default">yes</string>
628 )EOF";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 ASSERT_TRUE(testParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700630
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(
632 &mTable, "string/foo",
633 ConfigDescription::defaultConfig(), "phone"));
634 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(
635 &mTable, "string/foo",
636 ConfigDescription::defaultConfig(), "no-sdcard"));
637 EXPECT_NE(nullptr,
638 test::getValueForConfigAndProduct<String>(
639 &mTable, "string/bar", ConfigDescription::defaultConfig(), ""));
640 EXPECT_NE(nullptr,
641 test::getValueForConfigAndProduct<String>(
642 &mTable, "string/baz", ConfigDescription::defaultConfig(), ""));
643 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(
644 &mTable, "string/bit",
645 ConfigDescription::defaultConfig(), "phablet"));
646 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(
647 &mTable, "string/bot",
648 ConfigDescription::defaultConfig(), "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700649}
650
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800651TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 std::string input = R"EOF(
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800653 <public-group type="attr" first-id="0x01010040">
654 <public name="foo" />
655 <public name="bar" />
656 </public-group>)EOF";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 ASSERT_TRUE(testParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800658
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 Maybe<ResourceTable::SearchResult> result =
660 mTable.findResource(test::parseNameOrDie("attr/foo"));
661 AAPT_ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800662
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 AAPT_ASSERT_TRUE(result.value().package->id);
664 AAPT_ASSERT_TRUE(result.value().type->id);
665 AAPT_ASSERT_TRUE(result.value().entry->id);
666 ResourceId actualId(result.value().package->id.value(),
667 result.value().type->id.value(),
668 result.value().entry->id.value());
669 EXPECT_EQ(ResourceId(0x01010040), actualId);
670
671 result = mTable.findResource(test::parseNameOrDie("attr/bar"));
672 AAPT_ASSERT_TRUE(result);
673
674 AAPT_ASSERT_TRUE(result.value().package->id);
675 AAPT_ASSERT_TRUE(result.value().type->id);
676 AAPT_ASSERT_TRUE(result.value().entry->id);
677 actualId = ResourceId(result.value().package->id.value(),
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800678 result.value().type->id.value(),
679 result.value().entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 EXPECT_EQ(ResourceId(0x01010041), actualId);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800681}
682
Adam Lesinskifa105052015-11-07 13:34:39 -0800683TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 std::string input =
685 R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
686 ASSERT_TRUE(testParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800687
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
689 ASSERT_FALSE(testParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800690}
691
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692TEST_F(ResourceParserTest,
693 AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
694 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
695 ASSERT_TRUE(testParse(input));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 Maybe<ResourceTable::SearchResult> result =
698 mTable.findResource(test::parseNameOrDie("string/bar"));
699 AAPT_ASSERT_TRUE(result);
700 const ResourceEntry* entry = result.value().entry;
701 ASSERT_NE(nullptr, entry);
702 EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800703}
704
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800705TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 std::string input =
707 R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
708 ASSERT_TRUE(testParse(input));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800709
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 BinaryPrimitive* val =
711 test::getValue<BinaryPrimitive>(&mTable, "integer/foo");
712 ASSERT_NE(nullptr, val);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800713
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800715}
716
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717} // namespace aapt