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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { PromptTestUtils } = ChromeUtils.import(
"resource://testing-common/PromptTestUtils.jsm"
);
let { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const TEST_ROOT = getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
"https://example.com"
);
async function checkAlert(
pageToLoad,
expectedTitle,
expectedIcon = "chrome://global/skin/icons/defaultFavicon.svg"
) {
function openFn(browser) {
return SpecialPowers.spawn(browser, [], () => {
if (content.document.nodePrincipal.isSystemPrincipal) {
// Can't eval in privileged contexts due to CSP, just call directly:
content.alert("Test");
} else {
// Eval everywhere else so it gets the principal of the loaded page.
content.eval("alert('Test')");
}
});
}
return checkDialog(pageToLoad, openFn, expectedTitle, expectedIcon);
}
async function checkBeforeunload(
pageToLoad,
expectedTitle,
expectedIcon = "chrome://global/skin/icons/defaultFavicon.svg"
) {
async function openFn(browser) {
let tab = gBrowser.getTabForBrowser(browser);
await BrowserTestUtils.synthesizeMouseAtCenter(
"body",
{},
browser.browsingContext
);
return gBrowser.removeTab(tab); // trigger beforeunload.
}
return checkDialog(pageToLoad, openFn, expectedTitle, expectedIcon);
}
async function checkDialog(
pageToLoad,
openFn,
expectedTitle,
expectedIcon,
modalType = Ci.nsIPrompt.MODAL_TYPE_CONTENT
) {
return BrowserTestUtils.withNewTab(pageToLoad, async browser => {
let promptPromise = PromptTestUtils.waitForPrompt(browser, {
modalType,
});
let spawnPromise = openFn(browser);
let dialog = await promptPromise;
let doc = dialog.ui.prompt.document;
let titleEl = doc.getElementById("titleText");
if (expectedTitle.value) {
is(titleEl.textContent, expectedTitle.value, "Title should match.");
} else {
is(
titleEl.dataset.l10nId,
expectedTitle.l10nId,
"Title l10n id should match."
);
}
ok(
!titleEl.parentNode.hasAttribute("overflown"),
"Title should fit without overflowing."
);
ok(BrowserTestUtils.is_visible(titleEl), "New title should be shown.");
ok(
BrowserTestUtils.is_hidden(doc.getElementById("infoTitle")),
"Old title should be hidden."
);
let iconCS = doc.ownerGlobal.getComputedStyle(
doc.querySelector(".titleIcon")
);
is(
iconCS.backgroundImage,
`url("${expectedIcon}")`,
"Icon is as expected."
);
// This is not particularly neat, but we want to also test overflow
// Our test systems don't have hosts that long, so just fake it:
if (browser.currentURI.asciiHost == "example.com") {
let longerDomain = "extravagantly.long.".repeat(10) + "example.com";
doc.documentElement.setAttribute(
"headertitle",
JSON.stringify({ raw: longerDomain, shouldUseMaskFade: true })
);
info("Wait for the prompt title to update.");
await BrowserTestUtils.waitForMutationCondition(
titleEl,
{ characterData: true, attributes: true },
() =>
titleEl.textContent == longerDomain &&
titleEl.parentNode.hasAttribute("overflown")
);
is(titleEl.textContent, longerDomain, "The longer domain is reflected.");
ok(
titleEl.parentNode.hasAttribute("overflown"),
"The domain should overflow."
);
}
// Close the prompt again.
await PromptTestUtils.handlePrompt(dialog);
// The alert in the content process was sync, we need to make sure it gets
// cleaned up, but couldn't await it above because that'd hang the test!
await spawnPromise;
});
}
add_setup(async function() {
await SpecialPowers.pushPrefEnv({
set: [
["prompts.contentPromptSubDialog", true],
["prompts.modalType.httpAuth", Ci.nsIPrompt.MODAL_TYPE_TAB],
["prompts.tabChromePromptSubDialog", true],
],
});
});
add_task(async function test_check_prompt_origin_display() {
await checkAlert("https://example.com/", { value: "example.com" });
await checkAlert("http://example.com/", { value: "example.com" });
await checkAlert("data:text/html,<body>", {
l10nId: "common-dialog-title-null",
});
let homeDir = Services.dirsvc.get("Home", Ci.nsIFile);
let fileURI = Services.io.newFileURI(homeDir).spec;
await checkAlert(fileURI, { value: "file://" });
await checkAlert(
"about:config",
{ l10nId: "common-dialog-title-system" },
"chrome://branding/content/icon32.png"
);
await checkBeforeunload(TEST_ROOT + "file_beforeunload_stop.html", {
value: "example.com",
});
});
add_task(async function test_check_auth() {
let server = new HttpServer();
registerCleanupFunction(() => {
return new Promise(resolve => {
server.stop(() => {
server = null;
resolve();
});
});
});
function forbiddenHandler(meta, res) {
res.setStatusLine(meta.httpVersion, 401, "Unauthorized");
res.setHeader("WWW-Authenticate", 'Basic realm="Realm"');
}
function pageHandler(meta, res) {
res.setStatusLine(meta.httpVersion, 200, "OK");
res.setHeader("Content-Type", "text/html");
let body = "<html><body></body></html>";
res.bodyOutputStream.write(body, body.length);
}
server.registerPathHandler("/forbidden", forbiddenHandler);
server.registerPathHandler("/page", pageHandler);
server.start(-1);
const HOST = `localhost:${server.identity.primaryPort}`;
const AUTH_URI = `http://${HOST}/forbidden`;
// Try a simple load:
await checkDialog(
"https://example.com/",
browser => BrowserTestUtils.loadURI(browser, AUTH_URI),
HOST,
"chrome://global/skin/icons/defaultFavicon.svg",
Ci.nsIPrompt.MODAL_TYPE_TAB
);
let subframeLoad = function(browser) {
return SpecialPowers.spawn(browser, [AUTH_URI], uri => {
let f = content.document.createElement("iframe");
f.src = uri;
content.document.body.appendChild(f);
});
};
// Try x-origin subframe:
await checkDialog(
"http://example.org/1",
subframeLoad,
HOST,
/* Because this is x-origin, we expect a different icon: */
"chrome://global/skin/icons/security-broken.svg",
Ci.nsIPrompt.MODAL_TYPE_TAB
);
});
|