Bug 1428845 - Removed duplicate definition of waitForCondition(). r?johannh draft
authorRofael Aleezada <me@rofael.com>
Mon, 22 Jan 2018 18:50:32 -0600
changeset 724326 2ff70ddd41e37348af14a18f9ddf82bf8632316f
parent 723788 c5461973d6ee7845b3f560c05e1502429fd63184
child 747137 a0213f0c3bc8f16ef8c23d38e53fd352d98fbc14
push id96732
push userbmo:me@rofael.com
push dateWed, 24 Jan 2018 21:58:01 +0000
reviewersjohannh
bugs1428845
milestone60.0a1
Bug 1428845 - Removed duplicate definition of waitForCondition(). r?johannh MozReview-Commit-ID: UCZwR5eRc5
browser/components/preferences/in-content/tests/browser_applications_selection.js
browser/components/preferences/in-content/tests/browser_change_app_handler.js
browser/components/preferences/in-content/tests/browser_cookies_dialog.js
browser/components/preferences/in-content/tests/browser_password_management.js
browser/components/preferences/in-content/tests/browser_permissions_dialog.js
browser/components/preferences/in-content/tests/browser_site_login_exceptions.js
browser/components/preferences/in-content/tests/head.js
testing/modules/TestUtils.jsm
--- a/browser/components/preferences/in-content/tests/browser_applications_selection.js
+++ b/browser/components/preferences/in-content/tests/browser_applications_selection.js
@@ -28,31 +28,31 @@ add_task(async function getFeedItem() {
 
 add_task(async function selectInternalOptionForFeed() {
   // Select the item.
   feedItem.scrollIntoView();
   container.selectItem(feedItem);
   Assert.ok(feedItem.selected, "Should be able to select our item.");
 
   // Wait for the menu.
-  let list = await waitForCondition(() =>
+  let list = await TestUtils.waitForCondition(() =>
     win.document.getAnonymousElementByAttribute(feedItem, "class", "actionsMenu"));
   info("Got list after item was selected");
 
   // Find the "Add Live bookmarks option".
   let chooseItems = list.getElementsByAttribute("action", Ci.nsIHandlerInfo.handleInternally);
   Assert.equal(chooseItems.length, 1, "Should only be one action to handle internally");
 
   // Select the option.
   let cmdEvent = win.document.createEvent("xulcommandevent");
   cmdEvent.initCommandEvent("command", true, true, win, 0, false, false, false, false, null, 0);
   chooseItems[0].dispatchEvent(cmdEvent);
 
   // Check that we display the correct result.
-  list = await waitForCondition(() =>
+  list = await TestUtils.waitForCondition(() =>
     win.document.getAnonymousElementByAttribute(feedItem, "class", "actionsMenu"));
   info("Got list after item was selected");
   Assert.ok(list.selectedItem, "Should have a selected item.");
   Assert.equal(list.selectedItem.getAttribute("action"),
                Ci.nsIHandlerInfo.handleInternally,
                "Newly selected item should be the expected one.");
 });
 
@@ -60,24 +60,24 @@ add_task(async function selectInternalOp
 add_task(async function reselectInternalOptionForFeed() {
   // Now select a different option in the list - use the pdf item as that doesn't
   // need to load any favicons.
   let anotherItem = container.querySelector("richlistitem[type='application/pdf']");
 
   container.selectItem(anotherItem);
 
   // Wait for the menu so that we don't hit race conditions.
-  await waitForCondition(() =>
+  await TestUtils.waitForCondition(() =>
     win.document.getAnonymousElementByAttribute(anotherItem, "class", "actionsMenu"));
   info("Got list after item was selected");
 
   // Now select the feed item again, and check what it is displaying.
   container.selectItem(feedItem);
 
-  let list = await waitForCondition(() =>
+  let list = await TestUtils.waitForCondition(() =>
     win.document.getAnonymousElementByAttribute(feedItem, "class", "actionsMenu"));
   info("Got list after item was selected");
 
   Assert.ok(list.selectedItem,
             "Should have a selected item");
   Assert.equal(list.selectedItem.getAttribute("action"),
                Ci.nsIHandlerInfo.handleInternally,
                "Selected item should still be the same as the previously selected item.");
--- a/browser/components/preferences/in-content/tests/browser_change_app_handler.js
+++ b/browser/components/preferences/in-content/tests/browser_change_app_handler.js
@@ -24,17 +24,17 @@ add_task(async function() {
 
   let container = win.document.getElementById("handlersView");
   let ourItem = container.querySelector("richlistitem[type='text/x-test-handler']");
   ok(ourItem, "handlersView is present");
   ourItem.scrollIntoView();
   container.selectItem(ourItem);
   ok(ourItem.selected, "Should be able to select our item.");
 
-  let list = await waitForCondition(() => win.document.getAnonymousElementByAttribute(ourItem, "class", "actionsMenu"));
+  let list = await TestUtils.waitForCondition(() => win.document.getAnonymousElementByAttribute(ourItem, "class", "actionsMenu"));
   info("Got list after item was selected");
 
   let chooseItem = list.firstChild.querySelector(".choose-app-item");
   let dialogLoadedPromise = promiseLoadSubDialog("chrome://global/content/appPicker.xul");
   let cmdEvent = win.document.createEvent("xulcommandevent");
   cmdEvent.initCommandEvent("command", true, true, win, 0, false, false, false, false, null, 0);
   chooseItem.dispatchEvent(cmdEvent);
 
@@ -47,17 +47,17 @@ add_task(async function() {
   let selectedApp = dialogList.firstChild.handlerApp;
   dialogDoc.documentElement.acceptDialog();
 
   // Verify results are correct in mime service:
   let mimeInfo = gMimeSvc.getFromTypeAndExtension("text/x-test-handler", null);
   ok(mimeInfo.preferredApplicationHandler.equals(selectedApp), "App should be set as preferred.");
 
   // Check that we display this result:
-  list = await waitForCondition(() => win.document.getAnonymousElementByAttribute(ourItem, "class", "actionsMenu"));
+  list = await TestUtils.waitForCondition(() => win.document.getAnonymousElementByAttribute(ourItem, "class", "actionsMenu"));
   info("Got list after item was selected");
   ok(list.selectedItem, "Should have a selected item");
   ok(mimeInfo.preferredApplicationHandler.equals(list.selectedItem.handlerApp),
      "App should be visible as preferred item.");
 
 
   // Now try to 'manage' this list:
   dialogLoadedPromise = promiseLoadSubDialog("chrome://browser/content/preferences/applicationManager.xul");
@@ -80,17 +80,17 @@ add_task(async function() {
   is(dialogList.children.length, itemsBefore - 1, "Item got removed");
   dialogDoc.documentElement.acceptDialog();
 
   // Verify results are correct in mime service:
   mimeInfo = gMimeSvc.getFromTypeAndExtension("text/x-test-handler", null);
   ok(!mimeInfo.preferredApplicationHandler, "App should no longer be set as preferred.");
 
   // Check that we display this result:
-  list = await waitForCondition(() => win.document.getAnonymousElementByAttribute(ourItem, "class", "actionsMenu"));
+  list = await TestUtils.waitForCondition(() => win.document.getAnonymousElementByAttribute(ourItem, "class", "actionsMenu"));
   ok(list.selectedItem, "Should have a selected item");
   ok(!list.selectedItem.handlerApp,
      "No app should be visible as preferred item.");
 
   let tabRemovedPromise = BrowserTestUtils.tabRemoved(gBrowser.selectedTab);
   gBrowser.removeCurrentTab();
   await tabRemovedPromise;
 });
--- a/browser/components/preferences/in-content/tests/browser_cookies_dialog.js
+++ b/browser/components/preferences/in-content/tests/browser_cookies_dialog.js
@@ -33,17 +33,17 @@ add_task(async function testDeleteCookie
   tree.view.selection.select(0);
 
   if (AppConstants.platform == "macosx") {
     EventUtils.synthesizeKey("VK_BACK_SPACE", {});
   } else {
     EventUtils.synthesizeKey("VK_DELETE", {});
   }
 
-  await waitForCondition(() => tree.view.rowCount == 0);
+  await TestUtils.waitForCondition(() => tree.view.rowCount == 0);
 
   // eslint-disable-next-line mozilla/no-cpows-in-tests
   is_element_visible(content.gSubDialog._dialogs[0]._box,
     "Subdialog is visible after deleting an element");
 
 });
 
 add_task(async function removeTab() {
--- a/browser/components/preferences/in-content/tests/browser_password_management.js
+++ b/browser/components/preferences/in-content/tests/browser_password_management.js
@@ -59,14 +59,14 @@ add_task(async function test_deletePassw
   tree.view.selection.select(0);
 
   if (AppConstants.platform == "macosx") {
     EventUtils.synthesizeKey("VK_BACK_SPACE", {});
   } else {
     EventUtils.synthesizeKey("VK_DELETE", {});
   }
 
-  await waitForCondition(() => tree.view.rowCount == 0);
+  await TestUtils.waitForCondition(() => tree.view.rowCount == 0);
 
   // eslint-disable-next-line mozilla/no-cpows-in-tests
   is_element_visible(content.gSubDialog._dialogs[0]._box,
     "Subdialog is visible after deleting an element");
 });
--- a/browser/components/preferences/in-content/tests/browser_permissions_dialog.js
+++ b/browser/components/preferences/in-content/tests/browser_permissions_dialog.js
@@ -90,66 +90,66 @@ add_task(async function onPermissionChan
   doc.getElementsByAttribute("value", SitePermissions.BLOCK)[0].click();
 
   Assert.equal(SitePermissions.get(URI, "desktop-notification").state,
                SitePermissions.ALLOW,
                "Permission state does not change before saving changes");
 
   doc.getElementById("btnApplyChanges").click();
 
-  await waitForCondition(() =>
+  await TestUtils.waitForCondition(() =>
     SitePermissions.get(URI, "desktop-notification").state == SitePermissions.BLOCK);
 
   SitePermissions.remove(URI, "desktop-notification");
 });
 
 add_task(async function onPermissionDelete() {
   await openPermissionsDialog();
 
   let doc = sitePermissionsDialog.document;
   let richlistbox = doc.getElementById("permissionsBox");
 
   SitePermissions.set(URI, "desktop-notification", SitePermissions.ALLOW);
 
   richlistbox.selectItem(richlistbox.getItemAtIndex(0));
   doc.getElementById("removePermission").click();
 
-  await waitForCondition(() => richlistbox.itemCount == 0);
+  await TestUtils.waitForCondition(() => richlistbox.itemCount == 0);
 
   Assert.equal(SitePermissions.get(URI, "desktop-notification").state,
                SitePermissions.ALLOW,
                "Permission is not deleted before saving changes");
 
   doc.getElementById("btnApplyChanges").click();
 
-  await waitForCondition(() =>
+  await TestUtils.waitForCondition(() =>
     SitePermissions.get(URI, "desktop-notification").state == SitePermissions.UNKNOWN);
 });
 
 add_task(async function onAllPermissionsDelete() {
   await openPermissionsDialog();
 
   let doc = sitePermissionsDialog.document;
   let richlistbox = doc.getElementById("permissionsBox");
 
   SitePermissions.set(URI, "desktop-notification", SitePermissions.ALLOW);
   let u = Services.io.newURI("http://www.test.com");
   SitePermissions.set(u, "desktop-notification", SitePermissions.ALLOW);
 
   doc.getElementById("removeAllPermissions").click();
-  await waitForCondition(() => richlistbox.itemCount == 0);
+  await TestUtils.waitForCondition(() => richlistbox.itemCount == 0);
 
   Assert.equal(SitePermissions.get(URI, "desktop-notification").state,
      SitePermissions.ALLOW);
   Assert.equal(SitePermissions.get(u, "desktop-notification").state,
     SitePermissions.ALLOW, "Permissions are not deleted before saving changes");
 
   doc.getElementById("btnApplyChanges").click();
 
-  await waitForCondition(() =>
+  await TestUtils.waitForCondition(() =>
     (SitePermissions.get(URI, "desktop-notification").state == SitePermissions.UNKNOWN) &&
       (SitePermissions.get(u, "desktop-notification").state == SitePermissions.UNKNOWN));
 });
 
 add_task(async function onPermissionChangeAndDelete() {
   await openPermissionsDialog();
 
   let doc = sitePermissionsDialog.document;
@@ -159,21 +159,21 @@ add_task(async function onPermissionChan
 
   // Change the permission state in the UI.
   doc.getElementsByAttribute("value", SitePermissions.BLOCK)[0].click();
 
   // Remove that permission by clicking the "Remove" button.
   richlistbox.selectItem(richlistbox.getItemAtIndex(0));
   doc.getElementById("removePermission").click();
 
-  await waitForCondition(() => richlistbox.itemCount == 0);
+  await TestUtils.waitForCondition(() => richlistbox.itemCount == 0);
 
   doc.getElementById("btnApplyChanges").click();
 
-  await waitForCondition(() =>
+  await TestUtils.waitForCondition(() =>
     SitePermissions.get(URI, "desktop-notification").state == SitePermissions.UNKNOWN);
 });
 
 add_task(async function onPermissionChangeCancel() {
   await openPermissionsDialog();
 
   let doc = sitePermissionsDialog.document;
   SitePermissions.set(URI, "desktop-notification", SitePermissions.ALLOW);
@@ -196,17 +196,17 @@ add_task(async function onPermissionDele
   let doc = sitePermissionsDialog.document;
   let richlistbox = doc.getElementById("permissionsBox");
   SitePermissions.set(URI, "desktop-notification", SitePermissions.ALLOW);
 
   // Remove that permission by clicking the "Remove" button.
   richlistbox.selectItem(richlistbox.getItemAtIndex(0));
   doc.getElementById("removePermission").click();
 
-  await waitForCondition(() => richlistbox.itemCount == 0);
+  await TestUtils.waitForCondition(() => richlistbox.itemCount == 0);
 
   doc.getElementById("cancel").click();
 
   Assert.equal(SitePermissions.get(URI, "desktop-notification").state,
                SitePermissions.ALLOW,
                "Permission state does not change on clicking cancel");
 
   SitePermissions.remove(URI, "desktop-notification");
--- a/browser/components/preferences/in-content/tests/browser_site_login_exceptions.js
+++ b/browser/components/preferences/in-content/tests/browser_site_login_exceptions.js
@@ -44,17 +44,17 @@ add_task(async function addALoginExcepti
   let inputBox = doc.getElementById("url");
   inputBox.focus();
 
   EventUtils.sendString("www.example.com", exceptionsDialog);
 
   let btnBlock = doc.getElementById("btnBlock");
   btnBlock.click();
 
-  await waitForCondition(() => tree.view.rowCount == 1);
+  await TestUtils.waitForCondition(() => tree.view.rowCount == 1);
 
   Assert.equal(tree.view.getCellText(0, tree.treeBoxObject.columns.getColumnAt(0)),
                "http://www.example.com");
 });
 
 add_task(async function deleteALoginException() {
   let doc = exceptionsDialog.document;
 
@@ -64,14 +64,14 @@ add_task(async function deleteALoginExce
   tree.view.selection.select(0);
 
   if (AppConstants.platform == "macosx") {
     EventUtils.synthesizeKey("VK_BACK_SPACE", {});
   } else {
     EventUtils.synthesizeKey("VK_DELETE", {});
   }
 
-  await waitForCondition(() => tree.view.rowCount == 0);
+  await TestUtils.waitForCondition(() => tree.view.rowCount == 0);
 
   // eslint-disable-next-line mozilla/no-cpows-in-tests
   is_element_visible(content.gSubDialog._dialogs[0]._box,
     "Subdialog is visible after deleting an element");
 });
--- a/browser/components/preferences/in-content/tests/head.js
+++ b/browser/components/preferences/in-content/tests/head.js
@@ -137,37 +137,16 @@ function openPreferencesViaOpenPreferenc
           gBrowser.removeCurrentTab();
         resolve({ selectedPane });
       }, { once: true });
     }, { capture: true, once: true });
 
   });
 }
 
-function waitForCondition(aConditionFn, aMaxTries = 50, aCheckInterval = 100) {
-  return new Promise((resolve, reject) => {
-    function tryNow() {
-      tries++;
-      let rv = aConditionFn();
-      if (rv) {
-        resolve(rv);
-      } else if (tries < aMaxTries) {
-        tryAgain();
-      } else {
-        reject("Condition timed out: " + aConditionFn.toSource());
-      }
-    }
-    function tryAgain() {
-      setTimeout(tryNow, aCheckInterval);
-    }
-    let tries = 0;
-    tryAgain();
-  });
-}
-
 function promiseWindowDialogOpen(buttonAction, url) {
   return new Promise(resolve => {
     Services.ww.registerNotification(function onOpen(subj, topic, data) {
       if (topic == "domwindowopened" && subj instanceof Ci.nsIDOMWindow) {
         subj.addEventListener("load", function onLoad() {
           if (subj.document.documentURI == url) {
             Services.ww.unregisterNotification(onOpen);
             let doc = subj.document.documentElement;
--- a/testing/modules/TestUtils.jsm
+++ b/testing/modules/TestUtils.jsm
@@ -105,17 +105,17 @@ this.TestUtils = {
    * @param interval
    *        The time interval to poll the condition function. Defaults
    *        to 100ms.
    * @param attempts
    *        The number of times to poll before giving up and rejecting
    *        if the condition has not yet returned true. Defaults to 50
    *        (~5 seconds for 100ms intervals)
    * @return Promise
-   *        Resolves when condition is true.
+   *        Resolves with the return value of the condition function.
    *        Rejects if timeout is exceeded or condition ever throws.
    */
   waitForCondition(condition, msg, interval = 100, maxTries = 50) {
     return new Promise((resolve, reject) => {
       let tries = 0;
       let intervalID = setInterval(async function() {
         if (tries >= maxTries) {
           clearInterval(intervalID);
@@ -131,17 +131,17 @@ this.TestUtils = {
           msg += ` - threw exception: ${e}`;
           clearInterval(intervalID);
           reject(msg);
           return;
         }
 
         if (conditionPassed) {
           clearInterval(intervalID);
-          resolve();
+          resolve(conditionPassed);
         }
         tries++;
       }, interval);
     });
   },
 
   shuffle(array) {
     let results = [];