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