browser/base/content/test/plugins/browser_CTP_context_menu.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/plugins/browser_CTP_context_menu.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +var rootDir = getRootDirectory(gTestPath);
     1.5 +const gTestRoot = rootDir;
     1.6 +const gHttpTestRoot = rootDir.replace("chrome://mochitests/content/", "http://127.0.0.1:8888/");
     1.7 +
     1.8 +var gTestBrowser = null;
     1.9 +var gNextTest = null;
    1.10 +var gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
    1.11 +
    1.12 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.13 +
    1.14 +function test() {
    1.15 +  waitForExplicitFinish();
    1.16 +  registerCleanupFunction(function() {
    1.17 +    clearAllPluginPermissions();
    1.18 +    Services.prefs.clearUserPref("extensions.blocklist.suppressUI");
    1.19 +  });
    1.20 +  Services.prefs.setBoolPref("extensions.blocklist.suppressUI", true);
    1.21 +
    1.22 +  let newTab = gBrowser.addTab();
    1.23 +  gBrowser.selectedTab = newTab;
    1.24 +  gTestBrowser = gBrowser.selectedBrowser;
    1.25 +  gTestBrowser.addEventListener("load", pageLoad, true);
    1.26 +
    1.27 +  Services.prefs.setBoolPref("plugins.click_to_play", true);
    1.28 +  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
    1.29 +
    1.30 +  prepareTest(runAfterPluginBindingAttached(test1), gHttpTestRoot + "plugin_test.html");
    1.31 +}
    1.32 +
    1.33 +function finishTest() {
    1.34 +  clearAllPluginPermissions();
    1.35 +  gTestBrowser.removeEventListener("load", pageLoad, true);
    1.36 +  gBrowser.removeCurrentTab();
    1.37 +  window.focus();
    1.38 +  finish();
    1.39 +}
    1.40 +
    1.41 +function pageLoad() {
    1.42 +  // The plugin events are async dispatched and can come after the load event
    1.43 +  // This just allows the events to fire before we then go on to test the states
    1.44 +  executeSoon(gNextTest);
    1.45 +}
    1.46 +
    1.47 +function prepareTest(nextTest, url) {
    1.48 +  gNextTest = nextTest;
    1.49 +  gTestBrowser.contentWindow.location = url;
    1.50 +}
    1.51 +
    1.52 +// Due to layout being async, "PluginBindAttached" may trigger later.
    1.53 +// This wraps a function to force a layout flush, thus triggering it,
    1.54 +// and schedules the function execution so they're definitely executed
    1.55 +// afterwards.
    1.56 +function runAfterPluginBindingAttached(func) {
    1.57 +  return function() {
    1.58 +    let doc = gTestBrowser.contentDocument;
    1.59 +    let elems = doc.getElementsByTagName('embed');
    1.60 +    if (elems.length < 1) {
    1.61 +      elems = doc.getElementsByTagName('object');
    1.62 +    }
    1.63 +    elems[0].clientTop;
    1.64 +    executeSoon(func);
    1.65 +  };
    1.66 +}
    1.67 +
    1.68 +// Test that the activate action in content menus for CTP plugins works
    1.69 +function test1() {
    1.70 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.71 +  ok(popupNotification, "Test 1, Should have a click-to-play notification");
    1.72 +
    1.73 +  let plugin = gTestBrowser.contentDocument.getElementById("test");
    1.74 +  let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
    1.75 +  ok(!objLoadingContent.activated, "Test 1, Plugin should not be activated");
    1.76 +
    1.77 +  // When the popupshown DOM event is fired, the actual showing of the popup
    1.78 +  // may still be pending. Clear the event loop before continuing so that
    1.79 +  // subsequently-opened popups aren't cancelled by accident.
    1.80 +  let goToNext = function() {
    1.81 +    window.document.removeEventListener("popupshown", goToNext, false);
    1.82 +    executeSoon(test2);
    1.83 +  };
    1.84 +  window.document.addEventListener("popupshown", goToNext, false);
    1.85 +  EventUtils.synthesizeMouseAtCenter(plugin,
    1.86 +                                     { type: "contextmenu", button: 2 },
    1.87 +                                     gTestBrowser.contentWindow);
    1.88 +}
    1.89 +
    1.90 +function test2() {
    1.91 +  let activate = window.document.getElementById("context-ctp-play");
    1.92 +  ok(activate, "Test 2, Should have a context menu entry for activating the plugin");
    1.93 +
    1.94 +  let notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.95 +  ok(notification, "Test 2, Should have a click-to-play notification");
    1.96 +  ok(notification.dismissed, "Test 2, notification should be dismissed");
    1.97 +
    1.98 +  // Trigger the click-to-play popup
    1.99 +  activate.doCommand();
   1.100 +
   1.101 +  waitForCondition(() => !notification.dismissed,
   1.102 +		   test3, "Test 2, waited too long for context activation");
   1.103 +}
   1.104 +
   1.105 +function test3() {
   1.106 +  // Activate the plugin
   1.107 +  PopupNotifications.panel.firstChild._primaryButton.click();
   1.108 +
   1.109 +  let plugin = gTestBrowser.contentDocument.getElementById("test");
   1.110 +  let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
   1.111 +  waitForCondition(() => objLoadingContent.activated, test4, "Waited too long for plugin to activate");
   1.112 +}
   1.113 +
   1.114 +function test4() {
   1.115 +  finishTest();
   1.116 +}

mercurial