browser/base/content/test/plugins/browser_plugins_added_dynamically.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_plugins_added_dynamically.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,178 @@
     1.4 +var rootDir = getRootDirectory(gTestPath);
     1.5 +const gTestRoot = rootDir.replace("chrome://mochitests/content/", "http://mochi.test:8888/");
     1.6 +
     1.7 +let gTestBrowser = null;
     1.8 +let gNextTest = null;
     1.9 +let gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
    1.10 +
    1.11 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.12 +
    1.13 +// Let's do the XPCNativeWrapper dance!
    1.14 +function addPlugin(browser, type) {
    1.15 +  let contentWindow = XPCNativeWrapper.unwrap(browser.contentWindow);
    1.16 +  contentWindow.addPlugin(type);
    1.17 +}
    1.18 +
    1.19 +function test() {
    1.20 +  waitForExplicitFinish();
    1.21 +  registerCleanupFunction(function() {
    1.22 +    clearAllPluginPermissions();
    1.23 +    Services.prefs.clearUserPref("plugins.click_to_play");
    1.24 +  });
    1.25 +  Services.prefs.setBoolPref("plugins.click_to_play", true);
    1.26 +  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
    1.27 +  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY, "Second Test Plug-in");
    1.28 +
    1.29 +  let newTab = gBrowser.addTab();
    1.30 +  gBrowser.selectedTab = newTab;
    1.31 +  gTestBrowser = gBrowser.selectedBrowser;
    1.32 +  gTestBrowser.addEventListener("load", pageLoad, true);
    1.33 +  prepareTest(testActivateAddSameTypePart1, gTestRoot + "plugin_add_dynamically.html");
    1.34 +}
    1.35 +
    1.36 +function finishTest() {
    1.37 +  gTestBrowser.removeEventListener("load", pageLoad, true);
    1.38 +  gBrowser.removeCurrentTab();
    1.39 +  window.focus();
    1.40 +  finish();
    1.41 +}
    1.42 +
    1.43 +function pageLoad() {
    1.44 +  // The plugin events are async dispatched and can come after the load event
    1.45 +  // This just allows the events to fire before we then go on to test the states
    1.46 +  executeSoon(gNextTest);
    1.47 +}
    1.48 +
    1.49 +function prepareTest(nextTest, url) {
    1.50 +  gNextTest = nextTest;
    1.51 +  gTestBrowser.contentWindow.location = url;
    1.52 +}
    1.53 +
    1.54 +// "Activate" of a given type -> plugins of that type dynamically added should
    1.55 +// automatically play.
    1.56 +function testActivateAddSameTypePart1() {
    1.57 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.58 +  ok(!popupNotification, "testActivateAddSameTypePart1: should not have a click-to-play notification");
    1.59 +  addPlugin(gTestBrowser);
    1.60 +  let condition = function() PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.61 +  waitForCondition(condition, testActivateAddSameTypePart2, "testActivateAddSameTypePart1: waited too long for click-to-play-plugin notification");
    1.62 +}
    1.63 +
    1.64 +function testActivateAddSameTypePart2() {
    1.65 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.66 +  ok(popupNotification, "testActivateAddSameTypePart2: should have a click-to-play notification");
    1.67 +
    1.68 +  popupNotification.reshow();
    1.69 +  testActivateAddSameTypePart3();
    1.70 +}
    1.71 +
    1.72 +function testActivateAddSameTypePart3() {
    1.73 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.74 +  let centerAction = null;
    1.75 +  for (let action of popupNotification.options.pluginData.values()) {
    1.76 +    if (action.pluginName == "Test") {
    1.77 +      centerAction = action;
    1.78 +      break;
    1.79 +    }
    1.80 +  }
    1.81 +  ok(centerAction, "testActivateAddSameTypePart3: found center action for the Test plugin");
    1.82 +
    1.83 +  let centerItem = null;
    1.84 +  for (let item of PopupNotifications.panel.firstChild.childNodes) {
    1.85 +    if (item.action && item.action == centerAction) {
    1.86 +      centerItem = item;
    1.87 +      break;
    1.88 +    }
    1.89 +  }
    1.90 +  ok(centerItem, "testActivateAddSameTypePart3: found center item for the Test plugin");
    1.91 +
    1.92 +  let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0];
    1.93 +  ok(!plugin.activated, "testActivateAddSameTypePart3: plugin should not be activated");
    1.94 +
    1.95 +  // Change the state and click the ok button to activate the Test plugin
    1.96 +  centerItem.value = "allownow";
    1.97 +  PopupNotifications.panel.firstChild._primaryButton.click();
    1.98 +
    1.99 +  let condition = function() plugin.activated;
   1.100 +  waitForCondition(condition, testActivateAddSameTypePart4, "testActivateAddSameTypePart3: waited too long for plugin to activate");
   1.101 +}
   1.102 +
   1.103 +function testActivateAddSameTypePart4() {
   1.104 +  let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0];
   1.105 +  ok(plugin.activated, "testActivateAddSameTypePart4: plugin should be activated");
   1.106 +
   1.107 +  addPlugin(gTestBrowser);
   1.108 +  let condition = function() {
   1.109 +    let embeds = gTestBrowser.contentDocument.getElementsByTagName("embed");
   1.110 +    let allActivated = true;
   1.111 +    for (let embed of embeds) {
   1.112 +      if (!embed.activated)
   1.113 +        allActivated = false;
   1.114 +    }
   1.115 +    return allActivated && embeds.length == 2;
   1.116 +  };
   1.117 +  waitForCondition(condition, testActivateAddSameTypePart5, "testActivateAddSameTypePart4: waited too long for second plugin to activate"); }
   1.118 +
   1.119 +function testActivateAddSameTypePart5() {
   1.120 +  let embeds = gTestBrowser.contentDocument.getElementsByTagName("embed");
   1.121 +  for (let embed of embeds) {
   1.122 +    ok(embed.activated, "testActivateAddSameTypePart5: all plugins should be activated");
   1.123 +  }
   1.124 +  clearAllPluginPermissions();
   1.125 +  prepareTest(testActivateAddDifferentTypePart1, gTestRoot + "plugin_add_dynamically.html");
   1.126 +}
   1.127 +
   1.128 +// "Activate" of a given type -> plugins of other types dynamically added
   1.129 +// should not automatically play.
   1.130 +function testActivateAddDifferentTypePart1() {
   1.131 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
   1.132 +  ok(!popupNotification, "testActivateAddDifferentTypePart1: should not have a click-to-play notification");
   1.133 +  addPlugin(gTestBrowser);
   1.134 +  let condition = function() PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
   1.135 +  waitForCondition(condition, testActivateAddDifferentTypePart2, "testActivateAddDifferentTypePart1: waited too long for click-to-play-plugin notification");
   1.136 +}
   1.137 +
   1.138 +function testActivateAddDifferentTypePart2() {
   1.139 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
   1.140 +  ok(popupNotification, "testActivateAddDifferentTypePart2: should have a click-to-play notification");
   1.141 +
   1.142 +  // we have to actually show the panel to get the bindings to instantiate
   1.143 +  popupNotification.reshow();
   1.144 +  testActivateAddDifferentTypePart3();
   1.145 +}
   1.146 +
   1.147 +function testActivateAddDifferentTypePart3() {
   1.148 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
   1.149 +  is(popupNotification.options.pluginData.size, 1, "Should be one plugin action");
   1.150 +
   1.151 +  let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0];
   1.152 +  ok(!plugin.activated, "testActivateAddDifferentTypePart3: plugin should not be activated");
   1.153 +
   1.154 +  // "click" the button to activate the Test plugin
   1.155 +  PopupNotifications.panel.firstChild._primaryButton.click();
   1.156 +
   1.157 +  let condition = function() plugin.activated;
   1.158 +  waitForCondition(condition, testActivateAddDifferentTypePart4, "testActivateAddDifferentTypePart3: waited too long for plugin to activate");
   1.159 +}
   1.160 +
   1.161 +function testActivateAddDifferentTypePart4() {
   1.162 +  let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0];
   1.163 +  ok(plugin.activated, "testActivateAddDifferentTypePart4: plugin should be activated");
   1.164 +
   1.165 +  addPlugin(gTestBrowser);
   1.166 +  let condition = function() PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
   1.167 +  waitForCondition(condition, testActivateAddDifferentTypePart5, "testActivateAddDifferentTypePart5: waited too long for popup notification");
   1.168 +}
   1.169 +
   1.170 +function testActivateAddDifferentTypePart5() {
   1.171 +  ok(PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser), "testActivateAddDifferentTypePart5: should have popup notification");
   1.172 +  let embeds = gTestBrowser.contentDocument.getElementsByTagName("embed");
   1.173 +  for (let embed of embeds) {
   1.174 +    if (embed.type == "application/x-test")
   1.175 +      ok(embed.activated, "testActivateAddDifferentTypePart5: Test plugin should be activated");
   1.176 +    else if (embed.type == "application/x-second-test")
   1.177 +      ok(!embed.activated, "testActivateAddDifferentTypePart5: Second Test plugin should not be activated");
   1.178 +  }
   1.179 +
   1.180 +  finishTest();
   1.181 +}

mercurial