| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/common/shelf/shelf_background_animator.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "ash/common/shelf/shelf_background_animator_observer.h" | |
| 10 #include "ash/common/shelf/shelf_constants.h" | |
| 11 #include "ash/common/test/material_design_controller_test_api.h" | |
| 12 #include "base/bind.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/test/test_mock_time_task_runner.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace ash { | |
| 19 | |
| 20 using test::MaterialDesignControllerTestAPI; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const int kMaxAlpha = 255; | |
| 25 | |
| 26 // A valid alpha value that is distinct from any final animation state values. | |
| 27 // Used to check if alpha values are changed during animations. | |
| 28 const int kDummyAlpha = 111; | |
| 29 | |
| 30 // Observer that caches alpha values for the last observation. | |
| 31 class TestShelfBackgroundObserver : public ShelfBackgroundAnimatorObserver { | |
| 32 public: | |
| 33 TestShelfBackgroundObserver() {} | |
| 34 ~TestShelfBackgroundObserver() override {} | |
| 35 | |
| 36 int opaque_background_alpha() const { return opaque_background_alpha_; } | |
| 37 | |
| 38 int item_background_alpha() const { return item_background_alpha_; } | |
| 39 | |
| 40 int asset_background_alpha() const { return asset_background_alpha_; } | |
| 41 | |
| 42 // ShelfBackgroundObserver: | |
| 43 void UpdateShelfOpaqueBackground(int alpha) override; | |
| 44 void UpdateShelfAssetBackground(int alpha) override; | |
| 45 void UpdateShelfItemBackground(int alpha) override; | |
| 46 | |
| 47 private: | |
| 48 int opaque_background_alpha_ = 0; | |
| 49 int item_background_alpha_ = 0; | |
| 50 int asset_background_alpha_ = 0; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(TestShelfBackgroundObserver); | |
| 53 }; | |
| 54 | |
| 55 void TestShelfBackgroundObserver::UpdateShelfOpaqueBackground(int alpha) { | |
| 56 opaque_background_alpha_ = alpha; | |
| 57 } | |
| 58 | |
| 59 void TestShelfBackgroundObserver::UpdateShelfAssetBackground(int alpha) { | |
| 60 asset_background_alpha_ = alpha; | |
| 61 } | |
| 62 | |
| 63 void TestShelfBackgroundObserver::UpdateShelfItemBackground(int alpha) { | |
| 64 item_background_alpha_ = alpha; | |
| 65 } | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 // Provides internal access to a ShelfBackgroundAnimator instance. | |
| 70 class ShelfBackgroundAnimatorTestApi { | |
| 71 public: | |
| 72 explicit ShelfBackgroundAnimatorTestApi(ShelfBackgroundAnimator* animator) | |
| 73 : animator_(animator) {} | |
| 74 | |
| 75 ~ShelfBackgroundAnimatorTestApi() {} | |
| 76 | |
| 77 ShelfBackgroundType previous_background_type() const { | |
| 78 return animator_->previous_background_type_; | |
| 79 } | |
| 80 | |
| 81 BackgroundAnimator* opaque_background_animator() const { | |
| 82 return animator_->opaque_background_animator_.get(); | |
| 83 } | |
| 84 | |
| 85 BackgroundAnimator* asset_background_animator() const { | |
| 86 return animator_->asset_background_animator_.get(); | |
| 87 } | |
| 88 | |
| 89 BackgroundAnimator* item_background_animator() const { | |
| 90 return animator_->item_background_animator_.get(); | |
| 91 } | |
| 92 | |
| 93 bool can_reuse_animators() const { return animator_->can_reuse_animators_; } | |
| 94 | |
| 95 private: | |
| 96 // The instance to provide internal access to. | |
| 97 ShelfBackgroundAnimator* animator_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestApi); | |
| 100 }; | |
| 101 | |
| 102 // Note: this is not a parameterized test (like other MD tests) because the | |
| 103 // behavior is so different and not all tests need to be run for both MD and | |
| 104 // non-MD variants. | |
| 105 class ShelfBackgroundAnimatorTestBase : public testing::Test { | |
| 106 public: | |
| 107 ShelfBackgroundAnimatorTestBase() {} | |
| 108 ~ShelfBackgroundAnimatorTestBase() override {} | |
| 109 | |
| 110 virtual MaterialDesignController::Mode GetMaterialDesignMode() = 0; | |
| 111 | |
| 112 int expected_translucent_alpha() const { return expected_translucent_alpha_; } | |
| 113 | |
| 114 // testing::Test: | |
| 115 void SetUp() override; | |
| 116 | |
| 117 protected: | |
| 118 // Convenience wrapper for |animator_|'s PaintBackground() that always uses | |
| 119 // BACKGROUND_CHANGE_IMMEDIATE. | |
| 120 void PaintBackground(ShelfBackgroundType background_type); | |
| 121 | |
| 122 // Set all of the alpha values for the |observer_|. | |
| 123 void SetAlphaValuesOnObserver(int alpha); | |
| 124 | |
| 125 // Completes all the animations. | |
| 126 void CompleteAnimations(); | |
| 127 | |
| 128 TestShelfBackgroundObserver observer_; | |
| 129 | |
| 130 // Test target. | |
| 131 std::unique_ptr<ShelfBackgroundAnimator> animator_; | |
| 132 | |
| 133 // Provides internal access to |animator_|. | |
| 134 std::unique_ptr<ShelfBackgroundAnimatorTestApi> test_api_; | |
| 135 | |
| 136 // Used to control the animations. | |
| 137 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | |
| 138 | |
| 139 private: | |
| 140 std::unique_ptr<base::ThreadTaskRunnerHandle> task_runner_handle_; | |
| 141 | |
| 142 std::unique_ptr<MaterialDesignControllerTestAPI> material_mode_test_api_; | |
| 143 | |
| 144 // The expected alpha value for translucent items. Cannot be a constant | |
| 145 // because it is different for material design and non-material. | |
| 146 int expected_translucent_alpha_ = 0; | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestBase); | |
| 149 }; | |
| 150 | |
| 151 void ShelfBackgroundAnimatorTestBase::SetUp() { | |
| 152 task_runner_ = new base::TestMockTimeTaskRunner(); | |
| 153 task_runner_handle_.reset(new base::ThreadTaskRunnerHandle(task_runner_)); | |
| 154 | |
| 155 material_mode_test_api_.reset( | |
| 156 new MaterialDesignControllerTestAPI(GetMaterialDesignMode())); | |
| 157 animator_.reset( | |
| 158 new ShelfBackgroundAnimator(SHELF_BACKGROUND_DEFAULT, nullptr)); | |
| 159 animator_->AddObserver(&observer_); | |
| 160 | |
| 161 test_api_.reset(new ShelfBackgroundAnimatorTestApi(animator_.get())); | |
| 162 | |
| 163 // Initialized after the Material Design mode because GetShelfConstant() | |
| 164 // depends on the mode. | |
| 165 expected_translucent_alpha_ = GetShelfConstant(SHELF_BACKGROUND_ALPHA); | |
| 166 } | |
| 167 | |
| 168 void ShelfBackgroundAnimatorTestBase::PaintBackground( | |
| 169 ShelfBackgroundType background_type) { | |
| 170 animator_->PaintBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE); | |
| 171 } | |
| 172 | |
| 173 void ShelfBackgroundAnimatorTestBase::SetAlphaValuesOnObserver(int alpha) { | |
| 174 observer_.UpdateShelfOpaqueBackground(alpha); | |
| 175 observer_.UpdateShelfAssetBackground(alpha); | |
| 176 observer_.UpdateShelfItemBackground(alpha); | |
| 177 } | |
| 178 | |
| 179 void ShelfBackgroundAnimatorTestBase::CompleteAnimations() { | |
| 180 task_runner_->FastForwardUntilNoTasksRemain(); | |
| 181 } | |
| 182 | |
| 183 class ShelfBackgroundAnimatorNonMDTest | |
| 184 : public ShelfBackgroundAnimatorTestBase { | |
| 185 public: | |
| 186 ShelfBackgroundAnimatorNonMDTest() {} | |
| 187 ~ShelfBackgroundAnimatorNonMDTest() override {} | |
| 188 | |
| 189 MaterialDesignController::Mode GetMaterialDesignMode() override { | |
| 190 return MaterialDesignController::NON_MATERIAL; | |
| 191 } | |
| 192 | |
| 193 private: | |
| 194 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorNonMDTest); | |
| 195 }; | |
| 196 | |
| 197 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. | |
| 198 TEST_F(ShelfBackgroundAnimatorNonMDTest, DefaultBackground) { | |
| 199 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 200 | |
| 201 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 202 EXPECT_EQ(0, observer_.opaque_background_alpha()); | |
| 203 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
| 204 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); | |
| 205 } | |
| 206 | |
| 207 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. | |
| 208 TEST_F(ShelfBackgroundAnimatorNonMDTest, OverlapBackground) { | |
| 209 PaintBackground(SHELF_BACKGROUND_OVERLAP); | |
| 210 | |
| 211 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); | |
| 212 EXPECT_EQ(0, observer_.opaque_background_alpha()); | |
| 213 EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha()); | |
| 214 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); | |
| 215 } | |
| 216 | |
| 217 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. | |
| 218 TEST_F(ShelfBackgroundAnimatorNonMDTest, MaximizedBackground) { | |
| 219 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 220 | |
| 221 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
| 222 EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha()); | |
| 223 EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha()); | |
| 224 EXPECT_EQ(kMaxAlpha, observer_.item_background_alpha()); | |
| 225 } | |
| 226 | |
| 227 class ShelfBackgroundAnimatorMDTest : public ShelfBackgroundAnimatorTestBase { | |
| 228 public: | |
| 229 ShelfBackgroundAnimatorMDTest() {} | |
| 230 ~ShelfBackgroundAnimatorMDTest() override {} | |
| 231 | |
| 232 MaterialDesignController::Mode GetMaterialDesignMode() override { | |
| 233 return MaterialDesignController::MATERIAL_EXPERIMENTAL; | |
| 234 } | |
| 235 | |
| 236 private: | |
| 237 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorMDTest); | |
| 238 }; | |
| 239 | |
| 240 // Verify the |previous_background_type_| and |target_background_type_| values | |
| 241 // when animating to the same target type multiple times. | |
| 242 TEST_F(ShelfBackgroundAnimatorMDTest, | |
| 243 BackgroundTypesWhenAnimatingToSameTarget) { | |
| 244 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 245 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
| 246 | |
| 247 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 248 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 249 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); | |
| 250 | |
| 251 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 252 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 253 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); | |
| 254 } | |
| 255 | |
| 256 // Verify subsequent calls to PaintBackground() using the | |
| 257 // BACKGROUND_CHANGE_ANIMATE change type are ignored. | |
| 258 TEST_F(ShelfBackgroundAnimatorMDTest, | |
| 259 MultipleAnimateCallsToSameTargetAreIgnored) { | |
| 260 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 261 SetAlphaValuesOnObserver(kDummyAlpha); | |
| 262 animator_->PaintBackground(SHELF_BACKGROUND_DEFAULT, | |
| 263 BACKGROUND_CHANGE_ANIMATE); | |
| 264 CompleteAnimations(); | |
| 265 | |
| 266 EXPECT_NE(observer_.opaque_background_alpha(), kDummyAlpha); | |
| 267 EXPECT_NE(observer_.item_background_alpha(), kDummyAlpha); | |
| 268 | |
| 269 SetAlphaValuesOnObserver(kDummyAlpha); | |
| 270 animator_->PaintBackground(SHELF_BACKGROUND_DEFAULT, | |
| 271 BACKGROUND_CHANGE_ANIMATE); | |
| 272 CompleteAnimations(); | |
| 273 | |
| 274 EXPECT_EQ(observer_.opaque_background_alpha(), kDummyAlpha); | |
| 275 EXPECT_EQ(observer_.item_background_alpha(), kDummyAlpha); | |
| 276 } | |
| 277 | |
| 278 // Verify observers are updated with the current values when they are added. | |
| 279 TEST_F(ShelfBackgroundAnimatorMDTest, ObserversUpdatedWhenAdded) { | |
| 280 animator_->RemoveObserver(&observer_); | |
| 281 SetAlphaValuesOnObserver(kDummyAlpha); | |
| 282 | |
| 283 animator_->AddObserver(&observer_); | |
| 284 | |
| 285 EXPECT_NE(observer_.opaque_background_alpha(), kDummyAlpha); | |
| 286 EXPECT_NE(observer_.asset_background_alpha(), kDummyAlpha); | |
| 287 EXPECT_NE(observer_.item_background_alpha(), kDummyAlpha); | |
| 288 } | |
| 289 | |
| 290 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. | |
| 291 TEST_F(ShelfBackgroundAnimatorMDTest, DefaultBackground) { | |
| 292 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 293 | |
| 294 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 295 EXPECT_EQ(0, observer_.opaque_background_alpha()); | |
| 296 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
| 297 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); | |
| 298 } | |
| 299 | |
| 300 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. | |
| 301 TEST_F(ShelfBackgroundAnimatorMDTest, OverlapBackground) { | |
| 302 PaintBackground(SHELF_BACKGROUND_OVERLAP); | |
| 303 | |
| 304 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); | |
| 305 EXPECT_EQ(expected_translucent_alpha(), observer_.opaque_background_alpha()); | |
| 306 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
| 307 EXPECT_EQ(0, observer_.item_background_alpha()); | |
| 308 } | |
| 309 | |
| 310 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. | |
| 311 TEST_F(ShelfBackgroundAnimatorMDTest, MaximizedBackground) { | |
| 312 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 313 | |
| 314 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
| 315 EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha()); | |
| 316 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
| 317 EXPECT_EQ(0, observer_.item_background_alpha()); | |
| 318 } | |
| 319 | |
| 320 // Verify that existing animators are used when animating to the previous state. | |
| 321 TEST_F(ShelfBackgroundAnimatorMDTest, ExistingAnimatorsAreReused) { | |
| 322 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 323 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 324 EXPECT_TRUE(test_api_->can_reuse_animators()); | |
| 325 | |
| 326 const BackgroundAnimator* opaque_animator = | |
| 327 test_api_->opaque_background_animator(); | |
| 328 const BackgroundAnimator* asset_animator = | |
| 329 test_api_->asset_background_animator(); | |
| 330 const BackgroundAnimator* item_animator = | |
| 331 test_api_->item_background_animator(); | |
| 332 | |
| 333 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 334 | |
| 335 EXPECT_EQ(opaque_animator, test_api_->opaque_background_animator()); | |
| 336 EXPECT_EQ(asset_animator, test_api_->asset_background_animator()); | |
| 337 EXPECT_EQ(item_animator, test_api_->item_background_animator()); | |
| 338 } | |
| 339 | |
| 340 // Verify that existing animators are not re-used when the previous animation | |
| 341 // didn't complete. | |
| 342 TEST_F(ShelfBackgroundAnimatorMDTest, | |
| 343 ExistingAnimatorsNotReusedWhenPreviousAnimationsDontComplete) { | |
| 344 EXPECT_NE(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type()); | |
| 345 animator_->PaintBackground(SHELF_BACKGROUND_OVERLAP, | |
| 346 BACKGROUND_CHANGE_ANIMATE); | |
| 347 animator_->PaintBackground(SHELF_BACKGROUND_MAXIMIZED, | |
| 348 BACKGROUND_CHANGE_ANIMATE); | |
| 349 EXPECT_FALSE(test_api_->can_reuse_animators()); | |
| 350 | |
| 351 const BackgroundAnimator* opaque_animator = | |
| 352 test_api_->opaque_background_animator(); | |
| 353 const BackgroundAnimator* asset_animator = | |
| 354 test_api_->asset_background_animator(); | |
| 355 const BackgroundAnimator* item_animator = | |
| 356 test_api_->item_background_animator(); | |
| 357 | |
| 358 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type()); | |
| 359 animator_->PaintBackground(SHELF_BACKGROUND_OVERLAP, | |
| 360 BACKGROUND_CHANGE_ANIMATE); | |
| 361 | |
| 362 EXPECT_NE(opaque_animator, test_api_->opaque_background_animator()); | |
| 363 EXPECT_NE(asset_animator, test_api_->asset_background_animator()); | |
| 364 EXPECT_NE(item_animator, test_api_->item_background_animator()); | |
| 365 } | |
| 366 | |
| 367 // Verify that existing animators are not re-used when the target background | |
| 368 // isn't the same as the previous background. | |
| 369 TEST_F(ShelfBackgroundAnimatorMDTest, | |
| 370 ExistingAnimatorsNotReusedWhenTargetBackgroundNotPreviousBackground) { | |
| 371 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 372 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 373 EXPECT_TRUE(test_api_->can_reuse_animators()); | |
| 374 | |
| 375 const BackgroundAnimator* opaque_animator = | |
| 376 test_api_->opaque_background_animator(); | |
| 377 const BackgroundAnimator* asset_animator = | |
| 378 test_api_->asset_background_animator(); | |
| 379 const BackgroundAnimator* item_animator = | |
| 380 test_api_->item_background_animator(); | |
| 381 | |
| 382 EXPECT_NE(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type()); | |
| 383 PaintBackground(SHELF_BACKGROUND_OVERLAP); | |
| 384 | |
| 385 EXPECT_NE(opaque_animator, test_api_->opaque_background_animator()); | |
| 386 EXPECT_NE(asset_animator, test_api_->asset_background_animator()); | |
| 387 EXPECT_NE(item_animator, test_api_->item_background_animator()); | |
| 388 } | |
| 389 | |
| 390 // Verify animators are not re-used after snapping to the same background type. | |
| 391 TEST_F(ShelfBackgroundAnimatorMDTest, | |
| 392 ExistingAnimatorsNotUsedWhenSnappingToSameTargetBackground) { | |
| 393 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 394 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 395 | |
| 396 EXPECT_FALSE(test_api_->can_reuse_animators()); | |
| 397 } | |
| 398 | |
| 399 // Verify observers are always notified, even when alpha values don't change. | |
| 400 TEST_F(ShelfBackgroundAnimatorMDTest, | |
| 401 ObserversAreNotifiedWhenSnappingToSameTargetBackground) { | |
| 402 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 403 SetAlphaValuesOnObserver(kDummyAlpha); | |
| 404 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 405 | |
| 406 EXPECT_NE(observer_.opaque_background_alpha(), kDummyAlpha); | |
| 407 EXPECT_NE(observer_.asset_background_alpha(), kDummyAlpha); | |
| 408 EXPECT_NE(observer_.item_background_alpha(), kDummyAlpha); | |
| 409 } | |
| 410 | |
| 411 } // namespace ash | |
| OLD | NEW |