blob: 3118009fef902bb0eaaa8156a34de66497adb00c [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
y57cd1952018-04-12 14:26:23 -0700332TEST_F(AssetManager2Test, MergeStylesCircularDependency) {
333 AssetManager2 assetmanager;
334 assetmanager.SetApkAssets({style_assets_.get()});
335
336 // GetBag should stop traversing the parents of styles when a circular
337 // dependency is detected
338 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleFour);
339 ASSERT_NE(nullptr, bag_one);
340 ASSERT_EQ(3u, bag_one->entry_count);
341}
342
Adam Lesinski0c405242017-01-13 20:47:26 -0800343TEST_F(AssetManager2Test, ResolveReferenceToResource) {
344 AssetManager2 assetmanager;
345 assetmanager.SetApkAssets({basic_assets_.get()});
346
347 Res_value value;
348 ResTable_config selected_config;
349 uint32_t flags;
350 ApkAssetsCookie cookie =
351 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
352 0u /*density_override*/, &value, &selected_config, &flags);
353 ASSERT_NE(kInvalidCookie, cookie);
354
355 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
356 EXPECT_EQ(basic::R::integer::ref2, value.data);
357
Adam Lesinski1c855a02017-11-29 09:59:37 -0800358 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800359 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
360 ASSERT_NE(kInvalidCookie, cookie);
361 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
362 EXPECT_EQ(12000u, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800363 EXPECT_EQ(basic::R::integer::ref2, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800364}
365
366TEST_F(AssetManager2Test, ResolveReferenceToBag) {
367 AssetManager2 assetmanager;
368 assetmanager.SetApkAssets({basic_assets_.get()});
369
370 Res_value value;
371 ResTable_config selected_config;
372 uint32_t flags;
373 ApkAssetsCookie cookie =
374 assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
375 0u /*density_override*/, &value, &selected_config, &flags);
376 ASSERT_NE(kInvalidCookie, cookie);
377
378 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
379 EXPECT_EQ(basic::R::array::integerArray1, value.data);
380
Adam Lesinski1c855a02017-11-29 09:59:37 -0800381 uint32_t last_ref = 0u;
Adam Lesinski0c405242017-01-13 20:47:26 -0800382 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
383 ASSERT_NE(kInvalidCookie, cookie);
384 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
385 EXPECT_EQ(basic::R::array::integerArray1, value.data);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800386 EXPECT_EQ(basic::R::array::integerArray1, last_ref);
Adam Lesinski0c405242017-01-13 20:47:26 -0800387}
388
Adam Lesinski1c855a02017-11-29 09:59:37 -0800389TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
390 AssetManager2 assetmanager;
391 assetmanager.SetApkAssets({basic_assets_.get()});
392
393 ResTable_config selected_config;
394 memset(&selected_config, 0, sizeof(selected_config));
395
396 uint32_t flags = 0u;
397
398 // Create some kind of Res_value that is NOT a reference.
399 Res_value value;
400 value.dataType = Res_value::TYPE_STRING;
401 value.data = 0;
402
403 uint32_t last_ref = basic::R::string::test1;
404 EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
405 EXPECT_EQ(basic::R::string::test1, last_ref);
406}
407
Adam Lesinski0c405242017-01-13 20:47:26 -0800408static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
409 const ResTable_config& configuration) {
410 return configurations.count(configuration) > 0;
411}
412
413TEST_F(AssetManager2Test, GetResourceConfigurations) {
414 AssetManager2 assetmanager;
415 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
416
417 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
418
419 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
420 // And one extra for the default configuration.
421 EXPECT_EQ(4u, configurations.size());
422
423 ResTable_config expected_config;
424 memset(&expected_config, 0, sizeof(expected_config));
425 expected_config.language[0] = 's';
426 expected_config.language[1] = 'v';
427 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
428
429 expected_config.language[0] = 'd';
430 expected_config.language[1] = 'e';
431 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
432
433 expected_config.language[0] = 'f';
434 expected_config.language[1] = 'r';
435 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
436
437 // Take out the system assets.
438 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
439
440 // We expect de and fr from basic_de_fr assets.
441 EXPECT_EQ(2u, configurations.size());
442
443 expected_config.language[0] = 's';
444 expected_config.language[1] = 'v';
445 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
446
447 expected_config.language[0] = 'd';
448 expected_config.language[1] = 'e';
449 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
450
451 expected_config.language[0] = 'f';
452 expected_config.language[1] = 'r';
453 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
454}
455
456TEST_F(AssetManager2Test, GetResourceLocales) {
457 AssetManager2 assetmanager;
458 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
459
460 std::set<std::string> locales = assetmanager.GetResourceLocales();
461
462 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
463 EXPECT_EQ(3u, locales.size());
464 EXPECT_GT(locales.count("sv"), 0u);
465 EXPECT_GT(locales.count("de"), 0u);
466 EXPECT_GT(locales.count("fr"), 0u);
467
468 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
469 // We expect the de and fr locales from basic_de_fr assets.
470 EXPECT_EQ(2u, locales.size());
471 EXPECT_GT(locales.count("de"), 0u);
472 EXPECT_GT(locales.count("fr"), 0u);
473}
474
475TEST_F(AssetManager2Test, GetResourceId) {
476 AssetManager2 assetmanager;
477 assetmanager.SetApkAssets({basic_assets_.get()});
478
479 EXPECT_EQ(basic::R::layout::main,
480 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
481 EXPECT_EQ(basic::R::layout::main,
482 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
483 EXPECT_EQ(basic::R::layout::main,
484 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
485}
486
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800487TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
488 AssetManager2 assetmanager;
489 assetmanager.SetApkAssets({system_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700490
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800491 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
492 ASSERT_THAT(asset, NotNull());
493
494 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
495 ASSERT_THAT(data, NotNull());
496 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
497}
498
499TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
500 AssetManager2 assetmanager;
501 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
502
503 std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
504 ASSERT_THAT(asset, NotNull());
505
506 const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
507 ASSERT_THAT(data, NotNull());
508 EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
509}
510
511TEST_F(AssetManager2Test, OpenDir) {
512 AssetManager2 assetmanager;
513 assetmanager.SetApkAssets({system_assets_.get()});
514
515 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
516 ASSERT_THAT(asset_dir, NotNull());
517 ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
518
519 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
520 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
521
522 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
523 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
524
525 asset_dir = assetmanager.OpenDir("subdir");
526 ASSERT_THAT(asset_dir, NotNull());
527 ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
528
529 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
530 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
531}
532
533TEST_F(AssetManager2Test, OpenDirFromManyApks) {
534 AssetManager2 assetmanager;
535 assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
536
537 std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
538 ASSERT_THAT(asset_dir, NotNull());
539 ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
540
541 EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
542 EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
543
544 EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
545 EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
546
547 EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
548 EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
549}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700550
Adam Lesinski7ad11102016-10-28 16:39:15 -0700551} // namespace android