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