| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 Distributed under both the W3C Test Suite License [1] and the W3C | |
| 3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the | |
| 4 policies and contribution forms [3]. | |
| 5 | |
| 6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license | |
| 7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license | |
| 8 [3] http://www.w3.org/2004/10/27-testcases | |
| 9 */ | |
| 10 | |
| 11 "use strict"; | |
| 12 var MS_PER_SEC = 1000; | |
| 13 | |
| 14 // creates div element, appends it to the document body and | |
| 15 // removes the created element during test cleanup | |
| 16 function createDiv(test, doc) { | |
| 17 if (!doc) { | |
| 18 doc = document; | |
| 19 } | |
| 20 var div = doc.createElement('div'); | |
| 21 doc.body.appendChild(div); | |
| 22 test.add_cleanup(function() { | |
| 23 div.remove(); | |
| 24 }); | |
| 25 return div; | |
| 26 } | |
| 27 | |
| 28 // Creates a style element with the specified rules, appends it to the document | |
| 29 // head and removes the created element during test cleanup. | |
| 30 // |rules| is an object. For example: | |
| 31 // { '@keyframes anim': '' , | |
| 32 // '.className': 'animation: anim 100s;' }; | |
| 33 // or | |
| 34 // { '.className1::before': 'content: ""; width: 0px; transition: all 10s;', | |
| 35 // '.className2::before': 'width: 100px;' }; | |
| 36 // The object property name could be a keyframes name, or a selector. | |
| 37 // The object property value is declarations which are property:value pairs | |
| 38 // split by a space. | |
| 39 function createStyle(test, rules, doc) { | |
| 40 if (!doc) { | |
| 41 doc = document; | |
| 42 } | |
| 43 var extraStyle = doc.createElement('style'); | |
| 44 doc.head.appendChild(extraStyle); | |
| 45 if (rules) { | |
| 46 var sheet = extraStyle.sheet; | |
| 47 for (var selector in rules) { | |
| 48 sheet.insertRule(selector + '{' + rules[selector] + '}', | |
| 49 sheet.cssRules.length); | |
| 50 } | |
| 51 } | |
| 52 test.add_cleanup(function() { | |
| 53 extraStyle.remove(); | |
| 54 }); | |
| 55 } | |
| 56 | |
| 57 // Create a pseudo element | |
| 58 function createPseudo(test, type) { | |
| 59 createStyle(test, { '@keyframes anim': '', | |
| 60 ['.pseudo::' + type]: 'animation: anim 10s;' }); | |
| 61 var div = createDiv(test); | |
| 62 div.classList.add('pseudo'); | |
| 63 var anims = document.getAnimations(); | |
| 64 assert_true(anims.length >= 1); | |
| 65 var anim = anims[anims.length - 1]; | |
| 66 assert_equals(anim.effect.target.parentElement, div); | |
| 67 assert_equals(anim.effect.target.type, '::' + type); | |
| 68 anim.cancel(); | |
| 69 return anim.effect.target; | |
| 70 } | |
| 71 | |
| 72 // Convert px unit value to a Number | |
| 73 function pxToNum(str) { | |
| 74 return Number(String(str).match(/^(-?[\d.]+)px$/)[1]); | |
| 75 } | |
| 76 | |
| 77 // Cubic bezier with control points (0, 0), (x1, y1), (x2, y2), and (1, 1). | |
| 78 function cubicBezier(x1, y1, x2, y2) { | |
| 79 function xForT(t) { | |
| 80 var omt = 1-t; | |
| 81 return 3 * omt * omt * t * x1 + 3 * omt * t * t * x2 + t * t * t; | |
| 82 } | |
| 83 | |
| 84 function yForT(t) { | |
| 85 var omt = 1-t; | |
| 86 return 3 * omt * omt * t * y1 + 3 * omt * t * t * y2 + t * t * t; | |
| 87 } | |
| 88 | |
| 89 function tForX(x) { | |
| 90 // Binary subdivision. | |
| 91 var mint = 0, maxt = 1; | |
| 92 for (var i = 0; i < 30; ++i) { | |
| 93 var guesst = (mint + maxt) / 2; | |
| 94 var guessx = xForT(guesst); | |
| 95 if (x < guessx) { | |
| 96 maxt = guesst; | |
| 97 } else { | |
| 98 mint = guesst; | |
| 99 } | |
| 100 } | |
| 101 return (mint + maxt) / 2; | |
| 102 } | |
| 103 | |
| 104 return function bezierClosure(x) { | |
| 105 if (x == 0) { | |
| 106 return 0; | |
| 107 } | |
| 108 if (x == 1) { | |
| 109 return 1; | |
| 110 } | |
| 111 return yForT(tForX(x)); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 function stepEnd(nsteps) { | |
| 116 return function stepEndClosure(x) { | |
| 117 return Math.floor(x * nsteps) / nsteps; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 function stepStart(nsteps) { | |
| 122 return function stepStartClosure(x) { | |
| 123 var result = Math.floor(x * nsteps + 1.0) / nsteps; | |
| 124 return (result > 1.0) ? 1.0 : result; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 function waitForAnimationFrames(frameCount) { | |
| 129 return new Promise(function(resolve, reject) { | |
| 130 function handleFrame() { | |
| 131 if (--frameCount <= 0) { | |
| 132 resolve(); | |
| 133 } else { | |
| 134 window.requestAnimationFrame(handleFrame); // wait another frame | |
| 135 } | |
| 136 } | |
| 137 window.requestAnimationFrame(handleFrame); | |
| 138 }); | |
| 139 } | |
| OLD | NEW |