Bug 1521707 - Move promisePopupEvent to BrowserTestUtils.jsm. r=mak
authorMark Banner <standard8@mozilla.com>
Wed, 23 Jan 2019 14:04:46 +0000
changeset 515101 51025d682291eeae4040de994f1b76573e1d1596
parent 515100 60725e26d685afced3fdbcba797928f668bb3190
child 515102 248f94519729a71c76d3b1bd51c53bcb5f6942ff
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1521707
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1521707 - Move promisePopupEvent to BrowserTestUtils.jsm. r=mak Differential Revision: https://phabricator.services.mozilla.com/D17259
browser/base/content/test/general/browser_bug432599.js
browser/base/content/test/general/head.js
browser/base/content/test/webrtc/head.js
browser/components/customizableui/test/head.js
browser/components/urlbar/tests/browser/head-common.js
testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
--- a/browser/base/content/test/general/browser_bug432599.js
+++ b/browser/base/content/test/general/browser_bug432599.js
@@ -71,20 +71,20 @@ var initialValue;
 var initialRemoveHidden;
 function checkBookmarksPanel(phase) {
   let popupElement = document.getElementById("editBookmarkPanel");
   let titleElement = document.getElementById("editBookmarkPanelTitle");
   let removeElement = document.getElementById("editBookmarkPanelRemoveButton");
   switch (phase) {
   case 1:
   case 3:
-    return promisePopupEvent(popupElement, "shown");
+    return promisePopupShown(popupElement);
   case 2:
     initialValue = titleElement.value;
     initialRemoveHidden = removeElement.hidden;
-    return promisePopupEvent(popupElement, "hidden");
+    return promisePopupHidden(popupElement);
   case 4:
     Assert.equal(titleElement.value, initialValue, "The bookmark panel's title should be the same");
     Assert.equal(removeElement.hidden, initialRemoveHidden, "The bookmark panel's visibility should not change");
-    return promisePopupEvent(popupElement, "hidden");
+    return promisePopupHidden(popupElement);
   }
   return Promise.reject(new Error("Unknown phase"));
 }
--- a/browser/base/content/test/general/head.js
+++ b/browser/base/content/test/general/head.js
@@ -418,37 +418,22 @@ function is_element_visible(element, msg
   ok(BrowserTestUtils.is_visible(element), msg || "Element should be visible");
 }
 
 function is_element_hidden(element, msg) {
   isnot(element, null, "Element should not be null, when checking visibility");
   ok(is_hidden(element), msg || "Element should be hidden");
 }
 
-function promisePopupEvent(popup, eventSuffix) {
-  let endState = {shown: "open", hidden: "closed"}[eventSuffix];
-
-  if (popup.state == endState)
-    return Promise.resolve();
-
-  let eventType = "popup" + eventSuffix;
-  return new Promise(resolve => {
-    popup.addEventListener(eventType, function(event) {
-      resolve();
-    }, {once: true});
-
-  });
-}
-
 function promisePopupShown(popup) {
-  return promisePopupEvent(popup, "shown");
+  return BrowserTestUtils.waitForPopupEvent(popup, "shown");
 }
 
 function promisePopupHidden(popup) {
-  return promisePopupEvent(popup, "hidden");
+  return BrowserTestUtils.waitForPopupEvent(popup, "hidden");
 }
 
 function promiseNotificationShown(notification) {
   let win = notification.browser.ownerGlobal;
   if (win.PopupNotifications.panel.state == "open") {
     return Promise.resolve();
   }
   let panelPromise = promisePopupShown(win.PopupNotifications.panel);
--- a/browser/base/content/test/webrtc/head.js
+++ b/browser/base/content/test/webrtc/head.js
@@ -155,37 +155,23 @@ async function assertWebRTCIndicatorStat
            item + " global indicator attribute as expected");
       }
 
       ok(!indicator.hasMoreElements(), "only one global indicator window");
     }
   }
 }
 
-function promisePopupEvent(popup, eventSuffix) {
-  let endState = {shown: "open", hidden: "closed"}[eventSuffix];
-
-  if (popup.state == endState)
-    return Promise.resolve();
-
-  let eventType = "popup" + eventSuffix;
-  return new Promise(resolve => {
-    popup.addEventListener(eventType, function(event) {
-      executeSoon(resolve);
-    }, {once: true});
-
-  });
-}
-
 function promiseNotificationShown(notification) {
   let win = notification.browser.ownerGlobal;
   if (win.PopupNotifications.panel.state == "open") {
     return Promise.resolve();
   }
-  let panelPromise = promisePopupEvent(win.PopupNotifications.panel, "shown");
+  let panelPromise =
+    BrowserTestUtils.waitForPopupEvent(win.PopupNotifications.panel, "shown");
   notification.reshow();
   return panelPromise;
 }
 
 function _mm() {
   return gBrowser.selectedBrowser.messageManager;
 }
 
--- a/browser/components/customizableui/test/head.js
+++ b/browser/components/customizableui/test/head.js
@@ -404,50 +404,21 @@ function promiseAttributeMutation(aNode,
         }
       }
     });
     obs.observe(aNode, {attributeFilter: [aAttribute]});
   });
 }
 
 function popupShown(aPopup) {
-  return promisePopupEvent(aPopup, "shown");
+  return BrowserTestUtils.waitForPopupEvent(aPopup, "shown");
 }
 
 function popupHidden(aPopup) {
-  return promisePopupEvent(aPopup, "hidden");
-}
-
-/**
- * Returns a Promise that resolves when aPopup fires an event of type
- * aEventType. Times out and rejects after 20 seconds.
- *
- * @param aPopup the popup to monitor for events.
- * @param aEventSuffix the _suffix_ for the popup event type to watch for.
- *
- * Example usage:
- *   let popupShownPromise = promisePopupEvent(somePopup, "shown");
- *   // ... something that opens a popup
- *   yield popupShownPromise;
- *
- *  let popupHiddenPromise = promisePopupEvent(somePopup, "hidden");
- *  // ... something that hides a popup
- *  yield popupHiddenPromise;
- */
-function promisePopupEvent(aPopup, aEventSuffix) {
-  return new Promise(resolve => {
-    let eventType = "popup" + aEventSuffix;
-
-    function onPopupEvent(e) {
-      aPopup.removeEventListener(eventType, onPopupEvent);
-      resolve();
-    }
-
-    aPopup.addEventListener(eventType, onPopupEvent);
-  });
+  return BrowserTestUtils.waitForPopupEvent(aPopup, "hidden");
 }
 
 // This is a simpler version of the context menu check that
 // exists in contextmenu_common.js.
 function checkContextMenu(aContextMenu, aExpectedEntries, aWindow = window) {
   let children = [...aContextMenu.children];
   // Ignore hidden nodes:
   children = children.filter((n) => !n.hidden);
--- a/browser/components/urlbar/tests/browser/head-common.js
+++ b/browser/components/urlbar/tests/browser/head-common.js
@@ -31,37 +31,22 @@ function runHttpServer(scheme, host, por
     httpserver.identity.setPrimary(scheme, host, port);
   } catch (ex) {
     info("We can't launch our http server successfully.");
   }
   is(httpserver.identity.has(scheme, host, port), true, `${scheme}://${host}:${port} is listening.`);
   return httpserver;
 }
 
-function promisePopupEvent(popup, eventSuffix) {
-  let endState = {shown: "open", hidden: "closed"}[eventSuffix];
-
-  if (popup.state == endState)
-    return Promise.resolve();
-
-  let eventType = "popup" + eventSuffix;
-  return new Promise(resolve => {
-    popup.addEventListener(eventType, function(event) {
-      resolve();
-    }, {once: true});
-
-  });
-}
-
 function promisePopupShown(popup) {
-  return promisePopupEvent(popup, "shown");
+  return BrowserTestUtils.waitForPopupEvent(popup, "shown");
 }
 
 function promisePopupHidden(popup) {
-  return promisePopupEvent(popup, "hidden");
+  return BrowserTestUtils.waitForPopupEvent(popup, "hidden");
 }
 
 function promiseSearchComplete(win = window, dontAnimate = false) {
   return promisePopupShown(win.gURLBar.popup).then(() => {
     function searchIsComplete() {
       let isComplete = win.gURLBar.controller.searchStatus >=
                        Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH;
       if (isComplete) {
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -952,16 +952,35 @@ var BrowserTestUtils = {
               completion();
             }, capture, wantsUntrusted);
           });
         });
     /* eslint-enable no-eval */
   },
 
   /**
+   * Like waitForEvent, but acts on a popup. It ensures the popup is not already
+   * in the expected state.
+   *
+   * @param {Element} popup
+   *        The popup element that should receive the event.
+   * @param {string} eventSuffix
+   *        The event suffix expected to be received, one of "shown" or "hidden".
+   * @returns {Promise}
+   */
+  waitForPopupEvent(popup, eventSuffix) {
+    let endState = {shown: "open", hidden: "closed"}[eventSuffix];
+
+    if (popup.state == endState) {
+      return Promise.resolve();
+    }
+    return this.waitForEvent(popup, "popup" + eventSuffix);
+  },
+
+  /**
    * Adds a content event listener on the given browser
    * element. Similar to waitForContentEvent, but the listener will
    * fire until it is removed. A callable object is returned that,
    * when called, removes the event listener. Note that this function
    * works even if the browser's frameloader is swapped.
    *
    * @param {xul:browser} browser
    *        The browser element to listen for events in.