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