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" |
| 18 | #include "ResourceTable.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 19 | #include "ResourceUtils.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "test/Context.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 22 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <sstream> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace aapt { |
| 29 | |
| 30 | constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
| 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) { |
| 33 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 34 | std::stringstream input(kXmlPreamble); |
| 35 | input << "<attr name=\"foo\"/>" << std::endl; |
| 36 | ResourceTable table; |
| 37 | ResourceParser parser(context->getDiagnostics(), &table, Source{ "test" }, {}); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 38 | xml::XmlPullParser xmlParser(input); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | ASSERT_FALSE(parser.parse(&xmlParser)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | struct ResourceParserTest : public ::testing::Test { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | ResourceTable mTable; |
| 44 | std::unique_ptr<IAaptContext> mContext; |
| 45 | |
| 46 | void SetUp() override { |
| 47 | mContext = test::ContextBuilder().build(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 50 | ::testing::AssertionResult testParse(const StringPiece& str) { |
| 51 | return testParse(str, ConfigDescription{}, {}); |
| 52 | } |
| 53 | |
| 54 | ::testing::AssertionResult testParse(const StringPiece& str, const ConfigDescription& config) { |
| 55 | return testParse(str, config, {}); |
| 56 | } |
| 57 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 58 | ::testing::AssertionResult testParse(const StringPiece& str, |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 59 | std::initializer_list<std::u16string> products) { |
| 60 | return testParse(str, {}, std::move(products)); |
| 61 | } |
| 62 | |
| 63 | ::testing::AssertionResult testParse(const StringPiece& str, const ConfigDescription& config, |
| 64 | std::initializer_list<std::u16string> products) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | std::stringstream input(kXmlPreamble); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 66 | input << "<resources>\n" << str << "\n</resources>" << std::endl; |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 67 | ResourceParserOptions parserOptions; |
Adam Lesinski | 7751afc | 2016-01-06 15:45:28 -0800 | [diff] [blame] | 68 | parserOptions.products = products; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 69 | ResourceParser parser(mContext->getDiagnostics(), &mTable, Source{ "test" }, config, |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 70 | parserOptions); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 71 | xml::XmlPullParser xmlParser(input); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | if (parser.parse(&xmlParser)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | return ::testing::AssertionSuccess(); |
| 74 | } |
| 75 | return ::testing::AssertionFailure(); |
| 76 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | TEST_F(ResourceParserTest, ParseQuotedString) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 80 | std::string input = "<string name=\"foo\"> \" hey there \" </string>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | ASSERT_TRUE(testParse(input)); |
| 82 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | String* str = test::getValue<String>(&mTable, u"@string/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | ASSERT_NE(nullptr, str); |
| 85 | EXPECT_EQ(std::u16string(u" hey there "), *str->value); |
| 86 | } |
| 87 | |
| 88 | TEST_F(ResourceParserTest, ParseEscapedString) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 89 | std::string input = "<string name=\"foo\">\\?123</string>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | ASSERT_TRUE(testParse(input)); |
| 91 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | String* str = test::getValue<String>(&mTable, u"@string/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | ASSERT_NE(nullptr, str); |
| 94 | EXPECT_EQ(std::u16string(u"?123"), *str->value); |
| 95 | } |
| 96 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 97 | TEST_F(ResourceParserTest, ParseFormattedString) { |
| 98 | std::string input = "<string name=\"foo\">%d %s</string>"; |
| 99 | ASSERT_FALSE(testParse(input)); |
| 100 | |
| 101 | input = "<string name=\"foo\">%1$d %2$s</string>"; |
| 102 | ASSERT_TRUE(testParse(input)); |
| 103 | } |
| 104 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | TEST_F(ResourceParserTest, IgnoreXliffTags) { |
| 106 | std::string input = "<string name=\"foo\" \n" |
| 107 | " xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n" |
| 108 | " There are <xliff:g id=\"count\">%1$d</xliff:g> apples</string>"; |
| 109 | ASSERT_TRUE(testParse(input)); |
| 110 | |
| 111 | String* str = test::getValue<String>(&mTable, u"@string/foo"); |
| 112 | ASSERT_NE(nullptr, str); |
| 113 | EXPECT_EQ(StringPiece16(u"There are %1$d apples"), StringPiece16(*str->value)); |
| 114 | } |
| 115 | |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 116 | TEST_F(ResourceParserTest, ParseNull) { |
| 117 | std::string input = "<integer name=\"foo\">@null</integer>"; |
| 118 | ASSERT_TRUE(testParse(input)); |
| 119 | |
| 120 | // The Android runtime treats a value of android::Res_value::TYPE_NULL as |
| 121 | // a non-existing value, and this causes problems in styles when trying to resolve |
| 122 | // an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE |
| 123 | // with a data value of 0. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo"); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 125 | ASSERT_NE(nullptr, integer); |
| 126 | EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType); |
| 127 | EXPECT_EQ(0u, integer->value.data); |
| 128 | } |
| 129 | |
| 130 | TEST_F(ResourceParserTest, ParseEmpty) { |
| 131 | std::string input = "<integer name=\"foo\">@empty</integer>"; |
| 132 | ASSERT_TRUE(testParse(input)); |
| 133 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | BinaryPrimitive* integer = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo"); |
Adam Lesinski | dfa5e07 | 2015-05-12 21:42:59 -0700 | [diff] [blame] | 135 | ASSERT_NE(nullptr, integer); |
| 136 | EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType); |
| 137 | EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data); |
| 138 | } |
| 139 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | TEST_F(ResourceParserTest, ParseAttr) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 141 | std::string input = "<attr name=\"foo\" format=\"string\"/>\n" |
| 142 | "<attr name=\"bar\"/>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 143 | ASSERT_TRUE(testParse(input)); |
| 144 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 145 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
| 146 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask); |
| 148 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | attr = test::getValue<Attribute>(&mTable, u"@attr/bar"); |
| 150 | ASSERT_NE(nullptr, attr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 151 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->typeMask); |
| 152 | } |
| 153 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 154 | // Old AAPT allowed attributes to be defined under different configurations, but ultimately |
| 155 | // stored them with the default configuration. Check that we have the same behavior. |
| 156 | TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) { |
| 157 | const ConfigDescription watchConfig = test::parseConfigOrDie("watch"); |
| 158 | std::string input = R"EOF( |
| 159 | <attr name="foo" /> |
| 160 | <declare-styleable name="bar"> |
| 161 | <attr name="baz" /> |
| 162 | </declare-styleable>)EOF"; |
| 163 | ASSERT_TRUE(testParse(input, watchConfig)); |
| 164 | |
| 165 | EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, u"@attr/foo", watchConfig)); |
| 166 | EXPECT_EQ(nullptr, test::getValueForConfig<Attribute>(&mTable, u"@attr/baz", watchConfig)); |
| 167 | EXPECT_EQ(nullptr, test::getValueForConfig<Styleable>(&mTable, u"@styleable/bar", watchConfig)); |
| 168 | |
| 169 | EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, u"@attr/foo")); |
| 170 | EXPECT_NE(nullptr, test::getValue<Attribute>(&mTable, u"@attr/baz")); |
| 171 | EXPECT_NE(nullptr, test::getValue<Styleable>(&mTable, u"@styleable/bar")); |
| 172 | } |
| 173 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 174 | TEST_F(ResourceParserTest, ParseAttrWithMinMax) { |
| 175 | std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"integer\"/>"; |
| 176 | ASSERT_TRUE(testParse(input)); |
| 177 | |
| 178 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
| 179 | ASSERT_NE(nullptr, attr); |
| 180 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->typeMask); |
| 181 | EXPECT_EQ(10, attr->minInt); |
| 182 | EXPECT_EQ(23, attr->maxInt); |
| 183 | } |
| 184 | |
| 185 | TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) { |
| 186 | std::string input = "<attr name=\"foo\" min=\"10\" max=\"23\" format=\"string\"/>"; |
| 187 | ASSERT_FALSE(testParse(input)); |
| 188 | } |
| 189 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 190 | TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 191 | std::string input = "<declare-styleable name=\"Styleable\">\n" |
| 192 | " <attr name=\"foo\" />\n" |
| 193 | "</declare-styleable>\n" |
| 194 | "<attr name=\"foo\" format=\"string\"/>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 195 | ASSERT_TRUE(testParse(input)); |
| 196 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 197 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 198 | ASSERT_NE(nullptr, attr); |
| 199 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->typeMask); |
| 200 | } |
| 201 | |
| 202 | TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 203 | std::string input = "<declare-styleable name=\"Theme\">" |
| 204 | " <attr name=\"foo\" />\n" |
| 205 | "</declare-styleable>\n" |
| 206 | "<declare-styleable name=\"Window\">\n" |
| 207 | " <attr name=\"foo\" format=\"boolean\"/>\n" |
| 208 | "</declare-styleable>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 209 | ASSERT_TRUE(testParse(input)); |
| 210 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 212 | ASSERT_NE(nullptr, attr); |
| 213 | EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->typeMask); |
| 214 | } |
| 215 | |
| 216 | TEST_F(ResourceParserTest, ParseEnumAttr) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 217 | std::string input = "<attr name=\"foo\">\n" |
| 218 | " <enum name=\"bar\" value=\"0\"/>\n" |
| 219 | " <enum name=\"bat\" value=\"1\"/>\n" |
| 220 | " <enum name=\"baz\" value=\"2\"/>\n" |
| 221 | "</attr>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 222 | ASSERT_TRUE(testParse(input)); |
| 223 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 224 | Attribute* enumAttr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 225 | ASSERT_NE(enumAttr, nullptr); |
| 226 | EXPECT_EQ(enumAttr->typeMask, android::ResTable_map::TYPE_ENUM); |
| 227 | ASSERT_EQ(enumAttr->symbols.size(), 3u); |
| 228 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 229 | AAPT_ASSERT_TRUE(enumAttr->symbols[0].symbol.name); |
| 230 | EXPECT_EQ(enumAttr->symbols[0].symbol.name.value().entry, u"bar"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 231 | EXPECT_EQ(enumAttr->symbols[0].value, 0u); |
| 232 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 233 | AAPT_ASSERT_TRUE(enumAttr->symbols[1].symbol.name); |
| 234 | EXPECT_EQ(enumAttr->symbols[1].symbol.name.value().entry, u"bat"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 235 | EXPECT_EQ(enumAttr->symbols[1].value, 1u); |
| 236 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | AAPT_ASSERT_TRUE(enumAttr->symbols[2].symbol.name); |
| 238 | EXPECT_EQ(enumAttr->symbols[2].symbol.name.value().entry, u"baz"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 239 | EXPECT_EQ(enumAttr->symbols[2].value, 2u); |
| 240 | } |
| 241 | |
| 242 | TEST_F(ResourceParserTest, ParseFlagAttr) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 243 | std::string input = "<attr name=\"foo\">\n" |
| 244 | " <flag name=\"bar\" value=\"0\"/>\n" |
| 245 | " <flag name=\"bat\" value=\"1\"/>\n" |
| 246 | " <flag name=\"baz\" value=\"2\"/>\n" |
| 247 | "</attr>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 248 | ASSERT_TRUE(testParse(input)); |
| 249 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 250 | Attribute* flagAttr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 251 | ASSERT_NE(nullptr, flagAttr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 252 | EXPECT_EQ(flagAttr->typeMask, android::ResTable_map::TYPE_FLAGS); |
| 253 | ASSERT_EQ(flagAttr->symbols.size(), 3u); |
| 254 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 255 | AAPT_ASSERT_TRUE(flagAttr->symbols[0].symbol.name); |
| 256 | EXPECT_EQ(flagAttr->symbols[0].symbol.name.value().entry, u"bar"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 257 | EXPECT_EQ(flagAttr->symbols[0].value, 0u); |
| 258 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 259 | AAPT_ASSERT_TRUE(flagAttr->symbols[1].symbol.name); |
| 260 | EXPECT_EQ(flagAttr->symbols[1].symbol.name.value().entry, u"bat"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 261 | EXPECT_EQ(flagAttr->symbols[1].value, 1u); |
| 262 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 263 | AAPT_ASSERT_TRUE(flagAttr->symbols[2].symbol.name); |
| 264 | EXPECT_EQ(flagAttr->symbols[2].symbol.name.value().entry, u"baz"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 265 | EXPECT_EQ(flagAttr->symbols[2].value, 2u); |
| 266 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 267 | std::unique_ptr<BinaryPrimitive> flagValue = ResourceUtils::tryParseFlagSymbol(flagAttr, |
| 268 | u"baz|bat"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 269 | ASSERT_NE(nullptr, flagValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 270 | EXPECT_EQ(flagValue->value.data, 1u | 2u); |
| 271 | } |
| 272 | |
| 273 | TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 274 | std::string input = "<attr name=\"foo\">\n" |
| 275 | " <enum name=\"bar\" value=\"0\"/>\n" |
| 276 | " <enum name=\"bat\" value=\"1\"/>\n" |
| 277 | " <enum name=\"bat\" value=\"2\"/>\n" |
| 278 | "</attr>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 279 | ASSERT_FALSE(testParse(input)); |
| 280 | } |
| 281 | |
| 282 | TEST_F(ResourceParserTest, ParseStyle) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 283 | std::string input = "<style name=\"foo\" parent=\"@style/fu\">\n" |
| 284 | " <item name=\"bar\">#ffffffff</item>\n" |
| 285 | " <item name=\"bat\">@string/hey</item>\n" |
| 286 | " <item name=\"baz\"><b>hey</b></item>\n" |
| 287 | "</style>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 288 | ASSERT_TRUE(testParse(input)); |
| 289 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 290 | Style* style = test::getValue<Style>(&mTable, u"@style/foo"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 291 | ASSERT_NE(nullptr, style); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 292 | AAPT_ASSERT_TRUE(style->parent); |
| 293 | AAPT_ASSERT_TRUE(style->parent.value().name); |
| 294 | EXPECT_EQ(test::parseNameOrDie(u"@style/fu"), style->parent.value().name.value()); |
| 295 | ASSERT_EQ(3u, style->entries.size()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 296 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | AAPT_ASSERT_TRUE(style->entries[0].key.name); |
| 298 | EXPECT_EQ(test::parseNameOrDie(u"@attr/bar"), style->entries[0].key.name.value()); |
| 299 | |
| 300 | AAPT_ASSERT_TRUE(style->entries[1].key.name); |
| 301 | EXPECT_EQ(test::parseNameOrDie(u"@attr/bat"), style->entries[1].key.name.value()); |
| 302 | |
| 303 | AAPT_ASSERT_TRUE(style->entries[2].key.name); |
| 304 | EXPECT_EQ(test::parseNameOrDie(u"@attr/baz"), style->entries[2].key.name.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 307 | TEST_F(ResourceParserTest, ParseStyleWithShorthandParent) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 308 | std::string input = "<style name=\"foo\" parent=\"com.app:Theme\"/>"; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 309 | ASSERT_TRUE(testParse(input)); |
| 310 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 311 | Style* style = test::getValue<Style>(&mTable, u"@style/foo"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 312 | ASSERT_NE(nullptr, style); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 313 | AAPT_ASSERT_TRUE(style->parent); |
| 314 | AAPT_ASSERT_TRUE(style->parent.value().name); |
| 315 | EXPECT_EQ(test::parseNameOrDie(u"@com.app:style/Theme"), style->parent.value().name.value()); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 318 | TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedParent) { |
| 319 | std::string input = "<style xmlns:app=\"http://schemas.android.com/apk/res/android\"\n" |
| 320 | " name=\"foo\" parent=\"app:Theme\"/>"; |
| 321 | ASSERT_TRUE(testParse(input)); |
| 322 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 323 | Style* style = test::getValue<Style>(&mTable, u"@style/foo"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 324 | ASSERT_NE(nullptr, style); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 325 | AAPT_ASSERT_TRUE(style->parent); |
| 326 | AAPT_ASSERT_TRUE(style->parent.value().name); |
| 327 | EXPECT_EQ(test::parseNameOrDie(u"@android:style/Theme"), style->parent.value().name.value()); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | TEST_F(ResourceParserTest, ParseStyleWithPackageAliasedItems) { |
| 331 | std::string input = |
| 332 | "<style xmlns:app=\"http://schemas.android.com/apk/res/android\" name=\"foo\">\n" |
| 333 | " <item name=\"app:bar\">0</item>\n" |
| 334 | "</style>"; |
| 335 | ASSERT_TRUE(testParse(input)); |
| 336 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 337 | Style* style = test::getValue<Style>(&mTable, u"@style/foo"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 338 | ASSERT_NE(nullptr, style); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 339 | ASSERT_EQ(1u, style->entries.size()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 340 | EXPECT_EQ(test::parseNameOrDie(u"@android:attr/bar"), style->entries[0].key.name.value()); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 343 | TEST_F(ResourceParserTest, ParseStyleWithInferredParent) { |
| 344 | std::string input = "<style name=\"foo.bar\"/>"; |
| 345 | ASSERT_TRUE(testParse(input)); |
| 346 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 347 | Style* style = test::getValue<Style>(&mTable, u"@style/foo.bar"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 348 | ASSERT_NE(nullptr, style); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 349 | AAPT_ASSERT_TRUE(style->parent); |
| 350 | AAPT_ASSERT_TRUE(style->parent.value().name); |
| 351 | EXPECT_EQ(style->parent.value().name.value(), test::parseNameOrDie(u"@style/foo")); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 352 | EXPECT_TRUE(style->parentInferred); |
| 353 | } |
| 354 | |
| 355 | TEST_F(ResourceParserTest, ParseStyleWithInferredParentOverridenByEmptyParentAttribute) { |
| 356 | std::string input = "<style name=\"foo.bar\" parent=\"\"/>"; |
| 357 | ASSERT_TRUE(testParse(input)); |
| 358 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 359 | Style* style = test::getValue<Style>(&mTable, u"@style/foo.bar"); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 360 | ASSERT_NE(nullptr, style); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 361 | AAPT_EXPECT_FALSE(style->parent); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 362 | EXPECT_FALSE(style->parentInferred); |
| 363 | } |
| 364 | |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 365 | TEST_F(ResourceParserTest, ParseStyleWithPrivateParentReference) { |
| 366 | std::string input = R"EOF(<style name="foo" parent="*android:style/bar" />)EOF"; |
| 367 | ASSERT_TRUE(testParse(input)); |
| 368 | |
| 369 | Style* style = test::getValue<Style>(&mTable, u"@style/foo"); |
| 370 | ASSERT_NE(nullptr, style); |
| 371 | AAPT_ASSERT_TRUE(style->parent); |
| 372 | EXPECT_TRUE(style->parent.value().privateReference); |
| 373 | } |
| 374 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 375 | TEST_F(ResourceParserTest, ParseAutoGeneratedIdReference) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 376 | std::string input = "<string name=\"foo\">@+id/bar</string>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 377 | ASSERT_TRUE(testParse(input)); |
| 378 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 379 | Id* id = test::getValue<Id>(&mTable, u"@id/bar"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 380 | ASSERT_NE(id, nullptr); |
| 381 | } |
| 382 | |
| 383 | TEST_F(ResourceParserTest, ParseAttributesDeclareStyleable) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 384 | std::string input = "<declare-styleable name=\"foo\">\n" |
| 385 | " <attr name=\"bar\" />\n" |
| 386 | " <attr name=\"bat\" format=\"string|reference\"/>\n" |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 387 | " <attr name=\"baz\">\n" |
| 388 | " <enum name=\"foo\" value=\"1\"/>\n" |
| 389 | " </attr>\n" |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 390 | "</declare-styleable>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 391 | ASSERT_TRUE(testParse(input)); |
| 392 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 393 | Maybe<ResourceTable::SearchResult> result = |
| 394 | mTable.findResource(test::parseNameOrDie(u"@styleable/foo")); |
| 395 | AAPT_ASSERT_TRUE(result); |
| 396 | EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state); |
| 397 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 398 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/bar"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 399 | ASSERT_NE(attr, nullptr); |
| 400 | EXPECT_TRUE(attr->isWeak()); |
| 401 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 402 | attr = test::getValue<Attribute>(&mTable, u"@attr/bat"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 403 | ASSERT_NE(attr, nullptr); |
| 404 | EXPECT_TRUE(attr->isWeak()); |
| 405 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 406 | attr = test::getValue<Attribute>(&mTable, u"@attr/baz"); |
| 407 | ASSERT_NE(attr, nullptr); |
| 408 | EXPECT_TRUE(attr->isWeak()); |
| 409 | EXPECT_EQ(1u, attr->symbols.size()); |
| 410 | |
| 411 | EXPECT_NE(nullptr, test::getValue<Id>(&mTable, u"@id/foo")); |
| 412 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 413 | Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 414 | ASSERT_NE(styleable, nullptr); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 415 | ASSERT_EQ(3u, styleable->entries.size()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 416 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 417 | EXPECT_EQ(test::parseNameOrDie(u"@attr/bar"), styleable->entries[0].name.value()); |
| 418 | EXPECT_EQ(test::parseNameOrDie(u"@attr/bat"), styleable->entries[1].name.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 421 | TEST_F(ResourceParserTest, ParsePrivateAttributesDeclareStyleable) { |
| 422 | std::string input = "<declare-styleable name=\"foo\" xmlns:privAndroid=\"http://schemas.android.com/apk/prv/res/android\">\n" |
| 423 | " <attr name=\"*android:bar\" />\n" |
| 424 | " <attr name=\"privAndroid:bat\" />\n" |
| 425 | "</declare-styleable>"; |
| 426 | ASSERT_TRUE(testParse(input)); |
| 427 | Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo"); |
| 428 | ASSERT_NE(nullptr, styleable); |
| 429 | ASSERT_EQ(2u, styleable->entries.size()); |
| 430 | |
| 431 | EXPECT_TRUE(styleable->entries[0].privateReference); |
| 432 | AAPT_ASSERT_TRUE(styleable->entries[0].name); |
| 433 | EXPECT_EQ(std::u16string(u"android"), styleable->entries[0].name.value().package); |
| 434 | |
| 435 | EXPECT_TRUE(styleable->entries[1].privateReference); |
| 436 | AAPT_ASSERT_TRUE(styleable->entries[1].name); |
| 437 | EXPECT_EQ(std::u16string(u"android"), styleable->entries[1].name.value().package); |
| 438 | } |
| 439 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 440 | TEST_F(ResourceParserTest, ParseArray) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 441 | std::string input = "<array name=\"foo\">\n" |
| 442 | " <item>@string/ref</item>\n" |
| 443 | " <item>hey</item>\n" |
| 444 | " <item>23</item>\n" |
| 445 | "</array>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 446 | ASSERT_TRUE(testParse(input)); |
| 447 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 448 | Array* array = test::getValue<Array>(&mTable, u"@array/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 449 | ASSERT_NE(array, nullptr); |
| 450 | ASSERT_EQ(3u, array->items.size()); |
| 451 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 452 | EXPECT_NE(nullptr, valueCast<Reference>(array->items[0].get())); |
| 453 | EXPECT_NE(nullptr, valueCast<String>(array->items[1].get())); |
| 454 | EXPECT_NE(nullptr, valueCast<BinaryPrimitive>(array->items[2].get())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 457 | TEST_F(ResourceParserTest, ParseStringArray) { |
| 458 | std::string input = "<string-array name=\"foo\">\n" |
| 459 | " <item>\"Werk\"</item>\n" |
| 460 | "</string-array>\n"; |
| 461 | ASSERT_TRUE(testParse(input)); |
| 462 | EXPECT_NE(nullptr, test::getValue<Array>(&mTable, u"@array/foo")); |
| 463 | } |
| 464 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 465 | TEST_F(ResourceParserTest, ParsePlural) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 466 | std::string input = "<plurals name=\"foo\">\n" |
| 467 | " <item quantity=\"other\">apples</item>\n" |
| 468 | " <item quantity=\"one\">apple</item>\n" |
| 469 | "</plurals>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 470 | ASSERT_TRUE(testParse(input)); |
| 471 | } |
| 472 | |
| 473 | TEST_F(ResourceParserTest, ParseCommentsWithResource) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 474 | std::string input = "<!--This is a comment-->\n" |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 475 | "<string name=\"foo\">Hi</string>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 476 | ASSERT_TRUE(testParse(input)); |
| 477 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 478 | String* value = test::getValue<String>(&mTable, u"@string/foo"); |
| 479 | ASSERT_NE(nullptr, value); |
| 480 | EXPECT_EQ(value->getComment(), u"This is a comment"); |
| 481 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 482 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 483 | TEST_F(ResourceParserTest, DoNotCombineMultipleComments) { |
| 484 | std::string input = "<!--One-->\n" |
| 485 | "<!--Two-->\n" |
| 486 | "<string name=\"foo\">Hi</string>"; |
| 487 | |
| 488 | ASSERT_TRUE(testParse(input)); |
| 489 | |
| 490 | String* value = test::getValue<String>(&mTable, u"@string/foo"); |
| 491 | ASSERT_NE(nullptr, value); |
| 492 | EXPECT_EQ(value->getComment(), u"Two"); |
| 493 | } |
| 494 | |
| 495 | TEST_F(ResourceParserTest, IgnoreCommentBeforeEndTag) { |
| 496 | std::string input = "<!--One-->\n" |
| 497 | "<string name=\"foo\">\n" |
| 498 | " Hi\n" |
| 499 | "<!--Two-->\n" |
| 500 | "</string>"; |
| 501 | |
| 502 | ASSERT_TRUE(testParse(input)); |
| 503 | |
| 504 | String* value = test::getValue<String>(&mTable, u"@string/foo"); |
| 505 | ASSERT_NE(nullptr, value); |
| 506 | EXPECT_EQ(value->getComment(), u"One"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 509 | TEST_F(ResourceParserTest, ParseNestedComments) { |
| 510 | // We only care about declare-styleable and enum/flag attributes because comments |
| 511 | // from those end up in R.java |
| 512 | std::string input = R"EOF( |
| 513 | <declare-styleable name="foo"> |
| 514 | <!-- The name of the bar --> |
| 515 | <attr name="barName" format="string|reference" /> |
| 516 | </declare-styleable> |
| 517 | |
| 518 | <attr name="foo"> |
| 519 | <!-- The very first --> |
| 520 | <enum name="one" value="1" /> |
| 521 | </attr>)EOF"; |
| 522 | ASSERT_TRUE(testParse(input)); |
| 523 | |
| 524 | Styleable* styleable = test::getValue<Styleable>(&mTable, u"@styleable/foo"); |
| 525 | ASSERT_NE(nullptr, styleable); |
| 526 | ASSERT_EQ(1u, styleable->entries.size()); |
| 527 | |
| 528 | EXPECT_EQ(StringPiece16(u"The name of the bar"), styleable->entries.front().getComment()); |
| 529 | |
| 530 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/foo"); |
| 531 | ASSERT_NE(nullptr, attr); |
| 532 | ASSERT_EQ(1u, attr->symbols.size()); |
| 533 | |
| 534 | EXPECT_EQ(StringPiece16(u"The very first"), attr->symbols.front().symbol.getComment()); |
| 535 | } |
| 536 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 537 | /* |
| 538 | * Declaring an ID as public should not require a separate definition |
| 539 | * (as an ID has no value). |
| 540 | */ |
| 541 | TEST_F(ResourceParserTest, ParsePublicIdAsDefinition) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 542 | std::string input = "<public type=\"id\" name=\"foo\"/>"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 543 | ASSERT_TRUE(testParse(input)); |
| 544 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 545 | Id* id = test::getValue<Id>(&mTable, u"@id/foo"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 546 | ASSERT_NE(nullptr, id); |
| 547 | } |
| 548 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 549 | TEST_F(ResourceParserTest, FilterProductsThatDontMatch) { |
Adam Lesinski | 7751afc | 2016-01-06 15:45:28 -0800 | [diff] [blame] | 550 | std::string input = R"EOF( |
| 551 | <string name="foo" product="phone">hi</string> |
| 552 | <string name="foo" product="no-sdcard">ho</string> |
| 553 | <string name="bar" product="">wee</string> |
| 554 | <string name="baz">woo</string> |
| 555 | <string name="bit" product="phablet">hoot</string> |
| 556 | <string name="bot" product="default">yes</string> |
| 557 | )EOF"; |
| 558 | ASSERT_TRUE(testParse(input, { std::u16string(u"no-sdcard"), std::u16string(u"phablet") })); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 559 | |
| 560 | String* fooStr = test::getValue<String>(&mTable, u"@string/foo"); |
| 561 | ASSERT_NE(nullptr, fooStr); |
| 562 | EXPECT_EQ(StringPiece16(u"ho"), *fooStr->value); |
| 563 | |
| 564 | EXPECT_NE(nullptr, test::getValue<String>(&mTable, u"@string/bar")); |
| 565 | EXPECT_NE(nullptr, test::getValue<String>(&mTable, u"@string/baz")); |
Adam Lesinski | 7751afc | 2016-01-06 15:45:28 -0800 | [diff] [blame] | 566 | EXPECT_NE(nullptr, test::getValue<String>(&mTable, u"@string/bit")); |
| 567 | EXPECT_NE(nullptr, test::getValue<String>(&mTable, u"@string/bot")); |
| 568 | } |
| 569 | |
| 570 | TEST_F(ResourceParserTest, FilterProductsThatBothMatchInOrder) { |
| 571 | std::string input = R"EOF( |
| 572 | <string name="foo" product="phone">phone</string> |
| 573 | <string name="foo" product="default">default</string> |
| 574 | )EOF"; |
| 575 | ASSERT_TRUE(testParse(input, { std::u16string(u"phone") })); |
| 576 | |
| 577 | String* foo = test::getValue<String>(&mTable, u"@string/foo"); |
| 578 | ASSERT_NE(nullptr, foo); |
| 579 | EXPECT_EQ(std::u16string(u"phone"), *foo->value); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | TEST_F(ResourceParserTest, FailWhenProductFilterStripsOutAllVersionsOfResource) { |
| 583 | std::string input = "<string name=\"foo\" product=\"tablet\">hello</string>\n"; |
Adam Lesinski | 7751afc | 2016-01-06 15:45:28 -0800 | [diff] [blame] | 584 | ASSERT_FALSE(testParse(input, { std::u16string(u"phone") })); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 587 | TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) { |
| 588 | std::string input = R"EOF( |
| 589 | <public-group type="attr" first-id="0x01010040"> |
| 590 | <public name="foo" /> |
| 591 | <public name="bar" /> |
| 592 | </public-group>)EOF"; |
| 593 | ASSERT_TRUE(testParse(input)); |
| 594 | |
| 595 | Maybe<ResourceTable::SearchResult> result = mTable.findResource( |
| 596 | test::parseNameOrDie(u"@attr/foo")); |
| 597 | AAPT_ASSERT_TRUE(result); |
| 598 | |
| 599 | AAPT_ASSERT_TRUE(result.value().package->id); |
| 600 | AAPT_ASSERT_TRUE(result.value().type->id); |
| 601 | AAPT_ASSERT_TRUE(result.value().entry->id); |
| 602 | ResourceId actualId(result.value().package->id.value(), |
| 603 | result.value().type->id.value(), |
| 604 | result.value().entry->id.value()); |
| 605 | EXPECT_EQ(ResourceId(0x01010040), actualId); |
| 606 | |
| 607 | result = mTable.findResource(test::parseNameOrDie(u"@attr/bar")); |
| 608 | AAPT_ASSERT_TRUE(result); |
| 609 | |
| 610 | AAPT_ASSERT_TRUE(result.value().package->id); |
| 611 | AAPT_ASSERT_TRUE(result.value().type->id); |
| 612 | AAPT_ASSERT_TRUE(result.value().entry->id); |
| 613 | actualId = ResourceId(result.value().package->id.value(), |
| 614 | result.value().type->id.value(), |
| 615 | result.value().entry->id.value()); |
| 616 | EXPECT_EQ(ResourceId(0x01010041), actualId); |
| 617 | } |
| 618 | |
Adam Lesinski | fa10505 | 2015-11-07 13:34:39 -0800 | [diff] [blame] | 619 | TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) { |
| 620 | std::string input = R"EOF(<item type="layout" name="foo">@layout/bar</item>)EOF"; |
| 621 | ASSERT_TRUE(testParse(input)); |
| 622 | |
| 623 | input = R"EOF(<item type="layout" name="bar">"this is a string"</item>)EOF"; |
| 624 | ASSERT_FALSE(testParse(input)); |
| 625 | } |
| 626 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 627 | TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) { |
| 628 | std::string input = R"EOF(<add-resource name="bar" type="string" />)EOF"; |
| 629 | ASSERT_TRUE(testParse(input)); |
| 630 | |
| 631 | Maybe<ResourceTable::SearchResult> result = mTable.findResource( |
| 632 | test::parseNameOrDie(u"@string/bar")); |
| 633 | AAPT_ASSERT_TRUE(result); |
| 634 | const ResourceEntry* entry = result.value().entry; |
| 635 | ASSERT_NE(nullptr, entry); |
| 636 | EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state); |
| 637 | } |
| 638 | |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 639 | TEST_F(ResourceParserTest, ParseItemElementWithFormat) { |
| 640 | std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF"; |
| 641 | ASSERT_TRUE(testParse(input)); |
| 642 | |
| 643 | BinaryPrimitive* val = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo"); |
| 644 | ASSERT_NE(nullptr, val); |
| 645 | |
| 646 | EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType); |
| 647 | } |
| 648 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 649 | } // namespace aapt |