summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/test/browser/browser_memory_tree_map-02.js
blob: f48bb66c13ca60ae9b7bfd9f6bacee1e92e341f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Test the drag and zooming behavior

"use strict";

const CanvasUtils = require("resource://devtools/client/memory/components/tree-map/canvas-utils.js");
const DragZoom = require("resource://devtools/client/memory/components/tree-map/drag-zoom.js");

const TEST_URL = "data:text/html,<html><body></body></html>";
const PIXEL_SCROLL_MODE = 0;
const PIXEL_DELTA = 10;
const MAX_RAF_LOOP = 1000;

this.test = makeMemoryTest(TEST_URL, async function ({ panel }) {
  const panelWin = panel.panelWin;
  const panelDoc = panelWin.document;
  const div = panelDoc.createElement("div");

  Object.assign(div.style, {
    width: "100px",
    height: "200px",
    position: "absolute",
    left: 0,
    top: 0,
  });

  const rafMock = createRAFMock();

  panelDoc.body.appendChild(div);

  const canvases = new CanvasUtils(div, 0);
  const dragZoom = new DragZoom(canvases.container, 0, rafMock.raf);
  const style = canvases.container.style;

  info("Check initial state of dragZoom");
  {
    is(dragZoom.zoom, 0, "Zooming starts at 0");
    is(dragZoom.smoothZoom, 0, "Smoothed zooming starts at 0");
    is(rafMock.timesCalled, 0, "No RAFs have been queued");
    is(
      style.transform,
      "translate(0px) scale(1)",
      "No transforms have been done."
    );

    canvases.container.dispatchEvent(
      new WheelEvent("wheel", {
        deltaY: -PIXEL_DELTA,
        deltaMode: PIXEL_SCROLL_MODE,
      })
    );

    is(
      style.transform,
      "translate(0px) scale(1.05)",
      "The div has been slightly scaled."
    );
    is(
      dragZoom.zoom,
      PIXEL_DELTA * dragZoom.ZOOM_SPEED,
      "The zoom was increased"
    );
    ok(
      floatEquality(dragZoom.smoothZoom, 0.05),
      "The smooth zoom is between the initial value and the target"
    );
    is(rafMock.timesCalled, 1, "A RAF has been queued");
  }

  info("RAF will eventually stop once the smooth values approach the target");
  {
    let i;
    let lastCallCount;
    for (i = 0; i < MAX_RAF_LOOP; i++) {
      if (lastCallCount === rafMock.timesCalled) {
        break;
      }
      lastCallCount = rafMock.timesCalled;
      rafMock.nextFrame();
    }
    is(
      style.transform,
      "translate(0px) scale(1.1)",
      "The scale has been fully applied"
    );
    is(
      dragZoom.zoom,
      dragZoom.smoothZoom,
      "The smooth and target zoom values match"
    );
    isnot(MAX_RAF_LOOP, i, "The RAF loop correctly stopped");
  }

  info("Dragging correctly translates the div");
  {
    div.dispatchEvent(
      new MouseEvent("mousemove", {
        clientX: 10,
        clientY: 10,
      })
    );
    div.dispatchEvent(new MouseEvent("mousedown"));
    div.dispatchEvent(
      new MouseEvent("mousemove", {
        clientX: 20,
        clientY: 20,
      })
    );
    div.dispatchEvent(new MouseEvent("mouseup"));

    is(
      style.transform,
      "translate(2.5px, 5px) scale(1.1)",
      "The style is correctly translated"
    );
    ok(
      floatEquality(dragZoom.translateX, 5),
      "Translate X moved by some pixel amount"
    );
    ok(
      floatEquality(dragZoom.translateY, 10),
      "Translate Y moved by some pixel amount"
    );
  }

  info("Zooming centers around the mouse");
  {
    canvases.container.dispatchEvent(
      new WheelEvent("wheel", {
        deltaY: -PIXEL_DELTA,
        deltaMode: PIXEL_SCROLL_MODE,
      })
    );
    // Run through the RAF loop to zoom in towards that value.
    let lastCallCount;
    for (let i = 0; i < MAX_RAF_LOOP; i++) {
      if (lastCallCount === rafMock.timesCalled) {
        break;
      }
      lastCallCount = rafMock.timesCalled;
      rafMock.nextFrame();
    }
    is(
      style.transform,
      "translate(8.18182px, 18.1818px) scale(1.2)",
      "Zooming affects the translation to keep the mouse centered"
    );
    ok(
      floatEquality(dragZoom.translateX, 8.181818181818185),
      "Translate X was affected by the mouse position"
    );
    ok(
      floatEquality(dragZoom.translateY, 18.18181818181817),
      "Translate Y was affected by the mouse position"
    );
    is(dragZoom.zoom, 0.2, "Zooming starts at 0");
  }

  dragZoom.destroy();

  info("Scroll isn't tracked after destruction");
  {
    const previousZoom = dragZoom.zoom;
    const previousSmoothZoom = dragZoom.smoothZoom;

    canvases.container.dispatchEvent(
      new WheelEvent("wheel", {
        deltaY: -PIXEL_DELTA,
        deltaMode: PIXEL_SCROLL_MODE,
      })
    );

    is(dragZoom.zoom, previousZoom, "The zoom stayed the same");
    is(
      dragZoom.smoothZoom,
      previousSmoothZoom,
      "The smooth zoom stayed the same"
    );
  }

  info("Translation isn't tracked after destruction");
  {
    const initialX = dragZoom.translateX;
    const initialY = dragZoom.translateY;

    div.dispatchEvent(new MouseEvent("mousedown"));
    div.dispatchEvent(new MouseEvent("mousemove"), {
      clientX: 40,
      clientY: 40,
    });
    div.dispatchEvent(new MouseEvent("mouseup"));
    is(dragZoom.translateX, initialX, "The translationX didn't change");
    is(dragZoom.translateY, initialY, "The translationY didn't change");
  }
  panelDoc.body.removeChild(div);
});