blob: a7e55513eec655ba51d5d7ad680cab9c7f977946 [file] [log] [blame]
Stephen McGruer26390b32018-12-10 15:47:181<!DOCTYPE html>
2<meta charset="utf-8">
3<title>ScrollTimeline current time algorithm - interaction with writing modes</title>
4<link rel="help" href="https://wicg.github.io/scroll-animations/#current-time-algorithm">
5<script src="/resources/testharness.js"></script>
6<script src="/resources/testharnessreport.js"></script>
7
8<script src="./resources/scrolltimeline-utils.js"></script>
9
10<body></body>
11
12<script>
13'use strict';
14
15test(function() {
16 const scrollerOverrides = new Map([['direction', 'rtl']]);
17 const scroller = setupScrollTimelineTest(scrollerOverrides);
18
19 // Set the timeRange such that currentTime maps directly to the value
20 // scrolled. The contents and scroller are square, so it suffices to compute
21 // one edge and use it for all the timelines.
22 const scrollerSize = scroller.scrollHeight - scroller.clientHeight;
23
24 const blockScrollTimeline = new ScrollTimeline(
25 {scrollSource: scroller, timeRange: scrollerSize, orientation: 'block'});
26 const inlineScrollTimeline = new ScrollTimeline(
27 {scrollSource: scroller, timeRange: scrollerSize, orientation: 'inline'});
28 const horizontalScrollTimeline = new ScrollTimeline({
29 scrollSource: scroller,
30 timeRange: scrollerSize,
31 orientation: 'horizontal'
32 });
33 const verticalScrollTimeline = new ScrollTimeline({
34 scrollSource: scroller,
35 timeRange: scrollerSize,
36 orientation: 'vertical'
37 });
38
39 // Unscrolled, all timelines should read a current time of 0 even though the
40 // X-axis will have started at the right hand side for rtl.
41 assert_equals(
42 blockScrollTimeline.currentTime, 0, 'Unscrolled block timeline');
43 assert_equals(
44 inlineScrollTimeline.currentTime, 0, 'Unscrolled inline timeline');
45 assert_equals(
46 horizontalScrollTimeline.currentTime, 0,
47 'Unscrolled horizontal timeline');
48 assert_equals(
49 verticalScrollTimeline.currentTime, 0, 'Unscrolled vertical timeline');
50
51 // The offset in the inline/horizontal direction should be inverted. The
52 // block/vertical direction should be unaffected.
53 scroller.scrollTop = 50;
54 scroller.scrollLeft = 75;
55
56 assert_equals(blockScrollTimeline.currentTime, 50, 'Scrolled block timeline');
57 assert_equals(
58 inlineScrollTimeline.currentTime, scrollerSize - 75,
59 'Scrolled inline timeline');
60 assert_equals(
61 horizontalScrollTimeline.currentTime, scrollerSize - 75,
62 'Scrolled horizontal timeline');
63 assert_equals(
64 verticalScrollTimeline.currentTime, 50, 'Scrolled vertical timeline');
65}, 'currentTime handles direction: rtl correctly');
66
67test(function() {
68 const scrollerOverrides = new Map([['writing-mode', 'vertical-rl']]);
69 const scroller = setupScrollTimelineTest(scrollerOverrides);
70
71 // Set the timeRange such that currentTime maps directly to the value
72 // scrolled. The contents and scroller are square, so it suffices to compute
73 // one edge and use it for all the timelines.
74 const scrollerSize = scroller.scrollHeight - scroller.clientHeight;
75
76 const blockScrollTimeline = new ScrollTimeline(
77 {scrollSource: scroller, timeRange: scrollerSize, orientation: 'block'});
78 const inlineScrollTimeline = new ScrollTimeline(
79 {scrollSource: scroller, timeRange: scrollerSize, orientation: 'inline'});
80 const horizontalScrollTimeline = new ScrollTimeline({
81 scrollSource: scroller,
82 timeRange: scrollerSize,
83 orientation: 'horizontal'
84 });
85 const verticalScrollTimeline = new ScrollTimeline({
86 scrollSource: scroller,
87 timeRange: scrollerSize,
88 orientation: 'vertical'
89 });
90
91 // Unscrolled, all timelines should read a current time of 0 even though the
92 // X-axis will have started at the right hand side for vertical-rl.
93 assert_equals(
94 blockScrollTimeline.currentTime, 0, 'Unscrolled block timeline');
95 assert_equals(
96 inlineScrollTimeline.currentTime, 0, 'Unscrolled inline timeline');
97 assert_equals(
98 horizontalScrollTimeline.currentTime, 0,
99 'Unscrolled horizontal timeline');
100 assert_equals(
101 verticalScrollTimeline.currentTime, 0, 'Unscrolled vertical timeline');
102
103 // For vertical-rl, the X-axis starts on the right-hand-side and is the block
104 // axis. The Y-axis is normal but is the inline axis. For the
105 // horizontal/vertical cases, horizontal starts on the right-hand-side and
106 // vertical is normal.
107 scroller.scrollTop = 50;
108 scroller.scrollLeft = 75;
109
110 assert_equals(
111 blockScrollTimeline.currentTime, scrollerSize - 75,
112 'Scrolled block timeline');
113 assert_equals(
114 inlineScrollTimeline.currentTime, 50, 'SCrolled inline timeline');
115 assert_equals(
116 horizontalScrollTimeline.currentTime, scrollerSize - 75,
117 'Scrolled horizontal timeline');
118 assert_equals(
119 verticalScrollTimeline.currentTime, 50, 'Scrolled vertical timeline');
120}, 'currentTime handles writing-mode: vertical-rl correctly');
121
122test(function() {
123 const scrollerOverrides = new Map([['writing-mode', 'vertical-lr']]);
124 const scroller = setupScrollTimelineTest(scrollerOverrides);
125
126 // Set the timeRange such that currentTime maps directly to the value
127 // scrolled. The contents and scroller are square, so it suffices to compute
128 // one edge and use it for all the timelines.
129 const scrollerSize = scroller.scrollHeight - scroller.clientHeight;
130
131 const blockScrollTimeline = new ScrollTimeline(
132 {scrollSource: scroller, timeRange: scrollerSize, orientation: 'block'});
133 const inlineScrollTimeline = new ScrollTimeline(
134 {scrollSource: scroller, timeRange: scrollerSize, orientation: 'inline'});
135 const horizontalScrollTimeline = new ScrollTimeline({
136 scrollSource: scroller,
137 timeRange: scrollerSize,
138 orientation: 'horizontal'
139 });
140 const verticalScrollTimeline = new ScrollTimeline({
141 scrollSource: scroller,
142 timeRange: scrollerSize,
143 orientation: 'vertical'
144 });
145
146 // Unscrolled, all timelines should read a current time of 0.
147 assert_equals(
148 blockScrollTimeline.currentTime, 0, 'Unscrolled block timeline');
149 assert_equals(
150 inlineScrollTimeline.currentTime, 0, 'Unscrolled inline timeline');
151 assert_equals(
152 horizontalScrollTimeline.currentTime, 0,
153 'Unscrolled horizontal timeline');
154 assert_equals(
155 verticalScrollTimeline.currentTime, 0, 'Unscrolled vertical timeline');
156
157 // For vertical-lr, both axes start at their 'normal' positions but the X-axis
158 // is the block direction and the Y-axis is the inline direction. This does
159 // not affect horizontal/vertical.
160 scroller.scrollTop = 50;
161 scroller.scrollLeft = 75;
162
163 assert_equals(blockScrollTimeline.currentTime, 75, 'Scrolled block timeline');
164 assert_equals(
165 inlineScrollTimeline.currentTime, 50, 'Scrolled inline timeline');
166 assert_equals(
167 horizontalScrollTimeline.currentTime, 75, 'Scrolled horizontal timeline');
168 assert_equals(
169 verticalScrollTimeline.currentTime, 50, 'Scrolled vertical timeline');
170}, 'currentTime handles writing-mode: vertical-lr correctly');
171
172test(function() {
173 const scrollerOverrides = new Map([['direction', 'rtl']]);
174 const scroller = setupScrollTimelineTest(scrollerOverrides);
175 // Set the timeRange such that currentTime maps directly to the value
176 // scrolled. The contents and scroller are square, so it suffices to compute
177 // one edge and use it for all the timelines.
178 const scrollerSize = scroller.scrollHeight - scroller.clientHeight;
179
180 const lengthScrollTimeline = new ScrollTimeline({
181 scrollSource: scroller,
182 timeRange: scrollerSize,
183 orientation: 'horizontal',
184 startScrollOffset: '20px'
185 });
186 const percentageScrollTimeline = new ScrollTimeline({
187 scrollSource: scroller,
188 timeRange: scrollerSize,
189 orientation: 'horizontal',
190 startScrollOffset: '20%'
191 });
192 const calcScrollTimeline = new ScrollTimeline({
193 scrollSource: scroller,
194 timeRange: scrollerSize,
195 orientation: 'horizontal',
196 startScrollOffset: 'calc(20% - 5px)'
197 });
198
199 // Unscrolled, all timelines should read a current time of unresolved, since
200 // the current offset (0) will be less than the startScrollOffset.
201 assert_equals(
202 lengthScrollTimeline.currentTime, null,
203 'Unscrolled length-based timeline');
204 assert_equals(
205 percentageScrollTimeline.currentTime, null,
206 'Unscrolled percentage-based timeline');
207 assert_equals(
208 calcScrollTimeline.currentTime, null, 'Unscrolled calc-based timeline');
209
210 // With direction rtl offsets are inverted, such that scrollLeft ==
211 // scrollerSize is the 'zero' point for currentTime. However the
212 // startScrollOffset is an absolute distance along the offset, so doesn't
213 // need adjusting.
214
215 // Check the length-based ScrollTimeline.
216 scroller.scrollLeft = scrollerSize;
217 assert_equals(
218 lengthScrollTimeline.currentTime, null,
219 'Length-based timeline before the startScrollOffset point');
220 scroller.scrollLeft = scrollerSize - 20;
221 assert_equals(
222 lengthScrollTimeline.currentTime, 0,
223 'Length-based timeline at the startScrollOffset point');
224 scroller.scrollLeft = scrollerSize - 50;
225 assert_equals(
226 lengthScrollTimeline.currentTime,
227 calculateCurrentTime(50, 20, scrollerSize, scrollerSize),
228 'Length-based timeline after the startScrollOffset point');
229
230 // Check the percentage-based ScrollTimeline.
231 scroller.scrollLeft = scrollerSize - (0.19 * scrollerSize);
232 assert_equals(
233 percentageScrollTimeline.currentTime, null,
234 'Percentage-based timeline before the startScrollOffset point');
235 scroller.scrollLeft = scrollerSize - (0.20 * scrollerSize);
236 assert_equals(
237 percentageScrollTimeline.currentTime, 0,
238 'Percentage-based timeline at the startScrollOffset point');
239 scroller.scrollLeft = scrollerSize - (0.4 * scrollerSize);
240 assert_equals(
241 percentageScrollTimeline.currentTime,
242 calculateCurrentTime(
243 0.4 * scrollerSize, 0.2 * scrollerSize, scrollerSize, scrollerSize),
244 'Percentage-based timeline after the startScrollOffset point');
245
246 // Check the calc-based ScrollTimeline.
247 scroller.scrollLeft = scrollerSize - (0.2 * scrollerSize - 10);
248 assert_equals(
249 calcScrollTimeline.currentTime, null,
250 'Calc-based timeline before the startScrollOffset point');
251 scroller.scrollLeft = scrollerSize - (0.2 * scrollerSize - 5);
252 assert_equals(
253 calcScrollTimeline.currentTime, 0,
254 'Calc-based timeline at the startScrollOffset point');
255 scroller.scrollLeft = scrollerSize - (0.2 * scrollerSize);
256 assert_equals(
257 calcScrollTimeline.currentTime,
258 calculateCurrentTime(
259 0.2 * scrollerSize, 0.2 * scrollerSize - 5, scrollerSize,
260 scrollerSize),
261 'Calc-based timeline after the startScrollOffset point');
262}, 'currentTime handles startScrollOffset with direction: rtl correctly');
263
264test(function() {
265 const scrollerOverrides = new Map([['direction', 'rtl']]);
266 const scroller = setupScrollTimelineTest(scrollerOverrides);
267 // Set the timeRange such that currentTime maps directly to the value
268 // scrolled. The contents and scroller are square, so it suffices to compute
269 // one edge and use it for all the timelines.
270 const scrollerSize = scroller.scrollHeight - scroller.clientHeight;
271
272 const lengthScrollTimeline = new ScrollTimeline({
273 scrollSource: scroller,
274 timeRange: scrollerSize,
275 orientation: 'horizontal',
276 endScrollOffset: (scrollerSize - 20) + 'px'
277 });
278 const percentageScrollTimeline = new ScrollTimeline({
279 scrollSource: scroller,
280 timeRange: scrollerSize,
281 orientation: 'horizontal',
282 endScrollOffset: '80%'
283 });
284 const calcScrollTimeline = new ScrollTimeline({
285 scrollSource: scroller,
286 timeRange: scrollerSize,
287 orientation: 'horizontal',
288 endScrollOffset: 'calc(80% + 5px)'
289 });
290
291 // With direction rtl offsets are inverted, such that scrollLeft ==
292 // scrollerSize is the 'zero' point for currentTime. However the
293 // endScrollOffset is an absolute distance along the offset, so doesn't need
294 // adjusting.
295
296 // Check the length-based ScrollTimeline.
297 scroller.scrollLeft = 0;
298 assert_equals(
299 lengthScrollTimeline.currentTime, null,
300 'Length-based timeline after the endScrollOffset point');
301 scroller.scrollLeft = 20;
302 assert_equals(
303 lengthScrollTimeline.currentTime,
304 calculateCurrentTime(
305 scrollerSize - 20, 0, scrollerSize - 20, scrollerSize),
306 'Length-based timeline at the endScrollOffset point');
307 scroller.scrollLeft = 50;
308 assert_equals(
309 lengthScrollTimeline.currentTime,
310 calculateCurrentTime(
311 scrollerSize - 50, 0, scrollerSize - 20, scrollerSize),
312 'Length-based timeline before the endScrollOffset point');
313
314 // Check the percentage-based ScrollTimeline.
315 scroller.scrollLeft = 0.19 * scrollerSize;
316 assert_equals(
317 percentageScrollTimeline.currentTime, null,
318 'Percentage-based timeline after the endScrollOffset point');
319 scroller.scrollLeft = 0.20 * scrollerSize;
320 assert_equals(
321 percentageScrollTimeline.currentTime,
322 calculateCurrentTime(
323 0.8 * scrollerSize, 0, 0.8 * scrollerSize, scrollerSize),
324 'Percentage-based timeline at the endScrollOffset point');
325 scroller.scrollLeft = 0.4 * scrollerSize;
326 assert_equals(
327 percentageScrollTimeline.currentTime,
328 calculateCurrentTime(
329 0.6 * scrollerSize, 0, 0.8 * scrollerSize, scrollerSize),
330 'Percentage-based timeline before the endScrollOffset point');
331
332 // Check the calc-based ScrollTimeline. 80% + 5px
333 scroller.scrollLeft = 0.2 * scrollerSize - 10;
334 assert_equals(
335 calcScrollTimeline.currentTime, null,
336 'Calc-based timeline after the endScrollOffset point');
337 scroller.scrollLeft = 0.2 * scrollerSize - 5;
338 assert_equals(
339 calcScrollTimeline.currentTime,
340 calculateCurrentTime(
341 0.8 * scrollerSize + 5, 0, 0.8 * scrollerSize + 5, scrollerSize),
342 'Calc-based timeline at the endScrollOffset point');
343 scroller.scrollLeft = 0.2 * scrollerSize;
344 assert_equals(
345 calcScrollTimeline.currentTime,
346 calculateCurrentTime(
347 0.8 * scrollerSize, 0, 0.8 * scrollerSize + 5, scrollerSize),
348 'Calc-based timeline before the endScrollOffset point');
349}, 'currentTime handles endScrollOffset with direction: rtl correctly');
350</script>