summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_evaluation_context_selector_iframe_picker.js
blob: 215495ee5397321aa20ebd7bb2bc4c2cff738827 (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
/* 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 http://mozilla.org/MPL/2.0/. */

"use strict";

// Test that the evaluation context selector reacts as expected when using the Toolbox
// iframe picker.

const TEST_URI = `https://example.com/document-builder.sjs?html=${encodeURIComponent(`
  <html>
    <h1>example.com</h1>
    <iframe src="https://example.org/document-builder.sjs?html=example.org"></iframe>
    <iframe src="https://example.net/document-builder.sjs?html=example.net"></iframe>
  </html>
`)}`;

add_task(async function () {
  // Enable the context selector and the frames button.
  await pushPref("devtools.webconsole.input.context", true);
  await pushPref("devtools.command-button-frames.enabled", true);

  const hud = await openNewTabAndConsole(TEST_URI);

  info("Wait until the iframe picker button is displayed");
  try {
    await waitFor(() => getFramesButton(hud.toolbox));
    ok(
      !isFissionEnabled() || isEveryFrameTargetEnabled(),
      "iframe picker should only display remote frames when EFT is enabled"
    );
  } catch (e) {
    if (isFissionEnabled() && !isEveryFrameTargetEnabled()) {
      ok(true, "iframe picker displays remote frames only when EFT is enabled");
      return;
    }
    throw e;
  }

  const evaluationContextSelectorButton = hud.ui.outputNode.querySelector(
    ".webconsole-evaluation-selector-button"
  );
  await executeAndWaitForResultMessage(
    hud,
    "document.location.host",
    `"example.com"`
  );
  ok(true, "The expression was evaluated in the example.com document.");

  info("Select the example.org iframe");
  selectFrameInIframePicker(hud.toolbox, "https://example.org");
  try {
    await waitFor(() =>
      evaluationContextSelectorButton.innerText.includes("example.org")
    );
    if (!isEveryFrameTargetEnabled()) {
      todo(
        true,
        "context selector should only reacts to iframe picker when EFT is enabled"
      );
      return;
    }
  } catch (e) {
    if (!isEveryFrameTargetEnabled()) {
      todo(
        false,
        "context selector only reacts to iframe picker when EFT is enabled"
      );
      return;
    }
    throw e;
  }
  ok(true, "The context was set to the example.org document");

  await executeAndWaitForResultMessage(
    hud,
    "document.location.host",
    `"example.org"`
  );
  ok(true, "The expression was evaluated in the example.org document.");

  info("Select the example.net iframe");
  selectFrameInIframePicker(hud.toolbox, "https://example.net");
  await waitFor(() =>
    evaluationContextSelectorButton.innerText.includes("example.net")
  );
  ok(true, "The context was set to the example.net document");

  await executeAndWaitForResultMessage(
    hud,
    "document.location.host",
    `"example.net"`
  );
  ok(true, "The expression was evaluated in the example.net document.");

  info("Select the Top frame");
  selectFrameInIframePicker(hud.toolbox, "https://example.com");
  await waitFor(() =>
    evaluationContextSelectorButton.innerText.includes("Top")
  );
  ok(true, "The context was set to the example.com document");

  await executeAndWaitForResultMessage(
    hud,
    "document.location.host",
    `"example.com"`
  );
  ok(true, "The expression was evaluated in the example.com document.");
});

function getFramesButton(toolbox) {
  return toolbox.doc.getElementById("command-button-frames");
}

function selectFrameInIframePicker(toolbox, host) {
  const commandItem = Array.from(
    toolbox.doc.querySelectorAll("#toolbox-frame-menu .command .label")
  ).find(label => label.textContent.startsWith(host));
  if (!commandItem) {
    throw new Error(`Couldn't find any frame starting with "${host}"`);
  }

  commandItem.click();
}