browser_dbg-es-module-worker.js (2716B)
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 debug workers using ES Modules. 6 7 "use strict"; 8 9 const httpServer = createTestHTTPServer(); 10 httpServer.registerContentType("html", "text/html"); 11 httpServer.registerContentType("js", "application/javascript"); 12 13 httpServer.registerPathHandler(`/`, function (request, response) { 14 response.setStatusLine(request.httpVersion, 200, "OK"); 15 response.write(` 16 <html> 17 <script>window.worker = new Worker("worker.js", { type: "module" })</script> 18 </html>`); 19 }); 20 21 httpServer.registerPathHandler("/worker.js", function (request, response) { 22 response.setHeader("Content-Type", "application/javascript"); 23 response.write(` 24 onmessage = () => { 25 console.log("breakpoint line"); 26 postMessage("resume"); 27 } 28 `); 29 }); 30 const port = httpServer.identity.primaryPort; 31 const TEST_URL = `http://localhost:${port}/`; 32 const WORKER_URL = TEST_URL + "worker.js"; 33 34 add_task(async function () { 35 const dbg = await initDebuggerWithAbsoluteURL(TEST_URL); 36 37 // Note that the following APIs only returns the additional threads 38 await waitForThreadCount(dbg, 1); 39 const threads = dbg.selectors.getThreads(); 40 is(threads.length, 1, "Got the page and the worker threads"); 41 is(threads[0].name, "worker.js", "Thread name is correct"); 42 is(threads[0].url, WORKER_URL, "Thread URL is correct"); 43 44 const source = await waitForSource(dbg, "worker.js"); 45 await selectSource(dbg, source); 46 await addBreakpoint(dbg, source, 3); 47 48 info("Call worker code to trigger the breakpoint"); 49 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 50 content.wrappedJSObject.worker.postMessage("foo"); 51 }); 52 53 await waitForPaused(dbg); 54 await assertPausedAtSourceAndLine(dbg, source.id, 3); 55 assertTextContentOnLine(dbg, 3, `console.log("breakpoint line");`); 56 57 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 58 // Create a Promise which will resolve once the worker resumes and send a message 59 // Hold the promise on window, so that a following task can use it. 60 content.onWorkerResumed = new Promise( 61 resolve => (content.wrappedJSObject.worker.onmessage = resolve) 62 ); 63 }); 64 65 await resume(dbg); 66 67 info("Wait for the worker to resume its execution and send a message"); 68 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => { 69 ok( 70 content.onWorkerResumed, 71 "The promise created by the previous task exists" 72 ); 73 await content.onWorkerResumed; 74 ok(true, "The worker resumed its execution"); 75 }); 76 });