summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_console_logging_workers_api.js
blob: 560742a6f7169694a01277233eef6214e311fd82 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that the basic console.log() works for workers

"use strict";

const TEST_URI =
  "http://example.com/browser/devtools/client/webconsole/" +
  "test/browser/test-console-workers.html";

add_task(async function () {
  // Allow using SharedArrayBuffer in the test without special HTTP Headers
  await pushPref(
    "dom.postMessage.sharedArrayBuffer.bypassCOOP_COEP.insecure.enabled",
    true
  );

  info("Run the test with worker events dispatched to main thread");
  await pushPref("dom.worker.console.dispatch_events_to_main_thread", true);
  await testWorkerMessage();

  info("Run the test with worker events NOT dispatched to main thread");
  await pushPref("dom.worker.console.dispatch_events_to_main_thread", false);
  await testWorkerMessage(true);
});

async function testWorkerMessage(directConnectionToWorkerThread = false) {
  await addTab(TEST_URI);
  // Open the debugger first as it can cause some message to be duplicated (See Bug 1778852)
  await openDebugger();

  info("Open the console");
  const hud = await openConsole();

  const cachedMessage = await waitFor(() =>
    findConsoleAPIMessage(hud, "initial-message-from-worker")
  );
  is(
    findConsoleAPIMessages(hud, "initial-message-from-worker").length,
    1,
    "We get a single cached message from the worker"
  );

  ok(
    cachedMessage
      .querySelector(".message-body")
      .textContent.includes(`Object { foo: "bar" }`),
    "The simple object is logged as expected"
  );

  if (directConnectionToWorkerThread) {
    const scopeOi = cachedMessage.querySelector(
      ".object-inspector:last-of-type"
    );
    ok(
      scopeOi.textContent.includes(
        `DedicatedWorkerGlobalScope {`,
        `The worker scope is logged as expected: ${scopeOi.textContent}`
      )
    );
  }

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.wrappedJSObject.logFromWorker("live-message");
  });

  const liveMessage = await waitFor(() =>
    findConsoleAPIMessage(hud, "log-from-worker")
  );
  ok(true, "We get the cached message from the worker");

  ok(
    liveMessage
      .querySelector(".message-body")
      .textContent.includes(`live-message`),
    "The message is logged as expected"
  );

  if (directConnectionToWorkerThread) {
    const scopeOi = liveMessage.querySelector(".object-inspector:last-of-type");
    ok(
      scopeOi.textContent.includes(
        `DedicatedWorkerGlobalScope {`,
        `The worker scope is logged as expected: ${scopeOi.textContent}`
      )
    );

    info("Check that Symbol are properly logged");
    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
      content.wrappedJSObject.logFromWorker("live-message");
    });

    const symbolMessage = await waitFor(() =>
      findConsoleAPIMessage(hud, 'Symbol("logged-symbol-from-worker")')
    );
    ok(symbolMessage, "Symbol logged from worker is visible in the console");
  }

  const sabMessage = await waitFor(() =>
    findConsoleAPIMessage(hud, "sab-from-worker")
  );
  ok(sabMessage.textContent.includes("SharedArrayBuffer"));

  info("Check that Arrays are properly logged");
  const arrayMessage = await waitFor(() =>
    findConsoleAPIMessage(hud, '[ "array-item", 42, {…} ]')
  );
  ok(arrayMessage, "Array logged from worker is visible in the console");

  info("Click on the clear button and wait for messages to be removed");
  const onMessagesCacheCleared = hud.ui.once("messages-cache-cleared");
  hud.ui.window.document.querySelector(".devtools-clear-icon").click();
  await waitFor(
    () =>
      !findConsoleAPIMessage(hud, "initial-message-from-worker") &&
      !findConsoleAPIMessage(hud, "log-from-worker")
  );
  await onMessagesCacheCleared;
  ok(true, "Messages were removed");

  info("Close and reopen the console to check messages were cleared properly");
  await closeConsole();
  const toolbox = await openToolboxForTab(gBrowser.selectedTab, "webconsole");
  const newHud = toolbox.getCurrentPanel().hud;

  info(
    "Log a message and wait for it to appear so older messages would have been displayed"
  );
  const onSmokeMessage = waitForMessageByType(newHud, "smoke", ".console-api");
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.wrappedJSObject.console.log("smoke");
  });
  await onSmokeMessage;

  is(
    findConsoleAPIMessage(newHud, "initial-message-from-worker"),
    undefined,
    "Message cache was cleared"
  );
  is(
    findConsoleAPIMessage(newHud, "log-from-worker"),
    undefined,
    "Live message were cleared as well"
  );
  await closeTabAndToolbox();
}