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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
const { ComponentUtils } = ChromeUtils.importESModule(
"resource://gre/modules/ComponentUtils.sys.mjs"
);
const ROOT = getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
"https://example.com"
);
const PAGE_URL = ROOT + "download_page.html";
const SJS_URL = ROOT + "download.sjs";
const HELPERAPP_DIALOG_CONTRACT_ID = "@mozilla.org/helperapplauncherdialog;1";
const HELPERAPP_DIALOG_CID = Components.ID(
Cc[HELPERAPP_DIALOG_CONTRACT_ID].number
);
const MOCK_HELPERAPP_DIALOG_CID = Components.ID(
"{2f372b6f-56c9-46d5-af0d-9f09bb69860c}"
);
let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
let curDialogResolve = null;
function HelperAppLauncherDialog() {}
HelperAppLauncherDialog.prototype = {
show(aLauncher, aWindowContext, aReason) {
ok(true, "Showing the helper app dialog");
curDialogResolve(aWindowContext);
executeSoon(() => {
aLauncher.cancel(Cr.NS_ERROR_ABORT);
});
},
QueryInterface: ChromeUtils.generateQI(["nsIHelperAppLauncherDialog"]),
};
function promiseHelperAppDialog() {
return new Promise(resolve => {
curDialogResolve = resolve;
});
}
let mockHelperAppService;
add_setup(async function () {
// Replace the real helper app dialog with our own.
mockHelperAppService = ComponentUtils.generateSingletonFactory(
HelperAppLauncherDialog
);
registrar.registerFactory(
MOCK_HELPERAPP_DIALOG_CID,
"",
HELPERAPP_DIALOG_CONTRACT_ID,
mockHelperAppService
);
// Ensure we always prompt for these downloads.
const HandlerService = Cc[
"@mozilla.org/uriloader/handler-service;1"
].getService(Ci.nsIHandlerService);
const MIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
const mimeInfo = MIMEService.getFromTypeAndExtension(
"application/octet-stream",
"bin"
);
mimeInfo.alwaysAskBeforeHandling = true;
HandlerService.store(mimeInfo);
// On Mac, .bin is application/macbinary
let mimeInfoMac;
if (AppConstants.platform == "macosx") {
mimeInfoMac = MIMEService.getFromTypeAndExtension(
"application/macbinary",
"bin"
);
mimeInfoMac.alwaysAskBeforeHandling = true;
HandlerService.store(mimeInfoMac);
}
registerCleanupFunction(() => {
HandlerService.remove(mimeInfo);
if (mimeInfoMac) {
HandlerService.remove(mimeInfoMac);
}
});
});
add_task(async function simple_navigation() {
// Tests that simple navigation gives us the right windowContext (that is,
// the window that we're using).
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
let dialogAppeared = promiseHelperAppDialog();
await BrowserTestUtils.synthesizeMouseAtCenter(
"#regular_load",
{},
browser
);
let windowContext = await dialogAppeared;
is(windowContext, browser.ownerGlobal, "got the right windowContext");
}
);
});
add_task(async function accel_navigation() {
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
let dialogAppeared = promiseHelperAppDialog();
let tabOpened = BrowserTestUtils.waitForEvent(
gBrowser.tabContainer,
"TabOpen"
).then(event => {
return [event.target, BrowserTestUtils.waitForTabClosing(event.target)];
});
await BrowserTestUtils.synthesizeMouseAtCenter(
"#regular_load",
{ accelKey: true },
browser
);
let windowContext = await dialogAppeared;
is(windowContext, browser.ownerGlobal, "got the right windowContext");
let [tab, closingPromise] = await tabOpened;
await closingPromise;
is(tab.linkedBrowser, null, "tab was opened and closed");
}
);
});
// Given a browser pointing to download_page.html, clicks on the link that
// opens with target="_blank" (i.e. a new tab) and ensures that we
// automatically open and close that tab.
async function testNewTab(browser) {
let dialogAppeared = promiseHelperAppDialog();
let tabOpened = BrowserTestUtils.waitForEvent(
gBrowser.tabContainer,
"TabOpen"
).then(event => {
return [event.target, BrowserTestUtils.waitForTabClosing(event.target)];
});
await BrowserTestUtils.synthesizeMouseAtCenter("#target_blank", {}, browser);
let windowContext = await dialogAppeared;
is(windowContext, browser.ownerGlobal, "got the right windowContext");
let [tab, closingPromise] = await tabOpened;
await closingPromise;
is(tab.linkedBrowser, null, "tab was opened and closed");
}
add_task(async function target_blank() {
// Tests that a link with target=_blank opens a new tab and closes it,
// returning the window that we're using for navigation.
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
await testNewTab(browser);
}
);
});
add_task(async function target_blank_no_opener() {
// Tests that a link with target=_blank and no opener opens a new tab
// and closes it, returning the window that we're using for navigation.
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
let dialogAppeared = promiseHelperAppDialog();
let tabOpened = BrowserTestUtils.waitForEvent(
gBrowser.tabContainer,
"TabOpen"
).then(event => {
return [event.target, BrowserTestUtils.waitForTabClosing(event.target)];
});
await BrowserTestUtils.synthesizeMouseAtCenter(
"#target_blank_no_opener",
{},
browser
);
let windowContext = await dialogAppeared;
is(windowContext, browser.ownerGlobal, "got the right windowContext");
let [tab, closingPromise] = await tabOpened;
await closingPromise;
is(tab.linkedBrowser, null, "tab was opened and closed");
}
);
});
add_task(async function open_in_new_tab_no_opener() {
// Tests that a link with target=_blank and no opener opens a new tab
// and closes it, returning the window that we're using for navigation.
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
let dialogAppeared = promiseHelperAppDialog();
let tabOpened = BrowserTestUtils.waitForEvent(
gBrowser.tabContainer,
"TabOpen"
).then(event => {
return [event.target, BrowserTestUtils.waitForTabClosing(event.target)];
});
await BrowserTestUtils.synthesizeMouseAtCenter(
"#open_in_new_tab_no_opener",
{},
browser
);
let windowContext = await dialogAppeared;
is(windowContext, browser.ownerGlobal, "got the right windowContext");
let [tab, closingPromise] = await tabOpened;
await closingPromise;
is(tab.linkedBrowser, null, "tab was opened and closed");
}
);
});
add_task(async function new_window() {
// Tests that a link that forces us to open a new window (by specifying a
// width and a height in window.open) opens a new window for the load,
// realizes that we need to close that window and returns the *original*
// window as the window context.
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
let dialogAppeared = promiseHelperAppDialog();
let windowOpened = BrowserTestUtils.waitForNewWindow();
await BrowserTestUtils.synthesizeMouseAtCenter(
"#new_window",
{},
browser
);
let win = await windowOpened;
// Now allow request to complete:
fetch(SJS_URL + "?finish");
let windowContext = await dialogAppeared;
is(windowContext, browser.ownerGlobal, "got the right windowContext");
// The window should close on its own. If not, this test will time out.
await BrowserTestUtils.domWindowClosed(win);
ok(win.closed, "window was opened and closed");
is(
await fetch(SJS_URL + "?reset").then(r => r.text()),
"OK",
"Test reseted"
);
}
);
});
add_task(async function new_window_no_opener() {
// Tests that a link that forces us to open a new window (by specifying a
// width and a height in window.open) opens a new window for the load,
// realizes that we need to close that window and returns the *original*
// window as the window context.
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (browser) {
let dialogAppeared = promiseHelperAppDialog();
let windowOpened = BrowserTestUtils.waitForNewWindow();
await BrowserTestUtils.synthesizeMouseAtCenter(
"#new_window_no_opener",
{},
browser
);
let win = await windowOpened;
// Now allow request to complete:
fetch(SJS_URL + "?finish");
await dialogAppeared;
// The window should close on its own. If not, this test will time out.
await BrowserTestUtils.domWindowClosed(win);
ok(win.closed, "window was opened and closed");
is(
await fetch(SJS_URL + "?reset").then(r => r.text()),
"OK",
"Test reseted"
);
}
);
});
add_task(async function nested_window_opens() {
// Tests that the window auto-closing feature works if the download is
// initiated by a window that, itself, has an opener (see bug 1373109).
await BrowserTestUtils.withNewTab(
{ gBrowser, url: PAGE_URL },
async function (outerBrowser) {
let secondTabPromise = BrowserTestUtils.waitForNewTab(
gBrowser,
`${PAGE_URL}?newwin`,
true
);
BrowserTestUtils.synthesizeMouseAtCenter(
"#open_in_new_tab",
{},
outerBrowser
);
let secondTab = await secondTabPromise;
let nestedBrowser = secondTab.linkedBrowser;
await SpecialPowers.spawn(nestedBrowser, [], function () {
ok(content.opener, "this window has an opener");
});
await testNewTab(nestedBrowser);
isnot(
secondTab.linkedBrowser,
null,
"the page that triggered the download is still open"
);
BrowserTestUtils.removeTab(secondTab);
}
);
});
add_task(async function cleanup() {
// Unregister our factory from XPCOM and restore the original CID.
registrar.unregisterFactory(MOCK_HELPERAPP_DIALOG_CID, mockHelperAppService);
registrar.registerFactory(
HELPERAPP_DIALOG_CID,
"",
HELPERAPP_DIALOG_CONTRACT_ID,
null
);
});
|