Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: os == 'android' OR xorigin OR http3 OR http2
- Manifest: dom/notification/test/mochitest/mochitest.toml
<!DOCTYPE HTML>
<html>
<head>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/dom/serviceworkers/test/utils.js"></script>
<script type="text/javascript" src="/tests/dom/notification/test/mochitest/MockAlertsService.js"></script>
<script type="text/javascript" src="/tests/dom/notification/test/mochitest/NotificationTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
// eslint-disable-next-line mozilla/no-addtask-setup
add_task(async function setup() {
await SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
["dom.serviceWorkers.disable_open_click_delay", 1000],
["dom.serviceWorkers.idle_timeout", 299999],
["dom.serviceWorkers.idle_extended_timeout", 299999]
]});
await NotificationTest.allowNotifications();
await MockAlertsService.register();
await MockAlertsService.enableAutoClick();
});
add_task(async function test() {
info("Registering service worker.");
let swr = await navigator.serviceWorker.register("notification_openWindow_worker.js");
await waitForState(swr.installing, "activated");
SimpleTest.registerCleanupFunction(async () => {
await swr.unregister();
navigator.serviceWorker.onmessage = null;
});
for (let prefValue of [
SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_CURRENTWINDOW,
SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW,
SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_NEWTAB,
]) {
if (prefValue == SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_CURRENTWINDOW) {
// Let's open a new tab and focus on it. When the service
// worker notification is shown, the document will open in the focused tab.
// If we don't open a new tab, the document will be opened in the
// current test-runner tab and mess up the test setup.
window.open("");
}
info(`Setting browser.link.open_newwindow to ${prefValue}.`);
await SpecialPowers.pushPrefEnv({
set: [["browser.link.open_newwindow", prefValue]],
});
// The onnotificationclick handler uses Clients.openWindow() to open a new
// window. This newly created window will attempt to open another window with
// Window.open() and some arbitrary URL. We crash before the second window
// finishes loading.
info("Showing notification.");
await swr.showNotification("notification");
info("Waiting for \"DONE\" from worker.");
await new Promise(resolve => {
navigator.serviceWorker.onmessage = event => {
if (event.data !== "DONE") {
ok(false, `Unexpected message from service worker: ${JSON.stringify(event.data)}`);
}
resolve();
}
});
// If we make it here, then we didn't crash.
ok(true, "Didn't crash!");
navigator.serviceWorker.onmessage = null;
}
});
</script>
</pre>
</body>
</html>