CSSTransition-currentTime.tentative.html (4568B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>CSSTransition.currentTime</title> 4 <!-- TODO: Add a more specific link for this once it is specified. --> 5 <link rel="help" href="https://drafts.csswg.org/css-transitions-2/#csstransition"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="support/helper.js"></script> 9 <div id="log"></div> 10 <script> 11 12 'use strict'; 13 14 const marginLeft = div => parseFloat(getComputedStyle(div).marginLeft); 15 16 promise_test(async t => { 17 const div = addDiv(t, { 18 style: 'margin-left: 100px; transition: margin-left 100s linear 100s', 19 }); 20 getComputedStyle(div).marginLeft; 21 div.style.marginLeft = '200px'; 22 23 const animation = div.getAnimations()[0]; 24 assert_equals( 25 animation.currentTime, 26 0, 27 'currentTime should initially be zero' 28 ); 29 30 await animation.ready; 31 32 const seekTime = 150 * MS_PER_SEC; 33 animation.currentTime = seekTime; 34 35 assert_time_equals_literal( 36 animation.currentTime, 37 seekTime, 38 'currentTime is updated' 39 ); 40 assert_equals(getComputedStyle(div).marginLeft, '150px'); 41 }, 'currentTime can be used to seek a CSS transition'); 42 43 promise_test(async t => { 44 const div = addDiv(t, { 45 style: 'margin-left: 100px; transition: margin-left 100s linear 100s', 46 }); 47 const eventWatcher = new EventWatcher(t, div, 'transitionend'); 48 getComputedStyle(div).marginLeft; 49 div.style.marginLeft = '200px'; 50 51 const animation = div.getAnimations()[0]; 52 await animation.ready; 53 54 const marginLeft = () => parseFloat(getComputedStyle(div).marginLeft); 55 assert_equals(marginLeft(div), 100); 56 57 animation.currentTime = 100 * MS_PER_SEC; 58 assert_equals(marginLeft(div), 100); 59 60 animation.currentTime = 150 * MS_PER_SEC; 61 assert_equals(marginLeft(div), 150); 62 63 animation.currentTime = 200 * MS_PER_SEC; 64 await eventWatcher.wait_for('transitionend'); 65 assert_equals(marginLeft(div), 200); 66 }, 'Skipping forwards through transition'); 67 68 promise_test(async t => { 69 const div = addDiv(t, { 70 style: 'margin-left: 100px; transition: margin-left 100s linear 100s', 71 }); 72 const eventWatcher = new EventWatcher(t, div, 'transitionend'); 73 getComputedStyle(div).marginLeft; 74 div.style.marginLeft = '200px'; 75 76 const animation = div.getAnimations()[0]; 77 await animation.ready; 78 79 // Unlike in the case of CSS animations, we cannot skip to the end and skip 80 // backwards since when we reach the end the transition effect is removed and 81 // changes to the Animation object no longer affect the element. For 82 // this reason we only skip forwards as far as the 50% through point. 83 84 animation.currentTime = 150 * MS_PER_SEC; 85 assert_equals(marginLeft(div), 150); 86 87 animation.currentTime = 100 * MS_PER_SEC; 88 assert_equals(marginLeft(div), 100); 89 }, 'Skipping backwards through transition'); 90 91 promise_test(async t => { 92 const div = addDiv(t, { 93 style: 'margin-left: 100px; transition: margin-left 100s linear 100s', 94 }); 95 getComputedStyle(div).marginLeft; 96 div.style.marginLeft = '200px'; 97 const animation = div.getAnimations()[0]; 98 99 await animation.ready; 100 101 assert_throws_js( 102 TypeError, 103 () => { 104 animation.currentTime = null; 105 }, 106 'Expect TypeError exception on trying to set Animation.currentTime to null' 107 ); 108 }, 'Setting currentTime to null on a CSS transition throws'); 109 110 test(t => { 111 const div = addDiv(t); 112 113 div.style.left = '0px'; 114 getComputedStyle(div).transitionProperty; 115 div.style.transition = 'left 100s ease-in'; 116 div.style.left = '100px'; 117 118 const transition = div.getAnimations()[0]; 119 120 // Seek to the middle. Note, this is not equivalent to 50% progress since the 121 // timing-function is non-linear. 122 transition.currentTime = 50 * MS_PER_SEC; 123 const portion = transition.effect.getComputedTiming().progress; 124 125 // Reverse the transition. 126 div.style.left = '0px'; 127 const reversedTransition = div.getAnimations()[0]; 128 129 // If the transition reversing behavior does not advance the previous 130 // transition to the time set by currentTime, start and end values will both 131 // be 0px and no transition will be produced. 132 assert_not_equals(reversedTransition, undefined, 133 "A reversed transition is produced"); 134 135 const expectedDuration = 100 * MS_PER_SEC * portion; 136 assert_approx_equals( 137 reversedTransition.effect.getComputedTiming().activeDuration, 138 expectedDuration, 139 1, 140 "The reversed transition has correctly reduced duration" 141 ); 142 }, "Transition reversing behavior respects currentTime and uses the " + 143 "transition's current position."); 144 145 </script>