blob: 24531bc164459ef55b71e956e16a741615b01643 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <sstream>
20#include <string>
21
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "ResourceValues.h"
Adam Lesinski00451162017-10-03 07:44:08 -070025#include "io/StringStream.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070026#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080027#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029using ::aapt::io::StringInputStream;
Adam Lesinskia45893a2017-05-30 15:19:02 -070030using ::aapt::test::StrValueEq;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070031using ::aapt::test::ValueEq;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
Adam Lesinskia45893a2017-05-30 15:19:02 -070033using ::android::Res_value;
Adam Lesinski71be7052017-12-12 16:48:07 -080034using ::android::ResTable_map;
Adam Lesinskie597d682017-06-01 17:16:44 -070035using ::android::StringPiece;
36using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070037using ::testing::IsEmpty;
38using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070039using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070040using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070041using ::testing::SizeIs;
Adam Lesinski71be7052017-12-12 16:48:07 -080042using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080043
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
45
Adam Lesinskibab4ef52017-06-01 15:22:57 -070046constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070052
53 std::string input = kXmlPreamble;
54 input += R"(<attr name="foo"/>)";
55 StringInputStream in(input);
56 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060class ResourceParserTest : public ::testing::Test {
61 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070062 void SetUp() override {
63 context_ = test::ContextBuilder().Build();
64 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 ::testing::AssertionResult TestParse(const StringPiece& str) {
67 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
Adam Lesinski52364f72016-01-11 13:10:24 -080069
Adam Lesinskibab4ef52017-06-01 15:22:57 -070070 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070072 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
73 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070074
75 std::string input = kXmlPreamble;
76 input += "<resources>\n";
77 input.append(str.data(), str.size());
78 input += "\n</resources>";
79 StringInputStream in(input);
80 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return ::testing::AssertionFailure();
85 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086
87 protected:
88 ResourceTable table_;
89 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090};
91
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070093 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070096 ASSERT_THAT(str, NotNull());
97 EXPECT_THAT(*str, StrValueEq(" hey there "));
98 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -080099
100 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
101 str = test::GetValue<String>(&table_, "string/bar");
102 ASSERT_THAT(str, NotNull());
103 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
104
105 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
106 str = test::GetValue<String>(&table_, "string/baz");
107 ASSERT_THAT(str, NotNull());
108 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109}
110
111TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700112 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700115 ASSERT_THAT(str, NotNull());
116 EXPECT_THAT(*str, StrValueEq("?123"));
117 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700118
119 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
120 str = test::GetValue<String>(&table_, "string/bar");
121 ASSERT_THAT(str, NotNull());
122 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123}
124
Adam Lesinski9f222042015-11-04 13:51:45 -0800125TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700126 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
127 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800128}
129
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700130TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800132 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700134 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700138 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700139
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800140 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700141 EXPECT_THAT(str->value->spans, SizeIs(2));
142 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700143
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800144 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
145 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700146 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700147
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800148 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
149 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700150 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700151}
152
153TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700154 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700157 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800158 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700159 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700160
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700164 ASSERT_THAT(str, NotNull());
165 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700166}
167
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700168TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
169 // Tuncate leading and trailing whitespace
170 EXPECT_TRUE(TestParse(R"(<string name="foo">&#32;Hello&#32;</string>)"));
171
172 String* str = test::GetValue<String>(&table_, "string/foo");
173 ASSERT_THAT(str, NotNull());
174 EXPECT_THAT(*str->value, StrEq("Hello"));
175 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
176
177 // AAPT does not truncate unicode whitespace
178 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
179
180 str = test::GetValue<String>(&table_, "string/foo2");
181 ASSERT_THAT(str, NotNull());
182 EXPECT_THAT(*str->value, StrEq(" Hello "));
183 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
184
185 // Preserve non-ASCII whitespace including extended ASCII characters
Ryan Mitchell0f326752019-03-11 11:00:25 -0700186 EXPECT_TRUE(TestParse(R"(<string name="foo3">&#160;Hello&#x202F;World&#160;</string>)"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700187
188 str = test::GetValue<String>(&table_, "string/foo3");
189 ASSERT_THAT(str, NotNull());
Ryan Mitchell0f326752019-03-11 11:00:25 -0700190 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700191 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
192
193 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
194
195 str = test::GetValue<String>(&table_, "string/foo4");
196 ASSERT_THAT(str, NotNull());
197 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
198 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
199}
200
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800201TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
202 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
203 ASSERT_TRUE(TestParse(input));
204
205 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
206 ASSERT_THAT(str, NotNull());
207 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
208 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
209
210 ASSERT_THAT(str->value->spans, SizeIs(2u));
211 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
212 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
213 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
214
215 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
216 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
217 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
218}
219
Mihai Nitad1a65212019-03-26 13:47:45 -0700220TEST_F(ResourceParserTest, ParseStringTranslatableAttribute) {
221 // If there is no translate attribute the default is 'true'
222 EXPECT_TRUE(TestParse(R"(<string name="foo1">Translate</string>)"));
223 String* str = test::GetValue<String>(&table_, "string/foo1");
224 ASSERT_THAT(str, NotNull());
225 ASSERT_TRUE(str->IsTranslatable());
226
227 // Explicit 'true' translate attribute
228 EXPECT_TRUE(TestParse(R"(<string name="foo2" translatable="true">Translate</string>)"));
229 str = test::GetValue<String>(&table_, "string/foo2");
230 ASSERT_THAT(str, NotNull());
231 ASSERT_TRUE(str->IsTranslatable());
232
233 // Explicit 'false' translate attribute
234 EXPECT_TRUE(TestParse(R"(<string name="foo3" translatable="false">Do not translate</string>)"));
235 str = test::GetValue<String>(&table_, "string/foo3");
236 ASSERT_THAT(str, NotNull());
237 ASSERT_FALSE(str->IsTranslatable());
238
239 // Invalid value for the translate attribute, should be boolean ('true' or 'false')
240 EXPECT_FALSE(TestParse(R"(<string name="foo4" translatable="yes">Translate</string>)"));
241}
242
Adam Lesinski75421622017-01-06 15:20:04 -0800243TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700244 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800245 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700246 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800247 ASSERT_TRUE(TestParse(input));
248
249 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700250 ASSERT_THAT(str, NotNull());
251 EXPECT_THAT(*str, StrValueEq("There are no apples"));
252 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800253}
254
255TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700256 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800257 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700258 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800259 EXPECT_FALSE(TestParse(input));
260}
261
262TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700263 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800264 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700265 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700269 ASSERT_THAT(str, NotNull());
270 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800271
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800272 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
273 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700274 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800275}
276
277TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700278 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800279 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700280 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800281 ASSERT_TRUE(TestParse(input));
282
283 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700284 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800285 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800286
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800287 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
288 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
289 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
290
291 ASSERT_THAT(str->value->spans, SizeIs(1u));
292 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
293 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
294 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295}
296
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700297TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700298 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700300
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
302 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800303 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700305 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
306 ASSERT_THAT(null_ref, NotNull());
307 EXPECT_FALSE(null_ref->name);
308 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700309 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700310}
311
312TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700313 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700315
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700316 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700317 ASSERT_THAT(integer, NotNull());
318 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
319 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700320}
321
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700323 std::string input = R"(
324 <attr name="foo" format="string"/>
325 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700329 ASSERT_THAT(attr, NotNull());
330 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700333 ASSERT_THAT(attr, NotNull());
334 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335}
336
Adam Lesinskia45893a2017-05-30 15:19:02 -0700337// Old AAPT allowed attributes to be defined under different configurations, but ultimately
338// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700339TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700341 std::string input = R"(
342 <attr name="foo" />
343 <declare-styleable name="bar">
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700344 <attr name="baz" format="reference"/>
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700345 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800347
Adam Lesinskia45893a2017-05-30 15:19:02 -0700348 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
349 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
350 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800351
Adam Lesinskia45893a2017-05-30 15:19:02 -0700352 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
353 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
354 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800355}
356
Adam Lesinskia5870652015-11-20 15:32:30 -0800357TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700358 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700362 ASSERT_THAT(attr, NotNull());
363 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
364 EXPECT_THAT(attr->min_int, Eq(10));
365 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800366}
367
368TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700369 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800370}
371
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700373 std::string input = R"(
374 <declare-styleable name="Styleable">
375 <attr name="foo" />
376 </declare-styleable>
377 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700381 ASSERT_THAT(attr, NotNull());
382 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383}
384
385TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700386 std::string input = R"(
387 <declare-styleable name="Theme">
388 <attr name="foo" />
389 </declare-styleable>
390 <declare-styleable name="Window">
391 <attr name="foo" format="boolean"/>
392 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700396 ASSERT_THAT(attr, NotNull());
397 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800398}
399
400TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700401 std::string input = R"(
402 <attr name="foo">
403 <enum name="bar" value="0"/>
Ryan Mitchellc1676802019-05-20 16:47:08 -0700404 <enum name="bat" value="0x1"/>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700405 <enum name="baz" value="2"/>
406 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700410 ASSERT_THAT(enum_attr, NotNull());
411 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
412 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413
Adam Lesinskia45893a2017-05-30 15:19:02 -0700414 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
415 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
416 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700417 EXPECT_THAT(enum_attr->symbols[0].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418
Adam Lesinskia45893a2017-05-30 15:19:02 -0700419 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
420 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
421 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700422 EXPECT_THAT(enum_attr->symbols[1].type, Eq(Res_value::TYPE_INT_HEX));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800423
Adam Lesinskia45893a2017-05-30 15:19:02 -0700424 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
425 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
426 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700427 EXPECT_THAT(enum_attr->symbols[2].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428}
429
430TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700431 std::string input = R"(
432 <attr name="foo">
433 <flag name="bar" value="0"/>
434 <flag name="bat" value="1"/>
435 <flag name="baz" value="2"/>
436 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700440 ASSERT_THAT(flag_attr, NotNull());
441 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
442 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800443
Adam Lesinskia45893a2017-05-30 15:19:02 -0700444 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
445 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
446 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800447
Adam Lesinskia45893a2017-05-30 15:19:02 -0700448 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
449 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
450 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800451
Adam Lesinskia45893a2017-05-30 15:19:02 -0700452 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
453 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
454 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 std::unique_ptr<BinaryPrimitive> flag_value =
457 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700458 ASSERT_THAT(flag_value, NotNull());
459 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800460}
461
462TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700463 std::string input = R"(
464 <attr name="foo">
465 <enum name="bar" value="0"/>
466 <enum name="bat" value="1"/>
467 <enum name="bat" value="2"/>
468 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470}
471
472TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700473 std::string input = R"(
474 <style name="foo" parent="@style/fu">
475 <item name="bar">#ffffffff</item>
476 <item name="bat">@string/hey</item>
477 <item name="baz"><b>hey</b></item>
478 </style>)";
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 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700482 ASSERT_THAT(style, NotNull());
483 ASSERT_TRUE(style->parent);
484 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
485 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486
Adam Lesinskia45893a2017-05-30 15:19:02 -0700487 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
488 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
489 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490}
491
Adam Lesinski769de982015-04-10 19:43:55 -0700492TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700493 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700496 ASSERT_THAT(style, NotNull());
497 ASSERT_TRUE(style->parent);
498 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700499}
500
Adam Lesinski24aad162015-04-24 19:19:30 -0700501TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700502 std::string input = R"(
503 <style xmlns:app="http://schemas.android.com/apk/res/android"
504 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700506
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700508 ASSERT_THAT(style, NotNull());
509 ASSERT_TRUE(style->parent);
510 ASSERT_TRUE(style->parent.value().name);
511 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700512}
513
514TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700515 std::string input = R"(
516 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
517 <item name="app:bar">0</item>
518 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700522 ASSERT_THAT(style, NotNull());
523 ASSERT_THAT(style->entries, SizeIs(1));
524 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700525}
526
Ryan Mitchell633d7962018-06-11 15:29:21 -0700527TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) {
528 std::string input = R"(
529 <style name="foo">
530 <item name="bar">
531 com.helloworld.AppClass
532 </item>
533 </style>)";
534 ASSERT_TRUE(TestParse(input));
535
536 Style* style = test::GetValue<Style>(&table_, "style/foo");
537 ASSERT_THAT(style, NotNull());
538 EXPECT_THAT(style->entries[0].value, NotNull());
539 RawString* value = ValueCast<RawString>(style->entries[0].value.get());
540 EXPECT_THAT(value, NotNull());
541 EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)"));
542}
543
544
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700545TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700546 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700549 ASSERT_THAT(style, NotNull());
550 ASSERT_TRUE(style->parent);
551 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700553}
554
Adam Lesinskia45893a2017-05-30 15:19:02 -0700555TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
556 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700559 ASSERT_THAT(style, NotNull());
560 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700562}
563
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800564TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700565 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -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);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800571}
572
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800573TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700574 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
575 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800576}
577
578TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700579 std::string input = R"(
580 <declare-styleable name="foo">
581 <attr name="bar" />
582 <attr name="bat" format="string|reference"/>
583 <attr name="baz">
584 <enum name="foo" value="1"/>
585 </attr>
586 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700587 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800588
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700591 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800592 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700595 ASSERT_THAT(attr, IsNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700598 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700602 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700604 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700605
Adam Lesinskia45893a2017-05-30 15:19:02 -0700606 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700609 ASSERT_THAT(styleable, NotNull());
610 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800611
Adam Lesinskia45893a2017-05-30 15:19:02 -0700612 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
613 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
614 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615}
616
Donald Chai94e4a012020-01-06 13:52:41 -0800617TEST_F(ResourceParserTest, ParseDeclareStyleablePreservingVisibility) {
618 StringInputStream input(R"(
619 <resources>
620 <declare-styleable name="foo">
621 <attr name="myattr" />
622 </declare-styleable>
623 <declare-styleable name="bar">
624 <attr name="myattr" />
625 </declare-styleable>
626 <public type="styleable" name="bar" />
627 </resources>)");
628 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
629 ConfigDescription::DefaultConfig(),
630 ResourceParserOptions{.preserve_visibility_of_styleables = true});
631
632 xml::XmlPullParser xml_parser(&input);
633 ASSERT_TRUE(parser.Parse(&xml_parser));
634
635 EXPECT_EQ(
636 table_.FindResource(test::ParseNameOrDie("styleable/foo")).value().entry->visibility.level,
637 Visibility::Level::kUndefined);
638 EXPECT_EQ(
639 table_.FindResource(test::ParseNameOrDie("styleable/bar")).value().entry->visibility.level,
640 Visibility::Level::kPublic);
641}
642
Adam Lesinski467f1712015-11-16 17:35:44 -0800643TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700644 std::string input = R"(
645 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
646 name="foo">
647 <attr name="*android:bar" />
648 <attr name="privAndroid:bat" />
649 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 ASSERT_TRUE(TestParse(input));
651 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700652 ASSERT_THAT(styleable, NotNull());
653 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700656 ASSERT_TRUE(styleable->entries[0].name);
657 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800658
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700660 ASSERT_TRUE(styleable->entries[1].name);
661 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800662}
663
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800664TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700665 std::string input = R"(
666 <array name="foo">
667 <item>@string/ref</item>
668 <item>hey</item>
669 <item>23</item>
670 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800672
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700674 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700675 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800676
Adam Lesinski4ffea042017-08-10 15:37:28 -0700677 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
678 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
679 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800680}
681
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700682TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700683 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700684 <string-array name="foo">
685 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700686 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700688 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700689}
690
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700691TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700692 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700693 <array name="foo" format="string">
694 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700695 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700696 ASSERT_TRUE(TestParse(input));
697
698 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700699 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700700 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700701
Adam Lesinski4ffea042017-08-10 15:37:28 -0700702 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700703 ASSERT_THAT(str, NotNull());
704 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700705}
706
707TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700708 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700709 <array name="foo" format="integer">
710 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700711 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700712 ASSERT_FALSE(TestParse(input));
713}
714
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800715TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700716 std::string input = R"(
717 <plurals name="foo">
718 <item quantity="other">apples</item>
719 <item quantity="one">apple</item>
720 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800722
723 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700724 ASSERT_THAT(plural, NotNull());
725 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
726 EXPECT_THAT(plural->values[Plural::Two], IsNull());
727 EXPECT_THAT(plural->values[Plural::Few], IsNull());
728 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800729
Adam Lesinskia45893a2017-05-30 15:19:02 -0700730 EXPECT_THAT(plural->values[Plural::One], NotNull());
731 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732}
733
734TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700735 std::string input = R"(
736 <!--This is a comment-->
737 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700738 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800739
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700741 ASSERT_THAT(value, NotNull());
742 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700743}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700744
Adam Lesinskie78fd612015-10-22 12:48:43 -0700745TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700746 std::string input = R"(
747 <!--One-->
748 <!--Two-->
749 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700750
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700752
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700754 ASSERT_THAT(value, NotNull());
755 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700756}
757
758TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700759 std::string input = R"(
760 <!--One-->
761 <string name="foo">
762 Hi
763 <!--Two-->
764 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700766
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700768 ASSERT_THAT(value, NotNull());
769 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800770}
771
Adam Lesinskica5638f2015-10-21 14:42:43 -0700772TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700774 // comments from those end up in R.java
775 std::string input = R"(
776 <declare-styleable name="foo">
777 <!-- The name of the bar -->
778 <attr name="barName" format="string|reference" />
779 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700780
Adam Lesinskia45893a2017-05-30 15:19:02 -0700781 <attr name="foo">
782 <!-- The very first -->
783 <enum name="one" value="1" />
784 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700785 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700786
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700788 ASSERT_THAT(styleable, NotNull());
789 ASSERT_THAT(styleable->entries, SizeIs(1));
790 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700791
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700793 ASSERT_THAT(attr, NotNull());
794 ASSERT_THAT(attr->symbols, SizeIs(1));
795 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700796}
797
Adam Lesinskia45893a2017-05-30 15:19:02 -0700798// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800799TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700800 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
801 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800802}
803
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800804TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700805 std::string input = R"(
806 <string name="foo" product="phone">hi</string>
807 <string name="foo" product="no-sdcard">ho</string>
808 <string name="bar" product="">wee</string>
809 <string name="baz">woo</string>
810 <string name="bit" product="phablet">hoot</string>
811 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700813
Adam Lesinskia45893a2017-05-30 15:19:02 -0700814 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
815 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
816 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
817 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
818 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
819 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700820}
821
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800822TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700823 std::string input = R"(
824 <public-group type="attr" first-id="0x01010040">
825 <public name="foo" />
826 <public name="bar" />
827 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800829
Adam Lesinskia45893a2017-05-30 15:19:02 -0700830 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
831 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800832
Adam Lesinskia45893a2017-05-30 15:19:02 -0700833 ASSERT_TRUE(result.value().package->id);
834 ASSERT_TRUE(result.value().type->id);
835 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 ResourceId actual_id(result.value().package->id.value(),
837 result.value().type->id.value(),
838 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700839 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700842 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843
Adam Lesinskia45893a2017-05-30 15:19:02 -0700844 ASSERT_TRUE(result.value().package->id);
845 ASSERT_TRUE(result.value().type->id);
846 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 actual_id = ResourceId(result.value().package->id.value(),
848 result.value().type->id.value(),
849 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700850 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800851}
852
Adam Lesinski71be7052017-12-12 16:48:07 -0800853TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
854 std::string input = R"(
855 <!-- private -->
856 <java-symbol type="string" name="foo" />
857 <!-- public -->
858 <public type="string" name="foo" id="0x01020000" />
859 <!-- private2 -->
860 <java-symbol type="string" name="foo" />)";
861 ASSERT_TRUE(TestParse(input));
862
863 Maybe<ResourceTable::SearchResult> result =
864 table_.FindResource(test::ParseNameOrDie("string/foo"));
865 ASSERT_TRUE(result);
866
867 ResourceEntry* entry = result.value().entry;
868 ASSERT_THAT(entry, NotNull());
869 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
870 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
871}
872
Adam Lesinskifa105052015-11-07 13:34:39 -0800873TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700874 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
875 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800876}
877
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700878TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700879 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800880
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700882 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700883 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700885 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800886 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
887 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800888}
889
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800890TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700891 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800892
Adam Lesinskie597d682017-06-01 17:16:44 -0700893 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
894 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700895 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800896
Adam Lesinskia45893a2017-05-30 15:19:02 -0700897 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700898}
899
900// An <item> without a format specifier accepts all types of values.
901TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700902 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700903
904 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
905 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700906 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800907}
908
Adam Lesinski86d67df2017-01-31 13:47:27 -0800909TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700910 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
911 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800912}
913
914TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700915 std::string input = R"(
916 <bag name="bag" type="configVarying">
917 <item name="test">Hello!</item>
918 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800919 ASSERT_TRUE(TestParse(input));
920
921 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700922 ASSERT_THAT(val, NotNull());
923 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800924
Adam Lesinskia45893a2017-05-30 15:19:02 -0700925 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
926 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800927}
928
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700929TEST_F(ResourceParserTest, ParseElementWithNoValue) {
930 std::string input = R"(
931 <item type="drawable" format="reference" name="foo" />
932 <string name="foo" />)";
933 ASSERT_TRUE(TestParse(input));
934 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
935
936 String* str = test::GetValue<String>(&table_, "string/foo");
937 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700938 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700939}
940
Adam Lesinskib9f05482017-06-02 16:32:37 -0700941TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700942 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700943}
944
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700945TEST_F(ResourceParserTest, ParseOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800946 std::string input = R"(
947 <overlayable name="Name" actor="overlay://theme">
Winsonb2d7f532019-02-04 16:32:43 -0800948 <policy type="signature">
949 <item type="string" name="foo" />
950 <item type="drawable" name="bar" />
951 </policy>
Adam Lesinski46c4d722017-08-23 13:03:56 -0700952 </overlayable>)";
953 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -0800954
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700955 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
Adam Lesinski71be7052017-12-12 16:48:07 -0800956 ASSERT_TRUE(search_result);
957 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800958 ASSERT_TRUE(search_result.value().entry->overlayable_item);
959 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
960 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
961 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winsonb2d7f532019-02-04 16:32:43 -0800962 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700963
964 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
965 ASSERT_TRUE(search_result);
966 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800967 ASSERT_TRUE(search_result.value().entry->overlayable_item);
968 result_overlayable_item = search_result.value().entry->overlayable_item.value();
969 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
970 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winsonb2d7f532019-02-04 16:32:43 -0800971 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800972}
973
974TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
975 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
976 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
977 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
978}
979
980TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
981 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700982}
983
984TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800985 std::string input = R"(
986 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700987 <policy type="product">
988 <item type="string" name="bar" />
989 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700990 <policy type="system">
991 <item type="string" name="fiz" />
992 </policy>
993 <policy type="vendor">
994 <item type="string" name="fuz" />
995 </policy>
996 <policy type="public">
997 <item type="string" name="faz" />
998 </policy>
Winsonb2d7f532019-02-04 16:32:43 -0800999 <policy type="signature">
1000 <item type="string" name="foz" />
1001 </policy>
Ryan Mitchell939df092019-04-09 17:13:50 -07001002 <policy type="odm">
1003 <item type="string" name="biz" />
1004 </policy>
1005 <policy type="oem">
1006 <item type="string" name="buz" />
1007 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001008 </overlayable>)";
1009 ASSERT_TRUE(TestParse(input));
1010
Winsonb2d7f532019-02-04 16:32:43 -08001011 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001012 ASSERT_TRUE(search_result);
1013 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001014 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1015 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1016 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001017 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001018
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001019 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
1020 ASSERT_TRUE(search_result);
1021 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001022 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1023 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1024 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1025 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSystem));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001026
1027 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
1028 ASSERT_TRUE(search_result);
1029 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001030 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1031 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1032 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1033 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kVendor));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001034
1035 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
1036 ASSERT_TRUE(search_result);
1037 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001038 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1039 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1040 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1041 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kPublic));
Winsonb2d7f532019-02-04 16:32:43 -08001042
1043 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
1044 ASSERT_TRUE(search_result);
1045 ASSERT_THAT(search_result.value().entry, NotNull());
1046 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1047 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1048 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1049 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature));
Ryan Mitchell939df092019-04-09 17:13:50 -07001050
1051 search_result = table_.FindResource(test::ParseNameOrDie("string/biz"));
1052 ASSERT_TRUE(search_result);
1053 ASSERT_THAT(search_result.value().entry, NotNull());
1054 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1055 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1056 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1057 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kOdm));
1058
1059 search_result = table_.FindResource(test::ParseNameOrDie("string/buz"));
1060 ASSERT_TRUE(search_result);
1061 ASSERT_THAT(search_result.value().entry, NotNull());
1062 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1063 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1064 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1065 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kOem));
Winsonb2d7f532019-02-04 16:32:43 -08001066}
1067
1068TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
1069 std::string input = R"(
1070 <overlayable name="Name">
1071 <item type="string" name="foo" />
1072 </overlayable>)";
1073 EXPECT_FALSE(TestParse(input));
1074
1075 input = R"(
1076 <overlayable name="Name">
1077 <policy>
1078 <item name="foo" />
1079 </policy>
1080 </overlayable>)";
1081 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001082}
1083
1084TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1085 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001086 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001087 <policy type="illegal_policy">
1088 <item type="string" name="foo" />
1089 </policy>
1090 </overlayable>)";
1091 EXPECT_FALSE(TestParse(input));
1092
1093 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001094 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001095 <policy type="product">
1096 <item name="foo" />
1097 </policy>
1098 </overlayable>)";
1099 EXPECT_FALSE(TestParse(input));
1100
1101 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001102 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001103 <policy type="vendor">
1104 <item type="string" />
1105 </policy>
1106 </overlayable>)";
1107 EXPECT_FALSE(TestParse(input));
1108}
1109
1110TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1111 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001112 <overlayable name="Name">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001113 <policy type="vendor|public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001114 <item type="string" name="foo" />
1115 </policy>
1116 <policy type="product|system">
1117 <item type="string" name="bar" />
1118 </policy>
1119 </overlayable>)";
1120 ASSERT_TRUE(TestParse(input));
1121
1122 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1123 ASSERT_TRUE(search_result);
1124 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001125 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1126 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1127 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1128 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kVendor
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001129 | OverlayableItem::Policy::kPublic));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001130
1131 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1132 ASSERT_TRUE(search_result);
1133 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001134 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1135 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1136 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1137 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct
1138 | OverlayableItem::Policy::kSystem));
Adam Lesinski71be7052017-12-12 16:48:07 -08001139}
1140
1141TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1142 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001143 <overlayable name="Name">
Adam Lesinski71be7052017-12-12 16:48:07 -08001144 <item type="string" name="foo" />
1145 <item type="string" name="foo" />
1146 </overlayable>)";
1147 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001148
1149 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001150 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001151 <item type="string" name="foo" />
1152 </overlayable>
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001153 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001154 <item type="string" name="foo" />
1155 </overlayable>)";
1156 EXPECT_FALSE(TestParse(input));
1157
1158 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001159 <overlayable name="Name">
1160 <item type="string" name="foo" />
1161 </overlayable>
1162 <overlayable name="Other">
1163 <item type="string" name="foo" />
1164 </overlayable>)";
1165 EXPECT_FALSE(TestParse(input));
1166
1167 input = R"(
1168 <overlayable name="Name" actor="overlay://my.actor.one">
1169 <item type="string" name="foo" />
1170 </overlayable>
1171 <overlayable name="Other" actor="overlay://my.actor.two">
1172 <item type="string" name="foo" />
1173 </overlayable>)";
1174 EXPECT_FALSE(TestParse(input));
1175
1176 input = R"(
1177 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001178 <policy type="product">
1179 <item type="string" name="foo" />
1180 <item type="string" name="foo" />
1181 </policy>
1182 </overlayable>)";
1183 EXPECT_FALSE(TestParse(input));
1184
1185 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001186 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001187 <policy type="product">
1188 <item type="string" name="foo" />
1189 </policy>
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001190 <item type="string" name="foo" />
1191 </overlayable>)";
1192 EXPECT_FALSE(TestParse(input));
1193
1194 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001195 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001196 <policy type="product">
1197 <item type="string" name="foo" />
1198 </policy>
1199 <policy type="vendor">
1200 <item type="string" name="foo" />
1201 </policy>
1202 </overlayable>)";
1203 EXPECT_FALSE(TestParse(input));
1204
1205 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001206 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001207 <policy type="product">
1208 <item type="string" name="foo" />
1209 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001210 </overlayable>
1211
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001212 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001213 <policy type="product">
1214 <item type="string" name="foo" />
1215 </policy>
1216 </overlayable>)";
1217 EXPECT_FALSE(TestParse(input));
1218}
1219
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001220TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1221 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001222 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001223 <policy type="vendor|product">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001224 <policy type="public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001225 <item type="string" name="foo" />
1226 </policy>
1227 </policy>
1228 </overlayable>)";
1229 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001230}
1231
y9efbbef2018-04-18 11:29:09 -07001232TEST_F(ResourceParserTest, ParseIdItem) {
1233 std::string input = R"(
1234 <item name="foo" type="id">@id/bar</item>
1235 <item name="bar" type="id"/>
1236 <item name="baz" type="id"></item>)";
1237 ASSERT_TRUE(TestParse(input));
1238
1239 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1240 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1241 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1242
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001243 input = R"(
1244 <id name="foo2">@id/bar</id>
1245 <id name="bar2"/>
1246 <id name="baz2"></id>)";
1247 ASSERT_TRUE(TestParse(input));
1248
1249 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1250 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1251 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1252
y9efbbef2018-04-18 11:29:09 -07001253 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001254 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -07001255 ASSERT_FALSE(TestParse(input));
1256
1257 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001258 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -07001259 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001260 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -07001261 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001262 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -07001263 ASSERT_FALSE(TestParse(input));
1264
1265 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001266 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -07001267 ASSERT_FALSE(TestParse(input));
1268}
1269
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001270TEST_F(ResourceParserTest, ParseCData) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001271 // Double quotes should still change the state of whitespace processing
1272 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001273 ASSERT_TRUE(TestParse(input));
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001274 auto output = test::GetValue<String>(&table_, "string/foo");
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001275 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001276 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001277
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001278 input = R"(<string name="foo2"><![CDATA[Hello
1279 World]]></string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001280 ASSERT_TRUE(TestParse(input));
1281 output = test::GetValue<String>(&table_, "string/foo2");
1282 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001283 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001284
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001285 // Cdata blocks should have their whitespace trimmed
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001286 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1287 ASSERT_TRUE(TestParse(input));
1288 output = test::GetValue<String>(&table_, "string/foo3");
1289 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001290 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001291
1292 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1293 ASSERT_TRUE(TestParse(input));
1294 output = test::GetValue<String>(&table_, "string/foo4");
1295 ASSERT_THAT(output, NotNull());
1296 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1297
1298 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1299 ASSERT_TRUE(TestParse(input));
1300 output = test::GetValue<String>(&table_, "string/foo5");
1301 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001302 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1303
1304 // Single quotes must still be escaped
1305 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1306 ASSERT_FALSE(TestParse(input));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001307}
1308
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309} // namespace aapt