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/input/top_controls_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/time/time.h"
9 #include "cc/input/top_controls_manager_client.h"
10 #include "cc/layers/layer_impl.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_layer_tree_host_impl.h"
13 #include "cc/test/test_shared_bitmap_manager.h"
14 #include "cc/trees/layer_tree_impl.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gfx/frame_time.h"
17 #include "ui/gfx/geometry/vector2d_f.h"
22 static const float kTopControlsHeight
= 100;
24 class MockTopControlsManagerClient
: public TopControlsManagerClient
{
26 MockTopControlsManagerClient(float top_controls_show_threshold
,
27 float top_controls_hide_threshold
)
28 : host_impl_(&proxy_
, &shared_bitmap_manager_
),
29 redraw_needed_(false),
30 update_draw_properties_needed_(false),
31 top_controls_top_offset_(0.f
),
32 top_controls_show_threshold_(top_controls_show_threshold
),
33 top_controls_hide_threshold_(top_controls_hide_threshold
) {
34 active_tree_
= LayerTreeImpl::create(&host_impl_
);
35 root_scroll_layer_
= LayerImpl::Create(active_tree_
.get(), 1);
38 ~MockTopControlsManagerClient() override
{}
40 void DidChangeTopControlsPosition() override
{
41 redraw_needed_
= true;
42 update_draw_properties_needed_
= true;
45 bool HaveRootScrollLayer() const override
{ return true; }
47 void SetControlsTopOffset(float offset
) override
{
48 top_controls_top_offset_
= offset
;
51 float ControlsTopOffset() const override
{ return top_controls_top_offset_
; }
53 LayerImpl
* rootScrollLayer() {
54 return root_scroll_layer_
.get();
57 TopControlsManager
* manager() {
59 manager_
= TopControlsManager::Create(this,
61 top_controls_show_threshold_
,
62 top_controls_hide_threshold_
);
64 return manager_
.get();
69 TestSharedBitmapManager shared_bitmap_manager_
;
70 FakeLayerTreeHostImpl host_impl_
;
71 scoped_ptr
<LayerTreeImpl
> active_tree_
;
72 scoped_ptr
<LayerImpl
> root_scroll_layer_
;
73 scoped_ptr
<TopControlsManager
> manager_
;
75 bool update_draw_properties_needed_
;
77 float top_controls_top_offset_
;
78 float top_controls_show_threshold_
;
79 float top_controls_hide_threshold_
;
82 TEST(TopControlsManagerTest
, EnsureScrollThresholdApplied
) {
83 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
84 TopControlsManager
* manager
= client
.manager();
86 manager
->ScrollBegin();
88 // Scroll down to hide the controls entirely.
89 manager
->ScrollBy(gfx::Vector2dF(0.f
, 30.f
));
90 EXPECT_EQ(-30.f
, manager
->ControlsTopOffset());
92 manager
->ScrollBy(gfx::Vector2dF(0.f
, 30.f
));
93 EXPECT_EQ(-60.f
, manager
->ControlsTopOffset());
95 manager
->ScrollBy(gfx::Vector2dF(0.f
, 100.f
));
96 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
98 // Scroll back up a bit and ensure the controls don't move until we cross
100 manager
->ScrollBy(gfx::Vector2dF(0.f
, -10.f
));
101 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
103 manager
->ScrollBy(gfx::Vector2dF(0.f
, -50.f
));
104 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
106 // After hitting the threshold, further scrolling up should result in the top
108 manager
->ScrollBy(gfx::Vector2dF(0.f
, -10.f
));
109 EXPECT_EQ(-90.f
, manager
->ControlsTopOffset());
111 manager
->ScrollBy(gfx::Vector2dF(0.f
, -50.f
));
112 EXPECT_EQ(-40.f
, manager
->ControlsTopOffset());
114 // Reset the scroll threshold by going further up the page than the initial
116 manager
->ScrollBy(gfx::Vector2dF(0.f
, -100.f
));
117 EXPECT_EQ(0.f
, manager
->ControlsTopOffset());
119 // See that scrolling down the page now will result in the controls hiding.
120 manager
->ScrollBy(gfx::Vector2dF(0.f
, 20.f
));
121 EXPECT_EQ(-20.f
, manager
->ControlsTopOffset());
123 manager
->ScrollEnd();
126 TEST(TopControlsManagerTest
, PartialShownHideAnimation
) {
127 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
128 TopControlsManager
* manager
= client
.manager();
129 manager
->ScrollBegin();
130 manager
->ScrollBy(gfx::Vector2dF(0.f
, 300.f
));
131 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
132 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
133 manager
->ScrollEnd();
135 manager
->ScrollBegin();
136 manager
->ScrollBy(gfx::Vector2dF(0.f
, -15.f
));
137 EXPECT_EQ(-85.f
, manager
->ControlsTopOffset());
138 EXPECT_EQ(15.f
, manager
->ContentTopOffset());
139 manager
->ScrollEnd();
141 EXPECT_TRUE(manager
->animation());
143 base::TimeTicks time
= gfx::FrameTime::Now();
144 float previous_offset
= manager
->ControlsTopOffset();
145 while (manager
->animation()) {
146 time
= base::TimeDelta::FromMicroseconds(100) + time
;
147 manager
->Animate(time
);
148 EXPECT_LT(manager
->ControlsTopOffset(), previous_offset
);
149 previous_offset
= manager
->ControlsTopOffset();
151 EXPECT_FALSE(manager
->animation());
152 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
153 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
156 TEST(TopControlsManagerTest
, PartialShownShowAnimation
) {
157 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
158 TopControlsManager
* manager
= client
.manager();
159 manager
->ScrollBegin();
160 manager
->ScrollBy(gfx::Vector2dF(0.f
, 300.f
));
161 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
162 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
163 manager
->ScrollEnd();
165 manager
->ScrollBegin();
166 manager
->ScrollBy(gfx::Vector2dF(0.f
, -70.f
));
167 EXPECT_EQ(-30.f
, manager
->ControlsTopOffset());
168 EXPECT_EQ(70.f
, manager
->ContentTopOffset());
169 manager
->ScrollEnd();
171 EXPECT_TRUE(manager
->animation());
173 base::TimeTicks time
= gfx::FrameTime::Now();
174 float previous_offset
= manager
->ControlsTopOffset();
175 while (manager
->animation()) {
176 time
= base::TimeDelta::FromMicroseconds(100) + time
;
177 manager
->Animate(time
);
178 EXPECT_GT(manager
->ControlsTopOffset(), previous_offset
);
179 previous_offset
= manager
->ControlsTopOffset();
181 EXPECT_FALSE(manager
->animation());
182 EXPECT_EQ(0.f
, manager
->ControlsTopOffset());
183 EXPECT_EQ(100.f
, manager
->ContentTopOffset());
186 TEST(TopControlsManagerTest
, PartialHiddenWithAmbiguousThresholdShows
) {
187 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
188 TopControlsManager
* manager
= client
.manager();
190 manager
->ScrollBegin();
192 manager
->ScrollBy(gfx::Vector2dF(0.f
, 20.f
));
193 EXPECT_EQ(-20.f
, manager
->ControlsTopOffset());
194 EXPECT_EQ(80.f
, manager
->ContentTopOffset());
196 manager
->ScrollEnd();
197 EXPECT_TRUE(manager
->animation());
199 base::TimeTicks time
= gfx::FrameTime::Now();
200 float previous_offset
= manager
->ControlsTopOffset();
201 while (manager
->animation()) {
202 time
= base::TimeDelta::FromMicroseconds(100) + time
;
203 manager
->Animate(time
);
204 EXPECT_GT(manager
->ControlsTopOffset(), previous_offset
);
205 previous_offset
= manager
->ControlsTopOffset();
207 EXPECT_FALSE(manager
->animation());
208 EXPECT_EQ(0.f
, manager
->ControlsTopOffset());
209 EXPECT_EQ(100.f
, manager
->ContentTopOffset());
212 TEST(TopControlsManagerTest
, PartialHiddenWithAmbiguousThresholdHides
) {
213 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
214 TopControlsManager
* manager
= client
.manager();
216 manager
->ScrollBegin();
218 manager
->ScrollBy(gfx::Vector2dF(0.f
, 30.f
));
219 EXPECT_EQ(-30.f
, manager
->ControlsTopOffset());
220 EXPECT_EQ(70.f
, manager
->ContentTopOffset());
222 manager
->ScrollEnd();
223 EXPECT_TRUE(manager
->animation());
225 base::TimeTicks time
= gfx::FrameTime::Now();
226 float previous_offset
= manager
->ControlsTopOffset();
227 while (manager
->animation()) {
228 time
= base::TimeDelta::FromMicroseconds(100) + time
;
229 manager
->Animate(time
);
230 EXPECT_LT(manager
->ControlsTopOffset(), previous_offset
);
231 previous_offset
= manager
->ControlsTopOffset();
233 EXPECT_FALSE(manager
->animation());
234 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
235 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
238 TEST(TopControlsManagerTest
, PartialShownWithAmbiguousThresholdHides
) {
239 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
240 TopControlsManager
* manager
= client
.manager();
242 manager
->ScrollBy(gfx::Vector2dF(0.f
, 200.f
));
243 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
244 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
246 manager
->ScrollBegin();
248 manager
->ScrollBy(gfx::Vector2dF(0.f
, -20.f
));
249 EXPECT_EQ(-80.f
, manager
->ControlsTopOffset());
250 EXPECT_EQ(20.f
, manager
->ContentTopOffset());
252 manager
->ScrollEnd();
253 EXPECT_TRUE(manager
->animation());
255 base::TimeTicks time
= gfx::FrameTime::Now();
256 float previous_offset
= manager
->ControlsTopOffset();
257 while (manager
->animation()) {
258 time
= base::TimeDelta::FromMicroseconds(100) + time
;
259 manager
->Animate(time
);
260 EXPECT_LT(manager
->ControlsTopOffset(), previous_offset
);
261 previous_offset
= manager
->ControlsTopOffset();
263 EXPECT_FALSE(manager
->animation());
264 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
265 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
268 TEST(TopControlsManagerTest
, PartialShownWithAmbiguousThresholdShows
) {
269 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
270 TopControlsManager
* manager
= client
.manager();
272 manager
->ScrollBy(gfx::Vector2dF(0.f
, 200.f
));
273 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
274 EXPECT_EQ(0.f
, manager
->ContentTopOffset());
276 manager
->ScrollBegin();
278 manager
->ScrollBy(gfx::Vector2dF(0.f
, -30.f
));
279 EXPECT_EQ(-70.f
, manager
->ControlsTopOffset());
280 EXPECT_EQ(30.f
, manager
->ContentTopOffset());
282 manager
->ScrollEnd();
283 EXPECT_TRUE(manager
->animation());
285 base::TimeTicks time
= gfx::FrameTime::Now();
286 float previous_offset
= manager
->ControlsTopOffset();
287 while (manager
->animation()) {
288 time
= base::TimeDelta::FromMicroseconds(100) + time
;
289 manager
->Animate(time
);
290 EXPECT_GT(manager
->ControlsTopOffset(), previous_offset
);
291 previous_offset
= manager
->ControlsTopOffset();
293 EXPECT_FALSE(manager
->animation());
294 EXPECT_EQ(0.f
, manager
->ControlsTopOffset());
295 EXPECT_EQ(100.f
, manager
->ContentTopOffset());
298 TEST(TopControlsManagerTest
, PinchIgnoresScroll
) {
299 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
300 TopControlsManager
* manager
= client
.manager();
302 // Hide the controls.
303 manager
->ScrollBegin();
304 EXPECT_EQ(0.f
, manager
->ControlsTopOffset());
306 manager
->ScrollBy(gfx::Vector2dF(0.f
, 300.f
));
307 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
309 manager
->PinchBegin();
310 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
312 // Scrolls are ignored during pinch.
313 manager
->ScrollBy(gfx::Vector2dF(0.f
, -15.f
));
314 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
316 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
318 // Scrolls should no long be ignored.
319 manager
->ScrollBy(gfx::Vector2dF(0.f
, -15.f
));
320 EXPECT_EQ(-85.f
, manager
->ControlsTopOffset());
321 EXPECT_EQ(15.f
, manager
->ContentTopOffset());
322 manager
->ScrollEnd();
324 EXPECT_TRUE(manager
->animation());
327 TEST(TopControlsManagerTest
, PinchBeginStartsAnimationIfNecessary
) {
328 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
329 TopControlsManager
* manager
= client
.manager();
331 manager
->ScrollBegin();
332 manager
->ScrollBy(gfx::Vector2dF(0.f
, 300.f
));
333 EXPECT_EQ(-100.f
, manager
->ControlsTopOffset());
335 manager
->PinchBegin();
336 EXPECT_FALSE(manager
->animation());
339 EXPECT_FALSE(manager
->animation());
341 manager
->ScrollBy(gfx::Vector2dF(0.f
, -15.f
));
342 EXPECT_EQ(-85.f
, manager
->ControlsTopOffset());
343 EXPECT_EQ(15.f
, manager
->ContentTopOffset());
345 manager
->PinchBegin();
346 EXPECT_TRUE(manager
->animation());
348 base::TimeTicks time
= base::TimeTicks::Now();
349 float previous_offset
= manager
->ControlsTopOffset();
350 while (manager
->animation()) {
351 time
= base::TimeDelta::FromMicroseconds(100) + time
;
352 manager
->Animate(time
);
353 EXPECT_LT(manager
->ControlsTopOffset(), previous_offset
);
354 previous_offset
= manager
->ControlsTopOffset();
356 EXPECT_FALSE(manager
->animation());
359 EXPECT_FALSE(manager
->animation());
361 manager
->ScrollBy(gfx::Vector2dF(0.f
, -55.f
));
362 EXPECT_EQ(-45.f
, manager
->ControlsTopOffset());
363 EXPECT_EQ(55.f
, manager
->ContentTopOffset());
364 EXPECT_FALSE(manager
->animation());
366 manager
->ScrollEnd();
367 EXPECT_TRUE(manager
->animation());
369 time
= base::TimeTicks::Now();
370 previous_offset
= manager
->ControlsTopOffset();
371 while (manager
->animation()) {
372 time
= base::TimeDelta::FromMicroseconds(100) + time
;
373 manager
->Animate(time
);
374 EXPECT_GT(manager
->ControlsTopOffset(), previous_offset
);
375 previous_offset
= manager
->ControlsTopOffset();
377 EXPECT_FALSE(manager
->animation());
378 EXPECT_EQ(0.f
, manager
->ControlsTopOffset());