blob: 5352ca8679b3e055a181fa2ea3a23758ebb1d724 [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 Lesinski90919972017-06-01 15:22:57 -070028using ::aapt::test::ValueEq;
Adam Lesinskie597d682017-06-01 17:16:44 -070029using ::android::StringPiece;
30using ::testing::Eq;
31using ::testing::NotNull;
Adam Lesinski90919972017-06-01 15:22:57 -070032using ::testing::Pointee;
Adam Lesinskid5083f62017-01-16 15:07:21 -080033
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034namespace aapt {
35
Adam Lesinski90919972017-06-01 15:22:57 -070036constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 std::stringstream input(kXmlPreamble);
Adam Lesinski90919972017-06-01 15:22:57 -070041 input << R"(<attr name="foo"/>)" << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
44 xml::XmlPullParser xml_parser(input);
45 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070046}
47
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048class ResourceParserTest : public ::testing::Test {
49 public:
Adam Lesinski90919972017-06-01 15:22:57 -070050 void SetUp() override {
51 context_ = test::ContextBuilder().Build();
52 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 ::testing::AssertionResult TestParse(const StringPiece& str) {
55 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 }
Adam Lesinski52364f72016-01-11 13:10:24 -080057
Adam Lesinski90919972017-06-01 15:22:57 -070058 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 std::stringstream input(kXmlPreamble);
60 input << "<resources>\n" << str << "\n</resources>" << std::endl;
61 ResourceParserOptions parserOptions;
Adam Lesinski90919972017-06-01 15:22:57 -070062 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
63 parserOptions);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 xml::XmlPullParser xmlParser(input);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 return ::testing::AssertionFailure();
69 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070
71 protected:
72 ResourceTable table_;
73 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074};
75
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 std::string input = "<string name=\"foo\"> \" hey there \" </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 ASSERT_NE(nullptr, str);
82 EXPECT_EQ(std::string(" hey there "), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -080083 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
86TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 std::string input = "<string name=\"foo\">\\?123</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 ASSERT_NE(nullptr, str);
92 EXPECT_EQ(std::string("?123"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -080093 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094}
95
Adam Lesinski9f222042015-11-04 13:51:45 -080096TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 std::string input = "<string name=\"foo\">%d %s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 ASSERT_FALSE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -080099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 input = "<string name=\"foo\">%1$d %2$s</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 ASSERT_TRUE(TestParse(input));
Adam Lesinski9f222042015-11-04 13:51:45 -0800102}
103
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700104TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800106 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700108 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 ASSERT_NE(nullptr, str);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700113
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700114 const std::string expected_str = "This is my aunt\u2019s fickle string";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 EXPECT_EQ(expected_str, *str->value->str);
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700116 EXPECT_EQ(2u, str->value->spans.size());
Adam Lesinski75421622017-01-06 15:20:04 -0800117 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 EXPECT_EQ(std::string("b"), *str->value->spans[0].name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 EXPECT_EQ(17u, str->value->spans[0].first_char);
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700121 EXPECT_EQ(30u, str->value->spans[0].last_char);
122
123 EXPECT_EQ(std::string("small"), *str->value->spans[1].name);
124 EXPECT_EQ(24u, str->value->spans[1].first_char);
125 EXPECT_EQ(30u, str->value->spans[1].last_char);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700126}
127
128TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 std::string input = "<string name=\"foo\"> This is what I think </string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 ASSERT_NE(nullptr, str);
134 EXPECT_EQ(std::string("This is what I think"), *str->value);
Adam Lesinski75421622017-01-06 15:20:04 -0800135 EXPECT_TRUE(str->untranslatable_sections.empty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 input = "<string name=\"foo2\">\" This is what I think \"</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 ASSERT_NE(nullptr, str);
142 EXPECT_EQ(std::string(" This is what I think "), *str->value);
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700143}
144
Adam Lesinski75421622017-01-06 15:20:04 -0800145TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
146 std::string input = R"EOF(
147 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
148 There are <xliff:source>no</xliff:source> apples</string>)EOF";
149 ASSERT_TRUE(TestParse(input));
150
151 String* str = test::GetValue<String>(&table_, "string/foo");
152 ASSERT_NE(nullptr, str);
153 EXPECT_EQ(StringPiece("There are no apples"), StringPiece(*str->value));
154 EXPECT_TRUE(str->untranslatable_sections.empty());
155}
156
157TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
158 std::string input = R"EOF(
159 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
160 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)EOF";
161 EXPECT_FALSE(TestParse(input));
162}
163
164TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
165 std::string input = R"EOF(
166 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
167 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 ASSERT_NE(nullptr, str);
172 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value));
Adam Lesinski75421622017-01-06 15:20:04 -0800173
174 ASSERT_EQ(1u, str->untranslatable_sections.size());
175
176 // We expect indices and lengths that span to include the whitespace
177 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
178 // needed (to deal with line breaks, etc.).
179 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
180 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
181}
182
183TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
184 std::string input = R"EOF(
185 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
186 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)EOF";
187 ASSERT_TRUE(TestParse(input));
188
189 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
190 ASSERT_NE(nullptr, str);
191 EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value->str));
192
193 ASSERT_EQ(1u, str->untranslatable_sections.size());
194
195 // We expect indices and lengths that span to include the whitespace
196 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
197 // needed (to deal with line breaks, etc.).
198 EXPECT_EQ(9u, str->untranslatable_sections[0].start);
199 EXPECT_EQ(14u, str->untranslatable_sections[0].end);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200}
201
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700202TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 std::string input = "<integer name=\"foo\">@null</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
207 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800208 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 // with a data value of 0.
Adam Lesinski90919972017-06-01 15:22:57 -0700210 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
211 ASSERT_THAT(null_ref, NotNull());
212 EXPECT_FALSE(null_ref->name);
213 EXPECT_FALSE(null_ref->id);
214 EXPECT_EQ(Reference::Type::kResource, null_ref->reference_type);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700215}
216
217TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 std::string input = "<integer name=\"foo\">@empty</integer>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700220
Adam Lesinski90919972017-06-01 15:22:57 -0700221 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 ASSERT_NE(nullptr, integer);
223 EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType);
224 EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data);
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700225}
226
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 std::string input =
229 "<attr name=\"foo\" format=\"string\"/>\n"
230 "<attr name=\"bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240}
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242// Old AAPT allowed attributes to be defined under different configurations, but
243// ultimately
244// stored them with the default configuration. Check that we have the same
245// behavior.
Adam Lesinski90919972017-06-01 15:22:57 -0700246TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinski90919972017-06-01 15:22:57 -0700248 std::string input = R"(
249 <attr name="foo" />
250 <declare-styleable name="bar">
251 <attr name="baz" />
252 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800254
Adam Lesinski90919972017-06-01 15:22:57 -0700255 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config));
256 EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config));
257 EXPECT_EQ(nullptr, test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/foo"));
260 EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/baz"));
261 EXPECT_NE(nullptr, test::GetValue<Styleable>(&table_, "styleable/bar"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800262}
263
Adam Lesinskia5870652015-11-20 15:32:30 -0800264TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 std::string input =
266 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask);
272 EXPECT_EQ(10, attr->min_int);
273 EXPECT_EQ(23, attr->max_int);
Adam Lesinskia5870652015-11-20 15:32:30 -0800274}
275
276TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 std::string input =
278 "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 ASSERT_FALSE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800280}
281
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 std::string input =
284 "<declare-styleable name=\"Styleable\">\n"
285 " <attr name=\"foo\" />\n"
286 "</declare-styleable>\n"
287 "<attr name=\"foo\" format=\"string\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
295TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 std::string input =
297 "<declare-styleable name=\"Theme\">"
298 " <attr name=\"foo\" />\n"
299 "</declare-styleable>\n"
300 "<declare-styleable name=\"Window\">\n"
301 " <attr name=\"foo\" format=\"boolean\"/>\n"
302 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 ASSERT_NE(nullptr, attr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308}
309
310TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 std::string input =
312 "<attr name=\"foo\">\n"
313 " <enum name=\"bar\" value=\"0\"/>\n"
314 " <enum name=\"bat\" value=\"1\"/>\n"
315 " <enum name=\"baz\" value=\"2\"/>\n"
316 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
320 ASSERT_NE(enum_attr, nullptr);
321 EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM);
322 ASSERT_EQ(enum_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
325 EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar");
326 EXPECT_EQ(enum_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
329 EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat");
330 EXPECT_EQ(enum_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
333 EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz");
334 EXPECT_EQ(enum_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335}
336
337TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 std::string input =
339 "<attr name=\"foo\">\n"
340 " <flag name=\"bar\" value=\"0\"/>\n"
341 " <flag name=\"bat\" value=\"1\"/>\n"
342 " <flag name=\"baz\" value=\"2\"/>\n"
343 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
347 ASSERT_NE(nullptr, flag_attr);
348 EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS);
349 ASSERT_EQ(flag_attr->symbols.size(), 3u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
352 EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar");
353 EXPECT_EQ(flag_attr->symbols[0].value, 0u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
356 EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat");
357 EXPECT_EQ(flag_attr->symbols[1].value, 1u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
360 EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz");
361 EXPECT_EQ(flag_attr->symbols[2].value, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 std::unique_ptr<BinaryPrimitive> flag_value =
364 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
365 ASSERT_NE(nullptr, flag_value);
366 EXPECT_EQ(flag_value->value.data, 1u | 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367}
368
369TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 std::string input =
371 "<attr name=\"foo\">\n"
372 " <enum name=\"bar\" value=\"0\"/>\n"
373 " <enum name=\"bat\" value=\"1\"/>\n"
374 " <enum name=\"bat\" value=\"2\"/>\n"
375 "</attr>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800377}
378
379TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 std::string input =
381 "<style name=\"foo\" parent=\"@style/fu\">\n"
382 " <item name=\"bar\">#ffffffff</item>\n"
383 " <item name=\"bat\">@string/hey</item>\n"
384 " <item name=\"baz\"><b>hey</b></item>\n"
385 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800387
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 ASSERT_NE(nullptr, style);
390 AAPT_ASSERT_TRUE(style->parent);
391 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 EXPECT_EQ(test::ParseNameOrDie("style/fu"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 style->parent.value().name.value());
394 ASSERT_EQ(3u, style->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 AAPT_ASSERT_TRUE(style->entries[0].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 style->entries[0].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 AAPT_ASSERT_TRUE(style->entries[1].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 style->entries[1].key.name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 AAPT_ASSERT_TRUE(style->entries[2].key.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 EXPECT_EQ(test::ParseNameOrDie("attr/baz"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 style->entries[2].key.name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407}
408
Adam Lesinski769de982015-04-10 19:43:55 -0700409TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 ASSERT_TRUE(TestParse(input));
Adam Lesinski769de982015-04-10 19:43:55 -0700412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 ASSERT_NE(nullptr, style);
415 AAPT_ASSERT_TRUE(style->parent);
416 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 EXPECT_EQ(test::ParseNameOrDie("com.app:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 style->parent.value().name.value());
Adam Lesinski769de982015-04-10 19:43:55 -0700419}
420
Adam Lesinski24aad162015-04-24 19:19:30 -0700421TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 std::string input =
423 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
424 " name=\"foo\" parent=\"app:Theme\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 ASSERT_NE(nullptr, style);
429 AAPT_ASSERT_TRUE(style->parent);
430 AAPT_ASSERT_TRUE(style->parent.value().name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 EXPECT_EQ(test::ParseNameOrDie("android:style/Theme"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 style->parent.value().name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700433}
434
435TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 std::string input =
437 "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" "
438 "name=\"foo\">\n"
439 " <item name=\"app:bar\">0</item>\n"
440 "</style>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 ASSERT_NE(nullptr, style);
445 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 EXPECT_EQ(test::ParseNameOrDie("android:attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 style->entries[0].key.name.value());
Adam Lesinski24aad162015-04-24 19:19:30 -0700448}
449
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700450TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 std::string input = "<style name=\"foo.bar\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 ASSERT_NE(nullptr, style);
456 AAPT_ASSERT_TRUE(style->parent);
457 AAPT_ASSERT_TRUE(style->parent.value().name);
458 EXPECT_EQ(style->parent.value().name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 test::ParseNameOrDie("style/foo"));
460 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700461}
462
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463TEST_F(ResourceParserTest,
464 ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
465 std::string input = "<style name=\"foo.bar\" parent=\"\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 ASSERT_TRUE(TestParse(input));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 ASSERT_NE(nullptr, style);
470 AAPT_EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700472}
473
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800474TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 std::string input =
476 R"EOF(<style name="foo" parent="*android:style/bar" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 ASSERT_TRUE(TestParse(input));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 ASSERT_NE(nullptr, style);
481 AAPT_ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800483}
484
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 std::string input = "<string name=\"foo\">@+id/bar</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 Id* id = test::GetValue<Id>(&table_, "id/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 ASSERT_NE(id, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491}
492
493TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 std::string input =
495 "<declare-styleable name=\"foo\">\n"
496 " <attr name=\"bar\" />\n"
497 " <attr name=\"bat\" format=\"string|reference\"/>\n"
498 " <attr name=\"baz\">\n"
499 " <enum name=\"foo\" value=\"1\"/>\n"
500 " </attr>\n"
501 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 AAPT_ASSERT_TRUE(result);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state);
Adam Lesinski9f222042015-11-04 13:51:45 -0800508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800516
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 ASSERT_NE(attr, nullptr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 EXPECT_EQ(1u, attr->symbols.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 EXPECT_NE(nullptr, test::GetValue<Id>(&table_, "id/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 ASSERT_NE(styleable, nullptr);
526 ASSERT_EQ(3u, styleable->entries.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 EXPECT_EQ(test::ParseNameOrDie("attr/bar"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 styleable->entries[0].name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 EXPECT_EQ(test::ParseNameOrDie("attr/bat"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 styleable->entries[1].name.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532}
533
Adam Lesinski467f1712015-11-16 17:35:44 -0800534TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 std::string input =
536 "<declare-styleable name=\"foo\" "
537 "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n"
538 " <attr name=\"*android:bar\" />\n"
539 " <attr name=\"privAndroid:bat\" />\n"
540 "</declare-styleable>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 ASSERT_TRUE(TestParse(input));
542 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 ASSERT_NE(nullptr, styleable);
544 ASSERT_EQ(2u, styleable->entries.size());
Adam Lesinski467f1712015-11-16 17:35:44 -0800545
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 AAPT_ASSERT_TRUE(styleable->entries[0].name);
548 EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800549
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 AAPT_ASSERT_TRUE(styleable->entries[1].name);
552 EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package);
Adam Lesinski467f1712015-11-16 17:35:44 -0800553}
554
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800555TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 std::string input =
557 "<array name=\"foo\">\n"
558 " <item>@string/ref</item>\n"
559 " <item>hey</item>\n"
560 " <item>23</item>\n"
561 "</array>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 ASSERT_NE(array, nullptr);
566 ASSERT_EQ(3u, array->items.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 EXPECT_NE(nullptr, ValueCast<Reference>(array->items[0].get()));
569 EXPECT_NE(nullptr, ValueCast<String>(array->items[1].get()));
570 EXPECT_NE(nullptr, ValueCast<BinaryPrimitive>(array->items[2].get()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571}
572
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700573TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700574 std::string input = R"EOF(
575 <string-array name="foo">
576 <item>"Werk"</item>"
577 </string-array>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 ASSERT_TRUE(TestParse(input));
579 EXPECT_NE(nullptr, test::GetValue<Array>(&table_, "array/foo"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700580}
581
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700582TEST_F(ResourceParserTest, ParseArrayWithFormat) {
583 std::string input = R"EOF(
584 <array name="foo" format="string">
585 <item>100</item>
586 </array>)EOF";
587 ASSERT_TRUE(TestParse(input));
588
589 Array* array = test::GetValue<Array>(&table_, "array/foo");
590 ASSERT_NE(nullptr, array);
591
592 ASSERT_EQ(1u, array->items.size());
593
594 String* str = ValueCast<String>(array->items[0].get());
595 ASSERT_NE(nullptr, str);
596 EXPECT_EQ(std::string("100"), *str->value);
597}
598
599TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
600 std::string input = R"EOF(
601 <array name="foo" format="integer">
602 <item>Hi</item>
603 </array>)EOF";
604 ASSERT_FALSE(TestParse(input));
605}
606
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800607TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 std::string input =
609 "<plurals name=\"foo\">\n"
610 " <item quantity=\"other\">apples</item>\n"
611 " <item quantity=\"one\">apple</item>\n"
612 "</plurals>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800614
615 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
616 ASSERT_NE(nullptr, plural);
617 EXPECT_EQ(nullptr, plural->values[Plural::Zero]);
618 EXPECT_EQ(nullptr, plural->values[Plural::Two]);
619 EXPECT_EQ(nullptr, plural->values[Plural::Few]);
620 EXPECT_EQ(nullptr, plural->values[Plural::Many]);
621
622 EXPECT_NE(nullptr, plural->values[Plural::One]);
623 EXPECT_NE(nullptr, plural->values[Plural::Other]);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800624}
625
626TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 std::string input =
628 "<!--This is a comment-->\n"
629 "<string name=\"foo\">Hi</string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800631
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 EXPECT_EQ(value->GetComment(), "This is a comment");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700635}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700636
Adam Lesinskie78fd612015-10-22 12:48:43 -0700637TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 std::string input =
639 "<!--One-->\n"
640 "<!--Two-->\n"
641 "<string name=\"foo\">Hi</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700642
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700644
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 EXPECT_EQ(value->GetComment(), "Two");
Adam Lesinskie78fd612015-10-22 12:48:43 -0700648}
649
650TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 std::string input =
652 "<!--One-->\n"
653 "<string name=\"foo\">\n"
654 " Hi\n"
655 "<!--Two-->\n"
656 "</string>";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700657
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 ASSERT_NE(nullptr, value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 EXPECT_EQ(value->GetComment(), "One");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800663}
664
Adam Lesinskica5638f2015-10-21 14:42:43 -0700665TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 // We only care about declare-styleable and enum/flag attributes because
667 // comments
668 // from those end up in R.java
669 std::string input = R"EOF(
Adam Lesinskica5638f2015-10-21 14:42:43 -0700670 <declare-styleable name="foo">
671 <!-- The name of the bar -->
672 <attr name="barName" format="string|reference" />
673 </declare-styleable>
674
675 <attr name="foo">
676 <!-- The very first -->
677 <enum name="one" value="1" />
678 </attr>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 ASSERT_NE(nullptr, styleable);
683 ASSERT_EQ(1u, styleable->entries.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700684
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 EXPECT_EQ(StringPiece("The name of the bar"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 styleable->entries.front().GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 ASSERT_NE(nullptr, attr);
690 ASSERT_EQ(1u, attr->symbols.size());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700691
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 EXPECT_EQ(StringPiece("The very first"),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 attr->symbols.front().symbol.GetComment());
Adam Lesinskica5638f2015-10-21 14:42:43 -0700694}
695
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800696/*
697 * Declaring an ID as public should not require a separate definition
698 * (as an ID has no value).
699 */
700TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 std::string input = "<public type=\"id\" name=\"foo\"/>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800703
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 Id* id = test::GetValue<Id>(&table_, "id/foo");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 ASSERT_NE(nullptr, id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800706}
707
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800708TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 std::string input = R"EOF(
Adam Lesinski7751afc2016-01-06 15:45:28 -0800710 <string name="foo" product="phone">hi</string>
711 <string name="foo" product="no-sdcard">ho</string>
712 <string name="bar" product="">wee</string>
713 <string name="baz">woo</string>
714 <string name="bit" product="phablet">hoot</string>
715 <string name="bot" product="default">yes</string>
716 )EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
720 &table_, "string/foo",
721 ConfigDescription::DefaultConfig(), "phone"));
722 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
723 &table_, "string/foo",
724 ConfigDescription::DefaultConfig(), "no-sdcard"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 test::GetValueForConfigAndProduct<String>(
727 &table_, "string/bar", ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 test::GetValueForConfigAndProduct<String>(
730 &table_, "string/baz", ConfigDescription::DefaultConfig(), ""));
731 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
732 &table_, "string/bit",
733 ConfigDescription::DefaultConfig(), "phablet"));
734 EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>(
735 &table_, "string/bot",
736 ConfigDescription::DefaultConfig(), "default"));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700737}
738
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800739TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 std::string input = R"EOF(
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800741 <public-group type="attr" first-id="0x01010040">
742 <public name="foo" />
743 <public name="bar" />
744 </public-group>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800746
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 AAPT_ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800750
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 AAPT_ASSERT_TRUE(result.value().package->id);
752 AAPT_ASSERT_TRUE(result.value().type->id);
753 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 ResourceId actual_id(result.value().package->id.value(),
755 result.value().type->id.value(),
756 result.value().entry->id.value());
757 EXPECT_EQ(ResourceId(0x01010040), actual_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 AAPT_ASSERT_TRUE(result);
761
762 AAPT_ASSERT_TRUE(result.value().package->id);
763 AAPT_ASSERT_TRUE(result.value().type->id);
764 AAPT_ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 actual_id = ResourceId(result.value().package->id.value(),
766 result.value().type->id.value(),
767 result.value().entry->id.value());
768 EXPECT_EQ(ResourceId(0x01010041), actual_id);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800769}
770
Adam Lesinskifa105052015-11-07 13:34:39 -0800771TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 std::string input =
773 R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 ASSERT_TRUE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800775
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700776 input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 ASSERT_FALSE(TestParse(input));
Adam Lesinskifa105052015-11-07 13:34:39 -0800778}
779
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700780TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782 ASSERT_TRUE(TestParse(input));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800783
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700785 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 AAPT_ASSERT_TRUE(result);
787 const ResourceEntry* entry = result.value().entry;
788 ASSERT_NE(nullptr, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 EXPECT_EQ(SymbolState::kUndefined, entry->symbol_status.state);
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700790 EXPECT_TRUE(entry->symbol_status.allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800791}
792
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800793TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskie597d682017-06-01 17:16:44 -0700794 std::string input = R"(<item name="foo" type="integer" format="float">0.3</item>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700795 ASSERT_TRUE(TestParse(input));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800796
Adam Lesinskie597d682017-06-01 17:16:44 -0700797 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
798 ASSERT_THAT(val, NotNull());
799 EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800800
Adam Lesinskie597d682017-06-01 17:16:44 -0700801 input = R"(<item name="bar" type="integer" format="fraction">100</item>)";
802 ASSERT_FALSE(TestParse(input));
803}
804
805// An <item> without a format specifier accepts all types of values.
806TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
807 std::string input = R"(<item name="foo" type="integer">100%p</item>)";
808 ASSERT_TRUE(TestParse(input));
809
810 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
811 ASSERT_THAT(val, NotNull());
812 EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800813}
814
Adam Lesinski86d67df2017-01-31 13:47:27 -0800815TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
816 std::string input = R"EOF(<item name="foo" type="configVarying">Hey</item>)EOF";
817 ASSERT_TRUE(TestParse(input));
818 ASSERT_NE(nullptr, test::GetValue<String>(&table_, "configVarying/foo"));
819}
820
821TEST_F(ResourceParserTest, ParseBagElement) {
822 std::string input =
823 R"EOF(<bag name="bag" type="configVarying"><item name="test">Hello!</item></bag>)EOF";
824 ASSERT_TRUE(TestParse(input));
825
826 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
827 ASSERT_NE(nullptr, val);
828
829 ASSERT_EQ(1u, val->entries.size());
830 EXPECT_EQ(Reference(test::ParseNameOrDie("attr/test")), val->entries[0].key);
831 EXPECT_NE(nullptr, ValueCast<RawString>(val->entries[0].value.get()));
832}
833
Adam Lesinski90919972017-06-01 15:22:57 -0700834TEST_F(ResourceParserTest, ParseElementWithNoValue) {
835 std::string input = R"(
836 <item type="drawable" format="reference" name="foo" />
837 <string name="foo" />)";
838 ASSERT_TRUE(TestParse(input));
839 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
840
841 String* str = test::GetValue<String>(&table_, "string/foo");
842 ASSERT_THAT(str, NotNull());
843 EXPECT_THAT(*str->value, Eq(""));
844}
845
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846} // namespace aapt