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

Commit adbb2df8 authored by Ryan Mitchell's avatar Ryan Mitchell Committed by Android (Google) Code Review
Browse files

Merge "Add android:angle on <gradient> for min sdk <= Q"

parents d4f9d26e 4d5833ec
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -83,6 +83,15 @@ class XmlVisitor : public xml::PackageAwareVisitor {
    Attribute default_attribute(android::ResTable_map::TYPE_ANY);
    default_attribute.SetWeak(true);

    // The default orientation of gradients in android Q is different than previous android
    // versions. Set the android:angle attribute to "0" to ensure that the default gradient
    // orientation will remain left-to-right in android Q.
    if (el->name == "gradient" && context_->GetMinSdkVersion() <= SDK_Q) {
      if (!el->FindAttribute(xml::kSchemaAndroid, "angle")) {
        el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "angle", "0"});
      }
    }

    const Source source = source_.WithLine(el->line_number);
    for (xml::Attribute& attr : el->attributes) {
      // If the attribute has no namespace, interpret values as if
+62 −1
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ class XmlReferenceLinkerTest : public ::testing::Test {
                                            test::AttributeBuilder()
                                                .SetTypeMask(android::ResTable_map::TYPE_STRING)
                                                .Build())
                           .AddPublicSymbol("android:attr/angle", ResourceId(0x01010004),
                                            test::AttributeBuilder().Build())

                           // Add one real symbol that was introduces in v21
                           .AddPublicSymbol("android:attr/colorAccent", ResourceId(0x01010435),
@@ -75,7 +77,7 @@ class XmlReferenceLinkerTest : public ::testing::Test {
  }

 protected:
  std::unique_ptr<IAaptContext> context_;
  std::unique_ptr<test::Context> context_;
};

TEST_F(XmlReferenceLinkerTest, LinkBasicAttributes) {
@@ -254,4 +256,63 @@ TEST_F(XmlReferenceLinkerTest, LinkViewWithLocalPackageAndAliasOfTheSameName) {
  EXPECT_EQ(make_value(ResourceId(0x7f030000)), ref->id);
}


TEST_F(XmlReferenceLinkerTest, AddAngleOnGradientForAndroidQ) {
  std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"(
    <gradient />)");

  XmlReferenceLinker linker;
  ASSERT_TRUE(linker.Consume(context_.get(), doc.get()));

  xml::Element* gradient_el = doc->root.get();
  ASSERT_THAT(gradient_el, NotNull());

  xml::Attribute* xml_attr = gradient_el->FindAttribute(xml::kSchemaAndroid, "angle");
  ASSERT_THAT(xml_attr, NotNull());
  ASSERT_TRUE(xml_attr->compiled_attribute);
  EXPECT_EQ(make_value(ResourceId(0x01010004)), xml_attr->compiled_attribute.value().id);

  BinaryPrimitive* value = ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get());
  ASSERT_THAT(value, NotNull());
  EXPECT_EQ(value->value.dataType, android::Res_value::TYPE_INT_DEC);
  EXPECT_EQ(value->value.data, 0U);
}

TEST_F(XmlReferenceLinkerTest, DoNotOverwriteAngleOnGradientForAndroidQ) {
  std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"(
  <gradient xmlns:android="http://schemas.android.com/apk/res/android"
      android:angle="90"/>)");

  XmlReferenceLinker linker;
  ASSERT_TRUE(linker.Consume(context_.get(), doc.get()));

  xml::Element* gradient_el = doc->root.get();
  ASSERT_THAT(gradient_el, NotNull());

  xml::Attribute* xml_attr = gradient_el->FindAttribute(xml::kSchemaAndroid, "angle");
  ASSERT_THAT(xml_attr, NotNull());
  ASSERT_TRUE(xml_attr->compiled_attribute);
  EXPECT_EQ(make_value(ResourceId(0x01010004)), xml_attr->compiled_attribute.value().id);

  BinaryPrimitive* value = ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get());
  ASSERT_THAT(value, NotNull());
  EXPECT_EQ(value->value.dataType, android::Res_value::TYPE_INT_DEC);
  EXPECT_EQ(value->value.data, 90U);
}

TEST_F(XmlReferenceLinkerTest, DoNotOverwriteAngleOnGradientForPostAndroidQ) {
  std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"(
  <gradient xmlns:android="http://schemas.android.com/apk/res/android" />)");
  context_->SetMinSdkVersion(30);

  XmlReferenceLinker linker;
  ASSERT_TRUE(linker.Consume(context_.get(), doc.get()));

  xml::Element* gradient_el = doc->root.get();
  ASSERT_THAT(gradient_el, NotNull());

  xml::Attribute* xml_attr = gradient_el->FindAttribute(xml::kSchemaAndroid, "angle");
  ASSERT_THAT(xml_attr, IsNull());
}

}  // namespace aapt
+4 −0
Original line number Diff line number Diff line
@@ -81,6 +81,10 @@ class Context : public IAaptContext {
    return min_sdk_version_;
  }

  void SetMinSdkVersion(int min_sdk_version) {
    min_sdk_version_ = min_sdk_version;
  }

 const std::set<std::string>& GetSplitNameDependencies() override {
    return split_name_dependencies_;
  }