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

Side by Side Diff: cc/trees/draw_property_utils_unittest.cc

Issue 2733633002: Handle nested position:sticky elements correctly (compositor) (Closed)
Patch Set: Fix unittest Created 3 years, 9 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 2017 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/trees/draw_property_utils.h"
6
7 #include <stddef.h>
8
9 #include "base/memory/ref_counted.h"
10 #include "cc/layers/layer.h"
11 #include "cc/layers/layer_sticky_position_constraint.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "cc/test/layer_test_common.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace cc {
17 namespace draw_property_utils {
18
19 namespace {
20 class DrawPropertyUtilsTest : public LayerTestCommon::LayerImplTest,
21 public testing::Test {};
22 } // namespace
23
24 TEST_F(DrawPropertyUtilsTest, CalculateTotalStickyOffsetToScroller) {
25 scoped_refptr<Layer> root = Layer::Create();
26 scoped_refptr<Layer> container = Layer::Create();
27 scoped_refptr<Layer> scroller = Layer::Create();
28 scoped_refptr<Layer> child_sticky = Layer::Create();
29 scoped_refptr<Layer> grandchild_sticky = Layer::Create();
30 scoped_refptr<Layer> great_grandchild_layer = Layer::Create();
31
32 root->AddChild(container);
33 container->AddChild(scroller);
34 scroller->AddChild(child_sticky);
35 child_sticky->AddChild(grandchild_sticky);
36 grandchild_sticky->AddChild(great_grandchild_layer);
37
38 host()->SetRootLayer(root);
39 scroller->SetScrollClipLayerId(container->id());
40
41 LayerStickyPositionConstraint child_sticky_pos;
42 child_sticky_pos.is_sticky = true;
43 child_sticky_pos.local_sticky_offset = gfx::Vector2dF(10, 50);
44 child_sticky->SetStickyPositionConstraint(child_sticky_pos);
45
46 LayerStickyPositionConstraint grandchild_sticky_pos;
47 grandchild_sticky_pos.is_sticky = true;
48 grandchild_sticky_pos.nearest_layer_shifting_containing_block =
49 child_sticky.get();
50 grandchild_sticky_pos.local_sticky_offset = gfx::Vector2dF(100, 0);
51 grandchild_sticky->SetStickyPositionConstraint(grandchild_sticky_pos);
52
53 host()->host_impl()->CreatePendingTree();
54 host()->CommitAndCreatePendingTree();
55 host()->host_impl()->ActivateSyncTree();
56
57 gfx::Vector2dF offset =
58 CalculateTotalStickyOffsetToScroller(child_sticky.get(), scroller->id());
59 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(10, 50), offset);
60
61 offset = CalculateTotalStickyOffsetToScroller(grandchild_sticky.get(),
62 scroller->id());
63 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(110, 50), offset);
64
65 // The great grandchild, although not sticky itself, should be able to locate
66 // its nearest sticky ancestor and walk up from there.
67 offset = CalculateTotalStickyOffsetToScroller(great_grandchild_layer.get(),
68 scroller->id());
69 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(110, 50), offset);
70 }
71
72 TEST_F(DrawPropertyUtilsTest, CalculateTotalStickyOffsetToScrollerComplex) {
73 scoped_refptr<Layer> root = Layer::Create();
74 scoped_refptr<Layer> container = Layer::Create();
75 scoped_refptr<Layer> scroller = Layer::Create();
76 scoped_refptr<Layer> child_sticky = Layer::Create();
77 scoped_refptr<Layer> grandchild_sticky = Layer::Create();
78 scoped_refptr<Layer> great_grandchild_sticky = Layer::Create();
79
80 root->AddChild(container);
81 container->AddChild(scroller);
82 scroller->AddChild(child_sticky);
83 child_sticky->AddChild(grandchild_sticky);
84 grandchild_sticky->AddChild(great_grandchild_sticky);
85
86 host()->SetRootLayer(root);
87 scroller->SetScrollClipLayerId(container->id());
88
89 LayerStickyPositionConstraint child_sticky_pos;
90 child_sticky_pos.is_sticky = true;
91 child_sticky_pos.local_sticky_offset = gfx::Vector2dF(10, 50);
92 child_sticky->SetStickyPositionConstraint(child_sticky_pos);
93
94 LayerStickyPositionConstraint grandchild_sticky_pos;
95 grandchild_sticky_pos.is_sticky = true;
96 grandchild_sticky_pos.nearest_layer_shifting_containing_block =
97 child_sticky.get();
98 grandchild_sticky_pos.local_sticky_offset = gfx::Vector2dF(100, 0);
99 grandchild_sticky->SetStickyPositionConstraint(grandchild_sticky_pos);
100
101 // We are considering the grandchild to be something like an inline element
102 // that doesn't become a containing block for the great-grandchild. Therefore,
103 // the great-grandchild points at both the child and grandchild, but should
104 // accumulate both (via the grandchild) in the walk.
105 LayerStickyPositionConstraint great_grandchild_sticky_pos;
106 great_grandchild_sticky_pos.is_sticky = true;
107 great_grandchild_sticky_pos.local_sticky_offset = gfx::Vector2dF(0, 20);
108 great_grandchild_sticky_pos.nearest_layer_shifting_sticky_box =
109 grandchild_sticky.get();
110 great_grandchild_sticky_pos.nearest_layer_shifting_containing_block =
111 child_sticky.get();
112 great_grandchild_sticky->SetStickyPositionConstraint(
113 great_grandchild_sticky_pos);
114
115 host()->host_impl()->CreatePendingTree();
116 host()->CommitAndCreatePendingTree();
117 host()->host_impl()->ActivateSyncTree();
118
119 gfx::Vector2dF offset =
120 CalculateTotalStickyOffsetToScroller(child_sticky.get(), scroller->id());
121 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(10, 50), offset);
122
123 offset = CalculateTotalStickyOffsetToScroller(grandchild_sticky.get(),
124 scroller->id());
125 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(110, 50), offset);
126
127 offset = CalculateTotalStickyOffsetToScroller(great_grandchild_sticky.get(),
128 scroller->id());
129 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(110, 70), offset);
130 }
131
132 TEST_F(DrawPropertyUtilsTest,
133 CalculateTotalStickyOffsetToScrollerNoStickyLayers) {
134 scoped_refptr<Layer> root = Layer::Create();
135 scoped_refptr<Layer> container = Layer::Create();
136 scoped_refptr<Layer> other_container = Layer::Create();
137 scoped_refptr<Layer> scroller = Layer::Create();
138 scoped_refptr<Layer> first_child = Layer::Create();
139 scoped_refptr<Layer> second_child = Layer::Create();
140 scoped_refptr<Layer> first_grandchild = Layer::Create();
141
142 root->AddChild(container);
143 root->AddChild(other_container);
144 container->AddChild(scroller);
145 scroller->AddChild(first_child);
146 scroller->AddChild(second_child);
147 first_child->AddChild(first_grandchild);
148
149 host()->SetRootLayer(root);
150 scroller->SetScrollClipLayerId(container->id());
151
152 LayerStickyPositionConstraint sticky_position;
153 sticky_position.is_sticky = true;
154 sticky_position.local_sticky_offset = gfx::Vector2dF(10, 50);
155 second_child->SetStickyPositionConstraint(sticky_position);
156
157 host()->host_impl()->CreatePendingTree();
158 host()->CommitAndCreatePendingTree();
159 host()->host_impl()->ActivateSyncTree();
160
161 // We should ignore second_child, as it is not between us and the scroller.
162 gfx::Vector2dF offset = CalculateTotalStickyOffsetToScroller(
163 first_grandchild.get(), scroller->id());
164 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(0, 0), offset);
165
166 // There is no scroller above other_container, so its offset is zero.
167 offset = CalculateTotalStickyOffsetToScroller(other_container.get(), -1);
168 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(0, 0), offset);
169 }
170
171 TEST_F(DrawPropertyUtilsTest,
172 CalculateTotalStickyOffsetToScrollerNoStickyLayersBelowScroller) {
173 scoped_refptr<Layer> root = Layer::Create();
174 scoped_refptr<Layer> sticky = Layer::Create();
175 scoped_refptr<Layer> container = Layer::Create();
176 scoped_refptr<Layer> scroller = Layer::Create();
177 scoped_refptr<Layer> first_child = Layer::Create();
178 scoped_refptr<Layer> first_grandchild = Layer::Create();
179
180 root->AddChild(sticky);
181 sticky->AddChild(container);
182 container->AddChild(scroller);
183 scroller->AddChild(first_child);
184 first_child->AddChild(first_grandchild);
185
186 host()->SetRootLayer(root);
187 scroller->SetScrollClipLayerId(container->id());
188
189 LayerStickyPositionConstraint sticky_position;
190 sticky_position.is_sticky = true;
191 sticky_position.local_sticky_offset = gfx::Vector2dF(10, 50);
192 sticky->SetStickyPositionConstraint(sticky_position);
193
194 host()->host_impl()->CreatePendingTree();
195 host()->CommitAndCreatePendingTree();
196 host()->host_impl()->ActivateSyncTree();
197
198 // We should ignore the sticky element above the scroller.
199 gfx::Vector2dF offset = CalculateTotalStickyOffsetToScroller(
200 first_grandchild.get(), scroller->id());
201 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(0, 0), offset);
202 }
203
204 } // namespace draw_property_utils
205 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698