blob: b456c0460147677e0c76ba6d62847111bb887470 [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
29constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
30
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
32 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
33 std::stringstream input(kXmlPreamble);
34 input << "<attr name=\"foo\"/>" << std::endl;
35 ResourceTable table;
36 ResourceParser parser(context->getDiagnostics(), &table, Source{ "test" }, {});
Adam Lesinski467f1712015-11-16 17:35:44 -080037 xml::XmlPullParser xmlParser(input);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038 ASSERT_FALSE(parser.parse(&xmlParser));
Adam Lesinski769de982015-04-10 19:43:55 -070039}
40
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041struct ResourceParserTest : public ::testing::Test {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042 ResourceTable mTable;
43 std::unique_ptr<IAaptContext> mContext;
44
45 void SetUp() override {
46 mContext = test::ContextBuilder().build();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047 }
48
Adam Lesinski52364f72016-01-11 13:10:24 -080049 ::testing::AssertionResult testParse(const StringPiece& str) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080050 return testParse(str, ConfigDescription{});
Adam Lesinski52364f72016-01-11 13:10:24 -080051 }
52
53 ::testing::AssertionResult testParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054 std::stringstream input(kXmlPreamble);
Adam Lesinski24aad162015-04-24 19:19:30 -070055 input << "<resources>\n" << str << "\n</resources>" << std::endl;
Adam Lesinski9f222042015-11-04 13:51:45 -080056 ResourceParserOptions parserOptions;
Adam Lesinski52364f72016-01-11 13:10:24 -080057 ResourceParser parser(mContext->getDiagnostics(), &mTable, Source{ "test" }, config,
Adam Lesinski9f222042015-11-04 13:51:45 -080058 parserOptions);
Adam Lesinski467f1712015-11-16 17:35:44 -080059 xml::XmlPullParser xmlParser(input);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 if (parser.parse(&xmlParser)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061 return ::testing::AssertionSuccess();
62 }
63 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 Lesinski24aad162015-04-24 19:19:30 -070068 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069 ASSERT_TRUE(testParse(input));
70
Adam Lesinskid0f116b2016-07-08 15:00:32 -070071 String* str = test::getValue<String>(&mTable, "@string/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072 ASSERT_NE(nullptr, str);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070073 EXPECT_EQ(std::string(" hey there "), *str->value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074}
75
76TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinski24aad162015-04-24 19:19:30 -070077 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078 ASSERT_TRUE(testParse(input));
79
Adam Lesinskid0f116b2016-07-08 15:00:32 -070080 String* str = test::getValue<String>(&mTable, "@string/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081 ASSERT_NE(nullptr, str);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070082 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) {
86 std::string input = "<string name=\"foo\">%d %s</string>";
87 ASSERT_FALSE(testParse(input));
88
89 input = "<string name=\"foo\">%1$d %2$s</string>";
90 ASSERT_TRUE(testParse(input));
91}
92
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093TEST_F(ResourceParserTest, IgnoreXliffTags) {
94 std::string input = "<string name=\"foo\" \n"
95 " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"
96 " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>";
97 ASSERT_TRUE(testParse(input));
98
Adam Lesinskid0f116b2016-07-08 15:00:32 -070099 String* str = test::getValue<String>(&mTable, "@string/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 ASSERT_NE(nullptr, str);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700101 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102}
103
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700104TEST_F(ResourceParserTest, ParseNull) {
105 std::string input = "<integer name=\"foo\">@null</integer>";
106 ASSERT_TRUE(testParse(input));
107
108 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
109 // a non-existing value, and this causes problems in styles when trying to resolve
110 // an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
111 // with a data value of 0.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700112 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, "@integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700113 ASSERT_NE(nullptr, integer);
114 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
115 EXPECT_EQ(0u, integer->value.data);
116}
117
118TEST_F(ResourceParserTest, ParseEmpty) {
119 std::string input = "<integer name=\"foo\">@empty</integer>";
120 ASSERT_TRUE(testParse(input));
121
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700122 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, "@integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700123 ASSERT_NE(nullptr, integer);
124 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
125 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
126}
127
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700129 std::string input = "<attr name=\"foo\" format=\"string\"/>\n"
130 "<attr name=\"bar\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131 ASSERT_TRUE(testParse(input));
132
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700133 Attribute* attr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
136
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700137 attr = test::getValue<Attribute>(&mTable, "@attr/bar");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->typeMask);
140}
141
Adam Lesinski52364f72016-01-11 13:10:24 -0800142// Old AAPT allowed attributes to be defined under different configurations, but ultimately
143// stored them with the default configuration. Check that we have the same behavior.
144TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
145 const ConfigDescription watchConfig = test::parseConfigOrDie("watch");
146 std::string input = R"EOF(
147 <attr name="foo" />
148 <declare-styleable name="bar">
149 <attr name="baz" />
150 </declare-styleable>)EOF";
151 ASSERT_TRUE(testParse(input, watchConfig));
152
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700153 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, "@attr/foo", watchConfig));
154 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, "@attr/baz", watchConfig));
155 EXPECT_EQ(nullptr, test::getValueForConfig<Styleable>(&mTable, "@styleable/bar", watchConfig));
Adam Lesinski52364f72016-01-11 13:10:24 -0800156
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700157 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, "@attr/foo"));
158 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, "@attr/baz"));
159 EXPECT_NE(nullptr, test::getValue<Styleable>(&mTable, "@styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800160}
161
Adam Lesinskia5870652015-11-20 15:32:30 -0800162TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
163 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
164 ASSERT_TRUE(testParse(input));
165
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700166 Attribute* attr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinskia5870652015-11-20 15:32:30 -0800167 ASSERT_NE(nullptr, attr);
168 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->typeMask);
169 EXPECT_EQ(10, attr->minInt);
170 EXPECT_EQ(23, attr->maxInt);
171}
172
173TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
174 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
175 ASSERT_FALSE(testParse(input));
176}
177
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700179 std::string input = "<declare-styleable name=\"Styleable\">\n"
180 " <attr name=\"foo\" />\n"
181 "</declare-styleable>\n"
182 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183 ASSERT_TRUE(testParse(input));
184
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700185 Attribute* attr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186 ASSERT_NE(nullptr, attr);
187 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
188}
189
190TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700191 std::string input = "<declare-styleable name=\"Theme\">"
192 " <attr name=\"foo\" />\n"
193 "</declare-styleable>\n"
194 "<declare-styleable name=\"Window\">\n"
195 " <attr name=\"foo\" format=\"boolean\"/>\n"
196 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197 ASSERT_TRUE(testParse(input));
198
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700199 Attribute* attr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200 ASSERT_NE(nullptr, attr);
201 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->typeMask);
202}
203
204TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700205 std::string input = "<attr name=\"foo\">\n"
206 " <enum name=\"bar\" value=\"0\"/>\n"
207 " <enum name=\"bat\" value=\"1\"/>\n"
208 " <enum name=\"baz\" value=\"2\"/>\n"
209 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210 ASSERT_TRUE(testParse(input));
211
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700212 Attribute* enumAttr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213 ASSERT_NE(enumAttr, nullptr);
214 EXPECT_EQ(enumAttr->typeMask, android::ResTable_map::TYPE_ENUM);
215 ASSERT_EQ(enumAttr->symbols.size(), 3u);
216
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 AAPT_ASSERT_TRUE(enumAttr->symbols[0].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700218 EXPECT_EQ(enumAttr->symbols[0].symbol.name.value().entry, "bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219 EXPECT_EQ(enumAttr->symbols[0].value, 0u);
220
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221 AAPT_ASSERT_TRUE(enumAttr->symbols[1].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700222 EXPECT_EQ(enumAttr->symbols[1].symbol.name.value().entry, "bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223 EXPECT_EQ(enumAttr->symbols[1].value, 1u);
224
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225 AAPT_ASSERT_TRUE(enumAttr->symbols[2].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700226 EXPECT_EQ(enumAttr->symbols[2].symbol.name.value().entry, "baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227 EXPECT_EQ(enumAttr->symbols[2].value, 2u);
228}
229
230TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700231 std::string input = "<attr name=\"foo\">\n"
232 " <flag name=\"bar\" value=\"0\"/>\n"
233 " <flag name=\"bat\" value=\"1\"/>\n"
234 " <flag name=\"baz\" value=\"2\"/>\n"
235 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 ASSERT_TRUE(testParse(input));
237
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700238 Attribute* flagAttr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800239 ASSERT_NE(nullptr, flagAttr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240 EXPECT_EQ(flagAttr->typeMask, android::ResTable_map::TYPE_FLAGS);
241 ASSERT_EQ(flagAttr->symbols.size(), 3u);
242
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243 AAPT_ASSERT_TRUE(flagAttr->symbols[0].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700244 EXPECT_EQ(flagAttr->symbols[0].symbol.name.value().entry, "bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245 EXPECT_EQ(flagAttr->symbols[0].value, 0u);
246
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247 AAPT_ASSERT_TRUE(flagAttr->symbols[1].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700248 EXPECT_EQ(flagAttr->symbols[1].symbol.name.value().entry, "bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249 EXPECT_EQ(flagAttr->symbols[1].value, 1u);
250
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251 AAPT_ASSERT_TRUE(flagAttr->symbols[2].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700252 EXPECT_EQ(flagAttr->symbols[2].symbol.name.value().entry, "baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253 EXPECT_EQ(flagAttr->symbols[2].value, 2u);
254
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 std::unique_ptr<BinaryPrimitive> flagValue = ResourceUtils::tryParseFlagSymbol(flagAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700256 "baz|bat");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800257 ASSERT_NE(nullptr, flagValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258 EXPECT_EQ(flagValue->value.data, 1u | 2u);
259}
260
261TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700262 std::string input = "<attr name=\"foo\">\n"
263 " <enum name=\"bar\" value=\"0\"/>\n"
264 " <enum name=\"bat\" value=\"1\"/>\n"
265 " <enum name=\"bat\" value=\"2\"/>\n"
266 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267 ASSERT_FALSE(testParse(input));
268}
269
270TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700271 std::string input = "<style name=\"foo\" parent=\"@style/fu\">\n"
272 " <item name=\"bar\">#ffffffff</item>\n"
273 " <item name=\"bat\">@string/hey</item>\n"
274 " <item name=\"baz\"><b>hey</b></item>\n"
275 "</style>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276 ASSERT_TRUE(testParse(input));
277
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700278 Style* style = test::getValue<Style>(&mTable, "@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800279 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280 AAPT_ASSERT_TRUE(style->parent);
281 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700282 EXPECT_EQ(test::parseNameOrDie("@style/fu"), style->parent.value().name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700286 EXPECT_EQ(test::parseNameOrDie("@attr/bar"), style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287
288 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700289 EXPECT_EQ(test::parseNameOrDie("@attr/bat"), style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700290
291 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700292 EXPECT_EQ(test::parseNameOrDie("@attr/baz"), style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
Adam Lesinski769de982015-04-10 19:43:55 -0700295TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700296 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinski769de982015-04-10 19:43:55 -0700297 ASSERT_TRUE(testParse(input));
298
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700299 Style* style = test::getValue<Style>(&mTable, "@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800300 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 AAPT_ASSERT_TRUE(style->parent);
302 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700303 EXPECT_EQ(test::parseNameOrDie("@com.app:style/Theme"), style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700304}
305
Adam Lesinski24aad162015-04-24 19:19:30 -0700306TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
307 std::string input = "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
308 " name=\"foo\" parent=\"app:Theme\"/>";
309 ASSERT_TRUE(testParse(input));
310
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700311 Style* style = test::getValue<Style>(&mTable, "@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800312 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313 AAPT_ASSERT_TRUE(style->parent);
314 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700315 EXPECT_EQ(test::parseNameOrDie("@android:style/Theme"), style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700316}
317
318TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
319 std::string input =
320 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" name=\"foo\">\n"
321 " <item name=\"app:bar\">0</item>\n"
322 "</style>";
323 ASSERT_TRUE(testParse(input));
324
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700325 Style* style = test::getValue<Style>(&mTable, "@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800326 ASSERT_NE(nullptr, style);
Adam Lesinski24aad162015-04-24 19:19:30 -0700327 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700328 EXPECT_EQ(test::parseNameOrDie("@android:attr/bar"), style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700329}
330
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700331TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
332 std::string input = "<style name=\"foo.bar\"/>";
333 ASSERT_TRUE(testParse(input));
334
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700335 Style* style = test::getValue<Style>(&mTable, "@style/foo.bar");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800336 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700337 AAPT_ASSERT_TRUE(style->parent);
338 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700339 EXPECT_EQ(style->parent.value().name.value(), test::parseNameOrDie("@style/foo"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700340 EXPECT_TRUE(style->parentInferred);
341}
342
343TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
344 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
345 ASSERT_TRUE(testParse(input));
346
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700347 Style* style = test::getValue<Style>(&mTable, "@style/foo.bar");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800348 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700349 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700350 EXPECT_FALSE(style->parentInferred);
351}
352
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800353TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
354 std::string input = R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
355 ASSERT_TRUE(testParse(input));
356
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700357 Style* style = test::getValue<Style>(&mTable, "@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800358 ASSERT_NE(nullptr, style);
359 AAPT_ASSERT_TRUE(style->parent);
360 EXPECT_TRUE(style->parent.value().privateReference);
361}
362
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700364 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800365 ASSERT_TRUE(testParse(input));
366
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700367 Id* id = test::getValue<Id>(&mTable, "@id/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368 ASSERT_NE(id, nullptr);
369}
370
371TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700372 std::string input = "<declare-styleable name=\"foo\">\n"
373 " <attr name=\"bar\" />\n"
374 " <attr name=\"bat\" format=\"string|reference\"/>\n"
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700375 " <attr name=\"baz\">\n"
376 " <enum name=\"foo\" value=\"1\"/>\n"
377 " </attr>\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700378 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379 ASSERT_TRUE(testParse(input));
380
Adam Lesinski9f222042015-11-04 13:51:45 -0800381 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700382 mTable.findResource(test::parseNameOrDie("@styleable/foo"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800383 AAPT_ASSERT_TRUE(result);
384 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state);
385
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700386 Attribute* attr = test::getValue<Attribute>(&mTable, "@attr/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800387 ASSERT_NE(attr, nullptr);
388 EXPECT_TRUE(attr->isWeak());
389
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700390 attr = test::getValue<Attribute>(&mTable, "@attr/bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391 ASSERT_NE(attr, nullptr);
392 EXPECT_TRUE(attr->isWeak());
393
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700394 attr = test::getValue<Attribute>(&mTable, "@attr/baz");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700395 ASSERT_NE(attr, nullptr);
396 EXPECT_TRUE(attr->isWeak());
397 EXPECT_EQ(1u, attr->symbols.size());
398
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700399 EXPECT_NE(nullptr, test::getValue<Id>(&mTable, "@id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700400
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700401 Styleable* styleable = test::getValue<Styleable>(&mTable, "@styleable/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402 ASSERT_NE(styleable, nullptr);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700403 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700405 EXPECT_EQ(test::parseNameOrDie("@attr/bar"), styleable->entries[0].name.value());
406 EXPECT_EQ(test::parseNameOrDie("@attr/bat"), styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407}
408
Adam Lesinski467f1712015-11-16 17:35:44 -0800409TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
410 std::string input = "<declare-styleable name=\"foo\" xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
411 " <attr name=\"*android:bar\" />\n"
412 " <attr name=\"privAndroid:bat\" />\n"
413 "</declare-styleable>";
414 ASSERT_TRUE(testParse(input));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700415 Styleable* styleable = test::getValue<Styleable>(&mTable, "@styleable/foo");
Adam Lesinski467f1712015-11-16 17:35:44 -0800416 ASSERT_NE(nullptr, styleable);
417 ASSERT_EQ(2u, styleable->entries.size());
418
419 EXPECT_TRUE(styleable->entries[0].privateReference);
420 AAPT_ASSERT_TRUE(styleable->entries[0].name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700421 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800422
423 EXPECT_TRUE(styleable->entries[1].privateReference);
424 AAPT_ASSERT_TRUE(styleable->entries[1].name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700425 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800426}
427
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700429 std::string input = "<array name=\"foo\">\n"
430 " <item>@string/ref</item>\n"
431 " <item>hey</item>\n"
432 " <item>23</item>\n"
433 "</array>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434 ASSERT_TRUE(testParse(input));
435
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700436 Array* array = test::getValue<Array>(&mTable, "@array/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 ASSERT_NE(array, nullptr);
438 ASSERT_EQ(3u, array->items.size());
439
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440 EXPECT_NE(nullptr, valueCast<Reference>(array->items[0].get()));
441 EXPECT_NE(nullptr, valueCast<String>(array->items[1].get()));
442 EXPECT_NE(nullptr, valueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800443}
444
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700445TEST_F(ResourceParserTest, ParseStringArray) {
446 std::string input = "<string-array name=\"foo\">\n"
447 " <item>\"Werk\"</item>\n"
448 "</string-array>\n";
449 ASSERT_TRUE(testParse(input));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700450 EXPECT_NE(nullptr, test::getValue<Array>(&mTable, "@array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700451}
452
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700454 std::string input = "<plurals name=\"foo\">\n"
455 " <item quantity=\"other\">apples</item>\n"
456 " <item quantity=\"one\">apple</item>\n"
457 "</plurals>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458 ASSERT_TRUE(testParse(input));
459}
460
461TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700462 std::string input = "<!--This is a comment-->\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700463 "<string name=\"foo\">Hi</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464 ASSERT_TRUE(testParse(input));
465
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700466 String* value = test::getValue<String>(&mTable, "@string/foo");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700467 ASSERT_NE(nullptr, value);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700468 EXPECT_EQ(value->getComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700469}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700470
Adam Lesinskie78fd612015-10-22 12:48:43 -0700471TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
472 std::string input = "<!--One-->\n"
473 "<!--Two-->\n"
474 "<string name=\"foo\">Hi</string>";
475
476 ASSERT_TRUE(testParse(input));
477
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700478 String* value = test::getValue<String>(&mTable, "@string/foo");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700479 ASSERT_NE(nullptr, value);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700480 EXPECT_EQ(value->getComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700481}
482
483TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
484 std::string input = "<!--One-->\n"
485 "<string name=\"foo\">\n"
486 " Hi\n"
487 "<!--Two-->\n"
488 "</string>";
489
490 ASSERT_TRUE(testParse(input));
491
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700492 String* value = test::getValue<String>(&mTable, "@string/foo");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700493 ASSERT_NE(nullptr, value);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700494 EXPECT_EQ(value->getComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495}
496
Adam Lesinskica5638f2015-10-21 14:42:43 -0700497TEST_F(ResourceParserTest, ParseNestedComments) {
498 // We only care about declare-styleable and enum/flag attributes because comments
499 // from those end up in R.java
500 std::string input = R"EOF(
501 <declare-styleable name="foo">
502 <!-- The name of the bar -->
503 <attr name="barName" format="string|reference" />
504 </declare-styleable>
505
506 <attr name="foo">
507 <!-- The very first -->
508 <enum name="one" value="1" />
509 </attr>)EOF";
510 ASSERT_TRUE(testParse(input));
511
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700512 Styleable* styleable = test::getValue<Styleable>(&mTable, "@styleable/foo");
Adam Lesinskica5638f2015-10-21 14:42:43 -0700513 ASSERT_NE(nullptr, styleable);
514 ASSERT_EQ(1u, styleable->entries.size());
515
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700516 EXPECT_EQ(StringPiece("The name of the bar"), styleable->entries.front().getComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700517
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700518 Attribute* attr = test::getValue<Attribute>(&mTable, "@attr/foo");
Adam Lesinskica5638f2015-10-21 14:42:43 -0700519 ASSERT_NE(nullptr, attr);
520 ASSERT_EQ(1u, attr->symbols.size());
521
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700522 EXPECT_EQ(StringPiece("The very first"), attr->symbols.front().symbol.getComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700523}
524
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800525/*
526 * Declaring an ID as public should not require a separate definition
527 * (as an ID has no value).
528 */
529TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700530 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800531 ASSERT_TRUE(testParse(input));
532
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700533 Id* id = test::getValue<Id>(&mTable, "@id/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800534 ASSERT_NE(nullptr, id);
535}
536
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800537TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinski7751afc2016-01-06 15:45:28 -0800538 std::string input = R"EOF(
539 <string name="foo" product="phone">hi</string>
540 <string name="foo" product="no-sdcard">ho</string>
541 <string name="bar" product="">wee</string>
542 <string name="baz">woo</string>
543 <string name="bit" product="phablet">hoot</string>
544 <string name="bot" product="default">yes</string>
545 )EOF";
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800546 ASSERT_TRUE(testParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700547
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700548 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "@string/foo",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800549 ConfigDescription::defaultConfig(),
550 "phone"));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700551 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "@string/foo",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800552 ConfigDescription::defaultConfig(),
553 "no-sdcard"));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700554 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "@string/bar",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800555 ConfigDescription::defaultConfig(),
556 ""));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700557 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "@string/baz",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800558 ConfigDescription::defaultConfig(),
559 ""));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700560 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "@string/bit",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800561 ConfigDescription::defaultConfig(),
562 "phablet"));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700563 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "@string/bot",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800564 ConfigDescription::defaultConfig(),
565 "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700566}
567
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800568TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
569 std::string input = R"EOF(
570 <public-group type="attr" first-id="0x01010040">
571 <public name="foo" />
572 <public name="bar" />
573 </public-group>)EOF";
574 ASSERT_TRUE(testParse(input));
575
576 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700577 test::parseNameOrDie("@attr/foo"));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800578 AAPT_ASSERT_TRUE(result);
579
580 AAPT_ASSERT_TRUE(result.value().package->id);
581 AAPT_ASSERT_TRUE(result.value().type->id);
582 AAPT_ASSERT_TRUE(result.value().entry->id);
583 ResourceId actualId(result.value().package->id.value(),
584 result.value().type->id.value(),
585 result.value().entry->id.value());
586 EXPECT_EQ(ResourceId(0x01010040), actualId);
587
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700588 result = mTable.findResource(test::parseNameOrDie("@attr/bar"));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800589 AAPT_ASSERT_TRUE(result);
590
591 AAPT_ASSERT_TRUE(result.value().package->id);
592 AAPT_ASSERT_TRUE(result.value().type->id);
593 AAPT_ASSERT_TRUE(result.value().entry->id);
594 actualId = ResourceId(result.value().package->id.value(),
595 result.value().type->id.value(),
596 result.value().entry->id.value());
597 EXPECT_EQ(ResourceId(0x01010041), actualId);
598}
599
Adam Lesinskifa105052015-11-07 13:34:39 -0800600TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
601 std::string input = R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
602 ASSERT_TRUE(testParse(input));
603
604 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
605 ASSERT_FALSE(testParse(input));
606}
607
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800608TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
609 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
610 ASSERT_TRUE(testParse(input));
611
612 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700613 test::parseNameOrDie("@string/bar"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800614 AAPT_ASSERT_TRUE(result);
615 const ResourceEntry* entry = result.value().entry;
616 ASSERT_NE(nullptr, entry);
617 EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state);
618}
619
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800620TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
621 std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
622 ASSERT_TRUE(testParse(input));
623
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700624 BinaryPrimitive* val = test::getValue<BinaryPrimitive>(&mTable, "@integer/foo");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800625 ASSERT_NE(nullptr, val);
626
627 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
628}
629
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800630} // namespace aapt