blob: 5c1511f1403307d6d56875be1dfa3e61f5752da8 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinski467f1712015-11-16 17:35:44 -080017#include "link/ReferenceLinker.h"
Adam Lesinski64587af2016-02-18 18:33:06 -080018#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070019
Adam Lesinski64587af2016-02-18 18:33:06 -080020using android::ResTable_map;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021
22namespace aapt {
23
24TEST(ReferenceLinkerTest, LinkSimpleReferences) {
25 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -070026 .setPackageId("com.app.test", 0x7f)
Adam Lesinski58a20a62016-07-25 17:56:58 -070027 .addReference("com.app.test:string/foo", ResourceId(0x7f020000),
28 "com.app.test:string/bar")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
30 // Test use of local reference (w/o package name).
Adam Lesinski58a20a62016-07-25 17:56:58 -070031 .addReference("com.app.test:string/bar", ResourceId(0x7f020001), "string/baz")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinski58a20a62016-07-25 17:56:58 -070033 .addReference("com.app.test:string/baz", ResourceId(0x7f020002),
34 "android:string/ok")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035 .build();
36
37 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -070038 .setCompilationPackage("com.app.test")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039 .setPackageId(0x7f)
Adam Lesinskid0f116b2016-07-08 15:00:32 -070040 .setNameManglerPolicy(NameManglerPolicy{ "com.app.test" })
Adam Lesinski64587af2016-02-18 18:33:06 -080041 .addSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
42 .addSymbolSource(test::StaticSymbolSourceBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -070043 .addPublicSymbol("android:string/ok", ResourceId(0x01040034))
Adam Lesinski64587af2016-02-18 18:33:06 -080044 .build())
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045 .build();
46
47 ReferenceLinker linker;
48 ASSERT_TRUE(linker.consume(context.get(), table.get()));
49
Adam Lesinski58a20a62016-07-25 17:56:58 -070050 Reference* ref = test::getValue<Reference>(table.get(), "com.app.test:string/foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051 ASSERT_NE(ref, nullptr);
52 AAPT_ASSERT_TRUE(ref->id);
53 EXPECT_EQ(ref->id.value(), ResourceId(0x7f020001));
54
Adam Lesinski58a20a62016-07-25 17:56:58 -070055 ref = test::getValue<Reference>(table.get(), "com.app.test:string/bar");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 ASSERT_NE(ref, nullptr);
57 AAPT_ASSERT_TRUE(ref->id);
58 EXPECT_EQ(ref->id.value(), ResourceId(0x7f020002));
59
Adam Lesinski58a20a62016-07-25 17:56:58 -070060 ref = test::getValue<Reference>(table.get(), "com.app.test:string/baz");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061 ASSERT_NE(ref, nullptr);
62 AAPT_ASSERT_TRUE(ref->id);
63 EXPECT_EQ(ref->id.value(), ResourceId(0x01040034));
64}
65
66TEST(ReferenceLinkerTest, LinkStyleAttributes) {
67 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -070068 .setPackageId("com.app.test", 0x7f)
Adam Lesinski58a20a62016-07-25 17:56:58 -070069 .addValue("com.app.test:style/Theme", test::StyleBuilder()
70 .setParent("android:style/Theme.Material")
71 .addItem("android:attr/foo", ResourceUtils::tryParseColor("#ff00ff"))
72 .addItem("android:attr/bar", {} /* placeholder */)
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 .build())
74 .build();
75
76 {
77 // We need to fill in the value for the attribute android:attr/bar after we build the
78 // table, because we need access to the string pool.
Adam Lesinski58a20a62016-07-25 17:56:58 -070079 Style* style = test::getValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 ASSERT_NE(style, nullptr);
81 style->entries.back().value = util::make_unique<RawString>(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070082 table->stringPool.makeRef("one|two"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 }
84
85 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -070086 .setCompilationPackage("com.app.test")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 .setPackageId(0x7f)
Adam Lesinskid0f116b2016-07-08 15:00:32 -070088 .setNameManglerPolicy(NameManglerPolicy{ "com.app.test" })
Adam Lesinski64587af2016-02-18 18:33:06 -080089 .addSymbolSource(test::StaticSymbolSourceBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -070090 .addPublicSymbol("android:style/Theme.Material",
Adam Lesinski64587af2016-02-18 18:33:06 -080091 ResourceId(0x01060000))
Adam Lesinski58a20a62016-07-25 17:56:58 -070092 .addPublicSymbol("android:attr/foo", ResourceId(0x01010001),
Adam Lesinski64587af2016-02-18 18:33:06 -080093 test::AttributeBuilder()
94 .setTypeMask(ResTable_map::TYPE_COLOR)
95 .build())
Adam Lesinski58a20a62016-07-25 17:56:58 -070096 .addPublicSymbol("android:attr/bar", ResourceId(0x01010002),
Adam Lesinski64587af2016-02-18 18:33:06 -080097 test::AttributeBuilder()
98 .setTypeMask(ResTable_map::TYPE_FLAGS)
Adam Lesinskid0f116b2016-07-08 15:00:32 -070099 .addItem("one", 0x01)
100 .addItem("two", 0x02)
Adam Lesinski64587af2016-02-18 18:33:06 -0800101 .build())
102 .build())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 .build();
104
105 ReferenceLinker linker;
106 ASSERT_TRUE(linker.consume(context.get(), table.get()));
107
Adam Lesinski58a20a62016-07-25 17:56:58 -0700108 Style* style = test::getValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 ASSERT_NE(style, nullptr);
110 AAPT_ASSERT_TRUE(style->parent);
111 AAPT_ASSERT_TRUE(style->parent.value().id);
112 EXPECT_EQ(style->parent.value().id.value(), ResourceId(0x01060000));
113
114 ASSERT_EQ(2u, style->entries.size());
115
116 AAPT_ASSERT_TRUE(style->entries[0].key.id);
117 EXPECT_EQ(style->entries[0].key.id.value(), ResourceId(0x01010001));
118 ASSERT_NE(valueCast<BinaryPrimitive>(style->entries[0].value.get()), nullptr);
119
120 AAPT_ASSERT_TRUE(style->entries[1].key.id);
121 EXPECT_EQ(style->entries[1].key.id.value(), ResourceId(0x01010002));
122 ASSERT_NE(valueCast<BinaryPrimitive>(style->entries[1].value.get()), nullptr);
123}
124
125TEST(ReferenceLinkerTest, LinkMangledReferencesAndAttributes) {
126 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700127 .setCompilationPackage("com.app.test")
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 .setPackageId(0x7f)
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700129 .setNameManglerPolicy(NameManglerPolicy{ "com.app.test", { "com.android.support" } })
Adam Lesinski64587af2016-02-18 18:33:06 -0800130 .addSymbolSource(test::StaticSymbolSourceBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -0700131 .addPublicSymbol("com.app.test:attr/com.android.support$foo",
Adam Lesinski64587af2016-02-18 18:33:06 -0800132 ResourceId(0x7f010000),
133 test::AttributeBuilder()
134 .setTypeMask(ResTable_map::TYPE_COLOR)
135 .build())
136 .build())
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137 .build();
138
139 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700140 .setPackageId("com.app.test", 0x7f)
Adam Lesinski58a20a62016-07-25 17:56:58 -0700141 .addValue("com.app.test:style/Theme", ResourceId(0x7f020000),
142 test::StyleBuilder().addItem("com.android.support:attr/foo",
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700143 ResourceUtils::tryParseColor("#ff0000"))
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144 .build())
145 .build();
146
147 ReferenceLinker linker;
148 ASSERT_TRUE(linker.consume(context.get(), table.get()));
149
Adam Lesinski58a20a62016-07-25 17:56:58 -0700150 Style* style = test::getValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 ASSERT_NE(style, nullptr);
152 ASSERT_EQ(1u, style->entries.size());
153 AAPT_ASSERT_TRUE(style->entries.front().key.id);
154 EXPECT_EQ(style->entries.front().key.id.value(), ResourceId(0x7f010000));
155}
156
Adam Lesinski467f1712015-11-16 17:35:44 -0800157TEST(ReferenceLinkerTest, FailToLinkPrivateSymbols) {
158 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700159 .setPackageId("com.app.test", 0x7f)
Adam Lesinski58a20a62016-07-25 17:56:58 -0700160 .addReference("com.app.test:string/foo", ResourceId(0x7f020000),
161 "android:string/hidden")
Adam Lesinski467f1712015-11-16 17:35:44 -0800162 .build();
163
164 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700165 .setCompilationPackage("com.app.test")
Adam Lesinski467f1712015-11-16 17:35:44 -0800166 .setPackageId(0x7f)
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700167 .setNameManglerPolicy(NameManglerPolicy{ "com.app.test" })
Adam Lesinski64587af2016-02-18 18:33:06 -0800168 .addSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
169 .addSymbolSource(test::StaticSymbolSourceBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -0700170 .addSymbol("android:string/hidden", ResourceId(0x01040034))
Adam Lesinski64587af2016-02-18 18:33:06 -0800171 .build())
Adam Lesinski467f1712015-11-16 17:35:44 -0800172 .build();
173
174 ReferenceLinker linker;
175 ASSERT_FALSE(linker.consume(context.get(), table.get()));
176}
177
178TEST(ReferenceLinkerTest, FailToLinkPrivateMangledSymbols) {
179 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700180 .setPackageId("com.app.test", 0x7f)
Adam Lesinski58a20a62016-07-25 17:56:58 -0700181 .addReference("com.app.test:string/foo", ResourceId(0x7f020000),
182 "com.app.lib:string/hidden")
Adam Lesinski467f1712015-11-16 17:35:44 -0800183 .build();
184
185 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700186 .setCompilationPackage("com.app.test")
Adam Lesinski467f1712015-11-16 17:35:44 -0800187 .setPackageId(0x7f)
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700188 .setNameManglerPolicy(NameManglerPolicy{ "com.app.test", { "com.app.lib" } })
Adam Lesinski64587af2016-02-18 18:33:06 -0800189 .addSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
190 .addSymbolSource(test::StaticSymbolSourceBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -0700191 .addSymbol("com.app.test:string/com.app.lib$hidden",
Adam Lesinski64587af2016-02-18 18:33:06 -0800192 ResourceId(0x7f040034))
193 .build())
194
Adam Lesinski467f1712015-11-16 17:35:44 -0800195 .build();
196
197 ReferenceLinker linker;
198 ASSERT_FALSE(linker.consume(context.get(), table.get()));
199}
200
201TEST(ReferenceLinkerTest, FailToLinkPrivateStyleAttributes) {
202 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700203 .setPackageId("com.app.test", 0x7f)
Adam Lesinski58a20a62016-07-25 17:56:58 -0700204 .addValue("com.app.test:style/Theme", test::StyleBuilder()
205 .addItem("android:attr/hidden", ResourceUtils::tryParseColor("#ff00ff"))
Adam Lesinski467f1712015-11-16 17:35:44 -0800206 .build())
207 .build();
208
209 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700210 .setCompilationPackage("com.app.test")
Adam Lesinski467f1712015-11-16 17:35:44 -0800211 .setPackageId(0x7f)
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700212 .setNameManglerPolicy(NameManglerPolicy{ "com.app.test" })
Adam Lesinski64587af2016-02-18 18:33:06 -0800213 .addSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get()))
214 .addSymbolSource(test::StaticSymbolSourceBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -0700215 .addSymbol("android:attr/hidden", ResourceId(0x01010001),
Adam Lesinski64587af2016-02-18 18:33:06 -0800216 test::AttributeBuilder()
217 .setTypeMask(
218 android::ResTable_map::TYPE_COLOR)
219 .build())
220 .build())
Adam Lesinski467f1712015-11-16 17:35:44 -0800221 .build();
222
223 ReferenceLinker linker;
224 ASSERT_FALSE(linker.consume(context.get(), table.get()));
225}
226
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227} // namespace aapt