Bug 1254296 - [e10s] Fix browser_CTP_plugins.js to run in e10s. r=gfritzsche
authorJared Wein <jwein@mozilla.com>
Tue, 22 Mar 2016 17:40:02 -0400
changeset 290300 cdc495ec3550e43a6f183c1653346a72f2f6d880
parent 290299 f76214d65f7519ba93b085dc105de565ce23dd94
child 290301 be489a094b3ca6665d25d686ad04726ad722647e
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche
bugs1254296
milestone48.0a1
Bug 1254296 - [e10s] Fix browser_CTP_plugins.js to run in e10s. r=gfritzsche MozReview-Commit-ID: JsOQz7r14Ms
testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
toolkit/mozapps/extensions/test/browser/browser-common.ini
toolkit/mozapps/extensions/test/browser/browser_CTP_plugins.js
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -471,42 +471,44 @@ this.BrowserTestUtils = {
    *        Name of the event to listen to.
    * @param {bool} capture [optional]
    *        True to use a capturing listener.
    * @param {function} checkFn [optional]
    *        Called with the Event object as argument, should return true if the
    *        event is the expected one, or false if it should be ignored and
    *        listening should continue. If not specified, the first event with
    *        the specified name resolves the returned promise.
+   * @param {bool} wantsUntrusted [optional]
+   *        True to receive synthetic events dispatched by web content.
    *
    * @note Because this function is intended for testing, any error in checkFn
    *       will cause the returned promise to be rejected instead of waiting for
    *       the next event, since this is probably a bug in the test.
    *
    * @returns {Promise}
    * @resolves The Event object.
    */
-  waitForEvent(subject, eventName, capture, checkFn) {
+  waitForEvent(subject, eventName, capture, checkFn, wantsUntrusted) {
     return new Promise((resolve, reject) => {
       subject.addEventListener(eventName, function listener(event) {
         try {
           if (checkFn && !checkFn(event)) {
             return;
           }
           subject.removeEventListener(eventName, listener, capture);
           resolve(event);
         } catch (ex) {
           try {
             subject.removeEventListener(eventName, listener, capture);
           } catch (ex2) {
             // Maybe the provided object does not support removeEventListener.
           }
           reject(ex);
         }
-      }, capture);
+      }, capture, wantsUntrusted);
     });
   },
 
   /**
    * Like waitForEvent, but adds the event listener to the message manager
    * global for browser.
    *
    * @param {string} eventName
--- a/toolkit/mozapps/extensions/test/browser/browser-common.ini
+++ b/toolkit/mozapps/extensions/test/browser/browser-common.ini
@@ -62,9 +62,9 @@ skip-if = os == 'win' # Disabled on Wind
 [browser_inlinesettings.js]
 [browser_inlinesettings_browser.js]
 [browser_inlinesettings_custom.js]
 [browser_inlinesettings_info.js]
 [browser_tabsettings.js]
 [browser_pluginprefs.js]
 skip-if = buildapp == 'mulet'
 [browser_CTP_plugins.js]
-skip-if = buildapp == 'mulet' || e10s
+skip-if = buildapp == 'mulet'
--- a/toolkit/mozapps/extensions/test/browser/browser_CTP_plugins.js
+++ b/toolkit/mozapps/extensions/test/browser/browser_CTP_plugins.js
@@ -1,16 +1,9 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
 const gHttpTestRoot = "http://127.0.0.1:8888/" + RELATIVE_DIR + "/";
-var gManagerWindow;
-var gTestPluginId;
-var gPluginBrowser;
 
 function updateBlocklist(aCallback) {
   var blocklistNotifier = Cc["@mozilla.org/extensions/blocklist;1"]
                           .getService(Ci.nsITimerCallback);
   var observer = function() {
     Services.obs.removeObserver(observer, "blocklist-updated");
     SimpleTest.executeSoon(aCallback);
   };
@@ -26,209 +19,156 @@ function setAndUpdateBlocklist(aURL, aCa
   Services.prefs.setCharPref("extensions.blocklist.url", aURL);
   updateBlocklist(aCallback);
 }
 
 function resetBlocklist(aCallback) {
   Services.prefs.setCharPref("extensions.blocklist.url", _originalBlocklistURL);
 }
 
-function test() {
-  waitForExplicitFinish();
-  Services.prefs.setBoolPref("plugins.click_to_play", true);
-  Services.prefs.setBoolPref("extensions.blocklist.suppressUI", true);
+add_task(function*() {
+  SpecialPowers.pushPrefEnv({"set": [
+    ["plugins.click_to_play", true],
+    ["extensions.blocklist.suppressUI", true]
+  ]});
+  registerCleanupFunction(function*() {
+    let pluginTag = getTestPluginTag();
+    pluginTag.enabledState = Ci.nsIPluginTag.STATE_ENABLED;
+    yield new Promise(resolve => {
+      setAndUpdateBlocklist(gHttpTestRoot + "blockNoPlugins.xml", resolve);
+    });
+    resetBlocklist();
+  });
+
   let pluginTag = getTestPluginTag();
   pluginTag.enabledState = Ci.nsIPluginTag.STATE_CLICKTOPLAY;
-  open_manager("addons://list/plugin", part1);
-}
+  let managerWindow = yield new Promise(resolve => open_manager("addons://list/plugin", resolve));
+
+  let plugins = yield new Promise(resolve => AddonManager.getAddonsByTypes(["plugin"], resolve));
 
-function part1(aWindow) {
-  gManagerWindow = aWindow;
-  AddonManager.getAddonsByTypes(["plugin"], part2);
-}
-
-function part2(aPlugins) {
-  for (let plugin of aPlugins) {
+  let testPluginId;
+  for (let plugin of plugins) {
     if (plugin.name == "Test Plug-in") {
-      gTestPluginId = plugin.id;
+      testPluginId = plugin.id;
       break;
     }
   }
-  ok(gTestPluginId, "part2: Test Plug-in should exist");
-  AddonManager.getAddonByID(gTestPluginId, part3);
-}
+  ok(testPluginId, "part2: Test Plug-in should exist");
 
-function part3(aTestPlugin) {
-  let pluginEl = get_addon_element(gManagerWindow, gTestPluginId);
+  let testPlugin = yield new Promise(resolve => AddonManager.getAddonByID(testPluginId, resolve));
+
+  let pluginEl = get_addon_element(managerWindow, testPluginId);
   pluginEl.parentNode.ensureElementIsVisible(pluginEl);
-  let enableButton = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "enable-btn");
+  let enableButton = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "enable-btn");
   is_element_hidden(enableButton, "part3: enable button should not be visible");
-  let disableButton = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "enable-btn");
+  let disableButton = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "enable-btn");
   is_element_hidden(disableButton, "part3: disable button should not be visible");
-  let menu = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "state-menulist");
+  let menu = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "state-menulist");
   is_element_visible(menu, "part3: state menu should be visible");
-  let askToActivateItem = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "ask-to-activate-menuitem");
+  let askToActivateItem = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "ask-to-activate-menuitem");
   is(menu.selectedItem, askToActivateItem, "part3: state menu should have 'Ask To Activate' selected");
 
-  gBrowser.selectedTab = gBrowser.addTab();
-  gPluginBrowser = gBrowser.selectedBrowser;
-  gPluginBrowser.addEventListener("PluginBindingAttached", part4, true, true);
-  gPluginBrowser.contentWindow.location = gHttpTestRoot + "plugin_test.html";
-}
+  let pluginTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, gHttpTestRoot + "plugin_test.html");
+  let pluginBrowser = pluginTab.linkedBrowser;
+
+  let condition = () => PopupNotifications.getNotification("click-to-play-plugins", pluginBrowser);
+  yield BrowserTestUtils.waitForCondition(condition, "part4: should have a click-to-play notification");
 
-function part4() {
-  let condition = () => PopupNotifications.getNotification("click-to-play-plugins", gPluginBrowser);
-  waitForCondition(condition, () => {
-    gPluginBrowser.removeEventListener("PluginBindingAttached", part4);
-    gBrowser.removeCurrentTab();
+  yield BrowserTestUtils.removeTab(pluginTab);
+
+  let alwaysActivateItem = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "always-activate-menuitem");
+  menu.selectedItem = alwaysActivateItem;
+  alwaysActivateItem.doCommand();
 
-    let pluginEl = get_addon_element(gManagerWindow, gTestPluginId);
-    let menu = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "state-menulist");
-    let alwaysActivateItem = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "always-activate-menuitem");
-    menu.selectedItem = alwaysActivateItem;
-    alwaysActivateItem.doCommand();
-    gBrowser.selectedTab = gBrowser.addTab();
-    gPluginBrowser = gBrowser.selectedBrowser;
-    gPluginBrowser.addEventListener("load", part5, true);
-    gPluginBrowser.contentWindow.location = gHttpTestRoot + "plugin_test.html";
-  }, "part4: should have a click-to-play notification");
-}
+  pluginTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, gHttpTestRoot + "plugin_test.html");
 
-function part5() {
-  let testPlugin = gPluginBrowser.contentDocument.getElementById("test");
-  ok(testPlugin, "part5: should have a plugin element in the page");
-  let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
-  let condition = () => objLoadingContent.activated;
-  waitForCondition(condition, part6, "part5: waited too long for plugin to activate");
-}
+  yield ContentTask.spawn(pluginTab.linkedBrowser, null, function*() {
+    let testPlugin = content.document.getElementById("test");
+    ok(testPlugin, "part5: should have a plugin element in the page");
+    let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
+    let condition = () => objLoadingContent.activated;
+    yield ContentTaskUtils.waitForCondition(condition, "part5: waited too long for plugin to activate");
+    ok(objLoadingContent.activated, "part6: plugin should be activated");
+  });
 
-function part6() {
-  let testPlugin = gPluginBrowser.contentDocument.getElementById("test");
-  ok(testPlugin, "part6: should have a plugin element in the page");
-  let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
-  ok(objLoadingContent.activated, "part6: plugin should be activated");
-  gPluginBrowser.removeEventListener("load", part5);
-  gBrowser.removeCurrentTab();
+  yield BrowserTestUtils.removeTab(pluginTab);
 
-  let pluginEl = get_addon_element(gManagerWindow, gTestPluginId);
-  let menu = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "state-menulist");
-  let neverActivateItem = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "never-activate-menuitem");
+  let neverActivateItem = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "never-activate-menuitem");
   menu.selectedItem = neverActivateItem;
   neverActivateItem.doCommand();
-  gBrowser.selectedTab = gBrowser.addTab();
-  gPluginBrowser = gBrowser.selectedBrowser;
-  gPluginBrowser.addEventListener("PluginBindingAttached", part7, true, true);
-  gPluginBrowser.contentWindow.location = gHttpTestRoot + "plugin_test.html";
-}
+
+  pluginTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, gHttpTestRoot + "plugin_test.html");
+  pluginBrowser = pluginTab.linkedBrowser;
 
-function part7() {
-  let condition = () => PopupNotifications.getNotification("click-to-play-plugins", gPluginBrowser);
-  waitForCondition(condition, () => {
-    let testPlugin = gPluginBrowser.contentDocument.getElementById("test");
+  condition = () => PopupNotifications.getNotification("click-to-play-plugins", pluginBrowser);
+  yield BrowserTestUtils.waitForCondition(condition, "part7: disabled plugins still show a notification");
+  yield ContentTask.spawn(pluginTab.linkedBrowser, null, function*() {
+    let testPlugin = content.document.getElementById("test");
     ok(testPlugin, "part7: should have a plugin element in the page");
     let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
     ok(!objLoadingContent.activated, "part7: plugin should not be activated");
+  });
 
-    gPluginBrowser.removeEventListener("PluginBindingAttached", part7);
-    gBrowser.removeCurrentTab();
+  yield BrowserTestUtils.removeTab(pluginTab);
 
-    let pluginEl = get_addon_element(gManagerWindow, gTestPluginId);
-    let details = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "details-btn");
-    is_element_visible(details, "part7: details link should be visible");
-    EventUtils.synthesizeMouseAtCenter(details, {}, gManagerWindow);
-    wait_for_view_load(gManagerWindow, part8);
-  }, "part7: disabled plugins still show a notification");
-}
+  let details = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "details-btn");
+  is_element_visible(details, "part7: details link should be visible");
+  EventUtils.synthesizeMouseAtCenter(details, {}, managerWindow);
+  yield BrowserTestUtils.waitForEvent(managerWindow.document, "ViewChanged");
 
-function part8() {
-  let enableButton = gManagerWindow.document.getElementById("detail-enable-btn");
   is_element_hidden(enableButton, "part8: detail enable button should be hidden");
-  let disableButton = gManagerWindow.document.getElementById("detail-disable-btn");
   is_element_hidden(disableButton, "part8: detail disable button should be hidden");
-  let menu = gManagerWindow.document.getElementById("detail-state-menulist");
   is_element_visible(menu, "part8: detail state menu should be visible");
-  let neverActivateItem = gManagerWindow.document.getElementById("detail-never-activate-menuitem");
   is(menu.selectedItem, neverActivateItem, "part8: state menu should have 'Never Activate' selected");
 
-  let alwaysActivateItem = gManagerWindow.document.getElementById("detail-always-activate-menuitem");
   menu.selectedItem = alwaysActivateItem;
   alwaysActivateItem.doCommand();
-  gBrowser.selectedTab = gBrowser.addTab();
-  gPluginBrowser = gBrowser.selectedBrowser;
-  gPluginBrowser.addEventListener("load", part9, true);
-  gPluginBrowser.contentWindow.location = gHttpTestRoot + "plugin_test.html";
-}
+
+  pluginTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, gHttpTestRoot + "plugin_test.html");
+  pluginBrowser = pluginTab.linkedBrowser;
 
-function part9() {
-  let testPlugin = gPluginBrowser.contentDocument.getElementById("test");
-  ok(testPlugin, "part9: should have a plugin element in the page");
-  let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
-  let condition = () => objLoadingContent.activated;
-  waitForCondition(condition, part10, "part9: waited too long for plugin to activate");
-}
+  yield ContentTask.spawn(pluginTab.linkedBrowser, null, function*() {
+    let testPlugin = content.document.getElementById("test");
+    ok(testPlugin, "part9: should have a plugin element in the page");
+    let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
+    let condition = () => objLoadingContent.activated;
+    yield ContentTaskUtils.waitForCondition(condition, "part9: waited too long for plugin to activate");
+    ok(objLoadingContent.activated, "part10: plugin should be activated");
+  });
 
-function part10() {
-  let testPlugin = gPluginBrowser.contentDocument.getElementById("test");
-  ok(testPlugin, "part10: should have a plugin element in the page");
-  let objLoadingContent = testPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
-  ok(objLoadingContent.activated, "part10: plugin should be activated");
-  gPluginBrowser.removeEventListener("load", part9);
-  gBrowser.removeCurrentTab();
+  yield BrowserTestUtils.removeTab(pluginTab);
 
-  let menu = gManagerWindow.document.getElementById("detail-state-menulist");
-  let askToActivateItem = gManagerWindow.document.getElementById("detail-ask-to-activate-menuitem");
   menu.selectedItem = askToActivateItem;
   askToActivateItem.doCommand();
-  gBrowser.selectedTab = gBrowser.addTab();
-  gPluginBrowser = gBrowser.selectedBrowser;
-  gPluginBrowser.addEventListener("PluginBindingAttached", part11, true, true);
-  gPluginBrowser.contentWindow.location = gHttpTestRoot + "plugin_test.html";
-}
+
+  pluginTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, gHttpTestRoot + "plugin_test.html");
+  pluginBrowser = pluginTab.linkedBrowser;
 
-function part11() {
-  let condition = () => PopupNotifications.getNotification("click-to-play-plugins", gPluginBrowser);
-  waitForCondition(condition, () => {
-    gPluginBrowser.removeEventListener("PluginBindingAttached", part11);
-    gBrowser.removeCurrentTab();
+  condition = () => PopupNotifications.getNotification("click-to-play-plugins", pluginBrowser);
+  yield BrowserTestUtils.waitForCondition(condition, "part11: should have a click-to-play notification");
 
-    let pluginTag = getTestPluginTag();
+  yield BrowserTestUtils.removeTab(pluginTab);
 
   // causes appDisabled to be set
+  managerWindow = yield new Promise(resolve => {
     setAndUpdateBlocklist(gHttpTestRoot + "blockPluginHard.xml",
-      function() {
-        close_manager(gManagerWindow, function() {
-        open_manager("addons://list/plugin", part12);
-      });
-    });
-  }, "part11: should have a click-to-play notification");
-}
+      () => {
+        close_manager(managerWindow, function() {
+          open_manager("addons://list/plugin", resolve);
+        });
+      }
+    );
+  });
 
-function part12(aWindow) {
-  gManagerWindow = aWindow;
-  let pluginEl = get_addon_element(gManagerWindow, gTestPluginId);
+  pluginEl = get_addon_element(managerWindow, testPluginId);
   pluginEl.parentNode.ensureElementIsVisible(pluginEl);
-  let menu = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "state-menulist");
+  menu = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "state-menulist");
   is(menu.disabled, true, "part12: state menu should be disabled");
 
-  let details = gManagerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "details-btn");
-  EventUtils.synthesizeMouseAtCenter(details, {}, gManagerWindow);
-  wait_for_view_load(gManagerWindow, part13);
-}
+  details = managerWindow.document.getAnonymousElementByAttribute(pluginEl, "anonid", "details-btn");
+  EventUtils.synthesizeMouseAtCenter(details, {}, managerWindow);
+  yield BrowserTestUtils.waitForEvent(managerWindow.document, "ViewChanged");
 
-function part13() {
-  let menu = gManagerWindow.document.getElementById("detail-state-menulist");
+  menu = managerWindow.document.getElementById("detail-state-menulist");
   is(menu.disabled, true, "part13: detail state menu should be disabled");
 
-  setAndUpdateBlocklist(gHttpTestRoot + "blockNoPlugins.xml", function() {
-    run_next_test();
-  });
-}
-
-function end_test() {
-  Services.prefs.clearUserPref("plugins.click_to_play");
-  Services.prefs.clearUserPref("extensions.blocklist.suppressUI");
-  let pluginTag = getTestPluginTag();
-  pluginTag.enabledState = Ci.nsIPluginTag.STATE_ENABLED;
-  resetBlocklist();
-  close_manager(gManagerWindow, function() {
-    finish();
-  });
-}
+  managerWindow.close();
+});