134 lines
3.9 KiB
JavaScript
134 lines
3.9 KiB
JavaScript
|
/* Any copyright is dedicated to the Public Domain.
|
||
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||
|
|
||
|
/**
|
||
|
* Appends a div to the document body.
|
||
|
*
|
||
|
* @param t The testharness.js Test object. If provided, this will be used
|
||
|
* to register a cleanup callback to remove the div when the test
|
||
|
* finishes.
|
||
|
*
|
||
|
* @param attrs A dictionary object with attribute names and values to set on
|
||
|
* the div.
|
||
|
*/
|
||
|
function addDiv(t, attrs) {
|
||
|
var div = document.createElement('div');
|
||
|
if (attrs) {
|
||
|
for (var attrName in attrs) {
|
||
|
div.setAttribute(attrName, attrs[attrName]);
|
||
|
}
|
||
|
}
|
||
|
document.body.appendChild(div);
|
||
|
if (t && typeof t.add_cleanup === 'function') {
|
||
|
t.add_cleanup(function() {
|
||
|
if (div.parentNode) {
|
||
|
div.parentNode.removeChild(div);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
return div;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Appends a style div to the document head.
|
||
|
*
|
||
|
* @param t The testharness.js Test object. If provided, this will be used
|
||
|
* to register a cleanup callback to remove the style element
|
||
|
* when the test finishes.
|
||
|
*
|
||
|
* @param rules A dictionary object with selector names and rules to set on
|
||
|
* the style sheet.
|
||
|
*/
|
||
|
function addStyle(t, rules) {
|
||
|
var extraStyle = document.createElement('style');
|
||
|
document.head.appendChild(extraStyle);
|
||
|
if (rules) {
|
||
|
var sheet = extraStyle.sheet;
|
||
|
for (var selector in rules) {
|
||
|
sheet.insertRule(selector + '{' + rules[selector] + '}',
|
||
|
sheet.cssRules.length);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (t && typeof t.add_cleanup === 'function') {
|
||
|
t.add_cleanup(function() {
|
||
|
extraStyle.remove();
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Promise wrapper for requestAnimationFrame.
|
||
|
*/
|
||
|
function waitForFrame() {
|
||
|
return new Promise(function(resolve, reject) {
|
||
|
window.requestAnimationFrame(resolve);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a Promise that is resolved after the given number of consecutive
|
||
|
* animation frames have occured (using requestAnimationFrame callbacks).
|
||
|
*
|
||
|
* @param frameCount The number of animation frames.
|
||
|
* @param onFrame An optional function to be processed in each animation frame.
|
||
|
*/
|
||
|
function waitForAnimationFrames(frameCount, onFrame) {
|
||
|
return new Promise(function(resolve, reject) {
|
||
|
function handleFrame() {
|
||
|
if (--frameCount <= 0) {
|
||
|
resolve();
|
||
|
} else {
|
||
|
if (onFrame && typeof onFrame === 'function') {
|
||
|
onFrame();
|
||
|
}
|
||
|
window.requestAnimationFrame(handleFrame); // wait another frame
|
||
|
}
|
||
|
}
|
||
|
window.requestAnimationFrame(handleFrame);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Wrapper that takes a sequence of N animations and returns:
|
||
|
*
|
||
|
* Promise.all([animations[0].ready, animations[1].ready, ... animations[N-1].ready]);
|
||
|
*/
|
||
|
function waitForAllAnimations(animations) {
|
||
|
return Promise.all(animations.map(function(animation) {
|
||
|
return animation.ready;
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Flush the computed style for the given element. This is useful, for example,
|
||
|
* when we are testing a transition and need the initial value of a property
|
||
|
* to be computed so that when we synchronouslyet set it to a different value
|
||
|
* we actually get a transition instead of that being the initial value.
|
||
|
*/
|
||
|
function flushComputedStyle(elem) {
|
||
|
var cs = window.getComputedStyle(elem);
|
||
|
cs.marginLeft;
|
||
|
}
|
||
|
|
||
|
if (opener) {
|
||
|
for (var funcName of ["async_test", "assert_not_equals", "assert_equals",
|
||
|
"assert_approx_equals", "assert_less_than",
|
||
|
"assert_less_than_equal", "assert_greater_than",
|
||
|
"assert_between_inclusive",
|
||
|
"assert_true", "assert_false",
|
||
|
"assert_class_string", "assert_throws",
|
||
|
"assert_unreached", "promise_test", "test"]) {
|
||
|
window[funcName] = opener[funcName].bind(opener);
|
||
|
}
|
||
|
|
||
|
window.EventWatcher = opener.EventWatcher;
|
||
|
|
||
|
function done() {
|
||
|
opener.add_completion_callback(function() {
|
||
|
self.close();
|
||
|
});
|
||
|
opener.done();
|
||
|
}
|
||
|
}
|