summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/fullscreen_mouselock/fullscreen_mouselock.html
blob: e6593b3a5adaac4ef7c4140e3bee3e3a52877fd3 (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
<!DOCTYPE html>
<html>
<head>
<title>Fullscreen and Mouse Lock Scripts</title>
<style type="text/css">
  #HTMLCursor {
      border: solid black 1px;
      background: yellow;
      display: inline;
      position: absolute;
      pointer-events: none;
      z-index: 1;
  }
</style>
<script type="text/javascript">

function enterFullscreen() {
  console.log("enterFullscreen()");
  document.getElementById('container').webkitRequestFullScreen(
    Element.ALLOW_KEYBOARD_INPUT);
}

function exitFullscreen() {
  console.log("exitFullscreen()");
  document.webkitCancelFullScreen();
}

// Wait for notification from JS, then notify test of success or failure
// callback that the click has registered and the mouse lock state has changed.
function lockMouse1(callback) {
  console.log("lockMouse1()");
  var target = document.getElementById("lockTarget1");

  function failure() {
    console.log("lock failed");
    if (callback) {
      callback("failure");
    }
  };
  function possibleSuccess() {
    if (document.pointerLockElement == target) {
      console.log("lock success");
      if (callback)
        callback("success");
    } else {
      failure();
    }
  };
  document.onpointerlockchange = possibleSuccess;
  document.onpointerlockerror = failure;
  target.requestPointerLock();
}

// In the PyAuto test the fullscreen is initiated, accepted and enters into a
// wait state reading the value of lock_result. One of the two asynchronous
// functions in the JS will be executed. The PyAuto code waits for lock_result
// to return "success" or "failure". Sample PyAuto code:
// lock_result = self._driver.execute_script('lockMouse1AndSetLockResult()')
function lockMouseAndSetLockResult(targetId) {
  var target = document.getElementById(targetId);
  lock_result = "";

  function failure() {
    console.log("lock failed");
    lock_result = "failure"
  };
  function possibleSuccess() {
    if (document.pointerLockElement == target) {
      console.log("lock success");
      lock_result = "success"
    } else {
      failure();
    }
  };
  document.onpointerlockchange = possibleSuccess;
  document.onpointerlockerror = failure;
  target.requestPointerLock();
}

function lockMouse1AndSetLockResult() {
  console.log("lockMouse1AndSetLockResult()");
  lockMouseAndSetLockResult("lockTarget1");
}

// When mouse lock is initiated and accepted, PyAuto test will wait for the
// lock_result to return "success" or "failure" to initiate the next action.
function lockMouse2() {
  console.log("lockMouse2()");
  lockMouseAndSetLockResult("lockTarget2");
}

function delayedLockMouse1() {
  console.log("delayedLockMouse1()");
  window.setTimeout(lockMouse1, 1010);
  // Delay must be over 1 second or the click that initiated the delayed action
  // may still be considered active and treat this as a user gesture.
  // We want to test a lock not associated with a user gesture.
}

function spamLockMouse2() {
  console.log("spamLockMouse2()")
  window.setInterval(lockMouse2, 111);
}

function unlockMouse() {
  console.log("unlockMouse()");
  document.exitPointerLock();
}

function enterFullscreenAndLockMouse1() {
  console.log("enterFullscreenAndLockMouse1()");
  enterFullscreen();
  lockMouse1();
}

function lockMouse1AndEnterFullscreen() {
  console.log("lockMouse1AndEnterFullscreen()");
  lockMouse1();
  enterFullscreen();
}

function moveHTMLCursorTo(x, y) {
  HTMLCursor.style.left = x + "px";;
  HTMLCursor.style.top = y + "px";
}

function moveHTMLCursorToCenter() {
  moveHTMLCursorTo(window.innerWidth / 2, window.innerHeight / 2);
}

function moveHTMLCursorBy(x, y) {
  moveHTMLCursorTo(
      HTMLCursor.getBoundingClientRect().left + parseInt(x),
      HTMLCursor.getBoundingClientRect().top + parseInt(y));
}

var polyFillMouseEventMovementFromVenderPrefix = function (e) {
  e.movementX = (e.movementX !== undefined ? e.movementX :
                (e.mozMovementX !== undefined ? e.mozMovementX :
                (e.oMovementX !== undefined ? e.oMovementX :
                 e.msMovementY)));
  e.movementY = (e.movementY !== undefined ? e.movementY :
                (e.mozMovementY !== undefined ? e.mozMovementY :
                (e.oMovementY !== undefined ? e.oMovementY :
                 e.msMovementY)));
}

var clicked_elem_ID = ""
function clickElement(id) {
  clicked_elem_ID = id;
}

function init() {
  moveHTMLCursorToCenter();

  document.addEventListener("mousemove",
                            polyFillMouseEventMovementFromVenderPrefix);
  document.addEventListener("mousemove", function (e) {
    if (e.movementX !== undefined) {
      moveHTMLCursorBy(e.movementX, e.movementY);
      displaytext.innerHTML =
        "Document mousemove listener:<br>" +
        "<ul>" +
        "<li>Raw screen position: " + e.screenX + ", " + e.screenY + "</li>" +
        "<li>HTMLCursor: "
        + HTMLCursor.getBoundingClientRect().left + ", "
        + HTMLCursor.getBoundingClientRect().top + "</li>" +
        "<li>Movement: " + e.movementX + ", " + e.movementY + "</li>" +
        "</ul>";
    } else {
      displaytext.innerHTML =
        "<b>You must enable pointer lock in about:flags</b>";
    }
  });

  document.addEventListener("keypress", function (e) {
    switch (String.fromCharCode(e.charCode)) {
    case "f": enterFullscreen(); break;
    case "x": exitFullscreen(); break;
    case "1": lockMouse1(); break;
    case "2": lockMouse2(); break;
    case "d": delayedLockMouse1(); break;
    case "u": unlockMouse(); break;
    case "b": enterFullscreenAndLockMouse1(); break;
    case "B": lockMouse1AndEnterFullscreen(); break;
    default: moveHTMLCursorToCenter(); break;
    }
  });
}

</script>
</head>
<body onload="init()"
  title="This tooltip should not be shown if the mouse is locked.">
  <div id="container">
    <button id="enterFullscreen" onclick="enterFullscreen();">
      enterFullscreen() [f]
    </button><br>
    <button id="exitFullscreen" onclick="exitFullscreen();">
      exitFullscreen() [x]
    </button><br>
    <button id="lockMouse1" onclick="lockMouse1();">
      lockMouse1() [1]
    </button><br>
    <button id="lockMouse2" onclick="lockMouse2();">
      lockMouse2() [2]
    </button><br>
    <button id="delayedLockMouse1" onclick="delayedLockMouse1();">
      delayedLockMouse1() [d]
    </button><br>
    <button id="spamLockMouse2" onclick="spamLockMouse2();">
      spamLockMouse2()
    </button><br>
    <button id="unlockMouse" onclick="unlockMouse();">
      unlockMouse() [u]
    </button><br>
    <button id="enterFullscreenAndLockMouse1"
      onclick="enterFullscreenAndLockMouse1()">
      enterFullscreenAndLockMouse1() [b]
    </button><br>
    <button id="lockMouse1AndEnterFullscreen"
      onclick="lockMouse1AndEnterFullscreen()">
      lockMouse1AndEnterFullscreen() [B]
    </button><br>
    <div id="lockTarget1">lockTarget1</div>
    <div id="lockTarget2">lockTarget2</div>
    <form name="HTMLCursor" id="HTMLCursor">HTMLCursor</form>
    <form name="displaytext">...</form>
    <p>The <a href="#anchor" name="anchor" id="anchor"
      onclick="clickElement(this.id);">
      anchor link
    </a>
    navigates to an anchor on this page. The browser should not exit tab
    fullscreen or mouse lock.</p>
  </div>
  <p>This text is outside of the container that is made fullscreen. This text
  should not be visible when fullscreen.</p>
</body>
</html>