summaryrefslogtreecommitdiffstats
path: root/dom/events/test/file_coalesce_touchmove.html
blob: 00172ec596bf7864b83ee93631edb0bd48c7a253 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>touchmove coalescing</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
  window.oncontextmenu = function(e) {
    e.preventDefault();
  }

  window.addEventListener("touchstart", function(e) { e.preventDefault(); },
                          { passive: false} );

  var touchmoveCount = 0;
  function touchmove() {
    // Make touchmove handling slow
    var start = performance.now();
    while (performance.now() < (start + 10));
    ++touchmoveCount;
  }

  async function fireLotsOfSingleTouchMoves() {
    var ret = new Promise(function(resolve) {
      SpecialPowers.loadChromeScript(function() {
        var element = this.actorParent.rootFrameLoader.ownerElement;
        var rect = element.getBoundingClientRect();
        var win = element.ownerDocument.defaultView;
        var utils = win.windowUtils;
        var x = rect.x + (rect.width / 2);
        var y = Math.floor(rect.y + (rect.height / 4));
        var endY = Math.floor(rect.y + ((rect.height / 4) * 2));
        utils.sendTouchEvent("touchstart", [0], [x], [y], [1], [1], [0], [1],
                             0, false);
        while (y != endY) {
          utils.sendTouchEvent("touchmove", [0], [x], [y], [1], [1], [0], [1],
                               0, false);
          ++y;
        }
        utils.sendTouchEvent("touchend", [0], [x], [y], [1], [1], [0], [1],
                             0, false);

      });

      touchmoveCount = 0;
      window.addEventListener("touchmove", touchmove, true);
      window.addEventListener("touchend", function(e) {
        window.removeEventListener("touchmove", touchmove, true);
        resolve(touchmoveCount);
      }, {once: true});
    });

    return ret
  }

  async function fireTwoSingleTouches() {
    var ret = new Promise(function(resolve) {
      SpecialPowers.loadChromeScript(function() {
        var element = this.actorParent.rootFrameLoader.ownerElement;
        var rect = element.getBoundingClientRect();
        var win = element.ownerDocument.defaultView;
        var utils = win.windowUtils;
        var x = rect.x + (rect.width / 2);
        var startY = Math.floor(rect.y + (rect.height / 4));
        var endY = Math.floor(rect.y + ((rect.height / 4) * 2));
        utils.sendTouchEvent("touchstart", [0], [x], [startY], [1], [1], [0],
                             [1], 0, false);
        utils.sendTouchEvent("touchmove", [0], [x], [startY], [1], [1], [0],
                             [1], 0, false);
        utils.sendTouchEvent("touchmove", [0], [x], [startY + 1], [1], [1], [0],
                             [1], 0, false);
        utils.sendTouchEvent("touchend", [0], [x], [endY], [1], [1], [0],
                             [1], 0, false);
      });

      touchmoveCount = 0;
      window.addEventListener("touchmove", touchmove, true);
      window.addEventListener("touchend", function(e) {
        window.removeEventListener("touchmove", touchmove, true);
        resolve(touchmoveCount);
      }, {once: true});
    });

    return ret
  }

  async function fireLotsOfMultiTouchMoves() {
    var ret = new Promise(function(resolve) {
      SpecialPowers.loadChromeScript(function() {
        var element = this.actorParent.rootFrameLoader.ownerElement;
        var rect = element.getBoundingClientRect();
        var win = element.ownerDocument.defaultView;
        var utils = win.windowUtils;
        var x = rect.x + (rect.width / 2);
        var startY = Math.floor(rect.y + (rect.height / 4));
        var endY = Math.floor(rect.y + ((rect.height / 4) * 2));
        utils.sendTouchEvent("touchstart", [0, 1], [x, x + 1],
                             [startY, startY + 1], [1, 1], [1, 1], [0, 0],
                             [1, 1], 0, false);
        while (startY != endY) {
          utils.sendTouchEvent("touchmove", [0, 1], [x, x + 1],
                               [startY, startY + 1], [1, 1], [1, 1], [0, 0],
                               [1, 1], 0, false);
          ++startY;
        }
        utils.sendTouchEvent("touchend", [0, 1], [x, x + 1], [endY, endY + 1],
                             [1, 1], [1, 1], [0, 0], [1, 1], 0, false);

      });

      touchmoveCount = 0;
      window.addEventListener("touchmove", touchmove, true);
      window.addEventListener("touchend", function(e) {
        window.removeEventListener("touchmove", touchmove, true);
        resolve(touchmoveCount);
      }, {once: true});
    });

    return ret
  }

  function disableCoalescing() {
    return SpecialPowers.pushPrefEnv({"set": [["dom.events.compress.touchmove",
                                               false]]});
  }

  function enableCoalescing() {
    return SpecialPowers.pushPrefEnv({"set": [["dom.events.compress.touchmove",
                                               true]]});
  }

  async function runTests() {
    await disableCoalescing();
    var touchMovesWithoutCoalescing = await fireLotsOfSingleTouchMoves();
    await enableCoalescing();
    var touchMovesWithCoalescing = await fireLotsOfSingleTouchMoves();
    opener.ok(touchMovesWithoutCoalescing > touchMovesWithCoalescing,
              "Coalescing should reduce the number of touchmove events");

    await disableCoalescing();
    var twoTouchMovesWithoutCoalescing = await fireTwoSingleTouches();
    await enableCoalescing();
    var twoTouchMovesWithCoalescing = await fireTwoSingleTouches();
    opener.is(twoTouchMovesWithoutCoalescing, 2,
              "Should have got two touchmoves");
    opener.is(twoTouchMovesWithoutCoalescing, twoTouchMovesWithCoalescing,
              "Shouldn't have coalesced the initial touchmove.");

    await disableCoalescing();
    var multiTouchMovesWithoutCoalescing = await fireLotsOfMultiTouchMoves();
    await enableCoalescing();
    var multiTouchMovesWithCoalescing = await fireLotsOfMultiTouchMoves();
    opener.ok(multiTouchMovesWithoutCoalescing > multiTouchMovesWithCoalescing,
       "Coalescing should reduce the number of multitouch touchmove events");

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

  function init() {
    SpecialPowers.pushPrefEnv({"set": [["dom.w3c_touch_events.enabled", true]]},
                              runTests);
  }
  </script>
</head>
<body onload="SimpleTest.waitForFocus(init);">
</body>
</html>