blob: 0b7b1ce03a5240a401951d4c33c772d506dc16f9 [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 Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinski64587af2016-02-18 18:33:06 -080019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070021using ::android::ResTable_map;
22using ::testing::Eq;
23using ::testing::IsNull;
Adam Lesinskia45893a2017-05-30 15:19:02 -070024using ::testing::NotNull;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025
26namespace aapt {
27
28TEST(ReferenceLinkerTest, LinkSimpleReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 std::unique_ptr<ResourceTable> table =
30 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 .SetPackageId("com.app.test", 0x7f)
32 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 "com.app.test:string/bar")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 // Test use of local reference (w/o package name).
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 .AddReference("com.app.test:string/bar", ResourceId(0x7f020001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 "string/baz")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 .AddReference("com.app.test:string/baz", ResourceId(0x7f020002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 "android:string/ok")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 std::unique_ptr<IAaptContext> context =
44 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 .SetCompilationPackage("com.app.test")
46 .SetPackageId(0x7f)
47 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
48 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 .AddPublicSymbol("android:string/ok", ResourceId(0x01040034))
53 .Build())
54 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058
Adam Lesinskif34b6f42017-03-03 16:33:26 -080059 Reference* ref = test::GetValue<Reference>(table.get(), "com.app.test:string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070060 ASSERT_THAT(ref, NotNull());
61 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080062 EXPECT_EQ(ResourceId(0x7f020001), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 ref = test::GetValue<Reference>(table.get(), "com.app.test:string/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -070065 ASSERT_THAT(ref, NotNull());
66 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080067 EXPECT_EQ(ResourceId(0x7f020002), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 ref = test::GetValue<Reference>(table.get(), "com.app.test:string/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -070070 ASSERT_THAT(ref, NotNull());
71 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080072 EXPECT_EQ(ResourceId(0x01040034), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073}
74
75TEST(ReferenceLinkerTest, LinkStyleAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 std::unique_ptr<ResourceTable> table =
77 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 .SetPackageId("com.app.test", 0x7f)
79 .AddValue("com.app.test:style/Theme",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 .SetParent("android:style/Theme.Material")
82 .AddItem("android:attr/foo",
83 ResourceUtils::TryParseColor("#ff00ff"))
84 .AddItem("android:attr/bar", {} /* placeholder */)
85 .Build())
86 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 {
89 // We need to fill in the value for the attribute android:attr/bar after we
Adam Lesinskia45893a2017-05-30 15:19:02 -070090 // build the table, because we need access to the string pool.
Adam Lesinskif34b6f42017-03-03 16:33:26 -080091 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -070092 ASSERT_THAT(style, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 style->entries.back().value =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 util::make_unique<RawString>(table->string_pool.MakeRef("one|two"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 std::unique_ptr<IAaptContext> context =
98 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 .SetCompilationPackage("com.app.test")
100 .SetPackageId(0x7f)
101 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
102 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 .AddPublicSymbol("android:style/Theme.Material",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 ResourceId(0x01060000))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 .AddPublicSymbol("android:attr/foo", ResourceId(0x01010001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 .SetTypeMask(ResTable_map::TYPE_COLOR)
109 .Build())
110 .AddPublicSymbol("android:attr/bar", ResourceId(0x01010002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 .SetTypeMask(ResTable_map::TYPE_FLAGS)
113 .AddItem("one", 0x01)
114 .AddItem("two", 0x02)
115 .Build())
116 .Build())
117 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700123 ASSERT_THAT(style, NotNull());
124 ASSERT_TRUE(style->parent);
125 ASSERT_TRUE(style->parent.value().id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800126 EXPECT_EQ(ResourceId(0x01060000), style->parent.value().id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127
128 ASSERT_EQ(2u, style->entries.size());
129
Adam Lesinskia45893a2017-05-30 15:19:02 -0700130 ASSERT_TRUE(style->entries[0].key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800131 EXPECT_EQ(ResourceId(0x01010001), style->entries[0].key.id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700132 ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[0].value.get()), NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133
Adam Lesinskia45893a2017-05-30 15:19:02 -0700134 ASSERT_TRUE(style->entries[1].key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800135 EXPECT_EQ(ResourceId(0x01010002), style->entries[1].key.id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700136 ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[1].value.get()), NotNull());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137}
138
139TEST(ReferenceLinkerTest, LinkMangledReferencesAndAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 std::unique_ptr<IAaptContext> context =
141 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 .SetCompilationPackage("com.app.test")
143 .SetPackageId(0x7f)
144 .SetNameManglerPolicy(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 NameManglerPolicy{"com.app.test", {"com.android.support"}})
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 .AddPublicSymbol("com.app.test:attr/com.android.support$foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 ResourceId(0x7f010000),
150 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 .SetTypeMask(ResTable_map::TYPE_COLOR)
152 .Build())
153 .Build())
154 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 std::unique_ptr<ResourceTable> table =
157 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 .SetPackageId("com.app.test", 0x7f)
159 .AddValue("com.app.test:style/Theme", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 .AddItem("com.android.support:attr/foo",
162 ResourceUtils::TryParseColor("#ff0000"))
163 .Build())
164 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700170 ASSERT_THAT(style, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700172 ASSERT_TRUE(style->entries.front().key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800173 EXPECT_EQ(ResourceId(0x7f010000), style->entries.front().key.id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174}
175
Adam Lesinski467f1712015-11-16 17:35:44 -0800176TEST(ReferenceLinkerTest, FailToLinkPrivateSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 std::unique_ptr<ResourceTable> table =
178 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 .SetPackageId("com.app.test", 0x7f)
180 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 "android:string/hidden")
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 std::unique_ptr<IAaptContext> context =
185 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 .SetCompilationPackage("com.app.test")
187 .SetPackageId(0x7f)
188 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
189 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 .AddSymbol("android:string/hidden", ResourceId(0x01040034))
194 .Build())
195 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800196
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800199}
200
201TEST(ReferenceLinkerTest, FailToLinkPrivateMangledSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 std::unique_ptr<ResourceTable> table =
203 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 .SetPackageId("com.app.test", 0x7f)
205 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 "com.app.lib:string/hidden")
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800208
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 std::unique_ptr<IAaptContext> context =
210 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 .SetCompilationPackage("com.app.test")
212 .SetPackageId(0x7f)
213 .SetNameManglerPolicy(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 NameManglerPolicy{"com.app.test", {"com.app.lib"}})
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 .AddSymbol("com.app.test:string/com.app.lib$hidden",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 ResourceId(0x7f040034))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 .Build())
Adam Lesinski64587af2016-02-18 18:33:06 -0800222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800224
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800227}
228
229TEST(ReferenceLinkerTest, FailToLinkPrivateStyleAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 std::unique_ptr<ResourceTable> table =
231 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 .SetPackageId("com.app.test", 0x7f)
233 .AddValue("com.app.test:style/Theme",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 .AddItem("android:attr/hidden",
236 ResourceUtils::TryParseColor("#ff00ff"))
237 .Build())
238 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 std::unique_ptr<IAaptContext> context =
241 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 .SetCompilationPackage("com.app.test")
243 .SetPackageId(0x7f)
244 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
245 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 .AddSymbol("android:attr/hidden", ResourceId(0x01010001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 .SetTypeMask(android::ResTable_map::TYPE_COLOR)
252 .Build())
253 .Build())
254 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800255
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800258}
259
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800260TEST(ReferenceLinkerTest, AppsWithSamePackageButDifferentIdAreVisibleNonPublic) {
261 NameMangler mangler(NameManglerPolicy{"com.app.test"});
262 SymbolTable table(&mangler);
263 table.AppendSource(test::StaticSymbolSourceBuilder()
264 .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000))
265 .Build());
266
267 std::string error;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700268 const CallSite call_site{"com.app.test"};
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800269 const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveSymbolCheckVisibility(
Chris Warrington481f0272018-02-06 14:03:39 +0000270 *test::BuildReference("com.app.test:string/foo"), call_site, &table, false, &error);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700271 ASSERT_THAT(symbol, NotNull());
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800272 EXPECT_TRUE(error.empty());
273}
274
275TEST(ReferenceLinkerTest, AppsWithDifferentPackageCanNotUseEachOthersAttribute) {
276 NameMangler mangler(NameManglerPolicy{"com.app.ext"});
277 SymbolTable table(&mangler);
278 table.AppendSource(test::StaticSymbolSourceBuilder()
279 .AddSymbol("com.app.test:attr/foo", ResourceId(0x7f010000),
280 test::AttributeBuilder().Build())
281 .AddPublicSymbol("com.app.test:attr/public_foo", ResourceId(0x7f010001),
282 test::AttributeBuilder().Build())
283 .Build());
284
285 std::string error;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700286 const CallSite call_site{"com.app.ext"};
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800287
Chris Warrington481f0272018-02-06 14:03:39 +0000288 EXPECT_FALSE(ReferenceLinker::CompileXmlAttribute(*test::BuildReference("com.app.test:attr/foo"),
289 call_site, &table, false, &error));
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800290 EXPECT_FALSE(error.empty());
291
292 error = "";
Adam Lesinskia45893a2017-05-30 15:19:02 -0700293 ASSERT_TRUE(ReferenceLinker::CompileXmlAttribute(
Chris Warrington481f0272018-02-06 14:03:39 +0000294 *test::BuildReference("com.app.test:attr/public_foo"), call_site, &table, false, &error));
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800295 EXPECT_TRUE(error.empty());
296}
297
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700298TEST(ReferenceLinkerTest, ReferenceWithNoPackageUsesCallSitePackage) {
299 NameMangler mangler(NameManglerPolicy{"com.app.test"});
300 SymbolTable table(&mangler);
301 table.AppendSource(test::StaticSymbolSourceBuilder()
302 .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000))
303 .AddSymbol("com.app.lib:string/foo", ResourceId(0x7f010001))
304 .Build());
305
Chris Warrington481f0272018-02-06 14:03:39 +0000306 const SymbolTable::Symbol* s = ReferenceLinker::ResolveSymbol(
307 *test::BuildReference("string/foo"), CallSite{"com.app.test"}, &table, false);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700308 ASSERT_THAT(s, NotNull());
309 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010000)));
310
311 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"), CallSite{"com.app.lib"},
Chris Warrington481f0272018-02-06 14:03:39 +0000312 &table, false);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700313 ASSERT_THAT(s, NotNull());
314 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010001)));
315
316 EXPECT_THAT(ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"),
Chris Warrington481f0272018-02-06 14:03:39 +0000317 CallSite{"com.app.bad"}, &table, false),
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700318 IsNull());
319}
320
Chris Warrington481f0272018-02-06 14:03:39 +0000321TEST(ReferenceLinkerTest, AutomaticNamespace) {
322 NameMangler mangler(NameManglerPolicy{"com.example.thislib"});
323 SymbolTable table(&mangler);
324 table.AppendSource(
325 test::StaticSymbolSourceBuilder()
326 .AddSymbol("com.example.thislib:string/thislib_string", ResourceId(0x7f010006))
327 .AddSymbol("com.example.thislib:string/explicit_override_string", ResourceId(0x7f010007))
328 .Build());
329 // Lib2 is higher priority than lib1
330 table.AppendSource(
331 test::StaticSymbolSourceBuilder()
332 .AddSymbol("com.example.lib2:string/lib2_string", ResourceId(0x7f010003))
333 .AddSymbol("com.example.lib2:string/explicit_override_string", ResourceId(0x7f010004))
334 .AddSymbol("com.example.lib2:string/implicit_override_string", ResourceId(0x7f010005))
335 .Build());
336 table.AppendSource(
337 test::StaticSymbolSourceBuilder()
338 .AddSymbol("com.example.lib1:string/explicit_override_string", ResourceId(0x7f010001))
339 .AddSymbol("com.example.lib1:string/implicit_override_string", ResourceId(0x7f010002))
340 .Build());
341
342 // Sanity test: Local references are still fine.
343 const SymbolTable::Symbol* s =
344 ReferenceLinker::ResolveSymbol(*test::BuildReference("string/thislib_string"),
345 CallSite{"com.example.thislib"}, &table, true);
346 ASSERT_THAT(s, NotNull());
347 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010006)));
348
349 // Local references are fine, even if clash with remote ones.
350 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/explicit_override_string"),
351 CallSite{"com.example.thislib"}, &table, true);
352 ASSERT_THAT(s, NotNull());
353 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010007)));
354
355 // An unqualified reference to lib2 is rewritten
356 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/lib2_string"),
357 CallSite{"com.example.thislib"}, &table, true);
358 ASSERT_THAT(s, NotNull());
359 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010003)));
360
361 // Qualified references are left alone.
362 s = ReferenceLinker::ResolveSymbol(
363 *test::BuildReference("com.example.lib2:string/explicit_override_string"),
364 CallSite{"com.example.thislib"}, &table, true);
365 ASSERT_THAT(s, NotNull());
366 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010004)));
367
368 // Implicit overrides respect priority ordering.
369 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/implicit_override_string"),
370 CallSite{"com.example.thislib"}, &table, true);
371 ASSERT_THAT(s, NotNull());
372 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010005)));
373
374 //
375}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376} // namespace aapt