blob: c98c0b95b69bb17d8bffdb9e4696b9caf9b58070 [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::Res_value;
Adam Lesinski71be7052017-12-12 16:48:07 -080033using ::android::ResTable_map;
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 Lesinski71be7052017-12-12 16:48:07 -080041using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080042
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043namespace aapt {
44
Adam Lesinskibab4ef52017-06-01 15:22:57 -070045constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051
52 std::string input = kXmlPreamble;
53 input += R"(<attr name="foo"/>)";
54 StringInputStream in(input);
55 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059class ResourceParserTest : public ::testing::Test {
60 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070061 void SetUp() override {
62 context_ = test::ContextBuilder().Build();
63 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 ::testing::AssertionResult TestParse(const StringPiece& str) {
66 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
Adam Lesinski52364f72016-01-11 13:10:24 -080068
Adam Lesinskibab4ef52017-06-01 15:22:57 -070069 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070071 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
72 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070073
74 std::string input = kXmlPreamble;
75 input += "<resources>\n";
76 input.append(str.data(), str.size());
77 input += "\n</resources>";
78 StringInputStream in(input);
79 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 return ::testing::AssertionFailure();
84 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085
86 protected:
87 ResourceTable table_;
88 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089};
90
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070092 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </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(" hey there "));
97 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -080098
99 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
100 str = test::GetValue<String>(&table_, "string/bar");
101 ASSERT_THAT(str, NotNull());
102 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
103
104 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
105 str = test::GetValue<String>(&table_, "string/baz");
106 ASSERT_THAT(str, NotNull());
107 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108}
109
110TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700111 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700114 ASSERT_THAT(str, NotNull());
115 EXPECT_THAT(*str, StrValueEq("?123"));
116 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700117
118 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
119 str = test::GetValue<String>(&table_, "string/bar");
120 ASSERT_THAT(str, NotNull());
121 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinski9f222042015-11-04 13:51:45 -0800124TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700125 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
126 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800127}
128
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700129TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800131 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700133 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700137 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700138
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800139 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700140 EXPECT_THAT(str->value->spans, SizeIs(2));
141 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700142
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800143 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
144 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700145 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700146
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800147 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
148 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700149 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700150}
151
152TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700153 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800157 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700158 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700159
Adam Lesinskia45893a2017-05-30 15:19:02 -0700160 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 ASSERT_THAT(str, NotNull());
164 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700165}
166
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800167TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
168 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
169 ASSERT_TRUE(TestParse(input));
170
171 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
172 ASSERT_THAT(str, NotNull());
173 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
174 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
175
176 ASSERT_THAT(str->value->spans, SizeIs(2u));
177 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
178 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
179 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
180
181 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
182 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
183 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
184}
185
Adam Lesinski75421622017-01-06 15:20:04 -0800186TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700187 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800188 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700189 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800190 ASSERT_TRUE(TestParse(input));
191
192 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700193 ASSERT_THAT(str, NotNull());
194 EXPECT_THAT(*str, StrValueEq("There are no apples"));
195 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800196}
197
198TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700199 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800200 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700201 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800202 EXPECT_FALSE(TestParse(input));
203}
204
205TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700206 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800207 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700208 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700212 ASSERT_THAT(str, NotNull());
213 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800214
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800215 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
216 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700217 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800218}
219
220TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700221 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800222 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700223 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800224 ASSERT_TRUE(TestParse(input));
225
226 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700227 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800228 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800229
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800230 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
231 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
232 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
233
234 ASSERT_THAT(str->value->spans, SizeIs(1u));
235 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
236 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
237 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238}
239
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700240TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700241 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
245 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800246 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700248 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
249 ASSERT_THAT(null_ref, NotNull());
250 EXPECT_FALSE(null_ref->name);
251 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700252 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700253}
254
255TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700256 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700258
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700259 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700260 ASSERT_THAT(integer, NotNull());
261 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
262 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700263}
264
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700266 std::string input = R"(
267 <attr name="foo" format="string"/>
268 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700272 ASSERT_THAT(attr, NotNull());
273 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700276 ASSERT_THAT(attr, NotNull());
277 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278}
279
Adam Lesinskia45893a2017-05-30 15:19:02 -0700280// Old AAPT allowed attributes to be defined under different configurations, but ultimately
281// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700282TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700284 std::string input = R"(
285 <attr name="foo" />
286 <declare-styleable name="bar">
287 <attr name="baz" />
288 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800290
Adam Lesinskia45893a2017-05-30 15:19:02 -0700291 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
292 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
293 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800294
Adam Lesinskia45893a2017-05-30 15:19:02 -0700295 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
296 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
297 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800298}
299
Adam Lesinskia5870652015-11-20 15:32:30 -0800300TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700301 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700305 ASSERT_THAT(attr, NotNull());
306 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
307 EXPECT_THAT(attr->min_int, Eq(10));
308 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800309}
310
311TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700312 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800313}
314
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800315TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700316 std::string input = R"(
317 <declare-styleable name="Styleable">
318 <attr name="foo" />
319 </declare-styleable>
320 <attr name="foo" format="string"/>)";
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* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700324 ASSERT_THAT(attr, NotNull());
325 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800326}
327
328TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700329 std::string input = R"(
330 <declare-styleable name="Theme">
331 <attr name="foo" />
332 </declare-styleable>
333 <declare-styleable name="Window">
334 <attr name="foo" format="boolean"/>
335 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700339 ASSERT_THAT(attr, NotNull());
340 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341}
342
343TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700344 std::string input = R"(
345 <attr name="foo">
346 <enum name="bar" value="0"/>
347 <enum name="bat" value="1"/>
348 <enum name="baz" value="2"/>
349 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800351
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700353 ASSERT_THAT(enum_attr, NotNull());
354 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
355 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356
Adam Lesinskia45893a2017-05-30 15:19:02 -0700357 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
358 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
359 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360
Adam Lesinskia45893a2017-05-30 15:19:02 -0700361 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
362 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
363 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364
Adam Lesinskia45893a2017-05-30 15:19:02 -0700365 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
366 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
367 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368}
369
370TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700371 std::string input = R"(
372 <attr name="foo">
373 <flag name="bar" value="0"/>
374 <flag name="bat" value="1"/>
375 <flag name="baz" value="2"/>
376 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700380 ASSERT_THAT(flag_attr, NotNull());
381 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
382 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383
Adam Lesinskia45893a2017-05-30 15:19:02 -0700384 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
385 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
386 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800387
Adam Lesinskia45893a2017-05-30 15:19:02 -0700388 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
389 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
390 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391
Adam Lesinskia45893a2017-05-30 15:19:02 -0700392 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
393 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
394 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 std::unique_ptr<BinaryPrimitive> flag_value =
397 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700398 ASSERT_THAT(flag_value, NotNull());
399 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400}
401
402TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700403 std::string input = R"(
404 <attr name="foo">
405 <enum name="bar" value="0"/>
406 <enum name="bat" value="1"/>
407 <enum name="bat" value="2"/>
408 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410}
411
412TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700413 std::string input = R"(
414 <style name="foo" parent="@style/fu">
415 <item name="bar">#ffffffff</item>
416 <item name="bat">@string/hey</item>
417 <item name="baz"><b>hey</b></item>
418 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700422 ASSERT_THAT(style, NotNull());
423 ASSERT_TRUE(style->parent);
424 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
425 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800426
Adam Lesinskia45893a2017-05-30 15:19:02 -0700427 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
428 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
429 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430}
431
Adam Lesinski769de982015-04-10 19:43:55 -0700432TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700433 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700434
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700436 ASSERT_THAT(style, NotNull());
437 ASSERT_TRUE(style->parent);
438 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700439}
440
Adam Lesinski24aad162015-04-24 19:19:30 -0700441TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700442 std::string input = R"(
443 <style xmlns:app="http://schemas.android.com/apk/res/android"
444 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700448 ASSERT_THAT(style, NotNull());
449 ASSERT_TRUE(style->parent);
450 ASSERT_TRUE(style->parent.value().name);
451 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700452}
453
454TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700455 std::string input = R"(
456 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
457 <item name="app:bar">0</item>
458 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700462 ASSERT_THAT(style, NotNull());
463 ASSERT_THAT(style->entries, SizeIs(1));
464 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700465}
466
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700467TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700468 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700469
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700471 ASSERT_THAT(style, NotNull());
472 ASSERT_TRUE(style->parent);
473 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700475}
476
Adam Lesinskia45893a2017-05-30 15:19:02 -0700477TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
478 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700481 ASSERT_THAT(style, NotNull());
482 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700484}
485
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800486TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700487 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700490 ASSERT_THAT(style, NotNull());
491 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800493}
494
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700496 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
497 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498}
499
500TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700501 std::string input = R"(
502 <declare-styleable name="foo">
503 <attr name="bar" />
504 <attr name="bat" format="string|reference"/>
505 <attr name="baz">
506 <enum name="foo" value="1"/>
507 </attr>
508 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700513 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800514 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700517 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700521 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700525 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700527 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700528
Adam Lesinskia45893a2017-05-30 15:19:02 -0700529 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700532 ASSERT_THAT(styleable, NotNull());
533 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800534
Adam Lesinskia45893a2017-05-30 15:19:02 -0700535 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
536 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
537 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800538}
539
Adam Lesinski467f1712015-11-16 17:35:44 -0800540TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700541 std::string input = R"(
542 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
543 name="foo">
544 <attr name="*android:bar" />
545 <attr name="privAndroid:bat" />
546 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 ASSERT_TRUE(TestParse(input));
548 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700549 ASSERT_THAT(styleable, NotNull());
550 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700553 ASSERT_TRUE(styleable->entries[0].name);
554 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700557 ASSERT_TRUE(styleable->entries[1].name);
558 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800559}
560
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800561TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700562 std::string input = R"(
563 <array name="foo">
564 <item>@string/ref</item>
565 <item>hey</item>
566 <item>23</item>
567 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800569
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700571 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700572 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800573
Adam Lesinski4ffea042017-08-10 15:37:28 -0700574 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
575 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
576 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800577}
578
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700579TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700580 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700581 <string-array name="foo">
582 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700583 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700585 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700586}
587
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700588TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700589 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700590 <array name="foo" format="string">
591 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700592 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700593 ASSERT_TRUE(TestParse(input));
594
595 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700596 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700597 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700598
Adam Lesinski4ffea042017-08-10 15:37:28 -0700599 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700600 ASSERT_THAT(str, NotNull());
601 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700602}
603
604TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700605 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700606 <array name="foo" format="integer">
607 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700608 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700609 ASSERT_FALSE(TestParse(input));
610}
611
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800612TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700613 std::string input = R"(
614 <plurals name="foo">
615 <item quantity="other">apples</item>
616 <item quantity="one">apple</item>
617 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800619
620 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700621 ASSERT_THAT(plural, NotNull());
622 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
623 EXPECT_THAT(plural->values[Plural::Two], IsNull());
624 EXPECT_THAT(plural->values[Plural::Few], IsNull());
625 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800626
Adam Lesinskia45893a2017-05-30 15:19:02 -0700627 EXPECT_THAT(plural->values[Plural::One], NotNull());
628 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800629}
630
631TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700632 std::string input = R"(
633 <!--This is a comment-->
634 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700638 ASSERT_THAT(value, NotNull());
639 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700640}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700641
Adam Lesinskie78fd612015-10-22 12:48:43 -0700642TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700643 std::string input = R"(
644 <!--One-->
645 <!--Two-->
646 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700647
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700649
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700651 ASSERT_THAT(value, NotNull());
652 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700653}
654
655TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700656 std::string input = R"(
657 <!--One-->
658 <string name="foo">
659 Hi
660 <!--Two-->
661 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700663
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700665 ASSERT_THAT(value, NotNull());
666 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800667}
668
Adam Lesinskica5638f2015-10-21 14:42:43 -0700669TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700671 // comments from those end up in R.java
672 std::string input = R"(
673 <declare-styleable name="foo">
674 <!-- The name of the bar -->
675 <attr name="barName" format="string|reference" />
676 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700677
Adam Lesinskia45893a2017-05-30 15:19:02 -0700678 <attr name="foo">
679 <!-- The very first -->
680 <enum name="one" value="1" />
681 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700683
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700685 ASSERT_THAT(styleable, NotNull());
686 ASSERT_THAT(styleable->entries, SizeIs(1));
687 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700688
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700690 ASSERT_THAT(attr, NotNull());
691 ASSERT_THAT(attr->symbols, SizeIs(1));
692 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700693}
694
Adam Lesinskia45893a2017-05-30 15:19:02 -0700695// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800696TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700697 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
698 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800699}
700
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800701TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700702 std::string input = R"(
703 <string name="foo" product="phone">hi</string>
704 <string name="foo" product="no-sdcard">ho</string>
705 <string name="bar" product="">wee</string>
706 <string name="baz">woo</string>
707 <string name="bit" product="phablet">hoot</string>
708 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700710
Adam Lesinskia45893a2017-05-30 15:19:02 -0700711 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
712 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
713 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
714 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
715 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
716 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700717}
718
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800719TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700720 std::string input = R"(
721 <public-group type="attr" first-id="0x01010040">
722 <public name="foo" />
723 <public name="bar" />
724 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800726
Adam Lesinskia45893a2017-05-30 15:19:02 -0700727 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
728 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800729
Adam Lesinskia45893a2017-05-30 15:19:02 -0700730 ASSERT_TRUE(result.value().package->id);
731 ASSERT_TRUE(result.value().type->id);
732 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 ResourceId actual_id(result.value().package->id.value(),
734 result.value().type->id.value(),
735 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700736 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700738 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700739 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740
Adam Lesinskia45893a2017-05-30 15:19:02 -0700741 ASSERT_TRUE(result.value().package->id);
742 ASSERT_TRUE(result.value().type->id);
743 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 actual_id = ResourceId(result.value().package->id.value(),
745 result.value().type->id.value(),
746 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700747 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800748}
749
Adam Lesinski71be7052017-12-12 16:48:07 -0800750TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
751 std::string input = R"(
752 <!-- private -->
753 <java-symbol type="string" name="foo" />
754 <!-- public -->
755 <public type="string" name="foo" id="0x01020000" />
756 <!-- private2 -->
757 <java-symbol type="string" name="foo" />)";
758 ASSERT_TRUE(TestParse(input));
759
760 Maybe<ResourceTable::SearchResult> result =
761 table_.FindResource(test::ParseNameOrDie("string/foo"));
762 ASSERT_TRUE(result);
763
764 ResourceEntry* entry = result.value().entry;
765 ASSERT_THAT(entry, NotNull());
766 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
767 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
768}
769
Adam Lesinskifa105052015-11-07 13:34:39 -0800770TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700771 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
772 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800773}
774
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700775TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700776 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800777
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700780 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700782 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800783 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
784 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800785}
786
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800787TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700788 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800789
Adam Lesinskie597d682017-06-01 17:16:44 -0700790 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
791 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700792 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800793
Adam Lesinskia45893a2017-05-30 15:19:02 -0700794 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700795}
796
797// An <item> without a format specifier accepts all types of values.
798TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700799 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700800
801 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
802 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700803 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800804}
805
Adam Lesinski86d67df2017-01-31 13:47:27 -0800806TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700807 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
808 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800809}
810
811TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700812 std::string input = R"(
813 <bag name="bag" type="configVarying">
814 <item name="test">Hello!</item>
815 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800816 ASSERT_TRUE(TestParse(input));
817
818 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700819 ASSERT_THAT(val, NotNull());
820 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800821
Adam Lesinskia45893a2017-05-30 15:19:02 -0700822 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
823 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800824}
825
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700826TEST_F(ResourceParserTest, ParseElementWithNoValue) {
827 std::string input = R"(
828 <item type="drawable" format="reference" name="foo" />
829 <string name="foo" />)";
830 ASSERT_TRUE(TestParse(input));
831 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
832
833 String* str = test::GetValue<String>(&table_, "string/foo");
834 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700835 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700836}
837
Adam Lesinskib9f05482017-06-02 16:32:37 -0700838TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700839 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700840}
841
Adam Lesinski46c4d722017-08-23 13:03:56 -0700842TEST_F(ResourceParserTest, ParseOverlayableTagWithSystemPolicy) {
843 std::string input = R"(
844 <overlayable policy="illegal_policy">
845 <item type="string" name="foo" />
846 </overlayable>)";
847 EXPECT_FALSE(TestParse(input));
848
849 input = R"(
850 <overlayable policy="system">
851 <item name="foo" />
852 </overlayable>)";
853 EXPECT_FALSE(TestParse(input));
854
855 input = R"(
856 <overlayable policy="system">
857 <item type="attr" />
858 </overlayable>)";
859 EXPECT_FALSE(TestParse(input));
860
861 input = R"(
862 <overlayable policy="system">
863 <item type="bad_type" name="foo" />
864 </overlayable>)";
865 EXPECT_FALSE(TestParse(input));
866
867 input = R"(<overlayable policy="system" />)";
868 EXPECT_TRUE(TestParse(input));
869
870 input = R"(<overlayable />)";
871 EXPECT_TRUE(TestParse(input));
872
873 input = R"(
874 <overlayable policy="system">
875 <item type="string" name="foo" />
876 <item type="dimen" name="foo" />
877 </overlayable>)";
878 ASSERT_TRUE(TestParse(input));
879
880 input = R"(
881 <overlayable>
882 <item type="string" name="bar" />
883 </overlayable>)";
884 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -0800885
886 Maybe<ResourceTable::SearchResult> search_result =
887 table_.FindResource(test::ParseNameOrDie("string/bar"));
888 ASSERT_TRUE(search_result);
889 ASSERT_THAT(search_result.value().entry, NotNull());
890 EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kUndefined));
891 EXPECT_TRUE(search_result.value().entry->overlayable);
892}
893
894TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
895 std::string input = R"(
896 <overlayable>
897 <item type="string" name="foo" />
898 <item type="string" name="foo" />
899 </overlayable>)";
900 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -0700901}
902
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903} // namespace aapt