Bug 1205938 part 1 - Add waitForCondition() function to SimpleTest. r=jmaher

This function is roughly copied from browser/base/content/test/general/head.js.
It has been widely used in browser chrome mochitests, and should be fine to
have it available to all mochitests via SimpleTest.

MozReview-Commit-ID: DhIfgJiUhXK

--HG--
extra : rebase_source : 95ab1cd6c1ab5f6c0d8f0171865722ca76b41c6e
This commit is contained in:
Xidorn Quan 2015-09-18 11:06:47 +08:00
Родитель 81b5667a9b
Коммит ebe6f7b562
2 изменённых файлов: 43 добавлений и 17 удалений

Просмотреть файл

@ -23,21 +23,6 @@
const utils = window.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIDOMWindowUtils);
function waitForCondition(condition, nextTest, errorMsg) {
var tries = 0;
var interval = setInterval(function() {
if (tries >= 30) {
ok(false, errorMsg);
moveOn();
}
if (condition()) {
moveOn();
}
tries++;
}, 100);
var moveOn = function() { clearInterval(interval); nextTest(); };
}
function go() {
var plugin = document.getElementById('plugin');
var objLoadingContent = SpecialPowers.wrap(plugin);
@ -52,7 +37,8 @@
objLoadingContent.playPlugin();
var condition = () => plugin.setColor !== undefined;
waitForCondition(condition, afterPluginActivation, "Waited too long for plugin to activate");
SimpleTest.waitForCondition(condition, afterPluginActivation,
"Waited too long for plugin to activate");
}
function afterPluginActivation() {
@ -77,7 +63,8 @@
synthesizeMouseAtCenter(plugin, {});
var condition = () => plugin.getMouseUpEventCount() > 0;
waitForCondition(condition, afterFirstClick, "Waited too long for plugin to receive the mouse click");
SimpleTest.waitForCondition(condition, afterFirstClick,
"Waited too long for plugin to receive the mouse click");
}
function afterFirstClick() {

Просмотреть файл

@ -973,6 +973,45 @@ SimpleTest.waitForClipboard = function(aExpectedStringOrValidatorFn, aSetupFn,
}, aFailureFn, "text/unicode");
}
/**
* Wait for a condition for a while (actually up to 3s here).
*
* @param aCond
* A function returns the result of the condition
* @param aCallback
* A function called after the condition is passed or timeout.
* @param aErrorMsg
* The message displayed when the condition failed to pass
* before timeout.
*/
SimpleTest.waitForCondition = function (aCond, aCallback, aErrorMsg) {
var tries = 0;
var interval = setInterval(() => {
if (tries >= 30) {
ok(false, aErrorMsg);
moveOn();
return;
}
var conditionPassed;
try {
conditionPassed = aCond();
} catch (e) {
ok(false, `${e}\n${e.stack}`);
conditionPassed = false;
}
if (conditionPassed) {
moveOn();
}
tries++;
}, 100);
var moveOn = () => { clearInterval(interval); aCallback(); };
};
SimpleTest.promiseWaitForCondition = function (aCond, aErrorMsg) {
return new Promise(resolve => {
this.waitForCondition(aCond, resolve, aErrorMsg);
});
};
/**
* Executes a function shortly after the call, but lets the caller continue
* working (or finish).