Bug 1246677 - 1 - Make waitForSuccess work with async functions; r=miker

MozReview-Commit-ID: Ic0UyApQpB7

--HG--
extra : rebase_source : da22978c67e456ab5ba9fedecd228682cdd10ba5
This commit is contained in:
Patrick Brosset 2016-02-08 17:38:57 +01:00
parent 7c66c16a79
commit a932228b1a
3 changed files with 23 additions and 22 deletions

View File

@ -39,9 +39,9 @@ function* testPressingEnterCommitsChanges(swatch, ruleView) {
widget.coordinates = [0.1, 2, 0.9, -1];
let expected = "cubic-bezier(0.1, 2, 0.9, -1)";
yield waitForSuccess(() => {
yield waitForSuccess(function*() {
return content.getComputedStyle(content.document.body)
.transitionTimingFunction === expected;
.transitionTimingFunction === expected;
}, "Waiting for the change to be previewed on the element");
ok(getRuleViewProperty(ruleView, "body", "transition").valueSpan.textContent

View File

@ -78,8 +78,7 @@ function editorSelected(editor) {
function verifyLinkText(text, view) {
info("Verifying that the rule-view stylesheet link is " + text);
let label = getRuleViewLinkByIndex(view, 1).querySelector("label");
return waitForSuccess(
() => label.getAttribute("value") == text,
"Link text changed to display correct location: " + text
);
return waitForSuccess(function*() {
return label.getAttribute("value") == text;
}, "Link text changed to display correct location: " + text);
}

View File

@ -312,10 +312,10 @@ var waitForTab = Task.async(function*() {
});
/**
* Polls a given function waiting for it to return true.
* Polls a given generator function waiting for it to return true.
*
* @param {Function} validatorFn
* A validator function that returns a boolean.
* A validator generator function that returns a boolean.
* This is called every few milliseconds to check if the result is true.
* When it is true, the promise resolves.
* @param {String} name
@ -324,21 +324,22 @@ var waitForTab = Task.async(function*() {
* @return a promise that resolves when the function returned true or rejects
* if the timeout is reached
*/
function waitForSuccess(validatorFn, name="untitled") {
let def = promise.defer();
function wait(validator) {
if (validator()) {
ok(true, "Validator function " + name + " returned true");
def.resolve();
} else {
setTimeout(() => wait(validator), 200);
var waitForSuccess = Task.async(function*(validatorFn, desc = "untitled") {
let i = 0;
while (true) {
info("Checking: " + desc);
if (yield validatorFn()) {
ok(true, "Success: " + desc);
break;
}
i++;
if (i > 10) {
ok(false, "Failure: " + desc);
break;
}
yield new Promise(r => setTimeout(r, 200));
}
wait(validatorFn);
return def.promise;
}
});
/**
* Create a new style tag containing the given style text and append it to the
@ -525,8 +526,9 @@ var simulateColorPickerChange = Task.async(function*(ruleView, colorPicker,
if (expectedChange) {
info("Waiting for the style to be applied on the page");
yield waitForSuccess(() => {
yield waitForSuccess(function*() {
let {element, name, value} = expectedChange;
yield getComputedStyleProperty(selector, null, name)
return content.getComputedStyle(element)[name] === value;
}, "Color picker change applied on the page");
}