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"
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/trees/layer_tree_impl.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/vector2d_f.h"
20 static const float kTopControlsHeight
= 100;
22 class MockTopControlsManagerClient
: public TopControlsManagerClient
{
24 MockTopControlsManagerClient(float top_controls_show_threshold
,
25 float top_controls_hide_threshold
)
26 : host_impl_(&proxy_
),
27 redraw_needed_(false),
28 update_draw_properties_needed_(false),
29 top_controls_show_threshold_(top_controls_show_threshold
),
30 top_controls_hide_threshold_(top_controls_hide_threshold
) {
31 active_tree_
= LayerTreeImpl::create(&host_impl_
);
32 root_scroll_layer_
= LayerImpl::Create(active_tree_
.get(), 1);
35 virtual ~MockTopControlsManagerClient() {}
37 virtual void DidChangeTopControlsPosition() OVERRIDE
{
38 redraw_needed_
= true;
39 update_draw_properties_needed_
= true;
42 virtual bool HaveRootScrollLayer() const OVERRIDE
{
46 LayerImpl
* rootScrollLayer() {
47 return root_scroll_layer_
.get();
50 TopControlsManager
* manager() {
52 manager_
= TopControlsManager::Create(this,
54 top_controls_show_threshold_
,
55 top_controls_hide_threshold_
);
57 return manager_
.get();
62 FakeLayerTreeHostImpl host_impl_
;
63 scoped_ptr
<LayerTreeImpl
> active_tree_
;
64 scoped_ptr
<LayerImpl
> root_scroll_layer_
;
65 scoped_ptr
<TopControlsManager
> manager_
;
67 bool update_draw_properties_needed_
;
69 float top_controls_show_threshold_
;
70 float top_controls_hide_threshold_
;
73 TEST(TopControlsManagerTest
, EnsureScrollThresholdApplied
) {
74 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
75 TopControlsManager
* manager
= client
.manager();
77 manager
->ScrollBegin();
79 // Scroll down to hide the controls entirely.
80 manager
->ScrollBy(gfx::Vector2dF(0.f
, 30.f
));
81 EXPECT_EQ(-30.f
, manager
->controls_top_offset());
83 manager
->ScrollBy(gfx::Vector2dF(0.f
, 30.f
));
84 EXPECT_EQ(-60.f
, manager
->controls_top_offset());
86 manager
->ScrollBy(gfx::Vector2dF(0.f
, 100.f
));
87 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
89 // Scroll back up a bit and ensure the controls don't move until we cross
91 manager
->ScrollBy(gfx::Vector2dF(0.f
, -10.f
));
92 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
94 manager
->ScrollBy(gfx::Vector2dF(0.f
, -50.f
));
95 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
97 // After hitting the threshold, further scrolling up should result in the top
99 manager
->ScrollBy(gfx::Vector2dF(0.f
, -10.f
));
100 EXPECT_EQ(-90.f
, manager
->controls_top_offset());
102 manager
->ScrollBy(gfx::Vector2dF(0.f
, -50.f
));
103 EXPECT_EQ(-40.f
, manager
->controls_top_offset());
105 // Reset the scroll threshold by going further up the page than the initial
107 manager
->ScrollBy(gfx::Vector2dF(0.f
, -100.f
));
108 EXPECT_EQ(0.f
, manager
->controls_top_offset());
110 // See that scrolling down the page now will result in the controls hiding.
111 manager
->ScrollBy(gfx::Vector2dF(0.f
, 20.f
));
112 EXPECT_EQ(-20.f
, manager
->controls_top_offset());
114 manager
->ScrollEnd();
117 TEST(TopControlsManagerTest
, PartialShownHideAnimation
) {
118 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
119 TopControlsManager
* manager
= client
.manager();
120 manager
->ScrollBegin();
121 manager
->ScrollBy(gfx::Vector2dF(0.f
, 300.f
));
122 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
123 EXPECT_EQ(0.f
, manager
->content_top_offset());
124 manager
->ScrollEnd();
126 manager
->ScrollBegin();
127 manager
->ScrollBy(gfx::Vector2dF(0.f
, -15.f
));
128 EXPECT_EQ(-85.f
, manager
->controls_top_offset());
129 EXPECT_EQ(15.f
, manager
->content_top_offset());
130 manager
->ScrollEnd();
132 EXPECT_TRUE(manager
->animation());
134 base::TimeTicks time
= base::TimeTicks::Now();
135 float previous_offset
= manager
->controls_top_offset();
136 while (manager
->animation()) {
137 time
= base::TimeDelta::FromMicroseconds(100) + time
;
138 manager
->Animate(time
);
139 EXPECT_LT(manager
->controls_top_offset(), previous_offset
);
140 previous_offset
= manager
->controls_top_offset();
142 EXPECT_FALSE(manager
->animation());
143 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
144 EXPECT_EQ(0.f
, manager
->content_top_offset());
147 TEST(TopControlsManagerTest
, PartialShownShowAnimation
) {
148 MockTopControlsManagerClient
client(0.5f
, 0.5f
);
149 TopControlsManager
* manager
= client
.manager();
150 manager
->ScrollBegin();
151 manager
->ScrollBy(gfx::Vector2dF(0.f
, 300.f
));
152 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
153 EXPECT_EQ(0.f
, manager
->content_top_offset());
154 manager
->ScrollEnd();
156 manager
->ScrollBegin();
157 manager
->ScrollBy(gfx::Vector2dF(0.f
, -70.f
));
158 EXPECT_EQ(-30.f
, manager
->controls_top_offset());
159 EXPECT_EQ(70.f
, manager
->content_top_offset());
160 manager
->ScrollEnd();
162 EXPECT_TRUE(manager
->animation());
164 base::TimeTicks time
= base::TimeTicks::Now();
165 float previous_offset
= manager
->controls_top_offset();
166 while (manager
->animation()) {
167 time
= base::TimeDelta::FromMicroseconds(100) + time
;
168 manager
->Animate(time
);
169 EXPECT_GT(manager
->controls_top_offset(), previous_offset
);
170 previous_offset
= manager
->controls_top_offset();
172 EXPECT_FALSE(manager
->animation());
173 EXPECT_EQ(0.f
, manager
->controls_top_offset());
174 EXPECT_EQ(100.f
, manager
->content_top_offset());
177 TEST(TopControlsManagerTest
, PartialHiddenWithAmbiguousThresholdShows
) {
178 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
179 TopControlsManager
* manager
= client
.manager();
181 manager
->ScrollBegin();
183 manager
->ScrollBy(gfx::Vector2dF(0.f
, 20.f
));
184 EXPECT_EQ(-20.f
, manager
->controls_top_offset());
185 EXPECT_EQ(80.f
, manager
->content_top_offset());
187 manager
->ScrollEnd();
188 EXPECT_TRUE(manager
->animation());
190 base::TimeTicks time
= base::TimeTicks::Now();
191 float previous_offset
= manager
->controls_top_offset();
192 while (manager
->animation()) {
193 time
= base::TimeDelta::FromMicroseconds(100) + time
;
194 manager
->Animate(time
);
195 EXPECT_GT(manager
->controls_top_offset(), previous_offset
);
196 previous_offset
= manager
->controls_top_offset();
198 EXPECT_FALSE(manager
->animation());
199 EXPECT_EQ(0.f
, manager
->controls_top_offset());
200 EXPECT_EQ(100.f
, manager
->content_top_offset());
203 TEST(TopControlsManagerTest
, PartialHiddenWithAmbiguousThresholdHides
) {
204 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
205 TopControlsManager
* manager
= client
.manager();
207 manager
->ScrollBegin();
209 manager
->ScrollBy(gfx::Vector2dF(0.f
, 30.f
));
210 EXPECT_EQ(-30.f
, manager
->controls_top_offset());
211 EXPECT_EQ(70.f
, manager
->content_top_offset());
213 manager
->ScrollEnd();
214 EXPECT_TRUE(manager
->animation());
216 base::TimeTicks time
= base::TimeTicks::Now();
217 float previous_offset
= manager
->controls_top_offset();
218 while (manager
->animation()) {
219 time
= base::TimeDelta::FromMicroseconds(100) + time
;
220 manager
->Animate(time
);
221 EXPECT_LT(manager
->controls_top_offset(), previous_offset
);
222 previous_offset
= manager
->controls_top_offset();
224 EXPECT_FALSE(manager
->animation());
225 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
226 EXPECT_EQ(0.f
, manager
->content_top_offset());
229 TEST(TopControlsManagerTest
, PartialShownWithAmbiguousThresholdHides
) {
230 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
231 TopControlsManager
* manager
= client
.manager();
233 manager
->ScrollBy(gfx::Vector2dF(0.f
, 200.f
));
234 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
235 EXPECT_EQ(0.f
, manager
->content_top_offset());
237 manager
->ScrollBegin();
239 manager
->ScrollBy(gfx::Vector2dF(0.f
, -20.f
));
240 EXPECT_EQ(-80.f
, manager
->controls_top_offset());
241 EXPECT_EQ(20.f
, manager
->content_top_offset());
243 manager
->ScrollEnd();
244 EXPECT_TRUE(manager
->animation());
246 base::TimeTicks time
= base::TimeTicks::Now();
247 float previous_offset
= manager
->controls_top_offset();
248 while (manager
->animation()) {
249 time
= base::TimeDelta::FromMicroseconds(100) + time
;
250 manager
->Animate(time
);
251 EXPECT_LT(manager
->controls_top_offset(), previous_offset
);
252 previous_offset
= manager
->controls_top_offset();
254 EXPECT_FALSE(manager
->animation());
255 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
256 EXPECT_EQ(0.f
, manager
->content_top_offset());
259 TEST(TopControlsManagerTest
, PartialShownWithAmbiguousThresholdShows
) {
260 MockTopControlsManagerClient
client(0.25f
, 0.25f
);
261 TopControlsManager
* manager
= client
.manager();
263 manager
->ScrollBy(gfx::Vector2dF(0.f
, 200.f
));
264 EXPECT_EQ(-100.f
, manager
->controls_top_offset());
265 EXPECT_EQ(0.f
, manager
->content_top_offset());
267 manager
->ScrollBegin();
269 manager
->ScrollBy(gfx::Vector2dF(0.f
, -30.f
));
270 EXPECT_EQ(-70.f
, manager
->controls_top_offset());
271 EXPECT_EQ(30.f
, manager
->content_top_offset());
273 manager
->ScrollEnd();
274 EXPECT_TRUE(manager
->animation());
276 base::TimeTicks time
= base::TimeTicks::Now();
277 float previous_offset
= manager
->controls_top_offset();
278 while (manager
->animation()) {
279 time
= base::TimeDelta::FromMicroseconds(100) + time
;
280 manager
->Animate(time
);
281 EXPECT_GT(manager
->controls_top_offset(), previous_offset
);
282 previous_offset
= manager
->controls_top_offset();
284 EXPECT_FALSE(manager
->animation());
285 EXPECT_EQ(0.f
, manager
->controls_top_offset());
286 EXPECT_EQ(100.f
, manager
->content_top_offset());