2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
6 var gActiveListeners = {};
8 // These event (un)registration handlers only work for one window, DONOT use
9 // them with multiple windows.
10 function registerPopupEventHandler(eventName, callback, win) {
14 gActiveListeners[eventName] = function(event) {
15 if (event.target != win.PopupNotifications.panel) {
18 win.PopupNotifications.panel.removeEventListener(
20 gActiveListeners[eventName]
22 delete gActiveListeners[eventName];
24 callback.call(win.PopupNotifications.panel);
26 win.PopupNotifications.panel.addEventListener(
28 gActiveListeners[eventName]
32 function unregisterPopupEventHandler(eventName, win) {
36 win.PopupNotifications.panel.removeEventListener(
38 gActiveListeners[eventName]
40 delete gActiveListeners[eventName];
43 function unregisterAllPopupEventHandlers(win) {
47 for (let eventName in gActiveListeners) {
48 win.PopupNotifications.panel.removeEventListener(
50 gActiveListeners[eventName]
53 gActiveListeners = {};
56 function triggerMainCommand(popup) {
57 info("triggering main command");
58 let notifications = popup.childNodes;
59 ok(notifications.length, "at least one notification displayed");
60 let notification = notifications[0];
61 info("triggering command: " + notification.getAttribute("buttonlabel"));
63 EventUtils.synthesizeMouseAtCenter(notification.button, {});
66 function triggerSecondaryCommand(popup, win) {
70 info("triggering secondary command");
71 let notifications = popup.childNodes;
72 ok(notifications.length, "at least one notification displayed");
73 let notification = notifications[0];
74 EventUtils.synthesizeMouseAtCenter(notification.secondaryButton, {}, win);
77 function dismissNotification(popup) {
78 info("dismissing notification");
79 executeSoon(function() {
80 EventUtils.synthesizeKey("KEY_Escape");
84 function waitForMessage(aMessage, browser) {
85 // We cannot capture aMessage inside the checkFn, so we override the
86 // checkFn.toSource to tunnel aMessage instead.
87 let checkFn = function() {};
88 checkFn.toSource = function() {
89 return `function checkFn(event) {
90 let message = ${aMessage.toSource()};
91 if (event.data == message) {
95 \`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
100 return BrowserTestUtils.waitForContentEvent(
101 browser.selectedBrowser,
105 /* wantsUntrusted */ true
107 // An assertion in checkFn wouldn't be recorded as part of the test, so we
108 // use this assertion to confirm that we've successfully received the
109 // message (we'll only reach this point if that's the case).
110 ok(true, "Received message: " + aMessage);
114 function dispatchEvent(eventName) {
115 info("dispatching event: " + eventName);
116 let event = document.createEvent("Events");
117 event.initEvent(eventName, false, false);
118 gBrowser.selectedBrowser.contentWindow.dispatchEvent(event);
121 function setPermission(url, permission, originAttributes = {}) {
122 let uri = Services.io.newURI(url);
123 let principal = Services.scriptSecurityManager.createContentPrincipal(
128 Services.perms.addFromPrincipal(
131 Ci.nsIPermissionManager.ALLOW_ACTION
135 function removePermission(url, permission, originAttributes = {}) {
136 let uri = Services.io.newURI(url);
137 let principal = Services.scriptSecurityManager.createContentPrincipal(
142 Services.perms.removeFromPrincipal(principal, permission);
145 function getPermission(url, permission, originAttributes = {}) {
146 let uri = Services.io.newURI(url);
147 let principal = Services.scriptSecurityManager.createContentPrincipal(
152 return Services.perms.testPermissionFromPrincipal(principal, permission);