11ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski/*
21ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
31ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
41ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
51ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * you may not use this file except in compliance with the License.
61ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * You may obtain a copy of the License at
71ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
81ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
91ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Unless required by applicable law or agreed to in writing, software
111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * See the License for the specific language governing permissions and
141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * limitations under the License.
151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski */
161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1764587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski#include <test/Context.h>
181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "link/Linkers.h"
1964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski#include "test/Test.h"
201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
211ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskinamespace aapt {
221ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
231ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskiclass XmlReferenceLinkerTest : public ::testing::Test {
241ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskipublic:
251ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    void SetUp() override {
261ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski        mContext = test::ContextBuilder()
271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                .setCompilationPackage(u"com.app.test")
281ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                .setNameManglerPolicy(
291ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                        NameManglerPolicy{ u"com.app.test", { u"com.android.support" } })
3064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                .addSymbolSource(test::StaticSymbolSourceBuilder()
31467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@android:attr/layout_width", ResourceId(0x01010000),
321ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder()
331ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                        .setTypeMask(android::ResTable_map::TYPE_ENUM |
341ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                                     android::ResTable_map::TYPE_DIMENSION)
351ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                        .addItem(u"match_parent", 0xffffffff)
361ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                        .build())
37467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@android:attr/background", ResourceId(0x01010001),
381ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder()
391ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                        .setTypeMask(android::ResTable_map::TYPE_COLOR).build())
40467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@android:attr/attr", ResourceId(0x01010002),
411ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder().build())
42467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@android:attr/text", ResourceId(0x01010003),
431ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder()
441ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                        .setTypeMask(android::ResTable_map::TYPE_STRING)
451ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                        .build())
461ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
471ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                         // Add one real symbol that was introduces in v21
48467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@android:attr/colorAccent", ResourceId(0x01010435),
491ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder().build())
501ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
51467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        // Private symbol.
52467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addSymbol(u"@android:color/hidden", ResourceId(0x01020001))
53467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
54467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@android:id/id", ResourceId(0x01030000))
551ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                        .addSymbol(u"@com.app.test:id/id", ResourceId(0x7f030000))
561ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                        .addSymbol(u"@com.app.test:color/green", ResourceId(0x7f020000))
571ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                        .addSymbol(u"@com.app.test:color/red", ResourceId(0x7f020001))
581ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                        .addSymbol(u"@com.app.test:attr/colorAccent", ResourceId(0x7f010000),
591ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder()
601ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                       .setTypeMask(android::ResTable_map::TYPE_COLOR).build())
61467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@com.app.test:attr/com.android.support$colorAccent",
621ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   ResourceId(0x7f010001), test::AttributeBuilder()
631ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                       .setTypeMask(android::ResTable_map::TYPE_COLOR).build())
64467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                        .addPublicSymbol(u"@com.app.test:attr/attr", ResourceId(0x7f010002),
651ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                   test::AttributeBuilder().build())
661ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                        .build())
671ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                .build();
681ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    }
691ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
701ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskiprotected:
711ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    std::unique_ptr<IAaptContext> mContext;
721ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski};
731ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
741ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(XmlReferenceLinkerTest, LinkBasicAttributes) {
75467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
761ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski        <View xmlns:android="http://schemas.android.com/apk/res/android"
771ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski              android:layout_width="match_parent"
781ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski              android:background="@color/green"
791ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski              android:text="hello"
801ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski              class="hello" />)EOF");
811ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
821ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    XmlReferenceLinker linker;
831ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
841ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
85467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    xml::Element* viewEl = xml::findRootElement(doc.get());
861ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(viewEl, nullptr);
871ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
881ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xml::Attribute* xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android",
891ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                                    u"layout_width");
901ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
911ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
9264587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
9364587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x01010000));
941ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr->compiledValue, nullptr);
951ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(valueCast<BinaryPrimitive>(xmlAttr->compiledValue.get()), nullptr);
961ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
971ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android", u"background");
981ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
991ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
10064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
10164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x01010001));
1021ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr->compiledValue, nullptr);
1031ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get());
1041ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(ref, nullptr);
1051ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->name);
1061ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_EQ(ref->name.value(), test::parseNameOrDie(u"@color/green")); // Make sure the name
1071ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                                                         // didn't change.
1081ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->id);
1091ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_EQ(ref->id.value(), ResourceId(0x7f020000));
1101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android", u"text");
1121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
1131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
1141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_FALSE(xmlAttr->compiledValue);   // Strings don't get compiled for memory sake.
1151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xmlAttr = viewEl->findAttribute(u"", u"class");
1171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
1181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_FALSE(xmlAttr->compiledAttribute);
1191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_EQ(xmlAttr->compiledValue, nullptr);
1201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
1211ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
122467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam LesinskiTEST_F(XmlReferenceLinkerTest, PrivateSymbolsAreNotLinked) {
123467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
124467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski        <View xmlns:android="http://schemas.android.com/apk/res/android"
125467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski              android:colorAccent="@android:color/hidden" />)EOF");
126467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
127467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    XmlReferenceLinker linker;
128467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    ASSERT_FALSE(linker.consume(mContext.get(), doc.get()));
129467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski}
130467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
131467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam LesinskiTEST_F(XmlReferenceLinkerTest, PrivateSymbolsAreLinkedWhenReferenceHasStarPrefix) {
132467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
133467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    <View xmlns:android="http://schemas.android.com/apk/res/android"
134467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski          android:colorAccent="@*android:color/hidden" />)EOF");
135467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
136467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    XmlReferenceLinker linker;
137467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
138467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski}
139467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
1401ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(XmlReferenceLinkerTest, SdkLevelsAreRecorded) {
141467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
1421ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski        <View xmlns:android="http://schemas.android.com/apk/res/android"
1431ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski              android:colorAccent="#ffffff" />)EOF");
1441ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1451ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    XmlReferenceLinker linker;
1461ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
1471ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_TRUE(linker.getSdkLevels().count(21) == 1);
1481ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
1491ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1501ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(XmlReferenceLinkerTest, LinkMangledAttributes) {
151467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
1521ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            <View xmlns:support="http://schemas.android.com/apk/res/com.android.support"
1531ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                  support:colorAccent="#ff0000" />)EOF");
1541ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1551ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    XmlReferenceLinker linker;
1561ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
1571ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
158467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    xml::Element* viewEl = xml::findRootElement(doc.get());
1591ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(viewEl, nullptr);
1601ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1611ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xml::Attribute* xmlAttr = viewEl->findAttribute(
1621ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            u"http://schemas.android.com/apk/res/com.android.support", u"colorAccent");
1631ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
1641ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
16564587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
16664587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x7f010001));
1671ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(valueCast<BinaryPrimitive>(xmlAttr->compiledValue.get()), nullptr);
1681ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
1691ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1701ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(XmlReferenceLinkerTest, LinkAutoResReference) {
171467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
1721ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            <View xmlns:app="http://schemas.android.com/apk/res-auto"
1731ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                  app:colorAccent="@app:color/red" />)EOF");
1741ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1751ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    XmlReferenceLinker linker;
1761ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
1771ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
178467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    xml::Element* viewEl = xml::findRootElement(doc.get());
1791ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(viewEl, nullptr);
1801ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1811ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xml::Attribute* xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res-auto",
1821ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                                    u"colorAccent");
1831ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
1841ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
18564587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
18664587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x7f010000));
1871ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get());
1881ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(ref, nullptr);
1891ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->name);
1901ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->id);
1911ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_EQ(ref->id.value(), ResourceId(0x7f020001));
1921ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
1931ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1941ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(XmlReferenceLinkerTest, LinkViewWithShadowedPackageAlias) {
195467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
1961ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            <View xmlns:app="http://schemas.android.com/apk/res/android"
1971ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                  app:attr="@app:id/id">
1981ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski              <View xmlns:app="http://schemas.android.com/apk/res/com.app.test"
1991ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                    app:attr="@app:id/id"/>
2001ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            </View>)EOF");
2011ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2021ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    XmlReferenceLinker linker;
2031ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
2041ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
205467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    xml::Element* viewEl = xml::findRootElement(doc.get());
2061ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(viewEl, nullptr);
2071ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2081ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    // All attributes and references in this element should be referring to "android" (0x01).
2091ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xml::Attribute* xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/android",
2101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                                                    u"attr");
2111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
2121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
21364587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
21464587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x01010002));
2151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get());
2161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(ref, nullptr);
2171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->id);
2181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_EQ(ref->id.value(), ResourceId(0x01030000));
2191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_FALSE(viewEl->getChildElements().empty());
2211ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    viewEl = viewEl->getChildElements().front();
2221ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(viewEl, nullptr);
2231ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2241ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    // All attributes and references in this element should be referring to "com.app.test" (0x7f).
2251ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xmlAttr = viewEl->findAttribute(u"http://schemas.android.com/apk/res/com.app.test", u"attr");
2261ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
2271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
22864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
22964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x7f010002));
2301ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ref = valueCast<Reference>(xmlAttr->compiledValue.get());
2311ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(ref, nullptr);
2321ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->id);
2331ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_EQ(ref->id.value(), ResourceId(0x7f030000));
2341ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
2351ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2361ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(XmlReferenceLinkerTest, LinkViewWithLocalPackageAndAliasOfTheSameName) {
237467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF(
2381ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            <View xmlns:android="http://schemas.android.com/apk/res/com.app.test"
2391ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski                  android:attr="@id/id"/>)EOF");
2401ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2411ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    XmlReferenceLinker linker;
2421ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_TRUE(linker.consume(mContext.get(), doc.get()));
2431ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
244467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    xml::Element* viewEl = xml::findRootElement(doc.get());
2451ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(viewEl, nullptr);
2461ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2471ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    // All attributes and references in this element should be referring to "com.app.test" (0x7f).
2481ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    xml::Attribute* xmlAttr = viewEl->findAttribute(
2491ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski            u"http://schemas.android.com/apk/res/com.app.test", u"attr");
2501ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(xmlAttr, nullptr);
2511ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute);
25264587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    AAPT_ASSERT_TRUE(xmlAttr->compiledAttribute.value().id);
25364587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    EXPECT_EQ(xmlAttr->compiledAttribute.value().id.value(), ResourceId(0x7f010002));
2541ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    Reference* ref = valueCast<Reference>(xmlAttr->compiledValue.get());
2551ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ASSERT_NE(ref, nullptr);
2561ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    AAPT_ASSERT_TRUE(ref->id);
2571ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    EXPECT_EQ(ref->id.value(), ResourceId(0x7f030000));
2581ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
2591ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2601ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} // namespace aapt
261