Bug 1943514 - Close the RC sidebar panel when users opt out of/turn off Review Checke...
[gecko.git] / dom / quota / test / browser / head.js
blob38b2e82cf0f4a4c8587a7f26917d8f56b6c90ee2
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 // The path to the top level directory.
7 const depth = "../../../../";
9 var gActiveListeners = {};
11 loadScript("dom/quota/test/common/browser.js");
13 function loadScript(path) {
14 const url = new URL(depth + path, gTestPath);
15 Services.scriptloader.loadSubScript(url.href, this);
18 // These event (un)registration handlers only work for one window, DONOT use
19 // them with multiple windows.
21 function registerPopupEventHandler(eventName, callback, win) {
22 if (!win) {
23 win = window;
25 gActiveListeners[eventName] = function (event) {
26 if (event.target != win.PopupNotifications.panel) {
27 return;
29 win.PopupNotifications.panel.removeEventListener(
30 eventName,
31 gActiveListeners[eventName]
33 delete gActiveListeners[eventName];
35 callback.call(win.PopupNotifications.panel);
37 win.PopupNotifications.panel.addEventListener(
38 eventName,
39 gActiveListeners[eventName]
43 function unregisterAllPopupEventHandlers(win) {
44 if (!win) {
45 win = window;
47 for (let eventName in gActiveListeners) {
48 win.PopupNotifications.panel.removeEventListener(
49 eventName,
50 gActiveListeners[eventName]
53 gActiveListeners = {};
56 function triggerMainCommand(popup, win) {
57 if (!win) {
58 win = window;
60 info("triggering main command");
61 let notifications = popup.childNodes;
62 ok(notifications.length, "at least one notification displayed");
63 let notification = notifications[0];
64 info("triggering command: " + notification.getAttribute("buttonlabel"));
66 EventUtils.synthesizeMouseAtCenter(notification.button, {}, win);
69 async function triggerSecondaryCommand(popup, remember = false, win = window) {
70 info("triggering secondary command");
71 let notifications = popup.childNodes;
72 ok(notifications.length, "at least one notification displayed");
73 let notification = notifications[0];
75 if (remember) {
76 notification.checkbox.checked = true;
79 await EventUtils.synthesizeMouseAtCenter(
80 notification.secondaryButton,
81 {},
82 win
86 function dismissNotification(popup, win = window) {
87 info("dismissing notification");
88 executeSoon(function () {
89 EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
90 });
93 function waitForMessage(aMessage, browser) {
94 // We cannot capture aMessage inside the checkFn, so we override the
95 // checkFn.toSource to tunnel aMessage instead.
96 let checkFn = function () {};
97 checkFn.toSource = function () {
98 return `function checkFn(event) {
99 let message = ${aMessage.toSource()};
100 if (event.data == message) {
101 return true;
103 throw new Error(
104 \`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
109 return BrowserTestUtils.waitForContentEvent(
110 browser.selectedBrowser,
111 "message",
112 /* capture */ true,
113 checkFn,
114 /* wantsUntrusted */ true
115 ).then(() => {
116 // An assertion in checkFn wouldn't be recorded as part of the test, so we
117 // use this assertion to confirm that we've successfully received the
118 // message (we'll only reach this point if that's the case).
119 ok(true, "Received message: " + aMessage);
123 function removePermission(url, permission) {
124 let uri = Cc["@mozilla.org/network/io-service;1"]
125 .getService(Ci.nsIIOService)
126 .newURI(url);
127 let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
128 Ci.nsIScriptSecurityManager
130 let principal = ssm.createContentPrincipal(uri, {});
132 Cc["@mozilla.org/permissionmanager;1"]
133 .getService(Ci.nsIPermissionManager)
134 .removeFromPrincipal(principal, permission);
137 function getPermission(url, permission) {
138 let uri = Cc["@mozilla.org/network/io-service;1"]
139 .getService(Ci.nsIIOService)
140 .newURI(url);
141 let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
142 Ci.nsIScriptSecurityManager
144 let principal = ssm.createContentPrincipal(uri, {});
146 return Cc["@mozilla.org/permissionmanager;1"]
147 .getService(Ci.nsIPermissionManager)
148 .testPermissionFromPrincipal(principal, permission);