/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ResourceParser.h" #include #include #include "ResourceTable.h" #include "ResourceUtils.h" #include "ResourceValues.h" #include "test/Test.h" #include "xml/XmlPullParser.h" using android::StringPiece; namespace aapt { constexpr const char* kXmlPreamble = "\n"; TEST(ResourceParserSingleTest, FailToParseWithNoRootResourcesElement) { std::unique_ptr context = test::ContextBuilder().Build(); std::stringstream input(kXmlPreamble); input << "" << std::endl; ResourceTable table; ResourceParser parser(context->GetDiagnostics(), &table, Source{"test"}, {}); xml::XmlPullParser xml_parser(input); ASSERT_FALSE(parser.Parse(&xml_parser)); } class ResourceParserTest : public ::testing::Test { public: void SetUp() override { context_ = test::ContextBuilder().Build(); } ::testing::AssertionResult TestParse(const StringPiece& str) { return TestParse(str, ConfigDescription{}); } ::testing::AssertionResult TestParse(const StringPiece& str, const ConfigDescription& config) { std::stringstream input(kXmlPreamble); input << "\n" << str << "\n" << std::endl; ResourceParserOptions parserOptions; ResourceParser parser(context_->GetDiagnostics(), &table_, Source{"test"}, config, parserOptions); xml::XmlPullParser xmlParser(input); if (parser.Parse(&xmlParser)) { return ::testing::AssertionSuccess(); } return ::testing::AssertionFailure(); } protected: ResourceTable table_; std::unique_ptr context_; }; TEST_F(ResourceParserTest, ParseQuotedString) { std::string input = " \" hey there \" "; ASSERT_TRUE(TestParse(input)); String* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); EXPECT_EQ(std::string(" hey there "), *str->value); EXPECT_TRUE(str->untranslatable_sections.empty()); } TEST_F(ResourceParserTest, ParseEscapedString) { std::string input = "\\?123"; ASSERT_TRUE(TestParse(input)); String* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); EXPECT_EQ(std::string("?123"), *str->value); EXPECT_TRUE(str->untranslatable_sections.empty()); } TEST_F(ResourceParserTest, ParseFormattedString) { std::string input = "%d %s"; ASSERT_FALSE(TestParse(input)); input = "%1$d %2$s"; ASSERT_TRUE(TestParse(input)); } TEST_F(ResourceParserTest, ParseStyledString) { // Use a surrogate pair unicode point so that we can verify that the span // indices use UTF-16 length and not UTF-8 length. std::string input = "This is my aunt\u2019s string"; ASSERT_TRUE(TestParse(input)); StyledString* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); const std::string expected_str = "This is my aunt\u2019s string"; EXPECT_EQ(expected_str, *str->value->str); EXPECT_EQ(1u, str->value->spans.size()); EXPECT_TRUE(str->untranslatable_sections.empty()); EXPECT_EQ(std::string("b"), *str->value->spans[0].name); EXPECT_EQ(17u, str->value->spans[0].first_char); EXPECT_EQ(23u, str->value->spans[0].last_char); } TEST_F(ResourceParserTest, ParseStringWithWhitespace) { std::string input = " This is what I think "; ASSERT_TRUE(TestParse(input)); String* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); EXPECT_EQ(std::string("This is what I think"), *str->value); EXPECT_TRUE(str->untranslatable_sections.empty()); input = "\" This is what I think \""; ASSERT_TRUE(TestParse(input)); str = test::GetValue(&table_, "string/foo2"); ASSERT_NE(nullptr, str); EXPECT_EQ(std::string(" This is what I think "), *str->value); } TEST_F(ResourceParserTest, IgnoreXliffTagsOtherThanG) { std::string input = R"EOF( There are no apples)EOF"; ASSERT_TRUE(TestParse(input)); String* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); EXPECT_EQ(StringPiece("There are no apples"), StringPiece(*str->value)); EXPECT_TRUE(str->untranslatable_sections.empty()); } TEST_F(ResourceParserTest, NestedXliffGTagsAreIllegal) { std::string input = R"EOF( Do not translate this)EOF"; EXPECT_FALSE(TestParse(input)); } TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInString) { std::string input = R"EOF( There are %1$d apples)EOF"; ASSERT_TRUE(TestParse(input)); String* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value)); ASSERT_EQ(1u, str->untranslatable_sections.size()); // We expect indices and lengths that span to include the whitespace // before %1$d. This is due to how the StringBuilder withholds whitespace unless // needed (to deal with line breaks, etc.). EXPECT_EQ(9u, str->untranslatable_sections[0].start); EXPECT_EQ(14u, str->untranslatable_sections[0].end); } TEST_F(ResourceParserTest, RecordUntranslateableXliffSectionsInStyledString) { std::string input = R"EOF( There are %1$d apples)EOF"; ASSERT_TRUE(TestParse(input)); StyledString* str = test::GetValue(&table_, "string/foo"); ASSERT_NE(nullptr, str); EXPECT_EQ(StringPiece("There are %1$d apples"), StringPiece(*str->value->str)); ASSERT_EQ(1u, str->untranslatable_sections.size()); // We expect indices and lengths that span to include the whitespace // before %1$d. This is due to how the StringBuilder withholds whitespace unless // needed (to deal with line breaks, etc.). EXPECT_EQ(9u, str->untranslatable_sections[0].start); EXPECT_EQ(14u, str->untranslatable_sections[0].end); } TEST_F(ResourceParserTest, ParseNull) { std::string input = "@null"; ASSERT_TRUE(TestParse(input)); // The Android runtime treats a value of android::Res_value::TYPE_NULL as // a non-existing value, and this causes problems in styles when trying to // resolve an attribute. Null values must be encoded as android::Res_value::TYPE_REFERENCE // with a data value of 0. BinaryPrimitive* integer = test::GetValue(&table_, "integer/foo"); ASSERT_NE(nullptr, integer); EXPECT_EQ(uint16_t(android::Res_value::TYPE_REFERENCE), integer->value.dataType); EXPECT_EQ(0u, integer->value.data); } TEST_F(ResourceParserTest, ParseEmpty) { std::string input = "@empty"; ASSERT_TRUE(TestParse(input)); BinaryPrimitive* integer = test::GetValue(&table_, "integer/foo"); ASSERT_NE(nullptr, integer); EXPECT_EQ(uint16_t(android::Res_value::TYPE_NULL), integer->value.dataType); EXPECT_EQ(uint32_t(android::Res_value::DATA_NULL_EMPTY), integer->value.data); } TEST_F(ResourceParserTest, ParseAttr) { std::string input = "\n" ""; ASSERT_TRUE(TestParse(input)); Attribute* attr = test::GetValue(&table_, "attr/foo"); ASSERT_NE(nullptr, attr); EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask); attr = test::GetValue(&table_, "attr/bar"); ASSERT_NE(nullptr, attr); EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_ANY), attr->type_mask); } // Old AAPT allowed attributes to be defined under different configurations, but // ultimately // stored them with the default configuration. Check that we have the same // behavior. TEST_F(ResourceParserTest, ParseAttrAndDeclareStyleableUnderConfigButRecordAsNoConfig) { const ConfigDescription watch_config = test::ParseConfigOrDie("watch"); std::string input = R"EOF( )EOF"; ASSERT_TRUE(TestParse(input, watch_config)); EXPECT_EQ(nullptr, test::GetValueForConfig(&table_, "attr/foo", watch_config)); EXPECT_EQ(nullptr, test::GetValueForConfig(&table_, "attr/baz", watch_config)); EXPECT_EQ(nullptr, test::GetValueForConfig( &table_, "styleable/bar", watch_config)); EXPECT_NE(nullptr, test::GetValue(&table_, "attr/foo")); EXPECT_NE(nullptr, test::GetValue(&table_, "attr/baz")); EXPECT_NE(nullptr, test::GetValue(&table_, "styleable/bar")); } TEST_F(ResourceParserTest, ParseAttrWithMinMax) { std::string input = ""; ASSERT_TRUE(TestParse(input)); Attribute* attr = test::GetValue(&table_, "attr/foo"); ASSERT_NE(nullptr, attr); EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_INTEGER), attr->type_mask); EXPECT_EQ(10, attr->min_int); EXPECT_EQ(23, attr->max_int); } TEST_F(ResourceParserTest, FailParseAttrWithMinMaxButNotInteger) { std::string input = ""; ASSERT_FALSE(TestParse(input)); } TEST_F(ResourceParserTest, ParseUseAndDeclOfAttr) { std::string input = "\n" " \n" "\n" ""; ASSERT_TRUE(TestParse(input)); Attribute* attr = test::GetValue(&table_, "attr/foo"); ASSERT_NE(nullptr, attr); EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_STRING), attr->type_mask); } TEST_F(ResourceParserTest, ParseDoubleUseOfAttr) { std::string input = "" " \n" "\n" "\n" " \n" ""; ASSERT_TRUE(TestParse(input)); Attribute* attr = test::GetValue(&table_, "attr/foo"); ASSERT_NE(nullptr, attr); EXPECT_EQ(uint32_t(android::ResTable_map::TYPE_BOOLEAN), attr->type_mask); } TEST_F(ResourceParserTest, ParseEnumAttr) { std::string input = "\n" " \n" " \n" " \n" ""; ASSERT_TRUE(TestParse(input)); Attribute* enum_attr = test::GetValue(&table_, "attr/foo"); ASSERT_NE(enum_attr, nullptr); EXPECT_EQ(enum_attr->type_mask, android::ResTable_map::TYPE_ENUM); ASSERT_EQ(enum_attr->symbols.size(), 3u); AAPT_ASSERT_TRUE(enum_attr->symbols[0].symbol.name); EXPECT_EQ(enum_attr->symbols[0].symbol.name.value().entry, "bar"); EXPECT_EQ(enum_attr->symbols[0].value, 0u); AAPT_ASSERT_TRUE(enum_attr->symbols[1].symbol.name); EXPECT_EQ(enum_attr->symbols[1].symbol.name.value().entry, "bat"); EXPECT_EQ(enum_attr->symbols[1].value, 1u); AAPT_ASSERT_TRUE(enum_attr->symbols[2].symbol.name); EXPECT_EQ(enum_attr->symbols[2].symbol.name.value().entry, "baz"); EXPECT_EQ(enum_attr->symbols[2].value, 2u); } TEST_F(ResourceParserTest, ParseFlagAttr) { std::string input = "\n" " \n" " \n" " \n" ""; ASSERT_TRUE(TestParse(input)); Attribute* flag_attr = test::GetValue(&table_, "attr/foo"); ASSERT_NE(nullptr, flag_attr); EXPECT_EQ(flag_attr->type_mask, android::ResTable_map::TYPE_FLAGS); ASSERT_EQ(flag_attr->symbols.size(), 3u); AAPT_ASSERT_TRUE(flag_attr->symbols[0].symbol.name); EXPECT_EQ(flag_attr->symbols[0].symbol.name.value().entry, "bar"); EXPECT_EQ(flag_attr->symbols[0].value, 0u); AAPT_ASSERT_TRUE(flag_attr->symbols[1].symbol.name); EXPECT_EQ(flag_attr->symbols[1].symbol.name.value().entry, "bat"); EXPECT_EQ(flag_attr->symbols[1].value, 1u); AAPT_ASSERT_TRUE(flag_attr->symbols[2].symbol.name); EXPECT_EQ(flag_attr->symbols[2].symbol.name.value().entry, "baz"); EXPECT_EQ(flag_attr->symbols[2].value, 2u); std::unique_ptr flag_value = ResourceUtils::TryParseFlagSymbol(flag_attr, "baz|bat"); ASSERT_NE(nullptr, flag_value); EXPECT_EQ(flag_value->value.data, 1u | 2u); } TEST_F(ResourceParserTest, FailToParseEnumAttrWithNonUniqueKeys) { std::string input = "\n" " \n" " \n" " \n" ""; ASSERT_FALSE(TestParse(input)); } TEST_F(ResourceParserTest, ParseStyle) { std::string input = ""; ASSERT_TRUE(TestParse(input)); Style* style = test::GetValue"; ASSERT_TRUE(TestParse(input)); Style* style = test::GetValue