browser_openwindow_without_window.js (3945B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const PAGE_URI = 7 "https://example.com/browser/dom/notification/test/browser/empty.html"; 8 const OPEN_URI = 9 "https://example.com/browser/dom/notification/test/browser/file_openwindow.html"; 10 11 let { MockRegistrar } = ChromeUtils.importESModule( 12 "resource://testing-common/MockRegistrar.sys.mjs" 13 ); 14 15 let callListener = () => { 16 throw new Error("callListener is unexpectedly called before showAlert"); 17 }; 18 19 let mockAlertsService = { 20 showAlert(alert, alertListener) { 21 ok(true, "Showing alert"); 22 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 23 setTimeout(function () { 24 alertListener.observe(null, "alertshow", null); 25 }, 100); 26 callListener = () => 27 alertListener.observe(null, "alertclickcallback", null); 28 }, 29 30 QueryInterface: ChromeUtils.generateQI(["nsIAlertsService"]), 31 32 createInstance(aIID) { 33 return this.QueryInterface(aIID); 34 }, 35 }; 36 37 add_setup(() => { 38 let mockCid = MockRegistrar.register( 39 "@mozilla.org/alerts-service;1", 40 mockAlertsService 41 ); 42 43 registerCleanupFunction(() => { 44 MockRegistrar.unregister(mockCid); 45 }); 46 47 BrowserTestUtils.concealWindow(window, { signal: testSignal }); 48 }); 49 50 let configs = [ 51 { 52 permanentPbm: false, 53 browserStartup: 1, 54 }, 55 { 56 permanentPbm: true, 57 browserStartup: 1, 58 }, 59 { 60 permanentPbm: false, 61 browserStartup: 3, 62 }, 63 ]; 64 65 for (let { permanentPbm, browserStartup } of configs) { 66 add_task(async function () { 67 info(`Test with PBM: ${permanentPbm}`); 68 info(`Test with startup behavior: ${browserStartup}`); 69 70 await SpecialPowers.pushPrefEnv({ 71 set: [ 72 ["browser.privatebrowsing.autostart", permanentPbm], 73 ["browser.startup.page", browserStartup], 74 ], 75 }); 76 77 let win = await BrowserTestUtils.openNewBrowserWindow(); 78 let browser = win.gBrowser.selectedBrowser; 79 80 BrowserTestUtils.startLoadingURIString(browser, PAGE_URI); 81 82 await BrowserTestUtils.browserLoaded(browser); 83 84 // Register a service worker and show a notification 85 await SpecialPowers.spawn(browser, [], async () => { 86 await SpecialPowers.pushPermissions([ 87 { 88 type: "desktop-notification", 89 allow: SpecialPowers.Services.perms.ALLOW_ACTION, 90 context: content.document, 91 }, 92 ]); 93 94 // Registration of the SW 95 const swr = await content.navigator.serviceWorker.register( 96 "file_openwindow.serviceworker.js" 97 ); 98 99 // Activation 100 await content.navigator.serviceWorker.ready; 101 102 // Ask for an openWindow. 103 await swr.showNotification("testPopup"); 104 }); 105 106 registerCleanupFunction(() => SpecialPowers.removeAllServiceWorkerData()); 107 108 // Now close the current window via BrowserCommands 109 // (simple .close() will not trigger session store as it does not notify 110 // browser-lastwindow-close-granted) 111 let closedPromise = BrowserTestUtils.windowClosed(win); 112 win.BrowserCommands.tryToCloseWindow(); 113 await closedPromise; 114 115 // Let session startup read the changed pref again 116 SessionStartup.resetForTest(); 117 118 let newWinPromise = BrowserTestUtils.waitForNewWindow({ 119 url: OPEN_URI, 120 }); 121 122 // Simulate clicking the notification 123 callListener(); 124 125 // See it can open a new window 126 let newWin = await newWinPromise; 127 ok(true, "Should get the window"); 128 129 is( 130 PrivateBrowsingUtils.isWindowPrivate(newWin), 131 permanentPbm, 132 "PBM state of the new window should match the existing PBM mode" 133 ); 134 135 await SpecialPowers.spawn(newWin.gBrowser.selectedBrowser, [], async () => { 136 if (!content.wrappedJSObject.promise) { 137 throw new Error("Not a promise"); 138 } 139 await content.wrappedJSObject.promise; 140 }); 141 ok(true, "Should be able to post to the resulting WindowClient"); 142 143 newWin.close(); 144 }); 145 }