summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-animations/interfaces/Animation/finished.html
blob: bee4fd8fb7a63137f45681dccc3b53d3570eefc8 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!DOCTYPE html>
<meta charset=utf-8>
<title>Animation.finished</title>
<link rel="help" href="https://drafts.csswg.org/web-animations/#dom-animation-finished">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<body>
<div id="log"></div>
<script>
'use strict';

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;
  return animation.ready.then(() => {
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise is the same object when playing starts');
    animation.pause();
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise does not change when pausing');
    animation.play();
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise does not change when play() unpauses');

    animation.currentTime = 100 * MS_PER_SEC;

    return animation.finished;
  }).then(() => {
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise is the same object when playing completes');
  });
}, 'Test pausing then playing does not change the finished promise');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  let previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(() => {
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise is the same object when playing completes');
    animation.play();
    assert_not_equals(animation.finished, previousFinishedPromise,
                  'Finished promise changes when replaying animation');

    previousFinishedPromise = animation.finished;
    animation.play();
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise is the same after redundant play() call');

  });
}, 'Test restarting a finished animation');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  let previousFinishedPromise;
  animation.finish();
  return animation.finished.then(() => {
    previousFinishedPromise = animation.finished;
    animation.playbackRate = -1;
    assert_not_equals(animation.finished, previousFinishedPromise,
                      'Finished promise should be replaced when reversing a ' +
                      'finished promise');
    animation.currentTime = 0;
    return animation.finished;
  }).then(() => {
    previousFinishedPromise = animation.finished;
    animation.play();
    assert_not_equals(animation.finished, previousFinishedPromise,
                      'Finished promise is replaced after play() call on ' +
                      'finished, reversed animation');
  });
}, 'Test restarting a reversed finished animation');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(() => {
    animation.currentTime = 100 * MS_PER_SEC + 1000;
    assert_equals(animation.finished, previousFinishedPromise,
                  'Finished promise is unchanged jumping past end of ' +
                  'finished animation');
  });
}, 'Test redundant finishing of animation');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  // Setup callback to run if finished promise is resolved
  let finishPromiseResolved = false;
  animation.finished.then(() => {
    finishPromiseResolved = true;
  });
  return animation.ready.then(() => {
    // Jump to mid-way in interval and pause
    animation.currentTime = 100 * MS_PER_SEC / 2;
    animation.pause();
    return animation.ready;
  }).then(() => {
    // Jump to the end
    // (But don't use finish() since that should unpause as well)
    animation.currentTime = 100 * MS_PER_SEC;
    return waitForAnimationFrames(2);
  }).then(() => {
    assert_false(finishPromiseResolved,
                 'Finished promise should not resolve when paused');
  });
}, 'Finished promise does not resolve when paused');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  // Setup callback to run if finished promise is resolved
  let finishPromiseResolved = false;
  animation.finished.then(() => {
    finishPromiseResolved = true;
  });
  return animation.ready.then(() => {
    // Jump to mid-way in interval and pause
    animation.currentTime = 100 * MS_PER_SEC / 2;
    animation.pause();
    // Jump to the end
    animation.currentTime = 100 * MS_PER_SEC;
    return waitForAnimationFrames(2);
  }).then(() => {
    assert_false(finishPromiseResolved,
                 'Finished promise should not resolve when pause-pending');
  });
}, 'Finished promise does not resolve when pause-pending');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  animation.finish();
  return animation.finished.then(resolvedAnimation => {
    assert_equals(resolvedAnimation, animation,
                  'Object identity of animation passed to Promise callback'
                  + ' matches the animation object owning the Promise');
  });
}, 'The finished promise is fulfilled with its Animation');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;

  // Set up listeners on finished promise
  const retPromise = animation.finished.then(() => {
    assert_unreached('finished promise was fulfilled');
  }).catch(err => {
    assert_equals(err.name, 'AbortError',
                  'finished promise is rejected with AbortError');
    assert_not_equals(animation.finished, previousFinishedPromise,
                      'Finished promise should change after the original is ' +
                      'rejected');
  });

  animation.cancel();

  return retPromise;
}, 'finished promise is rejected when an animation is canceled by calling ' +
   'cancel()');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(() => {
    animation.cancel();
    assert_not_equals(animation.finished, previousFinishedPromise,
                      'A new finished promise should be created when'
                      + ' canceling a finished animation');
  });
}, 'canceling an already-finished animation replaces the finished promise');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const HALF_DUR = 100 * MS_PER_SEC / 2;
  const QUARTER_DUR = 100 * MS_PER_SEC / 4;
  let gotNextFrame = false;
  let currentTimeBeforeShortening;
  animation.currentTime = HALF_DUR;
  return animation.ready.then(() => {
    currentTimeBeforeShortening = animation.currentTime;
    animation.effect.updateTiming({ duration: QUARTER_DUR });
    // Below we use gotNextFrame to check that shortening of the animation
    // duration causes the finished promise to resolve, rather than it just
    // getting resolved on the next animation frame. This relies on the fact
    // that the promises are resolved as a micro-task before the next frame
    // happens.
    waitForAnimationFrames(1).then(() => {
      gotNextFrame = true;
    });

    return animation.finished;
  }).then(() => {
    assert_false(gotNextFrame, 'shortening of the animation duration should ' +
                               'resolve the finished promise');
    assert_equals(animation.currentTime, currentTimeBeforeShortening,
                  'currentTime should be unchanged when duration shortened');
    const previousFinishedPromise = animation.finished;
    animation.effect.updateTiming({ duration: 100 * MS_PER_SEC });
    assert_not_equals(animation.finished, previousFinishedPromise,
                      'Finished promise should change after lengthening the ' +
                      'duration causes the animation to become active');
  });
}, 'Test finished promise changes for animation duration changes');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const retPromise = animation.ready.then(() => {
    animation.playbackRate = 0;
    animation.currentTime = 100 * MS_PER_SEC + 1000;
    return waitForAnimationFrames(2);
  });

  animation.finished.then(t.step_func(() => {
    assert_unreached('finished promise should not resolve when playbackRate ' +
                     'is zero');
  }));

  return retPromise;
}, 'Test finished promise changes when playbackRate == 0');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  return animation.ready.then(() => {
    animation.playbackRate = -1;
    return animation.finished;
  });
}, 'Test finished promise resolves when reaching to the natural boundary.');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(() => {
    animation.currentTime = 0;
    assert_not_equals(animation.finished, previousFinishedPromise,
                      'Finished promise should change once a prior ' +
                      'finished promise resolved and the animation ' +
                      'falls out finished state');
  });
}, 'Test finished promise changes when a prior finished promise resolved ' +
   'and the animation falls out finished state');

test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;
  animation.currentTime = 100 * MS_PER_SEC;
  animation.currentTime = 100 * MS_PER_SEC / 2;
  assert_equals(animation.finished, previousFinishedPromise,
                'No new finished promise generated when finished state ' +
                'is checked asynchronously');
}, 'Test no new finished promise generated when finished state ' +
   'is checked asynchronously');

test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  const previousFinishedPromise = animation.finished;
  animation.finish();
  animation.currentTime = 100 * MS_PER_SEC / 2;
  assert_not_equals(animation.finished, previousFinishedPromise,
                    'New finished promise generated when finished state ' +
                    'is checked synchronously');
}, 'Test new finished promise generated when finished state ' +
   'is checked synchronously');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  let resolvedFinished = false;
  animation.finished.then(() => {
    resolvedFinished = true;
  });
  return animation.ready.then(() => {
    animation.finish();
    animation.currentTime = 100 * MS_PER_SEC / 2;
  }).then(() => {
    assert_true(resolvedFinished,
      'Animation.finished should be resolved even if ' +
      'the finished state is changed soon');
  });

}, 'Test synchronous finished promise resolved even if finished state ' +
   'is changed soon');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  let resolvedFinished = false;
  animation.finished.then(() => {
    resolvedFinished = true;
  });

  return animation.ready.then(() => {
    animation.currentTime = 100 * MS_PER_SEC;
    animation.finish();
  }).then(() => {
    assert_true(resolvedFinished,
      'Animation.finished should be resolved soon after finish() is ' +
      'called even if there are other asynchronous promises just before it');
  });
}, 'Test synchronous finished promise resolved even if asynchronous ' +
   'finished promise happens just before synchronous promise');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  animation.finished.then(t.step_func(() => {
    assert_unreached('Animation.finished should not be resolved');
  }));

  return animation.ready.then(() => {
    animation.currentTime = 100 * MS_PER_SEC;
    animation.currentTime = 100 * MS_PER_SEC / 2;
  });
}, 'Test finished promise is not resolved when the animation ' +
   'falls out finished state immediately');

promise_test(t => {
  const div = createDiv(t);
  const animation = div.animate({}, 100 * MS_PER_SEC);
  return animation.ready.then(() => {
    animation.currentTime = 100 * MS_PER_SEC;
    animation.finished.then(t.step_func(() => {
      assert_unreached('Animation.finished should not be resolved');
    }));
    animation.currentTime = 0;
  });

}, 'Test finished promise is not resolved once the animation ' +
   'falls out finished state even though the current finished ' +
   'promise is generated soon after animation state became finished');

promise_test(t => {
  const animation = createDiv(t).animate(null, 100 * MS_PER_SEC);
  let ready = false;
  animation.ready.then(
    t.step_func(() => {
      ready = true;
    }),
    t.unreached_func('Ready promise must not be rejected')
  );

  const testSuccess = animation.finished.then(
    t.step_func(() => {
      assert_true(ready, 'Ready promise has resolved');
    }),
    t.unreached_func('Finished promise must not be rejected')
  );

  const timeout = waitForAnimationFrames(3).then(() => {
    return Promise.reject('Finished promise did not arrive in time');
  });

  animation.finish();
  return Promise.race([timeout, testSuccess]);
}, 'Finished promise should be resolved after the ready promise is resolved');

promise_test(t => {
  const animation = createDiv(t).animate(null, 100 * MS_PER_SEC);
  let caught = false;
  animation.ready.then(
    t.unreached_func('Ready promise must not be resolved'),
    t.step_func(() => {
      caught = true;
    })
  );

  const testSuccess = animation.finished.then(
    t.unreached_func('Finished promise must not be resolved'),
    t.step_func(() => {
      assert_true(caught, 'Ready promise has been rejected');
    })
  );

  const timeout = waitForAnimationFrames(3).then(() => {
    return Promise.reject('Finished promise was not rejected in time');
  });

  animation.cancel();
  return Promise.race([timeout, testSuccess]);
}, 'Finished promise should be rejected after the ready promise is rejected');

promise_test(async t => {
  const animation = createDiv(t).animate(null, 100 * MS_PER_SEC);

  // Ensure the finished promise is created
  const finished = animation.finished;

  window.addEventListener(
    'unhandledrejection',
    t.unreached_func('Should not get an unhandled rejection')
  );

  animation.cancel();

  // Wait a moment to allow a chance for the event to be dispatched.
  await waitForAnimationFrames(2);
}, 'Finished promise does not report an unhandledrejection when rejected');

</script>
</body>