summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/gamepad/test_navigator_gamepads_iframe.html
blob: 9c98bac2d39b3b60d177dee21dc7b05adf2e4b0a (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Test gamepad</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript" src="mock_gamepad.js"></script>
<script class="testbody" type="text/javascript">
let ok = window.parent.ok;
let is = window.parent.is;
let isnot = window.parent.isnot;
let SimpleTest = window.parent.SimpleTest;
let SpecialPowers = window.parent.SpecialPowers;

var content_index1 = 0;
var internal_index2;
var content_index2 = 1;

var testNum = 0;
var tests = [
  check_first_gamepad,
  check_second_gamepad,
  check_gamepad_hole,
  check_no_gamepads,
];

function run_next_test(event) {
  SpecialPowers.executeSoon(async function() {
    await tests[testNum++](event);
  });
}

function buttonhandler(e) {
 run_next_test(e);
}

function disconnecthandler(e) {
  run_next_test(e);
}
window.addEventListener("gamepadbuttondown", buttonhandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);

runGamepadTest(startTest)

async function startTest() {
  // gamepads should be empty first
  is(navigator.getGamepads().length, 0, "should be zero gamepads exposed");
  // Add a gamepad
  internal_index1 = await GamepadService.addGamepad("test gamepad 1", // id
                     GamepadService.standardMapping,
                     GamepadService.noHand,
                     4, // buttons
                     2,
                     0,
                     0,
                     0);

  // Press a button to make the gamepad visible to the page.
  await GamepadService.newButtonEvent(internal_index1, 0, true, true);
}

async function check_first_gamepad(e) {
  ok(true, "Checking first gamepad");
  // First gamepad gets added.
  is(e.gamepad.id, "test gamepad 1", "correct gamepad name");
  var gamepads = navigator.getGamepads();
  is(gamepads.length, 1, "should have one gamepad exposed");
  is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
  is(gamepads[content_index1], e.gamepad, "gamepad counter working correctly");
  // Add a second gamepad, should automatically show up.
  internal_index2 = await GamepadService.addGamepad("test gamepad 2", // id
                     GamepadService.standardMapping,
                     GamepadService.noHand,
                     4, // buttons
                     2,
                     0,
                     0,
                     0);

  await GamepadService.newButtonEvent(internal_index2, 0, true, true);

  ok(true, "Done checking first gamepad");
}

async function check_second_gamepad(e) {
  ok(true, "Checking second gamepad");
  // Second gamepad gets added.
  is(e.gamepad.index, 1, "gamepad index should be 1")
  is(e.gamepad.id, "test gamepad 2", "correct gamepad name");
  var gamepads = navigator.getGamepads();
  is(gamepads.length, 2, "should have two gamepads exposed");
  is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
  is(gamepads[content_index2], e.gamepad, "gamepad counter working correctly");
  // Now remove the first one.
  await GamepadService.removeGamepad(internal_index1);
  ok(true, "Done checking second gamepad");
}

async function check_gamepad_hole(e) {
  ok(true, "Checking gamepad hole");
  // First gamepad gets removed.
  var gamepads = navigator.getGamepads();
  is(gamepads.length, 2, "gamepads should have two entries");
  is(gamepads[content_index1], null, "should be a hole in the gamepad list");
  isnot(gamepads[content_index2], null, "second gamepad should exist");
  // Now remove the second one.
  await GamepadService.removeGamepad(internal_index2);
  ok(true, "Done checking gamepad hole");
}

function check_no_gamepads(e) {
  ok(true, "Checking no gamepads");
  // Second gamepad gets removed.
  var gamepads = navigator.getGamepads();
  is(gamepads.length, 0, "gamepads should be empty");
  SimpleTest.finish();
}
</script>
</body>
</html>