summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_getProcess.js
blob: 30c9fff589a90127515ea2bee7c1d580a282a7f6 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test `RootActor.getProcess` method
 */

"use strict";

add_task(async () => {
  let client, tab;

  function connect() {
    // Fake a first connection to the content process
    const transport = DevToolsServer.connectPipe();
    client = new DevToolsClient(transport);
    return client.connect();
  }

  async function listProcess() {
    const onNewProcess = new Promise(resolve => {
      // Call listProcesses in order to start receiving new process notifications
      client.mainRoot.on("processListChanged", function listener() {
        client.off("processListChanged", listener);
        ok(true, "Received processListChanged event");
        resolve();
      });
    });
    await client.mainRoot.listProcesses();
    await createNewProcess();
    return onNewProcess;
  }

  async function createNewProcess() {
    tab = await BrowserTestUtils.openNewForegroundTab({
      gBrowser,
      url: "data:text/html,new-process",
      forceNewProcess: true,
    });
  }

  async function getProcess() {
    // Note that we can't assert process count as the number of processes
    // is affected by previous tests.
    const processes = await client.mainRoot.listProcesses();
    const { osPid } = tab.linkedBrowser.browsingContext.currentWindowGlobal;
    const descriptor = processes.find(process => process.id == osPid);
    ok(descriptor, "Got the new process descriptor");

    // Connect to the first content process available
    const content = processes.filter(p => !p.isParentProcessDescriptor)[0];

    const processDescriptor = await client.mainRoot.getProcess(content.id);
    const front = await processDescriptor.getTarget();
    const targetForm = front.targetForm;
    ok(targetForm.consoleActor, "Got the console actor");
    ok(targetForm.threadActor, "Got the thread actor");

    // Process target are no longer really used/supported beyond listing their workers
    // from RootFront.
    const { workers } = await front.listWorkers();
    is(workers.length, 0, "listWorkers worked and reported no workers");

    return [front, content.id];
  }

  // Assert that calling client.getProcess against the same process id is
  // returning the same actor.
  async function getProcessAgain(firstTargetFront, id) {
    const processDescriptor = await client.mainRoot.getProcess(id);
    const front = await processDescriptor.getTarget();
    is(
      front,
      firstTargetFront,
      "Second call to getProcess with the same id returns the same form"
    );
  }

  function processScript() {
    /* eslint-env mozilla/process-script */
    const listener = function () {
      Services.obs.removeObserver(listener, "devtools:loader:destroy");
      sendAsyncMessage("test:getProcess-destroy", null);
    };
    Services.obs.addObserver(listener, "devtools:loader:destroy");
  }

  async function closeClient() {
    const onLoaderDestroyed = new Promise(done => {
      const processListener = function () {
        Services.ppmm.removeMessageListener(
          "test:getProcess-destroy",
          processListener
        );
        done();
      };
      Services.ppmm.addMessageListener(
        "test:getProcess-destroy",
        processListener
      );
    });
    const script = `data:,(${encodeURI(processScript)})()`;
    Services.ppmm.loadProcessScript(script, true);
    await client.close();

    await onLoaderDestroyed;
    Services.ppmm.removeDelayedProcessScript(script);
    info("Loader destroyed in the content process");
  }

  // Instantiate a minimal server
  DevToolsServer.init();
  DevToolsServer.allowChromeProcess = true;
  if (!DevToolsServer.createRootActor) {
    DevToolsServer.registerAllActors();
  }

  await connect();
  await listProcess();

  const [front, contentId] = await getProcess();

  await getProcessAgain(front, contentId);

  await closeClient();

  BrowserTestUtils.removeTab(tab);
  DevToolsServer.destroy();
});