blob: 557d8d40dfc2e105d308e6faed0dee820074488c [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 Lesinskida431a22016-12-29 16:08:16 -050023#include "data/appaslib/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070024#include "data/basic/R.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050025#include "data/lib_one/R.h"
26#include "data/lib_two/R.h"
27#include "data/libclient/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070028#include "data/styles/R.h"
Adam Lesinski0c405242017-01-13 20:47:26 -080029#include "data/system/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070030
Adam Lesinski7ad11102016-10-28 16:39:15 -070031namespace app = com::android::app;
Adam Lesinskida431a22016-12-29 16:08:16 -050032namespace appaslib = com::android::appaslib::app;
33namespace basic = com::android::basic;
34namespace lib_one = com::android::lib_one;
35namespace lib_two = com::android::lib_two;
36namespace libclient = com::android::libclient;
Adam Lesinski7ad11102016-10-28 16:39:15 -070037
38namespace android {
39
40class AssetManager2Test : public ::testing::Test {
41 public:
42 void SetUp() override {
43 basic_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
44 ASSERT_NE(nullptr, basic_assets_);
45
46 basic_de_fr_assets_ = ApkAssets::Load(GetTestDataPath() + "/basic/basic_de_fr.apk");
47 ASSERT_NE(nullptr, basic_de_fr_assets_);
48
49 style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
50 ASSERT_NE(nullptr, style_assets_);
Adam Lesinskida431a22016-12-29 16:08:16 -050051
52 lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk");
53 ASSERT_NE(nullptr, lib_one_assets_);
54
55 lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk");
56 ASSERT_NE(nullptr, lib_two_assets_);
57
58 libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk");
59 ASSERT_NE(nullptr, libclient_assets_);
60
61 appaslib_assets_ = ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk");
62 ASSERT_NE(nullptr, appaslib_assets_);
Adam Lesinski0c405242017-01-13 20:47:26 -080063
64 system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
65 ASSERT_NE(nullptr, system_assets_);
Adam Lesinski7ad11102016-10-28 16:39:15 -070066 }
67
68 protected:
Adam Lesinski0c405242017-01-13 20:47:26 -080069 std::unique_ptr<const ApkAssets> basic_assets_;
70 std::unique_ptr<const ApkAssets> basic_de_fr_assets_;
71 std::unique_ptr<const ApkAssets> style_assets_;
72 std::unique_ptr<const ApkAssets> lib_one_assets_;
73 std::unique_ptr<const ApkAssets> lib_two_assets_;
74 std::unique_ptr<const ApkAssets> libclient_assets_;
75 std::unique_ptr<const ApkAssets> appaslib_assets_;
76 std::unique_ptr<const ApkAssets> system_assets_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070077};
78
Adam Lesinskida431a22016-12-29 16:08:16 -050079TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070080 ResTable_config desired_config;
81 memset(&desired_config, 0, sizeof(desired_config));
82 desired_config.language[0] = 'd';
83 desired_config.language[1] = 'e';
84
85 AssetManager2 assetmanager;
86 assetmanager.SetConfiguration(desired_config);
87 assetmanager.SetApkAssets({basic_assets_.get()});
88
89 Res_value value;
90 ResTable_config selected_config;
91 uint32_t flags;
92
93 ApkAssetsCookie cookie =
94 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
95 0 /*density_override*/, &value, &selected_config, &flags);
96 ASSERT_NE(kInvalidCookie, cookie);
97
98 // Came from our ApkAssets.
99 EXPECT_EQ(0, cookie);
100
101 // It is the default config.
102 EXPECT_EQ(0, selected_config.language[0]);
103 EXPECT_EQ(0, selected_config.language[1]);
104
105 // It is a string.
106 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
107}
108
Adam Lesinskida431a22016-12-29 16:08:16 -0500109TEST_F(AssetManager2Test, FindsResourceFromMultipleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700110 ResTable_config desired_config;
111 memset(&desired_config, 0, sizeof(desired_config));
112 desired_config.language[0] = 'd';
113 desired_config.language[1] = 'e';
114
115 AssetManager2 assetmanager;
116 assetmanager.SetConfiguration(desired_config);
117 assetmanager.SetApkAssets({basic_assets_.get(), basic_de_fr_assets_.get()});
118
119 Res_value value;
120 ResTable_config selected_config;
121 uint32_t flags;
122
123 ApkAssetsCookie cookie =
124 assetmanager.GetResource(basic::R::string::test1, false /*may_be_bag*/,
125 0 /*density_override*/, &value, &selected_config, &flags);
126 ASSERT_NE(kInvalidCookie, cookie);
127
128 // Came from our de_fr ApkAssets.
129 EXPECT_EQ(1, cookie);
130
Adam Lesinskida431a22016-12-29 16:08:16 -0500131 // The configuration is German.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700132 EXPECT_EQ('d', selected_config.language[0]);
133 EXPECT_EQ('e', selected_config.language[1]);
134
135 // It is a string.
136 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
137}
138
Adam Lesinskida431a22016-12-29 16:08:16 -0500139TEST_F(AssetManager2Test, FindsResourceFromSharedLibrary) {
140 AssetManager2 assetmanager;
141
142 // libclient is built with lib_one and then lib_two in order.
143 // Reverse the order to test that proper package ID re-assignment is happening.
144 assetmanager.SetApkAssets(
145 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
146
147 Res_value value;
148 ResTable_config selected_config;
149 uint32_t flags;
150
151 ApkAssetsCookie cookie =
152 assetmanager.GetResource(libclient::R::string::foo_one, false /*may_be_bag*/,
153 0 /*density_override*/, &value, &selected_config, &flags);
154 ASSERT_NE(kInvalidCookie, cookie);
155
156 // Reference comes from libclient.
157 EXPECT_EQ(2, cookie);
158 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
159
160 // Lookup the reference.
161 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
162 &value, &selected_config, &flags);
163 ASSERT_NE(kInvalidCookie, cookie);
164 EXPECT_EQ(1, cookie);
165 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
166 EXPECT_EQ(std::string("Foo from lib_one"),
167 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
168
169 cookie = assetmanager.GetResource(libclient::R::string::foo_two, false /*may_be_bag*/,
170 0 /*density_override*/, &value, &selected_config, &flags);
171 ASSERT_NE(kInvalidCookie, cookie);
172
173 // Reference comes from libclient.
174 EXPECT_EQ(2, cookie);
175 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
176
177 // Lookup the reference.
178 cookie = assetmanager.GetResource(value.data, false /* may_be_bag */, 0 /* density_override*/,
179 &value, &selected_config, &flags);
180 ASSERT_NE(kInvalidCookie, cookie);
181 EXPECT_EQ(0, cookie);
182 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
183 EXPECT_EQ(std::string("Foo from lib_two"),
184 GetStringFromPool(assetmanager.GetStringPoolForCookie(cookie), value.data));
185}
186
187TEST_F(AssetManager2Test, FindsResourceFromAppLoadedAsSharedLibrary) {
188 AssetManager2 assetmanager;
189 assetmanager.SetApkAssets({appaslib_assets_.get()});
190
191 // The appaslib package will have been assigned the package ID 0x02.
192
193 Res_value value;
194 ResTable_config selected_config;
195 uint32_t flags;
196 ApkAssetsCookie cookie = assetmanager.GetResource(
197 util::fix_package_id(appaslib::R::integer::number1, 0x02), false /*may_be_bag*/,
198 0u /*density_override*/, &value, &selected_config, &flags);
199 ASSERT_NE(kInvalidCookie, cookie);
200 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
201 EXPECT_EQ(util::fix_package_id(appaslib::R::array::integerArray1, 0x02), value.data);
202}
203
204TEST_F(AssetManager2Test, FindsBagResourceFromSingleApkAssets) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700205 AssetManager2 assetmanager;
206 assetmanager.SetApkAssets({basic_assets_.get()});
207
208 const ResolvedBag* bag = assetmanager.GetBag(basic::R::array::integerArray1);
209 ASSERT_NE(nullptr, bag);
210 ASSERT_EQ(3u, bag->entry_count);
211
212 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[0].value.dataType);
213 EXPECT_EQ(1u, bag->entries[0].value.data);
214 EXPECT_EQ(0, bag->entries[0].cookie);
215
216 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[1].value.dataType);
217 EXPECT_EQ(2u, bag->entries[1].value.data);
218 EXPECT_EQ(0, bag->entries[1].cookie);
219
220 EXPECT_EQ(static_cast<uint8_t>(Res_value::TYPE_INT_DEC), bag->entries[2].value.dataType);
221 EXPECT_EQ(3u, bag->entries[2].value.data);
222 EXPECT_EQ(0, bag->entries[2].cookie);
223}
224
Adam Lesinskida431a22016-12-29 16:08:16 -0500225TEST_F(AssetManager2Test, FindsBagResourceFromMultipleApkAssets) {}
226
227TEST_F(AssetManager2Test, FindsBagResourceFromSharedLibrary) {
228 AssetManager2 assetmanager;
229
230 // libclient is built with lib_one and then lib_two in order.
231 // Reverse the order to test that proper package ID re-assignment is happening.
232 assetmanager.SetApkAssets(
233 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
234
235 const ResolvedBag* bag = assetmanager.GetBag(libclient::R::style::Theme);
236 ASSERT_NE(nullptr, bag);
237 ASSERT_GE(bag->entry_count, 2u);
238
239 // First two attributes come from lib_one.
240 EXPECT_EQ(1, bag->entries[0].cookie);
241 EXPECT_EQ(0x03, util::get_package_id(bag->entries[0].key));
242 EXPECT_EQ(1, bag->entries[1].cookie);
243 EXPECT_EQ(0x03, util::get_package_id(bag->entries[1].key));
244}
245
Adam Lesinski7ad11102016-10-28 16:39:15 -0700246TEST_F(AssetManager2Test, MergesStylesWithParentFromSingleApkAssets) {
247 AssetManager2 assetmanager;
248 assetmanager.SetApkAssets({style_assets_.get()});
249
250 const ResolvedBag* bag_one = assetmanager.GetBag(app::R::style::StyleOne);
251 ASSERT_NE(nullptr, bag_one);
252 ASSERT_EQ(2u, bag_one->entry_count);
253
254 EXPECT_EQ(app::R::attr::attr_one, bag_one->entries[0].key);
255 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[0].value.dataType);
256 EXPECT_EQ(1u, bag_one->entries[0].value.data);
257 EXPECT_EQ(0, bag_one->entries[0].cookie);
258
259 EXPECT_EQ(app::R::attr::attr_two, bag_one->entries[1].key);
260 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_one->entries[1].value.dataType);
261 EXPECT_EQ(2u, bag_one->entries[1].value.data);
262 EXPECT_EQ(0, bag_one->entries[1].cookie);
263
264 const ResolvedBag* bag_two = assetmanager.GetBag(app::R::style::StyleTwo);
265 ASSERT_NE(nullptr, bag_two);
266 ASSERT_EQ(5u, bag_two->entry_count);
267
268 // attr_one is inherited from StyleOne.
269 EXPECT_EQ(app::R::attr::attr_one, bag_two->entries[0].key);
270 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[0].value.dataType);
271 EXPECT_EQ(1u, bag_two->entries[0].value.data);
272 EXPECT_EQ(0, bag_two->entries[0].cookie);
273
274 // attr_two should be overridden from StyleOne by StyleTwo.
275 EXPECT_EQ(app::R::attr::attr_two, bag_two->entries[1].key);
276 EXPECT_EQ(Res_value::TYPE_STRING, bag_two->entries[1].value.dataType);
277 EXPECT_EQ(0, bag_two->entries[1].cookie);
278 EXPECT_EQ(std::string("string"), GetStringFromPool(assetmanager.GetStringPoolForCookie(0),
279 bag_two->entries[1].value.data));
280
281 // The rest are new attributes.
282
283 EXPECT_EQ(app::R::attr::attr_three, bag_two->entries[2].key);
284 EXPECT_EQ(Res_value::TYPE_ATTRIBUTE, bag_two->entries[2].value.dataType);
285 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[2].value.data);
286 EXPECT_EQ(0, bag_two->entries[2].cookie);
287
288 EXPECT_EQ(app::R::attr::attr_five, bag_two->entries[3].key);
289 EXPECT_EQ(Res_value::TYPE_REFERENCE, bag_two->entries[3].value.dataType);
290 EXPECT_EQ(app::R::string::string_one, bag_two->entries[3].value.data);
291 EXPECT_EQ(0, bag_two->entries[3].cookie);
292
293 EXPECT_EQ(app::R::attr::attr_indirect, bag_two->entries[4].key);
294 EXPECT_EQ(Res_value::TYPE_INT_DEC, bag_two->entries[4].value.dataType);
295 EXPECT_EQ(3u, bag_two->entries[4].value.data);
296 EXPECT_EQ(0, bag_two->entries[4].cookie);
297}
298
Adam Lesinski0c405242017-01-13 20:47:26 -0800299TEST_F(AssetManager2Test, ResolveReferenceToResource) {
300 AssetManager2 assetmanager;
301 assetmanager.SetApkAssets({basic_assets_.get()});
302
303 Res_value value;
304 ResTable_config selected_config;
305 uint32_t flags;
306 ApkAssetsCookie cookie =
307 assetmanager.GetResource(basic::R::integer::ref1, false /*may_be_bag*/,
308 0u /*density_override*/, &value, &selected_config, &flags);
309 ASSERT_NE(kInvalidCookie, cookie);
310
311 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
312 EXPECT_EQ(basic::R::integer::ref2, value.data);
313
314 ResTable_ref last_ref;
315 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
316 ASSERT_NE(kInvalidCookie, cookie);
317 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
318 EXPECT_EQ(12000u, value.data);
319 EXPECT_EQ(basic::R::integer::ref2, last_ref.ident);
320}
321
322TEST_F(AssetManager2Test, ResolveReferenceToBag) {
323 AssetManager2 assetmanager;
324 assetmanager.SetApkAssets({basic_assets_.get()});
325
326 Res_value value;
327 ResTable_config selected_config;
328 uint32_t flags;
329 ApkAssetsCookie cookie =
330 assetmanager.GetResource(basic::R::integer::number2, true /*may_be_bag*/,
331 0u /*density_override*/, &value, &selected_config, &flags);
332 ASSERT_NE(kInvalidCookie, cookie);
333
334 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
335 EXPECT_EQ(basic::R::array::integerArray1, value.data);
336
337 ResTable_ref last_ref;
338 cookie = assetmanager.ResolveReference(cookie, &value, &selected_config, &flags, &last_ref);
339 ASSERT_NE(kInvalidCookie, cookie);
340 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
341 EXPECT_EQ(basic::R::array::integerArray1, value.data);
342 EXPECT_EQ(basic::R::array::integerArray1, last_ref.ident);
343}
344
345static bool IsConfigurationPresent(const std::set<ResTable_config>& configurations,
346 const ResTable_config& configuration) {
347 return configurations.count(configuration) > 0;
348}
349
350TEST_F(AssetManager2Test, GetResourceConfigurations) {
351 AssetManager2 assetmanager;
352 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
353
354 std::set<ResTable_config> configurations = assetmanager.GetResourceConfigurations();
355
356 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
357 // And one extra for the default configuration.
358 EXPECT_EQ(4u, configurations.size());
359
360 ResTable_config expected_config;
361 memset(&expected_config, 0, sizeof(expected_config));
362 expected_config.language[0] = 's';
363 expected_config.language[1] = 'v';
364 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
365
366 expected_config.language[0] = 'd';
367 expected_config.language[1] = 'e';
368 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
369
370 expected_config.language[0] = 'f';
371 expected_config.language[1] = 'r';
372 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
373
374 // Take out the system assets.
375 configurations = assetmanager.GetResourceConfigurations(true /* exclude_system */);
376
377 // We expect de and fr from basic_de_fr assets.
378 EXPECT_EQ(2u, configurations.size());
379
380 expected_config.language[0] = 's';
381 expected_config.language[1] = 'v';
382 EXPECT_FALSE(IsConfigurationPresent(configurations, expected_config));
383
384 expected_config.language[0] = 'd';
385 expected_config.language[1] = 'e';
386 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
387
388 expected_config.language[0] = 'f';
389 expected_config.language[1] = 'r';
390 EXPECT_TRUE(IsConfigurationPresent(configurations, expected_config));
391}
392
393TEST_F(AssetManager2Test, GetResourceLocales) {
394 AssetManager2 assetmanager;
395 assetmanager.SetApkAssets({system_assets_.get(), basic_de_fr_assets_.get()});
396
397 std::set<std::string> locales = assetmanager.GetResourceLocales();
398
399 // We expect the locale sv from the system assets, and de and fr from basic_de_fr assets.
400 EXPECT_EQ(3u, locales.size());
401 EXPECT_GT(locales.count("sv"), 0u);
402 EXPECT_GT(locales.count("de"), 0u);
403 EXPECT_GT(locales.count("fr"), 0u);
404
405 locales = assetmanager.GetResourceLocales(true /*exclude_system*/);
406 // We expect the de and fr locales from basic_de_fr assets.
407 EXPECT_EQ(2u, locales.size());
408 EXPECT_GT(locales.count("de"), 0u);
409 EXPECT_GT(locales.count("fr"), 0u);
410}
411
412TEST_F(AssetManager2Test, GetResourceId) {
413 AssetManager2 assetmanager;
414 assetmanager.SetApkAssets({basic_assets_.get()});
415
416 EXPECT_EQ(basic::R::layout::main,
417 assetmanager.GetResourceId("com.android.basic:layout/main", "", ""));
418 EXPECT_EQ(basic::R::layout::main,
419 assetmanager.GetResourceId("layout/main", "", "com.android.basic"));
420 EXPECT_EQ(basic::R::layout::main,
421 assetmanager.GetResourceId("main", "layout", "com.android.basic"));
422}
423
Adam Lesinski7ad11102016-10-28 16:39:15 -0700424TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {}
425
426TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {}
427
428} // namespace android