Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "link/Linkers.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 19 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 21 | using ::testing::IsNull; |
| 22 | using ::testing::NotNull; |
| 23 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 24 | namespace aapt { |
| 25 | |
| 26 | class XmlReferenceLinkerTest : public ::testing::Test { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 27 | public: |
| 28 | void SetUp() override { |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 29 | context_ = test::ContextBuilder() |
| 30 | .SetCompilationPackage("com.app.test") |
| 31 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test", {"com.android.support"}}) |
| 32 | .AddSymbolSource( |
| 33 | test::StaticSymbolSourceBuilder() |
| 34 | .AddPublicSymbol("android:attr/layout_width", ResourceId(0x01010000), |
| 35 | test::AttributeBuilder() |
| 36 | .SetTypeMask(android::ResTable_map::TYPE_ENUM | |
| 37 | android::ResTable_map::TYPE_DIMENSION) |
| 38 | .AddItem("match_parent", 0xffffffff) |
| 39 | .Build()) |
| 40 | .AddPublicSymbol("android:attr/background", ResourceId(0x01010001), |
| 41 | test::AttributeBuilder() |
| 42 | .SetTypeMask(android::ResTable_map::TYPE_COLOR) |
| 43 | .Build()) |
| 44 | .AddPublicSymbol("android:attr/attr", ResourceId(0x01010002), |
| 45 | test::AttributeBuilder().Build()) |
| 46 | .AddPublicSymbol("android:attr/text", ResourceId(0x01010003), |
| 47 | test::AttributeBuilder() |
| 48 | .SetTypeMask(android::ResTable_map::TYPE_STRING) |
| 49 | .Build()) |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 50 | .AddPublicSymbol("android:attr/angle", ResourceId(0x01010004), |
| 51 | test::AttributeBuilder().Build()) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 53 | // Add one real symbol that was introduces in v21 |
| 54 | .AddPublicSymbol("android:attr/colorAccent", ResourceId(0x01010435), |
| 55 | test::AttributeBuilder().Build()) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 57 | // Private symbol. |
| 58 | .AddSymbol("android:color/hidden", ResourceId(0x01020001)) |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 60 | .AddPublicSymbol("android:id/id", ResourceId(0x01030000)) |
| 61 | .AddSymbol("com.app.test:id/id", ResourceId(0x7f030000)) |
| 62 | .AddSymbol("com.app.test:color/green", ResourceId(0x7f020000)) |
| 63 | .AddSymbol("com.app.test:color/red", ResourceId(0x7f020001)) |
| 64 | .AddSymbol("com.app.test:attr/colorAccent", ResourceId(0x7f010000), |
| 65 | test::AttributeBuilder() |
| 66 | .SetTypeMask(android::ResTable_map::TYPE_COLOR) |
| 67 | .Build()) |
| 68 | .AddPublicSymbol("com.app.test:attr/com.android.support$colorAccent", |
| 69 | ResourceId(0x7f010001), |
| 70 | test::AttributeBuilder() |
| 71 | .SetTypeMask(android::ResTable_map::TYPE_COLOR) |
| 72 | .Build()) |
| 73 | .AddPublicSymbol("com.app.test:attr/attr", ResourceId(0x7f010002), |
| 74 | test::AttributeBuilder().Build()) |
| 75 | .Build()) |
| 76 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | protected: |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 80 | std::unique_ptr<test::Context> context_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | TEST_F(XmlReferenceLinkerTest, LinkBasicAttributes) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 84 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 85 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 86 | android:layout_width="match_parent" |
| 87 | android:background="@color/green" |
| 88 | android:text="hello" |
| 89 | android:attr="\?hello" |
| 90 | nonAaptAttr="1" |
| 91 | nonAaptAttrRef="@id/id" |
| 92 | class="hello" />)"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 93 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 94 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 97 | xml::Element* view_el = doc->root.get(); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 98 | ASSERT_THAT(view_el, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 100 | xml::Attribute* xml_attr = view_el->FindAttribute(xml::kSchemaAndroid, "layout_width"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 101 | ASSERT_THAT(xml_attr, NotNull()); |
| 102 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 103 | EXPECT_EQ(make_value(ResourceId(0x01010000)), xml_attr->compiled_attribute.value().id); |
| 104 | EXPECT_THAT(ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get()), NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | xml_attr = view_el->FindAttribute(xml::kSchemaAndroid, "background"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 107 | ASSERT_THAT(xml_attr, NotNull()); |
| 108 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 109 | EXPECT_EQ(make_value(ResourceId(0x01010001)), xml_attr->compiled_attribute.value().id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | Reference* ref = ValueCast<Reference>(xml_attr->compiled_value.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 111 | ASSERT_THAT(ref, NotNull()); |
| 112 | EXPECT_EQ(make_value(test::ParseNameOrDie("color/green")), ref->name); // Make sure the name |
| 113 | // didn't change. |
| 114 | EXPECT_EQ(make_value(ResourceId(0x7f020000)), ref->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | xml_attr = view_el->FindAttribute(xml::kSchemaAndroid, "text"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 117 | ASSERT_THAT(xml_attr, NotNull()); |
| 118 | EXPECT_TRUE(xml_attr->compiled_attribute); |
| 119 | EXPECT_THAT(xml_attr->compiled_value, IsNull()); // Strings don't get compiled for memory sake. |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 120 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 121 | xml_attr = view_el->FindAttribute(xml::kSchemaAndroid, "attr"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 122 | ASSERT_THAT(xml_attr, NotNull()); |
| 123 | EXPECT_TRUE(xml_attr->compiled_attribute); |
| 124 | EXPECT_THAT(xml_attr->compiled_value, IsNull()); // Should be a plain string. |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 126 | xml_attr = view_el->FindAttribute("", "nonAaptAttr"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 127 | ASSERT_THAT(xml_attr, NotNull()); |
| 128 | EXPECT_FALSE(xml_attr->compiled_attribute); |
| 129 | EXPECT_THAT(ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get()), NotNull()); |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 130 | |
| 131 | xml_attr = view_el->FindAttribute("", "nonAaptAttrRef"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 132 | ASSERT_THAT(xml_attr, NotNull()); |
| 133 | EXPECT_FALSE(xml_attr->compiled_attribute); |
| 134 | EXPECT_THAT(ValueCast<Reference>(xml_attr->compiled_value.get()), NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | xml_attr = view_el->FindAttribute("", "class"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 137 | ASSERT_THAT(xml_attr, NotNull()); |
| 138 | EXPECT_FALSE(xml_attr->compiled_attribute); |
| 139 | EXPECT_THAT(xml_attr->compiled_value, IsNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 142 | TEST_F(XmlReferenceLinkerTest, PrivateSymbolsAreNotLinked) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 143 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 144 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 145 | android:colorAccent="@android:color/hidden" />)"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 146 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 147 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | ASSERT_FALSE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 151 | TEST_F(XmlReferenceLinkerTest, PrivateSymbolsAreLinkedWhenReferenceHasStarPrefix) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 152 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 153 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 154 | android:colorAccent="@*android:color/hidden" />)"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 155 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 156 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 160 | TEST_F(XmlReferenceLinkerTest, LinkMangledAttributes) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 161 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 162 | <View xmlns:support="http://schemas.android.com/apk/res/com.android.support" |
| 163 | support:colorAccent="#ff0000" />)"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 164 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 165 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 167 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 168 | xml::Element* view_el = doc->root.get(); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 169 | ASSERT_THAT(view_el, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 171 | xml::Attribute* xml_attr = |
| 172 | view_el->FindAttribute(xml::BuildPackageNamespace("com.android.support"), "colorAccent"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 173 | ASSERT_THAT(xml_attr, NotNull()); |
| 174 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 175 | EXPECT_EQ(make_value(ResourceId(0x7f010001)), xml_attr->compiled_attribute.value().id); |
| 176 | EXPECT_THAT(ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get()), NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | TEST_F(XmlReferenceLinkerTest, LinkAutoResReference) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 180 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 181 | <View xmlns:app="http://schemas.android.com/apk/res-auto" |
| 182 | app:colorAccent="@app:color/red" />)"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 184 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 187 | xml::Element* view_el = doc->root.get(); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 188 | ASSERT_THAT(view_el, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 189 | |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 190 | xml::Attribute* xml_attr = view_el->FindAttribute(xml::kSchemaAuto, "colorAccent"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 191 | ASSERT_THAT(xml_attr, NotNull()); |
| 192 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 193 | EXPECT_EQ(make_value(ResourceId(0x7f010000)), xml_attr->compiled_attribute.value().id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | Reference* ref = ValueCast<Reference>(xml_attr->compiled_value.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 195 | ASSERT_THAT(ref, NotNull()); |
| 196 | ASSERT_TRUE(ref->name); |
| 197 | EXPECT_EQ(make_value(ResourceId(0x7f020001)), ref->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | TEST_F(XmlReferenceLinkerTest, LinkViewWithShadowedPackageAlias) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 201 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 202 | <View xmlns:app="http://schemas.android.com/apk/res/android" app:attr="@app:id/id"> |
| 203 | <View xmlns:app="http://schemas.android.com/apk/res/com.app.test" app:attr="@app:id/id"/> |
| 204 | </View>)"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 206 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 208 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 209 | xml::Element* view_el = doc->root.get(); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 210 | ASSERT_THAT(view_el, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | // All attributes and references in this element should be referring to |
| 213 | // "android" (0x01). |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 214 | xml::Attribute* xml_attr = view_el->FindAttribute(xml::kSchemaAndroid, "attr"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 215 | ASSERT_THAT(xml_attr, NotNull()); |
| 216 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 217 | EXPECT_EQ(make_value(ResourceId(0x01010002)), xml_attr->compiled_attribute.value().id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | Reference* ref = ValueCast<Reference>(xml_attr->compiled_value.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 219 | ASSERT_THAT(ref, NotNull()); |
| 220 | EXPECT_EQ(make_value(ResourceId(0x01030000)), ref->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | ASSERT_FALSE(view_el->GetChildElements().empty()); |
| 223 | view_el = view_el->GetChildElements().front(); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 224 | ASSERT_THAT(view_el, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 225 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 226 | // All attributes and references in this element should be referring to |
| 227 | // "com.app.test" (0x7f). |
Adam Lesinski | 3866554 | 2016-12-28 12:25:46 -0500 | [diff] [blame] | 228 | xml_attr = view_el->FindAttribute(xml::BuildPackageNamespace("com.app.test"), "attr"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 229 | ASSERT_THAT(xml_attr, NotNull()); |
| 230 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 231 | EXPECT_EQ(make_value(ResourceId(0x7f010002)), xml_attr->compiled_attribute.value().id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | ref = ValueCast<Reference>(xml_attr->compiled_value.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 233 | ASSERT_THAT(ref, NotNull()); |
| 234 | EXPECT_EQ(make_value(ResourceId(0x7f030000)), ref->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | TEST_F(XmlReferenceLinkerTest, LinkViewWithLocalPackageAndAliasOfTheSameName) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 238 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 239 | <View xmlns:android="http://schemas.android.com/apk/res/com.app.test" |
| 240 | android:attr="@id/id"/>)"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 241 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 242 | XmlReferenceLinker linker(nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 243 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 245 | xml::Element* view_el = doc->root.get(); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 246 | ASSERT_THAT(view_el, NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 247 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | // All attributes and references in this element should be referring to |
| 249 | // "com.app.test" (0x7f). |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 250 | xml::Attribute* xml_attr = view_el->FindAttribute(xml::BuildPackageNamespace("com.app.test"), "attr"); |
| 251 | ASSERT_THAT(xml_attr, NotNull()); |
| 252 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 253 | EXPECT_EQ(make_value(ResourceId(0x7f010002)), xml_attr->compiled_attribute.value().id); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | Reference* ref = ValueCast<Reference>(xml_attr->compiled_value.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 255 | ASSERT_THAT(ref, NotNull()); |
| 256 | EXPECT_EQ(make_value(ResourceId(0x7f030000)), ref->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 259 | |
| 260 | TEST_F(XmlReferenceLinkerTest, AddAngleOnGradientForAndroidQ) { |
| 261 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 262 | <gradient />)"); |
| 263 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 264 | XmlReferenceLinker linker(nullptr); |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 265 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
| 266 | |
| 267 | xml::Element* gradient_el = doc->root.get(); |
| 268 | ASSERT_THAT(gradient_el, NotNull()); |
| 269 | |
| 270 | xml::Attribute* xml_attr = gradient_el->FindAttribute(xml::kSchemaAndroid, "angle"); |
| 271 | ASSERT_THAT(xml_attr, NotNull()); |
| 272 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 273 | EXPECT_EQ(make_value(ResourceId(0x01010004)), xml_attr->compiled_attribute.value().id); |
| 274 | |
| 275 | BinaryPrimitive* value = ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get()); |
| 276 | ASSERT_THAT(value, NotNull()); |
| 277 | EXPECT_EQ(value->value.dataType, android::Res_value::TYPE_INT_DEC); |
| 278 | EXPECT_EQ(value->value.data, 0U); |
| 279 | } |
| 280 | |
| 281 | TEST_F(XmlReferenceLinkerTest, DoNotOverwriteAngleOnGradientForAndroidQ) { |
| 282 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 283 | <gradient xmlns:android="http://schemas.android.com/apk/res/android" |
| 284 | android:angle="90"/>)"); |
| 285 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 286 | XmlReferenceLinker linker(nullptr); |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 287 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
| 288 | |
| 289 | xml::Element* gradient_el = doc->root.get(); |
| 290 | ASSERT_THAT(gradient_el, NotNull()); |
| 291 | |
| 292 | xml::Attribute* xml_attr = gradient_el->FindAttribute(xml::kSchemaAndroid, "angle"); |
| 293 | ASSERT_THAT(xml_attr, NotNull()); |
| 294 | ASSERT_TRUE(xml_attr->compiled_attribute); |
| 295 | EXPECT_EQ(make_value(ResourceId(0x01010004)), xml_attr->compiled_attribute.value().id); |
| 296 | |
| 297 | BinaryPrimitive* value = ValueCast<BinaryPrimitive>(xml_attr->compiled_value.get()); |
| 298 | ASSERT_THAT(value, NotNull()); |
| 299 | EXPECT_EQ(value->value.dataType, android::Res_value::TYPE_INT_DEC); |
| 300 | EXPECT_EQ(value->value.data, 90U); |
| 301 | } |
| 302 | |
| 303 | TEST_F(XmlReferenceLinkerTest, DoNotOverwriteAngleOnGradientForPostAndroidQ) { |
| 304 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDomForPackageName(context_.get(), R"( |
| 305 | <gradient xmlns:android="http://schemas.android.com/apk/res/android" />)"); |
| 306 | context_->SetMinSdkVersion(30); |
| 307 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame^] | 308 | XmlReferenceLinker linker(nullptr); |
Ryan Mitchell | 4d5833e | 2019-09-04 02:46:03 -0700 | [diff] [blame] | 309 | ASSERT_TRUE(linker.Consume(context_.get(), doc.get())); |
| 310 | |
| 311 | xml::Element* gradient_el = doc->root.get(); |
| 312 | ASSERT_THAT(gradient_el, NotNull()); |
| 313 | |
| 314 | xml::Attribute* xml_attr = gradient_el->FindAttribute(xml::kSchemaAndroid, "angle"); |
| 315 | ASSERT_THAT(xml_attr, IsNull()); |
| 316 | } |
| 317 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | } // namespace aapt |