blob: f658735da5154a7c0bd3b2194bc23796519f71ef [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 {
Ryan Mitchell39cacf22020-03-16 14:54:02 -070039 system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", PROPERTY_SYSTEM);
Adam Lesinski03ebac82017-09-25 13:10:14 -070040 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());
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070070 EXPECT_FALSE(theme->GetAttribute(app::R::attr::attr_one).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -070071}
Adam Lesinski4c67a472016-11-10 16:43:59 -080072
Adam Lesinski7ad11102016-10-28 16:39:15 -070073TEST_F(ThemeTest, SingleThemeNoParent) {
74 AssetManager2 assetmanager;
75 assetmanager.SetApkAssets({style_assets_.get()});
76
77 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070078 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleOne).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -070079
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070080 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -070085
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070086 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -070091}
92
93TEST_F(ThemeTest, SingleThemeWithParent) {
94 AssetManager2 assetmanager;
95 assetmanager.SetApkAssets({style_assets_.get()});
96
97 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070098 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -070099
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700100 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700105
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700106 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700110 EXPECT_EQ(std::string("string"),
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700111 GetStringFromPool(assetmanager.GetStringPoolForCookie(0), value->data));
112 EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), value->flags);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700113
114 // This attribute should point to an attr_indirect, so the result should be 3.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700115 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700120}
121
Adam Lesinski30080e22017-10-16 16:18:09 -0700122TEST_F(ThemeTest, TryToUseBadResourceId) {
123 AssetManager2 assetmanager;
124 assetmanager.SetApkAssets({style_assets_.get()});
125
126 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700127 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
128 ASSERT_FALSE(theme->GetAttribute(0x7f000001));
Adam Lesinski30080e22017-10-16 16:18:09 -0700129}
130
Adam Lesinski7ad11102016-10-28 16:39:15 -0700131TEST_F(ThemeTest, MultipleThemesOverlaidNotForce) {
132 AssetManager2 assetmanager;
133 assetmanager.SetApkAssets({style_assets_.get()});
134
135 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700136 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
137 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700138
139 // attr_one is still here from the base.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700140 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700145
146 // check for the new attr_six
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700147 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700152
153 // check for the old attr_five (force=true was not used).
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700154 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700159}
160
161TEST_F(ThemeTest, MultipleThemesOverlaidForced) {
162 AssetManager2 assetmanager;
163 assetmanager.SetApkAssets({style_assets_.get()});
164
165 std::unique_ptr<Theme> theme = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700166 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo).has_value());
167 ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree, true /* force */).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700168
169 // attr_one is still here from the base.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700170 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700175
176 // check for the new attr_six
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700177 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700182
183 // check for the new attr_five (force=true was used).
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700184 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700189}
190
Adam Lesinskida431a22016-12-29 16:08:16 -0500191TEST_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();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700197 ASSERT_TRUE(theme->ApplyStyle(libclient::R::style::Theme, false /*force*/).has_value());
Adam Lesinskida431a22016-12-29 16:08:16 -0500198
199 // The attribute should be resolved to the final value.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700200 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);
Adam Lesinskida431a22016-12-29 16:08:16 -0500205
206 // The reference should be resolved to a TYPE_REFERENCE.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700207 value = theme->GetAttribute(libclient::R::attr::bar);
208 ASSERT_TRUE(value);
209 EXPECT_EQ(Res_value::TYPE_REFERENCE, value->type);
Adam Lesinskida431a22016-12-29 16:08:16 -0500210
211 // lib_one is assigned package ID 0x03.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700212 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));
Adam Lesinskida431a22016-12-29 16:08:16 -0500215}
216
Adam Lesinski7ad11102016-10-28 16:39:15 -0700217TEST_F(ThemeTest, CopyThemeSameAssetManager) {
218 AssetManager2 assetmanager;
219 assetmanager.SetApkAssets({style_assets_.get()});
220
221 std::unique_ptr<Theme> theme_one = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700222 ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700223
224 // attr_one is still here from the base.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700225 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700230
231 // attr_six is not here.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700232 ASSERT_FALSE(theme_one->GetAttribute(app::R::attr::attr_six).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700233
234 std::unique_ptr<Theme> theme_two = assetmanager.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700235 ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleThree).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700236
237 // Copy the theme to theme_one.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700238 theme_one->SetTo(*theme_two);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700239
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.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700244 ASSERT_FALSE(theme_one->GetAttribute(app::R::attr::attr_one).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700245
246 // attr_six is now here because it was copied.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700247 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);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700252}
253
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700254TEST_F(ThemeTest, OnlyCopySameAssetsThemeWhenAssetManagersDiffer) {
Ryan Mitchell93bca972019-03-08 17:26:28 -0800255 AssetManager2 assetmanager_dst;
256 assetmanager_dst.SetApkAssets({system_assets_.get(), lib_one_assets_.get(), style_assets_.get(),
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700257 libclient_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700258
Ryan Mitchell93bca972019-03-08 17:26:28 -0800259 AssetManager2 assetmanager_src;
260 assetmanager_src.SetApkAssets({system_assets_.get(), lib_two_assets_.get(), lib_one_assets_.get(),
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700261 style_assets_.get()});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700262
Ryan Mitchell93bca972019-03-08 17:26:28 -0800263 auto theme_dst = assetmanager_dst.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700264 ASSERT_TRUE(theme_dst->ApplyStyle(app::R::style::StyleOne).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700265
Ryan Mitchell93bca972019-03-08 17:26:28 -0800266 auto theme_src = assetmanager_src.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700267 ASSERT_TRUE(theme_src->ApplyStyle(R::style::Theme_One).has_value());
268 ASSERT_TRUE(theme_src->ApplyStyle(app::R::style::StyleTwo).has_value());
Ryan Mitchell93bca972019-03-08 17:26:28 -0800269 ASSERT_TRUE(theme_src->ApplyStyle(fix_package_id(lib_one::R::style::Theme, 0x03),
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700270 false /*force*/).has_value());
Ryan Mitchell93bca972019-03-08 17:26:28 -0800271 ASSERT_TRUE(theme_src->ApplyStyle(fix_package_id(lib_two::R::style::Theme, 0x02),
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700272 false /*force*/).has_value());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700273
Ryan Mitchell93bca972019-03-08 17:26:28 -0800274 theme_dst->SetTo(*theme_src);
Adam Lesinski03ebac82017-09-25 13:10:14 -0700275
Ryan Mitchell93bca972019-03-08 17:26:28 -0800276 // System resources (present in destination asset manager).
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700277 auto value = theme_dst->GetAttribute(R::attr::foreground);
278 ASSERT_TRUE(value.has_value());
279 EXPECT_EQ(0, value->cookie);
Adam Lesinski03ebac82017-09-25 13:10:14 -0700280
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700281 // The cookie of the style asset is 3 in the source and 2 in the destination.
Ryan Mitchell93bca972019-03-08 17:26:28 -0800282 // Check that the cookie has been rewritten to the destination values.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700283 value = theme_dst->GetAttribute(app::R::attr::attr_one);
284 ASSERT_TRUE(value.has_value());
285 EXPECT_EQ(2, value->cookie);
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700286
287 // The cookie of the lib_one asset is 2 in the source and 1 in the destination.
288 // The package id of the lib_one package is 0x03 in the source and 0x02 in the destination
Ryan Mitchell93bca972019-03-08 17:26:28 -0800289 // Check that the cookie and packages have been rewritten to the destination values.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700290 value = theme_dst->GetAttribute(fix_package_id(lib_one::R::attr::attr1, 0x02));
291 ASSERT_TRUE(value.has_value());
292 EXPECT_EQ(1, value->cookie);
293
294 value = theme_dst->GetAttribute(fix_package_id(lib_one::R::attr::attr2, 0x02));
295 ASSERT_TRUE(value.has_value());
296 EXPECT_EQ(1, value->cookie);
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -0700297
298 // attr2 references an attribute in lib_one. Check that the resolution of the attribute value is
Ryan Mitchell93bca972019-03-08 17:26:28 -0800299 // correct after the value of attr2 had its package id rewritten to the destination package id.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700300 EXPECT_EQ(700, value->data);
Adam Lesinski9d9cc622014-08-29 14:10:04 -0700301}
302
Ryan Mitchell93bca972019-03-08 17:26:28 -0800303TEST_F(ThemeTest, CopyNonReferencesWhenPackagesDiffer) {
304 AssetManager2 assetmanager_dst;
305 assetmanager_dst.SetApkAssets({system_assets_.get()});
306
307 AssetManager2 assetmanager_src;
308 assetmanager_src.SetApkAssets({system_assets_.get(), style_assets_.get()});
309
310 auto theme_dst = assetmanager_dst.NewTheme();
311 auto theme_src = assetmanager_src.NewTheme();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700312 ASSERT_TRUE(theme_src->ApplyStyle(app::R::style::StyleSeven).has_value());
Ryan Mitchell93bca972019-03-08 17:26:28 -0800313 theme_dst->SetTo(*theme_src);
314
Ryan Mitchell93bca972019-03-08 17:26:28 -0800315 // Allow inline resource values to be copied even if the source apk asset is not present in the
316 // destination.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700317 auto value = theme_dst->GetAttribute(0x0101021b /* android:versionCode */);
318 ASSERT_TRUE(value.has_value());
319 EXPECT_EQ(0, value->cookie);
Ryan Mitchell93bca972019-03-08 17:26:28 -0800320
321 // Do not copy strings since the data is an index into the values string pool of the source apk
322 // asset.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700323 EXPECT_FALSE(theme_dst->GetAttribute(0x01010001 /* android:label */).has_value());
Ryan Mitchell93bca972019-03-08 17:26:28 -0800324
325 // Do not copy values that reference another resource if the resource is not present in the
326 // destination.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700327 EXPECT_FALSE(theme_dst->GetAttribute(0x01010002 /* android:icon */).has_value());
328 EXPECT_FALSE(theme_dst->GetAttribute(0x010100d1 /* android:tag */).has_value());
Ryan Mitchell93bca972019-03-08 17:26:28 -0800329
330 // Allow @empty to and @null to be copied.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700331 value = theme_dst->GetAttribute(0x010100d0 /* android:id */);
332 ASSERT_TRUE(value.has_value());
333 EXPECT_EQ(0, value->cookie);
334
335 value = theme_dst->GetAttribute(0x01010000 /* android:theme */);
336 ASSERT_TRUE(value.has_value());
337 EXPECT_EQ(0, value->cookie);
Ryan Mitchell93bca972019-03-08 17:26:28 -0800338}
339
Adam Lesinski4c67a472016-11-10 16:43:59 -0800340} // namespace android