onfinish.html (4436B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Animation.onfinish</title> 4 <link rel="help" href="https://drafts.csswg.org/web-animations/#dom-animation-onfinish"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="../../testcommon.js"></script> 8 <body> 9 <div id="log"></div> 10 <script> 11 'use strict'; 12 13 async_test(t => { 14 const div = createDiv(t); 15 const animation = div.animate({}, 100 * MS_PER_SEC); 16 let finishedTimelineTime; 17 animation.finished.then(() => { 18 finishedTimelineTime = animation.timeline.currentTime; 19 }); 20 21 animation.onfinish = t.step_func_done(event => { 22 assert_equals(event.currentTime, 0, 23 'event.currentTime should be zero'); 24 assert_times_equal(event.timelineTime, finishedTimelineTime, 25 'event.timelineTime should equal to the animation timeline ' + 26 'when finished promise is resolved'); 27 }); 28 29 animation.playbackRate = -1; 30 }, 'onfinish event is fired when the currentTime < 0 and ' + 31 'the playbackRate < 0'); 32 33 async_test(t => { 34 const div = createDiv(t); 35 const animation = div.animate({}, 100 * MS_PER_SEC); 36 37 let finishedTimelineTime; 38 animation.finished.then(() => { 39 finishedTimelineTime = animation.timeline.currentTime; 40 }); 41 42 animation.onfinish = t.step_func_done(event => { 43 assert_times_equal(event.currentTime, 100 * MS_PER_SEC, 44 'event.currentTime should be the effect end'); 45 assert_times_equal(event.timelineTime, finishedTimelineTime, 46 'event.timelineTime should equal to the animation timeline ' + 47 'when finished promise is resolved'); 48 }); 49 50 animation.currentTime = 100 * MS_PER_SEC; 51 }, 'onfinish event is fired when the currentTime > 0 and ' + 52 'the playbackRate > 0'); 53 54 async_test(t => { 55 const div = createDiv(t); 56 const animation = div.animate({}, 100 * MS_PER_SEC); 57 58 let finishedTimelineTime; 59 animation.finished.then(() => { 60 finishedTimelineTime = animation.timeline.currentTime; 61 }); 62 63 animation.onfinish = t.step_func_done(event => { 64 assert_times_equal(event.currentTime, 100 * MS_PER_SEC, 65 'event.currentTime should be the effect end'); 66 assert_times_equal(event.timelineTime, finishedTimelineTime, 67 'event.timelineTime should equal to the animation timeline ' + 68 'when finished promise is resolved'); 69 }); 70 71 animation.finish(); 72 }, 'onfinish event is fired when animation.finish() is called'); 73 74 promise_test(async t => { 75 const div = createDiv(t); 76 const animation = div.animate({}, 100 * MS_PER_SEC); 77 78 animation.onfinish = event => { 79 assert_unreached('onfinish event should not be fired'); 80 }; 81 82 animation.currentTime = 100 * MS_PER_SEC / 2; 83 animation.pause(); 84 85 await animation.ready; 86 animation.currentTime = 100 * MS_PER_SEC; 87 await waitForAnimationFrames(2); 88 }, 'onfinish event is not fired when paused'); 89 90 promise_test(async t => { 91 const div = createDiv(t); 92 const animation = div.animate({}, 100 * MS_PER_SEC); 93 animation.onfinish = event => { 94 assert_unreached('onfinish event should not be fired'); 95 }; 96 97 await animation.ready; 98 animation.playbackRate = 0; 99 animation.currentTime = 100 * MS_PER_SEC; 100 await waitForAnimationFrames(2); 101 }, 'onfinish event is not fired when the playbackRate is zero'); 102 103 promise_test(async t => { 104 const div = createDiv(t); 105 const animation = div.animate({}, 100 * MS_PER_SEC); 106 107 animation.onfinish = event => { 108 assert_unreached('onfinish event should not be fired'); 109 }; 110 111 await animation.ready; 112 animation.currentTime = 100 * MS_PER_SEC; 113 animation.currentTime = 100 * MS_PER_SEC / 2; 114 await waitForAnimationFrames(2); 115 }, 'onfinish event is not fired when the animation falls out ' + 116 'finished state immediately'); 117 118 promise_test(async t => { 119 const div = createDiv(t); 120 const animation = div.animate({ opacity: [1, 0]}, 100 * MS_PER_SEC); 121 const restart_after_finish_promise = 122 new Promise(async (resolve) => { 123 let results = []; 124 animation.onfinish = () => { 125 animation.play(); 126 animation.ready.then(() => { 127 results.push(animation.ready); 128 resolve(results); 129 }); 130 }; 131 await animation.ready; 132 results.push(animation.ready); 133 animation.finish(); 134 }); 135 const results = await restart_after_finish_promise; 136 assert_not_equals(results[0], results[1], 137 'Play created a new ready promise'); 138 }, 'Animation can be restarted from the onfinish event'); 139 </script> 140 </body>