summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_console_evaluation_context_selector.js
blob: 98faa3a92a825d2a46845965480b1b59689031b8 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

"use strict";

add_task(async function () {
  await pushPref("devtools.webconsole.input.context", true);
  await pushPref("devtools.chrome.enabled", true);
  await pushPref("devtools.every-frame-target.enabled", true);
  await pushPref("devtools.browsertoolbox.scope", "everything");

  const hud = await BrowserConsoleManager.toggleBrowserConsole();

  const evaluationContextSelectorButton = await waitFor(() =>
    hud.ui.outputNode.querySelector(".webconsole-evaluation-selector-button")
  );

  ok(
    evaluationContextSelectorButton,
    "The evaluation context selector is visible"
  );
  is(
    evaluationContextSelectorButton.innerText,
    "Top",
    "The button has the expected 'Top' text"
  );
  is(
    evaluationContextSelectorButton.classList.contains("checked"),
    false,
    "The checked class isn't applied"
  );

  await executeAndWaitForResultMessage(
    hud,
    "document.location",
    "chrome://browser/content/browser.xhtml"
  );
  ok(true, "The evaluation was done in the top context");

  setInputValue(hud, "document.location.host");
  await waitForEagerEvaluationResult(hud, `"browser"`);

  info("Check the context selector menu");
  checkContextSelectorMenuItemAt(hud, 0, {
    label: "Top",
    tooltip: "chrome://browser/content/browser.xhtml",
    checked: true,
  });
  checkContextSelectorMenuItemAt(hud, 1, {
    separator: true,
  });

  info(
    "Add a tab with a worker and check both the document and the worker are displayed in the context selector"
  );
  const documentFile = "test-evaluate-worker.html";
  const documentWithWorkerUrl =
    "https://example.com/browser/devtools/client/webconsole/test/browser/" +
    documentFile;
  const tab = await addTab(documentWithWorkerUrl);

  const documentIndex = await waitFor(() => {
    const i = getContextSelectorItems(hud).findIndex(el =>
      el.querySelector(".label")?.innerText?.endsWith(documentFile)
    );
    return i == -1 ? null : i;
  });

  info("Select the document target");
  selectTargetInContextSelector(hud, documentWithWorkerUrl);

  await waitFor(() =>
    evaluationContextSelectorButton.innerText.includes(documentFile)
  );
  ok(true, "The context was set to the selected document");
  is(
    evaluationContextSelectorButton.classList.contains("checked"),
    true,
    "The checked class is applied"
  );

  checkContextSelectorMenuItemAt(hud, documentIndex, {
    label: documentWithWorkerUrl,
    tooltip: documentWithWorkerUrl,
    checked: true,
  });

  await waitForEagerEvaluationResult(hud, `"example.com"`);
  ok(true, "The instant evaluation result is updated in the document context");

  await executeAndWaitForResultMessage(
    hud,
    "document.location",
    documentWithWorkerUrl
  );
  ok(true, "The evaluation is done in the document context");

  info("Check that autocomplete is done in the tab document context");
  await setInputValueForAutocompletion(hud, "p");
  // `pauseInWorker` is defined in test-evaluate-worker.html
  ok(
    getAutocompletePopupLabels(hud.jsterm.autocompletePopup).includes(
      "pauseInWorker"
    ),
    "autocomplete happened in the tab document context"
  );

  // set input text so we can watch for instant evaluation result update
  setInputValue(hud, "globalThis.location.href");
  await waitForEagerEvaluationResult(hud, `"${documentWithWorkerUrl}"`);

  info("Select the worker target");
  const workerFile = "test-evaluate-worker.js";
  const workerUrl =
    "https://example.com/browser/devtools/client/webconsole/test/browser/" +
    workerFile;
  // Wait for the worker target to be displayed
  await waitFor(() =>
    getContextSelectorItems(hud).find(el =>
      el.querySelector(".label")?.innerText?.endsWith(workerFile)
    )
  );
  selectTargetInContextSelector(hud, workerFile);

  await waitFor(() =>
    evaluationContextSelectorButton.innerText.includes(workerFile)
  );
  ok(true, "The context was set to the selected worker");

  await waitForEagerEvaluationResult(hud, `"${workerUrl}"`);
  ok(true, "The instant evaluation result is updated in the worker context");

  const workerIndex = await waitFor(() => {
    const i = getContextSelectorItems(hud).findIndex(el =>
      el.querySelector(".label")?.innerText?.endsWith(workerFile)
    );
    return i == -1 ? null : i;
  });
  checkContextSelectorMenuItemAt(hud, workerIndex, {
    label: workerFile,
    tooltip: workerFile,
    checked: true,
  });

  await executeAndWaitForResultMessage(
    hud,
    "globalThis.location",
    `WorkerLocation`
  );
  ok(true, "The evaluation is done in the worker context");

  info("Check that autocomplete is done in the worker context");
  await setInputValueForAutocompletion(hud, "f");
  // `foo` is defined in test-evaluate-worker.js
  ok(
    getAutocompletePopupLabels(hud.jsterm.autocompletePopup).includes("foo"),
    "autocomplete happened in the worker context"
  );

  // set input text so we can watch for instant evaluation result update
  setInputValue(hud, "document.location.host");
  await waitForEagerEvaluationResult(
    hud,
    `ReferenceError: document is not defined`
  );

  info(
    "Remove the tab and make sure both the document and worker target are removed from the context selector"
  );
  await removeTab(tab);

  await waitFor(() => evaluationContextSelectorButton.innerText == "Top");
  ok(true, "The context is set back to Top");

  checkContextSelectorMenuItemAt(hud, 0, {
    label: "Top",
    tooltip: "chrome://browser/content/browser.xhtml",
    checked: true,
  });

  is(
    getContextSelectorItems(hud).every(el => {
      const label = el.querySelector(".label")?.innerText;
      return (
        !label ||
        (label !== "test-evaluate-worker.html" && label !== workerFile)
      );
    }),
    true,
    "the document and worker targets were removed"
  );

  await waitForEagerEvaluationResult(hud, `"browser"`);
  ok(true, "The instant evaluation was done in the top context");

  await executeAndWaitForResultMessage(
    hud,
    "document.location",
    "chrome://browser/content/browser.xhtml"
  );
  ok(true, "The evaluation was done in the top context");
});