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
19 #include "android-base/logging.h"
20
21 #include "TestHelpers.h"
22 #include "androidfw/ResourceUtils.h"
23 #include "data/lib_one/R.h"
24 #include "data/lib_two/R.h"
25 #include "data/libclient/R.h"
26 #include "data/styles/R.h"
27 #include "data/system/R.h"
28
29 namespace app = com::android::app;
30 namespace lib_one = com::android::lib_one;
31 namespace lib_two = com::android::lib_two;
32 namespace libclient = com::android::libclient;
33
34 namespace android {
35
36 class ThemeTest : public ::testing::Test {
37 public:
SetUp()38 void SetUp() override {
39 system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", PROPERTY_SYSTEM);
40 ASSERT_NE(nullptr, system_assets_);
41
42 style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
43 ASSERT_NE(nullptr, style_assets_);
44
45 libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk");
46 ASSERT_NE(nullptr, libclient_assets_);
47
48 lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk");
49 ASSERT_NE(nullptr, lib_one_assets_);
50
51 lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk");
52 ASSERT_NE(nullptr, lib_two_assets_);
53 }
54
55 protected:
56 std::unique_ptr<const ApkAssets> system_assets_;
57 std::unique_ptr<const ApkAssets> style_assets_;
58 std::unique_ptr<const ApkAssets> libclient_assets_;
59 std::unique_ptr<const ApkAssets> lib_one_assets_;
60 std::unique_ptr<const ApkAssets> lib_two_assets_;
61 };
62
TEST_F(ThemeTest,EmptyTheme)63 TEST_F(ThemeTest, EmptyTheme) {
64 AssetManager2 assetmanager;
65 assetmanager.SetApkAssets({style_assets_.get()});
66
67 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
68 EXPECT_EQ(0u, theme->GetChangingConfigurations());
69 EXPECT_EQ(&assetmanager, theme->GetAssetManager());
70 EXPECT_FALSE(theme->GetAttribute(app::R::attr::attr_one).has_value());
71 }
72
TEST_F(ThemeTest,SingleThemeNoParent)73 TEST_F(ThemeTest, SingleThemeNoParent) {
74 AssetManager2 assetmanager;
75 assetmanager.SetApkAssets({style_assets_.get()});
76
77 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
78 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleOne).has_value());
79
80 auto value = theme->GetAttribute(app::R::attr::attr_one);
81 ASSERT_TRUE(value);
82 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
83 EXPECT_EQ(1u, value->data);
84 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
85
86 value = theme->GetAttribute(app::R::attr::attr_two);
87 ASSERT_TRUE(value);
88 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
89 EXPECT_EQ(2u, value->data);
90 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
91 }
92
TEST_F(ThemeTest,SingleThemeWithParent)93 TEST_F(ThemeTest, SingleThemeWithParent) {
94 AssetManager2 assetmanager;
95 assetmanager.SetApkAssets({style_assets_.get()});
96
97 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
98 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
99
100 auto value = theme->GetAttribute(app::R::attr::attr_one);
101 ASSERT_TRUE(value);
102 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
103 EXPECT_EQ(1u, value->data);
104 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
105
106 value = theme->GetAttribute(app::R::attr::attr_two);
107 ASSERT_TRUE(value);
108 EXPECT_EQ(Res_value::TYPE_STRING, value->type);
109 EXPECT_EQ(0, value->cookie);
110 EXPECT_EQ(std::string("string"),
111 GetStringFromPool(assetmanager.GetStringPoolForCookie(0), value->data));
112 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
113
114 // This attribute should point to an attr_indirect, so the result should be 3.
115 value = theme->GetAttribute(app::R::attr::attr_three);
116 ASSERT_TRUE(value);
117 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
118 EXPECT_EQ(3u, value->data);
119 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
120 }
121
TEST_F(ThemeTest,TryToUseBadResourceId)122 TEST_F(ThemeTest, TryToUseBadResourceId) {
123 AssetManager2 assetmanager;
124 assetmanager.SetApkAssets({style_assets_.get()});
125
126 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
127 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
128 ASSERT_FALSE(theme->GetAttribute(0x7f000001));
129 }
130
TEST_F(ThemeTest,MultipleThemesOverlaidNotForce)131 TEST_F(ThemeTest, MultipleThemesOverlaidNotForce) {
132 AssetManager2 assetmanager;
133 assetmanager.SetApkAssets({style_assets_.get()});
134
135 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
136 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
137 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree).has_value());
138
139 // attr_one is still here from the base.
140 auto value = theme->GetAttribute(app::R::attr::attr_one);
141 ASSERT_TRUE(value);
142 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
143 EXPECT_EQ(1u, value->data);
144 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
145
146 // check for the new attr_six
147 value = theme->GetAttribute(app::R::attr::attr_six);
148 ASSERT_TRUE(value);
149 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
150 EXPECT_EQ(6u, value->data);
151 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
152
153 // check for the old attr_five (force=true was not used).
154 value = theme->GetAttribute(app::R::attr::attr_five);
155 ASSERT_TRUE(value);
156 EXPECT_EQ(Res_value::TYPE_REFERENCE, value->type);
157 EXPECT_EQ(app::R::string::string_one, value->data);
158 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
159 }
160
TEST_F(ThemeTest,MultipleThemesOverlaidForced)161 TEST_F(ThemeTest, MultipleThemesOverlaidForced) {
162 AssetManager2 assetmanager;
163 assetmanager.SetApkAssets({style_assets_.get()});
164
165 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
166 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
167 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree, true /* force */).has_value());
168
169 // attr_one is still here from the base.
170 auto value = theme->GetAttribute(app::R::attr::attr_one);
171 ASSERT_TRUE(value);
172 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
173 EXPECT_EQ(1u, value->data);
174 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
175
176 // check for the new attr_six
177 value = theme->GetAttribute(app::R::attr::attr_six);
178 ASSERT_TRUE(value);
179 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
180 EXPECT_EQ(6u, value->data);
181 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
182
183 // check for the new attr_five (force=true was used).
184 value = theme->GetAttribute(app::R::attr::attr_five);
185 ASSERT_TRUE(value);
186 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
187 EXPECT_EQ(5u, value->data);
188 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
189 }
190
TEST_F(ThemeTest,ResolveDynamicAttributesAndReferencesToSharedLibrary)191 TEST_F(ThemeTest, ResolveDynamicAttributesAndReferencesToSharedLibrary) {
192 AssetManager2 assetmanager;
193 assetmanager.SetApkAssets(
194 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
195
196 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
197 ASSERT_TRUE(theme->ApplyStyle(libclient::R::style::Theme, false /*force*/).has_value());
198
199 // The attribute should be resolved to the final value.
200 auto value = theme->GetAttribute(libclient::R::attr::foo);
201 ASSERT_TRUE(value);
202 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
203 EXPECT_EQ(700u, value->data);
204 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
205
206 // The reference should be resolved to a TYPE_REFERENCE.
207 value = theme->GetAttribute(libclient::R::attr::bar);
208 ASSERT_TRUE(value);
209 EXPECT_EQ(Res_value::TYPE_REFERENCE, value->type);
210
211 // lib_one is assigned package ID 0x03.
212 EXPECT_EQ(3u, get_package_id(value->data));
213 EXPECT_EQ(get_type_id(lib_one::R::string::foo), get_type_id(value->data));
214 EXPECT_EQ(get_entry_id(lib_one::R::string::foo), get_entry_id(value->data));
215 }
216
TEST_F(ThemeTest,CopyThemeSameAssetManager)217 TEST_F(ThemeTest, CopyThemeSameAssetManager) {
218 AssetManager2 assetmanager;
219 assetmanager.SetApkAssets({style_assets_.get()});
220
221 std::unique_ptr<Theme> theme_one = assetmanager.NewTheme();
222 ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne).has_value());
223
224 // attr_one is still here from the base.
225 auto value = theme_one->GetAttribute(app::R::attr::attr_one);
226 ASSERT_TRUE(value);
227 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
228 EXPECT_EQ(1u, value->data);
229 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
230
231 // attr_six is not here.
232 ASSERT_FALSE(theme_one->GetAttribute(app::R::attr::attr_six).has_value());
233
234 std::unique_ptr<Theme> theme_two = assetmanager.NewTheme();
235 ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleThree).has_value());
236
237 // Copy the theme to theme_one.
238 theme_one->SetTo(*theme_two);
239
240 // Clear theme_two to make sure we test that there WAS a copy.
241 theme_two->Clear();
242
243 // attr_one is now not here.
244 ASSERT_FALSE(theme_one->GetAttribute(app::R::attr::attr_one).has_value());
245
246 // attr_six is now here because it was copied.
247 value = theme_one->GetAttribute(app::R::attr::attr_six);
248 ASSERT_TRUE(value);
249 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
250 EXPECT_EQ(6u, value->data);
251 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
252 }
253
TEST_F(ThemeTest,ThemeRebase)254 TEST_F(ThemeTest, ThemeRebase) {
255 AssetManager2 am;
256 am.SetApkAssets({style_assets_.get()});
257
258 AssetManager2 am_night;
259 am_night.SetApkAssets({style_assets_.get()});
260
261 ResTable_config night{};
262 night.uiMode = ResTable_config::UI_MODE_NIGHT_YES;
263 night.version = 8u;
264 am_night.SetConfiguration(night);
265
266 auto theme = am.NewTheme();
267 {
268 const uint32_t styles[] = {app::R::style::StyleOne, app::R::style::StyleDayNight};
269 const uint8_t force[] = {true, true};
270 theme->Rebase(&am, styles, force, arraysize(styles));
271 }
272
273 // attr_one in StyleDayNight force overrides StyleOne. attr_one is defined in the StyleOne.
274 auto value = theme->GetAttribute(app::R::attr::attr_one);
275 ASSERT_TRUE(value);
276 EXPECT_EQ(10u, value->data);
277 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC | ResTable_config::CONFIG_UI_MODE |
278 ResTable_config::CONFIG_VERSION), value->flags);
279
280 // attr_two is defined in the StyleOne.
281 value = theme->GetAttribute(app::R::attr::attr_two);
282 ASSERT_TRUE(value);
283 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
284 EXPECT_EQ(2u, value->data);
285 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
286
287 {
288 const uint32_t styles[] = {app::R::style::StyleOne, app::R::style::StyleDayNight};
289 const uint8_t force[] = {false, false};
290 theme->Rebase(&am, styles, force, arraysize(styles));
291 }
292
293 // attr_one in StyleDayNight does not override StyleOne because `force` is false.
294 value = theme->GetAttribute(app::R::attr::attr_one);
295 ASSERT_TRUE(value);
296 EXPECT_EQ(1u, value->data);
297 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
298
299 // attr_two is defined in the StyleOne.
300 value = theme->GetAttribute(app::R::attr::attr_two);
301 ASSERT_TRUE(value);
302 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
303 EXPECT_EQ(2u, value->data);
304 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
305
306 {
307 const uint32_t styles[] = {app::R::style::StyleOne, app::R::style::StyleDayNight};
308 const uint8_t force[] = {false, true};
309 theme->Rebase(&am_night, styles, force, arraysize(styles));
310 }
311
312 // attr_one is defined in the StyleDayNight.
313 value = theme->GetAttribute(app::R::attr::attr_one);
314 ASSERT_TRUE(value);
315 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
316 EXPECT_EQ(100u, value->data);
317 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC | ResTable_config::CONFIG_UI_MODE |
318 ResTable_config::CONFIG_VERSION), value->flags);
319
320 // attr_two is now not here.
321 value = theme->GetAttribute(app::R::attr::attr_two);
322 ASSERT_TRUE(value);
323 EXPECT_EQ(Res_value::TYPE_INT_DEC, value->type);
324 EXPECT_EQ(2u, value->data);
325 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
326 }
327
TEST_F(ThemeTest,OnlyCopySameAssetsThemeWhenAssetManagersDiffer)328 TEST_F(ThemeTest, OnlyCopySameAssetsThemeWhenAssetManagersDiffer) {
329 AssetManager2 assetmanager_dst;
330 assetmanager_dst.SetApkAssets({system_assets_.get(), lib_one_assets_.get(), style_assets_.get(),
331 libclient_assets_.get()});
332
333 AssetManager2 assetmanager_src;
334 assetmanager_src.SetApkAssets({system_assets_.get(), lib_two_assets_.get(), lib_one_assets_.get(),
335 style_assets_.get()});
336
337 auto theme_dst = assetmanager_dst.NewTheme();
338 ASSERT_TRUE(theme_dst->ApplyStyle(app::R::style::StyleOne).has_value());
339
340 auto theme_src = assetmanager_src.NewTheme();
341 ASSERT_TRUE(theme_src->ApplyStyle(R::style::Theme_One).has_value());
342 ASSERT_TRUE(theme_src->ApplyStyle(app::R::style::StyleTwo).has_value());
343 ASSERT_TRUE(theme_src->ApplyStyle(fix_package_id(lib_one::R::style::Theme, 0x03),
344 false /*force*/).has_value());
345 ASSERT_TRUE(theme_src->ApplyStyle(fix_package_id(lib_two::R::style::Theme, 0x02),
346 false /*force*/).has_value());
347
348 theme_dst->SetTo(*theme_src);
349
350 // System resources (present in destination asset manager).
351 auto value = theme_dst->GetAttribute(R::attr::foreground);
352 ASSERT_TRUE(value.has_value());
353 EXPECT_EQ(0, value->cookie);
354
355 // The cookie of the style asset is 3 in the source and 2 in the destination.
356 // Check that the cookie has been rewritten to the destination values.
357 value = theme_dst->GetAttribute(app::R::attr::attr_one);
358 ASSERT_TRUE(value.has_value());
359 EXPECT_EQ(2, value->cookie);
360
361 // The cookie of the lib_one asset is 2 in the source and 1 in the destination.
362 // The package id of the lib_one package is 0x03 in the source and 0x02 in the destination
363 // Check that the cookie and packages have been rewritten to the destination values.
364 value = theme_dst->GetAttribute(fix_package_id(lib_one::R::attr::attr1, 0x02));
365 ASSERT_TRUE(value.has_value());
366 EXPECT_EQ(1, value->cookie);
367
368 value = theme_dst->GetAttribute(fix_package_id(lib_one::R::attr::attr2, 0x02));
369 ASSERT_TRUE(value.has_value());
370 EXPECT_EQ(1, value->cookie);
371
372 // attr2 references an attribute in lib_one. Check that the resolution of the attribute value is
373 // correct after the value of attr2 had its package id rewritten to the destination package id.
374 EXPECT_EQ(700, value->data);
375 }
376
TEST_F(ThemeTest,CopyNonReferencesWhenPackagesDiffer)377 TEST_F(ThemeTest, CopyNonReferencesWhenPackagesDiffer) {
378 AssetManager2 assetmanager_dst;
379 assetmanager_dst.SetApkAssets({system_assets_.get()});
380
381 AssetManager2 assetmanager_src;
382 assetmanager_src.SetApkAssets({system_assets_.get(), style_assets_.get()});
383
384 auto theme_dst = assetmanager_dst.NewTheme();
385 auto theme_src = assetmanager_src.NewTheme();
386 ASSERT_TRUE(theme_src->ApplyStyle(app::R::style::StyleSeven).has_value());
387 theme_dst->SetTo(*theme_src);
388
389 // Allow inline resource values to be copied even if the source apk asset is not present in the
390 // destination.
391 auto value = theme_dst->GetAttribute(0x0101021b /* android:versionCode */);
392 ASSERT_TRUE(value.has_value());
393 EXPECT_EQ(0, value->cookie);
394
395 // Do not copy strings since the data is an index into the values string pool of the source apk
396 // asset.
397 EXPECT_FALSE(theme_dst->GetAttribute(0x01010001 /* android:label */).has_value());
398
399 // Do not copy values that reference another resource if the resource is not present in the
400 // destination.
401 EXPECT_FALSE(theme_dst->GetAttribute(0x01010002 /* android:icon */).has_value());
402 EXPECT_FALSE(theme_dst->GetAttribute(0x010100d1 /* android:tag */).has_value());
403
404 // Allow @empty to and @null to be copied.
405 value = theme_dst->GetAttribute(0x010100d0 /* android:id */);
406 ASSERT_TRUE(value.has_value());
407 EXPECT_EQ(0, value->cookie);
408
409 value = theme_dst->GetAttribute(0x01010000 /* android:theme */);
410 ASSERT_TRUE(value.has_value());
411 EXPECT_EQ(0, value->cookie);
412 }
413
414 } // namespace android
415