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