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

Side by Side Diff: cc/input/scrollbar_animation_controller_linear_fade_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 2012 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_linear_fade.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/gtest/include/gtest/gtest.h"
14
15 namespace cc {
16 namespace {
17
18 class ScrollbarAnimationControllerLinearFadeTest
19 : public testing::Test,
20 public ScrollbarAnimationControllerClient {
21 public:
22 ScrollbarAnimationControllerLinearFadeTest()
23 : host_impl_(&task_runner_provider_,
24 &task_graph_runner_),
25 did_request_redraw_(false),
26 did_request_animate_(false) {}
27
28 void PostDelayedScrollbarAnimationTask(const base::Closure& start_fade,
29 base::TimeDelta delay) override {
30 start_fade_ = start_fade;
31 delay_ = delay;
32 }
33 void SetNeedsRedrawForScrollbarAnimation() override {
34 did_request_redraw_ = true;
35 }
36 void SetNeedsAnimateForScrollbarAnimation() override {
37 did_request_animate_ = true;
38 }
39 ScrollbarSet ScrollbarsFor(int scroll_layer_id) const override {
40 return host_impl_.ScrollbarsFor(scroll_layer_id);
41 }
42 void DidChangeScrollbarVisibility() override {}
43
44 protected:
45 void SetUp() override {
46 const int kThumbThickness = 10;
47 const int kTrackStart = 0;
48 const bool kIsLeftSideVerticalScrollbar = false;
49 const bool kIsOverlayScrollbar = true; // Allow opacity animations.
50
51 std::unique_ptr<LayerImpl> scroll_layer =
52 LayerImpl::Create(host_impl_.active_tree(), 1);
53 std::unique_ptr<SolidColorScrollbarLayerImpl> scrollbar =
54 SolidColorScrollbarLayerImpl::Create(
55 host_impl_.active_tree(), 2, orientation(), kThumbThickness,
56 kTrackStart, kIsLeftSideVerticalScrollbar, kIsOverlayScrollbar);
57 scrollbar_layer_ = scrollbar.get();
58 scrollbar_layer_->test_properties()->opacity_can_animate = true;
59 std::unique_ptr<LayerImpl> clip =
60 LayerImpl::Create(host_impl_.active_tree(), 3);
61 clip_layer_ = clip.get();
62 scroll_layer->SetScrollClipLayer(clip_layer_->id());
63 LayerImpl* scroll_layer_ptr = scroll_layer.get();
64 scroll_layer->test_properties()->AddChild(std::move(scrollbar));
65 clip->test_properties()->AddChild(std::move(scroll_layer));
66 host_impl_.active_tree()->SetRootLayerForTesting(std::move(clip));
67
68 scrollbar_layer_->SetScrollLayerId(scroll_layer_ptr->id());
69 clip_layer_->SetBounds(gfx::Size(100, 100));
70 scroll_layer_ptr->SetBounds(gfx::Size(200, 200));
71 host_impl_.active_tree()->BuildLayerListAndPropertyTreesForTesting();
72
73 scrollbar_controller_ = ScrollbarAnimationControllerLinearFade::Create(
74 scroll_layer_ptr->id(), this, base::TimeDelta::FromSeconds(2),
75 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromSeconds(3));
76 }
77
78 virtual ScrollbarOrientation orientation() const { return HORIZONTAL; }
79
80 FakeImplTaskRunnerProvider task_runner_provider_;
81 TestTaskGraphRunner task_graph_runner_;
82 FakeLayerTreeHostImpl host_impl_;
83 std::unique_ptr<ScrollbarAnimationControllerLinearFade> scrollbar_controller_;
84 LayerImpl* clip_layer_;
85 SolidColorScrollbarLayerImpl* scrollbar_layer_;
86
87 base::Closure start_fade_;
88 base::TimeDelta delay_;
89 bool did_request_redraw_;
90 bool did_request_animate_;
91 };
92
93 class VerticalScrollbarAnimationControllerLinearFadeTest
94 : public ScrollbarAnimationControllerLinearFadeTest {
95 protected:
96 ScrollbarOrientation orientation() const override { return VERTICAL; }
97 };
98
99 TEST_F(ScrollbarAnimationControllerLinearFadeTest, DelayAnimationOnResize) {
100 scrollbar_layer_->layer_tree_impl()
101 ->property_trees()
102 ->effect_tree.OnOpacityAnimated(0.0f,
103 scrollbar_layer_->effect_tree_index(),
104 scrollbar_layer_->layer_tree_impl());
105 scrollbar_controller_->DidScrollBegin();
106 scrollbar_controller_->DidScrollUpdate(true);
107 scrollbar_controller_->DidScrollEnd();
108 // Normal Animation delay of 2 seconds.
109 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
110 EXPECT_EQ(delay_, base::TimeDelta::FromSeconds(2));
111
112 scrollbar_layer_->layer_tree_impl()
113 ->property_trees()
114 ->effect_tree.OnOpacityAnimated(0.0f,
115 scrollbar_layer_->effect_tree_index(),
116 scrollbar_layer_->layer_tree_impl());
117 scrollbar_controller_->DidScrollUpdate(true);
118 // Delay animation on resize to 5 seconds.
119 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
120 EXPECT_EQ(delay_, base::TimeDelta::FromSeconds(5));
121 }
122
123 TEST_F(ScrollbarAnimationControllerLinearFadeTest, HiddenInBegin) {
124 scrollbar_layer_->layer_tree_impl()
125 ->property_trees()
126 ->effect_tree.OnOpacityAnimated(0.0f,
127 scrollbar_layer_->effect_tree_index(),
128 scrollbar_layer_->layer_tree_impl());
129 scrollbar_controller_->Animate(base::TimeTicks());
130 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
131 }
132
133 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
134 HiddenAfterNonScrollingGesture) {
135 scrollbar_layer_->layer_tree_impl()
136 ->property_trees()
137 ->effect_tree.OnOpacityAnimated(0.0f,
138 scrollbar_layer_->effect_tree_index(),
139 scrollbar_layer_->layer_tree_impl());
140 scrollbar_controller_->DidScrollBegin();
141
142 base::TimeTicks time;
143 time += base::TimeDelta::FromSeconds(100);
144 scrollbar_controller_->Animate(time);
145 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
146 scrollbar_controller_->DidScrollEnd();
147
148 EXPECT_TRUE(start_fade_.Equals(base::Closure()));
149
150 time += base::TimeDelta::FromSeconds(100);
151 scrollbar_controller_->Animate(time);
152 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
153 }
154
155 TEST_F(ScrollbarAnimationControllerLinearFadeTest, HideOnResize) {
156 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
157 ASSERT_TRUE(scroll_layer);
158 EXPECT_EQ(gfx::Size(200, 200), scroll_layer->bounds());
159
160 EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());
161
162 // Shrink along X axis, horizontal scrollbar should appear.
163 clip_layer_->SetBounds(gfx::Size(100, 200));
164 EXPECT_EQ(gfx::Size(100, 200), clip_layer_->bounds());
165
166 scrollbar_controller_->DidScrollBegin();
167
168 scrollbar_controller_->DidScrollUpdate(false);
169 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
170
171 scrollbar_controller_->DidScrollEnd();
172
173 // Shrink along Y axis and expand along X, horizontal scrollbar
174 // should disappear.
175 clip_layer_->SetBounds(gfx::Size(200, 100));
176 EXPECT_EQ(gfx::Size(200, 100), clip_layer_->bounds());
177
178 scrollbar_controller_->DidScrollBegin();
179
180 scrollbar_controller_->DidScrollUpdate(false);
181 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
182
183 scrollbar_controller_->DidScrollEnd();
184 }
185
186 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest, HideOnResize) {
187 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
188 ASSERT_TRUE(scroll_layer);
189 EXPECT_EQ(gfx::Size(200, 200), scroll_layer->bounds());
190
191 EXPECT_EQ(VERTICAL, scrollbar_layer_->orientation());
192
193 // Shrink along X axis, vertical scrollbar should remain invisible.
194 clip_layer_->SetBounds(gfx::Size(100, 200));
195 EXPECT_EQ(gfx::Size(100, 200), clip_layer_->bounds());
196
197 scrollbar_controller_->DidScrollBegin();
198
199 scrollbar_controller_->DidScrollUpdate(false);
200 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
201
202 scrollbar_controller_->DidScrollEnd();
203
204 // Shrink along Y axis and expand along X, vertical scrollbar should appear.
205 clip_layer_->SetBounds(gfx::Size(200, 100));
206 EXPECT_EQ(gfx::Size(200, 100), clip_layer_->bounds());
207
208 scrollbar_controller_->DidScrollBegin();
209
210 scrollbar_controller_->DidScrollUpdate(false);
211 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
212
213 scrollbar_controller_->DidScrollEnd();
214 }
215
216 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
217 HideOnUserNonScrollableHorz) {
218 EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());
219
220 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
221 ASSERT_TRUE(scroll_layer);
222 scroll_layer->set_user_scrollable_horizontal(false);
223
224 scrollbar_controller_->DidScrollBegin();
225
226 scrollbar_controller_->DidScrollUpdate(false);
227 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
228
229 scrollbar_controller_->DidScrollEnd();
230 }
231
232 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
233 ShowOnUserNonScrollableVert) {
234 EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());
235
236 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
237 ASSERT_TRUE(scroll_layer);
238 scroll_layer->set_user_scrollable_vertical(false);
239
240 scrollbar_controller_->DidScrollBegin();
241
242 scrollbar_controller_->DidScrollUpdate(false);
243 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
244
245 scrollbar_controller_->DidScrollEnd();
246 }
247
248 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest,
249 HideOnUserNonScrollableVert) {
250 EXPECT_EQ(VERTICAL, scrollbar_layer_->orientation());
251
252 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
253 ASSERT_TRUE(scroll_layer);
254 scroll_layer->set_user_scrollable_vertical(false);
255
256 scrollbar_controller_->DidScrollBegin();
257
258 scrollbar_controller_->DidScrollUpdate(false);
259 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
260
261 scrollbar_controller_->DidScrollEnd();
262 }
263
264 TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest,
265 ShowOnUserNonScrollableHorz) {
266 EXPECT_EQ(VERTICAL, scrollbar_layer_->orientation());
267
268 LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
269 ASSERT_TRUE(scroll_layer);
270 scroll_layer->set_user_scrollable_horizontal(false);
271
272 scrollbar_controller_->DidScrollBegin();
273
274 scrollbar_controller_->DidScrollUpdate(false);
275 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
276
277 scrollbar_controller_->DidScrollEnd();
278 }
279
280 TEST_F(ScrollbarAnimationControllerLinearFadeTest, AwakenByScrollingGesture) {
281 base::TimeTicks time;
282 time += base::TimeDelta::FromSeconds(1);
283 scrollbar_controller_->DidScrollBegin();
284 EXPECT_FALSE(did_request_animate_);
285
286 scrollbar_controller_->DidScrollUpdate(false);
287 EXPECT_FALSE(did_request_animate_);
288 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
289
290 EXPECT_TRUE(start_fade_.Equals(base::Closure()));
291
292 time += base::TimeDelta::FromSeconds(100);
293
294 scrollbar_controller_->Animate(time);
295 EXPECT_FALSE(did_request_animate_);
296 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
297 scrollbar_controller_->DidScrollEnd();
298 EXPECT_FALSE(did_request_animate_);
299 start_fade_.Run();
300 EXPECT_TRUE(did_request_animate_);
301 did_request_animate_ = false;
302
303 time += base::TimeDelta::FromSeconds(2);
304 scrollbar_controller_->Animate(time);
305 EXPECT_TRUE(did_request_animate_);
306 did_request_animate_ = false;
307 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
308
309 time += base::TimeDelta::FromSeconds(1);
310 scrollbar_controller_->Animate(time);
311 EXPECT_TRUE(did_request_animate_);
312 did_request_animate_ = false;
313 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
314
315 time += base::TimeDelta::FromSeconds(1);
316 scrollbar_controller_->Animate(time);
317 EXPECT_TRUE(did_request_animate_);
318 did_request_animate_ = false;
319 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
320
321 time += base::TimeDelta::FromSeconds(1);
322
323 scrollbar_controller_->DidScrollBegin();
324 scrollbar_controller_->DidScrollUpdate(false);
325 scrollbar_controller_->DidScrollEnd();
326
327 start_fade_.Run();
328 EXPECT_TRUE(did_request_animate_);
329 did_request_animate_ = false;
330
331 time += base::TimeDelta::FromSeconds(2);
332 scrollbar_controller_->Animate(time);
333 EXPECT_TRUE(did_request_animate_);
334 did_request_animate_ = false;
335 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
336
337 time += base::TimeDelta::FromSeconds(1);
338 scrollbar_controller_->Animate(time);
339 EXPECT_TRUE(did_request_animate_);
340 did_request_animate_ = false;
341 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
342
343 time += base::TimeDelta::FromSeconds(1);
344 scrollbar_controller_->Animate(time);
345 EXPECT_TRUE(did_request_animate_);
346 did_request_animate_ = false;
347 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
348
349 time += base::TimeDelta::FromSeconds(1);
350 scrollbar_controller_->Animate(time);
351 EXPECT_FALSE(did_request_animate_);
352 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
353 }
354
355 TEST_F(ScrollbarAnimationControllerLinearFadeTest, AwakenByProgrammaticScroll) {
356 base::TimeTicks time;
357 time += base::TimeDelta::FromSeconds(1);
358 scrollbar_controller_->DidScrollUpdate(false);
359 EXPECT_FALSE(did_request_animate_);
360
361 start_fade_.Run();
362 EXPECT_TRUE(did_request_animate_);
363 did_request_animate_ = false;
364 scrollbar_controller_->Animate(time);
365 EXPECT_TRUE(did_request_animate_);
366 did_request_animate_ = false;
367 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
368
369 time += base::TimeDelta::FromSeconds(1);
370 scrollbar_controller_->Animate(time);
371 EXPECT_TRUE(did_request_animate_);
372 did_request_animate_ = false;
373 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
374 scrollbar_controller_->DidScrollUpdate(false);
375 EXPECT_FALSE(did_request_animate_);
376
377 start_fade_.Run();
378 EXPECT_TRUE(did_request_animate_);
379 did_request_animate_ = false;
380 time += base::TimeDelta::FromSeconds(2);
381 scrollbar_controller_->Animate(time);
382 EXPECT_TRUE(did_request_animate_);
383 did_request_animate_ = false;
384 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
385
386 time += base::TimeDelta::FromSeconds(1);
387 scrollbar_controller_->Animate(time);
388 EXPECT_TRUE(did_request_animate_);
389 did_request_animate_ = false;
390 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
391
392 time += base::TimeDelta::FromSeconds(1);
393 scrollbar_controller_->Animate(time);
394 EXPECT_TRUE(did_request_animate_);
395 did_request_animate_ = false;
396 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
397
398 time += base::TimeDelta::FromSeconds(1);
399 scrollbar_controller_->DidScrollUpdate(false);
400 start_fade_.Run();
401 time += base::TimeDelta::FromSeconds(1);
402 scrollbar_controller_->Animate(time);
403 EXPECT_TRUE(did_request_animate_);
404 did_request_animate_ = false;
405 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
406
407 time += base::TimeDelta::FromSeconds(1);
408 scrollbar_controller_->Animate(time);
409 EXPECT_TRUE(did_request_animate_);
410 did_request_animate_ = false;
411 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
412
413 time += base::TimeDelta::FromSeconds(1);
414 scrollbar_controller_->Animate(time);
415 EXPECT_TRUE(did_request_animate_);
416 did_request_animate_ = false;
417 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
418
419 time += base::TimeDelta::FromSeconds(1);
420 scrollbar_controller_->Animate(time);
421 EXPECT_FALSE(did_request_animate_);
422 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
423 }
424
425 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
426 AnimationPreservedByNonScrollingGesture) {
427 base::TimeTicks time;
428 time += base::TimeDelta::FromSeconds(1);
429 scrollbar_controller_->DidScrollUpdate(false);
430 start_fade_.Run();
431 EXPECT_TRUE(did_request_animate_);
432 did_request_animate_ = false;
433 scrollbar_controller_->Animate(time);
434 EXPECT_TRUE(did_request_animate_);
435 did_request_animate_ = false;
436 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
437
438 time += base::TimeDelta::FromSeconds(1);
439 scrollbar_controller_->Animate(time);
440 EXPECT_TRUE(did_request_animate_);
441 did_request_animate_ = false;
442 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
443
444 scrollbar_controller_->DidScrollBegin();
445 EXPECT_FALSE(did_request_animate_);
446 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
447
448 time += base::TimeDelta::FromSeconds(1);
449 scrollbar_controller_->Animate(time);
450 EXPECT_TRUE(did_request_animate_);
451 did_request_animate_ = false;
452 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
453
454 scrollbar_controller_->DidScrollEnd();
455 EXPECT_FALSE(did_request_animate_);
456 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
457
458 time += base::TimeDelta::FromSeconds(1);
459 scrollbar_controller_->Animate(time);
460 EXPECT_FALSE(did_request_animate_);
461 EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->Opacity());
462 }
463
464 TEST_F(ScrollbarAnimationControllerLinearFadeTest,
465 AnimationOverriddenByScrollingGesture) {
466 base::TimeTicks time;
467 time += base::TimeDelta::FromSeconds(1);
468 scrollbar_controller_->DidScrollUpdate(false);
469 EXPECT_FALSE(did_request_animate_);
470 start_fade_.Run();
471 EXPECT_TRUE(did_request_animate_);
472 did_request_animate_ = false;
473 scrollbar_controller_->Animate(time);
474 EXPECT_TRUE(did_request_animate_);
475 did_request_animate_ = false;
476 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->Opacity());
477
478 time += base::TimeDelta::FromSeconds(1);
479 scrollbar_controller_->Animate(time);
480 EXPECT_TRUE(did_request_animate_);
481 did_request_animate_ = false;
482 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
483
484 scrollbar_controller_->DidScrollBegin();
485 EXPECT_FLOAT_EQ(2.0f / 3.0f, scrollbar_layer_->Opacity());
486
487 time += base::TimeDelta::FromSeconds(1);
488 scrollbar_controller_->Animate(time);
489 EXPECT_TRUE(did_request_animate_);
490 did_request_animate_ = false;
491 EXPECT_FLOAT_EQ(1.0f / 3.0f, scrollbar_layer_->Opacity());
492
493 time += base::TimeDelta::FromSeconds(1);
494 scrollbar_controller_->DidScrollUpdate(false);
495 EXPECT_FALSE(did_request_animate_);
496 EXPECT_FLOAT_EQ(1, scrollbar_layer_->Opacity());
497
498 time += base::TimeDelta::FromSeconds(1);
499 scrollbar_controller_->DidScrollEnd();
500 EXPECT_FALSE(did_request_animate_);
501 EXPECT_FLOAT_EQ(1, scrollbar_layer_->Opacity());
502 }
503
504 } // namespace
505 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698