Loading tools/aapt2/ResourceParser.cpp +6 −2 Original line number Diff line number Diff line Loading @@ -415,6 +415,10 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, if (resource_type == "item") { can_be_bag = false; // The default format for <item> is any. If a format attribute is present, that one will // override the default. resource_format = android::ResTable_map::TYPE_ANY; // Items have their type encoded in the type attribute. if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); Loading Loading @@ -481,8 +485,8 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, out_resource->name.type = item_iter->second.type; out_resource->name.entry = maybe_name.value().to_string(); // Only use the implicit format for this type if it wasn't overridden. if (!resource_format) { // Only use the implied format of the type when there is no explicit format. if (resource_format == 0u) { resource_format = item_iter->second.format; } Loading tools/aapt2/ResourceParser_test.cpp +19 −7 Original line number Diff line number Diff line Loading @@ -25,7 +25,9 @@ #include "test/Test.h" #include "xml/XmlPullParser.h" using android::StringPiece; using ::android::StringPiece; using ::testing::Eq; using ::testing::NotNull; namespace aapt { Loading Loading @@ -791,15 +793,25 @@ TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) } TEST_F(ResourceParserTest, ParseItemElementWithFormat) { std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF"; std::string input = R"(<item name="foo" type="integer" format="float">0.3</item>)"; ASSERT_TRUE(TestParse(input)); BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); ASSERT_NE(nullptr, val); BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); ASSERT_THAT(val, NotNull()); EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FLOAT)); input = R"(<item name="bar" type="integer" format="fraction">100</item>)"; ASSERT_FALSE(TestParse(input)); } // An <item> without a format specifier accepts all types of values. TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) { std::string input = R"(<item name="foo" type="integer">100%p</item>)"; ASSERT_TRUE(TestParse(input)); EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType); BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); ASSERT_THAT(val, NotNull()); EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FRACTION)); } TEST_F(ResourceParserTest, ParseConfigVaryingItem) { Loading tools/aapt2/integration-tests/AppOne/res/values/test.xml +2 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ <flag name="weak" value="4" /> </attr> <item name="value_that_allows_any_format" type="integer">-100%p</item> <!-- Override the Widget styleable declared in StaticLibOne. This should merge the two when built in overlay mode. --> <declare-styleable name="Widget"> Loading Loading
tools/aapt2/ResourceParser.cpp +6 −2 Original line number Diff line number Diff line Loading @@ -415,6 +415,10 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, if (resource_type == "item") { can_be_bag = false; // The default format for <item> is any. If a format attribute is present, that one will // override the default. resource_format = android::ResTable_map::TYPE_ANY; // Items have their type encoded in the type attribute. if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); Loading Loading @@ -481,8 +485,8 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, out_resource->name.type = item_iter->second.type; out_resource->name.entry = maybe_name.value().to_string(); // Only use the implicit format for this type if it wasn't overridden. if (!resource_format) { // Only use the implied format of the type when there is no explicit format. if (resource_format == 0u) { resource_format = item_iter->second.format; } Loading
tools/aapt2/ResourceParser_test.cpp +19 −7 Original line number Diff line number Diff line Loading @@ -25,7 +25,9 @@ #include "test/Test.h" #include "xml/XmlPullParser.h" using android::StringPiece; using ::android::StringPiece; using ::testing::Eq; using ::testing::NotNull; namespace aapt { Loading Loading @@ -791,15 +793,25 @@ TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol) } TEST_F(ResourceParserTest, ParseItemElementWithFormat) { std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF"; std::string input = R"(<item name="foo" type="integer" format="float">0.3</item>)"; ASSERT_TRUE(TestParse(input)); BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); ASSERT_NE(nullptr, val); BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); ASSERT_THAT(val, NotNull()); EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FLOAT)); input = R"(<item name="bar" type="integer" format="fraction">100</item>)"; ASSERT_FALSE(TestParse(input)); } // An <item> without a format specifier accepts all types of values. TEST_F(ResourceParserTest, ParseItemElementWithoutFormat) { std::string input = R"(<item name="foo" type="integer">100%p</item>)"; ASSERT_TRUE(TestParse(input)); EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType); BinaryPrimitive* val = test::GetValue<BinaryPrimitive>(&table_, "integer/foo"); ASSERT_THAT(val, NotNull()); EXPECT_THAT(val->value.dataType, Eq(android::Res_value::TYPE_FRACTION)); } TEST_F(ResourceParserTest, ParseConfigVaryingItem) { Loading
tools/aapt2/integration-tests/AppOne/res/values/test.xml +2 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ <flag name="weak" value="4" /> </attr> <item name="value_that_allows_any_format" type="integer">-100%p</item> <!-- Override the Widget styleable declared in StaticLibOne. This should merge the two when built in overlay mode. --> <declare-styleable name="Widget"> Loading