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