summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_inspector-mutations-childlist.html
blob: d845b987e86255b612530530a7d58985bd5d2c96 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug </title>

  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript;version=1.8" src="inspector-helpers.js"></script>
  <script type="application/javascript;version=1.8">
const inspector = require("devtools/shared/fronts/inspector");

window.onload = function() {
  SimpleTest.waitForExplicitFinish();
  runNextTest();
}

var gInspectee = null;
var gWalker = null;
var gClient = null;
var gCleanupConnection = null;

function setup(callback) {
  let url = document.getElementById("inspectorContent").href;
  gCleanupConnection = attachURL(url, function(err, client, tab, doc) {
    gInspectee = doc;
    let {InspectorFront} = require("devtools/shared/fronts/inspector");
    let inspector = InspectorFront(client, tab);
    promiseDone(inspector.getWalker().then(walker => {
      gClient = client;
      gWalker = walker;
    }).then(callback));
  });
}

function teardown() {
  gWalker = null;
  gClient = null;
  gInspectee = null;
  if (gCleanupConnection) {
    gCleanupConnection();
    gCleanupConnection = null;
  }
}

function assertOwnership() {
  let num = assertOwnershipTrees(gWalker);
}

function setParent(nodeSelector, newParentSelector) {
  let node = gInspectee.querySelector(nodeSelector);
  if (newParentSelector) {
    let newParent = gInspectee.querySelector(newParentSelector);
    newParent.appendChild(node);
  } else {
    node.parentNode.removeChild(node);
  }
}

function loadSelector(selector) {
  return gWalker.querySelectorAll(gWalker.rootNode, selector).then(nodeList => {
    return nodeList.items();
  });
}

function loadSelectors(selectors) {
  return promise.all(Array.from(selectors, (sel) => loadSelector(sel)));
}

function doMoves(moves) {
  for (let move of moves) {
    setParent(move[0], move[1]);
  }
}

/**
 * Test a set of tree rearrangements and make sure they cause the expected changes.
 */

var gDummySerial = 0;

function mutationTest(testSpec) {
  return function() {
    setup(() => {
      promiseDone(loadSelectors(testSpec.load || ["html"]).then(() => {
        gWalker.autoCleanup = !!testSpec.autoCleanup;
        if (testSpec.preCheck) {
          testSpec.preCheck();
        }
        doMoves(testSpec.moves || []);

        // Some of these moves will trigger no mutation events,
        // so do a dummy change to the root node to trigger
        // a mutation event anyway.
        gInspectee.documentElement.setAttribute("data-dummy", gDummySerial++);

        gWalker.once("mutations", (mutations) => {
          // Filter out our dummy mutation.
          mutations = mutations.filter(change => {
            if (change.type == "attributes" &&
                change.attributeName == "data-dummy") {
              return false;
            }
            return true;
          });
          assertOwnership();
          if (testSpec.postCheck) {
            testSpec.postCheck(mutations);
          }
          teardown();
          runNextTest();
        });
      }));
    })
  }
}

// Verify that our dummy mutation works.
addTest(mutationTest({
  autoCleanup: false,
  postCheck: function(mutations) {
    is(mutations.length, 0, "Dummy mutation is filtered out.");
  }
}));

// Test a simple move to a different location in the sibling list for the same
// parent.
addTest(mutationTest({
  autoCleanup: false,
  load: ["#longlist div"],
  moves: [
    ["#a", "#longlist"]
  ],
  postCheck: function(mutations) {
    let remove = mutations[0];
    is(remove.type, "childList", "First mutation should be a childList.")
    ok(remove.removed.length > 0, "First mutation should be a removal.")
    let add = mutations[1];
    is(add.type, "childList", "Second mutation should be a childList removal.")
    ok(add.added.length > 0, "Second mutation should be an addition.")
    let a = add.added[0];
    is(a.id, "a", "Added node should be #a");
    is(a.parentNode(), remove.target, "Should still be a child of longlist.");
    is(remove.target, add.target, "First and second mutations should be against the same node.");
  }
}));

// Test a move to another location that is within our ownership tree.
addTest(mutationTest({
  autoCleanup: false,
  load: ["#longlist div", "#longlist-sibling"],
  moves: [
    ["#a", "#longlist-sibling"]
  ],
  postCheck: function(mutations) {
    let remove = mutations[0];
    is(remove.type, "childList", "First mutation should be a childList.")
    ok(remove.removed.length > 0, "First mutation should be a removal.")
    let add = mutations[1];
    is(add.type, "childList", "Second mutation should be a childList removal.")
    ok(add.added.length > 0, "Second mutation should be an addition.")
    let a = add.added[0];
    is(a.id, "a", "Added node should be #a");
    is(a.parentNode(), add.target, "Should still be a child of longlist.");
    is(add.target.id, "longlist-sibling", "long-sibling should be the target.");
  }
}));

// Move an unseen node with a seen parent into our ownership tree - should generate a
// childList pair with no adds or removes.
addTest(mutationTest({
  autoCleanup: false,
  load: ["#longlist"],
  moves: [
    ["#longlist-sibling", "#longlist"]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 2, "Should generate two mutations");
    is(mutations[0].type, "childList", "Should be childList mutations.");
    is(mutations[0].added.length, 0, "Should have no adds.");
    is(mutations[0].removed.length, 0, "Should have no removes.");
    is(mutations[1].type, "childList", "Should be childList mutations.");
    is(mutations[1].added.length, 0, "Should have no adds.");
    is(mutations[1].removed.length, 0, "Should have no removes.");
  }
}));

// Move an unseen node with an unseen parent into our ownership tree.  Should only
// generate one childList mutation with no adds or removes.
addTest(mutationTest({
  autoCleanup: false,
  load: ["#longlist div"],
  moves: [
    ["#longlist-sibling-firstchild", "#longlist"]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 1, "Should generate two mutations");
    is(mutations[0].type, "childList", "Should be childList mutations.");
    is(mutations[0].added.length, 0, "Should have no adds.");
    is(mutations[0].removed.length, 0, "Should have no removes.");
  }
}));

// Move a node between unseen nodes, should generate no mutations.
addTest(mutationTest({
  autoCleanup: false,
  load: ["html"],
  moves: [
    ["#longlist-sibling", "#longlist"]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 0, "Should generate no mutations.");
  }
}));

// Orphan a node and don't clean it up
addTest(mutationTest({
  autoCleanup: false,
  load: ["#longlist div"],
  moves: [
    ["#longlist", null]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 1, "Should generate one mutation.");
    let change = mutations[0];
    is(change.type, "childList", "Should be a childList.");
    is(change.removed.length, 1, "Should have removed a child.");
    let ownership = clientOwnershipTree(gWalker);
    is(ownership.orphaned.length, 1, "Should have one orphaned subtree.");
    is(ownershipTreeSize(ownership.orphaned[0]), 1 + 26 + 26, "Should have orphaned longlist, and 26 children, and 26 singleTextChilds");
  }
}));

// Orphan a node, and do clean it up.
addTest(mutationTest({
  autoCleanup: true,
  load: ["#longlist div"],
  moves: [
    ["#longlist", null]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 1, "Should generate one mutation.");
    let change = mutations[0];
    is(change.type, "childList", "Should be a childList.");
    is(change.removed.length, 1, "Should have removed a child.");
    let ownership = clientOwnershipTree(gWalker);
    is(ownership.orphaned.length, 0, "Should have no orphaned subtrees.");
  }
}));

// Orphan a node by moving it into the tree but out of our visible subtree.
addTest(mutationTest({
  autoCleanup: false,
  load: ["#longlist div"],
  moves: [
    ["#longlist", "#longlist-sibling"]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 1, "Should generate one mutation.");
    let change = mutations[0];
    is(change.type, "childList", "Should be a childList.");
    is(change.removed.length, 1, "Should have removed a child.");
    let ownership = clientOwnershipTree(gWalker);
    is(ownership.orphaned.length, 1, "Should have one orphaned subtree.");
    is(ownershipTreeSize(ownership.orphaned[0]), 1 + 26 + 26, "Should have orphaned longlist, 26 children, and 26 singleTextChilds.");
  }
}));

// Orphan a node by moving it into the tree but out of our visible subtree, and clean it up.
addTest(mutationTest({
  autoCleanup: true,
  load: ["#longlist div"],
  moves: [
    ["#longlist", "#longlist-sibling"]
  ],
  postCheck: function(mutations) {
    is(mutations.length, 1, "Should generate one mutation.");
    let change = mutations[0];
    is(change.type, "childList", "Should be a childList.");
    is(change.removed.length, 1, "Should have removed a child.");
    let ownership = clientOwnershipTree(gWalker);
    is(ownership.orphaned.length, 0, "Should have no orphaned subtrees.");
  }
}));


addTest(function cleanup() {
  delete gInspectee;
  delete gWalker;
  delete gClient;
  runNextTest();
});


  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
<a id="inspectorContent" target="_blank" href="inspector-traversal-data.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>