summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/target/tests/browser_target_command_various_descriptors.js
blob: 4ee5dd8b2f759549ba11b060a242d9d49d0dc4db (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the TargetCommand API with all possible descriptors

const TEST_URL = "https://example.org/document-builder.sjs?html=org";
const SECOND_TEST_URL = "https://example.com/document-builder.sjs?html=org";
const CHROME_WORKER_URL = CHROME_URL_ROOT + "test_worker.js";

const DESCRIPTOR_TYPES = require("resource://devtools/client/fronts/descriptors/descriptor-types.js");

add_task(async function () {
  // Enabled fission prefs
  await pushPref("devtools.browsertoolbox.scope", "everything");
  // Disable the preloaded process as it gets created lazily and may interfere
  // with process count assertions
  await pushPref("dom.ipc.processPrelaunch.enabled", false);
  // This preference helps destroying the content process when we close the tab
  await pushPref("dom.ipc.keepProcessesAlive.web", 1);

  await testLocalTab();
  await testRemoteTab();
  await testParentProcess();
  await testWorker();
  await testWebExtension();
});

async function testParentProcess() {
  info("Test TargetCommand against parent process descriptor");

  const commands = await CommandsFactory.forMainProcess();
  const { descriptorFront } = commands;

  is(
    descriptorFront.descriptorType,
    DESCRIPTOR_TYPES.PROCESS,
    "The descriptor type is correct"
  );
  is(
    descriptorFront.isParentProcessDescriptor,
    true,
    "Descriptor front isParentProcessDescriptor is correct"
  );
  is(
    descriptorFront.isProcessDescriptor,
    true,
    "Descriptor front isProcessDescriptor is correct"
  );

  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();

  const targets = await targetCommand.getAllTargets(targetCommand.ALL_TYPES);
  Assert.greater(
    targets.length,
    1,
    "We get many targets when debugging the parent process"
  );
  const targetFront = targets[0];
  is(targetFront, targetCommand.targetFront, "The first is the top level one");
  is(
    targetFront.targetType,
    targetCommand.TYPES.FRAME,
    "the parent process target is of frame type, because it inherits from WindowGlobalTargetActor"
  );
  is(targetFront.isTopLevel, true, "This is flagged as top level");

  targetCommand.destroy();
  await waitForAllTargetsToBeAttached(targetCommand);

  await commands.destroy();
}

async function testLocalTab() {
  info("Test TargetCommand against local tab descriptor (via getTab({ tab }))");

  const tab = await addTab(TEST_URL);
  const commands = await CommandsFactory.forTab(tab);
  const { descriptorFront } = commands;
  is(
    descriptorFront.descriptorType,
    DESCRIPTOR_TYPES.TAB,
    "The descriptor type is correct"
  );
  is(
    descriptorFront.isTabDescriptor,
    true,
    "Descriptor front isTabDescriptor is correct"
  );

  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();

  const targets = await targetCommand.getAllTargets(targetCommand.ALL_TYPES);
  is(targets.length, 1, "Got a unique target");
  const targetFront = targets[0];
  is(targetFront, targetCommand.targetFront, "The first is the top level one");
  is(
    targetFront.targetType,
    targetCommand.TYPES.FRAME,
    "the tab target is of frame type"
  );
  is(targetFront.isTopLevel, true, "This is flagged as top level");

  targetCommand.destroy();

  BrowserTestUtils.removeTab(tab);

  await commands.destroy();
}

async function testRemoteTab() {
  info(
    "Test TargetCommand against remote tab descriptor (via getTab({ browserId }))"
  );

  const tab = await addTab(TEST_URL);
  const commands = await CommandsFactory.forRemoteTab(
    tab.linkedBrowser.browserId
  );
  const { descriptorFront } = commands;
  is(
    descriptorFront.descriptorType,
    DESCRIPTOR_TYPES.TAB,
    "The descriptor type is correct"
  );
  is(
    descriptorFront.isTabDescriptor,
    true,
    "Descriptor front isTabDescriptor is correct"
  );

  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();

  const targets = await targetCommand.getAllTargets(targetCommand.ALL_TYPES);
  is(targets.length, 1, "Got a unique target");
  const targetFront = targets[0];
  is(
    targetFront,
    targetCommand.targetFront,
    "TargetCommand top target is the same as the first target"
  );
  is(
    targetFront.targetType,
    targetCommand.TYPES.FRAME,
    "the tab target is of frame type"
  );
  is(targetFront.isTopLevel, true, "This is flagged as top level");

  const browser = tab.linkedBrowser;
  const onLoaded = BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.startLoadingURIString(browser, SECOND_TEST_URL);
  await onLoaded;

  info("Wait for the new target");
  await waitFor(() => targetCommand.targetFront != targetFront);
  isnot(
    targetCommand.targetFront,
    targetFront,
    "The top level target changes on navigation"
  );
  ok(
    !targetCommand.targetFront.isDestroyed(),
    "The new target isn't destroyed"
  );
  ok(targetFront.isDestroyed(), "While the previous target is destroyed");

  targetCommand.destroy();

  BrowserTestUtils.removeTab(tab);

  await commands.destroy();
}

async function testWebExtension() {
  info("Test TargetCommand against webextension descriptor");

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      name: "Sample extension",
    },
  });

  await extension.startup();

  const commands = await CommandsFactory.forAddon(extension.id);
  const { descriptorFront } = commands;
  is(
    descriptorFront.descriptorType,
    DESCRIPTOR_TYPES.EXTENSION,
    "The descriptor type is correct"
  );
  is(
    descriptorFront.isWebExtensionDescriptor,
    true,
    "Descriptor front isWebExtensionDescriptor is correct"
  );

  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();

  const targets = await targetCommand.getAllTargets(targetCommand.ALL_TYPES);
  is(targets.length, 1, "Got a unique target");
  const targetFront = targets[0];
  is(targetFront, targetCommand.targetFront, "The first is the top level one");
  is(
    targetFront.targetType,
    targetCommand.TYPES.FRAME,
    "the web extension target is of frame type, because it inherits from WindowGlobalTargetActor"
  );
  is(targetFront.isTopLevel, true, "This is flagged as top level");

  targetCommand.destroy();

  await extension.unload();

  await commands.destroy();
}

// CommandsFactory expect the worker id, which is computed from the nsIWorkerDebugger.id attribute
function getNextWorkerDebuggerId() {
  return new Promise(resolve => {
    const wdm = Cc[
      "@mozilla.org/dom/workers/workerdebuggermanager;1"
    ].createInstance(Ci.nsIWorkerDebuggerManager);
    const listener = {
      onRegister(dbg) {
        wdm.removeListener(listener);
        resolve(dbg.id);
      },
    };
    wdm.addListener(listener);
  });
}
async function testWorker() {
  info("Test TargetCommand against worker descriptor");

  const workerUrl = CHROME_WORKER_URL + "#descriptor";
  const onNextWorker = getNextWorkerDebuggerId();
  const worker = new Worker(workerUrl);
  const workerId = await onNextWorker;
  ok(workerId, "Found the worker Debugger ID");

  const commands = await CommandsFactory.forWorker(workerId);
  const { descriptorFront } = commands;
  is(
    descriptorFront.descriptorType,
    DESCRIPTOR_TYPES.WORKER,
    "The descriptor type is correct"
  );
  is(
    descriptorFront.isWorkerDescriptor,
    true,
    "Descriptor front isWorkerDescriptor is correct"
  );

  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();

  const targets = await targetCommand.getAllTargets(targetCommand.ALL_TYPES);
  is(targets.length, 1, "Got a unique target");
  const targetFront = targets[0];
  is(targetFront, targetCommand.targetFront, "The first is the top level one");
  is(
    targetFront.targetType,
    targetCommand.TYPES.WORKER,
    "the worker target is of worker type"
  );
  is(targetFront.isTopLevel, true, "This is flagged as top level");

  targetCommand.destroy();

  // Calling CommandsFactory.forWorker, will call RootFront.getWorker
  // which will spawn lots of worker legacy code, firing lots of requests,
  // which may still be pending
  await commands.waitForRequestsToSettle();

  await commands.destroy();
  worker.terminate();
}