blob: 9b70079a98c97d9c5b3faa638b086c5713ac247a [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <sstream>
20#include <string>
21
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "ResourceValues.h"
Adam Lesinski00451162017-10-03 07:44:08 -070025#include "io/StringStream.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070026#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080027#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029using ::aapt::io::StringInputStream;
Adam Lesinskia45893a2017-05-30 15:19:02 -070030using ::aapt::test::StrValueEq;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070031using ::aapt::test::ValueEq;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020032using ::android::ConfigDescription;
Adam Lesinskia45893a2017-05-30 15:19:02 -070033using ::android::Res_value;
Adam Lesinski71be7052017-12-12 16:48:07 -080034using ::android::ResTable_map;
Adam Lesinskie597d682017-06-01 17:16:44 -070035using ::android::StringPiece;
36using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070037using ::testing::IsEmpty;
38using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070039using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070040using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070041using ::testing::SizeIs;
Adam Lesinski71be7052017-12-12 16:48:07 -080042using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080043
Winson62ac8b52019-12-04 08:36:48 -080044using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
45
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046namespace aapt {
47
Adam Lesinskibab4ef52017-06-01 15:22:57 -070048constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054
55 std::string input = kXmlPreamble;
56 input += R"(<attr name="foo"/>)";
57 StringInputStream in(input);
58 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070060}
61
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062class ResourceParserTest : public ::testing::Test {
63 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070064 void SetUp() override {
65 context_ = test::ContextBuilder().Build();
66 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ::testing::AssertionResult TestParse(const StringPiece& str) {
69 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 }
Adam Lesinski52364f72016-01-11 13:10:24 -080071
Adam Lesinskibab4ef52017-06-01 15:22:57 -070072 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070074 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
75 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076
77 std::string input = kXmlPreamble;
78 input += "<resources>\n";
79 input.append(str.data(), str.size());
80 input += "\n</resources>";
81 StringInputStream in(input);
82 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 return ::testing::AssertionFailure();
87 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088
89 protected:
90 ResourceTable table_;
91 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092};
93
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070095 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070098 ASSERT_THAT(str, NotNull());
99 EXPECT_THAT(*str, StrValueEq(" hey there "));
100 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800101
102 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
103 str = test::GetValue<String>(&table_, "string/bar");
104 ASSERT_THAT(str, NotNull());
105 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
106
107 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
108 str = test::GetValue<String>(&table_, "string/baz");
109 ASSERT_THAT(str, NotNull());
110 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111}
112
113TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700114 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700117 ASSERT_THAT(str, NotNull());
118 EXPECT_THAT(*str, StrValueEq("?123"));
119 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700120
121 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
122 str = test::GetValue<String>(&table_, "string/bar");
123 ASSERT_THAT(str, NotNull());
124 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125}
126
Adam Lesinski9f222042015-11-04 13:51:45 -0800127TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700128 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
129 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800130}
131
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700132TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800134 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700136 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700140 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700141
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800142 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700143 EXPECT_THAT(str->value->spans, SizeIs(2));
144 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700145
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800146 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
147 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700148 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700149
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800150 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
151 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700152 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700153}
154
155TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700159 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800160 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700161 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700162
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700166 ASSERT_THAT(str, NotNull());
167 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700168}
169
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700170TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
171 // Tuncate leading and trailing whitespace
172 EXPECT_TRUE(TestParse(R"(<string name="foo">&#32;Hello&#32;</string>)"));
173
174 String* str = test::GetValue<String>(&table_, "string/foo");
175 ASSERT_THAT(str, NotNull());
176 EXPECT_THAT(*str->value, StrEq("Hello"));
177 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
178
179 // AAPT does not truncate unicode whitespace
180 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
181
182 str = test::GetValue<String>(&table_, "string/foo2");
183 ASSERT_THAT(str, NotNull());
184 EXPECT_THAT(*str->value, StrEq(" Hello "));
185 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
186
187 // Preserve non-ASCII whitespace including extended ASCII characters
Ryan Mitchell0f326752019-03-11 11:00:25 -0700188 EXPECT_TRUE(TestParse(R"(<string name="foo3">&#160;Hello&#x202F;World&#160;</string>)"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700189
190 str = test::GetValue<String>(&table_, "string/foo3");
191 ASSERT_THAT(str, NotNull());
Ryan Mitchell0f326752019-03-11 11:00:25 -0700192 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0"));
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700193 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
194
195 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
196
197 str = test::GetValue<String>(&table_, "string/foo4");
198 ASSERT_THAT(str, NotNull());
199 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
200 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
201}
202
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800203TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
204 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
205 ASSERT_TRUE(TestParse(input));
206
207 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
208 ASSERT_THAT(str, NotNull());
209 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
210 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
211
212 ASSERT_THAT(str->value->spans, SizeIs(2u));
213 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
214 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
215 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
216
217 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
218 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
219 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
220}
221
Mihai Nitad1a65212019-03-26 13:47:45 -0700222TEST_F(ResourceParserTest, ParseStringTranslatableAttribute) {
223 // If there is no translate attribute the default is 'true'
224 EXPECT_TRUE(TestParse(R"(<string name="foo1">Translate</string>)"));
225 String* str = test::GetValue<String>(&table_, "string/foo1");
226 ASSERT_THAT(str, NotNull());
227 ASSERT_TRUE(str->IsTranslatable());
228
229 // Explicit 'true' translate attribute
230 EXPECT_TRUE(TestParse(R"(<string name="foo2" translatable="true">Translate</string>)"));
231 str = test::GetValue<String>(&table_, "string/foo2");
232 ASSERT_THAT(str, NotNull());
233 ASSERT_TRUE(str->IsTranslatable());
234
235 // Explicit 'false' translate attribute
236 EXPECT_TRUE(TestParse(R"(<string name="foo3" translatable="false">Do not translate</string>)"));
237 str = test::GetValue<String>(&table_, "string/foo3");
238 ASSERT_THAT(str, NotNull());
239 ASSERT_FALSE(str->IsTranslatable());
240
241 // Invalid value for the translate attribute, should be boolean ('true' or 'false')
242 EXPECT_FALSE(TestParse(R"(<string name="foo4" translatable="yes">Translate</string>)"));
243}
244
Adam Lesinski75421622017-01-06 15:20:04 -0800245TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700246 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800247 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700248 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800249 ASSERT_TRUE(TestParse(input));
250
251 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700252 ASSERT_THAT(str, NotNull());
253 EXPECT_THAT(*str, StrValueEq("There are no apples"));
254 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800255}
256
257TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700258 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800259 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700260 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800261 EXPECT_FALSE(TestParse(input));
262}
263
264TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700265 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800266 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700267 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700271 ASSERT_THAT(str, NotNull());
272 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800273
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
275 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700276 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800277}
278
279TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700280 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800281 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700282 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800283 ASSERT_TRUE(TestParse(input));
284
285 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700286 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800287 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800288
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
290 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
291 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
292
293 ASSERT_THAT(str->value->spans, SizeIs(1u));
294 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
295 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
296 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297}
298
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700299TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700300 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700302
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
304 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800305 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700307 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
308 ASSERT_THAT(null_ref, NotNull());
309 EXPECT_FALSE(null_ref->name);
310 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700311 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700312}
313
314TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700315 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700317
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700318 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700319 ASSERT_THAT(integer, NotNull());
320 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
321 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700322}
323
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700325 std::string input = R"(
326 <attr name="foo" format="string"/>
327 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700331 ASSERT_THAT(attr, NotNull());
332 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700335 ASSERT_THAT(attr, NotNull());
336 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337}
338
Adam Lesinskia45893a2017-05-30 15:19:02 -0700339// Old AAPT allowed attributes to be defined under different configurations, but ultimately
340// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700341TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700343 std::string input = R"(
344 <attr name="foo" />
345 <declare-styleable name="bar">
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700346 <attr name="baz" format="reference"/>
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700347 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800349
Adam Lesinskia45893a2017-05-30 15:19:02 -0700350 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
351 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
352 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800353
Adam Lesinskia45893a2017-05-30 15:19:02 -0700354 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
355 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
356 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800357}
358
Adam Lesinskia5870652015-11-20 15:32:30 -0800359TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700360 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700364 ASSERT_THAT(attr, NotNull());
365 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
366 EXPECT_THAT(attr->min_int, Eq(10));
367 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800368}
369
370TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700371 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800372}
373
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700375 std::string input = R"(
376 <declare-styleable name="Styleable">
377 <attr name="foo" />
378 </declare-styleable>
379 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700383 ASSERT_THAT(attr, NotNull());
384 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800385}
386
387TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700388 std::string input = R"(
389 <declare-styleable name="Theme">
390 <attr name="foo" />
391 </declare-styleable>
392 <declare-styleable name="Window">
393 <attr name="foo" format="boolean"/>
394 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700398 ASSERT_THAT(attr, NotNull());
399 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400}
401
402TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700403 std::string input = R"(
404 <attr name="foo">
405 <enum name="bar" value="0"/>
Ryan Mitchellc1676802019-05-20 16:47:08 -0700406 <enum name="bat" value="0x1"/>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700407 <enum name="baz" value="2"/>
408 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700412 ASSERT_THAT(enum_attr, NotNull());
413 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
414 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800415
Adam Lesinskia45893a2017-05-30 15:19:02 -0700416 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
417 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
418 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700419 EXPECT_THAT(enum_attr->symbols[0].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420
Adam Lesinskia45893a2017-05-30 15:19:02 -0700421 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
422 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
423 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700424 EXPECT_THAT(enum_attr->symbols[1].type, Eq(Res_value::TYPE_INT_HEX));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425
Adam Lesinskia45893a2017-05-30 15:19:02 -0700426 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
427 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
428 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Ryan Mitchellc1676802019-05-20 16:47:08 -0700429 EXPECT_THAT(enum_attr->symbols[2].type, Eq(Res_value::TYPE_INT_DEC));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430}
431
432TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700433 std::string input = R"(
434 <attr name="foo">
435 <flag name="bar" value="0"/>
436 <flag name="bat" value="1"/>
437 <flag name="baz" value="2"/>
438 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700442 ASSERT_THAT(flag_attr, NotNull());
443 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
444 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445
Adam Lesinskia45893a2017-05-30 15:19:02 -0700446 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
447 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
448 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800449
Adam Lesinskia45893a2017-05-30 15:19:02 -0700450 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
451 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
452 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453
Adam Lesinskia45893a2017-05-30 15:19:02 -0700454 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
455 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
456 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 std::unique_ptr<BinaryPrimitive> flag_value =
459 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700460 ASSERT_THAT(flag_value, NotNull());
461 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800462}
463
464TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700465 std::string input = R"(
466 <attr name="foo">
467 <enum name="bar" value="0"/>
468 <enum name="bat" value="1"/>
469 <enum name="bat" value="2"/>
470 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800472}
473
474TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700475 std::string input = R"(
476 <style name="foo" parent="@style/fu">
477 <item name="bar">#ffffffff</item>
478 <item name="bat">@string/hey</item>
479 <item name="baz"><b>hey</b></item>
480 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700484 ASSERT_THAT(style, NotNull());
485 ASSERT_TRUE(style->parent);
486 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
487 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488
Adam Lesinskia45893a2017-05-30 15:19:02 -0700489 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
490 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
491 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800492}
493
Adam Lesinski769de982015-04-10 19:43:55 -0700494TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700495 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700496
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700498 ASSERT_THAT(style, NotNull());
499 ASSERT_TRUE(style->parent);
500 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700501}
502
Adam Lesinski24aad162015-04-24 19:19:30 -0700503TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700504 std::string input = R"(
505 <style xmlns:app="http://schemas.android.com/apk/res/android"
506 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700510 ASSERT_THAT(style, NotNull());
511 ASSERT_TRUE(style->parent);
512 ASSERT_TRUE(style->parent.value().name);
513 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700514}
515
516TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700517 std::string input = R"(
518 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
519 <item name="app:bar">0</item>
520 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700524 ASSERT_THAT(style, NotNull());
525 ASSERT_THAT(style->entries, SizeIs(1));
526 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700527}
528
Ryan Mitchell633d7962018-06-11 15:29:21 -0700529TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) {
530 std::string input = R"(
531 <style name="foo">
532 <item name="bar">
533 com.helloworld.AppClass
534 </item>
535 </style>)";
536 ASSERT_TRUE(TestParse(input));
537
538 Style* style = test::GetValue<Style>(&table_, "style/foo");
539 ASSERT_THAT(style, NotNull());
540 EXPECT_THAT(style->entries[0].value, NotNull());
541 RawString* value = ValueCast<RawString>(style->entries[0].value.get());
542 EXPECT_THAT(value, NotNull());
543 EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)"));
544}
545
546
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700547TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700548 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700549
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700551 ASSERT_THAT(style, NotNull());
552 ASSERT_TRUE(style->parent);
553 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700555}
556
Adam Lesinskia45893a2017-05-30 15:19:02 -0700557TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
558 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700561 ASSERT_THAT(style, NotNull());
562 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700564}
565
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800566TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700567 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800568
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700570 ASSERT_THAT(style, NotNull());
571 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800573}
574
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700576 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
577 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578}
579
580TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700581 std::string input = R"(
582 <declare-styleable name="foo">
583 <attr name="bar" />
584 <attr name="bat" format="string|reference"/>
585 <attr name="baz">
586 <enum name="foo" value="1"/>
587 </attr>
588 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800590
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700593 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800594 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800595
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Ryan Mitchellacde95c32019-04-23 05:44:21 -0700597 ASSERT_THAT(attr, IsNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700600 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700604 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700606 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700607
Adam Lesinskia45893a2017-05-30 15:19:02 -0700608 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700611 ASSERT_THAT(styleable, NotNull());
612 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800613
Adam Lesinskia45893a2017-05-30 15:19:02 -0700614 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
615 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
616 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800617}
618
Donald Chai94e4a012020-01-06 13:52:41 -0800619TEST_F(ResourceParserTest, ParseDeclareStyleablePreservingVisibility) {
620 StringInputStream input(R"(
621 <resources>
622 <declare-styleable name="foo">
623 <attr name="myattr" />
624 </declare-styleable>
625 <declare-styleable name="bar">
626 <attr name="myattr" />
627 </declare-styleable>
628 <public type="styleable" name="bar" />
629 </resources>)");
630 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"},
631 ConfigDescription::DefaultConfig(),
632 ResourceParserOptions{.preserve_visibility_of_styleables = true});
633
634 xml::XmlPullParser xml_parser(&input);
635 ASSERT_TRUE(parser.Parse(&xml_parser));
636
637 EXPECT_EQ(
638 table_.FindResource(test::ParseNameOrDie("styleable/foo")).value().entry->visibility.level,
639 Visibility::Level::kUndefined);
640 EXPECT_EQ(
641 table_.FindResource(test::ParseNameOrDie("styleable/bar")).value().entry->visibility.level,
642 Visibility::Level::kPublic);
643}
644
Adam Lesinski467f1712015-11-16 17:35:44 -0800645TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700646 std::string input = R"(
647 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
648 name="foo">
649 <attr name="*android:bar" />
650 <attr name="privAndroid:bat" />
651 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 ASSERT_TRUE(TestParse(input));
653 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700654 ASSERT_THAT(styleable, NotNull());
655 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800656
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700658 ASSERT_TRUE(styleable->entries[0].name);
659 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800660
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700662 ASSERT_TRUE(styleable->entries[1].name);
663 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800664}
665
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800666TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700667 std::string input = R"(
668 <array name="foo">
669 <item>@string/ref</item>
670 <item>hey</item>
671 <item>23</item>
672 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700676 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700677 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800678
Adam Lesinski4ffea042017-08-10 15:37:28 -0700679 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
680 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
681 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800682}
683
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700684TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700685 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700686 <string-array name="foo">
687 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700688 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700690 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700691}
692
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700693TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700694 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700695 <array name="foo" format="string">
696 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700697 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700698 ASSERT_TRUE(TestParse(input));
699
700 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700701 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700702 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700703
Adam Lesinski4ffea042017-08-10 15:37:28 -0700704 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700705 ASSERT_THAT(str, NotNull());
706 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700707}
708
709TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700710 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700711 <array name="foo" format="integer">
712 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700713 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700714 ASSERT_FALSE(TestParse(input));
715}
716
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800717TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700718 std::string input = R"(
719 <plurals name="foo">
720 <item quantity="other">apples</item>
721 <item quantity="one">apple</item>
722 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800724
725 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700726 ASSERT_THAT(plural, NotNull());
727 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
728 EXPECT_THAT(plural->values[Plural::Two], IsNull());
729 EXPECT_THAT(plural->values[Plural::Few], IsNull());
730 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800731
Adam Lesinskia45893a2017-05-30 15:19:02 -0700732 EXPECT_THAT(plural->values[Plural::One], NotNull());
733 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800734}
735
736TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700737 std::string input = R"(
738 <!--This is a comment-->
739 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800741
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700743 ASSERT_THAT(value, NotNull());
744 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700745}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700746
Adam Lesinskie78fd612015-10-22 12:48:43 -0700747TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700748 std::string input = R"(
749 <!--One-->
750 <!--Two-->
751 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700752
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700754
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700756 ASSERT_THAT(value, NotNull());
757 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700758}
759
760TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700761 std::string input = R"(
762 <!--One-->
763 <string name="foo">
764 Hi
765 <!--Two-->
766 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700768
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700769 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700770 ASSERT_THAT(value, NotNull());
771 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800772}
773
Adam Lesinskica5638f2015-10-21 14:42:43 -0700774TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700776 // comments from those end up in R.java
777 std::string input = R"(
778 <declare-styleable name="foo">
779 <!-- The name of the bar -->
780 <attr name="barName" format="string|reference" />
781 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700782
Adam Lesinskia45893a2017-05-30 15:19:02 -0700783 <attr name="foo">
784 <!-- The very first -->
785 <enum name="one" value="1" />
786 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700788
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700790 ASSERT_THAT(styleable, NotNull());
791 ASSERT_THAT(styleable->entries, SizeIs(1));
792 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700793
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700794 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700795 ASSERT_THAT(attr, NotNull());
796 ASSERT_THAT(attr->symbols, SizeIs(1));
797 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700798}
799
Adam Lesinskia45893a2017-05-30 15:19:02 -0700800// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800801TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700802 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
803 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800804}
805
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800806TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700807 std::string input = R"(
808 <string name="foo" product="phone">hi</string>
809 <string name="foo" product="no-sdcard">ho</string>
810 <string name="bar" product="">wee</string>
811 <string name="baz">woo</string>
812 <string name="bit" product="phablet">hoot</string>
813 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700815
Adam Lesinskia45893a2017-05-30 15:19:02 -0700816 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
817 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
818 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
819 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
820 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
821 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700822}
823
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800824TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700825 std::string input = R"(
826 <public-group type="attr" first-id="0x01010040">
827 <public name="foo" />
828 <public name="bar" />
829 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800831
Adam Lesinskia45893a2017-05-30 15:19:02 -0700832 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
833 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800834
Adam Lesinskia45893a2017-05-30 15:19:02 -0700835 ASSERT_TRUE(result.value().package->id);
836 ASSERT_TRUE(result.value().type->id);
837 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 ResourceId actual_id(result.value().package->id.value(),
839 result.value().type->id.value(),
840 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700841 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700844 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845
Adam Lesinskia45893a2017-05-30 15:19:02 -0700846 ASSERT_TRUE(result.value().package->id);
847 ASSERT_TRUE(result.value().type->id);
848 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700849 actual_id = ResourceId(result.value().package->id.value(),
850 result.value().type->id.value(),
851 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700852 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800853}
854
Adam Lesinski71be7052017-12-12 16:48:07 -0800855TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
856 std::string input = R"(
857 <!-- private -->
858 <java-symbol type="string" name="foo" />
859 <!-- public -->
860 <public type="string" name="foo" id="0x01020000" />
861 <!-- private2 -->
862 <java-symbol type="string" name="foo" />)";
863 ASSERT_TRUE(TestParse(input));
864
865 Maybe<ResourceTable::SearchResult> result =
866 table_.FindResource(test::ParseNameOrDie("string/foo"));
867 ASSERT_TRUE(result);
868
869 ResourceEntry* entry = result.value().entry;
870 ASSERT_THAT(entry, NotNull());
871 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
872 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
873}
874
Adam Lesinskifa105052015-11-07 13:34:39 -0800875TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700876 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
877 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800878}
879
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700880TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700881 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800882
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700885 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700886 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700887 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800888 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
889 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800890}
891
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800892TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700893 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800894
Adam Lesinskie597d682017-06-01 17:16:44 -0700895 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
896 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700897 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800898
Adam Lesinskia45893a2017-05-30 15:19:02 -0700899 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700900}
901
902// An <item> without a format specifier accepts all types of values.
903TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700904 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700905
906 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
907 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700908 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800909}
910
Adam Lesinski86d67df2017-01-31 13:47:27 -0800911TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700912 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
913 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800914}
915
916TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700917 std::string input = R"(
918 <bag name="bag" type="configVarying">
919 <item name="test">Hello!</item>
920 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800921 ASSERT_TRUE(TestParse(input));
922
923 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700924 ASSERT_THAT(val, NotNull());
925 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800926
Adam Lesinskia45893a2017-05-30 15:19:02 -0700927 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
928 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800929}
930
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700931TEST_F(ResourceParserTest, ParseElementWithNoValue) {
932 std::string input = R"(
933 <item type="drawable" format="reference" name="foo" />
934 <string name="foo" />)";
935 ASSERT_TRUE(TestParse(input));
936 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
937
938 String* str = test::GetValue<String>(&table_, "string/foo");
939 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700940 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700941}
942
Adam Lesinskib9f05482017-06-02 16:32:37 -0700943TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700944 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700945}
946
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700947TEST_F(ResourceParserTest, ParseOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800948 std::string input = R"(
949 <overlayable name="Name" actor="overlay://theme">
Winsonb2d7f532019-02-04 16:32:43 -0800950 <policy type="signature">
951 <item type="string" name="foo" />
952 <item type="drawable" name="bar" />
953 </policy>
Adam Lesinski46c4d722017-08-23 13:03:56 -0700954 </overlayable>)";
955 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -0800956
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700957 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
Adam Lesinski71be7052017-12-12 16:48:07 -0800958 ASSERT_TRUE(search_result);
959 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800960 ASSERT_TRUE(search_result.value().entry->overlayable_item);
961 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
962 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
963 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800964 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700965
966 search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar"));
967 ASSERT_TRUE(search_result);
968 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800969 ASSERT_TRUE(search_result.value().entry->overlayable_item);
970 result_overlayable_item = search_result.value().entry->overlayable_item.value();
971 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
972 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800973 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800974}
975
976TEST_F(ResourceParserTest, ParseOverlayableRequiresName) {
977 EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)"));
978 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)"));
979 EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)"));
980}
981
982TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) {
983 EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700984}
985
986TEST_F(ResourceParserTest, ParseOverlayablePolicy) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800987 std::string input = R"(
988 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700989 <policy type="product">
990 <item type="string" name="bar" />
991 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700992 <policy type="system">
993 <item type="string" name="fiz" />
994 </policy>
995 <policy type="vendor">
996 <item type="string" name="fuz" />
997 </policy>
998 <policy type="public">
999 <item type="string" name="faz" />
1000 </policy>
Winsonb2d7f532019-02-04 16:32:43 -08001001 <policy type="signature">
1002 <item type="string" name="foz" />
1003 </policy>
Ryan Mitchell939df092019-04-09 17:13:50 -07001004 <policy type="odm">
1005 <item type="string" name="biz" />
1006 </policy>
1007 <policy type="oem">
1008 <item type="string" name="buz" />
1009 </policy>
Winsonf56ade32019-12-04 11:32:41 -08001010 <policy type="actor">
1011 <item type="string" name="actor" />
1012 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001013 </overlayable>)";
1014 ASSERT_TRUE(TestParse(input));
1015
Winsonb2d7f532019-02-04 16:32:43 -08001016 auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001017 ASSERT_TRUE(search_result);
1018 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001019 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1020 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1021 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001022 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001023
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001024 search_result = table_.FindResource(test::ParseNameOrDie("string/fiz"));
1025 ASSERT_TRUE(search_result);
1026 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001027 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1028 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1029 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001030 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SYSTEM_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001031
1032 search_result = table_.FindResource(test::ParseNameOrDie("string/fuz"));
1033 ASSERT_TRUE(search_result);
1034 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001035 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1036 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1037 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001038 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001039
1040 search_result = table_.FindResource(test::ParseNameOrDie("string/faz"));
1041 ASSERT_TRUE(search_result);
1042 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001043 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1044 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1045 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001046 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PUBLIC));
Winsonb2d7f532019-02-04 16:32:43 -08001047
1048 search_result = table_.FindResource(test::ParseNameOrDie("string/foz"));
1049 ASSERT_TRUE(search_result);
1050 ASSERT_THAT(search_result.value().entry, NotNull());
1051 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1052 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1053 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001054 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::SIGNATURE));
Ryan Mitchell939df092019-04-09 17:13:50 -07001055
1056 search_result = table_.FindResource(test::ParseNameOrDie("string/biz"));
1057 ASSERT_TRUE(search_result);
1058 ASSERT_THAT(search_result.value().entry, NotNull());
1059 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1060 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1061 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001062 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ODM_PARTITION));
Ryan Mitchell939df092019-04-09 17:13:50 -07001063
1064 search_result = table_.FindResource(test::ParseNameOrDie("string/buz"));
1065 ASSERT_TRUE(search_result);
1066 ASSERT_THAT(search_result.value().entry, NotNull());
1067 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1068 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1069 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001070 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::OEM_PARTITION));
Winsonf56ade32019-12-04 11:32:41 -08001071
1072 search_result = table_.FindResource(test::ParseNameOrDie("string/actor"));
1073 ASSERT_TRUE(search_result);
1074 ASSERT_THAT(search_result.value().entry, NotNull());
1075 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1076 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1077 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
1078 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::ACTOR_SIGNATURE));
Winsonb2d7f532019-02-04 16:32:43 -08001079}
1080
1081TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) {
1082 std::string input = R"(
1083 <overlayable name="Name">
1084 <item type="string" name="foo" />
1085 </overlayable>)";
1086 EXPECT_FALSE(TestParse(input));
1087
1088 input = R"(
1089 <overlayable name="Name">
1090 <policy>
1091 <item name="foo" />
1092 </policy>
1093 </overlayable>)";
1094 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001095}
1096
1097TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) {
1098 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001099 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001100 <policy type="illegal_policy">
1101 <item type="string" name="foo" />
1102 </policy>
1103 </overlayable>)";
1104 EXPECT_FALSE(TestParse(input));
1105
1106 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001107 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001108 <policy type="product">
1109 <item name="foo" />
1110 </policy>
1111 </overlayable>)";
1112 EXPECT_FALSE(TestParse(input));
1113
1114 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001115 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001116 <policy type="vendor">
1117 <item type="string" />
1118 </policy>
1119 </overlayable>)";
1120 EXPECT_FALSE(TestParse(input));
1121}
1122
1123TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) {
1124 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001125 <overlayable name="Name">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001126 <policy type="vendor|public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001127 <item type="string" name="foo" />
1128 </policy>
1129 <policy type="product|system">
1130 <item type="string" name="bar" />
1131 </policy>
1132 </overlayable>)";
1133 ASSERT_TRUE(TestParse(input));
1134
1135 auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo"));
1136 ASSERT_TRUE(search_result);
1137 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001138 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1139 OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value();
1140 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001141 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::VENDOR_PARTITION
1142 | PolicyFlags::PUBLIC));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001143
1144 search_result = table_.FindResource(test::ParseNameOrDie("string/bar"));
1145 ASSERT_TRUE(search_result);
1146 ASSERT_THAT(search_result.value().entry, NotNull());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001147 ASSERT_TRUE(search_result.value().entry->overlayable_item);
1148 result_overlayable_item = search_result.value().entry->overlayable_item.value();
1149 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
Winson62ac8b52019-12-04 08:36:48 -08001150 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
1151 | PolicyFlags::SYSTEM_PARTITION));
Adam Lesinski71be7052017-12-12 16:48:07 -08001152}
1153
1154TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
1155 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001156 <overlayable name="Name">
Adam Lesinski71be7052017-12-12 16:48:07 -08001157 <item type="string" name="foo" />
1158 <item type="string" name="foo" />
1159 </overlayable>)";
1160 EXPECT_FALSE(TestParse(input));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001161
1162 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001163 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001164 <item type="string" name="foo" />
1165 </overlayable>
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001166 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001167 <item type="string" name="foo" />
1168 </overlayable>)";
1169 EXPECT_FALSE(TestParse(input));
1170
1171 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001172 <overlayable name="Name">
1173 <item type="string" name="foo" />
1174 </overlayable>
1175 <overlayable name="Other">
1176 <item type="string" name="foo" />
1177 </overlayable>)";
1178 EXPECT_FALSE(TestParse(input));
1179
1180 input = R"(
1181 <overlayable name="Name" actor="overlay://my.actor.one">
1182 <item type="string" name="foo" />
1183 </overlayable>
1184 <overlayable name="Other" actor="overlay://my.actor.two">
1185 <item type="string" name="foo" />
1186 </overlayable>)";
1187 EXPECT_FALSE(TestParse(input));
1188
1189 input = R"(
1190 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001191 <policy type="product">
1192 <item type="string" name="foo" />
1193 <item type="string" name="foo" />
1194 </policy>
1195 </overlayable>)";
1196 EXPECT_FALSE(TestParse(input));
1197
1198 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001199 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001200 <policy type="product">
1201 <item type="string" name="foo" />
1202 </policy>
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001203 <item type="string" name="foo" />
1204 </overlayable>)";
1205 EXPECT_FALSE(TestParse(input));
1206
1207 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001208 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001209 <policy type="product">
1210 <item type="string" name="foo" />
1211 </policy>
1212 <policy type="vendor">
1213 <item type="string" name="foo" />
1214 </policy>
1215 </overlayable>)";
1216 EXPECT_FALSE(TestParse(input));
1217
1218 input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001219 <overlayable name="Name">
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001220 <policy type="product">
1221 <item type="string" name="foo" />
1222 </policy>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001223 </overlayable>
1224
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001225 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001226 <policy type="product">
1227 <item type="string" name="foo" />
1228 </policy>
1229 </overlayable>)";
1230 EXPECT_FALSE(TestParse(input));
1231}
1232
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001233TEST_F(ResourceParserTest, NestPolicyInOverlayableError) {
1234 std::string input = R"(
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001235 <overlayable name="Name">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001236 <policy type="vendor|product">
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -08001237 <policy type="public">
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001238 <item type="string" name="foo" />
1239 </policy>
1240 </policy>
1241 </overlayable>)";
1242 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001243}
1244
y9efbbef2018-04-18 11:29:09 -07001245TEST_F(ResourceParserTest, ParseIdItem) {
1246 std::string input = R"(
1247 <item name="foo" type="id">@id/bar</item>
1248 <item name="bar" type="id"/>
1249 <item name="baz" type="id"></item>)";
1250 ASSERT_TRUE(TestParse(input));
1251
1252 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
1253 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
1254 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
1255
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001256 input = R"(
1257 <id name="foo2">@id/bar</id>
1258 <id name="bar2"/>
1259 <id name="baz2"></id>)";
1260 ASSERT_TRUE(TestParse(input));
1261
1262 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
1263 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
1264 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
1265
y9efbbef2018-04-18 11:29:09 -07001266 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001267 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -07001268 ASSERT_FALSE(TestParse(input));
1269
1270 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001271 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -07001272 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001273 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -07001274 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001275 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -07001276 ASSERT_FALSE(TestParse(input));
1277
1278 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -07001279 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -07001280 ASSERT_FALSE(TestParse(input));
1281}
1282
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001283TEST_F(ResourceParserTest, ParseCData) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001284 // Double quotes should still change the state of whitespace processing
1285 std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001286 ASSERT_TRUE(TestParse(input));
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001287 auto output = test::GetValue<String>(&table_, "string/foo");
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001288 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001289 EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001290
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001291 input = R"(<string name="foo2"><![CDATA[Hello
1292 World]]></string>)";
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001293 ASSERT_TRUE(TestParse(input));
1294 output = test::GetValue<String>(&table_, "string/foo2");
1295 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001296 EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001297
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001298 // Cdata blocks should have their whitespace trimmed
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001299 input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)";
1300 ASSERT_TRUE(TestParse(input));
1301 output = test::GetValue<String>(&table_, "string/foo3");
1302 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001303 EXPECT_THAT(*output, StrValueEq(std::string("text").data()));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001304
1305 input = R"(<string name="foo4"> <![CDATA[]]> </string>)";
1306 ASSERT_TRUE(TestParse(input));
1307 output = test::GetValue<String>(&table_, "string/foo4");
1308 ASSERT_THAT(output, NotNull());
1309 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1310
1311 input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)";
1312 ASSERT_TRUE(TestParse(input));
1313 output = test::GetValue<String>(&table_, "string/foo5");
1314 ASSERT_THAT(output, NotNull());
Ryan Mitchell1d358ff2019-03-06 15:06:49 -08001315 EXPECT_THAT(*output, StrValueEq(std::string("").data()));
1316
1317 // Single quotes must still be escaped
1318 input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)";
1319 ASSERT_FALSE(TestParse(input));
Ryan Mitchellcb76d732018-06-05 10:15:04 -07001320}
1321
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322} // namespace aapt