4 <title>Bug
1578070</title>
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <script src=
"utils.js"></script>
7 <script type=
"text/javascript" src=
"/tests/dom/notification/test/mochitest/MockServices.js"></script>
8 <script type=
"text/javascript" src=
"/tests/dom/notification/test/mochitest/NotificationTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
13 <div id=
"content" style=
"display: none"></div>
15 <script class=
"testbody" type=
"text/javascript">
16 // eslint-disable-next-line mozilla/no-addtask-setup
17 add_task(async function setup() {
18 await SpecialPowers.pushPrefEnv({
"set": [
19 [
"dom.serviceWorkers.exemptFromPerDomainMax", true],
20 [
"dom.serviceWorkers.enabled", true],
21 [
"dom.serviceWorkers.testing.enabled", true],
22 [
"notification.prompt.testing", true],
23 [
"dom.serviceWorkers.disable_open_click_delay",
1000],
24 [
"dom.serviceWorkers.idle_timeout",
299999],
25 [
"dom.serviceWorkers.idle_extended_timeout",
299999]
28 MockServices.register();
29 SimpleTest.requestFlakyTimeout(
"Mock alert service dispatches show and click events.");
30 SimpleTest.registerCleanupFunction(() =
> {
31 MockServices.unregister();
35 add_task(async function test() {
36 info(
"Registering service worker.");
37 let swr = await navigator.serviceWorker.register(
"notification_openWindow_worker.js");
38 await waitForState(swr.installing,
"activated");
40 SimpleTest.registerCleanupFunction(async () =
> {
41 await swr.unregister();
42 navigator.serviceWorker.onmessage = null;
45 for (let prefValue of [
46 SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_CURRENTWINDOW,
47 SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW,
48 SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_NEWTAB,
50 if (prefValue == SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_CURRENTWINDOW) {
51 // Let's open a new tab and focus on it. When the service
52 // worker notification is shown, the document will open in the focused tab.
53 // If we don't open a new tab, the document will be opened in the
54 // current test-runner tab and mess up the test setup.
57 info(`Setting browser.link.open_newwindow to ${prefValue}.`);
58 await SpecialPowers.pushPrefEnv({
59 set: [[
"browser.link.open_newwindow", prefValue]],
62 // The onclicknotification handler uses Clients.openWindow() to open a new
63 // window. This newly created window will attempt to open another window with
64 // Window.open() and some arbitrary URL. We crash before the second window
66 info(
"Showing notification.");
67 await swr.showNotification(
"notification");
69 info(
"Waiting for \"DONE\
" from worker.");
70 await new Promise(resolve =
> {
71 navigator.serviceWorker.onmessage = event =
> {
72 if (event.data !==
"DONE") {
73 ok(false, `Unexpected message from service worker: ${JSON.stringify(event.data)}`);
79 // If we make it here, then we didn't crash.
80 ok(true,
"Didn't crash!");
82 navigator.serviceWorker.onmessage = null;