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

Side by Side Diff: cc/input/scrollbar_animation_controller_thinning_unittest.cc

Issue 2692243005: Merge Compositor's ScrollbarAnimationControllers into single class (Closed)
Patch Set: fix confict constant Created 3 years, 10 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 2013 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 "cc/input/scrollbar_animation_controller_thinning.h"
6
7 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
8 #include "cc/test/fake_impl_task_runner_provider.h"
9 #include "cc/test/fake_layer_tree_host_impl.h"
10 #include "cc/test/geometry_test_utils.h"
11 #include "cc/test/test_task_graph_runner.h"
12 #include "cc/trees/layer_tree_impl.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using testing::AtLeast;
17 using testing::Mock;
18 using testing::NiceMock;
19 using testing::_;
20
21 namespace cc {
22 namespace {
23
24 // These constants are hard-coded and should match the values in
25 // scrollbar_animation_controller_thinning.cc.
26 const float kIdleThicknessScale = 0.4f;
27 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f;
28
29 class MockScrollbarAnimationControllerClient
30 : public ScrollbarAnimationControllerClient {
31 public:
32 explicit MockScrollbarAnimationControllerClient(LayerTreeHostImpl* host_impl)
33 : host_impl_(host_impl) {}
34 virtual ~MockScrollbarAnimationControllerClient() {}
35
36 void PostDelayedScrollbarAnimationTask(const base::Closure& start_fade,
37 base::TimeDelta delay) override {
38 start_fade_ = start_fade;
39 delay_ = delay;
40 }
41 void SetNeedsRedrawForScrollbarAnimation() override {}
42 void SetNeedsAnimateForScrollbarAnimation() override {}
43 ScrollbarSet ScrollbarsFor(int scroll_layer_id) const override {
44 return host_impl_->ScrollbarsFor(scroll_layer_id);
45 }
46 MOCK_METHOD0(DidChangeScrollbarVisibility, void());
47
48 base::Closure& start_fade() { return start_fade_; }
49 base::TimeDelta& delay() { return delay_; }
50
51 private:
52 base::Closure start_fade_;
53 base::TimeDelta delay_;
54 LayerTreeHostImpl* host_impl_;
55 };
56
57 class ScrollbarAnimationControllerThinningTest : public testing::Test {
58 public:
59 ScrollbarAnimationControllerThinningTest()
60 : host_impl_(&task_runner_provider_, &task_graph_runner_),
61 client_(&host_impl_) {}
62
63 void ExpectScrollbarsOpacity(float opacity) {
64 EXPECT_FLOAT_EQ(opacity, v_scrollbar_layer_->Opacity());
65 EXPECT_FLOAT_EQ(opacity, h_scrollbar_layer_->Opacity());
66 }
67
68 protected:
69 const base::TimeDelta kDelayBeforeStarting = base::TimeDelta::FromSeconds(2);
70 const base::TimeDelta kResizeDelayBeforeStarting =
71 base::TimeDelta::FromSeconds(5);
72 const base::TimeDelta kFadeDuration = base::TimeDelta::FromSeconds(3);
73 const base::TimeDelta kThinningDuration = base::TimeDelta::FromSeconds(2);
74
75 void SetUp() override {
76 std::unique_ptr<LayerImpl> scroll_layer =
77 LayerImpl::Create(host_impl_.active_tree(), 1);
78 std::unique_ptr<LayerImpl> clip =
79 LayerImpl::Create(host_impl_.active_tree(), 2);
80 clip_layer_ = clip.get();
81 scroll_layer->SetScrollClipLayer(clip_layer_->id());
82 LayerImpl* scroll_layer_ptr = scroll_layer.get();
83
84 const int kThumbThickness = 10;
85 const int kTrackStart = 0;
86 const bool kIsLeftSideVerticalScrollbar = false;
87 const bool kIsOverlayScrollbar = true;
88
89 std::unique_ptr<SolidColorScrollbarLayerImpl> h_scrollbar =
90 SolidColorScrollbarLayerImpl::Create(
91 host_impl_.active_tree(), 3, HORIZONTAL, kThumbThickness,
92 kTrackStart, kIsLeftSideVerticalScrollbar, kIsOverlayScrollbar);
93 std::unique_ptr<SolidColorScrollbarLayerImpl> v_scrollbar =
94 SolidColorScrollbarLayerImpl::Create(
95 host_impl_.active_tree(), 4, VERTICAL, kThumbThickness, kTrackStart,
96 kIsLeftSideVerticalScrollbar, kIsOverlayScrollbar);
97 v_scrollbar_layer_ = v_scrollbar.get();
98 h_scrollbar_layer_ = h_scrollbar.get();
99
100 scroll_layer->test_properties()->AddChild(std::move(v_scrollbar));
101 scroll_layer->test_properties()->AddChild(std::move(h_scrollbar));
102 clip_layer_->test_properties()->AddChild(std::move(scroll_layer));
103 host_impl_.active_tree()->SetRootLayerForTesting(std::move(clip));
104
105 v_scrollbar_layer_->SetScrollLayerId(scroll_layer_ptr->id());
106 h_scrollbar_layer_->SetScrollLayerId(scroll_layer_ptr->id());
107 v_scrollbar_layer_->test_properties()->opacity_can_animate = true;
108 h_scrollbar_layer_->test_properties()->opacity_can_animate = true;
109 clip_layer_->SetBounds(gfx::Size(100, 100));
110 scroll_layer_ptr->SetBounds(gfx::Size(200, 200));
111 host_impl_.active_tree()->BuildLayerListAndPropertyTreesForTesting();
112
113 scrollbar_controller_ = ScrollbarAnimationControllerThinning::Create(
114 scroll_layer_ptr->id(), &client_, kDelayBeforeStarting,
115 kResizeDelayBeforeStarting, kFadeDuration, kThinningDuration);
116 }
117
118 FakeImplTaskRunnerProvider task_runner_provider_;
119 TestTaskGraphRunner task_graph_runner_;
120 FakeLayerTreeHostImpl host_impl_;
121 std::unique_ptr<ScrollbarAnimationControllerThinning> scrollbar_controller_;
122 LayerImpl* clip_layer_;
123 SolidColorScrollbarLayerImpl* v_scrollbar_layer_;
124 SolidColorScrollbarLayerImpl* h_scrollbar_layer_;
125 NiceMock<MockScrollbarAnimationControllerClient> client_;
126 };
127
128 // Check initialization of scrollbar. Should start off invisible and thin.
129 TEST_F(ScrollbarAnimationControllerThinningTest, Idle) {
130 ExpectScrollbarsOpacity(0);
131 EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
132 EXPECT_FLOAT_EQ(kIdleThicknessScale,
133 v_scrollbar_layer_->thumb_thickness_scale_factor());
134 EXPECT_FLOAT_EQ(kIdleThicknessScale,
135 v_scrollbar_layer_->thumb_thickness_scale_factor());
136 }
137
138 // Check that scrollbar appears again when the layer becomes scrollable.
139 TEST_F(ScrollbarAnimationControllerThinningTest, AppearOnResize) {
140 base::TimeTicks time;
141 time += base::TimeDelta::FromSeconds(1);
142
143 scrollbar_controller_->DidScrollBegin();
144 scrollbar_controller_->DidScrollUpdate(false);
145 scrollbar_controller_->DidScrollEnd();
146 ExpectScrollbarsOpacity(1);
147
148 // Make the Layer non-scrollable, scrollbar disappears.
149 clip_layer_->SetBounds(gfx::Size(200, 200));
150 scrollbar_controller_->DidScrollUpdate(false);
151 ExpectScrollbarsOpacity(0);
152
153 // Make the layer scrollable, scrollbar appears again.
154 clip_layer_->SetBounds(gfx::Size(100, 100));
155 scrollbar_controller_->DidScrollUpdate(false);
156 ExpectScrollbarsOpacity(1);
157 }
158
159 // Check that scrollbar disappears when the layer becomes non-scrollable.
160 TEST_F(ScrollbarAnimationControllerThinningTest, HideOnResize) {
161 base::TimeTicks time;
162 time += base::TimeDelta::FromSeconds(1);
163
164 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
165 ASSERT_TRUE(scroll_layer);
166 EXPECT_EQ(gfx::Size(200, 200), scroll_layer->bounds());
167
168 // Shrink along X axis, horizontal scrollbar should appear.
169 clip_layer_->SetBounds(gfx::Size(100, 200));
170 EXPECT_EQ(gfx::Size(100, 200), clip_layer_->bounds());
171
172 scrollbar_controller_->DidScrollBegin();
173
174 scrollbar_controller_->DidScrollUpdate(false);
175 EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->Opacity());
176
177 scrollbar_controller_->DidScrollEnd();
178
179 // Shrink along Y axis and expand along X, horizontal scrollbar
180 // should disappear.
181 clip_layer_->SetBounds(gfx::Size(200, 100));
182 EXPECT_EQ(gfx::Size(200, 100), clip_layer_->bounds());
183
184 scrollbar_controller_->DidScrollBegin();
185
186 scrollbar_controller_->DidScrollUpdate(false);
187 EXPECT_FLOAT_EQ(0.0f, h_scrollbar_layer_->Opacity());
188
189 scrollbar_controller_->DidScrollEnd();
190 }
191
192 // Scroll content. Confirm the scrollbar appears and fades out.
193 TEST_F(ScrollbarAnimationControllerThinningTest, BasicAppearAndFadeOut) {
194 base::TimeTicks time;
195 time += base::TimeDelta::FromSeconds(1);
196
197 // Scrollbar should be invisible.
198 ExpectScrollbarsOpacity(0);
199 EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
200
201 // Scrollbar should appear only on scroll update.
202 scrollbar_controller_->DidScrollBegin();
203 ExpectScrollbarsOpacity(0);
204 EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
205
206 scrollbar_controller_->DidScrollUpdate(false);
207 ExpectScrollbarsOpacity(1);
208 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
209
210 scrollbar_controller_->DidScrollEnd();
211 ExpectScrollbarsOpacity(1);
212 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
213
214 // An animation should have been enqueued.
215 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
216 EXPECT_FALSE(client_.start_fade().is_null());
217 client_.start_fade().Run();
218
219 // Scrollbar should fade out over kFadeDuration.
220 scrollbar_controller_->Animate(time);
221 time += kFadeDuration;
222 scrollbar_controller_->Animate(time);
223
224 ExpectScrollbarsOpacity(0);
225 EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
226 }
227
228 // Scroll content. Move the mouse near the scrollbar and confirm it becomes
229 // thick. Ensure it remains visible as long as the mouse is near the scrollbar.
230 TEST_F(ScrollbarAnimationControllerThinningTest, MoveNearAndDontFadeOut) {
231 base::TimeTicks time;
232 time += base::TimeDelta::FromSeconds(1);
233
234 scrollbar_controller_->DidScrollBegin();
235 scrollbar_controller_->DidScrollUpdate(false);
236 scrollbar_controller_->DidScrollEnd();
237
238 // An animation should have been enqueued.
239 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
240 EXPECT_FALSE(client_.start_fade().is_null());
241 EXPECT_FALSE(client_.start_fade().IsCancelled());
242
243 // Now move the mouse near the scrollbar. This should cancel the currently
244 // queued fading animation and start animating thickness.
245 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
246 ExpectScrollbarsOpacity(1);
247 EXPECT_FLOAT_EQ(kIdleThicknessScale,
248 v_scrollbar_layer_->thumb_thickness_scale_factor());
249 EXPECT_FLOAT_EQ(kIdleThicknessScale,
250 h_scrollbar_layer_->thumb_thickness_scale_factor());
251 EXPECT_TRUE(client_.start_fade().IsCancelled());
252
253 // Vertical scrollbar should become thick.
254 scrollbar_controller_->Animate(time);
255 time += kThinningDuration;
256 scrollbar_controller_->Animate(time);
257 ExpectScrollbarsOpacity(1);
258 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
259 EXPECT_FLOAT_EQ(kIdleThicknessScale,
260 h_scrollbar_layer_->thumb_thickness_scale_factor());
261
262 // Mouse is still near the Scrollbar. Once the thickness animation is
263 // complete, the queued delayed fade animation should be either cancelled or
264 // null.
265 EXPECT_TRUE(client_.start_fade().is_null() ||
266 client_.start_fade().IsCancelled());
267 }
268
269 // Scroll content. Move the mouse over the scrollbar and confirm it becomes
270 // thick. Ensure it remains visible as long as the mouse is over the scrollbar.
271 TEST_F(ScrollbarAnimationControllerThinningTest, MoveOverAndDontFadeOut) {
272 base::TimeTicks time;
273 time += base::TimeDelta::FromSeconds(1);
274
275 scrollbar_controller_->DidScrollBegin();
276 scrollbar_controller_->DidScrollUpdate(false);
277 scrollbar_controller_->DidScrollEnd();
278
279 // An animation should have been enqueued.
280 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
281 EXPECT_FALSE(client_.start_fade().is_null());
282 EXPECT_FALSE(client_.start_fade().IsCancelled());
283
284 // Now move the mouse over the scrollbar. This should cancel the currently
285 // queued fading animation and start animating thickness.
286 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
287 ExpectScrollbarsOpacity(1);
288 EXPECT_FLOAT_EQ(kIdleThicknessScale,
289 v_scrollbar_layer_->thumb_thickness_scale_factor());
290 EXPECT_FLOAT_EQ(kIdleThicknessScale,
291 h_scrollbar_layer_->thumb_thickness_scale_factor());
292 EXPECT_TRUE(client_.start_fade().IsCancelled());
293
294 // Vertical scrollbar should become thick.
295 scrollbar_controller_->Animate(time);
296 time += kThinningDuration;
297 scrollbar_controller_->Animate(time);
298 ExpectScrollbarsOpacity(1);
299 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
300 EXPECT_FLOAT_EQ(kIdleThicknessScale,
301 h_scrollbar_layer_->thumb_thickness_scale_factor());
302
303 // Mouse is still over the Scrollbar. Once the thickness animation is
304 // complete, the queued delayed fade animation should be either cancelled or
305 // null.
306 EXPECT_TRUE(client_.start_fade().is_null() ||
307 client_.start_fade().IsCancelled());
308 }
309
310 // Make sure a scrollbar captured before the thickening animation doesn't try
311 // to fade out.
312 TEST_F(ScrollbarAnimationControllerThinningTest,
313 DontFadeWhileCapturedBeforeThick) {
314 base::TimeTicks time;
315 time += base::TimeDelta::FromSeconds(1);
316
317 scrollbar_controller_->DidScrollBegin();
318 scrollbar_controller_->DidScrollUpdate(false);
319 scrollbar_controller_->DidScrollEnd();
320
321 // An animation should have been enqueued.
322 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
323 EXPECT_FALSE(client_.start_fade().is_null());
324
325 // Now move the mouse over the scrollbar and capture it. It should become
326 // thick without need for an animation.
327 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
328 scrollbar_controller_->DidMouseDown();
329 ExpectScrollbarsOpacity(1);
330 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
331 EXPECT_FLOAT_EQ(kIdleThicknessScale,
332 h_scrollbar_layer_->thumb_thickness_scale_factor());
333
334 // The fade animation should have been cleared or cancelled.
335 EXPECT_TRUE(client_.start_fade().is_null() ||
336 client_.start_fade().IsCancelled());
337 }
338
339 // Make sure a scrollbar captured then move mouse away doesn't try to fade out.
340 TEST_F(ScrollbarAnimationControllerThinningTest,
341 DontFadeWhileCapturedThenAway) {
342 base::TimeTicks time;
343 time += base::TimeDelta::FromSeconds(1);
344
345 scrollbar_controller_->DidScrollBegin();
346 scrollbar_controller_->DidScrollUpdate(false);
347 scrollbar_controller_->DidScrollEnd();
348
349 // An animation should have been enqueued.
350 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
351 EXPECT_FALSE(client_.start_fade().is_null());
352
353 // Now move the mouse over the scrollbar and capture it. It should become
354 // thick without need for an animation.
355 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
356 scrollbar_controller_->DidMouseDown();
357 ExpectScrollbarsOpacity(1);
358 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
359 EXPECT_FLOAT_EQ(kIdleThicknessScale,
360 h_scrollbar_layer_->thumb_thickness_scale_factor());
361
362 // The fade animation should have been cleared or cancelled.
363 EXPECT_TRUE(client_.start_fade().is_null() ||
364 client_.start_fade().IsCancelled());
365
366 // Then move mouse away, The fade animation should have been cleared or
367 // cancelled.
368 scrollbar_controller_->DidMouseMoveNear(
369 VERTICAL, kDefaultMouseMoveDistanceToTriggerAnimation);
370
371 EXPECT_TRUE(client_.start_fade().is_null() ||
372 client_.start_fade().IsCancelled());
373 }
374
375 // Make sure a scrollbar captured after a thickening animation doesn't try to
376 // fade out.
377 TEST_F(ScrollbarAnimationControllerThinningTest, DontFadeWhileCaptured) {
378 base::TimeTicks time;
379 time += base::TimeDelta::FromSeconds(1);
380
381 scrollbar_controller_->DidScrollBegin();
382 scrollbar_controller_->DidScrollUpdate(false);
383 scrollbar_controller_->DidScrollEnd();
384
385 // An animation should have been enqueued.
386 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
387 EXPECT_FALSE(client_.start_fade().is_null());
388 EXPECT_FALSE(client_.start_fade().IsCancelled());
389
390 // Now move the mouse over the scrollbar and animate it until it's thick.
391 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
392 scrollbar_controller_->Animate(time);
393 time += kThinningDuration;
394 scrollbar_controller_->Animate(time);
395 ExpectScrollbarsOpacity(1);
396 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
397 EXPECT_FLOAT_EQ(kIdleThicknessScale,
398 h_scrollbar_layer_->thumb_thickness_scale_factor());
399
400 // Since the mouse is over the scrollbar, it should either clear or cancel the
401 // queued fade.
402 EXPECT_TRUE(client_.start_fade().is_null() ||
403 client_.start_fade().IsCancelled());
404
405 // Make sure the queued fade animation is still null or cancelled after
406 // capturing the scrollbar.
407 scrollbar_controller_->DidMouseDown();
408 EXPECT_TRUE(client_.start_fade().is_null() ||
409 client_.start_fade().IsCancelled());
410 }
411
412 // Make sure releasing a captured scrollbar when the mouse isn't near it, causes
413 // the scrollbar to fade out.
414 TEST_F(ScrollbarAnimationControllerThinningTest, FadeAfterReleasedFar) {
415 base::TimeTicks time;
416 time += base::TimeDelta::FromSeconds(1);
417
418 scrollbar_controller_->DidScrollBegin();
419 scrollbar_controller_->DidScrollUpdate(false);
420 scrollbar_controller_->DidScrollEnd();
421
422 // An animation should have been enqueued.
423 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
424 EXPECT_FALSE(client_.start_fade().is_null());
425 EXPECT_FALSE(client_.start_fade().IsCancelled());
426
427 // Now move the mouse over the scrollbar and capture it.
428 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
429 scrollbar_controller_->DidMouseDown();
430 ExpectScrollbarsOpacity(1);
431 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
432 EXPECT_FLOAT_EQ(kIdleThicknessScale,
433 h_scrollbar_layer_->thumb_thickness_scale_factor());
434
435 // Since the mouse is still near the scrollbar, the queued fade should be
436 // either null or cancelled.
437 EXPECT_TRUE(client_.start_fade().is_null() ||
438 client_.start_fade().IsCancelled());
439
440 // Now move the mouse away from the scrollbar and release it.
441 scrollbar_controller_->DidMouseMoveNear(
442 VERTICAL, kDefaultMouseMoveDistanceToTriggerAnimation);
443 scrollbar_controller_->DidMouseUp();
444
445 scrollbar_controller_->Animate(time);
446 ExpectScrollbarsOpacity(1);
447 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
448 EXPECT_FLOAT_EQ(kIdleThicknessScale,
449 h_scrollbar_layer_->thumb_thickness_scale_factor());
450 time += kThinningDuration;
451 scrollbar_controller_->Animate(time);
452 ExpectScrollbarsOpacity(1);
453 EXPECT_FLOAT_EQ(kIdleThicknessScale,
454 v_scrollbar_layer_->thumb_thickness_scale_factor());
455 EXPECT_FLOAT_EQ(kIdleThicknessScale,
456 h_scrollbar_layer_->thumb_thickness_scale_factor());
457
458 // The thickness animation is complete, a fade out must be queued.
459 EXPECT_FALSE(client_.start_fade().is_null());
460 EXPECT_FALSE(client_.start_fade().IsCancelled());
461 }
462
463 // Make sure releasing a captured scrollbar when the mouse is near/over it,
464 // doesn't cause the scrollbar to fade out.
465 TEST_F(ScrollbarAnimationControllerThinningTest, DontFadeAfterReleasedNear) {
466 base::TimeTicks time;
467 time += base::TimeDelta::FromSeconds(1);
468
469 scrollbar_controller_->DidScrollBegin();
470 scrollbar_controller_->DidScrollUpdate(false);
471 scrollbar_controller_->DidScrollEnd();
472
473 // An animation should have been enqueued.
474 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
475 EXPECT_FALSE(client_.start_fade().is_null());
476 EXPECT_FALSE(client_.start_fade().IsCancelled());
477
478 // Now move the mouse over the scrollbar and capture it.
479 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
480 scrollbar_controller_->DidMouseDown();
481 ExpectScrollbarsOpacity(1);
482 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
483 EXPECT_FLOAT_EQ(kIdleThicknessScale,
484 h_scrollbar_layer_->thumb_thickness_scale_factor());
485
486 // Since the mouse is over the scrollbar, the queued fade must be either
487 // null or cancelled.
488 EXPECT_TRUE(client_.start_fade().is_null() ||
489 client_.start_fade().IsCancelled());
490
491 // Mouse is still near the scrollbar, releasing it shouldn't do anything.
492 scrollbar_controller_->DidMouseUp();
493 EXPECT_TRUE(client_.start_fade().is_null() ||
494 client_.start_fade().IsCancelled());
495 ExpectScrollbarsOpacity(1);
496 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
497 EXPECT_FLOAT_EQ(kIdleThicknessScale,
498 h_scrollbar_layer_->thumb_thickness_scale_factor());
499 }
500
501 // Make sure moving near a scrollbar while it's fading out causes it to reset
502 // the opacity and thicken.
503 TEST_F(ScrollbarAnimationControllerThinningTest, MoveNearScrollbarWhileFading) {
504 base::TimeTicks time;
505 time += base::TimeDelta::FromSeconds(1);
506
507 scrollbar_controller_->DidScrollBegin();
508 scrollbar_controller_->DidScrollUpdate(false);
509 scrollbar_controller_->DidScrollEnd();
510
511 // A fade animation should have been enqueued. Start it.
512 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
513 EXPECT_FALSE(client_.start_fade().is_null());
514 client_.start_fade().Run();
515
516 scrollbar_controller_->Animate(time);
517 ExpectScrollbarsOpacity(1);
518
519 // Proceed half way through the fade out animation.
520 time += kFadeDuration / 2;
521 scrollbar_controller_->Animate(time);
522 ExpectScrollbarsOpacity(.5f);
523
524 // Now move the mouse near the scrollbar. It should reset opacity to 1
525 // instantly and start animating to thick.
526 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
527 ExpectScrollbarsOpacity(1);
528 EXPECT_FLOAT_EQ(kIdleThicknessScale,
529 v_scrollbar_layer_->thumb_thickness_scale_factor());
530 EXPECT_FLOAT_EQ(kIdleThicknessScale,
531 h_scrollbar_layer_->thumb_thickness_scale_factor());
532
533 scrollbar_controller_->Animate(time);
534 time += kThinningDuration;
535 scrollbar_controller_->Animate(time);
536 ExpectScrollbarsOpacity(1);
537 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
538 EXPECT_FLOAT_EQ(kIdleThicknessScale,
539 h_scrollbar_layer_->thumb_thickness_scale_factor());
540 }
541
542 // Make sure we can't capture scrollbar that's completely faded out.
543 TEST_F(ScrollbarAnimationControllerThinningTest, TestCantCaptureWhenFaded) {
544 base::TimeTicks time;
545 time += base::TimeDelta::FromSeconds(1);
546
547 scrollbar_controller_->DidScrollBegin();
548 scrollbar_controller_->DidScrollUpdate(false);
549 scrollbar_controller_->DidScrollEnd();
550
551 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
552 EXPECT_FALSE(client_.start_fade().is_null());
553 EXPECT_FALSE(client_.start_fade().IsCancelled());
554 client_.start_fade().Run();
555 scrollbar_controller_->Animate(time);
556 ExpectScrollbarsOpacity(1);
557
558 // Fade the scrollbar out completely.
559 time += kFadeDuration;
560 scrollbar_controller_->Animate(time);
561 ExpectScrollbarsOpacity(0);
562
563 // Move mouse over the scrollbar. It shouldn't thicken the scrollbar since
564 // it's completely faded out.
565 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 0);
566 scrollbar_controller_->Animate(time);
567 time += kThinningDuration;
568 scrollbar_controller_->Animate(time);
569 ExpectScrollbarsOpacity(0);
570 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
571 EXPECT_FLOAT_EQ(kIdleThicknessScale,
572 h_scrollbar_layer_->thumb_thickness_scale_factor());
573
574 client_.start_fade().Reset();
575
576 // Now try to capture the scrollbar. It shouldn't do anything since it's
577 // completely faded out.
578 scrollbar_controller_->DidMouseDown();
579 ExpectScrollbarsOpacity(0);
580 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
581 EXPECT_FLOAT_EQ(kIdleThicknessScale,
582 h_scrollbar_layer_->thumb_thickness_scale_factor());
583 EXPECT_TRUE(client_.start_fade().is_null());
584
585 // Similarly, releasing the scrollbar should have no effect.
586 scrollbar_controller_->DidMouseUp();
587 ExpectScrollbarsOpacity(0);
588 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
589 EXPECT_FLOAT_EQ(kIdleThicknessScale,
590 h_scrollbar_layer_->thumb_thickness_scale_factor());
591 EXPECT_TRUE(client_.start_fade().is_null());
592 }
593
594 // Initiate a scroll when the pointer is already near the scrollbar. It should
595 // appear thick and remain thick.
596 TEST_F(ScrollbarAnimationControllerThinningTest, ScrollWithMouseNear) {
597 base::TimeTicks time;
598 time += base::TimeDelta::FromSeconds(1);
599
600 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
601 scrollbar_controller_->Animate(time);
602 time += kThinningDuration;
603
604 // Since the scrollbar isn't visible yet (because we haven't scrolled), we
605 // shouldn't have applied the thickening.
606 scrollbar_controller_->Animate(time);
607 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
608 EXPECT_FLOAT_EQ(kIdleThicknessScale,
609 h_scrollbar_layer_->thumb_thickness_scale_factor());
610
611 scrollbar_controller_->DidScrollBegin();
612 scrollbar_controller_->DidScrollUpdate(false);
613
614 // Now that we've received a scroll, we should be thick without an animation.
615 ExpectScrollbarsOpacity(1);
616
617 // An animation for the fade should be either null or cancelled, since
618 // mouse is still near the scrollbar.
619 scrollbar_controller_->DidScrollEnd();
620 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
621 EXPECT_TRUE(client_.start_fade().is_null() ||
622 client_.start_fade().IsCancelled());
623
624 scrollbar_controller_->Animate(time);
625 ExpectScrollbarsOpacity(1);
626 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
627 EXPECT_FLOAT_EQ(kIdleThicknessScale,
628 h_scrollbar_layer_->thumb_thickness_scale_factor());
629
630 // Scrollbar should still be thick and visible.
631 time += kFadeDuration;
632 scrollbar_controller_->Animate(time);
633 ExpectScrollbarsOpacity(1);
634 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
635 EXPECT_FLOAT_EQ(kIdleThicknessScale,
636 h_scrollbar_layer_->thumb_thickness_scale_factor());
637 }
638
639 // Tests that main thread scroll updates immediatley queue a fade animation
640 // without requiring a ScrollEnd.
641 TEST_F(ScrollbarAnimationControllerThinningTest, MainThreadScrollQueuesFade) {
642 ASSERT_TRUE(client_.start_fade().is_null());
643
644 // A ScrollUpdate without a ScrollBegin indicates a main thread scroll update
645 // so we should schedule a fade animation without waiting for a ScrollEnd
646 // (which will never come).
647 scrollbar_controller_->DidScrollUpdate(false);
648 EXPECT_FALSE(client_.start_fade().is_null());
649 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
650
651 client_.start_fade().Reset();
652
653 // If we got a ScrollBegin, we shouldn't schedule the fade animation until we
654 // get a corresponding ScrollEnd.
655 scrollbar_controller_->DidScrollBegin();
656 scrollbar_controller_->DidScrollUpdate(false);
657 EXPECT_TRUE(client_.start_fade().is_null());
658 scrollbar_controller_->DidScrollEnd();
659 EXPECT_FALSE(client_.start_fade().is_null());
660 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
661 }
662
663 // Make sure that if the scroll update is as a result of a resize, we use the
664 // resize delay time instead of the default one.
665 TEST_F(ScrollbarAnimationControllerThinningTest, ResizeFadeDuration) {
666 ASSERT_TRUE(client_.delay().is_zero());
667
668 scrollbar_controller_->DidScrollUpdate(true);
669 EXPECT_FALSE(client_.start_fade().is_null());
670 EXPECT_EQ(kResizeDelayBeforeStarting, client_.delay());
671
672 client_.delay() = base::TimeDelta();
673
674 // We should use the gesture delay rather than the resize delay if we're in a
675 // gesture scroll, even if the resize param is set.
676 scrollbar_controller_->DidScrollBegin();
677 scrollbar_controller_->DidScrollUpdate(true);
678 scrollbar_controller_->DidScrollEnd();
679
680 EXPECT_FALSE(client_.start_fade().is_null());
681 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
682 }
683
684 // Tests that the fade effect is animated.
685 TEST_F(ScrollbarAnimationControllerThinningTest, FadeAnimated) {
686 base::TimeTicks time;
687 time += base::TimeDelta::FromSeconds(1);
688
689 // Scroll to make the scrollbars visible.
690 scrollbar_controller_->DidScrollBegin();
691 scrollbar_controller_->DidScrollUpdate(false);
692 scrollbar_controller_->DidScrollEnd();
693
694 // Appearance is instant.
695 ExpectScrollbarsOpacity(1);
696
697 // An animation should have been enqueued.
698 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
699 EXPECT_FALSE(client_.start_fade().is_null());
700 client_.start_fade().Run();
701
702 // Test that at half the fade duration time, the opacity is at half.
703 scrollbar_controller_->Animate(time);
704 ExpectScrollbarsOpacity(1);
705
706 time += kFadeDuration / 2;
707 scrollbar_controller_->Animate(time);
708 ExpectScrollbarsOpacity(.5f);
709
710 time += kFadeDuration / 2;
711 scrollbar_controller_->Animate(time);
712 ExpectScrollbarsOpacity(0);
713 }
714
715 // Tests that the controller tells the client when the scrollbars hide/show.
716 TEST_F(ScrollbarAnimationControllerThinningTest, NotifyChangedVisibility) {
717 base::TimeTicks time;
718 time += base::TimeDelta::FromSeconds(1);
719
720 EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(1);
721 // Scroll to make the scrollbars visible.
722 scrollbar_controller_->DidScrollBegin();
723 scrollbar_controller_->DidScrollUpdate(false);
724 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
725 Mock::VerifyAndClearExpectations(&client_);
726
727 scrollbar_controller_->DidScrollEnd();
728
729 // Play out the fade animation. We shouldn't notify that the scrollbars are
730 // hidden until the animation is completly over. We can (but don't have to)
731 // notify during the animation that the scrollbars are still visible.
732 EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(0);
733 ASSERT_FALSE(client_.start_fade().is_null());
734 client_.start_fade().Run();
735 scrollbar_controller_->Animate(time);
736 time += kFadeDuration / 4;
737 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
738 scrollbar_controller_->Animate(time);
739 time += kFadeDuration / 4;
740 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
741 scrollbar_controller_->Animate(time);
742 time += kFadeDuration / 4;
743 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
744 scrollbar_controller_->Animate(time);
745 ExpectScrollbarsOpacity(.25f);
746 Mock::VerifyAndClearExpectations(&client_);
747
748 EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(1);
749 time += kFadeDuration / 4;
750 scrollbar_controller_->Animate(time);
751 EXPECT_TRUE(scrollbar_controller_->ScrollbarsHidden());
752 ExpectScrollbarsOpacity(0);
753 Mock::VerifyAndClearExpectations(&client_);
754
755 // Calling DidScrollUpdate without a begin (i.e. update from commit) should
756 // also notify.
757 EXPECT_CALL(client_, DidChangeScrollbarVisibility()).Times(1);
758 scrollbar_controller_->DidScrollUpdate(false);
759 EXPECT_FALSE(scrollbar_controller_->ScrollbarsHidden());
760 Mock::VerifyAndClearExpectations(&client_);
761 }
762
763 // Move the pointer near each scrollbar. Confirm it gets thick and narrow when
764 // moved away.
765 TEST_F(ScrollbarAnimationControllerThinningTest, MouseNearEach) {
766 base::TimeTicks time;
767 time += base::TimeDelta::FromSeconds(1);
768
769 // Scroll to make the scrollbars visible.
770 scrollbar_controller_->DidScrollBegin();
771 scrollbar_controller_->DidScrollUpdate(false);
772 scrollbar_controller_->DidScrollEnd();
773
774 // Near vertical scrollbar
775 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
776 scrollbar_controller_->Animate(time);
777 ExpectScrollbarsOpacity(1);
778 EXPECT_FLOAT_EQ(kIdleThicknessScale,
779 v_scrollbar_layer_->thumb_thickness_scale_factor());
780 EXPECT_FLOAT_EQ(kIdleThicknessScale,
781 h_scrollbar_layer_->thumb_thickness_scale_factor());
782
783 // Should animate to thickened.
784 time += kThinningDuration;
785 scrollbar_controller_->Animate(time);
786 ExpectScrollbarsOpacity(1);
787 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
788 EXPECT_FLOAT_EQ(kIdleThicknessScale,
789 h_scrollbar_layer_->thumb_thickness_scale_factor());
790
791 // Subsequent moves within the nearness threshold should not change anything.
792 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 2);
793 scrollbar_controller_->Animate(time);
794 time += base::TimeDelta::FromSeconds(10);
795 scrollbar_controller_->Animate(time);
796 ExpectScrollbarsOpacity(1);
797 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
798 EXPECT_FLOAT_EQ(kIdleThicknessScale,
799 h_scrollbar_layer_->thumb_thickness_scale_factor());
800
801 // Now move away from bar.
802 scrollbar_controller_->DidMouseMoveNear(
803 VERTICAL, kDefaultMouseMoveDistanceToTriggerAnimation);
804 scrollbar_controller_->Animate(time);
805 time += kThinningDuration;
806 scrollbar_controller_->Animate(time);
807 ExpectScrollbarsOpacity(1);
808 EXPECT_FLOAT_EQ(kIdleThicknessScale,
809 v_scrollbar_layer_->thumb_thickness_scale_factor());
810 EXPECT_FLOAT_EQ(kIdleThicknessScale,
811 h_scrollbar_layer_->thumb_thickness_scale_factor());
812
813 // Near horizontal scrollbar
814 scrollbar_controller_->DidMouseMoveNear(HORIZONTAL, 2);
815 scrollbar_controller_->Animate(time);
816 ExpectScrollbarsOpacity(1);
817 EXPECT_FLOAT_EQ(kIdleThicknessScale,
818 v_scrollbar_layer_->thumb_thickness_scale_factor());
819 EXPECT_FLOAT_EQ(kIdleThicknessScale,
820 h_scrollbar_layer_->thumb_thickness_scale_factor());
821
822 // Should animate to thickened.
823 time += kThinningDuration;
824 scrollbar_controller_->Animate(time);
825 ExpectScrollbarsOpacity(1);
826 EXPECT_FLOAT_EQ(kIdleThicknessScale,
827 v_scrollbar_layer_->thumb_thickness_scale_factor());
828 EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
829
830 // Subsequent moves within the nearness threshold should not change anything.
831 scrollbar_controller_->DidMouseMoveNear(HORIZONTAL, 1);
832 scrollbar_controller_->Animate(time);
833 time += base::TimeDelta::FromSeconds(10);
834 scrollbar_controller_->Animate(time);
835 ExpectScrollbarsOpacity(1);
836 EXPECT_FLOAT_EQ(kIdleThicknessScale,
837 v_scrollbar_layer_->thumb_thickness_scale_factor());
838 EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
839
840 // Now move away from bar.
841 scrollbar_controller_->DidMouseMoveNear(
842 HORIZONTAL, kDefaultMouseMoveDistanceToTriggerAnimation);
843 scrollbar_controller_->Animate(time);
844 time += kThinningDuration;
845 scrollbar_controller_->Animate(time);
846 ExpectScrollbarsOpacity(1);
847 EXPECT_FLOAT_EQ(kIdleThicknessScale,
848 v_scrollbar_layer_->thumb_thickness_scale_factor());
849 EXPECT_FLOAT_EQ(kIdleThicknessScale,
850 h_scrollbar_layer_->thumb_thickness_scale_factor());
851
852 // An animation should have been enqueued.
853 EXPECT_FALSE(client_.start_fade().is_null());
854 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
855 }
856
857 // Move mouse near both scrollbars at the same time.
858 TEST_F(ScrollbarAnimationControllerThinningTest, MouseNearBoth) {
859 base::TimeTicks time;
860 time += base::TimeDelta::FromSeconds(1);
861
862 // Scroll to make the scrollbars visible.
863 scrollbar_controller_->DidScrollBegin();
864 scrollbar_controller_->DidScrollUpdate(false);
865 scrollbar_controller_->DidScrollEnd();
866
867 // Near both Scrollbar
868 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
869 scrollbar_controller_->DidMouseMoveNear(HORIZONTAL, 1);
870 scrollbar_controller_->Animate(time);
871 ExpectScrollbarsOpacity(1);
872 EXPECT_FLOAT_EQ(kIdleThicknessScale,
873 v_scrollbar_layer_->thumb_thickness_scale_factor());
874 EXPECT_FLOAT_EQ(kIdleThicknessScale,
875 h_scrollbar_layer_->thumb_thickness_scale_factor());
876
877 // Should animate to thickened.
878 time += kThinningDuration;
879 scrollbar_controller_->Animate(time);
880 ExpectScrollbarsOpacity(1);
881 EXPECT_FLOAT_EQ(1, v_scrollbar_layer_->thumb_thickness_scale_factor());
882 EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
883 }
884
885 // Move mouse from one to the other scrollbar before animation is finished, then
886 // away before animation finished.
887 TEST_F(ScrollbarAnimationControllerThinningTest,
888 MouseNearOtherBeforeAnimationFinished) {
889 base::TimeTicks time;
890 time += base::TimeDelta::FromSeconds(1);
891
892 // Scroll to make the scrollbars visible.
893 scrollbar_controller_->DidScrollBegin();
894 scrollbar_controller_->DidScrollUpdate(false);
895 scrollbar_controller_->DidScrollEnd();
896
897 // Near vertical scrollbar.
898 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
899 scrollbar_controller_->Animate(time);
900 ExpectScrollbarsOpacity(1);
901 EXPECT_FLOAT_EQ(kIdleThicknessScale,
902 v_scrollbar_layer_->thumb_thickness_scale_factor());
903 EXPECT_FLOAT_EQ(kIdleThicknessScale,
904 h_scrollbar_layer_->thumb_thickness_scale_factor());
905
906 // Vertical scrollbar animate to half thickened.
907 time += kThinningDuration / 2;
908 scrollbar_controller_->Animate(time);
909 ExpectScrollbarsOpacity(1);
910 EXPECT_FLOAT_EQ(kIdleThicknessScale + (1.0f - kIdleThicknessScale) / 2,
911 v_scrollbar_layer_->thumb_thickness_scale_factor());
912 EXPECT_FLOAT_EQ(kIdleThicknessScale,
913 h_scrollbar_layer_->thumb_thickness_scale_factor());
914
915 // Away vertical scrollbar and near horizontal scrollbar.
916 scrollbar_controller_->DidMouseMoveNear(
917 VERTICAL, kDefaultMouseMoveDistanceToTriggerAnimation);
918 scrollbar_controller_->DidMouseMoveNear(HORIZONTAL, 1);
919 scrollbar_controller_->Animate(time);
920
921 // Vertical scrollbar animate to thin. horizontal scrollbar animate to
922 // thickened.
923 time += kThinningDuration;
924 scrollbar_controller_->Animate(time);
925 ExpectScrollbarsOpacity(1);
926 EXPECT_FLOAT_EQ(kIdleThicknessScale,
927 v_scrollbar_layer_->thumb_thickness_scale_factor());
928 EXPECT_FLOAT_EQ(1, h_scrollbar_layer_->thumb_thickness_scale_factor());
929
930 // Away horizontal scrollbar.
931 scrollbar_controller_->DidMouseMoveNear(
932 HORIZONTAL, kDefaultMouseMoveDistanceToTriggerAnimation);
933 scrollbar_controller_->Animate(time);
934
935 // Horizontal scrollbar animate to thin.
936 time += kThinningDuration;
937 scrollbar_controller_->Animate(time);
938 ExpectScrollbarsOpacity(1);
939 EXPECT_FLOAT_EQ(kIdleThicknessScale,
940 v_scrollbar_layer_->thumb_thickness_scale_factor());
941 EXPECT_FLOAT_EQ(kIdleThicknessScale,
942 h_scrollbar_layer_->thumb_thickness_scale_factor());
943
944 // An animation should have been enqueued.
945 EXPECT_FALSE(client_.start_fade().is_null());
946 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
947 }
948
949 // Ensure we have a delay fadeout animation after mouse leave without a mouse
950 // move.
951 TEST_F(ScrollbarAnimationControllerThinningTest, MouseLeaveFadeOut) {
952 base::TimeTicks time;
953 time += base::TimeDelta::FromSeconds(1);
954
955 // Move mouse near scrollbar.
956 scrollbar_controller_->DidMouseMoveNear(VERTICAL, 1);
957
958 // Scroll to make the scrollbars visible.
959 scrollbar_controller_->DidScrollBegin();
960 scrollbar_controller_->DidScrollUpdate(false);
961 scrollbar_controller_->DidScrollEnd();
962
963 // Should not have delay fadeout animation.
964 EXPECT_TRUE(client_.start_fade().is_null() ||
965 client_.start_fade().IsCancelled());
966
967 // Mouse leave.
968 scrollbar_controller_->DidMouseLeave();
969
970 // An animation should have been enqueued.
971 EXPECT_FALSE(client_.start_fade().is_null());
972 EXPECT_EQ(kDelayBeforeStarting, client_.delay());
973 }
974
975 } // namespace
976 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698