summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/browser_aboutdebugging_serviceworker_fetch_flag.js
blob: 960635a56dd87582aca8ee3686e08a9c2c03f6f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from helper-serviceworker.js */
Services.scriptloader.loadSubScript(
  CHROME_URL_ROOT + "helper-serviceworker.js",
  this
);

const FETCH_SW_JS = URL_ROOT_SSL + "resources/service-workers/fetch-sw.js";
const FETCH_SW_HTML = URL_ROOT_SSL + "resources/service-workers/fetch-sw.html";

const EMPTY_SW_JS = URL_ROOT_SSL + "resources/service-workers/empty-sw.js";
const EMPTY_SW_HTML = URL_ROOT_SSL + "resources/service-workers/empty-sw.html";

/**
 * Test that the appropriate fetch flag is displayed for service workers.
 */
add_task(async function () {
  await enableServiceWorkerDebugging();
  const { document, tab, window } = await openAboutDebugging({
    enableWorkerUpdates: true,
  });
  await selectThisFirefoxPage(document, window.AboutDebugging.store);

  info("Test fetch status for a service worker listening to fetch events");
  await testServiceWorkerFetchStatus(
    document,
    FETCH_SW_HTML,
    FETCH_SW_JS,
    true
  );

  info("Test fetch status for a service worker not listening to fetch events");
  await testServiceWorkerFetchStatus(
    document,
    EMPTY_SW_HTML,
    EMPTY_SW_JS,
    false
  );

  await removeTab(tab);
});

async function testServiceWorkerFetchStatus(doc, url, workerUrl, isListening) {
  // Open a tab that registers a fetch service worker.
  const swTab = await addTab(url);

  info("Wait until the service worker appears and is running");
  const targetElement = await waitForServiceWorkerRunning(workerUrl, doc);

  const expectedClassName = isListening
    ? ".qa-worker-fetch-listening"
    : ".qa-worker-fetch-not-listening";
  const fetchStatus = targetElement.querySelector(expectedClassName);
  ok(!!fetchStatus, "Found the expected fetch status: " + expectedClassName);

  info("Unregister the service worker");
  await unregisterServiceWorker(swTab);

  info("Wait until the service worker disappears from about:debugging");
  await waitUntil(() => !findDebugTargetByText(workerUrl, doc));

  info("Remove the service worker tab");
  await removeTab(swTab);
}