• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
23 #include "androidfw/ResourceUtils.h"
24 #include "data/appaslib/R.h"
25 #include "data/basic/R.h"
26 #include "data/lib_one/R.h"
27 #include "data/lib_two/R.h"
28 #include "data/libclient/R.h"
29 #include "data/styles/R.h"
30 #include "data/system/R.h"
31 
32 namespace app = com::android::app;
33 namespace appaslib = com::android::appaslib::app;
34 namespace basic = com::android::basic;
35 namespace lib_one = com::android::lib_one;
36 namespace lib_two = com::android::lib_two;
37 namespace libclient = com::android::libclient;
38 
39 using ::testing::Eq;
40 using ::testing::NotNull;
41 using ::testing::StrEq;
42 
43 namespace android {
44 
45 class AssetManager2Test : public ::testing::Test {
46  public:
SetUp()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_);
56 
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 
66     appaslib_assets_ = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk");
67     ASSERT_NE(nullptr, appaslib_assets_);
68 
69     system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
70     ASSERT_NE(nullptr, system_assets_);
71 
72     app_assets_ = ApkAssets::Load(GetTestDataPath() + "/app/app.apk");
73     ASSERT_THAT(app_assets_, NotNull());
74   }
75 
76  protected:
77   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_;
85   std::unique_ptr<const ApkAssets> app_assets_;
86 };
87 
TEST_F(AssetManager2Test,FindsResourceFromSingleApkAssets)88 TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
89   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 
TEST_F(AssetManager2Test,FindsResourceFromMultipleApkAssets)118 TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
119   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 
140   // The configuration is German.
141   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 
TEST_F(AssetManager2Test,FindsResourceFromSharedLibrary)148 TEST_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 
TEST_F(AssetManager2Test,FindsResourceFromAppLoadedAsSharedLibrary)196 TEST_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(
206       fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
207       0u /*density_override*/, &value, &selected_config, &flags);
208   ASSERT_NE(kInvalidCookie, cookie);
209   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
210   EXPECT_EQ(fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
211 }
212 
TEST_F(AssetManager2Test,FindsBagResourceFromSingleApkAssets)213 TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
214   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 
TEST_F(AssetManager2Test,FindsBagResourceFromMultipleApkAssets)234 TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
235 
TEST_F(AssetManager2Test,FindsBagResourceFromSharedLibrary)236 TEST_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 
244   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 
TEST_F(AssetManager2Test,FindsStyleResourceWithParentFromSharedLibrary)255 TEST_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 
263   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);
269   EXPECT_EQ(0x03, get_package_id(bag->entries[0].key));
270   EXPECT_EQ(1, bag->entries[1].cookie);
271   EXPECT_EQ(0x03, get_package_id(bag->entries[1].key));
272 }
273 
TEST_F(AssetManager2Test,MergesStylesWithParentFromSingleApkAssets)274 TEST_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);
294   ASSERT_EQ(6u, bag_two->entry_count);
295 
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);
325 
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);
330 }
331 
TEST_F(AssetManager2Test,MergeStylesCircularDependency)332 TEST_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 
TEST_F(AssetManager2Test,ResolveReferenceToResource)343 TEST_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 
358   uint32_t last_ref = 0u;
359   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);
363   EXPECT_EQ(basic::R::integer::ref2, last_ref);
364 }
365 
TEST_F(AssetManager2Test,ResolveReferenceToBag)366 TEST_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 
381   uint32_t last_ref = 0u;
382   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);
386   EXPECT_EQ(basic::R::array::integerArray1, last_ref);
387 }
388 
TEST_F(AssetManager2Test,ResolveDeepIdReference)389 TEST_F(AssetManager2Test, ResolveDeepIdReference) {
390   AssetManager2 assetmanager;
391   assetmanager.SetApkAssets({basic_assets_.get()});
392 
393   // Set up the resource ids
394   const uint32_t high_ref = assetmanager
395       .GetResourceId("@id/high_ref", "values", "com.android.basic");
396   ASSERT_NE(high_ref, 0u);
397   const uint32_t middle_ref = assetmanager
398       .GetResourceId("@id/middle_ref", "values", "com.android.basic");
399   ASSERT_NE(middle_ref, 0u);
400   const uint32_t low_ref = assetmanager
401       .GetResourceId("@id/low_ref", "values", "com.android.basic");
402   ASSERT_NE(low_ref, 0u);
403 
404   // Retrieve the most shallow resource
405   Res_value value;
406   ResTable_config config;
407   uint32_t flags;
408   ApkAssetsCookie cookie = assetmanager.GetResource(high_ref, false /*may_be_bag*/,
409                                                     0 /*density_override*/,
410                                                     &value, &config, &flags);
411   ASSERT_NE(kInvalidCookie, cookie);
412   EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
413   EXPECT_EQ(middle_ref, value.data);
414 
415   // Check that resolving the reference resolves to the deepest id
416   uint32_t last_ref = high_ref;
417   assetmanager.ResolveReference(cookie, &value, &config, &flags, &last_ref);
418   EXPECT_EQ(last_ref, low_ref);
419 }
420 
TEST_F(AssetManager2Test,KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved)421 TEST_F(AssetManager2Test, KeepLastReferenceIdUnmodifiedIfNoReferenceIsResolved) {
422   AssetManager2 assetmanager;
423   assetmanager.SetApkAssets({basic_assets_.get()});
424 
425   ResTable_config selected_config;
426   memset(&selected_config, 0, sizeof(selected_config));
427 
428   uint32_t flags = 0u;
429 
430   // Create some kind of Res_value that is NOT a reference.
431   Res_value value;
432   value.dataType = Res_value::TYPE_STRING;
433   value.data = 0;
434 
435   uint32_t last_ref = basic::R::string::test1;
436   EXPECT_EQ(1, assetmanager.ResolveReference(1, &value, &selected_config, &flags, &last_ref));
437   EXPECT_EQ(basic::R::string::test1, last_ref);
438 }
439 
IsConfigurationPresent(const std::set<ResTable_config> & configurations,const ResTable_config & configuration)440 static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
441                                    const ResTable_config& configuration) {
442   return configurations.count(configuration) > 0;
443 }
444 
TEST_F(AssetManager2Test,GetResourceConfigurations)445 TEST_F(AssetManager2Test, GetResourceConfigurations) {
446   AssetManager2 assetmanager;
447   assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
448 
449   std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
450 
451   // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
452   // And one extra for the default configuration.
453   EXPECT_EQ(4u, configurations.size());
454 
455   ResTable_config expected_config;
456   memset(&expected_config, 0, sizeof(expected_config));
457   expected_config.language[0] = 's';
458   expected_config.language[1] = 'v';
459   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
460 
461   expected_config.language[0] = 'd';
462   expected_config.language[1] = 'e';
463   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
464 
465   expected_config.language[0] = 'f';
466   expected_config.language[1] = 'r';
467   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
468 
469   // Take out the system assets.
470   configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
471 
472   // We expect de and fr from basic_de_fr assets.
473   EXPECT_EQ(2u, configurations.size());
474 
475   expected_config.language[0] = 's';
476   expected_config.language[1] = 'v';
477   EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
478 
479   expected_config.language[0] = 'd';
480   expected_config.language[1] = 'e';
481   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
482 
483   expected_config.language[0] = 'f';
484   expected_config.language[1] = 'r';
485   EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
486 }
487 
TEST_F(AssetManager2Test,GetResourceLocales)488 TEST_F(AssetManager2Test, GetResourceLocales) {
489   AssetManager2 assetmanager;
490   assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
491 
492   std::set<std::string> locales = assetmanager.GetResourceLocales();
493 
494   // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
495   EXPECT_EQ(3u, locales.size());
496   EXPECT_GT(locales.count("sv"), 0u);
497   EXPECT_GT(locales.count("de"), 0u);
498   EXPECT_GT(locales.count("fr"), 0u);
499 
500   locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
501   // We expect the de and fr locales from basic_de_fr assets.
502   EXPECT_EQ(2u, locales.size());
503   EXPECT_GT(locales.count("de"), 0u);
504   EXPECT_GT(locales.count("fr"), 0u);
505 }
506 
TEST_F(AssetManager2Test,GetResourceId)507 TEST_F(AssetManager2Test, GetResourceId) {
508   AssetManager2 assetmanager;
509   assetmanager.SetApkAssets({basic_assets_.get()});
510 
511   EXPECT_EQ(basic::R::layout::main,
512             assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
513   EXPECT_EQ(basic::R::layout::main,
514             assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
515   EXPECT_EQ(basic::R::layout::main,
516             assetmanager.GetResourceId("main", "layout", "com.android.basic"));
517 }
518 
TEST_F(AssetManager2Test,OpensFileFromSingleApkAssets)519 TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
520   AssetManager2 assetmanager;
521   assetmanager.SetApkAssets({system_assets_.get()});
522 
523   std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
524   ASSERT_THAT(asset, NotNull());
525 
526   const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
527   ASSERT_THAT(data, NotNull());
528   EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
529 }
530 
TEST_F(AssetManager2Test,OpensFileFromMultipleApkAssets)531 TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
532   AssetManager2 assetmanager;
533   assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
534 
535   std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
536   ASSERT_THAT(asset, NotNull());
537 
538   const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
539   ASSERT_THAT(data, NotNull());
540   EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
541 }
542 
TEST_F(AssetManager2Test,OpenDir)543 TEST_F(AssetManager2Test, OpenDir) {
544   AssetManager2 assetmanager;
545   assetmanager.SetApkAssets({system_assets_.get()});
546 
547   std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
548   ASSERT_THAT(asset_dir, NotNull());
549   ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
550 
551   EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
552   EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
553 
554   EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
555   EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
556 
557   asset_dir = assetmanager.OpenDir("subdir");
558   ASSERT_THAT(asset_dir, NotNull());
559   ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
560 
561   EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
562   EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
563 }
564 
TEST_F(AssetManager2Test,OpenDirFromManyApks)565 TEST_F(AssetManager2Test, OpenDirFromManyApks) {
566   AssetManager2 assetmanager;
567   assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
568 
569   std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
570   ASSERT_THAT(asset_dir, NotNull());
571   ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
572 
573   EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
574   EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
575 
576   EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
577   EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
578 
579   EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
580   EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
581 }
582 
583 }  // namespace android
584