2017-01-17 05:33:00 +03:00
|
|
|
const TEST_URL = "http://www.example.com/browser/dom/tests/browser/dummy.html";
|
|
|
|
|
2021-04-13 18:51:48 +03:00
|
|
|
const { PromptTestUtils } = ChromeUtils.import(
|
|
|
|
"resource://testing-common/PromptTestUtils.jsm"
|
|
|
|
);
|
|
|
|
|
2017-01-17 05:33:00 +03:00
|
|
|
function pageScript() {
|
2019-01-23 00:26:30 +03:00
|
|
|
window.addEventListener(
|
|
|
|
"beforeunload",
|
|
|
|
function(event) {
|
2017-01-17 05:33:00 +03:00
|
|
|
var str = "Leaving?";
|
|
|
|
event.returnValue = str;
|
|
|
|
return str;
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-27 19:46:59 +03:00
|
|
|
function injectBeforeUnload(browser) {
|
|
|
|
return ContentTask.spawn(browser, null, async function() {
|
2019-01-23 00:26:30 +03:00
|
|
|
content.window.addEventListener(
|
|
|
|
"beforeunload",
|
|
|
|
function(event) {
|
2017-04-27 19:46:59 +03:00
|
|
|
sendAsyncMessage("Test:OnBeforeUnloadReceived");
|
|
|
|
var str = "Leaving?";
|
|
|
|
event.returnValue = str;
|
|
|
|
return str;
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
2017-01-17 05:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for onbeforeunload dialog, and dismiss it immediately.
|
2021-04-13 18:51:48 +03:00
|
|
|
function awaitAndCloseBeforeUnloadDialog(browser, doStayOnPage) {
|
|
|
|
return PromptTestUtils.handleNextPrompt(
|
|
|
|
browser,
|
|
|
|
{ modalType: Services.prompt.MODAL_TYPE_CONTENT, promptType: "confirmEx" },
|
|
|
|
{ buttonNumClick: doStayOnPage ? 1 : 0 }
|
|
|
|
);
|
2017-01-17 05:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-01-23 00:26:02 +03:00
|
|
|
/* global messageManager */
|
2017-01-17 05:33:00 +03:00
|
|
|
add_task(async function() {
|
|
|
|
let beforeUnloadCount = 0;
|
|
|
|
messageManager.addMessageListener("Test:OnBeforeUnloadReceived", function() {
|
|
|
|
beforeUnloadCount++;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Open a content page.
|
|
|
|
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
|
|
|
|
let browser = tab.linkedBrowser;
|
|
|
|
|
|
|
|
ok(browser.isRemoteBrowser, "Browser should be remote.");
|
|
|
|
|
2017-04-27 19:46:59 +03:00
|
|
|
await injectBeforeUnload(browser);
|
2017-01-17 05:33:00 +03:00
|
|
|
// Navigate to a chrome page.
|
2021-04-13 18:51:48 +03:00
|
|
|
let dialogShown1 = awaitAndCloseBeforeUnloadDialog(browser, false);
|
Bug 1671983 - Part 4: Stop awaiting BrowserTestUtils.loadURI, r=annyG,remote-protocol-reviewers,extension-reviewers,preferences-reviewers,whimboo,zombie
This method only is async in order to allow callers to wait for a process switch
triggered by the call to `loadURI` to be finished before resolving. With
DocumentChannel, we should never trigger a process switch eagerly like this
again, so we don't need any of the async behaviour here anymore.
This part is largely mechanical changes to tests, removing the `await` calls on
`loadURI`, and a follow-up part will remove the actual async logic from
`BrowserTestUtils.loadURI`.
Differential Revision: https://phabricator.services.mozilla.com/D94641
2020-11-12 21:01:03 +03:00
|
|
|
BrowserTestUtils.loadURI(browser, "about:support");
|
2019-01-23 00:26:30 +03:00
|
|
|
await Promise.all([dialogShown1, BrowserTestUtils.browserLoaded(browser)]);
|
2017-04-27 19:46:59 +03:00
|
|
|
|
2017-01-17 05:33:00 +03:00
|
|
|
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();
|
|
|
|
await BrowserTestUtils.browserLoaded(browser);
|
2017-04-27 19:46:59 +03:00
|
|
|
await injectBeforeUnload(browser);
|
2017-01-17 05:33:00 +03:00
|
|
|
|
|
|
|
// 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();
|
2019-01-23 00:26:30 +03:00
|
|
|
await Promise.all([dialogShown2, BrowserTestUtils.browserLoaded(browser)]);
|
2017-01-17 05:33:00 +03:00
|
|
|
is(beforeUnloadCount, 2, "Should have received two beforeunload events.");
|
|
|
|
|
2018-03-19 05:16:45 +03:00
|
|
|
BrowserTestUtils.removeTab(tab);
|
2017-01-17 05:33:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test navigation from a chrome page to a content page. Also check that only
|
|
|
|
* one beforeunload event is fired.
|
|
|
|
*/
|
|
|
|
add_task(async function() {
|
|
|
|
let beforeUnloadCount = 0;
|
|
|
|
messageManager.addMessageListener("Test:OnBeforeUnloadReceived", function() {
|
|
|
|
beforeUnloadCount++;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Open a chrome page.
|
|
|
|
let tab = await BrowserTestUtils.openNewForegroundTab(
|
|
|
|
gBrowser,
|
|
|
|
"about:support"
|
|
|
|
);
|
|
|
|
let browser = tab.linkedBrowser;
|
|
|
|
|
|
|
|
ok(!browser.isRemoteBrowser, "Browser should not be remote.");
|
|
|
|
await ContentTask.spawn(browser, null, async function() {
|
2019-01-23 00:26:30 +03:00
|
|
|
content.window.addEventListener(
|
|
|
|
"beforeunload",
|
|
|
|
function(event) {
|
2017-01-17 05:33:00 +03:00
|
|
|
sendAsyncMessage("Test:OnBeforeUnloadReceived");
|
|
|
|
var str = "Leaving?";
|
|
|
|
event.returnValue = str;
|
|
|
|
return str;
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Navigate to a content page.
|
|
|
|
let dialogShown1 = awaitAndCloseBeforeUnloadDialog(false);
|
Bug 1671983 - Part 4: Stop awaiting BrowserTestUtils.loadURI, r=annyG,remote-protocol-reviewers,extension-reviewers,preferences-reviewers,whimboo,zombie
This method only is async in order to allow callers to wait for a process switch
triggered by the call to `loadURI` to be finished before resolving. With
DocumentChannel, we should never trigger a process switch eagerly like this
again, so we don't need any of the async behaviour here anymore.
This part is largely mechanical changes to tests, removing the `await` calls on
`loadURI`, and a follow-up part will remove the actual async logic from
`BrowserTestUtils.loadURI`.
Differential Revision: https://phabricator.services.mozilla.com/D94641
2020-11-12 21:01:03 +03:00
|
|
|
BrowserTestUtils.loadURI(browser, TEST_URL);
|
2019-01-23 00:26:30 +03:00
|
|
|
await Promise.all([dialogShown1, BrowserTestUtils.browserLoaded(browser)]);
|
2017-01-17 05:33:00 +03:00
|
|
|
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();
|
|
|
|
await BrowserTestUtils.browserLoaded(browser);
|
|
|
|
await ContentTask.spawn(browser, null, async function() {
|
2019-01-23 00:26:30 +03:00
|
|
|
content.window.addEventListener(
|
|
|
|
"beforeunload",
|
|
|
|
function(event) {
|
2017-01-17 05:33:00 +03:00
|
|
|
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();
|
2019-01-23 00:26:30 +03:00
|
|
|
await Promise.all([dialogShown2, BrowserTestUtils.browserLoaded(browser)]);
|
2017-01-17 05:33:00 +03:00
|
|
|
is(beforeUnloadCount, 2, "Should have received two beforeunload events.");
|
|
|
|
|
2018-03-19 05:16:45 +03:00
|
|
|
BrowserTestUtils.removeTab(tab);
|
2017-01-17 05:33:00 +03:00
|
|
|
});
|