dom/tests/mochitest/gamepad/test_navigator_gamepads.html

branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
equal deleted inserted replaced
-1:000000000000 0:a5ad7b1fb62e
1 <!-- Any copyright is dedicated to the Public Domain.
2 - http://creativecommons.org/publicdomain/zero/1.0/ -->
3 <!DOCTYPE HTML>
4 <html>
5 <head>
6 <title>Test gamepad</title>
7 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
9 </head>
10 <body>
11 <script type="text/javascript" src="mock_gamepad.js"></script>
12 <script class="testbody" type="text/javascript">
13 SimpleTest.waitForExplicitFinish();
14
15 var testNum = 0;
16 var tests = [
17 check_first_gamepad,
18 check_second_gamepad,
19 check_gamepad_hole,
20 check_no_gamepads,
21 ];
22
23 function run_next_test(event) {
24 SpecialPowers.executeSoon(function() { tests[testNum++](event); });
25 }
26
27 // gamepads should be empty first
28 is(navigator.getGamepads().length, 0, "should be zero gamepads exposed");
29
30 function connecthandler(e) {
31 run_next_test(e);
32 }
33
34 function disconnecthandler(e) {
35 run_next_test(e);
36 }
37 window.addEventListener("gamepadconnected", connecthandler);
38 window.addEventListener("gamepaddisconnected", disconnecthandler);
39 // Add a gamepad
40 var index1 = GamepadService.addGamepad("test gamepad 1", // id
41 SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
42 4, // buttons
43 2);// axes
44 var index2;
45
46 // Press a button to make the gamepad visible to the page.
47 GamepadService.newButtonEvent(index1, 0, true);
48
49 function check_first_gamepad(e) {
50 // First gamepad gets added.
51 is(e.gamepad.id, "test gamepad 1", "correct gamepad name");
52 var gamepads = navigator.getGamepads();
53 is(gamepads.length, 1, "should have one gamepad exposed");
54 is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
55 // Add a second gamepad, should automatically show up.
56 index2 = GamepadService.addGamepad("test gamepad 2", // id
57 SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
58 4, // buttons
59 2);// axes
60 }
61
62 function check_second_gamepad(e) {
63 // Second gamepad gets added.
64 is(e.gamepad.id, "test gamepad 2", "correct gamepad name");
65 var gamepads = navigator.getGamepads();
66 is(gamepads.length, 2, "should have two gamepads exposed");
67 is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
68 // Now remove the first one.
69 GamepadService.removeGamepad(index1);
70 }
71
72 function check_gamepad_hole(e) {
73 // First gamepad gets removed.
74 var gamepads = navigator.getGamepads();
75 is(gamepads.length, 2, "gamepads should have two entries");
76 is(gamepads[index1], null, "should be a hole in the gamepad list");
77 isnot(gamepads[index2], null, "second gamepad should exist");
78 // Now remove the second one.
79 GamepadService.removeGamepad(index2);
80 }
81
82 function check_no_gamepads(e) {
83 // Second gamepad gets removed.
84 var gamepads = navigator.getGamepads();
85 is(gamepads.length, 0, "gamepads should be empty");
86 SimpleTest.finish();
87 }
88 </script>
89 </body>
90 </html>
91

mercurial