diff options
Diffstat (limited to 'tests/wpt/web-platform-tests/resources/testharness.js')
-rw-r--r-- | tests/wpt/web-platform-tests/resources/testharness.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/wpt/web-platform-tests/resources/testharness.js b/tests/wpt/web-platform-tests/resources/testharness.js index cf361bac392..7e2d4600f0f 100644 --- a/tests/wpt/web-platform-tests/resources/testharness.js +++ b/tests/wpt/web-platform-tests/resources/testharness.js @@ -2035,6 +2035,105 @@ policies and contribution forms [3]. return setTimeout(this.step_func(function() { return f.apply(test_this, args); }), timeout * tests.timeout_multiplier); + }; + + Test.prototype.step_wait_func = function(cond, func, description, + timeout=3000, interval=100) { + /** + * Poll for a function to return true, and call a callback + * function once it does, or assert if a timeout is + * reached. This is preferred over a simple step_timeout + * whenever possible since it allows the timeout to be longer + * to reduce intermittents without compromising test execution + * speed when the condition is quickly met. + * + * @param {Function} cond A function taking no arguments and + * returning a boolean. The callback is called + * when this function returns true. + * @param {Function} func A function taking no arguments to call once + * the condition is met. + * @param {string} description Error message to add to assert in case of + * failure. + * @param {number} timeout Timeout in ms. This is multiplied by the global + * timeout_multiplier + * @param {number} interval Polling interval in ms + * + **/ + + var timeout_full = timeout * tests.timeout_multiplier; + var remaining = Math.ceil(timeout_full / interval); + var test_this = this; + + var wait_for_inner = test_this.step_func(() => { + if (cond()) { + func(); + } else { + if(remaining === 0) { + assert(false, "wait_for", description, + "Timed out waiting on condition"); + } + remaining--; + setTimeout(wait_for_inner, interval); + } + }); + + wait_for_inner(); + }; + + Test.prototype.step_wait_func_done = function(cond, func, description, + timeout=3000, interval=100) { + /** + * Poll for a function to return true, and invoke a callback + * followed this.done() once it does, or assert if a timeout + * is reached. This is preferred over a simple step_timeout + * whenever possible since it allows the timeout to be longer + * to reduce intermittents without compromising test execution speed + * when the condition is quickly met. + * + * @param {Function} cond A function taking no arguments and + * returning a boolean. The callback is called + * when this function returns true. + * @param {Function} func A function taking no arguments to call once + * the condition is met. + * @param {string} description Error message to add to assert in case of + * failure. + * @param {number} timeout Timeout in ms. This is multiplied by the global + * timeout_multiplier + * @param {number} interval Polling interval in ms + * + **/ + + this.step_wait_func(cond, () => { + if (func) { + func(); + } + this.done(); + }, description, timeout, interval); + } + + Test.prototype.step_wait = function(cond, description, timeout=3000, interval=100) { + /** + * Poll for a function to return true, and resolve a promise + * once it does, or assert if a timeout is reached. This is + * preferred over a simple step_timeout whenever possible + * since it allows the timeout to be longer to reduce + * intermittents without compromising test execution speed + * when the condition is quickly met. + * + * @param {Function} cond A function taking no arguments and + * returning a boolean. + * @param {string} description Error message to add to assert in case of + * failure. + * @param {number} timeout Timeout in ms. This is multiplied by the global + * timeout_multiplier + * @param {number} interval Polling interval in ms + * @returns {Promise} Promise resolved once cond is met. + * + **/ + + return new Promise(resolve => { + this.step_wait_func(cond, resolve, description, timeout, interval); + }); } /* |