blob: e60ef66a21dbdcde83a228d0c155ec93088e9f1c [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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <sstream>
20#include <string>
21
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070025#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080026#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskie597d682017-06-01 17:16:44 -070028using ::android::StringPiece;
29using ::testing::Eq;
30using ::testing::NotNull;
Adam Lesinskid5083f62017-01-16 15:07:21 -080031
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034constexpr const char* kXmlPreamble =
35 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 std::stringstream input(kXmlPreamble);
40 input << "<attr name=\"foo\"/>" << std::endl;
41 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
43 xml::XmlPullParser xml_parser(input);
44 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070045}
46
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047class ResourceParserTest : public ::testing::Test {
48 public:
49 void SetUp() override { context_ = test::ContextBuilder().Build(); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 ::testing::AssertionResult TestParse(const StringPiece& str) {
52 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinski52364f72016-01-11 13:10:24 -080054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 ::testing::AssertionResult TestParse(const StringPiece& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 const ConfigDescription& config) {
57 std::stringstream input(kXmlPreamble);
58 input << "<resources>\n" << str << "\n</resources>" << std::endl;
59 ResourceParserOptions parserOptions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 config, parserOptions);
62 xml::XmlPullParser xmlParser(input);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return ::testing::AssertionFailure();
67 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068
69 protected:
70 ResourceTable table_;
71 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072};
73
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 ASSERT_NE(nullptr, str);
80 EXPECT_EQ(std::string(" hey there "), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -080081 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082}
83
84TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 ASSERT_NE(nullptr, str);
90 EXPECT_EQ(std::string("?123"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -080091 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092}
93
Adam Lesinski9f222042015-11-04 13:51:45 -080094TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::string input = "<string name=\"foo\">%d %s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 ASSERT_FALSE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 input = "<string name=\"foo\">%1$d %2$s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 ASSERT_TRUE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -0800100}
101
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700102TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800104 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700106 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 ASSERT_NE(nullptr, str);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700111
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700112 const std::string expected_str = "This is my aunt\u2019s fickle string";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 EXPECT_EQ(expected_str, *str->value->str);
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700114 EXPECT_EQ(2u, str->value->spans.size());
Adam Lesinski75421622017-01-06 15:20:04 -0800115 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 EXPECT_EQ(17u, str->value->spans[0].first_char);
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700119 EXPECT_EQ(30u, str->value->spans[0].last_char);
120
121 EXPECT_EQ(std::string("small"), *str->value->spans[1].name);
122 EXPECT_EQ(24u, str->value->spans[1].first_char);
123 EXPECT_EQ(30u, str->value->spans[1].last_char);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700124}
125
126TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 std::string input = "<string name=\"foo\"> This is what I think </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 ASSERT_NE(nullptr, str);
132 EXPECT_EQ(std::string("This is what I think"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -0800133 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 input = "<string name=\"foo2\">\" This is what I think \"</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 ASSERT_NE(nullptr, str);
140 EXPECT_EQ(std::string(" This is what I think "), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700141}
142
Adam Lesinski75421622017-01-06 15:20:04 -0800143TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
144 std::string input = R"EOF(
145 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
146 There are <xliff:source>no</xliff:source> apples</string>)EOF";
147 ASSERT_TRUE(TestParse(input));
148
149 String* str = test::GetValue<String>(&table_, "string/foo");
150 ASSERT_NE(nullptr, str);
151 EXPECT_EQ(StringPiece("There are no apples"), StringPiece(*str->value));
152 EXPECT_TRUE(str->untranslatable_sections.empty());
153}
154
155TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
156 std::string input = R"EOF(
157 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
158 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)EOF";
159 EXPECT_FALSE(TestParse(input));
160}
161
162TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
163 std::string input = R"EOF(
164 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
165 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 ASSERT_NE(nullptr, str);
170 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski75421622017-01-06 15:20:04 -0800171
172 ASSERT_EQ(1u, str->untranslatable_sections.size());
173
174 // We expect indices and lengths that span to include the whitespace
175 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
176 // needed (to deal with line breaks, etc.).
177 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
178 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
179}
180
181TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
182 std::string input = R"EOF(
183 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
184 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)EOF";
185 ASSERT_TRUE(TestParse(input));
186
187 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
188 ASSERT_NE(nullptr, str);
189 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value->str));
190
191 ASSERT_EQ(1u, str->untranslatable_sections.size());
192
193 // We expect indices and lengths that span to include the whitespace
194 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
195 // needed (to deal with line breaks, etc.).
196 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
197 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198}
199
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700200TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 std::string input = "<integer name=\"foo\">@null</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
205 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800206 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 // with a data value of 0.
Adam Lesinski75421622017-01-06 15:20:04 -0800208 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 ASSERT_NE(nullptr, integer);
Adam Lesinski75421622017-01-06 15:20:04 -0800210 EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 EXPECT_EQ(0u, integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700212}
213
214TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 std::string input = "<integer name=\"foo\">@empty</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700217
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 BinaryPrimitive* integer =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 ASSERT_NE(nullptr, integer);
221 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
222 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700223}
224
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 std::string input =
227 "<attr name=\"foo\" format=\"string\"/>\n"
228 "<attr name=\"bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238}
239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240// Old AAPT allowed attributes to be defined under different configurations, but
241// ultimately
242// stored them with the default configuration. Check that we have the same
243// behavior.
244TEST_F(ResourceParserTest,
245 ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 std::string input = R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800248 <attr name="foo" />
249 <declare-styleable name="bar">
250 <attr name="baz" />
251 </declare-styleable>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/foo",
255 watch_config));
256 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/baz",
257 watch_config));
258 EXPECT_EQ(nullptr, test::GetValueForConfig<Styleable>(
259 &table_, "styleable/bar", watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/foo"));
262 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/baz"));
263 EXPECT_NE(nullptr, test::GetValue<Styleable>(&table_, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800264}
265
Adam Lesinskia5870652015-11-20 15:32:30 -0800266TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 std::string input =
268 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask);
274 EXPECT_EQ(10, attr->min_int);
275 EXPECT_EQ(23, attr->max_int);
Adam Lesinskia5870652015-11-20 15:32:30 -0800276}
277
278TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 std::string input =
280 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 ASSERT_FALSE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800282}
283
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 std::string input =
286 "<declare-styleable name=\"Styleable\">\n"
287 " <attr name=\"foo\" />\n"
288 "</declare-styleable>\n"
289 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295}
296
297TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 std::string input =
299 "<declare-styleable name=\"Theme\">"
300 " <attr name=\"foo\" />\n"
301 "</declare-styleable>\n"
302 "<declare-styleable name=\"Window\">\n"
303 " <attr name=\"foo\" format=\"boolean\"/>\n"
304 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800310}
311
312TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 std::string input =
314 "<attr name=\"foo\">\n"
315 " <enum name=\"bar\" value=\"0\"/>\n"
316 " <enum name=\"bat\" value=\"1\"/>\n"
317 " <enum name=\"baz\" value=\"2\"/>\n"
318 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
322 ASSERT_NE(enum_attr, nullptr);
323 EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM);
324 ASSERT_EQ(enum_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800325
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
327 EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar");
328 EXPECT_EQ(enum_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
331 EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat");
332 EXPECT_EQ(enum_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
335 EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz");
336 EXPECT_EQ(enum_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337}
338
339TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 std::string input =
341 "<attr name=\"foo\">\n"
342 " <flag name=\"bar\" value=\"0\"/>\n"
343 " <flag name=\"bat\" value=\"1\"/>\n"
344 " <flag name=\"baz\" value=\"2\"/>\n"
345 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
349 ASSERT_NE(nullptr, flag_attr);
350 EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS);
351 ASSERT_EQ(flag_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
354 EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar");
355 EXPECT_EQ(flag_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
358 EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat");
359 EXPECT_EQ(flag_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
362 EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz");
363 EXPECT_EQ(flag_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 std::unique_ptr<BinaryPrimitive> flag_value =
366 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
367 ASSERT_NE(nullptr, flag_value);
368 EXPECT_EQ(flag_value->value.data, 1u | 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800369}
370
371TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 std::string input =
373 "<attr name=\"foo\">\n"
374 " <enum name=\"bar\" value=\"0\"/>\n"
375 " <enum name=\"bat\" value=\"1\"/>\n"
376 " <enum name=\"bat\" value=\"2\"/>\n"
377 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379}
380
381TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 std::string input =
383 "<style name=\"foo\" parent=\"@style/fu\">\n"
384 " <item name=\"bar\">#ffffffff</item>\n"
385 " <item name=\"bat\">@string/hey</item>\n"
386 " <item name=\"baz\"><b>hey</b></item>\n"
387 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 ASSERT_NE(nullptr, style);
392 AAPT_ASSERT_TRUE(style->parent);
393 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 EXPECT_EQ(test::ParseNameOrDie("style/fu"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 style->parent.value().name.value());
396 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700401
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 EXPECT_EQ(test::ParseNameOrDie("attr/baz"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800409}
410
Adam Lesinski769de982015-04-10 19:43:55 -0700411TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 ASSERT_TRUE(TestParse(input));
Adam Lesinski769de982015-04-10 19:43:55 -0700414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 ASSERT_NE(nullptr, style);
417 AAPT_ASSERT_TRUE(style->parent);
418 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 EXPECT_EQ(test::ParseNameOrDie("com.app:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700421}
422
Adam Lesinski24aad162015-04-24 19:19:30 -0700423TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 std::string input =
425 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
426 " name=\"foo\" parent=\"app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 ASSERT_NE(nullptr, style);
431 AAPT_ASSERT_TRUE(style->parent);
432 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 EXPECT_EQ(test::ParseNameOrDie("android:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700435}
436
437TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 std::string input =
439 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" "
440 "name=\"foo\">\n"
441 " <item name=\"app:bar\">0</item>\n"
442 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 ASSERT_NE(nullptr, style);
447 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 EXPECT_EQ(test::ParseNameOrDie("android:attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700450}
451
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700452TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 std::string input = "<style name=\"foo.bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 ASSERT_NE(nullptr, style);
458 AAPT_ASSERT_TRUE(style->parent);
459 AAPT_ASSERT_TRUE(style->parent.value().name);
460 EXPECT_EQ(style->parent.value().name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 test::ParseNameOrDie("style/foo"));
462 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700463}
464
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465TEST_F(ResourceParserTest,
466 ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
467 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700469
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 ASSERT_NE(nullptr, style);
472 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700474}
475
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800476TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 std::string input =
478 R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 ASSERT_TRUE(TestParse(input));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 ASSERT_NE(nullptr, style);
483 AAPT_ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800485}
486
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 Id* id = test::GetValue<Id>(&table_, "id/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 ASSERT_NE(id, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493}
494
495TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 std::string input =
497 "<declare-styleable name=\"foo\">\n"
498 " <attr name=\"bar\" />\n"
499 " <attr name=\"bat\" format=\"string|reference\"/>\n"
500 " <attr name=\"baz\">\n"
501 " <enum name=\"foo\" value=\"1\"/>\n"
502 " </attr>\n"
503 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 AAPT_ASSERT_TRUE(result);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state);
Adam Lesinski9f222042015-11-04 13:51:45 -0800510
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 EXPECT_EQ(1u, attr->symbols.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 EXPECT_NE(nullptr, test::GetValue<Id>(&table_, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700525
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 ASSERT_NE(styleable, nullptr);
528 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 styleable->entries[0].name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800534}
535
Adam Lesinski467f1712015-11-16 17:35:44 -0800536TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 std::string input =
538 "<declare-styleable name=\"foo\" "
539 "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
540 " <attr name=\"*android:bar\" />\n"
541 " <attr name=\"privAndroid:bat\" />\n"
542 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 ASSERT_TRUE(TestParse(input));
544 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 ASSERT_NE(nullptr, styleable);
546 ASSERT_EQ(2u, styleable->entries.size());
Adam Lesinski467f1712015-11-16 17:35:44 -0800547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 AAPT_ASSERT_TRUE(styleable->entries[0].name);
550 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 AAPT_ASSERT_TRUE(styleable->entries[1].name);
554 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800555}
556
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800557TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 std::string input =
559 "<array name=\"foo\">\n"
560 " <item>@string/ref</item>\n"
561 " <item>hey</item>\n"
562 " <item>23</item>\n"
563 "</array>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800565
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 ASSERT_NE(array, nullptr);
568 ASSERT_EQ(3u, array->items.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800569
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 EXPECT_NE(nullptr, ValueCast<Reference>(array->items[0].get()));
571 EXPECT_NE(nullptr, ValueCast<String>(array->items[1].get()));
572 EXPECT_NE(nullptr, ValueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800573}
574
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700575TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700576 std::string input = R"EOF(
577 <string-array name="foo">
578 <item>"Werk"</item>"
579 </string-array>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 ASSERT_TRUE(TestParse(input));
581 EXPECT_NE(nullptr, test::GetValue<Array>(&table_, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700582}
583
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700584TEST_F(ResourceParserTest, ParseArrayWithFormat) {
585 std::string input = R"EOF(
586 <array name="foo" format="string">
587 <item>100</item>
588 </array>)EOF";
589 ASSERT_TRUE(TestParse(input));
590
591 Array* array = test::GetValue<Array>(&table_, "array/foo");
592 ASSERT_NE(nullptr, array);
593
594 ASSERT_EQ(1u, array->items.size());
595
596 String* str = ValueCast<String>(array->items[0].get());
597 ASSERT_NE(nullptr, str);
598 EXPECT_EQ(std::string("100"), *str->value);
599}
600
601TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
602 std::string input = R"EOF(
603 <array name="foo" format="integer">
604 <item>Hi</item>
605 </array>)EOF";
606 ASSERT_FALSE(TestParse(input));
607}
608
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800609TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 std::string input =
611 "<plurals name=\"foo\">\n"
612 " <item quantity=\"other\">apples</item>\n"
613 " <item quantity=\"one\">apple</item>\n"
614 "</plurals>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800616
617 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
618 ASSERT_NE(nullptr, plural);
619 EXPECT_EQ(nullptr, plural->values[Plural::Zero]);
620 EXPECT_EQ(nullptr, plural->values[Plural::Two]);
621 EXPECT_EQ(nullptr, plural->values[Plural::Few]);
622 EXPECT_EQ(nullptr, plural->values[Plural::Many]);
623
624 EXPECT_NE(nullptr, plural->values[Plural::One]);
625 EXPECT_NE(nullptr, plural->values[Plural::Other]);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800626}
627
628TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 std::string input =
630 "<!--This is a comment-->\n"
631 "<string name=\"foo\">Hi</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 EXPECT_EQ(value->GetComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700637}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638
Adam Lesinskie78fd612015-10-22 12:48:43 -0700639TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 std::string input =
641 "<!--One-->\n"
642 "<!--Two-->\n"
643 "<string name=\"foo\">Hi</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700644
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649 EXPECT_EQ(value->GetComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700650}
651
652TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 std::string input =
654 "<!--One-->\n"
655 "<string name=\"foo\">\n"
656 " Hi\n"
657 "<!--Two-->\n"
658 "</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700661
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 EXPECT_EQ(value->GetComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800665}
666
Adam Lesinskica5638f2015-10-21 14:42:43 -0700667TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 // We only care about declare-styleable and enum/flag attributes because
669 // comments
670 // from those end up in R.java
671 std::string input = R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -0700672 <declare-styleable name="foo">
673 <!-- The name of the bar -->
674 <attr name="barName" format="string|reference" />
675 </declare-styleable>
676
677 <attr name="foo">
678 <!-- The very first -->
679 <enum name="one" value="1" />
680 </attr>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 ASSERT_NE(nullptr, styleable);
685 ASSERT_EQ(1u, styleable->entries.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700686
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 EXPECT_EQ(StringPiece("The name of the bar"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 styleable->entries.front().GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700689
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 ASSERT_NE(nullptr, attr);
692 ASSERT_EQ(1u, attr->symbols.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700693
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 EXPECT_EQ(StringPiece("The very first"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 attr->symbols.front().symbol.GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700696}
697
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800698/*
699 * Declaring an ID as public should not require a separate definition
700 * (as an ID has no value).
701 */
702TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800705
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 Id* id = test::GetValue<Id>(&table_, "id/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 ASSERT_NE(nullptr, id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800708}
709
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800710TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 std::string input = R"EOF(
Adam Lesinski7751afc2016-01-06 15:45:28 -0800712 <string name="foo" product="phone">hi</string>
713 <string name="foo" product="no-sdcard">ho</string>
714 <string name="bar" product="">wee</string>
715 <string name="baz">woo</string>
716 <string name="bit" product="phablet">hoot</string>
717 <string name="bot" product="default">yes</string>
718 )EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700720
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
722 &table_, "string/foo",
723 ConfigDescription::DefaultConfig(), "phone"));
724 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
725 &table_, "string/foo",
726 ConfigDescription::DefaultConfig(), "no-sdcard"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 test::GetValueForConfigAndProduct<String>(
729 &table_, "string/bar", ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 test::GetValueForConfigAndProduct<String>(
732 &table_, "string/baz", ConfigDescription::DefaultConfig(), ""));
733 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
734 &table_, "string/bit",
735 ConfigDescription::DefaultConfig(), "phablet"));
736 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
737 &table_, "string/bot",
738 ConfigDescription::DefaultConfig(), "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700739}
740
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800741TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 std::string input = R"EOF(
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800743 <public-group type="attr" first-id="0x01010040">
744 <public name="foo" />
745 <public name="bar" />
746 </public-group>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800748
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 AAPT_ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800752
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 AAPT_ASSERT_TRUE(result.value().package->id);
754 AAPT_ASSERT_TRUE(result.value().type->id);
755 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756 ResourceId actual_id(result.value().package->id.value(),
757 result.value().type->id.value(),
758 result.value().entry->id.value());
759 EXPECT_EQ(ResourceId(0x01010040), actual_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 AAPT_ASSERT_TRUE(result);
763
764 AAPT_ASSERT_TRUE(result.value().package->id);
765 AAPT_ASSERT_TRUE(result.value().type->id);
766 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 actual_id = ResourceId(result.value().package->id.value(),
768 result.value().type->id.value(),
769 result.value().entry->id.value());
770 EXPECT_EQ(ResourceId(0x01010041), actual_id);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800771}
772
Adam Lesinskifa105052015-11-07 13:34:39 -0800773TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 std::string input =
775 R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 ASSERT_TRUE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800777
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 ASSERT_FALSE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800780}
781
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700782TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 ASSERT_TRUE(TestParse(input));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800785
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 AAPT_ASSERT_TRUE(result);
789 const ResourceEntry* entry = result.value().entry;
790 ASSERT_NE(nullptr, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791 EXPECT_EQ(SymbolState::kUndefined, entry->symbol_status.state);
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700792 EXPECT_TRUE(entry->symbol_status.allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800793}
794
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800795TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskie597d682017-06-01 17:16:44 -0700796 std::string input = R"(<item name="foo" type="integer" format="float">0.3</item>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 ASSERT_TRUE(TestParse(input));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800798
Adam Lesinskie597d682017-06-01 17:16:44 -0700799 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
800 ASSERT_THAT(val, NotNull());
801 EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800802
Adam Lesinskie597d682017-06-01 17:16:44 -0700803 input = R"(<item name="bar" type="integer" format="fraction">100</item>)";
804 ASSERT_FALSE(TestParse(input));
805}
806
807// An <item> without a format specifier accepts all types of values.
808TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
809 std::string input = R"(<item name="foo" type="integer">100%p</item>)";
810 ASSERT_TRUE(TestParse(input));
811
812 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
813 ASSERT_THAT(val, NotNull());
814 EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800815}
816
Adam Lesinski86d67df2017-01-31 13:47:27 -0800817TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
818 std::string input = R"EOF(<item name="foo" type="configVarying">Hey</item>)EOF";
819 ASSERT_TRUE(TestParse(input));
820 ASSERT_NE(nullptr, test::GetValue<String>(&table_, "configVarying/foo"));
821}
822
823TEST_F(ResourceParserTest, ParseBagElement) {
824 std::string input =
825 R"EOF(<bag name="bag" type="configVarying"><item name="test">Hello!</item></bag>)EOF";
826 ASSERT_TRUE(TestParse(input));
827
828 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
829 ASSERT_NE(nullptr, val);
830
831 ASSERT_EQ(1u, val->entries.size());
832 EXPECT_EQ(Reference(test::ParseNameOrDie("attr/test")), val->entries[0].key);
833 EXPECT_NE(nullptr, ValueCast<RawString>(val->entries[0].value.get()));
834}
835
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836} // namespace aapt