Bug 1163418 - Remove use of CPOWs in browser plugin tests. r=felipe, a=test-only
authorBlake Kaplan <mrbkap@gmail.com>
Thu, 09 Jun 2016 11:57:32 -0700
changeset 339721 08b451fd07c56bcf15a1ef51dc851db6f7b4beb7
parent 339720 27d8dcd55b2e91c4008b36307f3a42ce12927683
child 339722 9cb326caa69ef3ed6d2460ddf58504d6b6cfbc76
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfelipe, test-only
bugs1163418
milestone49.0a2
Bug 1163418 - Remove use of CPOWs in browser plugin tests. r=felipe, a=test-only
browser/base/content/test/plugins/browser_pluginnotification.js
browser/base/content/test/plugins/head.js
--- a/browser/base/content/test/plugins/browser_pluginnotification.js
+++ b/browser/base/content/test/plugins/browser_pluginnotification.js
@@ -113,17 +113,17 @@ add_task(function* () {
   yield promisePopupNotification("click-to-play-plugins");
 
   yield promiseTabLoadEvent(gBrowser.selectedTab, "data:text/html,<html>hi</html>");
 
   // make sure the notification is gone
   let notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
   ok(!notification, "Test 11b, Should not have a click-to-play notification");
 
-  gTestBrowser.contentWindow.history.back();
+  gTestBrowser.webNavigation.goBack();
 
   yield promisePopupNotification("click-to-play-plugins");
 });
 
 // Tests that the "Allow Always" permission works for click-to-play plugins
 add_task(function* () {
   updateAllTestPlugins(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
 
--- a/browser/base/content/test/plugins/head.js
+++ b/browser/base/content/test/plugins/head.js
@@ -1,14 +1,16 @@
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "Task",
   "resource://gre/modules/Task.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
   "resource://gre/modules/PlacesUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PromiseUtils",
+  "resource://gre/modules/PromiseUtils.jsm");
 
 // The blocklist shim running in the content process does not initialize at
 // start up, so it's not active until we load content that needs to do a
 // check. This helper bypasses the delay to get the svc up and running
 // immediately. Note, call this after remote content has loaded.
 function promiseInitContentBlocklistSvc(aBrowser)
 {
   return ContentTask.spawn(aBrowser, {}, function* () {
@@ -66,50 +68,54 @@ function waitForEvent(subject, eventName
 /**
  * Waits for a load (or custom) event to finish in a given tab. If provided
  * load an uri into the tab.
  *
  * @param tab
  *        The tab to load into.
  * @param [optional] url
  *        The url to load, or the current url.
- * @param [optional] event
- *        The load event type to wait for.  Defaults to "load".
  * @return {Promise} resolved when the event is handled.
  * @resolves to the received event
  * @rejects if a valid load event is not received within a meaningful interval
  */
-function promiseTabLoadEvent(tab, url, eventType="load") {
-  return new Promise((resolve, reject) => {
-    info("Wait tab event: " + eventType);
+function promiseTabLoadEvent(tab, url) {
+  let deferred = PromiseUtils.defer();
+  info("Wait tab event: load");
 
-    function handle(event) {
-      if (event.originalTarget != tab.linkedBrowser.contentDocument ||
-          event.target.location.href == "about:blank" ||
-          (url && event.target.location.href != url)) {
-        info("Skipping spurious '" + eventType + "'' event" +
-              " for " + event.target.location.href);
-        return;
-      }
-      clearTimeout(timeout);
-      tab.linkedBrowser.removeEventListener(eventType, handle, true);
-      info("Tab event received: " + eventType);
-      resolve(event);
+  function handle(loadedUrl) {
+    if (loadedUrl === "about:blank" || (url && loadedUrl !== url)) {
+      info(`Skipping spurious load event for ${loadedUrl}`);
+      return false;
     }
 
-    let timeout = setTimeout(() => {
-      tab.linkedBrowser.removeEventListener(eventType, handle, true);
-      reject(new Error("Timed out while waiting for a '" + eventType + "'' event"));
-    }, 30000);
+    info("Tab event received: load");
+    return true;
+  }
+
+  // Create two promises: one resolved from the content process when the page
+  // loads and one that is rejected if we take too long to load the url.
+  let loaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, handle);
+
+  let timeout = setTimeout(() => {
+    deferred.reject(new Error("Timed out while waiting for a 'load' event"));
+  }, 30000);
 
-    tab.linkedBrowser.addEventListener(eventType, handle, true, true);
-    if (url) {
-      tab.linkedBrowser.loadURI(url);
-    }
+  loaded.then(() => {
+    clearTimeout(timeout);
+    deferred.resolve()
   });
+
+  if (url)
+    BrowserTestUtils.loadURI(tab.linkedBrowser, url);
+
+  // Promise.all rejects if either promise rejects (i.e. if we time out) and
+  // if our loaded promise resolves before the timeout, then we resolve the
+  // timeout promise as well, causing the all promise to resolve.
+  return Promise.all([deferred.promise, loaded]);
 }
 
 function waitForCondition(condition, nextTest, errorMsg, aTries, aWait) {
   let tries = 0;
   let maxTries = aTries || 100; // 100 tries
   let maxWait = aWait || 100; // 100 msec x 100 tries = ten seconds
   let interval = setInterval(function() {
     if (tries >= maxTries) {