blob: e189144108716ee61c3d3c1e562713817cba9789 [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 Lesinskid0f116b2016-07-08 15:00:32 -070025#include "test/Test.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080026#include "xml/XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskia45893a2017-05-30 15:19:02 -070028using ::aapt::test::StrValueEq;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070029using ::aapt::test::ValueEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070030using ::android::ResTable_map;
31using ::android::Res_value;
Adam Lesinskie597d682017-06-01 17:16:44 -070032using ::android::StringPiece;
33using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070034using ::testing::IsEmpty;
35using ::testing::IsNull;
Adam Lesinskie597d682017-06-01 17:16:44 -070036using ::testing::NotNull;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070037using ::testing::Pointee;
Adam Lesinskia45893a2017-05-30 15:19:02 -070038using ::testing::SizeIs;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskibab4ef52017-06-01 15:22:57 -070042constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 std::stringstream input(kXmlPreamble);
Adam Lesinskibab4ef52017-06-01 15:22:57 -070047 input << R"(<attr name="foo"/>)" << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {});
50 xml::XmlPullParser xml_parser(input);
51 ASSERT_FALSE(parser.Parse(&xml_parser));
Adam Lesinski769de982015-04-10 19:43:55 -070052}
53
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054class ResourceParserTest : public ::testing::Test {
55 public:
Adam Lesinskibab4ef52017-06-01 15:22:57 -070056 void SetUp() override {
57 context_ = test::ContextBuilder().Build();
58 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 ::testing::AssertionResult TestParse(const StringPiece& str) {
61 return TestParse(str, ConfigDescription{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Adam Lesinski52364f72016-01-11 13:10:24 -080063
Adam Lesinskibab4ef52017-06-01 15:22:57 -070064 ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 std::stringstream input(kXmlPreamble);
66 input << "<resources>\n" << str << "\n</resources>" << std::endl;
67 ResourceParserOptions parserOptions;
Adam Lesinskibab4ef52017-06-01 15:22:57 -070068 ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config,
69 parserOptions);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 xml::XmlPullParser xmlParser(input);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 if (parser.Parse(&xmlParser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 return ::testing::AssertionFailure();
75 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076
77 protected:
78 ResourceTable table_;
79 std::unique_ptr<IAaptContext> context_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080};
81
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082TEST_F(ResourceParserTest, ParseQuotedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070083 ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070086 ASSERT_THAT(str, NotNull());
87 EXPECT_THAT(*str, StrValueEq(" hey there "));
88 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089}
90
91TEST_F(ResourceParserTest, ParseEscapedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -070092 ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</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("?123"));
97 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098}
99
Adam Lesinski9f222042015-11-04 13:51:45 -0800100TEST_F(ResourceParserTest, ParseFormattedString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700101 ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)"));
102 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)"));
Adam Lesinski9f222042015-11-04 13:51:45 -0800103}
104
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700105TEST_F(ResourceParserTest, ParseStyledString) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 // Use a surrogate pair unicode point so that we can verify that the span
Adam Lesinski75421622017-01-06 15:20:04 -0800107 // indices use UTF-16 length and not UTF-8 length.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 std::string input =
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700109 "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 ASSERT_TRUE(TestParse(input));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700113 ASSERT_THAT(str, NotNull());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700114
Adam Lesinskia45893a2017-05-30 15:19:02 -0700115 EXPECT_THAT(*str->value->str, Eq("This is my aunt\u2019s fickle string"));
116 EXPECT_THAT(str->value->spans, SizeIs(2));
117 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700118
Adam Lesinskia45893a2017-05-30 15:19:02 -0700119 EXPECT_THAT(*str->value->spans[0].name, Eq("b"));
120 EXPECT_THAT(str->value->spans[0].first_char, Eq(17u));
121 EXPECT_THAT(str->value->spans[0].last_char, Eq(30u));
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700122
Adam Lesinskia45893a2017-05-30 15:19:02 -0700123 EXPECT_THAT(*str->value->spans[1].name, Eq("small"));
124 EXPECT_THAT(str->value->spans[1].first_char, Eq(24u));
125 EXPECT_THAT(str->value->spans[1].last_char, Eq(30u));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700126}
127
128TEST_F(ResourceParserTest, ParseStringWithWhitespace) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700129 ASSERT_TRUE(TestParse(R"(<string name="foo"> This is what I think </string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700132 ASSERT_THAT(str, NotNull());
133 EXPECT_THAT(*str->value, Eq("This is what I think"));
134 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700135
Adam Lesinskia45893a2017-05-30 15:19:02 -0700136 ASSERT_TRUE(TestParse(R"(<string name="foo2">" This is what I think "</string>)"));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 str = test::GetValue<String>(&table_, "string/foo2");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700139 ASSERT_THAT(str, NotNull());
140 EXPECT_THAT(*str, StrValueEq(" This is what I think "));
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700141}
142
Adam Lesinski75421622017-01-06 15:20:04 -0800143TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700144 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800145 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700146 There are <xliff:source>no</xliff:source> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800147 ASSERT_TRUE(TestParse(input));
148
149 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700150 ASSERT_THAT(str, NotNull());
151 EXPECT_THAT(*str, StrValueEq("There are no apples"));
152 EXPECT_THAT(str->untranslatable_sections, IsEmpty());
Adam Lesinski75421622017-01-06 15:20:04 -0800153}
154
155TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800157 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700158 Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800159 EXPECT_FALSE(TestParse(input));
160}
161
162TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800164 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700165 There are <xliff:g id="count">%1$d</xliff:g> apples</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 ASSERT_TRUE(TestParse(input));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 String* str = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700169 ASSERT_THAT(str, NotNull());
170 EXPECT_THAT(*str, StrValueEq("There are %1$d apples"));
171 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800172
173 // We expect indices and lengths that span to include the whitespace
174 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
175 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700176 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
177 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski75421622017-01-06 15:20:04 -0800178}
179
180TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700181 std::string input = R"(
Adam Lesinski75421622017-01-06 15:20:04 -0800182 <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Adam Lesinskia45893a2017-05-30 15:19:02 -0700183 There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)";
Adam Lesinski75421622017-01-06 15:20:04 -0800184 ASSERT_TRUE(TestParse(input));
185
186 StyledString* str = test::GetValue<StyledString>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700187 ASSERT_THAT(str, NotNull());
188 EXPECT_THAT(*str->value->str, Eq("There are %1$d apples"));
189 ASSERT_THAT(str->untranslatable_sections, SizeIs(1));
Adam Lesinski75421622017-01-06 15:20:04 -0800190
191 // We expect indices and lengths that span to include the whitespace
192 // before %1$d. This is due to how the StringBuilder withholds whitespace unless
193 // needed (to deal with line breaks, etc.).
Adam Lesinskia45893a2017-05-30 15:19:02 -0700194 EXPECT_THAT(str->untranslatable_sections[0].start, Eq(9u));
195 EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196}
197
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700198TEST_F(ResourceParserTest, ParseNull) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700199 std::string input = R"(<integer name="foo">@null</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 // The Android runtime treats a value of android::Res_value::TYPE_NULL as
203 // a non-existing value, and this causes problems in styles when trying to
Adam Lesinski75421622017-01-06 15:20:04 -0800204 // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 // with a data value of 0.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700206 Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo");
207 ASSERT_THAT(null_ref, NotNull());
208 EXPECT_FALSE(null_ref->name);
209 EXPECT_FALSE(null_ref->id);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700210 EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700211}
212
213TEST_F(ResourceParserTest, ParseEmpty) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700214 std::string input = R"(<integer name="foo">@empty</integer>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 ASSERT_TRUE(TestParse(input));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700216
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700217 BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700218 ASSERT_THAT(integer, NotNull());
219 EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL));
220 EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY));
Adam Lesinskidfa5e072015-05-12 21:42:59 -0700221}
222
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223TEST_F(ResourceParserTest, ParseAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700224 std::string input = R"(
225 <attr name="foo" format="string"/>
226 <attr name="bar"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700230 ASSERT_THAT(attr, NotNull());
231 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700234 ASSERT_THAT(attr, NotNull());
235 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236}
237
Adam Lesinskia45893a2017-05-30 15:19:02 -0700238// Old AAPT allowed attributes to be defined under different configurations, but ultimately
239// stored them with the default configuration. Check that we have the same behavior.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700240TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 const ConfigDescription watch_config = test::ParseConfigOrDie("watch");
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700242 std::string input = R"(
243 <attr name="foo" />
244 <declare-styleable name="bar">
245 <attr name="baz" />
246 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 ASSERT_TRUE(TestParse(input, watch_config));
Adam Lesinski52364f72016-01-11 13:10:24 -0800248
Adam Lesinskia45893a2017-05-30 15:19:02 -0700249 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull());
250 EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull());
251 EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800252
Adam Lesinskia45893a2017-05-30 15:19:02 -0700253 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull());
254 EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull());
255 EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800256}
257
Adam Lesinskia5870652015-11-20 15:32:30 -0800258TEST_F(ResourceParserTest, ParseAttrWithMinMax) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700259 std::string input = R"(<attr name="foo" min="10" max="23" format="integer"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 ASSERT_TRUE(TestParse(input));
Adam Lesinskia5870652015-11-20 15:32:30 -0800261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700263 ASSERT_THAT(attr, NotNull());
264 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER));
265 EXPECT_THAT(attr->min_int, Eq(10));
266 EXPECT_THAT(attr->max_int, Eq(23));
Adam Lesinskia5870652015-11-20 15:32:30 -0800267}
268
269TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700270 ASSERT_FALSE(TestParse(R"(<attr name="foo" min="10" max="23" format="string"/>)"));
Adam Lesinskia5870652015-11-20 15:32:30 -0800271}
272
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700274 std::string input = R"(
275 <declare-styleable name="Styleable">
276 <attr name="foo" />
277 </declare-styleable>
278 <attr name="foo" format="string"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700282 ASSERT_THAT(attr, NotNull());
283 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284}
285
286TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700287 std::string input = R"(
288 <declare-styleable name="Theme">
289 <attr name="foo" />
290 </declare-styleable>
291 <declare-styleable name="Window">
292 <attr name="foo" format="boolean"/>
293 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700297 ASSERT_THAT(attr, NotNull());
298 EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299}
300
301TEST_F(ResourceParserTest, ParseEnumAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700302 std::string input = R"(
303 <attr name="foo">
304 <enum name="bar" value="0"/>
305 <enum name="bat" value="1"/>
306 <enum name="baz" value="2"/>
307 </attr>)";
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* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700311 ASSERT_THAT(enum_attr, NotNull());
312 EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM));
313 ASSERT_THAT(enum_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314
Adam Lesinskia45893a2017-05-30 15:19:02 -0700315 ASSERT_TRUE(enum_attr->symbols[0].symbol.name);
316 EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
317 EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskia45893a2017-05-30 15:19:02 -0700319 ASSERT_TRUE(enum_attr->symbols[1].symbol.name);
320 EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
321 EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322
Adam Lesinskia45893a2017-05-30 15:19:02 -0700323 ASSERT_TRUE(enum_attr->symbols[2].symbol.name);
324 EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
325 EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800326}
327
328TEST_F(ResourceParserTest, ParseFlagAttr) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700329 std::string input = R"(
330 <attr name="foo">
331 <flag name="bar" value="0"/>
332 <flag name="bat" value="1"/>
333 <flag name="baz" value="2"/>
334 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700338 ASSERT_THAT(flag_attr, NotNull());
339 EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS));
340 ASSERT_THAT(flag_attr->symbols, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Adam Lesinskia45893a2017-05-30 15:19:02 -0700342 ASSERT_TRUE(flag_attr->symbols[0].symbol.name);
343 EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar"));
344 EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800345
Adam Lesinskia45893a2017-05-30 15:19:02 -0700346 ASSERT_TRUE(flag_attr->symbols[1].symbol.name);
347 EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat"));
348 EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800349
Adam Lesinskia45893a2017-05-30 15:19:02 -0700350 ASSERT_TRUE(flag_attr->symbols[2].symbol.name);
351 EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz"));
352 EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 std::unique_ptr<BinaryPrimitive> flag_value =
355 ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700356 ASSERT_THAT(flag_value, NotNull());
357 EXPECT_THAT(flag_value->value.data, Eq(1u | 2u));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800358}
359
360TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700361 std::string input = R"(
362 <attr name="foo">
363 <enum name="bar" value="0"/>
364 <enum name="bat" value="1"/>
365 <enum name="bat" value="2"/>
366 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 ASSERT_FALSE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800368}
369
370TEST_F(ResourceParserTest, ParseStyle) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700371 std::string input = R"(
372 <style name="foo" parent="@style/fu">
373 <item name="bar">#ffffffff</item>
374 <item name="bat">@string/hey</item>
375 <item name="baz"><b>hey</b></item>
376 </style>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700380 ASSERT_THAT(style, NotNull());
381 ASSERT_TRUE(style->parent);
382 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu"))));
383 ASSERT_THAT(style->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800384
Adam Lesinskia45893a2017-05-30 15:19:02 -0700385 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
386 EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
387 EXPECT_THAT(style->entries[2].key.name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388}
389
Adam Lesinski769de982015-04-10 19:43:55 -0700390TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700391 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)"));
Adam Lesinski769de982015-04-10 19:43:55 -0700392
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("com.app:style/Theme"))));
Adam Lesinski769de982015-04-10 19:43:55 -0700397}
398
Adam Lesinski24aad162015-04-24 19:19:30 -0700399TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700400 std::string input = R"(
401 <style xmlns:app="http://schemas.android.com/apk/res/android"
402 name="foo" parent="app:Theme"/>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 ASSERT_TRUE(TestParse(input));
Adam Lesinski24aad162015-04-24 19:19:30 -0700404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700406 ASSERT_THAT(style, NotNull());
407 ASSERT_TRUE(style->parent);
408 ASSERT_TRUE(style->parent.value().name);
409 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("android:style/Theme"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700410}
411
412TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700413 std::string input = R"(
414 <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo">
415 <item name="app:bar">0</item>
416 </style>)";
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_THAT(style->entries, SizeIs(1));
422 EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("android:attr/bar"))));
Adam Lesinski24aad162015-04-24 19:19:30 -0700423}
424
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700425TEST_F(ResourceParserTest, ParseStyleWithInferredParent) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700426 ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700427
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700429 ASSERT_THAT(style, NotNull());
430 ASSERT_TRUE(style->parent);
431 EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/foo"))));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 EXPECT_TRUE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700433}
434
Adam Lesinskia45893a2017-05-30 15:19:02 -0700435TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) {
436 ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)"));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 Style* style = test::GetValue<Style>(&table_, "style/foo.bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700439 ASSERT_THAT(style, NotNull());
440 EXPECT_FALSE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 EXPECT_FALSE(style->parent_inferred);
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700442}
443
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800444TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700445 ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)"));
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 Style* style = test::GetValue<Style>(&table_, "style/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700448 ASSERT_THAT(style, NotNull());
449 ASSERT_TRUE(style->parent);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 EXPECT_TRUE(style->parent.value().private_reference);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800451}
452
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700454 ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)"));
455 ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456}
457
458TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700459 std::string input = R"(
460 <declare-styleable name="foo">
461 <attr name="bar" />
462 <attr name="bat" format="string|reference"/>
463 <attr name="baz">
464 <enum name="foo" value="1"/>
465 </attr>
466 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 table_.FindResource(test::ParseNameOrDie("styleable/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700471 ASSERT_TRUE(result);
472 EXPECT_THAT(result.value().entry->symbol_status.state, Eq(SymbolState::kPublic));
Adam Lesinski9f222042015-11-04 13:51:45 -0800473
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700475 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 attr = test::GetValue<Attribute>(&table_, "attr/bat");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700479 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 attr = test::GetValue<Attribute>(&table_, "attr/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700483 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 EXPECT_TRUE(attr->IsWeak());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700485 EXPECT_THAT(attr->symbols, SizeIs(1));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700486
Adam Lesinskia45893a2017-05-30 15:19:02 -0700487 EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700490 ASSERT_THAT(styleable, NotNull());
491 ASSERT_THAT(styleable->entries, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800492
Adam Lesinskia45893a2017-05-30 15:19:02 -0700493 EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar"))));
494 EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat"))));
495 EXPECT_THAT(styleable->entries[2].name, Eq(make_value(test::ParseNameOrDie("attr/baz"))));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496}
497
Adam Lesinski467f1712015-11-16 17:35:44 -0800498TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700499 std::string input = R"(
500 <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android"
501 name="foo">
502 <attr name="*android:bar" />
503 <attr name="privAndroid:bat" />
504 </declare-styleable>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 ASSERT_TRUE(TestParse(input));
506 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700507 ASSERT_THAT(styleable, NotNull());
508 ASSERT_THAT(styleable->entries, SizeIs(2));
Adam Lesinski467f1712015-11-16 17:35:44 -0800509
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 EXPECT_TRUE(styleable->entries[0].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700511 ASSERT_TRUE(styleable->entries[0].name);
512 EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 EXPECT_TRUE(styleable->entries[1].private_reference);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700515 ASSERT_TRUE(styleable->entries[1].name);
516 EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android"));
Adam Lesinski467f1712015-11-16 17:35:44 -0800517}
518
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800519TEST_F(ResourceParserTest, ParseArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700520 std::string input = R"(
521 <array name="foo">
522 <item>@string/ref</item>
523 <item>hey</item>
524 <item>23</item>
525 </array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700529 ASSERT_THAT(array, NotNull());
530 ASSERT_THAT(array->items, SizeIs(3));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800531
Adam Lesinskia45893a2017-05-30 15:19:02 -0700532 EXPECT_THAT(ValueCast<Reference>(array->items[0].get()), NotNull());
533 EXPECT_THAT(ValueCast<String>(array->items[1].get()), NotNull());
534 EXPECT_THAT(ValueCast<BinaryPrimitive>(array->items[2].get()), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800535}
536
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700537TEST_F(ResourceParserTest, ParseStringArray) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700538 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700539 <string-array name="foo">
540 <item>"Werk"</item>"
Adam Lesinskia45893a2017-05-30 15:19:02 -0700541 </string-array>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 ASSERT_TRUE(TestParse(input));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700543 EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700544}
545
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700546TEST_F(ResourceParserTest, ParseArrayWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700547 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700548 <array name="foo" format="string">
549 <item>100</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700550 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700551 ASSERT_TRUE(TestParse(input));
552
553 Array* array = test::GetValue<Array>(&table_, "array/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700554 ASSERT_THAT(array, NotNull());
555 ASSERT_THAT(array->items, SizeIs(1));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700556
557 String* str = ValueCast<String>(array->items[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700558 ASSERT_THAT(str, NotNull());
559 EXPECT_THAT(*str, StrValueEq("100"));
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700560}
561
562TEST_F(ResourceParserTest, ParseArrayWithBadFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700563 std::string input = R"(
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700564 <array name="foo" format="integer">
565 <item>Hi</item>
Adam Lesinskia45893a2017-05-30 15:19:02 -0700566 </array>)";
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700567 ASSERT_FALSE(TestParse(input));
568}
569
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800570TEST_F(ResourceParserTest, ParsePlural) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700571 std::string input = R"(
572 <plurals name="foo">
573 <item quantity="other">apples</item>
574 <item quantity="one">apple</item>
575 </plurals>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 ASSERT_TRUE(TestParse(input));
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800577
578 Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700579 ASSERT_THAT(plural, NotNull());
580 EXPECT_THAT(plural->values[Plural::Zero], IsNull());
581 EXPECT_THAT(plural->values[Plural::Two], IsNull());
582 EXPECT_THAT(plural->values[Plural::Few], IsNull());
583 EXPECT_THAT(plural->values[Plural::Many], IsNull());
Adam Lesinski8f7c5502017-03-02 17:45:01 -0800584
Adam Lesinskia45893a2017-05-30 15:19:02 -0700585 EXPECT_THAT(plural->values[Plural::One], NotNull());
586 EXPECT_THAT(plural->values[Plural::Other], NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587}
588
589TEST_F(ResourceParserTest, ParseCommentsWithResource) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700590 std::string input = R"(
591 <!--This is a comment-->
592 <string name="foo">Hi</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 ASSERT_TRUE(TestParse(input));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700596 ASSERT_THAT(value, NotNull());
597 EXPECT_THAT(value->GetComment(), Eq("This is a comment"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700598}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599
Adam Lesinskie78fd612015-10-22 12:48:43 -0700600TEST_F(ResourceParserTest, DoNotCombineMultipleComments) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700601 std::string input = R"(
602 <!--One-->
603 <!--Two-->
604 <string name="foo">Hi</string>)";
Adam Lesinskie78fd612015-10-22 12:48:43 -0700605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 ASSERT_TRUE(TestParse(input));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 String* value = test::GetValue<String>(&table_, "string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700609 ASSERT_THAT(value, NotNull());
610 EXPECT_THAT(value->GetComment(), Eq("Two"));
Adam Lesinskie78fd612015-10-22 12:48:43 -0700611}
612
613TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700614 std::string input = R"(
615 <!--One-->
616 <string name="foo">
617 Hi
618 <!--Two-->
619 </string>)";
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("One"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800625}
626
Adam Lesinskica5638f2015-10-21 14:42:43 -0700627TEST_F(ResourceParserTest, ParseNestedComments) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 // We only care about declare-styleable and enum/flag attributes because
Adam Lesinskia45893a2017-05-30 15:19:02 -0700629 // comments from those end up in R.java
630 std::string input = R"(
631 <declare-styleable name="foo">
632 <!-- The name of the bar -->
633 <attr name="barName" format="string|reference" />
634 </declare-styleable>
Adam Lesinskica5638f2015-10-21 14:42:43 -0700635
Adam Lesinskia45893a2017-05-30 15:19:02 -0700636 <attr name="foo">
637 <!-- The very first -->
638 <enum name="one" value="1" />
639 </attr>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 ASSERT_TRUE(TestParse(input));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700643 ASSERT_THAT(styleable, NotNull());
644 ASSERT_THAT(styleable->entries, SizeIs(1));
645 EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700648 ASSERT_THAT(attr, NotNull());
649 ASSERT_THAT(attr->symbols, SizeIs(1));
650 EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first"));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700651}
652
Adam Lesinskia45893a2017-05-30 15:19:02 -0700653// Declaring an ID as public should not require a separate definition (as an ID has no value).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800654TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700655 ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)"));
656 ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800657}
658
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800659TEST_F(ResourceParserTest, KeepAllProducts) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700660 std::string input = R"(
661 <string name="foo" product="phone">hi</string>
662 <string name="foo" product="no-sdcard">ho</string>
663 <string name="bar" product="">wee</string>
664 <string name="baz">woo</string>
665 <string name="bit" product="phablet">hoot</string>
666 <string name="bot" product="default">yes</string>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 ASSERT_TRUE(TestParse(input));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700668
Adam Lesinskia45893a2017-05-30 15:19:02 -0700669 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull());
670 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull());
671 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull());
672 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull());
673 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull());
674 ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700675}
676
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800677TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700678 std::string input = R"(
679 <public-group type="attr" first-id="0x01010040">
680 <public name="foo" />
681 <public name="bar" />
682 </public-group>)";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 ASSERT_TRUE(TestParse(input));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800684
Adam Lesinskia45893a2017-05-30 15:19:02 -0700685 Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo"));
686 ASSERT_TRUE(result);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800687
Adam Lesinskia45893a2017-05-30 15:19:02 -0700688 ASSERT_TRUE(result.value().package->id);
689 ASSERT_TRUE(result.value().type->id);
690 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691 ResourceId actual_id(result.value().package->id.value(),
692 result.value().type->id.value(),
693 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700694 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 result = table_.FindResource(test::ParseNameOrDie("attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700697 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698
Adam Lesinskia45893a2017-05-30 15:19:02 -0700699 ASSERT_TRUE(result.value().package->id);
700 ASSERT_TRUE(result.value().type->id);
701 ASSERT_TRUE(result.value().entry->id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 actual_id = ResourceId(result.value().package->id.value(),
703 result.value().type->id.value(),
704 result.value().entry->id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700705 EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041)));
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800706}
707
Adam Lesinskifa105052015-11-07 13:34:39 -0800708TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700709 ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)"));
710 ASSERT_FALSE(TestParse(R"(<item type="layout" name="bar">"this is a string"</item>)"));
Adam Lesinskifa105052015-11-07 13:34:39 -0800711}
712
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700713TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700714 ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)"));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800715
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 table_.FindResource(test::ParseNameOrDie("string/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700718 ASSERT_TRUE(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 const ResourceEntry* entry = result.value().entry;
Adam Lesinskia45893a2017-05-30 15:19:02 -0700720 ASSERT_THAT(entry, NotNull());
721 EXPECT_THAT(entry->symbol_status.state, Eq(SymbolState::kUndefined));
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700722 EXPECT_TRUE(entry->symbol_status.allow_new);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800723}
724
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800725TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700726 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer" format="float">0.3</item>)"));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800727
Adam Lesinskie597d682017-06-01 17:16:44 -0700728 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
729 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700730 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800731
Adam Lesinskia45893a2017-05-30 15:19:02 -0700732 ASSERT_FALSE(TestParse(R"(<item name="bar" type="integer" format="fraction">100</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700733}
734
735// An <item> without a format specifier accepts all types of values.
736TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700737 ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)"));
Adam Lesinskie597d682017-06-01 17:16:44 -0700738
739 BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo");
740 ASSERT_THAT(val, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700741 EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800742}
743
Adam Lesinski86d67df2017-01-31 13:47:27 -0800744TEST_F(ResourceParserTest, ParseConfigVaryingItem) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700745 ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)"));
746 ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800747}
748
749TEST_F(ResourceParserTest, ParseBagElement) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700750 std::string input = R"(
751 <bag name="bag" type="configVarying">
752 <item name="test">Hello!</item>
753 </bag>)";
Adam Lesinski86d67df2017-01-31 13:47:27 -0800754 ASSERT_TRUE(TestParse(input));
755
756 Style* val = test::GetValue<Style>(&table_, "configVarying/bag");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700757 ASSERT_THAT(val, NotNull());
758 ASSERT_THAT(val->entries, SizeIs(1));
Adam Lesinski86d67df2017-01-31 13:47:27 -0800759
Adam Lesinskia45893a2017-05-30 15:19:02 -0700760 EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test"))));
761 EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800762}
763
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700764TEST_F(ResourceParserTest, ParseElementWithNoValue) {
765 std::string input = R"(
766 <item type="drawable" format="reference" name="foo" />
767 <string name="foo" />)";
768 ASSERT_TRUE(TestParse(input));
769 ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference())));
770
771 String* str = test::GetValue<String>(&table_, "string/foo");
772 ASSERT_THAT(str, NotNull());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700773 EXPECT_THAT(*str, StrValueEq(""));
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700774}
775
Adam Lesinskib9f05482017-06-02 16:32:37 -0700776TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700777 ASSERT_TRUE(TestParse(R"(<string name="foo">%1$s %n %2$s</string>)"));
Adam Lesinskib9f05482017-06-02 16:32:37 -0700778}
779
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780} // namespace aapt