blob: 447fdf5d306a6e1ed1e4693e5d783e5cac2b6261 [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());
Adam Lesinski7ad11102016-10-28 16:39:15 -070074 }
75
76 protected:
Adam Lesinski0c405242017-01-13 20:47:26 -080077 std::unique_ptr<const ApkAssets> basic_assets_;
78 std::unique_ptr<const ApkAssets> basic_de_fr_assets_;
79 std::unique_ptr<const ApkAssets> style_assets_;
80 std::unique_ptr<const ApkAssets> lib_one_assets_;
81 std::unique_ptr<const ApkAssets> lib_two_assets_;
82 std::unique_ptr<const ApkAssets> libclient_assets_;
83 std::unique_ptr<const ApkAssets> appaslib_assets_;
84 std::unique_ptr<const ApkAssets> system_assets_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080085 std::unique_ptr<const ApkAssets> app_assets_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070086};
87
Adam Lesinskida431a22016-12-29 16:08:16 -050088TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070089 ResTable_config desired_config;
90 memset(&desired_config, 0, sizeof(desired_config));
91 desired_config.language[0] = 'd';
92 desired_config.language[1] = 'e';
93
94 AssetManager2 assetmanager;
95 assetmanager.SetConfiguration(desired_config);
96 assetmanager.SetApkAssets({basic_assets_.get()});
97
98 Res_value value;
99 ResTable_config selected_config;
100 uint32_t flags;
101
102 ApkAssetsCookie cookie =
103 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
104 0 /*density_override*/, &value, &selected_config, &flags);
105 ASSERT_NE(kInvalidCookie, cookie);
106
107 // Came from our ApkAssets.
108 EXPECT_EQ(0, cookie);
109
110 // It is the default config.
111 EXPECT_EQ(0, selected_config.language[0]);
112 EXPECT_EQ(0, selected_config.language[1]);
113
114 // It is a string.
115 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
116}
117
Adam Lesinskida431a22016-12-29 16:08:16 -0500118TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700119 ResTable_config desired_config;
120 memset(&desired_config, 0, sizeof(desired_config));
121 desired_config.language[0] = 'd';
122 desired_config.language[1] = 'e';
123
124 AssetManager2 assetmanager;
125 assetmanager.SetConfiguration(desired_config);
126 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
127
128 Res_value value;
129 ResTable_config selected_config;
130 uint32_t flags;
131
132 ApkAssetsCookie cookie =
133 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
134 0 /*density_override*/, &value, &selected_config, &flags);
135 ASSERT_NE(kInvalidCookie, cookie);
136
137 // Came from our de_fr ApkAssets.
138 EXPECT_EQ(1, cookie);
139
Adam Lesinskida431a22016-12-29 16:08:16 -0500140 // The configuration is German.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700141 EXPECT_EQ('d', selected_config.language[0]);
142 EXPECT_EQ('e', selected_config.language[1]);
143
144 // It is a string.
145 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
146}
147
Adam Lesinskida431a22016-12-29 16:08:16 -0500148TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) {
149 AssetManager2 assetmanager;
150
151 // libclient is built with lib_one and then lib_two in order.
152 // Reverse the order to test that proper package ID re-assignment is happening.
153 assetmanager.SetApkAssets(
154 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
155
156 Res_value value;
157 ResTable_config selected_config;
158 uint32_t flags;
159
160 ApkAssetsCookie cookie =
161 assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/,
162 0 /*density_override*/, &value, &selected_config, &flags);
163 ASSERT_NE(kInvalidCookie, cookie);
164
165 // Reference comes from libclient.
166 EXPECT_EQ(2, cookie);
167 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
168
169 // Lookup the reference.
170 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
171 &value, &selected_config, &flags);
172 ASSERT_NE(kInvalidCookie, cookie);
173 EXPECT_EQ(1, cookie);
174 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
175 EXPECT_EQ(std::string("Foo from lib_one"),
176 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
177
178 cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/,
179 0 /*density_override*/, &value, &selected_config, &flags);
180 ASSERT_NE(kInvalidCookie, cookie);
181
182 // Reference comes from libclient.
183 EXPECT_EQ(2, cookie);
184 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
185
186 // Lookup the reference.
187 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
188 &value, &selected_config, &flags);
189 ASSERT_NE(kInvalidCookie, cookie);
190 EXPECT_EQ(0, cookie);
191 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
192 EXPECT_EQ(std::string("Foo from lib_two"),
193 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
194}
195
196TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) {
197 AssetManager2 assetmanager;
198 assetmanager.SetApkAssets({appaslib_assets_.get()});
199
200 // The appaslib package will have been assigned the package ID 0x02.
201
202 Res_value value;
203 ResTable_config selected_config;
204 uint32_t flags;
205 ApkAssetsCookie cookie = assetmanager.GetResource(
Adam Lesinski929d6512017-01-16 19:11:19 -0800206 fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
Adam Lesinskida431a22016-12-29 16:08:16 -0500207 0u /*density_override*/, &value, &selected_config, &flags);
208 ASSERT_NE(kInvalidCookie, cookie);
209 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
Adam Lesinski929d6512017-01-16 19:11:19 -0800210 EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
Adam Lesinskida431a22016-12-29 16:08:16 -0500211}
212
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800213TEST_F(AssetManager2Test, GetSharedLibraryResourceName) {
214 AssetManager2 assetmanager;
215 assetmanager.SetApkAssets({lib_one_assets_.get()});
216
217 AssetManager2::ResourceName name;
218 ASSERT_TRUE(assetmanager.GetResourceName(lib_one::R::string::foo, &name));
219 std::string formatted_name = ToFormattedResourceString(&name);
220 ASSERT_EQ(formatted_name, "com.android.lib_one:string/foo");
221}
222
Adam Lesinskida431a22016-12-29 16:08:16 -0500223TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700224 AssetManager2 assetmanager;
225 assetmanager.SetApkAssets({basic_assets_.get()});
226
227 const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1);
228 ASSERT_NE(nullptr, bag);
229 ASSERT_EQ(3u, bag->entry_count);
230
231 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType);
232 EXPECT_EQ(1u, bag->entries[0].value.data);
233 EXPECT_EQ(0, bag->entries[0].cookie);
234
235 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType);
236 EXPECT_EQ(2u, bag->entries[1].value.data);
237 EXPECT_EQ(0, bag->entries[1].cookie);
238
239 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType);
240 EXPECT_EQ(3u, bag->entries[2].value.data);
241 EXPECT_EQ(0, bag->entries[2].cookie);
242}
243
Adam Lesinskida431a22016-12-29 16:08:16 -0500244TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
245
246TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
247 AssetManager2 assetmanager;
248
249 // libclient is built with lib_one and then lib_two in order.
250 // Reverse the order to test that proper package ID re-assignment is happening.
251 assetmanager.SetApkAssets(
252 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
253
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800254 const ResolvedBag* bag = assetmanager.GetBag(fix_package_id(lib_one::R::style::Theme, 0x03));
255 ASSERT_NE(nullptr, bag);
256 ASSERT_GE(bag->entry_count, 2u);
257
258 // First two attributes come from lib_one.
259 EXPECT_EQ(1, bag->entries[0].cookie);
260 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
261 EXPECT_EQ(1, bag->entries[1].cookie);
262 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
263}
264
265TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) {
266 AssetManager2 assetmanager;
267
268 // libclient is built with lib_one and then lib_two in order.
269 // Reverse the order to test that proper package ID re-assignment is happening.
270 assetmanager.SetApkAssets(
271 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
272
Adam Lesinskida431a22016-12-29 16:08:16 -0500273 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
274 ASSERT_NE(nullptr, bag);
275 ASSERT_GE(bag->entry_count, 2u);
276
277 // First two attributes come from lib_one.
278 EXPECT_EQ(1, bag->entries[0].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800279 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500280 EXPECT_EQ(1, bag->entries[1].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800281 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500282}
283
Adam Lesinski7ad11102016-10-28 16:39:15 -0700284TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
285 AssetManager2 assetmanager;
286 assetmanager.SetApkAssets({style_assets_.get()});
287
288 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
289 ASSERT_NE(nullptr, bag_one);
290 ASSERT_EQ(2u, bag_one->entry_count);
291
292 EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
293 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
294 EXPECT_EQ(1u, bag_one->entries[0].value.data);
295 EXPECT_EQ(0, bag_one->entries[0].cookie);
296
297 EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
298 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
299 EXPECT_EQ(2u, bag_one->entries[1].value.data);
300 EXPECT_EQ(0, bag_one->entries[1].cookie);
301
302 const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
303 ASSERT_NE(nullptr, bag_two);
Adam Lesinski32e75012017-05-09 15:25:37 -0700304 ASSERT_EQ(6u, bag_two->entry_count);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700305
306 // attr_one is inherited from StyleOne.
307 EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
308 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
309 EXPECT_EQ(1u, bag_two->entries[0].value.data);
310 EXPECT_EQ(0, bag_two->entries[0].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800311 EXPECT_EQ(app::R::style::StyleOne, bag_two->entries[0].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700312
313 // attr_two should be overridden from StyleOne by StyleTwo.
314 EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
315 EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
316 EXPECT_EQ(0, bag_two->entries[1].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800317 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[1].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700318 EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
319 bag_two->entries[1].value.data));
320
321 // The rest are new attributes.
322
323 EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
324 EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
325 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
326 EXPECT_EQ(0, bag_two->entries[2].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800327 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[2].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700328
329 EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
330 EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
331 EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
332 EXPECT_EQ(0, bag_two->entries[3].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800333 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[3].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700334
335 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
336 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
337 EXPECT_EQ(3u, bag_two->entries[4].value.data);
338 EXPECT_EQ(0, bag_two->entries[4].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800339 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[4].style);
Adam Lesinski32e75012017-05-09 15:25:37 -0700340
341 EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key);
342 EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType);
343 EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data);
344 EXPECT_EQ(0, bag_two->entries[5].cookie);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -0800345 EXPECT_EQ(app::R::style::StyleTwo, bag_two->entries[5].style);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700346}
347
y57cd1952018-04-12 14:26:23 -0700348TEST_F(AssetManager2Test, MergeStylesCircularDependency) {
349 AssetManager2 assetmanager;
350 assetmanager.SetApkAssets({style_assets_.get()});
351
352 // GetBag should stop traversing the parents of styles when a circular
353 // dependency is detected
354 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour);
355 ASSERT_NE(nullptr, bag_one);
356 ASSERT_EQ(3u, bag_one->entry_count);
357}
358
Adam Lesinski0c405242017-01-13 20:47:26 -0800359TEST_F(AssetManager2Test, ResolveReferenceToResource) {
360 AssetManager2 assetmanager;
361 assetmanager.SetApkAssets({basic_assets_.get()});
362
363 Res_value value;
364 ResTable_config selected_config;
365 uint32_t flags;
366 ApkAssetsCookie cookie =
367 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
368 0u /*density_override*/, &value, &selected_config, &flags);
369 ASSERT_NE(kInvalidCookie, cookie);
370
371 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
372 EXPECT_EQ(basic::R::integer::ref2, value.data);
373
Adam Lesinski1c855a02017-11-29 09:59:37 -0800374 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800375 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
376 ASSERT_NE(kInvalidCookie, cookie);
377 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
378 EXPECT_EQ(12000u, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800379 EXPECT_EQ(basic::R::integer::ref2, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800380}
381
382TEST_F(AssetManager2Test, ResolveReferenceToBag) {
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::number2, true /*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::array::integerArray1, 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_REFERENCE, value.dataType);
401 EXPECT_EQ(basic::R::array::integerArray1, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800402 EXPECT_EQ(basic::R::array::integerArray1, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800403}
404
y9efbbef2018-04-18 11:29:09 -0700405TEST_F(AssetManager2Test, ResolveDeepIdReference) {
406 AssetManager2 assetmanager;
407 assetmanager.SetApkAssets({basic_assets_.get()});
408
409 // Set up the resource ids
410 const uint32_t high_ref = assetmanager
411 .GetResourceId("@id/high_ref", "values", "com.android.basic");
412 ASSERT_NE(high_ref, 0u);
413 const uint32_t middle_ref = assetmanager
414 .GetResourceId("@id/middle_ref", "values", "com.android.basic");
415 ASSERT_NE(middle_ref, 0u);
416 const uint32_t low_ref = assetmanager
417 .GetResourceId("@id/low_ref", "values", "com.android.basic");
418 ASSERT_NE(low_ref, 0u);
419
420 // Retrieve the most shallow resource
421 Res_value value;
422 ResTable_config config;
423 uint32_t flags;
424 ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/,
425 0 /*density_override*/,
426 &value, &config, &flags);
427 ASSERT_NE(kInvalidCookie, cookie);
428 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
429 EXPECT_EQ(middle_ref, value.data);
430
431 // Check that resolving the reference resolves to the deepest id
432 uint32_t last_ref = high_ref;
433 assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref);
434 EXPECT_EQ(last_ref, low_ref);
435}
436
Adam Lesinski1c855a02017-11-29 09:59:37 -0800437TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
438 AssetManager2 assetmanager;
439 assetmanager.SetApkAssets({basic_assets_.get()});
440
441 ResTable_config selected_config;
442 memset(&selected_config, 0, sizeof(selected_config));
443
444 uint32_t flags = 0u;
445
446 // Create some kind of Res_value that is NOT a reference.
447 Res_value value;
448 value.dataType = Res_value::TYPE_STRING;
449 value.data = 0;
450
451 uint32_t last_ref = basic::R::string::test1;
452 EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
453 EXPECT_EQ(basic::R::string::test1, last_ref);
454}
455
Adam Lesinski0c405242017-01-13 20:47:26 -0800456static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
457 const ResTable_config& configuration) {
458 return configurations.count(configuration) > 0;
459}
460
461TEST_F(AssetManager2Test, GetResourceConfigurations) {
462 AssetManager2 assetmanager;
463 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
464
465 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
466
467 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
468 // And one extra for the default configuration.
469 EXPECT_EQ(4u, configurations.size());
470
471 ResTable_config expected_config;
472 memset(&expected_config, 0, sizeof(expected_config));
473 expected_config.language[0] = 's';
474 expected_config.language[1] = 'v';
475 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
476
477 expected_config.language[0] = 'd';
478 expected_config.language[1] = 'e';
479 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
480
481 expected_config.language[0] = 'f';
482 expected_config.language[1] = 'r';
483 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
484
485 // Take out the system assets.
486 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
487
488 // We expect de and fr from basic_de_fr assets.
489 EXPECT_EQ(2u, configurations.size());
490
491 expected_config.language[0] = 's';
492 expected_config.language[1] = 'v';
493 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
494
495 expected_config.language[0] = 'd';
496 expected_config.language[1] = 'e';
497 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
498
499 expected_config.language[0] = 'f';
500 expected_config.language[1] = 'r';
501 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
502}
503
504TEST_F(AssetManager2Test, GetResourceLocales) {
505 AssetManager2 assetmanager;
506 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
507
508 std::set<std::string> locales = assetmanager.GetResourceLocales();
509
510 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
511 EXPECT_EQ(3u, locales.size());
512 EXPECT_GT(locales.count("sv"), 0u);
513 EXPECT_GT(locales.count("de"), 0u);
514 EXPECT_GT(locales.count("fr"), 0u);
515
516 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
517 // We expect the de and fr locales from basic_de_fr assets.
518 EXPECT_EQ(2u, locales.size());
519 EXPECT_GT(locales.count("de"), 0u);
520 EXPECT_GT(locales.count("fr"), 0u);
521}
522
523TEST_F(AssetManager2Test, GetResourceId) {
524 AssetManager2 assetmanager;
525 assetmanager.SetApkAssets({basic_assets_.get()});
526
527 EXPECT_EQ(basic::R::layout::main,
528 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
529 EXPECT_EQ(basic::R::layout::main,
530 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
531 EXPECT_EQ(basic::R::layout::main,
532 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
533}
534
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800535TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
536 AssetManager2 assetmanager;
537 assetmanager.SetApkAssets({system_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700538
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800539 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
540 ASSERT_THAT(asset, NotNull());
541
542 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
543 ASSERT_THAT(data, NotNull());
544 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
545}
546
547TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
548 AssetManager2 assetmanager;
549 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
550
551 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
552 ASSERT_THAT(asset, NotNull());
553
554 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
555 ASSERT_THAT(data, NotNull());
556 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
557}
558
559TEST_F(AssetManager2Test, OpenDir) {
560 AssetManager2 assetmanager;
561 assetmanager.SetApkAssets({system_assets_.get()});
562
563 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
564 ASSERT_THAT(asset_dir, NotNull());
565 ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
566
567 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
568 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
569
570 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
571 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
572
573 asset_dir = assetmanager.OpenDir("subdir");
574 ASSERT_THAT(asset_dir, NotNull());
575 ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
576
577 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
578 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
579}
580
581TEST_F(AssetManager2Test, OpenDirFromManyApks) {
582 AssetManager2 assetmanager;
583 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
584
585 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
586 ASSERT_THAT(asset_dir, NotNull());
587 ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
588
589 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
590 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
591
592 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
593 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
594
595 EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
596 EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
597}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700598
Winson2f3669b2019-01-11 11:28:34 -0800599TEST_F(AssetManager2Test, GetLastPathWithoutEnablingReturnsEmpty) {
600 ResTable_config desired_config;
601
602 AssetManager2 assetmanager;
603 assetmanager.SetConfiguration(desired_config);
604 assetmanager.SetApkAssets({basic_assets_.get()});
605 assetmanager.SetResourceResolutionLoggingEnabled(false);
606
607 Res_value value;
608 ResTable_config selected_config;
609 uint32_t flags;
610
611 ApkAssetsCookie cookie =
612 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
613 0 /*density_override*/, &value, &selected_config, &flags);
614 ASSERT_NE(kInvalidCookie, cookie);
615
616 auto result = assetmanager.GetLastResourceResolution();
617 EXPECT_EQ("", result);
618}
619
620TEST_F(AssetManager2Test, GetLastPathWithoutResolutionReturnsEmpty) {
621 ResTable_config desired_config;
622
623 AssetManager2 assetmanager;
624 assetmanager.SetConfiguration(desired_config);
625 assetmanager.SetApkAssets({basic_assets_.get()});
626
627 auto result = assetmanager.GetLastResourceResolution();
628 EXPECT_EQ("", result);
629}
630
631TEST_F(AssetManager2Test, GetLastPathWithSingleApkAssets) {
632 ResTable_config desired_config;
633 memset(&desired_config, 0, sizeof(desired_config));
634 desired_config.language[0] = 'd';
635 desired_config.language[1] = 'e';
636
637 AssetManager2 assetmanager;
638 assetmanager.SetResourceResolutionLoggingEnabled(true);
639 assetmanager.SetConfiguration(desired_config);
640 assetmanager.SetApkAssets({basic_assets_.get()});
641
642 Res_value value;
643 ResTable_config selected_config;
644 uint32_t flags;
645
646 ApkAssetsCookie cookie =
647 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
648 0 /*density_override*/, &value, &selected_config, &flags);
649 ASSERT_NE(kInvalidCookie, cookie);
650
651 auto result = assetmanager.GetLastResourceResolution();
652 EXPECT_EQ("Resolution for 0x7f030000 com.android.basic:string/test1\n\tFor config -de\n\tFound initial: com.android.basic", result);
653}
654
655TEST_F(AssetManager2Test, GetLastPathWithMultipleApkAssets) {
656 ResTable_config desired_config;
657 memset(&desired_config, 0, sizeof(desired_config));
658 desired_config.language[0] = 'd';
659 desired_config.language[1] = 'e';
660
661 AssetManager2 assetmanager;
662 assetmanager.SetResourceResolutionLoggingEnabled(true);
663 assetmanager.SetConfiguration(desired_config);
664 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
665
666 Res_value value = Res_value();
667 ResTable_config selected_config;
668 uint32_t flags;
669
670 ApkAssetsCookie cookie =
671 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
672 0 /*density_override*/, &value, &selected_config, &flags);
673 ASSERT_NE(kInvalidCookie, cookie);
674
675 auto result = assetmanager.GetLastResourceResolution();
676 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);
677}
678
679TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) {
680 ResTable_config desired_config;
681 memset(&desired_config, 0, sizeof(desired_config));
682
683 AssetManager2 assetmanager;
684 assetmanager.SetResourceResolutionLoggingEnabled(true);
685 assetmanager.SetConfiguration(desired_config);
686 assetmanager.SetApkAssets({basic_assets_.get()});
687
688 Res_value value = Res_value();
689 ResTable_config selected_config;
690 uint32_t flags;
691
692 ApkAssetsCookie cookie =
693 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
694 0 /*density_override*/, &value, &selected_config, &flags);
695 ASSERT_NE(kInvalidCookie, cookie);
696
697 auto resultEnabled = assetmanager.GetLastResourceResolution();
698 ASSERT_NE("", resultEnabled);
699
700 assetmanager.SetResourceResolutionLoggingEnabled(false);
701
702 auto resultDisabled = assetmanager.GetLastResourceResolution();
703 EXPECT_EQ("", resultDisabled);
704}
705
Adam Lesinski7ad11102016-10-28 16:39:15 -0700706} // namespace android