blob: 9a5cd3edb47fe62f649b1752bc83ac86220c3638 [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 Lesinski00451162017-10-03 07:44:08 -070025#include "io/StringStream.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070026#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080027#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029using ::aapt::io::StringInputStream;
Adam Lesinskia45893a2017-05-30 15:19:02 -070030using ::aapt::test::StrValueEq;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070031using ::aapt::test::ValueEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070032using ::android::ResTable_map;
33using ::android::Res_value;
Adam Lesinskie597d682017-06-01 17:16:44 -070034using ::android::StringPiece;
35using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070036using ::testing::IsEmpty;
37using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070038using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070039using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070040using ::testing::SizeIs;
Adam Lesinskid5083f62017-01-16 15:07:21 -080041
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042namespace aapt {
43
Adam Lesinskibab4ef52017-06-01 15:22:57 -070044constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
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"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070050
51 std::string input = kXmlPreamble;
52 input += R"(<attr name="foo"/>)";
53 StringInputStream in(input);
54 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070056}
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058class ResourceParserTest : public ::testing::Test {
59 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070060 void SetUp() override {
61 context_ = test::ContextBuilder().Build();
62 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 ::testing::AssertionResult TestParse(const StringPiece& str) {
65 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinski52364f72016-01-11 13:10:24 -080067
Adam Lesinskibab4ef52017-06-01 15:22:57 -070068 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070070 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
71 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070072
73 std::string input = kXmlPreamble;
74 input += "<resources>\n";
75 input.append(str.data(), str.size());
76 input += "\n</resources>";
77 StringInputStream in(input);
78 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 return ::testing::AssertionFailure();
83 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084
85 protected:
86 ResourceTable table_;
87 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088};
89
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070091 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070094 ASSERT_THAT(str, NotNull());
95 EXPECT_THAT(*str, StrValueEq(" hey there "));
96 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097}
98
99TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700100 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700103 ASSERT_THAT(str, NotNull());
104 EXPECT_THAT(*str, StrValueEq("?123"));
105 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700106
107 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
108 str = test::GetValue<String>(&table_, "string/bar");
109 ASSERT_THAT(str, NotNull());
110 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111}
112
Adam Lesinski9f222042015-11-04 13:51:45 -0800113TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700114 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
115 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800116}
117
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700118TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800120 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700122 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700126 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700127
Adam Lesinski060b53d2017-07-28 17:10:35 -0700128 EXPECT_THAT(str->value->value, Eq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700129 EXPECT_THAT(str->value->spans, SizeIs(2));
130 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700131
Adam Lesinskia45893a2017-05-30 15:19:02 -0700132 EXPECT_THAT(*str->value->spans[0].name, Eq("b"));
133 EXPECT_THAT(str->value->spans[0].first_char, Eq(17u));
134 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700135
Adam Lesinskia45893a2017-05-30 15:19:02 -0700136 EXPECT_THAT(*str->value->spans[1].name, Eq("small"));
137 EXPECT_THAT(str->value->spans[1].first_char, Eq(24u));
138 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700139}
140
141TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700142 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700145 ASSERT_THAT(str, NotNull());
146 EXPECT_THAT(*str->value, Eq("This is what I think"));
147 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700148
Adam Lesinskia45893a2017-05-30 15:19:02 -0700149 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700152 ASSERT_THAT(str, NotNull());
153 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700154}
155
Adam Lesinski75421622017-01-06 15:20:04 -0800156TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700157 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800158 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700159 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800160 ASSERT_TRUE(TestParse(input));
161
162 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 ASSERT_THAT(str, NotNull());
164 EXPECT_THAT(*str, StrValueEq("There are no apples"));
165 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800166}
167
168TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700169 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800170 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700171 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800172 EXPECT_FALSE(TestParse(input));
173}
174
175TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700176 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800177 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700178 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700182 ASSERT_THAT(str, NotNull());
183 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
184 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800185
186 // We expect indices and lengths that span to include the whitespace
187 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
188 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700189 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
190 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800191}
192
193TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700194 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800195 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700196 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800197 ASSERT_TRUE(TestParse(input));
198
199 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700200 ASSERT_THAT(str, NotNull());
Adam Lesinski060b53d2017-07-28 17:10:35 -0700201 EXPECT_THAT(str->value->value, Eq("There are %1$d apples"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700202 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800203
204 // We expect indices and lengths that span to include the whitespace
205 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
206 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700207 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
208 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209}
210
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700211TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700212 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
216 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800217 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700219 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
220 ASSERT_THAT(null_ref, NotNull());
221 EXPECT_FALSE(null_ref->name);
222 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700223 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700224}
225
226TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700227 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700229
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700230 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700231 ASSERT_THAT(integer, NotNull());
232 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
233 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700234}
235
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700237 std::string input = R"(
238 <attr name="foo" format="string"/>
239 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700243 ASSERT_THAT(attr, NotNull());
244 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700247 ASSERT_THAT(attr, NotNull());
248 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249}
250
Adam Lesinskia45893a2017-05-30 15:19:02 -0700251// Old AAPT allowed attributes to be defined under different configurations, but ultimately
252// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700253TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700255 std::string input = R"(
256 <attr name="foo" />
257 <declare-styleable name="bar">
258 <attr name="baz" />
259 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800261
Adam Lesinskia45893a2017-05-30 15:19:02 -0700262 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
263 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
264 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800265
Adam Lesinskia45893a2017-05-30 15:19:02 -0700266 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
267 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
268 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800269}
270
Adam Lesinskia5870652015-11-20 15:32:30 -0800271TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700272 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700276 ASSERT_THAT(attr, NotNull());
277 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
278 EXPECT_THAT(attr->min_int, Eq(10));
279 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800280}
281
282TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700283 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800284}
285
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700287 std::string input = R"(
288 <declare-styleable name="Styleable">
289 <attr name="foo" />
290 </declare-styleable>
291 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700295 ASSERT_THAT(attr, NotNull());
296 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297}
298
299TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700300 std::string input = R"(
301 <declare-styleable name="Theme">
302 <attr name="foo" />
303 </declare-styleable>
304 <declare-styleable name="Window">
305 <attr name="foo" format="boolean"/>
306 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700310 ASSERT_THAT(attr, NotNull());
311 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312}
313
314TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700315 std::string input = R"(
316 <attr name="foo">
317 <enum name="bar" value="0"/>
318 <enum name="bat" value="1"/>
319 <enum name="baz" value="2"/>
320 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700324 ASSERT_THAT(enum_attr, NotNull());
325 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
326 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskia45893a2017-05-30 15:19:02 -0700328 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
329 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
330 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331
Adam Lesinskia45893a2017-05-30 15:19:02 -0700332 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
333 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
334 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335
Adam Lesinskia45893a2017-05-30 15:19:02 -0700336 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
337 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
338 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800339}
340
341TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700342 std::string input = R"(
343 <attr name="foo">
344 <flag name="bar" value="0"/>
345 <flag name="bat" value="1"/>
346 <flag name="baz" value="2"/>
347 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700351 ASSERT_THAT(flag_attr, NotNull());
352 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
353 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354
Adam Lesinskia45893a2017-05-30 15:19:02 -0700355 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
356 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
357 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358
Adam Lesinskia45893a2017-05-30 15:19:02 -0700359 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
360 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
361 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800362
Adam Lesinskia45893a2017-05-30 15:19:02 -0700363 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
364 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
365 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800366
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 std::unique_ptr<BinaryPrimitive> flag_value =
368 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700369 ASSERT_THAT(flag_value, NotNull());
370 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800371}
372
373TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700374 std::string input = R"(
375 <attr name="foo">
376 <enum name="bar" value="0"/>
377 <enum name="bat" value="1"/>
378 <enum name="bat" value="2"/>
379 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381}
382
383TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700384 std::string input = R"(
385 <style name="foo" parent="@style/fu">
386 <item name="bar">#ffffffff</item>
387 <item name="bat">@string/hey</item>
388 <item name="baz"><b>hey</b></item>
389 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700393 ASSERT_THAT(style, NotNull());
394 ASSERT_TRUE(style->parent);
395 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
396 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397
Adam Lesinskia45893a2017-05-30 15:19:02 -0700398 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
399 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
400 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401}
402
Adam Lesinski769de982015-04-10 19:43:55 -0700403TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700404 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700407 ASSERT_THAT(style, NotNull());
408 ASSERT_TRUE(style->parent);
409 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700410}
411
Adam Lesinski24aad162015-04-24 19:19:30 -0700412TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700413 std::string input = R"(
414 <style xmlns:app="http://schemas.android.com/apk/res/android"
415 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700419 ASSERT_THAT(style, NotNull());
420 ASSERT_TRUE(style->parent);
421 ASSERT_TRUE(style->parent.value().name);
422 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700423}
424
425TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700426 std::string input = R"(
427 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
428 <item name="app:bar">0</item>
429 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700433 ASSERT_THAT(style, NotNull());
434 ASSERT_THAT(style->entries, SizeIs(1));
435 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700436}
437
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700438TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700439 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700442 ASSERT_THAT(style, NotNull());
443 ASSERT_TRUE(style->parent);
444 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700446}
447
Adam Lesinskia45893a2017-05-30 15:19:02 -0700448TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
449 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700452 ASSERT_THAT(style, NotNull());
453 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700455}
456
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800457TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700458 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700461 ASSERT_THAT(style, NotNull());
462 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800464}
465
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800466TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700467 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
468 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469}
470
471TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700472 std::string input = R"(
473 <declare-styleable name="foo">
474 <attr name="bar" />
475 <attr name="bat" format="string|reference"/>
476 <attr name="baz">
477 <enum name="foo" value="1"/>
478 </attr>
479 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700484 ASSERT_TRUE(result);
485 EXPECT_THAT(result.value().entry->symbol_status.state, Eq(SymbolState::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700492 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700496 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700498 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700499
Adam Lesinskia45893a2017-05-30 15:19:02 -0700500 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700503 ASSERT_THAT(styleable, NotNull());
504 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505
Adam Lesinskia45893a2017-05-30 15:19:02 -0700506 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
507 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
508 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509}
510
Adam Lesinski467f1712015-11-16 17:35:44 -0800511TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700512 std::string input = R"(
513 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
514 name="foo">
515 <attr name="*android:bar" />
516 <attr name="privAndroid:bat" />
517 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 ASSERT_TRUE(TestParse(input));
519 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700520 ASSERT_THAT(styleable, NotNull());
521 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700524 ASSERT_TRUE(styleable->entries[0].name);
525 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800526
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700528 ASSERT_TRUE(styleable->entries[1].name);
529 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800530}
531
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700533 std::string input = R"(
534 <array name="foo">
535 <item>@string/ref</item>
536 <item>hey</item>
537 <item>23</item>
538 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700542 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700543 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800544
Adam Lesinski4ffea042017-08-10 15:37:28 -0700545 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
546 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
547 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800548}
549
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700550TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700551 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700552 <string-array name="foo">
553 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700554 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700556 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700557}
558
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700559TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700560 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700561 <array name="foo" format="string">
562 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700563 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700564 ASSERT_TRUE(TestParse(input));
565
566 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700567 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700568 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700569
Adam Lesinski4ffea042017-08-10 15:37:28 -0700570 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700571 ASSERT_THAT(str, NotNull());
572 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700573}
574
575TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700576 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700577 <array name="foo" format="integer">
578 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700579 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700580 ASSERT_FALSE(TestParse(input));
581}
582
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800583TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700584 std::string input = R"(
585 <plurals name="foo">
586 <item quantity="other">apples</item>
587 <item quantity="one">apple</item>
588 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800590
591 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700592 ASSERT_THAT(plural, NotNull());
593 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
594 EXPECT_THAT(plural->values[Plural::Two], IsNull());
595 EXPECT_THAT(plural->values[Plural::Few], IsNull());
596 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800597
Adam Lesinskia45893a2017-05-30 15:19:02 -0700598 EXPECT_THAT(plural->values[Plural::One], NotNull());
599 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800600}
601
602TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700603 std::string input = R"(
604 <!--This is a comment-->
605 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700609 ASSERT_THAT(value, NotNull());
610 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700611}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700612
Adam Lesinskie78fd612015-10-22 12:48:43 -0700613TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700614 std::string input = R"(
615 <!--One-->
616 <!--Two-->
617 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700620
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700622 ASSERT_THAT(value, NotNull());
623 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700624}
625
626TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700627 std::string input = R"(
628 <!--One-->
629 <string name="foo">
630 Hi
631 <!--Two-->
632 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700634
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700636 ASSERT_THAT(value, NotNull());
637 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800638}
639
Adam Lesinskica5638f2015-10-21 14:42:43 -0700640TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700642 // comments from those end up in R.java
643 std::string input = R"(
644 <declare-styleable name="foo">
645 <!-- The name of the bar -->
646 <attr name="barName" format="string|reference" />
647 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700648
Adam Lesinskia45893a2017-05-30 15:19:02 -0700649 <attr name="foo">
650 <!-- The very first -->
651 <enum name="one" value="1" />
652 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700656 ASSERT_THAT(styleable, NotNull());
657 ASSERT_THAT(styleable->entries, SizeIs(1));
658 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700661 ASSERT_THAT(attr, NotNull());
662 ASSERT_THAT(attr->symbols, SizeIs(1));
663 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700664}
665
Adam Lesinskia45893a2017-05-30 15:19:02 -0700666// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800667TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700668 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
669 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800670}
671
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800672TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700673 std::string input = R"(
674 <string name="foo" product="phone">hi</string>
675 <string name="foo" product="no-sdcard">ho</string>
676 <string name="bar" product="">wee</string>
677 <string name="baz">woo</string>
678 <string name="bit" product="phablet">hoot</string>
679 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700681
Adam Lesinskia45893a2017-05-30 15:19:02 -0700682 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
683 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
684 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
685 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
686 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
687 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700688}
689
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800690TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700691 std::string input = R"(
692 <public-group type="attr" first-id="0x01010040">
693 <public name="foo" />
694 <public name="bar" />
695 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800697
Adam Lesinskia45893a2017-05-30 15:19:02 -0700698 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
699 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800700
Adam Lesinskia45893a2017-05-30 15:19:02 -0700701 ASSERT_TRUE(result.value().package->id);
702 ASSERT_TRUE(result.value().type->id);
703 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 ResourceId actual_id(result.value().package->id.value(),
705 result.value().type->id.value(),
706 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700707 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700710 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711
Adam Lesinskia45893a2017-05-30 15:19:02 -0700712 ASSERT_TRUE(result.value().package->id);
713 ASSERT_TRUE(result.value().type->id);
714 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 actual_id = ResourceId(result.value().package->id.value(),
716 result.value().type->id.value(),
717 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700718 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800719}
720
Adam Lesinskifa105052015-11-07 13:34:39 -0800721TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700722 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
723 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800724}
725
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700726TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700727 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800728
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700731 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700733 ASSERT_THAT(entry, NotNull());
734 EXPECT_THAT(entry->symbol_status.state, Eq(SymbolState::kUndefined));
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700735 EXPECT_TRUE(entry->symbol_status.allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800736}
737
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800738TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700739 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800740
Adam Lesinskie597d682017-06-01 17:16:44 -0700741 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
742 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700743 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800744
Adam Lesinskia45893a2017-05-30 15:19:02 -0700745 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700746}
747
748// An <item> without a format specifier accepts all types of values.
749TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700750 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700751
752 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
753 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700754 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800755}
756
Adam Lesinski86d67df2017-01-31 13:47:27 -0800757TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700758 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
759 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800760}
761
762TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700763 std::string input = R"(
764 <bag name="bag" type="configVarying">
765 <item name="test">Hello!</item>
766 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800767 ASSERT_TRUE(TestParse(input));
768
769 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700770 ASSERT_THAT(val, NotNull());
771 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800772
Adam Lesinskia45893a2017-05-30 15:19:02 -0700773 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
774 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800775}
776
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700777TEST_F(ResourceParserTest, ParseElementWithNoValue) {
778 std::string input = R"(
779 <item type="drawable" format="reference" name="foo" />
780 <string name="foo" />)";
781 ASSERT_TRUE(TestParse(input));
782 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
783
784 String* str = test::GetValue<String>(&table_, "string/foo");
785 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700786 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700787}
788
Adam Lesinskib9f05482017-06-02 16:32:37 -0700789TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700790 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700791}
792
Adam Lesinski46c4d722017-08-23 13:03:56 -0700793TEST_F(ResourceParserTest, ParseOverlayableTagWithSystemPolicy) {
794 std::string input = R"(
795 <overlayable policy="illegal_policy">
796 <item type="string" name="foo" />
797 </overlayable>)";
798 EXPECT_FALSE(TestParse(input));
799
800 input = R"(
801 <overlayable policy="system">
802 <item name="foo" />
803 </overlayable>)";
804 EXPECT_FALSE(TestParse(input));
805
806 input = R"(
807 <overlayable policy="system">
808 <item type="attr" />
809 </overlayable>)";
810 EXPECT_FALSE(TestParse(input));
811
812 input = R"(
813 <overlayable policy="system">
814 <item type="bad_type" name="foo" />
815 </overlayable>)";
816 EXPECT_FALSE(TestParse(input));
817
818 input = R"(<overlayable policy="system" />)";
819 EXPECT_TRUE(TestParse(input));
820
821 input = R"(<overlayable />)";
822 EXPECT_TRUE(TestParse(input));
823
824 input = R"(
825 <overlayable policy="system">
826 <item type="string" name="foo" />
827 <item type="dimen" name="foo" />
828 </overlayable>)";
829 ASSERT_TRUE(TestParse(input));
830
831 input = R"(
832 <overlayable>
833 <item type="string" name="bar" />
834 </overlayable>)";
835 ASSERT_TRUE(TestParse(input));
836}
837
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838} // namespace aapt