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

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

Issue 2636253002: Handle nested position:sticky elements (Closed)
Patch Set: Rebase Created 3 years, 10 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
« no previous file with comments | « cc/trees/draw_property_utils.cc ('k') | cc/trees/layer_tree_host_common_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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.cached_computed_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.cached_computed_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.cached_computed_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.cached_computed_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.cached_computed_sticky_offset =
108 gfx::Vector2dF(0, 20);
109 great_grandchild_sticky_pos.nearest_layer_shifting_sticky_box =
110 grandchild_sticky.get();
111 great_grandchild_sticky_pos.nearest_layer_shifting_containing_block =
112 child_sticky.get();
113 great_grandchild_sticky->SetStickyPositionConstraint(
114 great_grandchild_sticky_pos);
115
116 host()->host_impl()->CreatePendingTree();
117 host()->CommitAndCreatePendingTree();
118 host()->host_impl()->ActivateSyncTree();
119
120 gfx::Vector2dF offset =
121 CalculateTotalStickyOffsetToScroller(child_sticky.get(), scroller->id());
122 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(10, 50), offset);
123
124 offset = CalculateTotalStickyOffsetToScroller(grandchild_sticky.get(),
125 scroller->id());
126 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(110, 50), offset);
127
128 offset = CalculateTotalStickyOffsetToScroller(great_grandchild_sticky.get(),
129 scroller->id());
130 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(110, 70), offset);
131 }
132
133 TEST_F(DrawPropertyUtilsTest,
134 CalculateTotalStickyOffsetToScrollerNoStickyLayers) {
135 scoped_refptr<Layer> root = Layer::Create();
136 scoped_refptr<Layer> container = Layer::Create();
137 scoped_refptr<Layer> other_container = Layer::Create();
138 scoped_refptr<Layer> scroller = Layer::Create();
139 scoped_refptr<Layer> first_child = Layer::Create();
140 scoped_refptr<Layer> second_child = Layer::Create();
141 scoped_refptr<Layer> first_grandchild = Layer::Create();
142
143 root->AddChild(container);
144 root->AddChild(other_container);
145 container->AddChild(scroller);
146 scroller->AddChild(first_child);
147 scroller->AddChild(second_child);
148 first_child->AddChild(first_grandchild);
149
150 host()->SetRootLayer(root);
151 scroller->SetScrollClipLayerId(container->id());
152
153 LayerStickyPositionConstraint sticky_position;
154 sticky_position.is_sticky = true;
155 sticky_position.cached_computed_sticky_offset = gfx::Vector2dF(10, 50);
156 second_child->SetStickyPositionConstraint(sticky_position);
157
158 host()->host_impl()->CreatePendingTree();
159 host()->CommitAndCreatePendingTree();
160 host()->host_impl()->ActivateSyncTree();
161
162 // We should ignore second_child, as it is not between us and the scroller.
163 gfx::Vector2dF offset = CalculateTotalStickyOffsetToScroller(
164 first_grandchild.get(), scroller->id());
165 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(0, 0), offset);
166
167 // There is no scroller above other_container, so its offset is zero.
168 offset = CalculateTotalStickyOffsetToScroller(other_container.get(), -1);
169 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(0, 0), offset);
170 }
171
172 TEST_F(DrawPropertyUtilsTest,
173 CalculateTotalStickyOffsetToScrollerNoStickyLayersBelowScroller) {
174 scoped_refptr<Layer> root = Layer::Create();
175 scoped_refptr<Layer> sticky = Layer::Create();
176 scoped_refptr<Layer> container = Layer::Create();
177 scoped_refptr<Layer> scroller = Layer::Create();
178 scoped_refptr<Layer> first_child = Layer::Create();
179 scoped_refptr<Layer> first_grandchild = Layer::Create();
180
181 root->AddChild(sticky);
182 sticky->AddChild(container);
183 container->AddChild(scroller);
184 scroller->AddChild(first_child);
185 first_child->AddChild(first_grandchild);
186
187 host()->SetRootLayer(root);
188 scroller->SetScrollClipLayerId(container->id());
189
190 LayerStickyPositionConstraint sticky_position;
191 sticky_position.is_sticky = true;
192 sticky_position.cached_computed_sticky_offset = gfx::Vector2dF(10, 50);
193 sticky->SetStickyPositionConstraint(sticky_position);
194
195 host()->host_impl()->CreatePendingTree();
196 host()->CommitAndCreatePendingTree();
197 host()->host_impl()->ActivateSyncTree();
198
199 // We should ignore the sticky element above the scroller.
200 gfx::Vector2dF offset = CalculateTotalStickyOffsetToScroller(
201 first_grandchild.get(), scroller->id());
202 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(0, 0), offset);
203 }
204
205 } // namespace draw_property_utils
206 } // namespace cc
OLDNEW
« no previous file with comments | « cc/trees/draw_property_utils.cc ('k') | cc/trees/layer_tree_host_common_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698