summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_breadcrumbs_mutations.js
blob: 91e39769070a546ce193850195e3e1ff6bbb5b88 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that the breadcrumbs widget refreshes correctly when there are markup
// mutations (and that it doesn't refresh when those mutations don't change its
// output).

const TEST_URI = URL_ROOT + "doc_inspector_breadcrumbs.html";

// Each item in the TEST_DATA array is a test case that should contain the
// following properties:
// - desc {String} A description of this test case (will be logged).
// - setup {Function*} A generator function (can yield promises) that sets up
//   the test case. Useful for selecting a node before starting the test.
// - run {Function*} A generator function (can yield promises) that runs the
//   actual test case, i.e, mutates the content DOM to cause the breadcrumbs
//   to refresh, or not.
// - shouldRefresh {Boolean} Once the `run` function has completed, and the test
//   has detected that the page has changed, this boolean instructs the test to
//   verify if the breadcrumbs has refreshed or not.
// - output {Array} A list of strings for the text that should be found in each
//   button after the test has run.
const TEST_DATA = [
  {
    desc: "Adding a child at the end of the chain shouldn't change anything",
    async setup(inspector) {
      await selectNode("#i1111", inspector);
    },
    async run({ walker, selection }) {
      await walker.setInnerHTML(selection.nodeFront, "<b>test</b>");
    },
    shouldRefresh: false,
    output: ["html", "body", "article#i1", "div#i11", "div#i111", "div#i1111"],
  },
  {
    desc: "Updating an ID to an displayed element should refresh",
    setup() {},
    async run({ walker }) {
      const node = await walker.querySelector(walker.rootNode, "#i1");
      await node.modifyAttributes([
        {
          attributeName: "id",
          newValue: "i1-changed",
        },
      ]);
    },
    shouldRefresh: true,
    output: [
      "html",
      "body",
      "article#i1-changed",
      "div#i11",
      "div#i111",
      "div#i1111",
    ],
  },
  {
    desc: "Updating an class to a displayed element should refresh",
    setup() {},
    async run({ walker }) {
      const node = await walker.querySelector(walker.rootNode, "body");
      await node.modifyAttributes([
        {
          attributeName: "class",
          newValue: "test-class",
        },
      ]);
    },
    shouldRefresh: true,
    output: [
      "html",
      "body.test-class",
      "article#i1-changed",
      "div#i11",
      "div#i111",
      "div#i1111",
    ],
  },
  {
    desc:
      "Updating a non id/class attribute to a displayed element should not " +
      "refresh",
    setup() {},
    async run({ walker }) {
      const node = await walker.querySelector(walker.rootNode, "#i11");
      await node.modifyAttributes([
        {
          attributeName: "name",
          newValue: "value",
        },
      ]);
    },
    shouldRefresh: false,
    output: [
      "html",
      "body.test-class",
      "article#i1-changed",
      "div#i11",
      "div#i111",
      "div#i1111",
    ],
  },
  {
    desc: "Moving a child in an element that's not displayed should not refresh",
    setup() {},
    async run({ walker }) {
      // Re-append #i1211 as a last child of #i2.
      const parent = await walker.querySelector(walker.rootNode, "#i2");
      const child = await walker.querySelector(walker.rootNode, "#i211");
      await walker.insertBefore(child, parent);
    },
    shouldRefresh: false,
    output: [
      "html",
      "body.test-class",
      "article#i1-changed",
      "div#i11",
      "div#i111",
      "div#i1111",
    ],
  },
  {
    desc: "Moving an undisplayed child in a displayed element should not refresh",
    setup() {},
    async run({ walker }) {
      // Re-append #i2 in body (move it to the end).
      const parent = await walker.querySelector(walker.rootNode, "body");
      const child = await walker.querySelector(walker.rootNode, "#i2");
      await walker.insertBefore(child, parent);
    },
    shouldRefresh: false,
    output: [
      "html",
      "body.test-class",
      "article#i1-changed",
      "div#i11",
      "div#i111",
      "div#i1111",
    ],
  },
  {
    desc:
      "Updating attributes on an element that's not displayed should not " +
      "refresh",
    setup() {},
    async run({ walker }) {
      const node = await walker.querySelector(walker.rootNode, "#i2");
      await node.modifyAttributes([
        {
          attributeName: "id",
          newValue: "i2-changed",
        },
        {
          attributeName: "class",
          newValue: "test-class",
        },
      ]);
    },
    shouldRefresh: false,
    output: [
      "html",
      "body.test-class",
      "article#i1-changed",
      "div#i11",
      "div#i111",
      "div#i1111",
    ],
  },
  {
    desc: "Removing the currently selected node should refresh",
    async setup(inspector) {
      await selectNode("#i2-changed", inspector);
    },
    async run({ walker, selection }) {
      await walker.removeNode(selection.nodeFront);
    },
    shouldRefresh: true,
    output: ["html", "body.test-class"],
  },
  {
    desc: "Changing the class of the currently selected node should refresh",
    setup() {},
    async run({ selection }) {
      await selection.nodeFront.modifyAttributes([
        {
          attributeName: "class",
          newValue: "test-class-changed",
        },
      ]);
    },
    shouldRefresh: true,
    output: ["html", "body.test-class-changed"],
  },
  {
    desc: "Changing the id of the currently selected node should refresh",
    setup() {},
    async run({ selection }) {
      await selection.nodeFront.modifyAttributes([
        {
          attributeName: "id",
          newValue: "new-id",
        },
      ]);
    },
    shouldRefresh: true,
    output: ["html", "body#new-id.test-class-changed"],
  },
];

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

  for (const { desc, setup, run, shouldRefresh, output } of TEST_DATA) {
    info("Running test case: " + desc);

    info(
      "Listen to markupmutation events from the inspector to know when a " +
        "test case has completed"
    );
    const onContentMutation = inspector.once("markupmutation");

    info("Running setup");
    await setup(inspector);

    info("Listen to mutations on the breadcrumbs container");
    let hasBreadcrumbsMutated = false;
    const observer = new win.MutationObserver(mutations => {
      // Only consider childList changes or tooltiptext/checked attributes
      // changes. The rest may be mutations caused by the overflowing arrowbox.
      for (const { type, attributeName } of mutations) {
        const isChildList = type === "childList";
        const isAttributes =
          type === "attributes" &&
          (attributeName === "checked" || attributeName === "tooltiptext");
        if (isChildList || isAttributes) {
          hasBreadcrumbsMutated = true;
          break;
        }
      }
    });
    observer.observe(container, {
      attributes: true,
      childList: true,
      subtree: true,
    });

    info("Running the test case");
    await run(inspector);

    info("Wait until the page has mutated");
    await onContentMutation;

    if (shouldRefresh) {
      info("The breadcrumbs is expected to refresh, so wait for it");
      await inspector.once("inspector-updated");
    } else {
      ok(
        !inspector._updateProgress,
        "The breadcrumbs widget is not currently updating"
      );
    }

    is(shouldRefresh, hasBreadcrumbsMutated, "Has the breadcrumbs refreshed?");
    observer.disconnect();

    info("Check the output of the breadcrumbs widget");
    is(container.childNodes.length, output.length, "Correct number of buttons");
    for (let i = 0; i < container.childNodes.length; i++) {
      is(
        output[i],
        container.childNodes[i].textContent,
        "Text content for button " + i + " is correct"
      );
    }
  }
});