blob: b59b16574c42ba03111d20ed255b8d3a927e7777 [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
Winson62ac8b52019-12-04 08:36:48 -080044using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
45
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046namespace aapt {
47
Adam Lesinskibab4ef52017-06-01 15:22:57 -070048constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 ResourceTable table;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000053 ResourceParser parser(context->GetDiagnostics(), &table, android::Source{"test"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054
55 std::string input = kXmlPreamble;
56 input += R"(<attr name="foo"/>)";
57 StringInputStream in(input);
58 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070060}
61
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062class ResourceParserTest : public ::testing::Test {
63 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070064 void SetUp() override {
65 context_ = test::ContextBuilder().Build();
66 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070068 ::testing::AssertionResult TestParse(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 }
Adam Lesinski52364f72016-01-11 13:10:24 -080071
Jeremy Meyer16c83af2024-07-26 16:21:49 -070072 ::testing::AssertionResult TestParse(StringPiece str, const ConfigDescription& config) {
73 ResourceParserOptions parserOptions;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000074 ResourceParser parser(context_->GetDiagnostics(), &table_, android::Source{"test"}, config,
Adam Lesinskibab4ef52017-06-01 15:22:57 -070075 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076
77 std::string input = kXmlPreamble;
78 input += "<resources>\n";
79 input.append(str.data(), str.size());
80 input += "\n</resources>";
81 StringInputStream in(input);
82 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 return ::testing::AssertionFailure();
87 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088
89 protected:
90 ResourceTable table_;
91 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092};
93
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070095 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070098 ASSERT_THAT(str, NotNull());
99 EXPECT_THAT(*str, StrValueEq(" hey there "));
100 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800101
102 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
103 str = test::GetValue<String>(&table_, "string/bar");
104 ASSERT_THAT(str, NotNull());
105 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
106
107 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
108 str = test::GetValue<String>(&table_, "string/baz");
109 ASSERT_THAT(str, NotNull());
110 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111}
112
113TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700114 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700117 ASSERT_THAT(str, NotNull());
118 EXPECT_THAT(*str, StrValueEq("?123"));
119 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700120
121 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
122 str = test::GetValue<String>(&table_, "string/bar");
123 ASSERT_THAT(str, NotNull());
124 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125}
126
Adam Lesinski9f222042015-11-04 13:51:45 -0800127TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700128 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
129 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800130}
131
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700132TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800134 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700136 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700140 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700141
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800142 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700143 EXPECT_THAT(str->value->spans, SizeIs(2));
144 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700145
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800146 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
147 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700148 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700149
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800150 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
151 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700152 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700153}
154
155TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700159 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800160 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700162
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700166 ASSERT_THAT(str, NotNull());
167 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700168}
169
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700170TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
171 // Tuncate leading and trailing whitespace
172 EXPECT_TRUE(TestParse(R"(<string name="foo">&#32;Hello&#32;</string>)"));
173
174 String* str = test::GetValue<String>(&table_, "string/foo");
175 ASSERT_THAT(str, NotNull());
176 EXPECT_THAT(*str->value, StrEq("Hello"));
177 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
178
179 // AAPT does not truncate unicode whitespace
180 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
181
182 str = test::GetValue<String>(&table_, "string/foo2");
183 ASSERT_THAT(str, NotNull());
184 EXPECT_THAT(*str->value, StrEq(" Hello "));
185 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
186
187 // Preserve non-ASCII whitespace including extended ASCII characters
Ryan Mitchell0f326752019-03-11 11:00:25 -0700188 EXPECT_TRUE(TestParse(R"(<string name="foo3">&#160;Hello&#x202F;World&#160;</string>)"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700189
190 str = test::GetValue<String>(&table_, "string/foo3");
191 ASSERT_THAT(str, NotNull());
Ryan Mitchell0f326752019-03-11 11:00:25 -0700192 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700193 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
194
195 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
196
197 str = test::GetValue<String>(&table_, "string/foo4");
198 ASSERT_THAT(str, NotNull());
199 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
200 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
201}
202
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800203TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
204 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
205 ASSERT_TRUE(TestParse(input));
206
207 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
208 ASSERT_THAT(str, NotNull());
209 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
210 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
211
212 ASSERT_THAT(str->value->spans, SizeIs(2u));
213 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
214 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
215 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
216
217 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
218 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
219 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
220}
221
Mihai Nitad1a65212019-03-26 13:47:45 -0700222TEST_F(ResourceParserTest, ParseStringTranslatableAttribute) {
223 // If there is no translate attribute the default is 'true'
224 EXPECT_TRUE(TestParse(R"(<string name="foo1">Translate</string>)"));
225 String* str = test::GetValue<String>(&table_, "string/foo1");
226 ASSERT_THAT(str, NotNull());
227 ASSERT_TRUE(str->IsTranslatable());
228
229 // Explicit 'true' translate attribute
230 EXPECT_TRUE(TestParse(R"(<string name="foo2" translatable="true">Translate</string>)"));
231 str = test::GetValue<String>(&table_, "string/foo2");
232 ASSERT_THAT(str, NotNull());
233 ASSERT_TRUE(str->IsTranslatable());
234
235 // Explicit 'false' translate attribute
236 EXPECT_TRUE(TestParse(R"(<string name="foo3" translatable="false">Do not translate</string>)"));
237 str = test::GetValue<String>(&table_, "string/foo3");
238 ASSERT_THAT(str, NotNull());
239 ASSERT_FALSE(str->IsTranslatable());
240
241 // Invalid value for the translate attribute, should be boolean ('true' or 'false')
242 EXPECT_FALSE(TestParse(R"(<string name="foo4" translatable="yes">Translate</string>)"));
243}
244
Adam Lesinski75421622017-01-06 15:20:04 -0800245TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700246 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800247 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700248 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800249 ASSERT_TRUE(TestParse(input));
250
251 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700252 ASSERT_THAT(str, NotNull());
253 EXPECT_THAT(*str, StrValueEq("There are no apples"));
254 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800255}
256
257TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700258 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800259 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700260 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800261 EXPECT_FALSE(TestParse(input));
262}
263
264TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700265 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800266 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700267 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700271 ASSERT_THAT(str, NotNull());
272 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800273
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
275 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700276 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800277}
278
279TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700280 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800281 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700282 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800283 ASSERT_TRUE(TestParse(input));
284
285 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700286 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800287 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800288
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
290 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
291 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
292
293 ASSERT_THAT(str->value->spans, SizeIs(1u));
294 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
295 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
296 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297}
298
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700299TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700300 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700302
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
304 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800305 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700307 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
308 ASSERT_THAT(null_ref, NotNull());
309 EXPECT_FALSE(null_ref->name);
310 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700311 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700312}
313
314TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700315 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700317
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700318 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700319 ASSERT_THAT(integer, NotNull());
320 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
321 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700322}
323
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700325 std::string input = R"(
326 <attr name="foo" format="string"/>
327 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700331 ASSERT_THAT(attr, NotNull());
332 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700335 ASSERT_THAT(attr, NotNull());
336 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337}
338
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700339TEST_F(ResourceParserTest, ParseMacro) {
340 std::string input = R"(<macro name="foo">12345</macro>)";
341 ASSERT_TRUE(TestParse(input));
342
343 Macro* macro = test::GetValue<Macro>(&table_, "macro/foo");
344 ASSERT_THAT(macro, NotNull());
345 EXPECT_THAT(macro->raw_value, Eq("12345"));
346 EXPECT_THAT(macro->style_string.str, Eq("12345"));
347 EXPECT_THAT(macro->style_string.spans, IsEmpty());
348 EXPECT_THAT(macro->untranslatable_sections, IsEmpty());
349 EXPECT_THAT(macro->alias_namespaces, IsEmpty());
350}
351
352TEST_F(ResourceParserTest, ParseMacroUntranslatableSection) {
353 std::string input = R"(<macro name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
354This being <b><xliff:g>human</xliff:g></b> is a guest house.</macro>)";
355 ASSERT_TRUE(TestParse(input));
356
357 Macro* macro = test::GetValue<Macro>(&table_, "macro/foo");
358 ASSERT_THAT(macro, NotNull());
359 EXPECT_THAT(macro->raw_value, Eq("\nThis being human is a guest house."));
360 EXPECT_THAT(macro->style_string.str, Eq(" This being human is a guest house."));
361 EXPECT_THAT(macro->style_string.spans.size(), Eq(1));
362 EXPECT_THAT(macro->style_string.spans[0].name, Eq("b"));
363 EXPECT_THAT(macro->style_string.spans[0].first_char, Eq(12));
364 EXPECT_THAT(macro->style_string.spans[0].last_char, Eq(16));
365 ASSERT_THAT(macro->untranslatable_sections.size(), Eq(1));
366 EXPECT_THAT(macro->untranslatable_sections[0].start, Eq(12));
367 EXPECT_THAT(macro->untranslatable_sections[0].end, Eq(17));
368 EXPECT_THAT(macro->alias_namespaces, IsEmpty());
369}
370
371TEST_F(ResourceParserTest, ParseMacroNamespaces) {
372 std::string input = R"(<macro name="foo" xmlns:app="http://schemas.android.com/apk/res/android">
373@app:string/foo</macro>)";
374 ASSERT_TRUE(TestParse(input));
375
376 Macro* macro = test::GetValue<Macro>(&table_, "macro/foo");
377 ASSERT_THAT(macro, NotNull());
378 EXPECT_THAT(macro->raw_value, Eq("\n@app:string/foo"));
379 EXPECT_THAT(macro->style_string.str, Eq("@app:string/foo"));
380 EXPECT_THAT(macro->style_string.spans, IsEmpty());
381 EXPECT_THAT(macro->untranslatable_sections, IsEmpty());
382 EXPECT_THAT(macro->alias_namespaces.size(), Eq(1));
383 EXPECT_THAT(macro->alias_namespaces[0].alias, Eq("app"));
384 EXPECT_THAT(macro->alias_namespaces[0].package_name, Eq("android"));
385 EXPECT_THAT(macro->alias_namespaces[0].is_private, Eq(false));
386}
387
388TEST_F(ResourceParserTest, ParseMacroReference) {
389 std::string input = R"(<string name="res_string">@macro/foo</string>)";
390 ASSERT_TRUE(TestParse(input));
391
392 Reference* macro = test::GetValue<Reference>(&table_, "string/res_string");
393 ASSERT_THAT(macro, NotNull());
394 EXPECT_THAT(macro->type_flags, Eq(ResTable_map::TYPE_STRING));
395 EXPECT_THAT(macro->allow_raw, Eq(false));
396
397 input = R"(<style name="foo">
398 <item name="bar">@macro/foo</item>
399 </style>)";
400
401 ASSERT_TRUE(TestParse(input));
402 Style* style = test::GetValue<Style>(&table_, "style/foo");
403 ASSERT_THAT(style, NotNull());
404 EXPECT_THAT(style->entries.size(), Eq(1));
405
406 macro = ValueCast<Reference>(style->entries[0].value.get());
407 ASSERT_THAT(macro, NotNull());
408 EXPECT_THAT(macro->type_flags, Eq(0U));
409 EXPECT_THAT(macro->allow_raw, Eq(true));
410}
411
412TEST_F(ResourceParserTest, ParseMacroNoNameFail) {
413 std::string input = R"(<macro>12345</macro>)";
414 ASSERT_FALSE(TestParse(input));
415}
416
417TEST_F(ResourceParserTest, ParseMacroNonDefaultConfigurationFail) {
418 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
419 std::string input = R"(<macro name="foo">12345</macro>)";
420 ASSERT_FALSE(TestParse(input, watch_config));
421}
422
Adam Lesinskia45893a2017-05-30 15:19:02 -0700423// Old AAPT allowed attributes to be defined under different configurations, but ultimately
424// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700425TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700427 std::string input = R"(
428 <attr name="foo" />
429 <declare-styleable name="bar">
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700430 <attr name="baz" format="reference"/>
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700431 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800433
Adam Lesinskia45893a2017-05-30 15:19:02 -0700434 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
435 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
436 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800437
Adam Lesinskia45893a2017-05-30 15:19:02 -0700438 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
439 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
440 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800441}
442
Adam Lesinskia5870652015-11-20 15:32:30 -0800443TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700444 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700448 ASSERT_THAT(attr, NotNull());
449 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
450 EXPECT_THAT(attr->min_int, Eq(10));
451 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800452}
453
454TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700455 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800456}
457
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700459 std::string input = R"(
460 <declare-styleable name="Styleable">
461 <attr name="foo" />
462 </declare-styleable>
463 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700467 ASSERT_THAT(attr, NotNull());
468 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469}
470
471TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700472 std::string input = R"(
473 <declare-styleable name="Theme">
474 <attr name="foo" />
475 </declare-styleable>
476 <declare-styleable name="Window">
477 <attr name="foo" format="boolean"/>
478 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700482 ASSERT_THAT(attr, NotNull());
483 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484}
485
486TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700487 std::string input = R"(
488 <attr name="foo">
489 <enum name="bar" value="0"/>
Ryan Mitchellc1676802019-05-20 16:47:08 -0700490 <enum name="bat" value="0x1"/>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700491 <enum name="baz" value="2"/>
492 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700496 ASSERT_THAT(enum_attr, NotNull());
497 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
498 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499
Adam Lesinskia45893a2017-05-30 15:19:02 -0700500 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
501 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
502 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700503 EXPECT_THAT(enum_attr->symbols[0].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504
Adam Lesinskia45893a2017-05-30 15:19:02 -0700505 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
506 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
507 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700508 EXPECT_THAT(enum_attr->symbols[1].type, Eq(Res_value::TYPE_INT_HEX));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509
Adam Lesinskia45893a2017-05-30 15:19:02 -0700510 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
511 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
512 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700513 EXPECT_THAT(enum_attr->symbols[2].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514}
515
516TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700517 std::string input = R"(
518 <attr name="foo">
519 <flag name="bar" value="0"/>
520 <flag name="bat" value="1"/>
521 <flag name="baz" value="2"/>
522 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700526 ASSERT_THAT(flag_attr, NotNull());
527 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
528 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529
Adam Lesinskia45893a2017-05-30 15:19:02 -0700530 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
531 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
532 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533
Adam Lesinskia45893a2017-05-30 15:19:02 -0700534 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
535 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
536 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800537
Adam Lesinskia45893a2017-05-30 15:19:02 -0700538 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
539 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
540 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 std::unique_ptr<BinaryPrimitive> flag_value =
543 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700544 ASSERT_THAT(flag_value, NotNull());
545 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546}
547
548TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700549 std::string input = R"(
550 <attr name="foo">
551 <enum name="bar" value="0"/>
552 <enum name="bat" value="1"/>
553 <enum name="bat" value="2"/>
554 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800556}
557
558TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700559 std::string input = R"(
560 <style name="foo" parent="@style/fu">
561 <item name="bar">#ffffffff</item>
562 <item name="bat">@string/hey</item>
563 <item name="baz"><b>hey</b></item>
564 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800566
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700568 ASSERT_THAT(style, NotNull());
569 ASSERT_TRUE(style->parent);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700570 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("style/fu")));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700571 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800572
Ryan Mitchell4382e442021-07-14 12:53:01 -0700573 EXPECT_THAT(style->entries[0].key.name, Eq(test::ParseNameOrDie("attr/bar")));
574 EXPECT_THAT(style->entries[1].key.name, Eq(test::ParseNameOrDie("attr/bat")));
575 EXPECT_THAT(style->entries[2].key.name, Eq(test::ParseNameOrDie("attr/baz")));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800576}
577
Adam Lesinski769de982015-04-10 19:43:55 -0700578TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700579 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700580
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700582 ASSERT_THAT(style, NotNull());
583 ASSERT_TRUE(style->parent);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700584 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("com.app:style/Theme")));
Adam Lesinski769de982015-04-10 19:43:55 -0700585}
586
Adam Lesinski24aad162015-04-24 19:19:30 -0700587TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700588 std::string input = R"(
589 <style xmlns:app="http://schemas.android.com/apk/res/android"
590 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700594 ASSERT_THAT(style, NotNull());
595 ASSERT_TRUE(style->parent);
596 ASSERT_TRUE(style->parent.value().name);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700597 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("android:style/Theme")));
Adam Lesinski24aad162015-04-24 19:19:30 -0700598}
599
600TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700601 std::string input = R"(
602 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
603 <item name="app:bar">0</item>
604 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700606
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700608 ASSERT_THAT(style, NotNull());
609 ASSERT_THAT(style->entries, SizeIs(1));
Ryan Mitchell4382e442021-07-14 12:53:01 -0700610 EXPECT_THAT(style->entries[0].key.name, Eq(test::ParseNameOrDie("android:attr/bar")));
Adam Lesinski24aad162015-04-24 19:19:30 -0700611}
612
Ryan Mitchell633d7962018-06-11 15:29:21 -0700613TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) {
614 std::string input = R"(
615 <style name="foo">
616 <item name="bar">
617 com.helloworld.AppClass
618 </item>
619 </style>)";
620 ASSERT_TRUE(TestParse(input));
621
622 Style* style = test::GetValue<Style>(&table_, "style/foo");
623 ASSERT_THAT(style, NotNull());
624 EXPECT_THAT(style->entries[0].value, NotNull());
625 RawString* value = ValueCast<RawString>(style->entries[0].value.get());
626 EXPECT_THAT(value, NotNull());
627 EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)"));
628}
629
630
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700631TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700632 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700635 ASSERT_THAT(style, NotNull());
636 ASSERT_TRUE(style->parent);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700637 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("style/foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700639}
640
Adam Lesinskia45893a2017-05-30 15:19:02 -0700641TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
642 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700643
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700645 ASSERT_THAT(style, NotNull());
646 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700648}
649
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800650TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700651 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800652
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700654 ASSERT_THAT(style, NotNull());
655 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800657}
658
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700660 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
661 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800662}
663
664TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700665 std::string input = R"(
666 <declare-styleable name="foo">
667 <attr name="bar" />
668 <attr name="bat" format="string|reference"/>
669 <attr name="baz">
670 <enum name="foo" value="1"/>
671 </attr>
672 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800674
Ryan Mitchell4382e442021-07-14 12:53:01 -0700675 std::optional<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700677 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800678 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800679
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700681 ASSERT_THAT(attr, IsNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700684 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800686
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700688 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700690 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700691
Adam Lesinskia45893a2017-05-30 15:19:02 -0700692 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700695 ASSERT_THAT(styleable, NotNull());
696 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800697
Ryan Mitchell4382e442021-07-14 12:53:01 -0700698 EXPECT_THAT(styleable->entries[0].name, Eq(test::ParseNameOrDie("attr/bar")));
699 EXPECT_THAT(styleable->entries[1].name, Eq(test::ParseNameOrDie("attr/bat")));
700 EXPECT_THAT(styleable->entries[2].name, Eq(test::ParseNameOrDie("attr/baz")));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800701}
702
Donald Chai94e4a012020-01-06 13:52:41 -0800703TEST_F(ResourceParserTest, ParseDeclareStyleablePreservingVisibility) {
704 StringInputStream input(R"(
705 <resources>
706 <declare-styleable name="foo">
707 <attr name="myattr" />
708 </declare-styleable>
709 <declare-styleable name="bar">
710 <attr name="myattr" />
711 </declare-styleable>
712 <public type="styleable" name="bar" />
713 </resources>)");
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000714 ResourceParser parser(context_->GetDiagnostics(), &table_, android::Source{"test"},
Donald Chai94e4a012020-01-06 13:52:41 -0800715 ConfigDescription::DefaultConfig(),
716 ResourceParserOptions{.preserve_visibility_of_styleables = true});
717
718 xml::XmlPullParser xml_parser(&input);
719 ASSERT_TRUE(parser.Parse(&xml_parser));
720
721 EXPECT_EQ(
722 table_.FindResource(test::ParseNameOrDie("styleable/foo")).value().entry->visibility.level,
723 Visibility::Level::kUndefined);
724 EXPECT_EQ(
725 table_.FindResource(test::ParseNameOrDie("styleable/bar")).value().entry->visibility.level,
726 Visibility::Level::kPublic);
727}
728
Adam Lesinski467f1712015-11-16 17:35:44 -0800729TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700730 std::string input = R"(
731 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
732 name="foo">
733 <attr name="*android:bar" />
734 <attr name="privAndroid:bat" />
735 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 ASSERT_TRUE(TestParse(input));
737 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700738 ASSERT_THAT(styleable, NotNull());
739 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800740
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700742 ASSERT_TRUE(styleable->entries[0].name);
743 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800744
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700746 ASSERT_TRUE(styleable->entries[1].name);
747 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800748}
749
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800750TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700751 std::string input = R"(
752 <array name="foo">
753 <item>@string/ref</item>
754 <item>hey</item>
755 <item>23</item>
756 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700760 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700761 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800762
Adam Lesinski4ffea042017-08-10 15:37:28 -0700763 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
764 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
765 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800766}
767
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700768TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700769 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700770 <string-array name="foo">
771 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700772 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700773 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700774 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700775}
776
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700777TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700778 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700779 <array name="foo" format="string">
780 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700781 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700782 ASSERT_TRUE(TestParse(input));
783
784 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700785 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700786 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700787
Adam Lesinski4ffea042017-08-10 15:37:28 -0700788 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700789 ASSERT_THAT(str, NotNull());
790 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700791}
792
793TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700794 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700795 <array name="foo" format="integer">
796 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700797 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700798 ASSERT_FALSE(TestParse(input));
799}
800
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800801TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700802 std::string input = R"(
803 <plurals name="foo">
804 <item quantity="other">apples</item>
805 <item quantity="one">apple</item>
806 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800808
809 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700810 ASSERT_THAT(plural, NotNull());
811 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
812 EXPECT_THAT(plural->values[Plural::Two], IsNull());
813 EXPECT_THAT(plural->values[Plural::Few], IsNull());
814 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800815
Adam Lesinskia45893a2017-05-30 15:19:02 -0700816 EXPECT_THAT(plural->values[Plural::One], NotNull());
817 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800818}
819
820TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700821 std::string input = R"(
822 <!--This is a comment-->
823 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800825
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700827 ASSERT_THAT(value, NotNull());
828 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700829}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700830
Adam Lesinskie78fd612015-10-22 12:48:43 -0700831TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700832 std::string input = R"(
833 <!--One-->
834 <!--Two-->
835 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700836
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700838
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700839 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700840 ASSERT_THAT(value, NotNull());
841 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700842}
843
844TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700845 std::string input = R"(
846 <!--One-->
847 <string name="foo">
848 Hi
849 <!--Two-->
850 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700851 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700852
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700853 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700854 ASSERT_THAT(value, NotNull());
855 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800856}
857
Adam Lesinskica5638f2015-10-21 14:42:43 -0700858TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700860 // comments from those end up in R.java
861 std::string input = R"(
862 <declare-styleable name="foo">
863 <!-- The name of the bar -->
864 <attr name="barName" format="string|reference" />
865 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700866
Adam Lesinskia45893a2017-05-30 15:19:02 -0700867 <attr name="foo">
868 <!-- The very first -->
869 <enum name="one" value="1" />
870 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700872
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700874 ASSERT_THAT(styleable, NotNull());
875 ASSERT_THAT(styleable->entries, SizeIs(1));
876 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700879 ASSERT_THAT(attr, NotNull());
880 ASSERT_THAT(attr->symbols, SizeIs(1));
881 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700882}
883
Adam Lesinskia45893a2017-05-30 15:19:02 -0700884// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800885TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700886 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
887 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800888}
889
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800890TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700891 std::string input = R"(
892 <string name="foo" product="phone">hi</string>
893 <string name="foo" product="no-sdcard">ho</string>
894 <string name="bar" product="">wee</string>
895 <string name="baz">woo</string>
896 <string name="bit" product="phablet">hoot</string>
897 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700899
Adam Lesinskia45893a2017-05-30 15:19:02 -0700900 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
901 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
902 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
903 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
904 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
905 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700906}
907
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800908TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700909 std::string input = R"(
910 <public-group type="attr" first-id="0x01010040">
911 <public name="foo" />
912 <public name="bar" />
913 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800915
Ryan Mitchell4382e442021-07-14 12:53:01 -0700916 std::optional<ResourceTable::SearchResult> result =
917 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700918 ASSERT_TRUE(result);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700919 ASSERT_TRUE(result.value().entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700920 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700922 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700923 ASSERT_TRUE(result);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700924 ASSERT_TRUE(result.value().entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700925 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800926}
927
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700928TEST_F(ResourceParserTest, StagingPublicGroup) {
929 std::string input = R"(
930 <staging-public-group type="attr" first-id="0x01ff0049">
931 <public name="foo" />
932 <public name="bar" />
933 </staging-public-group>)";
934 ASSERT_TRUE(TestParse(input));
935
Ryan Mitchell4382e442021-07-14 12:53:01 -0700936 std::optional<ResourceTable::SearchResult> result =
937 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700938 ASSERT_TRUE(result);
939
940 ASSERT_TRUE(result.value().entry->id);
941 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01ff0049)));
942 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
943 EXPECT_TRUE(result.value().entry->visibility.staged_api);
944
945 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
946 ASSERT_TRUE(result);
947
948 ASSERT_TRUE(result.value().entry->id);
949 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01ff004a)));
950 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
951 EXPECT_TRUE(result.value().entry->visibility.staged_api);
952}
953
Adam Lesinski71be7052017-12-12 16:48:07 -0800954TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
955 std::string input = R"(
956 <!-- private -->
957 <java-symbol type="string" name="foo" />
958 <!-- public -->
959 <public type="string" name="foo" id="0x01020000" />
960 <!-- private2 -->
961 <java-symbol type="string" name="foo" />)";
962 ASSERT_TRUE(TestParse(input));
963
Ryan Mitchell4382e442021-07-14 12:53:01 -0700964 std::optional<ResourceTable::SearchResult> result =
Adam Lesinski71be7052017-12-12 16:48:07 -0800965 table_.FindResource(test::ParseNameOrDie("string/foo"));
966 ASSERT_TRUE(result);
967
968 ResourceEntry* entry = result.value().entry;
969 ASSERT_THAT(entry, NotNull());
970 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
971 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
972}
973
Adam Lesinskifa105052015-11-07 13:34:39 -0800974TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700975 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
976 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800977}
978
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700979TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700980 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800981
Ryan Mitchell4382e442021-07-14 12:53:01 -0700982 std::optional<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700984 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700986 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800987 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
988 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800989}
990
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800991TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700992 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800993
Adam Lesinskie597d682017-06-01 17:16:44 -0700994 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
995 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700996 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800997
Adam Lesinskia45893a2017-05-30 15:19:02 -0700998 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700999}
1000
1001// An <item> without a format specifier accepts all types of values.
1002TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001003 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -07001004
1005 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
1006 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001007 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001008}
1009
Adam Lesinski86d67df2017-01-31 13:47:27 -08001010TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001011 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
1012 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -08001013}
1014
1015TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001016 std::string input = R"(
1017 <bag name="bag" type="configVarying">
1018 <item name="test">Hello!</item>
1019 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -08001020 ASSERT_TRUE(TestParse(input));
1021
1022 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -07001023 ASSERT_THAT(val, NotNull());
1024 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -08001025
Adam Lesinskia45893a2017-05-30 15:19:02 -07001026 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
1027 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -08001028}
1029
Adam Lesinskibab4ef52017-06-01 15:22:57 -07001030TEST_F(ResourceParserTest, ParseElementWithNoValue) {
1031 std::string input = R"(
1032 <item type="drawable" format="reference" name="foo" />
1033 <string name="foo" />)";
1034 ASSERT_TRUE(TestParse(input));
1035 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
1036
1037 String* str = test::GetValue<String>(&table_, "string/foo");
1038 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001039 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -07001040}
1041
Adam Lesinskib9f05482017-06-02 16:32:37 -07001042TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001043 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -07001044}
1045
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001046TEST_F(ResourceParserTest, ParseOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001047 std::string input = R"(
1048 <overlayable name="Name" actor="overlay://theme">
Winsonb2d7f532019-02-04 16:32:43 -08001049 <policy type="signature">
1050 <item type="string" name="foo" />
1051 <item type="drawable" name="bar" />
1052 </policy>
Adam Lesinski46c4d722017-08-23 13:03:56 -07001053 </overlayable>)";
1054 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -08001055
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001056 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
Adam Lesinski71be7052017-12-12 16:48:07 -08001057 ASSERT_TRUE(search_result);
1058 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001059 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1060 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
1061 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1062 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -08001063 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001064
1065 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
1066 ASSERT_TRUE(search_result);
1067 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001068 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1069 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1070 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1071 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -08001072 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001073}
1074
1075TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
1076 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
1077 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
1078 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
1079}
1080
1081TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
1082 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001083}
1084
1085TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001086 std::string input = R"(
1087 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001088 <policy type="product">
1089 <item type="string" name="bar" />
1090 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001091 <policy type="system">
1092 <item type="string" name="fiz" />
1093 </policy>
1094 <policy type="vendor">
1095 <item type="string" name="fuz" />
1096 </policy>
1097 <policy type="public">
1098 <item type="string" name="faz" />
1099 </policy>
Winsonb2d7f532019-02-04 16:32:43 -08001100 <policy type="signature">
1101 <item type="string" name="foz" />
1102 </policy>
Ryan Mitchell939df092019-04-09 17:13:50 -07001103 <policy type="odm">
1104 <item type="string" name="biz" />
1105 </policy>
1106 <policy type="oem">
1107 <item type="string" name="buz" />
1108 </policy>
Winsonf56ade32019-12-04 11:32:41 -08001109 <policy type="actor">
1110 <item type="string" name="actor" />
1111 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001112 </overlayable>)";
1113 ASSERT_TRUE(TestParse(input));
1114
Winsonb2d7f532019-02-04 16:32:43 -08001115 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001116 ASSERT_TRUE(search_result);
1117 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001118 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1119 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1120 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001121 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001122
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001123 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
1124 ASSERT_TRUE(search_result);
1125 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001126 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1127 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1128 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001129 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SYSTEM_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001130
1131 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
1132 ASSERT_TRUE(search_result);
1133 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001134 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1135 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1136 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001137 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001138
1139 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
1140 ASSERT_TRUE(search_result);
1141 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001142 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1143 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1144 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001145 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PUBLIC));
Winsonb2d7f532019-02-04 16:32:43 -08001146
1147 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
1148 ASSERT_TRUE(search_result);
1149 ASSERT_THAT(search_result.value().entry, NotNull());
1150 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1151 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1152 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001153 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell939df092019-04-09 17:13:50 -07001154
1155 search_result = table_.FindResource(test::ParseNameOrDie("string/biz"));
1156 ASSERT_TRUE(search_result);
1157 ASSERT_THAT(search_result.value().entry, NotNull());
1158 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1159 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1160 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001161 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ODM_PARTITION));
Ryan Mitchell939df092019-04-09 17:13:50 -07001162
1163 search_result = table_.FindResource(test::ParseNameOrDie("string/buz"));
1164 ASSERT_TRUE(search_result);
1165 ASSERT_THAT(search_result.value().entry, NotNull());
1166 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1167 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1168 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001169 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::OEM_PARTITION));
Winsonf56ade32019-12-04 11:32:41 -08001170
1171 search_result = table_.FindResource(test::ParseNameOrDie("string/actor"));
1172 ASSERT_TRUE(search_result);
1173 ASSERT_THAT(search_result.value().entry, NotNull());
1174 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1175 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1176 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1177 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ACTOR_SIGNATURE));
Winsonb2d7f532019-02-04 16:32:43 -08001178}
1179
1180TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
1181 std::string input = R"(
1182 <overlayable name="Name">
1183 <item type="string" name="foo" />
1184 </overlayable>)";
1185 EXPECT_FALSE(TestParse(input));
1186
1187 input = R"(
1188 <overlayable name="Name">
1189 <policy>
1190 <item name="foo" />
1191 </policy>
1192 </overlayable>)";
1193 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001194}
1195
1196TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1197 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001198 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001199 <policy type="illegal_policy">
1200 <item type="string" name="foo" />
1201 </policy>
1202 </overlayable>)";
1203 EXPECT_FALSE(TestParse(input));
1204
1205 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001206 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001207 <policy type="product">
1208 <item name="foo" />
1209 </policy>
1210 </overlayable>)";
1211 EXPECT_FALSE(TestParse(input));
1212
1213 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001214 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001215 <policy type="vendor">
1216 <item type="string" />
1217 </policy>
1218 </overlayable>)";
1219 EXPECT_FALSE(TestParse(input));
1220}
1221
1222TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1223 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001224 <overlayable name="Name">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001225 <policy type="vendor|public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001226 <item type="string" name="foo" />
1227 </policy>
1228 <policy type="product|system">
1229 <item type="string" name="bar" />
1230 </policy>
1231 </overlayable>)";
1232 ASSERT_TRUE(TestParse(input));
1233
1234 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1235 ASSERT_TRUE(search_result);
1236 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001237 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1238 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1239 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001240 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION
1241 | PolicyFlags::PUBLIC));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001242
1243 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1244 ASSERT_TRUE(search_result);
1245 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001246 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1247 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1248 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001249 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
1250 | PolicyFlags::SYSTEM_PARTITION));
Adam Lesinski71be7052017-12-12 16:48:07 -08001251}
1252
1253TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1254 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001255 <overlayable name="Name">
Adam Lesinski71be7052017-12-12 16:48:07 -08001256 <item type="string" name="foo" />
1257 <item type="string" name="foo" />
1258 </overlayable>)";
1259 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001260
1261 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001262 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001263 <item type="string" name="foo" />
1264 </overlayable>
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001265 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001266 <item type="string" name="foo" />
1267 </overlayable>)";
1268 EXPECT_FALSE(TestParse(input));
1269
1270 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001271 <overlayable name="Name">
1272 <item type="string" name="foo" />
1273 </overlayable>
1274 <overlayable name="Other">
1275 <item type="string" name="foo" />
1276 </overlayable>)";
1277 EXPECT_FALSE(TestParse(input));
1278
1279 input = R"(
1280 <overlayable name="Name" actor="overlay://my.actor.one">
1281 <item type="string" name="foo" />
1282 </overlayable>
1283 <overlayable name="Other" actor="overlay://my.actor.two">
1284 <item type="string" name="foo" />
1285 </overlayable>)";
1286 EXPECT_FALSE(TestParse(input));
1287
1288 input = R"(
1289 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001290 <policy type="product">
1291 <item type="string" name="foo" />
1292 <item type="string" name="foo" />
1293 </policy>
1294 </overlayable>)";
1295 EXPECT_FALSE(TestParse(input));
1296
1297 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001298 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001299 <policy type="product">
1300 <item type="string" name="foo" />
1301 </policy>
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001302 <item type="string" name="foo" />
1303 </overlayable>)";
1304 EXPECT_FALSE(TestParse(input));
1305
1306 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001307 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001308 <policy type="product">
1309 <item type="string" name="foo" />
1310 </policy>
1311 <policy type="vendor">
1312 <item type="string" name="foo" />
1313 </policy>
1314 </overlayable>)";
1315 EXPECT_FALSE(TestParse(input));
1316
1317 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001318 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001319 <policy type="product">
1320 <item type="string" name="foo" />
1321 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001322 </overlayable>
1323
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001324 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001325 <policy type="product">
1326 <item type="string" name="foo" />
1327 </policy>
1328 </overlayable>)";
1329 EXPECT_FALSE(TestParse(input));
1330}
1331
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001332TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1333 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001334 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001335 <policy type="vendor|product">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001336 <policy type="public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001337 <item type="string" name="foo" />
1338 </policy>
1339 </policy>
1340 </overlayable>)";
1341 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001342}
1343
y9efbbef2018-04-18 11:29:09 -07001344TEST_F(ResourceParserTest, ParseIdItem) {
1345 std::string input = R"(
1346 <item name="foo" type="id">@id/bar</item>
1347 <item name="bar" type="id"/>
1348 <item name="baz" type="id"></item>)";
1349 ASSERT_TRUE(TestParse(input));
1350
1351 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1352 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1353 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1354
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001355 input = R"(
1356 <id name="foo2">@id/bar</id>
1357 <id name="bar2"/>
1358 <id name="baz2"></id>)";
1359 ASSERT_TRUE(TestParse(input));
1360
1361 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1362 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1363 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1364
y9efbbef2018-04-18 11:29:09 -07001365 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001366 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -07001367 ASSERT_FALSE(TestParse(input));
1368
1369 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001370 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -07001371 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001372 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -07001373 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001374 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -07001375 ASSERT_FALSE(TestParse(input));
1376
1377 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001378 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -07001379 ASSERT_FALSE(TestParse(input));
1380}
1381
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001382TEST_F(ResourceParserTest, ParseCData) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001383 // Double quotes should still change the state of whitespace processing
1384 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001385 ASSERT_TRUE(TestParse(input));
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001386 auto output = test::GetValue<String>(&table_, "string/foo");
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001387 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001388 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001389
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001390 input = R"(<string name="foo2"><![CDATA[Hello
1391 World]]></string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001392 ASSERT_TRUE(TestParse(input));
1393 output = test::GetValue<String>(&table_, "string/foo2");
1394 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001395 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001396
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001397 // Cdata blocks should have their whitespace trimmed
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001398 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1399 ASSERT_TRUE(TestParse(input));
1400 output = test::GetValue<String>(&table_, "string/foo3");
1401 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001402 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001403
1404 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1405 ASSERT_TRUE(TestParse(input));
1406 output = test::GetValue<String>(&table_, "string/foo4");
1407 ASSERT_THAT(output, NotNull());
1408 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1409
1410 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1411 ASSERT_TRUE(TestParse(input));
1412 output = test::GetValue<String>(&table_, "string/foo5");
1413 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001414 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1415
1416 // Single quotes must still be escaped
1417 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1418 ASSERT_FALSE(TestParse(input));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001419}
1420
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421} // namespace aapt