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.
5 #include "cc/animation/scrollbar_animation_controller_thinning.h"
7 #include "cc/layers/solid_color_scrollbar_layer_impl.h"
8 #include "cc/test/fake_impl_proxy.h"
9 #include "cc/test/fake_layer_tree_host_impl.h"
10 #include "cc/test/geometry_test_utils.h"
11 #include "cc/test/test_shared_bitmap_manager.h"
12 #include "cc/trees/layer_tree_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class ScrollbarAnimationControllerThinningTest
19 : public testing::Test
,
20 public ScrollbarAnimationControllerClient
{
22 ScrollbarAnimationControllerThinningTest()
23 : host_impl_(&proxy_
, &shared_bitmap_manager_
) {}
25 void PostDelayedScrollbarFade(const base::Closure
& start_fade
,
26 base::TimeDelta delay
) override
{
27 start_fade_
= start_fade
;
29 void SetNeedsScrollbarAnimationFrame() override
{}
32 void SetUp() override
{
33 scoped_ptr
<LayerImpl
> scroll_layer
=
34 LayerImpl::Create(host_impl_
.active_tree(), 1);
35 clip_layer_
= LayerImpl::Create(host_impl_
.active_tree(), 3);
36 scroll_layer
->SetScrollClipLayer(clip_layer_
->id());
37 LayerImpl
* scroll_layer_ptr
= scroll_layer
.get();
38 clip_layer_
->AddChild(scroll_layer
.Pass());
41 const int kThumbThickness
= 10;
42 const int kTrackStart
= 0;
43 const bool kIsLeftSideVerticalScrollbar
= false;
44 const bool kIsOverlayScrollbar
= true;
46 SolidColorScrollbarLayerImpl::Create(host_impl_
.active_tree(),
51 kIsLeftSideVerticalScrollbar
,
54 scrollbar_layer_
->SetScrollLayerAndClipLayerByIds(scroll_layer_ptr
->id(),
56 clip_layer_
->SetBounds(gfx::Size(100, 100));
57 scroll_layer_ptr
->SetBounds(gfx::Size(200, 200));
59 scrollbar_controller_
= ScrollbarAnimationControllerThinning::Create(
62 base::TimeDelta::FromSeconds(2),
63 base::TimeDelta::FromSeconds(5),
64 base::TimeDelta::FromSeconds(3));
68 TestSharedBitmapManager shared_bitmap_manager_
;
69 FakeLayerTreeHostImpl host_impl_
;
70 scoped_ptr
<ScrollbarAnimationControllerThinning
> scrollbar_controller_
;
71 scoped_ptr
<LayerImpl
> clip_layer_
;
72 scoped_ptr
<SolidColorScrollbarLayerImpl
> scrollbar_layer_
;
74 base::Closure start_fade_
;
77 // Check initialization of scrollbar.
78 TEST_F(ScrollbarAnimationControllerThinningTest
, Idle
) {
79 scrollbar_controller_
->Animate(base::TimeTicks());
80 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
81 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
84 // Check that scrollbar disappears when the layer becomes non-scrollable.
85 TEST_F(ScrollbarAnimationControllerThinningTest
, HideOnResize
) {
86 LayerImpl
* scroll_layer
= host_impl_
.active_tree()->LayerById(1);
87 ASSERT_TRUE(scroll_layer
);
88 EXPECT_EQ(gfx::Size(200, 200), scroll_layer
->bounds());
90 EXPECT_EQ(HORIZONTAL
, scrollbar_layer_
->orientation());
92 // Shrink along X axis, horizontal scrollbar should appear.
93 clip_layer_
->SetBounds(gfx::Size(100, 200));
94 EXPECT_EQ(gfx::Size(100, 200), clip_layer_
->bounds());
96 scrollbar_controller_
->DidScrollBegin();
98 scrollbar_controller_
->DidScrollUpdate(false);
99 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
101 scrollbar_controller_
->DidScrollEnd();
103 // Shrink along Y axis and expand along X, horizontal scrollbar
105 clip_layer_
->SetBounds(gfx::Size(200, 100));
106 EXPECT_EQ(gfx::Size(200, 100), clip_layer_
->bounds());
108 scrollbar_controller_
->DidScrollBegin();
110 scrollbar_controller_
->DidScrollUpdate(false);
111 EXPECT_FLOAT_EQ(0.0f
, scrollbar_layer_
->opacity());
113 scrollbar_controller_
->DidScrollEnd();
116 // Scroll content. Confirm the scrollbar gets dark and then becomes light
118 TEST_F(ScrollbarAnimationControllerThinningTest
, AwakenByProgrammaticScroll
) {
119 base::TimeTicks time
;
120 time
+= base::TimeDelta::FromSeconds(1);
121 scrollbar_controller_
->DidScrollUpdate(false);
122 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
123 // Scrollbar doesn't change size if triggered by scroll.
124 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
128 time
+= base::TimeDelta::FromSeconds(1);
129 scrollbar_controller_
->Animate(time
);
130 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
131 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
133 // Subsequent scroll restarts animation.
134 scrollbar_controller_
->DidScrollUpdate(false);
138 time
+= base::TimeDelta::FromSeconds(2);
139 scrollbar_controller_
->Animate(time
);
140 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
141 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
143 time
+= base::TimeDelta::FromSeconds(1);
144 scrollbar_controller_
->Animate(time
);
145 EXPECT_FLOAT_EQ(0.9f
, scrollbar_layer_
->opacity());
146 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
148 time
+= base::TimeDelta::FromSeconds(1);
149 scrollbar_controller_
->Animate(time
);
150 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->opacity());
151 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
153 time
+= base::TimeDelta::FromSeconds(1);
154 scrollbar_controller_
->Animate(time
);
155 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
156 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
159 // Initiate a scroll when the pointer is already near the scrollbar. It should
161 TEST_F(ScrollbarAnimationControllerThinningTest
, ScrollWithMouseNear
) {
162 base::TimeTicks time
;
163 time
+= base::TimeDelta::FromSeconds(1);
165 scrollbar_controller_
->DidMouseMoveNear(1);
166 scrollbar_controller_
->Animate(time
);
167 time
+= base::TimeDelta::FromSeconds(3);
169 scrollbar_controller_
->Animate(time
);
170 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
172 scrollbar_controller_
->DidScrollUpdate(false);
174 scrollbar_controller_
->Animate(time
);
175 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
176 // Scrollbar should still be thick.
177 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
179 time
+= base::TimeDelta::FromSeconds(5);
180 scrollbar_controller_
->Animate(time
);
181 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
182 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
185 // Move the pointer near the scrollbar. Confirm it gets thick and narrow when
187 TEST_F(ScrollbarAnimationControllerThinningTest
, MouseNear
) {
188 base::TimeTicks time
;
189 time
+= base::TimeDelta::FromSeconds(1);
190 scrollbar_controller_
->DidMouseMoveNear(1);
191 scrollbar_controller_
->Animate(time
);
192 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
193 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
195 // Should animate to thickened but not darken.
196 time
+= base::TimeDelta::FromSeconds(1);
197 scrollbar_controller_
->Animate(time
);
198 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
199 EXPECT_FLOAT_EQ(0.6f
, scrollbar_layer_
->thumb_thickness_scale_factor());
201 time
+= base::TimeDelta::FromSeconds(1);
202 scrollbar_controller_
->Animate(time
);
203 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
204 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
206 time
+= base::TimeDelta::FromSeconds(1);
207 scrollbar_controller_
->Animate(time
);
208 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
209 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
211 // Subsequent moves should not change anything.
212 scrollbar_controller_
->DidMouseMoveNear(1);
213 scrollbar_controller_
->Animate(time
);
214 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
215 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
217 // Now move away from bar.
218 time
+= base::TimeDelta::FromSeconds(1);
219 scrollbar_controller_
->DidMouseMoveNear(26);
220 scrollbar_controller_
->Animate(time
);
221 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
222 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
224 // Animate to narrow.
225 time
+= base::TimeDelta::FromSeconds(1);
226 scrollbar_controller_
->Animate(time
);
227 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
228 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
230 time
+= base::TimeDelta::FromSeconds(1);
231 scrollbar_controller_
->Animate(time
);
232 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
233 EXPECT_FLOAT_EQ(0.6f
, scrollbar_layer_
->thumb_thickness_scale_factor());
235 time
+= base::TimeDelta::FromSeconds(1);
236 scrollbar_controller_
->Animate(time
);
237 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
238 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
241 // Move the pointer over the scrollbar. Make sure it gets thick and dark
242 // and that it gets thin and light when moved away.
243 TEST_F(ScrollbarAnimationControllerThinningTest
, MouseOver
) {
244 base::TimeTicks time
;
245 time
+= base::TimeDelta::FromSeconds(1);
246 scrollbar_controller_
->DidMouseMoveNear(0);
247 scrollbar_controller_
->Animate(time
);
248 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
249 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
251 // Should animate to thickened and darkened.
252 time
+= base::TimeDelta::FromSeconds(1);
253 scrollbar_controller_
->Animate(time
);
254 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->opacity());
255 EXPECT_FLOAT_EQ(0.6f
, scrollbar_layer_
->thumb_thickness_scale_factor());
257 time
+= base::TimeDelta::FromSeconds(1);
258 scrollbar_controller_
->Animate(time
);
259 EXPECT_FLOAT_EQ(0.9f
, scrollbar_layer_
->opacity());
260 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
262 time
+= base::TimeDelta::FromSeconds(1);
263 scrollbar_controller_
->Animate(time
);
264 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
265 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
267 // Subsequent moves should not change anything.
268 scrollbar_controller_
->DidMouseMoveNear(0);
269 scrollbar_controller_
->Animate(time
);
270 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
271 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
273 // Now move away from bar.
274 time
+= base::TimeDelta::FromSeconds(1);
275 scrollbar_controller_
->DidMouseMoveNear(26);
276 scrollbar_controller_
->Animate(time
);
277 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
278 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
280 // Animate to narrow.
281 time
+= base::TimeDelta::FromSeconds(1);
282 scrollbar_controller_
->Animate(time
);
283 EXPECT_FLOAT_EQ(0.9f
, scrollbar_layer_
->opacity());
284 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
286 time
+= base::TimeDelta::FromSeconds(1);
287 scrollbar_controller_
->Animate(time
);
288 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->opacity());
289 EXPECT_FLOAT_EQ(0.6f
, scrollbar_layer_
->thumb_thickness_scale_factor());
291 time
+= base::TimeDelta::FromSeconds(1);
292 scrollbar_controller_
->Animate(time
);
293 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
294 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
297 // First move the pointer near the scrollbar, then over it, then back near
298 // then far away. Confirm that first the bar gets thick, then dark, then light,
300 TEST_F(ScrollbarAnimationControllerThinningTest
, MouseNearThenOver
) {
301 base::TimeTicks time
;
302 time
+= base::TimeDelta::FromSeconds(1);
303 scrollbar_controller_
->DidMouseMoveNear(1);
304 scrollbar_controller_
->Animate(time
);
305 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
306 EXPECT_FLOAT_EQ(0.4f
, scrollbar_layer_
->thumb_thickness_scale_factor());
308 // Should animate to thickened but not darken.
309 time
+= base::TimeDelta::FromSeconds(3);
310 scrollbar_controller_
->Animate(time
);
311 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
312 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
315 scrollbar_controller_
->DidMouseMoveNear(0);
316 scrollbar_controller_
->Animate(time
);
318 // Should animate to darkened.
319 time
+= base::TimeDelta::FromSeconds(1);
320 scrollbar_controller_
->Animate(time
);
321 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->opacity());
322 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
324 time
+= base::TimeDelta::FromSeconds(1);
325 scrollbar_controller_
->Animate(time
);
326 EXPECT_FLOAT_EQ(0.9f
, scrollbar_layer_
->opacity());
327 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
329 time
+= base::TimeDelta::FromSeconds(1);
330 scrollbar_controller_
->Animate(time
);
331 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->opacity());
332 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());
334 // This is tricky. The DidMouseMoveOffScrollbar() is sent before the
335 // subsequent DidMouseMoveNear(), if the mouse moves in that direction.
336 // This results in the thumb thinning. We want to make sure that when the
337 // thumb starts expanding it doesn't first narrow to the idle thinness.
338 time
+= base::TimeDelta::FromSeconds(1);
339 scrollbar_controller_
->DidMouseMoveOffScrollbar();
340 scrollbar_controller_
->Animate(time
);
342 time
+= base::TimeDelta::FromSeconds(1);
343 scrollbar_controller_
->Animate(time
);
344 EXPECT_FLOAT_EQ(0.9f
, scrollbar_layer_
->opacity());
345 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
347 scrollbar_controller_
->DidMouseMoveNear(1);
348 scrollbar_controller_
->Animate(time
);
349 // A new animation is kicked off.
351 time
+= base::TimeDelta::FromSeconds(1);
352 scrollbar_controller_
->Animate(time
);
353 // We will initiate the narrowing again, but it won't get decremented until
354 // the new animation catches up to it.
355 EXPECT_FLOAT_EQ(0.9f
, scrollbar_layer_
->opacity());
356 // Now the thickness should be increasing, but it shouldn't happen until the
357 // animation catches up.
358 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
360 time
+= base::TimeDelta::FromSeconds(1);
361 scrollbar_controller_
->Animate(time
);
362 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->opacity());
363 // The thickness now gets big again.
364 EXPECT_FLOAT_EQ(0.8f
, scrollbar_layer_
->thumb_thickness_scale_factor());
366 time
+= base::TimeDelta::FromSeconds(1);
367 scrollbar_controller_
->Animate(time
);
368 EXPECT_FLOAT_EQ(0.7f
, scrollbar_layer_
->opacity());
369 // The thickness now gets big again.
370 EXPECT_FLOAT_EQ(1.0f
, scrollbar_layer_
->thumb_thickness_scale_factor());