1 // Copyright 2011 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/layers/layer_impl.h"
7 #include "cc/output/filter_operation.h"
8 #include "cc/output/filter_operations.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "cc/trees/layer_tree_impl.h"
14 #include "cc/trees/single_thread_proxy.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
22 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
23 root->ResetAllChangeTrackingForSubtree(); \
25 EXPECT_TRUE(root->LayerPropertyChanged()); \
26 EXPECT_TRUE(child->LayerPropertyChanged()); \
27 EXPECT_TRUE(grand_child->LayerPropertyChanged());
29 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \
30 root->ResetAllChangeTrackingForSubtree(); \
32 EXPECT_FALSE(root->LayerPropertyChanged()); \
33 EXPECT_FALSE(child->LayerPropertyChanged()); \
34 EXPECT_FALSE(grand_child->LayerPropertyChanged());
36 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \
37 root->ResetAllChangeTrackingForSubtree(); \
39 EXPECT_TRUE(root->LayerPropertyChanged()); \
40 EXPECT_FALSE(child->LayerPropertyChanged()); \
41 EXPECT_FALSE(grand_child->LayerPropertyChanged());
43 #define EXECUTE_AND_VERIFY_ONLY_DESCENDANTS_CHANGED(code_to_test) \
44 root->ResetAllChangeTrackingForSubtree(); \
46 EXPECT_FALSE(root->LayerPropertyChanged()); \
47 EXPECT_TRUE(child->LayerPropertyChanged()); \
48 EXPECT_TRUE(grand_child->LayerPropertyChanged());
50 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
51 root->ResetAllChangeTrackingForSubtree(); \
52 host_impl.ForcePrepareToDraw(); \
53 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
55 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties());
57 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
58 root->ResetAllChangeTrackingForSubtree(); \
59 host_impl.ForcePrepareToDraw(); \
60 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
62 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties());
64 TEST(LayerImplTest
, VerifyLayerChangesAreTrackedProperly
) {
66 // This test checks that layerPropertyChanged() has the correct behavior.
69 // The constructor on this will fake that we are on the correct thread.
70 // Create a simple LayerImpl tree:
72 FakeLayerTreeHostImpl
host_impl(&proxy
);
73 EXPECT_TRUE(host_impl
.InitializeRenderer(CreateFakeOutputSurface()));
74 scoped_ptr
<LayerImpl
> root
= LayerImpl::Create(host_impl
.active_tree(), 1);
75 root
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 2));
76 LayerImpl
* child
= root
->children()[0];
77 child
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 3));
78 LayerImpl
* grand_child
= child
->children()[0];
80 root
->SetScrollable(true);
82 // Adding children is an internal operation and should not mark layers as
84 EXPECT_FALSE(root
->LayerPropertyChanged());
85 EXPECT_FALSE(child
->LayerPropertyChanged());
86 EXPECT_FALSE(grand_child
->LayerPropertyChanged());
88 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
89 float arbitrary_number
= 0.352f
;
90 gfx::Size arbitrary_size
= gfx::Size(111, 222);
91 gfx::Point arbitrary_point
= gfx::Point(333, 444);
92 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
93 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
94 gfx::RectF arbitrary_rect_f
=
95 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
96 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
97 gfx::Transform arbitrary_transform
;
98 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
99 FilterOperations arbitrary_filters
;
100 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
101 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
103 // These properties are internal, and should not be considered "change" when
105 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
106 root
->set_update_rect(arbitrary_rect_f
));
107 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
108 root
->SetMaxScrollOffset(arbitrary_vector2d
));
110 // Changing these properties affects the entire subtree of layers.
111 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetAnchorPoint(arbitrary_point_f
));
112 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetAnchorPointZ(arbitrary_number
));
113 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(arbitrary_filters
));
114 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(FilterOperations()));
115 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
116 root
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 4)));
117 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetMasksToBounds(true));
118 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetContentsOpaque(true));
119 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
120 root
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 5)));
121 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetPosition(arbitrary_point_f
));
122 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetPreserves3d(true));
123 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
124 root
->SetDoubleSided(false)); // constructor initializes it to "true".
125 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->ScrollBy(arbitrary_vector2d
));
126 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollDelta(gfx::Vector2d()));
127 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollOffset(arbitrary_vector2d
));
128 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetHideLayerAndSubtree(true));
129 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetOpacity(arbitrary_number
));
130 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBlendMode(arbitrary_blend_mode
));
131 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetTransform(arbitrary_transform
));
133 // Changing these properties only affects the layer itself.
134 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetContentBounds(arbitrary_size
));
135 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
136 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
137 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetDrawsContent(true));
138 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
139 root
->SetBackgroundColor(arbitrary_color
));
140 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
141 root
->SetBackgroundFilters(arbitrary_filters
));
143 // Changing these properties affects all layer's descendants,
144 // but not the layer itself.
145 EXECUTE_AND_VERIFY_ONLY_DESCENDANTS_CHANGED(
146 root
->SetSublayerTransform(arbitrary_transform
));
148 // Special case: check that SetBounds changes behavior depending on
150 root
->SetMasksToBounds(false);
151 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(gfx::Size(135, 246)));
152 root
->SetMasksToBounds(true);
153 // Should be a different size than previous call, to ensure it marks tree
155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBounds(arbitrary_size
));
157 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
158 root
->SetIsRootForIsolatedGroup(true));
160 // After setting all these properties already, setting to the exact same
161 // values again should not cause any change.
162 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
163 root
->SetAnchorPoint(arbitrary_point_f
));
164 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
165 root
->SetAnchorPointZ(arbitrary_number
));
166 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetMasksToBounds(true));
167 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
168 root
->SetPosition(arbitrary_point_f
));
169 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetPreserves3d(true));
170 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
171 root
->SetTransform(arbitrary_transform
));
172 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
173 root
->SetDoubleSided(false)); // constructor initializes it to "true".
174 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
175 root
->SetScrollDelta(gfx::Vector2d()));
176 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
177 root
->SetScrollOffset(arbitrary_vector2d
));
178 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
179 root
->SetContentBounds(arbitrary_size
));
180 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
181 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
182 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetContentsOpaque(true));
183 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetOpacity(arbitrary_number
));
184 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
185 root
->SetBlendMode(arbitrary_blend_mode
));
186 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
187 root
->SetIsRootForIsolatedGroup(true));
188 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetDrawsContent(true));
189 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
190 root
->SetSublayerTransform(arbitrary_transform
));
191 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetBounds(arbitrary_size
));
194 TEST(LayerImplTest
, VerifyNeedsUpdateDrawProperties
) {
196 FakeLayerTreeHostImpl
host_impl(&proxy
);
197 EXPECT_TRUE(host_impl
.InitializeRenderer(CreateFakeOutputSurface()));
198 scoped_ptr
<LayerImpl
> root
= LayerImpl::Create(host_impl
.active_tree(), 1);
199 root
->SetScrollable(true);
201 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
202 float arbitrary_number
= 0.352f
;
203 gfx::Size arbitrary_size
= gfx::Size(111, 222);
204 gfx::Point arbitrary_point
= gfx::Point(333, 444);
205 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
206 gfx::Vector2d large_vector2d
= gfx::Vector2d(1000, 1000);
207 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
208 gfx::RectF arbitrary_rect_f
=
209 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
210 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
211 gfx::Transform arbitrary_transform
;
212 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
213 FilterOperations arbitrary_filters
;
214 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
215 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
217 // Related filter functions.
218 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetFilters(arbitrary_filters
));
219 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetFilters(arbitrary_filters
));
220 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetFilters(FilterOperations()));
221 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetFilters(arbitrary_filters
));
223 // Related scrolling functions.
224 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetMaxScrollOffset(large_vector2d
));
225 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
226 root
->SetMaxScrollOffset(large_vector2d
));
227 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->ScrollBy(arbitrary_vector2d
));
228 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->ScrollBy(gfx::Vector2d()));
229 root
->SetScrollDelta(gfx::Vector2d(0, 0));
230 host_impl
.ForcePrepareToDraw();
231 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetScrollDelta(arbitrary_vector2d
));
232 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
233 root
->SetScrollDelta(arbitrary_vector2d
));
234 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
235 root
->SetScrollOffset(arbitrary_vector2d
));
236 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
237 root
->SetScrollOffset(arbitrary_vector2d
));
239 // Unrelated functions, always set to new values, always set needs update.
240 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetAnchorPointZ(arbitrary_number
));
241 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
242 root
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 4)));
243 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetMasksToBounds(true));
244 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetContentsOpaque(true));
245 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
246 root
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 5)));
247 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetPosition(arbitrary_point_f
));
248 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetPreserves3d(true));
249 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
250 root
->SetDoubleSided(false)); // constructor initializes it to "true".
251 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetContentBounds(arbitrary_size
));
252 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
253 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
254 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetDrawsContent(true));
255 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
256 root
->SetBackgroundColor(arbitrary_color
));
257 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
258 root
->SetBackgroundFilters(arbitrary_filters
));
259 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetOpacity(arbitrary_number
));
260 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetBlendMode(arbitrary_blend_mode
));
261 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetTransform(arbitrary_transform
));
262 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
263 root
->SetSublayerTransform(arbitrary_transform
));
264 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetBounds(arbitrary_size
));
266 // Unrelated functions, set to the same values, no needs update.
267 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
268 root
->SetAnchorPointZ(arbitrary_number
));
269 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetIsRootForIsolatedGroup(true));
270 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetFilters(arbitrary_filters
));
271 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetMasksToBounds(true));
272 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetContentsOpaque(true));
273 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetPosition(arbitrary_point_f
));
274 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetPreserves3d(true));
275 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
276 root
->SetDoubleSided(false)); // constructor initializes it to "true".
277 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
278 root
->SetContentBounds(arbitrary_size
));
279 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
280 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
281 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetDrawsContent(true));
282 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
283 root
->SetBackgroundColor(arbitrary_color
));
284 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
285 root
->SetBackgroundFilters(arbitrary_filters
));
286 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetOpacity(arbitrary_number
));
287 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
288 root
->SetBlendMode(arbitrary_blend_mode
));
289 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetIsRootForIsolatedGroup(true));
290 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
291 root
->SetTransform(arbitrary_transform
));
292 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
293 root
->SetSublayerTransform(arbitrary_transform
));
294 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root
->SetBounds(arbitrary_size
));
297 TEST(LayerImplTest
, SafeOpaqueBackgroundColor
) {
299 FakeLayerTreeHostImpl
host_impl(&proxy
);
300 EXPECT_TRUE(host_impl
.InitializeRenderer(CreateFakeOutputSurface()));
301 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
303 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
304 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
305 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
306 layer
->SetContentsOpaque(!!contents_opaque
);
307 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
308 : SK_ColorTRANSPARENT
);
309 host_impl
.active_tree()->set_background_color(
310 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
312 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
313 if (contents_opaque
) {
314 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
315 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
316 << host_opaque
<< "\n";
318 EXPECT_NE(SkColorGetA(safe_color
), 255u)
319 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
320 << host_opaque
<< "\n";
327 class LayerImplScrollTest
: public testing::Test
{
329 LayerImplScrollTest() : host_impl_(&proxy_
), root_id_(7) {
330 host_impl_
.active_tree()
331 ->SetRootLayer(LayerImpl::Create(host_impl_
.active_tree(), root_id_
));
332 host_impl_
.active_tree()->root_layer()->SetScrollable(true);
335 LayerImpl
* layer() { return host_impl_
.active_tree()->root_layer(); }
338 FakeImplProxy proxy_
;
339 FakeLayerTreeHostImpl host_impl_
;
343 TEST_F(LayerImplScrollTest
, ScrollByWithZeroOffset
) {
344 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
345 // offset is bounded by the range [0, max scroll offset].
346 gfx::Vector2d
max_scroll_offset(50, 80);
347 layer()->SetMaxScrollOffset(max_scroll_offset
);
349 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset());
350 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
351 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
353 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
354 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
356 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
357 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
359 layer()->ScrollBy(gfx::Vector2dF(100, -100));
360 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
362 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
363 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
366 TEST_F(LayerImplScrollTest
, ScrollByWithNonZeroOffset
) {
367 gfx::Vector2d
max_scroll_offset(50, 80);
368 gfx::Vector2d
scroll_offset(10, 5);
369 layer()->SetMaxScrollOffset(max_scroll_offset
);
370 layer()->SetScrollOffset(scroll_offset
);
372 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
373 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
374 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
376 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
377 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
379 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
380 layer()->TotalScrollOffset());
381 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
383 layer()->ScrollBy(gfx::Vector2dF(100, -100));
384 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
386 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
387 layer()->TotalScrollOffset());
388 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
391 class ScrollDelegateIgnore
: public LayerScrollOffsetDelegate
{
393 virtual void SetMaxScrollOffset(gfx::Vector2dF max_scroll_offset
) OVERRIDE
{}
394 virtual void SetTotalScrollOffset(gfx::Vector2dF new_value
) OVERRIDE
{}
395 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
396 return fixed_offset_
;
398 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
400 void set_fixed_offset(gfx::Vector2dF fixed_offset
) {
401 fixed_offset_
= fixed_offset
;
404 virtual void SetTotalPageScaleFactor(float page_scale_factor
) OVERRIDE
{}
405 virtual void SetScrollableSize(gfx::SizeF scrollable_size
) OVERRIDE
{}
408 gfx::Vector2dF fixed_offset_
;
411 TEST_F(LayerImplScrollTest
, ScrollByWithIgnoringDelegate
) {
412 gfx::Vector2d
max_scroll_offset(50, 80);
413 gfx::Vector2d
scroll_offset(10, 5);
414 layer()->SetMaxScrollOffset(max_scroll_offset
);
415 layer()->SetScrollOffset(scroll_offset
);
417 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
418 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
419 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
421 ScrollDelegateIgnore delegate
;
422 gfx::Vector2dF
fixed_offset(32, 12);
423 delegate
.set_fixed_offset(fixed_offset
);
424 layer()->SetScrollOffsetDelegate(&delegate
);
426 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
427 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
429 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
431 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
432 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
434 layer()->SetScrollOffsetDelegate(NULL
);
436 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
437 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
439 gfx::Vector2dF
scroll_delta(1, 1);
440 layer()->ScrollBy(scroll_delta
);
442 EXPECT_VECTOR_EQ(fixed_offset
+ scroll_delta
, layer()->TotalScrollOffset());
443 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
446 class ScrollDelegateAccept
: public LayerScrollOffsetDelegate
{
448 virtual void SetMaxScrollOffset(gfx::Vector2dF max_scroll_offset
) OVERRIDE
{}
449 virtual void SetTotalScrollOffset(gfx::Vector2dF new_value
) OVERRIDE
{
450 current_offset_
= new_value
;
452 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
453 return current_offset_
;
455 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
456 virtual void SetTotalPageScaleFactor(float page_scale_factor
) OVERRIDE
{}
457 virtual void SetScrollableSize(gfx::SizeF scrollable_size
) OVERRIDE
{}
460 gfx::Vector2dF current_offset_
;
463 TEST_F(LayerImplScrollTest
, ScrollByWithAcceptingDelegate
) {
464 gfx::Vector2d
max_scroll_offset(50, 80);
465 gfx::Vector2d
scroll_offset(10, 5);
466 layer()->SetMaxScrollOffset(max_scroll_offset
);
467 layer()->SetScrollOffset(scroll_offset
);
469 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
470 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
471 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
473 ScrollDelegateAccept delegate
;
474 layer()->SetScrollOffsetDelegate(&delegate
);
476 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
477 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
478 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
480 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
482 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
483 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
485 layer()->SetScrollOffsetDelegate(NULL
);
487 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
488 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
490 gfx::Vector2dF
scroll_delta(1, 1);
491 layer()->ScrollBy(scroll_delta
);
493 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset());
494 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
497 TEST_F(LayerImplScrollTest
, ApplySentScrollsNoDelegate
) {
498 gfx::Vector2d
max_scroll_offset(50, 80);
499 gfx::Vector2d
scroll_offset(10, 5);
500 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
501 gfx::Vector2d
sent_scroll_delta(12, -3);
503 layer()->SetMaxScrollOffset(max_scroll_offset
);
504 layer()->SetScrollOffset(scroll_offset
);
505 layer()->ScrollBy(scroll_delta
);
506 layer()->SetSentScrollDelta(sent_scroll_delta
);
508 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
509 EXPECT_VECTOR_EQ(scroll_delta
, layer()->ScrollDelta());
510 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
511 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
513 layer()->ApplySentScrollDeltasFromAbortedCommit();
515 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
516 EXPECT_VECTOR_EQ(scroll_delta
- sent_scroll_delta
, layer()->ScrollDelta());
517 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
518 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
521 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithIgnoringDelegate
) {
522 gfx::Vector2d
max_scroll_offset(50, 80);
523 gfx::Vector2d
scroll_offset(10, 5);
524 gfx::Vector2d
sent_scroll_delta(12, -3);
525 gfx::Vector2dF
fixed_offset(32, 12);
527 layer()->SetMaxScrollOffset(max_scroll_offset
);
528 layer()->SetScrollOffset(scroll_offset
);
529 ScrollDelegateIgnore delegate
;
530 delegate
.set_fixed_offset(fixed_offset
);
531 layer()->SetScrollOffsetDelegate(&delegate
);
532 layer()->SetSentScrollDelta(sent_scroll_delta
);
534 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
535 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
536 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
538 layer()->ApplySentScrollDeltasFromAbortedCommit();
540 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
541 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
542 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
545 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithAcceptingDelegate
) {
546 gfx::Vector2d
max_scroll_offset(50, 80);
547 gfx::Vector2d
scroll_offset(10, 5);
548 gfx::Vector2d
sent_scroll_delta(12, -3);
549 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
551 layer()->SetMaxScrollOffset(max_scroll_offset
);
552 layer()->SetScrollOffset(scroll_offset
);
553 ScrollDelegateAccept delegate
;
554 layer()->SetScrollOffsetDelegate(&delegate
);
555 layer()->ScrollBy(scroll_delta
);
556 layer()->SetSentScrollDelta(sent_scroll_delta
);
558 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
559 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
560 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
562 layer()->ApplySentScrollDeltasFromAbortedCommit();
564 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
565 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
566 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
569 // The user-scrollability breaks for zoomed-in pages. So disable this.
570 // http://crbug.com/322223
571 TEST_F(LayerImplScrollTest
, DISABLED_ScrollUserUnscrollableLayer
) {
572 gfx::Vector2d
max_scroll_offset(50, 80);
573 gfx::Vector2d
scroll_offset(10, 5);
574 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
576 layer()->set_user_scrollable_vertical(false);
577 layer()->SetMaxScrollOffset(max_scroll_offset
);
578 layer()->SetScrollOffset(scroll_offset
);
579 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
581 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f
), unscrolled
);
582 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f
, 5), layer()->TotalScrollOffset());