1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
5 * Use this variable if you specify duration or some other properties
6 * for script animation.
7 * E.g., div.animate({ opacity: [0, 1] }, 100 * MS_PER_SEC);
9 * NOTE: Creating animations with short duration may cause intermittent
10 * failures in asynchronous test. For example, the short duration animation
11 * might be finished when animation.ready has been fulfilled because of slow
12 * platforms or busyness of the main thread.
13 * Setting short duration to cancel its animation does not matter but
14 * if you don't want to cancel the animation, consider using longer duration.
16 const MS_PER_SEC = 1000;
18 /* The recommended minimum precision to use for time values[1].
20 * [1] https://drafts.csswg.org/web-animations/#precision-of-time-values
22 var TIME_PRECISION = 0.0005; // ms
25 * Allow implementations to substitute an alternative method for comparing
26 * times based on their precision requirements.
28 function assert_times_equal(actual, expected, description) {
29 assert_approx_equals(actual, expected, TIME_PRECISION * 2, description);
33 * Compare a time value based on its precision requirements with a fixed value.
35 function assert_time_equals_literal(actual, expected, description) {
36 assert_approx_equals(actual, expected, TIME_PRECISION, description);
40 * Compare matrix string like 'matrix(1, 0, 0, 1, 100, 0)'.
41 * This function allows error, 0.01, because on Android when we are scaling down
42 * the document, it results in some errors.
44 function assert_matrix_equals(actual, expected, description) {
45 var matrixRegExp = /^matrix\((.+),(.+),(.+),(.+),(.+),(.+)\)/;
46 assert_regexp_match(actual, matrixRegExp, "Actual value should be a matrix");
50 "Expected value should be a matrix"
53 var actualMatrixArray = actual.match(matrixRegExp).slice(1).map(Number);
54 var expectedMatrixArray = expected.match(matrixRegExp).slice(1).map(Number);
57 actualMatrixArray.length,
58 expectedMatrixArray.length,
59 "Array lengths should be equal (got '" +
66 for (var i = 0; i < actualMatrixArray.length; i++) {
69 expectedMatrixArray[i],
71 "Matrix array should be equal (got '" +
82 * Compare given values which are same format of
83 * KeyframeEffectReadonly::GetProperties.
85 function assert_properties_equal(actual, expected) {
86 assert_equals(actual.length, expected.length);
88 const compareProperties = (a, b) =>
89 a.property == b.property ? 0 : a.property < b.property ? -1 : 1;
91 const sortedActual = actual.sort(compareProperties);
92 const sortedExpected = expected.sort(compareProperties);
94 const serializeValues = values =>
99 ["offset", "value", "easing", "composite"]
100 .map(member => `${member}: ${value[member]}`)
106 for (let i = 0; i < sortedActual.length; i++) {
108 sortedActual[i].property,
109 sortedExpected[i].property,
110 "CSS property name should match"
113 serializeValues(sortedActual[i].values),
114 serializeValues(sortedExpected[i].values),
115 `Values arrays do not match for ` + `${sortedActual[i].property} property`
121 * Construct a object which is same to a value of
122 * KeyframeEffectReadonly::GetProperties().
123 * The method returns undefined as a value in case of missing keyframe.
124 * Therefor, we can use undefined for |value| and |easing| parameter.
125 * @param offset - keyframe offset. e.g. 0.1
126 * @param value - any keyframe value. e.g. undefined '1px', 'center', 0.5
127 * @param composite - 'replace', 'add', 'accumulate'
128 * @param easing - e.g. undefined, 'linear', 'ease' and so on
130 * e.g. { offset: 0.1, value: '1px', composite: 'replace', easing: 'ease'}
132 function valueFormat(offset, value, composite, easing) {
133 return { offset, value, easing, composite };
137 * Appends a div to the document body and creates an animation on the div.
138 * NOTE: This function asserts when trying to create animations with durations
139 * shorter than 100s because the shorter duration may cause intermittent
140 * failures. If you are not sure how long it is suitable, use 100s; it's
141 * long enough but shorter than our test framework timeout (330s).
142 * If you really need to use shorter durations, use animate() function directly.
144 * @param t The testharness.js Test object. If provided, this will be used
145 * to register a cleanup callback to remove the div when the test
147 * @param attrs A dictionary object with attribute names and values to set on
149 * @param frames The keyframes passed to Element.animate().
150 * @param options The options passed to Element.animate().
152 function addDivAndAnimate(t, attrs, frames, options) {
153 let animDur = typeof options === "object" ? options.duration : options;
154 assert_greater_than_equal(
157 "Clients of this addDivAndAnimate API must request a duration " +
158 "of at least 100s, to avoid intermittent failures from e.g." +
159 "the main thread being busy for an extended period"
162 return addDiv(t, attrs).animate(frames, options);
166 * Appends a div to the document body.
168 * @param t The testharness.js Test object. If provided, this will be used
169 * to register a cleanup callback to remove the div when the test
172 * @param attrs A dictionary object with attribute names and values to set on
175 function addDiv(t, attrs) {
176 var div = document.createElement("div");
178 for (var attrName in attrs) {
179 div.setAttribute(attrName, attrs[attrName]);
182 document.body.appendChild(div);
183 if (t && typeof t.add_cleanup === "function") {
184 t.add_cleanup(function () {
185 if (div.parentNode) {
194 * Appends a style div to the document head.
196 * @param t The testharness.js Test object. If provided, this will be used
197 * to register a cleanup callback to remove the style element
198 * when the test finishes.
200 * @param rules A dictionary object with selector names and rules to set on
203 function addStyle(t, rules) {
204 var extraStyle = document.createElement("style");
205 document.head.appendChild(extraStyle);
207 var sheet = extraStyle.sheet;
208 for (var selector in rules) {
210 selector + "{" + rules[selector] + "}",
211 sheet.cssRules.length
216 if (t && typeof t.add_cleanup === "function") {
217 t.add_cleanup(function () {
224 * Takes a CSS property (e.g. margin-left) and returns the equivalent IDL
225 * name (e.g. marginLeft).
227 function propertyToIDL(property) {
228 var prefixMatch = property.match(/^-(\w+)-/);
230 var prefix = prefixMatch[1] === "moz" ? "Moz" : prefixMatch[1];
231 property = prefix + property.substring(prefixMatch[0].length - 1);
233 // https://drafts.csswg.org/cssom/#css-property-to-idl-attribute
234 return property.replace(/-([a-z])/gi, function (str, group) {
235 return group.toUpperCase();
240 * Promise wrapper for requestAnimationFrame.
242 function waitForFrame() {
243 return new Promise(function (resolve, reject) {
244 window.requestAnimationFrame(resolve);
249 * Waits for a requestAnimationFrame callback in the next refresh driver tick.
251 function waitForNextFrame(aWindow = window) {
252 const timeAtStart = aWindow.document.timeline.currentTime;
253 return new Promise(resolve => {
254 aWindow.requestAnimationFrame(() => {
255 if (timeAtStart === aWindow.document.timeline.currentTime) {
256 aWindow.requestAnimationFrame(resolve);
265 * Returns a Promise that is resolved after the given number of consecutive
266 * animation frames have occured (using requestAnimationFrame callbacks).
268 * @param aFrameCount The number of animation frames.
269 * @param aOnFrame An optional function to be processed in each animation frame.
270 * @param aWindow An optional window object to be used for requestAnimationFrame.
272 function waitForAnimationFrames(aFrameCount, aOnFrame, aWindow = window) {
273 const timeAtStart = aWindow.document.timeline.currentTime;
274 return new Promise(function (resolve, reject) {
275 function handleFrame() {
276 if (aOnFrame && typeof aOnFrame === "function") {
280 timeAtStart != aWindow.document.timeline.currentTime &&
285 aWindow.requestAnimationFrame(handleFrame); // wait another frame
288 aWindow.requestAnimationFrame(handleFrame);
293 * Promise wrapper for requestIdleCallback.
295 function waitForIdle() {
296 return new Promise(resolve => {
297 requestIdleCallback(resolve);
302 * Wrapper that takes a sequence of N animations and returns:
304 * Promise.all([animations[0].ready, animations[1].ready, ... animations[N-1].ready]);
306 function waitForAllAnimations(animations) {
308 animations.map(function (animation) {
309 return animation.ready;
315 * Flush the computed style for the given element. This is useful, for example,
316 * when we are testing a transition and need the initial value of a property
317 * to be computed so that when we synchronouslyet set it to a different value
318 * we actually get a transition instead of that being the initial value.
320 function flushComputedStyle(elem) {
321 var cs = getComputedStyle(elem);
326 for (var funcName of [
330 "assert_approx_equals",
332 "assert_less_than_equal",
333 "assert_greater_than",
334 "assert_between_inclusive",
337 "assert_class_string",
340 "assert_regexp_match",
344 if (opener[funcName]) {
345 window[funcName] = opener[funcName].bind(opener);
349 window.EventWatcher = opener.EventWatcher;
352 opener.add_completion_callback(function () {
360 * Returns a promise that is resolved when the document has finished loading.
362 function waitForDocumentLoad() {
363 return new Promise(function (resolve, reject) {
364 if (document.readyState === "complete") {
367 window.addEventListener("load", resolve);
373 * Enters test refresh mode, and restores the mode when |t| finishes.
375 function useTestRefreshMode(t) {
376 function ensureNoSuppressedPaints() {
377 return new Promise(resolve => {
378 function checkSuppressedPaints() {
379 if (!SpecialPowers.DOMWindowUtils.paintingSuppressed) {
382 window.requestAnimationFrame(checkSuppressedPaints);
385 checkSuppressedPaints();
389 return ensureNoSuppressedPaints().then(() => {
390 SpecialPowers.DOMWindowUtils.advanceTimeAndRefresh(0);
391 t.add_cleanup(() => {
392 SpecialPowers.DOMWindowUtils.restoreNormalRefresh();
398 * Returns true if off-main-thread animations.
400 function isOMTAEnabled() {
401 const OMTAPrefKey = "layers.offmainthreadcomposition.async-animations";
403 SpecialPowers.DOMWindowUtils.layerManagerRemote &&
404 SpecialPowers.getBoolPref(OMTAPrefKey)
409 * Append an SVG element to the target element.
411 * @param target The element which want to append.
412 * @param attrs A array object with attribute name and values to set on
414 * @return An SVG outer element.
416 function addSVGElement(target, tag, attrs) {
420 var element = document.createElementNS("http://www.w3.org/2000/svg", tag);
422 for (var attrName in attrs) {
423 element.setAttributeNS(null, attrName, attrs[attrName]);
426 target.appendChild(element);
431 * Get Animation distance between two specified values for a specific property.
433 * @param target The target element.
434 * @param prop The CSS property.
435 * @param v1 The first property value.
436 * @param v2 The Second property value.
438 * @return The distance between |v1| and |v2| for |prop| on |target|.
440 function getDistance(target, prop, v1, v2) {
444 return SpecialPowers.DOMWindowUtils.computeAnimationDistance(
453 * A promise wrapper for waiting MozAfterPaint.
455 function waitForPaints() {
456 // FIXME: Bug 1415065. Instead waiting for two requestAnimationFrames, we
457 // should wait for MozAfterPaint once after MozAfterPaint is fired properly
459 return waitForAnimationFrames(2);
462 // Returns true if |aAnimation| begins at the current timeline time. We
463 // sometimes need to detect this case because if we started an animation
464 // asynchronously (e.g. using play()) and then ended up running the next frame
465 // at precisely the time the animation started (due to aligning with vsync
466 // refresh rate) then we won't end up restyling in that frame.
467 function animationStartsRightNow(aAnimation) {
469 aAnimation.startTime === aAnimation.timeline.currentTime &&
470 aAnimation.currentTime === 0
474 // Waits for a given animation being ready to restyle.
475 async function waitForAnimationReadyToRestyle(aAnimation) {
476 await aAnimation.ready;
477 // If |aAnimation| begins at the current timeline time, we will not process
478 // restyling in the initial frame because of aligning with the refresh driver,
479 // the animation frame in which the ready promise is resolved happens to
480 // coincide perfectly with the start time of the animation. In this case no
481 // restyling is needed in the frame so we have to wait one more frame.
482 if (animationStartsRightNow(aAnimation)) {
483 await waitForNextFrame(aAnimation.ownerGlobal);
487 // Returns the animation restyle markers observed during |frameCount| refresh
488 // driver ticks in this `window`. This function is typically used to count the
489 // number of restyles that take place as part of the style update that happens
490 // on each refresh driver tick, as opposed to synchronous restyles triggered by
493 // For the latter observeAnimSyncStyling (below) should be used.
494 function observeStyling(frameCount, onFrame) {
495 return observeStylingInTargetWindow(window, frameCount, onFrame);
498 // As with observeStyling but applied to target window |aWindow|.
499 function observeStylingInTargetWindow(aWindow, aFrameCount, aOnFrame) {
500 let priorAnimationTriggeredRestyles =
501 SpecialPowers.wrap(aWindow).windowUtils.animationTriggeredRestyles;
503 return new Promise(resolve => {
504 return waitForAnimationFrames(aFrameCount, aOnFrame, aWindow).then(() => {
506 SpecialPowers.wrap(aWindow).windowUtils.animationTriggeredRestyles -
507 priorAnimationTriggeredRestyles;
509 resolve(restyleCount);