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 | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 25 | #include "io/StringStream.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; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 32 | using ::android::ConfigDescription; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 33 | using ::android::Res_value; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 34 | using ::android::ResTable_map; |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 35 | using ::android::StringPiece; |
| 36 | using ::testing::Eq; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 37 | using ::testing::IsEmpty; |
| 38 | using ::testing::IsNull; |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 39 | using ::testing::NotNull; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 40 | using ::testing::Pointee; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 41 | using ::testing::SizeIs; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 42 | using ::testing::StrEq; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | namespace aapt { |
| 45 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 46 | constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | ResourceTable table; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {}); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 52 | |
| 53 | std::string input = kXmlPreamble; |
| 54 | input += R"(<attr name="foo"/>)"; |
| 55 | StringInputStream in(input); |
| 56 | xml::XmlPullParser xml_parser(&in); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | ASSERT_FALSE(parser.Parse(&xml_parser)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | class ResourceParserTest : public ::testing::Test { |
| 61 | public: |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 62 | void SetUp() override { |
| 63 | context_ = test::ContextBuilder().Build(); |
| 64 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | ::testing::AssertionResult TestParse(const StringPiece& str) { |
| 67 | return TestParse(str, ConfigDescription{}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 69 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 70 | ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | ResourceParserOptions parserOptions; |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 72 | ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config, |
| 73 | parserOptions); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 74 | |
| 75 | std::string input = kXmlPreamble; |
| 76 | input += "<resources>\n"; |
| 77 | input.append(str.data(), str.size()); |
| 78 | input += "\n</resources>"; |
| 79 | StringInputStream in(input); |
| 80 | xml::XmlPullParser xmlParser(&in); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | if (parser.Parse(&xmlParser)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | return ::testing::AssertionSuccess(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | return ::testing::AssertionFailure(); |
| 85 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | |
| 87 | protected: |
| 88 | ResourceTable table_; |
| 89 | std::unique_ptr<IAaptContext> context_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | TEST_F(ResourceParserTest, ParseQuotedString) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 93 | ASSERT_TRUE(TestParse(R"(<string name="foo"> " hey there " </string>)")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 96 | ASSERT_THAT(str, NotNull()); |
| 97 | EXPECT_THAT(*str, StrValueEq(" hey there ")); |
| 98 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 99 | |
| 100 | ASSERT_TRUE(TestParse(R"(<string name="bar">Isn\'t it cool?</string>)")); |
| 101 | str = test::GetValue<String>(&table_, "string/bar"); |
| 102 | ASSERT_THAT(str, NotNull()); |
| 103 | EXPECT_THAT(*str, StrValueEq("Isn't it cool?")); |
| 104 | |
| 105 | ASSERT_TRUE(TestParse(R"(<string name="baz">"Isn't it cool?"</string>)")); |
| 106 | str = test::GetValue<String>(&table_, "string/baz"); |
| 107 | ASSERT_THAT(str, NotNull()); |
| 108 | EXPECT_THAT(*str, StrValueEq("Isn't it cool?")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST_F(ResourceParserTest, ParseEscapedString) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(TestParse(R"(<string name="foo">\?123</string>)")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 115 | ASSERT_THAT(str, NotNull()); |
| 116 | EXPECT_THAT(*str, StrValueEq("?123")); |
| 117 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 118 | |
| 119 | ASSERT_TRUE(TestParse(R"(<string name="bar">This isn\’t a bad string</string>)")); |
| 120 | str = test::GetValue<String>(&table_, "string/bar"); |
| 121 | ASSERT_THAT(str, NotNull()); |
| 122 | EXPECT_THAT(*str, StrValueEq("This isn’t a bad string")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 125 | TEST_F(ResourceParserTest, ParseFormattedString) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 126 | ASSERT_FALSE(TestParse(R"(<string name="foo">%d %s</string>)")); |
| 127 | ASSERT_TRUE(TestParse(R"(<string name="foo">%1$d %2$s</string>)")); |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 130 | TEST_F(ResourceParserTest, ParseStyledString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | // 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] | 132 | // indices use UTF-16 length and not UTF-8 length. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | std::string input = |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 134 | "<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] | 135 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | StyledString* str = test::GetValue<StyledString>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 138 | ASSERT_THAT(str, NotNull()); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 140 | EXPECT_THAT(str->value->value, StrEq("This is my aunt\u2019s fickle string")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 141 | EXPECT_THAT(str->value->spans, SizeIs(2)); |
| 142 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 144 | EXPECT_THAT(*str->value->spans[0].name, StrEq("b")); |
| 145 | EXPECT_THAT(str->value->spans[0].first_char, Eq(18u)); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 146 | EXPECT_THAT(str->value->spans[0].last_char, Eq(30u)); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 147 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 148 | EXPECT_THAT(*str->value->spans[1].name, StrEq("small")); |
| 149 | EXPECT_THAT(str->value->spans[1].first_char, Eq(25u)); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 150 | EXPECT_THAT(str->value->spans[1].last_char, Eq(30u)); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(ResourceParserTest, ParseStringWithWhitespace) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 154 | 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] | 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 157 | ASSERT_THAT(str, NotNull()); |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 158 | EXPECT_THAT(*str->value, StrEq("This is what I think")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 159 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 160 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 161 | 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] | 162 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | str = test::GetValue<String>(&table_, "string/foo2"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 164 | ASSERT_THAT(str, NotNull()); |
| 165 | EXPECT_THAT(*str, StrValueEq(" This is what I think ")); |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ryan Mitchell | 9beaa9c | 2018-03-28 18:22:57 -0700 | [diff] [blame] | 168 | TEST_F(ResourceParserTest, ParseStringTruncateASCII) { |
| 169 | // Tuncate leading and trailing whitespace |
| 170 | EXPECT_TRUE(TestParse(R"(<string name="foo"> Hello </string>)")); |
| 171 | |
| 172 | String* str = test::GetValue<String>(&table_, "string/foo"); |
| 173 | ASSERT_THAT(str, NotNull()); |
| 174 | EXPECT_THAT(*str->value, StrEq("Hello")); |
| 175 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
| 176 | |
| 177 | // AAPT does not truncate unicode whitespace |
| 178 | EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)")); |
| 179 | |
| 180 | str = test::GetValue<String>(&table_, "string/foo2"); |
| 181 | ASSERT_THAT(str, NotNull()); |
| 182 | EXPECT_THAT(*str->value, StrEq(" Hello ")); |
| 183 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
| 184 | |
| 185 | // Preserve non-ASCII whitespace including extended ASCII characters |
Ryan Mitchell | 0f32675 | 2019-03-11 11:00:25 -0700 | [diff] [blame] | 186 | EXPECT_TRUE(TestParse(R"(<string name="foo3"> Hello World </string>)")); |
Ryan Mitchell | 9beaa9c | 2018-03-28 18:22:57 -0700 | [diff] [blame] | 187 | |
| 188 | str = test::GetValue<String>(&table_, "string/foo3"); |
| 189 | ASSERT_THAT(str, NotNull()); |
Ryan Mitchell | 0f32675 | 2019-03-11 11:00:25 -0700 | [diff] [blame] | 190 | EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xE2\x80\xAFWorld\xC2\xA0")); |
Ryan Mitchell | 9beaa9c | 2018-03-28 18:22:57 -0700 | [diff] [blame] | 191 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
| 192 | |
| 193 | EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)")); |
| 194 | |
| 195 | str = test::GetValue<String>(&table_, "string/foo4"); |
| 196 | ASSERT_THAT(str, NotNull()); |
| 197 | EXPECT_THAT(*str->value, StrEq("2005年6月1日")); |
| 198 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
| 199 | } |
| 200 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 201 | TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) { |
| 202 | std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)"; |
| 203 | ASSERT_TRUE(TestParse(input)); |
| 204 | |
| 205 | StyledString* str = test::GetValue<StyledString>(&table_, "string/foo"); |
| 206 | ASSERT_THAT(str, NotNull()); |
| 207 | EXPECT_THAT(str->value->value, StrEq(" My favorite string ")); |
| 208 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
| 209 | |
| 210 | ASSERT_THAT(str->value->spans, SizeIs(2u)); |
| 211 | EXPECT_THAT(*str->value->spans[0].name, StrEq("b")); |
| 212 | EXPECT_THAT(str->value->spans[0].first_char, Eq(1u)); |
| 213 | EXPECT_THAT(str->value->spans[0].last_char, Eq(21u)); |
| 214 | |
| 215 | EXPECT_THAT(*str->value->spans[1].name, StrEq("i")); |
| 216 | EXPECT_THAT(str->value->spans[1].first_char, Eq(5u)); |
| 217 | EXPECT_THAT(str->value->spans[1].last_char, Eq(13u)); |
| 218 | } |
| 219 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 220 | TEST_F(ResourceParserTest, ParseStringTranslatableAttribute) { |
| 221 | // If there is no translate attribute the default is 'true' |
| 222 | EXPECT_TRUE(TestParse(R"(<string name="foo1">Translate</string>)")); |
| 223 | String* str = test::GetValue<String>(&table_, "string/foo1"); |
| 224 | ASSERT_THAT(str, NotNull()); |
| 225 | ASSERT_TRUE(str->IsTranslatable()); |
| 226 | |
| 227 | // Explicit 'true' translate attribute |
| 228 | EXPECT_TRUE(TestParse(R"(<string name="foo2" translatable="true">Translate</string>)")); |
| 229 | str = test::GetValue<String>(&table_, "string/foo2"); |
| 230 | ASSERT_THAT(str, NotNull()); |
| 231 | ASSERT_TRUE(str->IsTranslatable()); |
| 232 | |
| 233 | // Explicit 'false' translate attribute |
| 234 | EXPECT_TRUE(TestParse(R"(<string name="foo3" translatable="false">Do not translate</string>)")); |
| 235 | str = test::GetValue<String>(&table_, "string/foo3"); |
| 236 | ASSERT_THAT(str, NotNull()); |
| 237 | ASSERT_FALSE(str->IsTranslatable()); |
| 238 | |
| 239 | // Invalid value for the translate attribute, should be boolean ('true' or 'false') |
| 240 | EXPECT_FALSE(TestParse(R"(<string name="foo4" translatable="yes">Translate</string>)")); |
| 241 | } |
| 242 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 243 | TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 244 | std::string input = R"( |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 245 | <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] | 246 | There are <xliff:source>no</xliff:source> apples</string>)"; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 247 | ASSERT_TRUE(TestParse(input)); |
| 248 | |
| 249 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 250 | ASSERT_THAT(str, NotNull()); |
| 251 | EXPECT_THAT(*str, StrValueEq("There are no apples")); |
| 252 | EXPECT_THAT(str->untranslatable_sections, IsEmpty()); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 256 | std::string input = R"( |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 257 | <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] | 258 | 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] | 259 | EXPECT_FALSE(TestParse(input)); |
| 260 | } |
| 261 | |
| 262 | TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 263 | std::string input = R"( |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 264 | <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] | 265 | There are <xliff:g id="count">%1$d</xliff:g> apples</string>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 267 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | String* str = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 269 | ASSERT_THAT(str, NotNull()); |
| 270 | EXPECT_THAT(*str, StrValueEq("There are %1$d apples")); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 271 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 272 | ASSERT_THAT(str->untranslatable_sections, SizeIs(1)); |
| 273 | EXPECT_THAT(str->untranslatable_sections[0].start, Eq(10u)); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 274 | EXPECT_THAT(str->untranslatable_sections[0].end, Eq(14u)); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 278 | std::string input = R"( |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 279 | <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] | 280 | 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] | 281 | ASSERT_TRUE(TestParse(input)); |
| 282 | |
| 283 | StyledString* str = test::GetValue<StyledString>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 284 | ASSERT_THAT(str, NotNull()); |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 285 | EXPECT_THAT(str->value->value, Eq(" There are %1$d apples")); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 286 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 287 | ASSERT_THAT(str->untranslatable_sections, SizeIs(1)); |
| 288 | EXPECT_THAT(str->untranslatable_sections[0].start, Eq(11u)); |
| 289 | EXPECT_THAT(str->untranslatable_sections[0].end, Eq(15u)); |
| 290 | |
| 291 | ASSERT_THAT(str->value->spans, SizeIs(1u)); |
| 292 | EXPECT_THAT(*str->value->spans[0].name, StrEq("b")); |
| 293 | EXPECT_THAT(str->value->spans[0].first_char, Eq(11u)); |
| 294 | EXPECT_THAT(str->value->spans[0].last_char, Eq(14u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 297 | TEST_F(ResourceParserTest, ParseNull) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 298 | std::string input = R"(<integer name="foo">@null</integer>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 300 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 | // The Android runtime treats a value of android::Res_value::TYPE_NULL as |
| 302 | // 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] | 303 | // 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] | 304 | // with a data value of 0. |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 305 | Reference* null_ref = test::GetValue<Reference>(&table_, "integer/foo"); |
| 306 | ASSERT_THAT(null_ref, NotNull()); |
| 307 | EXPECT_FALSE(null_ref->name); |
| 308 | EXPECT_FALSE(null_ref->id); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 309 | EXPECT_THAT(null_ref->reference_type, Eq(Reference::Type::kResource)); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | TEST_F(ResourceParserTest, ParseEmpty) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 313 | std::string input = R"(<integer name="foo">@empty</integer>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 314 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 315 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 316 | BinaryPrimitive* integer = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 317 | ASSERT_THAT(integer, NotNull()); |
| 318 | EXPECT_THAT(integer->value.dataType, Eq(Res_value::TYPE_NULL)); |
| 319 | EXPECT_THAT(integer->value.data, Eq(Res_value::DATA_NULL_EMPTY)); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 322 | TEST_F(ResourceParserTest, ParseAttr) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 323 | std::string input = R"( |
| 324 | <attr name="foo" format="string"/> |
| 325 | <attr name="bar"/>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 326 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 329 | ASSERT_THAT(attr, NotNull()); |
| 330 | EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | attr = test::GetValue<Attribute>(&table_, "attr/bar"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 333 | ASSERT_THAT(attr, NotNull()); |
| 334 | EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_ANY)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 337 | // Old AAPT allowed attributes to be defined under different configurations, but ultimately |
| 338 | // 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] | 339 | TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | const ConfigDescription watch_config = test::ParseConfigOrDie("watch"); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 341 | std::string input = R"( |
| 342 | <attr name="foo" /> |
| 343 | <declare-styleable name="bar"> |
| 344 | <attr name="baz" /> |
| 345 | </declare-styleable>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | ASSERT_TRUE(TestParse(input, watch_config)); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 347 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 348 | EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/foo", watch_config), IsNull()); |
| 349 | EXPECT_THAT(test::GetValueForConfig<Attribute>(&table_, "attr/baz", watch_config), IsNull()); |
| 350 | EXPECT_THAT(test::GetValueForConfig<Styleable>(&table_, "styleable/bar", watch_config), IsNull()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 351 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 352 | EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/foo"), NotNull()); |
| 353 | EXPECT_THAT(test::GetValue<Attribute>(&table_, "attr/baz"), NotNull()); |
| 354 | EXPECT_THAT(test::GetValue<Styleable>(&table_, "styleable/bar"), NotNull()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 357 | TEST_F(ResourceParserTest, ParseAttrWithMinMax) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 358 | 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] | 359 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 360 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 362 | ASSERT_THAT(attr, NotNull()); |
| 363 | EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_INTEGER)); |
| 364 | EXPECT_THAT(attr->min_int, Eq(10)); |
| 365 | EXPECT_THAT(attr->max_int, Eq(23)); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 369 | 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] | 370 | } |
| 371 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 372 | TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 373 | std::string input = R"( |
| 374 | <declare-styleable name="Styleable"> |
| 375 | <attr name="foo" /> |
| 376 | </declare-styleable> |
| 377 | <attr name="foo" format="string"/>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 379 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 380 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 381 | ASSERT_THAT(attr, NotNull()); |
| 382 | EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_STRING)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 386 | std::string input = R"( |
| 387 | <declare-styleable name="Theme"> |
| 388 | <attr name="foo" /> |
| 389 | </declare-styleable> |
| 390 | <declare-styleable name="Window"> |
| 391 | <attr name="foo" format="boolean"/> |
| 392 | </declare-styleable>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 393 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 394 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 396 | ASSERT_THAT(attr, NotNull()); |
| 397 | EXPECT_THAT(attr->type_mask, Eq(ResTable_map::TYPE_BOOLEAN)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | TEST_F(ResourceParserTest, ParseEnumAttr) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 401 | std::string input = R"( |
| 402 | <attr name="foo"> |
| 403 | <enum name="bar" value="0"/> |
| 404 | <enum name="bat" value="1"/> |
| 405 | <enum name="baz" value="2"/> |
| 406 | </attr>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 407 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 408 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 409 | Attribute* enum_attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 410 | ASSERT_THAT(enum_attr, NotNull()); |
| 411 | EXPECT_THAT(enum_attr->type_mask, Eq(ResTable_map::TYPE_ENUM)); |
| 412 | ASSERT_THAT(enum_attr->symbols, SizeIs(3)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 413 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 414 | ASSERT_TRUE(enum_attr->symbols[0].symbol.name); |
| 415 | EXPECT_THAT(enum_attr->symbols[0].symbol.name.value().entry, Eq("bar")); |
| 416 | EXPECT_THAT(enum_attr->symbols[0].value, Eq(0u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 417 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 418 | ASSERT_TRUE(enum_attr->symbols[1].symbol.name); |
| 419 | EXPECT_THAT(enum_attr->symbols[1].symbol.name.value().entry, Eq("bat")); |
| 420 | EXPECT_THAT(enum_attr->symbols[1].value, Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 421 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 422 | ASSERT_TRUE(enum_attr->symbols[2].symbol.name); |
| 423 | EXPECT_THAT(enum_attr->symbols[2].symbol.name.value().entry, Eq("baz")); |
| 424 | EXPECT_THAT(enum_attr->symbols[2].value, Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | TEST_F(ResourceParserTest, ParseFlagAttr) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 428 | std::string input = R"( |
| 429 | <attr name="foo"> |
| 430 | <flag name="bar" value="0"/> |
| 431 | <flag name="bat" value="1"/> |
| 432 | <flag name="baz" value="2"/> |
| 433 | </attr>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 434 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 435 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 436 | Attribute* flag_attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 437 | ASSERT_THAT(flag_attr, NotNull()); |
| 438 | EXPECT_THAT(flag_attr->type_mask, Eq(ResTable_map::TYPE_FLAGS)); |
| 439 | ASSERT_THAT(flag_attr->symbols, SizeIs(3)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 440 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 441 | ASSERT_TRUE(flag_attr->symbols[0].symbol.name); |
| 442 | EXPECT_THAT(flag_attr->symbols[0].symbol.name.value().entry, Eq("bar")); |
| 443 | EXPECT_THAT(flag_attr->symbols[0].value, Eq(0u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 444 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 445 | ASSERT_TRUE(flag_attr->symbols[1].symbol.name); |
| 446 | EXPECT_THAT(flag_attr->symbols[1].symbol.name.value().entry, Eq("bat")); |
| 447 | EXPECT_THAT(flag_attr->symbols[1].value, Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 448 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 449 | ASSERT_TRUE(flag_attr->symbols[2].symbol.name); |
| 450 | EXPECT_THAT(flag_attr->symbols[2].symbol.name.value().entry, Eq("baz")); |
| 451 | EXPECT_THAT(flag_attr->symbols[2].value, Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 452 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 453 | std::unique_ptr<BinaryPrimitive> flag_value = |
| 454 | ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 455 | ASSERT_THAT(flag_value, NotNull()); |
| 456 | EXPECT_THAT(flag_value->value.data, Eq(1u | 2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 460 | std::string input = R"( |
| 461 | <attr name="foo"> |
| 462 | <enum name="bar" value="0"/> |
| 463 | <enum name="bat" value="1"/> |
| 464 | <enum name="bat" value="2"/> |
| 465 | </attr>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 466 | ASSERT_FALSE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | TEST_F(ResourceParserTest, ParseStyle) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 470 | std::string input = R"( |
| 471 | <style name="foo" parent="@style/fu"> |
| 472 | <item name="bar">#ffffffff</item> |
| 473 | <item name="bat">@string/hey</item> |
| 474 | <item name="baz"><b>hey</b></item> |
| 475 | </style>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 476 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 477 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 478 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 479 | ASSERT_THAT(style, NotNull()); |
| 480 | ASSERT_TRUE(style->parent); |
| 481 | EXPECT_THAT(style->parent.value().name, Eq(make_value(test::ParseNameOrDie("style/fu")))); |
| 482 | ASSERT_THAT(style->entries, SizeIs(3)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 483 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 484 | EXPECT_THAT(style->entries[0].key.name, Eq(make_value(test::ParseNameOrDie("attr/bar")))); |
| 485 | EXPECT_THAT(style->entries[1].key.name, Eq(make_value(test::ParseNameOrDie("attr/bat")))); |
| 486 | 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] | 487 | } |
| 488 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 489 | TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 490 | ASSERT_TRUE(TestParse(R"(<style name="foo" parent="com.app:Theme"/>)")); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 491 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 493 | ASSERT_THAT(style, NotNull()); |
| 494 | ASSERT_TRUE(style->parent); |
| 495 | 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] | 496 | } |
| 497 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 498 | TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 499 | std::string input = R"( |
| 500 | <style xmlns:app="http://schemas.android.com/apk/res/android" |
| 501 | name="foo" parent="app:Theme"/>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 502 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 503 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 504 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 505 | ASSERT_THAT(style, NotNull()); |
| 506 | ASSERT_TRUE(style->parent); |
| 507 | ASSERT_TRUE(style->parent.value().name); |
| 508 | 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] | 509 | } |
| 510 | |
| 511 | TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 512 | std::string input = R"( |
| 513 | <style xmlns:app="http://schemas.android.com/apk/res/android" name="foo"> |
| 514 | <item name="app:bar">0</item> |
| 515 | </style>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 516 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 517 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 518 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 519 | ASSERT_THAT(style, NotNull()); |
| 520 | ASSERT_THAT(style->entries, SizeIs(1)); |
| 521 | 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] | 522 | } |
| 523 | |
Ryan Mitchell | 633d796 | 2018-06-11 15:29:21 -0700 | [diff] [blame] | 524 | TEST_F(ResourceParserTest, ParseStyleWithRawStringItem) { |
| 525 | std::string input = R"( |
| 526 | <style name="foo"> |
| 527 | <item name="bar"> |
| 528 | com.helloworld.AppClass |
| 529 | </item> |
| 530 | </style>)"; |
| 531 | ASSERT_TRUE(TestParse(input)); |
| 532 | |
| 533 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
| 534 | ASSERT_THAT(style, NotNull()); |
| 535 | EXPECT_THAT(style->entries[0].value, NotNull()); |
| 536 | RawString* value = ValueCast<RawString>(style->entries[0].value.get()); |
| 537 | EXPECT_THAT(value, NotNull()); |
| 538 | EXPECT_THAT(*value->value, StrEq(R"(com.helloworld.AppClass)")); |
| 539 | } |
| 540 | |
| 541 | |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 542 | TEST_F(ResourceParserTest, ParseStyleWithInferredParent) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 543 | ASSERT_TRUE(TestParse(R"(<style name="foo.bar"/>)")); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 544 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 545 | Style* style = test::GetValue<Style>(&table_, "style/foo.bar"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 546 | ASSERT_THAT(style, NotNull()); |
| 547 | ASSERT_TRUE(style->parent); |
| 548 | 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] | 549 | EXPECT_TRUE(style->parent_inferred); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 552 | TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) { |
| 553 | ASSERT_TRUE(TestParse(R"(<style name="foo.bar" parent=""/>)")); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 554 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 555 | Style* style = test::GetValue<Style>(&table_, "style/foo.bar"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 556 | ASSERT_THAT(style, NotNull()); |
| 557 | EXPECT_FALSE(style->parent); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 558 | EXPECT_FALSE(style->parent_inferred); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 561 | TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 562 | ASSERT_TRUE(TestParse(R"(<style name="foo" parent="*android:style/bar" />)")); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 563 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 564 | Style* style = test::GetValue<Style>(&table_, "style/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 565 | ASSERT_THAT(style, NotNull()); |
| 566 | ASSERT_TRUE(style->parent); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 567 | EXPECT_TRUE(style->parent.value().private_reference); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 570 | TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 571 | ASSERT_TRUE(TestParse(R"(<string name="foo">@+id/bar</string>)")); |
| 572 | ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 576 | std::string input = R"( |
| 577 | <declare-styleable name="foo"> |
| 578 | <attr name="bar" /> |
| 579 | <attr name="bat" format="string|reference"/> |
| 580 | <attr name="baz"> |
| 581 | <enum name="foo" value="1"/> |
| 582 | </attr> |
| 583 | </declare-styleable>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 584 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 585 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 586 | Maybe<ResourceTable::SearchResult> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 587 | table_.FindResource(test::ParseNameOrDie("styleable/foo")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 588 | ASSERT_TRUE(result); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 589 | EXPECT_THAT(result.value().entry->visibility.level, Eq(Visibility::Level::kPublic)); |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 590 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 591 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/bar"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 592 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 593 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 594 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 595 | attr = test::GetValue<Attribute>(&table_, "attr/bat"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 596 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 597 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 598 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 599 | attr = test::GetValue<Attribute>(&table_, "attr/baz"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 600 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 601 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 602 | EXPECT_THAT(attr->symbols, SizeIs(1)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 603 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 604 | EXPECT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 605 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 606 | Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 607 | ASSERT_THAT(styleable, NotNull()); |
| 608 | ASSERT_THAT(styleable->entries, SizeIs(3)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 609 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 610 | EXPECT_THAT(styleable->entries[0].name, Eq(make_value(test::ParseNameOrDie("attr/bar")))); |
| 611 | EXPECT_THAT(styleable->entries[1].name, Eq(make_value(test::ParseNameOrDie("attr/bat")))); |
| 612 | 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] | 613 | } |
| 614 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 615 | TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 616 | std::string input = R"( |
| 617 | <declare-styleable xmlns:privAndroid="http://schemas.android.com/apk/prv/res/android" |
| 618 | name="foo"> |
| 619 | <attr name="*android:bar" /> |
| 620 | <attr name="privAndroid:bat" /> |
| 621 | </declare-styleable>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 622 | ASSERT_TRUE(TestParse(input)); |
| 623 | Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 624 | ASSERT_THAT(styleable, NotNull()); |
| 625 | ASSERT_THAT(styleable->entries, SizeIs(2)); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 626 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 627 | EXPECT_TRUE(styleable->entries[0].private_reference); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 628 | ASSERT_TRUE(styleable->entries[0].name); |
| 629 | EXPECT_THAT(styleable->entries[0].name.value().package, Eq("android")); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 630 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 631 | EXPECT_TRUE(styleable->entries[1].private_reference); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 632 | ASSERT_TRUE(styleable->entries[1].name); |
| 633 | EXPECT_THAT(styleable->entries[1].name.value().package, Eq("android")); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 636 | TEST_F(ResourceParserTest, ParseArray) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 637 | std::string input = R"( |
| 638 | <array name="foo"> |
| 639 | <item>@string/ref</item> |
| 640 | <item>hey</item> |
| 641 | <item>23</item> |
| 642 | </array>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 643 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 644 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 645 | Array* array = test::GetValue<Array>(&table_, "array/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 646 | ASSERT_THAT(array, NotNull()); |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 647 | ASSERT_THAT(array->elements, SizeIs(3)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 648 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 649 | EXPECT_THAT(ValueCast<Reference>(array->elements[0].get()), NotNull()); |
| 650 | EXPECT_THAT(ValueCast<String>(array->elements[1].get()), NotNull()); |
| 651 | EXPECT_THAT(ValueCast<BinaryPrimitive>(array->elements[2].get()), NotNull()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 654 | TEST_F(ResourceParserTest, ParseStringArray) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 655 | std::string input = R"( |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 656 | <string-array name="foo"> |
| 657 | <item>"Werk"</item>" |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 658 | </string-array>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 659 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 660 | EXPECT_THAT(test::GetValue<Array>(&table_, "array/foo"), NotNull()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 663 | TEST_F(ResourceParserTest, ParseArrayWithFormat) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 664 | std::string input = R"( |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 665 | <array name="foo" format="string"> |
| 666 | <item>100</item> |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 667 | </array>)"; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 668 | ASSERT_TRUE(TestParse(input)); |
| 669 | |
| 670 | Array* array = test::GetValue<Array>(&table_, "array/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 671 | ASSERT_THAT(array, NotNull()); |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 672 | ASSERT_THAT(array->elements, SizeIs(1)); |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 673 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 674 | String* str = ValueCast<String>(array->elements[0].get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 675 | ASSERT_THAT(str, NotNull()); |
| 676 | EXPECT_THAT(*str, StrValueEq("100")); |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | TEST_F(ResourceParserTest, ParseArrayWithBadFormat) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 680 | std::string input = R"( |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 681 | <array name="foo" format="integer"> |
| 682 | <item>Hi</item> |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 683 | </array>)"; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 684 | ASSERT_FALSE(TestParse(input)); |
| 685 | } |
| 686 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 687 | TEST_F(ResourceParserTest, ParsePlural) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 688 | std::string input = R"( |
| 689 | <plurals name="foo"> |
| 690 | <item quantity="other">apples</item> |
| 691 | <item quantity="one">apple</item> |
| 692 | </plurals>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 693 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 694 | |
| 695 | Plural* plural = test::GetValue<Plural>(&table_, "plurals/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 696 | ASSERT_THAT(plural, NotNull()); |
| 697 | EXPECT_THAT(plural->values[Plural::Zero], IsNull()); |
| 698 | EXPECT_THAT(plural->values[Plural::Two], IsNull()); |
| 699 | EXPECT_THAT(plural->values[Plural::Few], IsNull()); |
| 700 | EXPECT_THAT(plural->values[Plural::Many], IsNull()); |
Adam Lesinski | 8f7c550 | 2017-03-02 17:45:01 -0800 | [diff] [blame] | 701 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 702 | EXPECT_THAT(plural->values[Plural::One], NotNull()); |
| 703 | EXPECT_THAT(plural->values[Plural::Other], NotNull()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | TEST_F(ResourceParserTest, ParseCommentsWithResource) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 707 | std::string input = R"( |
| 708 | <!--This is a comment--> |
| 709 | <string name="foo">Hi</string>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 710 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 711 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 712 | String* value = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 713 | ASSERT_THAT(value, NotNull()); |
| 714 | EXPECT_THAT(value->GetComment(), Eq("This is a comment")); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 715 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 716 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 717 | TEST_F(ResourceParserTest, DoNotCombineMultipleComments) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 718 | std::string input = R"( |
| 719 | <!--One--> |
| 720 | <!--Two--> |
| 721 | <string name="foo">Hi</string>)"; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 722 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 723 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 724 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 725 | String* value = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 726 | ASSERT_THAT(value, NotNull()); |
| 727 | EXPECT_THAT(value->GetComment(), Eq("Two")); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 731 | std::string input = R"( |
| 732 | <!--One--> |
| 733 | <string name="foo"> |
| 734 | Hi |
| 735 | <!--Two--> |
| 736 | </string>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 737 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 738 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 739 | String* value = test::GetValue<String>(&table_, "string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 740 | ASSERT_THAT(value, NotNull()); |
| 741 | EXPECT_THAT(value->GetComment(), Eq("One")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 744 | TEST_F(ResourceParserTest, ParseNestedComments) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 745 | // We only care about declare-styleable and enum/flag attributes because |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 746 | // comments from those end up in R.java |
| 747 | std::string input = R"( |
| 748 | <declare-styleable name="foo"> |
| 749 | <!-- The name of the bar --> |
| 750 | <attr name="barName" format="string|reference" /> |
| 751 | </declare-styleable> |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 752 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 753 | <attr name="foo"> |
| 754 | <!-- The very first --> |
| 755 | <enum name="one" value="1" /> |
| 756 | </attr>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 757 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 758 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 759 | Styleable* styleable = test::GetValue<Styleable>(&table_, "styleable/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 760 | ASSERT_THAT(styleable, NotNull()); |
| 761 | ASSERT_THAT(styleable->entries, SizeIs(1)); |
| 762 | EXPECT_THAT(styleable->entries[0].GetComment(), Eq("The name of the bar")); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 763 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 764 | Attribute* attr = test::GetValue<Attribute>(&table_, "attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 765 | ASSERT_THAT(attr, NotNull()); |
| 766 | ASSERT_THAT(attr->symbols, SizeIs(1)); |
| 767 | EXPECT_THAT(attr->symbols[0].symbol.GetComment(), Eq("The very first")); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 770 | // 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] | 771 | TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 772 | ASSERT_TRUE(TestParse(R"(<public type="id" name="foo"/>)")); |
| 773 | ASSERT_THAT(test::GetValue<Id>(&table_, "id/foo"), NotNull()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 776 | TEST_F(ResourceParserTest, KeepAllProducts) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 777 | std::string input = R"( |
| 778 | <string name="foo" product="phone">hi</string> |
| 779 | <string name="foo" product="no-sdcard">ho</string> |
| 780 | <string name="bar" product="">wee</string> |
| 781 | <string name="baz">woo</string> |
| 782 | <string name="bit" product="phablet">hoot</string> |
| 783 | <string name="bot" product="default">yes</string>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 784 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 785 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 786 | ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo", ConfigDescription::DefaultConfig(), "phone"), NotNull()); |
| 787 | ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/foo",ConfigDescription::DefaultConfig(), "no-sdcard"), NotNull()); |
| 788 | ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bar", ConfigDescription::DefaultConfig(), ""), NotNull()); |
| 789 | ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/baz", ConfigDescription::DefaultConfig(), ""), NotNull()); |
| 790 | ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bit", ConfigDescription::DefaultConfig(), "phablet"), NotNull()); |
| 791 | ASSERT_THAT(test::GetValueForConfigAndProduct<String>(&table_, "string/bot", ConfigDescription::DefaultConfig(), "default"), NotNull()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 794 | TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 795 | std::string input = R"( |
| 796 | <public-group type="attr" first-id="0x01010040"> |
| 797 | <public name="foo" /> |
| 798 | <public name="bar" /> |
| 799 | </public-group>)"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 800 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 801 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 802 | Maybe<ResourceTable::SearchResult> result = table_.FindResource(test::ParseNameOrDie("attr/foo")); |
| 803 | ASSERT_TRUE(result); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 804 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 805 | ASSERT_TRUE(result.value().package->id); |
| 806 | ASSERT_TRUE(result.value().type->id); |
| 807 | ASSERT_TRUE(result.value().entry->id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 808 | ResourceId actual_id(result.value().package->id.value(), |
| 809 | result.value().type->id.value(), |
| 810 | result.value().entry->id.value()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 811 | EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 812 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 813 | result = table_.FindResource(test::ParseNameOrDie("attr/bar")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 814 | ASSERT_TRUE(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 815 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 816 | ASSERT_TRUE(result.value().package->id); |
| 817 | ASSERT_TRUE(result.value().type->id); |
| 818 | ASSERT_TRUE(result.value().entry->id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 819 | actual_id = ResourceId(result.value().package->id.value(), |
| 820 | result.value().type->id.value(), |
| 821 | result.value().entry->id.value()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 822 | EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041))); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 825 | TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) { |
| 826 | std::string input = R"( |
| 827 | <!-- private --> |
| 828 | <java-symbol type="string" name="foo" /> |
| 829 | <!-- public --> |
| 830 | <public type="string" name="foo" id="0x01020000" /> |
| 831 | <!-- private2 --> |
| 832 | <java-symbol type="string" name="foo" />)"; |
| 833 | ASSERT_TRUE(TestParse(input)); |
| 834 | |
| 835 | Maybe<ResourceTable::SearchResult> result = |
| 836 | table_.FindResource(test::ParseNameOrDie("string/foo")); |
| 837 | ASSERT_TRUE(result); |
| 838 | |
| 839 | ResourceEntry* entry = result.value().entry; |
| 840 | ASSERT_THAT(entry, NotNull()); |
| 841 | EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kPublic)); |
| 842 | EXPECT_THAT(entry->visibility.comment, StrEq("public")); |
| 843 | } |
| 844 | |
Adam Lesinski | fa10505 | 2015-11-07 13:34:39 -0800 | [diff] [blame] | 845 | TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 846 | ASSERT_TRUE(TestParse(R"(<item type="layout" name="foo">@layout/bar</item>)")); |
| 847 | 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] | 848 | } |
| 849 | |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 850 | TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 851 | ASSERT_TRUE(TestParse(R"(<add-resource name="bar" type="string" />)")); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 852 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 853 | Maybe<ResourceTable::SearchResult> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 854 | table_.FindResource(test::ParseNameOrDie("string/bar")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 855 | ASSERT_TRUE(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 856 | const ResourceEntry* entry = result.value().entry; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 857 | ASSERT_THAT(entry, NotNull()); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 858 | EXPECT_THAT(entry->visibility.level, Eq(Visibility::Level::kUndefined)); |
| 859 | EXPECT_TRUE(entry->allow_new); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 862 | TEST_F(ResourceParserTest, ParseItemElementWithFormat) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 863 | 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] | 864 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 865 | BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); |
| 866 | ASSERT_THAT(val, NotNull()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 867 | EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FLOAT)); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 868 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 869 | 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] | 870 | } |
| 871 | |
| 872 | // An <item> without a format specifier accepts all types of values. |
| 873 | TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 874 | ASSERT_TRUE(TestParse(R"(<item name="foo" type="integer">100%p</item>)")); |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 875 | |
| 876 | BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); |
| 877 | ASSERT_THAT(val, NotNull()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 878 | EXPECT_THAT(val->value.dataType, Eq(Res_value::TYPE_FRACTION)); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 881 | TEST_F(ResourceParserTest, ParseConfigVaryingItem) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 882 | ASSERT_TRUE(TestParse(R"(<item name="foo" type="configVarying">Hey</item>)")); |
| 883 | ASSERT_THAT(test::GetValue<String>(&table_, "configVarying/foo"), NotNull()); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | TEST_F(ResourceParserTest, ParseBagElement) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 887 | std::string input = R"( |
| 888 | <bag name="bag" type="configVarying"> |
| 889 | <item name="test">Hello!</item> |
| 890 | </bag>)"; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 891 | ASSERT_TRUE(TestParse(input)); |
| 892 | |
| 893 | Style* val = test::GetValue<Style>(&table_, "configVarying/bag"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 894 | ASSERT_THAT(val, NotNull()); |
| 895 | ASSERT_THAT(val->entries, SizeIs(1)); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 896 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 897 | EXPECT_THAT(val->entries[0].key, Eq(Reference(test::ParseNameOrDie("attr/test")))); |
| 898 | EXPECT_THAT(ValueCast<RawString>(val->entries[0].value.get()), NotNull()); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 901 | TEST_F(ResourceParserTest, ParseElementWithNoValue) { |
| 902 | std::string input = R"( |
| 903 | <item type="drawable" format="reference" name="foo" /> |
| 904 | <string name="foo" />)"; |
| 905 | ASSERT_TRUE(TestParse(input)); |
| 906 | ASSERT_THAT(test::GetValue(&table_, "drawable/foo"), Pointee(ValueEq(Reference()))); |
| 907 | |
| 908 | String* str = test::GetValue<String>(&table_, "string/foo"); |
| 909 | ASSERT_THAT(str, NotNull()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 910 | EXPECT_THAT(*str, StrValueEq("")); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 913 | TEST_F(ResourceParserTest, ParsePlatformIndependentNewline) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 914 | 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] | 915 | } |
| 916 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 917 | TEST_F(ResourceParserTest, ParseOverlayable) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 918 | std::string input = R"( |
| 919 | <overlayable name="Name" actor="overlay://theme"> |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 920 | <policy type="signature"> |
| 921 | <item type="string" name="foo" /> |
| 922 | <item type="drawable" name="bar" /> |
| 923 | </policy> |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 924 | </overlayable>)"; |
| 925 | ASSERT_TRUE(TestParse(input)); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 926 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 927 | auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo")); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 928 | ASSERT_TRUE(search_result); |
| 929 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 930 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 931 | OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 932 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 933 | EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme")); |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 934 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 935 | |
| 936 | search_result = table_.FindResource(test::ParseNameOrDie("drawable/bar")); |
| 937 | ASSERT_TRUE(search_result); |
| 938 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 939 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 940 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 941 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 942 | EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme")); |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 943 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature)); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | TEST_F(ResourceParserTest, ParseOverlayableRequiresName) { |
| 947 | EXPECT_FALSE(TestParse(R"(<overlayable actor="overlay://theme" />)")); |
| 948 | EXPECT_TRUE(TestParse(R"(<overlayable name="Name" />)")); |
| 949 | EXPECT_TRUE(TestParse(R"(<overlayable name="Name" actor="overlay://theme" />)")); |
| 950 | } |
| 951 | |
| 952 | TEST_F(ResourceParserTest, ParseOverlayableBadActorFail) { |
| 953 | EXPECT_FALSE(TestParse(R"(<overlayable name="Name" actor="overley://theme" />)")); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | TEST_F(ResourceParserTest, ParseOverlayablePolicy) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 957 | std::string input = R"( |
| 958 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 959 | <policy type="product"> |
| 960 | <item type="string" name="bar" /> |
| 961 | </policy> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 962 | <policy type="system"> |
| 963 | <item type="string" name="fiz" /> |
| 964 | </policy> |
| 965 | <policy type="vendor"> |
| 966 | <item type="string" name="fuz" /> |
| 967 | </policy> |
| 968 | <policy type="public"> |
| 969 | <item type="string" name="faz" /> |
| 970 | </policy> |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 971 | <policy type="signature"> |
| 972 | <item type="string" name="foz" /> |
| 973 | </policy> |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 974 | <policy type="odm"> |
| 975 | <item type="string" name="biz" /> |
| 976 | </policy> |
| 977 | <policy type="oem"> |
| 978 | <item type="string" name="buz" /> |
| 979 | </policy> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 980 | </overlayable>)"; |
| 981 | ASSERT_TRUE(TestParse(input)); |
| 982 | |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 983 | auto search_result = table_.FindResource(test::ParseNameOrDie("string/bar")); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 984 | ASSERT_TRUE(search_result); |
| 985 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 986 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 987 | OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 988 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 989 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 990 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 991 | search_result = table_.FindResource(test::ParseNameOrDie("string/fiz")); |
| 992 | ASSERT_TRUE(search_result); |
| 993 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 994 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 995 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 996 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 997 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSystem)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 998 | |
| 999 | search_result = table_.FindResource(test::ParseNameOrDie("string/fuz")); |
| 1000 | ASSERT_TRUE(search_result); |
| 1001 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1002 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1003 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1004 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1005 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kVendor)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1006 | |
| 1007 | search_result = table_.FindResource(test::ParseNameOrDie("string/faz")); |
| 1008 | ASSERT_TRUE(search_result); |
| 1009 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1010 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1011 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1012 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1013 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kPublic)); |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1014 | |
| 1015 | search_result = table_.FindResource(test::ParseNameOrDie("string/foz")); |
| 1016 | ASSERT_TRUE(search_result); |
| 1017 | ASSERT_THAT(search_result.value().entry, NotNull()); |
| 1018 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1019 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1020 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1021 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kSignature)); |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1022 | |
| 1023 | search_result = table_.FindResource(test::ParseNameOrDie("string/biz")); |
| 1024 | ASSERT_TRUE(search_result); |
| 1025 | ASSERT_THAT(search_result.value().entry, NotNull()); |
| 1026 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1027 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1028 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1029 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kOdm)); |
| 1030 | |
| 1031 | search_result = table_.FindResource(test::ParseNameOrDie("string/buz")); |
| 1032 | ASSERT_TRUE(search_result); |
| 1033 | ASSERT_THAT(search_result.value().entry, NotNull()); |
| 1034 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1035 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1036 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1037 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kOem)); |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | TEST_F(ResourceParserTest, ParseOverlayableNoPolicyError) { |
| 1041 | std::string input = R"( |
| 1042 | <overlayable name="Name"> |
| 1043 | <item type="string" name="foo" /> |
| 1044 | </overlayable>)"; |
| 1045 | EXPECT_FALSE(TestParse(input)); |
| 1046 | |
| 1047 | input = R"( |
| 1048 | <overlayable name="Name"> |
| 1049 | <policy> |
| 1050 | <item name="foo" /> |
| 1051 | </policy> |
| 1052 | </overlayable>)"; |
| 1053 | EXPECT_FALSE(TestParse(input)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | TEST_F(ResourceParserTest, ParseOverlayableBadPolicyError) { |
| 1057 | std::string input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1058 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1059 | <policy type="illegal_policy"> |
| 1060 | <item type="string" name="foo" /> |
| 1061 | </policy> |
| 1062 | </overlayable>)"; |
| 1063 | EXPECT_FALSE(TestParse(input)); |
| 1064 | |
| 1065 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1066 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1067 | <policy type="product"> |
| 1068 | <item name="foo" /> |
| 1069 | </policy> |
| 1070 | </overlayable>)"; |
| 1071 | EXPECT_FALSE(TestParse(input)); |
| 1072 | |
| 1073 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1074 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1075 | <policy type="vendor"> |
| 1076 | <item type="string" /> |
| 1077 | </policy> |
| 1078 | </overlayable>)"; |
| 1079 | EXPECT_FALSE(TestParse(input)); |
| 1080 | } |
| 1081 | |
| 1082 | TEST_F(ResourceParserTest, ParseOverlayableMultiplePolicy) { |
| 1083 | std::string input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1084 | <overlayable name="Name"> |
Ryan Mitchell | 02d9c1e | 2019-01-11 16:36:58 -0800 | [diff] [blame] | 1085 | <policy type="vendor|public"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1086 | <item type="string" name="foo" /> |
| 1087 | </policy> |
| 1088 | <policy type="product|system"> |
| 1089 | <item type="string" name="bar" /> |
| 1090 | </policy> |
| 1091 | </overlayable>)"; |
| 1092 | ASSERT_TRUE(TestParse(input)); |
| 1093 | |
| 1094 | auto search_result = table_.FindResource(test::ParseNameOrDie("string/foo")); |
| 1095 | ASSERT_TRUE(search_result); |
| 1096 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1097 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1098 | OverlayableItem result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1099 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1100 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kVendor |
Ryan Mitchell | 02d9c1e | 2019-01-11 16:36:58 -0800 | [diff] [blame] | 1101 | | OverlayableItem::Policy::kPublic)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1102 | |
| 1103 | search_result = table_.FindResource(test::ParseNameOrDie("string/bar")); |
| 1104 | ASSERT_TRUE(search_result); |
| 1105 | ASSERT_THAT(search_result.value().entry, NotNull()); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1106 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
| 1107 | result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 1108 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 1109 | EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct |
| 1110 | | OverlayableItem::Policy::kSystem)); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | TEST_F(ResourceParserTest, DuplicateOverlayableIsError) { |
| 1114 | std::string input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1115 | <overlayable name="Name"> |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1116 | <item type="string" name="foo" /> |
| 1117 | <item type="string" name="foo" /> |
| 1118 | </overlayable>)"; |
| 1119 | EXPECT_FALSE(TestParse(input)); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1120 | |
| 1121 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1122 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1123 | <item type="string" name="foo" /> |
| 1124 | </overlayable> |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1125 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1126 | <item type="string" name="foo" /> |
| 1127 | </overlayable>)"; |
| 1128 | EXPECT_FALSE(TestParse(input)); |
| 1129 | |
| 1130 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1131 | <overlayable name="Name"> |
| 1132 | <item type="string" name="foo" /> |
| 1133 | </overlayable> |
| 1134 | <overlayable name="Other"> |
| 1135 | <item type="string" name="foo" /> |
| 1136 | </overlayable>)"; |
| 1137 | EXPECT_FALSE(TestParse(input)); |
| 1138 | |
| 1139 | input = R"( |
| 1140 | <overlayable name="Name" actor="overlay://my.actor.one"> |
| 1141 | <item type="string" name="foo" /> |
| 1142 | </overlayable> |
| 1143 | <overlayable name="Other" actor="overlay://my.actor.two"> |
| 1144 | <item type="string" name="foo" /> |
| 1145 | </overlayable>)"; |
| 1146 | EXPECT_FALSE(TestParse(input)); |
| 1147 | |
| 1148 | input = R"( |
| 1149 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1150 | <policy type="product"> |
| 1151 | <item type="string" name="foo" /> |
| 1152 | <item type="string" name="foo" /> |
| 1153 | </policy> |
| 1154 | </overlayable>)"; |
| 1155 | EXPECT_FALSE(TestParse(input)); |
| 1156 | |
| 1157 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1158 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1159 | <policy type="product"> |
| 1160 | <item type="string" name="foo" /> |
| 1161 | </policy> |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1162 | <item type="string" name="foo" /> |
| 1163 | </overlayable>)"; |
| 1164 | EXPECT_FALSE(TestParse(input)); |
| 1165 | |
| 1166 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1167 | <overlayable name="Name"> |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1168 | <policy type="product"> |
| 1169 | <item type="string" name="foo" /> |
| 1170 | </policy> |
| 1171 | <policy type="vendor"> |
| 1172 | <item type="string" name="foo" /> |
| 1173 | </policy> |
| 1174 | </overlayable>)"; |
| 1175 | EXPECT_FALSE(TestParse(input)); |
| 1176 | |
| 1177 | input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1178 | <overlayable name="Name"> |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1179 | <policy type="product"> |
| 1180 | <item type="string" name="foo" /> |
| 1181 | </policy> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1182 | </overlayable> |
| 1183 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1184 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1185 | <policy type="product"> |
| 1186 | <item type="string" name="foo" /> |
| 1187 | </policy> |
| 1188 | </overlayable>)"; |
| 1189 | EXPECT_FALSE(TestParse(input)); |
| 1190 | } |
| 1191 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1192 | TEST_F(ResourceParserTest, NestPolicyInOverlayableError) { |
| 1193 | std::string input = R"( |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1194 | <overlayable name="Name"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1195 | <policy type="vendor|product"> |
Ryan Mitchell | 02d9c1e | 2019-01-11 16:36:58 -0800 | [diff] [blame] | 1196 | <policy type="public"> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1197 | <item type="string" name="foo" /> |
| 1198 | </policy> |
| 1199 | </policy> |
| 1200 | </overlayable>)"; |
| 1201 | EXPECT_FALSE(TestParse(input)); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1204 | TEST_F(ResourceParserTest, ParseIdItem) { |
| 1205 | std::string input = R"( |
| 1206 | <item name="foo" type="id">@id/bar</item> |
| 1207 | <item name="bar" type="id"/> |
| 1208 | <item name="baz" type="id"></item>)"; |
| 1209 | ASSERT_TRUE(TestParse(input)); |
| 1210 | |
| 1211 | ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo"), NotNull()); |
| 1212 | ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull()); |
| 1213 | ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull()); |
| 1214 | |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 1215 | input = R"( |
| 1216 | <id name="foo2">@id/bar</id> |
| 1217 | <id name="bar2"/> |
| 1218 | <id name="baz2"></id>)"; |
| 1219 | ASSERT_TRUE(TestParse(input)); |
| 1220 | |
| 1221 | ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull()); |
| 1222 | ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull()); |
| 1223 | ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull()); |
| 1224 | |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1225 | // Reject attribute references |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 1226 | input = R"(<item name="foo3" type="id">?attr/bar"</item>)"; |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1227 | ASSERT_FALSE(TestParse(input)); |
| 1228 | |
| 1229 | // Reject non-references |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 1230 | input = R"(<item name="foo4" type="id">0x7f010001</item>)"; |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1231 | ASSERT_FALSE(TestParse(input)); |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 1232 | input = R"(<item name="foo5" type="id">@drawable/my_image</item>)"; |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1233 | ASSERT_FALSE(TestParse(input)); |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 1234 | input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)"; |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1235 | ASSERT_FALSE(TestParse(input)); |
| 1236 | |
| 1237 | // Ids that reference other resource ids cannot be public |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 1238 | input = R"(<public name="foo7" type="id">@id/bar7</item>)"; |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 1239 | ASSERT_FALSE(TestParse(input)); |
| 1240 | } |
| 1241 | |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1242 | TEST_F(ResourceParserTest, ParseCData) { |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1243 | // Double quotes should still change the state of whitespace processing |
| 1244 | std::string input = R"(<string name="foo">Hello<![CDATA[ "</string>' ]]> World</string>)"; |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1245 | ASSERT_TRUE(TestParse(input)); |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1246 | auto output = test::GetValue<String>(&table_, "string/foo"); |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1247 | ASSERT_THAT(output, NotNull()); |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1248 | EXPECT_THAT(*output, StrValueEq(std::string("Hello </string>' World").data())); |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1249 | |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1250 | input = R"(<string name="foo2"><![CDATA[Hello |
| 1251 | World]]></string>)"; |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1252 | ASSERT_TRUE(TestParse(input)); |
| 1253 | output = test::GetValue<String>(&table_, "string/foo2"); |
| 1254 | ASSERT_THAT(output, NotNull()); |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1255 | EXPECT_THAT(*output, StrValueEq(std::string("Hello World").data())); |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1256 | |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1257 | // Cdata blocks should have their whitespace trimmed |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1258 | input = R"(<string name="foo3"> <![CDATA[ text ]]> </string>)"; |
| 1259 | ASSERT_TRUE(TestParse(input)); |
| 1260 | output = test::GetValue<String>(&table_, "string/foo3"); |
| 1261 | ASSERT_THAT(output, NotNull()); |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1262 | EXPECT_THAT(*output, StrValueEq(std::string("text").data())); |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1263 | |
| 1264 | input = R"(<string name="foo4"> <![CDATA[]]> </string>)"; |
| 1265 | ASSERT_TRUE(TestParse(input)); |
| 1266 | output = test::GetValue<String>(&table_, "string/foo4"); |
| 1267 | ASSERT_THAT(output, NotNull()); |
| 1268 | EXPECT_THAT(*output, StrValueEq(std::string("").data())); |
| 1269 | |
| 1270 | input = R"(<string name="foo5"> <![CDATA[ ]]> </string>)"; |
| 1271 | ASSERT_TRUE(TestParse(input)); |
| 1272 | output = test::GetValue<String>(&table_, "string/foo5"); |
| 1273 | ASSERT_THAT(output, NotNull()); |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 1274 | EXPECT_THAT(*output, StrValueEq(std::string("").data())); |
| 1275 | |
| 1276 | // Single quotes must still be escaped |
| 1277 | input = R"(<string name="foo6"><![CDATA[some text and ' apostrophe]]></string>)"; |
| 1278 | ASSERT_FALSE(TestParse(input)); |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1281 | } // namespace aapt |