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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test beforeunload dialog events.
add_task(async function({ client, tab }) {
info("Allow to trigger onbeforeunload without user interaction");
await new Promise(resolve => {
const options = {
set: [["dom.require_user_interaction_for_beforeunload", false]],
};
SpecialPowers.pushPrefEnv(options, resolve);
});
const { Page } = client;
info("Enable the page domain");
await Page.enable();
info("Attach a valid onbeforeunload handler");
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
content.onbeforeunload = () => true;
});
info("Trigger the beforeunload again but reject the prompt");
const { type } = await triggerBeforeUnload(Page, tab, false);
is(type, "beforeunload", "dialog event contains the correct type");
info("Trigger the beforeunload again and accept the prompt");
const onTabClose = BrowserTestUtils.waitForEvent(tab, "TabClose");
await triggerBeforeUnload(Page, tab, true);
info("Wait for the TabClose event");
await onTabClose;
});
function triggerBeforeUnload(Page, tab, accept) {
// We use then here because after clicking on the close button, nothing
// in the main block of the function will be executed until the prompt
// is accepted or rejected. Attaching a then to this promise still works.
const onDialogOpen = Page.javascriptDialogOpening().then(
async dialogEvent => {
await Page.handleJavaScriptDialog({ accept });
return dialogEvent;
}
);
info("Click on the tab close icon");
tab.closeButton.click();
return onDialogOpen;
}
|