diff options
| author | 2019-09-04 02:46:03 -0700 | |
|---|---|---|
| committer | 2019-09-04 09:18:55 -0700 | |
| commit | 4d5833ecc8b87ef291f7a810a63b630b76c047ca (patch) | |
| tree | 17d26cb5001e27ce946e7682c7c537feb9fab70f | |
| parent | 3dc631cfc9b08b385f2f11b072b314cce0f8bdb3 (diff) | |
Add android:angle on <gradient> for min sdk <= Q
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.
Bug: 139883313
Test: aapt2_tests
Change-Id: I82e222f635858ed3ecf4a3f3761503d857adb93b
| -rw-r--r-- | tools/aapt2/link/XmlReferenceLinker.cpp | 9 | ||||
| -rw-r--r-- | tools/aapt2/link/XmlReferenceLinker_test.cpp | 63 | ||||
| -rw-r--r-- | tools/aapt2/test/Context.h | 4 |
3 files changed, 75 insertions, 1 deletions
diff --git a/tools/aapt2/link/XmlReferenceLinker.cpp b/tools/aapt2/link/XmlReferenceLinker.cpp index f3be483ab728..c3c16b92f712 100644 --- a/tools/aapt2/link/XmlReferenceLinker.cpp +++ b/tools/aapt2/link/XmlReferenceLinker.cpp @@ -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 diff --git a/tools/aapt2/link/XmlReferenceLinker_test.cpp b/tools/aapt2/link/XmlReferenceLinker_test.cpp index ef99355e5b5f..0ce2e50d6e44 100644 --- a/tools/aapt2/link/XmlReferenceLinker_test.cpp +++ b/tools/aapt2/link/XmlReferenceLinker_test.cpp @@ -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 diff --git a/tools/aapt2/test/Context.h b/tools/aapt2/test/Context.h index 7e10a59df877..553c43e6c469 100644 --- a/tools/aapt2/test/Context.h +++ b/tools/aapt2/test/Context.h @@ -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_; } |