blob: 35fea7ab86cb6edd8c99a6536c1d9d444a9f57c9 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
2 * Copyright (C) 2016 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 "androidfw/AssetManager2.h"
18#include "androidfw/AssetManager.h"
19
20#include "android-base/logging.h"
21
22#include "TestHelpers.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080023#include "androidfw/ResourceUtils.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050024#include "data/appaslib/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070025#include "data/basic/R.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050026#include "data/lib_one/R.h"
27#include "data/lib_two/R.h"
28#include "data/libclient/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070029#include "data/styles/R.h"
Adam Lesinski0c405242017-01-13 20:47:26 -080030#include "data/system/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070031
Adam Lesinski7ad11102016-10-28 16:39:15 -070032namespace app = com::android::app;
Adam Lesinskida431a22016-12-29 16:08:16 -050033namespace appaslib = com::android::appaslib::app;
34namespace basic = com::android::basic;
35namespace lib_one = com::android::lib_one;
36namespace lib_two = com::android::lib_two;
37namespace libclient = com::android::libclient;
Adam Lesinski7ad11102016-10-28 16:39:15 -070038
Adam Lesinskibebfcc42018-02-12 14:27:46 -080039using ::testing::Eq;
40using ::testing::NotNull;
41using ::testing::StrEq;
42
Adam Lesinski7ad11102016-10-28 16:39:15 -070043namespace android {
44
45class AssetManager2Test : public ::testing::Test {
46 public:
47 void SetUp() override {
48 basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
49 ASSERT_NE(nullptr, basic_assets_);
50
51 basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk");
52 ASSERT_NE(nullptr, basic_de_fr_assets_);
53
54 style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
55 ASSERT_NE(nullptr, style_assets_);
Adam Lesinskida431a22016-12-29 16:08:16 -050056
57 lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk");
58 ASSERT_NE(nullptr, lib_one_assets_);
59
60 lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk");
61 ASSERT_NE(nullptr, lib_two_assets_);
62
63 libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk");
64 ASSERT_NE(nullptr, libclient_assets_);
65
Adam Lesinskibebfcc42018-02-12 14:27:46 -080066 appaslib_assets_ = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk");
Adam Lesinskida431a22016-12-29 16:08:16 -050067 ASSERT_NE(nullptr, appaslib_assets_);
Adam Lesinski0c405242017-01-13 20:47:26 -080068
69 system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
70 ASSERT_NE(nullptr, system_assets_);
Adam Lesinskibebfcc42018-02-12 14:27:46 -080071
72 app_assets_ = ApkAssets::Load(GetTestDataPath() + "/app/app.apk");
73 ASSERT_THAT(app_assets_, NotNull());
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +010074
75 overlayable_assets_ = ApkAssets::Load(GetTestDataPath() + "/overlayable/overlayable.apk");
76 ASSERT_THAT(overlayable_assets_, NotNull());
Adam Lesinski7ad11102016-10-28 16:39:15 -070077 }
78
79 protected:
Adam Lesinski0c405242017-01-13 20:47:26 -080080 std::unique_ptr<const ApkAssets> basic_assets_;
81 std::unique_ptr<const ApkAssets> basic_de_fr_assets_;
82 std::unique_ptr<const ApkAssets> style_assets_;
83 std::unique_ptr<const ApkAssets> lib_one_assets_;
84 std::unique_ptr<const ApkAssets> lib_two_assets_;
85 std::unique_ptr<const ApkAssets> libclient_assets_;
86 std::unique_ptr<const ApkAssets> appaslib_assets_;
87 std::unique_ptr<const ApkAssets> system_assets_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080088 std::unique_ptr<const ApkAssets> app_assets_;
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +010089 std::unique_ptr<const ApkAssets> overlayable_assets_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070090};
91
Adam Lesinskida431a22016-12-29 16:08:16 -050092TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070093 ResTable_config desired_config;
94 memset(&desired_config, 0, sizeof(desired_config));
95 desired_config.language[0] = 'd';
96 desired_config.language[1] = 'e';
97
98 AssetManager2 assetmanager;
99 assetmanager.SetConfiguration(desired_config);
100 assetmanager.SetApkAssets({basic_assets_.get()});
101
102 Res_value value;
103 ResTable_config selected_config;
104 uint32_t flags;
105
106 ApkAssetsCookie cookie =
107 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
108 0 /*density_override*/, &value, &selected_config, &flags);
109 ASSERT_NE(kInvalidCookie, cookie);
110
111 // Came from our ApkAssets.
112 EXPECT_EQ(0, cookie);
113
114 // It is the default config.
115 EXPECT_EQ(0, selected_config.language[0]);
116 EXPECT_EQ(0, selected_config.language[1]);
117
118 // It is a string.
119 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
120}
121
Adam Lesinskida431a22016-12-29 16:08:16 -0500122TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700123 ResTable_config desired_config;
124 memset(&desired_config, 0, sizeof(desired_config));
125 desired_config.language[0] = 'd';
126 desired_config.language[1] = 'e';
127
128 AssetManager2 assetmanager;
129 assetmanager.SetConfiguration(desired_config);
130 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
131
132 Res_value value;
133 ResTable_config selected_config;
134 uint32_t flags;
135
136 ApkAssetsCookie cookie =
137 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
138 0 /*density_override*/, &value, &selected_config, &flags);
139 ASSERT_NE(kInvalidCookie, cookie);
140
141 // Came from our de_fr ApkAssets.
142 EXPECT_EQ(1, cookie);
143
Adam Lesinskida431a22016-12-29 16:08:16 -0500144 // The configuration is German.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700145 EXPECT_EQ('d', selected_config.language[0]);
146 EXPECT_EQ('e', selected_config.language[1]);
147
148 // It is a string.
149 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
150}
151
Adam Lesinskida431a22016-12-29 16:08:16 -0500152TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) {
153 AssetManager2 assetmanager;
154
155 // libclient is built with lib_one and then lib_two in order.
156 // Reverse the order to test that proper package ID re-assignment is happening.
157 assetmanager.SetApkAssets(
158 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
159
160 Res_value value;
161 ResTable_config selected_config;
162 uint32_t flags;
163
164 ApkAssetsCookie cookie =
165 assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/,
166 0 /*density_override*/, &value, &selected_config, &flags);
167 ASSERT_NE(kInvalidCookie, cookie);
168
169 // Reference comes from libclient.
170 EXPECT_EQ(2, cookie);
171 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
172
173 // Lookup the reference.
174 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
175 &value, &selected_config, &flags);
176 ASSERT_NE(kInvalidCookie, cookie);
177 EXPECT_EQ(1, cookie);
178 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
179 EXPECT_EQ(std::string("Foo from lib_one"),
180 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
181
182 cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/,
183 0 /*density_override*/, &value, &selected_config, &flags);
184 ASSERT_NE(kInvalidCookie, cookie);
185
186 // Reference comes from libclient.
187 EXPECT_EQ(2, cookie);
188 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
189
190 // Lookup the reference.
191 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
192 &value, &selected_config, &flags);
193 ASSERT_NE(kInvalidCookie, cookie);
194 EXPECT_EQ(0, cookie);
195 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
196 EXPECT_EQ(std::string("Foo from lib_two"),
197 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
198}
199
200TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) {
201 AssetManager2 assetmanager;
202 assetmanager.SetApkAssets({appaslib_assets_.get()});
203
204 // The appaslib package will have been assigned the package ID 0x02.
205
206 Res_value value;
207 ResTable_config selected_config;
208 uint32_t flags;
209 ApkAssetsCookie cookie = assetmanager.GetResource(
Adam Lesinski929d6512017-01-16 19:11:19 -0800210 fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
Adam Lesinskida431a22016-12-29 16:08:16 -0500211 0u /*density_override*/, &value, &selected_config, &flags);
212 ASSERT_NE(kInvalidCookie, cookie);
213 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
Adam Lesinski929d6512017-01-16 19:11:19 -0800214 EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
Adam Lesinskida431a22016-12-29 16:08:16 -0500215}
216
Ryan Mitchellfe50d732019-12-19 16:12:35 -0800217TEST_F(AssetManager2Test, AssignsUnchangingPackageIdToSharedLibrary) {
218 DynamicLibManager lib_manager;
219 AssetManager2 assetmanager(&lib_manager);
220 assetmanager.SetApkAssets(
221 {lib_one_assets_.get(), lib_two_assets_.get(), libclient_assets_.get()});
222
223 AssetManager2 assetmanager2(&lib_manager);
224 assetmanager2.SetApkAssets(
225 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
226
227 uint32_t res_id = assetmanager.GetResourceId("com.android.lib_one:string/foo");
228 ASSERT_NE(0U, res_id);
229
230 uint32_t res_id_2 = assetmanager2.GetResourceId("com.android.lib_one:string/foo");
231 ASSERT_NE(0U, res_id_2);
232
233 ASSERT_EQ(res_id, res_id_2);
234}
235
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800236TEST_F(AssetManager2Test, GetSharedLibraryResourceName) {
237 AssetManager2 assetmanager;
238 assetmanager.SetApkAssets({lib_one_assets_.get()});
239
240 AssetManager2::ResourceName name;
241 ASSERT_TRUE(assetmanager.GetResourceName(lib_one::R::string::foo, &name));
242 std::string formatted_name = ToFormattedResourceString(&name);
243 ASSERT_EQ(formatted_name, "com.android.lib_one:string/foo");
244}
245
Adam Lesinskida431a22016-12-29 16:08:16 -0500246TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700247 AssetManager2 assetmanager;
248 assetmanager.SetApkAssets({basic_assets_.get()});
249
250 const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1);
251 ASSERT_NE(nullptr, bag);
252 ASSERT_EQ(3u, bag->entry_count);
253
254 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType);
255 EXPECT_EQ(1u, bag->entries[0].value.data);
256 EXPECT_EQ(0, bag->entries[0].cookie);
257
258 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType);
259 EXPECT_EQ(2u, bag->entries[1].value.data);
260 EXPECT_EQ(0, bag->entries[1].cookie);
261
262 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType);
263 EXPECT_EQ(3u, bag->entries[2].value.data);
264 EXPECT_EQ(0, bag->entries[2].cookie);
265}
266
Adam Lesinskida431a22016-12-29 16:08:16 -0500267TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
268
269TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
270 AssetManager2 assetmanager;
271
272 // libclient is built with lib_one and then lib_two in order.
273 // Reverse the order to test that proper package ID re-assignment is happening.
274 assetmanager.SetApkAssets(
275 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
276
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800277 const ResolvedBag* bag = assetmanager.GetBag(fix_package_id(lib_one::R::style::Theme, 0x03));
278 ASSERT_NE(nullptr, bag);
279 ASSERT_GE(bag->entry_count, 2u);
280
281 // First two attributes come from lib_one.
282 EXPECT_EQ(1, bag->entries[0].cookie);
283 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
284 EXPECT_EQ(1, bag->entries[1].cookie);
285 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
286}
287
Ryan Mitchell155d5392020-02-10 13:35:24 -0800288TEST_F(AssetManager2Test, FindsBagResourceFromMultipleSharedLibraries) {
289 AssetManager2 assetmanager;
290
291 // libclient is built with lib_one and then lib_two in order.
292 // Reverse the order to test that proper package ID re-assignment is happening.
293 assetmanager.SetApkAssets(
294 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
295
296 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::ThemeMultiLib);
297 ASSERT_NE(nullptr, bag);
298 ASSERT_EQ(bag->entry_count, 2u);
299
300 // First attribute comes from lib_two.
301 EXPECT_EQ(2, bag->entries[0].cookie);
302 EXPECT_EQ(0x02, get_package_id(bag->entries[0].key));
303
304 // The next two attributes come from lib_one.
305 EXPECT_EQ(2, bag->entries[1].cookie);
306 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
307}
308
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800309TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) {
310 AssetManager2 assetmanager;
311
312 // libclient is built with lib_one and then lib_two in order.
313 // Reverse the order to test that proper package ID re-assignment is happening.
314 assetmanager.SetApkAssets(
315 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
316
Adam Lesinskida431a22016-12-29 16:08:16 -0500317 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
318 ASSERT_NE(nullptr, bag);
319 ASSERT_GE(bag->entry_count, 2u);
320
321 // First two attributes come from lib_one.
322 EXPECT_EQ(1, bag->entries[0].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800323 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500324 EXPECT_EQ(1, bag->entries[1].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800325 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500326}
327
Adam Lesinski7ad11102016-10-28 16:39:15 -0700328TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
329 AssetManager2 assetmanager;
330 assetmanager.SetApkAssets({style_assets_.get()});
331
332 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
333 ASSERT_NE(nullptr, bag_one);
334 ASSERT_EQ(2u, bag_one->entry_count);
335
336 EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
337 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
338 EXPECT_EQ(1u, bag_one->entries[0].value.data);
339 EXPECT_EQ(0, bag_one->entries[0].cookie);
340
341 EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
342 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
343 EXPECT_EQ(2u, bag_one->entries[1].value.data);
344 EXPECT_EQ(0, bag_one->entries[1].cookie);
345
346 const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
347 ASSERT_NE(nullptr, bag_two);
Adam Lesinski32e75012017-05-09 15:25:37 -0700348 ASSERT_EQ(6u, bag_two->entry_count);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700349
350 // attr_one is inherited from StyleOne.
351 EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
352 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
353 EXPECT_EQ(1u, bag_two->entries[0].value.data);
354 EXPECT_EQ(0, bag_two->entries[0].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800355 EXPECT_EQ(app::R::style::StyleOne, bag_two->entries[0].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700356
357 // attr_two should be overridden from StyleOne by StyleTwo.
358 EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
359 EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
360 EXPECT_EQ(0, bag_two->entries[1].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800361 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[1].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700362 EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
363 bag_two->entries[1].value.data));
364
365 // The rest are new attributes.
366
367 EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
368 EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
369 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
370 EXPECT_EQ(0, bag_two->entries[2].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800371 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[2].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700372
373 EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
374 EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
375 EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
376 EXPECT_EQ(0, bag_two->entries[3].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800377 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[3].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700378
379 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
380 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
381 EXPECT_EQ(3u, bag_two->entries[4].value.data);
382 EXPECT_EQ(0, bag_two->entries[4].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800383 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[4].style);
Adam Lesinski32e75012017-05-09 15:25:37 -0700384
385 EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key);
386 EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType);
387 EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data);
388 EXPECT_EQ(0, bag_two->entries[5].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800389 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[5].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700390}
391
y57cd1952018-04-12 14:26:23 -0700392TEST_F(AssetManager2Test, MergeStylesCircularDependency) {
393 AssetManager2 assetmanager;
394 assetmanager.SetApkAssets({style_assets_.get()});
395
396 // GetBag should stop traversing the parents of styles when a circular
397 // dependency is detected
398 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour);
399 ASSERT_NE(nullptr, bag_one);
400 ASSERT_EQ(3u, bag_one->entry_count);
401}
402
Adam Lesinski0c405242017-01-13 20:47:26 -0800403TEST_F(AssetManager2Test, ResolveReferenceToResource) {
404 AssetManager2 assetmanager;
405 assetmanager.SetApkAssets({basic_assets_.get()});
406
407 Res_value value;
408 ResTable_config selected_config;
409 uint32_t flags;
410 ApkAssetsCookie cookie =
411 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
412 0u /*density_override*/, &value, &selected_config, &flags);
413 ASSERT_NE(kInvalidCookie, cookie);
414
415 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
416 EXPECT_EQ(basic::R::integer::ref2, value.data);
417
Adam Lesinski1c855a02017-11-29 09:59:37 -0800418 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800419 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
420 ASSERT_NE(kInvalidCookie, cookie);
421 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
422 EXPECT_EQ(12000u, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800423 EXPECT_EQ(basic::R::integer::ref2, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800424}
425
426TEST_F(AssetManager2Test, ResolveReferenceToBag) {
427 AssetManager2 assetmanager;
428 assetmanager.SetApkAssets({basic_assets_.get()});
429
430 Res_value value;
431 ResTable_config selected_config;
432 uint32_t flags;
433 ApkAssetsCookie cookie =
434 assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
435 0u /*density_override*/, &value, &selected_config, &flags);
436 ASSERT_NE(kInvalidCookie, cookie);
437
438 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
439 EXPECT_EQ(basic::R::array::integerArray1, value.data);
440
Adam Lesinski1c855a02017-11-29 09:59:37 -0800441 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800442 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
443 ASSERT_NE(kInvalidCookie, cookie);
444 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
445 EXPECT_EQ(basic::R::array::integerArray1, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800446 EXPECT_EQ(basic::R::array::integerArray1, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800447}
448
y9efbbef2018-04-18 11:29:09 -0700449TEST_F(AssetManager2Test, ResolveDeepIdReference) {
450 AssetManager2 assetmanager;
451 assetmanager.SetApkAssets({basic_assets_.get()});
452
453 // Set up the resource ids
454 const uint32_t high_ref = assetmanager
455 .GetResourceId("@id/high_ref", "values", "com.android.basic");
456 ASSERT_NE(high_ref, 0u);
457 const uint32_t middle_ref = assetmanager
458 .GetResourceId("@id/middle_ref", "values", "com.android.basic");
459 ASSERT_NE(middle_ref, 0u);
460 const uint32_t low_ref = assetmanager
461 .GetResourceId("@id/low_ref", "values", "com.android.basic");
462 ASSERT_NE(low_ref, 0u);
463
464 // Retrieve the most shallow resource
465 Res_value value;
466 ResTable_config config;
467 uint32_t flags;
468 ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/,
469 0 /*density_override*/,
470 &value, &config, &flags);
471 ASSERT_NE(kInvalidCookie, cookie);
472 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
473 EXPECT_EQ(middle_ref, value.data);
474
475 // Check that resolving the reference resolves to the deepest id
476 uint32_t last_ref = high_ref;
477 assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref);
478 EXPECT_EQ(last_ref, low_ref);
479}
480
Adam Lesinski1c855a02017-11-29 09:59:37 -0800481TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
482 AssetManager2 assetmanager;
483 assetmanager.SetApkAssets({basic_assets_.get()});
484
485 ResTable_config selected_config;
486 memset(&selected_config, 0, sizeof(selected_config));
487
488 uint32_t flags = 0u;
489
490 // Create some kind of Res_value that is NOT a reference.
491 Res_value value;
492 value.dataType = Res_value::TYPE_STRING;
493 value.data = 0;
494
495 uint32_t last_ref = basic::R::string::test1;
496 EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
497 EXPECT_EQ(basic::R::string::test1, last_ref);
498}
499
Adam Lesinski0c405242017-01-13 20:47:26 -0800500static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
501 const ResTable_config& configuration) {
502 return configurations.count(configuration) > 0;
503}
504
505TEST_F(AssetManager2Test, GetResourceConfigurations) {
506 AssetManager2 assetmanager;
507 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
508
509 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
510
511 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
512 // And one extra for the default configuration.
513 EXPECT_EQ(4u, configurations.size());
514
515 ResTable_config expected_config;
516 memset(&expected_config, 0, sizeof(expected_config));
517 expected_config.language[0] = 's';
518 expected_config.language[1] = 'v';
519 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
520
521 expected_config.language[0] = 'd';
522 expected_config.language[1] = 'e';
523 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
524
525 expected_config.language[0] = 'f';
526 expected_config.language[1] = 'r';
527 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
528
529 // Take out the system assets.
530 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
531
532 // We expect de and fr from basic_de_fr assets.
533 EXPECT_EQ(2u, configurations.size());
534
535 expected_config.language[0] = 's';
536 expected_config.language[1] = 'v';
537 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
538
539 expected_config.language[0] = 'd';
540 expected_config.language[1] = 'e';
541 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
542
543 expected_config.language[0] = 'f';
544 expected_config.language[1] = 'r';
545 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
546}
547
548TEST_F(AssetManager2Test, GetResourceLocales) {
549 AssetManager2 assetmanager;
550 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
551
552 std::set<std::string> locales = assetmanager.GetResourceLocales();
553
554 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
555 EXPECT_EQ(3u, locales.size());
556 EXPECT_GT(locales.count("sv"), 0u);
557 EXPECT_GT(locales.count("de"), 0u);
558 EXPECT_GT(locales.count("fr"), 0u);
559
560 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
561 // We expect the de and fr locales from basic_de_fr assets.
562 EXPECT_EQ(2u, locales.size());
563 EXPECT_GT(locales.count("de"), 0u);
564 EXPECT_GT(locales.count("fr"), 0u);
565}
566
567TEST_F(AssetManager2Test, GetResourceId) {
568 AssetManager2 assetmanager;
569 assetmanager.SetApkAssets({basic_assets_.get()});
570
571 EXPECT_EQ(basic::R::layout::main,
572 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
573 EXPECT_EQ(basic::R::layout::main,
574 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
575 EXPECT_EQ(basic::R::layout::main,
576 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
577}
578
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800579TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
580 AssetManager2 assetmanager;
581 assetmanager.SetApkAssets({system_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700582
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800583 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
584 ASSERT_THAT(asset, NotNull());
585
586 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
587 ASSERT_THAT(data, NotNull());
588 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
589}
590
591TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
592 AssetManager2 assetmanager;
593 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
594
595 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
596 ASSERT_THAT(asset, NotNull());
597
598 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
599 ASSERT_THAT(data, NotNull());
600 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
601}
602
603TEST_F(AssetManager2Test, OpenDir) {
604 AssetManager2 assetmanager;
605 assetmanager.SetApkAssets({system_assets_.get()});
606
607 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
608 ASSERT_THAT(asset_dir, NotNull());
609 ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
610
611 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
612 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
613
614 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
615 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
616
617 asset_dir = assetmanager.OpenDir("subdir");
618 ASSERT_THAT(asset_dir, NotNull());
619 ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
620
621 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
622 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
623}
624
625TEST_F(AssetManager2Test, OpenDirFromManyApks) {
626 AssetManager2 assetmanager;
627 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
628
629 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
630 ASSERT_THAT(asset_dir, NotNull());
631 ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
632
633 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
634 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
635
636 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
637 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
638
639 EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
640 EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
641}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700642
Winson2f3669b2019-01-11 11:28:34 -0800643TEST_F(AssetManager2Test, GetLastPathWithoutEnablingReturnsEmpty) {
644 ResTable_config desired_config;
645
646 AssetManager2 assetmanager;
647 assetmanager.SetConfiguration(desired_config);
648 assetmanager.SetApkAssets({basic_assets_.get()});
649 assetmanager.SetResourceResolutionLoggingEnabled(false);
650
651 Res_value value;
652 ResTable_config selected_config;
653 uint32_t flags;
654
655 ApkAssetsCookie cookie =
656 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
657 0 /*density_override*/, &value, &selected_config, &flags);
658 ASSERT_NE(kInvalidCookie, cookie);
659
660 auto result = assetmanager.GetLastResourceResolution();
661 EXPECT_EQ("", result);
662}
663
664TEST_F(AssetManager2Test, GetLastPathWithoutResolutionReturnsEmpty) {
665 ResTable_config desired_config;
666
667 AssetManager2 assetmanager;
668 assetmanager.SetConfiguration(desired_config);
669 assetmanager.SetApkAssets({basic_assets_.get()});
670
671 auto result = assetmanager.GetLastResourceResolution();
672 EXPECT_EQ("", result);
673}
674
675TEST_F(AssetManager2Test, GetLastPathWithSingleApkAssets) {
676 ResTable_config desired_config;
677 memset(&desired_config, 0, sizeof(desired_config));
678 desired_config.language[0] = 'd';
679 desired_config.language[1] = 'e';
680
681 AssetManager2 assetmanager;
682 assetmanager.SetResourceResolutionLoggingEnabled(true);
683 assetmanager.SetConfiguration(desired_config);
684 assetmanager.SetApkAssets({basic_assets_.get()});
685
686 Res_value value;
687 ResTable_config selected_config;
688 uint32_t flags;
689
690 ApkAssetsCookie cookie =
691 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
692 0 /*density_override*/, &value, &selected_config, &flags);
693 ASSERT_NE(kInvalidCookie, cookie);
694
695 auto result = assetmanager.GetLastResourceResolution();
696 EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic", result);
697}
698
699TEST_F(AssetManager2Test, GetLastPathWithMultipleApkAssets) {
700 ResTable_config desired_config;
701 memset(&desired_config, 0, sizeof(desired_config));
702 desired_config.language[0] = 'd';
703 desired_config.language[1] = 'e';
704
705 AssetManager2 assetmanager;
706 assetmanager.SetResourceResolutionLoggingEnabled(true);
707 assetmanager.SetConfiguration(desired_config);
708 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
709
710 Res_value value = Res_value();
711 ResTable_config selected_config;
712 uint32_t flags;
713
714 ApkAssetsCookie cookie =
715 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
716 0 /*density_override*/, &value, &selected_config, &flags);
717 ASSERT_NE(kInvalidCookie, cookie);
718
719 auto result = assetmanager.GetLastResourceResolution();
720 EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic\n\tFound better: com.android.basic -de", result);
721}
722
723TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) {
724 ResTable_config desired_config;
725 memset(&desired_config, 0, sizeof(desired_config));
726
727 AssetManager2 assetmanager;
728 assetmanager.SetResourceResolutionLoggingEnabled(true);
729 assetmanager.SetConfiguration(desired_config);
730 assetmanager.SetApkAssets({basic_assets_.get()});
731
732 Res_value value = Res_value();
733 ResTable_config selected_config;
734 uint32_t flags;
735
736 ApkAssetsCookie cookie =
737 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
738 0 /*density_override*/, &value, &selected_config, &flags);
739 ASSERT_NE(kInvalidCookie, cookie);
740
741 auto resultEnabled = assetmanager.GetLastResourceResolution();
742 ASSERT_NE("", resultEnabled);
743
744 assetmanager.SetResourceResolutionLoggingEnabled(false);
745
746 auto resultDisabled = assetmanager.GetLastResourceResolution();
747 EXPECT_EQ("", resultDisabled);
748}
749
Ryan Mitchell2e394222019-08-28 12:10:51 -0700750TEST_F(AssetManager2Test, GetOverlayablesToString) {
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100751 ResTable_config desired_config;
752 memset(&desired_config, 0, sizeof(desired_config));
753
754 AssetManager2 assetmanager;
755 assetmanager.SetResourceResolutionLoggingEnabled(true);
756 assetmanager.SetConfiguration(desired_config);
757 assetmanager.SetApkAssets({overlayable_assets_.get()});
758
759 const auto map = assetmanager.GetOverlayableMapForPackage(0x7f);
760 ASSERT_NE(nullptr, map);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700761 ASSERT_EQ(3, map->size());
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100762 ASSERT_EQ(map->at("OverlayableResources1"), "overlay://theme");
763 ASSERT_EQ(map->at("OverlayableResources2"), "overlay://com.android.overlayable");
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700764 ASSERT_EQ(map->at("OverlayableResources3"), "");
Ryan Mitchell2e394222019-08-28 12:10:51 -0700765
766 std::string api;
767 ASSERT_TRUE(assetmanager.GetOverlayablesToString("com.android.overlayable", &api));
768 ASSERT_EQ(api.find("not_overlayable"), std::string::npos);
769 ASSERT_NE(api.find("resource='com.android.overlayable:string/overlayable2' overlayable='OverlayableResources1' actor='overlay://theme' policy='0x0000000a'\n"),
770 std::string::npos);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700771
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100772}
773
Adam Lesinski7ad11102016-10-28 16:39:15 -0700774} // namespace android