browser_flame-graph-03c.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that vertical panning in the flame graph widget works properly.
  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_WIDTH = 200;
  22. const TEST_HEIGHT = 100;
  23. const TEST_DPI_DENSITIY = 2;
  24. const {FlameGraph} = require("devtools/client/shared/widgets/FlameGraph");
  25. add_task(function* () {
  26. yield addTab("about:blank");
  27. yield performTest();
  28. gBrowser.removeCurrentTab();
  29. });
  30. function* performTest() {
  31. let [host,, doc] = yield createHost();
  32. doc.body.setAttribute("style",
  33. "position: fixed; width: 100%; height: 100%; margin: 0;");
  34. let graph = new FlameGraph(doc.body, TEST_DPI_DENSITIY);
  35. graph.fixedWidth = TEST_WIDTH;
  36. graph.fixedHeight = TEST_HEIGHT;
  37. yield graph.ready();
  38. testGraph(graph);
  39. yield graph.destroy();
  40. host.destroy();
  41. }
  42. function testGraph(graph) {
  43. graph.setData({ data: TEST_DATA, bounds: TEST_BOUNDS });
  44. // Drag up vertically only.
  45. dragStart(graph, TEST_WIDTH / 2, TEST_HEIGHT / 2);
  46. is(graph.getViewRange().startTime | 0, 0,
  47. "The selection start boundary is correct (1).");
  48. is(graph.getViewRange().endTime | 0, 150,
  49. "The selection end boundary is correct (1).");
  50. is(graph.getViewRange().verticalOffset | 0, 0,
  51. "The vertical offset is correct (1).");
  52. hover(graph, TEST_WIDTH / 2, TEST_HEIGHT / 2 - 50);
  53. is(graph.getViewRange().startTime | 0, 0,
  54. "The selection start boundary is correct (2).");
  55. is(graph.getViewRange().endTime | 0, 150,
  56. "The selection end boundary is correct (2).");
  57. is(graph.getViewRange().verticalOffset | 0, 17,
  58. "The vertical offset is correct (2).");
  59. dragStop(graph, TEST_WIDTH / 2, TEST_HEIGHT / 2 - 100);
  60. is(graph.getViewRange().startTime | 0, 0,
  61. "The selection start boundary is correct (3).");
  62. is(graph.getViewRange().endTime | 0, 150,
  63. "The selection end boundary is correct (3).");
  64. is(graph.getViewRange().verticalOffset | 0, 42,
  65. "The vertical offset is correct (3).");
  66. // Drag down strongly vertically and slightly horizontally.
  67. dragStart(graph, TEST_WIDTH / 2, TEST_HEIGHT / 2);
  68. is(graph.getViewRange().startTime | 0, 0,
  69. "The selection start boundary is correct (4).");
  70. is(graph.getViewRange().endTime | 0, 150,
  71. "The selection end boundary is correct (4).");
  72. is(graph.getViewRange().verticalOffset | 0, 42,
  73. "The vertical offset is correct (4).");
  74. hover(graph, TEST_WIDTH / 2, TEST_HEIGHT / 2 + 50);
  75. is(graph.getViewRange().startTime | 0, 0,
  76. "The selection start boundary is correct (5).");
  77. is(graph.getViewRange().endTime | 0, 150,
  78. "The selection end boundary is correct (5).");
  79. is(graph.getViewRange().verticalOffset | 0, 25,
  80. "The vertical offset is correct (5).");
  81. dragStop(graph, TEST_WIDTH / 2 + 100, TEST_HEIGHT / 2 + 500);
  82. is(graph.getViewRange().startTime | 0, 0,
  83. "The selection start boundary is correct (6).");
  84. is(graph.getViewRange().endTime | 0, 150,
  85. "The selection end boundary is correct (6).");
  86. is(graph.getViewRange().verticalOffset | 0, 0,
  87. "The vertical offset is correct (6).");
  88. // Drag up slightly vertically and strongly horizontally.
  89. dragStart(graph, TEST_WIDTH / 2, TEST_HEIGHT / 2);
  90. is(graph.getViewRange().startTime | 0, 0,
  91. "The selection start boundary is correct (7).");
  92. is(graph.getViewRange().endTime | 0, 150,
  93. "The selection end boundary is correct (7).");
  94. is(graph.getViewRange().verticalOffset | 0, 0,
  95. "The vertical offset is correct (7).");
  96. hover(graph, TEST_WIDTH / 2 + 50, TEST_HEIGHT / 2);
  97. is(graph.getViewRange().startTime | 0, 0,
  98. "The selection start boundary is correct (8).");
  99. is(graph.getViewRange().endTime | 0, 116,
  100. "The selection end boundary is correct (8).");
  101. is(graph.getViewRange().verticalOffset | 0, 0,
  102. "The vertical offset is correct (8).");
  103. dragStop(graph, TEST_WIDTH / 2 + 500, TEST_HEIGHT / 2 + 100);
  104. is(graph.getViewRange().startTime | 0, 0,
  105. "The selection start boundary is correct (9).");
  106. is(graph.getViewRange().endTime | 0, 0,
  107. "The selection end boundary is correct (9).");
  108. is(graph.getViewRange().verticalOffset | 0, 0,
  109. "The vertical offset is correct (9).");
  110. }
  111. // EventUtils just doesn't work!
  112. function hover(graph, x, y = 1) {
  113. x /= window.devicePixelRatio;
  114. y /= window.devicePixelRatio;
  115. graph._onMouseMove({ testX: x, testY: y });
  116. }
  117. function dragStart(graph, x, y = 1) {
  118. x /= window.devicePixelRatio;
  119. y /= window.devicePixelRatio;
  120. graph._onMouseMove({ testX: x, testY: y });
  121. graph._onMouseDown({ testX: x, testY: y });
  122. }
  123. function dragStop(graph, x, y = 1) {
  124. x /= window.devicePixelRatio;
  125. y /= window.devicePixelRatio;
  126. graph._onMouseMove({ testX: x, testY: y });
  127. graph._onMouseUp({ testX: x, testY: y });
  128. }