summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/pointerlock/file_retargetMouseEvents.html
blob: 810154c58c9120f3ddd7a82732ab32bec3ce406e (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=633602
-->
<head>
  <title>Bug 633602 - file_retargetMouseEvents.html</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js">
  </script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="application/javascript" src="pointerlock_utils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=633602">
    Mozilla Bug 633602
  </a>

  <div id="parent">
    <div id="child" style="width: 100%; height: 100%;">
    </div>
  </div>

  <pre id="test">
    <script type="application/javascript">
      /*
       * Test for Bug 633602
       * Retarget mouse events to the locked element
       */

      SimpleTest.waitForExplicitFinish();

      function MouseEventStats() {
        this.mouseMove = false;
        this.mouseDown = false;
        this.mouseUp = false;
        this.mouseClick = false;
        this.mouseScroll = false;
        this.wheel = false;
      }

      var parent = document.getElementById("parent")
        , child = document.getElementById("child")
        , parentStats = new MouseEventStats()
        , childStats = new MouseEventStats();

      function runTests () {
        is(childStats.mouseMove, false, "Child shound not receive mousemove event.");
        is(childStats.mouseDown, false, "Child should not receive mousedown event.");
        is(childStats.mouseUp, false, "Child should not receive mouseup event.");
        is(childStats.mouseClick, false, "Child should not receive click event.");
        is(childStats.mouseScroll, false, "Child should not receive DOMMouseScroll event.");
        is(childStats.wheel, false, "Child should not receive wheel event.");

        ok(parentStats.mouseMove, "Parent should receive mousemove event.");
        ok(parentStats.mouseDown, "Parent should receive mousedown event.");
        ok(parentStats.mouseUp, "Parent should receive mouseup event.");
        ok(parentStats.mouseClick, "Parent should receive click event.");
        ok(parentStats.mouseScroll, "Parent should receive DOMMouseScroll event.");
        ok(parentStats.wheel, "Parent should receive wheel event.");
      }


      /**
       * The event listeners for the child element shouldn't be fired
       * Mouse events will only happen when the pointer is locked
       * and if the pointer is locked all the mouse events should be
       * retargetted to the locked element
       **/
      var childMoveTest = function() {
        childStats.mouseMove = true;
      }

      var childDownTest = function() {
        childStats.mouseDown = true;
      };

      var childUpTest = function() {
        childStats.mouseUp = true;
      };

      var childClickTest = function() {
        childStats.mouseClick = true;
      };

      var childScrollTest = function() {
        childStats.mouseScroll = true;
      };

      var childWheelTest = function() {
        childStats.wheel = true;
      };

      //  Event listeners for the parent element
      var startMouseTests = function() {
        info("Got parent mousemove");
        parent.removeEventListener("mousemove", startMouseTests);
        parent.addEventListener("DOMMouseScroll", parentScrollTest);
        child.addEventListener("DOMMouseScroll", childScrollTest);
        SimpleTest.executeSoon(function () {
          synthesizeWheel(child, 5, 5, {'deltaY': 10, 'lineOrPageDeltaY': 10,
                                        'deltaMode': WheelEvent.DOM_DELTA_LINE});
        });
      };

      var parentScrollTest = function (e) {
        info("Got parent DOMMouseScroll");
        parentStats.mouseScroll = true;
        parent.removeEventListener("DOMMouseScroll", parentScrollTest);
        child.removeEventListener("DOMMouseScroll", childScrollTest);
        parent.addEventListener("wheel", parentWheelTest);
        child.addEventListener("wheel", childWheelTest);
        SimpleTest.executeSoon(function () {
          synthesizeWheel(child, 5, 5, {'deltaY': 10, 'lineOrPageDeltaY': 10,
                                        'deltaMode': WheelEvent.DOM_DELTA_LINE});
        });
      };

      var parentWheelTest = function (e) {
        info("Got parent wheel");
        parentStats.wheel = true;
        parent.removeEventListener("wheel", parentWheelTest);
        child.removeEventListener("wheel", childWheelTest);
        parent.addEventListener("mousedown", parentDownTest);
        child.addEventListener("mousedown", childDownTest);
        SimpleTest.executeSoon(function () {
          synthesizeMouseAtCenter(child, {type: "mousedown"}, window);
        });
      };

      var parentDownTest = function (e) {
        info("Got parent mousedown");
        parentStats.mouseDown = true;
        parent.removeEventListener("mousedown", parentDownTest);
        child.removeEventListener("mousedown", childDownTest);
        parent.addEventListener("mouseup", parentUpTest);
        child.addEventListener("mouseup", childUpTest);
        SimpleTest.executeSoon(function () {
          synthesizeMouseAtCenter(child, {type: "mouseup"}, window);
        });
      };

      var parentUpTest = function (e) {
        info("Got parent mouseup");
        parentStats.mouseUp = true;
        parent.removeEventListener("mouseup", parentUpTest);
        child.removeEventListener("mouseup", childUpTest);
        parent.addEventListener("click", parentClickTest);
        child.addEventListener("click", childClickTest);
        SimpleTest.executeSoon(function () {
          synthesizeMouseAtCenter(child, {}, window);
        });
      };

      var parentClickTest = function (e) {
        info("Got parent click");
        parentStats.mouseClick = true;
        parent.removeEventListener("click", parentClickTest);
        child.removeEventListener("click", childClickTest);
        parent.addEventListener("mousemove", parentMoveTest);
        child.addEventListener("mousemove", childMoveTest);
        SimpleTest.executeSoon(function () {
          synthesizeMouseAtCenter(child, {type: "mousemove"}, window);
        });
      };

      var parentMoveTest = function (e) {
        info("Got parent mousemove");
        parentStats.mouseMove = true;
        parent.removeEventListener("mousemove", parentMoveTest);
        child.removeEventListener("mousemove", childMoveTest);
        SimpleTest.executeSoon(function () {
          info("Exit fullscreen");
          addFullscreenChangeContinuation("exit", function() {
            info("Got fullscreenchange for exiting");
            runTests();
            SimpleTest.finish();
          });
          document.exitFullscreen();
        });
      }

      document.addEventListener("pointerlockchange", function (e) {
        if (document.pointerLockElement === parent) {
          info("Got pointerlockchange for entering");
          parent.addEventListener("mousemove", startMouseTests);
          child.addEventListener("mousemove", childMoveTest);
          SimpleTest.executeSoon(function () {
            synthesizeMouseAtCenter(parent, {type: "mousemove"}, window);
          });
        } else {
          info("Got pointerlockchange for exiting");
        }
      }, false);

      function start() {
        info("Requesting fullscreen on parent");
        addFullscreenChangeContinuation("enter", function() {
          info("Got fullscreenchange for entering");
          parent.requestPointerLock();
        });
        parent.requestFullscreen();
      }
    </script>
  </pre>
</body>
</html>