1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* eslint-env mozilla/chrome-script */
const { clearInterval, setInterval, setTimeout } = ChromeUtils.importESModule(
"resource://gre/modules/Timer.sys.mjs"
);
const { BrowserTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/BrowserTestUtils.sys.mjs"
);
// Define these to make EventUtils happy.
let window = this;
let parent = {};
let EventUtils = {};
Services.scriptloader.loadSubScript(
"chrome://mochikit/content/tests/SimpleTest/EventUtils.js",
EventUtils
);
addMessageListener("handlePrompt", msg => {
handlePromptWhenItAppears(msg.action, msg.modalType, msg.isSelect);
});
async function handlePromptWhenItAppears(action, modalType, isSelect) {
if (!(await handlePrompt(action, modalType, isSelect))) {
setTimeout(
() => this.handlePromptWhenItAppears(action, modalType, isSelect),
100
);
}
}
async function handlePrompt(action, modalType, isSelect) {
let ui;
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
let doc = getDialogDoc();
if (!doc) {
return false; // try again in a bit
}
if (isSelect) {
ui = doc;
} else {
ui = doc.defaultView.Dialog.ui;
}
let dialogClosed = BrowserTestUtils.waitForEvent(
browserWin,
"DOMModalDialogClosed"
);
let promptState;
if (isSelect) {
promptState = getSelectState(ui);
dismissSelect(ui, action);
} else {
promptState = getPromptState(ui);
dismissPrompt(ui, action);
}
// Wait until the prompt has been closed before sending callback msg.
// Unless the test explicitly doesn't request a button click.
if (action.buttonClick !== "none") {
await dialogClosed;
}
sendAsyncMessage("promptHandled", { promptState });
return true;
}
function getSelectState(ui) {
let listbox = ui.getElementById("list");
let state = {};
state.msg = ui.getElementById("info.txt").firstChild.textContent;
state.selectedIndex = listbox.selectedIndex;
state.items = [];
for (let i = 0; i < listbox.itemCount; i++) {
let item = listbox.getItemAtIndex(i).label;
state.items.push(item);
}
return state;
}
function getPromptState(ui) {
let state = {};
state.msg = ui.infoBody.textContent;
state.infoRowHidden = ui.infoRow?.hidden || false;
state.titleHidden = ui.infoTitle.hidden;
state.textHidden = ui.loginContainer.hidden;
state.passHidden = ui.password1Container.hidden;
state.checkHidden = ui.checkboxContainer.hidden;
state.checkMsg = state.checkHidden ? "" : ui.checkbox.label;
state.checked = state.checkHidden ? false : ui.checkbox.checked;
state.iconClass = ui.infoIcon.className;
state.textValue = ui.loginTextbox.value;
state.passValue = ui.password1Textbox.value;
state.butt0Label = ui.button0.label;
state.butt1Label = ui.button1.label;
state.butt2Label = ui.button2.label;
state.butt0Disabled = ui.button0.disabled;
state.butt1Disabled = ui.button1.disabled;
state.butt2Disabled = ui.button2.disabled;
function isDefaultButton(b) {
return b.hasAttribute("default") && b.getAttribute("default") == "true";
}
state.defButton0 = isDefaultButton(ui.button0);
state.defButton1 = isDefaultButton(ui.button1);
state.defButton2 = isDefaultButton(ui.button2);
let e = Services.focus.focusedElement;
if (e == null) {
state.focused = null;
} else if (ui.button0.isSameNode(e)) {
state.focused = "button0";
} else if (ui.button1.isSameNode(e)) {
state.focused = "button1";
} else if (ui.button2.isSameNode(e)) {
state.focused = "button2";
} else if (e.isSameNode(ui.loginTextbox)) {
state.focused = "textField";
} else if (e.isSameNode(ui.password1Textbox)) {
state.focused = "passField";
} else if (ui.infoBody.isSameNode(e)) {
state.focused = "infoBody";
} else {
state.focused =
"ERROR: unexpected element focused: " + (e ? e.localName : "<null>");
}
let treeOwner =
ui.prompt && ui.prompt.docShell && ui.prompt.docShell.treeOwner;
if (treeOwner && treeOwner.QueryInterface(Ci.nsIInterfaceRequestor)) {
// Check that the dialog is modal, chrome and dependent;
// We can't just check window.opener because that'll be
// a content window, which therefore isn't exposed (it'll lie and
// be null).
let flags = treeOwner.getInterface(Ci.nsIAppWindow).chromeFlags;
state.chrome = (flags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_CHROME) != 0;
state.dialog = (flags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_DIALOG) != 0;
state.chromeDependent =
(flags & Ci.nsIWebBrowserChrome.CHROME_DEPENDENT) != 0;
let wbc = treeOwner.getInterface(Ci.nsIWebBrowserChrome);
state.isWindowModal = wbc.isWindowModal();
}
// Check the dialog is a common dialog document and has been embedded.
let isEmbedded = !!ui.prompt?.docShell?.chromeEventHandler;
let isCommonDialogDoc =
getDialogDoc()?.location.href.includes("commonDialog.xhtml");
state.isSubDialogPrompt = isCommonDialogDoc && isEmbedded;
state.showCallerOrigin = ui.prompt.args.showCallerOrigin;
return state;
}
function dismissSelect(ui, action) {
let dialog = ui.getElementsByTagName("dialog")[0];
let listbox = ui.getElementById("list");
if (action.selectItem) {
listbox.selectedIndex = 1;
}
if (action.buttonClick == "ok") {
dialog.acceptDialog();
} else if (action.buttonClick == "cancel") {
dialog.cancelDialog();
}
}
function dismissPrompt(ui, action) {
if (action.setCheckbox) {
// Annoyingly, the prompt code is driven by oncommand.
ui.checkbox.checked = true;
ui.checkbox.doCommand();
}
if ("textField" in action) {
ui.loginTextbox.setAttribute("value", action.textField);
}
if ("passField" in action) {
ui.password1Textbox.setAttribute("value", action.passField);
}
switch (action.buttonClick) {
case "ok":
case 0:
ui.button0.click();
break;
case "cancel":
case 1:
ui.button1.click();
break;
case 2:
ui.button2.click();
break;
case "ESC": {
// XXX This is assuming tab-modal.
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
EventUtils.synthesizeKey("KEY_Escape", {}, browserWin);
break;
}
case "pollOK": {
// Buttons are disabled at the moment, poll until they're reenabled.
// Can't use setInterval here, because the window's in a modal state
// and thus DOM events are suppressed.
let interval = setInterval(() => {
if (ui.button0.disabled) {
return;
}
ui.button0.click();
clearInterval(interval);
}, 100);
break;
}
case "abort_dialogs": {
let abortDialogEvent = new ui.prompt.CustomEvent("dialogclosing", {
bubbles: true,
detail: { abort: true },
});
ui.prompt.close(abortDialogEvent);
break;
}
case "none":
break;
default:
throw new Error("dismissPrompt action listed unknown button.");
}
}
function getDialogDoc() {
// Trudge through all the open windows, until we find the one
// that has either commonDialog.xhtml or selectDialog.xhtml loaded.
// var enumerator = Services.wm.getEnumerator("navigator:browser");
for (let { docShell } of Services.wm.getEnumerator(null)) {
var containedDocShells = docShell.getAllDocShellsInSubtree(
docShell.typeChrome,
docShell.ENUMERATE_FORWARDS
);
for (let childDocShell of containedDocShells) {
// Get the corresponding document for this docshell
// We don't want it if it's not done loading.
if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE) {
continue;
}
var childDoc = childDocShell.docViewer.DOMDocument;
if (
childDoc.location.href !=
"chrome://global/content/commonDialog.xhtml" &&
childDoc.location.href != "chrome://global/content/selectDialog.xhtml"
) {
continue;
}
// We're expecting the dialog to be focused. If it's not yet, try later.
// (In particular, this is needed on Linux to reliably check focused elements.)
if (Services.focus.focusedWindow != childDoc.defaultView) {
continue;
}
return childDoc;
}
}
return null;
}
|