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

"use strict";

// Test basic features of DevToolsServer

add_task(async function() {
  // When running some other tests before, they may not destroy the main server.
  // Do it manually before running our tests.
  if (DevToolsServer.initialized) {
    DevToolsServer.destroy();
  }

  await testDevToolsServerInitialized();
  await testDevToolsServerKeepAlive();
});

async function testDevToolsServerInitialized() {
  const browser = await addTab("data:text/html;charset=utf-8,foo");
  const tab = gBrowser.getTabForBrowser(browser);

  ok(
    !DevToolsServer.initialized,
    "By default, the DevToolsServer isn't initialized in parent process"
  );
  await assertServerInitialized(
    browser,
    false,
    "By default, the DevToolsServer isn't initialized not in content process"
  );

  const commands = await CommandsFactory.forTab(tab);

  ok(
    DevToolsServer.initialized,
    "Creating the commands will initialize the DevToolsServer in parent process"
  );
  await assertServerInitialized(
    browser,
    false,
    "Creating the commands isn't enough to initialize the DevToolsServer in content process"
  );

  await commands.targetCommand.startListening();

  await assertServerInitialized(
    browser,
    true,
    "Initializing the TargetCommand will initialize the DevToolsServer in content process"
  );

  await commands.destroy();

  // Disconnecting the client will remove all connections from both server, in parent and content process.
  ok(
    !DevToolsServer.initialized,
    "Destroying the commands destroys the DevToolsServer in the parent process"
  );
  await assertServerInitialized(
    browser,
    false,
    "But destroying the commands ends up destroying the DevToolsServer in the content process"
  );

  gBrowser.removeCurrentTab();
  DevToolsServer.destroy();
}

async function testDevToolsServerKeepAlive() {
  const browser = await addTab("data:text/html;charset=utf-8,foo");
  const tab = gBrowser.getTabForBrowser(browser);

  await assertServerInitialized(
    browser,
    false,
    "Server not started in content process"
  );

  const commands = await CommandsFactory.forTab(tab);
  await commands.targetCommand.startListening();

  await assertServerInitialized(
    browser,
    true,
    "Server started in content process"
  );

  info("Set DevToolsServer.keepAlive to true in the content process");
  DevToolsServer.keepAlive = true;
  await setContentServerKeepAlive(browser, true);

  info("Destroy the commands, the content server should be kept alive");
  await commands.destroy();

  await assertServerInitialized(
    browser,
    true,
    "Server still running in content process"
  );

  ok(
    DevToolsServer.initialized,
    "Destroying the commands never destroys the DevToolsServer in the parent process when keepAlive is true"
  );

  info("Set DevToolsServer.keepAlive back to false");
  DevToolsServer.keepAlive = false;
  await setContentServerKeepAlive(browser, false);

  info("Create and destroy a commands again");
  const newCommands = await CommandsFactory.forTab(tab);
  await newCommands.targetCommand.startListening();

  await newCommands.destroy();

  await assertServerInitialized(
    browser,
    false,
    "Server stopped in content process"
  );

  ok(
    !DevToolsServer.initialized,
    "When turning keepAlive to false, the server in the parent process is destroyed"
  );

  gBrowser.removeCurrentTab();
  DevToolsServer.destroy();
}

async function assertServerInitialized(browser, expected, message) {
  const isInitialized = await SpecialPowers.spawn(browser, [], function() {
    const { require } = ChromeUtils.importESModule(
      "resource://devtools/shared/loader/Loader.sys.mjs"
    );
    const {
      DevToolsServer,
    } = require("resource://devtools/server/devtools-server.js");
    return DevToolsServer.initialized;
  });
  is(isInitialized, expected, message);
}

async function setContentServerKeepAlive(browser, keepAlive, message) {
  await SpecialPowers.spawn(browser, [keepAlive], function(_keepAlive) {
    const { require } = ChromeUtils.importESModule(
      "resource://devtools/shared/loader/Loader.sys.mjs"
    );
    const {
      DevToolsServer,
    } = require("resource://devtools/server/devtools-server.js");
    DevToolsServer.keepAlive = _keepAlive;
  });
}