summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/gamepad
diff options
context:
space:
mode:
Diffstat (limited to 'dom/tests/mochitest/gamepad')
-rw-r--r--dom/tests/mochitest/gamepad/gamepad_frame.html20
-rw-r--r--dom/tests/mochitest/gamepad/gamepad_frame_state.html16
-rw-r--r--dom/tests/mochitest/gamepad/mochitest.ini12
-rw-r--r--dom/tests/mochitest/gamepad/mock_gamepad.js12
-rw-r--r--dom/tests/mochitest/gamepad/test_check_timestamp.html65
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad.html71
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_connect_events.html76
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html102
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html79
-rw-r--r--dom/tests/mochitest/gamepad/test_navigator_gamepads.html111
10 files changed, 564 insertions, 0 deletions
diff --git a/dom/tests/mochitest/gamepad/gamepad_frame.html b/dom/tests/mochitest/gamepad/gamepad_frame.html
new file mode 100644
index 000000000..345fec469
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/gamepad_frame.html
@@ -0,0 +1,20 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>frame</title>
+ <script type="text/javascript">
+ var connectedEvents = 0;
+ var buttonPresses = 0;
+ window.addEventListener("gamepadconnected", function() {
+ connectedEvents++;
+});
+ window.addEventListener("gamepadbuttondown", function() {
+ buttonPresses++;
+});
+ </script>
+</head>
+<body>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/gamepad_frame_state.html b/dom/tests/mochitest/gamepad/gamepad_frame_state.html
new file mode 100644
index 000000000..a53b91b2c
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/gamepad_frame_state.html
@@ -0,0 +1,16 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>frame</title>
+ <script type="text/javascript">
+ var gamepad;
+ window.addEventListener("gamepadconnected", function(e) {
+ gamepad = e.gamepad;
+ });
+ </script>
+</head>
+<body>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/mochitest.ini b/dom/tests/mochitest/gamepad/mochitest.ini
new file mode 100644
index 000000000..add02cb59
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/mochitest.ini
@@ -0,0 +1,12 @@
+[DEFAULT]
+support-files =
+ gamepad_frame.html
+ gamepad_frame_state.html
+ mock_gamepad.js
+
+[test_check_timestamp.html]
+[test_gamepad.html]
+[test_gamepad_connect_events.html]
+[test_gamepad_frame_state_sync.html]
+[test_gamepad_hidden_frame.html]
+[test_navigator_gamepads.html]
diff --git a/dom/tests/mochitest/gamepad/mock_gamepad.js b/dom/tests/mochitest/gamepad/mock_gamepad.js
new file mode 100644
index 000000000..a24660424
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/mock_gamepad.js
@@ -0,0 +1,12 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+var GamepadService;
+
+function runGamepadTest (callback) {
+ SpecialPowers.pushPrefEnv({"set" : [["dom.gamepad.test.enabled", true]]},
+ () => {
+ GamepadService = navigator.requestGamepadServiceTest();
+ callback();
+ });
+} \ No newline at end of file
diff --git a/dom/tests/mochitest/gamepad/test_check_timestamp.html b/dom/tests/mochitest/gamepad/test_check_timestamp.html
new file mode 100644
index 000000000..fa167d57b
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_check_timestamp.html
@@ -0,0 +1,65 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test Gamepad.timestamp</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <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">
+
+window.addEventListener("gamepadbuttondown", buttonpresshandler);
+
+var index;
+var timea=0;
+var firstPress = true;
+var testOver = false;
+
+SimpleTest.waitForExplicitFinish();
+runGamepadTest(checkTimestamp);
+
+function checkTimestamp(){
+ GamepadService.addGamepad("test gamepad 1",
+ GamepadService.standardMapping,
+ 4,
+ 2).then(function(i) {
+ index = i;
+ // Press a button to make the gamepad visible
+ // to the page.
+ GamepadService.newButtonEvent(index, 0, true);
+ GamepadService.newButtonEvent(index, 0, true);
+ ok(true, "test");
+ });
+}
+
+function cleanup(){
+ SpecialPowers.executeSoon(function() {
+ GamepadService.removeGamepad(index);
+ SimpleTest.finish();
+ });
+}
+
+function buttonpresshandler(e) {
+ if (testOver) {
+ return;
+ }
+ if (timea == 0){
+ timea = e.gamepad.timestamp;
+ } else {
+ ok(timea <= e.gamepad.timestamp, "Timestamp less than last timestamp");
+ }
+ GamepadService.newButtonEvent(index, 0, false);
+ if (!firstPress) {
+ testOver = true;
+ SpecialPowers.executeSoon(cleanup);
+ } else {
+ firstPress = false;
+ }
+}
+
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad.html b/dom/tests/mochitest/gamepad/test_gamepad.html
new file mode 100644
index 000000000..e66f7ff2a
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad.html
@@ -0,0 +1,71 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test gamepad</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <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">
+SimpleTest.waitForExplicitFinish();
+// Due to gamepad being a polling API instead of event driven, test ordering
+// ends up being a little weird in order to deal with e10s. Calls to
+// GamepadService are async across processes, so we'll need to make sure
+// we account for timing before checking values.
+window.addEventListener("gamepadconnected", connecthandler);
+var index;
+var testNum = 0;
+
+window.addEventListener("gamepadbuttondown", () => {
+ SpecialPowers.executeSoon(buttontest1);
+});
+
+window.addEventListener("gamepadbuttonup", () => {
+ SpecialPowers.executeSoon(buttontest2);
+});
+
+runGamepadTest(startTest);
+
+function startTest() {
+ // Add a gamepad
+ GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ 4,
+ 2).then(function(i) {
+ index = i;
+ // Simulate button events on the gamepad we added
+ GamepadService.newButtonEvent(index, 0, true);
+ });
+}
+
+function connecthandler(e) {
+ ok(e.gamepad.timestamp <= performance.now(),
+ "gamepad.timestamp should less than or equal to performance.now()");
+ is(e.gamepad.index, 0, "correct gamepad index");
+ is(e.gamepad.id, "test gamepad", "correct gamepad name");
+ is(e.gamepad.mapping, "standard", "standard mapping");
+ is(e.gamepad.buttons.length, 4, "correct number of buttons");
+ is(e.gamepad.axes.length, 2, "correct number of axes");
+}
+
+function buttontest1() {
+ var gamepads = navigator.getGamepads();
+ is(gamepads[0].buttons[0].pressed, true, "gamepad button should register as pressed");
+ GamepadService.newButtonValueEvent(index, 1, true, 0.5);
+}
+
+function buttontest2() {
+ var gamepads = navigator.getGamepads();
+ is(gamepads[0].buttons[1].pressed, true, "gamepad button should register as pressed");
+ is(gamepads[0].buttons[1].value, 0.5, "gamepad button value should be 0.5");
+ GamepadService.removeGamepad(index);
+ SimpleTest.finish();
+}
+
+</script>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html b/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html
new file mode 100644
index 000000000..e8102c24f
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html
@@ -0,0 +1,76 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!-- bug 893785 - Test that sending a gamepadconnected event to a new window
+ doesn't result in a gamepadconnected event being sent to existing
+ windows that have already received it. -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test hidden frames</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <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">
+SimpleTest.waitForExplicitFinish();
+
+var gamepad_index;
+
+function pressButton() {
+ GamepadService.newButtonEvent(gamepad_index, 0, true);
+ GamepadService.newButtonEvent(gamepad_index, 0, false);
+}
+
+ // Add a gamepad
+function startTests() {
+ window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ SpecialPowers.executeSoon(tests[testNum++]);
+ });
+
+ GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ 4, // buttons
+ 2).then(function(i) {
+ gamepad_index = i;
+ gamepad_connected()
+ });
+}
+
+var f1, f2;
+function gamepad_connected() {
+ f1 = document.getElementById('f1');
+ pressButton();
+}
+
+var testNum = 0;
+var tests = [
+ test1,
+ test2,
+];
+
+function test1() {
+ is(f1.contentWindow.connectedEvents, 1, "right number of connection events in frame 1");
+
+ // Now add another frame.
+ f2 = document.createElement("iframe");
+ f2.addEventListener("load", function() {
+ // When the frame is loaded, press a button again.
+ pressButton();
+ });
+ f2.src = "gamepad_frame.html";
+ document.body.appendChild(f2);
+}
+
+function test2() {
+ is(f1.contentWindow.connectedEvents, 1, "right number of connection events in frame 1");
+ is(f2.contentWindow.connectedEvents, 1, "right number of connection events in frame 2");
+ GamepadService.removeGamepad(gamepad_index);
+ SimpleTest.finish();
+}
+
+</script>
+<iframe id="f1" src="gamepad_frame.html" onload="runGamepadTest(startTests)"></iframe>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html
new file mode 100644
index 000000000..abf721789
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html
@@ -0,0 +1,102 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test hidden frames</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <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">
+SimpleTest.waitForExplicitFinish();
+ // Add a gamepad
+var index;
+
+function setFrameVisible(f, visible) {
+ var Ci = SpecialPowers.Ci;
+ var docshell = SpecialPowers.wrap(f.contentWindow).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShell);
+ docshell.isActive = visible;
+}
+
+var frames_loaded = 0;
+function startTest() {
+ frames_loaded++;
+ if (frames_loaded == 2) {
+ GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ 4, // buttons
+ 2).then(function(i) {
+ index = i;
+ gamepad_loaded();
+ });
+ }
+}
+var f1, f2;
+function gamepad_loaded() {
+ f1 = document.getElementById('f1');
+ f2 = document.getElementById('f2');
+ let w1 = f1.contentWindow;
+ let w2 = f2.contentWindow;
+ w1.addEventListener("gamepadbuttonup", () => {
+ ok(!f1.contentWindow.gamepad.buttons[0].pressed,
+ "frame 1 no button pressed");
+ });
+ w2.addEventListener("gamepadbuttonup", () => {
+ ok(!f2.contentWindow.gamepad.buttons[0].pressed,
+ "frame 2 no button pressed");
+ setFrameVisible(f2, false);
+ SpecialPowers.executeSoon(function() {
+ GamepadService.newButtonEvent(index, 0, true);
+ });
+ })
+ // Now press the button, but don't release it.
+ GamepadService.newButtonEvent(index, 0, true);
+}
+
+window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ SpecialPowers.executeSoon(tests[testNum++]);
+});
+
+var testNum = 0;
+var tests = [
+ check_button_pressed,
+ check_second_frame_no_button_press,
+];
+
+function check_button_pressed() {
+ // At this point the both frames should see the button as pressed.
+ ok(f1.contentWindow.gamepad.buttons[0].pressed, "frame 1 sees button pressed");
+ ok(f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 sees button pressed");
+
+ // Now release the button, then hide the second frame.
+ GamepadService.newButtonEvent(index, 0, false);
+}
+
+function check_second_frame_no_button_press () {
+ /*
+ * At this point the first frame should see the button as pressed,
+ * but the second frame should not, since it's hidden.
+ */
+ ok(f1.contentWindow.gamepad.buttons[0].pressed, "frame 1 sees button pressed");
+ ok(!f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 should not see button pressed");
+
+ // Now unhide the second frame.
+ setFrameVisible(f2, true);
+ SpecialPowers.executeSoon(function() {
+ // Now that the frame is visible again, it should see the button
+ // that was pressed.
+ ok(f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 sees button pressed");
+ // cleanup
+ GamepadService.removeGamepad(index);
+ SimpleTest.finish();
+ });
+}
+
+</script>
+<iframe id="f1" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></iframe>
+<iframe id="f2" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></iframe>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html
new file mode 100644
index 000000000..d287a128d
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html
@@ -0,0 +1,79 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test hidden frames</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <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">
+SimpleTest.waitForExplicitFinish();
+
+window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ SpecialPowers.executeSoon(tests[testNum++]);
+});
+
+function pressButton() {
+ GamepadService.newButtonEvent(index, 0, true);
+ GamepadService.newButtonEvent(index, 0, false);
+}
+
+function setFrameVisible(f, visible) {
+ var Ci = SpecialPowers.Ci;
+ var docshell = SpecialPowers.wrap(f.contentWindow).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShell);
+ docshell.isActive = visible;
+}
+
+var frames_loaded = 0;
+function startTest() {
+ frames_loaded++;
+ if (frames_loaded == 2) {
+ GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ 4, // buttons
+ 2).then(function(i) {
+ index = i;
+ gamepad_loaded();
+ });
+ }
+}
+var f1, f2;
+function gamepad_loaded() {
+ f1 = document.getElementById('f1');
+ f2 = document.getElementById('f2');
+ pressButton();
+}
+
+
+
+var testNum = 0;
+var tests = [
+ test1,
+ test2,
+];
+
+function test1() {
+ is(f1.contentWindow.buttonPresses, 1, "right number of button presses in frame 1");
+ is(f2.contentWindow.buttonPresses, 1, "right number of button presses in frame 2");
+
+ // Now hide the second frame and send another button press.
+ setFrameVisible(f2, false);
+ SpecialPowers.executeSoon( () => { pressButton(); });
+}
+
+function test2() {
+ is(f1.contentWindow.buttonPresses, 2, "right number of button presses in frame 1");
+ is(f2.contentWindow.buttonPresses, 1, "right number of button presses in frame 2");
+ GamepadService.removeGamepad(index);
+ SimpleTest.finish();
+}
+
+</script>
+<iframe id="f1" src="gamepad_frame.html" onload="runGamepadTest(startTest)"></iframe>
+<iframe id="f2" src="gamepad_frame.html" onload="runGamepadTest(startTest)"></iframe>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_navigator_gamepads.html b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html
new file mode 100644
index 000000000..48f78397a
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html
@@ -0,0 +1,111 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test gamepad</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <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">
+SimpleTest.waitForExplicitFinish();
+
+var testNum = 0;
+var tests = [
+ check_first_gamepad,
+ check_second_gamepad,
+ check_gamepad_hole,
+ check_no_gamepads,
+];
+
+function run_next_test(event) {
+ SpecialPowers.executeSoon(function() { 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)
+
+function startTest() {
+ // gamepads should be empty first
+ is(navigator.getGamepads().length, 0, "should be zero gamepads exposed");
+ // Add a gamepad
+ GamepadService.addGamepad("test gamepad 1", // id
+ GamepadService.standardMapping,
+ 4, // buttons
+ 2).then(function(index) {
+ internal_index1 = index;
+ // Press a button to make the gamepad visible to the page.
+ GamepadService.newButtonEvent(internal_index1, 0, true);
+ });
+}
+
+var content_index1 = 0;
+var internal_index2;
+var content_index2 = 1;
+
+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.
+ GamepadService.addGamepad("test gamepad 2", // id
+ GamepadService.standardMapping,
+ 4, // buttons
+ 2).then(function(index) {
+ internal_index2 = index;
+ GamepadService.newButtonEvent(internal_index2, 0, true);
+ });
+ ok(true, "Done checking first gamepad");
+}
+
+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.
+ GamepadService.removeGamepad(internal_index1);
+ ok(true, "Done checking second gamepad");
+}
+
+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.
+ 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>