blob: feb454e144e3d24f1e45c32fcb82ae19ef5804f6 [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"
24#include "data/libclient/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070025#include "data/styles/R.h"
Adam Lesinski03ebac82017-09-25 13:10:14 -070026#include "data/system/R.h"
Adam Lesinski9d9cc622014-08-29 14:10:04 -070027
Adam Lesinski4c67a472016-11-10 16:43:59 -080028namespace app = com::android::app;
Adam Lesinskida431a22016-12-29 16:08:16 -050029namespace lib_one = com::android::lib_one;
30namespace libclient = com::android::libclient;
Adam Lesinski9d9cc622014-08-29 14:10:04 -070031
Adam Lesinski4c67a472016-11-10 16:43:59 -080032namespace android {
Adam Lesinski9d9cc622014-08-29 14:10:04 -070033
Adam Lesinski7ad11102016-10-28 16:39:15 -070034class ThemeTest : public ::testing::Test {
35 public:
36 void SetUp() override {
Adam Lesinski03ebac82017-09-25 13:10:14 -070037 system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
38 ASSERT_NE(nullptr, system_assets_);
39
Adam Lesinski7ad11102016-10-28 16:39:15 -070040 style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
41 ASSERT_NE(nullptr, style_assets_);
Adam Lesinskida431a22016-12-29 16:08:16 -050042
43 libclient_assets_ = ApkAssets::Load(GetTestDataPath() + "/libclient/libclient.apk");
44 ASSERT_NE(nullptr, libclient_assets_);
45
46 lib_one_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_one/lib_one.apk");
47 ASSERT_NE(nullptr, lib_one_assets_);
48
49 lib_two_assets_ = ApkAssets::Load(GetTestDataPath() + "/lib_two/lib_two.apk");
50 ASSERT_NE(nullptr, lib_two_assets_);
Adam Lesinski7ad11102016-10-28 16:39:15 -070051 }
Adam Lesinski9d9cc622014-08-29 14:10:04 -070052
Adam Lesinski7ad11102016-10-28 16:39:15 -070053 protected:
Adam Lesinski03ebac82017-09-25 13:10:14 -070054 std::unique_ptr<const ApkAssets> system_assets_;
Adam Lesinski0c405242017-01-13 20:47:26 -080055 std::unique_ptr<const ApkAssets> style_assets_;
56 std::unique_ptr<const ApkAssets> libclient_assets_;
57 std::unique_ptr<const ApkAssets> lib_one_assets_;
58 std::unique_ptr<const ApkAssets> lib_two_assets_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070059};
Adam Lesinski9d9cc622014-08-29 14:10:04 -070060
Adam Lesinski7ad11102016-10-28 16:39:15 -070061TEST_F(ThemeTest, EmptyTheme) {
62 AssetManager2 assetmanager;
63 assetmanager.SetApkAssets({style_assets_.get()});
Adam Lesinski9d9cc622014-08-29 14:10:04 -070064
Adam Lesinski7ad11102016-10-28 16:39:15 -070065 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
66 EXPECT_EQ(0u, theme->GetChangingConfigurations());
67 EXPECT_EQ(&assetmanager, theme->GetAssetManager());
Adam Lesinski4c67a472016-11-10 16:43:59 -080068
Adam Lesinski7ad11102016-10-28 16:39:15 -070069 Res_value value;
70 uint32_t flags;
71 EXPECT_EQ(kInvalidCookie, theme->GetAttribute(app::R::attr::attr_one, &value, &flags));
72}
Adam Lesinski4c67a472016-11-10 16:43:59 -080073
Adam Lesinski7ad11102016-10-28 16:39:15 -070074TEST_F(ThemeTest, SingleThemeNoParent) {
75 AssetManager2 assetmanager;
76 assetmanager.SetApkAssets({style_assets_.get()});
77
78 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
79 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleOne));
80
81 Res_value value;
82 uint32_t flags;
83 ApkAssetsCookie cookie;
84
85 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
86 ASSERT_NE(kInvalidCookie, cookie);
87 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
88 EXPECT_EQ(1u, value.data);
89 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
90
91 cookie = theme->GetAttribute(app::R::attr::attr_two, &value, &flags);
92 ASSERT_NE(kInvalidCookie, cookie);
93 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
94 EXPECT_EQ(2u, value.data);
95 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
96}
97
98TEST_F(ThemeTest, SingleThemeWithParent) {
99 AssetManager2 assetmanager;
100 assetmanager.SetApkAssets({style_assets_.get()});
101
102 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
103 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo));
104
105 Res_value value;
106 uint32_t flags;
107 ApkAssetsCookie cookie;
108
109 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
110 ASSERT_NE(kInvalidCookie, cookie);
111 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
112 EXPECT_EQ(1u, value.data);
113 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
114
115 cookie = theme->GetAttribute(app::R::attr::attr_two, &value, &flags);
116 ASSERT_NE(kInvalidCookie, cookie);
117 EXPECT_EQ(Res_value::TYPE_STRING, value.dataType);
118 EXPECT_EQ(0, cookie);
119 EXPECT_EQ(std::string("string"),
120 GetStringFromPool(assetmanager.GetStringPoolForCookie(0), value.data));
121 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
122
123 // This attribute should point to an attr_indirect, so the result should be 3.
124 cookie = theme->GetAttribute(app::R::attr::attr_three, &value, &flags);
125 ASSERT_NE(kInvalidCookie, cookie);
126 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
127 EXPECT_EQ(3u, value.data);
128 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
129}
130
131TEST_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));
137 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree));
138
139 Res_value value;
140 uint32_t flags;
141 ApkAssetsCookie cookie;
142
143 // attr_one is still here from the base.
144 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
145 ASSERT_NE(kInvalidCookie, cookie);
146 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
147 EXPECT_EQ(1u, value.data);
148 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
149
150 // check for the new attr_six
151 cookie = theme->GetAttribute(app::R::attr::attr_six, &value, &flags);
152 ASSERT_NE(kInvalidCookie, cookie);
153 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
154 EXPECT_EQ(6u, value.data);
155 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
156
157 // check for the old attr_five (force=true was not used).
158 cookie = theme->GetAttribute(app::R::attr::attr_five, &value, &flags);
159 ASSERT_NE(kInvalidCookie, cookie);
160 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
161 EXPECT_EQ(app::R::string::string_one, value.data);
162 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
163}
164
165TEST_F(ThemeTest, MultipleThemesOverlaidForced) {
166 AssetManager2 assetmanager;
167 assetmanager.SetApkAssets({style_assets_.get()});
168
169 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
170 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo));
171 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree, true /* force */));
172
173 Res_value value;
174 uint32_t flags;
175 ApkAssetsCookie cookie;
176
177 // attr_one is still here from the base.
178 cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags);
179 ASSERT_NE(kInvalidCookie, cookie);
180 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
181 EXPECT_EQ(1u, value.data);
182 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
183
184 // check for the new attr_six
185 cookie = theme->GetAttribute(app::R::attr::attr_six, &value, &flags);
186 ASSERT_NE(kInvalidCookie, cookie);
187 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
188 EXPECT_EQ(6u, value.data);
189 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
190
191 // check for the new attr_five (force=true was used).
192 cookie = theme->GetAttribute(app::R::attr::attr_five, &value, &flags);
193 ASSERT_NE(kInvalidCookie, cookie);
194 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
195 EXPECT_EQ(5u, value.data);
196 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
197}
198
Adam Lesinskida431a22016-12-29 16:08:16 -0500199TEST_F(ThemeTest, ResolveDynamicAttributesAndReferencesToSharedLibrary) {
200 AssetManager2 assetmanager;
201 assetmanager.SetApkAssets(
202 {lib_two_assets_.get(), lib_one_assets_.get(), libclient_assets_.get()});
203
204 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
205 ASSERT_TRUE(theme->ApplyStyle(libclient::R::style::Theme, false /*force*/));
206
207 Res_value value;
208 uint32_t flags;
209 ApkAssetsCookie cookie;
210
211 // The attribute should be resolved to the final value.
212 cookie = theme->GetAttribute(libclient::R::attr::foo, &value, &flags);
213 ASSERT_NE(kInvalidCookie, cookie);
214 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
215 EXPECT_EQ(700u, value.data);
216 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
217
218 // The reference should be resolved to a TYPE_REFERENCE.
219 cookie = theme->GetAttribute(libclient::R::attr::bar, &value, &flags);
220 ASSERT_NE(kInvalidCookie, cookie);
221 EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType);
222
223 // lib_one is assigned package ID 0x03.
Adam Lesinski929d6512017-01-16 19:11:19 -0800224 EXPECT_EQ(3u, get_package_id(value.data));
225 EXPECT_EQ(get_type_id(lib_one::R::string::foo), get_type_id(value.data));
226 EXPECT_EQ(get_entry_id(lib_one::R::string::foo), get_entry_id(value.data));
Adam Lesinskida431a22016-12-29 16:08:16 -0500227}
228
Adam Lesinski7ad11102016-10-28 16:39:15 -0700229TEST_F(ThemeTest, CopyThemeSameAssetManager) {
230 AssetManager2 assetmanager;
231 assetmanager.SetApkAssets({style_assets_.get()});
232
233 std::unique_ptr<Theme> theme_one = assetmanager.NewTheme();
234 ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne));
235
236 Res_value value;
237 uint32_t flags;
238 ApkAssetsCookie cookie;
239
240 // attr_one is still here from the base.
241 cookie = theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags);
242 ASSERT_NE(kInvalidCookie, cookie);
243 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
244 EXPECT_EQ(1u, value.data);
245 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
246
247 // attr_six is not here.
248 EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_six, &value, &flags));
249
250 std::unique_ptr<Theme> theme_two = assetmanager.NewTheme();
251 ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleThree));
252
253 // Copy the theme to theme_one.
254 ASSERT_TRUE(theme_one->SetTo(*theme_two));
255
256 // Clear theme_two to make sure we test that there WAS a copy.
257 theme_two->Clear();
258
259 // attr_one is now not here.
260 EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags));
261
262 // attr_six is now here because it was copied.
263 cookie = theme_one->GetAttribute(app::R::attr::attr_six, &value, &flags);
264 ASSERT_NE(kInvalidCookie, cookie);
265 EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType);
266 EXPECT_EQ(6u, value.data);
267 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags);
268}
269
Adam Lesinski03ebac82017-09-25 13:10:14 -0700270TEST_F(ThemeTest, OnlyCopySystemThemeWhenAssetManagersDiffer) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700271 AssetManager2 assetmanager_one;
Adam Lesinski03ebac82017-09-25 13:10:14 -0700272 assetmanager_one.SetApkAssets({system_assets_.get(), style_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700273
274 AssetManager2 assetmanager_two;
Adam Lesinski03ebac82017-09-25 13:10:14 -0700275 assetmanager_two.SetApkAssets({system_assets_.get(), style_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700276
277 auto theme_one = assetmanager_one.NewTheme();
278 ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne));
279
280 auto theme_two = assetmanager_two.NewTheme();
Adam Lesinski03ebac82017-09-25 13:10:14 -0700281 ASSERT_TRUE(theme_two->ApplyStyle(R::style::Theme_One));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700282 ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleTwo));
283
Adam Lesinski03ebac82017-09-25 13:10:14 -0700284 EXPECT_TRUE(theme_one->SetTo(*theme_two));
285
286 Res_value value;
287 uint32_t flags;
288
289 // No app resources.
290 EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags));
291
292 // Only system.
293 EXPECT_NE(kInvalidCookie, theme_one->GetAttribute(R::attr::foreground, &value, &flags));
Adam Lesinski9d9cc622014-08-29 14:10:04 -0700294}
295
Adam Lesinski4c67a472016-11-10 16:43:59 -0800296} // namespace android