OLD | NEW |
---|---|
(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 "bindings/core/v8/ScrollIntoViewOptionsOrBoolean.h" | |
6 #include "core/frame/ScrollIntoViewOptions.h" | |
7 #include "core/frame/ScrollToOptions.h" | |
8 #include "public/web/WebScriptSource.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "web/WebLocalFrameImpl.h" | |
11 #include "web/tests/sim/SimCompositor.h" | |
12 #include "web/tests/sim/SimDisplayItemList.h" | |
13 #include "web/tests/sim/SimRequest.h" | |
14 #include "web/tests/sim/SimTest.h" | |
15 | |
16 namespace blink { | |
17 | |
18 namespace { | |
19 | |
20 class SmoothScrollTest : public SimTest {}; | |
21 | |
22 TEST_F(SmoothScrollTest, InstantScroll) { | |
23 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
24 WebView().Resize(WebSize(800, 600)); | |
25 SimRequest request("https://example.com/test.html", "text/html"); | |
26 LoadURL("https://example.com/test.html"); | |
27 request.Complete( | |
28 "<div id='space' style='height: 1000px'></div>" | |
29 "<div id='content' style='height: 1000px'></div>"); | |
30 | |
31 Compositor().BeginFrame(); | |
32 ASSERT_EQ(Window().scrollY(), 0); | |
33 Element* content = GetDocument().getElementById("content"); | |
34 ScrollIntoViewOptionsOrBoolean arg; | |
35 content->scrollIntoView(arg); | |
36 | |
37 ASSERT_EQ(Window().scrollY(), content->OffsetTop()); | |
38 } | |
39 | |
40 TEST_F(SmoothScrollTest, SmoothScroll) { | |
41 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
42 WebView().Resize(WebSize(800, 600)); | |
43 SimRequest request("https://example.com/test.html", "text/html"); | |
44 LoadURL("https://example.com/test.html"); | |
45 request.Complete( | |
46 "<div id='space' style='height: 1000px'></div>" | |
47 "<div id='content' style='height: 1000px'></div>"); | |
48 | |
49 Element* content = GetDocument().getElementById("content"); | |
50 ScrollIntoViewOptionsOrBoolean arg; | |
51 ScrollIntoViewOptions options; | |
52 options.setBlock("start"); | |
53 options.setBehavior("smooth"); | |
54 arg.setScrollIntoViewOptions(options); | |
55 Compositor().BeginFrame(); | |
56 ASSERT_EQ(Window().scrollY(), 0); | |
57 | |
58 content->scrollIntoView(arg); | |
59 // Scrolling the container | |
60 Compositor().BeginFrame(); // update run_state_. | |
61 Compositor().BeginFrame(); // Set start_time = now. | |
62 Compositor().BeginFrame(0.2); | |
63 ASSERT_EQ(Window().scrollY(), 299); | |
64 | |
65 // Finish scrolling the container | |
66 Compositor().BeginFrame(1); | |
67 ASSERT_EQ(Window().scrollY(), content->OffsetTop()); | |
68 } | |
69 | |
70 TEST_F(SmoothScrollTest, NestedContainer) { | |
71 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
72 WebView().Resize(WebSize(800, 600)); | |
73 SimRequest request("https://example.com/test.html", "text/html"); | |
74 LoadURL("https://example.com/test.html"); | |
75 request.Complete( | |
76 "<div id='space' style='height: 1000px'></div>" | |
77 "<div id='container' style='height: 600px; overflow: scroll'>" | |
78 " <div id='space1' style='height: 1000px'></div>" | |
79 " <div id='content' style='height: 1000px'></div>" | |
80 "</div>"); | |
81 | |
82 Element* container = GetDocument().getElementById("container"); | |
83 Element* content = GetDocument().getElementById("content"); | |
84 ScrollIntoViewOptionsOrBoolean arg; | |
85 ScrollIntoViewOptions options; | |
86 options.setBlock("start"); | |
87 options.setBehavior("smooth"); | |
88 arg.setScrollIntoViewOptions(options); | |
89 Compositor().BeginFrame(); | |
90 ASSERT_EQ(Window().scrollY(), 0); | |
91 ASSERT_EQ(container->scrollTop(), 0); | |
92 | |
93 content->scrollIntoView(arg); | |
94 // Scrolling the outer container | |
95 Compositor().BeginFrame(); // update run_state_. | |
96 Compositor().BeginFrame(); // Set start_time = now. | |
97 Compositor().BeginFrame(0.2); | |
98 ASSERT_EQ(Window().scrollY(), 299); | |
99 ASSERT_EQ(container->scrollTop(), 0); | |
100 | |
101 // Finish scrolling the outer container | |
102 Compositor().BeginFrame(1); | |
103 ASSERT_EQ(Window().scrollY(), container->OffsetTop()); | |
104 ASSERT_EQ(container->scrollTop(), 0); | |
105 | |
106 // Scrolling the inner container | |
107 Compositor().BeginFrame(); // Set start_time = now. | |
108 Compositor().BeginFrame(0.2); | |
109 ASSERT_EQ(container->scrollTop(), 299); | |
110 | |
111 // Finish scrolling the inner container | |
112 Compositor().BeginFrame(1); | |
113 ASSERT_EQ(container->scrollTop(), | |
114 content->OffsetTop() - container->OffsetTop()); | |
115 } | |
116 | |
117 TEST_F(SmoothScrollTest, NewScrollIntoViewAbortsCurrentAnimation) { | |
118 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
119 WebView().Resize(WebSize(800, 600)); | |
120 SimRequest request("https://example.com/test.html", "text/html"); | |
121 LoadURL("https://example.com/test.html"); | |
122 request.Complete( | |
123 "<div id='container2' style='height: 1000px; overflow: scroll'>" | |
bokan
2017/05/19 18:37:14
This <div> is missing a </div>
sunyunjia
2017/05/19 22:30:41
Done.
| |
124 " <div id='space2' style='height: 1200px'></div>" | |
125 " <div id='content2' style='height: 1000px'></div>" | |
126 "<div id='container1' style='height: 600px; overflow: scroll'>" | |
127 " <div id='space1' style='height: 1000px'></div>" | |
128 " <div id='content1' style='height: 1000px'></div>" | |
129 "</div>"); | |
130 | |
131 Element* container1 = GetDocument().getElementById("container1"); | |
132 Element* container2 = GetDocument().getElementById("container2"); | |
133 Element* content1 = GetDocument().getElementById("content1"); | |
134 Element* content2 = GetDocument().getElementById("content2"); | |
135 ScrollIntoViewOptionsOrBoolean arg; | |
136 ScrollIntoViewOptions options; | |
137 options.setBlock("start"); | |
138 options.setBehavior("smooth"); | |
139 arg.setScrollIntoViewOptions(options); | |
140 | |
141 Compositor().BeginFrame(); | |
142 ASSERT_EQ(Window().scrollY(), 0); | |
143 ASSERT_EQ(container1->scrollTop(), 0); | |
144 ASSERT_EQ(container2->scrollTop(), 0); | |
145 | |
146 content1->scrollIntoView(arg); | |
147 Compositor().BeginFrame(); // update run_state_. | |
148 Compositor().BeginFrame(); // Set start_time = now. | |
149 Compositor().BeginFrame(0.2); | |
150 ASSERT_EQ(Window().scrollY(), 268); | |
151 ASSERT_EQ(container1->scrollTop(), 0); | |
152 | |
153 content2->scrollIntoView(arg); | |
154 Compositor().BeginFrame(); // update run_state_. | |
155 Compositor().BeginFrame(); // Set start_time = now. | |
156 Compositor().BeginFrame(0.2); | |
157 ASSERT_EQ(Window().scrollY(), 43); | |
158 ASSERT_EQ(container1->scrollTop(), | |
159 0); // The first inner scroller should not scroll. | |
160 | |
161 Compositor().BeginFrame(1); | |
162 ASSERT_EQ(Window().scrollY(), container2->OffsetTop()); | |
163 ASSERT_EQ(container2->scrollTop(), 0); | |
164 | |
165 // Scrolling content2 in container2 | |
166 Compositor().BeginFrame(); // Set start_time = now. | |
167 Compositor().BeginFrame(0.2); | |
168 ASSERT_EQ(container2->scrollTop(), 300); | |
169 | |
170 // Finish scrolling content2 | |
171 Compositor().BeginFrame(1); | |
bokan
2017/05/19 18:37:14
Add one or two more begin frames here to make sure
sunyunjia
2017/05/19 22:30:41
Done.
| |
172 ASSERT_EQ(Window().scrollY(), container2->OffsetTop()); | |
173 ASSERT_EQ(container2->scrollTop(), | |
174 content2->OffsetTop() - container2->OffsetTop()); | |
175 ASSERT_EQ(container1->scrollTop(), 0); | |
176 } | |
177 | |
178 TEST_F(SmoothScrollTest, ScrollWindowAbortsCurrentAnimation) { | |
179 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
180 WebView().Resize(WebSize(800, 600)); | |
181 SimRequest request("https://example.com/test.html", "text/html"); | |
182 LoadURL("https://example.com/test.html"); | |
183 request.Complete( | |
184 "<div id='space' style='height: 1000px'></div>" | |
185 "<div id='container' style='height: 600px; overflow: scroll'>" | |
186 " <div id='space1' style='height: 1000px'></div>" | |
187 " <div id='content' style='height: 1000px'></div>" | |
188 "</div>"); | |
189 | |
190 Element* container = GetDocument().getElementById("container"); | |
191 Element* content = GetDocument().getElementById("content"); | |
192 ScrollIntoViewOptionsOrBoolean arg; | |
193 ScrollIntoViewOptions options; | |
194 options.setBlock("start"); | |
195 options.setBehavior("smooth"); | |
196 arg.setScrollIntoViewOptions(options); | |
197 Compositor().BeginFrame(); | |
198 ASSERT_EQ(Window().scrollY(), 0); | |
199 ASSERT_EQ(container->scrollTop(), 0); | |
200 | |
201 content->scrollIntoView(arg); | |
202 // Scrolling the outer container | |
203 Compositor().BeginFrame(); // update run_state_. | |
204 Compositor().BeginFrame(); // Set start_time = now. | |
205 Compositor().BeginFrame(0.2); | |
206 ASSERT_EQ(Window().scrollY(), 299); | |
207 ASSERT_EQ(container->scrollTop(), 0); | |
208 | |
209 ScrollToOptions window_option; | |
210 window_option.setLeft(0); | |
211 window_option.setTop(0); | |
212 window_option.setBehavior("smooth"); | |
213 Window().scrollTo(window_option); | |
214 Compositor().BeginFrame(); // update run_state_. | |
215 Compositor().BeginFrame(); // Set start_time = now. | |
216 Compositor().BeginFrame(0.2); | |
217 ASSERT_EQ(Window().scrollY(), 58); | |
218 | |
219 Compositor().BeginFrame(1); | |
220 ASSERT_EQ(Window().scrollY(), 0); | |
221 ASSERT_EQ(container->scrollTop(), 0); | |
222 } | |
223 | |
224 TEST_F(SmoothScrollTest, BlockAndInlineSettings) { | |
225 v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); | |
226 WebView().Resize(WebSize(800, 600)); | |
227 SimRequest request("https://example.com/test.html", "text/html"); | |
228 LoadURL("https://example.com/test.html"); | |
229 request.Complete( | |
230 "<div id='container' style='height: 2500px; width: 2500px;'>" | |
231 "<div id='content' style='height: 500px; width: 500px;" | |
232 "margin-left: 1000px; margin-right: 1000px; margin-top: 1000px;" | |
233 "margin-bottom: 1000px'></div></div>"); | |
234 | |
235 int content_height = 500; | |
236 int content_width = 500; | |
237 int window_height = 600; | |
238 int window_width = 800; | |
239 | |
240 Element* content = GetDocument().getElementById("content"); | |
241 ScrollIntoViewOptionsOrBoolean arg1, arg2, arg3, arg4; | |
242 ScrollIntoViewOptions options; | |
243 ASSERT_EQ(Window().scrollY(), 0); | |
244 | |
245 options.setBlock("nearest"); | |
246 options.setInlinePosition("nearest"); | |
247 arg1.setScrollIntoViewOptions(options); | |
248 content->scrollIntoView(arg1); | |
249 ASSERT_EQ(Window().scrollX(), | |
250 content->OffsetLeft() + content_width - window_width); | |
251 ASSERT_EQ(Window().scrollY(), | |
252 content->OffsetTop() + content_height - window_height); | |
253 | |
254 options.setBlock("start"); | |
255 options.setInlinePosition("start"); | |
256 arg2.setScrollIntoViewOptions(options); | |
257 content->scrollIntoView(arg2); | |
258 ASSERT_EQ(Window().scrollX(), content->OffsetLeft()); | |
259 ASSERT_EQ(Window().scrollY(), content->OffsetTop()); | |
260 | |
261 options.setBlock("center"); | |
262 options.setInlinePosition("center"); | |
263 arg3.setScrollIntoViewOptions(options); | |
264 content->scrollIntoView(arg3); | |
265 ASSERT_EQ(Window().scrollX(), | |
266 content->OffsetLeft() + (content_width - window_width) / 2); | |
267 ASSERT_EQ(Window().scrollY(), | |
268 content->OffsetTop() + (content_height - window_height) / 2); | |
269 | |
270 options.setBlock("end"); | |
271 options.setInlinePosition("end"); | |
272 arg4.setScrollIntoViewOptions(options); | |
273 content->scrollIntoView(arg4); | |
274 ASSERT_EQ(Window().scrollX(), | |
275 content->OffsetLeft() + content_width - window_width); | |
276 ASSERT_EQ(Window().scrollY(), | |
277 content->OffsetTop() + content_height - window_height); | |
278 } | |
279 | |
280 } // namespace | |
281 | |
282 } // namespace blink | |
OLD | NEW |