browser_graphs-11b.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that bar graph's legend items handle mouseover/mouseout.
  5. const BarGraphWidget = require("devtools/client/shared/widgets/BarGraphWidget");
  6. const CATEGORIES = [
  7. { color: "#46afe3", label: "Foo" },
  8. { color: "#eb5368", label: "Bar" },
  9. { color: "#70bf53", label: "Baz" }
  10. ];
  11. add_task(function* () {
  12. yield addTab("about:blank");
  13. yield performTest();
  14. gBrowser.removeCurrentTab();
  15. });
  16. function* performTest() {
  17. let [host,, doc] = yield createHost();
  18. doc.body.setAttribute("style",
  19. "position: fixed; width: 100%; height: 100%; margin: 0;");
  20. let graph = new BarGraphWidget(doc.body, 1);
  21. graph.fixedWidth = 200;
  22. graph.fixedHeight = 100;
  23. yield graph.once("ready");
  24. yield testGraph(graph);
  25. yield graph.destroy();
  26. host.destroy();
  27. }
  28. function* testGraph(graph) {
  29. graph.format = CATEGORIES;
  30. graph.dataOffsetX = 1000;
  31. graph.setData([{
  32. delta: 1100, values: [0, 2, 3]
  33. }, {
  34. delta: 1200, values: [1, 0, 2]
  35. }, {
  36. delta: 1300, values: [2, 1, 0]
  37. }, {
  38. delta: 1400, values: [0, 3, 1]
  39. }, {
  40. delta: 1500, values: [3, 0, 2]
  41. }, {
  42. delta: 1600, values: [3, 2, 0]
  43. }]);
  44. /* eslint-disable max-len */
  45. is(graph._blocksBoundingRects.toSource(), "[{type:1, start:0, end:33.33333333333333, top:70, bottom:100}, {type:2, start:0, end:33.33333333333333, top:24, bottom:69}, {type:0, start:34.33333333333333, end:66.66666666666666, top:85, bottom:100}, {type:2, start:34.33333333333333, end:66.66666666666666, top:54, bottom:84}, {type:0, start:67.66666666666666, end:100, top:70, bottom:100}, {type:1, start:67.66666666666666, end:100, top:54, bottom:69}, {type:1, start:101, end:133.33333333333331, top:55, bottom:100}, {type:2, start:101, end:133.33333333333331, top:39, bottom:54}, {type:0, start:134.33333333333331, end:166.66666666666666, top:55, bottom:100}, {type:2, start:134.33333333333331, end:166.66666666666666, top:24, bottom:54}, {type:0, start:167.66666666666666, end:200, top:55, bottom:100}, {type:1, start:167.66666666666666, end:200, top:24, bottom:54}]",
  46. "The correct blocks bounding rects were calculated for the bar graph.");
  47. let legendItems = graph._document.querySelectorAll(".bar-graph-widget-legend-item");
  48. is(legendItems.length, 3,
  49. "Three legend items should exist in the entire graph.");
  50. yield testLegend(graph, 0, {
  51. highlights: "[{type:0, start:34.33333333333333, end:66.66666666666666, top:85, bottom:100}, {type:0, start:67.66666666666666, end:100, top:70, bottom:100}, {type:0, start:134.33333333333331, end:166.66666666666666, top:55, bottom:100}, {type:0, start:167.66666666666666, end:200, top:55, bottom:100}]",
  52. selection: "({start:34.33333333333333, end:200})",
  53. leftmost: "({type:0, start:34.33333333333333, end:66.66666666666666, top:85, bottom:100})",
  54. rightmost: "({type:0, start:167.66666666666666, end:200, top:55, bottom:100})"
  55. });
  56. yield testLegend(graph, 1, {
  57. highlights: "[{type:1, start:0, end:33.33333333333333, top:70, bottom:100}, {type:1, start:67.66666666666666, end:100, top:54, bottom:69}, {type:1, start:101, end:133.33333333333331, top:55, bottom:100}, {type:1, start:167.66666666666666, end:200, top:24, bottom:54}]",
  58. selection: "({start:0, end:200})",
  59. leftmost: "({type:1, start:0, end:33.33333333333333, top:70, bottom:100})",
  60. rightmost: "({type:1, start:167.66666666666666, end:200, top:24, bottom:54})"
  61. });
  62. yield testLegend(graph, 2, {
  63. highlights: "[{type:2, start:0, end:33.33333333333333, top:24, bottom:69}, {type:2, start:34.33333333333333, end:66.66666666666666, top:54, bottom:84}, {type:2, start:101, end:133.33333333333331, top:39, bottom:54}, {type:2, start:134.33333333333331, end:166.66666666666666, top:24, bottom:54}]",
  64. selection: "({start:0, end:166.66666666666666})",
  65. leftmost: "({type:2, start:0, end:33.33333333333333, top:24, bottom:69})",
  66. rightmost: "({type:2, start:134.33333333333331, end:166.66666666666666, top:24, bottom:54})"
  67. });
  68. /* eslint-enable max-len */
  69. }
  70. function* testLegend(graph, index, { highlights, selection, leftmost, rightmost }) {
  71. // Hover.
  72. let legendItems = graph._document.querySelectorAll(".bar-graph-widget-legend-item");
  73. let colorBlock = legendItems[index].querySelector("[view=color]");
  74. let debounced = graph.once("legend-hover");
  75. graph._onLegendMouseOver({ target: colorBlock });
  76. ok(!graph.hasMask(), "The graph shouldn't get highlights immediately.");
  77. let [type, rects] = yield debounced;
  78. ok(graph.hasMask(), "The graph should now have highlights.");
  79. is(type, index,
  80. "The legend item was correctly hovered.");
  81. is(rects.toSource(), highlights,
  82. "The legend item highlighted the correct regions.");
  83. // Unhover.
  84. let unhovered = graph.once("legend-unhover");
  85. graph._onLegendMouseOut();
  86. ok(!graph.hasMask(), "The graph shouldn't have highlights anymore.");
  87. yield unhovered;
  88. ok(true, "The 'legend-mouseout' event was emitted.");
  89. // Select.
  90. let selected = graph.once("legend-selection");
  91. graph._onLegendMouseDown(mockEvent(colorBlock));
  92. ok(graph.hasSelection(), "The graph should now have a selection.");
  93. is(graph.getSelection().toSource(), selection, "The graph has a correct selection.");
  94. let [left, right] = yield selected;
  95. is(left.toSource(), leftmost, "The correct leftmost data block was found.");
  96. is(right.toSource(), rightmost, "The correct rightmost data block was found.");
  97. // Deselect.
  98. graph.dropSelection();
  99. }
  100. function mockEvent(node) {
  101. return {
  102. target: node,
  103. preventDefault: () => {},
  104. stopPropagation: () => {}
  105. };
  106. }