blob: 7cac2b3417b57c0efafd75ebe7a6d9aebf0b160e [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
213TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700214 AssetManager2 assetmanager;
215 assetmanager.SetApkAssets({basic_assets_.get()});
216
217 const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1);
218 ASSERT_NE(nullptr, bag);
219 ASSERT_EQ(3u, bag->entry_count);
220
221 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType);
222 EXPECT_EQ(1u, bag->entries[0].value.data);
223 EXPECT_EQ(0, bag->entries[0].cookie);
224
225 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType);
226 EXPECT_EQ(2u, bag->entries[1].value.data);
227 EXPECT_EQ(0, bag->entries[1].cookie);
228
229 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType);
230 EXPECT_EQ(3u, bag->entries[2].value.data);
231 EXPECT_EQ(0, bag->entries[2].cookie);
232}
233
Adam Lesinskida431a22016-12-29 16:08:16 -0500234TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
235
236TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
237 AssetManager2 assetmanager;
238
239 // libclient is built with lib_one and then lib_two in order.
240 // Reverse the order to test that proper package ID re-assignment is happening.
241 assetmanager.SetApkAssets(
242 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
243
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800244 const ResolvedBag* bag = assetmanager.GetBag(fix_package_id(lib_one::R::style::Theme, 0x03));
245 ASSERT_NE(nullptr, bag);
246 ASSERT_GE(bag->entry_count, 2u);
247
248 // First two attributes come from lib_one.
249 EXPECT_EQ(1, bag->entries[0].cookie);
250 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
251 EXPECT_EQ(1, bag->entries[1].cookie);
252 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
253}
254
255TEST_F(AssetManager2Test, FindsStyleResourceWithParentFromSharedLibrary) {
256 AssetManager2 assetmanager;
257
258 // libclient is built with lib_one and then lib_two in order.
259 // Reverse the order to test that proper package ID re-assignment is happening.
260 assetmanager.SetApkAssets(
261 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
262
Adam Lesinskida431a22016-12-29 16:08:16 -0500263 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
264 ASSERT_NE(nullptr, bag);
265 ASSERT_GE(bag->entry_count, 2u);
266
267 // First two attributes come from lib_one.
268 EXPECT_EQ(1, bag->entries[0].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800269 EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500270 EXPECT_EQ(1, bag->entries[1].cookie);
Adam Lesinski929d6512017-01-16 19:11:19 -0800271 EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
Adam Lesinskida431a22016-12-29 16:08:16 -0500272}
273
Adam Lesinski7ad11102016-10-28 16:39:15 -0700274TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
275 AssetManager2 assetmanager;
276 assetmanager.SetApkAssets({style_assets_.get()});
277
278 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
279 ASSERT_NE(nullptr, bag_one);
280 ASSERT_EQ(2u, bag_one->entry_count);
281
282 EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
283 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
284 EXPECT_EQ(1u, bag_one->entries[0].value.data);
285 EXPECT_EQ(0, bag_one->entries[0].cookie);
286
287 EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
288 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
289 EXPECT_EQ(2u, bag_one->entries[1].value.data);
290 EXPECT_EQ(0, bag_one->entries[1].cookie);
291
292 const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
293 ASSERT_NE(nullptr, bag_two);
Adam Lesinski32e75012017-05-09 15:25:37 -0700294 ASSERT_EQ(6u, bag_two->entry_count);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700295
296 // attr_one is inherited from StyleOne.
297 EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
298 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
299 EXPECT_EQ(1u, bag_two->entries[0].value.data);
300 EXPECT_EQ(0, bag_two->entries[0].cookie);
301
302 // attr_two should be overridden from StyleOne by StyleTwo.
303 EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
304 EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
305 EXPECT_EQ(0, bag_two->entries[1].cookie);
306 EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
307 bag_two->entries[1].value.data));
308
309 // The rest are new attributes.
310
311 EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
312 EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
313 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
314 EXPECT_EQ(0, bag_two->entries[2].cookie);
315
316 EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
317 EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
318 EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
319 EXPECT_EQ(0, bag_two->entries[3].cookie);
320
321 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
322 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
323 EXPECT_EQ(3u, bag_two->entries[4].value.data);
324 EXPECT_EQ(0, bag_two->entries[4].cookie);
Adam Lesinski32e75012017-05-09 15:25:37 -0700325
326 EXPECT_EQ(app::R::attr::attr_empty, bag_two->entries[5].key);
327 EXPECT_EQ(Res_value::TYPE_NULL, bag_two->entries[5].value.dataType);
328 EXPECT_EQ(Res_value::DATA_NULL_EMPTY, bag_two->entries[5].value.data);
329 EXPECT_EQ(0, bag_two->entries[5].cookie);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700330}
331
Adam Lesinski0c405242017-01-13 20:47:26 -0800332TEST_F(AssetManager2Test, ResolveReferenceToResource) {
333 AssetManager2 assetmanager;
334 assetmanager.SetApkAssets({basic_assets_.get()});
335
336 Res_value value;
337 ResTable_config selected_config;
338 uint32_t flags;
339 ApkAssetsCookie cookie =
340 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
341 0u /*density_override*/, &value, &selected_config, &flags);
342 ASSERT_NE(kInvalidCookie, cookie);
343
344 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
345 EXPECT_EQ(basic::R::integer::ref2, value.data);
346
Adam Lesinski1c855a02017-11-29 09:59:37 -0800347 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800348 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
349 ASSERT_NE(kInvalidCookie, cookie);
350 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
351 EXPECT_EQ(12000u, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800352 EXPECT_EQ(basic::R::integer::ref2, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800353}
354
355TEST_F(AssetManager2Test, ResolveReferenceToBag) {
356 AssetManager2 assetmanager;
357 assetmanager.SetApkAssets({basic_assets_.get()});
358
359 Res_value value;
360 ResTable_config selected_config;
361 uint32_t flags;
362 ApkAssetsCookie cookie =
363 assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
364 0u /*density_override*/, &value, &selected_config, &flags);
365 ASSERT_NE(kInvalidCookie, cookie);
366
367 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
368 EXPECT_EQ(basic::R::array::integerArray1, value.data);
369
Adam Lesinski1c855a02017-11-29 09:59:37 -0800370 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800371 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
372 ASSERT_NE(kInvalidCookie, cookie);
373 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
374 EXPECT_EQ(basic::R::array::integerArray1, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800375 EXPECT_EQ(basic::R::array::integerArray1, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800376}
377
Adam Lesinski1c855a02017-11-29 09:59:37 -0800378TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
379 AssetManager2 assetmanager;
380 assetmanager.SetApkAssets({basic_assets_.get()});
381
382 ResTable_config selected_config;
383 memset(&selected_config, 0, sizeof(selected_config));
384
385 uint32_t flags = 0u;
386
387 // Create some kind of Res_value that is NOT a reference.
388 Res_value value;
389 value.dataType = Res_value::TYPE_STRING;
390 value.data = 0;
391
392 uint32_t last_ref = basic::R::string::test1;
393 EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
394 EXPECT_EQ(basic::R::string::test1, last_ref);
395}
396
Adam Lesinski0c405242017-01-13 20:47:26 -0800397static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
398 const ResTable_config& configuration) {
399 return configurations.count(configuration) > 0;
400}
401
402TEST_F(AssetManager2Test, GetResourceConfigurations) {
403 AssetManager2 assetmanager;
404 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
405
406 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
407
408 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
409 // And one extra for the default configuration.
410 EXPECT_EQ(4u, configurations.size());
411
412 ResTable_config expected_config;
413 memset(&expected_config, 0, sizeof(expected_config));
414 expected_config.language[0] = 's';
415 expected_config.language[1] = 'v';
416 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
417
418 expected_config.language[0] = 'd';
419 expected_config.language[1] = 'e';
420 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
421
422 expected_config.language[0] = 'f';
423 expected_config.language[1] = 'r';
424 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
425
426 // Take out the system assets.
427 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
428
429 // We expect de and fr from basic_de_fr assets.
430 EXPECT_EQ(2u, configurations.size());
431
432 expected_config.language[0] = 's';
433 expected_config.language[1] = 'v';
434 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
435
436 expected_config.language[0] = 'd';
437 expected_config.language[1] = 'e';
438 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
439
440 expected_config.language[0] = 'f';
441 expected_config.language[1] = 'r';
442 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
443}
444
445TEST_F(AssetManager2Test, GetResourceLocales) {
446 AssetManager2 assetmanager;
447 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
448
449 std::set<std::string> locales = assetmanager.GetResourceLocales();
450
451 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
452 EXPECT_EQ(3u, locales.size());
453 EXPECT_GT(locales.count("sv"), 0u);
454 EXPECT_GT(locales.count("de"), 0u);
455 EXPECT_GT(locales.count("fr"), 0u);
456
457 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
458 // We expect the de and fr locales from basic_de_fr assets.
459 EXPECT_EQ(2u, locales.size());
460 EXPECT_GT(locales.count("de"), 0u);
461 EXPECT_GT(locales.count("fr"), 0u);
462}
463
464TEST_F(AssetManager2Test, GetResourceId) {
465 AssetManager2 assetmanager;
466 assetmanager.SetApkAssets({basic_assets_.get()});
467
468 EXPECT_EQ(basic::R::layout::main,
469 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
470 EXPECT_EQ(basic::R::layout::main,
471 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
472 EXPECT_EQ(basic::R::layout::main,
473 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
474}
475
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800476TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
477 AssetManager2 assetmanager;
478 assetmanager.SetApkAssets({system_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700479
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800480 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
481 ASSERT_THAT(asset, NotNull());
482
483 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
484 ASSERT_THAT(data, NotNull());
485 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
486}
487
488TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
489 AssetManager2 assetmanager;
490 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
491
492 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
493 ASSERT_THAT(asset, NotNull());
494
495 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
496 ASSERT_THAT(data, NotNull());
497 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
498}
499
500TEST_F(AssetManager2Test, OpenDir) {
501 AssetManager2 assetmanager;
502 assetmanager.SetApkAssets({system_assets_.get()});
503
504 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
505 ASSERT_THAT(asset_dir, NotNull());
506 ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
507
508 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
509 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
510
511 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
512 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
513
514 asset_dir = assetmanager.OpenDir("subdir");
515 ASSERT_THAT(asset_dir, NotNull());
516 ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
517
518 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
519 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
520}
521
522TEST_F(AssetManager2Test, OpenDirFromManyApks) {
523 AssetManager2 assetmanager;
524 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
525
526 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
527 ASSERT_THAT(asset_dir, NotNull());
528 ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
529
530 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
531 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
532
533 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
534 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
535
536 EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
537 EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
538}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700539
Adam Lesinski7ad11102016-10-28 16:39:15 -0700540} // namespace android