summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_treeWidget_basic.js
blob: 48702b9b8dd8f9fc38dbfcd8b273299d162b54a3 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that the tree widget api works fine
"use strict";

const TEST_URI =
  "data:text/html;charset=utf-8,<head>" +
  "<link rel='stylesheet' type='text/css' href='chrome://devtools/skin/widg" +
  "ets.css'></head><body><div></div><span></span></body>";
const {
  TreeWidget,
} = require("resource://devtools/client/shared/widgets/TreeWidget.js");

add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["security.allow_unsafe_parent_loads", true]],
  });
  await addTab("about:blank");
  const { host, doc } = await createHost("bottom", TEST_URI);

  const tree = new TreeWidget(doc.querySelector("div"), {
    defaultType: "store",
  });

  populateTree(tree, doc);
  testTreeItemInsertedCorrectly(tree, doc);
  testAPI(tree, doc);
  populateUnsortedTree(tree, doc);
  testUnsortedTreeItemInsertedCorrectly(tree, doc);

  tree.destroy();
  host.destroy();
  gBrowser.removeCurrentTab();
});

function populateTree(tree, doc) {
  tree.add([
    {
      id: "level1",
      label: "Level 1",
    },
    {
      id: "level2-1",
      label: "Level 2",
    },
    {
      id: "level3-1",
      label: "Level 3 - Child 1",
      type: "dir",
    },
  ]);
  tree.add([
    "level1",
    "level2-1",
    {
      id: "level3-2",
      label: "Level 3 - Child 2",
    },
  ]);
  tree.add([
    "level1",
    "level2-1",
    {
      id: "level3-3",
      label: "Level 3 - Child 3",
    },
  ]);
  tree.add([
    "level1",
    {
      id: "level2-2",
      label: "Level 2.1",
    },
    {
      id: "level3-1",
      label: "Level 3.1",
    },
  ]);
  tree.add([
    {
      id: "level1",
      label: "Level 1",
    },
    {
      id: "level2",
      label: "Level 2",
    },
    {
      id: "level3",
      label: "Level 3",
      type: "js",
    },
  ]);
  tree.add(["level1.1", "level2", { id: "level3", type: "url" }]);
}

/**
 * Test if the nodes are inserted correctly in the tree.
 */
function testTreeItemInsertedCorrectly(tree, doc) {
  is(
    tree.root.children.children.length,
    2,
    "Number of top level elements match"
  );
  is(
    tree.root.children.firstChild.lastChild.children.length,
    3,
    "Number of first second level elements match"
  );
  is(
    tree.root.children.lastChild.lastChild.children.length,
    1,
    "Number of second second level elements match"
  );

  ok(tree.root.items.has("level1"), "Level1 top level element exists");
  is(
    tree.root.children.firstChild.dataset.id,
    JSON.stringify(["level1"]),
    "Data id of first top level element matches"
  );
  is(
    tree.root.children.firstChild.firstChild.textContent,
    "Level 1",
    "Text content of first top level element matches"
  );

  ok(tree.root.items.has("level1.1"), "Level1.1 top level element exists");
  is(
    tree.root.children.firstChild.nextSibling.dataset.id,
    JSON.stringify(["level1.1"]),
    "Data id of second top level element matches"
  );
  is(
    tree.root.children.firstChild.nextSibling.firstChild.textContent,
    "level1.1",
    "Text content of second top level element matches"
  );

  // Adding a new non text item in the tree.
  const node = doc.createElement("div");
  node.textContent = "Foo Bar";
  node.className = "foo bar";
  tree.add([
    {
      id: "level1.2",
      node,
      attachment: {
        foo: "bar",
      },
    },
  ]);

  is(
    tree.root.children.children.length,
    3,
    "Number of top level elements match after update"
  );
  ok(tree.root.items.has("level1.2"), "New level node got added");
  ok(
    tree.attachments.has(JSON.stringify(["level1.2"])),
    "Attachment is present for newly added node"
  );
  // The item should be added before level1 and level 1.1 as lexical sorting
  is(
    tree.root.children.firstChild.dataset.id,
    JSON.stringify(["level1.2"]),
    "Data id of last top level element matches"
  );
  is(
    tree.root.children.firstChild.firstChild.firstChild,
    node,
    "Newly added node is inserted at the right location"
  );
}

/**
 * Populate the unsorted tree.
 */
function populateUnsortedTree(tree, doc) {
  tree.sorted = false;

  tree.add([{ id: "g-1", label: "g-1" }]);
  tree.add(["g-1", { id: "d-2", label: "d-2.1" }]);
  tree.add(["g-1", { id: "b-2", label: "b-2.2" }]);
  tree.add(["g-1", { id: "a-2", label: "a-2.3" }]);
}

/**
 * Test if the nodes are inserted correctly in the unsorted tree.
 */
function testUnsortedTreeItemInsertedCorrectly(tree, doc) {
  ok(tree.root.items.has("g-1"), "g-1 top level element exists");

  is(
    tree.root.children.firstChild.lastChild.children.length,
    3,
    "Number of children for g-1 matches"
  );
  is(
    tree.root.children.firstChild.dataset.id,
    JSON.stringify(["g-1"]),
    "Data id of g-1 matches"
  );
  is(
    tree.root.children.firstChild.firstChild.textContent,
    "g-1",
    "Text content of g-1 matches"
  );
  is(
    tree.root.children.firstChild.lastChild.firstChild.dataset.id,
    JSON.stringify(["g-1", "d-2"]),
    "Data id of d-2 matches"
  );
  is(
    tree.root.children.firstChild.lastChild.firstChild.textContent,
    "d-2.1",
    "Text content of d-2 matches"
  );
  is(
    tree.root.children.firstChild.lastChild.firstChild.nextSibling.textContent,
    "b-2.2",
    "Text content of b-2 matches"
  );
  is(
    tree.root.children.firstChild.lastChild.lastChild.textContent,
    "a-2.3",
    "Text content of a-2 matches"
  );
}

/**
 * Tests if the API exposed by TreeWidget works properly
 */
function testAPI(tree, doc) {
  info("Testing TreeWidget API");
  // Check if selectItem and selectedItem setter works as expected
  // Nothing should be selected beforehand
  ok(!doc.querySelector(".theme-selected"), "Nothing is selected");
  tree.selectItem(["level1"]);
  const node = doc.querySelector(".theme-selected");
  ok(!!node, "Something got selected");
  is(
    node.parentNode.dataset.id,
    JSON.stringify(["level1"]),
    "Correct node selected"
  );

  tree.selectItem(["level1", "level2"]);
  const node2 = doc.querySelector(".theme-selected");
  ok(!!node2, "Something is still selected");
  isnot(node, node2, "Newly selected node is different from previous");
  is(
    node2.parentNode.dataset.id,
    JSON.stringify(["level1", "level2"]),
    "Correct node selected"
  );

  // test if selectedItem getter works
  is(tree.selectedItem.length, 2, "Correct length of selected item");
  is(tree.selectedItem[0], "level1", "Correct selected item");
  is(tree.selectedItem[1], "level2", "Correct selected item");

  // test if isSelected works
  ok(tree.isSelected(["level1", "level2"]), "isSelected works");

  tree.selectedItem = ["level1"];
  const node3 = doc.querySelector(".theme-selected");
  ok(!!node3, "Something is still selected");
  isnot(node2, node3, "Newly selected node is different from previous");
  is(node3, node, "First and third selected nodes should be same");
  is(
    node3.parentNode.dataset.id,
    JSON.stringify(["level1"]),
    "Correct node selected"
  );

  // test if selectedItem getter works
  is(tree.selectedItem.length, 1, "Correct length of selected item");
  is(tree.selectedItem[0], "level1", "Correct selected item");

  // test if clear selection works
  tree.clearSelection();
  ok(
    !doc.querySelector(".theme-selected"),
    "Nothing selected after clear selection call"
  );

  // test if collapseAll/expandAll work
  ok(!!doc.querySelectorAll("[expanded]").length, "Some nodes are expanded");
  tree.collapseAll();
  is(
    doc.querySelectorAll("[expanded]").length,
    0,
    "Nothing is expanded after collapseAll call"
  );
  tree.expandAll();
  is(
    doc.querySelectorAll("[expanded]").length,
    13,
    "All tree items expanded after expandAll call"
  );

  // test if selectNextItem and selectPreviousItem work
  tree.selectedItem = ["level1", "level2"];
  ok(tree.isSelected(["level1", "level2"]), "Correct item selected");
  tree.selectNextItem();
  ok(
    tree.isSelected(["level1", "level2", "level3"]),
    "Correct item selected after selectNextItem call"
  );

  tree.selectNextItem();
  ok(
    tree.isSelected(["level1", "level2-1"]),
    "Correct item selected after second selectNextItem call"
  );

  tree.selectNextItem();
  ok(
    tree.isSelected(["level1", "level2-1", "level3-1"]),
    "Correct item selected after third selectNextItem call"
  );

  tree.selectPreviousItem();
  ok(
    tree.isSelected(["level1", "level2-1"]),
    "Correct item selected after selectPreviousItem call"
  );

  tree.selectPreviousItem();
  ok(
    tree.isSelected(["level1", "level2", "level3"]),
    "Correct item selected after second selectPreviousItem call"
  );

  // test if remove works
  ok(
    doc.querySelector(
      "[data-id='" + JSON.stringify(["level1", "level2", "level3"]) + "']"
    ),
    "level1-level2-level3 item exists before removing"
  );
  tree.remove(["level1", "level2", "level3"]);
  ok(
    !doc.querySelector(
      "[data-id='" + JSON.stringify(["level1", "level2", "level3"]) + "']"
    ),
    "level1-level2-level3 item does not exist after removing"
  );
  const level2item = doc.querySelector(
    "[data-id='" +
      JSON.stringify(["level1", "level2"]) +
      "'] > .tree-widget-item"
  );
  ok(
    level2item.hasAttribute("empty"),
    "level1-level2 item is marked as empty after removing"
  );

  tree.add([
    {
      id: "level1",
      label: "Level 1",
    },
    {
      id: "level2",
      label: "Level 2",
    },
    {
      id: "level3",
      label: "Level 3",
      type: "js",
    },
  ]);

  // test if clearing the tree works
  is(
    doc.querySelectorAll("[level='1']").length,
    3,
    "Correct number of top level items before clearing"
  );
  tree.clear();
  is(
    doc.querySelectorAll("[level='1']").length,
    0,
    "No top level item after clearing the tree"
  );
}