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