|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 |
|
6 let gActiveListeners = {}; |
|
7 |
|
8 function registerPopupEventHandler(eventName, callback) { |
|
9 gActiveListeners[eventName] = function (event) { |
|
10 if (event.target != PopupNotifications.panel) |
|
11 return; |
|
12 PopupNotifications.panel.removeEventListener(eventName, |
|
13 gActiveListeners[eventName], |
|
14 false); |
|
15 delete gActiveListeners[eventName]; |
|
16 |
|
17 callback.call(PopupNotifications.panel); |
|
18 } |
|
19 PopupNotifications.panel.addEventListener(eventName, |
|
20 gActiveListeners[eventName], |
|
21 false); |
|
22 } |
|
23 |
|
24 function unregisterPopupEventHandler(eventName) |
|
25 { |
|
26 PopupNotifications.panel.removeEventListener(eventName, |
|
27 gActiveListeners[eventName], |
|
28 false); |
|
29 delete gActiveListeners[eventName]; |
|
30 } |
|
31 |
|
32 function unregisterAllPopupEventHandlers() |
|
33 { |
|
34 for (let eventName in gActiveListeners) { |
|
35 PopupNotifications.panel.removeEventListener(eventName, |
|
36 gActiveListeners[eventName], |
|
37 false); |
|
38 } |
|
39 gActiveListeners = {}; |
|
40 } |
|
41 |
|
42 function triggerMainCommand(popup) |
|
43 { |
|
44 info("triggering main command"); |
|
45 let notifications = popup.childNodes; |
|
46 ok(notifications.length > 0, "at least one notification displayed"); |
|
47 let notification = notifications[0]; |
|
48 info("triggering command: " + notification.getAttribute("buttonlabel")); |
|
49 |
|
50 // 20, 10 so that the inner button is hit |
|
51 EventUtils.synthesizeMouse(notification.button, 20, 10, {}); |
|
52 } |
|
53 |
|
54 function triggerSecondaryCommand(popup, index) |
|
55 { |
|
56 info("triggering secondary command, " + index); |
|
57 let notifications = popup.childNodes; |
|
58 ok(notifications.length > 0, "at least one notification displayed"); |
|
59 let notification = notifications[0]; |
|
60 |
|
61 // Cancel the arrow panel slide-in transition (bug 767133) such that |
|
62 // it won't interfere with us interacting with the dropdown. |
|
63 SpecialPowers.wrap(document).getAnonymousNodes(popup)[0].style.transition = "none"; |
|
64 |
|
65 notification.button.focus(); |
|
66 |
|
67 popup.addEventListener("popupshown", function () { |
|
68 popup.removeEventListener("popupshown", arguments.callee, false); |
|
69 |
|
70 // Press down until the desired command is selected |
|
71 for (let i = 0; i <= index; i++) |
|
72 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
73 |
|
74 // Activate |
|
75 EventUtils.synthesizeKey("VK_RETURN", {}); |
|
76 }, false); |
|
77 |
|
78 // One down event to open the popup |
|
79 EventUtils.synthesizeKey("VK_DOWN", { altKey: (navigator.platform.indexOf("Mac") == -1) }); |
|
80 } |
|
81 |
|
82 function dismissNotification(popup) |
|
83 { |
|
84 info("dismissing notification"); |
|
85 executeSoon(function () { |
|
86 EventUtils.synthesizeKey("VK_ESCAPE", {}); |
|
87 }); |
|
88 } |
|
89 |
|
90 function setFinishedCallback(callback, win) |
|
91 { |
|
92 if (!win) { |
|
93 win = window; |
|
94 } |
|
95 let testPage = win.gBrowser.selectedBrowser.contentWindow.wrappedJSObject; |
|
96 testPage.testFinishedCallback = function(result, exception) { |
|
97 setTimeout(function() { |
|
98 info("got finished callback"); |
|
99 callback(result, exception); |
|
100 }, 0); |
|
101 } |
|
102 } |
|
103 |
|
104 function dispatchEvent(eventName) |
|
105 { |
|
106 info("dispatching event: " + eventName); |
|
107 let event = document.createEvent("Events"); |
|
108 event.initEvent(eventName, false, false); |
|
109 gBrowser.selectedBrowser.contentWindow.dispatchEvent(event); |
|
110 } |
|
111 |
|
112 function setPermission(url, permission, value) |
|
113 { |
|
114 const nsIPermissionManager = Components.interfaces.nsIPermissionManager; |
|
115 |
|
116 switch (value) { |
|
117 case "allow": |
|
118 value = nsIPermissionManager.ALLOW_ACTION; |
|
119 break; |
|
120 case "deny": |
|
121 value = nsIPermissionManager.DENY_ACTION; |
|
122 break; |
|
123 case "unknown": |
|
124 value = nsIPermissionManager.UNKNOWN_ACTION; |
|
125 break; |
|
126 default: |
|
127 throw new Error("No idea what to set here!"); |
|
128 } |
|
129 |
|
130 let uri = Components.classes["@mozilla.org/network/io-service;1"] |
|
131 .getService(Components.interfaces.nsIIOService) |
|
132 .newURI(url, null, null); |
|
133 let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"] |
|
134 .getService(Ci.nsIScriptSecurityManager) |
|
135 .getNoAppCodebasePrincipal(uri); |
|
136 |
|
137 Components.classes["@mozilla.org/permissionmanager;1"] |
|
138 .getService(Components.interfaces.nsIPermissionManager) |
|
139 .addFromPrincipal(principal, permission, value); |
|
140 } |
|
141 |
|
142 function removePermission(url, permission) |
|
143 { |
|
144 let uri = Components.classes["@mozilla.org/network/io-service;1"] |
|
145 .getService(Components.interfaces.nsIIOService) |
|
146 .newURI(url, null, null); |
|
147 let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"] |
|
148 .getService(Ci.nsIScriptSecurityManager) |
|
149 .getNoAppCodebasePrincipal(uri); |
|
150 |
|
151 Components.classes["@mozilla.org/permissionmanager;1"] |
|
152 .getService(Components.interfaces.nsIPermissionManager) |
|
153 .removeFromPrincipal(principal, permission); |
|
154 } |
|
155 |
|
156 function getPermission(url, permission) |
|
157 { |
|
158 let uri = Components.classes["@mozilla.org/network/io-service;1"] |
|
159 .getService(Components.interfaces.nsIIOService) |
|
160 .newURI(url, null, null); |
|
161 let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"] |
|
162 .getService(Ci.nsIScriptSecurityManager) |
|
163 .getNoAppCodebasePrincipal(uri); |
|
164 |
|
165 return Components.classes["@mozilla.org/permissionmanager;1"] |
|
166 .getService(Components.interfaces.nsIPermissionManager) |
|
167 .testPermissionFromPrincipal(principal, permission); |
|
168 } |