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 | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 22 | #include "data/styles/R.h" |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 23 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 24 | namespace app = com::android::app; |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 26 | namespace android { |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 28 | class ThemeTest : public ::testing::Test { |
| 29 | public: |
| 30 | void SetUp() override { |
| 31 | style_assets_ = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
| 32 | ASSERT_NE(nullptr, style_assets_); |
| 33 | } |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 35 | protected: |
| 36 | std::unique_ptr<ApkAssets> style_assets_; |
| 37 | }; |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 39 | TEST_F(ThemeTest, EmptyTheme) { |
| 40 | AssetManager2 assetmanager; |
| 41 | assetmanager.SetApkAssets({style_assets_.get()}); |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 43 | std::unique_ptr<Theme> theme = assetmanager.NewTheme(); |
| 44 | EXPECT_EQ(0u, theme->GetChangingConfigurations()); |
| 45 | EXPECT_EQ(&assetmanager, theme->GetAssetManager()); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 46 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 47 | Res_value value; |
| 48 | uint32_t flags; |
| 49 | EXPECT_EQ(kInvalidCookie, theme->GetAttribute(app::R::attr::attr_one, &value, &flags)); |
| 50 | } |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 51 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame^] | 52 | TEST_F(ThemeTest, SingleThemeNoParent) { |
| 53 | AssetManager2 assetmanager; |
| 54 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 55 | |
| 56 | std::unique_ptr<Theme> theme = assetmanager.NewTheme(); |
| 57 | ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleOne)); |
| 58 | |
| 59 | Res_value value; |
| 60 | uint32_t flags; |
| 61 | ApkAssetsCookie cookie; |
| 62 | |
| 63 | cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags); |
| 64 | ASSERT_NE(kInvalidCookie, cookie); |
| 65 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 66 | EXPECT_EQ(1u, value.data); |
| 67 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 68 | |
| 69 | cookie = theme->GetAttribute(app::R::attr::attr_two, &value, &flags); |
| 70 | ASSERT_NE(kInvalidCookie, cookie); |
| 71 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 72 | EXPECT_EQ(2u, value.data); |
| 73 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 74 | } |
| 75 | |
| 76 | TEST_F(ThemeTest, SingleThemeWithParent) { |
| 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::StyleTwo)); |
| 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_STRING, value.dataType); |
| 96 | EXPECT_EQ(0, cookie); |
| 97 | EXPECT_EQ(std::string("string"), |
| 98 | GetStringFromPool(assetmanager.GetStringPoolForCookie(0), value.data)); |
| 99 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 100 | |
| 101 | // This attribute should point to an attr_indirect, so the result should be 3. |
| 102 | cookie = theme->GetAttribute(app::R::attr::attr_three, &value, &flags); |
| 103 | ASSERT_NE(kInvalidCookie, cookie); |
| 104 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 105 | EXPECT_EQ(3u, value.data); |
| 106 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 107 | } |
| 108 | |
| 109 | TEST_F(ThemeTest, MultipleThemesOverlaidNotForce) { |
| 110 | AssetManager2 assetmanager; |
| 111 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 112 | |
| 113 | std::unique_ptr<Theme> theme = assetmanager.NewTheme(); |
| 114 | ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo)); |
| 115 | ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree)); |
| 116 | |
| 117 | Res_value value; |
| 118 | uint32_t flags; |
| 119 | ApkAssetsCookie cookie; |
| 120 | |
| 121 | // attr_one is still here from the base. |
| 122 | cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags); |
| 123 | ASSERT_NE(kInvalidCookie, cookie); |
| 124 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 125 | EXPECT_EQ(1u, value.data); |
| 126 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 127 | |
| 128 | // check for the new attr_six |
| 129 | cookie = theme->GetAttribute(app::R::attr::attr_six, &value, &flags); |
| 130 | ASSERT_NE(kInvalidCookie, cookie); |
| 131 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 132 | EXPECT_EQ(6u, value.data); |
| 133 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 134 | |
| 135 | // check for the old attr_five (force=true was not used). |
| 136 | cookie = theme->GetAttribute(app::R::attr::attr_five, &value, &flags); |
| 137 | ASSERT_NE(kInvalidCookie, cookie); |
| 138 | EXPECT_EQ(Res_value::TYPE_REFERENCE, value.dataType); |
| 139 | EXPECT_EQ(app::R::string::string_one, value.data); |
| 140 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 141 | } |
| 142 | |
| 143 | TEST_F(ThemeTest, MultipleThemesOverlaidForced) { |
| 144 | AssetManager2 assetmanager; |
| 145 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 146 | |
| 147 | std::unique_ptr<Theme> theme = assetmanager.NewTheme(); |
| 148 | ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleTwo)); |
| 149 | ASSERT_TRUE(theme->ApplyStyle(app::R::style::StyleThree, true /* force */)); |
| 150 | |
| 151 | Res_value value; |
| 152 | uint32_t flags; |
| 153 | ApkAssetsCookie cookie; |
| 154 | |
| 155 | // attr_one is still here from the base. |
| 156 | cookie = theme->GetAttribute(app::R::attr::attr_one, &value, &flags); |
| 157 | ASSERT_NE(kInvalidCookie, cookie); |
| 158 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 159 | EXPECT_EQ(1u, value.data); |
| 160 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 161 | |
| 162 | // check for the new attr_six |
| 163 | cookie = theme->GetAttribute(app::R::attr::attr_six, &value, &flags); |
| 164 | ASSERT_NE(kInvalidCookie, cookie); |
| 165 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 166 | EXPECT_EQ(6u, value.data); |
| 167 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 168 | |
| 169 | // check for the new attr_five (force=true was used). |
| 170 | cookie = theme->GetAttribute(app::R::attr::attr_five, &value, &flags); |
| 171 | ASSERT_NE(kInvalidCookie, cookie); |
| 172 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 173 | EXPECT_EQ(5u, value.data); |
| 174 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 175 | } |
| 176 | |
| 177 | TEST_F(ThemeTest, CopyThemeSameAssetManager) { |
| 178 | AssetManager2 assetmanager; |
| 179 | assetmanager.SetApkAssets({style_assets_.get()}); |
| 180 | |
| 181 | std::unique_ptr<Theme> theme_one = assetmanager.NewTheme(); |
| 182 | ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne)); |
| 183 | |
| 184 | Res_value value; |
| 185 | uint32_t flags; |
| 186 | ApkAssetsCookie cookie; |
| 187 | |
| 188 | // attr_one is still here from the base. |
| 189 | cookie = theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags); |
| 190 | ASSERT_NE(kInvalidCookie, cookie); |
| 191 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 192 | EXPECT_EQ(1u, value.data); |
| 193 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 194 | |
| 195 | // attr_six is not here. |
| 196 | EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_six, &value, &flags)); |
| 197 | |
| 198 | std::unique_ptr<Theme> theme_two = assetmanager.NewTheme(); |
| 199 | ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleThree)); |
| 200 | |
| 201 | // Copy the theme to theme_one. |
| 202 | ASSERT_TRUE(theme_one->SetTo(*theme_two)); |
| 203 | |
| 204 | // Clear theme_two to make sure we test that there WAS a copy. |
| 205 | theme_two->Clear(); |
| 206 | |
| 207 | // attr_one is now not here. |
| 208 | EXPECT_EQ(kInvalidCookie, theme_one->GetAttribute(app::R::attr::attr_one, &value, &flags)); |
| 209 | |
| 210 | // attr_six is now here because it was copied. |
| 211 | cookie = theme_one->GetAttribute(app::R::attr::attr_six, &value, &flags); |
| 212 | ASSERT_NE(kInvalidCookie, cookie); |
| 213 | EXPECT_EQ(Res_value::TYPE_INT_DEC, value.dataType); |
| 214 | EXPECT_EQ(6u, value.data); |
| 215 | EXPECT_EQ(static_cast<uint32_t>(ResTable_typeSpec::SPEC_PUBLIC), flags); |
| 216 | } |
| 217 | |
| 218 | TEST_F(ThemeTest, FailToCopyThemeWithDifferentAssetManager) { |
| 219 | AssetManager2 assetmanager_one; |
| 220 | assetmanager_one.SetApkAssets({style_assets_.get()}); |
| 221 | |
| 222 | AssetManager2 assetmanager_two; |
| 223 | assetmanager_two.SetApkAssets({style_assets_.get()}); |
| 224 | |
| 225 | auto theme_one = assetmanager_one.NewTheme(); |
| 226 | ASSERT_TRUE(theme_one->ApplyStyle(app::R::style::StyleOne)); |
| 227 | |
| 228 | auto theme_two = assetmanager_two.NewTheme(); |
| 229 | ASSERT_TRUE(theme_two->ApplyStyle(app::R::style::StyleTwo)); |
| 230 | |
| 231 | EXPECT_FALSE(theme_one->SetTo(*theme_two)); |
Adam Lesinski | 9d9cc62 | 2014-08-29 14:10:04 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 234 | } // namespace android |