From d673356e6917ae93bbcc6604918a80515e3e6cc8 Mon Sep 17 00:00:00 2001 From: Brian Hackett Date: Wed, 6 Nov 2019 17:10:49 +0000 Subject: [PATCH] Bug 1563607 Part 5 - Add test for windowless service worker debugging, r=jdescottes. Depends on D47544 Differential Revision: https://phabricator.services.mozilla.com/D47545 --HG-- extra : moz-landing-system : lando --- .../debugger/test/mochitest/browser.ini | 1 + .../browser_dbg-windowless-service-workers.js | 68 +++++++++++++++++++ .../examples/doc-service-workers.html | 32 +++++++++ .../test/mochitest/examples/service-worker.js | 20 ++++++ 4 files changed, 121 insertions(+) create mode 100644 devtools/client/debugger/test/mochitest/browser_dbg-windowless-service-workers.js create mode 100644 devtools/client/debugger/test/mochitest/examples/doc-service-workers.html create mode 100644 devtools/client/debugger/test/mochitest/examples/service-worker.js diff --git a/devtools/client/debugger/test/mochitest/browser.ini b/devtools/client/debugger/test/mochitest/browser.ini index 0005b19ab135..bbdd21e6c67d 100644 --- a/devtools/client/debugger/test/mochitest/browser.ini +++ b/devtools/client/debugger/test/mochitest/browser.ini @@ -159,6 +159,7 @@ skip-if = os == "win" [browser_dbg-windowless-workers.js] [browser_dbg-windowless-workers-early-breakpoint.js] [browser_dbg-worker-exception.js] +[browser_dbg-windowless-service-workers.js] [browser_dbg-worker-scopes.js] skip-if = (os == 'linux' && debug) || (os == 'linux' && asan) || ccov #Bug 1456013, Bug 1559547 [browser_dbg-event-handler.js] diff --git a/devtools/client/debugger/test/mochitest/browser_dbg-windowless-service-workers.js b/devtools/client/debugger/test/mochitest/browser_dbg-windowless-service-workers.js new file mode 100644 index 000000000000..596a883735fe --- /dev/null +++ b/devtools/client/debugger/test/mochitest/browser_dbg-windowless-service-workers.js @@ -0,0 +1,68 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at . */ + +// Test that we can detect a new service worker and hit breakpoints that we've +// set in it. +add_task(async function() { + await pushPref("devtools.debugger.features.windowless-service-workers", true); + await pushPref("devtools.debugger.workers-visible", true); + await pushPref("dom.serviceWorkers.enabled", true); + await pushPref("dom.serviceWorkers.testing.enabled", true); + + let dbg = await initDebugger("doc-service-workers.html"); + + invokeInTab("registerWorker"); + + await waitForSource(dbg, "service-worker.js"); + const workerSource = findSource(dbg, "service-worker.js"); + + await addBreakpoint(dbg, "service-worker.js", 14); + + invokeInTab("fetchFromWorker"); + + await waitForPaused(dbg); + assertPausedAtSourceAndLine(dbg, workerSource.id, 14); + + await dbg.actions.removeAllBreakpoints(getContext(dbg)); + + await resume(dbg); + + // Leave the worker registration in place for the next task. + + // Set startup breakpoint for the next test. + //await addBreakpoint(dbg, "service-worker.js", 6); +}); + +async function checkWorkerThreads(dbg, count) { + await waitUntil(() => dbg.selectors.getThreads().length == count); + ok(true, `Have ${count} threads`); +} + +// Test that service workers remain after reloading. +add_task(async function() { + const toolbox = await openNewTabAndToolbox(EXAMPLE_URL + "doc-service-workers.html", "jsdebugger"); + const dbg = createDebuggerContext(toolbox); + + await checkWorkerThreads(dbg, 1); + + await reload(dbg); + + await waitForSource(dbg, "service-worker.js"); + const workerSource = findSource(dbg, "service-worker.js"); + + await addBreakpoint(dbg, "service-worker.js", 14); + + invokeInTab("fetchFromWorker"); + + await waitForPaused(dbg); + assertPausedAtSourceAndLine(dbg, workerSource.id, 14); + await checkWorkerThreads(dbg, 1); + + await resume(dbg); + + invokeInTab("unregisterWorker"); + + await dbg.actions.removeAllBreakpoints(getContext(dbg)); + await checkWorkerThreads(dbg, 0); +}); diff --git a/devtools/client/debugger/test/mochitest/examples/doc-service-workers.html b/devtools/client/debugger/test/mochitest/examples/doc-service-workers.html new file mode 100644 index 000000000000..80a02ec3ca18 --- /dev/null +++ b/devtools/client/debugger/test/mochitest/examples/doc-service-workers.html @@ -0,0 +1,32 @@ + diff --git a/devtools/client/debugger/test/mochitest/examples/service-worker.js b/devtools/client/debugger/test/mochitest/examples/service-worker.js new file mode 100644 index 000000000000..09b394654fb8 --- /dev/null +++ b/devtools/client/debugger/test/mochitest/examples/service-worker.js @@ -0,0 +1,20 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +dump("Starting\n"); + +self.addEventListener("activate", event => { + dump("Activate\n"); + event.waitUntil(self.clients.claim()); +}); + +self.addEventListener("fetch", event => { + const url = event.request.url; + dump(`Fetch: ${url}\n`); + if (url.includes("whatever")) { + const response = new Response("Service worker response", { statusText: "OK" }); + event.respondWith(response); + } +});