summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-animations/interfaces/Animation/finished.html
blob: d56de1b0313b4c7148c0f9c547ff507ece881f90 (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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Animation.finished</title>
<link rel="help" href="https://w3c.github.io/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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var previousFinishedPromise = animation.finished;
  return animation.ready.then(function() {
    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(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var previousFinishedPromise;
  animation.finish();
  return animation.finished.then(function() {
    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(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  // Setup callback to run if finished promise is resolved
  var finishPromiseResolved = false;
  animation.finished.then(function() {
    finishPromiseResolved = true;
  });
  return animation.ready.then(function() {
    // Jump to mid-way in interval and pause
    animation.currentTime = 100 * MS_PER_SEC / 2;
    animation.pause();
    return animation.ready;
  }).then(function() {
    // 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(function() {
    assert_false(finishPromiseResolved,
                 'Finished promise should not resolve when paused');
  });
}, 'Finished promise does not resolve when paused');

promise_test(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  // Setup callback to run if finished promise is resolved
  var finishPromiseResolved = false;
  animation.finished.then(function() {
    finishPromiseResolved = true;
  });
  return animation.ready.then(function() {
    // 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(function() {
    assert_false(finishPromiseResolved,
                 'Finished promise should not resolve when pause-pending');
  });
}, 'Finished promise does not resolve when pause-pending');

promise_test(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  animation.finish();
  return animation.finished.then(function(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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var previousFinishedPromise = animation.finished;

  // Set up listeners on finished promise
  var retPromise = animation.finished.then(function() {
    assert_unreached('finished promise was fulfilled');
  }).catch(function(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 cancelled by calling ' +
   'cancel()');

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

promise_test(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  animation.cancel();
  // The spec says we still create a new finished promise and reject the old
  // one even if we're already idle. That behavior might change, but for now
  // test that we do that.
  var retPromise = animation.finished.catch(function(err) {
    assert_equals(err.name, 'AbortError',
                  'finished promise is rejected with AbortError');
  });

  // Redundant call to cancel();
  var previousFinishedPromise = animation.finished;
  animation.cancel();
  assert_not_equals(animation.finished, previousFinishedPromise,
                    'A redundant call to cancel() should still generate a new'
                    + ' finished promise');
  return retPromise;
}, 'cancelling an idle animation still replaces the finished promise');

promise_test(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  const HALF_DUR = 100 * MS_PER_SEC / 2;
  const QUARTER_DUR = 100 * MS_PER_SEC / 4;
  var gotNextFrame = false;
  var currentTimeBeforeShortening;
  animation.currentTime = HALF_DUR;
  return animation.ready.then(function() {
    currentTimeBeforeShortening = animation.currentTime;
    animation.effect.timing.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(function() {
      gotNextFrame = true;
    });

    return animation.finished;
  }).then(function() {
    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');
    var previousFinishedPromise = animation.finished;
    animation.effect.timing.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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var retPromise = animation.ready.then(function() {
    animation.playbackRate = 0;
    animation.currentTime = 100 * MS_PER_SEC + 1000;
    return waitForAnimationFrames(2);
  });

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

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

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

promise_test(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var previousFinishedPromise = animation.finished;
  animation.finish();
  return animation.finished.then(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var 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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var 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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var resolvedFinished = false;
  animation.finished.then(function() {
    resolvedFinished = true;
  });
  return animation.ready.then(function() {
    animation.finish();
    animation.currentTime = 100 * MS_PER_SEC / 2;
  }).then(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  var resolvedFinished = false;
  animation.finished.then(function() {
    resolvedFinished = true;
  });

  return animation.ready.then(function() {
    animation.currentTime = 100 * MS_PER_SEC;
    animation.finish();
  }).then(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  animation.finished.then(t.step_func(function() {
    assert_unreached('Animation.finished should not be resolved');
  }));

  return animation.ready.then(function() {
    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(function(t) {
  var div = createDiv(t);
  var animation = div.animate({}, 100 * MS_PER_SEC);
  return animation.ready.then(function() {
    animation.currentTime = 100 * MS_PER_SEC;
    animation.finished.then(t.step_func(function() {
      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');

</script>
</body>