blob: a2808dd0e0778b8df8271335beeb8678d7a6ba7a [file] [log] [blame]
Ted Mielczarek634cdba2014-12-11 14:23:301<!doctype html>
2<html>
3<head>
4<title>Manual Gamepad timestamp tests</title>
Raphael Kubo da Costaeea78e62017-08-01 11:22:465<link rel="help" href="https://w3c.github.io/gamepad/#dom-gamepad-timestamp">
Ted Mielczarek634cdba2014-12-11 14:23:306<script src="/resources/testharness.js"></script>
7<script src="/resources/testharnessreport.js"></script>
8<script>
9setup({explicit_timeout: true});
10
11function set_instructions(text) {
12 document.getElementById("instructions").textContent = text;
13}
14
15var index = -1;
16var lastTimestamp = performance.now();
17var id = -1;
18addEventListener("gamepadconnected", function (e) {
19 assert_equals(index, -1, "Too many connected events?");
20 index = e.gamepad.index;
21 assert_greater_than(e.gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
22 lastTimestamp = e.gamepad.timestamp;
23 set_instructions("Found a gamepad. Now release the button you pressed and press it again.");
24 // There may not be a button pressed here, so handle it cleanly either way.
25 if (e.gamepad.buttons.some(function (b) { return b.pressed; })) {
26 id = setInterval(waitForButtonRelease, 15);
27 } else {
28 id = setInterval(waitForButtonPress, 15);
29 }
30});
31
32function waitForButtonRelease() {
33 var gamepad = navigator.getGamepads()[index];
34 assert_true(!!gamepad);
35 if (gamepad.buttons.every(function (b) { return !b.pressed; })) {
36 assert_greater_than(gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
37 lastTimestamp = gamepad.timestamp;
38 clearInterval(id);
39 id = setInterval(waitForButtonPress, 15);
40 }
41}
42
43function waitForButtonPress() {
44 var gamepad = navigator.getGamepads()[index];
45 assert_true(!!gamepad);
46 if (gamepad.buttons.some(function (b) { return b.pressed; })) {
47 assert_greater_than(gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
48 clearInterval(id);
49 done();
50 }
51}
52
53</script>
54</head>
55<body>
56<p id="instructions">This test requires a gamepad. Connect one and press any button to start the test.</p>
57</body>
58</html>