diff options
-rw-r--r-- | tools/aapt2/ResourceParser.cpp | 8 | ||||
-rw-r--r-- | tools/aapt2/ResourceParser_test.cpp | 26 | ||||
-rw-r--r-- | tools/aapt2/integration-tests/AppOne/res/values/test.xml | 2 |
3 files changed, 27 insertions, 9 deletions
diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp index 57ae2700127c..32e0fd52acfa 100644 --- a/tools/aapt2/ResourceParser.cpp +++ b/tools/aapt2/ResourceParser.cpp @@ -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(); @@ -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; } diff --git a/tools/aapt2/ResourceParser_test.cpp b/tools/aapt2/ResourceParser_test.cpp index e3abde6bef29..e60ef66a21db 100644 --- a/tools/aapt2/ResourceParser_test.cpp +++ b/tools/aapt2/ResourceParser_test.cpp @@ -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 { @@ -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) { diff --git a/tools/aapt2/integration-tests/AppOne/res/values/test.xml b/tools/aapt2/integration-tests/AppOne/res/values/test.xml index 91f8bfd0dd14..5104f8212d8b 100644 --- a/tools/aapt2/integration-tests/AppOne/res/values/test.xml +++ b/tools/aapt2/integration-tests/AppOne/res/values/test.xml @@ -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"> |