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" |
| 18 | |
| 19 | #include "test/Builders.h" |
| 20 | #include "test/Context.h" |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | namespace aapt { |
| 25 | |
| 26 | class XmlReferenceLinkerTest : public ::testing::Test { |
| 27 | public: |
| 28 | void SetUp() override { |
| 29 | mContext = test::ContextBuilder() |
| 30 | .setCompilationPackage(u"com.app.test") |
| 31 | .setNameManglerPolicy( |
| 32 | NameManglerPolicy{ u"com.app.test", { u"com.android.support" } }) |
| 33 | .setSymbolTable(test::StaticSymbolTableBuilder() |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 34 | .addPublicSymbol(u"@android:attr/layout_width", ResourceId(0x01010000), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | test::AttributeBuilder() |
| 36 | .setTypeMask(android::ResTable_map::TYPE_ENUM | |
| 37 | android::ResTable_map::TYPE_DIMENSION) |
| 38 | .addItem(u"match_parent", 0xffffffff) |
| 39 | .build()) |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 40 | .addPublicSymbol(u"@android:attr/background", ResourceId(0x01010001), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | test::AttributeBuilder() |
| 42 | .setTypeMask(android::ResTable_map::TYPE_COLOR).build()) |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 43 | .addPublicSymbol(u"@android:attr/attr", ResourceId(0x01010002), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | test::AttributeBuilder().build()) |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 45 | .addPublicSymbol(u"@android:attr/text", ResourceId(0x01010003), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | test::AttributeBuilder() |
| 47 | .setTypeMask(android::ResTable_map::TYPE_STRING) |
| 48 | .build()) |
| 49 | |
| 50 | // Add one real symbol that was introduces in v21 |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 51 | .addPublicSymbol(u"@android:attr/colorAccent", ResourceId(0x01010435), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | test::AttributeBuilder().build()) |
| 53 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 54 | // Private symbol. |
| 55 | .addSymbol(u"@android:color/hidden", ResourceId(0x01020001)) |
| 56 | |
| 57 | .addPublicSymbol(u"@android:id/id", ResourceId(0x01030000)) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | .addSymbol(u"@com.app.test:id/id", ResourceId(0x7f030000)) |
| 59 | .addSymbol(u"@com.app.test:color/green", ResourceId(0x7f020000)) |
| 60 | .addSymbol(u"@com.app.test:color/red", ResourceId(0x7f020001)) |
| 61 | .addSymbol(u"@com.app.test:attr/colorAccent", ResourceId(0x7f010000), |
| 62 | test::AttributeBuilder() |
| 63 | .setTypeMask(android::ResTable_map::TYPE_COLOR).build()) |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 64 | .addPublicSymbol(u"@com.app.test:attr/com.android.support$colorAccent", |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | ResourceId(0x7f010001), test::AttributeBuilder() |
| 66 | .setTypeMask(android::ResTable_map::TYPE_COLOR).build()) |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 67 | .addPublicSymbol(u"@com.app.test:attr/attr", ResourceId(0x7f010002), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | test::AttributeBuilder().build()) |
| 69 | .build()) |
| 70 | .build(); |
| 71 | } |
| 72 | |
| 73 | protected: |
| 74 | std::unique_ptr<IAaptContext> mContext; |
| 75 | }; |
| 76 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | TEST_F(XmlReferenceLinkerTest, LinkBasicAttributes) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 78 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 80 | android:layout_width="match_parent" |
| 81 | android:background="@color/green" |
| 82 | android:text="hello" |
| 83 | class="hello" />)EOF"); |
| 84 | |
| 85 | XmlReferenceLinker linker; |
| 86 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 87 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 88 | xml::Element* viewEl = xml::findRootElement(doc.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 | ASSERT_NE(viewEl, nullptr); |
| 90 | |
| 91 | xml::Attribute* xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android", |
| 92 | u"layout_width"); |
| 93 | ASSERT_NE(xmlAttr, nullptr); |
| 94 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 95 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x01010000)); |
| 96 | ASSERT_NE(xmlAttr->compiledValue, nullptr); |
| 97 | ASSERT_NE(valueCast<BinaryPrimitive>(xmlAttr->compiledValue.get()), nullptr); |
| 98 | |
| 99 | xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android", u"background"); |
| 100 | ASSERT_NE(xmlAttr, nullptr); |
| 101 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 102 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x01010001)); |
| 103 | ASSERT_NE(xmlAttr->compiledValue, nullptr); |
| 104 | Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get()); |
| 105 | ASSERT_NE(ref, nullptr); |
| 106 | AAPT_ASSERT_TRUE(ref->name); |
| 107 | EXPECT_EQ(ref->name.value(), test::parseNameOrDie(u"@color/green")); // Make sure the name |
| 108 | // didn't change. |
| 109 | AAPT_ASSERT_TRUE(ref->id); |
| 110 | EXPECT_EQ(ref->id.value(), ResourceId(0x7f020000)); |
| 111 | |
| 112 | xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android", u"text"); |
| 113 | ASSERT_NE(xmlAttr, nullptr); |
| 114 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 115 | ASSERT_FALSE(xmlAttr->compiledValue); // Strings don't get compiled for memory sake. |
| 116 | |
| 117 | xmlAttr = viewEl->findAttribute(u"", u"class"); |
| 118 | ASSERT_NE(xmlAttr, nullptr); |
| 119 | AAPT_ASSERT_FALSE(xmlAttr->compiledAttribute); |
| 120 | ASSERT_EQ(xmlAttr->compiledValue, nullptr); |
| 121 | } |
| 122 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 123 | TEST_F(XmlReferenceLinkerTest, PrivateSymbolsAreNotLinked) { |
| 124 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
| 125 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 126 | android:colorAccent="@android:color/hidden" />)EOF"); |
| 127 | |
| 128 | XmlReferenceLinker linker; |
| 129 | ASSERT_FALSE(linker.consume(mContext.get(), doc.get())); |
| 130 | } |
| 131 | |
| 132 | TEST_F(XmlReferenceLinkerTest, PrivateSymbolsAreLinkedWhenReferenceHasStarPrefix) { |
| 133 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
| 134 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 135 | android:colorAccent="@*android:color/hidden" />)EOF"); |
| 136 | |
| 137 | XmlReferenceLinker linker; |
| 138 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 139 | } |
| 140 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 141 | TEST_F(XmlReferenceLinkerTest, SdkLevelsAreRecorded) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 142 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 144 | android:colorAccent="#ffffff" />)EOF"); |
| 145 | |
| 146 | XmlReferenceLinker linker; |
| 147 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 148 | EXPECT_TRUE(linker.getSdkLevels().count(21) == 1); |
| 149 | } |
| 150 | |
| 151 | TEST_F(XmlReferenceLinkerTest, LinkMangledAttributes) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 152 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | <View xmlns:support="http://schemas.android.com/apk/res/com.android.support" |
| 154 | support:colorAccent="#ff0000" />)EOF"); |
| 155 | |
| 156 | XmlReferenceLinker linker; |
| 157 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 158 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 159 | xml::Element* viewEl = xml::findRootElement(doc.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 160 | ASSERT_NE(viewEl, nullptr); |
| 161 | |
| 162 | xml::Attribute* xmlAttr = viewEl->findAttribute( |
| 163 | u"http://schemas.android.com/apk/res/com.android.support", u"colorAccent"); |
| 164 | ASSERT_NE(xmlAttr, nullptr); |
| 165 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 166 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x7f010001)); |
| 167 | ASSERT_NE(valueCast<BinaryPrimitive>(xmlAttr->compiledValue.get()), nullptr); |
| 168 | } |
| 169 | |
| 170 | TEST_F(XmlReferenceLinkerTest, LinkAutoResReference) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 171 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | <View xmlns:app="http://schemas.android.com/apk/res-auto" |
| 173 | app:colorAccent="@app:color/red" />)EOF"); |
| 174 | |
| 175 | XmlReferenceLinker linker; |
| 176 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 177 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 178 | xml::Element* viewEl = xml::findRootElement(doc.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 179 | ASSERT_NE(viewEl, nullptr); |
| 180 | |
| 181 | xml::Attribute* xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res-auto", |
| 182 | u"colorAccent"); |
| 183 | ASSERT_NE(xmlAttr, nullptr); |
| 184 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 185 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x7f010000)); |
| 186 | Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get()); |
| 187 | ASSERT_NE(ref, nullptr); |
| 188 | AAPT_ASSERT_TRUE(ref->name); |
| 189 | AAPT_ASSERT_TRUE(ref->id); |
| 190 | EXPECT_EQ(ref->id.value(), ResourceId(0x7f020001)); |
| 191 | } |
| 192 | |
| 193 | TEST_F(XmlReferenceLinkerTest, LinkViewWithShadowedPackageAlias) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 194 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 195 | <View xmlns:app="http://schemas.android.com/apk/res/android" |
| 196 | app:attr="@app:id/id"> |
| 197 | <View xmlns:app="http://schemas.android.com/apk/res/com.app.test" |
| 198 | app:attr="@app:id/id"/> |
| 199 | </View>)EOF"); |
| 200 | |
| 201 | XmlReferenceLinker linker; |
| 202 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 203 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 204 | xml::Element* viewEl = xml::findRootElement(doc.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | ASSERT_NE(viewEl, nullptr); |
| 206 | |
| 207 | // All attributes and references in this element should be referring to "android" (0x01). |
| 208 | xml::Attribute* xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android", |
| 209 | u"attr"); |
| 210 | ASSERT_NE(xmlAttr, nullptr); |
| 211 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 212 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x01010002)); |
| 213 | Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get()); |
| 214 | ASSERT_NE(ref, nullptr); |
| 215 | AAPT_ASSERT_TRUE(ref->id); |
| 216 | EXPECT_EQ(ref->id.value(), ResourceId(0x01030000)); |
| 217 | |
| 218 | ASSERT_FALSE(viewEl->getChildElements().empty()); |
| 219 | viewEl = viewEl->getChildElements().front(); |
| 220 | ASSERT_NE(viewEl, nullptr); |
| 221 | |
| 222 | // All attributes and references in this element should be referring to "com.app.test" (0x7f). |
| 223 | xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/com.app.test", u"attr"); |
| 224 | ASSERT_NE(xmlAttr, nullptr); |
| 225 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 226 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x7f010002)); |
| 227 | ref = valueCast<Reference>(xmlAttr->compiledValue.get()); |
| 228 | ASSERT_NE(ref, nullptr); |
| 229 | AAPT_ASSERT_TRUE(ref->id); |
| 230 | EXPECT_EQ(ref->id.value(), ResourceId(0x7f030000)); |
| 231 | } |
| 232 | |
| 233 | TEST_F(XmlReferenceLinkerTest, LinkViewWithLocalPackageAndAliasOfTheSameName) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 234 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 235 | <View xmlns:android="http://schemas.android.com/apk/res/com.app.test" |
| 236 | android:attr="@id/id"/>)EOF"); |
| 237 | |
| 238 | XmlReferenceLinker linker; |
| 239 | ASSERT_TRUE(linker.consume(mContext.get(), doc.get())); |
| 240 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame^] | 241 | xml::Element* viewEl = xml::findRootElement(doc.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 242 | ASSERT_NE(viewEl, nullptr); |
| 243 | |
| 244 | // All attributes and references in this element should be referring to "com.app.test" (0x7f). |
| 245 | xml::Attribute* xmlAttr = viewEl->findAttribute( |
| 246 | u"http://schemas.android.com/apk/res/com.app.test", u"attr"); |
| 247 | ASSERT_NE(xmlAttr, nullptr); |
| 248 | AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute); |
| 249 | EXPECT_EQ(xmlAttr->compiledAttribute.value().id, ResourceId(0x7f010002)); |
| 250 | Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get()); |
| 251 | ASSERT_NE(ref, nullptr); |
| 252 | AAPT_ASSERT_TRUE(ref->id); |
| 253 | EXPECT_EQ(ref->id.value(), ResourceId(0x7f030000)); |
| 254 | } |
| 255 | |
| 256 | } // namespace aapt |