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