| 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/animation/animation_change_type.h" | |
| 10 #include "ash/common/shelf/shelf_background_animator_observer.h" | |
| 11 #include "ash/common/shelf/shelf_constants.h" | |
| 12 #include "ash/common/wallpaper/wallpaper_controller.h" | |
| 13 #include "ash/common/wm_shell.h" | |
| 14 #include "base/bind.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/test/test_mock_time_task_runner.h" | |
| 17 #include "base/threading/thread_task_runner_handle.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include "ui/gfx/animation/slide_animation.h" | |
| 20 | |
| 21 namespace ash { | |
| 22 namespace { | |
| 23 | |
| 24 static auto kMaxAlpha = ShelfBackgroundAnimator::kMaxAlpha; | |
| 25 | |
| 26 // A valid color value that is distinct from any final animation state values. | |
| 27 // Used to check if color values are changed during animations. | |
| 28 const SkColor kDummyColor = SK_ColorBLUE; | |
| 29 | |
| 30 // Observer that caches color values for the last observation. | |
| 31 class TestShelfBackgroundObserver : public ShelfBackgroundAnimatorObserver { | |
| 32 public: | |
| 33 TestShelfBackgroundObserver() {} | |
| 34 ~TestShelfBackgroundObserver() override {} | |
| 35 | |
| 36 SkColor background_color() const { return background_color_; } | |
| 37 | |
| 38 // Convenience function to get the alpha value from |background_color_|. | |
| 39 int GetBackgroundAlpha() const; | |
| 40 | |
| 41 SkColor item_background_color() const { return item_background_color_; } | |
| 42 | |
| 43 // Convenience function to get the alpha value from |item_background_color_|. | |
| 44 int GetItemBackgroundAlpha() const; | |
| 45 | |
| 46 // ShelfBackgroundObserver: | |
| 47 void UpdateShelfBackground(SkColor color) override; | |
| 48 void UpdateShelfItemBackground(SkColor color) override; | |
| 49 | |
| 50 private: | |
| 51 int background_color_ = SK_ColorTRANSPARENT; | |
| 52 int item_background_color_ = SK_ColorTRANSPARENT; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(TestShelfBackgroundObserver); | |
| 55 }; | |
| 56 | |
| 57 int TestShelfBackgroundObserver::GetBackgroundAlpha() const { | |
| 58 return SkColorGetA(background_color_); | |
| 59 } | |
| 60 | |
| 61 int TestShelfBackgroundObserver::GetItemBackgroundAlpha() const { | |
| 62 return SkColorGetA(item_background_color_); | |
| 63 } | |
| 64 | |
| 65 void TestShelfBackgroundObserver::UpdateShelfBackground(SkColor color) { | |
| 66 background_color_ = color; | |
| 67 } | |
| 68 | |
| 69 void TestShelfBackgroundObserver::UpdateShelfItemBackground(SkColor color) { | |
| 70 item_background_color_ = color; | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 // Provides internal access to a ShelfBackgroundAnimator instance. | |
| 76 class ShelfBackgroundAnimatorTestApi { | |
| 77 public: | |
| 78 explicit ShelfBackgroundAnimatorTestApi(ShelfBackgroundAnimator* animator) | |
| 79 : animator_(animator) {} | |
| 80 | |
| 81 ~ShelfBackgroundAnimatorTestApi() {} | |
| 82 | |
| 83 ShelfBackgroundType previous_background_type() const { | |
| 84 return animator_->previous_background_type_; | |
| 85 } | |
| 86 | |
| 87 gfx::SlideAnimation* animator() const { return animator_->animator_.get(); } | |
| 88 | |
| 89 private: | |
| 90 // The instance to provide internal access to. | |
| 91 ShelfBackgroundAnimator* animator_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestApi); | |
| 94 }; | |
| 95 | |
| 96 class ShelfBackgroundAnimatorTest : public testing::Test { | |
| 97 public: | |
| 98 ShelfBackgroundAnimatorTest() {} | |
| 99 ~ShelfBackgroundAnimatorTest() override {} | |
| 100 | |
| 101 // testing::Test: | |
| 102 void SetUp() override; | |
| 103 | |
| 104 protected: | |
| 105 // Convenience wrapper for |animator_|'s PaintBackground(). | |
| 106 void PaintBackground( | |
| 107 ShelfBackgroundType background_type, | |
| 108 AnimationChangeType change_type = AnimationChangeType::IMMEDIATE); | |
| 109 | |
| 110 // Set all of the color values for the |observer_|. | |
| 111 void SetColorValuesOnObserver(SkColor color); | |
| 112 | |
| 113 // Completes all the animations. | |
| 114 void CompleteAnimations(); | |
| 115 | |
| 116 TestShelfBackgroundObserver observer_; | |
| 117 | |
| 118 // Test target. | |
| 119 std::unique_ptr<ShelfBackgroundAnimator> animator_; | |
| 120 | |
| 121 // Provides internal access to |animator_|. | |
| 122 std::unique_ptr<ShelfBackgroundAnimatorTestApi> test_api_; | |
| 123 | |
| 124 // Used to control the animations. | |
| 125 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | |
| 126 | |
| 127 private: | |
| 128 std::unique_ptr<base::ThreadTaskRunnerHandle> task_runner_handle_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTest); | |
| 131 }; | |
| 132 | |
| 133 void ShelfBackgroundAnimatorTest::SetUp() { | |
| 134 task_runner_ = new base::TestMockTimeTaskRunner(); | |
| 135 task_runner_handle_.reset(new base::ThreadTaskRunnerHandle(task_runner_)); | |
| 136 | |
| 137 animator_.reset( | |
| 138 new ShelfBackgroundAnimator(SHELF_BACKGROUND_DEFAULT, nullptr, nullptr)); | |
| 139 animator_->AddObserver(&observer_); | |
| 140 | |
| 141 test_api_.reset(new ShelfBackgroundAnimatorTestApi(animator_.get())); | |
| 142 } | |
| 143 | |
| 144 void ShelfBackgroundAnimatorTest::PaintBackground( | |
| 145 ShelfBackgroundType background_type, | |
| 146 AnimationChangeType change_type) { | |
| 147 animator_->PaintBackground(background_type, change_type); | |
| 148 } | |
| 149 | |
| 150 void ShelfBackgroundAnimatorTest::SetColorValuesOnObserver(SkColor color) { | |
| 151 observer_.UpdateShelfBackground(color); | |
| 152 observer_.UpdateShelfItemBackground(color); | |
| 153 } | |
| 154 | |
| 155 void ShelfBackgroundAnimatorTest::CompleteAnimations() { | |
| 156 task_runner_->FastForwardUntilNoTasksRemain(); | |
| 157 } | |
| 158 | |
| 159 // Verify the |previous_background_type_| and |target_background_type_| values | |
| 160 // when animating to the same target type multiple times. | |
| 161 TEST_F(ShelfBackgroundAnimatorTest, BackgroundTypesWhenAnimatingToSameTarget) { | |
| 162 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 163 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
| 164 | |
| 165 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 166 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 167 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); | |
| 168 | |
| 169 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 170 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 171 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); | |
| 172 } | |
| 173 | |
| 174 // Verify subsequent calls to PaintBackground() using the | |
| 175 // AnimationChangeType::ANIMATE change type are ignored. | |
| 176 TEST_F(ShelfBackgroundAnimatorTest, | |
| 177 MultipleAnimateCallsToSameTargetAreIgnored) { | |
| 178 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 179 SetColorValuesOnObserver(kDummyColor); | |
| 180 animator_->PaintBackground(SHELF_BACKGROUND_DEFAULT, | |
| 181 AnimationChangeType::ANIMATE); | |
| 182 CompleteAnimations(); | |
| 183 | |
| 184 EXPECT_NE(observer_.background_color(), kDummyColor); | |
| 185 EXPECT_NE(observer_.item_background_color(), kDummyColor); | |
| 186 | |
| 187 SetColorValuesOnObserver(kDummyColor); | |
| 188 animator_->PaintBackground(SHELF_BACKGROUND_DEFAULT, | |
| 189 AnimationChangeType::ANIMATE); | |
| 190 CompleteAnimations(); | |
| 191 | |
| 192 EXPECT_EQ(observer_.background_color(), kDummyColor); | |
| 193 EXPECT_EQ(observer_.item_background_color(), kDummyColor); | |
| 194 } | |
| 195 | |
| 196 // Verify observers are updated with the current values when they are added. | |
| 197 TEST_F(ShelfBackgroundAnimatorTest, ObserversUpdatedWhenAdded) { | |
| 198 animator_->RemoveObserver(&observer_); | |
| 199 SetColorValuesOnObserver(kDummyColor); | |
| 200 | |
| 201 animator_->AddObserver(&observer_); | |
| 202 | |
| 203 EXPECT_NE(observer_.background_color(), kDummyColor); | |
| 204 EXPECT_NE(observer_.item_background_color(), kDummyColor); | |
| 205 } | |
| 206 | |
| 207 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. | |
| 208 TEST_F(ShelfBackgroundAnimatorTest, DefaultBackground) { | |
| 209 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 210 | |
| 211 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
| 212 EXPECT_EQ(0, observer_.GetBackgroundAlpha()); | |
| 213 EXPECT_EQ(kShelfTranslucentAlpha, observer_.GetItemBackgroundAlpha()); | |
| 214 } | |
| 215 | |
| 216 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. | |
| 217 TEST_F(ShelfBackgroundAnimatorTest, OverlapBackground) { | |
| 218 PaintBackground(SHELF_BACKGROUND_OVERLAP); | |
| 219 | |
| 220 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); | |
| 221 EXPECT_EQ(kShelfTranslucentAlpha, observer_.GetBackgroundAlpha()); | |
| 222 EXPECT_EQ(0, observer_.GetItemBackgroundAlpha()); | |
| 223 } | |
| 224 | |
| 225 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. | |
| 226 TEST_F(ShelfBackgroundAnimatorTest, MaximizedBackground) { | |
| 227 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
| 228 | |
| 229 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
| 230 EXPECT_EQ(kMaxAlpha, observer_.GetBackgroundAlpha()); | |
| 231 EXPECT_EQ(0, observer_.GetItemBackgroundAlpha()); | |
| 232 } | |
| 233 | |
| 234 TEST_F(ShelfBackgroundAnimatorTest, | |
| 235 AnimatorIsDetroyedWhenCompletingSuccessfully) { | |
| 236 PaintBackground(SHELF_BACKGROUND_OVERLAP, AnimationChangeType::ANIMATE); | |
| 237 EXPECT_TRUE(test_api_->animator()); | |
| 238 CompleteAnimations(); | |
| 239 EXPECT_FALSE(test_api_->animator()); | |
| 240 } | |
| 241 | |
| 242 TEST_F(ShelfBackgroundAnimatorTest, | |
| 243 AnimatorDestroyedWhenChangingBackgroundImmediately) { | |
| 244 PaintBackground(SHELF_BACKGROUND_OVERLAP, AnimationChangeType::ANIMATE); | |
| 245 EXPECT_TRUE(test_api_->animator()); | |
| 246 | |
| 247 PaintBackground(SHELF_BACKGROUND_OVERLAP, AnimationChangeType::IMMEDIATE); | |
| 248 EXPECT_FALSE(test_api_->animator()); | |
| 249 } | |
| 250 | |
| 251 // Verify that existing animator is used when animating to the previous state. | |
| 252 TEST_F(ShelfBackgroundAnimatorTest, | |
| 253 ExistingAnimatorIsReusedWhenAnimatingToPreviousState) { | |
| 254 PaintBackground(SHELF_BACKGROUND_DEFAULT, AnimationChangeType::ANIMATE); | |
| 255 PaintBackground(SHELF_BACKGROUND_MAXIMIZED, AnimationChangeType::ANIMATE); | |
| 256 | |
| 257 const gfx::SlideAnimation* animator = test_api_->animator(); | |
| 258 EXPECT_TRUE(animator); | |
| 259 | |
| 260 PaintBackground(SHELF_BACKGROUND_DEFAULT, AnimationChangeType::ANIMATE); | |
| 261 | |
| 262 EXPECT_EQ(animator, test_api_->animator()); | |
| 263 } | |
| 264 | |
| 265 // Verify that existing animator is not re-used when the target background isn't | |
| 266 // the same as the previous background. | |
| 267 TEST_F(ShelfBackgroundAnimatorTest, | |
| 268 ExistingAnimatorNotReusedWhenTargetBackgroundNotPreviousBackground) { | |
| 269 PaintBackground(SHELF_BACKGROUND_DEFAULT, AnimationChangeType::ANIMATE); | |
| 270 PaintBackground(SHELF_BACKGROUND_MAXIMIZED, AnimationChangeType::ANIMATE); | |
| 271 | |
| 272 const gfx::SlideAnimation* animator = test_api_->animator(); | |
| 273 EXPECT_TRUE(animator); | |
| 274 | |
| 275 EXPECT_NE(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type()); | |
| 276 PaintBackground(SHELF_BACKGROUND_OVERLAP, AnimationChangeType::ANIMATE); | |
| 277 | |
| 278 EXPECT_NE(animator, test_api_->animator()); | |
| 279 } | |
| 280 | |
| 281 TEST_F(ShelfBackgroundAnimatorTest, | |
| 282 AnimationProgressesToTargetWhenStoppingUnfinishedAnimator) { | |
| 283 PaintBackground(SHELF_BACKGROUND_OVERLAP, AnimationChangeType::ANIMATE); | |
| 284 | |
| 285 EXPECT_NE(kShelfTranslucentAlpha, observer_.GetBackgroundAlpha()); | |
| 286 EXPECT_NE(0, observer_.GetItemBackgroundAlpha()); | |
| 287 | |
| 288 test_api_->animator()->Stop(); | |
| 289 | |
| 290 EXPECT_EQ(kShelfTranslucentAlpha, observer_.GetBackgroundAlpha()); | |
| 291 EXPECT_EQ(0, observer_.GetItemBackgroundAlpha()); | |
| 292 } | |
| 293 | |
| 294 // Verify observers are always notified, even when alpha values don't change. | |
| 295 TEST_F(ShelfBackgroundAnimatorTest, | |
| 296 ObserversAreNotifiedWhenSnappingToSameTargetBackground) { | |
| 297 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 298 SetColorValuesOnObserver(kDummyColor); | |
| 299 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
| 300 | |
| 301 EXPECT_NE(observer_.background_color(), kDummyColor); | |
| 302 EXPECT_NE(observer_.item_background_color(), kDummyColor); | |
| 303 } | |
| 304 | |
| 305 } // namespace ash | |
| OLD | NEW |