Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 1 | /* |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 17 | #include "androidfw/AssetManager2.h" |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 19 | #include "android-base/logging.h" |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 20 | |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 21 | #include "TestHelpers.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 22 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 23 | #include "data/lib_one/R.h" |
| 24 | #include "data/libclient/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 25 | #include "data/styles/R.h" |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 26 | #include "data/system/R.h" |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 28 | namespace app = com::android::app; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 29 | namespace lib_one = com::android::lib_one; |
| 30 | namespace libclient = com::android::libclient; |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 32 | namespace android { |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 33 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 34 | class ThemeTest : public ::testing::Test { |
| 35 | public: |
| 36 | void SetUp() override { |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 37 | system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/); |
| 38 | ASSERT_NE(nullptr, system_assets_); |
| 39 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 40 | style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
| 41 | ASSERT_NE(nullptr, style_assets_); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 42 | |
| 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 Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 51 | } |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 53 | protected: |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 54 | std::unique_ptr<const ApkAssets> system_assets_; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 55 | 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 Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 59 | }; |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 61 | TEST_F(ThemeTest, EmptyTheme) { |
| 62 | AssetManager2 assetmanager; |
| 63 | assetmanager.SetApkAssets({style_assets_.get()}); |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 65 | std::unique_ptr<Theme> theme = assetmanager.NewTheme(); |
| 66 | EXPECT_EQ(0u, theme->GetChangingConfigurations()); |
| 67 | EXPECT_EQ(&assetmanager, theme->GetAssetManager()); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 68 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 69 | Res_value value; |
| 70 | uint32_t flags; |
| 71 | EXPECT_EQ(kInvalidCookie, theme->GetAttribute(app::R::attr::attr_one, &value, &flags)); |
| 72 | } |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 73 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 74 | TEST_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 | |
| 98 | TEST_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 | |
| 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)); |
| 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 | |
| 165 | TEST_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 Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 199 | TEST_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 Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 224 | 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 Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 229 | TEST_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 Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 270 | TEST_F(ThemeTest, OnlyCopySystemThemeWhenAssetManagersDiffer) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 271 | AssetManager2 assetmanager_one; |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 272 | assetmanager_one.SetApkAssets({system_assets_.get(), style_assets_.get()}); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 273 | |
| 274 | AssetManager2 assetmanager_two; |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 275 | assetmanager_two.SetApkAssets({system_assets_.get(), style_assets_.get()}); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 276 | |
| 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 Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 281 | ASSERT_TRUE(theme_two->ApplyStyle(R::style::Theme_One)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 282 | ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleTwo)); |
| 283 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame^] | 284 | 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 Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 296 | } // namespace android |