blob: e097740966f6df20190eb0f5f986078ea412bd6e [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 Lesinski58a20a62016-07-25 17:56:58 -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 Lesinski58a20a62016-07-25 17:56:58 -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 Lesinski8c3f31f2016-09-07 13:45:13 -070093TEST_F(ResourceParserTest, ParseStyledString) {
94 // Use a surrogate pair unicode point so that we can verify that the span indices
95 // use UTF-16 length and not UTF-18 length.
96 std::string input = "<string name=\"foo\">This is my aunt\u2019s <b>string</b></string>";
97 ASSERT_TRUE(testParse(input));
98
99 StyledString* str = test::getValue<StyledString>(&mTable, "string/foo");
100 ASSERT_NE(nullptr, str);
101
102 const std::string expectedStr = "This is my aunt\u2019s string";
103 EXPECT_EQ(expectedStr, *str->value->str);
104 EXPECT_EQ(1u, str->value->spans.size());
105
106 EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
107 EXPECT_EQ(17u, str->value->spans[0].firstChar);
108 EXPECT_EQ(23u, str->value->spans[0].lastChar);
109}
110
111TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
112 std::string input = "<string name=\"foo\"> This is what I think </string>";
113 ASSERT_TRUE(testParse(input));
114
115 String* str = test::getValue<String>(&mTable, "string/foo");
116 ASSERT_NE(nullptr, str);
117 EXPECT_EQ(std::string("This is what I think"), *str->value);
118
119 input = "<string name=\"foo2\">\" This is what I think \"</string>";
120 ASSERT_TRUE(testParse(input));
121
122 str = test::getValue<String>(&mTable, "string/foo2");
123 ASSERT_NE(nullptr, str);
124 EXPECT_EQ(std::string(" This is what I think "), *str->value);
125}
126
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127TEST_F(ResourceParserTest, IgnoreXliffTags) {
128 std::string input = "<string name=\"foo\" \n"
129 " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n"
130 " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>";
131 ASSERT_TRUE(testParse(input));
132
Adam Lesinski58a20a62016-07-25 17:56:58 -0700133 String* str = test::getValue<String>(&mTable, "string/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 ASSERT_NE(nullptr, str);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700135 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136}
137
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700138TEST_F(ResourceParserTest, ParseNull) {
139 std::string input = "<integer name=\"foo\">@null</integer>";
140 ASSERT_TRUE(testParse(input));
141
142 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
143 // a non-existing value, and this causes problems in styles when trying to resolve
144 // an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
145 // with a data value of 0.
Adam Lesinski58a20a62016-07-25 17:56:58 -0700146 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, "integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700147 ASSERT_NE(nullptr, integer);
148 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
149 EXPECT_EQ(0u, integer->value.data);
150}
151
152TEST_F(ResourceParserTest, ParseEmpty) {
153 std::string input = "<integer name=\"foo\">@empty</integer>";
154 ASSERT_TRUE(testParse(input));
155
Adam Lesinski58a20a62016-07-25 17:56:58 -0700156 BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, "integer/foo");
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700157 ASSERT_NE(nullptr, integer);
158 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
159 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
160}
161
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700163 std::string input = "<attr name=\"foo\" format=\"string\"/>\n"
164 "<attr name=\"bar\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165 ASSERT_TRUE(testParse(input));
166
Adam Lesinski58a20a62016-07-25 17:56:58 -0700167 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
170
Adam Lesinski58a20a62016-07-25 17:56:58 -0700171 attr = test::getValue<Attribute>(&mTable, "attr/bar");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172 ASSERT_NE(nullptr, attr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800173 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->typeMask);
174}
175
Adam Lesinski52364f72016-01-11 13:10:24 -0800176// Old AAPT allowed attributes to be defined under different configurations, but ultimately
177// stored them with the default configuration. Check that we have the same behavior.
178TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
179 const ConfigDescription watchConfig = test::parseConfigOrDie("watch");
180 std::string input = R"EOF(
181 <attr name="foo" />
182 <declare-styleable name="bar">
183 <attr name="baz" />
184 </declare-styleable>)EOF";
185 ASSERT_TRUE(testParse(input, watchConfig));
186
Adam Lesinski58a20a62016-07-25 17:56:58 -0700187 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, "attr/foo", watchConfig));
188 EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, "attr/baz", watchConfig));
189 EXPECT_EQ(nullptr, test::getValueForConfig<Styleable>(&mTable, "styleable/bar", watchConfig));
Adam Lesinski52364f72016-01-11 13:10:24 -0800190
Adam Lesinski58a20a62016-07-25 17:56:58 -0700191 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, "attr/foo"));
192 EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, "attr/baz"));
193 EXPECT_NE(nullptr, test::getValue<Styleable>(&mTable, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800194}
195
Adam Lesinskia5870652015-11-20 15:32:30 -0800196TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
197 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
198 ASSERT_TRUE(testParse(input));
199
Adam Lesinski58a20a62016-07-25 17:56:58 -0700200 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinskia5870652015-11-20 15:32:30 -0800201 ASSERT_NE(nullptr, attr);
202 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->typeMask);
203 EXPECT_EQ(10, attr->minInt);
204 EXPECT_EQ(23, attr->maxInt);
205}
206
207TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
208 std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
209 ASSERT_FALSE(testParse(input));
210}
211
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700213 std::string input = "<declare-styleable name=\"Styleable\">\n"
214 " <attr name=\"foo\" />\n"
215 "</declare-styleable>\n"
216 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217 ASSERT_TRUE(testParse(input));
218
Adam Lesinski58a20a62016-07-25 17:56:58 -0700219 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220 ASSERT_NE(nullptr, attr);
221 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask);
222}
223
224TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700225 std::string input = "<declare-styleable name=\"Theme\">"
226 " <attr name=\"foo\" />\n"
227 "</declare-styleable>\n"
228 "<declare-styleable name=\"Window\">\n"
229 " <attr name=\"foo\" format=\"boolean\"/>\n"
230 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231 ASSERT_TRUE(testParse(input));
232
Adam Lesinski58a20a62016-07-25 17:56:58 -0700233 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234 ASSERT_NE(nullptr, attr);
235 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->typeMask);
236}
237
238TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700239 std::string input = "<attr name=\"foo\">\n"
240 " <enum name=\"bar\" value=\"0\"/>\n"
241 " <enum name=\"bat\" value=\"1\"/>\n"
242 " <enum name=\"baz\" value=\"2\"/>\n"
243 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244 ASSERT_TRUE(testParse(input));
245
Adam Lesinski58a20a62016-07-25 17:56:58 -0700246 Attribute* enumAttr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247 ASSERT_NE(enumAttr, nullptr);
248 EXPECT_EQ(enumAttr->typeMask, android::ResTable_map::TYPE_ENUM);
249 ASSERT_EQ(enumAttr->symbols.size(), 3u);
250
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251 AAPT_ASSERT_TRUE(enumAttr->symbols[0].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700252 EXPECT_EQ(enumAttr->symbols[0].symbol.name.value().entry, "bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253 EXPECT_EQ(enumAttr->symbols[0].value, 0u);
254
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 AAPT_ASSERT_TRUE(enumAttr->symbols[1].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700256 EXPECT_EQ(enumAttr->symbols[1].symbol.name.value().entry, "bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257 EXPECT_EQ(enumAttr->symbols[1].value, 1u);
258
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 AAPT_ASSERT_TRUE(enumAttr->symbols[2].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700260 EXPECT_EQ(enumAttr->symbols[2].symbol.name.value().entry, "baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 EXPECT_EQ(enumAttr->symbols[2].value, 2u);
262}
263
264TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700265 std::string input = "<attr name=\"foo\">\n"
266 " <flag name=\"bar\" value=\"0\"/>\n"
267 " <flag name=\"bat\" value=\"1\"/>\n"
268 " <flag name=\"baz\" value=\"2\"/>\n"
269 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270 ASSERT_TRUE(testParse(input));
271
Adam Lesinski58a20a62016-07-25 17:56:58 -0700272 Attribute* flagAttr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800273 ASSERT_NE(nullptr, flagAttr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274 EXPECT_EQ(flagAttr->typeMask, android::ResTable_map::TYPE_FLAGS);
275 ASSERT_EQ(flagAttr->symbols.size(), 3u);
276
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700277 AAPT_ASSERT_TRUE(flagAttr->symbols[0].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700278 EXPECT_EQ(flagAttr->symbols[0].symbol.name.value().entry, "bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279 EXPECT_EQ(flagAttr->symbols[0].value, 0u);
280
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 AAPT_ASSERT_TRUE(flagAttr->symbols[1].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700282 EXPECT_EQ(flagAttr->symbols[1].symbol.name.value().entry, "bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283 EXPECT_EQ(flagAttr->symbols[1].value, 1u);
284
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285 AAPT_ASSERT_TRUE(flagAttr->symbols[2].symbol.name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700286 EXPECT_EQ(flagAttr->symbols[2].symbol.name.value().entry, "baz");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287 EXPECT_EQ(flagAttr->symbols[2].value, 2u);
288
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 std::unique_ptr<BinaryPrimitive> flagValue = ResourceUtils::tryParseFlagSymbol(flagAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700290 "baz|bat");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800291 ASSERT_NE(nullptr, flagValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292 EXPECT_EQ(flagValue->value.data, 1u | 2u);
293}
294
295TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700296 std::string input = "<attr name=\"foo\">\n"
297 " <enum name=\"bar\" value=\"0\"/>\n"
298 " <enum name=\"bat\" value=\"1\"/>\n"
299 " <enum name=\"bat\" value=\"2\"/>\n"
300 "</attr>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301 ASSERT_FALSE(testParse(input));
302}
303
304TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700305 std::string input = "<style name=\"foo\" parent=\"@style/fu\">\n"
306 " <item name=\"bar\">#ffffffff</item>\n"
307 " <item name=\"bat\">@string/hey</item>\n"
308 " <item name=\"baz\"><b>hey</b></item>\n"
309 "</style>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800310 ASSERT_TRUE(testParse(input));
311
Adam Lesinski58a20a62016-07-25 17:56:58 -0700312 Style* style = test::getValue<Style>(&mTable, "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);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700316 EXPECT_EQ(test::parseNameOrDie("style/fu"), style->parent.value().name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700317 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700319 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700320 EXPECT_EQ(test::parseNameOrDie("attr/bar"), style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321
322 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700323 EXPECT_EQ(test::parseNameOrDie("attr/bat"), style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700324
325 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700326 EXPECT_EQ(test::parseNameOrDie("attr/baz"), style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
Adam Lesinski769de982015-04-10 19:43:55 -0700329TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700330 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinski769de982015-04-10 19:43:55 -0700331 ASSERT_TRUE(testParse(input));
332
Adam Lesinski58a20a62016-07-25 17:56:58 -0700333 Style* style = test::getValue<Style>(&mTable, "style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800334 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335 AAPT_ASSERT_TRUE(style->parent);
336 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700337 EXPECT_EQ(test::parseNameOrDie("com.app:style/Theme"), style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700338}
339
Adam Lesinski24aad162015-04-24 19:19:30 -0700340TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
341 std::string input = "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
342 " name=\"foo\" parent=\"app:Theme\"/>";
343 ASSERT_TRUE(testParse(input));
344
Adam Lesinski58a20a62016-07-25 17:56:58 -0700345 Style* style = test::getValue<Style>(&mTable, "style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800346 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700347 AAPT_ASSERT_TRUE(style->parent);
348 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700349 EXPECT_EQ(test::parseNameOrDie("android:style/Theme"), style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700350}
351
352TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
353 std::string input =
354 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" name=\"foo\">\n"
355 " <item name=\"app:bar\">0</item>\n"
356 "</style>";
357 ASSERT_TRUE(testParse(input));
358
Adam Lesinski58a20a62016-07-25 17:56:58 -0700359 Style* style = test::getValue<Style>(&mTable, "style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800360 ASSERT_NE(nullptr, style);
Adam Lesinski24aad162015-04-24 19:19:30 -0700361 ASSERT_EQ(1u, style->entries.size());
Adam Lesinski58a20a62016-07-25 17:56:58 -0700362 EXPECT_EQ(test::parseNameOrDie("android:attr/bar"), style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700363}
364
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700365TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
366 std::string input = "<style name=\"foo.bar\"/>";
367 ASSERT_TRUE(testParse(input));
368
Adam Lesinski58a20a62016-07-25 17:56:58 -0700369 Style* style = test::getValue<Style>(&mTable, "style/foo.bar");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800370 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700371 AAPT_ASSERT_TRUE(style->parent);
372 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinski58a20a62016-07-25 17:56:58 -0700373 EXPECT_EQ(style->parent.value().name.value(), test::parseNameOrDie("style/foo"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700374 EXPECT_TRUE(style->parentInferred);
375}
376
377TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
378 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
379 ASSERT_TRUE(testParse(input));
380
Adam Lesinski58a20a62016-07-25 17:56:58 -0700381 Style* style = test::getValue<Style>(&mTable, "style/foo.bar");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800382 ASSERT_NE(nullptr, style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700383 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700384 EXPECT_FALSE(style->parentInferred);
385}
386
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800387TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
388 std::string input = R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
389 ASSERT_TRUE(testParse(input));
390
Adam Lesinski58a20a62016-07-25 17:56:58 -0700391 Style* style = test::getValue<Style>(&mTable, "style/foo");
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800392 ASSERT_NE(nullptr, style);
393 AAPT_ASSERT_TRUE(style->parent);
394 EXPECT_TRUE(style->parent.value().privateReference);
395}
396
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700398 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800399 ASSERT_TRUE(testParse(input));
400
Adam Lesinski58a20a62016-07-25 17:56:58 -0700401 Id* id = test::getValue<Id>(&mTable, "id/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402 ASSERT_NE(id, nullptr);
403}
404
405TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700406 std::string input = "<declare-styleable name=\"foo\">\n"
407 " <attr name=\"bar\" />\n"
408 " <attr name=\"bat\" format=\"string|reference\"/>\n"
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700409 " <attr name=\"baz\">\n"
410 " <enum name=\"foo\" value=\"1\"/>\n"
411 " </attr>\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700412 "</declare-styleable>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413 ASSERT_TRUE(testParse(input));
414
Adam Lesinski9f222042015-11-04 13:51:45 -0800415 Maybe<ResourceTable::SearchResult> result =
Adam Lesinski58a20a62016-07-25 17:56:58 -0700416 mTable.findResource(test::parseNameOrDie("styleable/foo"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800417 AAPT_ASSERT_TRUE(result);
418 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state);
419
Adam Lesinski58a20a62016-07-25 17:56:58 -0700420 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421 ASSERT_NE(attr, nullptr);
422 EXPECT_TRUE(attr->isWeak());
423
Adam Lesinski58a20a62016-07-25 17:56:58 -0700424 attr = test::getValue<Attribute>(&mTable, "attr/bat");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425 ASSERT_NE(attr, nullptr);
426 EXPECT_TRUE(attr->isWeak());
427
Adam Lesinski58a20a62016-07-25 17:56:58 -0700428 attr = test::getValue<Attribute>(&mTable, "attr/baz");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700429 ASSERT_NE(attr, nullptr);
430 EXPECT_TRUE(attr->isWeak());
431 EXPECT_EQ(1u, attr->symbols.size());
432
Adam Lesinski58a20a62016-07-25 17:56:58 -0700433 EXPECT_NE(nullptr, test::getValue<Id>(&mTable, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700434
Adam Lesinski58a20a62016-07-25 17:56:58 -0700435 Styleable* styleable = test::getValue<Styleable>(&mTable, "styleable/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436 ASSERT_NE(styleable, nullptr);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700437 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438
Adam Lesinski58a20a62016-07-25 17:56:58 -0700439 EXPECT_EQ(test::parseNameOrDie("attr/bar"), styleable->entries[0].name.value());
440 EXPECT_EQ(test::parseNameOrDie("attr/bat"), styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441}
442
Adam Lesinski467f1712015-11-16 17:35:44 -0800443TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
444 std::string input = "<declare-styleable name=\"foo\" xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
445 " <attr name=\"*android:bar\" />\n"
446 " <attr name=\"privAndroid:bat\" />\n"
447 "</declare-styleable>";
448 ASSERT_TRUE(testParse(input));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700449 Styleable* styleable = test::getValue<Styleable>(&mTable, "styleable/foo");
Adam Lesinski467f1712015-11-16 17:35:44 -0800450 ASSERT_NE(nullptr, styleable);
451 ASSERT_EQ(2u, styleable->entries.size());
452
453 EXPECT_TRUE(styleable->entries[0].privateReference);
454 AAPT_ASSERT_TRUE(styleable->entries[0].name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700455 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800456
457 EXPECT_TRUE(styleable->entries[1].privateReference);
458 AAPT_ASSERT_TRUE(styleable->entries[1].name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700459 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800460}
461
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800462TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700463 std::string input = "<array name=\"foo\">\n"
464 " <item>@string/ref</item>\n"
465 " <item>hey</item>\n"
466 " <item>23</item>\n"
467 "</array>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468 ASSERT_TRUE(testParse(input));
469
Adam Lesinski58a20a62016-07-25 17:56:58 -0700470 Array* array = test::getValue<Array>(&mTable, "array/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800471 ASSERT_NE(array, nullptr);
472 ASSERT_EQ(3u, array->items.size());
473
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474 EXPECT_NE(nullptr, valueCast<Reference>(array->items[0].get()));
475 EXPECT_NE(nullptr, valueCast<String>(array->items[1].get()));
476 EXPECT_NE(nullptr, valueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477}
478
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700479TEST_F(ResourceParserTest, ParseStringArray) {
480 std::string input = "<string-array name=\"foo\">\n"
481 " <item>\"Werk\"</item>\n"
482 "</string-array>\n";
483 ASSERT_TRUE(testParse(input));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700484 EXPECT_NE(nullptr, test::getValue<Array>(&mTable, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700485}
486
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700488 std::string input = "<plurals name=\"foo\">\n"
489 " <item quantity=\"other\">apples</item>\n"
490 " <item quantity=\"one\">apple</item>\n"
491 "</plurals>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800492 ASSERT_TRUE(testParse(input));
493}
494
495TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700496 std::string input = "<!--This is a comment-->\n"
Adam Lesinski24aad162015-04-24 19:19:30 -0700497 "<string name=\"foo\">Hi</string>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498 ASSERT_TRUE(testParse(input));
499
Adam Lesinski58a20a62016-07-25 17:56:58 -0700500 String* value = test::getValue<String>(&mTable, "string/foo");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700501 ASSERT_NE(nullptr, value);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700502 EXPECT_EQ(value->getComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700503}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700504
Adam Lesinskie78fd612015-10-22 12:48:43 -0700505TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
506 std::string input = "<!--One-->\n"
507 "<!--Two-->\n"
508 "<string name=\"foo\">Hi</string>";
509
510 ASSERT_TRUE(testParse(input));
511
Adam Lesinski58a20a62016-07-25 17:56:58 -0700512 String* value = test::getValue<String>(&mTable, "string/foo");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700513 ASSERT_NE(nullptr, value);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700514 EXPECT_EQ(value->getComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700515}
516
517TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
518 std::string input = "<!--One-->\n"
519 "<string name=\"foo\">\n"
520 " Hi\n"
521 "<!--Two-->\n"
522 "</string>";
523
524 ASSERT_TRUE(testParse(input));
525
Adam Lesinski58a20a62016-07-25 17:56:58 -0700526 String* value = test::getValue<String>(&mTable, "string/foo");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700527 ASSERT_NE(nullptr, value);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700528 EXPECT_EQ(value->getComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529}
530
Adam Lesinskica5638f2015-10-21 14:42:43 -0700531TEST_F(ResourceParserTest, ParseNestedComments) {
532 // We only care about declare-styleable and enum/flag attributes because comments
533 // from those end up in R.java
534 std::string input = R"EOF(
535 <declare-styleable name="foo">
536 <!-- The name of the bar -->
537 <attr name="barName" format="string|reference" />
538 </declare-styleable>
539
540 <attr name="foo">
541 <!-- The very first -->
542 <enum name="one" value="1" />
543 </attr>)EOF";
544 ASSERT_TRUE(testParse(input));
545
Adam Lesinski58a20a62016-07-25 17:56:58 -0700546 Styleable* styleable = test::getValue<Styleable>(&mTable, "styleable/foo");
Adam Lesinskica5638f2015-10-21 14:42:43 -0700547 ASSERT_NE(nullptr, styleable);
548 ASSERT_EQ(1u, styleable->entries.size());
549
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700550 EXPECT_EQ(StringPiece("The name of the bar"), styleable->entries.front().getComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700551
Adam Lesinski58a20a62016-07-25 17:56:58 -0700552 Attribute* attr = test::getValue<Attribute>(&mTable, "attr/foo");
Adam Lesinskica5638f2015-10-21 14:42:43 -0700553 ASSERT_NE(nullptr, attr);
554 ASSERT_EQ(1u, attr->symbols.size());
555
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700556 EXPECT_EQ(StringPiece("The very first"), attr->symbols.front().symbol.getComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700557}
558
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800559/*
560 * Declaring an ID as public should not require a separate definition
561 * (as an ID has no value).
562 */
563TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700564 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800565 ASSERT_TRUE(testParse(input));
566
Adam Lesinski58a20a62016-07-25 17:56:58 -0700567 Id* id = test::getValue<Id>(&mTable, "id/foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800568 ASSERT_NE(nullptr, id);
569}
570
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800571TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinski7751afc2016-01-06 15:45:28 -0800572 std::string input = R"EOF(
573 <string name="foo" product="phone">hi</string>
574 <string name="foo" product="no-sdcard">ho</string>
575 <string name="bar" product="">wee</string>
576 <string name="baz">woo</string>
577 <string name="bit" product="phablet">hoot</string>
578 <string name="bot" product="default">yes</string>
579 )EOF";
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800580 ASSERT_TRUE(testParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700581
Adam Lesinski58a20a62016-07-25 17:56:58 -0700582 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "string/foo",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800583 ConfigDescription::defaultConfig(),
584 "phone"));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700585 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "string/foo",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800586 ConfigDescription::defaultConfig(),
587 "no-sdcard"));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700588 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "string/bar",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800589 ConfigDescription::defaultConfig(),
590 ""));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700591 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "string/baz",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800592 ConfigDescription::defaultConfig(),
593 ""));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700594 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "string/bit",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800595 ConfigDescription::defaultConfig(),
596 "phablet"));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700597 EXPECT_NE(nullptr, test::getValueForConfigAndProduct<String>(&mTable, "string/bot",
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800598 ConfigDescription::defaultConfig(),
599 "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700600}
601
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800602TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
603 std::string input = R"EOF(
604 <public-group type="attr" first-id="0x01010040">
605 <public name="foo" />
606 <public name="bar" />
607 </public-group>)EOF";
608 ASSERT_TRUE(testParse(input));
609
610 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
Adam Lesinski58a20a62016-07-25 17:56:58 -0700611 test::parseNameOrDie("attr/foo"));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800612 AAPT_ASSERT_TRUE(result);
613
614 AAPT_ASSERT_TRUE(result.value().package->id);
615 AAPT_ASSERT_TRUE(result.value().type->id);
616 AAPT_ASSERT_TRUE(result.value().entry->id);
617 ResourceId actualId(result.value().package->id.value(),
618 result.value().type->id.value(),
619 result.value().entry->id.value());
620 EXPECT_EQ(ResourceId(0x01010040), actualId);
621
Adam Lesinski58a20a62016-07-25 17:56:58 -0700622 result = mTable.findResource(test::parseNameOrDie("attr/bar"));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800623 AAPT_ASSERT_TRUE(result);
624
625 AAPT_ASSERT_TRUE(result.value().package->id);
626 AAPT_ASSERT_TRUE(result.value().type->id);
627 AAPT_ASSERT_TRUE(result.value().entry->id);
628 actualId = ResourceId(result.value().package->id.value(),
629 result.value().type->id.value(),
630 result.value().entry->id.value());
631 EXPECT_EQ(ResourceId(0x01010041), actualId);
632}
633
Adam Lesinskifa105052015-11-07 13:34:39 -0800634TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
635 std::string input = R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
636 ASSERT_TRUE(testParse(input));
637
638 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
639 ASSERT_FALSE(testParse(input));
640}
641
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800642TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
643 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
644 ASSERT_TRUE(testParse(input));
645
646 Maybe<ResourceTable::SearchResult> result = mTable.findResource(
Adam Lesinski58a20a62016-07-25 17:56:58 -0700647 test::parseNameOrDie("string/bar"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800648 AAPT_ASSERT_TRUE(result);
649 const ResourceEntry* entry = result.value().entry;
650 ASSERT_NE(nullptr, entry);
651 EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state);
652}
653
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800654TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
655 std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
656 ASSERT_TRUE(testParse(input));
657
Adam Lesinski58a20a62016-07-25 17:56:58 -0700658 BinaryPrimitive* val = test::getValue<BinaryPrimitive>(&mTable, "integer/foo");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800659 ASSERT_NE(nullptr, val);
660
661 EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
662}
663
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800664} // namespace aapt