Merge "AAPT2: Allow any value type for <item> without format attr" into oc-dev am: 1ac325e4d5
am: 3c860ed41d

Change-Id: I44719ae4d742b518f59c9c9bbc226ec9773ce365
diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp
index bc13378..084325a 100644
--- a/tools/aapt2/ResourceParser.cpp
+++ b/tools/aapt2/ResourceParser.cpp
@@ -415,6 +415,10 @@
   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 @@
       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 e3abde6..e60ef66 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, 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));
 
-  EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
+  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));
+
+  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 91f8bfd..5104f82 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">