blob: 228c5bd743a08009b9d928ce3759f3328ddfd734 [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 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 "com.app.test:string/bar")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 // Test use of local reference (w/o package name).
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 .AddReference("com.app.test:string/bar", ResourceId(0x7f020001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 "string/baz")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 .AddReference("com.app.test:string/baz", ResourceId(0x7f020002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 "android:string/ok")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 std::unique_ptr<IAaptContext> context =
43 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 .SetCompilationPackage("com.app.test")
45 .SetPackageId(0x7f)
46 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
47 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 .AddPublicSymbol("android:string/ok", ResourceId(0x01040034))
52 .Build())
53 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057
Adam Lesinskif34b6f42017-03-03 16:33:26 -080058 Reference* ref = test::GetValue<Reference>(table.get(), "com.app.test:string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070059 ASSERT_THAT(ref, NotNull());
60 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080061 EXPECT_EQ(ResourceId(0x7f020001), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 ref = test::GetValue<Reference>(table.get(), "com.app.test:string/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -070064 ASSERT_THAT(ref, NotNull());
65 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080066 EXPECT_EQ(ResourceId(0x7f020002), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ref = test::GetValue<Reference>(table.get(), "com.app.test:string/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -070069 ASSERT_THAT(ref, NotNull());
70 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080071 EXPECT_EQ(ResourceId(0x01040034), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072}
73
74TEST(ReferenceLinkerTest, LinkStyleAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 std::unique_ptr<ResourceTable> table =
76 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 .AddValue("com.app.test:style/Theme",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 .SetParent("android:style/Theme.Material")
80 .AddItem("android:attr/foo",
81 ResourceUtils::TryParseColor("#ff00ff"))
82 .AddItem("android:attr/bar", {} /* placeholder */)
83 .Build())
84 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 {
87 // We need to fill in the value for the attribute android:attr/bar after we
Adam Lesinskia45893a2017-05-30 15:19:02 -070088 // build the table, because we need access to the string pool.
Adam Lesinskif34b6f42017-03-03 16:33:26 -080089 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -070090 ASSERT_THAT(style, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 style->entries.back().value =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 util::make_unique<RawString>(table->string_pool.MakeRef("one|two"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::unique_ptr<IAaptContext> context =
96 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 .SetCompilationPackage("com.app.test")
98 .SetPackageId(0x7f)
99 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
100 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 .AddPublicSymbol("android:style/Theme.Material",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceId(0x01060000))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 .AddPublicSymbol("android:attr/foo", ResourceId(0x01010001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 .SetTypeMask(ResTable_map::TYPE_COLOR)
107 .Build())
108 .AddPublicSymbol("android:attr/bar", ResourceId(0x01010002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 .SetTypeMask(ResTable_map::TYPE_FLAGS)
111 .AddItem("one", 0x01)
112 .AddItem("two", 0x02)
113 .Build())
114 .Build())
115 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700121 ASSERT_THAT(style, NotNull());
122 ASSERT_TRUE(style->parent);
123 ASSERT_TRUE(style->parent.value().id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800124 EXPECT_EQ(ResourceId(0x01060000), style->parent.value().id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125
126 ASSERT_EQ(2u, style->entries.size());
127
Adam Lesinskia45893a2017-05-30 15:19:02 -0700128 ASSERT_TRUE(style->entries[0].key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800129 EXPECT_EQ(ResourceId(0x01010001), style->entries[0].key.id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700130 ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[0].value.get()), NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131
Adam Lesinskia45893a2017-05-30 15:19:02 -0700132 ASSERT_TRUE(style->entries[1].key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800133 EXPECT_EQ(ResourceId(0x01010002), style->entries[1].key.id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700134 ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[1].value.get()), NotNull());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135}
136
137TEST(ReferenceLinkerTest, LinkMangledReferencesAndAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 std::unique_ptr<IAaptContext> context =
139 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 .SetCompilationPackage("com.app.test")
141 .SetPackageId(0x7f)
142 .SetNameManglerPolicy(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 NameManglerPolicy{"com.app.test", {"com.android.support"}})
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 .AddPublicSymbol("com.app.test:attr/com.android.support$foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 ResourceId(0x7f010000),
148 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 .SetTypeMask(ResTable_map::TYPE_COLOR)
150 .Build())
151 .Build())
152 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 std::unique_ptr<ResourceTable> table =
155 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 .AddValue("com.app.test:style/Theme", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 .AddItem("com.android.support:attr/foo",
159 ResourceUtils::TryParseColor("#ff0000"))
160 .Build())
161 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700167 ASSERT_THAT(style, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700169 ASSERT_TRUE(style->entries.front().key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800170 EXPECT_EQ(ResourceId(0x7f010000), style->entries.front().key.id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171}
172
Adam Lesinski467f1712015-11-16 17:35:44 -0800173TEST(ReferenceLinkerTest, FailToLinkPrivateSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 std::unique_ptr<ResourceTable> table =
175 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 "android:string/hidden")
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 std::unique_ptr<IAaptContext> context =
181 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 .SetCompilationPackage("com.app.test")
183 .SetPackageId(0x7f)
184 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
185 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 .AddSymbol("android:string/hidden", ResourceId(0x01040034))
190 .Build())
191 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800192
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800195}
196
197TEST(ReferenceLinkerTest, FailToLinkPrivateMangledSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 std::unique_ptr<ResourceTable> table =
199 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 "com.app.lib:string/hidden")
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 std::unique_ptr<IAaptContext> context =
205 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 .SetCompilationPackage("com.app.test")
207 .SetPackageId(0x7f)
208 .SetNameManglerPolicy(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 NameManglerPolicy{"com.app.test", {"com.app.lib"}})
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 .AddSymbol("com.app.test:string/com.app.lib$hidden",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 ResourceId(0x7f040034))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 .Build())
Adam Lesinski64587af2016-02-18 18:33:06 -0800217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800222}
223
224TEST(ReferenceLinkerTest, FailToLinkPrivateStyleAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 std::unique_ptr<ResourceTable> table =
226 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 .AddValue("com.app.test:style/Theme",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 .AddItem("android:attr/hidden",
230 ResourceUtils::TryParseColor("#ff00ff"))
231 .Build())
232 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 std::unique_ptr<IAaptContext> context =
235 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 .SetCompilationPackage("com.app.test")
237 .SetPackageId(0x7f)
238 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
239 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 .AddSymbol("android:attr/hidden", ResourceId(0x01010001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 .SetTypeMask(android::ResTable_map::TYPE_COLOR)
246 .Build())
247 .Build())
248 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800252}
253
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800254TEST(ReferenceLinkerTest, AppsWithSamePackageButDifferentIdAreVisibleNonPublic) {
255 NameMangler mangler(NameManglerPolicy{"com.app.test"});
256 SymbolTable table(&mangler);
257 table.AppendSource(test::StaticSymbolSourceBuilder()
258 .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000))
259 .Build());
260
261 std::string error;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700262 const CallSite call_site{"com.app.test"};
Udam Sainib228df32019-06-18 16:50:34 -0700263 std::unique_ptr<IAaptContext> context =
264 test::ContextBuilder()
265 .SetCompilationPackage("com.app.test")
266 .SetPackageId(0x7f)
267 .Build();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800268 const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveSymbolCheckVisibility(
Udam Sainib228df32019-06-18 16:50:34 -0700269 *test::BuildReference("com.app.test:string/foo"), call_site, context.get(), &table, &error);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700270 ASSERT_THAT(symbol, NotNull());
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800271 EXPECT_TRUE(error.empty());
272}
273
274TEST(ReferenceLinkerTest, AppsWithDifferentPackageCanNotUseEachOthersAttribute) {
275 NameMangler mangler(NameManglerPolicy{"com.app.ext"});
276 SymbolTable table(&mangler);
277 table.AppendSource(test::StaticSymbolSourceBuilder()
278 .AddSymbol("com.app.test:attr/foo", ResourceId(0x7f010000),
279 test::AttributeBuilder().Build())
280 .AddPublicSymbol("com.app.test:attr/public_foo", ResourceId(0x7f010001),
281 test::AttributeBuilder().Build())
282 .Build());
Udam Sainib228df32019-06-18 16:50:34 -0700283 std::unique_ptr<IAaptContext> context =
284 test::ContextBuilder()
285 .SetCompilationPackage("com.app.ext")
286 .SetPackageId(0x7f)
287 .Build();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800288
289 std::string error;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700290 const CallSite call_site{"com.app.ext"};
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800291
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000292 EXPECT_FALSE(ReferenceLinker::CompileXmlAttribute(
Udam Sainib228df32019-06-18 16:50:34 -0700293 *test::BuildReference("com.app.test:attr/foo"), call_site, context.get(), &table, &error));
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800294 EXPECT_FALSE(error.empty());
295
296 error = "";
Adam Lesinskia45893a2017-05-30 15:19:02 -0700297 ASSERT_TRUE(ReferenceLinker::CompileXmlAttribute(
Udam Sainib228df32019-06-18 16:50:34 -0700298 *test::BuildReference("com.app.test:attr/public_foo"), call_site, context.get(), &table,
299 &error));
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800300 EXPECT_TRUE(error.empty());
301}
302
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700303TEST(ReferenceLinkerTest, ReferenceWithNoPackageUsesCallSitePackage) {
304 NameMangler mangler(NameManglerPolicy{"com.app.test"});
305 SymbolTable table(&mangler);
306 table.AppendSource(test::StaticSymbolSourceBuilder()
307 .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000))
308 .AddSymbol("com.app.lib:string/foo", ResourceId(0x7f010001))
309 .Build());
Udam Sainib228df32019-06-18 16:50:34 -0700310 std::unique_ptr<IAaptContext> context =
311 test::ContextBuilder()
312 .SetCompilationPackage("com.app.test")
313 .SetPackageId(0x7f)
314 .Build();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700315
Chris Warrington58e2fbf2018-07-23 14:12:20 +0000316 const SymbolTable::Symbol* s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"),
Udam Sainib228df32019-06-18 16:50:34 -0700317 CallSite{"com.app.test"},
318 context.get(), &table);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700319 ASSERT_THAT(s, NotNull());
320 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010000)));
321
322 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"), CallSite{"com.app.lib"},
Udam Sainib228df32019-06-18 16:50:34 -0700323 context.get(), &table);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700324 ASSERT_THAT(s, NotNull());
325 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010001)));
326
327 EXPECT_THAT(ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"),
Udam Sainib228df32019-06-18 16:50:34 -0700328 CallSite{"com.app.bad"}, context.get(), &table),
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700329 IsNull());
330}
331
Udam Sainib228df32019-06-18 16:50:34 -0700332TEST(ReferenceLinkerTest, ReferenceSymbolFromOtherSplit) {
333 NameMangler mangler(NameManglerPolicy{"com.app.test"});
334 SymbolTable table(&mangler);
335 table.AppendSource(test::StaticSymbolSourceBuilder()
336 .AddSymbol("com.app.test.feature:string/bar", ResourceId(0x80010000))
337 .Build());
338 std::set<std::string> split_name_dependencies;
339 split_name_dependencies.insert("feature");
340 std::unique_ptr<IAaptContext> context =
341 test::ContextBuilder()
342 .SetCompilationPackage("com.app.test")
343 .SetPackageId(0x81)
344 .SetSplitNameDependencies(split_name_dependencies)
345 .Build();
346
347 const SymbolTable::Symbol* s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/bar"),
348 CallSite{"com.app.test"},
349 context.get(), &table);
350 ASSERT_THAT(s, NotNull());
351 EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x80010000)));
352
353 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"), CallSite{"com.app.lib"},
354 context.get(), &table);
355 EXPECT_THAT(s, IsNull());
356
357 context =
358 test::ContextBuilder()
359 .SetCompilationPackage("com.app.test")
360 .SetPackageId(0x81)
361 .Build();
362 s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/bar"),CallSite{"com.app.test"},
363 context.get(), &table);
364
365 EXPECT_THAT(s, IsNull());
366}
367
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368} // namespace aapt