summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/gamepad/timestamp-manual.html
blob: a2808dd0e0778b8df8271335beeb8678d7a6ba7a (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
<!doctype html>
<html>
<head>
<title>Manual Gamepad timestamp tests</title>
<link rel="help" href="https://w3c.github.io/gamepad/#dom-gamepad-timestamp">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
setup({explicit_timeout: true});

function set_instructions(text) {
  document.getElementById("instructions").textContent = text;
}

var index = -1;
var lastTimestamp = performance.now();
var id = -1;
addEventListener("gamepadconnected", function (e) {
  assert_equals(index, -1, "Too many connected events?");
  index = e.gamepad.index;
  assert_greater_than(e.gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
  lastTimestamp = e.gamepad.timestamp;
  set_instructions("Found a gamepad. Now release the button you pressed and press it again.");
  // There may not be a button pressed here, so handle it cleanly either way.
  if (e.gamepad.buttons.some(function (b) { return b.pressed; })) {
    id = setInterval(waitForButtonRelease, 15);
  } else {
    id = setInterval(waitForButtonPress, 15);
  }
});

function waitForButtonRelease() {
  var gamepad = navigator.getGamepads()[index];
  assert_true(!!gamepad);
  if (gamepad.buttons.every(function (b) { return !b.pressed; })) {
    assert_greater_than(gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
    lastTimestamp = gamepad.timestamp;
    clearInterval(id);
    id = setInterval(waitForButtonPress, 15);
  }
}

function waitForButtonPress() {
  var gamepad = navigator.getGamepads()[index];
  assert_true(!!gamepad);
  if (gamepad.buttons.some(function (b) { return b.pressed; })) {
    assert_greater_than(gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
    clearInterval(id);
    done();
  }
}

</script>
</head>
<body>
<p id="instructions">This test requires a gamepad. Connect one and press any button to start the test.</p>
</body>
</html>