blob: 618c8ed4afd15136bc66c5a22c16cb09d1d9cd34 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080098}
99
100TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700101 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700104 ASSERT_THAT(str, NotNull());
105 EXPECT_THAT(*str, StrValueEq("?123"));
106 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski549e4372017-06-27 18:39:07 -0700107
108 ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)"));
109 str = test::GetValue<String>(&table_, "string/bar");
110 ASSERT_THAT(str, NotNull());
111 EXPECT_THAT(*str, StrValueEq("This isn’t a bad string"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112}
113
Adam Lesinski9f222042015-11-04 13:51:45 -0800114TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700115 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
116 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800117}
118
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700119TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800121 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700123 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700127 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700128
Adam Lesinski060b53d2017-07-28 17:10:35 -0700129 EXPECT_THAT(str->value->value, Eq("This is my aunt\u2019s fickle string"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700130 EXPECT_THAT(str->value->spans, SizeIs(2));
131 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700132
Adam Lesinskia45893a2017-05-30 15:19:02 -0700133 EXPECT_THAT(*str->value->spans[0].name, Eq("b"));
134 EXPECT_THAT(str->value->spans[0].first_char, Eq(17u));
135 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700136
Adam Lesinskia45893a2017-05-30 15:19:02 -0700137 EXPECT_THAT(*str->value->spans[1].name, Eq("small"));
138 EXPECT_THAT(str->value->spans[1].first_char, Eq(24u));
139 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700140}
141
142TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700143 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700146 ASSERT_THAT(str, NotNull());
147 EXPECT_THAT(*str->value, Eq("This is what I think"));
148 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700149
Adam Lesinskia45893a2017-05-30 15:19:02 -0700150 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700153 ASSERT_THAT(str, NotNull());
154 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700155}
156
Adam Lesinski75421622017-01-06 15:20:04 -0800157TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700158 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800159 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700160 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800161 ASSERT_TRUE(TestParse(input));
162
163 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700164 ASSERT_THAT(str, NotNull());
165 EXPECT_THAT(*str, StrValueEq("There are no apples"));
166 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800167}
168
169TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700170 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800171 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700172 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800173 EXPECT_FALSE(TestParse(input));
174}
175
176TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700177 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800178 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700179 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700183 ASSERT_THAT(str, NotNull());
184 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
185 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800186
187 // We expect indices and lengths that span to include the whitespace
188 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
189 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700190 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
191 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800192}
193
194TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700195 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800196 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700197 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800198 ASSERT_TRUE(TestParse(input));
199
200 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700201 ASSERT_THAT(str, NotNull());
Adam Lesinski060b53d2017-07-28 17:10:35 -0700202 EXPECT_THAT(str->value->value, Eq("There are %1$d apples"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700203 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800204
205 // We expect indices and lengths that span to include the whitespace
206 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
207 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700208 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
209 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210}
211
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700212TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700213 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700215
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
217 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800218 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700220 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
221 ASSERT_THAT(null_ref, NotNull());
222 EXPECT_FALSE(null_ref->name);
223 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700224 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700225}
226
227TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700228 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700230
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700231 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700232 ASSERT_THAT(integer, NotNull());
233 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
234 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700235}
236
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700238 std::string input = R"(
239 <attr name="foo" format="string"/>
240 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700244 ASSERT_THAT(attr, NotNull());
245 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700248 ASSERT_THAT(attr, NotNull());
249 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250}
251
Adam Lesinskia45893a2017-05-30 15:19:02 -0700252// Old AAPT allowed attributes to be defined under different configurations, but ultimately
253// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700254TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700256 std::string input = R"(
257 <attr name="foo" />
258 <declare-styleable name="bar">
259 <attr name="baz" />
260 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800262
Adam Lesinskia45893a2017-05-30 15:19:02 -0700263 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
264 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
265 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800266
Adam Lesinskia45893a2017-05-30 15:19:02 -0700267 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
268 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
269 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800270}
271
Adam Lesinskia5870652015-11-20 15:32:30 -0800272TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700273 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700277 ASSERT_THAT(attr, NotNull());
278 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
279 EXPECT_THAT(attr->min_int, Eq(10));
280 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800281}
282
283TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700284 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800285}
286
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700288 std::string input = R"(
289 <declare-styleable name="Styleable">
290 <attr name="foo" />
291 </declare-styleable>
292 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700296 ASSERT_THAT(attr, NotNull());
297 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298}
299
300TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700301 std::string input = R"(
302 <declare-styleable name="Theme">
303 <attr name="foo" />
304 </declare-styleable>
305 <declare-styleable name="Window">
306 <attr name="foo" format="boolean"/>
307 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700311 ASSERT_THAT(attr, NotNull());
312 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313}
314
315TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700316 std::string input = R"(
317 <attr name="foo">
318 <enum name="bar" value="0"/>
319 <enum name="bat" value="1"/>
320 <enum name="baz" value="2"/>
321 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700325 ASSERT_THAT(enum_attr, NotNull());
326 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
327 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328
Adam Lesinskia45893a2017-05-30 15:19:02 -0700329 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
330 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
331 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800332
Adam Lesinskia45893a2017-05-30 15:19:02 -0700333 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
334 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
335 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336
Adam Lesinskia45893a2017-05-30 15:19:02 -0700337 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
338 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
339 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800340}
341
342TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700343 std::string input = R"(
344 <attr name="foo">
345 <flag name="bar" value="0"/>
346 <flag name="bat" value="1"/>
347 <flag name="baz" value="2"/>
348 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700352 ASSERT_THAT(flag_attr, NotNull());
353 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
354 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800355
Adam Lesinskia45893a2017-05-30 15:19:02 -0700356 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
357 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
358 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359
Adam Lesinskia45893a2017-05-30 15:19:02 -0700360 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
361 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
362 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363
Adam Lesinskia45893a2017-05-30 15:19:02 -0700364 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
365 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
366 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 std::unique_ptr<BinaryPrimitive> flag_value =
369 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700370 ASSERT_THAT(flag_value, NotNull());
371 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372}
373
374TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700375 std::string input = R"(
376 <attr name="foo">
377 <enum name="bar" value="0"/>
378 <enum name="bat" value="1"/>
379 <enum name="bat" value="2"/>
380 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800382}
383
384TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700385 std::string input = R"(
386 <style name="foo" parent="@style/fu">
387 <item name="bar">#ffffffff</item>
388 <item name="bat">@string/hey</item>
389 <item name="baz"><b>hey</b></item>
390 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700394 ASSERT_THAT(style, NotNull());
395 ASSERT_TRUE(style->parent);
396 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
397 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800398
Adam Lesinskia45893a2017-05-30 15:19:02 -0700399 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
400 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
401 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402}
403
Adam Lesinski769de982015-04-10 19:43:55 -0700404TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700405 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700406
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700408 ASSERT_THAT(style, NotNull());
409 ASSERT_TRUE(style->parent);
410 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700411}
412
Adam Lesinski24aad162015-04-24 19:19:30 -0700413TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700414 std::string input = R"(
415 <style xmlns:app="http://schemas.android.com/apk/res/android"
416 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700420 ASSERT_THAT(style, NotNull());
421 ASSERT_TRUE(style->parent);
422 ASSERT_TRUE(style->parent.value().name);
423 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700424}
425
426TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700427 std::string input = R"(
428 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
429 <item name="app:bar">0</item>
430 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700434 ASSERT_THAT(style, NotNull());
435 ASSERT_THAT(style->entries, SizeIs(1));
436 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700437}
438
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700439TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700440 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700443 ASSERT_THAT(style, NotNull());
444 ASSERT_TRUE(style->parent);
445 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700447}
448
Adam Lesinskia45893a2017-05-30 15:19:02 -0700449TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
450 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700451
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700453 ASSERT_THAT(style, NotNull());
454 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700456}
457
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800458TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700459 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700462 ASSERT_THAT(style, NotNull());
463 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800465}
466
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800467TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700468 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
469 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470}
471
472TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700473 std::string input = R"(
474 <declare-styleable name="foo">
475 <attr name="bar" />
476 <attr name="bat" format="string|reference"/>
477 <attr name="baz">
478 <enum name="foo" value="1"/>
479 </attr>
480 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700485 ASSERT_TRUE(result);
Adam Lesinski71be7052017-12-12 16:48:07 -0800486 EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700489 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700493 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700497 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700499 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700500
Adam Lesinskia45893a2017-05-30 15:19:02 -0700501 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700502
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700504 ASSERT_THAT(styleable, NotNull());
505 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800506
Adam Lesinskia45893a2017-05-30 15:19:02 -0700507 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
508 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
509 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510}
511
Adam Lesinski467f1712015-11-16 17:35:44 -0800512TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700513 std::string input = R"(
514 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
515 name="foo">
516 <attr name="*android:bar" />
517 <attr name="privAndroid:bat" />
518 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 ASSERT_TRUE(TestParse(input));
520 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700521 ASSERT_THAT(styleable, NotNull());
522 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700525 ASSERT_TRUE(styleable->entries[0].name);
526 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700529 ASSERT_TRUE(styleable->entries[1].name);
530 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800531}
532
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700534 std::string input = R"(
535 <array name="foo">
536 <item>@string/ref</item>
537 <item>hey</item>
538 <item>23</item>
539 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700543 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700544 ASSERT_THAT(array->elements, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800545
Adam Lesinski4ffea042017-08-10 15:37:28 -0700546 EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull());
547 EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull());
548 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549}
550
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700551TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700552 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700553 <string-array name="foo">
554 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700555 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700557 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700558}
559
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700560TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700561 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700562 <array name="foo" format="string">
563 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700564 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700565 ASSERT_TRUE(TestParse(input));
566
567 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700568 ASSERT_THAT(array, NotNull());
Adam Lesinski4ffea042017-08-10 15:37:28 -0700569 ASSERT_THAT(array->elements, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700570
Adam Lesinski4ffea042017-08-10 15:37:28 -0700571 String* str = ValueCast<String>(array->elements[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700572 ASSERT_THAT(str, NotNull());
573 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700574}
575
576TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700577 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700578 <array name="foo" format="integer">
579 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700580 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700581 ASSERT_FALSE(TestParse(input));
582}
583
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700585 std::string input = R"(
586 <plurals name="foo">
587 <item quantity="other">apples</item>
588 <item quantity="one">apple</item>
589 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800591
592 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700593 ASSERT_THAT(plural, NotNull());
594 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
595 EXPECT_THAT(plural->values[Plural::Two], IsNull());
596 EXPECT_THAT(plural->values[Plural::Few], IsNull());
597 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800598
Adam Lesinskia45893a2017-05-30 15:19:02 -0700599 EXPECT_THAT(plural->values[Plural::One], NotNull());
600 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800601}
602
603TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700604 std::string input = R"(
605 <!--This is a comment-->
606 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700610 ASSERT_THAT(value, NotNull());
611 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700612}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613
Adam Lesinskie78fd612015-10-22 12:48:43 -0700614TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700615 std::string input = R"(
616 <!--One-->
617 <!--Two-->
618 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700619
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700623 ASSERT_THAT(value, NotNull());
624 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700625}
626
627TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700628 std::string input = R"(
629 <!--One-->
630 <string name="foo">
631 Hi
632 <!--Two-->
633 </string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700637 ASSERT_THAT(value, NotNull());
638 EXPECT_THAT(value->GetComment(), Eq("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800639}
640
Adam Lesinskica5638f2015-10-21 14:42:43 -0700641TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700643 // comments from those end up in R.java
644 std::string input = R"(
645 <declare-styleable name="foo">
646 <!-- The name of the bar -->
647 <attr name="barName" format="string|reference" />
648 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700649
Adam Lesinskia45893a2017-05-30 15:19:02 -0700650 <attr name="foo">
651 <!-- The very first -->
652 <enum name="one" value="1" />
653 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700657 ASSERT_THAT(styleable, NotNull());
658 ASSERT_THAT(styleable->entries, SizeIs(1));
659 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700660
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700662 ASSERT_THAT(attr, NotNull());
663 ASSERT_THAT(attr->symbols, SizeIs(1));
664 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700665}
666
Adam Lesinskia45893a2017-05-30 15:19:02 -0700667// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800668TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700669 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
670 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800671}
672
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800673TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700674 std::string input = R"(
675 <string name="foo" product="phone">hi</string>
676 <string name="foo" product="no-sdcard">ho</string>
677 <string name="bar" product="">wee</string>
678 <string name="baz">woo</string>
679 <string name="bit" product="phablet">hoot</string>
680 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700682
Adam Lesinskia45893a2017-05-30 15:19:02 -0700683 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
684 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
685 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
686 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
687 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
688 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700689}
690
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800691TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700692 std::string input = R"(
693 <public-group type="attr" first-id="0x01010040">
694 <public name="foo" />
695 <public name="bar" />
696 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800698
Adam Lesinskia45893a2017-05-30 15:19:02 -0700699 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
700 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800701
Adam Lesinskia45893a2017-05-30 15:19:02 -0700702 ASSERT_TRUE(result.value().package->id);
703 ASSERT_TRUE(result.value().type->id);
704 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 ResourceId actual_id(result.value().package->id.value(),
706 result.value().type->id.value(),
707 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700708 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700711 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712
Adam Lesinskia45893a2017-05-30 15:19:02 -0700713 ASSERT_TRUE(result.value().package->id);
714 ASSERT_TRUE(result.value().type->id);
715 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 actual_id = ResourceId(result.value().package->id.value(),
717 result.value().type->id.value(),
718 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700719 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800720}
721
Adam Lesinski71be7052017-12-12 16:48:07 -0800722TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) {
723 std::string input = R"(
724 <!-- private -->
725 <java-symbol type="string" name="foo" />
726 <!-- public -->
727 <public type="string" name="foo" id="0x01020000" />
728 <!-- private2 -->
729 <java-symbol type="string" name="foo" />)";
730 ASSERT_TRUE(TestParse(input));
731
732 Maybe<ResourceTable::SearchResult> result =
733 table_.FindResource(test::ParseNameOrDie("string/foo"));
734 ASSERT_TRUE(result);
735
736 ResourceEntry* entry = result.value().entry;
737 ASSERT_THAT(entry, NotNull());
738 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic));
739 EXPECT_THAT(entry->visibility.comment, StrEq("public"));
740}
741
Adam Lesinskifa105052015-11-07 13:34:39 -0800742TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700743 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
744 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800745}
746
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700747TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700748 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800749
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700752 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700754 ASSERT_THAT(entry, NotNull());
Adam Lesinski71be7052017-12-12 16:48:07 -0800755 EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined));
756 EXPECT_TRUE(entry->allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800757}
758
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800759TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700760 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800761
Adam Lesinskie597d682017-06-01 17:16:44 -0700762 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
763 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700764 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800765
Adam Lesinskia45893a2017-05-30 15:19:02 -0700766 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700767}
768
769// An <item> without a format specifier accepts all types of values.
770TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700771 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700772
773 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
774 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700775 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800776}
777
Adam Lesinski86d67df2017-01-31 13:47:27 -0800778TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700779 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
780 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800781}
782
783TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700784 std::string input = R"(
785 <bag name="bag" type="configVarying">
786 <item name="test">Hello!</item>
787 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800788 ASSERT_TRUE(TestParse(input));
789
790 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700791 ASSERT_THAT(val, NotNull());
792 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800793
Adam Lesinskia45893a2017-05-30 15:19:02 -0700794 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
795 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800796}
797
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700798TEST_F(ResourceParserTest, ParseElementWithNoValue) {
799 std::string input = R"(
800 <item type="drawable" format="reference" name="foo" />
801 <string name="foo" />)";
802 ASSERT_TRUE(TestParse(input));
803 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
804
805 String* str = test::GetValue<String>(&table_, "string/foo");
806 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700807 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700808}
809
Adam Lesinskib9f05482017-06-02 16:32:37 -0700810TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700811 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700812}
813
Adam Lesinski46c4d722017-08-23 13:03:56 -0700814TEST_F(ResourceParserTest, ParseOverlayableTagWithSystemPolicy) {
815 std::string input = R"(
816 <overlayable policy="illegal_policy">
817 <item type="string" name="foo" />
818 </overlayable>)";
819 EXPECT_FALSE(TestParse(input));
820
821 input = R"(
822 <overlayable policy="system">
823 <item name="foo" />
824 </overlayable>)";
825 EXPECT_FALSE(TestParse(input));
826
827 input = R"(
828 <overlayable policy="system">
829 <item type="attr" />
830 </overlayable>)";
831 EXPECT_FALSE(TestParse(input));
832
833 input = R"(
834 <overlayable policy="system">
835 <item type="bad_type" name="foo" />
836 </overlayable>)";
837 EXPECT_FALSE(TestParse(input));
838
839 input = R"(<overlayable policy="system" />)";
840 EXPECT_TRUE(TestParse(input));
841
842 input = R"(<overlayable />)";
843 EXPECT_TRUE(TestParse(input));
844
845 input = R"(
846 <overlayable policy="system">
847 <item type="string" name="foo" />
848 <item type="dimen" name="foo" />
849 </overlayable>)";
850 ASSERT_TRUE(TestParse(input));
851
852 input = R"(
853 <overlayable>
854 <item type="string" name="bar" />
855 </overlayable>)";
856 ASSERT_TRUE(TestParse(input));
Adam Lesinski71be7052017-12-12 16:48:07 -0800857
858 Maybe<ResourceTable::SearchResult> search_result =
859 table_.FindResource(test::ParseNameOrDie("string/bar"));
860 ASSERT_TRUE(search_result);
861 ASSERT_THAT(search_result.value().entry, NotNull());
862 EXPECT_THAT(search_result.value().entry->visibility.level, Eq(Visibility::Level::kUndefined));
863 EXPECT_TRUE(search_result.value().entry->overlayable);
864}
865
866TEST_F(ResourceParserTest, DuplicateOverlayableIsError) {
867 std::string input = R"(
868 <overlayable>
869 <item type="string" name="foo" />
870 <item type="string" name="foo" />
871 </overlayable>)";
872 EXPECT_FALSE(TestParse(input));
Adam Lesinski46c4d722017-08-23 13:03:56 -0700873}
874
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875} // namespace aapt