Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1ac325e4 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: Allow any value type for <item> without format attr" into oc-dev

parents 9f7e653d e597d68d
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -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;
      }

+19 −7
Original line number Diff line number Diff line
@@ -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) {
+2 −0
Original line number Diff line number Diff line
@@ -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">