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