summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_breadcrumbs.js
blob: 01d53820ac3ff4db678ccf681937591b787ca816 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that the breadcrumbs widget content is correct.

const TEST_URI = URL_ROOT + "doc_inspector_breadcrumbs.html";
const NODES = [
  {
    selector: "#i1111",
    ids: "i1 i11 i111 i1111",
    nodeName: "div",
    title: "div#i1111",
  },
  { selector: "#i22", ids: "i2 i22", nodeName: "div", title: "div#i22" },
  {
    selector: "#i2111",
    ids: "i2 i21 i211 i2111",
    nodeName: "div",
    title: "div#i2111",
  },
  {
    selector: "#i21",
    ids: "i2 i21 i211 i2111",
    nodeName: "div",
    title: "div#i21",
  },
  {
    selector: "#i22211",
    ids: "i2 i22 i222 i2221 i22211",
    nodeName: "div",
    title: "div#i22211",
  },
  {
    selector: "#i22",
    ids: "i2 i22 i222 i2221 i22211",
    nodeName: "div",
    title: "div#i22",
  },
  { selector: "#i3", ids: "i3", nodeName: "article", title: "article#i3" },
  {
    selector: "clipPath",
    ids: "vector clip",
    nodeName: "clipPath",
    title: "clipPath#clip",
  },
];

add_task(async function () {
  const { inspector } = await openInspectorForURL(TEST_URI);
  const breadcrumbs = inspector.panelDoc.getElementById(
    "inspector-breadcrumbs"
  );
  const container = breadcrumbs.querySelector(".html-arrowscrollbox-inner");

  for (const node of NODES) {
    info("Testing node " + node.selector);

    info("Selecting node and waiting for breadcrumbs to update");
    const breadcrumbsUpdated = inspector.once("breadcrumbs-updated");
    await selectNode(node.selector, inspector);
    await breadcrumbsUpdated;

    info("Performing checks for node " + node.selector);
    const buttonsLabelIds = node.ids.split(" ");

    // html > body > …
    is(
      container.childNodes.length,
      buttonsLabelIds.length + 2,
      "Node " + node.selector + ": Items count"
    );

    for (let i = 2; i < container.childNodes.length; i++) {
      const expectedId = "#" + buttonsLabelIds[i - 2];
      const button = container.childNodes[i];
      const labelId = button.querySelector(".breadcrumbs-widget-item-id");
      is(
        labelId.textContent,
        expectedId,
        "Node " + node.selector + ": button " + i + " matches"
      );
    }

    const checkedButton = container.querySelector("button[checked]");
    const labelId = checkedButton.querySelector(".breadcrumbs-widget-item-id");
    const id = inspector.selection.nodeFront.id;
    is(
      labelId.textContent,
      "#" + id,
      "Node " + node.selector + ": selection matches"
    );

    const labelTag = checkedButton.querySelector(
      ".breadcrumbs-widget-item-tag"
    );
    is(
      labelTag.textContent,
      node.nodeName,
      "Node " + node.selector + " has the expected tag name"
    );

    is(
      checkedButton.getAttribute("title"),
      node.title,
      "Node " + node.selector + " has the expected tooltip"
    );
  }

  await testPseudoElements(inspector, container);
  await testComments(inspector, container);
});

async function testPseudoElements(inspector, container) {
  info("Checking for pseudo elements");

  const pseudoParent = await getNodeFront("#pseudo-container", inspector);
  const children = await inspector.walker.children(pseudoParent);
  is(children.nodes.length, 2, "Pseudo children returned from walker");

  const beforeElement = children.nodes[0];
  let breadcrumbsUpdated = inspector.once("breadcrumbs-updated");
  await selectNode(beforeElement, inspector);
  await breadcrumbsUpdated;
  is(
    container.childNodes[3].textContent,
    "::before",
    "::before shows up in breadcrumb"
  );

  const afterElement = children.nodes[1];
  breadcrumbsUpdated = inspector.once("breadcrumbs-updated");
  await selectNode(afterElement, inspector);
  await breadcrumbsUpdated;
  is(
    container.childNodes[3].textContent,
    "::after",
    "::before shows up in breadcrumb"
  );
}

async function testComments(inspector, container) {
  info("Checking for comment elements");

  const breadcrumbs = inspector.breadcrumbs;
  const checkedButtonIndex = 2;
  const button = container.childNodes[checkedButtonIndex];

  let onBreadcrumbsUpdated = inspector.once("breadcrumbs-updated");
  button.click();
  await onBreadcrumbsUpdated;

  is(breadcrumbs.currentIndex, checkedButtonIndex, "New button is selected");
  ok(
    breadcrumbs.outer.hasAttribute("aria-activedescendant"),
    "Active descendant must be set"
  );

  const comment = [...inspector.markup._containers].find(
    ([node]) => node.nodeType === Node.COMMENT_NODE
  )[0];

  let onInspectorUpdated = inspector.once("inspector-updated");
  inspector.selection.setNodeFront(comment);
  await onInspectorUpdated;

  is(
    breadcrumbs.currentIndex,
    -1,
    "When comment is selected no breadcrumb should be checked"
  );
  ok(
    !breadcrumbs.outer.hasAttribute("aria-activedescendant"),
    "Active descendant must not be set"
  );

  onInspectorUpdated = inspector.once("inspector-updated");
  onBreadcrumbsUpdated = inspector.once("breadcrumbs-updated");
  button.click();
  await Promise.all([onInspectorUpdated, onBreadcrumbsUpdated]);

  is(
    breadcrumbs.currentIndex,
    checkedButtonIndex,
    "Same button is selected again"
  );
  ok(
    breadcrumbs.outer.hasAttribute("aria-activedescendant"),
    "Active descendant must be set again"
  );
}