blob: 2c39ceead123ad17ff42252d5dc78a4f6a39862f [file] [log] [blame]
Adam Lesinski9d9cc622014-08-29 14:10:04 -07001/*
Adam Lesinski7ad11102016-10-28 16:39:15 -07002 * Copyright (C) 2016 The Android Open Source Project
Adam Lesinski9d9cc622014-08-29 14:10:04 -07003 *
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
Adam Lesinski7ad11102016-10-28 16:39:15 -070017#include "androidfw/AssetManager2.h"
Adam Lesinski9d9cc622014-08-29 14:10:04 -070018
Adam Lesinski7ad11102016-10-28 16:39:15 -070019#include "android-base/logging.h"
Adam Lesinski4c67a472016-11-10 16:43:59 -080020
Adam Lesinski9d9cc622014-08-29 14:10:04 -070021#include "TestHelpers.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080022#include "androidfw/ResourceUtils.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050023#include "data/lib_one/R.h"
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -070024#include "data/lib_two/R.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050025#include "data/libclient/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070026#include "data/styles/R.h"
Adam Lesinski03ebac82017-09-25 13:10:14 -070027#include "data/system/R.h"
Adam Lesinski9d9cc622014-08-29 14:10:04 -070028
Adam Lesinski4c67a472016-11-10 16:43:59 -080029namespace app = com::android::app;
Adam Lesinskida431a22016-12-29 16:08:16 -050030namespace lib_one = com::android::lib_one;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -070031namespace lib_two = com::android::lib_two;
Adam Lesinskida431a22016-12-29 16:08:16 -050032namespace libclient = com::android::libclient;
Adam Lesinski9d9cc622014-08-29 14:10:04 -070033
Adam Lesinski4c67a472016-11-10 16:43:59 -080034namespace android {
Adam Lesinski9d9cc622014-08-29 14:10:04 -070035
Adam Lesinski7ad11102016-10-28 16:39:15 -070036class ThemeTest : public ::testing::Test {
37 public:
38 void SetUp() override {
Adam Lesinski03ebac82017-09-25 13:10:14 -070039 system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
40 ASSERT_NE(nullptr, system_assets_);
41
Adam Lesinski7ad11102016-10-28 16:39:15 -070042 style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
43 ASSERT_NE(nullptr, style_assets_);
Adam Lesinskida431a22016-12-29 16:08:16 -050044
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_);
Adam Lesinski7ad11102016-10-28 16:39:15 -070053 }
Adam Lesinski9d9cc622014-08-29 14:10:04 -070054
Adam Lesinski7ad11102016-10-28 16:39:15 -070055 protected:
Adam Lesinski03ebac82017-09-25 13:10:14 -070056 std::unique_ptr<const ApkAssets> system_assets_;
Adam Lesinski0c405242017-01-13 20:47:26 -080057 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_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070061};
Adam Lesinski9d9cc622014-08-29 14:10:04 -070062
Adam Lesinski7ad11102016-10-28 16:39:15 -070063TEST_F(ThemeTest, EmptyTheme) {
64 AssetManager2 assetmanager;
65 assetmanager.SetApkAssets({style_assets_.get()});
Adam Lesinski9d9cc622014-08-29 14:10:04 -070066
Adam Lesinski7ad11102016-10-28 16:39:15 -070067 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
68 EXPECT_EQ(0u, theme->GetChangingConfigurations());
69 EXPECT_EQ(&assetmanager, theme->GetAssetManager());
Adam Lesinski4c67a472016-11-10 16:43:59 -080070
Adam Lesinski7ad11102016-10-28 16:39:15 -070071 Res_value value;
72 uint32_t flags;
73 EXPECT_EQ(kInvalidCookie, theme->GetAttribute(app::R::attr::attr_one, &value, &flags));
74}
Adam Lesinski4c67a472016-11-10 16:43:59 -080075
Adam Lesinski7ad11102016-10-28 16:39:15 -070076TEST_F(ThemeTest, SingleThemeNoParent) {
77 AssetManager2 assetmanager;
78 assetmanager.SetApkAssets({style_assets_.get()});
79
80 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
81 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleOne));
82
83 Res_value value;
84 uint32_t flags;
85 ApkAssetsCookie cookie;
86
87 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
88 ASSERT_NE(kInvalidCookie, cookie);
89 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
90 EXPECT_EQ(1u, value.data);
91 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
92
93 cookie = theme->GetAttribute(app::R::attr::attr_two, &value, &flags);
94 ASSERT_NE(kInvalidCookie, cookie);
95 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
96 EXPECT_EQ(2u, value.data);
97 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
98}
99
100TEST_F(ThemeTest, SingleThemeWithParent) {
101 AssetManager2 assetmanager;
102 assetmanager.SetApkAssets({style_assets_.get()});
103
104 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
105 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo));
106
107 Res_value value;
108 uint32_t flags;
109 ApkAssetsCookie cookie;
110
111 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
112 ASSERT_NE(kInvalidCookie, cookie);
113 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
114 EXPECT_EQ(1u, value.data);
115 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
116
117 cookie = theme->GetAttribute(app::R::attr::attr_two, &value, &flags);
118 ASSERT_NE(kInvalidCookie, cookie);
119 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
120 EXPECT_EQ(0, cookie);
121 EXPECT_EQ(std::string("string"),
122 GetStringFromPool(assetmanager.GetStringPoolForCookie(0), value.data));
123 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
124
125 // This attribute should point to an attr_indirect, so the result should be 3.
126 cookie = theme->GetAttribute(app::R::attr::attr_three, &value, &flags);
127 ASSERT_NE(kInvalidCookie, cookie);
128 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
129 EXPECT_EQ(3u, value.data);
130 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
131}
132
Adam Lesinski30080e22017-10-16 16:18:09 -0700133TEST_F(ThemeTest, TryToUseBadResourceId) {
134 AssetManager2 assetmanager;
135 assetmanager.SetApkAssets({style_assets_.get()});
136
137 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
138 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo));
139
140 Res_value value;
141 uint32_t flags;
142 ASSERT_EQ(kInvalidCookie, theme->GetAttribute(0x7f000001, &value, &flags));
143}
144
Adam Lesinski7ad11102016-10-28 16:39:15 -0700145TEST_F(ThemeTest, MultipleThemesOverlaidNotForce) {
146 AssetManager2 assetmanager;
147 assetmanager.SetApkAssets({style_assets_.get()});
148
149 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
150 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo));
151 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree));
152
153 Res_value value;
154 uint32_t flags;
155 ApkAssetsCookie cookie;
156
157 // attr_one is still here from the base.
158 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
159 ASSERT_NE(kInvalidCookie, cookie);
160 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
161 EXPECT_EQ(1u, value.data);
162 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
163
164 // check for the new attr_six
165 cookie = theme->GetAttribute(app::R::attr::attr_six, &value, &flags);
166 ASSERT_NE(kInvalidCookie, cookie);
167 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
168 EXPECT_EQ(6u, value.data);
169 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
170
171 // check for the old attr_five (force=true was not used).
172 cookie = theme->GetAttribute(app::R::attr::attr_five, &value, &flags);
173 ASSERT_NE(kInvalidCookie, cookie);
174 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
175 EXPECT_EQ(app::R::string::string_one, value.data);
176 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
177}
178
179TEST_F(ThemeTest, MultipleThemesOverlaidForced) {
180 AssetManager2 assetmanager;
181 assetmanager.SetApkAssets({style_assets_.get()});
182
183 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
184 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo));
185 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree, true /* force */));
186
187 Res_value value;
188 uint32_t flags;
189 ApkAssetsCookie cookie;
190
191 // attr_one is still here from the base.
192 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
193 ASSERT_NE(kInvalidCookie, cookie);
194 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
195 EXPECT_EQ(1u, value.data);
196 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
197
198 // check for the new attr_six
199 cookie = theme->GetAttribute(app::R::attr::attr_six, &value, &flags);
200 ASSERT_NE(kInvalidCookie, cookie);
201 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
202 EXPECT_EQ(6u, value.data);
203 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
204
205 // check for the new attr_five (force=true was used).
206 cookie = theme->GetAttribute(app::R::attr::attr_five, &value, &flags);
207 ASSERT_NE(kInvalidCookie, cookie);
208 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
209 EXPECT_EQ(5u, value.data);
210 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
211}
212
Adam Lesinskida431a22016-12-29 16:08:16 -0500213TEST_F(ThemeTest, ResolveDynamicAttributesAndReferencesToSharedLibrary) {
214 AssetManager2 assetmanager;
215 assetmanager.SetApkAssets(
216 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
217
218 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
219 ASSERT_TRUE(theme->ApplyStyle(libclient::R::style::Theme, false /*force*/));
220
221 Res_value value;
222 uint32_t flags;
223 ApkAssetsCookie cookie;
224
225 // The attribute should be resolved to the final value.
226 cookie = theme->GetAttribute(libclient::R::attr::foo, &value, &flags);
227 ASSERT_NE(kInvalidCookie, cookie);
228 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
229 EXPECT_EQ(700u, value.data);
230 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
231
232 // The reference should be resolved to a TYPE_REFERENCE.
233 cookie = theme->GetAttribute(libclient::R::attr::bar, &value, &flags);
234 ASSERT_NE(kInvalidCookie, cookie);
235 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
236
237 // lib_one is assigned package ID 0x03.
Adam Lesinski929d6512017-01-16 19:11:19 -0800238 EXPECT_EQ(3u, get_package_id(value.data));
239 EXPECT_EQ(get_type_id(lib_one::R::string::foo), get_type_id(value.data));
240 EXPECT_EQ(get_entry_id(lib_one::R::string::foo), get_entry_id(value.data));
Adam Lesinskida431a22016-12-29 16:08:16 -0500241}
242
Adam Lesinski7ad11102016-10-28 16:39:15 -0700243TEST_F(ThemeTest, CopyThemeSameAssetManager) {
244 AssetManager2 assetmanager;
245 assetmanager.SetApkAssets({style_assets_.get()});
246
247 std::unique_ptr<Theme> theme_one = assetmanager.NewTheme();
248 ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne));
249
250 Res_value value;
251 uint32_t flags;
252 ApkAssetsCookie cookie;
253
254 // attr_one is still here from the base.
255 cookie = theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags);
256 ASSERT_NE(kInvalidCookie, cookie);
257 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
258 EXPECT_EQ(1u, value.data);
259 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
260
261 // attr_six is not here.
262 EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_six, &value, &flags));
263
264 std::unique_ptr<Theme> theme_two = assetmanager.NewTheme();
265 ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleThree));
266
267 // Copy the theme to theme_one.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700268 theme_one->SetTo(*theme_two);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700269
270 // Clear theme_two to make sure we test that there WAS a copy.
271 theme_two->Clear();
272
273 // attr_one is now not here.
274 EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags));
275
276 // attr_six is now here because it was copied.
277 cookie = theme_one->GetAttribute(app::R::attr::attr_six, &value, &flags);
278 ASSERT_NE(kInvalidCookie, cookie);
279 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
280 EXPECT_EQ(6u, value.data);
281 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
282}
283
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700284TEST_F(ThemeTest, OnlyCopySameAssetsThemeWhenAssetManagersDiffer) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700285 AssetManager2 assetmanager_one;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700286 assetmanager_one.SetApkAssets({system_assets_.get(), lib_one_assets_.get(), style_assets_.get(),
287 libclient_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700288
289 AssetManager2 assetmanager_two;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700290 assetmanager_two.SetApkAssets({system_assets_.get(), lib_two_assets_.get(), lib_one_assets_.get(),
291 style_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700292
293 auto theme_one = assetmanager_one.NewTheme();
294 ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne));
295
296 auto theme_two = assetmanager_two.NewTheme();
Adam Lesinski03ebac82017-09-25 13:10:14 -0700297 ASSERT_TRUE(theme_two->ApplyStyle(R::style::Theme_One));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700298 ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleTwo));
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700299 ASSERT_TRUE(theme_two->ApplyStyle(fix_package_id(lib_one::R::style::Theme, 0x03),
300 false /*force*/));
301 ASSERT_TRUE(theme_two->ApplyStyle(fix_package_id(lib_two::R::style::Theme, 0x02),
302 false /*force*/));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700303
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700304 theme_one->SetTo(*theme_two);
Adam Lesinski03ebac82017-09-25 13:10:14 -0700305
306 Res_value value;
307 uint32_t flags;
308
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700309 // System resources (present in destination asset manager)
310 EXPECT_EQ(0, theme_one->GetAttribute(R::attr::foreground, &value, &flags));
Adam Lesinski03ebac82017-09-25 13:10:14 -0700311
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700312 // The cookie of the style asset is 3 in the source and 2 in the destination.
313 // Check that the cookie has been rewritten to the destination values
314 EXPECT_EQ(2, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags));
315
316 // The cookie of the lib_one asset is 2 in the source and 1 in the destination.
317 // The package id of the lib_one package is 0x03 in the source and 0x02 in the destination
318 // Check that the cookie and packages have been rewritten to the destination values
319 EXPECT_EQ(1, theme_one->GetAttribute(fix_package_id(lib_one::R::attr::attr1, 0x02), &value,
320 &flags));
321 EXPECT_EQ(1, theme_one->GetAttribute(fix_package_id(lib_one::R::attr::attr2, 0x02), &value,
322 &flags));
323
324 // attr2 references an attribute in lib_one. Check that the resolution of the attribute value is
325 // correct after the value of attr2 had its package id rewritten to the destination package id
326 EXPECT_EQ(700, value.data);
Adam Lesinski9d9cc622014-08-29 14:10:04 -0700327}
328
Adam Lesinski4c67a472016-11-10 16:43:59 -0800329} // namespace android