browser_graphs-07e.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that selections are drawn onto the canvas.
  5. const TEST_DATA = [
  6. { delta: 112, value: 48 }, { delta: 213, value: 59 },
  7. { delta: 313, value: 60 }, { delta: 413, value: 59 },
  8. { delta: 530, value: 59 }, { delta: 646, value: 58 },
  9. { delta: 747, value: 60 }, { delta: 863, value: 48 },
  10. { delta: 980, value: 37 }, { delta: 1097, value: 30 },
  11. { delta: 1213, value: 29 }, { delta: 1330, value: 23 },
  12. { delta: 1430, value: 10 }, { delta: 1534, value: 17 },
  13. { delta: 1645, value: 20 }, { delta: 1746, value: 22 },
  14. { delta: 1846, value: 39 }, { delta: 1963, value: 26 },
  15. { delta: 2080, value: 27 }, { delta: 2197, value: 35 },
  16. { delta: 2312, value: 47 }, { delta: 2412, value: 53 },
  17. { delta: 2514, value: 60 }, { delta: 2630, value: 37 },
  18. { delta: 2730, value: 36 }, { delta: 2830, value: 37 },
  19. { delta: 2946, value: 36 }, { delta: 3046, value: 40 },
  20. { delta: 3163, value: 47 }, { delta: 3280, value: 41 },
  21. { delta: 3380, value: 35 }, { delta: 3480, value: 27 },
  22. { delta: 3580, value: 39 }, { delta: 3680, value: 42 },
  23. { delta: 3780, value: 49 }, { delta: 3880, value: 55 },
  24. { delta: 3980, value: 60 }, { delta: 4080, value: 60 },
  25. { delta: 4180, value: 60 }
  26. ];
  27. const LineGraphWidget = require("devtools/client/shared/widgets/LineGraphWidget");
  28. let CURRENT_ZOOM = 1;
  29. add_task(function* () {
  30. yield addTab("about:blank");
  31. yield performTest();
  32. gBrowser.removeCurrentTab();
  33. });
  34. function* performTest() {
  35. let [host,, doc] = yield createHost();
  36. let graph = new LineGraphWidget(doc.body, "fps");
  37. yield graph.once("ready");
  38. graph.setData(TEST_DATA);
  39. info("Testing with normal zoom.");
  40. testGraph(graph);
  41. info("Testing while zoomed out.");
  42. setZoom(host.frame, .5);
  43. testGraph(graph);
  44. info("Testing while zoomed in.");
  45. setZoom(host.frame, 2);
  46. testGraph(graph);
  47. yield graph.destroy();
  48. host.destroy();
  49. }
  50. function testGraph(graph) {
  51. graph.dropSelection();
  52. info("Making a selection.");
  53. dragStart(graph, 100);
  54. ok(graph.hasSelectionInProgress(),
  55. "The selection should start (1).");
  56. is(graph.getSelection().start, 100,
  57. "The current selection start value is correct (1).");
  58. is(graph.getSelection().end, 100,
  59. "The current selection end value is correct (1).");
  60. hover(graph, 200);
  61. ok(graph.hasSelectionInProgress(),
  62. "The selection should still be in progress (2).");
  63. is(graph.getSelection().start, 100,
  64. "The current selection start value is correct (2).");
  65. is(graph.getSelection().end, 200,
  66. "The current selection end value is correct (2).");
  67. dragStop(graph, 300);
  68. ok(!graph.hasSelectionInProgress(),
  69. "The selection should have stopped (3).");
  70. is(graph.getSelection().start, 100,
  71. "The current selection start value is correct (3).");
  72. is(graph.getSelection().end, 300,
  73. "The current selection end value is correct (3).");
  74. }
  75. function setZoom(frame, zoomValue) {
  76. let contViewer = frame.docShell.contentViewer;
  77. CURRENT_ZOOM = contViewer.fullZoom = zoomValue;
  78. }
  79. // EventUtils just doesn't work!
  80. function dispatchEvent(graph, x, y, fn) {
  81. x *= CURRENT_ZOOM;
  82. y *= CURRENT_ZOOM;
  83. x /= window.devicePixelRatio;
  84. y /= window.devicePixelRatio;
  85. let quad = graph._canvas.getBoxQuads({
  86. relativeTo: window.document
  87. })[0];
  88. let screenX = (window.screenX + quad.p1.x + x);
  89. let screenY = (window.screenY + quad.p1.y + y);
  90. fn({
  91. screenX: screenX,
  92. screenY: screenY,
  93. });
  94. }
  95. function hover(graph, x, y = 1) {
  96. dispatchEvent(graph, x, y, graph._onMouseMove);
  97. }
  98. function dragStart(graph, x, y = 1) {
  99. dispatchEvent(graph, x, y, graph._onMouseMove);
  100. dispatchEvent(graph, x, y, graph._onMouseDown);
  101. }
  102. function dragStop(graph, x, y = 1) {
  103. dispatchEvent(graph, x, y, graph._onMouseMove);
  104. dispatchEvent(graph, x, y, graph._onMouseUp);
  105. }