summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/browser_aboutdebugging_serviceworker_status.js
blob: 50426dcb47207deb1bacb2ec8fb2a6207aab9076 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* 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 SW_TAB_URL =
  URL_ROOT_SSL + "resources/service-workers/controlled-sw.html";
const SW_URL = URL_ROOT_SSL + "resources/service-workers/controlled-sw.js";

/**
 * Test that the service worker has the status "registering" when the service worker is
 * not installed yet. Other states (stopped, running) are covered by the existing tests.
 */
add_task(async function () {
  await enableServiceWorkerDebugging();

  const { document, tab, window } = await openAboutDebugging({
    enableWorkerUpdates: true,
  });
  await selectThisFirefoxPage(document, window.AboutDebugging.store);

  info("Open tab with a service worker that never leaves `registering` status");
  const swTab = await addTab(SW_TAB_URL);

  // Wait for the registration to make sure service worker has been started, and that we
  // are not just reading STOPPED as the initial state.
  await waitForRegistration(swTab);

  info("Wait until the service worker is in registering status");
  await waitForServiceWorkerRegistering(SW_URL, document);

  // Check that the buttons are displayed as expected.
  checkButtons(
    { inspect: true, push: false, start: false, unregister: false },
    SW_URL,
    document
  );

  info("Install the service worker");
  SpecialPowers.spawn(swTab.linkedBrowser, [], () =>
    content.wrappedJSObject.installServiceWorker()
  );

  info("Wait until the service worker is running");
  await waitForServiceWorkerRunning(SW_URL, document);

  checkButtons(
    { inspect: true, push: true, start: false, unregister: true },
    SW_URL,
    document
  );

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

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

  info("Remove tabs");
  await removeTab(swTab);
  await removeTab(tab);
});

function checkButtons(
  { inspect, push, start, unregister },
  workerText,
  document
) {
  const targetElement = findDebugTargetByText(SW_URL, document);

  const inspectButton = targetElement.querySelector(
    ".qa-debug-target-inspect-button"
  );
  const pushButton = targetElement.querySelector(".qa-push-button");
  const startButton = targetElement.querySelector(".qa-start-button");
  const unregisterButton = targetElement.querySelector(".qa-unregister-button");

  is(
    !!inspectButton,
    inspect,
    "Inspect button should be " + (inspect ? "visible" : "hidden")
  );
  is(
    !!pushButton,
    push,
    "Push button should be " + (push ? "visible" : "hidden")
  );
  is(
    !!startButton,
    start,
    "Start button should be " + (start ? "visible" : "hidden")
  );
  is(
    !!unregisterButton,
    unregister,
    "Unregister button should be " + (unregister ? "visible" : "hidden")
  );
}