summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/browser_aboutdebugging_serviceworker_console.js
blob: 4fd0b727d694edc7dd3f1700686c850261dd9816 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* 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
);

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/devtools/client/debugger/test/mochitest/shared-head.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 various simple debugging operation against service workers debugged through about:debugging.
 */
add_task(async function () {
  await enableServiceWorkerDebugging();

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

  // Open a tab that registers a basic service worker.
  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("Open a toolbox to debug the worker");
  const { devtoolsTab, devtoolsWindow } = await openAboutDevtoolsToolbox(
    document,
    tab,
    window,
    SW_URL
  );

  const toolbox = getToolbox(devtoolsWindow);

  info("Assert the default tools displayed in worker toolboxes");
  const toolTabs = toolbox.doc.querySelectorAll(".devtools-tab");
  const activeTools = [...toolTabs].map(toolTab =>
    toolTab.getAttribute("data-id")
  );

  is(
    activeTools.join(","),
    "webconsole,jsdebugger",
    "Correct set of tools supported by worker"
  );

  const webconsole = await toolbox.selectTool("webconsole");
  const { hud } = webconsole;

  info("Evaluate location in the console");
  await executeAndWaitForMessage(hud, "this.location.toString()", SW_URL);
  ok(true, "Got the location logged in the console");

  info(
    "Evaluate Date and RegExp to ensure their formater also work from worker threads"
  );
  await executeAndWaitForMessage(
    hud,
    "new Date(2013, 3, 1)",
    "Mon Apr 01 2013 00:00:00"
  );
  ok(true, "Date object has expected text content");
  await executeAndWaitForMessage(hud, "new RegExp('.*')", "/.*/");
  ok(true, "RegExp has expected text content");

  await toolbox.selectTool("jsdebugger");
  const dbg = createDebuggerContext(toolbox);
  const {
    selectors: { getIsWaitingOnBreak, getCurrentThread },
  } = dbg;

  info("Wait for next interupt in the worker thread");
  await clickElement(dbg, "pause");
  await waitForState(dbg, () => getIsWaitingOnBreak(getCurrentThread()));

  info("Trigger some code in the worker and wait for pause");
  await SpecialPowers.spawn(swTab.linkedBrowser, [], async function () {
    content.wrappedJSObject.installServiceWorker();
  });
  await waitForPaused(dbg);
  ok(true, "successfully paused");

  info(
    "Evaluate some variable only visible if we execute in the breakpoint frame"
  );
  await executeAndWaitForMessage(hud, "event.data", "install-service-worker");

  info("Resume execution");
  await resume(dbg);

  info("Test pausing from console evaluation");
  hud.ui.wrapper.dispatchEvaluateExpression("debugger; 42");
  await waitForPaused(dbg);
  ok(true, "successfully paused");
  info("Immediately resume");
  await resume(dbg);
  await waitFor(() => findMessagesByType(hud, "42", ".result"));
  ok("The paused console evaluation resumed and logged its magic number");

  info("Destroy the toolbox");
  await closeAboutDevtoolsToolbox(document, devtoolsTab, window);

  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);
});

async function executeAndWaitForMessage(hud, evaluationString, expectedResult) {
  hud.ui.wrapper.dispatchEvaluateExpression();
  await waitFor(() => findMessagesByType(hud, expectedResult, ".result"));
}