blob: 53361414e9b87d51f8f94f4bf24c9c594eb33340 [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;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
Adam Lesinskia45893a2017-05-30 15:19:02 -070033using ::android::Res_value;
Adam Lesinski71be7052017-12-12 16:48:07 -080034using ::android::ResTable_map;
Adam Lesinskie597d682017-06-01 17:16:44 -070035using ::android::StringPiece;
36using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070037using ::testing::IsEmpty;
38using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070039using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070040using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070041using ::testing::SizeIs;
Adam Lesinski71be7052017-12-12 16:48:07 -080042using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080043
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
45
Adam Lesinskibab4ef52017-06-01 15:22:57 -070046constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070052
53 std::string input = kXmlPreamble;
54 input += R"(<attr name="foo"/>)";
55 StringInputStream in(input);
56 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060class ResourceParserTest : public ::testing::Test {
61 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070062 void SetUp() override {
63 context_ = test::ContextBuilder().Build();
64 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 ::testing::AssertionResult TestParse(const StringPiece& str) {
67 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
Adam Lesinski52364f72016-01-11 13:10:24 -080069
Adam Lesinskibab4ef52017-06-01 15:22:57 -070070 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070072 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
73 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070074
75 std::string input = kXmlPreamble;
76 input += "<resources>\n";
77 input.append(str.data(), str.size());
78 input += "\n</resources>";
79 StringInputStream in(input);
80 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return ::testing::AssertionFailure();
85 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086
87 protected:
88 ResourceTable table_;
89 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090};
91
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070093 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070096 ASSERT_THAT(str, NotNull());
97 EXPECT_THAT(*str, StrValueEq(" hey there "));
98 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -080099
100 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
101 str = test::GetValue<String>(&table_, "string/bar");
102 ASSERT_THAT(str, NotNull());
103 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
104
105 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
106 str = test::GetValue<String>(&table_, "string/baz");
107 ASSERT_THAT(str, NotNull());
108 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109}
110
111TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700112 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700115 ASSERT_THAT(str, NotNull());
116 EXPECT_THAT(*str, StrValueEq("?123"));
117 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700118
119 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
120 str = test::GetValue<String>(&table_, "string/bar");
121 ASSERT_THAT(str, NotNull());
122 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123}
124
Adam Lesinski9f222042015-11-04 13:51:45 -0800125TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700126 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
127 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800128}
129
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700130TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800132 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700134 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700138 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700139
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800140 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700141 EXPECT_THAT(str->value->spans, SizeIs(2));
142 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700143
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800144 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
145 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700146 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700147
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800148 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
149 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700150 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700151}
152
153TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700154 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700157 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800158 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700159 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700160
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700164 ASSERT_THAT(str, NotNull());
165 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700166}
167
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700168TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
169 // Tuncate leading and trailing whitespace
170 EXPECT_TRUE(TestParse(R"(<string name="foo">&#32;Hello&#32;</string>)"));
171
172 String* str = test::GetValue<String>(&table_, "string/foo");
173 ASSERT_THAT(str, NotNull());
174 EXPECT_THAT(*str->value, StrEq("Hello"));
175 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
176
177 // AAPT does not truncate unicode whitespace
178 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
179
180 str = test::GetValue<String>(&table_, "string/foo2");
181 ASSERT_THAT(str, NotNull());
182 EXPECT_THAT(*str->value, StrEq(" Hello "));
183 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
184
185 // Preserve non-ASCII whitespace including extended ASCII characters
Ryan Mitchell0f326752019-03-11 11:00:25 -0700186 EXPECT_TRUE(TestParse(R"(<string name="foo3">&#160;Hello&#x202F;World&#160;</string>)"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700187
188 str = test::GetValue<String>(&table_, "string/foo3");
189 ASSERT_THAT(str, NotNull());
Ryan Mitchell0f326752019-03-11 11:00:25 -0700190 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700191 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
192
193 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
194
195 str = test::GetValue<String>(&table_, "string/foo4");
196 ASSERT_THAT(str, NotNull());
197 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
198 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
199}
200
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800201TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
202 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
203 ASSERT_TRUE(TestParse(input));
204
205 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
206 ASSERT_THAT(str, NotNull());
207 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
208 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
209
210 ASSERT_THAT(str->value->spans, SizeIs(2u));
211 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
212 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
213 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
214
215 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
216 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
217 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
218}
219
Adam Lesinski75421622017-01-06 15:20:04 -0800220TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
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 <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800224 ASSERT_TRUE(TestParse(input));
225
226 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700227 ASSERT_THAT(str, NotNull());
228 EXPECT_THAT(*str, StrValueEq("There are no apples"));
229 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800230}
231
232TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700233 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800234 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700235 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800236 EXPECT_FALSE(TestParse(input));
237}
238
239TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700240 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800241 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700242 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700246 ASSERT_THAT(str, NotNull());
247 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800248
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800249 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
250 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700251 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800252}
253
254TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700255 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800256 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700257 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800258 ASSERT_TRUE(TestParse(input));
259
260 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700261 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800262 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800263
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800264 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
265 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
266 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
267
268 ASSERT_THAT(str->value->spans, SizeIs(1u));
269 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
270 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
271 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272}
273
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700274TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700275 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700277
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
279 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800280 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700282 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
283 ASSERT_THAT(null_ref, NotNull());
284 EXPECT_FALSE(null_ref->name);
285 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700286 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700287}
288
289TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700290 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700292
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700293 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700294 ASSERT_THAT(integer, NotNull());
295 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
296 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700297}
298
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700300 std::string input = R"(
301 <attr name="foo" format="string"/>
302 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700306 ASSERT_THAT(attr, NotNull());
307 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700310 ASSERT_THAT(attr, NotNull());
311 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312}
313
Adam Lesinskia45893a2017-05-30 15:19:02 -0700314// Old AAPT allowed attributes to be defined under different configurations, but ultimately
315// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700316TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700318 std::string input = R"(
319 <attr name="foo" />
320 <declare-styleable name="bar">
321 <attr name="baz" />
322 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800324
Adam Lesinskia45893a2017-05-30 15:19:02 -0700325 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
326 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
327 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800328
Adam Lesinskia45893a2017-05-30 15:19:02 -0700329 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
330 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
331 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800332}
333
Adam Lesinskia5870652015-11-20 15:32:30 -0800334TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700335 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -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_INTEGER));
341 EXPECT_THAT(attr->min_int, Eq(10));
342 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800343}
344
345TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700346 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800347}
348
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800349TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700350 std::string input = R"(
351 <declare-styleable name="Styleable">
352 <attr name="foo" />
353 </declare-styleable>
354 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700358 ASSERT_THAT(attr, NotNull());
359 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360}
361
362TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700363 std::string input = R"(
364 <declare-styleable name="Theme">
365 <attr name="foo" />
366 </declare-styleable>
367 <declare-styleable name="Window">
368 <attr name="foo" format="boolean"/>
369 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700373 ASSERT_THAT(attr, NotNull());
374 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800375}
376
377TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700378 std::string input = R"(
379 <attr name="foo">
380 <enum name="bar" value="0"/>
381 <enum name="bat" value="1"/>
382 <enum name="baz" value="2"/>
383 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700387 ASSERT_THAT(enum_attr, NotNull());
388 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
389 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800390
Adam Lesinskia45893a2017-05-30 15:19:02 -0700391 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
392 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
393 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394
Adam Lesinskia45893a2017-05-30 15:19:02 -0700395 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
396 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
397 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800398
Adam Lesinskia45893a2017-05-30 15:19:02 -0700399 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
400 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
401 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402}
403
404TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700405 std::string input = R"(
406 <attr name="foo">
407 <flag name="bar" value="0"/>
408 <flag name="bat" value="1"/>
409 <flag name="baz" value="2"/>
410 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700414 ASSERT_THAT(flag_attr, NotNull());
415 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
416 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800417
Adam Lesinskia45893a2017-05-30 15:19:02 -0700418 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
419 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
420 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421
Adam Lesinskia45893a2017-05-30 15:19:02 -0700422 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
423 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
424 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425
Adam Lesinskia45893a2017-05-30 15:19:02 -0700426 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
427 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
428 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 std::unique_ptr<BinaryPrimitive> flag_value =
431 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700432 ASSERT_THAT(flag_value, NotNull());
433 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434}
435
436TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700437 std::string input = R"(
438 <attr name="foo">
439 <enum name="bar" value="0"/>
440 <enum name="bat" value="1"/>
441 <enum name="bat" value="2"/>
442 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444}
445
446TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700447 std::string input = R"(
448 <style name="foo" parent="@style/fu">
449 <item name="bar">#ffffffff</item>
450 <item name="bat">@string/hey</item>
451 <item name="baz"><b>hey</b></item>
452 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700456 ASSERT_THAT(style, NotNull());
457 ASSERT_TRUE(style->parent);
458 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
459 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800460
Adam Lesinskia45893a2017-05-30 15:19:02 -0700461 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
462 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
463 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464}
465
Adam Lesinski769de982015-04-10 19:43:55 -0700466TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700467 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700470 ASSERT_THAT(style, NotNull());
471 ASSERT_TRUE(style->parent);
472 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700473}
474
Adam Lesinski24aad162015-04-24 19:19:30 -0700475TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700476 std::string input = R"(
477 <style xmlns:app="http://schemas.android.com/apk/res/android"
478 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700482 ASSERT_THAT(style, NotNull());
483 ASSERT_TRUE(style->parent);
484 ASSERT_TRUE(style->parent.value().name);
485 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700486}
487
488TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700489 std::string input = R"(
490 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
491 <item name="app:bar">0</item>
492 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700496 ASSERT_THAT(style, NotNull());
497 ASSERT_THAT(style->entries, SizeIs(1));
498 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700499}
500
Ryan Mitchell633d7962018-06-11 15:29:21 -0700501TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) {
502 std::string input = R"(
503 <style name="foo">
504 <item name="bar">
505 com.helloworld.AppClass
506 </item>
507 </style>)";
508 ASSERT_TRUE(TestParse(input));
509
510 Style* style = test::GetValue<Style>(&table_, "style/foo");
511 ASSERT_THAT(style, NotNull());
512 EXPECT_THAT(style->entries[0].value, NotNull());
513 RawString* value = ValueCast<RawString>(style->entries[0].value.get());
514 EXPECT_THAT(value, NotNull());
515 EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)"));
516}
517
518
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700519TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700520 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700523 ASSERT_THAT(style, NotNull());
524 ASSERT_TRUE(style->parent);
525 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700527}
528
Adam Lesinskia45893a2017-05-30 15:19:02 -0700529TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
530 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700533 ASSERT_THAT(style, NotNull());
534 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700536}
537
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800538TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700539 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700542 ASSERT_THAT(style, NotNull());
543 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800545}
546
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800547TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700548 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
549 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800550}
551
552TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700553 std::string input = R"(
554 <declare-styleable name="foo">
555 <attr name="bar" />
556 <attr name="bat" format="string|reference"/>
557 <attr name="baz">
558 <enum name="foo" value="1"/>
559 </attr>
560 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800562
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700565 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800566 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700569 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700573 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700577 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700579 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700580
Adam Lesinskia45893a2017-05-30 15:19:02 -0700581 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700584 ASSERT_THAT(styleable, NotNull());
585 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800586
Adam Lesinskia45893a2017-05-30 15:19:02 -0700587 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
588 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
589 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800590}
591
Adam Lesinski467f1712015-11-16 17:35:44 -0800592TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700593 std::string input = R"(
594 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
595 name="foo">
596 <attr name="*android:bar" />
597 <attr name="privAndroid:bat" />
598 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 ASSERT_TRUE(TestParse(input));
600 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700601 ASSERT_THAT(styleable, NotNull());
602 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800603
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700605 ASSERT_TRUE(styleable->entries[0].name);
606 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700609 ASSERT_TRUE(styleable->entries[1].name);
610 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800611}
612
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800613TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700614 std::string input = R"(
615 <array name="foo">
616 <item>@string/ref</item>
617 <item>hey</item>
618 <item>23</item>
619 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700623 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700624 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800625
Adam Lesinski4ffea042017-08-10 15:37:28 -0700626 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
627 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
628 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800629}
630
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700631TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700632 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700633 <string-array name="foo">
634 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700635 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700637 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700638}
639
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700640TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700641 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700642 <array name="foo" format="string">
643 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700644 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700645 ASSERT_TRUE(TestParse(input));
646
647 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700648 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700649 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700650
Adam Lesinski4ffea042017-08-10 15:37:28 -0700651 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700652 ASSERT_THAT(str, NotNull());
653 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700654}
655
656TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700657 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700658 <array name="foo" format="integer">
659 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700660 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700661 ASSERT_FALSE(TestParse(input));
662}
663
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800664TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700665 std::string input = R"(
666 <plurals name="foo">
667 <item quantity="other">apples</item>
668 <item quantity="one">apple</item>
669 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800671
672 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700673 ASSERT_THAT(plural, NotNull());
674 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
675 EXPECT_THAT(plural->values[Plural::Two], IsNull());
676 EXPECT_THAT(plural->values[Plural::Few], IsNull());
677 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800678
Adam Lesinskia45893a2017-05-30 15:19:02 -0700679 EXPECT_THAT(plural->values[Plural::One], NotNull());
680 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800681}
682
683TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700684 std::string input = R"(
685 <!--This is a comment-->
686 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800688
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700690 ASSERT_THAT(value, NotNull());
691 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700692}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700693
Adam Lesinskie78fd612015-10-22 12:48:43 -0700694TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700695 std::string input = R"(
696 <!--One-->
697 <!--Two-->
698 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700699
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700701
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700703 ASSERT_THAT(value, NotNull());
704 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700705}
706
707TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700708 std::string input = R"(
709 <!--One-->
710 <string name="foo">
711 Hi
712 <!--Two-->
713 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700717 ASSERT_THAT(value, NotNull());
718 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800719}
720
Adam Lesinskica5638f2015-10-21 14:42:43 -0700721TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700723 // comments from those end up in R.java
724 std::string input = R"(
725 <declare-styleable name="foo">
726 <!-- The name of the bar -->
727 <attr name="barName" format="string|reference" />
728 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700729
Adam Lesinskia45893a2017-05-30 15:19:02 -0700730 <attr name="foo">
731 <!-- The very first -->
732 <enum name="one" value="1" />
733 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700735
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700737 ASSERT_THAT(styleable, NotNull());
738 ASSERT_THAT(styleable->entries, SizeIs(1));
739 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700740
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700742 ASSERT_THAT(attr, NotNull());
743 ASSERT_THAT(attr->symbols, SizeIs(1));
744 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700745}
746
Adam Lesinskia45893a2017-05-30 15:19:02 -0700747// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800748TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700749 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
750 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800751}
752
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800753TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700754 std::string input = R"(
755 <string name="foo" product="phone">hi</string>
756 <string name="foo" product="no-sdcard">ho</string>
757 <string name="bar" product="">wee</string>
758 <string name="baz">woo</string>
759 <string name="bit" product="phablet">hoot</string>
760 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700762
Adam Lesinskia45893a2017-05-30 15:19:02 -0700763 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
764 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
765 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
766 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
767 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
768 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700769}
770
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800771TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700772 std::string input = R"(
773 <public-group type="attr" first-id="0x01010040">
774 <public name="foo" />
775 <public name="bar" />
776 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800778
Adam Lesinskia45893a2017-05-30 15:19:02 -0700779 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
780 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800781
Adam Lesinskia45893a2017-05-30 15:19:02 -0700782 ASSERT_TRUE(result.value().package->id);
783 ASSERT_TRUE(result.value().type->id);
784 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700785 ResourceId actual_id(result.value().package->id.value(),
786 result.value().type->id.value(),
787 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700788 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700791 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792
Adam Lesinskia45893a2017-05-30 15:19:02 -0700793 ASSERT_TRUE(result.value().package->id);
794 ASSERT_TRUE(result.value().type->id);
795 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700796 actual_id = ResourceId(result.value().package->id.value(),
797 result.value().type->id.value(),
798 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700799 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800800}
801
Adam Lesinski71be7052017-12-12 16:48:07 -0800802TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
803 std::string input = R"(
804 <!-- private -->
805 <java-symbol type="string" name="foo" />
806 <!-- public -->
807 <public type="string" name="foo" id="0x01020000" />
808 <!-- private2 -->
809 <java-symbol type="string" name="foo" />)";
810 ASSERT_TRUE(TestParse(input));
811
812 Maybe<ResourceTable::SearchResult> result =
813 table_.FindResource(test::ParseNameOrDie("string/foo"));
814 ASSERT_TRUE(result);
815
816 ResourceEntry* entry = result.value().entry;
817 ASSERT_THAT(entry, NotNull());
818 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
819 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
820}
821
Adam Lesinskifa105052015-11-07 13:34:39 -0800822TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700823 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
824 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800825}
826
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700827TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700828 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800829
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700830 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700832 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700834 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800835 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
836 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800837}
838
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800839TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700840 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800841
Adam Lesinskie597d682017-06-01 17:16:44 -0700842 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
843 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700844 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800845
Adam Lesinskia45893a2017-05-30 15:19:02 -0700846 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700847}
848
849// An <item> without a format specifier accepts all types of values.
850TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700851 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700852
853 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
854 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700855 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800856}
857
Adam Lesinski86d67df2017-01-31 13:47:27 -0800858TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700859 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
860 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800861}
862
863TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700864 std::string input = R"(
865 <bag name="bag" type="configVarying">
866 <item name="test">Hello!</item>
867 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800868 ASSERT_TRUE(TestParse(input));
869
870 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700871 ASSERT_THAT(val, NotNull());
872 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800873
Adam Lesinskia45893a2017-05-30 15:19:02 -0700874 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
875 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800876}
877
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700878TEST_F(ResourceParserTest, ParseElementWithNoValue) {
879 std::string input = R"(
880 <item type="drawable" format="reference" name="foo" />
881 <string name="foo" />)";
882 ASSERT_TRUE(TestParse(input));
883 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
884
885 String* str = test::GetValue<String>(&table_, "string/foo");
886 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700887 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700888}
889
Adam Lesinskib9f05482017-06-02 16:32:37 -0700890TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700891 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700892}
893
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700894TEST_F(ResourceParserTest, ParseOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800895 std::string input = R"(
896 <overlayable name="Name" actor="overlay://theme">
Winsonb2d7f532019-02-04 16:32:43 -0800897 <policy type="signature">
898 <item type="string" name="foo" />
899 <item type="drawable" name="bar" />
900 </policy>
Adam Lesinski46c4d722017-08-23 13:03:56 -0700901 </overlayable>)";
902 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -0800903
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700904 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
Adam Lesinski71be7052017-12-12 16:48:07 -0800905 ASSERT_TRUE(search_result);
906 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800907 ASSERT_TRUE(search_result.value().entry->overlayable_item);
908 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
909 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
910 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winsonb2d7f532019-02-04 16:32:43 -0800911 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700912
913 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
914 ASSERT_TRUE(search_result);
915 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800916 ASSERT_TRUE(search_result.value().entry->overlayable_item);
917 result_overlayable_item = search_result.value().entry->overlayable_item.value();
918 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
919 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winsonb2d7f532019-02-04 16:32:43 -0800920 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800921}
922
923TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
924 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
925 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
926 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
927}
928
929TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
930 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700931}
932
933TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800934 std::string input = R"(
935 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700936 <policy type="product">
937 <item type="string" name="bar" />
938 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700939 <policy type="system">
940 <item type="string" name="fiz" />
941 </policy>
942 <policy type="vendor">
943 <item type="string" name="fuz" />
944 </policy>
945 <policy type="public">
946 <item type="string" name="faz" />
947 </policy>
Winsonb2d7f532019-02-04 16:32:43 -0800948 <policy type="signature">
949 <item type="string" name="foz" />
950 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700951 </overlayable>)";
952 ASSERT_TRUE(TestParse(input));
953
Winsonb2d7f532019-02-04 16:32:43 -0800954 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700955 ASSERT_TRUE(search_result);
956 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800957 ASSERT_TRUE(search_result.value().entry->overlayable_item);
958 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
959 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800960 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700961
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700962 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
963 ASSERT_TRUE(search_result);
964 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800965 ASSERT_TRUE(search_result.value().entry->overlayable_item);
966 result_overlayable_item = search_result.value().entry->overlayable_item.value();
967 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
968 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSystem));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700969
970 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
971 ASSERT_TRUE(search_result);
972 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800973 ASSERT_TRUE(search_result.value().entry->overlayable_item);
974 result_overlayable_item = search_result.value().entry->overlayable_item.value();
975 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
976 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kVendor));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700977
978 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
979 ASSERT_TRUE(search_result);
980 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800981 ASSERT_TRUE(search_result.value().entry->overlayable_item);
982 result_overlayable_item = search_result.value().entry->overlayable_item.value();
983 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
984 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kPublic));
Winsonb2d7f532019-02-04 16:32:43 -0800985
986 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
987 ASSERT_TRUE(search_result);
988 ASSERT_THAT(search_result.value().entry, NotNull());
989 ASSERT_TRUE(search_result.value().entry->overlayable_item);
990 result_overlayable_item = search_result.value().entry->overlayable_item.value();
991 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
992 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature));
993}
994
995TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
996 std::string input = R"(
997 <overlayable name="Name">
998 <item type="string" name="foo" />
999 </overlayable>)";
1000 EXPECT_FALSE(TestParse(input));
1001
1002 input = R"(
1003 <overlayable name="Name">
1004 <policy>
1005 <item name="foo" />
1006 </policy>
1007 </overlayable>)";
1008 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001009}
1010
1011TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1012 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001013 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001014 <policy type="illegal_policy">
1015 <item type="string" name="foo" />
1016 </policy>
1017 </overlayable>)";
1018 EXPECT_FALSE(TestParse(input));
1019
1020 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001021 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001022 <policy type="product">
1023 <item name="foo" />
1024 </policy>
1025 </overlayable>)";
1026 EXPECT_FALSE(TestParse(input));
1027
1028 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001029 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001030 <policy type="vendor">
1031 <item type="string" />
1032 </policy>
1033 </overlayable>)";
1034 EXPECT_FALSE(TestParse(input));
1035}
1036
1037TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1038 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001039 <overlayable name="Name">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001040 <policy type="vendor|public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001041 <item type="string" name="foo" />
1042 </policy>
1043 <policy type="product|system">
1044 <item type="string" name="bar" />
1045 </policy>
1046 </overlayable>)";
1047 ASSERT_TRUE(TestParse(input));
1048
1049 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1050 ASSERT_TRUE(search_result);
1051 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001052 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1053 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1054 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1055 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kVendor
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001056 | OverlayableItem::Policy::kPublic));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001057
1058 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1059 ASSERT_TRUE(search_result);
1060 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001061 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1062 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1063 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1064 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct
1065 | OverlayableItem::Policy::kSystem));
Adam Lesinski71be7052017-12-12 16:48:07 -08001066}
1067
1068TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1069 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001070 <overlayable name="Name">
Adam Lesinski71be7052017-12-12 16:48:07 -08001071 <item type="string" name="foo" />
1072 <item type="string" name="foo" />
1073 </overlayable>)";
1074 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001075
1076 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001077 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001078 <item type="string" name="foo" />
1079 </overlayable>
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001080 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001081 <item type="string" name="foo" />
1082 </overlayable>)";
1083 EXPECT_FALSE(TestParse(input));
1084
1085 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001086 <overlayable name="Name">
1087 <item type="string" name="foo" />
1088 </overlayable>
1089 <overlayable name="Other">
1090 <item type="string" name="foo" />
1091 </overlayable>)";
1092 EXPECT_FALSE(TestParse(input));
1093
1094 input = R"(
1095 <overlayable name="Name" actor="overlay://my.actor.one">
1096 <item type="string" name="foo" />
1097 </overlayable>
1098 <overlayable name="Other" actor="overlay://my.actor.two">
1099 <item type="string" name="foo" />
1100 </overlayable>)";
1101 EXPECT_FALSE(TestParse(input));
1102
1103 input = R"(
1104 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001105 <policy type="product">
1106 <item type="string" name="foo" />
1107 <item type="string" name="foo" />
1108 </policy>
1109 </overlayable>)";
1110 EXPECT_FALSE(TestParse(input));
1111
1112 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001113 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001114 <policy type="product">
1115 <item type="string" name="foo" />
1116 </policy>
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001117 <item type="string" name="foo" />
1118 </overlayable>)";
1119 EXPECT_FALSE(TestParse(input));
1120
1121 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001122 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001123 <policy type="product">
1124 <item type="string" name="foo" />
1125 </policy>
1126 <policy type="vendor">
1127 <item type="string" name="foo" />
1128 </policy>
1129 </overlayable>)";
1130 EXPECT_FALSE(TestParse(input));
1131
1132 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001133 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001134 <policy type="product">
1135 <item type="string" name="foo" />
1136 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001137 </overlayable>
1138
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001139 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001140 <policy type="product">
1141 <item type="string" name="foo" />
1142 </policy>
1143 </overlayable>)";
1144 EXPECT_FALSE(TestParse(input));
1145}
1146
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001147TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1148 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001149 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001150 <policy type="vendor|product">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001151 <policy type="public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001152 <item type="string" name="foo" />
1153 </policy>
1154 </policy>
1155 </overlayable>)";
1156 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001157}
1158
y9efbbef2018-04-18 11:29:09 -07001159TEST_F(ResourceParserTest, ParseIdItem) {
1160 std::string input = R"(
1161 <item name="foo" type="id">@id/bar</item>
1162 <item name="bar" type="id"/>
1163 <item name="baz" type="id"></item>)";
1164 ASSERT_TRUE(TestParse(input));
1165
1166 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1167 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1168 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1169
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001170 input = R"(
1171 <id name="foo2">@id/bar</id>
1172 <id name="bar2"/>
1173 <id name="baz2"></id>)";
1174 ASSERT_TRUE(TestParse(input));
1175
1176 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1177 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1178 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1179
y9efbbef2018-04-18 11:29:09 -07001180 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001181 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -07001182 ASSERT_FALSE(TestParse(input));
1183
1184 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001185 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -07001186 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001187 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -07001188 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001189 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -07001190 ASSERT_FALSE(TestParse(input));
1191
1192 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001193 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -07001194 ASSERT_FALSE(TestParse(input));
1195}
1196
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001197TEST_F(ResourceParserTest, ParseCData) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001198 // Double quotes should still change the state of whitespace processing
1199 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001200 ASSERT_TRUE(TestParse(input));
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001201 auto output = test::GetValue<String>(&table_, "string/foo");
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001202 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001203 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001204
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001205 input = R"(<string name="foo2"><![CDATA[Hello
1206 World]]></string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001207 ASSERT_TRUE(TestParse(input));
1208 output = test::GetValue<String>(&table_, "string/foo2");
1209 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001210 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001211
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001212 // Cdata blocks should have their whitespace trimmed
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001213 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1214 ASSERT_TRUE(TestParse(input));
1215 output = test::GetValue<String>(&table_, "string/foo3");
1216 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001217 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001218
1219 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1220 ASSERT_TRUE(TestParse(input));
1221 output = test::GetValue<String>(&table_, "string/foo4");
1222 ASSERT_THAT(output, NotNull());
1223 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1224
1225 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1226 ASSERT_TRUE(TestParse(input));
1227 output = test::GetValue<String>(&table_, "string/foo5");
1228 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001229 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1230
1231 // Single quotes must still be escaped
1232 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1233 ASSERT_FALSE(TestParse(input));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001234}
1235
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236} // namespace aapt