| Ted Mielczarek | 634cdba | 2014-12-11 14:23:30 | [diff] [blame^] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Manual Gamepad timestamp tests</title> |
| 5 | <link rel="help" href="https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#widl-Gamepad-timestamp"> |
| 6 | <script src="/resources/testharness.js"></script> |
| 7 | <script src="/resources/testharnessreport.js"></script> |
| 8 | <script> |
| 9 | setup({explicit_timeout: true}); |
| 10 | |
| 11 | function set_instructions(text) { |
| 12 | document.getElementById("instructions").textContent = text; |
| 13 | } |
| 14 | |
| 15 | var index = -1; |
| 16 | var lastTimestamp = performance.now(); |
| 17 | var id = -1; |
| 18 | addEventListener("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 | |
| 32 | function 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 | |
| 43 | function 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> |