Bug 1246677 - 1 - Make waitForSuccess work with async functions draft
authorPatrick Brosset <pbrosset@mozilla.com>
Mon, 08 Feb 2016 17:38:57 +0100
changeset 330653 27d9cc1f54846b50670c5fe0af0785a0434924bd
parent 330652 75237a73cb18cc305054ff86c776381889960fba
child 330654 b45072b89826eff44fba8089948c611641f244e0
push id10801
push userpbrosset@mozilla.com
push dateFri, 12 Feb 2016 13:33:31 +0000
bugs1246677
milestone47.0a1
Bug 1246677 - 1 - Make waitForSuccess work with async functions MozReview-Commit-ID: Ic0UyApQpB7
devtools/client/inspector/rules/test/browser_rules_cubicbezier-commit-on-ENTER.js
devtools/client/inspector/rules/test/browser_rules_original-source-link.js
devtools/client/inspector/rules/test/head.js
--- a/devtools/client/inspector/rules/test/browser_rules_cubicbezier-commit-on-ENTER.js
+++ b/devtools/client/inspector/rules/test/browser_rules_cubicbezier-commit-on-ENTER.js
@@ -34,19 +34,19 @@ function* testPressingEnterCommitsChange
   swatch.click();
   yield onShown;
 
   let widget = yield bezierTooltip.widget;
   info("Simulating a change of curve in the widget");
   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
     .indexOf("cubic-bezier(") !== -1,
     "The text of the timing-function was updated");
 
   info("Sending RETURN key within the tooltip document");
   // Pressing RETURN ends up doing 2 rule-view updates, one for the preview and
--- a/devtools/client/inspector/rules/test/browser_rules_original-source-link.js
+++ b/devtools/client/inspector/rules/test/browser_rules_original-source-link.js
@@ -73,13 +73,12 @@ function editorSelected(editor) {
 
   let {line} = editor.sourceEditor.getCursor();
   is(line, 3, "cursor is at correct line number in original source");
 }
 
 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);
 }
--- a/devtools/client/inspector/rules/test/head.js
+++ b/devtools/client/inspector/rules/test/head.js
@@ -307,43 +307,44 @@ var waitForTab = Task.async(function*() 
   let tab = gBrowser.selectedTab;
   let browser = tab.linkedBrowser;
   yield once(browser, "load", true);
   info("The tab load completed");
   return tab;
 });
 
 /**
- * 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
  *        Optional name of the test. This is used to generate
  *        the success and failure messages.
  * @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
  * document's head node
  *
  * @param {Document} doc
  * @param {String} style
  * @return {DOMNode} The newly created style node
@@ -520,18 +521,19 @@ var simulateColorPickerChange = Task.asy
   info("Applying the change");
   spectrum.updateUI();
   spectrum.onChange();
   info("Waiting for rule-view to update");
   yield onRuleViewChanged;
 
   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");
   }
 });
 
 /**
  * Get a rule-link from the rule-view given its index
  *