browser_flame-graph-05.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that flame graph widget has proper keyboard support.
  5. const TEST_DATA = [
  6. {
  7. color: "#f00",
  8. blocks: [
  9. { x: 0, y: 0, width: 50, height: 20, text: "FOO" },
  10. { x: 50, y: 0, width: 100, height: 20, text: "BAR" }
  11. ]
  12. },
  13. {
  14. color: "#00f",
  15. blocks: [
  16. { x: 0, y: 30, width: 30, height: 20, text: "BAZ" }
  17. ]
  18. }
  19. ];
  20. const TEST_BOUNDS = { startTime: 0, endTime: 150 };
  21. const TEST_DPI_DENSITIY = 2;
  22. const KEY_CODE_UP = 38;
  23. const KEY_CODE_LEFT = 37;
  24. const KEY_CODE_RIGHT = 39;
  25. var {FlameGraph} = require("devtools/client/shared/widgets/FlameGraph");
  26. add_task(function* () {
  27. yield addTab("about:blank");
  28. yield performTest();
  29. gBrowser.removeCurrentTab();
  30. });
  31. function* performTest() {
  32. let [host,, doc] = yield createHost();
  33. doc.body.setAttribute("style",
  34. "position: fixed; width: 100%; height: 100%; margin: 0;");
  35. let graph = new FlameGraph(doc.body, TEST_DPI_DENSITIY);
  36. yield graph.ready();
  37. yield testGraph(host, graph);
  38. yield graph.destroy();
  39. host.destroy();
  40. }
  41. function* testGraph(host, graph) {
  42. graph.setData({ data: TEST_DATA, bounds: TEST_BOUNDS });
  43. is(graph._selection.start, 0,
  44. "The graph's selection start value is initially correct.");
  45. is(graph._selection.end, TEST_BOUNDS.endTime * TEST_DPI_DENSITIY,
  46. "The graph's selection end value is initially correct.");
  47. yield pressKeyForTime(graph, KEY_CODE_LEFT, 1000);
  48. is(graph._selection.start, 0,
  49. "The graph's selection start value is correct after pressing LEFT.");
  50. ok(graph._selection.end < TEST_BOUNDS.endTime * TEST_DPI_DENSITIY,
  51. "The graph's selection end value is correct after pressing LEFT.");
  52. graph._selection.start = 0;
  53. graph._selection.end = TEST_BOUNDS.endTime * TEST_DPI_DENSITIY;
  54. info("Graph selection was reset (1).");
  55. yield pressKeyForTime(graph, KEY_CODE_RIGHT, 1000);
  56. ok(graph._selection.start > 0,
  57. "The graph's selection start value is correct after pressing RIGHT.");
  58. is(graph._selection.end, TEST_BOUNDS.endTime * TEST_DPI_DENSITIY,
  59. "The graph's selection end value is correct after pressing RIGHT.");
  60. graph._selection.start = 0;
  61. graph._selection.end = TEST_BOUNDS.endTime * TEST_DPI_DENSITIY;
  62. info("Graph selection was reset (2).");
  63. yield pressKeyForTime(graph, KEY_CODE_UP, 1000);
  64. ok(graph._selection.start > 0,
  65. "The graph's selection start value is correct after pressing UP.");
  66. ok(graph._selection.end < TEST_BOUNDS.endTime * TEST_DPI_DENSITIY,
  67. "The graph's selection end value is correct after pressing UP.");
  68. let distanceLeft = graph._selection.start;
  69. let distanceRight = TEST_BOUNDS.endTime * TEST_DPI_DENSITIY - graph._selection.end;
  70. ok(Math.abs(distanceRight - distanceLeft) < 0.1,
  71. "The graph zoomed correctly towards the center point.");
  72. }
  73. function pressKeyForTime(graph, keyCode, ms) {
  74. graph._onKeyDown({
  75. keyCode,
  76. preventDefault: () => {},
  77. stopPropagation: () => {},
  78. });
  79. return new Promise(resolve => {
  80. setTimeout(() => {
  81. graph._onKeyUp({
  82. keyCode,
  83. preventDefault: () => {},
  84. stopPropagation: () => {},
  85. });
  86. resolve();
  87. }, ms);
  88. });
  89. }