Get timestamps and ids working in WK2 gamepads (and test them!)
[WebKit-https.git] / LayoutTests / gamepad / gamepad-timestamp.html
1 <head>
2 <script>
3
4 if (window.testRunner) {
5     testRunner.dumpAsText();
6     testRunner.waitUntilDone();
7 }
8
9 function log(msg)
10 {
11     document.getElementById("logger").innerHTML += msg + "<br>";
12 }
13
14 function finishTest()
15 {
16     if (testRunner)
17         testRunner.notifyDone();
18 }
19
20 var rafCount = 0;
21 var increasingTimestampsSeen = 0;
22 var timestamp = 0;
23
24 function rafCallback()
25 {
26     var gamepad = navigator.getGamepads()[0];
27     
28     if (gamepad.timestamp < timestamp) {
29         log("Timestamp on gamepad is " + gamepad.timestamp + " which is less than " + timestamp);
30         finishTest();
31     }
32
33     if (gamepad.timestamp > timestamp)
34         ++increasingTimestampsSeen;
35
36     if (increasingTimestampsSeen == 10) {
37         log("Increasing timestamp values seen for 10 RAF cycles");
38         finishTest();
39     }
40
41     if (++rafCount == 120) {
42         log("Went 120 RAF cycles without seeing 10 increasing timestamp values... yikes!");
43         finishTest();
44     }
45
46     timestamp = gamepad.timestamp;
47
48     if (gamepad.buttons[0] == 1.0)
49         testRunner.setMockGamepadButtonValue(0, 0, 0.0);
50     else
51         testRunner.setMockGamepadButtonValue(0, 0, 1.0);
52     
53     requestAnimationFrame(rafCallback);
54 }
55
56 function handleGamepadConnect()
57 {
58     requestAnimationFrame(rafCallback);
59 }
60
61 function runTest() {
62     addEventListener("gamepadconnected", handleGamepadConnect);
63
64     testRunner.setMockGamepadDetails(0, "Test Joystick", 2, 2);
65     testRunner.setMockGamepadAxisValue(0, 0, 0.7);
66     testRunner.setMockGamepadAxisValue(0, 1, -1.0);
67     testRunner.setMockGamepadButtonValue(0, 0, 1.0);
68     testRunner.setMockGamepadButtonValue(0, 1, 1.0);
69     testRunner.connectMockGamepad(0);
70 }
71
72 </script>
73 </head>
74 <body onload="runTest();">
75 <div id="logger"></div>
76 </body>