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
|
const TEST_URL = "http://www.example.com/browser/dom/tests/browser/dummy.html";
function pageScript() {
window.addEventListener("beforeunload", function (event) {
var str = "Leaving?";
event.returnValue = str;
return str;
}, true);
}
function frameScript() {
content.window.addEventListener("beforeunload", function (event) {
sendAsyncMessage("Test:OnBeforeUnloadReceived");
var str = "Leaving?";
event.returnValue = str;
return str;
}, true);
}
// Wait for onbeforeunload dialog, and dismiss it immediately.
function awaitAndCloseBeforeUnloadDialog(doStayOnPage) {
return new Promise(resolve => {
function onDialogShown(node) {
Services.obs.removeObserver(onDialogShown, "tabmodal-dialog-loaded");
let button = doStayOnPage ? node.ui.button1 : node.ui.button0;
button.click();
resolve();
}
Services.obs.addObserver(onDialogShown, "tabmodal-dialog-loaded");
});
}
SpecialPowers.pushPrefEnv(
{"set": [["dom.require_user_interaction_for_beforeunload", false]]});
/**
* Test navigation from a content page to a chrome page. Also check that only
* one beforeunload event is fired.
*/
add_task(function* () {
let beforeUnloadCount = 0;
messageManager.addMessageListener("Test:OnBeforeUnloadReceived", function() {
beforeUnloadCount++;
});
// Open a content page.
let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
let browser = tab.linkedBrowser;
ok(browser.isRemoteBrowser, "Browser should be remote.");
browser.messageManager.loadFrameScript(
"data:,(" + frameScript.toString() + ")();", true);
// Navigate to a chrome page.
let dialogShown1 = awaitAndCloseBeforeUnloadDialog(false);
yield BrowserTestUtils.loadURI(browser, "about:support");
yield Promise.all([
dialogShown1,
BrowserTestUtils.browserLoaded(browser)
]);
is(beforeUnloadCount, 1, "Should have received one beforeunload event.");
ok(!browser.isRemoteBrowser, "Browser should not be remote.");
// Go back to content page.
ok(gBrowser.webNavigation.canGoBack, "Should be able to go back.");
gBrowser.goBack();
yield BrowserTestUtils.browserLoaded(browser);
browser.messageManager.loadFrameScript(
"data:,(" + frameScript.toString() + ")();", true);
// Test that going forward triggers beforeunload prompt as well.
ok(gBrowser.webNavigation.canGoForward, "Should be able to go forward.");
let dialogShown2 = awaitAndCloseBeforeUnloadDialog(false);
gBrowser.goForward();
yield Promise.all([
dialogShown2,
BrowserTestUtils.browserLoaded(browser)
]);
is(beforeUnloadCount, 2, "Should have received two beforeunload events.");
yield BrowserTestUtils.removeTab(tab);
});
/**
* Test navigation from a chrome page to a content page. Also check that only
* one beforeunload event is fired.
*/
add_task(function* () {
let beforeUnloadCount = 0;
messageManager.addMessageListener("Test:OnBeforeUnloadReceived", function() {
beforeUnloadCount++;
});
// Open a chrome page.
let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
"about:support");
let browser = tab.linkedBrowser;
ok(!browser.isRemoteBrowser, "Browser should not be remote.");
yield ContentTask.spawn(browser, null, function* () {
content.window.addEventListener("beforeunload", function (event) {
sendAsyncMessage("Test:OnBeforeUnloadReceived");
var str = "Leaving?";
event.returnValue = str;
return str;
}, true);
});
// Navigate to a content page.
let dialogShown1 = awaitAndCloseBeforeUnloadDialog(false);
yield BrowserTestUtils.loadURI(browser, TEST_URL);
yield Promise.all([
dialogShown1,
BrowserTestUtils.browserLoaded(browser)
]);
is(beforeUnloadCount, 1, "Should have received one beforeunload event.");
ok(browser.isRemoteBrowser, "Browser should be remote.");
// Go back to chrome page.
ok(gBrowser.webNavigation.canGoBack, "Should be able to go back.");
gBrowser.goBack();
yield BrowserTestUtils.browserLoaded(browser);
yield ContentTask.spawn(browser, null, function* () {
content.window.addEventListener("beforeunload", function (event) {
sendAsyncMessage("Test:OnBeforeUnloadReceived");
var str = "Leaving?";
event.returnValue = str;
return str;
}, true);
});
// Test that going forward triggers beforeunload prompt as well.
ok(gBrowser.webNavigation.canGoForward, "Should be able to go forward.");
let dialogShown2 = awaitAndCloseBeforeUnloadDialog(false);
gBrowser.goForward();
yield Promise.all([
dialogShown2,
BrowserTestUtils.browserLoaded(browser)
]);
is(beforeUnloadCount, 2, "Should have received two beforeunload events.");
yield BrowserTestUtils.removeTab(tab);
});
|