summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_coalesce_mousewheel.html
blob: 41af7d5b037e671be04b8296e6ac8f4b46f989f7 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>mousewheel coalescing</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="apz_test_utils.js"></script>
  <script src="apz_test_native_event_utils.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
  let wheelEvents = [];
  function mousewheel(aWheelEvent) {
    // Make mousewheel handling slow
    var start = performance.now();
    while (performance.now() < (start + 10));
    wheelEvents.push(aWheelEvent);
  }

  function resolveIfProcessed(resolve, minamount) {
    if (wheelEvents.length >= minamount) {
      SimpleTest.requestFlakyTimeout("Make sure we got all events.");
      setTimeout(function() { resolve(); }, 20);
    } else {
      setTimeout(function() { resolveIfProcessed(resolve, minamount); });
    }
  }

  const kLineDeltaFactor =
    SpecialPowers.getBoolPref("mousewheel.system_scroll_override.enabled", false)
      ? SpecialPowers.getIntPref("mousewheel.system_scroll_override.vertical.factor", 200) / 100
      : 1;

  function checkWheelEvents(aExpectedDeltaMode, aExpectedSumOfDeltaY, aDescription) {
    const lineDeltaFactor =
      aExpectedDeltaMode === WheelEvent.DOM_DELTA_LINE ? kLineDeltaFactor : 1;
    let succeeded = true;
    let deltaY = 0;
    for (const wheelEvent of wheelEvents) {
      succeeded &= wheelEvent.deltaMode === aExpectedDeltaMode;
      is(wheelEvent.deltaMode, aExpectedDeltaMode,
        `When ${aDescription}, the deltaMode of all wheel events should be ${aExpectedDeltaMode}`);
      deltaY += wheelEvent.deltaY;
    }
    succeeded &= deltaY == aExpectedSumOfDeltaY * lineDeltaFactor;
    is(deltaY, aExpectedSumOfDeltaY * lineDeltaFactor,
      `When ${aDescription}, sum of the deltaY of all wheel events should be ${aExpectedSumOfDeltaY * lineDeltaFactor}`);
    return succeeded;
  }

  async function testOneSingleWheelEvent() {
    await new Promise(function(resolve) {
      wheelEvents = [];
      var element = document.getElementById("wheelEventReceiver");
      element.addEventListener("wheel", mousewheel, true);

      synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });

      setTimeout(function() { resolveIfProcessed(resolve, 1); });
    });
    is(wheelEvents.length, 1,
      "Synthesizing a wheel event via the parent process should cause 1 wheel event");
    is(wheelEvents[0]?.deltaMode, WheelEvent.DOM_DELTA_LINE,
      "When Synthesizing a wheel event via the parent process, the deltaMode of a wheel event should be WheelEvent.DOM_DELTA_LINE");
    is(wheelEvents[0]?.deltaY, 3 * kLineDeltaFactor,
      `When Synthesizing a wheel event via the parent process, the deltaY of a wheel event should be ${3 * kLineDeltaFactor}`);
  }

  const kMaxRetry = 10;

  async function testTwoSingleWheelEvents() {
    function tryIt() {
      return new Promise(function(resolve) {
        info("Synthesizing 2 wheel events via the parent process...");
        wheelEvents = [];
        var element = document.getElementById("wheelEventReceiver");
        element.addEventListener("wheel", mousewheel, true);

        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });

        setTimeout(function() { resolveIfProcessed(resolve, 1); });
      });
    }
    for (let i = 0; i < kMaxRetry; i++) {
      await tryIt();
      if (wheelEvents.length == 2) {
        // Even if failed to coalescing, the sum of deltaY values should be same
        // as sum of the synthesized ones.
        if (!checkWheelEvents(WheelEvent.DOM_DELTA_LINE, 6,
                              "synthesizing 2 wheel events via the parent process")) {
          return;
        }
        continue;  // retry
      }
      is(wheelEvents.length, 1,
        "Synthesizing 2 wheel events via the parent process should cause only 1 wheel event");
      checkWheelEvents(WheelEvent.DOM_DELTA_LINE, 6,
                       "synthesizing 2 wheel events via the parent process");
      return;
    }
  }

  async function testManySingleWheelEvents() {
    function tryIt() {
      return new Promise(function(resolve) {
        info("Synthesizing 5 wheel events via the parent process...");
        wheelEvents = [];
        var element = document.getElementById("wheelEventReceiver");
        element.addEventListener("wheel", mousewheel, true);

        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });

        setTimeout(function() { resolveIfProcessed(resolve, 1); });
      });
    }
    for (let i = 0; i < kMaxRetry; i++) {
      await tryIt();
      if (wheelEvents.length > 1 && wheelEvents.length <= 5) {
        // Even if failed to coalescing, the sum of deltaY values should be same
        // as sum of the synthesized ones.
        if (!checkWheelEvents(WheelEvent.DOM_DELTA_LINE, 15,
                              "synthesizing 5 wheel events via the parent process")) {
          return;
        }
        continue;  // retry
      }
      is(wheelEvents.length, 1,
        "Synthesizing 5 wheel events via the parent process should cause only 1 wheel event");
      checkWheelEvents(WheelEvent.DOM_DELTA_LINE, 15,
                       "synthesizing 5 wheel events via the parent process");
      return;
    }
  }

  async function testMixedWheelEvents() {
    function tryIt() {
      return new Promise(function(resolve) {
        info("Synthesizing 2 line wheel events, 1 page wheel event and 1 line wheel event...");
        wheelEvents = [];
        var element = document.getElementById("wheelEventReceiver");
        element.addEventListener("wheel", mousewheel, true);

        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_PAGE });
        synthesizeWheel(element, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });

        setTimeout(function() { resolveIfProcessed(resolve, 3); });
      });
    }
    function checkGroupsOfWheelEvents() {
      let succeeded = true;
      let deltaY = [0, 0, 0];
      let index = 0;
      const description = "synthesizing 2 line wheel events, 1 page wheel event and 1 line wheel event";
      for (const wheelEvent of wheelEvents) {
        switch (index) {
          case 0:
            if (wheelEvent.deltaMode === WheelEvent.DOM_DELTA_LINE) {
              is(wheelEvent.deltaMode, WheelEvent.DOM_DELTA_LINE,
                `When ${description}, the deltaMode of the first group should be WheelEvent.DOM_DELTA_LINE`);
              break;
            }
            index++;
            // fallthrough
          case 1:
            if (wheelEvent.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
              is(wheelEvent.deltaMode, WheelEvent.DOM_DELTA_PAGE,
                `When ${description}, the deltaMode of the seconde group should be WheelEvent.DOM_DELTA_PAGE`);
              break;
            }
            index++;
            // fallthrough
          case 2:
            succeeded &= wheelEvent.deltaMode === WheelEvent.DOM_DELTA_LINE;
            is(wheelEvent.deltaMode, WheelEvent.DOM_DELTA_LINE,
              `When ${description}, the deltaMode of the last group should be WheelEvent.DOM_DELTA_LINE`);
            break;
        }
        deltaY[index] += wheelEvent.deltaY;
      }
      succeeded &= deltaY == [6 * kLineDeltaFactor, 3, 3 * kLineDeltaFactor];
      isDeeply(deltaY, [6 * kLineDeltaFactor, 3, 3 * kLineDeltaFactor],
        `When ${description}, sum of the deltaY of the each wheel event group should be same as sum of the synthesized ones`);
      return succeeded;
    }
    for (let i = 0; i < kMaxRetry; i++) {
      await tryIt();
      if (wheelEvents.length != 3 && wheelEvents.length > 1 && wheelEvents.length <= 5) {
        // Even if failed to coalescing, the sum of deltaY values should be same
        // as sum of the synthesized ones and the wheel events shouldn't be
        // reordered.
        if (!checkGroupsOfWheelEvents()) {
          return;
        }
        continue;  // retry
      }
      is(wheelEvents.length, 3,
        "Synthesizing 2 line wheel events, 1 page wheel event and 1 line wheel event via the parent process should cause only 3 wheel events");
      checkGroupsOfWheelEvents();
      return;
    }
  }

  async function runTests() {
    var enabled = SpecialPowers.getDOMWindowUtils(window).asyncPanZoomEnabled;

    // Avoid synthesized mousemove events to be fired at the system cursor.
    await promiseNativeMouseEvent({
      type: "mousemove",
      screenX: 0,
      screenY: 0,
    });

    await SpecialPowers.pushPrefEnv({set: [
      ["test.events.async.enabled", true],
      ["dom.event.wheel-coalesced.testing", true],
    ]});
    await promiseElementReadyForUserInput(document.documentElement);
    await SpecialPowers.pushPrefEnv({clear: [["test.events.async.enabled"]]});

    await testOneSingleWheelEvent();
    await testTwoSingleWheelEvents();
    await testManySingleWheelEvents();
    await testMixedWheelEvents();

    setTimeout(SimpleTest.finish);
    window.close();
  }

  </script>
</head>
<body onload="SimpleTest.waitForFocus(runTests);">
  <div id="wheelEventReceiver" style="width:100px;height:100px;"></div>
</body>
</html>