blob: 279ebcba2f71efcf01b7b1896dfe3d00bc182ccc [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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 ResourceParser parser(context->GetDiagnostics(), &table, 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ::testing::AssertionResult TestParse(const StringPiece& str) {
69 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 }
Adam Lesinski52364f72016-01-11 13:10:24 -080071
Adam Lesinskibab4ef52017-06-01 15:22:57 -070072 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070074 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
75 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);
570 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
571 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800572
Adam Lesinskia45893a2017-05-30 15:19:02 -0700573 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
574 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
575 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(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);
584 EXPECT_THAT(style->parent.value().name, Eq(make_value(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);
597 EXPECT_THAT(style->parent.value().name, Eq(make_value(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));
610 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(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);
637 EXPECT_THAT(style->parent.value().name, Eq(make_value(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
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 Maybe<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
Adam Lesinskia45893a2017-05-30 15:19:02 -0700698 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
699 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
700 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(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>)");
714 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
715 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
Adam Lesinskia45893a2017-05-30 15:19:02 -0700916 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
917 ASSERT_TRUE(result);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700918 ASSERT_TRUE(result.value().entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700919 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700920
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700922 ASSERT_TRUE(result);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700923 ASSERT_TRUE(result.value().entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700924 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800925}
926
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700927TEST_F(ResourceParserTest, StagingPublicGroup) {
928 std::string input = R"(
929 <staging-public-group type="attr" first-id="0x01ff0049">
930 <public name="foo" />
931 <public name="bar" />
932 </staging-public-group>)";
933 ASSERT_TRUE(TestParse(input));
934
935 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
936 ASSERT_TRUE(result);
937
938 ASSERT_TRUE(result.value().entry->id);
939 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01ff0049)));
940 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
941 EXPECT_TRUE(result.value().entry->visibility.staged_api);
942
943 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
944 ASSERT_TRUE(result);
945
946 ASSERT_TRUE(result.value().entry->id);
947 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01ff004a)));
948 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
949 EXPECT_TRUE(result.value().entry->visibility.staged_api);
950}
951
Adam Lesinski71be7052017-12-12 16:48:07 -0800952TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
953 std::string input = R"(
954 <!-- private -->
955 <java-symbol type="string" name="foo" />
956 <!-- public -->
957 <public type="string" name="foo" id="0x01020000" />
958 <!-- private2 -->
959 <java-symbol type="string" name="foo" />)";
960 ASSERT_TRUE(TestParse(input));
961
962 Maybe<ResourceTable::SearchResult> result =
963 table_.FindResource(test::ParseNameOrDie("string/foo"));
964 ASSERT_TRUE(result);
965
966 ResourceEntry* entry = result.value().entry;
967 ASSERT_THAT(entry, NotNull());
968 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
969 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
970}
971
Adam Lesinskifa105052015-11-07 13:34:39 -0800972TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700973 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
974 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800975}
976
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700977TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700978 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800979
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700982 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700984 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800985 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
986 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800987}
988
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800989TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700990 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800991
Adam Lesinskie597d682017-06-01 17:16:44 -0700992 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
993 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700994 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800995
Adam Lesinskia45893a2017-05-30 15:19:02 -0700996 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700997}
998
999// An <item> without a format specifier accepts all types of values.
1000TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001001 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -07001002
1003 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
1004 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001005 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001006}
1007
Adam Lesinski86d67df2017-01-31 13:47:27 -08001008TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001009 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
1010 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -08001011}
1012
1013TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001014 std::string input = R"(
1015 <bag name="bag" type="configVarying">
1016 <item name="test">Hello!</item>
1017 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -08001018 ASSERT_TRUE(TestParse(input));
1019
1020 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -07001021 ASSERT_THAT(val, NotNull());
1022 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -08001023
Adam Lesinskia45893a2017-05-30 15:19:02 -07001024 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
1025 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -08001026}
1027
Adam Lesinskibab4ef52017-06-01 15:22:57 -07001028TEST_F(ResourceParserTest, ParseElementWithNoValue) {
1029 std::string input = R"(
1030 <item type="drawable" format="reference" name="foo" />
1031 <string name="foo" />)";
1032 ASSERT_TRUE(TestParse(input));
1033 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
1034
1035 String* str = test::GetValue<String>(&table_, "string/foo");
1036 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001037 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -07001038}
1039
Adam Lesinskib9f05482017-06-02 16:32:37 -07001040TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001041 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -07001042}
1043
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001044TEST_F(ResourceParserTest, ParseOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001045 std::string input = R"(
1046 <overlayable name="Name" actor="overlay://theme">
Winsonb2d7f532019-02-04 16:32:43 -08001047 <policy type="signature">
1048 <item type="string" name="foo" />
1049 <item type="drawable" name="bar" />
1050 </policy>
Adam Lesinski46c4d722017-08-23 13:03:56 -07001051 </overlayable>)";
1052 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -08001053
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001054 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
Adam Lesinski71be7052017-12-12 16:48:07 -08001055 ASSERT_TRUE(search_result);
1056 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001057 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1058 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
1059 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1060 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -08001061 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001062
1063 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
1064 ASSERT_TRUE(search_result);
1065 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001066 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1067 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1068 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1069 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -08001070 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001071}
1072
1073TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
1074 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
1075 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
1076 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
1077}
1078
1079TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
1080 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001081}
1082
1083TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001084 std::string input = R"(
1085 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001086 <policy type="product">
1087 <item type="string" name="bar" />
1088 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001089 <policy type="system">
1090 <item type="string" name="fiz" />
1091 </policy>
1092 <policy type="vendor">
1093 <item type="string" name="fuz" />
1094 </policy>
1095 <policy type="public">
1096 <item type="string" name="faz" />
1097 </policy>
Winsonb2d7f532019-02-04 16:32:43 -08001098 <policy type="signature">
1099 <item type="string" name="foz" />
1100 </policy>
Ryan Mitchell939df092019-04-09 17:13:50 -07001101 <policy type="odm">
1102 <item type="string" name="biz" />
1103 </policy>
1104 <policy type="oem">
1105 <item type="string" name="buz" />
1106 </policy>
Winsonf56ade32019-12-04 11:32:41 -08001107 <policy type="actor">
1108 <item type="string" name="actor" />
1109 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001110 </overlayable>)";
1111 ASSERT_TRUE(TestParse(input));
1112
Winsonb2d7f532019-02-04 16:32:43 -08001113 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001114 ASSERT_TRUE(search_result);
1115 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001116 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1117 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1118 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001119 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001120
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001121 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
1122 ASSERT_TRUE(search_result);
1123 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001124 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1125 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1126 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001127 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SYSTEM_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001128
1129 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
1130 ASSERT_TRUE(search_result);
1131 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001132 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1133 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1134 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001135 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001136
1137 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
1138 ASSERT_TRUE(search_result);
1139 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001140 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1141 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1142 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001143 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PUBLIC));
Winsonb2d7f532019-02-04 16:32:43 -08001144
1145 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
1146 ASSERT_TRUE(search_result);
1147 ASSERT_THAT(search_result.value().entry, NotNull());
1148 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1149 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1150 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001151 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell939df092019-04-09 17:13:50 -07001152
1153 search_result = table_.FindResource(test::ParseNameOrDie("string/biz"));
1154 ASSERT_TRUE(search_result);
1155 ASSERT_THAT(search_result.value().entry, NotNull());
1156 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1157 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1158 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001159 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ODM_PARTITION));
Ryan Mitchell939df092019-04-09 17:13:50 -07001160
1161 search_result = table_.FindResource(test::ParseNameOrDie("string/buz"));
1162 ASSERT_TRUE(search_result);
1163 ASSERT_THAT(search_result.value().entry, NotNull());
1164 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1165 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1166 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001167 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::OEM_PARTITION));
Winsonf56ade32019-12-04 11:32:41 -08001168
1169 search_result = table_.FindResource(test::ParseNameOrDie("string/actor"));
1170 ASSERT_TRUE(search_result);
1171 ASSERT_THAT(search_result.value().entry, NotNull());
1172 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1173 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1174 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1175 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ACTOR_SIGNATURE));
Winsonb2d7f532019-02-04 16:32:43 -08001176}
1177
1178TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
1179 std::string input = R"(
1180 <overlayable name="Name">
1181 <item type="string" name="foo" />
1182 </overlayable>)";
1183 EXPECT_FALSE(TestParse(input));
1184
1185 input = R"(
1186 <overlayable name="Name">
1187 <policy>
1188 <item name="foo" />
1189 </policy>
1190 </overlayable>)";
1191 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001192}
1193
1194TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1195 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001196 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001197 <policy type="illegal_policy">
1198 <item type="string" name="foo" />
1199 </policy>
1200 </overlayable>)";
1201 EXPECT_FALSE(TestParse(input));
1202
1203 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001204 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001205 <policy type="product">
1206 <item name="foo" />
1207 </policy>
1208 </overlayable>)";
1209 EXPECT_FALSE(TestParse(input));
1210
1211 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001212 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001213 <policy type="vendor">
1214 <item type="string" />
1215 </policy>
1216 </overlayable>)";
1217 EXPECT_FALSE(TestParse(input));
1218}
1219
1220TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1221 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001222 <overlayable name="Name">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001223 <policy type="vendor|public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001224 <item type="string" name="foo" />
1225 </policy>
1226 <policy type="product|system">
1227 <item type="string" name="bar" />
1228 </policy>
1229 </overlayable>)";
1230 ASSERT_TRUE(TestParse(input));
1231
1232 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1233 ASSERT_TRUE(search_result);
1234 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001235 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1236 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1237 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001238 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION
1239 | PolicyFlags::PUBLIC));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001240
1241 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1242 ASSERT_TRUE(search_result);
1243 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001244 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1245 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1246 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001247 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
1248 | PolicyFlags::SYSTEM_PARTITION));
Adam Lesinski71be7052017-12-12 16:48:07 -08001249}
1250
1251TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1252 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001253 <overlayable name="Name">
Adam Lesinski71be7052017-12-12 16:48:07 -08001254 <item type="string" name="foo" />
1255 <item type="string" name="foo" />
1256 </overlayable>)";
1257 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001258
1259 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001260 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001261 <item type="string" name="foo" />
1262 </overlayable>
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001263 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001264 <item type="string" name="foo" />
1265 </overlayable>)";
1266 EXPECT_FALSE(TestParse(input));
1267
1268 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001269 <overlayable name="Name">
1270 <item type="string" name="foo" />
1271 </overlayable>
1272 <overlayable name="Other">
1273 <item type="string" name="foo" />
1274 </overlayable>)";
1275 EXPECT_FALSE(TestParse(input));
1276
1277 input = R"(
1278 <overlayable name="Name" actor="overlay://my.actor.one">
1279 <item type="string" name="foo" />
1280 </overlayable>
1281 <overlayable name="Other" actor="overlay://my.actor.two">
1282 <item type="string" name="foo" />
1283 </overlayable>)";
1284 EXPECT_FALSE(TestParse(input));
1285
1286 input = R"(
1287 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001288 <policy type="product">
1289 <item type="string" name="foo" />
1290 <item type="string" name="foo" />
1291 </policy>
1292 </overlayable>)";
1293 EXPECT_FALSE(TestParse(input));
1294
1295 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001296 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001297 <policy type="product">
1298 <item type="string" name="foo" />
1299 </policy>
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001300 <item type="string" name="foo" />
1301 </overlayable>)";
1302 EXPECT_FALSE(TestParse(input));
1303
1304 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001305 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001306 <policy type="product">
1307 <item type="string" name="foo" />
1308 </policy>
1309 <policy type="vendor">
1310 <item type="string" name="foo" />
1311 </policy>
1312 </overlayable>)";
1313 EXPECT_FALSE(TestParse(input));
1314
1315 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001316 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001317 <policy type="product">
1318 <item type="string" name="foo" />
1319 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001320 </overlayable>
1321
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001322 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001323 <policy type="product">
1324 <item type="string" name="foo" />
1325 </policy>
1326 </overlayable>)";
1327 EXPECT_FALSE(TestParse(input));
1328}
1329
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001330TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1331 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001332 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001333 <policy type="vendor|product">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001334 <policy type="public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001335 <item type="string" name="foo" />
1336 </policy>
1337 </policy>
1338 </overlayable>)";
1339 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001340}
1341
y9efbbef2018-04-18 11:29:09 -07001342TEST_F(ResourceParserTest, ParseIdItem) {
1343 std::string input = R"(
1344 <item name="foo" type="id">@id/bar</item>
1345 <item name="bar" type="id"/>
1346 <item name="baz" type="id"></item>)";
1347 ASSERT_TRUE(TestParse(input));
1348
1349 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1350 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1351 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1352
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001353 input = R"(
1354 <id name="foo2">@id/bar</id>
1355 <id name="bar2"/>
1356 <id name="baz2"></id>)";
1357 ASSERT_TRUE(TestParse(input));
1358
1359 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1360 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1361 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1362
y9efbbef2018-04-18 11:29:09 -07001363 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001364 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -07001365 ASSERT_FALSE(TestParse(input));
1366
1367 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001368 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -07001369 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001370 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -07001371 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001372 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -07001373 ASSERT_FALSE(TestParse(input));
1374
1375 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001376 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -07001377 ASSERT_FALSE(TestParse(input));
1378}
1379
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001380TEST_F(ResourceParserTest, ParseCData) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001381 // Double quotes should still change the state of whitespace processing
1382 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001383 ASSERT_TRUE(TestParse(input));
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001384 auto output = test::GetValue<String>(&table_, "string/foo");
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001385 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001386 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001387
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001388 input = R"(<string name="foo2"><![CDATA[Hello
1389 World]]></string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001390 ASSERT_TRUE(TestParse(input));
1391 output = test::GetValue<String>(&table_, "string/foo2");
1392 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001393 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001394
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001395 // Cdata blocks should have their whitespace trimmed
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001396 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1397 ASSERT_TRUE(TestParse(input));
1398 output = test::GetValue<String>(&table_, "string/foo3");
1399 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001400 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001401
1402 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1403 ASSERT_TRUE(TestParse(input));
1404 output = test::GetValue<String>(&table_, "string/foo4");
1405 ASSERT_THAT(output, NotNull());
1406 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1407
1408 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1409 ASSERT_TRUE(TestParse(input));
1410 output = test::GetValue<String>(&table_, "string/foo5");
1411 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001412 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1413
1414 // Single quotes must still be escaped
1415 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1416 ASSERT_FALSE(TestParse(input));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001417}
1418
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001419} // namespace aapt