blob: 3450de9078bb105be877253fe039af646bed5df6 [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 Lesinski52364f72016-01-11 13:10:24 -080050 ::testing::AssertionResult testParse(const StringPiece& str) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080051 return testParse(str, ConfigDescription{});
Adam Lesinski52364f72016-01-11 13:10:24 -080052 }
53
54 ::testing::AssertionResult testParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055 std::stringstream input(kXmlPreamble);
Adam Lesinski24aad162015-04-24 19:19:30 -070056 input << "<resources>\n" << str << "\n</resources>" << std::endl;
Adam Lesinski9f222042015-11-04 13:51:45 -080057 ResourceParserOptions parserOptions;
Adam Lesinski52364f72016-01-11 13:10:24 -080058 ResourceParser parser(mContext->getDiagnostics(), &mTable, Source{ "test" }, config,
Adam Lesinski9f222042015-11-04 13:51:45 -080059 parserOptions);
Adam Lesinski467f1712015-11-16 17:35:44 -080060 xml::XmlPullParser xmlParser(input);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061 if (parser.parse(&xmlParser)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062 return ::testing::AssertionSuccess();
63 }
64 return ::testing::AssertionFailure();
65 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066};
67
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinski24aad162015-04-24 19:19:30 -070069 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070 ASSERT_TRUE(testParse(input));
71
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 String* str = test::getValue<String>(&mTable, u"@string/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 ASSERT_NE(nullptr, str);
74 EXPECT_EQ(std::u16string(u" hey there "), *str->value);
75}
76
77TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinski24aad162015-04-24 19:19:30 -070078 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079 ASSERT_TRUE(testParse(input));
80
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081 String* str = test::getValue<String>(&mTable, u"@string/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082 ASSERT_NE(nullptr, str);
83 EXPECT_EQ(std::u16string(u"?123"), *str->value);
84}
85
Adam Lesinski9f222042015-11-04 13:51:45 -080086TEST_F(ResourceParserTest, ParseFormattedString) {
87 std::string input = "<string name=\"foo\">%d %s</string>";
88 ASSERT_FALSE(testParse(input));
89
90 input = "<string name=\"foo\">%1$d %2$s</string>";
91 ASSERT_TRUE(testParse(input));
92}
93
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094TEST_F(ResourceParserTest, IgnoreXliffTags) {
95 std::string input = "<string name=\"foo\" \n"
96 " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"
97 " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>";
98 ASSERT_TRUE(testParse(input));
99
100 String* str = test::getValue<String>(&mTable, u"@string/foo");
101 ASSERT_NE(nullptr, str);
102 EXPECT_EQ(StringPiece16(u"There are %1$d apples"), StringPiece16(*str->value));
103}
104
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700105TEST_F(ResourceParserTest, ParseNull) {
106 std::string input = "<integer name=\"foo\">@null</integer>";
107 ASSERT_TRUE(testParse(input));
108
109 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
110 // a non-existing value, and this causes problems in styles when trying to resolve
111 // an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
112 // with a data value of 0.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700114 ASSERT_NE(nullptr, integer);
115 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
116 EXPECT_EQ(0u, integer->value.data);
117}
118
119TEST_F(ResourceParserTest, ParseEmpty) {
120 std::string input = "<integer name=\"foo\">@empty</integer>";
121 ASSERT_TRUE(testParse(input));
122
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700124 ASSERT_NE(nullptr, integer);
125 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
126 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
127}
128
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700130 std::string input = "<attr name=\"foo\" format=\"string\"/>\n"
131 "<attr name=\"bar\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132 ASSERT_TRUE(testParse(input));
133
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
135 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
137
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138 attr = test::getValue<Attribute>(&mTable, u"@attr/bar");
139 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->typeMask);
141}
142
Adam Lesinski52364f72016-01-11 13:10:24 -0800143// Old AAPT allowed attributes to be defined under different configurations, but ultimately
144// stored them with the default configuration. Check that we have the same behavior.
145TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
146 const ConfigDescription watchConfig = test::parseConfigOrDie("watch");
147 std::string input = R"EOF(
148 <attr name="foo" />
149 <declare-styleable name="bar">
150 <attr name="baz" />
151 </declare-styleable>)EOF";
152 ASSERT_TRUE(testParse(input, watchConfig));
153
154 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, u"@attr/foo", watchConfig));
155 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, u"@attr/baz", watchConfig));
156 EXPECT_EQ(nullptr, test::getValueForConfig<Styleable>(&mTable, u"@styleable/bar", watchConfig));
157
158 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, u"@attr/foo"));
159 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, u"@attr/baz"));
160 EXPECT_NE(nullptr, test::getValue<Styleable>(&mTable, u"@styleable/bar"));
161}
162
Adam Lesinskia5870652015-11-20 15:32:30 -0800163TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
164 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
165 ASSERT_TRUE(testParse(input));
166
167 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
168 ASSERT_NE(nullptr, attr);
169 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->typeMask);
170 EXPECT_EQ(10, attr->minInt);
171 EXPECT_EQ(23, attr->maxInt);
172}
173
174TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
175 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
176 ASSERT_FALSE(testParse(input));
177}
178
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700180 std::string input = "<declare-styleable name=\"Styleable\">\n"
181 " <attr name=\"foo\" />\n"
182 "</declare-styleable>\n"
183 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184 ASSERT_TRUE(testParse(input));
185
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187 ASSERT_NE(nullptr, attr);
188 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
189}
190
191TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700192 std::string input = "<declare-styleable name=\"Theme\">"
193 " <attr name=\"foo\" />\n"
194 "</declare-styleable>\n"
195 "<declare-styleable name=\"Window\">\n"
196 " <attr name=\"foo\" format=\"boolean\"/>\n"
197 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198 ASSERT_TRUE(testParse(input));
199
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800201 ASSERT_NE(nullptr, attr);
202 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->typeMask);
203}
204
205TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700206 std::string input = "<attr name=\"foo\">\n"
207 " <enum name=\"bar\" value=\"0\"/>\n"
208 " <enum name=\"bat\" value=\"1\"/>\n"
209 " <enum name=\"baz\" value=\"2\"/>\n"
210 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800211 ASSERT_TRUE(testParse(input));
212
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213 Attribute* enumAttr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214 ASSERT_NE(enumAttr, nullptr);
215 EXPECT_EQ(enumAttr->typeMask, android::ResTable_map::TYPE_ENUM);
216 ASSERT_EQ(enumAttr->symbols.size(), 3u);
217
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218 AAPT_ASSERT_TRUE(enumAttr->symbols[0].symbol.name);
219 EXPECT_EQ(enumAttr->symbols[0].symbol.name.value().entry, u"bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220 EXPECT_EQ(enumAttr->symbols[0].value, 0u);
221
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 AAPT_ASSERT_TRUE(enumAttr->symbols[1].symbol.name);
223 EXPECT_EQ(enumAttr->symbols[1].symbol.name.value().entry, u"bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 EXPECT_EQ(enumAttr->symbols[1].value, 1u);
225
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226 AAPT_ASSERT_TRUE(enumAttr->symbols[2].symbol.name);
227 EXPECT_EQ(enumAttr->symbols[2].symbol.name.value().entry, u"baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228 EXPECT_EQ(enumAttr->symbols[2].value, 2u);
229}
230
231TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700232 std::string input = "<attr name=\"foo\">\n"
233 " <flag name=\"bar\" value=\"0\"/>\n"
234 " <flag name=\"bat\" value=\"1\"/>\n"
235 " <flag name=\"baz\" value=\"2\"/>\n"
236 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237 ASSERT_TRUE(testParse(input));
238
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239 Attribute* flagAttr = test::getValue<Attribute>(&mTable, u"@attr/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800240 ASSERT_NE(nullptr, flagAttr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241 EXPECT_EQ(flagAttr->typeMask, android::ResTable_map::TYPE_FLAGS);
242 ASSERT_EQ(flagAttr->symbols.size(), 3u);
243
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244 AAPT_ASSERT_TRUE(flagAttr->symbols[0].symbol.name);
245 EXPECT_EQ(flagAttr->symbols[0].symbol.name.value().entry, u"bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246 EXPECT_EQ(flagAttr->symbols[0].value, 0u);
247
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248 AAPT_ASSERT_TRUE(flagAttr->symbols[1].symbol.name);
249 EXPECT_EQ(flagAttr->symbols[1].symbol.name.value().entry, u"bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250 EXPECT_EQ(flagAttr->symbols[1].value, 1u);
251
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 AAPT_ASSERT_TRUE(flagAttr->symbols[2].symbol.name);
253 EXPECT_EQ(flagAttr->symbols[2].symbol.name.value().entry, u"baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 EXPECT_EQ(flagAttr->symbols[2].value, 2u);
255
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256 std::unique_ptr<BinaryPrimitive> flagValue = ResourceUtils::tryParseFlagSymbol(flagAttr,
257 u"baz|bat");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800258 ASSERT_NE(nullptr, flagValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259 EXPECT_EQ(flagValue->value.data, 1u | 2u);
260}
261
262TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700263 std::string input = "<attr name=\"foo\">\n"
264 " <enum name=\"bar\" value=\"0\"/>\n"
265 " <enum name=\"bat\" value=\"1\"/>\n"
266 " <enum name=\"bat\" value=\"2\"/>\n"
267 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268 ASSERT_FALSE(testParse(input));
269}
270
271TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700272 std::string input = "<style name=\"foo\" parent=\"@style/fu\">\n"
273 " <item name=\"bar\">#ffffffff</item>\n"
274 " <item name=\"bat\">@string/hey</item>\n"
275 " <item name=\"baz\"><b>hey</b></item>\n"
276 "</style>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277 ASSERT_TRUE(testParse(input));
278
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800280 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 AAPT_ASSERT_TRUE(style->parent);
282 AAPT_ASSERT_TRUE(style->parent.value().name);
283 EXPECT_EQ(test::parseNameOrDie(u"@style/fu"), style->parent.value().name.value());
284 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 AAPT_ASSERT_TRUE(style->entries[0].key.name);
287 EXPECT_EQ(test::parseNameOrDie(u"@attr/bar"), style->entries[0].key.name.value());
288
289 AAPT_ASSERT_TRUE(style->entries[1].key.name);
290 EXPECT_EQ(test::parseNameOrDie(u"@attr/bat"), style->entries[1].key.name.value());
291
292 AAPT_ASSERT_TRUE(style->entries[2].key.name);
293 EXPECT_EQ(test::parseNameOrDie(u"@attr/baz"), style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294}
295
Adam Lesinski769de982015-04-10 19:43:55 -0700296TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700297 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinski769de982015-04-10 19:43:55 -0700298 ASSERT_TRUE(testParse(input));
299
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800301 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700302 AAPT_ASSERT_TRUE(style->parent);
303 AAPT_ASSERT_TRUE(style->parent.value().name);
304 EXPECT_EQ(test::parseNameOrDie(u"@com.app:style/Theme"), style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700305}
306
Adam Lesinski24aad162015-04-24 19:19:30 -0700307TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
308 std::string input = "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
309 " name=\"foo\" parent=\"app:Theme\"/>";
310 ASSERT_TRUE(testParse(input));
311
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700312 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800313 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314 AAPT_ASSERT_TRUE(style->parent);
315 AAPT_ASSERT_TRUE(style->parent.value().name);
316 EXPECT_EQ(test::parseNameOrDie(u"@android:style/Theme"), style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700317}
318
319TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
320 std::string input =
321 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" name=\"foo\">\n"
322 " <item name=\"app:bar\">0</item>\n"
323 "</style>";
324 ASSERT_TRUE(testParse(input));
325
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700326 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800327 ASSERT_NE(nullptr, style);
Adam Lesinski24aad162015-04-24 19:19:30 -0700328 ASSERT_EQ(1u, style->entries.size());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329 EXPECT_EQ(test::parseNameOrDie(u"@android:attr/bar"), style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700330}
331
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700332TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
333 std::string input = "<style name=\"foo.bar\"/>";
334 ASSERT_TRUE(testParse(input));
335
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336 Style* style = test::getValue<Style>(&mTable, u"@style/foo.bar");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800337 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700338 AAPT_ASSERT_TRUE(style->parent);
339 AAPT_ASSERT_TRUE(style->parent.value().name);
340 EXPECT_EQ(style->parent.value().name.value(), test::parseNameOrDie(u"@style/foo"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700341 EXPECT_TRUE(style->parentInferred);
342}
343
344TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
345 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
346 ASSERT_TRUE(testParse(input));
347
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348 Style* style = test::getValue<Style>(&mTable, u"@style/foo.bar");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800349 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700351 EXPECT_FALSE(style->parentInferred);
352}
353
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800354TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
355 std::string input = R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
356 ASSERT_TRUE(testParse(input));
357
358 Style* style = test::getValue<Style>(&mTable, u"@style/foo");
359 ASSERT_NE(nullptr, style);
360 AAPT_ASSERT_TRUE(style->parent);
361 EXPECT_TRUE(style->parent.value().privateReference);
362}
363
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700365 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800366 ASSERT_TRUE(testParse(input));
367
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700368 Id* id = test::getValue<Id>(&mTable, u"@id/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800369 ASSERT_NE(id, nullptr);
370}
371
372TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700373 std::string input = "<declare-styleable name=\"foo\">\n"
374 " <attr name=\"bar\" />\n"
375 " <attr name=\"bat\" format=\"string|reference\"/>\n"
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700376 " <attr name=\"baz\">\n"
377 " <enum name=\"foo\" value=\"1\"/>\n"
378 " </attr>\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700379 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800380 ASSERT_TRUE(testParse(input));
381
Adam Lesinski9f222042015-11-04 13:51:45 -0800382 Maybe<ResourceTable::SearchResult> result =
383 mTable.findResource(test::parseNameOrDie(u"@styleable/foo"));
384 AAPT_ASSERT_TRUE(result);
385 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state);
386
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700387 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388 ASSERT_NE(attr, nullptr);
389 EXPECT_TRUE(attr->isWeak());
390
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391 attr = test::getValue<Attribute>(&mTable, u"@attr/bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392 ASSERT_NE(attr, nullptr);
393 EXPECT_TRUE(attr->isWeak());
394
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700395 attr = test::getValue<Attribute>(&mTable, u"@attr/baz");
396 ASSERT_NE(attr, nullptr);
397 EXPECT_TRUE(attr->isWeak());
398 EXPECT_EQ(1u, attr->symbols.size());
399
400 EXPECT_NE(nullptr, test::getValue<Id>(&mTable, u"@id/foo"));
401
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700402 Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403 ASSERT_NE(styleable, nullptr);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700404 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800405
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406 EXPECT_EQ(test::parseNameOrDie(u"@attr/bar"), styleable->entries[0].name.value());
407 EXPECT_EQ(test::parseNameOrDie(u"@attr/bat"), styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408}
409
Adam Lesinski467f1712015-11-16 17:35:44 -0800410TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
411 std::string input = "<declare-styleable name=\"foo\" xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
412 " <attr name=\"*android:bar\" />\n"
413 " <attr name=\"privAndroid:bat\" />\n"
414 "</declare-styleable>";
415 ASSERT_TRUE(testParse(input));
416 Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo");
417 ASSERT_NE(nullptr, styleable);
418 ASSERT_EQ(2u, styleable->entries.size());
419
420 EXPECT_TRUE(styleable->entries[0].privateReference);
421 AAPT_ASSERT_TRUE(styleable->entries[0].name);
422 EXPECT_EQ(std::u16string(u"android"), styleable->entries[0].name.value().package);
423
424 EXPECT_TRUE(styleable->entries[1].privateReference);
425 AAPT_ASSERT_TRUE(styleable->entries[1].name);
426 EXPECT_EQ(std::u16string(u"android"), styleable->entries[1].name.value().package);
427}
428
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700430 std::string input = "<array name=\"foo\">\n"
431 " <item>@string/ref</item>\n"
432 " <item>hey</item>\n"
433 " <item>23</item>\n"
434 "</array>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435 ASSERT_TRUE(testParse(input));
436
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700437 Array* array = test::getValue<Array>(&mTable, u"@array/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438 ASSERT_NE(array, nullptr);
439 ASSERT_EQ(3u, array->items.size());
440
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700441 EXPECT_NE(nullptr, valueCast<Reference>(array->items[0].get()));
442 EXPECT_NE(nullptr, valueCast<String>(array->items[1].get()));
443 EXPECT_NE(nullptr, valueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444}
445
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700446TEST_F(ResourceParserTest, ParseStringArray) {
447 std::string input = "<string-array name=\"foo\">\n"
448 " <item>\"Werk\"</item>\n"
449 "</string-array>\n";
450 ASSERT_TRUE(testParse(input));
451 EXPECT_NE(nullptr, test::getValue<Array>(&mTable, u"@array/foo"));
452}
453
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700455 std::string input = "<plurals name=\"foo\">\n"
456 " <item quantity=\"other\">apples</item>\n"
457 " <item quantity=\"one\">apple</item>\n"
458 "</plurals>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459 ASSERT_TRUE(testParse(input));
460}
461
462TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700463 std::string input = "<!--This is a comment-->\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700464 "<string name=\"foo\">Hi</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465 ASSERT_TRUE(testParse(input));
466
Adam Lesinskie78fd612015-10-22 12:48:43 -0700467 String* value = test::getValue<String>(&mTable, u"@string/foo");
468 ASSERT_NE(nullptr, value);
469 EXPECT_EQ(value->getComment(), u"This is a comment");
470}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700471
Adam Lesinskie78fd612015-10-22 12:48:43 -0700472TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
473 std::string input = "<!--One-->\n"
474 "<!--Two-->\n"
475 "<string name=\"foo\">Hi</string>";
476
477 ASSERT_TRUE(testParse(input));
478
479 String* value = test::getValue<String>(&mTable, u"@string/foo");
480 ASSERT_NE(nullptr, value);
481 EXPECT_EQ(value->getComment(), u"Two");
482}
483
484TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
485 std::string input = "<!--One-->\n"
486 "<string name=\"foo\">\n"
487 " Hi\n"
488 "<!--Two-->\n"
489 "</string>";
490
491 ASSERT_TRUE(testParse(input));
492
493 String* value = test::getValue<String>(&mTable, u"@string/foo");
494 ASSERT_NE(nullptr, value);
495 EXPECT_EQ(value->getComment(), u"One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496}
497
Adam Lesinskica5638f2015-10-21 14:42:43 -0700498TEST_F(ResourceParserTest, ParseNestedComments) {
499 // We only care about declare-styleable and enum/flag attributes because comments
500 // from those end up in R.java
501 std::string input = R"EOF(
502 <declare-styleable name="foo">
503 <!-- The name of the bar -->
504 <attr name="barName" format="string|reference" />
505 </declare-styleable>
506
507 <attr name="foo">
508 <!-- The very first -->
509 <enum name="one" value="1" />
510 </attr>)EOF";
511 ASSERT_TRUE(testParse(input));
512
513 Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo");
514 ASSERT_NE(nullptr, styleable);
515 ASSERT_EQ(1u, styleable->entries.size());
516
517 EXPECT_EQ(StringPiece16(u"The name of the bar"), styleable->entries.front().getComment());
518
519 Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo");
520 ASSERT_NE(nullptr, attr);
521 ASSERT_EQ(1u, attr->symbols.size());
522
523 EXPECT_EQ(StringPiece16(u"The very first"), attr->symbols.front().symbol.getComment());
524}
525
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800526/*
527 * Declaring an ID as public should not require a separate definition
528 * (as an ID has no value).
529 */
530TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700531 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532 ASSERT_TRUE(testParse(input));
533
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700534 Id* id = test::getValue<Id>(&mTable, u"@id/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800535 ASSERT_NE(nullptr, id);
536}
537
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800538TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinski7751afc2016-01-06 15:45:28 -0800539 std::string input = R"EOF(
540 <string name="foo" product="phone">hi</string>
541 <string name="foo" product="no-sdcard">ho</string>
542 <string name="bar" product="">wee</string>
543 <string name="baz">woo</string>
544 <string name="bit" product="phablet">hoot</string>
545 <string name="bot" product="default">yes</string>
546 )EOF";
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800547 ASSERT_TRUE(testParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700548
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800549 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, u"@string/foo",
550 ConfigDescription::defaultConfig(),
551 "phone"));
552 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, u"@string/foo",
553 ConfigDescription::defaultConfig(),
554 "no-sdcard"));
555 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, u"@string/bar",
556 ConfigDescription::defaultConfig(),
557 ""));
558 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, u"@string/baz",
559 ConfigDescription::defaultConfig(),
560 ""));
561 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, u"@string/bit",
562 ConfigDescription::defaultConfig(),
563 "phablet"));
564 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, u"@string/bot",
565 ConfigDescription::defaultConfig(),
566 "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700567}
568
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800569TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
570 std::string input = R"EOF(
571 <public-group type="attr" first-id="0x01010040">
572 <public name="foo" />
573 <public name="bar" />
574 </public-group>)EOF";
575 ASSERT_TRUE(testParse(input));
576
577 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
578 test::parseNameOrDie(u"@attr/foo"));
579 AAPT_ASSERT_TRUE(result);
580
581 AAPT_ASSERT_TRUE(result.value().package->id);
582 AAPT_ASSERT_TRUE(result.value().type->id);
583 AAPT_ASSERT_TRUE(result.value().entry->id);
584 ResourceId actualId(result.value().package->id.value(),
585 result.value().type->id.value(),
586 result.value().entry->id.value());
587 EXPECT_EQ(ResourceId(0x01010040), actualId);
588
589 result = mTable.findResource(test::parseNameOrDie(u"@attr/bar"));
590 AAPT_ASSERT_TRUE(result);
591
592 AAPT_ASSERT_TRUE(result.value().package->id);
593 AAPT_ASSERT_TRUE(result.value().type->id);
594 AAPT_ASSERT_TRUE(result.value().entry->id);
595 actualId = ResourceId(result.value().package->id.value(),
596 result.value().type->id.value(),
597 result.value().entry->id.value());
598 EXPECT_EQ(ResourceId(0x01010041), actualId);
599}
600
Adam Lesinskifa105052015-11-07 13:34:39 -0800601TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
602 std::string input = R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
603 ASSERT_TRUE(testParse(input));
604
605 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
606 ASSERT_FALSE(testParse(input));
607}
608
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800609TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
610 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
611 ASSERT_TRUE(testParse(input));
612
613 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
614 test::parseNameOrDie(u"@string/bar"));
615 AAPT_ASSERT_TRUE(result);
616 const ResourceEntry* entry = result.value().entry;
617 ASSERT_NE(nullptr, entry);
618 EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state);
619}
620
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800621TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
622 std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
623 ASSERT_TRUE(testParse(input));
624
625 BinaryPrimitive* val = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
626 ASSERT_NE(nullptr, val);
627
628 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
629}
630
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800631} // namespace aapt