blob: de73d2c203e4405f474dd916d75d85152d1d97b7 [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 Lesinskicacb28f2016-10-19 12:18:14 -070018#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070020#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "util/Util.h"
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <ostream>
25#include <string>
26
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020027using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080028using ::android::StringPiece;
29using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070030using ::testing::NotNull;
Adam Lesinski71be7052017-12-12 16:48:07 -080031using ::testing::StrEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070032
Winson62ac8b52019-12-04 08:36:48 -080033using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
34
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035namespace aapt {
36
Adam Lesinskicc5609d2016-04-05 12:41:07 -070037TEST(ResourceTableTest, FailToAddResourceWithBadName) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Ryan Mitchell1d008d12021-03-19 14:54:17 -070040 EXPECT_FALSE(
41 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey,there")).Build(),
42 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Ryan Mitchell1d008d12021-03-19 14:54:17 -070044 EXPECT_FALSE(
45 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey:there")).Build(),
46 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047}
48
Adam Lesinskib1afa072017-03-29 13:52:38 -070049TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
50 ResourceTable table;
51
Ryan Mitchell1d008d12021-03-19 14:54:17 -070052 EXPECT_TRUE(
53 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/heythere "))
54 .SetAllowMangled(true)
55 .Build(),
56 test::GetDiagnostics()));
Adam Lesinskib1afa072017-03-29 13:52:38 -070057}
58
Adam Lesinskicc5609d2016-04-05 12:41:07 -070059TEST(ResourceTableTest, AddOneResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 EXPECT_TRUE(table.AddResource(
Ryan Mitchell1d008d12021-03-19 14:54:17 -070063 NewResourceBuilder(test::ParseNameOrDie("android:attr/id"))
64 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build())
65 .Build(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
Adam Lesinskia45893a2017-05-30 15:19:02 -070068 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069}
70
Adam Lesinskicc5609d2016-04-05 12:41:07 -070071TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 ConfigDescription language_config;
75 memcpy(language_config.language, "pl", sizeof(language_config.language));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 EXPECT_TRUE(table.AddResource(
Ryan Mitchell1d008d12021-03-19 14:54:17 -070078 NewResourceBuilder(test::ParseNameOrDie("android:attr/layout_width"))
79 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 .Build(),
81 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Ryan Mitchell1d008d12021-03-19 14:54:17 -070083 EXPECT_TRUE(table.AddResource(
84 NewResourceBuilder(test::ParseNameOrDie("android:attr/id"))
85 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build())
86 .Build(),
87 test::GetDiagnostics()));
88
89 EXPECT_TRUE(table.AddResource(
90 NewResourceBuilder(test::ParseNameOrDie("android:string/ok"))
91 .SetValue(test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build())
92 .Build(),
93 test::GetDiagnostics()));
94
95 EXPECT_TRUE(
96 table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:string/ok"))
97 .SetValue(test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
98 .SetSource("test/path/file.xml", 20u)
99 .Build(),
100 language_config)
101 .Build(),
102 test::GetDiagnostics()));
103
Adam Lesinskia45893a2017-05-30 15:19:02 -0700104 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull());
105 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
106 EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull());
107 EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108}
109
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700110TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 ResourceTable table;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700112 ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo"))
113 .SetValue(test::AttributeBuilder().SetWeak(true).Build())
114 .Build(),
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800115 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700118 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700121 ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo"))
122 .SetValue(util::make_unique<Attribute>())
123 .Build(),
124 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700127 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 EXPECT_FALSE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129}
130
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800131TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) {
132 ResourceTable table;
133
134 const ResourceName name = test::ParseNameOrDie("android:attr/foo");
135 Attribute attr_one(android::ResTable_map::TYPE_STRING);
136 attr_one.SetWeak(true);
137 Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE);
138 attr_two.SetWeak(true);
139
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700140 ASSERT_TRUE(table.AddResource(
141 NewResourceBuilder(name).SetValue(util::make_unique<Attribute>(attr_one)).Build(),
142 test::GetDiagnostics()));
143 ASSERT_TRUE(table.AddResource(
144 NewResourceBuilder(name).SetValue(util::make_unique<Attribute>(attr_two)).Build(),
145 test::GetDiagnostics()));
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800146}
147
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700148TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 ResourceTable table;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700150 ASSERT_TRUE(table.AddResource(
151 NewResourceBuilder(test::ParseNameOrDie("android:string/foo"))
152 .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land"), "tablet")
153 .Build(),
154 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800155
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700156 ASSERT_TRUE(table.AddResource(
157 NewResourceBuilder(test::ParseNameOrDie("android:string/foo"))
158 .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land"), "phone")
159 .Build(),
160 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800161
Adam Lesinskia45893a2017-05-30 15:19:02 -0700162 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull());
163 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800164
Ryan Mitchell4382e442021-07-14 12:53:01 -0700165 std::optional<ResourceTable::SearchResult> sr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 table.FindResource(test::ParseNameOrDie("android:string/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700167 ASSERT_TRUE(sr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 std::vector<ResourceConfigValue*> values =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700169 sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 ASSERT_EQ(2u, values.size());
171 EXPECT_EQ(std::string("phone"), values[0]->product);
172 EXPECT_EQ(std::string("tablet"), values[1]->product);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800173}
174
Adam Lesinski71be7052017-12-12 16:48:07 -0800175static StringPiece LevelToString(Visibility::Level level) {
176 switch (level) {
177 case Visibility::Level::kPrivate:
178 return "private";
179 case Visibility::Level::kPublic:
180 return "private";
181 default:
182 return "undefined";
183 }
184}
185
186static ::testing::AssertionResult VisibilityOfResource(const ResourceTable& table,
187 const ResourceNameRef& name,
188 Visibility::Level level,
189 const StringPiece& comment) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700190 std::optional<ResourceTable::SearchResult> result = table.FindResource(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800191 if (!result) {
192 return ::testing::AssertionFailure() << "no resource '" << name << "' found in table";
193 }
194
195 const Visibility& visibility = result.value().entry->visibility;
196 if (visibility.level != level) {
197 return ::testing::AssertionFailure() << "expected visibility " << LevelToString(level)
198 << " but got " << LevelToString(visibility.level);
199 }
200
201 if (visibility.comment != comment) {
202 return ::testing::AssertionFailure() << "expected visibility comment '" << comment
203 << "' but got '" << visibility.comment << "'";
204 }
205 return ::testing::AssertionSuccess();
206}
207
208TEST(ResourceTableTest, SetVisibility) {
209 using Level = Visibility::Level;
210
211 ResourceTable table;
212 const ResourceName name = test::ParseNameOrDie("android:string/foo");
213
214 Visibility visibility;
215 visibility.level = Visibility::Level::kPrivate;
216 visibility.comment = "private";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700217 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
218 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800219 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
220
221 visibility.level = Visibility::Level::kUndefined;
222 visibility.comment = "undefined";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700223 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
224 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800225 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
226
227 visibility.level = Visibility::Level::kPublic;
228 visibility.comment = "public";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700229 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
230 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800231 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
232
233 visibility.level = Visibility::Level::kPrivate;
234 visibility.comment = "private";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700235 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(),
236 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800237 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
238}
239
240TEST(ResourceTableTest, SetAllowNew) {
241 ResourceTable table;
242 const ResourceName name = test::ParseNameOrDie("android:string/foo");
243
244 AllowNew allow_new;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700245 std::optional<ResourceTable::SearchResult> result;
Adam Lesinski71be7052017-12-12 16:48:07 -0800246
247 allow_new.comment = "first";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700248 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(),
249 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800250 result = table.FindResource(name);
251 ASSERT_TRUE(result);
252 ASSERT_TRUE(result.value().entry->allow_new);
253 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first"));
254
255 allow_new.comment = "second";
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700256 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(),
257 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800258 result = table.FindResource(name);
259 ASSERT_TRUE(result);
260 ASSERT_TRUE(result.value().entry->allow_new);
261 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("second"));
262}
263
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800264TEST(ResourceTableTest, SetOverlayable) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800265 ResourceTable table;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800266 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme",
267 Source("res/values/overlayable.xml", 40));
268 OverlayableItem overlayable_item(overlayable);
Winson62ac8b52019-12-04 08:36:48 -0800269 overlayable_item.policies |= PolicyFlags::PRODUCT_PARTITION;
270 overlayable_item.policies |= PolicyFlags::VENDOR_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800271 overlayable_item.comment = "comment";
272 overlayable_item.source = Source("res/values/overlayable.xml", 42);
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800273
Adam Lesinski71be7052017-12-12 16:48:07 -0800274 const ResourceName name = test::ParseNameOrDie("android:string/foo");
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700275 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
276 test::GetDiagnostics()));
Ryan Mitchell4382e442021-07-14 12:53:01 -0700277 std::optional<ResourceTable::SearchResult> search_result = table.FindResource(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800278
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800279 ASSERT_TRUE(search_result);
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800280 ASSERT_TRUE(search_result.value().entry->overlayable_item);
Adam Lesinski71be7052017-12-12 16:48:07 -0800281
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800282 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
283 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
284 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
285 EXPECT_THAT(result_overlayable_item.overlayable->source.path, Eq("res/values/overlayable.xml"));
286 EXPECT_THAT(result_overlayable_item.overlayable->source.line, 40);
Winson62ac8b52019-12-04 08:36:48 -0800287 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
288 | PolicyFlags::VENDOR_PARTITION));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800289 ASSERT_THAT(result_overlayable_item.comment, StrEq("comment"));
290 EXPECT_THAT(result_overlayable_item.source.path, Eq("res/values/overlayable.xml"));
291 EXPECT_THAT(result_overlayable_item.source.line, 42);
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700292}
293
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800294TEST(ResourceTableTest, SetMultipleOverlayableResources) {
295 ResourceTable table;
296
297 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
298 auto group = std::make_shared<Overlayable>("Name", "overlay://theme");
299 OverlayableItem overlayable(group);
Winson62ac8b52019-12-04 08:36:48 -0800300 overlayable.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700301 ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable).Build(),
302 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800303
304 const ResourceName bar = test::ParseNameOrDie("android:string/bar");
305 OverlayableItem overlayable2(group);
Winson62ac8b52019-12-04 08:36:48 -0800306 overlayable2.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700307 ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable2).Build(),
308 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800309
310 const ResourceName baz = test::ParseNameOrDie("android:string/baz");
311 OverlayableItem overlayable3(group);
Winson62ac8b52019-12-04 08:36:48 -0800312 overlayable3.policies = PolicyFlags::VENDOR_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700313 ASSERT_TRUE(table.AddResource(NewResourceBuilder(baz).SetOverlayable(overlayable3).Build(),
314 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800315}
316
317TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) {
318 ResourceTable table;
319
320 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
321 OverlayableItem overlayable_item(std::make_shared<Overlayable>("Name", "overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800322 overlayable_item.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700323 ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable_item).Build(),
324 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800325
326 const ResourceName bar = test::ParseNameOrDie("android:string/bar");
327 OverlayableItem overlayable_item2(std::make_shared<Overlayable>("Name2", "overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800328 overlayable_item2.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700329 ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable_item2).Build(),
330 test::GetDiagnostics()));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800331}
332
333TEST(ResourceTableTest, SetOverlayableSameResourcesFail) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700334 ResourceTable table;
335 const ResourceName name = test::ParseNameOrDie("android:string/foo");
336
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800337 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
338 OverlayableItem overlayable_item(overlayable);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700339 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
340 test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700341
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800342 OverlayableItem overlayable_item2(overlayable);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700343 ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(),
344 test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700345}
346
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800347TEST(ResourceTableTest, SetOverlayableSameResourcesDifferentNameFail) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700348 ResourceTable table;
349 const ResourceName name = test::ParseNameOrDie("android:string/foo");
350
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800351 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
352 OverlayableItem overlayable_item(overlayable);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700353 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(),
354 test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700355
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800356 auto overlayable2 = std::make_shared<Overlayable>("Other", "overlay://theme");
357 OverlayableItem overlayable_item2(overlayable2);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700358 ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(),
359 test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800360}
361
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700362TEST(ResourceTableTest, ConflictingIds) {
363 ResourceTable table;
364 const ResourceName name = test::ParseNameOrDie("android:string/foo");
365 ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetId(0x01010000).Build(),
366 test::GetDiagnostics()));
367 ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetId(0x01010001).Build(),
368 test::GetDiagnostics()));
369}
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700370
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700371TEST(ResourceTableTest, ConflictingIdsCreateEntry) {
372 ResourceTable table;
373 const ResourceName name = test::ParseNameOrDie("android:string/foo");
374 ASSERT_TRUE(table.AddResource(
375 NewResourceBuilder(name).SetId(0x01010000, OnIdConflict::CREATE_ENTRY).Build(),
376 test::GetDiagnostics()));
377 ASSERT_TRUE(table.AddResource(
378 NewResourceBuilder(name).SetId(0x01010001, OnIdConflict::CREATE_ENTRY).Build(),
379 test::GetDiagnostics()));
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700380
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700381 // Non-ambiguous query
382 ASSERT_TRUE(table.AddResource(
383 NewResourceBuilder(name).SetId(0x01010001).SetValue(std::make_unique<Id>()).Build(),
384 test::GetDiagnostics()));
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700385}
386
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387} // namespace aapt