blob: 1683c64a6a5c02fb2ff3a9cf2bcba1ab45ca1231 [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 Lesinskia45893a2017-05-30 15:19:02 -070028using ::aapt::test::StrValueEq;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070029using ::aapt::test::ValueEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070030using ::android::ResTable_map;
31using ::android::Res_value;
Adam Lesinskie597d682017-06-01 17:16:44 -070032using ::android::StringPiece;
33using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070034using ::testing::IsEmpty;
35using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070036using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070037using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070038using ::testing::SizeIs;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskibab4ef52017-06-01 15:22:57 -070042constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 std::stringstream input(kXmlPreamble);
Adam Lesinskibab4ef52017-06-01 15:22:57 -070047 input << R"(<attr name="foo"/>)" << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
50 xml::XmlPullParser xml_parser(input);
51 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070052}
53
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054class ResourceParserTest : public ::testing::Test {
55 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070056 void SetUp() override {
57 context_ = test::ContextBuilder().Build();
58 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 ::testing::AssertionResult TestParse(const StringPiece& str) {
61 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Adam Lesinski52364f72016-01-11 13:10:24 -080063
Adam Lesinskibab4ef52017-06-01 15:22:57 -070064 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 std::stringstream input(kXmlPreamble);
66 input << "<resources>\n" << str << "\n</resources>" << std::endl;
67 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070068 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
69 parserOptions);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 xml::XmlPullParser xmlParser(input);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 return ::testing::AssertionFailure();
75 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076
77 protected:
78 ResourceTable table_;
79 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080};
81
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070083 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070086 ASSERT_THAT(str, NotNull());
87 EXPECT_THAT(*str, StrValueEq(" hey there "));
88 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089}
90
91TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070092 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070095 ASSERT_THAT(str, NotNull());
96 EXPECT_THAT(*str, StrValueEq("?123"));
97 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -070098
99 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
100 str = test::GetValue<String>(&table_, "string/bar");
101 ASSERT_THAT(str, NotNull());
102 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103}
104
Adam Lesinski9f222042015-11-04 13:51:45 -0800105TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700106 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
107 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800108}
109
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700110TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800112 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700114 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700118 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700119
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700120 EXPECT_THAT(str->value->value, Eq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700121 EXPECT_THAT(str->value->spans, SizeIs(2));
122 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700123
Adam Lesinskia45893a2017-05-30 15:19:02 -0700124 EXPECT_THAT(*str->value->spans[0].name, Eq("b"));
125 EXPECT_THAT(str->value->spans[0].first_char, Eq(17u));
126 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700127
Adam Lesinskia45893a2017-05-30 15:19:02 -0700128 EXPECT_THAT(*str->value->spans[1].name, Eq("small"));
129 EXPECT_THAT(str->value->spans[1].first_char, Eq(24u));
130 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700131}
132
133TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700134 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700137 ASSERT_THAT(str, NotNull());
138 EXPECT_THAT(*str->value, Eq("This is what I think"));
139 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700140
Adam Lesinskia45893a2017-05-30 15:19:02 -0700141 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700144 ASSERT_THAT(str, NotNull());
145 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700146}
147
Adam Lesinski75421622017-01-06 15:20:04 -0800148TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700149 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800150 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700151 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800152 ASSERT_TRUE(TestParse(input));
153
154 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700155 ASSERT_THAT(str, NotNull());
156 EXPECT_THAT(*str, StrValueEq("There are no apples"));
157 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800158}
159
160TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800162 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800164 EXPECT_FALSE(TestParse(input));
165}
166
167TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700168 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800169 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700170 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700174 ASSERT_THAT(str, NotNull());
175 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
176 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800177
178 // We expect indices and lengths that span to include the whitespace
179 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
180 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700181 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
182 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800183}
184
185TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700186 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800187 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700188 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800189 ASSERT_TRUE(TestParse(input));
190
191 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700192 ASSERT_THAT(str, NotNull());
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700193 EXPECT_THAT(str->value->value, Eq("There are %1$d apples"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700194 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800195
196 // We expect indices and lengths that span to include the whitespace
197 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
198 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700199 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
200 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201}
202
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700203TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700204 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700206
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
208 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800209 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700211 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
212 ASSERT_THAT(null_ref, NotNull());
213 EXPECT_FALSE(null_ref->name);
214 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700215 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700216}
217
218TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700219 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700221
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700222 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700223 ASSERT_THAT(integer, NotNull());
224 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
225 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700226}
227
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700229 std::string input = R"(
230 <attr name="foo" format="string"/>
231 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700235 ASSERT_THAT(attr, NotNull());
236 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700239 ASSERT_THAT(attr, NotNull());
240 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241}
242
Adam Lesinskia45893a2017-05-30 15:19:02 -0700243// Old AAPT allowed attributes to be defined under different configurations, but ultimately
244// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700245TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700247 std::string input = R"(
248 <attr name="foo" />
249 <declare-styleable name="bar">
250 <attr name="baz" />
251 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800253
Adam Lesinskia45893a2017-05-30 15:19:02 -0700254 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
255 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
256 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800257
Adam Lesinskia45893a2017-05-30 15:19:02 -0700258 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
259 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
260 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800261}
262
Adam Lesinskia5870652015-11-20 15:32:30 -0800263TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700264 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700268 ASSERT_THAT(attr, NotNull());
269 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
270 EXPECT_THAT(attr->min_int, Eq(10));
271 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800272}
273
274TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700275 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800276}
277
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700279 std::string input = R"(
280 <declare-styleable name="Styleable">
281 <attr name="foo" />
282 </declare-styleable>
283 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700287 ASSERT_THAT(attr, NotNull());
288 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289}
290
291TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700292 std::string input = R"(
293 <declare-styleable name="Theme">
294 <attr name="foo" />
295 </declare-styleable>
296 <declare-styleable name="Window">
297 <attr name="foo" format="boolean"/>
298 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700302 ASSERT_THAT(attr, NotNull());
303 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304}
305
306TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700307 std::string input = R"(
308 <attr name="foo">
309 <enum name="bar" value="0"/>
310 <enum name="bat" value="1"/>
311 <enum name="baz" value="2"/>
312 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700316 ASSERT_THAT(enum_attr, NotNull());
317 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
318 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319
Adam Lesinskia45893a2017-05-30 15:19:02 -0700320 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
321 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
322 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323
Adam Lesinskia45893a2017-05-30 15:19:02 -0700324 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
325 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
326 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskia45893a2017-05-30 15:19:02 -0700328 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
329 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
330 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331}
332
333TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700334 std::string input = R"(
335 <attr name="foo">
336 <flag name="bar" value="0"/>
337 <flag name="bat" value="1"/>
338 <flag name="baz" value="2"/>
339 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700343 ASSERT_THAT(flag_attr, NotNull());
344 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
345 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800346
Adam Lesinskia45893a2017-05-30 15:19:02 -0700347 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
348 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
349 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350
Adam Lesinskia45893a2017-05-30 15:19:02 -0700351 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
352 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
353 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskia45893a2017-05-30 15:19:02 -0700355 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
356 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
357 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 std::unique_ptr<BinaryPrimitive> flag_value =
360 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700361 ASSERT_THAT(flag_value, NotNull());
362 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363}
364
365TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700366 std::string input = R"(
367 <attr name="foo">
368 <enum name="bar" value="0"/>
369 <enum name="bat" value="1"/>
370 <enum name="bat" value="2"/>
371 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800373}
374
375TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700376 std::string input = R"(
377 <style name="foo" parent="@style/fu">
378 <item name="bar">#ffffffff</item>
379 <item name="bat">@string/hey</item>
380 <item name="baz"><b>hey</b></item>
381 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700385 ASSERT_THAT(style, NotNull());
386 ASSERT_TRUE(style->parent);
387 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
388 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800389
Adam Lesinskia45893a2017-05-30 15:19:02 -0700390 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
391 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
392 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800393}
394
Adam Lesinski769de982015-04-10 19:43:55 -0700395TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700396 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700397
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700399 ASSERT_THAT(style, NotNull());
400 ASSERT_TRUE(style->parent);
401 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700402}
403
Adam Lesinski24aad162015-04-24 19:19:30 -0700404TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700405 std::string input = R"(
406 <style xmlns:app="http://schemas.android.com/apk/res/android"
407 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700411 ASSERT_THAT(style, NotNull());
412 ASSERT_TRUE(style->parent);
413 ASSERT_TRUE(style->parent.value().name);
414 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700415}
416
417TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700418 std::string input = R"(
419 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
420 <item name="app:bar">0</item>
421 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700425 ASSERT_THAT(style, NotNull());
426 ASSERT_THAT(style->entries, SizeIs(1));
427 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700428}
429
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700430TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700431 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700434 ASSERT_THAT(style, NotNull());
435 ASSERT_TRUE(style->parent);
436 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700438}
439
Adam Lesinskia45893a2017-05-30 15:19:02 -0700440TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
441 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700444 ASSERT_THAT(style, NotNull());
445 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700447}
448
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800449TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700450 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800451
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700453 ASSERT_THAT(style, NotNull());
454 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800456}
457
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700459 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
460 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800461}
462
463TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700464 std::string input = R"(
465 <declare-styleable name="foo">
466 <attr name="bar" />
467 <attr name="bat" format="string|reference"/>
468 <attr name="baz">
469 <enum name="foo" value="1"/>
470 </attr>
471 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700476 ASSERT_TRUE(result);
477 EXPECT_THAT(result.value().entry->symbol_status.state, Eq(SymbolState::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700480 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700484 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700488 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700490 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700491
Adam Lesinskia45893a2017-05-30 15:19:02 -0700492 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700495 ASSERT_THAT(styleable, NotNull());
496 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497
Adam Lesinskia45893a2017-05-30 15:19:02 -0700498 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
499 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
500 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501}
502
Adam Lesinski467f1712015-11-16 17:35:44 -0800503TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700504 std::string input = R"(
505 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
506 name="foo">
507 <attr name="*android:bar" />
508 <attr name="privAndroid:bat" />
509 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 ASSERT_TRUE(TestParse(input));
511 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700512 ASSERT_THAT(styleable, NotNull());
513 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700516 ASSERT_TRUE(styleable->entries[0].name);
517 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700520 ASSERT_TRUE(styleable->entries[1].name);
521 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800522}
523
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700525 std::string input = R"(
526 <array name="foo">
527 <item>@string/ref</item>
528 <item>hey</item>
529 <item>23</item>
530 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700534 ASSERT_THAT(array, NotNull());
535 ASSERT_THAT(array->items, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800536
Adam Lesinskia45893a2017-05-30 15:19:02 -0700537 EXPECT_THAT(ValueCast<Reference>(array->items[0].get()), NotNull());
538 EXPECT_THAT(ValueCast<String>(array->items[1].get()), NotNull());
539 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->items[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800540}
541
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700542TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700543 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700544 <string-array name="foo">
545 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700546 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700548 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700549}
550
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700551TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700552 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700553 <array name="foo" format="string">
554 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700555 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700556 ASSERT_TRUE(TestParse(input));
557
558 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700559 ASSERT_THAT(array, NotNull());
560 ASSERT_THAT(array->items, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700561
562 String* str = ValueCast<String>(array->items[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700563 ASSERT_THAT(str, NotNull());
564 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700565}
566
567TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700568 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700569 <array name="foo" format="integer">
570 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700571 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700572 ASSERT_FALSE(TestParse(input));
573}
574
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700576 std::string input = R"(
577 <plurals name="foo">
578 <item quantity="other">apples</item>
579 <item quantity="one">apple</item>
580 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800582
583 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700584 ASSERT_THAT(plural, NotNull());
585 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
586 EXPECT_THAT(plural->values[Plural::Two], IsNull());
587 EXPECT_THAT(plural->values[Plural::Few], IsNull());
588 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800589
Adam Lesinskia45893a2017-05-30 15:19:02 -0700590 EXPECT_THAT(plural->values[Plural::One], NotNull());
591 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800592}
593
594TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700595 std::string input = R"(
596 <!--This is a comment-->
597 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700601 ASSERT_THAT(value, NotNull());
602 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700603}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700604
Adam Lesinskie78fd612015-10-22 12:48:43 -0700605TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700606 std::string input = R"(
607 <!--One-->
608 <!--Two-->
609 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700612
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700614 ASSERT_THAT(value, NotNull());
615 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700616}
617
618TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700619 std::string input = R"(
620 <!--One-->
621 <string name="foo">
622 Hi
623 <!--Two-->
624 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700628 ASSERT_THAT(value, NotNull());
629 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800630}
631
Adam Lesinskica5638f2015-10-21 14:42:43 -0700632TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700634 // comments from those end up in R.java
635 std::string input = R"(
636 <declare-styleable name="foo">
637 <!-- The name of the bar -->
638 <attr name="barName" format="string|reference" />
639 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700640
Adam Lesinskia45893a2017-05-30 15:19:02 -0700641 <attr name="foo">
642 <!-- The very first -->
643 <enum name="one" value="1" />
644 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700648 ASSERT_THAT(styleable, NotNull());
649 ASSERT_THAT(styleable->entries, SizeIs(1));
650 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700653 ASSERT_THAT(attr, NotNull());
654 ASSERT_THAT(attr->symbols, SizeIs(1));
655 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700656}
657
Adam Lesinskia45893a2017-05-30 15:19:02 -0700658// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700660 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
661 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800662}
663
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800664TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700665 std::string input = R"(
666 <string name="foo" product="phone">hi</string>
667 <string name="foo" product="no-sdcard">ho</string>
668 <string name="bar" product="">wee</string>
669 <string name="baz">woo</string>
670 <string name="bit" product="phablet">hoot</string>
671 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700673
Adam Lesinskia45893a2017-05-30 15:19:02 -0700674 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
675 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
676 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
677 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
678 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
679 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700680}
681
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800682TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700683 std::string input = R"(
684 <public-group type="attr" first-id="0x01010040">
685 <public name="foo" />
686 <public name="bar" />
687 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800689
Adam Lesinskia45893a2017-05-30 15:19:02 -0700690 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
691 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800692
Adam Lesinskia45893a2017-05-30 15:19:02 -0700693 ASSERT_TRUE(result.value().package->id);
694 ASSERT_TRUE(result.value().type->id);
695 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 ResourceId actual_id(result.value().package->id.value(),
697 result.value().type->id.value(),
698 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700699 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700702 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703
Adam Lesinskia45893a2017-05-30 15:19:02 -0700704 ASSERT_TRUE(result.value().package->id);
705 ASSERT_TRUE(result.value().type->id);
706 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 actual_id = ResourceId(result.value().package->id.value(),
708 result.value().type->id.value(),
709 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700710 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800711}
712
Adam Lesinskifa105052015-11-07 13:34:39 -0800713TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700714 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
715 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800716}
717
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700718TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700719 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800720
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700723 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700725 ASSERT_THAT(entry, NotNull());
726 EXPECT_THAT(entry->symbol_status.state, Eq(SymbolState::kUndefined));
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700727 EXPECT_TRUE(entry->symbol_status.allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800728}
729
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800730TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700731 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800732
Adam Lesinskie597d682017-06-01 17:16:44 -0700733 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
734 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700735 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800736
Adam Lesinskia45893a2017-05-30 15:19:02 -0700737 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700738}
739
740// An <item> without a format specifier accepts all types of values.
741TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700742 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700743
744 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
745 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700746 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800747}
748
Adam Lesinski86d67df2017-01-31 13:47:27 -0800749TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700750 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
751 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800752}
753
754TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700755 std::string input = R"(
756 <bag name="bag" type="configVarying">
757 <item name="test">Hello!</item>
758 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800759 ASSERT_TRUE(TestParse(input));
760
761 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700762 ASSERT_THAT(val, NotNull());
763 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800764
Adam Lesinskia45893a2017-05-30 15:19:02 -0700765 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
766 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800767}
768
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700769TEST_F(ResourceParserTest, ParseElementWithNoValue) {
770 std::string input = R"(
771 <item type="drawable" format="reference" name="foo" />
772 <string name="foo" />)";
773 ASSERT_TRUE(TestParse(input));
774 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
775
776 String* str = test::GetValue<String>(&table_, "string/foo");
777 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700778 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700779}
780
Adam Lesinskib9f05482017-06-02 16:32:37 -0700781TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700782 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700783}
784
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785} // namespace aapt