bug 820303 - scripted click-to-play plugins: handle deny permission r=jaws
[gecko.git] / browser / base / content / test / browser_clickToPlayPluginScriptAccessPopup.js
blob02967edd2e02b2310118c13dcdf5b1dbc4c2ada8
1 const gHttpTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/", "http://127.0.0.1:8888/");
2 const EXPECTED_PLUGINSCRIPTED_EVENT_COUNT = 7;
4 var gTestBrowser = null;
5 var gNextTestList = [];
6 var gNextTest = null;
7 var gPluginScriptedFired = false;
8 var gPluginScriptedFiredCount = 0;
10 Components.utils.import("resource://gre/modules/Services.jsm");
12 function test() {
13   waitForExplicitFinish();
14   registerCleanupFunction(function() {
15     Services.prefs.clearUserPref("plugins.click_to_play");
16     gTestBrowser.removeEventListener("load", pageLoad, true);
17     gTestBrowser.removeEventListener("PluginScripted", pluginScripted, true);
18   });
19   Services.prefs.setBoolPref("plugins.click_to_play", true);
21   gBrowser.selectedTab = gBrowser.addTab();
22   gTestBrowser = gBrowser.selectedBrowser;
23   gTestBrowser.addEventListener("load", pageLoad, true);
24   gTestBrowser.addEventListener("PluginScripted", pluginScripted, true);
26   // This list is iterated in reverse order, since it uses Array.pop to get the next test.
27   gNextTestList = [
28     // Doesn't show a popup since not the first instance of a small plugin
29     { func: testExpectNoPopupPart1,
30       url: gHttpTestRoot + "plugin_test_scriptedPopup1.html" },
31     // Doesn't show a popup since not the first instance of a small plugin
32     { func: testExpectNoPopupPart1,
33       url: gHttpTestRoot + "plugin_test_scriptedPopup2.html" },
34     // Shows a popup since it is the first instance of a small plugin
35     { func: testExpectPopupPart1,
36       url: gHttpTestRoot + "plugin_test_scriptedPopup3.html" },
37     { func: testExpectNoPopupPart1,
38       url: gHttpTestRoot + "plugin_test_scriptedNoPopup1.html" },
39     { func: testExpectNoPopupPart1,
40       url: gHttpTestRoot + "plugin_test_scriptedNoPopup2.html" },
41     { func: testExpectNoPopupPart1,
42       url: gHttpTestRoot + "plugin_test_scriptedNoPopup3.html" }
43   ];
45   prepareTest(testNoEventFired, gHttpTestRoot + "plugin_test_noScriptNoPopup.html");
48 function getCurrentTestLocation() {
49   var loc = gTestBrowser.contentWindow.location.toString();
50   return loc.replace(gHttpTestRoot, "");
53 function runNextTest() {
54   var nextTest = gNextTestList.pop();
55   if (nextTest) {
56     gPluginScriptedFired = false;
57     prepareTest(nextTest.func, nextTest.url);
58   }
59   else {
60     finishTest();
61   }
64 function finishTest() {
65   is(gPluginScriptedFiredCount, EXPECTED_PLUGINSCRIPTED_EVENT_COUNT, "PluginScripted event count is correct");
66   gBrowser.removeCurrentTab();
67   window.focus();
68   finish();
71 function pluginScripted() {
72   gPluginScriptedFired = true;
73   gPluginScriptedFiredCount++;
76 function pageLoad() {
77   // The plugin events are async dispatched and can come after the load event
78   // This just allows the events to fire before we then go on to test the states
79   executeSoon(gNextTest);
82 function prepareTest(nextTest, url) {
83   gNextTest = nextTest;
84   gTestBrowser.contentWindow.location = url;
87 function testNoEventFired() {
88   var notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
89   ok(notification, "should have a click-to-play notification (" + getCurrentTestLocation() + ")");
90   ok(notification.dismissed, "notification should not be showing (" + getCurrentTestLocation() + ")");
91   ok(!gPluginScriptedFired, "PluginScripted should not have fired (" + getCurrentTestLocation() + ")");
93   prepareTest(testDenyPermissionPart1, gHttpTestRoot + "plugin_test_noScriptNoPopup.html");
96 function testDenyPermissionPart1() {
97   var notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
98   ok(notification, "test deny permission: should have a click-to-play notification");
99   // Simulate clicking the "Deny Always" button.
100   notification.secondaryActions[1].callback();
101   gPluginScriptedFired = false;
102   prepareTest(testDenyPermissionPart2, gHttpTestRoot + "plugin_test_scriptedPopup1.html");
105 function testDenyPermissionPart2() {
106   var condition = function() gPluginScriptedFired;
107   waitForCondition(condition, testDenyPermissionPart3, "test deny permission: waited too long for PluginScripted event");
110 function testDenyPermissionPart3() {
111   var condition = function() gTestBrowser._pluginScriptedState == gPluginHandler.PLUGIN_SCRIPTED_STATE_DONE;
112   waitForCondition(condition, testDenyPermissionPart4, "test deny permission: waited too long for PluginScripted event handling");
115 function testDenyPermissionPart4() {
116   var notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
117   ok(!notification, "test deny permission: should not have a click-to-play notification");
119   var pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
120   Services.perms.remove("127.0.0.1:8888", pluginHost.getPermissionStringForType("application/x-test"));
122   runNextTest();
125 function testExpectNoPopupPart1() {
126   var condition = function() gPluginScriptedFired;
127   waitForCondition(condition, testExpectNoPopupPart2, "waited too long for PluginScripted event (" + getCurrentTestLocation() + ")");
130 function testExpectNoPopupPart2() {
131   var condition = function() gTestBrowser._pluginScriptedState == gPluginHandler.PLUGIN_SCRIPTED_STATE_DONE;
132   waitForCondition(condition, testExpectNoPopupPart3, "waited too long for PluginScripted event handling (" + getCurrentTestLocation() + ")");
135 function testExpectNoPopupPart3() {
136   var notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
137   ok(notification, "should have a click-to-play notification (" + getCurrentTestLocation() + ")");
138   ok(notification.dismissed, "notification should not be showing (" + getCurrentTestLocation() + ")");
140   runNextTest();
143 function testExpectPopupPart1() {
144   var notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
145   ok(notification, "should have a click-to-play notification (" + getCurrentTestLocation() + ")");
147   var condition = function() !notification.dismissed;
148   waitForCondition(condition, testExpectPopupPart2, "waited too long for popup notification to show (" + getCurrentTestLocation() + ")");
151 function testExpectPopupPart2() {
152   var notification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
153   ok(!notification.dismissed, "notification should be showing (" + getCurrentTestLocation() + ")");
155   runNextTest();