summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/target/tests/browser_target_command_tab_workers.js
blob: 92f5629d4c1dbb4597c0010aec938f8af144e2b5 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the TargetCommand API around workers

const FISSION_TEST_URL = URL_ROOT_SSL + "fission_document.html";
const IFRAME_FILE = "fission_iframe.html";
const REMOTE_IFRAME_URL = URL_ROOT_ORG_SSL + IFRAME_FILE;
const IFRAME_URL = URL_ROOT_SSL + IFRAME_FILE;
const WORKER_FILE = "test_worker.js";
const WORKER_URL = URL_ROOT_SSL + WORKER_FILE;
const REMOTE_IFRAME_WORKER_URL = URL_ROOT_ORG_SSL + WORKER_FILE;

add_task(async function () {
  // Disable the preloaded process as it creates processes intermittently
  // which forces the emission of RDP requests we aren't correctly waiting for.
  await pushPref("dom.ipc.processPrelaunch.enabled", false);

  // The WorkerDebuggerManager#getWorkerDebuggerEnumerator method we're using to retrieve
  // workers loops through _all_ the workers in the process, which means it goes over workers
  // from other tabs as well. Here we add a few tabs that are not going to be used in the
  // test, just to check that their workers won't be retrieved by getAllTargets/watchTargets.
  await addTab(`${FISSION_TEST_URL}?id=first-untargetted-tab&noServiceWorker`);
  await addTab(`${FISSION_TEST_URL}?id=second-untargetted-tab&noServiceWorker`);

  info("Test TargetCommand against workers via a tab target");
  const tab = await addTab(`${FISSION_TEST_URL}?&noServiceWorker`);

  // Create a TargetCommand for the tab
  const commands = await CommandsFactory.forTab(tab);
  const targetCommand = commands.targetCommand;

  // Workaround to allow listening for workers in the content toolbox
  // without the fission preferences
  targetCommand.listenForWorkers = true;

  await commands.targetCommand.startListening();

  const { TYPES } = targetCommand;

  info("Check that getAllTargets only returns dedicated workers");
  const workers = await targetCommand.getAllTargets([
    TYPES.WORKER,
    TYPES.SHARED_WORKER,
  ]);

  // XXX: This should be modified in Bug 1607778, where we plan to add support for shared workers.
  is(workers.length, 2, "Retrieved two worker…");
  const mainPageWorker = workers.find(
    worker => worker.url == `${WORKER_URL}#simple-worker`
  );
  const iframeWorker = workers.find(worker => {
    return worker.url == `${REMOTE_IFRAME_WORKER_URL}#simple-worker-in-iframe`;
  });
  ok(mainPageWorker, "…the dedicated worker on the main page");
  ok(iframeWorker, "…and the dedicated worker on the iframe");

  info(
    "Assert that watchTargets will call the create callback for existing dedicated workers"
  );
  const targets = [];
  const destroyedTargets = [];
  const onAvailable = async ({ targetFront }) => {
    info(`onAvailable called for ${targetFront.url}`);
    is(
      targetFront.targetType,
      TYPES.WORKER,
      "We are only notified about worker targets"
    );
    ok(!targetFront.isTopLevel, "The workers are never top level");
    targets.push(targetFront);
    info(`Handled ${targets.length} targets\n`);
  };
  const onDestroyed = async ({ targetFront }) => {
    is(
      targetFront.targetType,
      TYPES.WORKER,
      "We are only notified about worker targets"
    );
    ok(!targetFront.isTopLevel, "The workers are never top level");
    destroyedTargets.push(targetFront);
  };

  await targetCommand.watchTargets({
    types: [TYPES.WORKER, TYPES.SHARED_WORKER],
    onAvailable,
    onDestroyed,
  });

  // XXX: This should be modified in Bug 1607778, where we plan to add support for shared workers.
  info("Check that watched targets return the same fronts as getAllTargets");
  is(targets.length, 2, "watcheTargets retrieved 2 worker…");
  const mainPageWorkerTarget = targets.find(t => t === mainPageWorker);
  const iframeWorkerTarget = targets.find(t => t === iframeWorker);

  ok(
    mainPageWorkerTarget,
    "…the dedicated worker in main page, which is the same front we received from getAllTargets"
  );
  ok(
    iframeWorkerTarget,
    "…the dedicated worker in iframe, which is the same front we received from getAllTargets"
  );

  info("Spawn workers in main page and iframe");
  await SpecialPowers.spawn(tab.linkedBrowser, [WORKER_FILE], workerUrl => {
    // Put the worker on the global so we can access it later
    content.spawnedWorker = new content.Worker(`${workerUrl}#spawned-worker`);
    const iframe = content.document.querySelector("iframe");
    SpecialPowers.spawn(iframe, [workerUrl], innerWorkerUrl => {
      // Put the worker on the global so we can access it later
      content.spawnedWorker = new content.Worker(
        `${innerWorkerUrl}#spawned-worker-in-iframe`
      );
    });
  });

  await waitFor(
    () => targets.length === 4,
    "Wait for the target list to notify us about the spawned worker"
  );
  const mainPageSpawnedWorkerTarget = targets.find(
    innerTarget => innerTarget.url == `${WORKER_URL}#spawned-worker`
  );
  ok(mainPageSpawnedWorkerTarget, "Retrieved spawned worker");
  const iframeSpawnedWorkerTarget = targets.find(
    innerTarget =>
      innerTarget.url == `${REMOTE_IFRAME_WORKER_URL}#spawned-worker-in-iframe`
  );
  ok(iframeSpawnedWorkerTarget, "Retrieved spawned worker in iframe");

  await wait(100);

  info(
    "Check that the target list calls onDestroy when a worker is terminated"
  );
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    content.spawnedWorker.terminate();
    content.spawnedWorker = null;

    SpecialPowers.spawn(content.document.querySelector("iframe"), [], () => {
      content.spawnedWorker.terminate();
      content.spawnedWorker = null;
    });
  });
  await waitFor(
    () =>
      destroyedTargets.includes(mainPageSpawnedWorkerTarget) &&
      destroyedTargets.includes(iframeSpawnedWorkerTarget),
    "Wait for the target list to notify us about the terminated workers"
  );

  ok(
    true,
    "The target list handled the terminated workers (from the main page and the iframe)"
  );

  info(
    "Check that reloading the page will notify about the terminated worker and the new existing one"
  );
  const targetsCountBeforeReload = targets.length;
  await reloadBrowser();

  await waitFor(() => {
    return (
      destroyedTargets.includes(mainPageWorkerTarget) &&
      destroyedTargets.includes(iframeWorkerTarget)
    );
  }, `Wait for the target list to notify us about the terminated workers when reloading`);
  ok(
    true,
    "The target list notified us about all the expected workers being destroyed when reloading"
  );

  await waitFor(
    () => targets.length === targetsCountBeforeReload + 2,
    "Wait for the target list to notify us about the new workers after reloading"
  );

  const mainPageWorkerTargetAfterReload = targets.find(
    t => t !== mainPageWorkerTarget && t.url == `${WORKER_URL}#simple-worker`
  );
  const iframeWorkerTargetAfterReload = targets.find(
    t =>
      t !== iframeWorkerTarget &&
      t.url == `${REMOTE_IFRAME_WORKER_URL}#simple-worker-in-iframe`
  );

  ok(
    mainPageWorkerTargetAfterReload,
    "The target list handled the worker created once the page navigated"
  );
  ok(
    iframeWorkerTargetAfterReload,
    "The target list handled the worker created in the iframe once the page navigated"
  );

  const targetCount = targets.length;

  info(
    "Check that when removing an iframe we're notified about its workers being terminated"
  );
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    content.document.querySelector("iframe").remove();
  });
  await waitFor(() => {
    return destroyedTargets.includes(iframeWorkerTargetAfterReload);
  }, `Wait for the target list to notify us about the terminated workers when removing an iframe`);

  info("Check that target list handles adding iframes with workers");
  const iframeUrl = `${IFRAME_URL}?noServiceWorker=true&hashSuffix=in-created-iframe`;
  const remoteIframeUrl = `${REMOTE_IFRAME_URL}?noServiceWorker=true&hashSuffix=in-created-remote-iframe`;

  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [iframeUrl, remoteIframeUrl],
    (url, remoteUrl) => {
      const firstIframe = content.document.createElement("iframe");
      content.document.body.append(firstIframe);
      firstIframe.src = url + "-1";

      const secondIframe = content.document.createElement("iframe");
      content.document.body.append(secondIframe);
      secondIframe.src = url + "-2";

      const firstRemoteIframe = content.document.createElement("iframe");
      content.document.body.append(firstRemoteIframe);
      firstRemoteIframe.src = remoteUrl + "-1";

      const secondRemoteIframe = content.document.createElement("iframe");
      content.document.body.append(secondRemoteIframe);
      secondRemoteIframe.src = remoteUrl + "-2";
    }
  );

  // It's important to check the length of `targets` here to ensure we don't get unwanted
  // worker targets.
  await waitFor(
    () => targets.length === targetCount + 4,
    "Wait for the target list to notify us about the workers in the new iframes"
  );
  const firstSpawnedIframeWorkerTarget = targets.find(
    worker => worker.url == `${WORKER_URL}#simple-worker-in-created-iframe-1`
  );
  const secondSpawnedIframeWorkerTarget = targets.find(
    worker => worker.url == `${WORKER_URL}#simple-worker-in-created-iframe-2`
  );
  const firstSpawnedRemoteIframeWorkerTarget = targets.find(
    worker =>
      worker.url ==
      `${REMOTE_IFRAME_WORKER_URL}#simple-worker-in-created-remote-iframe-1`
  );
  const secondSpawnedRemoteIframeWorkerTarget = targets.find(
    worker =>
      worker.url ==
      `${REMOTE_IFRAME_WORKER_URL}#simple-worker-in-created-remote-iframe-2`
  );

  ok(
    firstSpawnedIframeWorkerTarget,
    "The target list handled the worker in the first new same-origin iframe"
  );
  ok(
    secondSpawnedIframeWorkerTarget,
    "The target list handled the worker in the second new same-origin iframe"
  );
  ok(
    firstSpawnedRemoteIframeWorkerTarget,
    "The target list handled the worker in the first new remote iframe"
  );
  ok(
    secondSpawnedRemoteIframeWorkerTarget,
    "The target list handled the worker in the second new remote iframe"
  );

  info("Check that navigating away does destroy all targets");
  BrowserTestUtils.startLoadingURIString(
    tab.linkedBrowser,
    "data:text/html,<meta charset=utf8>Away"
  );

  await waitFor(
    () => destroyedTargets.length === targets.length,
    "Wait for all the targets to be reported as destroyed"
  );

  ok(
    destroyedTargets.includes(mainPageWorkerTargetAfterReload),
    "main page worker target was destroyed"
  );
  ok(
    destroyedTargets.includes(firstSpawnedIframeWorkerTarget),
    "first spawned same-origin iframe worker target was destroyed"
  );
  ok(
    destroyedTargets.includes(secondSpawnedIframeWorkerTarget),
    "second spawned same-origin iframe worker target was destroyed"
  );
  ok(
    destroyedTargets.includes(firstSpawnedRemoteIframeWorkerTarget),
    "first spawned remote iframe worker target was destroyed"
  );
  ok(
    destroyedTargets.includes(secondSpawnedRemoteIframeWorkerTarget),
    "second spawned remote iframe worker target was destroyed"
  );

  targetCommand.unwatchTargets({
    types: [TYPES.WORKER, TYPES.SHARED_WORKER],
    onAvailable,
    onDestroyed,
  });
  targetCommand.destroy();

  info("Unregister service workers so they don't appear in other tests.");
  await unregisterAllServiceWorkers(commands.client);

  BrowserTestUtils.removeTab(tab);
  await commands.destroy();
});