Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(691)

Side by Side Diff: ash/common/shelf/shelf_background_animator_unittest.cc

Issue 2053113002: Replaced BackgroundAnimator with ShelfBackgroundAnimator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Made the ShelfView add/remove itself as an observer from the ShelfBackgroundAnimator. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 namespace test {
James Cook 2016/07/27 17:33:10 nit: Remove. Generally we only do this for test su
bruthig 2016/07/27 18:09:00 Done.
70
71 // Provides internal access to a ShelfBackgroundAnimator instance.
72 class ShelfBackgroundAnimatorTestApi {
73 public:
74 explicit ShelfBackgroundAnimatorTestApi(ShelfBackgroundAnimator* animator)
75 : animator_(animator) {}
76
77 ~ShelfBackgroundAnimatorTestApi() {}
78
79 ShelfBackgroundType previous_background_type() const {
80 return animator_->previous_background_type_;
81 }
82
83 BackgroundAnimator* opaque_background_animator() const {
84 return animator_->opaque_background_animator_.get();
85 }
86
87 BackgroundAnimator* asset_background_animator() const {
88 return animator_->asset_background_animator_.get();
89 }
90
91 BackgroundAnimator* item_background_animator() const {
92 return animator_->item_background_animator_.get();
93 }
94
95 bool can_reuse_animators() const { return animator_->can_reuse_animators_; }
96
97 private:
98 // The instance to provide internal access to.
99 ShelfBackgroundAnimator* animator_;
100
101 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestApi);
102 };
103
104 } // namespace test
105
106 // Note: this is not a parameterized test (like other MD tests) because the
107 // behavior is so different and not all tests need to be run for both MD and
108 // non-MD variants.
109 class ShelfBackgroundAnimatorTestBase : public testing::Test {
110 public:
111 ShelfBackgroundAnimatorTestBase() {}
112 ~ShelfBackgroundAnimatorTestBase() override {}
113
114 virtual MaterialDesignController::Mode GetMaterialDesignMode() = 0;
115
116 int expected_translucent_alpha() const { return expected_translucent_alpha_; }
117
118 // testing::Test:
119 void SetUp() override;
120
121 protected:
122 // Convenience wrapper for |animator_|'s PaintBackground() that always uses
123 // BACKGROUND_CHANGE_IMMEDIATE.
124 void PaintBackground(ShelfBackgroundType background_type);
125
126 // Set all of the alpha values for the |observer_|.
127 void SetAlphaValuesOnObserver(int alpha);
128
129 // Completes all the animations.
130 void CompleteAnimations();
131
132 TestShelfBackgroundObserver observer_;
133
134 // Test target.
135 std::unique_ptr<ShelfBackgroundAnimator> animator_;
136
137 // Provides internal access to |animator_|.
138 std::unique_ptr<test::ShelfBackgroundAnimatorTestApi> test_api_;
139
140 // Used to control the animations.
141 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
142
143 private:
144 std::unique_ptr<base::ThreadTaskRunnerHandle> task_runner_handle_;
145
146 std::unique_ptr<MaterialDesignControllerTestAPI> material_mode_test_api_;
147
148 // The expected alpha value for translucent items. Cannot be a constant
149 // because it is different for material design and non-material.
150 int expected_translucent_alpha_ = 0;
151
152 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestBase);
153 };
154
155 void ShelfBackgroundAnimatorTestBase::SetUp() {
156 task_runner_ = new base::TestMockTimeTaskRunner();
157 task_runner_handle_.reset(new base::ThreadTaskRunnerHandle(task_runner_));
158
159 material_mode_test_api_.reset(
160 new MaterialDesignControllerTestAPI(GetMaterialDesignMode()));
161 animator_.reset(
162 new ShelfBackgroundAnimator(SHELF_BACKGROUND_DEFAULT, nullptr));
163 animator_->AddObserver(&observer_);
164
165 test_api_.reset(new test::ShelfBackgroundAnimatorTestApi(animator_.get()));
166
167 // Initialized after the Material Design mode because GetShelfConstant()
168 // depends on the mode.
169 expected_translucent_alpha_ = GetShelfConstant(SHELF_BACKGROUND_ALPHA);
170 }
171
172 void ShelfBackgroundAnimatorTestBase::PaintBackground(
173 ShelfBackgroundType background_type) {
174 animator_->PaintBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE);
175 }
176
177 void ShelfBackgroundAnimatorTestBase::SetAlphaValuesOnObserver(int alpha) {
178 observer_.UpdateShelfOpaqueBackground(alpha);
179 observer_.UpdateShelfAssetBackground(alpha);
180 observer_.UpdateShelfItemBackground(alpha);
181 }
182
183 void ShelfBackgroundAnimatorTestBase::CompleteAnimations() {
184 task_runner_->FastForwardUntilNoTasksRemain();
185 }
186
187 class ShelfBackgroundAnimatorNonMDTest
188 : public ShelfBackgroundAnimatorTestBase {
189 public:
190 ShelfBackgroundAnimatorNonMDTest() {}
191 ~ShelfBackgroundAnimatorNonMDTest() override {}
192
193 MaterialDesignController::Mode GetMaterialDesignMode() override {
194 return MaterialDesignController::NON_MATERIAL;
195 }
196
197 private:
198 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorNonMDTest);
199 };
200
201 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state.
202 TEST_F(ShelfBackgroundAnimatorNonMDTest, DefaultBackground) {
203 PaintBackground(SHELF_BACKGROUND_DEFAULT);
204
205 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type());
206 EXPECT_EQ(0, observer_.opaque_background_alpha());
207 EXPECT_EQ(0, observer_.asset_background_alpha());
208 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha());
209 }
210
211 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state.
212 TEST_F(ShelfBackgroundAnimatorNonMDTest, OverlapBackground) {
213 PaintBackground(SHELF_BACKGROUND_OVERLAP);
214
215 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type());
216 EXPECT_EQ(0, observer_.opaque_background_alpha());
217 EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha());
218 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha());
219 }
220
221 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state.
222 TEST_F(ShelfBackgroundAnimatorNonMDTest, MaximizedBackground) {
223 PaintBackground(SHELF_BACKGROUND_MAXIMIZED);
224
225 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type());
226 EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha());
227 EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha());
228 EXPECT_EQ(kMaxAlpha, observer_.item_background_alpha());
229 }
230
231 class ShelfBackgroundAnimatorMDTest : public ShelfBackgroundAnimatorTestBase {
232 public:
233 ShelfBackgroundAnimatorMDTest() {}
234 ~ShelfBackgroundAnimatorMDTest() override {}
235
236 MaterialDesignController::Mode GetMaterialDesignMode() override {
237 return MaterialDesignController::MATERIAL_EXPERIMENTAL;
238 }
239
240 private:
241 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorMDTest);
242 };
243
244 // Verify the |previous_background_type_| and |target_background_type_| values
245 // when animating to the same target type multiple times.
246 TEST_F(ShelfBackgroundAnimatorMDTest,
247 BackgroundTypesWhenAnimatingToSameTarget) {
248 PaintBackground(SHELF_BACKGROUND_MAXIMIZED);
249 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_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 PaintBackground(SHELF_BACKGROUND_DEFAULT);
256 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type());
257 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type());
258 }
259
260 // Verify subsequent calls to PaintBackground() using the
261 // BACKGROUND_CHANGE_ANIMATE change type are ignored.
262 TEST_F(ShelfBackgroundAnimatorMDTest,
263 MultipleAnimateCallsToSameTargetAreIgnored) {
264 PaintBackground(SHELF_BACKGROUND_MAXIMIZED);
265 SetAlphaValuesOnObserver(kDummyAlpha);
266 animator_->PaintBackground(SHELF_BACKGROUND_DEFAULT,
267 BACKGROUND_CHANGE_ANIMATE);
268 CompleteAnimations();
269
270 EXPECT_NE(observer_.opaque_background_alpha(), kDummyAlpha);
271 EXPECT_NE(observer_.item_background_alpha(), kDummyAlpha);
272
273 SetAlphaValuesOnObserver(kDummyAlpha);
274 animator_->PaintBackground(SHELF_BACKGROUND_DEFAULT,
275 BACKGROUND_CHANGE_ANIMATE);
276 CompleteAnimations();
277
278 EXPECT_EQ(observer_.opaque_background_alpha(), kDummyAlpha);
279 EXPECT_EQ(observer_.item_background_alpha(), kDummyAlpha);
280 }
281
282 // Verify observers are updated with the current values when they are added.
283 TEST_F(ShelfBackgroundAnimatorMDTest, ObserversUpdatedWhenAdded) {
284 animator_->RemoveObserver(&observer_);
285 SetAlphaValuesOnObserver(kDummyAlpha);
286
287 animator_->AddObserver(&observer_);
288
289 EXPECT_NE(observer_.opaque_background_alpha(), kDummyAlpha);
290 EXPECT_NE(observer_.asset_background_alpha(), kDummyAlpha);
291 EXPECT_NE(observer_.item_background_alpha(), kDummyAlpha);
292 }
293
294 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state.
295 TEST_F(ShelfBackgroundAnimatorMDTest, DefaultBackground) {
296 PaintBackground(SHELF_BACKGROUND_DEFAULT);
297
298 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type());
299 EXPECT_EQ(0, observer_.opaque_background_alpha());
300 EXPECT_EQ(0, observer_.asset_background_alpha());
301 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha());
302 }
303
304 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state.
305 TEST_F(ShelfBackgroundAnimatorMDTest, OverlapBackground) {
306 PaintBackground(SHELF_BACKGROUND_OVERLAP);
307
308 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type());
309 EXPECT_EQ(expected_translucent_alpha(), observer_.opaque_background_alpha());
310 EXPECT_EQ(0, observer_.asset_background_alpha());
311 EXPECT_EQ(0, observer_.item_background_alpha());
312 }
313
314 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state.
315 TEST_F(ShelfBackgroundAnimatorMDTest, MaximizedBackground) {
316 PaintBackground(SHELF_BACKGROUND_MAXIMIZED);
317
318 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type());
319 EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha());
320 EXPECT_EQ(0, observer_.asset_background_alpha());
321 EXPECT_EQ(0, observer_.item_background_alpha());
322 }
323
324 // Verify that existing animators are used when animating to the previous state.
325 TEST_F(ShelfBackgroundAnimatorMDTest, ExistingAnimatorsAreReused) {
326 PaintBackground(SHELF_BACKGROUND_DEFAULT);
327 PaintBackground(SHELF_BACKGROUND_MAXIMIZED);
328 EXPECT_TRUE(test_api_->can_reuse_animators());
329
330 const BackgroundAnimator* opaque_animator =
331 test_api_->opaque_background_animator();
332 const BackgroundAnimator* asset_animator =
333 test_api_->asset_background_animator();
334 const BackgroundAnimator* item_animator =
335 test_api_->item_background_animator();
336
337 PaintBackground(SHELF_BACKGROUND_DEFAULT);
338
339 EXPECT_EQ(opaque_animator, test_api_->opaque_background_animator());
340 EXPECT_EQ(asset_animator, test_api_->asset_background_animator());
341 EXPECT_EQ(item_animator, test_api_->item_background_animator());
342 }
343
344 // Verify that existing animators are not re-used when the previous animation
345 // didn't complete.
346 TEST_F(ShelfBackgroundAnimatorMDTest,
347 ExistingAnimatorsNotReusedWhenPreviousAnimationsDontComplete) {
348 EXPECT_NE(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type());
349 animator_->PaintBackground(SHELF_BACKGROUND_OVERLAP,
350 BACKGROUND_CHANGE_ANIMATE);
351 animator_->PaintBackground(SHELF_BACKGROUND_MAXIMIZED,
352 BACKGROUND_CHANGE_ANIMATE);
353 EXPECT_FALSE(test_api_->can_reuse_animators());
354
355 const BackgroundAnimator* opaque_animator =
356 test_api_->opaque_background_animator();
357 const BackgroundAnimator* asset_animator =
358 test_api_->asset_background_animator();
359 const BackgroundAnimator* item_animator =
360 test_api_->item_background_animator();
361
362 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type());
363 animator_->PaintBackground(SHELF_BACKGROUND_OVERLAP,
364 BACKGROUND_CHANGE_ANIMATE);
365
366 EXPECT_NE(opaque_animator, test_api_->opaque_background_animator());
367 EXPECT_NE(asset_animator, test_api_->asset_background_animator());
368 EXPECT_NE(item_animator, test_api_->item_background_animator());
369 }
370
371 // Verify that existing animators are not re-used when the target background
372 // isn't the same as the previous background.
373 TEST_F(ShelfBackgroundAnimatorMDTest,
374 ExistingAnimatorsNotReusedWhenTargetBackgroundNotPreviousBackground) {
375 PaintBackground(SHELF_BACKGROUND_DEFAULT);
376 PaintBackground(SHELF_BACKGROUND_MAXIMIZED);
377 EXPECT_TRUE(test_api_->can_reuse_animators());
378
379 const BackgroundAnimator* opaque_animator =
380 test_api_->opaque_background_animator();
381 const BackgroundAnimator* asset_animator =
382 test_api_->asset_background_animator();
383 const BackgroundAnimator* item_animator =
384 test_api_->item_background_animator();
385
386 EXPECT_NE(SHELF_BACKGROUND_OVERLAP, test_api_->previous_background_type());
387 PaintBackground(SHELF_BACKGROUND_OVERLAP);
388
389 EXPECT_NE(opaque_animator, test_api_->opaque_background_animator());
390 EXPECT_NE(asset_animator, test_api_->asset_background_animator());
391 EXPECT_NE(item_animator, test_api_->item_background_animator());
392 }
393
394 // Verify animators are not re-used after snapping to the same background type.
395 TEST_F(ShelfBackgroundAnimatorMDTest,
396 ExistingAnimatorsNotUsedWhenSnappingToSameTargetBackground) {
397 PaintBackground(SHELF_BACKGROUND_DEFAULT);
398 PaintBackground(SHELF_BACKGROUND_DEFAULT);
399
400 EXPECT_FALSE(test_api_->can_reuse_animators());
401 }
402
403 // Verify observers are always notified, even when alpha values don't change.
404 TEST_F(ShelfBackgroundAnimatorMDTest,
405 ObserversAreNotifiedWhenSnappingToSameTargetBackground) {
406 PaintBackground(SHELF_BACKGROUND_DEFAULT);
407 SetAlphaValuesOnObserver(kDummyAlpha);
408 PaintBackground(SHELF_BACKGROUND_DEFAULT);
409
410 EXPECT_NE(observer_.opaque_background_alpha(), kDummyAlpha);
411 EXPECT_NE(observer_.asset_background_alpha(), kDummyAlpha);
412 EXPECT_NE(observer_.item_background_alpha(), kDummyAlpha);
413 }
414
415 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698