test_notification_serviceworker_openWindow.html (3168B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Bug 1578070</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="/tests/dom/serviceworkers/test/utils.js"></script> 7 <script type="text/javascript" src="/tests/dom/notification/test/mochitest/MockAlertsService.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" /> 10 </head> 11 <body> 12 <p id="display"></p> 13 <div id="content" style="display: none"></div> 14 <pre id="test"></pre> 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 ["dom.webnotifications.disable_open_click_delay", 1000], 23 ["dom.serviceWorkers.idle_timeout", 299999], 24 ["dom.serviceWorkers.idle_extended_timeout", 299999] 25 ]}); 26 27 await NotificationTest.allowNotifications(); 28 await MockAlertsService.register(); 29 await MockAlertsService.enableAutoClick(); 30 }); 31 32 add_task(async function test() { 33 info("Registering service worker."); 34 let swr = await navigator.serviceWorker.register("notification_openWindow.serviceworker.js"); 35 await waitForState(swr.installing, "activated"); 36 37 SimpleTest.registerCleanupFunction(async () => { 38 await swr.unregister(); 39 navigator.serviceWorker.onmessage = null; 40 }); 41 42 for (let prefValue of [ 43 SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_CURRENTWINDOW, 44 SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW, 45 SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_NEWTAB, 46 ]) { 47 if (prefValue == SpecialPowers.Ci.nsIBrowserDOMWindow.OPEN_CURRENTWINDOW) { 48 // Let's open a new tab and focus on it. When the service 49 // worker notification is shown, the document will open in the focused tab. 50 // If we don't open a new tab, the document will be opened in the 51 // current test-runner tab and mess up the test setup. 52 window.open(""); 53 } 54 info(`Setting browser.link.open_newwindow to ${prefValue}.`); 55 await SpecialPowers.pushPrefEnv({ 56 set: [["browser.link.open_newwindow", prefValue]], 57 }); 58 59 // The onnotificationclick handler uses Clients.openWindow() to open a new 60 // window. This newly created window will attempt to open another window with 61 // Window.open() and some arbitrary URL. We crash before the second window 62 // finishes loading. 63 info("Showing notification."); 64 await swr.showNotification("notification"); 65 66 info("Waiting for \"DONE\" from worker."); 67 const result = await new Promise(resolve => { 68 navigator.serviceWorker.addEventListener("message", (event) => { 69 if (event.data?.type !== "DONE") { 70 ok(false, `Unexpected message from service worker: ${JSON.stringify(event.data)}`); 71 } 72 resolve(event.data); 73 }, { once: true }); 74 }); 75 76 is(result.referrer, "", "The referrer should be empty"); 77 } 78 }); 79 80 </script> 81 </pre> 82 </body> 83 </html>