blob: 2cc94d4e97d57116596ad4c3ed5a90351be7df1e [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 Lesinski1ab598f2015-08-14 14:26:04 -070021#include "test/Context.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
24#include <gtest/gtest.h>
25#include <sstream>
26#include <string>
27
28namespace aapt {
29
30constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
33 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" }, {});
Adam Lesinski467f1712015-11-16 17:35:44 -080038 xml::XmlPullParser xmlParser(input);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039 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 Lesinski1ab598f2015-08-14 14:26:04 -070043 ResourceTable mTable;
44 std::unique_ptr<IAaptContext> mContext;
45
46 void SetUp() override {
47 mContext = test::ContextBuilder().build();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048 }
49
Adam Lesinski9ba47d82015-10-13 11:37:10 -070050 ::testing::AssertionResult testParse(const StringPiece& str,
51 Maybe<std::u16string> product = {}) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052 std::stringstream input(kXmlPreamble);
Adam Lesinski24aad162015-04-24 19:19:30 -070053 input << "<resources>\n" << str << "\n</resources>" << std::endl;
Adam Lesinski9f222042015-11-04 13:51:45 -080054 ResourceParserOptions parserOptions;
55 parserOptions.product = product;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070056 ResourceParser parser(mContext->getDiagnostics(), &mTable, Source{ "test" }, {},
Adam Lesinski9f222042015-11-04 13:51:45 -080057 parserOptions);
Adam Lesinski467f1712015-11-16 17:35:44 -080058 xml::XmlPullParser xmlParser(input);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 if (parser.parse(&xmlParser)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060 return ::testing::AssertionSuccess();
61 }
62 return ::testing::AssertionFailure();
63 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064};
65
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinski24aad162015-04-24 19:19:30 -070067 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068 ASSERT_TRUE(testParse(input));
69
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070 String* str = test::getValue<String>(&mTable, u"@string/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071 ASSERT_NE(nullptr, str);
72 EXPECT_EQ(std::u16string(u" hey there "), *str->value);
73}
74
75TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinski24aad162015-04-24 19:19:30 -070076 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077 ASSERT_TRUE(testParse(input));
78
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079 String* str = test::getValue<String>(&mTable, u"@string/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080 ASSERT_NE(nullptr, str);
81 EXPECT_EQ(std::u16string(u"?123"), *str->value);
82}
83
Adam Lesinski9f222042015-11-04 13:51:45 -080084TEST_F(ResourceParserTest, ParseFormattedString) {
85 std::string input = "<string name=\"foo\">%d %s</string>";
86 ASSERT_FALSE(testParse(input));
87
88 input = "<string name=\"foo\">%1$d %2$s</string>";
89 ASSERT_TRUE(testParse(input));
90}
91
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092TEST_F(ResourceParserTest, IgnoreXliffTags) {
93 std::string input = "<string name=\"foo\" \n"
94 " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"
95 " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>";
96 ASSERT_TRUE(testParse(input));
97
98 String* str = test::getValue<String>(&mTable, u"@string/foo");
99 ASSERT_NE(nullptr, str);
100 EXPECT_EQ(StringPiece16(u"There are %1$d apples"), StringPiece16(*str->value));
101}
102
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700103TEST_F(ResourceParserTest, ParseNull) {
104 std::string input = "<integer name=\"foo\">@null</integer>";
105 ASSERT_TRUE(testParse(input));
106
107 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
108 // a non-existing value, and this causes problems in styles when trying to resolve
109 // an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
110 // with a data value of 0.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700112 ASSERT_NE(nullptr, integer);
113 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
114 EXPECT_EQ(0u, integer->value.data);
115}
116
117TEST_F(ResourceParserTest, ParseEmpty) {
118 std::string input = "<integer name=\"foo\">@empty</integer>";
119 ASSERT_TRUE(testParse(input));
120
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700122 ASSERT_NE(nullptr, integer);
123 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
124 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
125}
126
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700128 std::string input = "<attr name=\"foo\" format=\"string\"/>\n"
129 "<attr name=\"bar\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130 ASSERT_TRUE(testParse(input));
131
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
133 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
135
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136 attr = test::getValue<Attribute>(&mTable, u"@attr/bar");
137 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->typeMask);
139}
140
Adam Lesinskia5870652015-11-20 15:32:30 -0800141TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
142 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
143 ASSERT_TRUE(testParse(input));
144
145 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
146 ASSERT_NE(nullptr, attr);
147 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->typeMask);
148 EXPECT_EQ(10, attr->minInt);
149 EXPECT_EQ(23, attr->maxInt);
150}
151
152TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
153 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
154 ASSERT_FALSE(testParse(input));
155}
156
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700158 std::string input = "<declare-styleable name=\"Styleable\">\n"
159 " <attr name=\"foo\" />\n"
160 "</declare-styleable>\n"
161 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162 ASSERT_TRUE(testParse(input));
163
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165 ASSERT_NE(nullptr, attr);
166 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
167}
168
169TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700170 std::string input = "<declare-styleable name=\"Theme\">"
171 " <attr name=\"foo\" />\n"
172 "</declare-styleable>\n"
173 "<declare-styleable name=\"Window\">\n"
174 " <attr name=\"foo\" format=\"boolean\"/>\n"
175 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176 ASSERT_TRUE(testParse(input));
177
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179 ASSERT_NE(nullptr, attr);
180 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->typeMask);
181}
182
183TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700184 std::string input = "<attr name=\"foo\">\n"
185 " <enum name=\"bar\" value=\"0\"/>\n"
186 " <enum name=\"bat\" value=\"1\"/>\n"
187 " <enum name=\"baz\" value=\"2\"/>\n"
188 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189 ASSERT_TRUE(testParse(input));
190
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191 Attribute* enumAttr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192 ASSERT_NE(enumAttr, nullptr);
193 EXPECT_EQ(enumAttr->typeMask, android::ResTable_map::TYPE_ENUM);
194 ASSERT_EQ(enumAttr->symbols.size(), 3u);
195
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196 AAPT_ASSERT_TRUE(enumAttr->symbols[0].symbol.name);
197 EXPECT_EQ(enumAttr->symbols[0].symbol.name.value().entry, u"bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198 EXPECT_EQ(enumAttr->symbols[0].value, 0u);
199
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200 AAPT_ASSERT_TRUE(enumAttr->symbols[1].symbol.name);
201 EXPECT_EQ(enumAttr->symbols[1].symbol.name.value().entry, u"bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202 EXPECT_EQ(enumAttr->symbols[1].value, 1u);
203
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204 AAPT_ASSERT_TRUE(enumAttr->symbols[2].symbol.name);
205 EXPECT_EQ(enumAttr->symbols[2].symbol.name.value().entry, u"baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206 EXPECT_EQ(enumAttr->symbols[2].value, 2u);
207}
208
209TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700210 std::string input = "<attr name=\"foo\">\n"
211 " <flag name=\"bar\" value=\"0\"/>\n"
212 " <flag name=\"bat\" value=\"1\"/>\n"
213 " <flag name=\"baz\" value=\"2\"/>\n"
214 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215 ASSERT_TRUE(testParse(input));
216
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 Attribute* flagAttr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218 ASSERT_NE(flagAttr, nullptr);
219 EXPECT_EQ(flagAttr->typeMask, android::ResTable_map::TYPE_FLAGS);
220 ASSERT_EQ(flagAttr->symbols.size(), 3u);
221
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 AAPT_ASSERT_TRUE(flagAttr->symbols[0].symbol.name);
223 EXPECT_EQ(flagAttr->symbols[0].symbol.name.value().entry, u"bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 EXPECT_EQ(flagAttr->symbols[0].value, 0u);
225
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226 AAPT_ASSERT_TRUE(flagAttr->symbols[1].symbol.name);
227 EXPECT_EQ(flagAttr->symbols[1].symbol.name.value().entry, u"bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228 EXPECT_EQ(flagAttr->symbols[1].value, 1u);
229
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230 AAPT_ASSERT_TRUE(flagAttr->symbols[2].symbol.name);
231 EXPECT_EQ(flagAttr->symbols[2].symbol.name.value().entry, u"baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232 EXPECT_EQ(flagAttr->symbols[2].value, 2u);
233
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234 std::unique_ptr<BinaryPrimitive> flagValue = ResourceUtils::tryParseFlagSymbol(flagAttr,
235 u"baz|bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 ASSERT_NE(flagValue, nullptr);
237 EXPECT_EQ(flagValue->value.data, 1u | 2u);
238}
239
240TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700241 std::string input = "<attr name=\"foo\">\n"
242 " <enum name=\"bar\" value=\"0\"/>\n"
243 " <enum name=\"bat\" value=\"1\"/>\n"
244 " <enum name=\"bat\" value=\"2\"/>\n"
245 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246 ASSERT_FALSE(testParse(input));
247}
248
249TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700250 std::string input = "<style name=\"foo\" parent=\"@style/fu\">\n"
251 " <item name=\"bar\">#ffffffff</item>\n"
252 " <item name=\"bat\">@string/hey</item>\n"
253 " <item name=\"baz\"><b>hey</b></item>\n"
254 "</style>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255 ASSERT_TRUE(testParse(input));
256
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258 ASSERT_NE(style, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 AAPT_ASSERT_TRUE(style->parent);
260 AAPT_ASSERT_TRUE(style->parent.value().name);
261 EXPECT_EQ(test::parseNameOrDie(u"@style/fu"), style->parent.value().name.value());
262 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264 AAPT_ASSERT_TRUE(style->entries[0].key.name);
265 EXPECT_EQ(test::parseNameOrDie(u"@attr/bar"), style->entries[0].key.name.value());
266
267 AAPT_ASSERT_TRUE(style->entries[1].key.name);
268 EXPECT_EQ(test::parseNameOrDie(u"@attr/bat"), style->entries[1].key.name.value());
269
270 AAPT_ASSERT_TRUE(style->entries[2].key.name);
271 EXPECT_EQ(test::parseNameOrDie(u"@attr/baz"), style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272}
273
Adam Lesinski769de982015-04-10 19:43:55 -0700274TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700275 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinski769de982015-04-10 19:43:55 -0700276 ASSERT_TRUE(testParse(input));
277
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski769de982015-04-10 19:43:55 -0700279 ASSERT_NE(style, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280 AAPT_ASSERT_TRUE(style->parent);
281 AAPT_ASSERT_TRUE(style->parent.value().name);
282 EXPECT_EQ(test::parseNameOrDie(u"@com.app:style/Theme"), style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700283}
284
Adam Lesinski24aad162015-04-24 19:19:30 -0700285TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
286 std::string input = "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
287 " name=\"foo\" parent=\"app:Theme\"/>";
288 ASSERT_TRUE(testParse(input));
289
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700290 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski24aad162015-04-24 19:19:30 -0700291 ASSERT_NE(style, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292 AAPT_ASSERT_TRUE(style->parent);
293 AAPT_ASSERT_TRUE(style->parent.value().name);
294 EXPECT_EQ(test::parseNameOrDie(u"@android:style/Theme"), style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700295}
296
297TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
298 std::string input =
299 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" name=\"foo\">\n"
300 " <item name=\"app:bar\">0</item>\n"
301 "</style>";
302 ASSERT_TRUE(testParse(input));
303
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski24aad162015-04-24 19:19:30 -0700305 ASSERT_NE(style, nullptr);
306 ASSERT_EQ(1u, style->entries.size());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 EXPECT_EQ(test::parseNameOrDie(u"@android:attr/bar"), style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700308}
309
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700310TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
311 std::string input = "<style name=\"foo.bar\"/>";
312 ASSERT_TRUE(testParse(input));
313
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314 Style* style = test::getValue<Style>(&mTable, u"@style/foo.bar");
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700315 ASSERT_NE(style, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700316 AAPT_ASSERT_TRUE(style->parent);
317 AAPT_ASSERT_TRUE(style->parent.value().name);
318 EXPECT_EQ(style->parent.value().name.value(), test::parseNameOrDie(u"@style/foo"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700319 EXPECT_TRUE(style->parentInferred);
320}
321
322TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
323 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
324 ASSERT_TRUE(testParse(input));
325
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700326 Style* style = test::getValue<Style>(&mTable, u"@style/foo.bar");
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700327 ASSERT_NE(style, nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700328 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700329 EXPECT_FALSE(style->parentInferred);
330}
331
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800332TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700333 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334 ASSERT_TRUE(testParse(input));
335
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336 Id* id = test::getValue<Id>(&mTable, u"@id/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337 ASSERT_NE(id, nullptr);
338}
339
340TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700341 std::string input = "<declare-styleable name=\"foo\">\n"
342 " <attr name=\"bar\" />\n"
343 " <attr name=\"bat\" format=\"string|reference\"/>\n"
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700344 " <attr name=\"baz\">\n"
345 " <enum name=\"foo\" value=\"1\"/>\n"
346 " </attr>\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700347 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800348 ASSERT_TRUE(testParse(input));
349
Adam Lesinski9f222042015-11-04 13:51:45 -0800350 Maybe<ResourceTable::SearchResult> result =
351 mTable.findResource(test::parseNameOrDie(u"@styleable/foo"));
352 AAPT_ASSERT_TRUE(result);
353 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state);
354
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700355 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356 ASSERT_NE(attr, nullptr);
357 EXPECT_TRUE(attr->isWeak());
358
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700359 attr = test::getValue<Attribute>(&mTable, u"@attr/bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360 ASSERT_NE(attr, nullptr);
361 EXPECT_TRUE(attr->isWeak());
362
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700363 attr = test::getValue<Attribute>(&mTable, u"@attr/baz");
364 ASSERT_NE(attr, nullptr);
365 EXPECT_TRUE(attr->isWeak());
366 EXPECT_EQ(1u, attr->symbols.size());
367
368 EXPECT_NE(nullptr, test::getValue<Id>(&mTable, u"@id/foo"));
369
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700370 Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800371 ASSERT_NE(styleable, nullptr);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700372 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800373
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700374 EXPECT_EQ(test::parseNameOrDie(u"@attr/bar"), styleable->entries[0].name.value());
375 EXPECT_EQ(test::parseNameOrDie(u"@attr/bat"), styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376}
377
Adam Lesinski467f1712015-11-16 17:35:44 -0800378TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
379 std::string input = "<declare-styleable name=\"foo\" xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
380 " <attr name=\"*android:bar\" />\n"
381 " <attr name=\"privAndroid:bat\" />\n"
382 "</declare-styleable>";
383 ASSERT_TRUE(testParse(input));
384 Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo");
385 ASSERT_NE(nullptr, styleable);
386 ASSERT_EQ(2u, styleable->entries.size());
387
388 EXPECT_TRUE(styleable->entries[0].privateReference);
389 AAPT_ASSERT_TRUE(styleable->entries[0].name);
390 EXPECT_EQ(std::u16string(u"android"), styleable->entries[0].name.value().package);
391
392 EXPECT_TRUE(styleable->entries[1].privateReference);
393 AAPT_ASSERT_TRUE(styleable->entries[1].name);
394 EXPECT_EQ(std::u16string(u"android"), styleable->entries[1].name.value().package);
395}
396
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700398 std::string input = "<array name=\"foo\">\n"
399 " <item>@string/ref</item>\n"
400 " <item>hey</item>\n"
401 " <item>23</item>\n"
402 "</array>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403 ASSERT_TRUE(testParse(input));
404
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405 Array* array = test::getValue<Array>(&mTable, u"@array/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 ASSERT_NE(array, nullptr);
407 ASSERT_EQ(3u, array->items.size());
408
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700409 EXPECT_NE(nullptr, valueCast<Reference>(array->items[0].get()));
410 EXPECT_NE(nullptr, valueCast<String>(array->items[1].get()));
411 EXPECT_NE(nullptr, valueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800412}
413
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700414TEST_F(ResourceParserTest, ParseStringArray) {
415 std::string input = "<string-array name=\"foo\">\n"
416 " <item>\"Werk\"</item>\n"
417 "</string-array>\n";
418 ASSERT_TRUE(testParse(input));
419 EXPECT_NE(nullptr, test::getValue<Array>(&mTable, u"@array/foo"));
420}
421
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800422TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700423 std::string input = "<plurals name=\"foo\">\n"
424 " <item quantity=\"other\">apples</item>\n"
425 " <item quantity=\"one\">apple</item>\n"
426 "</plurals>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427 ASSERT_TRUE(testParse(input));
428}
429
430TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700431 std::string input = "<!--This is a comment-->\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700432 "<string name=\"foo\">Hi</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433 ASSERT_TRUE(testParse(input));
434
Adam Lesinskie78fd612015-10-22 12:48:43 -0700435 String* value = test::getValue<String>(&mTable, u"@string/foo");
436 ASSERT_NE(nullptr, value);
437 EXPECT_EQ(value->getComment(), u"This is a comment");
438}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700439
Adam Lesinskie78fd612015-10-22 12:48:43 -0700440TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
441 std::string input = "<!--One-->\n"
442 "<!--Two-->\n"
443 "<string name=\"foo\">Hi</string>";
444
445 ASSERT_TRUE(testParse(input));
446
447 String* value = test::getValue<String>(&mTable, u"@string/foo");
448 ASSERT_NE(nullptr, value);
449 EXPECT_EQ(value->getComment(), u"Two");
450}
451
452TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
453 std::string input = "<!--One-->\n"
454 "<string name=\"foo\">\n"
455 " Hi\n"
456 "<!--Two-->\n"
457 "</string>";
458
459 ASSERT_TRUE(testParse(input));
460
461 String* value = test::getValue<String>(&mTable, u"@string/foo");
462 ASSERT_NE(nullptr, value);
463 EXPECT_EQ(value->getComment(), u"One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464}
465
Adam Lesinskica5638f2015-10-21 14:42:43 -0700466TEST_F(ResourceParserTest, ParseNestedComments) {
467 // We only care about declare-styleable and enum/flag attributes because comments
468 // from those end up in R.java
469 std::string input = R"EOF(
470 <declare-styleable name="foo">
471 <!-- The name of the bar -->
472 <attr name="barName" format="string|reference" />
473 </declare-styleable>
474
475 <attr name="foo">
476 <!-- The very first -->
477 <enum name="one" value="1" />
478 </attr>)EOF";
479 ASSERT_TRUE(testParse(input));
480
481 Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo");
482 ASSERT_NE(nullptr, styleable);
483 ASSERT_EQ(1u, styleable->entries.size());
484
485 EXPECT_EQ(StringPiece16(u"The name of the bar"), styleable->entries.front().getComment());
486
487 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
488 ASSERT_NE(nullptr, attr);
489 ASSERT_EQ(1u, attr->symbols.size());
490
491 EXPECT_EQ(StringPiece16(u"The very first"), attr->symbols.front().symbol.getComment());
492}
493
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494/*
495 * Declaring an ID as public should not require a separate definition
496 * (as an ID has no value).
497 */
498TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700499 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500 ASSERT_TRUE(testParse(input));
501
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502 Id* id = test::getValue<Id>(&mTable, u"@id/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503 ASSERT_NE(nullptr, id);
504}
505
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700506TEST_F(ResourceParserTest, FilterProductsThatDontMatch) {
507 std::string input = "<string name=\"foo\" product=\"phone\">hi</string>\n"
508 "<string name=\"foo\" product=\"no-sdcard\">ho</string>\n"
509 "<string name=\"bar\" product=\"\">wee</string>\n"
510 "<string name=\"baz\">woo</string>\n";
511 ASSERT_TRUE(testParse(input, std::u16string(u"no-sdcard")));
512
513 String* fooStr = test::getValue<String>(&mTable, u"@string/foo");
514 ASSERT_NE(nullptr, fooStr);
515 EXPECT_EQ(StringPiece16(u"ho"), *fooStr->value);
516
517 EXPECT_NE(nullptr, test::getValue<String>(&mTable, u"@string/bar"));
518 EXPECT_NE(nullptr, test::getValue<String>(&mTable, u"@string/baz"));
519}
520
521TEST_F(ResourceParserTest, FailWhenProductFilterStripsOutAllVersionsOfResource) {
522 std::string input = "<string name=\"foo\" product=\"tablet\">hello</string>\n";
523 ASSERT_FALSE(testParse(input, std::u16string(u"phone")));
524}
525
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800526TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
527 std::string input = R"EOF(
528 <public-group type="attr" first-id="0x01010040">
529 <public name="foo" />
530 <public name="bar" />
531 </public-group>)EOF";
532 ASSERT_TRUE(testParse(input));
533
534 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
535 test::parseNameOrDie(u"@attr/foo"));
536 AAPT_ASSERT_TRUE(result);
537
538 AAPT_ASSERT_TRUE(result.value().package->id);
539 AAPT_ASSERT_TRUE(result.value().type->id);
540 AAPT_ASSERT_TRUE(result.value().entry->id);
541 ResourceId actualId(result.value().package->id.value(),
542 result.value().type->id.value(),
543 result.value().entry->id.value());
544 EXPECT_EQ(ResourceId(0x01010040), actualId);
545
546 result = mTable.findResource(test::parseNameOrDie(u"@attr/bar"));
547 AAPT_ASSERT_TRUE(result);
548
549 AAPT_ASSERT_TRUE(result.value().package->id);
550 AAPT_ASSERT_TRUE(result.value().type->id);
551 AAPT_ASSERT_TRUE(result.value().entry->id);
552 actualId = ResourceId(result.value().package->id.value(),
553 result.value().type->id.value(),
554 result.value().entry->id.value());
555 EXPECT_EQ(ResourceId(0x01010041), actualId);
556}
557
Adam Lesinskifa105052015-11-07 13:34:39 -0800558TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
559 std::string input = R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
560 ASSERT_TRUE(testParse(input));
561
562 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
563 ASSERT_FALSE(testParse(input));
564}
565
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800566TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
567 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
568 ASSERT_TRUE(testParse(input));
569
570 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
571 test::parseNameOrDie(u"@string/bar"));
572 AAPT_ASSERT_TRUE(result);
573 const ResourceEntry* entry = result.value().entry;
574 ASSERT_NE(nullptr, entry);
575 EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state);
576}
577
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800578TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
579 std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
580 ASSERT_TRUE(testParse(input));
581
582 BinaryPrimitive* val = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
583 ASSERT_NE(nullptr, val);
584
585 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
586}
587
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800588} // namespace aapt