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