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

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

Issue 2554913002: Prevent overlay scrollbars expand or hover together (Closed)
Patch Set: fix for test Created 4 years 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/single_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 // single_scrollbar_animation_controller_thinning.cc.
26 const float kIdleThicknessScale = 0.4f;
27 const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f;
28
29 class MockSingleScrollbarAnimationControllerClient
30 : public ScrollbarAnimationControllerClient {
31 public:
32 explicit MockSingleScrollbarAnimationControllerClient(
33 LayerTreeHostImpl* host_impl)
34 : host_impl_(host_impl) {}
35 virtual ~MockSingleScrollbarAnimationControllerClient() {}
36
37 void PostDelayedScrollbarAnimationTask(const base::Closure& start_fade,
38 base::TimeDelta delay) override {
39 start_fade_ = start_fade;
40 delay_ = delay;
41 }
42 void SetNeedsRedrawForScrollbarAnimation() override {}
43 void SetNeedsAnimateForScrollbarAnimation() override {}
44 ScrollbarSet ScrollbarsFor(int scroll_layer_id) const override {
45 return host_impl_->ScrollbarsFor(scroll_layer_id);
46 }
47 MOCK_METHOD0(DidChangeScrollbarVisibility, void());
48
49 base::Closure& start_fade() { return start_fade_; }
50 base::TimeDelta& delay() { return delay_; }
51
52 private:
53 base::Closure start_fade_;
54 base::TimeDelta delay_;
55 LayerTreeHostImpl* host_impl_;
56 };
57
58 class SingleScrollbarAnimationControllerThinningTest : public testing::Test {
59 public:
60 SingleScrollbarAnimationControllerThinningTest()
61 : host_impl_(&task_runner_provider_, &task_graph_runner_),
62 client_(&host_impl_) {}
63
64 protected:
65 const base::TimeDelta kThinningDuration = base::TimeDelta::FromSeconds(2);
66
67 void SetUp() override {
68 std::unique_ptr<LayerImpl> scroll_layer =
69 LayerImpl::Create(host_impl_.active_tree(), 1);
70 std::unique_ptr<LayerImpl> clip =
71 LayerImpl::Create(host_impl_.active_tree(), 3);
72 clip_layer_ = clip.get();
73 scroll_layer->SetScrollClipLayer(clip_layer_->id());
74 LayerImpl* scroll_layer_ptr = scroll_layer.get();
75
76 const int kId = 2;
77 const int kThumbThickness = 10;
78 const int kTrackStart = 0;
79 const bool kIsLeftSideVerticalScrollbar = false;
80 const bool kIsOverlayScrollbar = true;
81
82 std::unique_ptr<SolidColorScrollbarLayerImpl> scrollbar =
83 SolidColorScrollbarLayerImpl::Create(
84 host_impl_.active_tree(), kId, HORIZONTAL, kThumbThickness,
85 kTrackStart, kIsLeftSideVerticalScrollbar, kIsOverlayScrollbar);
86 scrollbar_layer_ = scrollbar.get();
87
88 scroll_layer->test_properties()->AddChild(std::move(scrollbar));
89 clip_layer_->test_properties()->AddChild(std::move(scroll_layer));
90 host_impl_.active_tree()->SetRootLayerForTesting(std::move(clip));
91
92 scrollbar_layer_->SetScrollLayerId(scroll_layer_ptr->id());
93 scrollbar_layer_->test_properties()->opacity_can_animate = true;
94 clip_layer_->SetBounds(gfx::Size(100, 100));
95 scroll_layer_ptr->SetBounds(gfx::Size(200, 200));
96 host_impl_.active_tree()->BuildLayerListAndPropertyTreesForTesting();
97
98 scrollbar_controller_ = SingleScrollbarAnimationControllerThinning::Create(
99 scroll_layer_ptr->id(), HORIZONTAL, &client_, kThinningDuration);
100 }
101
102 FakeImplTaskRunnerProvider task_runner_provider_;
103 TestTaskGraphRunner task_graph_runner_;
104 FakeLayerTreeHostImpl host_impl_;
105 std::unique_ptr<SingleScrollbarAnimationControllerThinning>
106 scrollbar_controller_;
107 LayerImpl* clip_layer_;
108 SolidColorScrollbarLayerImpl* scrollbar_layer_;
109 NiceMock<MockSingleScrollbarAnimationControllerClient> client_;
110 };
111
112 // Check initialization of scrollbar. Should start thin.
113 TEST_F(SingleScrollbarAnimationControllerThinningTest, Idle) {
114 EXPECT_FLOAT_EQ(kIdleThicknessScale,
115 scrollbar_layer_->thumb_thickness_scale_factor());
116 }
117
118 // Move the pointer near the scrollbar. Confirm it gets thick and narrow when
119 // moved away.
120 TEST_F(SingleScrollbarAnimationControllerThinningTest, MouseNear) {
121 base::TimeTicks time;
122 time += base::TimeDelta::FromSeconds(1);
123
124 // Scroll to make the scrollbars visible.
125 scrollbar_controller_->setHidden(false);
126
127 scrollbar_controller_->DidMouseMoveNear(1);
128 scrollbar_controller_->Animate(time);
129 EXPECT_FLOAT_EQ(kIdleThicknessScale,
130 scrollbar_layer_->thumb_thickness_scale_factor());
131
132 // Should animate to thickened.
133 time += kThinningDuration;
134 scrollbar_controller_->Animate(time);
135 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
136
137 // Subsequent moves within the nearness threshold should not change anything.
138 scrollbar_controller_->DidMouseMoveNear(2);
139 scrollbar_controller_->Animate(time);
140 time += base::TimeDelta::FromSeconds(10);
141 scrollbar_controller_->Animate(time);
142 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
143
144 // Now move away from bar.
145 scrollbar_controller_->DidMouseMoveNear(
146 kDefaultMouseMoveDistanceToTriggerAnimation);
147 scrollbar_controller_->Animate(time);
148 time += kThinningDuration;
149 scrollbar_controller_->Animate(time);
150 EXPECT_FLOAT_EQ(kIdleThicknessScale,
151 scrollbar_layer_->thumb_thickness_scale_factor());
152 }
153
154 // Move the pointer over the scrollbar. Make sure it gets thick that it gets
155 // thin when moved away.
156 TEST_F(SingleScrollbarAnimationControllerThinningTest, MouseOver) {
157 // Scroll to make the scrollbars visible.
158 scrollbar_controller_->setHidden(false);
159
160 base::TimeTicks time;
161 time += base::TimeDelta::FromSeconds(1);
162
163 scrollbar_controller_->DidMouseMoveNear(0);
164 scrollbar_controller_->Animate(time);
165 EXPECT_FLOAT_EQ(kIdleThicknessScale,
166 scrollbar_layer_->thumb_thickness_scale_factor());
167
168 // Should animate to thickened.
169 time += kThinningDuration;
170 scrollbar_controller_->Animate(time);
171 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
172
173 // Subsequent moves should not change anything.
174 scrollbar_controller_->DidMouseMoveNear(0);
175 scrollbar_controller_->Animate(time);
176 time += base::TimeDelta::FromSeconds(10);
177 scrollbar_controller_->Animate(time);
178 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
179
180 // Moving off the scrollbar but still withing the "near" threshold should do
181 // nothing.
182 scrollbar_controller_->DidMouseMoveNear(
183 kDefaultMouseMoveDistanceToTriggerAnimation - 1.f);
184 scrollbar_controller_->Animate(time);
185 time += base::TimeDelta::FromSeconds(10);
186 scrollbar_controller_->Animate(time);
187 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
188
189 // Now move away from bar.
190 scrollbar_controller_->DidMouseMoveNear(
191 kDefaultMouseMoveDistanceToTriggerAnimation);
192 scrollbar_controller_->Animate(time);
193 time += kThinningDuration;
194 scrollbar_controller_->Animate(time);
195 EXPECT_FLOAT_EQ(kIdleThicknessScale,
196 scrollbar_layer_->thumb_thickness_scale_factor());
197 }
198
199 // First move the pointer over the scrollbar off of it. Make sure the thinning
200 // animation kicked off in DidMouseMoveOffScrollbar gets overridden by the
201 // thickening animation in the DidMouseMoveNear call.
202 TEST_F(SingleScrollbarAnimationControllerThinningTest,
203 MouseNearThenAwayWhileAnimating) {
204 // Scroll to make the scrollbars visible.
205 scrollbar_controller_->setHidden(false);
206
207 base::TimeTicks time;
208 time += base::TimeDelta::FromSeconds(1);
209
210 scrollbar_controller_->DidMouseMoveNear(0);
211 scrollbar_controller_->Animate(time);
212 EXPECT_FLOAT_EQ(kIdleThicknessScale,
213 scrollbar_layer_->thumb_thickness_scale_factor());
214
215 // Should animate to thickened.
216 time += kThinningDuration;
217 scrollbar_controller_->Animate(time);
218 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
219
220 // This is tricky. The DidMouseLeave() is sent before the
221 // subsequent DidMouseMoveNear(), if the mouse moves in that direction.
222 // This results in the thumb thinning. We want to make sure that when the
223 // thumb starts expanding it doesn't first narrow to the idle thinness.
224 time += base::TimeDelta::FromSeconds(1);
225 scrollbar_controller_->DidMouseLeave();
226 scrollbar_controller_->Animate(time);
227 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
228
229 // Let the animation run half of the way through the thinning animation.
230 time += kThinningDuration / 2;
231 scrollbar_controller_->Animate(time);
232 EXPECT_FLOAT_EQ(1.0f - (1.0f - kIdleThicknessScale) / 2.0f,
233 scrollbar_layer_->thumb_thickness_scale_factor());
234
235 // Now we get a notification for the mouse moving over the scroller. The
236 // animation is reset to the thickening direction but we won't start
237 // thickening until the new animation catches up to the current thickness.
238 scrollbar_controller_->DidMouseMoveNear(1);
239 scrollbar_controller_->Animate(time);
240 EXPECT_FLOAT_EQ(1.0f - (1.0f - kIdleThicknessScale) / 2.0f,
241 scrollbar_layer_->thumb_thickness_scale_factor());
242
243 // Until we reach the half way point, the animation will have no effect.
244 time += kThinningDuration / 4;
245 scrollbar_controller_->Animate(time);
246 EXPECT_FLOAT_EQ(1.0f - (1.0f - kIdleThicknessScale) / 2.0f,
247 scrollbar_layer_->thumb_thickness_scale_factor());
248
249 time += kThinningDuration / 4;
250 scrollbar_controller_->Animate(time);
251 EXPECT_FLOAT_EQ(1.0f - (1.0f - kIdleThicknessScale) / 2.0f,
252 scrollbar_layer_->thumb_thickness_scale_factor());
253
254 // We're now at three quarters of the way through so it should now started
255 // thickening again.
256 time += kThinningDuration / 4;
257 scrollbar_controller_->Animate(time);
258 EXPECT_FLOAT_EQ(kIdleThicknessScale + 3 * (1.0f - kIdleThicknessScale) / 4.0f,
259 scrollbar_layer_->thumb_thickness_scale_factor());
260
261 // And all the way to the end.
262 time += kThinningDuration / 4;
263 scrollbar_controller_->Animate(time);
264 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
265 }
266
267 // First move the pointer on the scrollbar, then press it, then away.
268 // Confirm that the bar gets thick. Then mouse up. Confirm that
269 // the bar gets thin.
270 TEST_F(SingleScrollbarAnimationControllerThinningTest,
271 MouseCaptureAndReleaseOutOfBar) {
272 // Scroll to make the scrollbars visible.
273 scrollbar_controller_->setHidden(false);
274
275 base::TimeTicks time;
276 time += base::TimeDelta::FromSeconds(1);
277
278 // Move over the scrollbar.
279 scrollbar_controller_->DidMouseMoveNear(0);
280 scrollbar_controller_->Animate(time);
281 time += kThinningDuration;
282 scrollbar_controller_->Animate(time);
283 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
284
285 // Capture
286 scrollbar_controller_->DidMouseDown();
287 time += base::TimeDelta::FromSeconds(1);
288 scrollbar_controller_->Animate(time);
289 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
290
291 // Should stay thick for a while.
292 time += base::TimeDelta::FromSeconds(10);
293 scrollbar_controller_->Animate(time);
294
295 // Move outside the "near" threshold. Because the scrollbar is captured it
296 // should remain thick.
297 scrollbar_controller_->DidMouseMoveNear(
298 kDefaultMouseMoveDistanceToTriggerAnimation);
299 time += kThinningDuration;
300 scrollbar_controller_->Animate(time);
301 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
302
303 // Release.
304 scrollbar_controller_->DidMouseUp();
305
306 // Should become thin.
307 time += base::TimeDelta::FromSeconds(1);
308 scrollbar_controller_->Animate(time);
309 time += kThinningDuration;
310 scrollbar_controller_->Animate(time);
311 EXPECT_FLOAT_EQ(kIdleThicknessScale,
312 scrollbar_layer_->thumb_thickness_scale_factor());
313 }
314
315 // First move the pointer on the scrollbar, then press it, then away. Confirm
316 // that the bar gets thick. Then move point on the scrollbar and mouse up.
317 // Confirm that the bar stays thick.
318 TEST_F(SingleScrollbarAnimationControllerThinningTest,
319 MouseCaptureAndReleaseOnBar) {
320 // Scroll to make the scrollbars visible.
321 scrollbar_controller_->setHidden(false);
322
323 base::TimeTicks time;
324 time += base::TimeDelta::FromSeconds(1);
325
326 // Move over scrollbar.
327 scrollbar_controller_->DidMouseMoveNear(0);
328 scrollbar_controller_->Animate(time);
329 time += kThinningDuration;
330 scrollbar_controller_->Animate(time);
331 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
332
333 // Capture. Nothing should change.
334 scrollbar_controller_->DidMouseDown();
335 time += base::TimeDelta::FromSeconds(1);
336 scrollbar_controller_->Animate(time);
337 time += base::TimeDelta::FromSeconds(10);
338 scrollbar_controller_->Animate(time);
339 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
340
341 // Move away from scrollbar. Nothing should change.
342 scrollbar_controller_->DidMouseMoveNear(
343 kDefaultMouseMoveDistanceToTriggerAnimation);
344 time += base::TimeDelta::FromSeconds(1);
345 scrollbar_controller_->Animate(time);
346 time += base::TimeDelta::FromSeconds(10);
347 scrollbar_controller_->Animate(time);
348 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
349
350 // Move over scrollbar and release. Since we're near the scrollbar, it should
351 // remain thick.
352 scrollbar_controller_->DidMouseMoveNear(0);
353 scrollbar_controller_->DidMouseUp();
354 time += base::TimeDelta::FromSeconds(1);
355 scrollbar_controller_->Animate(time);
356 time += base::TimeDelta::FromSeconds(10);
357 scrollbar_controller_->Animate(time);
358 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
359 }
360
361 // Tests that the thickening/thinning effects are animated.
362 TEST_F(SingleScrollbarAnimationControllerThinningTest, ThicknessAnimated) {
363 // Scroll to make the scrollbars visible.
364 scrollbar_controller_->setHidden(false);
365
366 base::TimeTicks time;
367 time += base::TimeDelta::FromSeconds(1);
368
369 // Move mouse near scrollbar. Test that at half the duration time, the
370 // thickness is half way through its animation.
371 scrollbar_controller_->DidMouseMoveNear(1);
372 scrollbar_controller_->Animate(time);
373 EXPECT_FLOAT_EQ(kIdleThicknessScale,
374 scrollbar_layer_->thumb_thickness_scale_factor());
375
376 time += kThinningDuration / 2;
377 scrollbar_controller_->Animate(time);
378 EXPECT_FLOAT_EQ(kIdleThicknessScale + (1.0f - kIdleThicknessScale) / 2.0f,
379 scrollbar_layer_->thumb_thickness_scale_factor());
380
381 time += kThinningDuration / 2;
382 scrollbar_controller_->Animate(time);
383 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
384
385 // Move mouse away from scrollbar. Same check.
386 time += base::TimeDelta::FromSeconds(1);
387 scrollbar_controller_->DidMouseMoveNear(
388 kDefaultMouseMoveDistanceToTriggerAnimation);
389 scrollbar_controller_->Animate(time);
390 EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->thumb_thickness_scale_factor());
391
392 time += kThinningDuration / 2;
393 scrollbar_controller_->Animate(time);
394 EXPECT_FLOAT_EQ(1.0f - (1.0f - kIdleThicknessScale) / 2.0f,
395 scrollbar_layer_->thumb_thickness_scale_factor());
396
397 time += kThinningDuration / 2;
398 scrollbar_controller_->Animate(time);
399 EXPECT_FLOAT_EQ(kIdleThicknessScale,
400 scrollbar_layer_->thumb_thickness_scale_factor());
401 }
402
403 } // namespace
404 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698