Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include "ResourceTable.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "ResourceUtils.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include "ResourceValues.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 25 | #include "test/Test.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 26 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 28 | using ::aapt::test::ValueEq; |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 29 | using ::android::StringPiece; |
| 30 | using ::testing::Eq; |
| 31 | using ::testing::NotNull; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 32 | using ::testing::Pointee; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | namespace aapt { |
| 35 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 36 | constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | std::stringstream input(kXmlPreamble); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 41 | input << R"(<attr name="foo"/>)" << std::endl; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | ResourceTable table; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {}); |
| 44 | xml::XmlPullParser xml_parser(input); |
| 45 | ASSERT_FALSE(parser.Parse(&xml_parser)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | class ResourceParserTest : public ::testing::Test { |
| 49 | public: |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 50 | void SetUp() override { |
| 51 | context_ = test::ContextBuilder().Build(); |
| 52 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | ::testing::AssertionResult TestParse(const StringPiece& str) { |
| 55 | return TestParse(str, ConfigDescription{}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 58 | ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | std::stringstream input(kXmlPreamble); |
| 60 | input << "<resources>\n" << str << "\n</resources>" << std::endl; |
| 61 | ResourceParserOptions parserOptions; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 62 | ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config, |
| 63 | parserOptions); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | xml::XmlPullParser xmlParser(input); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | if (parser.Parse(&xmlParser)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | return ::testing::AssertionSuccess(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | return ::testing::AssertionFailure(); |
| 69 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | ResourceTable table_; |
| 73 | std::unique_ptr<IAaptContext> context_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | TEST_F(ResourceParserTest, ParseQuotedString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | std::string input = "<string name=\"foo\"> \" hey there \" </string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | ASSERT_NE(nullptr, str); |
| 82 | EXPECT_EQ(std::string(" hey there "), *str->value); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 83 | EXPECT_TRUE(str->untranslatable_sections.empty()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_F(ResourceParserTest, ParseEscapedString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | std::string input = "<string name=\"foo\">\\?123</string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | ASSERT_NE(nullptr, str); |
| 92 | EXPECT_EQ(std::string("?123"), *str->value); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 93 | EXPECT_TRUE(str->untranslatable_sections.empty()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 96 | TEST_F(ResourceParserTest, ParseFormattedString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | std::string input = "<string name=\"foo\">%d %s</string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | ASSERT_FALSE(TestParse(input)); |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | input = "<string name=\"foo\">%1$d %2$s</string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 104 | TEST_F(ResourceParserTest, ParseStyledString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | // Use a surrogate pair unicode point so that we can verify that the span |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 106 | // indices use UTF-16 length and not UTF-8 length. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | std::string input = |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 108 | "<string name=\"foo\">This is my aunt\u2019s <b>fickle <small>string</small></b></string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 110 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | StyledString* str = test::GetValue<StyledString>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | ASSERT_NE(nullptr, str); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 114 | const std::string expected_str = "This is my aunt\u2019s fickle string"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | EXPECT_EQ(expected_str, *str->value->str); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 116 | EXPECT_EQ(2u, str->value->spans.size()); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 117 | EXPECT_TRUE(str->untranslatable_sections.empty()); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | EXPECT_EQ(std::string("b"), *str->value->spans[0].name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | EXPECT_EQ(17u, str->value->spans[0].first_char); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 121 | EXPECT_EQ(30u, str->value->spans[0].last_char); |
| 122 | |
| 123 | EXPECT_EQ(std::string("small"), *str->value->spans[1].name); |
| 124 | EXPECT_EQ(24u, str->value->spans[1].first_char); |
| 125 | EXPECT_EQ(30u, str->value->spans[1].last_char); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(ResourceParserTest, ParseStringWithWhitespace) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | std::string input = "<string name=\"foo\"> This is what I think </string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 131 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | ASSERT_NE(nullptr, str); |
| 134 | EXPECT_EQ(std::string("This is what I think"), *str->value); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 135 | EXPECT_TRUE(str->untranslatable_sections.empty()); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | input = "<string name=\"foo2\">\" This is what I think \"</string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | str = test::GetValue<String>(&table_, "string/foo2"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | ASSERT_NE(nullptr, str); |
| 142 | EXPECT_EQ(std::string(" This is what I think "), *str->value); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 145 | TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) { |
| 146 | std::string input = R"EOF( |
| 147 | <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 148 | There are <xliff:source>no</xliff:source> apples</string>)EOF"; |
| 149 | ASSERT_TRUE(TestParse(input)); |
| 150 | |
| 151 | String* str = test::GetValue<String>(&table_, "string/foo"); |
| 152 | ASSERT_NE(nullptr, str); |
| 153 | EXPECT_EQ(StringPiece("There are no apples"), StringPiece(*str->value)); |
| 154 | EXPECT_TRUE(str->untranslatable_sections.empty()); |
| 155 | } |
| 156 | |
| 157 | TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) { |
| 158 | std::string input = R"EOF( |
| 159 | <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 160 | Do not <xliff:g>translate <xliff:g>this</xliff:g></xliff:g></string>)EOF"; |
| 161 | EXPECT_FALSE(TestParse(input)); |
| 162 | } |
| 163 | |
| 164 | TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) { |
| 165 | std::string input = R"EOF( |
| 166 | <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 167 | There are <xliff:g id="count">%1$d</xliff:g> apples</string>)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | ASSERT_NE(nullptr, str); |
| 172 | EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value)); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 173 | |
| 174 | ASSERT_EQ(1u, str->untranslatable_sections.size()); |
| 175 | |
| 176 | // We expect indices and lengths that span to include the whitespace |
| 177 | // before %1$d. This is due to how the StringBuilder withholds whitespace unless |
| 178 | // needed (to deal with line breaks, etc.). |
| 179 | EXPECT_EQ(9u, str->untranslatable_sections[0].start); |
| 180 | EXPECT_EQ(14u, str->untranslatable_sections[0].end); |
| 181 | } |
| 182 | |
| 183 | TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) { |
| 184 | std::string input = R"EOF( |
| 185 | <string name="foo" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 186 | There are <b><xliff:g id="count">%1$d</xliff:g></b> apples</string>)EOF"; |
| 187 | ASSERT_TRUE(TestParse(input)); |
| 188 | |
| 189 | StyledString* str = test::GetValue<StyledString>(&table_, "string/foo"); |
| 190 | ASSERT_NE(nullptr, str); |
| 191 | EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value->str)); |
| 192 | |
| 193 | ASSERT_EQ(1u, str->untranslatable_sections.size()); |
| 194 | |
| 195 | // We expect indices and lengths that span to include the whitespace |
| 196 | // before %1$d. This is due to how the StringBuilder withholds whitespace unless |
| 197 | // needed (to deal with line breaks, etc.). |
| 198 | EXPECT_EQ(9u, str->untranslatable_sections[0].start); |
| 199 | EXPECT_EQ(14u, str->untranslatable_sections[0].end); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 202 | TEST_F(ResourceParserTest, ParseNull) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | std::string input = "<integer name=\"foo\">@null</integer>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | // The Android runtime treats a value of android::Res_value::TYPE_NULL as |
| 207 | // a non-existing value, and this causes problems in styles when trying to |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 208 | // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | // with a data value of 0. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 210 | Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo"); |
| 211 | ASSERT_THAT(null_ref, NotNull()); |
| 212 | EXPECT_FALSE(null_ref->name); |
| 213 | EXPECT_FALSE(null_ref->id); |
| 214 | EXPECT_EQ(Reference::Type::kResource, null_ref->reference_type); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_F(ResourceParserTest, ParseEmpty) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | std::string input = "<integer name=\"foo\">@empty</integer>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 220 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 221 | BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | ASSERT_NE(nullptr, integer); |
| 223 | EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType); |
| 224 | EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | TEST_F(ResourceParserTest, ParseAttr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | std::string input = |
| 229 | "<attr name=\"foo\" format=\"string\"/>\n" |
| 230 | "<attr name=\"bar\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 232 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 236 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | attr = test::GetValue<Attribute>(&table_, "attr/bar"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->type_mask); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | // Old AAPT allowed attributes to be defined under different configurations, but |
| 243 | // ultimately |
| 244 | // stored them with the default configuration. Check that we have the same |
| 245 | // behavior. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 246 | TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | const ConfigDescription watch_config = test::ParseConfigOrDie("watch"); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 248 | std::string input = R"( |
| 249 | <attr name="foo" /> |
| 250 | <declare-styleable name="bar"> |
| 251 | <attr name="baz" /> |
| 252 | </declare-styleable>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | ASSERT_TRUE(TestParse(input, watch_config)); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 254 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 255 | EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config)); |
| 256 | EXPECT_EQ(nullptr, test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config)); |
| 257 | EXPECT_EQ(nullptr, test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config)); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 258 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 259 | EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/foo")); |
| 260 | EXPECT_NE(nullptr, test::GetValue<Attribute>(&table_, "attr/baz")); |
| 261 | EXPECT_NE(nullptr, test::GetValue<Styleable>(&table_, "styleable/bar")); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 264 | TEST_F(ResourceParserTest, ParseAttrWithMinMax) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | std::string input = |
| 266 | "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 268 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask); |
| 272 | EXPECT_EQ(10, attr->min_int); |
| 273 | EXPECT_EQ(23, attr->max_int); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 277 | std::string input = |
| 278 | "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | ASSERT_FALSE(TestParse(input)); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 282 | TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | std::string input = |
| 284 | "<declare-styleable name=\"Styleable\">\n" |
| 285 | " <attr name=\"foo\" />\n" |
| 286 | "</declare-styleable>\n" |
| 287 | "<attr name=\"foo\" format=\"string\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 289 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | std::string input = |
| 297 | "<declare-styleable name=\"Theme\">" |
| 298 | " <attr name=\"foo\" />\n" |
| 299 | "</declare-styleable>\n" |
| 300 | "<declare-styleable name=\"Window\">\n" |
| 301 | " <attr name=\"foo\" format=\"boolean\"/>\n" |
| 302 | "</declare-styleable>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 303 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 304 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | TEST_F(ResourceParserTest, ParseEnumAttr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | std::string input = |
| 312 | "<attr name=\"foo\">\n" |
| 313 | " <enum name=\"bar\" value=\"0\"/>\n" |
| 314 | " <enum name=\"bat\" value=\"1\"/>\n" |
| 315 | " <enum name=\"baz\" value=\"2\"/>\n" |
| 316 | "</attr>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 317 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 318 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
| 320 | ASSERT_NE(enum_attr, nullptr); |
| 321 | EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM); |
| 322 | ASSERT_EQ(enum_attr->symbols.size(), 3u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 323 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name); |
| 325 | EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar"); |
| 326 | EXPECT_EQ(enum_attr->symbols[0].value, 0u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name); |
| 329 | EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat"); |
| 330 | EXPECT_EQ(enum_attr->symbols[1].value, 1u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name); |
| 333 | EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz"); |
| 334 | EXPECT_EQ(enum_attr->symbols[2].value, 2u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | TEST_F(ResourceParserTest, ParseFlagAttr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 338 | std::string input = |
| 339 | "<attr name=\"foo\">\n" |
| 340 | " <flag name=\"bar\" value=\"0\"/>\n" |
| 341 | " <flag name=\"bat\" value=\"1\"/>\n" |
| 342 | " <flag name=\"baz\" value=\"2\"/>\n" |
| 343 | "</attr>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 345 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
| 347 | ASSERT_NE(nullptr, flag_attr); |
| 348 | EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS); |
| 349 | ASSERT_EQ(flag_attr->symbols.size(), 3u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 350 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name); |
| 352 | EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar"); |
| 353 | EXPECT_EQ(flag_attr->symbols[0].value, 0u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 354 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name); |
| 356 | EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat"); |
| 357 | EXPECT_EQ(flag_attr->symbols[1].value, 1u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 358 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name); |
| 360 | EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz"); |
| 361 | EXPECT_EQ(flag_attr->symbols[2].value, 2u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 362 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | std::unique_ptr<BinaryPrimitive> flag_value = |
| 364 | ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat"); |
| 365 | ASSERT_NE(nullptr, flag_value); |
| 366 | EXPECT_EQ(flag_value->value.data, 1u | 2u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | std::string input = |
| 371 | "<attr name=\"foo\">\n" |
| 372 | " <enum name=\"bar\" value=\"0\"/>\n" |
| 373 | " <enum name=\"bat\" value=\"1\"/>\n" |
| 374 | " <enum name=\"bat\" value=\"2\"/>\n" |
| 375 | "</attr>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | ASSERT_FALSE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | TEST_F(ResourceParserTest, ParseStyle) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | std::string input = |
| 381 | "<style name=\"foo\" parent=\"@style/fu\">\n" |
| 382 | " <item name=\"bar\">#ffffffff</item>\n" |
| 383 | " <item name=\"bat\">@string/hey</item>\n" |
| 384 | " <item name=\"baz\"><b>hey</b></item>\n" |
| 385 | "</style>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 386 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 387 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | ASSERT_NE(nullptr, style); |
| 390 | AAPT_ASSERT_TRUE(style->parent); |
| 391 | AAPT_ASSERT_TRUE(style->parent.value().name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 392 | EXPECT_EQ(test::ParseNameOrDie("style/fu"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 393 | style->parent.value().name.value()); |
| 394 | ASSERT_EQ(3u, style->entries.size()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 395 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | AAPT_ASSERT_TRUE(style->entries[0].key.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 397 | EXPECT_EQ(test::ParseNameOrDie("attr/bar"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 398 | style->entries[0].key.name.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 399 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 400 | AAPT_ASSERT_TRUE(style->entries[1].key.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | EXPECT_EQ(test::ParseNameOrDie("attr/bat"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | style->entries[1].key.name.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 403 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 404 | AAPT_ASSERT_TRUE(style->entries[2].key.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 405 | EXPECT_EQ(test::ParseNameOrDie("attr/baz"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | style->entries[2].key.name.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 409 | TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 410 | std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 411 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 412 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 414 | ASSERT_NE(nullptr, style); |
| 415 | AAPT_ASSERT_TRUE(style->parent); |
| 416 | AAPT_ASSERT_TRUE(style->parent.value().name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 417 | EXPECT_EQ(test::ParseNameOrDie("com.app:style/Theme"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 418 | style->parent.value().name.value()); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 421 | TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | std::string input = |
| 423 | "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n" |
| 424 | " name=\"foo\" parent=\"app:Theme\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 425 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 426 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 427 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | ASSERT_NE(nullptr, style); |
| 429 | AAPT_ASSERT_TRUE(style->parent); |
| 430 | AAPT_ASSERT_TRUE(style->parent.value().name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 431 | EXPECT_EQ(test::ParseNameOrDie("android:style/Theme"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 432 | style->parent.value().name.value()); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 436 | std::string input = |
| 437 | "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" " |
| 438 | "name=\"foo\">\n" |
| 439 | " <item name=\"app:bar\">0</item>\n" |
| 440 | "</style>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 441 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 442 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 443 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 444 | ASSERT_NE(nullptr, style); |
| 445 | ASSERT_EQ(1u, style->entries.size()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 446 | EXPECT_EQ(test::ParseNameOrDie("android:attr/bar"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 447 | style->entries[0].key.name.value()); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 450 | TEST_F(ResourceParserTest, ParseStyleWithInferredParent) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | std::string input = "<style name=\"foo.bar\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 452 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 453 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 454 | Style* style = test::GetValue<Style>(&table_, "style/foo.bar"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 455 | ASSERT_NE(nullptr, style); |
| 456 | AAPT_ASSERT_TRUE(style->parent); |
| 457 | AAPT_ASSERT_TRUE(style->parent.value().name); |
| 458 | EXPECT_EQ(style->parent.value().name.value(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 459 | test::ParseNameOrDie("style/foo")); |
| 460 | EXPECT_TRUE(style->parent_inferred); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 463 | TEST_F(ResourceParserTest, |
| 464 | ParseStyleWithInferredParentOverridenByEmptyParentAttribute) { |
| 465 | std::string input = "<style name=\"foo.bar\" parent=\"\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 466 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 467 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 468 | Style* style = test::GetValue<Style>(&table_, "style/foo.bar"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 469 | ASSERT_NE(nullptr, style); |
| 470 | AAPT_EXPECT_FALSE(style->parent); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | EXPECT_FALSE(style->parent_inferred); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 474 | TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 475 | std::string input = |
| 476 | R"EOF(<style name="foo" parent="*android:style/bar" />)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 477 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 478 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 479 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | ASSERT_NE(nullptr, style); |
| 481 | AAPT_ASSERT_TRUE(style->parent); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 482 | EXPECT_TRUE(style->parent.value().private_reference); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 485 | TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 486 | std::string input = "<string name=\"foo\">@+id/bar</string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 487 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 488 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 489 | Id* id = test::GetValue<Id>(&table_, "id/bar"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 490 | ASSERT_NE(id, nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 494 | std::string input = |
| 495 | "<declare-styleable name=\"foo\">\n" |
| 496 | " <attr name=\"bar\" />\n" |
| 497 | " <attr name=\"bat\" format=\"string|reference\"/>\n" |
| 498 | " <attr name=\"baz\">\n" |
| 499 | " <enum name=\"foo\" value=\"1\"/>\n" |
| 500 | " </attr>\n" |
| 501 | "</declare-styleable>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 502 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 503 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 504 | Maybe<ResourceTable::SearchResult> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 505 | table_.FindResource(test::ParseNameOrDie("styleable/foo")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 506 | AAPT_ASSERT_TRUE(result); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 507 | EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbol_status.state); |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 508 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 510 | ASSERT_NE(attr, nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 511 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 512 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 513 | attr = test::GetValue<Attribute>(&table_, "attr/bat"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 514 | ASSERT_NE(attr, nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 515 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 516 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 517 | attr = test::GetValue<Attribute>(&table_, "attr/baz"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 518 | ASSERT_NE(attr, nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 519 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 520 | EXPECT_EQ(1u, attr->symbols.size()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 521 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 522 | EXPECT_NE(nullptr, test::GetValue<Id>(&table_, "id/foo")); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 523 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 524 | Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 525 | ASSERT_NE(styleable, nullptr); |
| 526 | ASSERT_EQ(3u, styleable->entries.size()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 527 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 528 | EXPECT_EQ(test::ParseNameOrDie("attr/bar"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 529 | styleable->entries[0].name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | EXPECT_EQ(test::ParseNameOrDie("attr/bat"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 531 | styleable->entries[1].name.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 534 | TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 535 | std::string input = |
| 536 | "<declare-styleable name=\"foo\" " |
| 537 | "xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n" |
| 538 | " <attr name=\"*android:bar\" />\n" |
| 539 | " <attr name=\"privAndroid:bat\" />\n" |
| 540 | "</declare-styleable>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 541 | ASSERT_TRUE(TestParse(input)); |
| 542 | Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 543 | ASSERT_NE(nullptr, styleable); |
| 544 | ASSERT_EQ(2u, styleable->entries.size()); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 545 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 546 | EXPECT_TRUE(styleable->entries[0].private_reference); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 547 | AAPT_ASSERT_TRUE(styleable->entries[0].name); |
| 548 | EXPECT_EQ(std::string("android"), styleable->entries[0].name.value().package); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 549 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 550 | EXPECT_TRUE(styleable->entries[1].private_reference); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 551 | AAPT_ASSERT_TRUE(styleable->entries[1].name); |
| 552 | EXPECT_EQ(std::string("android"), styleable->entries[1].name.value().package); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 555 | TEST_F(ResourceParserTest, ParseArray) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 556 | std::string input = |
| 557 | "<array name=\"foo\">\n" |
| 558 | " <item>@string/ref</item>\n" |
| 559 | " <item>hey</item>\n" |
| 560 | " <item>23</item>\n" |
| 561 | "</array>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 562 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 563 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 564 | Array* array = test::GetValue<Array>(&table_, "array/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 565 | ASSERT_NE(array, nullptr); |
| 566 | ASSERT_EQ(3u, array->items.size()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 567 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 568 | EXPECT_NE(nullptr, ValueCast<Reference>(array->items[0].get())); |
| 569 | EXPECT_NE(nullptr, ValueCast<String>(array->items[1].get())); |
| 570 | EXPECT_NE(nullptr, ValueCast<BinaryPrimitive>(array->items[2].get())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 573 | TEST_F(ResourceParserTest, ParseStringArray) { |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 574 | std::string input = R"EOF( |
| 575 | <string-array name="foo"> |
| 576 | <item>"Werk"</item>" |
| 577 | </string-array>)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 578 | ASSERT_TRUE(TestParse(input)); |
| 579 | EXPECT_NE(nullptr, test::GetValue<Array>(&table_, "array/foo")); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 582 | TEST_F(ResourceParserTest, ParseArrayWithFormat) { |
| 583 | std::string input = R"EOF( |
| 584 | <array name="foo" format="string"> |
| 585 | <item>100</item> |
| 586 | </array>)EOF"; |
| 587 | ASSERT_TRUE(TestParse(input)); |
| 588 | |
| 589 | Array* array = test::GetValue<Array>(&table_, "array/foo"); |
| 590 | ASSERT_NE(nullptr, array); |
| 591 | |
| 592 | ASSERT_EQ(1u, array->items.size()); |
| 593 | |
| 594 | String* str = ValueCast<String>(array->items[0].get()); |
| 595 | ASSERT_NE(nullptr, str); |
| 596 | EXPECT_EQ(std::string("100"), *str->value); |
| 597 | } |
| 598 | |
| 599 | TEST_F(ResourceParserTest, ParseArrayWithBadFormat) { |
| 600 | std::string input = R"EOF( |
| 601 | <array name="foo" format="integer"> |
| 602 | <item>Hi</item> |
| 603 | </array>)EOF"; |
| 604 | ASSERT_FALSE(TestParse(input)); |
| 605 | } |
| 606 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 607 | TEST_F(ResourceParserTest, ParsePlural) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 608 | std::string input = |
| 609 | "<plurals name=\"foo\">\n" |
| 610 | " <item quantity=\"other\">apples</item>\n" |
| 611 | " <item quantity=\"one\">apple</item>\n" |
| 612 | "</plurals>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 613 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 614 | |
| 615 | Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo"); |
| 616 | ASSERT_NE(nullptr, plural); |
| 617 | EXPECT_EQ(nullptr, plural->values[Plural::Zero]); |
| 618 | EXPECT_EQ(nullptr, plural->values[Plural::Two]); |
| 619 | EXPECT_EQ(nullptr, plural->values[Plural::Few]); |
| 620 | EXPECT_EQ(nullptr, plural->values[Plural::Many]); |
| 621 | |
| 622 | EXPECT_NE(nullptr, plural->values[Plural::One]); |
| 623 | EXPECT_NE(nullptr, plural->values[Plural::Other]); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | TEST_F(ResourceParserTest, ParseCommentsWithResource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 627 | std::string input = |
| 628 | "<!--This is a comment-->\n" |
| 629 | "<string name=\"foo\">Hi</string>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 630 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 631 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 632 | String* value = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 633 | ASSERT_NE(nullptr, value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 634 | EXPECT_EQ(value->GetComment(), "This is a comment"); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 635 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 636 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 637 | TEST_F(ResourceParserTest, DoNotCombineMultipleComments) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 638 | std::string input = |
| 639 | "<!--One-->\n" |
| 640 | "<!--Two-->\n" |
| 641 | "<string name=\"foo\">Hi</string>"; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 642 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 643 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 644 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 645 | String* value = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 646 | ASSERT_NE(nullptr, value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 647 | EXPECT_EQ(value->GetComment(), "Two"); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 651 | std::string input = |
| 652 | "<!--One-->\n" |
| 653 | "<string name=\"foo\">\n" |
| 654 | " Hi\n" |
| 655 | "<!--Two-->\n" |
| 656 | "</string>"; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 657 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 658 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 659 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 660 | String* value = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 661 | ASSERT_NE(nullptr, value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 662 | EXPECT_EQ(value->GetComment(), "One"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 665 | TEST_F(ResourceParserTest, ParseNestedComments) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 666 | // We only care about declare-styleable and enum/flag attributes because |
| 667 | // comments |
| 668 | // from those end up in R.java |
| 669 | std::string input = R"EOF( |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 670 | <declare-styleable name="foo"> |
| 671 | <!-- The name of the bar --> |
| 672 | <attr name="barName" format="string|reference" /> |
| 673 | </declare-styleable> |
| 674 | |
| 675 | <attr name="foo"> |
| 676 | <!-- The very first --> |
| 677 | <enum name="one" value="1" /> |
| 678 | </attr>)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 679 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 680 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 681 | Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 682 | ASSERT_NE(nullptr, styleable); |
| 683 | ASSERT_EQ(1u, styleable->entries.size()); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 684 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 685 | EXPECT_EQ(StringPiece("The name of the bar"), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 686 | styleable->entries.front().GetComment()); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 687 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 688 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 689 | ASSERT_NE(nullptr, attr); |
| 690 | ASSERT_EQ(1u, attr->symbols.size()); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 691 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 692 | EXPECT_EQ(StringPiece("The very first"), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 693 | attr->symbols.front().symbol.GetComment()); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 696 | /* |
| 697 | * Declaring an ID as public should not require a separate definition |
| 698 | * (as an ID has no value). |
| 699 | */ |
| 700 | TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 701 | std::string input = "<public type=\"id\" name=\"foo\"/>"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 702 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 703 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 704 | Id* id = test::GetValue<Id>(&table_, "id/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 705 | ASSERT_NE(nullptr, id); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 708 | TEST_F(ResourceParserTest, KeepAllProducts) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 709 | std::string input = R"EOF( |
Adam Lesinski | 7751afc | 2016-01-06 15:45:28 -0800 | [diff] [blame] | 710 | <string name="foo" product="phone">hi</string> |
| 711 | <string name="foo" product="no-sdcard">ho</string> |
| 712 | <string name="bar" product="">wee</string> |
| 713 | <string name="baz">woo</string> |
| 714 | <string name="bit" product="phablet">hoot</string> |
| 715 | <string name="bot" product="default">yes</string> |
| 716 | )EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 717 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 718 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 719 | EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>( |
| 720 | &table_, "string/foo", |
| 721 | ConfigDescription::DefaultConfig(), "phone")); |
| 722 | EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>( |
| 723 | &table_, "string/foo", |
| 724 | ConfigDescription::DefaultConfig(), "no-sdcard")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 725 | EXPECT_NE(nullptr, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 726 | test::GetValueForConfigAndProduct<String>( |
| 727 | &table_, "string/bar", ConfigDescription::DefaultConfig(), "")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 728 | EXPECT_NE(nullptr, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 729 | test::GetValueForConfigAndProduct<String>( |
| 730 | &table_, "string/baz", ConfigDescription::DefaultConfig(), "")); |
| 731 | EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>( |
| 732 | &table_, "string/bit", |
| 733 | ConfigDescription::DefaultConfig(), "phablet")); |
| 734 | EXPECT_NE(nullptr, test::GetValueForConfigAndProduct<String>( |
| 735 | &table_, "string/bot", |
| 736 | ConfigDescription::DefaultConfig(), "default")); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 739 | TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 740 | std::string input = R"EOF( |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 741 | <public-group type="attr" first-id="0x01010040"> |
| 742 | <public name="foo" /> |
| 743 | <public name="bar" /> |
| 744 | </public-group>)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 745 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 746 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 747 | Maybe<ResourceTable::SearchResult> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 748 | table_.FindResource(test::ParseNameOrDie("attr/foo")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 749 | AAPT_ASSERT_TRUE(result); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 750 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 751 | AAPT_ASSERT_TRUE(result.value().package->id); |
| 752 | AAPT_ASSERT_TRUE(result.value().type->id); |
| 753 | AAPT_ASSERT_TRUE(result.value().entry->id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 754 | ResourceId actual_id(result.value().package->id.value(), |
| 755 | result.value().type->id.value(), |
| 756 | result.value().entry->id.value()); |
| 757 | EXPECT_EQ(ResourceId(0x01010040), actual_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 758 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 759 | result = table_.FindResource(test::ParseNameOrDie("attr/bar")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 760 | AAPT_ASSERT_TRUE(result); |
| 761 | |
| 762 | AAPT_ASSERT_TRUE(result.value().package->id); |
| 763 | AAPT_ASSERT_TRUE(result.value().type->id); |
| 764 | AAPT_ASSERT_TRUE(result.value().entry->id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 765 | actual_id = ResourceId(result.value().package->id.value(), |
| 766 | result.value().type->id.value(), |
| 767 | result.value().entry->id.value()); |
| 768 | EXPECT_EQ(ResourceId(0x01010041), actual_id); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Adam Lesinski | fa10505 | 2015-11-07 13:34:39 -0800 | [diff] [blame] | 771 | TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 772 | std::string input = |
| 773 | R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 774 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | fa10505 | 2015-11-07 13:34:39 -0800 | [diff] [blame] | 775 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 776 | input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 777 | ASSERT_FALSE(TestParse(input)); |
Adam Lesinski | fa10505 | 2015-11-07 13:34:39 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 780 | TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 781 | std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 782 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 783 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 784 | Maybe<ResourceTable::SearchResult> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 785 | table_.FindResource(test::ParseNameOrDie("string/bar")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 786 | AAPT_ASSERT_TRUE(result); |
| 787 | const ResourceEntry* entry = result.value().entry; |
| 788 | ASSERT_NE(nullptr, entry); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 789 | EXPECT_EQ(SymbolState::kUndefined, entry->symbol_status.state); |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 790 | EXPECT_TRUE(entry->symbol_status.allow_new); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 793 | TEST_F(ResourceParserTest, ParseItemElementWithFormat) { |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 794 | std::string input = R"(<item name="foo" type="integer" format="float">0.3</item>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 795 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 796 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 797 | BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); |
| 798 | ASSERT_THAT(val, NotNull()); |
| 799 | EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FLOAT)); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 800 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 801 | input = R"(<item name="bar" type="integer" format="fraction">100</item>)"; |
| 802 | ASSERT_FALSE(TestParse(input)); |
| 803 | } |
| 804 | |
| 805 | // An <item> without a format specifier accepts all types of values. |
| 806 | TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) { |
| 807 | std::string input = R"(<item name="foo" type="integer">100%p</item>)"; |
| 808 | ASSERT_TRUE(TestParse(input)); |
| 809 | |
| 810 | BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); |
| 811 | ASSERT_THAT(val, NotNull()); |
| 812 | EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FRACTION)); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 815 | TEST_F(ResourceParserTest, ParseConfigVaryingItem) { |
| 816 | std::string input = R"EOF(<item name="foo" type="configVarying">Hey</item>)EOF"; |
| 817 | ASSERT_TRUE(TestParse(input)); |
| 818 | ASSERT_NE(nullptr, test::GetValue<String>(&table_, "configVarying/foo")); |
| 819 | } |
| 820 | |
| 821 | TEST_F(ResourceParserTest, ParseBagElement) { |
| 822 | std::string input = |
| 823 | R"EOF(<bag name="bag" type="configVarying"><item name="test">Hello!</item></bag>)EOF"; |
| 824 | ASSERT_TRUE(TestParse(input)); |
| 825 | |
| 826 | Style* val = test::GetValue<Style>(&table_, "configVarying/bag"); |
| 827 | ASSERT_NE(nullptr, val); |
| 828 | |
| 829 | ASSERT_EQ(1u, val->entries.size()); |
| 830 | EXPECT_EQ(Reference(test::ParseNameOrDie("attr/test")), val->entries[0].key); |
| 831 | EXPECT_NE(nullptr, ValueCast<RawString>(val->entries[0].value.get())); |
| 832 | } |
| 833 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 834 | TEST_F(ResourceParserTest, ParseElementWithNoValue) { |
| 835 | std::string input = R"( |
| 836 | <item type="drawable" format="reference" name="foo" /> |
| 837 | <string name="foo" />)"; |
| 838 | ASSERT_TRUE(TestParse(input)); |
| 839 | ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference()))); |
| 840 | |
| 841 | String* str = test::GetValue<String>(&table_, "string/foo"); |
| 842 | ASSERT_THAT(str, NotNull()); |
| 843 | EXPECT_THAT(*str->value, Eq("")); |
| 844 | } |
| 845 | |
Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 846 | TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) { |
| 847 | std::string input = R"(<string name="foo">%1$s %n %2$s</string>)"; |
| 848 | ASSERT_TRUE(TestParse(input)); |
| 849 | } |
| 850 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 851 | } // namespace aapt |