blob: 41b4041efb7af780ad244a858c15666a785701b2 [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;
Adam Lesinskia45893a2017-05-30 15:19:02 -070032using ::android::Res_value;
Adam Lesinski71be7052017-12-12 16:48:07 -080033using ::android::ResTable_map;
Adam Lesinskie597d682017-06-01 17:16:44 -070034using ::android::StringPiece;
35using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070036using ::testing::IsEmpty;
37using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070038using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070039using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070040using ::testing::SizeIs;
Adam Lesinski71be7052017-12-12 16:48:07 -080041using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080042
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043namespace aapt {
44
Adam Lesinskibab4ef52017-06-01 15:22:57 -070045constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051
52 std::string input = kXmlPreamble;
53 input += R"(<attr name="foo"/>)";
54 StringInputStream in(input);
55 xml::XmlPullParser xml_parser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059class ResourceParserTest : public ::testing::Test {
60 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070061 void SetUp() override {
62 context_ = test::ContextBuilder().Build();
63 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 ::testing::AssertionResult TestParse(const StringPiece& str) {
66 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
Adam Lesinski52364f72016-01-11 13:10:24 -080068
Adam Lesinskibab4ef52017-06-01 15:22:57 -070069 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070071 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
72 parserOptions);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070073
74 std::string input = kXmlPreamble;
75 input += "<resources>\n";
76 input.append(str.data(), str.size());
77 input += "\n</resources>";
78 StringInputStream in(input);
79 xml::XmlPullParser xmlParser(&in);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 return ::testing::AssertionFailure();
84 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085
86 protected:
87 ResourceTable table_;
88 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089};
90
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070092 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070095 ASSERT_THAT(str, NotNull());
96 EXPECT_THAT(*str, StrValueEq(" hey there "));
97 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski2eed52e2018-02-21 15:55:58 -080098
99 ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)"));
100 str = test::GetValue<String>(&table_, "string/bar");
101 ASSERT_THAT(str, NotNull());
102 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
103
104 ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)"));
105 str = test::GetValue<String>(&table_, "string/baz");
106 ASSERT_THAT(str, NotNull());
107 EXPECT_THAT(*str, StrValueEq("Isn't it cool?"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108}
109
110TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700111 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700114 ASSERT_THAT(str, NotNull());
115 EXPECT_THAT(*str, StrValueEq("?123"));
116 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700117
118 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
119 str = test::GetValue<String>(&table_, "string/bar");
120 ASSERT_THAT(str, NotNull());
121 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinski9f222042015-11-04 13:51:45 -0800124TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700125 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
126 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800127}
128
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700129TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800131 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700133 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700137 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700138
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800139 EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700140 EXPECT_THAT(str->value->spans, SizeIs(2));
141 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700142
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800143 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
144 EXPECT_THAT(str->value->spans[0].first_char, Eq(18u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700145 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700146
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800147 EXPECT_THAT(*str->value->spans[1].name, StrEq("small"));
148 EXPECT_THAT(str->value->spans[1].first_char, Eq(25u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700149 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700150}
151
152TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700153 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800157 EXPECT_THAT(*str->value, StrEq("This is what I think"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700158 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700159
Adam Lesinskia45893a2017-05-30 15:19:02 -0700160 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 ASSERT_THAT(str, NotNull());
164 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700165}
166
Ryan Mitchell9beaa9c2018-03-28 18:22:57 -0700167TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
168 // Tuncate leading and trailing whitespace
169 EXPECT_TRUE(TestParse(R"(<string name="foo">&#32;Hello&#32;</string>)"));
170
171 String* str = test::GetValue<String>(&table_, "string/foo");
172 ASSERT_THAT(str, NotNull());
173 EXPECT_THAT(*str->value, StrEq("Hello"));
174 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
175
176 // AAPT does not truncate unicode whitespace
177 EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
178
179 str = test::GetValue<String>(&table_, "string/foo2");
180 ASSERT_THAT(str, NotNull());
181 EXPECT_THAT(*str->value, StrEq(" Hello "));
182 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
183
184 // Preserve non-ASCII whitespace including extended ASCII characters
185 EXPECT_TRUE(TestParse(R"(<string name="foo3">&#160;Hello&#160;</string>)"));
186
187 str = test::GetValue<String>(&table_, "string/foo3");
188 ASSERT_THAT(str, NotNull());
189 EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xC2\xA0"));
190 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
191
192 EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
193
194 str = test::GetValue<String>(&table_, "string/foo4");
195 ASSERT_THAT(str, NotNull());
196 EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
197 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
198}
199
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800200TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
201 std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
202 ASSERT_TRUE(TestParse(input));
203
204 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
205 ASSERT_THAT(str, NotNull());
206 EXPECT_THAT(str->value->value, StrEq(" My favorite string "));
207 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
208
209 ASSERT_THAT(str->value->spans, SizeIs(2u));
210 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
211 EXPECT_THAT(str->value->spans[0].first_char, Eq(1u));
212 EXPECT_THAT(str->value->spans[0].last_char, Eq(21u));
213
214 EXPECT_THAT(*str->value->spans[1].name, StrEq("i"));
215 EXPECT_THAT(str->value->spans[1].first_char, Eq(5u));
216 EXPECT_THAT(str->value->spans[1].last_char, Eq(13u));
217}
218
Adam Lesinski75421622017-01-06 15:20:04 -0800219TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700220 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800221 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700222 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800223 ASSERT_TRUE(TestParse(input));
224
225 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700226 ASSERT_THAT(str, NotNull());
227 EXPECT_THAT(*str, StrValueEq("There are no apples"));
228 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800229}
230
231TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700232 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800233 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700234 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800235 EXPECT_FALSE(TestParse(input));
236}
237
238TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700239 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800240 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700241 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700245 ASSERT_THAT(str, NotNull());
246 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800247
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800248 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
249 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700250 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800251}
252
253TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700254 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800255 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700256 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800257 ASSERT_TRUE(TestParse(input));
258
259 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700260 ASSERT_THAT(str, NotNull());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800261 EXPECT_THAT(str->value->value, Eq(" There are %1$d apples"));
Adam Lesinski75421622017-01-06 15:20:04 -0800262
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800263 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
264 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u));
265 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u));
266
267 ASSERT_THAT(str->value->spans, SizeIs(1u));
268 EXPECT_THAT(*str->value->spans[0].name, StrEq("b"));
269 EXPECT_THAT(str->value->spans[0].first_char, Eq(11u));
270 EXPECT_THAT(str->value->spans[0].last_char, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271}
272
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700273TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700274 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
278 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800279 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700281 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
282 ASSERT_THAT(null_ref, NotNull());
283 EXPECT_FALSE(null_ref->name);
284 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700285 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700286}
287
288TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700289 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700291
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700292 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700293 ASSERT_THAT(integer, NotNull());
294 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
295 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700296}
297
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700299 std::string input = R"(
300 <attr name="foo" format="string"/>
301 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700305 ASSERT_THAT(attr, NotNull());
306 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700309 ASSERT_THAT(attr, NotNull());
310 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800311}
312
Adam Lesinskia45893a2017-05-30 15:19:02 -0700313// Old AAPT allowed attributes to be defined under different configurations, but ultimately
314// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700315TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700317 std::string input = R"(
318 <attr name="foo" />
319 <declare-styleable name="bar">
320 <attr name="baz" />
321 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800323
Adam Lesinskia45893a2017-05-30 15:19:02 -0700324 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
325 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
326 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800327
Adam Lesinskia45893a2017-05-30 15:19:02 -0700328 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
329 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
330 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800331}
332
Adam Lesinskia5870652015-11-20 15:32:30 -0800333TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700334 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700338 ASSERT_THAT(attr, NotNull());
339 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
340 EXPECT_THAT(attr->min_int, Eq(10));
341 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800342}
343
344TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700345 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800346}
347
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800348TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700349 std::string input = R"(
350 <declare-styleable name="Styleable">
351 <attr name="foo" />
352 </declare-styleable>
353 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700357 ASSERT_THAT(attr, NotNull());
358 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359}
360
361TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700362 std::string input = R"(
363 <declare-styleable name="Theme">
364 <attr name="foo" />
365 </declare-styleable>
366 <declare-styleable name="Window">
367 <attr name="foo" format="boolean"/>
368 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800370
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700372 ASSERT_THAT(attr, NotNull());
373 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374}
375
376TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700377 std::string input = R"(
378 <attr name="foo">
379 <enum name="bar" value="0"/>
380 <enum name="bat" value="1"/>
381 <enum name="baz" value="2"/>
382 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700386 ASSERT_THAT(enum_attr, NotNull());
387 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
388 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800389
Adam Lesinskia45893a2017-05-30 15:19:02 -0700390 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
391 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
392 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800393
Adam Lesinskia45893a2017-05-30 15:19:02 -0700394 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
395 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
396 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397
Adam Lesinskia45893a2017-05-30 15:19:02 -0700398 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
399 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
400 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800401}
402
403TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700404 std::string input = R"(
405 <attr name="foo">
406 <flag name="bar" value="0"/>
407 <flag name="bat" value="1"/>
408 <flag name="baz" value="2"/>
409 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700413 ASSERT_THAT(flag_attr, NotNull());
414 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
415 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800416
Adam Lesinskia45893a2017-05-30 15:19:02 -0700417 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
418 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
419 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420
Adam Lesinskia45893a2017-05-30 15:19:02 -0700421 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
422 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
423 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424
Adam Lesinskia45893a2017-05-30 15:19:02 -0700425 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
426 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
427 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 std::unique_ptr<BinaryPrimitive> flag_value =
430 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700431 ASSERT_THAT(flag_value, NotNull());
432 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433}
434
435TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700436 std::string input = R"(
437 <attr name="foo">
438 <enum name="bar" value="0"/>
439 <enum name="bat" value="1"/>
440 <enum name="bat" value="2"/>
441 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800443}
444
445TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700446 std::string input = R"(
447 <style name="foo" parent="@style/fu">
448 <item name="bar">#ffffffff</item>
449 <item name="bat">@string/hey</item>
450 <item name="baz"><b>hey</b></item>
451 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700455 ASSERT_THAT(style, NotNull());
456 ASSERT_TRUE(style->parent);
457 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
458 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459
Adam Lesinskia45893a2017-05-30 15:19:02 -0700460 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
461 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
462 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463}
464
Adam Lesinski769de982015-04-10 19:43:55 -0700465TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700466 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700469 ASSERT_THAT(style, NotNull());
470 ASSERT_TRUE(style->parent);
471 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700472}
473
Adam Lesinski24aad162015-04-24 19:19:30 -0700474TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700475 std::string input = R"(
476 <style xmlns:app="http://schemas.android.com/apk/res/android"
477 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700481 ASSERT_THAT(style, NotNull());
482 ASSERT_TRUE(style->parent);
483 ASSERT_TRUE(style->parent.value().name);
484 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700485}
486
487TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700488 std::string input = R"(
489 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
490 <item name="app:bar">0</item>
491 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700495 ASSERT_THAT(style, NotNull());
496 ASSERT_THAT(style->entries, SizeIs(1));
497 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700498}
499
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700500TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700501 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700502
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700504 ASSERT_THAT(style, NotNull());
505 ASSERT_TRUE(style->parent);
506 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700508}
509
Adam Lesinskia45893a2017-05-30 15:19:02 -0700510TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
511 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700514 ASSERT_THAT(style, NotNull());
515 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700517}
518
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800519TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700520 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700523 ASSERT_THAT(style, NotNull());
524 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800526}
527
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700529 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
530 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800531}
532
533TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700534 std::string input = R"(
535 <declare-styleable name="foo">
536 <attr name="bar" />
537 <attr name="bat" format="string|reference"/>
538 <attr name="baz">
539 <enum name="foo" value="1"/>
540 </attr>
541 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800543
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700546 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800547 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700550 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800552
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700554 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800556
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700558 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700560 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700561
Adam Lesinskia45893a2017-05-30 15:19:02 -0700562 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700565 ASSERT_THAT(styleable, NotNull());
566 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567
Adam Lesinskia45893a2017-05-30 15:19:02 -0700568 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
569 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
570 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571}
572
Adam Lesinski467f1712015-11-16 17:35:44 -0800573TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700574 std::string input = R"(
575 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
576 name="foo">
577 <attr name="*android:bar" />
578 <attr name="privAndroid:bat" />
579 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 ASSERT_TRUE(TestParse(input));
581 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700582 ASSERT_THAT(styleable, NotNull());
583 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700586 ASSERT_TRUE(styleable->entries[0].name);
587 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700590 ASSERT_TRUE(styleable->entries[1].name);
591 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800592}
593
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700595 std::string input = R"(
596 <array name="foo">
597 <item>@string/ref</item>
598 <item>hey</item>
599 <item>23</item>
600 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700604 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700605 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606
Adam Lesinski4ffea042017-08-10 15:37:28 -0700607 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
608 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
609 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800610}
611
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700612TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700613 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700614 <string-array name="foo">
615 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700616 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700618 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700619}
620
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700621TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700622 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700623 <array name="foo" format="string">
624 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700625 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700626 ASSERT_TRUE(TestParse(input));
627
628 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700629 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700630 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700631
Adam Lesinski4ffea042017-08-10 15:37:28 -0700632 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700633 ASSERT_THAT(str, NotNull());
634 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700635}
636
637TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700638 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700639 <array name="foo" format="integer">
640 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700641 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700642 ASSERT_FALSE(TestParse(input));
643}
644
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800645TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700646 std::string input = R"(
647 <plurals name="foo">
648 <item quantity="other">apples</item>
649 <item quantity="one">apple</item>
650 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800652
653 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700654 ASSERT_THAT(plural, NotNull());
655 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
656 EXPECT_THAT(plural->values[Plural::Two], IsNull());
657 EXPECT_THAT(plural->values[Plural::Few], IsNull());
658 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800659
Adam Lesinskia45893a2017-05-30 15:19:02 -0700660 EXPECT_THAT(plural->values[Plural::One], NotNull());
661 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800662}
663
664TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700665 std::string input = R"(
666 <!--This is a comment-->
667 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800669
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700671 ASSERT_THAT(value, NotNull());
672 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700673}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700674
Adam Lesinskie78fd612015-10-22 12:48:43 -0700675TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700676 std::string input = R"(
677 <!--One-->
678 <!--Two-->
679 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700684 ASSERT_THAT(value, NotNull());
685 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700686}
687
688TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700689 std::string input = R"(
690 <!--One-->
691 <string name="foo">
692 Hi
693 <!--Two-->
694 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700696
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700698 ASSERT_THAT(value, NotNull());
699 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800700}
701
Adam Lesinskica5638f2015-10-21 14:42:43 -0700702TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700704 // comments from those end up in R.java
705 std::string input = R"(
706 <declare-styleable name="foo">
707 <!-- The name of the bar -->
708 <attr name="barName" format="string|reference" />
709 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700710
Adam Lesinskia45893a2017-05-30 15:19:02 -0700711 <attr name="foo">
712 <!-- The very first -->
713 <enum name="one" value="1" />
714 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700718 ASSERT_THAT(styleable, NotNull());
719 ASSERT_THAT(styleable->entries, SizeIs(1));
720 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700721
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700723 ASSERT_THAT(attr, NotNull());
724 ASSERT_THAT(attr->symbols, SizeIs(1));
725 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700726}
727
Adam Lesinskia45893a2017-05-30 15:19:02 -0700728// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800729TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700730 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
731 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732}
733
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800734TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700735 std::string input = R"(
736 <string name="foo" product="phone">hi</string>
737 <string name="foo" product="no-sdcard">ho</string>
738 <string name="bar" product="">wee</string>
739 <string name="baz">woo</string>
740 <string name="bit" product="phablet">hoot</string>
741 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700743
Adam Lesinskia45893a2017-05-30 15:19:02 -0700744 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
745 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
746 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
747 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
748 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
749 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700750}
751
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800752TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700753 std::string input = R"(
754 <public-group type="attr" first-id="0x01010040">
755 <public name="foo" />
756 <public name="bar" />
757 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800759
Adam Lesinskia45893a2017-05-30 15:19:02 -0700760 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
761 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800762
Adam Lesinskia45893a2017-05-30 15:19:02 -0700763 ASSERT_TRUE(result.value().package->id);
764 ASSERT_TRUE(result.value().type->id);
765 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 ResourceId actual_id(result.value().package->id.value(),
767 result.value().type->id.value(),
768 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700769 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700771 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700772 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773
Adam Lesinskia45893a2017-05-30 15:19:02 -0700774 ASSERT_TRUE(result.value().package->id);
775 ASSERT_TRUE(result.value().type->id);
776 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 actual_id = ResourceId(result.value().package->id.value(),
778 result.value().type->id.value(),
779 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700780 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800781}
782
Adam Lesinski71be7052017-12-12 16:48:07 -0800783TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
784 std::string input = R"(
785 <!-- private -->
786 <java-symbol type="string" name="foo" />
787 <!-- public -->
788 <public type="string" name="foo" id="0x01020000" />
789 <!-- private2 -->
790 <java-symbol type="string" name="foo" />)";
791 ASSERT_TRUE(TestParse(input));
792
793 Maybe<ResourceTable::SearchResult> result =
794 table_.FindResource(test::ParseNameOrDie("string/foo"));
795 ASSERT_TRUE(result);
796
797 ResourceEntry* entry = result.value().entry;
798 ASSERT_THAT(entry, NotNull());
799 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
800 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
801}
802
Adam Lesinskifa105052015-11-07 13:34:39 -0800803TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700804 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
805 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800806}
807
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700808TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700809 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800810
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700813 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700815 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800816 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
817 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800818}
819
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800820TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700821 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800822
Adam Lesinskie597d682017-06-01 17:16:44 -0700823 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
824 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700825 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800826
Adam Lesinskia45893a2017-05-30 15:19:02 -0700827 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700828}
829
830// An <item> without a format specifier accepts all types of values.
831TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700832 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700833
834 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
835 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700836 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800837}
838
Adam Lesinski86d67df2017-01-31 13:47:27 -0800839TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700840 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
841 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800842}
843
844TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700845 std::string input = R"(
846 <bag name="bag" type="configVarying">
847 <item name="test">Hello!</item>
848 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800849 ASSERT_TRUE(TestParse(input));
850
851 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700852 ASSERT_THAT(val, NotNull());
853 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800854
Adam Lesinskia45893a2017-05-30 15:19:02 -0700855 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
856 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800857}
858
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700859TEST_F(ResourceParserTest, ParseElementWithNoValue) {
860 std::string input = R"(
861 <item type="drawable" format="reference" name="foo" />
862 <string name="foo" />)";
863 ASSERT_TRUE(TestParse(input));
864 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
865
866 String* str = test::GetValue<String>(&table_, "string/foo");
867 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700868 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700869}
870
Adam Lesinskib9f05482017-06-02 16:32:37 -0700871TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700872 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700873}
874
Adam Lesinski46c4d722017-08-23 13:03:56 -0700875TEST_F(ResourceParserTest, ParseOverlayableTagWithSystemPolicy) {
876 std::string input = R"(
877 <overlayable policy="illegal_policy">
878 <item type="string" name="foo" />
879 </overlayable>)";
880 EXPECT_FALSE(TestParse(input));
881
882 input = R"(
883 <overlayable policy="system">
884 <item name="foo" />
885 </overlayable>)";
886 EXPECT_FALSE(TestParse(input));
887
888 input = R"(
889 <overlayable policy="system">
890 <item type="attr" />
891 </overlayable>)";
892 EXPECT_FALSE(TestParse(input));
893
894 input = R"(
895 <overlayable policy="system">
896 <item type="bad_type" name="foo" />
897 </overlayable>)";
898 EXPECT_FALSE(TestParse(input));
899
900 input = R"(<overlayable policy="system" />)";
901 EXPECT_TRUE(TestParse(input));
902
903 input = R"(<overlayable />)";
904 EXPECT_TRUE(TestParse(input));
905
906 input = R"(
907 <overlayable policy="system">
908 <item type="string" name="foo" />
909 <item type="dimen" name="foo" />
910 </overlayable>)";
911 ASSERT_TRUE(TestParse(input));
912
913 input = R"(
914 <overlayable>
915 <item type="string" name="bar" />
916 </overlayable>)";
917 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -0800918
919 Maybe<ResourceTable::SearchResult> search_result =
920 table_.FindResource(test::ParseNameOrDie("string/bar"));
921 ASSERT_TRUE(search_result);
922 ASSERT_THAT(search_result.value().entry, NotNull());
923 EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kUndefined));
924 EXPECT_TRUE(search_result.value().entry->overlayable);
925}
926
927TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
928 std::string input = R"(
929 <overlayable>
930 <item type="string" name="foo" />
931 <item type="string" name="foo" />
932 </overlayable>)";
933 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -0700934}
935
y9efbbef2018-04-18 11:29:09 -0700936TEST_F(ResourceParserTest, ParseIdItem) {
937 std::string input = R"(
938 <item name="foo" type="id">@id/bar</item>
939 <item name="bar" type="id"/>
940 <item name="baz" type="id"></item>)";
941 ASSERT_TRUE(TestParse(input));
942
943 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull());
944 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
945 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
946
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700947 input = R"(
948 <id name="foo2">@id/bar</id>
949 <id name="bar2"/>
950 <id name="baz2"></id>)";
951 ASSERT_TRUE(TestParse(input));
952
953 ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
954 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
955 ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
956
y9efbbef2018-04-18 11:29:09 -0700957 // Reject attribute references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700958 input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
y9efbbef2018-04-18 11:29:09 -0700959 ASSERT_FALSE(TestParse(input));
960
961 // Reject non-references
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700962 input = R"(<item name="foo4" type="id">0x7f010001</item>)";
y9efbbef2018-04-18 11:29:09 -0700963 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700964 input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
y9efbbef2018-04-18 11:29:09 -0700965 ASSERT_FALSE(TestParse(input));
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700966 input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
y9efbbef2018-04-18 11:29:09 -0700967 ASSERT_FALSE(TestParse(input));
968
969 // Ids that reference other resource ids cannot be public
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700970 input = R"(<public name="foo7" type="id">@id/bar7</item>)";
y9efbbef2018-04-18 11:29:09 -0700971 ASSERT_FALSE(TestParse(input));
972}
973
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974} // namespace aapt