summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scroll-animations/scroll-timelines/setting-timeline.tentative.html
blob: 69b40cb6c4974dc21d35ded4892dfc815b9043b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<meta charset=utf-8>
<title>Setting the timeline of scroll animation</title>
<link rel="help"
      href="https://drafts.csswg.org/web-animations-1/#setting-the-timeline">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/web-animations/testcommon.js"></script>
<script src="testcommon.js"></script>
<style>
  .scroller {
    overflow: auto;
    height: 200px;
    width: 100px;
    will-change: transform;
  }
  .contents {
    /* The height is set to align scrolling in pixels with logical time in ms */
    height: 1200px;
    width: 100%;
  }
  @keyframes anim {
    from { opacity: 0; }
    to { opacity: 1; }
  }
  .anim {
    animation: anim 1s paused linear;
  }
</style>
<body>
<script>
'use strict';

function createAnimation(t) {
  const elem = createDiv(t);
  const animation = elem.animate({ opacity: [1, 0] }, 1000);
  return animation;
}

function createPausedCssAnimation(t) {
  const elem = createDiv(t);
  elem.classList.add('anim');
  return elem.getAnimations()[0];
}

function updateScrollPosition(timeline, offset) {
  const scroller = timeline.source;
  assert_true(!!scroller, 'source is resolved');
  scroller.scrollTop = offset;
  // Wait for new animation frame which allows the timeline to compute new
  // current time.
  return waitForNextFrame();
}

function assert_timeline_current_time(animation, timeline_current_time) {
  if (animation.currentTime instanceof CSSUnitValue){
    assert_percents_equal(animation.timeline.currentTime, timeline_current_time,
                          `Timeline's currentTime aligns with the scroll ` +
                          `position even when paused`);
  }
  else {
    assert_times_equal(animation.timeline.currentTime, timeline_current_time,
                       `Timeline's currentTime aligns with the scroll ` +
                       `position even when paused`);
  }
}

function assert_scroll_synced_times(animation, timeline_current_time,
                                    animation_current_time) {
  assert_timeline_current_time(animation, timeline_current_time);
  if (animation.currentTime instanceof CSSUnitValue){
    assert_percents_equal(animation.currentTime, animation_current_time,
        `Animation's currentTime aligns with the scroll position`);
  }
  else {
    assert_times_equal(animation.currentTime, animation_current_time,
        `Animation's currentTime aligns with the scroll position`);
  }
}

function assert_paused_times(animation, timeline_current_time,
                             animation_current_time) {
  assert_timeline_current_time(animation, timeline_current_time);
  if (animation.currentTime instanceof CSSUnitValue){
    assert_percents_equal(animation.currentTime, animation_current_time,
                          `Animation's currentTime is fixed while paused`);
  }
  else {
    assert_times_equal(animation.currentTime, animation_current_time,
                       `Animation's currentTime is fixed while paused`);
  }
}

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.timeline = scrollTimeline;
  assert_true(animation.pending);
  await animation.ready;

  assert_equals(animation.playState, 'running');
  assert_scroll_synced_times(animation, 10, 10);
}, 'Setting a scroll timeline on a play-pending animation synchronizes ' +
   'currentTime of the animation with the scroll position.');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.pause();
  animation.timeline = scrollTimeline;
  assert_true(animation.pending);
  await animation.ready;

  assert_equals(animation.playState, 'paused');
  assert_paused_times(animation, 10, 0);

  await updateScrollPosition(animation.timeline, 200);

  assert_equals(animation.playState, 'paused');
  assert_paused_times(animation, 20, 0);

  animation.play();
  await animation.ready;

  assert_scroll_synced_times(animation, 20, 20);
}, 'Setting a scroll timeline on a pause-pending animation fixes the ' +
   'currentTime of the animation based on the scroll position once resumed');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.reverse();
  animation.timeline = scrollTimeline;
  await animation.ready;

  assert_equals(animation.playState, 'running');
  assert_scroll_synced_times(animation, 10, 90);
},  'Setting a scroll timeline on a reversed play-pending animation ' +
    'synchronizes the currentTime of the animation with the scroll ' +
    'position.');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  await animation.ready;

  animation.timeline = scrollTimeline;
  assert_false(animation.pending);
  assert_equals(animation.playState, 'running');
  assert_scroll_synced_times(animation, 10, 10);
},  'Setting a scroll timeline on a running animation synchronizes the ' +
    'currentTime of the animation with the scroll position.');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.pause();
  await animation.ready;

  animation.timeline = scrollTimeline;
  assert_false(animation.pending);
  assert_equals(animation.playState, 'paused');
  assert_paused_times(animation, 10, 0);

  animation.play();
  await animation.ready;

  assert_scroll_synced_times(animation, 10, 10);
}, 'Setting a scroll timeline on a paused animation fixes the currentTime of ' +
   'the animation based on the scroll position when resumed');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.reverse();
  animation.pause();
  await animation.ready;

  animation.timeline = scrollTimeline;
  assert_false(animation.pending);
  assert_equals(animation.playState, 'paused');
  assert_paused_times(animation, 10, 100);

  animation.play();
  await animation.ready;

  assert_scroll_synced_times(animation, 10, 90);
}, 'Setting a scroll timeline on a reversed paused animation ' +
   'fixes the currentTime of the animation based on the scroll ' +
   'position when resumed');

promise_test(async t => {
  const animation = createAnimation(t);
  const scrollTimeline = createScrollTimeline(t);
  animation.timeline = scrollTimeline;
  await animation.ready;
  await updateScrollPosition(scrollTimeline, 100);

  animation.timeline = document.timeline;
  assert_times_equal(animation.currentTime, 100);
}, 'Transitioning from a scroll timeline to a document timeline on a running ' +
   'animation preserves currentTime');

promise_test(async t => {
  const animation = createAnimation(t);
  const scrollTimeline = createScrollTimeline(t);
  animation.timeline = scrollTimeline;
  await animation.ready;
  await updateScrollPosition(scrollTimeline, 100);

  animation.pause();
  animation.timeline = document.timeline;

  await animation.ready;
  assert_times_equal(animation.currentTime, 100);
}, 'Transitioning from a scroll timeline to a document timeline on a ' +
   'pause-pending animation preserves currentTime');

promise_test(async t => {
  const animation = createAnimation(t);
  const scrollTimeline = createScrollTimeline(t);
  animation.timeline = scrollTimeline;
  await animation.ready;

  animation.reverse();
  animation.pause();
  await updateScrollPosition(scrollTimeline, 100);
  assert_scroll_synced_times(animation, 10, 90);

  await animation.ready;

  animation.timeline = document.timeline;
  assert_false(animation.pending);
  assert_equals(animation.playState, 'paused');
  assert_times_equal(animation.currentTime, 900);
}, 'Transition from a scroll timeline to a document timeline on a reversed ' +
   'paused animation maintains correct currentTime');

promise_test(async t => {
  const animation = createAnimation(t);
  const scrollTimeline = createScrollTimeline(t);
  animation.timeline = scrollTimeline;
  await animation.ready;
  await updateScrollPosition(scrollTimeline, 100);

  assert_equals(animation.playState, 'running');
  animation.timeline = null;

  assert_equals(animation.playState, 'running');
}, 'Transitioning from a scroll timeline to a null timeline on a running ' +
    'animation preserves the play state');

promise_test(async t => {
  const keyframeEfect = new KeyframeEffect(createDiv(t),
                                           { opacity: [0, 1] },
                                           1000);
  const animation = new Animation(keyframeEfect, null);
  animation.startTime = 0;
  assert_equals(animation.playState, 'running');

  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  animation.timeline = scrollTimeline;
  assert_equals(animation.playState, 'running');
  assert_percents_equal(animation.currentTime, 10);
}, 'Switching from a null timeline to a scroll timeline on an animation with ' +
   'a resolved start time preserved the play state');

promise_test(async t => {
  const firstScrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(firstScrollTimeline, 100);

  const secondScrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(secondScrollTimeline, 200);

  const animation = createAnimation(t);
  animation.timeline = firstScrollTimeline;
  await animation.ready;
  assert_percents_equal(animation.currentTime, 10);

  animation.timeline = secondScrollTimeline;
  assert_percents_equal(animation.currentTime, 20);
}, 'Switching from one scroll timeline to another updates currentTime');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createPausedCssAnimation(t);
  animation.timeline = scrollTimeline;
  await animation.ready;
  assert_equals(animation.playState, 'paused');
  assert_percents_equal(animation.currentTime, 0);

  const target = animation.effect.target;
  target.style.animationPlayState = 'running';
  await animation.ready;

  assert_percents_equal(animation.currentTime, 10);
}, 'Switching from a document timeline to a scroll timeline updates ' +
   'currentTime when unpaused via CSS.');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.pause();
  animation.timeline = scrollTimeline;
  await animation.ready;
  assert_percents_equal(animation.currentTime, 0);

  animation.currentTime = CSSNumericValue.parse("50%");

  animation.play();
  await animation.ready;
  assert_percents_equal(animation.currentTime, 50);
}, 'Switching from a document timeline to a scroll timeline and updating ' +
   'currentTime preserves the new value when unpaused.');

promise_test(async t => {
  const scrollTimeline = createScrollTimeline(t);
  await updateScrollPosition(scrollTimeline, 100);

  const animation = createAnimation(t);
  animation.pause();
  animation.timeline = scrollTimeline;
  await animation.ready;
  assert_percents_equal(animation.currentTime, 0);
  assert_equals(animation.playState, 'paused');

  // Set a start time that will ensure that current time is in the active
  // region in order not to trigger a seek when play is called.
  animation.startTime = CSSNumericValue.parse("-10%");
  assert_equals(animation.playState, 'running');

  animation.play();
  await animation.ready;
  assert_percents_equal(animation.startTime, -10);
}, 'Switching from a document timeline to a scroll timeline and updating ' +
   'startTime preserves the new value when play is called.');

promise_test(async t => {
  const elem = createDiv(t);
  const animation = elem.animate(null, Infinity);
  await animation.ready;

  animation.timeline = new ScrollTimeline();
  let timing = animation.effect.getComputedTiming();
  assert_percents_equal(timing.endTime, 100);
  assert_percents_equal(timing.activeDuration, 100);
  assert_percents_equal(timing.duration, 100);

  animation.effect.updateTiming({ iterations: 2 });
  timing = animation.effect.getComputedTiming();
  assert_percents_equal(timing.endTime, 100);
  assert_percents_equal(timing.activeDuration, 100);
  assert_percents_equal(timing.duration, 50);

  // Blink implementation does not permit setting an infinite number of
  // iterations on a scroll-linked animation. Workaround by temporarily
  // switching back to a document timeline.
  animation.timeline = document.timeline;
  animation.effect.updateTiming({ iterations: Infinity });
  animation.timeline = new ScrollTimeline();
  timing = animation.effect.getComputedTiming();
  // Having an infinite number of iterations with a finite timeline results in
  // each iteration having zero duration.
  assert_percents_equal(timing.duration, 0);
  // If either the iteration duration or iteration count is zero, the active
  // duration is always zero.
  assert_percents_equal(timing.activeDuration, 0);
  assert_percents_equal(timing.endTime, 0);

}, 'Switching from a document timeline to a scroll timeline on an infinite ' +
   'duration animation.')

</script>
</body>