michael@0: var rootDir = getRootDirectory(gTestPath); michael@0: const gTestRoot = rootDir.replace("chrome://mochitests/content/", "http://mochi.test:8888/"); michael@0: michael@0: let gTestBrowser = null; michael@0: let gNextTest = null; michael@0: let gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost); michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: // Let's do the XPCNativeWrapper dance! michael@0: function addPlugin(browser, type) { michael@0: let contentWindow = XPCNativeWrapper.unwrap(browser.contentWindow); michael@0: contentWindow.addPlugin(type); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: registerCleanupFunction(function() { michael@0: clearAllPluginPermissions(); michael@0: Services.prefs.clearUserPref("plugins.click_to_play"); michael@0: }); michael@0: Services.prefs.setBoolPref("plugins.click_to_play", true); michael@0: setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY); michael@0: setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY, "Second Test Plug-in"); michael@0: michael@0: let newTab = gBrowser.addTab(); michael@0: gBrowser.selectedTab = newTab; michael@0: gTestBrowser = gBrowser.selectedBrowser; michael@0: gTestBrowser.addEventListener("load", pageLoad, true); michael@0: prepareTest(testActivateAddSameTypePart1, gTestRoot + "plugin_add_dynamically.html"); michael@0: } michael@0: michael@0: function finishTest() { michael@0: gTestBrowser.removeEventListener("load", pageLoad, true); michael@0: gBrowser.removeCurrentTab(); michael@0: window.focus(); michael@0: finish(); michael@0: } michael@0: michael@0: function pageLoad() { michael@0: // The plugin events are async dispatched and can come after the load event michael@0: // This just allows the events to fire before we then go on to test the states michael@0: executeSoon(gNextTest); michael@0: } michael@0: michael@0: function prepareTest(nextTest, url) { michael@0: gNextTest = nextTest; michael@0: gTestBrowser.contentWindow.location = url; michael@0: } michael@0: michael@0: // "Activate" of a given type -> plugins of that type dynamically added should michael@0: // automatically play. michael@0: function testActivateAddSameTypePart1() { michael@0: let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: ok(!popupNotification, "testActivateAddSameTypePart1: should not have a click-to-play notification"); michael@0: addPlugin(gTestBrowser); michael@0: let condition = function() PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: waitForCondition(condition, testActivateAddSameTypePart2, "testActivateAddSameTypePart1: waited too long for click-to-play-plugin notification"); michael@0: } michael@0: michael@0: function testActivateAddSameTypePart2() { michael@0: let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: ok(popupNotification, "testActivateAddSameTypePart2: should have a click-to-play notification"); michael@0: michael@0: popupNotification.reshow(); michael@0: testActivateAddSameTypePart3(); michael@0: } michael@0: michael@0: function testActivateAddSameTypePart3() { michael@0: let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: let centerAction = null; michael@0: for (let action of popupNotification.options.pluginData.values()) { michael@0: if (action.pluginName == "Test") { michael@0: centerAction = action; michael@0: break; michael@0: } michael@0: } michael@0: ok(centerAction, "testActivateAddSameTypePart3: found center action for the Test plugin"); michael@0: michael@0: let centerItem = null; michael@0: for (let item of PopupNotifications.panel.firstChild.childNodes) { michael@0: if (item.action && item.action == centerAction) { michael@0: centerItem = item; michael@0: break; michael@0: } michael@0: } michael@0: ok(centerItem, "testActivateAddSameTypePart3: found center item for the Test plugin"); michael@0: michael@0: let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0]; michael@0: ok(!plugin.activated, "testActivateAddSameTypePart3: plugin should not be activated"); michael@0: michael@0: // Change the state and click the ok button to activate the Test plugin michael@0: centerItem.value = "allownow"; michael@0: PopupNotifications.panel.firstChild._primaryButton.click(); michael@0: michael@0: let condition = function() plugin.activated; michael@0: waitForCondition(condition, testActivateAddSameTypePart4, "testActivateAddSameTypePart3: waited too long for plugin to activate"); michael@0: } michael@0: michael@0: function testActivateAddSameTypePart4() { michael@0: let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0]; michael@0: ok(plugin.activated, "testActivateAddSameTypePart4: plugin should be activated"); michael@0: michael@0: addPlugin(gTestBrowser); michael@0: let condition = function() { michael@0: let embeds = gTestBrowser.contentDocument.getElementsByTagName("embed"); michael@0: let allActivated = true; michael@0: for (let embed of embeds) { michael@0: if (!embed.activated) michael@0: allActivated = false; michael@0: } michael@0: return allActivated && embeds.length == 2; michael@0: }; michael@0: waitForCondition(condition, testActivateAddSameTypePart5, "testActivateAddSameTypePart4: waited too long for second plugin to activate"); } michael@0: michael@0: function testActivateAddSameTypePart5() { michael@0: let embeds = gTestBrowser.contentDocument.getElementsByTagName("embed"); michael@0: for (let embed of embeds) { michael@0: ok(embed.activated, "testActivateAddSameTypePart5: all plugins should be activated"); michael@0: } michael@0: clearAllPluginPermissions(); michael@0: prepareTest(testActivateAddDifferentTypePart1, gTestRoot + "plugin_add_dynamically.html"); michael@0: } michael@0: michael@0: // "Activate" of a given type -> plugins of other types dynamically added michael@0: // should not automatically play. michael@0: function testActivateAddDifferentTypePart1() { michael@0: let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: ok(!popupNotification, "testActivateAddDifferentTypePart1: should not have a click-to-play notification"); michael@0: addPlugin(gTestBrowser); michael@0: let condition = function() PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: waitForCondition(condition, testActivateAddDifferentTypePart2, "testActivateAddDifferentTypePart1: waited too long for click-to-play-plugin notification"); michael@0: } michael@0: michael@0: function testActivateAddDifferentTypePart2() { michael@0: let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: ok(popupNotification, "testActivateAddDifferentTypePart2: should have a click-to-play notification"); michael@0: michael@0: // we have to actually show the panel to get the bindings to instantiate michael@0: popupNotification.reshow(); michael@0: testActivateAddDifferentTypePart3(); michael@0: } michael@0: michael@0: function testActivateAddDifferentTypePart3() { michael@0: let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: is(popupNotification.options.pluginData.size, 1, "Should be one plugin action"); michael@0: michael@0: let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0]; michael@0: ok(!plugin.activated, "testActivateAddDifferentTypePart3: plugin should not be activated"); michael@0: michael@0: // "click" the button to activate the Test plugin michael@0: PopupNotifications.panel.firstChild._primaryButton.click(); michael@0: michael@0: let condition = function() plugin.activated; michael@0: waitForCondition(condition, testActivateAddDifferentTypePart4, "testActivateAddDifferentTypePart3: waited too long for plugin to activate"); michael@0: } michael@0: michael@0: function testActivateAddDifferentTypePart4() { michael@0: let plugin = gTestBrowser.contentDocument.getElementsByTagName("embed")[0]; michael@0: ok(plugin.activated, "testActivateAddDifferentTypePart4: plugin should be activated"); michael@0: michael@0: addPlugin(gTestBrowser); michael@0: let condition = function() PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser); michael@0: waitForCondition(condition, testActivateAddDifferentTypePart5, "testActivateAddDifferentTypePart5: waited too long for popup notification"); michael@0: } michael@0: michael@0: function testActivateAddDifferentTypePart5() { michael@0: ok(PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser), "testActivateAddDifferentTypePart5: should have popup notification"); michael@0: let embeds = gTestBrowser.contentDocument.getElementsByTagName("embed"); michael@0: for (let embed of embeds) { michael@0: if (embed.type == "application/x-test") michael@0: ok(embed.activated, "testActivateAddDifferentTypePart5: Test plugin should be activated"); michael@0: else if (embed.type == "application/x-second-test") michael@0: ok(!embed.activated, "testActivateAddDifferentTypePart5: Second Test plugin should not be activated"); michael@0: } michael@0: michael@0: finishTest(); michael@0: }