blob: 2f6f3dfcaf1c83176e7fde4e027b27879692e60a [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
288TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) {
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
Adam Lesinskida431a22016-12-29 16:08:16 -0500296 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
297 ASSERT_NE(nullptr, bag);
298 ASSERT_GE(bag->entry_count, 2u);
299
300 // First two attributes come from lib_one.
301 EXPECT_EQ(1, bag->entries[0].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800302 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500303 EXPECT_EQ(1, bag->entries[1].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800304 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500305}
306
Adam Lesinski7ad11102016-10-28 16:39:15 -0700307TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
308 AssetManager2 assetmanager;
309 assetmanager.SetApkAssets({style_assets_.get()});
310
311 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
312 ASSERT_NE(nullptr, bag_one);
313 ASSERT_EQ(2u, bag_one->entry_count);
314
315 EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
316 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
317 EXPECT_EQ(1u, bag_one->entries[0].value.data);
318 EXPECT_EQ(0, bag_one->entries[0].cookie);
319
320 EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
321 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
322 EXPECT_EQ(2u, bag_one->entries[1].value.data);
323 EXPECT_EQ(0, bag_one->entries[1].cookie);
324
325 const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
326 ASSERT_NE(nullptr, bag_two);
Adam Lesinski32e75012017-05-09 15:25:37 -0700327 ASSERT_EQ(6u, bag_two->entry_count);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700328
329 // attr_one is inherited from StyleOne.
330 EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
331 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
332 EXPECT_EQ(1u, bag_two->entries[0].value.data);
333 EXPECT_EQ(0, bag_two->entries[0].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800334 EXPECT_EQ(app::R::style::StyleOne, bag_two->entries[0].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700335
336 // attr_two should be overridden from StyleOne by StyleTwo.
337 EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
338 EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
339 EXPECT_EQ(0, bag_two->entries[1].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800340 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[1].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700341 EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
342 bag_two->entries[1].value.data));
343
344 // The rest are new attributes.
345
346 EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
347 EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
348 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
349 EXPECT_EQ(0, bag_two->entries[2].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800350 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[2].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700351
352 EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
353 EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
354 EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
355 EXPECT_EQ(0, bag_two->entries[3].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800356 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[3].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700357
358 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
359 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
360 EXPECT_EQ(3u, bag_two->entries[4].value.data);
361 EXPECT_EQ(0, bag_two->entries[4].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800362 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[4].style);
Adam Lesinski32e75012017-05-09 15:25:37 -0700363
364 EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key);
365 EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType);
366 EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data);
367 EXPECT_EQ(0, bag_two->entries[5].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800368 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[5].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700369}
370
y57cd1952018-04-12 14:26:23 -0700371TEST_F(AssetManager2Test, MergeStylesCircularDependency) {
372 AssetManager2 assetmanager;
373 assetmanager.SetApkAssets({style_assets_.get()});
374
375 // GetBag should stop traversing the parents of styles when a circular
376 // dependency is detected
377 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour);
378 ASSERT_NE(nullptr, bag_one);
379 ASSERT_EQ(3u, bag_one->entry_count);
380}
381
Adam Lesinski0c405242017-01-13 20:47:26 -0800382TEST_F(AssetManager2Test, ResolveReferenceToResource) {
383 AssetManager2 assetmanager;
384 assetmanager.SetApkAssets({basic_assets_.get()});
385
386 Res_value value;
387 ResTable_config selected_config;
388 uint32_t flags;
389 ApkAssetsCookie cookie =
390 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
391 0u /*density_override*/, &value, &selected_config, &flags);
392 ASSERT_NE(kInvalidCookie, cookie);
393
394 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
395 EXPECT_EQ(basic::R::integer::ref2, value.data);
396
Adam Lesinski1c855a02017-11-29 09:59:37 -0800397 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800398 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
399 ASSERT_NE(kInvalidCookie, cookie);
400 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
401 EXPECT_EQ(12000u, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800402 EXPECT_EQ(basic::R::integer::ref2, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800403}
404
405TEST_F(AssetManager2Test, ResolveReferenceToBag) {
406 AssetManager2 assetmanager;
407 assetmanager.SetApkAssets({basic_assets_.get()});
408
409 Res_value value;
410 ResTable_config selected_config;
411 uint32_t flags;
412 ApkAssetsCookie cookie =
413 assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
414 0u /*density_override*/, &value, &selected_config, &flags);
415 ASSERT_NE(kInvalidCookie, cookie);
416
417 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
418 EXPECT_EQ(basic::R::array::integerArray1, value.data);
419
Adam Lesinski1c855a02017-11-29 09:59:37 -0800420 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800421 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
422 ASSERT_NE(kInvalidCookie, cookie);
423 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
424 EXPECT_EQ(basic::R::array::integerArray1, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800425 EXPECT_EQ(basic::R::array::integerArray1, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800426}
427
y9efbbef2018-04-18 11:29:09 -0700428TEST_F(AssetManager2Test, ResolveDeepIdReference) {
429 AssetManager2 assetmanager;
430 assetmanager.SetApkAssets({basic_assets_.get()});
431
432 // Set up the resource ids
433 const uint32_t high_ref = assetmanager
434 .GetResourceId("@id/high_ref", "values", "com.android.basic");
435 ASSERT_NE(high_ref, 0u);
436 const uint32_t middle_ref = assetmanager
437 .GetResourceId("@id/middle_ref", "values", "com.android.basic");
438 ASSERT_NE(middle_ref, 0u);
439 const uint32_t low_ref = assetmanager
440 .GetResourceId("@id/low_ref", "values", "com.android.basic");
441 ASSERT_NE(low_ref, 0u);
442
443 // Retrieve the most shallow resource
444 Res_value value;
445 ResTable_config config;
446 uint32_t flags;
447 ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/,
448 0 /*density_override*/,
449 &value, &config, &flags);
450 ASSERT_NE(kInvalidCookie, cookie);
451 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
452 EXPECT_EQ(middle_ref, value.data);
453
454 // Check that resolving the reference resolves to the deepest id
455 uint32_t last_ref = high_ref;
456 assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref);
457 EXPECT_EQ(last_ref, low_ref);
458}
459
Adam Lesinski1c855a02017-11-29 09:59:37 -0800460TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
461 AssetManager2 assetmanager;
462 assetmanager.SetApkAssets({basic_assets_.get()});
463
464 ResTable_config selected_config;
465 memset(&selected_config, 0, sizeof(selected_config));
466
467 uint32_t flags = 0u;
468
469 // Create some kind of Res_value that is NOT a reference.
470 Res_value value;
471 value.dataType = Res_value::TYPE_STRING;
472 value.data = 0;
473
474 uint32_t last_ref = basic::R::string::test1;
475 EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
476 EXPECT_EQ(basic::R::string::test1, last_ref);
477}
478
Adam Lesinski0c405242017-01-13 20:47:26 -0800479static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
480 const ResTable_config& configuration) {
481 return configurations.count(configuration) > 0;
482}
483
484TEST_F(AssetManager2Test, GetResourceConfigurations) {
485 AssetManager2 assetmanager;
486 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
487
488 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
489
490 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
491 // And one extra for the default configuration.
492 EXPECT_EQ(4u, configurations.size());
493
494 ResTable_config expected_config;
495 memset(&expected_config, 0, sizeof(expected_config));
496 expected_config.language[0] = 's';
497 expected_config.language[1] = 'v';
498 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
499
500 expected_config.language[0] = 'd';
501 expected_config.language[1] = 'e';
502 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
503
504 expected_config.language[0] = 'f';
505 expected_config.language[1] = 'r';
506 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
507
508 // Take out the system assets.
509 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
510
511 // We expect de and fr from basic_de_fr assets.
512 EXPECT_EQ(2u, configurations.size());
513
514 expected_config.language[0] = 's';
515 expected_config.language[1] = 'v';
516 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
517
518 expected_config.language[0] = 'd';
519 expected_config.language[1] = 'e';
520 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
521
522 expected_config.language[0] = 'f';
523 expected_config.language[1] = 'r';
524 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
525}
526
527TEST_F(AssetManager2Test, GetResourceLocales) {
528 AssetManager2 assetmanager;
529 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
530
531 std::set<std::string> locales = assetmanager.GetResourceLocales();
532
533 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
534 EXPECT_EQ(3u, locales.size());
535 EXPECT_GT(locales.count("sv"), 0u);
536 EXPECT_GT(locales.count("de"), 0u);
537 EXPECT_GT(locales.count("fr"), 0u);
538
539 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
540 // We expect the de and fr locales from basic_de_fr assets.
541 EXPECT_EQ(2u, locales.size());
542 EXPECT_GT(locales.count("de"), 0u);
543 EXPECT_GT(locales.count("fr"), 0u);
544}
545
546TEST_F(AssetManager2Test, GetResourceId) {
547 AssetManager2 assetmanager;
548 assetmanager.SetApkAssets({basic_assets_.get()});
549
550 EXPECT_EQ(basic::R::layout::main,
551 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
552 EXPECT_EQ(basic::R::layout::main,
553 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
554 EXPECT_EQ(basic::R::layout::main,
555 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
556}
557
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800558TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
559 AssetManager2 assetmanager;
560 assetmanager.SetApkAssets({system_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700561
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800562 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
563 ASSERT_THAT(asset, NotNull());
564
565 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
566 ASSERT_THAT(data, NotNull());
567 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
568}
569
570TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
571 AssetManager2 assetmanager;
572 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
573
574 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
575 ASSERT_THAT(asset, NotNull());
576
577 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
578 ASSERT_THAT(data, NotNull());
579 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
580}
581
582TEST_F(AssetManager2Test, OpenDir) {
583 AssetManager2 assetmanager;
584 assetmanager.SetApkAssets({system_assets_.get()});
585
586 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
587 ASSERT_THAT(asset_dir, NotNull());
588 ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
589
590 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
591 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
592
593 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
594 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
595
596 asset_dir = assetmanager.OpenDir("subdir");
597 ASSERT_THAT(asset_dir, NotNull());
598 ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
599
600 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
601 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
602}
603
604TEST_F(AssetManager2Test, OpenDirFromManyApks) {
605 AssetManager2 assetmanager;
606 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
607
608 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
609 ASSERT_THAT(asset_dir, NotNull());
610 ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
611
612 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
613 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
614
615 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
616 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
617
618 EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
619 EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
620}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700621
Winson2f3669b2019-01-11 11:28:34 -0800622TEST_F(AssetManager2Test, GetLastPathWithoutEnablingReturnsEmpty) {
623 ResTable_config desired_config;
624
625 AssetManager2 assetmanager;
626 assetmanager.SetConfiguration(desired_config);
627 assetmanager.SetApkAssets({basic_assets_.get()});
628 assetmanager.SetResourceResolutionLoggingEnabled(false);
629
630 Res_value value;
631 ResTable_config selected_config;
632 uint32_t flags;
633
634 ApkAssetsCookie cookie =
635 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
636 0 /*density_override*/, &value, &selected_config, &flags);
637 ASSERT_NE(kInvalidCookie, cookie);
638
639 auto result = assetmanager.GetLastResourceResolution();
640 EXPECT_EQ("", result);
641}
642
643TEST_F(AssetManager2Test, GetLastPathWithoutResolutionReturnsEmpty) {
644 ResTable_config desired_config;
645
646 AssetManager2 assetmanager;
647 assetmanager.SetConfiguration(desired_config);
648 assetmanager.SetApkAssets({basic_assets_.get()});
649
650 auto result = assetmanager.GetLastResourceResolution();
651 EXPECT_EQ("", result);
652}
653
654TEST_F(AssetManager2Test, GetLastPathWithSingleApkAssets) {
655 ResTable_config desired_config;
656 memset(&desired_config, 0, sizeof(desired_config));
657 desired_config.language[0] = 'd';
658 desired_config.language[1] = 'e';
659
660 AssetManager2 assetmanager;
661 assetmanager.SetResourceResolutionLoggingEnabled(true);
662 assetmanager.SetConfiguration(desired_config);
663 assetmanager.SetApkAssets({basic_assets_.get()});
664
665 Res_value value;
666 ResTable_config selected_config;
667 uint32_t flags;
668
669 ApkAssetsCookie cookie =
670 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
671 0 /*density_override*/, &value, &selected_config, &flags);
672 ASSERT_NE(kInvalidCookie, cookie);
673
674 auto result = assetmanager.GetLastResourceResolution();
675 EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic", result);
676}
677
678TEST_F(AssetManager2Test, GetLastPathWithMultipleApkAssets) {
679 ResTable_config desired_config;
680 memset(&desired_config, 0, sizeof(desired_config));
681 desired_config.language[0] = 'd';
682 desired_config.language[1] = 'e';
683
684 AssetManager2 assetmanager;
685 assetmanager.SetResourceResolutionLoggingEnabled(true);
686 assetmanager.SetConfiguration(desired_config);
687 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
688
689 Res_value value = Res_value();
690 ResTable_config selected_config;
691 uint32_t flags;
692
693 ApkAssetsCookie cookie =
694 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
695 0 /*density_override*/, &value, &selected_config, &flags);
696 ASSERT_NE(kInvalidCookie, cookie);
697
698 auto result = assetmanager.GetLastResourceResolution();
699 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);
700}
701
702TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) {
703 ResTable_config desired_config;
704 memset(&desired_config, 0, sizeof(desired_config));
705
706 AssetManager2 assetmanager;
707 assetmanager.SetResourceResolutionLoggingEnabled(true);
708 assetmanager.SetConfiguration(desired_config);
709 assetmanager.SetApkAssets({basic_assets_.get()});
710
711 Res_value value = Res_value();
712 ResTable_config selected_config;
713 uint32_t flags;
714
715 ApkAssetsCookie cookie =
716 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
717 0 /*density_override*/, &value, &selected_config, &flags);
718 ASSERT_NE(kInvalidCookie, cookie);
719
720 auto resultEnabled = assetmanager.GetLastResourceResolution();
721 ASSERT_NE("", resultEnabled);
722
723 assetmanager.SetResourceResolutionLoggingEnabled(false);
724
725 auto resultDisabled = assetmanager.GetLastResourceResolution();
726 EXPECT_EQ("", resultDisabled);
727}
728
Ryan Mitchell2e394222019-08-28 12:10:51 -0700729TEST_F(AssetManager2Test, GetOverlayablesToString) {
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100730 ResTable_config desired_config;
731 memset(&desired_config, 0, sizeof(desired_config));
732
733 AssetManager2 assetmanager;
734 assetmanager.SetResourceResolutionLoggingEnabled(true);
735 assetmanager.SetConfiguration(desired_config);
736 assetmanager.SetApkAssets({overlayable_assets_.get()});
737
738 const auto map = assetmanager.GetOverlayableMapForPackage(0x7f);
739 ASSERT_NE(nullptr, map);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700740 ASSERT_EQ(3, map->size());
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100741 ASSERT_EQ(map->at("OverlayableResources1"), "overlay://theme");
742 ASSERT_EQ(map->at("OverlayableResources2"), "overlay://com.android.overlayable");
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700743 ASSERT_EQ(map->at("OverlayableResources3"), "");
Ryan Mitchell2e394222019-08-28 12:10:51 -0700744
745 std::string api;
746 ASSERT_TRUE(assetmanager.GetOverlayablesToString("com.android.overlayable", &api));
747 ASSERT_EQ(api.find("not_overlayable"), std::string::npos);
748 ASSERT_NE(api.find("resource='com.android.overlayable:string/overlayable2' overlayable='OverlayableResources1' actor='overlay://theme' policy='0x0000000a'\n"),
749 std::string::npos);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700750
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100751}
752
Adam Lesinski7ad11102016-10-28 16:39:15 -0700753} // namespace android