summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_inspect_panels.js
blob: 2b7d92f9c5b2fc392e31910f85b9d55d37f94c93 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Check that the inspect() jsterm helper function works.

"use strict";

const TEST_URI =
  "https://example.com/browser/devtools/client/webconsole/test/browser/" +
  "test-simple-function.html";

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  await testInspectingElement(hud);
  await testInspectingFunction(hud);
});

async function testInspectingElement(hud) {
  info("Test `inspect(el)`");
  execute(hud, "inspect(document.querySelector('p'))");
  await waitForSelectedElementInInspector(hud.toolbox, "p");
  ok(true, "inspected element is now selected in the inspector");

  info(
    "Test that inspect selects the node in the inspector in the split console as well"
  );
  const onSplitConsoleReady = hud.toolbox.once("split-console");
  EventUtils.sendKey("ESCAPE", hud.toolbox.win);
  await onSplitConsoleReady;

  execute(hud, "inspect(document.querySelector('body'))");
  await waitForSelectedElementInInspector(hud.toolbox, "body");
  ok(true, "the inspected element is selected in the inspector");
}

async function testInspectingFunction(hud) {
  info("Test `inspect(test)`");
  execute(hud, "inspect(test)");
  await waitFor(expectedSourceSelected("test-simple-function.js", 3));
  ok(true, "inspected function is now selected in the debugger");

  info("Test `inspect(test_mangled)`");
  execute(hud, "inspect(test_mangled)");
  await waitFor(expectedSourceSelected("test-mangled-function.js", 3, true));
  ok(true, "inspected source-mapped function is now selected in the debugger");

  info("Test `inspect(test_bound)`");
  execute(hud, "inspect(test_bound)");
  await waitFor(expectedSourceSelected("test-simple-function.js", 7));
  ok(true, "inspected bound target function is now selected in the debugger");

  function expectedSourceSelected(
    sourceFilename,
    sourceLine,
    isOriginalSource
  ) {
    return () => {
      const dbg = hud.toolbox.getPanel("jsdebugger");
      if (!dbg) {
        return false;
      }

      const selectedLocation = dbg._selectors.getSelectedLocation(
        dbg._getState()
      );

      if (!selectedLocation) {
        return false;
      }

      if (
        isOriginalSource &&
        !selectedLocation.source.id.includes("/originalSource-")
      ) {
        return false;
      }

      return (
        selectedLocation.source.id.includes(sourceFilename) &&
        selectedLocation.line == sourceLine
      );
    };
  }
}

async function waitForSelectedElementInInspector(toolbox, displayName) {
  return waitFor(() => {
    const inspector = toolbox.getPanel("inspector");
    if (!inspector) {
      return false;
    }

    const selection = inspector.selection;
    return (
      selection &&
      selection.nodeFront &&
      selection.nodeFront.displayName == displayName
    );
  });
}