blob: 0cf84736a081e8c9cdee4cdb67e83f6754ce97a2 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <ostream>
21#include <string>
22
Jeremy Meyer56f36e82022-05-20 20:35:42 +000023#include "ResourceValues.h"
24#include "androidfw/IDiagnostics.h"
25#include "test/Test.h"
26#include "util/Util.h"
27
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020028using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080029using ::android::StringPiece;
30using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070031using ::testing::NotNull;
Adam Lesinski71be7052017-12-12 16:48:07 -080032using ::testing::StrEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070033
Winson62ac8b52019-12-04 08:36:48 -080034using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
35
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036namespace aapt {
37
Adam Lesinskicc5609d2016-04-05 12:41:07 -070038TEST(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Ryan Mitchell1d008d12021-03-19 14:54:17 -070041 EXPECT_FALSE(
42 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey,there")).Build(),
43 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Ryan Mitchell1d008d12021-03-19 14:54:17 -070045 EXPECT_FALSE(
46 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey:there")).Build(),
47 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048}
49
Adam Lesinskib1afa072017-03-29 13:52:38 -070050TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
51 ResourceTable table;
52
Ryan Mitchell1d008d12021-03-19 14:54:17 -070053 EXPECT_TRUE(
54 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/heythere "))
55 .SetAllowMangled(true)
56 .Build(),
57 test::GetDiagnostics()));
Adam Lesinskib1afa072017-03-29 13:52:38 -070058}
59
Adam Lesinskicc5609d2016-04-05 12:41:07 -070060TEST(ResourceTableTest, AddOneResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 EXPECT_TRUE(table.AddResource(
Ryan Mitchell1d008d12021-03-19 14:54:17 -070064 NewResourceBuilder(test::ParseNameOrDie("android:attr/id"))
65 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build())
66 .Build(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskia45893a2017-05-30 15:19:02 -070069 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070}
71
Adam Lesinskicc5609d2016-04-05 12:41:07 -070072TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 ConfigDescription language_config;
76 memcpy(language_config.language, "pl", sizeof(language_config.language));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 EXPECT_TRUE(table.AddResource(
Ryan Mitchell1d008d12021-03-19 14:54:17 -070079 NewResourceBuilder(test::ParseNameOrDie("android:attr/layout_width"))
80 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 .Build(),
82 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Ryan Mitchell1d008d12021-03-19 14:54:17 -070084 EXPECT_TRUE(table.AddResource(
85 NewResourceBuilder(test::ParseNameOrDie("android:attr/id"))
86 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build())
87 .Build(),
88 test::GetDiagnostics()));
89
90 EXPECT_TRUE(table.AddResource(
91 NewResourceBuilder(test::ParseNameOrDie("android:string/ok"))
92 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build())
93 .Build(),
94 test::GetDiagnostics()));
95
96 EXPECT_TRUE(
97 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:string/ok"))
98 .SetValue(test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
99 .SetSource("test/path/file.xml", 20u)
100 .Build(),
101 language_config)
102 .Build(),
103 test::GetDiagnostics()));
104
Adam Lesinskia45893a2017-05-30 15:19:02 -0700105 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull());
106 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
107 EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull());
108 EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109}
110
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700111TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 ResourceTable table;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700113 ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo"))
114 .SetValue(test::AttributeBuilder().SetWeak(true).Build())
115 .Build(),
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800116 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700119 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700122 ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo"))
123 .SetValue(util::make_unique<Attribute>())
124 .Build(),
125 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700128 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 EXPECT_FALSE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130}
131
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800132TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) {
133 ResourceTable table;
134
135 const ResourceName name = test::ParseNameOrDie("android:attr/foo");
136 Attribute attr_one(android::ResTable_map::TYPE_STRING);
137 attr_one.SetWeak(true);
138 Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE);
139 attr_two.SetWeak(true);
140
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700141 ASSERT_TRUE(table.AddResource(
142 NewResourceBuilder(name).SetValue(util::make_unique<Attribute>(attr_one)).Build(),
143 test::GetDiagnostics()));
144 ASSERT_TRUE(table.AddResource(
145 NewResourceBuilder(name).SetValue(util::make_unique<Attribute>(attr_two)).Build(),
146 test::GetDiagnostics()));
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800147}
148
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700149TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 ResourceTable table;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700151 ASSERT_TRUE(table.AddResource(
152 NewResourceBuilder(test::ParseNameOrDie("android:string/foo"))
153 .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land"), "tablet")
154 .Build(),
155 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800156
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700157 ASSERT_TRUE(table.AddResource(
158 NewResourceBuilder(test::ParseNameOrDie("android:string/foo"))
159 .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land"), "phone")
160 .Build(),
161 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800162
Adam Lesinskia45893a2017-05-30 15:19:02 -0700163 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull());
164 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800165
Ryan Mitchell4382e442021-07-14 12:53:01 -0700166 std::optional<ResourceTable::SearchResult> sr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 table.FindResource(test::ParseNameOrDie("android:string/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700168 ASSERT_TRUE(sr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 std::vector<ResourceConfigValue*> values =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700170 sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 ASSERT_EQ(2u, values.size());
172 EXPECT_EQ(std::string("phone"), values[0]->product);
173 EXPECT_EQ(std::string("tablet"), values[1]->product);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800174}
175
Adam Lesinski71be7052017-12-12 16:48:07 -0800176static StringPiece LevelToString(Visibility::Level level) {
177 switch (level) {
178 case Visibility::Level::kPrivate:
179 return "private";
180 case Visibility::Level::kPublic:
181 return "private";
182 default:
183 return "undefined";
184 }
185}
186
187static ::testing::AssertionResult VisibilityOfResource(const ResourceTable& table,
188 const ResourceNameRef& name,
189 Visibility::Level level,
190 const StringPiece& comment) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700191 std::optional<ResourceTable::SearchResult> result = table.FindResource(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800192 if (!result) {
193 return ::testing::AssertionFailure() << "no resource '" << name << "' found in table";
194 }
195
196 const Visibility& visibility = result.value().entry->visibility;
197 if (visibility.level != level) {
198 return ::testing::AssertionFailure() << "expected visibility " << LevelToString(level)
199 << " but got " << LevelToString(visibility.level);
200 }
201
202 if (visibility.comment != comment) {
203 return ::testing::AssertionFailure() << "expected visibility comment '" << comment
204 << "' but got '" << visibility.comment << "'";
205 }
206 return ::testing::AssertionSuccess();
207}
208
209TEST(ResourceTableTest, SetVisibility) {
210 using Level = Visibility::Level;
211
212 ResourceTable table;
213 const ResourceName name = test::ParseNameOrDie("android:string/foo");
214
215 Visibility visibility;
216 visibility.level = Visibility::Level::kPrivate;
217 visibility.comment = "private";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700218 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
219 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800220 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
221
222 visibility.level = Visibility::Level::kUndefined;
223 visibility.comment = "undefined";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700224 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
225 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800226 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
227
228 visibility.level = Visibility::Level::kPublic;
229 visibility.comment = "public";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700230 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
231 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800232 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
233
234 visibility.level = Visibility::Level::kPrivate;
235 visibility.comment = "private";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700236 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
237 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800238 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
239}
240
241TEST(ResourceTableTest, SetAllowNew) {
242 ResourceTable table;
243 const ResourceName name = test::ParseNameOrDie("android:string/foo");
244
245 AllowNew allow_new;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700246 std::optional<ResourceTable::SearchResult> result;
Adam Lesinski71be7052017-12-12 16:48:07 -0800247
248 allow_new.comment = "first";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700249 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(),
250 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800251 result = table.FindResource(name);
252 ASSERT_TRUE(result);
253 ASSERT_TRUE(result.value().entry->allow_new);
254 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first"));
255
256 allow_new.comment = "second";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700257 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(),
258 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800259 result = table.FindResource(name);
260 ASSERT_TRUE(result);
261 ASSERT_TRUE(result.value().entry->allow_new);
262 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("second"));
263}
264
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800265TEST(ResourceTableTest, SetOverlayable) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800266 ResourceTable table;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000267 auto overlayable = std::make_shared<Overlayable>(
268 "Name", "overlay://theme", android::Source("res/values/overlayable.xml", 40));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800269 OverlayableItem overlayable_item(overlayable);
Winson62ac8b52019-12-04 08:36:48 -0800270 overlayable_item.policies |= PolicyFlags::PRODUCT_PARTITION;
271 overlayable_item.policies |= PolicyFlags::VENDOR_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800272 overlayable_item.comment = "comment";
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000273 overlayable_item.source = android::Source("res/values/overlayable.xml", 42);
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800274
Adam Lesinski71be7052017-12-12 16:48:07 -0800275 const ResourceName name = test::ParseNameOrDie("android:string/foo");
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700276 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
277 test::GetDiagnostics()));
Ryan Mitchell4382e442021-07-14 12:53:01 -0700278 std::optional<ResourceTable::SearchResult> search_result = table.FindResource(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800279
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800280 ASSERT_TRUE(search_result);
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800281 ASSERT_TRUE(search_result.value().entry->overlayable_item);
Adam Lesinski71be7052017-12-12 16:48:07 -0800282
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800283 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
284 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
285 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
286 EXPECT_THAT(result_overlayable_item.overlayable->source.path, Eq("res/values/overlayable.xml"));
287 EXPECT_THAT(result_overlayable_item.overlayable->source.line, 40);
Winson62ac8b52019-12-04 08:36:48 -0800288 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
289 | PolicyFlags::VENDOR_PARTITION));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800290 ASSERT_THAT(result_overlayable_item.comment, StrEq("comment"));
291 EXPECT_THAT(result_overlayable_item.source.path, Eq("res/values/overlayable.xml"));
292 EXPECT_THAT(result_overlayable_item.source.line, 42);
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700293}
294
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800295TEST(ResourceTableTest, SetMultipleOverlayableResources) {
296 ResourceTable table;
297
298 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
299 auto group = std::make_shared<Overlayable>("Name", "overlay://theme");
300 OverlayableItem overlayable(group);
Winson62ac8b52019-12-04 08:36:48 -0800301 overlayable.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700302 ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable).Build(),
303 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800304
305 const ResourceName bar = test::ParseNameOrDie("android:string/bar");
306 OverlayableItem overlayable2(group);
Winson62ac8b52019-12-04 08:36:48 -0800307 overlayable2.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700308 ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable2).Build(),
309 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800310
311 const ResourceName baz = test::ParseNameOrDie("android:string/baz");
312 OverlayableItem overlayable3(group);
Winson62ac8b52019-12-04 08:36:48 -0800313 overlayable3.policies = PolicyFlags::VENDOR_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700314 ASSERT_TRUE(table.AddResource(NewResourceBuilder(baz).SetOverlayable(overlayable3).Build(),
315 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800316}
317
318TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) {
319 ResourceTable table;
320
321 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
322 OverlayableItem overlayable_item(std::make_shared<Overlayable>("Name", "overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800323 overlayable_item.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700324 ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable_item).Build(),
325 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800326
327 const ResourceName bar = test::ParseNameOrDie("android:string/bar");
328 OverlayableItem overlayable_item2(std::make_shared<Overlayable>("Name2", "overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800329 overlayable_item2.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700330 ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable_item2).Build(),
331 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800332}
333
334TEST(ResourceTableTest, SetOverlayableSameResourcesFail) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700335 ResourceTable table;
336 const ResourceName name = test::ParseNameOrDie("android:string/foo");
337
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800338 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
339 OverlayableItem overlayable_item(overlayable);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700340 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
341 test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700342
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800343 OverlayableItem overlayable_item2(overlayable);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700344 ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(),
345 test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700346}
347
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800348TEST(ResourceTableTest, SetOverlayableSameResourcesDifferentNameFail) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700349 ResourceTable table;
350 const ResourceName name = test::ParseNameOrDie("android:string/foo");
351
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800352 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
353 OverlayableItem overlayable_item(overlayable);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700354 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
355 test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700356
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800357 auto overlayable2 = std::make_shared<Overlayable>("Other", "overlay://theme");
358 OverlayableItem overlayable_item2(overlayable2);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700359 ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(),
360 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800361}
362
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700363TEST(ResourceTableTest, ConflictingIds) {
364 ResourceTable table;
365 const ResourceName name = test::ParseNameOrDie("android:string/foo");
366 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetId(0x01010000).Build(),
367 test::GetDiagnostics()));
368 ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetId(0x01010001).Build(),
369 test::GetDiagnostics()));
370}
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700371
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700372TEST(ResourceTableTest, ConflictingIdsCreateEntry) {
373 ResourceTable table;
374 const ResourceName name = test::ParseNameOrDie("android:string/foo");
375 ASSERT_TRUE(table.AddResource(
376 NewResourceBuilder(name).SetId(0x01010000, OnIdConflict::CREATE_ENTRY).Build(),
377 test::GetDiagnostics()));
378 ASSERT_TRUE(table.AddResource(
379 NewResourceBuilder(name).SetId(0x01010001, OnIdConflict::CREATE_ENTRY).Build(),
380 test::GetDiagnostics()));
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700381
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700382 // Non-ambiguous query
383 ASSERT_TRUE(table.AddResource(
384 NewResourceBuilder(name).SetId(0x01010001).SetValue(std::make_unique<Id>()).Build(),
385 test::GetDiagnostics()));
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700386}
387
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388} // namespace aapt