browser_dbg-windowless-service-workers.js (6089B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 // Test that we can detect a new service worker and hit breakpoints that we've 6 // set in it. 7 8 "use strict"; 9 10 let SW_URL; 11 12 add_task(async function () { 13 info("Subtest #1"); 14 await pushPref("devtools.debugger.threads-visible", true); 15 await pushPref("dom.serviceWorkers.testing.enabled", true); 16 17 // Use an URL with a specific port to check that Bug 1876533 is fixed 18 // We're using URL without port in other tests, like browser_dbg-windowless-service-workers-reload.js 19 const dbg = await initDebuggerWithAbsoluteURL( 20 EXAMPLE_URL_WITH_PORT + "doc-service-workers.html" 21 ); 22 23 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () { 24 await content.wrappedJSObject.registerWorker(); 25 }); 26 const workerSource = await waitForSource(dbg, "service-worker.sjs"); 27 28 // Retrieve the live URL of the Service Worker as it should be "http://" URL. 29 // (based on EXAMPLE_URL_WITH_PORT URL http: + custom port) 30 // But when running with --use-http3-server, the worker has an https URL. 31 SW_URL = workerSource.url; 32 33 await addBreakpoint(dbg, "service-worker.sjs", 13); 34 35 const onFetched = invokeInTab("fetchFromWorker"); 36 37 await waitForPaused(dbg); 38 await assertPausedAtSourceAndLine(dbg, workerSource.id, 13); 39 // Leave the breakpoint and worker in place for the next subtest. 40 await resume(dbg); 41 info("Waiting for the fetch request done from the page to complete"); 42 await onFetched; 43 await waitForRequestsToSettle(dbg); 44 await closeTabAndToolbox(); 45 }); 46 47 // Test that breakpoints can be immediately hit in service workers when reloading. 48 add_task(async function () { 49 info("Subtest #2"); 50 51 const toolbox = await openNewTabAndToolbox( 52 `${EXAMPLE_URL_WITH_PORT}doc-service-workers.html`, 53 "jsdebugger" 54 ); 55 const dbg = createDebuggerContext(toolbox); 56 57 await checkAdditionalThreadCount(dbg, 1); 58 59 // The test page will immediately fetch from the service worker if registered. 60 const onReloaded = reload(dbg); 61 62 const workerSource = await waitForSource(dbg, "service-worker.sjs"); 63 64 await waitForPaused(dbg); 65 await assertPausedAtSourceAndLine(dbg, workerSource.id, 13); 66 await checkAdditionalThreadCount(dbg, 1); 67 68 await resume(dbg); 69 await dbg.actions.removeAllBreakpoints(); 70 71 info("Wait for reload to complete after resume"); 72 await onReloaded; 73 74 await unregisterServiceWorker(SW_URL); 75 76 await checkAdditionalThreadCount(dbg, 0); 77 await waitForRequestsToSettle(dbg); 78 79 await closeTabAndToolbox(); 80 }); 81 82 // Test having a waiting and active service worker for the same registration. 83 add_task(async function () { 84 info("Subtest #3"); 85 86 const toolbox = await openNewTabAndToolbox( 87 `${EXAMPLE_URL_WITH_PORT}doc-service-workers.html`, 88 "jsdebugger" 89 ); 90 const dbg = createDebuggerContext(toolbox); 91 92 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () { 93 await content.wrappedJSObject.registerWorker(); 94 }); 95 await checkAdditionalThreadCount(dbg, 1); 96 await checkWorkerStatus(dbg, "activated"); 97 98 const firstTab = gBrowser.selectedTab; 99 100 await addTab( 101 `${EXAMPLE_URL_WITH_PORT}service-worker.sjs?setStatus=newServiceWorker` 102 ); 103 await removeTab(gBrowser.selectedTab); 104 105 const secondTab = await addTab( 106 `${EXAMPLE_URL_WITH_PORT}doc-service-workers.html` 107 ); 108 109 await gBrowser.selectTabAtIndex(gBrowser.tabs.indexOf(firstTab)); 110 await checkAdditionalThreadCount(dbg, 2); 111 112 const sources = await waitFor(() => { 113 const list = dbg.selectors 114 .getSourceList() 115 .filter(s => s.url.includes("service-worker.sjs")); 116 return list.length == 1 ? list : null; 117 }); 118 ok(sources.length, "Found one sources for service worker"); 119 120 // Add a breakpoint for the next subtest. 121 await addBreakpoint(dbg, "service-worker.sjs", 2); 122 123 await unregisterServiceWorker(SW_URL); 124 125 await checkAdditionalThreadCount(dbg, 0); 126 await waitForRequestsToSettle(dbg); 127 await removeTab(firstTab); 128 await removeTab(secondTab); 129 130 // Reset the SJS in case we will be repeating the test. 131 await addTab(`${EXAMPLE_URL_WITH_PORT}service-worker.sjs?setStatus=`); 132 await removeTab(gBrowser.selectedTab); 133 }); 134 135 // Test setting breakpoints while the service worker is starting up. 136 add_task(async function () { 137 info("Subtest #4"); 138 if (Services.appinfo.fissionAutostart) { 139 // Disabled when serviceworker isolation is used due to bug 1749341 140 return; 141 } 142 143 const toolbox = await openNewTabAndToolbox( 144 `${EXAMPLE_URL_WITH_PORT}doc-service-workers.html`, 145 "jsdebugger" 146 ); 147 const dbg = createDebuggerContext(toolbox); 148 149 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () { 150 await content.wrappedJSObject.registerWorker(); 151 }); 152 await checkAdditionalThreadCount(dbg, 1); 153 154 const workerSource = await waitForSource(dbg, "service-worker.sjs"); 155 156 await waitForBreakpointCount(dbg, 1); 157 await waitForPaused(dbg); 158 await assertPausedAtSourceAndLine(dbg, workerSource.id, 2); 159 await checkWorkerStatus(dbg, "parsed"); 160 161 await addBreakpoint(dbg, "service-worker.sjs", 19); 162 await resume(dbg); 163 await waitForPaused(dbg); 164 await assertPausedAtSourceAndLine(dbg, workerSource.id, 19); 165 await checkWorkerStatus(dbg, "installing"); 166 167 await addBreakpoint(dbg, "service-worker.sjs", 5); 168 await resume(dbg); 169 await waitForPaused(dbg); 170 await assertPausedAtSourceAndLine(dbg, workerSource.id, 5); 171 await checkWorkerStatus(dbg, "activating"); 172 173 await resume(dbg); 174 await unregisterServiceWorker(SW_URL); 175 176 await checkAdditionalThreadCount(dbg, 0); 177 await waitForRequestsToSettle(dbg); 178 179 await closeTabAndToolbox(); 180 }); 181 182 async function checkWorkerStatus(_dbg, _status) { 183 /* TODO: Re-Add support for showing service worker status (Bug 1641099) 184 await waitUntil(() => { 185 const threads = dbg.selectors.getThreads(); 186 return threads.some(t => t.serviceWorkerStatus == status); 187 }); 188 ok(true, `Have thread with status ${status}`); 189 */ 190 }