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
This commit is contained in:
Ryan Mitchell
2019-09-04 02:46:03 -07:00
parent 3dc631cfc9
commit 4d5833ecc8
3 changed files with 75 additions and 1 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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_;
}