blob: 8c255d16fe1f30d291fa2cebd1487d88edf08c32 [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
Adam Lesinski7ad11102016-10-28 16:39:15 -070020#include "TestHelpers.h"
Ryan Mitchell824cc492020-02-12 10:48:14 -080021#include "android-base/file.h"
22#include "android-base/logging.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 {
Ryan Mitchell824cc492020-02-12 10:48:14 -080048 // Move to the test data directory so the idmap can locate the overlay APK.
49 std::string original_path = base::GetExecutableDirectory();
50 chdir(GetTestDataPath().c_str());
51
52 basic_assets_ = ApkAssets::Load("basic/basic.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -070053 ASSERT_NE(nullptr, basic_assets_);
54
Ryan Mitchell824cc492020-02-12 10:48:14 -080055 basic_de_fr_assets_ = ApkAssets::Load("basic/basic_de_fr.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -070056 ASSERT_NE(nullptr, basic_de_fr_assets_);
57
Ryan Mitchell824cc492020-02-12 10:48:14 -080058 style_assets_ = ApkAssets::Load("styles/styles.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -070059 ASSERT_NE(nullptr, style_assets_);
Adam Lesinskida431a22016-12-29 16:08:16 -050060
Ryan Mitchell824cc492020-02-12 10:48:14 -080061 lib_one_assets_ = ApkAssets::Load("lib_one/lib_one.apk");
Adam Lesinskida431a22016-12-29 16:08:16 -050062 ASSERT_NE(nullptr, lib_one_assets_);
63
Ryan Mitchell824cc492020-02-12 10:48:14 -080064 lib_two_assets_ = ApkAssets::Load("lib_two/lib_two.apk");
Adam Lesinskida431a22016-12-29 16:08:16 -050065 ASSERT_NE(nullptr, lib_two_assets_);
66
Ryan Mitchell824cc492020-02-12 10:48:14 -080067 libclient_assets_ = ApkAssets::Load("libclient/libclient.apk");
Adam Lesinskida431a22016-12-29 16:08:16 -050068 ASSERT_NE(nullptr, libclient_assets_);
69
Ryan Mitchell824cc492020-02-12 10:48:14 -080070 appaslib_assets_ = ApkAssets::Load("appaslib/appaslib.apk", PROPERTY_DYNAMIC);
Adam Lesinskida431a22016-12-29 16:08:16 -050071 ASSERT_NE(nullptr, appaslib_assets_);
Adam Lesinski0c405242017-01-13 20:47:26 -080072
Ryan Mitchell824cc492020-02-12 10:48:14 -080073 system_assets_ = ApkAssets::Load("system/system.apk", PROPERTY_SYSTEM);
Adam Lesinski0c405242017-01-13 20:47:26 -080074 ASSERT_NE(nullptr, system_assets_);
Adam Lesinskibebfcc42018-02-12 14:27:46 -080075
Ryan Mitchell824cc492020-02-12 10:48:14 -080076 app_assets_ = ApkAssets::Load("app/app.apk");
Adam Lesinskibebfcc42018-02-12 14:27:46 -080077 ASSERT_THAT(app_assets_, NotNull());
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +010078
Ryan Mitchell824cc492020-02-12 10:48:14 -080079 overlay_assets_ = ApkAssets::LoadOverlay("overlay/overlay.idmap");
80 ASSERT_NE(nullptr, overlay_assets_);
81
82 overlayable_assets_ = ApkAssets::Load("overlayable/overlayable.apk");
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +010083 ASSERT_THAT(overlayable_assets_, NotNull());
Ryan Mitchell824cc492020-02-12 10:48:14 -080084 chdir(original_path.c_str());
Adam Lesinski7ad11102016-10-28 16:39:15 -070085 }
86
87 protected:
Adam Lesinski0c405242017-01-13 20:47:26 -080088 std::unique_ptr<const ApkAssets> basic_assets_;
89 std::unique_ptr<const ApkAssets> basic_de_fr_assets_;
90 std::unique_ptr<const ApkAssets> style_assets_;
91 std::unique_ptr<const ApkAssets> lib_one_assets_;
92 std::unique_ptr<const ApkAssets> lib_two_assets_;
93 std::unique_ptr<const ApkAssets> libclient_assets_;
94 std::unique_ptr<const ApkAssets> appaslib_assets_;
95 std::unique_ptr<const ApkAssets> system_assets_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080096 std::unique_ptr<const ApkAssets> app_assets_;
Ryan Mitchell824cc492020-02-12 10:48:14 -080097 std::unique_ptr<const ApkAssets> overlay_assets_;
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +010098 std::unique_ptr<const ApkAssets> overlayable_assets_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070099};
100
Adam Lesinskida431a22016-12-29 16:08:16 -0500101TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700102 ResTable_config desired_config;
103 memset(&desired_config, 0, sizeof(desired_config));
104 desired_config.language[0] = 'd';
105 desired_config.language[1] = 'e';
106
107 AssetManager2 assetmanager;
108 assetmanager.SetConfiguration(desired_config);
109 assetmanager.SetApkAssets({basic_assets_.get()});
110
111 Res_value value;
112 ResTable_config selected_config;
113 uint32_t flags;
114
115 ApkAssetsCookie cookie =
116 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
117 0 /*density_override*/, &value, &selected_config, &flags);
118 ASSERT_NE(kInvalidCookie, cookie);
119
120 // Came from our ApkAssets.
121 EXPECT_EQ(0, cookie);
122
123 // It is the default config.
124 EXPECT_EQ(0, selected_config.language[0]);
125 EXPECT_EQ(0, selected_config.language[1]);
126
127 // It is a string.
128 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
129}
130
Adam Lesinskida431a22016-12-29 16:08:16 -0500131TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700132 ResTable_config desired_config;
133 memset(&desired_config, 0, sizeof(desired_config));
134 desired_config.language[0] = 'd';
135 desired_config.language[1] = 'e';
136
137 AssetManager2 assetmanager;
138 assetmanager.SetConfiguration(desired_config);
139 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
140
141 Res_value value;
142 ResTable_config selected_config;
143 uint32_t flags;
144
145 ApkAssetsCookie cookie =
146 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
147 0 /*density_override*/, &value, &selected_config, &flags);
148 ASSERT_NE(kInvalidCookie, cookie);
149
150 // Came from our de_fr ApkAssets.
151 EXPECT_EQ(1, cookie);
152
Adam Lesinskida431a22016-12-29 16:08:16 -0500153 // The configuration is German.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700154 EXPECT_EQ('d', selected_config.language[0]);
155 EXPECT_EQ('e', selected_config.language[1]);
156
157 // It is a string.
158 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
159}
160
Adam Lesinskida431a22016-12-29 16:08:16 -0500161TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) {
162 AssetManager2 assetmanager;
163
164 // libclient is built with lib_one and then lib_two in order.
165 // Reverse the order to test that proper package ID re-assignment is happening.
166 assetmanager.SetApkAssets(
167 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
168
169 Res_value value;
170 ResTable_config selected_config;
171 uint32_t flags;
172
173 ApkAssetsCookie cookie =
174 assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/,
175 0 /*density_override*/, &value, &selected_config, &flags);
176 ASSERT_NE(kInvalidCookie, cookie);
177
178 // Reference comes from libclient.
179 EXPECT_EQ(2, cookie);
180 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
181
182 // Lookup the reference.
183 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
184 &value, &selected_config, &flags);
185 ASSERT_NE(kInvalidCookie, cookie);
186 EXPECT_EQ(1, cookie);
187 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
188 EXPECT_EQ(std::string("Foo from lib_one"),
189 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
190
191 cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/,
192 0 /*density_override*/, &value, &selected_config, &flags);
193 ASSERT_NE(kInvalidCookie, cookie);
194
195 // Reference comes from libclient.
196 EXPECT_EQ(2, cookie);
197 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
198
199 // Lookup the reference.
200 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
201 &value, &selected_config, &flags);
202 ASSERT_NE(kInvalidCookie, cookie);
203 EXPECT_EQ(0, cookie);
204 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
205 EXPECT_EQ(std::string("Foo from lib_two"),
206 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
207}
208
209TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) {
210 AssetManager2 assetmanager;
211 assetmanager.SetApkAssets({appaslib_assets_.get()});
212
213 // The appaslib package will have been assigned the package ID 0x02.
214
215 Res_value value;
216 ResTable_config selected_config;
217 uint32_t flags;
218 ApkAssetsCookie cookie = assetmanager.GetResource(
Adam Lesinski929d6512017-01-16 19:11:19 -0800219 fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
Adam Lesinskida431a22016-12-29 16:08:16 -0500220 0u /*density_override*/, &value, &selected_config, &flags);
221 ASSERT_NE(kInvalidCookie, cookie);
222 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
Adam Lesinski929d6512017-01-16 19:11:19 -0800223 EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
Adam Lesinskida431a22016-12-29 16:08:16 -0500224}
225
Ryan Mitchell824cc492020-02-12 10:48:14 -0800226TEST_F(AssetManager2Test, AssignsOverlayPackageIdLast) {
227 AssetManager2 assetmanager;
228 assetmanager.SetApkAssets(
229 {overlayable_assets_.get(), overlay_assets_.get(), lib_one_assets_.get()});
230
231 auto apk_assets = assetmanager.GetApkAssets();
232 ASSERT_EQ(3, apk_assets.size());
233 ASSERT_EQ(overlayable_assets_.get(), apk_assets[0]);
234 ASSERT_EQ(overlay_assets_.get(), apk_assets[1]);
235 ASSERT_EQ(lib_one_assets_.get(), apk_assets[2]);
236
237 auto get_first_package_id = [&assetmanager](const ApkAssets* apkAssets) -> uint8_t {
238 return assetmanager.GetAssignedPackageId(apkAssets->GetLoadedArsc()->GetPackages()[0].get());
239 };
240
241 ASSERT_EQ(get_first_package_id(overlayable_assets_.get()), 0x7f);
242 ASSERT_EQ(get_first_package_id(overlay_assets_.get()), 0x03);
243 ASSERT_EQ(get_first_package_id(lib_one_assets_.get()), 0x02);
244}
245
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800246TEST_F(AssetManager2Test, GetSharedLibraryResourceName) {
247 AssetManager2 assetmanager;
248 assetmanager.SetApkAssets({lib_one_assets_.get()});
249
250 AssetManager2::ResourceName name;
251 ASSERT_TRUE(assetmanager.GetResourceName(lib_one::R::string::foo, &name));
252 std::string formatted_name = ToFormattedResourceString(&name);
253 ASSERT_EQ(formatted_name, "com.android.lib_one:string/foo");
254}
255
Adam Lesinskida431a22016-12-29 16:08:16 -0500256TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700257 AssetManager2 assetmanager;
258 assetmanager.SetApkAssets({basic_assets_.get()});
259
260 const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1);
261 ASSERT_NE(nullptr, bag);
262 ASSERT_EQ(3u, bag->entry_count);
263
264 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType);
265 EXPECT_EQ(1u, bag->entries[0].value.data);
266 EXPECT_EQ(0, bag->entries[0].cookie);
267
268 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType);
269 EXPECT_EQ(2u, bag->entries[1].value.data);
270 EXPECT_EQ(0, bag->entries[1].cookie);
271
272 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType);
273 EXPECT_EQ(3u, bag->entries[2].value.data);
274 EXPECT_EQ(0, bag->entries[2].cookie);
275}
276
Adam Lesinskida431a22016-12-29 16:08:16 -0500277TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
278
279TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
280 AssetManager2 assetmanager;
281
282 // libclient is built with lib_one and then lib_two in order.
283 // Reverse the order to test that proper package ID re-assignment is happening.
284 assetmanager.SetApkAssets(
285 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
286
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800287 const ResolvedBag* bag = assetmanager.GetBag(fix_package_id(lib_one::R::style::Theme, 0x03));
288 ASSERT_NE(nullptr, bag);
289 ASSERT_GE(bag->entry_count, 2u);
290
291 // First two attributes come from lib_one.
292 EXPECT_EQ(1, bag->entries[0].cookie);
293 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
294 EXPECT_EQ(1, bag->entries[1].cookie);
295 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
296}
297
Ryan Mitchell155d5392020-02-10 13:35:24 -0800298TEST_F(AssetManager2Test, FindsBagResourceFromMultipleSharedLibraries) {
299 AssetManager2 assetmanager;
300
301 // libclient is built with lib_one and then lib_two in order.
302 // Reverse the order to test that proper package ID re-assignment is happening.
303 assetmanager.SetApkAssets(
304 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
305
306 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::ThemeMultiLib);
307 ASSERT_NE(nullptr, bag);
308 ASSERT_EQ(bag->entry_count, 2u);
309
310 // First attribute comes from lib_two.
311 EXPECT_EQ(2, bag->entries[0].cookie);
312 EXPECT_EQ(0x02, get_package_id(bag->entries[0].key));
313
314 // The next two attributes come from lib_one.
315 EXPECT_EQ(2, bag->entries[1].cookie);
316 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
317}
318
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800319TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) {
320 AssetManager2 assetmanager;
321
322 // libclient is built with lib_one and then lib_two in order.
323 // Reverse the order to test that proper package ID re-assignment is happening.
324 assetmanager.SetApkAssets(
325 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
326
Adam Lesinskida431a22016-12-29 16:08:16 -0500327 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
328 ASSERT_NE(nullptr, bag);
329 ASSERT_GE(bag->entry_count, 2u);
330
331 // First two attributes come from lib_one.
332 EXPECT_EQ(1, bag->entries[0].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800333 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500334 EXPECT_EQ(1, bag->entries[1].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800335 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500336}
337
Adam Lesinski7ad11102016-10-28 16:39:15 -0700338TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
339 AssetManager2 assetmanager;
340 assetmanager.SetApkAssets({style_assets_.get()});
341
342 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
343 ASSERT_NE(nullptr, bag_one);
344 ASSERT_EQ(2u, bag_one->entry_count);
345
346 EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
347 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
348 EXPECT_EQ(1u, bag_one->entries[0].value.data);
349 EXPECT_EQ(0, bag_one->entries[0].cookie);
350
351 EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
352 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
353 EXPECT_EQ(2u, bag_one->entries[1].value.data);
354 EXPECT_EQ(0, bag_one->entries[1].cookie);
355
356 const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
357 ASSERT_NE(nullptr, bag_two);
Adam Lesinski32e75012017-05-09 15:25:37 -0700358 ASSERT_EQ(6u, bag_two->entry_count);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700359
360 // attr_one is inherited from StyleOne.
361 EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
362 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
363 EXPECT_EQ(1u, bag_two->entries[0].value.data);
364 EXPECT_EQ(0, bag_two->entries[0].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800365 EXPECT_EQ(app::R::style::StyleOne, bag_two->entries[0].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700366
367 // attr_two should be overridden from StyleOne by StyleTwo.
368 EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
369 EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
370 EXPECT_EQ(0, bag_two->entries[1].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800371 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[1].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700372 EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
373 bag_two->entries[1].value.data));
374
375 // The rest are new attributes.
376
377 EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
378 EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
379 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
380 EXPECT_EQ(0, bag_two->entries[2].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800381 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[2].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700382
383 EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
384 EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
385 EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
386 EXPECT_EQ(0, bag_two->entries[3].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800387 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[3].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700388
389 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
390 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
391 EXPECT_EQ(3u, bag_two->entries[4].value.data);
392 EXPECT_EQ(0, bag_two->entries[4].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800393 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[4].style);
Adam Lesinski32e75012017-05-09 15:25:37 -0700394
395 EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key);
396 EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType);
397 EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data);
398 EXPECT_EQ(0, bag_two->entries[5].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800399 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[5].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700400}
401
y57cd1952018-04-12 14:26:23 -0700402TEST_F(AssetManager2Test, MergeStylesCircularDependency) {
403 AssetManager2 assetmanager;
404 assetmanager.SetApkAssets({style_assets_.get()});
405
406 // GetBag should stop traversing the parents of styles when a circular
407 // dependency is detected
408 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour);
409 ASSERT_NE(nullptr, bag_one);
410 ASSERT_EQ(3u, bag_one->entry_count);
411}
412
Adam Lesinski0c405242017-01-13 20:47:26 -0800413TEST_F(AssetManager2Test, ResolveReferenceToResource) {
414 AssetManager2 assetmanager;
415 assetmanager.SetApkAssets({basic_assets_.get()});
416
417 Res_value value;
418 ResTable_config selected_config;
419 uint32_t flags;
420 ApkAssetsCookie cookie =
421 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
422 0u /*density_override*/, &value, &selected_config, &flags);
423 ASSERT_NE(kInvalidCookie, cookie);
424
425 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
426 EXPECT_EQ(basic::R::integer::ref2, value.data);
427
Adam Lesinski1c855a02017-11-29 09:59:37 -0800428 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800429 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
430 ASSERT_NE(kInvalidCookie, cookie);
431 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
432 EXPECT_EQ(12000u, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800433 EXPECT_EQ(basic::R::integer::ref2, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800434}
435
436TEST_F(AssetManager2Test, ResolveReferenceToBag) {
437 AssetManager2 assetmanager;
438 assetmanager.SetApkAssets({basic_assets_.get()});
439
440 Res_value value;
441 ResTable_config selected_config;
442 uint32_t flags;
443 ApkAssetsCookie cookie =
444 assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
445 0u /*density_override*/, &value, &selected_config, &flags);
446 ASSERT_NE(kInvalidCookie, cookie);
447
448 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
449 EXPECT_EQ(basic::R::array::integerArray1, value.data);
450
Adam Lesinski1c855a02017-11-29 09:59:37 -0800451 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800452 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
453 ASSERT_NE(kInvalidCookie, cookie);
454 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
455 EXPECT_EQ(basic::R::array::integerArray1, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800456 EXPECT_EQ(basic::R::array::integerArray1, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800457}
458
y9efbbef2018-04-18 11:29:09 -0700459TEST_F(AssetManager2Test, ResolveDeepIdReference) {
460 AssetManager2 assetmanager;
461 assetmanager.SetApkAssets({basic_assets_.get()});
462
463 // Set up the resource ids
464 const uint32_t high_ref = assetmanager
465 .GetResourceId("@id/high_ref", "values", "com.android.basic");
466 ASSERT_NE(high_ref, 0u);
467 const uint32_t middle_ref = assetmanager
468 .GetResourceId("@id/middle_ref", "values", "com.android.basic");
469 ASSERT_NE(middle_ref, 0u);
470 const uint32_t low_ref = assetmanager
471 .GetResourceId("@id/low_ref", "values", "com.android.basic");
472 ASSERT_NE(low_ref, 0u);
473
474 // Retrieve the most shallow resource
475 Res_value value;
476 ResTable_config config;
477 uint32_t flags;
478 ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/,
479 0 /*density_override*/,
480 &value, &config, &flags);
481 ASSERT_NE(kInvalidCookie, cookie);
482 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
483 EXPECT_EQ(middle_ref, value.data);
484
485 // Check that resolving the reference resolves to the deepest id
486 uint32_t last_ref = high_ref;
487 assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref);
488 EXPECT_EQ(last_ref, low_ref);
489}
490
Adam Lesinski1c855a02017-11-29 09:59:37 -0800491TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
492 AssetManager2 assetmanager;
493 assetmanager.SetApkAssets({basic_assets_.get()});
494
495 ResTable_config selected_config;
496 memset(&selected_config, 0, sizeof(selected_config));
497
498 uint32_t flags = 0u;
499
500 // Create some kind of Res_value that is NOT a reference.
501 Res_value value;
502 value.dataType = Res_value::TYPE_STRING;
503 value.data = 0;
504
505 uint32_t last_ref = basic::R::string::test1;
506 EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
507 EXPECT_EQ(basic::R::string::test1, last_ref);
508}
509
Adam Lesinski0c405242017-01-13 20:47:26 -0800510static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
511 const ResTable_config& configuration) {
512 return configurations.count(configuration) > 0;
513}
514
515TEST_F(AssetManager2Test, GetResourceConfigurations) {
516 AssetManager2 assetmanager;
517 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
518
519 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
520
521 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
522 // And one extra for the default configuration.
523 EXPECT_EQ(4u, configurations.size());
524
525 ResTable_config expected_config;
526 memset(&expected_config, 0, sizeof(expected_config));
527 expected_config.language[0] = 's';
528 expected_config.language[1] = 'v';
529 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
530
531 expected_config.language[0] = 'd';
532 expected_config.language[1] = 'e';
533 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
534
535 expected_config.language[0] = 'f';
536 expected_config.language[1] = 'r';
537 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
538
539 // Take out the system assets.
540 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
541
542 // We expect de and fr from basic_de_fr assets.
543 EXPECT_EQ(2u, configurations.size());
544
545 expected_config.language[0] = 's';
546 expected_config.language[1] = 'v';
547 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
548
549 expected_config.language[0] = 'd';
550 expected_config.language[1] = 'e';
551 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
552
553 expected_config.language[0] = 'f';
554 expected_config.language[1] = 'r';
555 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
556}
557
558TEST_F(AssetManager2Test, GetResourceLocales) {
559 AssetManager2 assetmanager;
560 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
561
562 std::set<std::string> locales = assetmanager.GetResourceLocales();
563
564 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
565 EXPECT_EQ(3u, locales.size());
566 EXPECT_GT(locales.count("sv"), 0u);
567 EXPECT_GT(locales.count("de"), 0u);
568 EXPECT_GT(locales.count("fr"), 0u);
569
570 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
571 // We expect the de and fr locales from basic_de_fr assets.
572 EXPECT_EQ(2u, locales.size());
573 EXPECT_GT(locales.count("de"), 0u);
574 EXPECT_GT(locales.count("fr"), 0u);
575}
576
577TEST_F(AssetManager2Test, GetResourceId) {
578 AssetManager2 assetmanager;
579 assetmanager.SetApkAssets({basic_assets_.get()});
580
581 EXPECT_EQ(basic::R::layout::main,
582 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
583 EXPECT_EQ(basic::R::layout::main,
584 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
585 EXPECT_EQ(basic::R::layout::main,
586 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
587}
588
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800589TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
590 AssetManager2 assetmanager;
591 assetmanager.SetApkAssets({system_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700592
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800593 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
594 ASSERT_THAT(asset, NotNull());
595
596 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
597 ASSERT_THAT(data, NotNull());
598 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
599}
600
601TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
602 AssetManager2 assetmanager;
603 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
604
605 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
606 ASSERT_THAT(asset, NotNull());
607
608 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
609 ASSERT_THAT(data, NotNull());
610 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
611}
612
613TEST_F(AssetManager2Test, OpenDir) {
614 AssetManager2 assetmanager;
615 assetmanager.SetApkAssets({system_assets_.get()});
616
617 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
618 ASSERT_THAT(asset_dir, NotNull());
619 ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
620
621 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
622 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
623
624 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
625 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
626
627 asset_dir = assetmanager.OpenDir("subdir");
628 ASSERT_THAT(asset_dir, NotNull());
629 ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
630
631 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
632 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
633}
634
635TEST_F(AssetManager2Test, OpenDirFromManyApks) {
636 AssetManager2 assetmanager;
637 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
638
639 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
640 ASSERT_THAT(asset_dir, NotNull());
641 ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
642
643 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
644 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
645
646 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
647 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
648
649 EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
650 EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
651}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700652
Winson2f3669b2019-01-11 11:28:34 -0800653TEST_F(AssetManager2Test, GetLastPathWithoutEnablingReturnsEmpty) {
654 ResTable_config desired_config;
655
656 AssetManager2 assetmanager;
657 assetmanager.SetConfiguration(desired_config);
658 assetmanager.SetApkAssets({basic_assets_.get()});
659 assetmanager.SetResourceResolutionLoggingEnabled(false);
660
661 Res_value value;
662 ResTable_config selected_config;
663 uint32_t flags;
664
665 ApkAssetsCookie cookie =
666 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
667 0 /*density_override*/, &value, &selected_config, &flags);
668 ASSERT_NE(kInvalidCookie, cookie);
669
670 auto result = assetmanager.GetLastResourceResolution();
671 EXPECT_EQ("", result);
672}
673
674TEST_F(AssetManager2Test, GetLastPathWithoutResolutionReturnsEmpty) {
675 ResTable_config desired_config;
676
677 AssetManager2 assetmanager;
678 assetmanager.SetConfiguration(desired_config);
679 assetmanager.SetApkAssets({basic_assets_.get()});
680
681 auto result = assetmanager.GetLastResourceResolution();
682 EXPECT_EQ("", result);
683}
684
685TEST_F(AssetManager2Test, GetLastPathWithSingleApkAssets) {
686 ResTable_config desired_config;
687 memset(&desired_config, 0, sizeof(desired_config));
688 desired_config.language[0] = 'd';
689 desired_config.language[1] = 'e';
690
691 AssetManager2 assetmanager;
692 assetmanager.SetResourceResolutionLoggingEnabled(true);
693 assetmanager.SetConfiguration(desired_config);
694 assetmanager.SetApkAssets({basic_assets_.get()});
695
696 Res_value value;
697 ResTable_config selected_config;
698 uint32_t flags;
699
700 ApkAssetsCookie cookie =
701 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
702 0 /*density_override*/, &value, &selected_config, &flags);
703 ASSERT_NE(kInvalidCookie, cookie);
704
705 auto result = assetmanager.GetLastResourceResolution();
706 EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic", result);
707}
708
709TEST_F(AssetManager2Test, GetLastPathWithMultipleApkAssets) {
710 ResTable_config desired_config;
711 memset(&desired_config, 0, sizeof(desired_config));
712 desired_config.language[0] = 'd';
713 desired_config.language[1] = 'e';
714
715 AssetManager2 assetmanager;
716 assetmanager.SetResourceResolutionLoggingEnabled(true);
717 assetmanager.SetConfiguration(desired_config);
718 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
719
720 Res_value value = Res_value();
721 ResTable_config selected_config;
722 uint32_t flags;
723
724 ApkAssetsCookie cookie =
725 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
726 0 /*density_override*/, &value, &selected_config, &flags);
727 ASSERT_NE(kInvalidCookie, cookie);
728
729 auto result = assetmanager.GetLastResourceResolution();
730 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);
731}
732
733TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) {
734 ResTable_config desired_config;
735 memset(&desired_config, 0, sizeof(desired_config));
736
737 AssetManager2 assetmanager;
738 assetmanager.SetResourceResolutionLoggingEnabled(true);
739 assetmanager.SetConfiguration(desired_config);
740 assetmanager.SetApkAssets({basic_assets_.get()});
741
742 Res_value value = Res_value();
743 ResTable_config selected_config;
744 uint32_t flags;
745
746 ApkAssetsCookie cookie =
747 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
748 0 /*density_override*/, &value, &selected_config, &flags);
749 ASSERT_NE(kInvalidCookie, cookie);
750
751 auto resultEnabled = assetmanager.GetLastResourceResolution();
752 ASSERT_NE("", resultEnabled);
753
754 assetmanager.SetResourceResolutionLoggingEnabled(false);
755
756 auto resultDisabled = assetmanager.GetLastResourceResolution();
757 EXPECT_EQ("", resultDisabled);
758}
759
Ryan Mitchell2e394222019-08-28 12:10:51 -0700760TEST_F(AssetManager2Test, GetOverlayablesToString) {
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100761 ResTable_config desired_config;
762 memset(&desired_config, 0, sizeof(desired_config));
763
764 AssetManager2 assetmanager;
765 assetmanager.SetResourceResolutionLoggingEnabled(true);
766 assetmanager.SetConfiguration(desired_config);
767 assetmanager.SetApkAssets({overlayable_assets_.get()});
768
769 const auto map = assetmanager.GetOverlayableMapForPackage(0x7f);
770 ASSERT_NE(nullptr, map);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700771 ASSERT_EQ(3, map->size());
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100772 ASSERT_EQ(map->at("OverlayableResources1"), "overlay://theme");
773 ASSERT_EQ(map->at("OverlayableResources2"), "overlay://com.android.overlayable");
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700774 ASSERT_EQ(map->at("OverlayableResources3"), "");
Ryan Mitchell2e394222019-08-28 12:10:51 -0700775
776 std::string api;
777 ASSERT_TRUE(assetmanager.GetOverlayablesToString("com.android.overlayable", &api));
778 ASSERT_EQ(api.find("not_overlayable"), std::string::npos);
779 ASSERT_NE(api.find("resource='com.android.overlayable:string/overlayable2' overlayable='OverlayableResources1' actor='overlay://theme' policy='0x0000000a'\n"),
780 std::string::npos);
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100781}
782
Adam Lesinski7ad11102016-10-28 16:39:15 -0700783} // namespace android