blob: 2e6ad13d99de87c72f67dc98442c72b44f3ffa9c [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 Meyerefa42b12024-07-17 14:57:33 -070072 ::testing::AssertionResult TestParse(StringPiece str, ResourceParserOptions parserOptions) {
73 return TestParse(str, ConfigDescription{}, parserOptions);
74 }
75
76 ::testing::AssertionResult TestParse(
77 StringPiece str, const ConfigDescription& config,
78 ResourceParserOptions parserOptions = ResourceParserOptions()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000079 ResourceParser parser(context_->GetDiagnostics(), &table_, android::Source{"test"}, config,
Adam Lesinskibab4ef52017-06-01 15:22:57 -070080 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070081
82 std::string input = kXmlPreamble;
83 input += "<resources>\n";
84 input.append(str.data(), str.size());
85 input += "\n</resources>";
86 StringInputStream in(input);
87 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return ::testing::AssertionFailure();
92 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093
94 protected:
95 ResourceTable table_;
96 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097};
98
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700100 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700103 ASSERT_THAT(str, NotNull());
104 EXPECT_THAT(*str, StrValueEq(" hey there "));
105 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800106
107 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
108 str = test::GetValue<String>(&table_, "string/bar");
109 ASSERT_THAT(str, NotNull());
110 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
111
112 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
113 str = test::GetValue<String>(&table_, "string/baz");
114 ASSERT_THAT(str, NotNull());
115 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116}
117
118TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700119 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700122 ASSERT_THAT(str, NotNull());
123 EXPECT_THAT(*str, StrValueEq("?123"));
124 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700125
126 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
127 str = test::GetValue<String>(&table_, "string/bar");
128 ASSERT_THAT(str, NotNull());
129 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130}
131
Adam Lesinski9f222042015-11-04 13:51:45 -0800132TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700133 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
134 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800135}
136
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700137TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800139 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700141 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700145 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700146
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800147 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700148 EXPECT_THAT(str->value->spans, SizeIs(2));
149 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700150
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800151 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
152 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700153 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700154
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800155 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
156 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700157 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700158}
159
160TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700164 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800165 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700166 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700167
Adam Lesinskia45893a2017-05-30 15:19:02 -0700168 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700171 ASSERT_THAT(str, NotNull());
172 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700173}
174
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700175TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
176 // Tuncate leading and trailing whitespace
177 EXPECT_TRUE(TestParse(R"(<string name="foo">&#32;Hello&#32;</string>)"));
178
179 String* str = test::GetValue<String>(&table_, "string/foo");
180 ASSERT_THAT(str, NotNull());
181 EXPECT_THAT(*str->value, StrEq("Hello"));
182 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
183
184 // AAPT does not truncate unicode whitespace
185 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
186
187 str = test::GetValue<String>(&table_, "string/foo2");
188 ASSERT_THAT(str, NotNull());
189 EXPECT_THAT(*str->value, StrEq(" Hello "));
190 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
191
192 // Preserve non-ASCII whitespace including extended ASCII characters
Ryan Mitchell0f326752019-03-11 11:00:25 -0700193 EXPECT_TRUE(TestParse(R"(<string name="foo3">&#160;Hello&#x202F;World&#160;</string>)"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700194
195 str = test::GetValue<String>(&table_, "string/foo3");
196 ASSERT_THAT(str, NotNull());
Ryan Mitchell0f326752019-03-11 11:00:25 -0700197 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700198 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
199
200 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
201
202 str = test::GetValue<String>(&table_, "string/foo4");
203 ASSERT_THAT(str, NotNull());
204 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
205 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
206}
207
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800208TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
209 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
210 ASSERT_TRUE(TestParse(input));
211
212 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
213 ASSERT_THAT(str, NotNull());
214 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
215 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
216
217 ASSERT_THAT(str->value->spans, SizeIs(2u));
218 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
219 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
220 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
221
222 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
223 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
224 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
225}
226
Mihai Nitad1a65212019-03-26 13:47:45 -0700227TEST_F(ResourceParserTest, ParseStringTranslatableAttribute) {
228 // If there is no translate attribute the default is 'true'
229 EXPECT_TRUE(TestParse(R"(<string name="foo1">Translate</string>)"));
230 String* str = test::GetValue<String>(&table_, "string/foo1");
231 ASSERT_THAT(str, NotNull());
232 ASSERT_TRUE(str->IsTranslatable());
233
234 // Explicit 'true' translate attribute
235 EXPECT_TRUE(TestParse(R"(<string name="foo2" translatable="true">Translate</string>)"));
236 str = test::GetValue<String>(&table_, "string/foo2");
237 ASSERT_THAT(str, NotNull());
238 ASSERT_TRUE(str->IsTranslatable());
239
240 // Explicit 'false' translate attribute
241 EXPECT_TRUE(TestParse(R"(<string name="foo3" translatable="false">Do not translate</string>)"));
242 str = test::GetValue<String>(&table_, "string/foo3");
243 ASSERT_THAT(str, NotNull());
244 ASSERT_FALSE(str->IsTranslatable());
245
246 // Invalid value for the translate attribute, should be boolean ('true' or 'false')
247 EXPECT_FALSE(TestParse(R"(<string name="foo4" translatable="yes">Translate</string>)"));
248}
249
Jeremy Meyerefa42b12024-07-17 14:57:33 -0700250TEST_F(ResourceParserTest, ParseStringBehindDisabledFlag) {
251 FeatureFlagProperties flag_properties(true, false);
252 ResourceParserOptions options;
253 options.feature_flag_values = {{"falseFlag", flag_properties}};
254 ASSERT_TRUE(TestParse(
255 R"(<string name="foo" android:featureFlag="falseFlag"
256 xmlns:android="http://schemas.android.com/apk/res/android">foo</string>)",
257 options));
258
259 String* str = test::GetValue<String>(&table_, "string/foo");
260 ASSERT_THAT(str, IsNull());
261}
262
Adam Lesinski75421622017-01-06 15:20:04 -0800263TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700264 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800265 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700266 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800267 ASSERT_TRUE(TestParse(input));
268
269 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700270 ASSERT_THAT(str, NotNull());
271 EXPECT_THAT(*str, StrValueEq("There are no apples"));
272 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800273}
274
275TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700276 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800277 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700278 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800279 EXPECT_FALSE(TestParse(input));
280}
281
282TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700283 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800284 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700285 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700289 ASSERT_THAT(str, NotNull());
290 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800291
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800292 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
293 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700294 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800295}
296
297TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700298 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800299 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700300 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800301 ASSERT_TRUE(TestParse(input));
302
303 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700304 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800305 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800306
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800307 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
308 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
309 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
310
311 ASSERT_THAT(str->value->spans, SizeIs(1u));
312 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
313 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
314 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315}
316
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700317TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700318 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700320
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
322 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800323 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700325 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
326 ASSERT_THAT(null_ref, NotNull());
327 EXPECT_FALSE(null_ref->name);
328 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700329 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700330}
331
332TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700333 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700335
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700336 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700337 ASSERT_THAT(integer, NotNull());
338 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
339 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700340}
341
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700343 std::string input = R"(
344 <attr name="foo" format="string"/>
345 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700349 ASSERT_THAT(attr, NotNull());
350 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800351
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700353 ASSERT_THAT(attr, NotNull());
354 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355}
356
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700357TEST_F(ResourceParserTest, ParseMacro) {
358 std::string input = R"(<macro name="foo">12345</macro>)";
359 ASSERT_TRUE(TestParse(input));
360
361 Macro* macro = test::GetValue<Macro>(&table_, "macro/foo");
362 ASSERT_THAT(macro, NotNull());
363 EXPECT_THAT(macro->raw_value, Eq("12345"));
364 EXPECT_THAT(macro->style_string.str, Eq("12345"));
365 EXPECT_THAT(macro->style_string.spans, IsEmpty());
366 EXPECT_THAT(macro->untranslatable_sections, IsEmpty());
367 EXPECT_THAT(macro->alias_namespaces, IsEmpty());
368}
369
370TEST_F(ResourceParserTest, ParseMacroUntranslatableSection) {
371 std::string input = R"(<macro name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
372This being <b><xliff:g>human</xliff:g></b> is a guest house.</macro>)";
373 ASSERT_TRUE(TestParse(input));
374
375 Macro* macro = test::GetValue<Macro>(&table_, "macro/foo");
376 ASSERT_THAT(macro, NotNull());
377 EXPECT_THAT(macro->raw_value, Eq("\nThis being human is a guest house."));
378 EXPECT_THAT(macro->style_string.str, Eq(" This being human is a guest house."));
379 EXPECT_THAT(macro->style_string.spans.size(), Eq(1));
380 EXPECT_THAT(macro->style_string.spans[0].name, Eq("b"));
381 EXPECT_THAT(macro->style_string.spans[0].first_char, Eq(12));
382 EXPECT_THAT(macro->style_string.spans[0].last_char, Eq(16));
383 ASSERT_THAT(macro->untranslatable_sections.size(), Eq(1));
384 EXPECT_THAT(macro->untranslatable_sections[0].start, Eq(12));
385 EXPECT_THAT(macro->untranslatable_sections[0].end, Eq(17));
386 EXPECT_THAT(macro->alias_namespaces, IsEmpty());
387}
388
389TEST_F(ResourceParserTest, ParseMacroNamespaces) {
390 std::string input = R"(<macro name="foo" xmlns:app="http://schemas.android.com/apk/res/android">
391@app:string/foo</macro>)";
392 ASSERT_TRUE(TestParse(input));
393
394 Macro* macro = test::GetValue<Macro>(&table_, "macro/foo");
395 ASSERT_THAT(macro, NotNull());
396 EXPECT_THAT(macro->raw_value, Eq("\n@app:string/foo"));
397 EXPECT_THAT(macro->style_string.str, Eq("@app:string/foo"));
398 EXPECT_THAT(macro->style_string.spans, IsEmpty());
399 EXPECT_THAT(macro->untranslatable_sections, IsEmpty());
400 EXPECT_THAT(macro->alias_namespaces.size(), Eq(1));
401 EXPECT_THAT(macro->alias_namespaces[0].alias, Eq("app"));
402 EXPECT_THAT(macro->alias_namespaces[0].package_name, Eq("android"));
403 EXPECT_THAT(macro->alias_namespaces[0].is_private, Eq(false));
404}
405
406TEST_F(ResourceParserTest, ParseMacroReference) {
407 std::string input = R"(<string name="res_string">@macro/foo</string>)";
408 ASSERT_TRUE(TestParse(input));
409
410 Reference* macro = test::GetValue<Reference>(&table_, "string/res_string");
411 ASSERT_THAT(macro, NotNull());
412 EXPECT_THAT(macro->type_flags, Eq(ResTable_map::TYPE_STRING));
413 EXPECT_THAT(macro->allow_raw, Eq(false));
414
415 input = R"(<style name="foo">
416 <item name="bar">@macro/foo</item>
417 </style>)";
418
419 ASSERT_TRUE(TestParse(input));
420 Style* style = test::GetValue<Style>(&table_, "style/foo");
421 ASSERT_THAT(style, NotNull());
422 EXPECT_THAT(style->entries.size(), Eq(1));
423
424 macro = ValueCast<Reference>(style->entries[0].value.get());
425 ASSERT_THAT(macro, NotNull());
426 EXPECT_THAT(macro->type_flags, Eq(0U));
427 EXPECT_THAT(macro->allow_raw, Eq(true));
428}
429
430TEST_F(ResourceParserTest, ParseMacroNoNameFail) {
431 std::string input = R"(<macro>12345</macro>)";
432 ASSERT_FALSE(TestParse(input));
433}
434
435TEST_F(ResourceParserTest, ParseMacroNonDefaultConfigurationFail) {
436 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
437 std::string input = R"(<macro name="foo">12345</macro>)";
438 ASSERT_FALSE(TestParse(input, watch_config));
439}
440
Adam Lesinskia45893a2017-05-30 15:19:02 -0700441// Old AAPT allowed attributes to be defined under different configurations, but ultimately
442// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700443TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700445 std::string input = R"(
446 <attr name="foo" />
447 <declare-styleable name="bar">
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700448 <attr name="baz" format="reference"/>
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700449 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800451
Adam Lesinskia45893a2017-05-30 15:19:02 -0700452 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
453 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
454 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800455
Adam Lesinskia45893a2017-05-30 15:19:02 -0700456 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
457 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
458 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800459}
460
Adam Lesinskia5870652015-11-20 15:32:30 -0800461TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700462 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800464
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700466 ASSERT_THAT(attr, NotNull());
467 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
468 EXPECT_THAT(attr->min_int, Eq(10));
469 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800470}
471
472TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700473 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800474}
475
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800476TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700477 std::string input = R"(
478 <declare-styleable name="Styleable">
479 <attr name="foo" />
480 </declare-styleable>
481 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700485 ASSERT_THAT(attr, NotNull());
486 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487}
488
489TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700490 std::string input = R"(
491 <declare-styleable name="Theme">
492 <attr name="foo" />
493 </declare-styleable>
494 <declare-styleable name="Window">
495 <attr name="foo" format="boolean"/>
496 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700500 ASSERT_THAT(attr, NotNull());
501 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502}
503
504TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700505 std::string input = R"(
506 <attr name="foo">
507 <enum name="bar" value="0"/>
Ryan Mitchellc1676802019-05-20 16:47:08 -0700508 <enum name="bat" value="0x1"/>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700509 <enum name="baz" value="2"/>
510 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700514 ASSERT_THAT(enum_attr, NotNull());
515 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
516 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517
Adam Lesinskia45893a2017-05-30 15:19:02 -0700518 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
519 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
520 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700521 EXPECT_THAT(enum_attr->symbols[0].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522
Adam Lesinskia45893a2017-05-30 15:19:02 -0700523 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
524 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
525 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700526 EXPECT_THAT(enum_attr->symbols[1].type, Eq(Res_value::TYPE_INT_HEX));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800527
Adam Lesinskia45893a2017-05-30 15:19:02 -0700528 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
529 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
530 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700531 EXPECT_THAT(enum_attr->symbols[2].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532}
533
534TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700535 std::string input = R"(
536 <attr name="foo">
537 <flag name="bar" value="0"/>
538 <flag name="bat" value="1"/>
539 <flag name="baz" value="2"/>
540 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700544 ASSERT_THAT(flag_attr, NotNull());
545 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
546 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800547
Adam Lesinskia45893a2017-05-30 15:19:02 -0700548 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
549 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
550 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800551
Adam Lesinskia45893a2017-05-30 15:19:02 -0700552 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
553 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
554 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800555
Adam Lesinskia45893a2017-05-30 15:19:02 -0700556 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
557 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
558 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 std::unique_ptr<BinaryPrimitive> flag_value =
561 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700562 ASSERT_THAT(flag_value, NotNull());
563 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800564}
565
566TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700567 std::string input = R"(
568 <attr name="foo">
569 <enum name="bar" value="0"/>
570 <enum name="bat" value="1"/>
571 <enum name="bat" value="2"/>
572 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574}
575
576TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700577 std::string input = R"(
578 <style name="foo" parent="@style/fu">
579 <item name="bar">#ffffffff</item>
580 <item name="bat">@string/hey</item>
581 <item name="baz"><b>hey</b></item>
582 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700586 ASSERT_THAT(style, NotNull());
587 ASSERT_TRUE(style->parent);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700588 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("style/fu")));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700589 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800590
Ryan Mitchell4382e442021-07-14 12:53:01 -0700591 EXPECT_THAT(style->entries[0].key.name, Eq(test::ParseNameOrDie("attr/bar")));
592 EXPECT_THAT(style->entries[1].key.name, Eq(test::ParseNameOrDie("attr/bat")));
593 EXPECT_THAT(style->entries[2].key.name, Eq(test::ParseNameOrDie("attr/baz")));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594}
595
Adam Lesinski769de982015-04-10 19:43:55 -0700596TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700597 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700600 ASSERT_THAT(style, NotNull());
601 ASSERT_TRUE(style->parent);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700602 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("com.app:style/Theme")));
Adam Lesinski769de982015-04-10 19:43:55 -0700603}
604
Adam Lesinski24aad162015-04-24 19:19:30 -0700605TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700606 std::string input = R"(
607 <style xmlns:app="http://schemas.android.com/apk/res/android"
608 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700612 ASSERT_THAT(style, NotNull());
613 ASSERT_TRUE(style->parent);
614 ASSERT_TRUE(style->parent.value().name);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700615 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("android:style/Theme")));
Adam Lesinski24aad162015-04-24 19:19:30 -0700616}
617
618TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700619 std::string input = R"(
620 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
621 <item name="app:bar">0</item>
622 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700626 ASSERT_THAT(style, NotNull());
627 ASSERT_THAT(style->entries, SizeIs(1));
Ryan Mitchell4382e442021-07-14 12:53:01 -0700628 EXPECT_THAT(style->entries[0].key.name, Eq(test::ParseNameOrDie("android:attr/bar")));
Adam Lesinski24aad162015-04-24 19:19:30 -0700629}
630
Ryan Mitchell633d7962018-06-11 15:29:21 -0700631TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) {
632 std::string input = R"(
633 <style name="foo">
634 <item name="bar">
635 com.helloworld.AppClass
636 </item>
637 </style>)";
638 ASSERT_TRUE(TestParse(input));
639
640 Style* style = test::GetValue<Style>(&table_, "style/foo");
641 ASSERT_THAT(style, NotNull());
642 EXPECT_THAT(style->entries[0].value, NotNull());
643 RawString* value = ValueCast<RawString>(style->entries[0].value.get());
644 EXPECT_THAT(value, NotNull());
645 EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)"));
646}
647
648
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700649TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700650 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700653 ASSERT_THAT(style, NotNull());
654 ASSERT_TRUE(style->parent);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700655 EXPECT_THAT(style->parent.value().name, Eq(test::ParseNameOrDie("style/foo")));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700657}
658
Adam Lesinskia45893a2017-05-30 15:19:02 -0700659TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
660 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700661
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700663 ASSERT_THAT(style, NotNull());
664 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700666}
667
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800668TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700669 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800670
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700672 ASSERT_THAT(style, NotNull());
673 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800675}
676
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800677TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700678 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
679 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800680}
681
682TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700683 std::string input = R"(
684 <declare-styleable name="foo">
685 <attr name="bar" />
686 <attr name="bat" format="string|reference"/>
687 <attr name="baz">
688 <enum name="foo" value="1"/>
689 </attr>
690 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800692
Ryan Mitchell4382e442021-07-14 12:53:01 -0700693 std::optional<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700695 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800696 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800697
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700699 ASSERT_THAT(attr, IsNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800700
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700702 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700706 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700708 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700709
Adam Lesinskia45893a2017-05-30 15:19:02 -0700710 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700711
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700713 ASSERT_THAT(styleable, NotNull());
714 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800715
Ryan Mitchell4382e442021-07-14 12:53:01 -0700716 EXPECT_THAT(styleable->entries[0].name, Eq(test::ParseNameOrDie("attr/bar")));
717 EXPECT_THAT(styleable->entries[1].name, Eq(test::ParseNameOrDie("attr/bat")));
718 EXPECT_THAT(styleable->entries[2].name, Eq(test::ParseNameOrDie("attr/baz")));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800719}
720
Donald Chai94e4a012020-01-06 13:52:41 -0800721TEST_F(ResourceParserTest, ParseDeclareStyleablePreservingVisibility) {
722 StringInputStream input(R"(
723 <resources>
724 <declare-styleable name="foo">
725 <attr name="myattr" />
726 </declare-styleable>
727 <declare-styleable name="bar">
728 <attr name="myattr" />
729 </declare-styleable>
730 <public type="styleable" name="bar" />
731 </resources>)");
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000732 ResourceParser parser(context_->GetDiagnostics(), &table_, android::Source{"test"},
Donald Chai94e4a012020-01-06 13:52:41 -0800733 ConfigDescription::DefaultConfig(),
734 ResourceParserOptions{.preserve_visibility_of_styleables = true});
735
736 xml::XmlPullParser xml_parser(&input);
737 ASSERT_TRUE(parser.Parse(&xml_parser));
738
739 EXPECT_EQ(
740 table_.FindResource(test::ParseNameOrDie("styleable/foo")).value().entry->visibility.level,
741 Visibility::Level::kUndefined);
742 EXPECT_EQ(
743 table_.FindResource(test::ParseNameOrDie("styleable/bar")).value().entry->visibility.level,
744 Visibility::Level::kPublic);
745}
746
Adam Lesinski467f1712015-11-16 17:35:44 -0800747TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700748 std::string input = R"(
749 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
750 name="foo">
751 <attr name="*android:bar" />
752 <attr name="privAndroid:bat" />
753 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 ASSERT_TRUE(TestParse(input));
755 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700756 ASSERT_THAT(styleable, NotNull());
757 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700760 ASSERT_TRUE(styleable->entries[0].name);
761 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700764 ASSERT_TRUE(styleable->entries[1].name);
765 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800766}
767
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800768TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700769 std::string input = R"(
770 <array name="foo">
771 <item>@string/ref</item>
772 <item>hey</item>
773 <item>23</item>
774 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800776
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700778 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700779 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800780
Adam Lesinski4ffea042017-08-10 15:37:28 -0700781 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
782 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
783 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784}
785
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700786TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700787 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700788 <string-array name="foo">
789 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700790 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700792 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700793}
794
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700795TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700796 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700797 <array name="foo" format="string">
798 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700799 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700800 ASSERT_TRUE(TestParse(input));
801
802 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700803 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700804 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700805
Adam Lesinski4ffea042017-08-10 15:37:28 -0700806 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700807 ASSERT_THAT(str, NotNull());
808 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700809}
810
811TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700812 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700813 <array name="foo" format="integer">
814 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700815 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700816 ASSERT_FALSE(TestParse(input));
817}
818
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800819TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700820 std::string input = R"(
821 <plurals name="foo">
822 <item quantity="other">apples</item>
823 <item quantity="one">apple</item>
824 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700825 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800826
827 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700828 ASSERT_THAT(plural, NotNull());
829 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
830 EXPECT_THAT(plural->values[Plural::Two], IsNull());
831 EXPECT_THAT(plural->values[Plural::Few], IsNull());
832 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800833
Adam Lesinskia45893a2017-05-30 15:19:02 -0700834 EXPECT_THAT(plural->values[Plural::One], NotNull());
835 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800836}
837
838TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700839 std::string input = R"(
840 <!--This is a comment-->
841 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800843
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700845 ASSERT_THAT(value, NotNull());
846 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700847}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700848
Adam Lesinskie78fd612015-10-22 12:48:43 -0700849TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700850 std::string input = R"(
851 <!--One-->
852 <!--Two-->
853 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700858 ASSERT_THAT(value, NotNull());
859 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700860}
861
862TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700863 std::string input = R"(
864 <!--One-->
865 <string name="foo">
866 Hi
867 <!--Two-->
868 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700872 ASSERT_THAT(value, NotNull());
873 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800874}
875
Adam Lesinskica5638f2015-10-21 14:42:43 -0700876TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700878 // comments from those end up in R.java
879 std::string input = R"(
880 <declare-styleable name="foo">
881 <!-- The name of the bar -->
882 <attr name="barName" format="string|reference" />
883 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700884
Adam Lesinskia45893a2017-05-30 15:19:02 -0700885 <attr name="foo">
886 <!-- The very first -->
887 <enum name="one" value="1" />
888 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700889 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700892 ASSERT_THAT(styleable, NotNull());
893 ASSERT_THAT(styleable->entries, SizeIs(1));
894 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700895
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700897 ASSERT_THAT(attr, NotNull());
898 ASSERT_THAT(attr->symbols, SizeIs(1));
899 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700900}
901
Adam Lesinskia45893a2017-05-30 15:19:02 -0700902// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800903TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700904 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
905 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906}
907
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800908TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700909 std::string input = R"(
910 <string name="foo" product="phone">hi</string>
911 <string name="foo" product="no-sdcard">ho</string>
912 <string name="bar" product="">wee</string>
913 <string name="baz">woo</string>
914 <string name="bit" product="phablet">hoot</string>
915 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700917
Adam Lesinskia45893a2017-05-30 15:19:02 -0700918 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
919 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
920 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
921 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
922 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
923 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700924}
925
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800926TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700927 std::string input = R"(
928 <public-group type="attr" first-id="0x01010040">
929 <public name="foo" />
930 <public name="bar" />
931 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800933
Ryan Mitchell4382e442021-07-14 12:53:01 -0700934 std::optional<ResourceTable::SearchResult> result =
935 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700936 ASSERT_TRUE(result);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700937 ASSERT_TRUE(result.value().entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700938 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700941 ASSERT_TRUE(result);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700942 ASSERT_TRUE(result.value().entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700943 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800944}
945
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700946TEST_F(ResourceParserTest, StagingPublicGroup) {
947 std::string input = R"(
948 <staging-public-group type="attr" first-id="0x01ff0049">
949 <public name="foo" />
950 <public name="bar" />
951 </staging-public-group>)";
952 ASSERT_TRUE(TestParse(input));
953
Ryan Mitchell4382e442021-07-14 12:53:01 -0700954 std::optional<ResourceTable::SearchResult> result =
955 table_.FindResource(test::ParseNameOrDie("attr/foo"));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700956 ASSERT_TRUE(result);
957
958 ASSERT_TRUE(result.value().entry->id);
959 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01ff0049)));
960 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
961 EXPECT_TRUE(result.value().entry->visibility.staged_api);
962
963 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
964 ASSERT_TRUE(result);
965
966 ASSERT_TRUE(result.value().entry->id);
967 EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01ff004a)));
968 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
969 EXPECT_TRUE(result.value().entry->visibility.staged_api);
970}
971
Adam Lesinski71be7052017-12-12 16:48:07 -0800972TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
973 std::string input = R"(
974 <!-- private -->
975 <java-symbol type="string" name="foo" />
976 <!-- public -->
977 <public type="string" name="foo" id="0x01020000" />
978 <!-- private2 -->
979 <java-symbol type="string" name="foo" />)";
980 ASSERT_TRUE(TestParse(input));
981
Ryan Mitchell4382e442021-07-14 12:53:01 -0700982 std::optional<ResourceTable::SearchResult> result =
Adam Lesinski71be7052017-12-12 16:48:07 -0800983 table_.FindResource(test::ParseNameOrDie("string/foo"));
984 ASSERT_TRUE(result);
985
986 ResourceEntry* entry = result.value().entry;
987 ASSERT_THAT(entry, NotNull());
988 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
989 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
990}
991
Adam Lesinskifa105052015-11-07 13:34:39 -0800992TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700993 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
994 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800995}
996
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700997TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700998 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800999
Ryan Mitchell4382e442021-07-14 12:53:01 -07001000 std::optional<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -07001002 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -07001004 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -08001005 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
1006 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001007}
1008
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001009TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001010 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001011
Adam Lesinskie597d682017-06-01 17:16:44 -07001012 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
1013 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001014 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001015
Adam Lesinskia45893a2017-05-30 15:19:02 -07001016 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -07001017}
1018
1019// An <item> without a format specifier accepts all types of values.
1020TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001021 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -07001022
1023 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
1024 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001025 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001026}
1027
Adam Lesinski86d67df2017-01-31 13:47:27 -08001028TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001029 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
1030 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -08001031}
1032
1033TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001034 std::string input = R"(
1035 <bag name="bag" type="configVarying">
1036 <item name="test">Hello!</item>
1037 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -08001038 ASSERT_TRUE(TestParse(input));
1039
1040 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -07001041 ASSERT_THAT(val, NotNull());
1042 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -08001043
Adam Lesinskia45893a2017-05-30 15:19:02 -07001044 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
1045 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -08001046}
1047
Adam Lesinskibab4ef52017-06-01 15:22:57 -07001048TEST_F(ResourceParserTest, ParseElementWithNoValue) {
1049 std::string input = R"(
1050 <item type="drawable" format="reference" name="foo" />
1051 <string name="foo" />)";
1052 ASSERT_TRUE(TestParse(input));
1053 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
1054
1055 String* str = test::GetValue<String>(&table_, "string/foo");
1056 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -07001057 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -07001058}
1059
Adam Lesinskib9f05482017-06-02 16:32:37 -07001060TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -07001061 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -07001062}
1063
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001064TEST_F(ResourceParserTest, ParseOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001065 std::string input = R"(
1066 <overlayable name="Name" actor="overlay://theme">
Winsonb2d7f532019-02-04 16:32:43 -08001067 <policy type="signature">
1068 <item type="string" name="foo" />
1069 <item type="drawable" name="bar" />
1070 </policy>
Adam Lesinski46c4d722017-08-23 13:03:56 -07001071 </overlayable>)";
1072 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -08001073
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001074 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
Adam Lesinski71be7052017-12-12 16:48:07 -08001075 ASSERT_TRUE(search_result);
1076 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001077 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1078 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
1079 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1080 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -08001081 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001082
1083 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
1084 ASSERT_TRUE(search_result);
1085 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001086 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1087 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1088 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1089 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -08001090 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001091}
1092
1093TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
1094 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
1095 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
1096 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
1097}
1098
1099TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
1100 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001101}
1102
1103TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001104 std::string input = R"(
1105 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001106 <policy type="product">
1107 <item type="string" name="bar" />
1108 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001109 <policy type="system">
1110 <item type="string" name="fiz" />
1111 </policy>
1112 <policy type="vendor">
1113 <item type="string" name="fuz" />
1114 </policy>
1115 <policy type="public">
1116 <item type="string" name="faz" />
1117 </policy>
Winsonb2d7f532019-02-04 16:32:43 -08001118 <policy type="signature">
1119 <item type="string" name="foz" />
1120 </policy>
Ryan Mitchell939df092019-04-09 17:13:50 -07001121 <policy type="odm">
1122 <item type="string" name="biz" />
1123 </policy>
1124 <policy type="oem">
1125 <item type="string" name="buz" />
1126 </policy>
Winsonf56ade32019-12-04 11:32:41 -08001127 <policy type="actor">
1128 <item type="string" name="actor" />
1129 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001130 </overlayable>)";
1131 ASSERT_TRUE(TestParse(input));
1132
Winsonb2d7f532019-02-04 16:32:43 -08001133 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001134 ASSERT_TRUE(search_result);
1135 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001136 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1137 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1138 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001139 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001140
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001141 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
1142 ASSERT_TRUE(search_result);
1143 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001144 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1145 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1146 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001147 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SYSTEM_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001148
1149 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
1150 ASSERT_TRUE(search_result);
1151 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001152 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1153 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1154 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001155 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001156
1157 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
1158 ASSERT_TRUE(search_result);
1159 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001160 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1161 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1162 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001163 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PUBLIC));
Winsonb2d7f532019-02-04 16:32:43 -08001164
1165 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
1166 ASSERT_TRUE(search_result);
1167 ASSERT_THAT(search_result.value().entry, NotNull());
1168 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1169 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1170 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001171 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell939df092019-04-09 17:13:50 -07001172
1173 search_result = table_.FindResource(test::ParseNameOrDie("string/biz"));
1174 ASSERT_TRUE(search_result);
1175 ASSERT_THAT(search_result.value().entry, NotNull());
1176 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1177 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1178 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001179 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ODM_PARTITION));
Ryan Mitchell939df092019-04-09 17:13:50 -07001180
1181 search_result = table_.FindResource(test::ParseNameOrDie("string/buz"));
1182 ASSERT_TRUE(search_result);
1183 ASSERT_THAT(search_result.value().entry, NotNull());
1184 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1185 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1186 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001187 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::OEM_PARTITION));
Winsonf56ade32019-12-04 11:32:41 -08001188
1189 search_result = table_.FindResource(test::ParseNameOrDie("string/actor"));
1190 ASSERT_TRUE(search_result);
1191 ASSERT_THAT(search_result.value().entry, NotNull());
1192 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1193 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1194 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1195 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ACTOR_SIGNATURE));
Winsonb2d7f532019-02-04 16:32:43 -08001196}
1197
1198TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
1199 std::string input = R"(
1200 <overlayable name="Name">
1201 <item type="string" name="foo" />
1202 </overlayable>)";
1203 EXPECT_FALSE(TestParse(input));
1204
1205 input = R"(
1206 <overlayable name="Name">
1207 <policy>
1208 <item name="foo" />
1209 </policy>
1210 </overlayable>)";
1211 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001212}
1213
1214TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1215 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001216 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001217 <policy type="illegal_policy">
1218 <item type="string" name="foo" />
1219 </policy>
1220 </overlayable>)";
1221 EXPECT_FALSE(TestParse(input));
1222
1223 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001224 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001225 <policy type="product">
1226 <item name="foo" />
1227 </policy>
1228 </overlayable>)";
1229 EXPECT_FALSE(TestParse(input));
1230
1231 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001232 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001233 <policy type="vendor">
1234 <item type="string" />
1235 </policy>
1236 </overlayable>)";
1237 EXPECT_FALSE(TestParse(input));
1238}
1239
1240TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1241 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001242 <overlayable name="Name">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001243 <policy type="vendor|public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001244 <item type="string" name="foo" />
1245 </policy>
1246 <policy type="product|system">
1247 <item type="string" name="bar" />
1248 </policy>
1249 </overlayable>)";
1250 ASSERT_TRUE(TestParse(input));
1251
1252 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1253 ASSERT_TRUE(search_result);
1254 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001255 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1256 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1257 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001258 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION
1259 | PolicyFlags::PUBLIC));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001260
1261 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1262 ASSERT_TRUE(search_result);
1263 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001264 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1265 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1266 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001267 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
1268 | PolicyFlags::SYSTEM_PARTITION));
Adam Lesinski71be7052017-12-12 16:48:07 -08001269}
1270
1271TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1272 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001273 <overlayable name="Name">
Adam Lesinski71be7052017-12-12 16:48:07 -08001274 <item type="string" name="foo" />
1275 <item type="string" name="foo" />
1276 </overlayable>)";
1277 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001278
1279 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001280 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001281 <item type="string" name="foo" />
1282 </overlayable>
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001283 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001284 <item type="string" name="foo" />
1285 </overlayable>)";
1286 EXPECT_FALSE(TestParse(input));
1287
1288 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001289 <overlayable name="Name">
1290 <item type="string" name="foo" />
1291 </overlayable>
1292 <overlayable name="Other">
1293 <item type="string" name="foo" />
1294 </overlayable>)";
1295 EXPECT_FALSE(TestParse(input));
1296
1297 input = R"(
1298 <overlayable name="Name" actor="overlay://my.actor.one">
1299 <item type="string" name="foo" />
1300 </overlayable>
1301 <overlayable name="Other" actor="overlay://my.actor.two">
1302 <item type="string" name="foo" />
1303 </overlayable>)";
1304 EXPECT_FALSE(TestParse(input));
1305
1306 input = R"(
1307 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001308 <policy type="product">
1309 <item type="string" name="foo" />
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 Mitchelle4e989c2018-10-29 02:21:50 -07001317 <policy type="product">
1318 <item type="string" name="foo" />
1319 </policy>
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001320 <item type="string" name="foo" />
1321 </overlayable>)";
1322 EXPECT_FALSE(TestParse(input));
1323
1324 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001325 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001326 <policy type="product">
1327 <item type="string" name="foo" />
1328 </policy>
1329 <policy type="vendor">
1330 <item type="string" name="foo" />
1331 </policy>
1332 </overlayable>)";
1333 EXPECT_FALSE(TestParse(input));
1334
1335 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001336 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001337 <policy type="product">
1338 <item type="string" name="foo" />
1339 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001340 </overlayable>
1341
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001342 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001343 <policy type="product">
1344 <item type="string" name="foo" />
1345 </policy>
1346 </overlayable>)";
1347 EXPECT_FALSE(TestParse(input));
1348}
1349
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001350TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1351 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001352 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001353 <policy type="vendor|product">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001354 <policy type="public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001355 <item type="string" name="foo" />
1356 </policy>
1357 </policy>
1358 </overlayable>)";
1359 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001360}
1361
y9efbbef2018-04-18 11:29:09 -07001362TEST_F(ResourceParserTest, ParseIdItem) {
1363 std::string input = R"(
1364 <item name="foo" type="id">@id/bar</item>
1365 <item name="bar" type="id"/>
1366 <item name="baz" type="id"></item>)";
1367 ASSERT_TRUE(TestParse(input));
1368
1369 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1370 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1371 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1372
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001373 input = R"(
1374 <id name="foo2">@id/bar</id>
1375 <id name="bar2"/>
1376 <id name="baz2"></id>)";
1377 ASSERT_TRUE(TestParse(input));
1378
1379 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1380 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1381 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1382
y9efbbef2018-04-18 11:29:09 -07001383 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001384 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -07001385 ASSERT_FALSE(TestParse(input));
1386
1387 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001388 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -07001389 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001390 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -07001391 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001392 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -07001393 ASSERT_FALSE(TestParse(input));
1394
1395 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001396 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -07001397 ASSERT_FALSE(TestParse(input));
1398}
1399
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001400TEST_F(ResourceParserTest, ParseCData) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001401 // Double quotes should still change the state of whitespace processing
1402 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001403 ASSERT_TRUE(TestParse(input));
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001404 auto output = test::GetValue<String>(&table_, "string/foo");
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001405 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001406 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001407
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001408 input = R"(<string name="foo2"><![CDATA[Hello
1409 World]]></string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001410 ASSERT_TRUE(TestParse(input));
1411 output = test::GetValue<String>(&table_, "string/foo2");
1412 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001413 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001414
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001415 // Cdata blocks should have their whitespace trimmed
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001416 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1417 ASSERT_TRUE(TestParse(input));
1418 output = test::GetValue<String>(&table_, "string/foo3");
1419 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001420 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001421
1422 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1423 ASSERT_TRUE(TestParse(input));
1424 output = test::GetValue<String>(&table_, "string/foo4");
1425 ASSERT_THAT(output, NotNull());
1426 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1427
1428 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1429 ASSERT_TRUE(TestParse(input));
1430 output = test::GetValue<String>(&table_, "string/foo5");
1431 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001432 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1433
1434 // Single quotes must still be escaped
1435 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1436 ASSERT_FALSE(TestParse(input));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001437}
1438
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439} // namespace aapt