summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_console_messages_workers.js
blob: 4b10f1d2e426b99c09167b719ce3dc94290b0b93 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the ResourceCommand API around CONSOLE_MESSAGE in workers

const FISSION_TEST_URL = URL_ROOT_SSL + "fission_document_workers.html";
const WORKER_FILE = "test_worker.js";
const IFRAME_FILE = `${URL_ROOT_ORG_SSL}fission_iframe_workers.html`;

add_task(async function () {
  // Set the following pref to false as it's the one that enables direct connection
  // to the worker targets
  await pushPref("dom.worker.console.dispatch_events_to_main_thread", false);

  const tab = await addTab(FISSION_TEST_URL);

  const { client, resourceCommand, targetCommand } = await initResourceCommand(
    tab,
    { listenForWorkers: true }
  );

  info("Wait for the workers (from the main page and the iframe) to be ready");
  const targets = [];
  await new Promise(resolve => {
    const onAvailable = async ({ targetFront }) => {
      targets.push(targetFront);
      if (targets.length === 2) {
        resolve();
      }
    };
    targetCommand.watchTargets({
      types: [targetCommand.TYPES.WORKER],
      onAvailable,
    });
  });

  // The worker logs a message right when it starts, containing its location, so we can
  // assert that we get the logs from the worker spawned in the content page and from the
  // worker spawned in the iframe.
  info("Check that we receive the cached messages");

  const resources = [];
  const onAvailable = innerResources => {
    for (const resource of innerResources) {
      // Ignore resources from non worker targets
      if (!resource.targetFront.isWorkerTarget) {
        continue;
      }

      resources.push(resource);
    }
  };

  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable,
    }
  );

  is(resources.length, 2, "Got the expected number of existing messages");
  const startLogFromWorkerInMainPage = resources.find(
    ({ message }) =>
      message.arguments[1] === `${URL_ROOT_SSL}${WORKER_FILE}#simple-worker`
  );
  const startLogFromWorkerInIframe = resources.find(
    ({ message }) =>
      message.arguments[1] ===
      `${URL_ROOT_ORG_SSL}${WORKER_FILE}#simple-worker-in-iframe`
  );

  checkStartWorkerLogMessage(startLogFromWorkerInMainPage, {
    expectedUrl: `${URL_ROOT_SSL}${WORKER_FILE}#simple-worker`,
    isAlreadyExistingResource: true,
  });
  checkStartWorkerLogMessage(startLogFromWorkerInIframe, {
    expectedUrl: `${URL_ROOT_ORG_SSL}${WORKER_FILE}#simple-worker-in-iframe`,
    isAlreadyExistingResource: true,
  });
  let messageCount = resources.length;

  info(
    "Now log messages *after* the call to ResourceCommand.watchResources and after having received all existing messages"
  );

  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    content.wrappedJSObject.logMessageInWorker("live message from main page");

    const iframe = content.document.querySelector("iframe");
    SpecialPowers.spawn(iframe, [], () => {
      content.wrappedJSObject.logMessageInWorker("live message from iframe");
    });
  });

  // Wait until the 2 new logs are available
  await waitUntil(() => resources.length === messageCount + 2);
  const liveMessageFromWorkerInMainPage = resources.find(
    ({ message }) => message.arguments[1] === "live message from main page"
  );
  const liveMessageFromWorkerInIframe = resources.find(
    ({ message }) => message.arguments[1] === "live message from iframe"
  );

  checkLogInWorkerMessage(
    liveMessageFromWorkerInMainPage,
    "live message from main page"
  );

  checkLogInWorkerMessage(
    liveMessageFromWorkerInIframe,
    "live message from iframe"
  );

  // update the current number of resources received
  messageCount = resources.length;

  info("Now spawn new workers and log messages in main page and iframe");
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [WORKER_FILE],
    async workerUrl => {
      const spawnedWorker = new content.Worker(`${workerUrl}#spawned-worker`);
      spawnedWorker.postMessage({
        type: "log-in-worker",
        message: "live message in spawned worker from main page",
      });

      const iframe = content.document.querySelector("iframe");
      SpecialPowers.spawn(iframe, [workerUrl], async innerWorkerUrl => {
        const spawnedWorkerInIframe = new content.Worker(
          `${innerWorkerUrl}#spawned-worker-in-iframe`
        );
        spawnedWorkerInIframe.postMessage({
          type: "log-in-worker",
          message: "live message in spawned worker from iframe",
        });
      });
    }
  );

  info(
    "Wait until the 4 new logs are available (the ones logged at worker creation + the ones from postMessage"
  );
  await waitUntil(
    () => resources.length === messageCount + 4,
    `Couldn't get the expected number of resources (expected ${
      messageCount + 4
    }, got ${resources.length})`
  );
  const startLogFromSpawnedWorkerInMainPage = resources.find(
    ({ message }) =>
      message.arguments[1] === `${URL_ROOT_SSL}${WORKER_FILE}#spawned-worker`
  );
  const startLogFromSpawnedWorkerInIframe = resources.find(
    ({ message }) =>
      message.arguments[1] ===
      `${URL_ROOT_ORG_SSL}${WORKER_FILE}#spawned-worker-in-iframe`
  );
  const liveMessageFromSpawnedWorkerInMainPage = resources.find(
    ({ message }) =>
      message.arguments[1] === "live message in spawned worker from main page"
  );
  const liveMessageFromSpawnedWorkerInIframe = resources.find(
    ({ message }) =>
      message.arguments[1] === "live message in spawned worker from iframe"
  );

  checkStartWorkerLogMessage(startLogFromSpawnedWorkerInMainPage, {
    expectedUrl: `${URL_ROOT_SSL}${WORKER_FILE}#spawned-worker`,
  });
  checkStartWorkerLogMessage(startLogFromSpawnedWorkerInIframe, {
    expectedUrl: `${URL_ROOT_ORG_SSL}${WORKER_FILE}#spawned-worker-in-iframe`,
  });
  checkLogInWorkerMessage(
    liveMessageFromSpawnedWorkerInMainPage,
    "live message in spawned worker from main page"
  );
  checkLogInWorkerMessage(
    liveMessageFromSpawnedWorkerInIframe,
    "live message in spawned worker from iframe"
  );
  // update the current number of resources received
  messageCount = resources.length;

  info(
    "Add a remote iframe on the same origin we already have an iframe and check we get the messages"
  );
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [IFRAME_FILE],
    async iframeUrl => {
      const iframe = content.document.createElement("iframe");
      iframe.src = `${iframeUrl}?hashSuffix=in-second-iframe`;
      content.document.body.append(iframe);
    }
  );

  info("Wait until the new log is available");
  await waitUntil(
    () => resources.length === messageCount + 1,
    `Couldn't get the expected number of resources (expected ${
      messageCount + 1
    }, got ${resources.length})`
  );
  const startLogFromWorkerInSecondIframe = resources[resources.length - 1];
  checkStartWorkerLogMessage(startLogFromWorkerInSecondIframe, {
    expectedUrl: `${URL_ROOT_ORG_SSL}${WORKER_FILE}#simple-worker-in-second-iframe`,
  });

  targetCommand.destroy();
  await client.close();

  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    // registrationPromise is set by the test page.
    const registration = await content.wrappedJSObject.registrationPromise;
    registration.unregister();
  });
});

function checkStartWorkerLogMessage(
  resource,
  { expectedUrl, isAlreadyExistingResource = false }
) {
  const { message } = resource;
  const [firstArg, secondArg, thirdArg] = message.arguments;
  is(firstArg, "[WORKER] started", "Got the expected first argument");
  is(secondArg, expectedUrl, "expected url was logged");
  is(
    thirdArg?._grip?.class,
    "DedicatedWorkerGlobalScope",
    "the global scope was logged as expected"
  );
  is(
    resource.isAlreadyExistingResource,
    isAlreadyExistingResource,
    "Resource has expected value for isAlreadyExistingResource"
  );
}

function checkLogInWorkerMessage(resource, expectedMessage) {
  const { message } = resource;
  const [firstArg, secondArg, thirdArg] = message.arguments;
  is(firstArg, "[WORKER]", "Got the expected first argument");
  is(secondArg, expectedMessage, "expected message was logged");
  is(
    thirdArg?._grip?.class,
    "MessageEvent",
    "the message event object was logged as expected"
  );
  is(
    resource.isAlreadyExistingResource,
    false,
    "Resource has expected value for isAlreadyExistingResource"
  );
}