blob: 9271a7e6bae157898111ef5805d4fbafcc9ca951 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 EXPECT_FALSE(table.AddResource(
41 test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "",
42 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
43 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 EXPECT_FALSE(table.AddResource(
46 test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "",
47 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(),
48 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049}
50
Adam Lesinskib1afa072017-03-29 13:52:38 -070051TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) {
52 ResourceTable table;
53
Adam Lesinski71be7052017-12-12 16:48:07 -080054 EXPECT_TRUE(table.AddResourceMangled(
Adam Lesinskib1afa072017-03-29 13:52:38 -070055 test::ParseNameOrDie("android:id/heythere "), ConfigDescription{}, "",
56 test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), test::GetDiagnostics()));
57}
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(
63 test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "",
64 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build(),
65 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066
Adam Lesinskia45893a2017-05-30 15:19:02 -070067 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068}
69
Adam Lesinskicc5609d2016-04-05 12:41:07 -070070TEST(ResourceTableTest, AddMultipleResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ConfigDescription config;
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(
78 test::ParseNameOrDie("android:attr/layout_width"), config, "",
79 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build(),
80 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 EXPECT_TRUE(table.AddResource(
83 test::ParseNameOrDie("android:attr/id"), config, "",
84 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build(),
85 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 EXPECT_TRUE(table.AddResource(
88 test::ParseNameOrDie("android:string/ok"), config, "",
89 test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build(),
90 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 EXPECT_TRUE(table.AddResource(
93 test::ParseNameOrDie("android:string/ok"), language_config, "",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 test::ValueBuilder<BinaryPrimitive>(android::Res_value{})
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 .SetSource("test/path/file.xml", 20u)
96 .Build(),
97 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskia45893a2017-05-30 15:19:02 -070099 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull());
100 EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull());
101 EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull());
102 EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103}
104
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700105TEST(ResourceTableTest, OverrideWeakResourceValue) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 ResourceTable table;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800108 ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
109 test::AttributeBuilder().SetWeak(true).Build(),
110 test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700113 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 EXPECT_TRUE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800116 ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "",
117 util::make_unique<Attribute>(), test::GetDiagnostics()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 attr = test::GetValue<Attribute>(&table, "android:attr/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700120 ASSERT_THAT(attr, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 EXPECT_FALSE(attr->IsWeak());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800124TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) {
125 ResourceTable table;
126
127 const ResourceName name = test::ParseNameOrDie("android:attr/foo");
128 Attribute attr_one(android::ResTable_map::TYPE_STRING);
129 attr_one.SetWeak(true);
130 Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE);
131 attr_two.SetWeak(true);
132
133 ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "",
134 util::make_unique<Attribute>(attr_one), test::GetDiagnostics()));
135 ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "",
136 util::make_unique<Attribute>(attr_two), test::GetDiagnostics()));
137}
138
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700139TEST(ResourceTableTest, ProductVaryingValues) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 ResourceTable table;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
143 test::ParseConfigOrDie("land"), "tablet",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 test::GetDiagnostics()));
146 EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"),
147 test::ParseConfigOrDie("land"), "phone",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 util::make_unique<Id>(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 test::GetDiagnostics()));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800150
Adam Lesinskia45893a2017-05-30 15:19:02 -0700151 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull());
152 EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 Maybe<ResourceTable::SearchResult> sr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 table.FindResource(test::ParseNameOrDie("android:string/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700156 ASSERT_TRUE(sr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 std::vector<ResourceConfigValue*> values =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700158 sr.value().entry->FindAllValues(test::ParseConfigOrDie("land"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 ASSERT_EQ(2u, values.size());
160 EXPECT_EQ(std::string("phone"), values[0]->product);
161 EXPECT_EQ(std::string("tablet"), values[1]->product);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800162}
163
Adam Lesinski71be7052017-12-12 16:48:07 -0800164static StringPiece LevelToString(Visibility::Level level) {
165 switch (level) {
166 case Visibility::Level::kPrivate:
167 return "private";
168 case Visibility::Level::kPublic:
169 return "private";
170 default:
171 return "undefined";
172 }
173}
174
175static ::testing::AssertionResult VisibilityOfResource(const ResourceTable& table,
176 const ResourceNameRef& name,
177 Visibility::Level level,
178 const StringPiece& comment) {
179 Maybe<ResourceTable::SearchResult> result = table.FindResource(name);
180 if (!result) {
181 return ::testing::AssertionFailure() << "no resource '" << name << "' found in table";
182 }
183
184 const Visibility& visibility = result.value().entry->visibility;
185 if (visibility.level != level) {
186 return ::testing::AssertionFailure() << "expected visibility " << LevelToString(level)
187 << " but got " << LevelToString(visibility.level);
188 }
189
190 if (visibility.comment != comment) {
191 return ::testing::AssertionFailure() << "expected visibility comment '" << comment
192 << "' but got '" << visibility.comment << "'";
193 }
194 return ::testing::AssertionSuccess();
195}
196
197TEST(ResourceTableTest, SetVisibility) {
198 using Level = Visibility::Level;
199
200 ResourceTable table;
201 const ResourceName name = test::ParseNameOrDie("android:string/foo");
202
203 Visibility visibility;
204 visibility.level = Visibility::Level::kPrivate;
205 visibility.comment = "private";
206 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
207 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
208
209 visibility.level = Visibility::Level::kUndefined;
210 visibility.comment = "undefined";
211 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
212 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private"));
213
214 visibility.level = Visibility::Level::kPublic;
215 visibility.comment = "public";
216 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
217 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
218
219 visibility.level = Visibility::Level::kPrivate;
220 visibility.comment = "private";
221 ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics()));
222 ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public"));
223}
224
225TEST(ResourceTableTest, SetAllowNew) {
226 ResourceTable table;
227 const ResourceName name = test::ParseNameOrDie("android:string/foo");
228
229 AllowNew allow_new;
230 Maybe<ResourceTable::SearchResult> result;
231
232 allow_new.comment = "first";
233 ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics()));
234 result = table.FindResource(name);
235 ASSERT_TRUE(result);
236 ASSERT_TRUE(result.value().entry->allow_new);
237 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first"));
238
239 allow_new.comment = "second";
240 ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics()));
241 result = table.FindResource(name);
242 ASSERT_TRUE(result);
243 ASSERT_TRUE(result.value().entry->allow_new);
244 ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("second"));
245}
246
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800247TEST(ResourceTableTest, SetOverlayable) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800248 ResourceTable table;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800249 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme",
250 Source("res/values/overlayable.xml", 40));
251 OverlayableItem overlayable_item(overlayable);
Winson62ac8b52019-12-04 08:36:48 -0800252 overlayable_item.policies |= PolicyFlags::PRODUCT_PARTITION;
253 overlayable_item.policies |= PolicyFlags::VENDOR_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800254 overlayable_item.comment = "comment";
255 overlayable_item.source = Source("res/values/overlayable.xml", 42);
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800256
Adam Lesinski71be7052017-12-12 16:48:07 -0800257 const ResourceName name = test::ParseNameOrDie("android:string/foo");
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800258 ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics()));
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800259 Maybe<ResourceTable::SearchResult> search_result = table.FindResource(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800260
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800261 ASSERT_TRUE(search_result);
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800262 ASSERT_TRUE(search_result.value().entry->overlayable_item);
Adam Lesinski71be7052017-12-12 16:48:07 -0800263
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800264 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
265 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name"));
266 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme"));
267 EXPECT_THAT(result_overlayable_item.overlayable->source.path, Eq("res/values/overlayable.xml"));
268 EXPECT_THAT(result_overlayable_item.overlayable->source.line, 40);
Winson62ac8b52019-12-04 08:36:48 -0800269 EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION
270 | PolicyFlags::VENDOR_PARTITION));
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800271 ASSERT_THAT(result_overlayable_item.comment, StrEq("comment"));
272 EXPECT_THAT(result_overlayable_item.source.path, Eq("res/values/overlayable.xml"));
273 EXPECT_THAT(result_overlayable_item.source.line, 42);
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700274}
275
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800276TEST(ResourceTableTest, SetMultipleOverlayableResources) {
277 ResourceTable table;
278
279 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
280 auto group = std::make_shared<Overlayable>("Name", "overlay://theme");
281 OverlayableItem overlayable(group);
Winson62ac8b52019-12-04 08:36:48 -0800282 overlayable.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800283 ASSERT_TRUE(table.SetOverlayable(foo, overlayable, test::GetDiagnostics()));
284
285 const ResourceName bar = test::ParseNameOrDie("android:string/bar");
286 OverlayableItem overlayable2(group);
Winson62ac8b52019-12-04 08:36:48 -0800287 overlayable2.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800288 ASSERT_TRUE(table.SetOverlayable(bar, overlayable2, test::GetDiagnostics()));
289
290 const ResourceName baz = test::ParseNameOrDie("android:string/baz");
291 OverlayableItem overlayable3(group);
Winson62ac8b52019-12-04 08:36:48 -0800292 overlayable3.policies = PolicyFlags::VENDOR_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800293 ASSERT_TRUE(table.SetOverlayable(baz, overlayable3, test::GetDiagnostics()));
294}
295
296TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) {
297 ResourceTable table;
298
299 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
300 OverlayableItem overlayable_item(std::make_shared<Overlayable>("Name", "overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800301 overlayable_item.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800302 ASSERT_TRUE(table.SetOverlayable(foo, overlayable_item, test::GetDiagnostics()));
303
304 const ResourceName bar = test::ParseNameOrDie("android:string/bar");
305 OverlayableItem overlayable_item2(std::make_shared<Overlayable>("Name2", "overlay://theme"));
Winson62ac8b52019-12-04 08:36:48 -0800306 overlayable_item2.policies = PolicyFlags::PRODUCT_PARTITION;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800307 ASSERT_TRUE(table.SetOverlayable(bar, overlayable_item2, test::GetDiagnostics()));
308}
309
310TEST(ResourceTableTest, SetOverlayableSameResourcesFail) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700311 ResourceTable table;
312 const ResourceName name = test::ParseNameOrDie("android:string/foo");
313
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800314 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
315 OverlayableItem overlayable_item(overlayable);
316 ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700317
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800318 OverlayableItem overlayable_item2(overlayable);
319 ASSERT_FALSE(table.SetOverlayable(name, overlayable_item2, test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700320}
321
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800322TEST(ResourceTableTest, SetOverlayableSameResourcesDifferentNameFail) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700323 ResourceTable table;
324 const ResourceName name = test::ParseNameOrDie("android:string/foo");
325
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800326 auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme");
327 OverlayableItem overlayable_item(overlayable);
328 ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700329
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800330 auto overlayable2 = std::make_shared<Overlayable>("Other", "overlay://theme");
331 OverlayableItem overlayable_item2(overlayable2);
332 ASSERT_FALSE(table.SetOverlayable(name, overlayable_item2, test::GetDiagnostics()));
Adam Lesinski71be7052017-12-12 16:48:07 -0800333}
334
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700335TEST(ResourceTableTest, AllowDuplictaeResourcesNames) {
336 ResourceTable table(/* validate_resources */ false);
337
338 const ResourceName foo_name = test::ParseNameOrDie("android:bool/foo");
339 ASSERT_TRUE(table.AddResourceWithId(foo_name, ResourceId(0x7f0100ff), ConfigDescription{} , "",
340 test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0),
341 test::GetDiagnostics()));
342 ASSERT_TRUE(table.AddResourceWithId(foo_name, ResourceId(0x7f010100), ConfigDescription{} , "",
343 test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1),
344 test::GetDiagnostics()));
345
346 ASSERT_TRUE(table.SetVisibilityWithId(foo_name, Visibility{Visibility::Level::kPublic},
347 ResourceId(0x7f0100ff), test::GetDiagnostics()));
348 ASSERT_TRUE(table.SetVisibilityWithId(foo_name, Visibility{Visibility::Level::kPrivate},
349 ResourceId(0x7f010100), test::GetDiagnostics()));
350
351 auto package = table.FindPackageById(0x7f);
352 ASSERT_THAT(package, NotNull());
353 auto type = package->FindType(ResourceType::kBool);
354 ASSERT_THAT(type, NotNull());
355
356 auto entry1 = type->FindEntry("foo", 0x00ff);
357 ASSERT_THAT(entry1, NotNull());
358 ASSERT_THAT(entry1->id, Eq(0x00ff));
359 ASSERT_THAT(entry1->values[0], NotNull());
360 ASSERT_THAT(entry1->values[0]->value, NotNull());
361 ASSERT_THAT(ValueCast<BinaryPrimitive>(entry1->values[0]->value.get()), NotNull());
362 ASSERT_THAT(ValueCast<BinaryPrimitive>(entry1->values[0]->value.get())->value.data, Eq(0u));
363 ASSERT_THAT(entry1->visibility.level, Visibility::Level::kPublic);
364
365 auto entry2 = type->FindEntry("foo", 0x0100);
366 ASSERT_THAT(entry2, NotNull());
367 ASSERT_THAT(entry2->id, Eq(0x0100));
368 ASSERT_THAT(entry2->values[0], NotNull());
369 ASSERT_THAT(entry1->values[0]->value, NotNull());
370 ASSERT_THAT(ValueCast<BinaryPrimitive>(entry2->values[0]->value.get()), NotNull());
371 ASSERT_THAT(ValueCast<BinaryPrimitive>(entry2->values[0]->value.get())->value.data, Eq(1u));
372 ASSERT_THAT(entry2->visibility.level, Visibility::Level::kPrivate);
373}
374
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375} // namespace aapt