browser_perf-tree-abstract-02.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. /**
  5. * Tests if the abstract tree base class for the profiler's tree view
  6. * has a functional public API.
  7. */
  8. const { appendAndWaitForPaint } = require("devtools/client/performance/test/helpers/dom-utils");
  9. const { synthesizeCustomTreeClass } = require("devtools/client/performance/test/helpers/synth-utils");
  10. add_task(function* () {
  11. let { MyCustomTreeItem, myDataSrc } = synthesizeCustomTreeClass();
  12. let container = document.createElement("vbox");
  13. yield appendAndWaitForPaint(gBrowser.selectedBrowser.parentNode, container);
  14. // Populate the tree and test the root item...
  15. let treeRoot = new MyCustomTreeItem(myDataSrc, { parent: null });
  16. treeRoot.autoExpandDepth = 1;
  17. treeRoot.attachTo(container);
  18. ok(treeRoot.expanded,
  19. "The root node should now be expanded.");
  20. ok(treeRoot.populated,
  21. "The root node should now be populated.");
  22. let fooItem = treeRoot.getChild(0);
  23. let barItem = treeRoot.getChild(1);
  24. ok(!fooItem.expanded && !barItem.expanded,
  25. "The 'foo' and 'bar' nodes should not be expanded yet.");
  26. ok(!fooItem.populated && !barItem.populated,
  27. "The 'foo' and 'bar' nodes should not be populated yet.");
  28. fooItem.expand();
  29. barItem.expand();
  30. ok(fooItem.expanded && barItem.expanded,
  31. "The 'foo' and 'bar' nodes should now be expanded.");
  32. ok(!fooItem.populated,
  33. "The 'foo' node should not be populated because it's empty.");
  34. ok(barItem.populated,
  35. "The 'bar' node should now be populated.");
  36. let bazItem = barItem.getChild(0);
  37. ok(!bazItem.expanded,
  38. "The 'bar' node should not be expanded yet.");
  39. ok(!bazItem.populated,
  40. "The 'bar' node should not be populated yet.");
  41. bazItem.expand();
  42. ok(bazItem.expanded,
  43. "The 'baz' node should now be expanded.");
  44. ok(!bazItem.populated,
  45. "The 'baz' node should not be populated because it's empty.");
  46. ok(!treeRoot.getChild(-1) && !treeRoot.getChild(2),
  47. "Calling `getChild` with out of bounds indices will return null (1).");
  48. ok(!fooItem.getChild(-1) && !fooItem.getChild(0),
  49. "Calling `getChild` with out of bounds indices will return null (2).");
  50. ok(!barItem.getChild(-1) && !barItem.getChild(1),
  51. "Calling `getChild` with out of bounds indices will return null (3).");
  52. ok(!bazItem.getChild(-1) && !bazItem.getChild(0),
  53. "Calling `getChild` with out of bounds indices will return null (4).");
  54. // Finished expanding all nodes in the tree...
  55. // Continue checking.
  56. is(container.childNodes.length, 4,
  57. "The container node should now have four children available.");
  58. is(container.childNodes[0], treeRoot.target,
  59. "The root node's target is a child of the container node.");
  60. is(container.childNodes[1], fooItem.target,
  61. "The 'foo' node's target is a child of the container node.");
  62. is(container.childNodes[2], barItem.target,
  63. "The 'bar' node's target is a child of the container node.");
  64. is(container.childNodes[3], bazItem.target,
  65. "The 'baz' node's target is a child of the container node.");
  66. treeRoot.collapse();
  67. is(container.childNodes.length, 1,
  68. "The container node should now have one children available.");
  69. ok(!treeRoot.expanded,
  70. "The root node should not be expanded anymore.");
  71. ok(fooItem.expanded && barItem.expanded && bazItem.expanded,
  72. "The 'foo', 'bar' and 'baz' nodes should still be expanded.");
  73. ok(treeRoot.populated && barItem.populated,
  74. "The root and 'bar' nodes should still be populated.");
  75. ok(!fooItem.populated && !bazItem.populated,
  76. "The 'foo' and 'baz' nodes should still not be populated because they're empty.");
  77. treeRoot.expand();
  78. is(container.childNodes.length, 4,
  79. "The container node should now have four children available again.");
  80. ok(treeRoot.expanded && fooItem.expanded && barItem.expanded && bazItem.expanded,
  81. "The root, 'foo', 'bar' and 'baz' nodes should now be reexpanded.");
  82. ok(treeRoot.populated && barItem.populated,
  83. "The root and 'bar' nodes should still be populated.");
  84. ok(!fooItem.populated && !bazItem.populated,
  85. "The 'foo' and 'baz' nodes should still not be populated because they're empty.");
  86. // Test `focus` on the root node...
  87. treeRoot.focus();
  88. is(document.commandDispatcher.focusedElement, treeRoot.target,
  89. "The root node is now focused.");
  90. // Test `focus` on a leaf node...
  91. bazItem.focus();
  92. is(document.commandDispatcher.focusedElement, bazItem.target,
  93. "The 'baz' node is now focused.");
  94. // Test `remove`...
  95. barItem.remove();
  96. is(container.childNodes.length, 2,
  97. "The container node should now have two children available.");
  98. is(container.childNodes[0], treeRoot.target,
  99. "The root node should be the first in the container node.");
  100. is(container.childNodes[1], fooItem.target,
  101. "The 'foo' node should be the second in the container node.");
  102. fooItem.remove();
  103. is(container.childNodes.length, 1,
  104. "The container node should now have one children available.");
  105. is(container.childNodes[0], treeRoot.target,
  106. "The root node should be the only in the container node.");
  107. treeRoot.remove();
  108. is(container.childNodes.length, 0,
  109. "The container node should now have no children available.");
  110. container.remove();
  111. });