summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/gamepad
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/tests/mochitest/gamepad
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/tests/mochitest/gamepad')
-rw-r--r--dom/tests/mochitest/gamepad/gamepad_frame.html22
-rw-r--r--dom/tests/mochitest/gamepad/gamepad_frame_state.html16
-rw-r--r--dom/tests/mochitest/gamepad/mochitest.ini23
-rw-r--r--dom/tests/mochitest/gamepad/mock_gamepad.js19
-rw-r--r--dom/tests/mochitest/gamepad/test_check_timestamp.html20
-rw-r--r--dom/tests/mochitest/gamepad/test_check_timestamp_iframe.html71
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad.html20
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_connect_events.html22
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_connect_events_iframe.html83
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_extensions.html19
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_extensions_iframe.html179
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html19
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync_iframe.html113
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html19
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_hidden_frame_iframe.html81
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_iframe.html81
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin.html20
-rw-r--r--dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin_iframe.html256
-rw-r--r--dom/tests/mochitest/gamepad/test_navigator_gamepads.html19
-rw-r--r--dom/tests/mochitest/gamepad/test_navigator_gamepads_iframe.html123
20 files changed, 1225 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 0000000000..dae1e92b1b
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/gamepad_frame.html
@@ -0,0 +1,22 @@
+<!-- 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;
+ var idlConnected = 0;
+ window.addEventListener("gamepadconnected", function() {
+ connectedEvents++;
+ });
+ window.addEventListener("gamepadbuttondown", function() {
+ buttonPresses++;
+ });
+ window.ongamepadconnected = () => ++idlConnected;
+ </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 0000000000..a53b91b2cb
--- /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 0000000000..63aecbc623
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/mochitest.ini
@@ -0,0 +1,23 @@
+[DEFAULT]
+scheme = https
+support-files =
+ gamepad_frame.html
+ gamepad_frame_state.html
+ mock_gamepad.js
+
+[test_check_timestamp.html]
+support-files = test_check_timestamp_iframe.html
+[test_gamepad.html]
+support-files = test_gamepad_iframe.html
+[test_gamepad_connect_events.html]
+support-files = test_gamepad_connect_events_iframe.html
+[test_gamepad_extensions.html]
+support-files = test_gamepad_extensions_iframe.html
+[test_gamepad_frame_state_sync.html]
+support-files = test_gamepad_frame_state_sync_iframe.html
+[test_gamepad_hidden_frame.html]
+support-files = test_gamepad_hidden_frame_iframe.html
+[test_gamepad_multitouch_crossorigin.html]
+support-files = test_gamepad_multitouch_crossorigin_iframe.html
+[test_navigator_gamepads.html]
+support-files = test_navigator_gamepads_iframe.html
diff --git a/dom/tests/mochitest/gamepad/mock_gamepad.js b/dom/tests/mochitest/gamepad/mock_gamepad.js
new file mode 100644
index 0000000000..778035ef10
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/mock_gamepad.js
@@ -0,0 +1,19 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+var GamepadService;
+
+async function setGamepadPreferenceAndCreateIframe(iframeSrc) {
+ await SpecialPowers.pushPrefEnv({
+ set: [["dom.gamepad.test.enabled", true]],
+ });
+
+ let iframe = document.createElement("iframe");
+ iframe.src = iframeSrc;
+ document.body.appendChild(iframe);
+}
+
+function runGamepadTest(callback) {
+ GamepadService = navigator.requestGamepadServiceTest();
+ callback();
+}
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 0000000000..04fc8dc9d8
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_check_timestamp.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>Test Gamepad.timestamp</title>
+ <script 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_check_timestamp_iframe.html");
+
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_check_timestamp_iframe.html b/dom/tests/mochitest/gamepad/test_check_timestamp_iframe.html
new file mode 100644
index 0000000000..ca99c26746
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_check_timestamp_iframe.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.timestamp</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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+window.addEventListener("gamepadbuttondown", buttonpresshandler);
+
+var index;
+var timea=0;
+var firstPress = true;
+var testOver = false;
+
+runGamepadTest(checkTimestamp);
+
+async function checkTimestamp(){
+ const index = await GamepadService.addGamepad("test gamepad 1",
+ GamepadService.standardMapping,
+ GamepadService.noHand,
+ 4,
+ 2,
+ 0,
+ 0,
+ 0);
+
+ // Press a button to make the gamepad visible
+ // to the page.
+ await GamepadService.newButtonEvent(index, 0, true, true);
+ await GamepadService.newButtonEvent(index, 0, true, true);
+ ok(true, "test");
+}
+
+function cleanup(){
+ SpecialPowers.executeSoon(async function() {
+ await GamepadService.removeGamepad(index);
+ SimpleTest.finish();
+ });
+}
+
+async function buttonpresshandler(e) {
+ if (testOver) {
+ return;
+ }
+ if (timea == 0){
+ timea = e.gamepad.timestamp;
+ } else {
+ ok(timea <= e.gamepad.timestamp, "Timestamp less than last timestamp");
+ }
+ await GamepadService.newButtonEvent(index, 0, false, 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 0000000000..4e7c9a32bc
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad.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>Test gamepad</title>
+ <script 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_gamepad_iframe.html");
+
+</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 0000000000..f505ceb9d9
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html
@@ -0,0 +1,22 @@
+<!-- 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 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_gamepad_connect_events_iframe.html");
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_connect_events_iframe.html b/dom/tests/mochitest/gamepad/test_gamepad_connect_events_iframe.html
new file mode 100644
index 0000000000..278e6796c0
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_connect_events_iframe.html
@@ -0,0 +1,83 @@
+<!-- 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>
+ <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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+var gamepad_index;
+
+async function pressButton() {
+ await GamepadService.newButtonEvent(gamepad_index, 0, true, true);
+ await GamepadService.newButtonEvent(gamepad_index, 0, false, false);
+}
+
+ // Add a gamepad
+async function startTests() {
+ window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ SpecialPowers.executeSoon(tests[testNum++]);
+ });
+
+ gamepad_index = await GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ GamepadService.noHand,
+ 4, // buttons
+ 2,
+ 0,
+ 0,
+ 0)
+
+ await gamepad_connected();
+}
+
+var f1, f2;
+async function gamepad_connected() {
+ f1 = document.getElementById('f1');
+ await 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", async () => {
+ // When the frame is loaded, press a button again.
+ await pressButton();
+ });
+ f2.src = "gamepad_frame.html";
+ document.body.appendChild(f2);
+}
+
+async 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");
+ is(f1.contentWindow.idlConnected, 1, "right number of IDL connection events in frame 1");
+ is(f2.contentWindow.idlConnected, 1, "right number of IDL connection events in frame 2");
+ await 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_extensions.html b/dom/tests/mochitest/gamepad/test_gamepad_extensions.html
new file mode 100644
index 0000000000..364ad540fe
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_extensions.html
@@ -0,0 +1,19 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test gamepad</title>
+ <script 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_gamepad_extensions_iframe.html");
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_extensions_iframe.html b/dom/tests/mochitest/gamepad/test_gamepad_extensions_iframe.html
new file mode 100644
index 0000000000..6db4b85bb6
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_extensions_iframe.html
@@ -0,0 +1,179 @@
+<!-- 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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+var tests = [
+ poseadd,
+ posecheck,
+ touchadd,
+ touchcheck,
+ haptictest // Keep haptictest at the last because it will test removing gamepad from the service.
+];
+var gamepad_index = 0;
+var testNum = 0;
+var poseOrient = new Float32Array([-0.203, -0.235, 0.740, -0.596]);
+var posePos = new Float32Array([-0.0233, -0.707, -0.763]);
+var poseAngVel = new Float32Array([-0.0008, 0.00147, 0.001]);
+var poseAngAcc = new Float32Array([-0.494, 0.476, -0.241]);
+var poseLinVel = new Float32Array([0.003,0.024,-0.068]);
+var poseLinAcc = new Float32Array([-1.211,21.427,-2.348]);
+var touchData = [{touchId: 0, surfaceId: 0, pos: new Float32Array([-0.5, 0.5]), surf: new Float32Array([100, 100])},
+ {touchId: 1, surfaceId: 0, pos: new Float32Array([-0.1, 1.0]), surf: new Float32Array([100, 100])}];
+
+window.addEventListener("gamepadconnected", connecthandler);
+window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ SpecialPowers.executeSoon(async ()=> {
+ await tests[testNum++]()
+ });
+});
+
+async function pressButton() {
+ await GamepadService.newButtonEvent(gamepad_index, 0, true, true);
+ await GamepadService.newButtonEvent(gamepad_index, 0, false, false);
+}
+
+async function startTest() {
+ await SpecialPowers.pushPrefEnv({ "set": [
+ ["dom.gamepad.extensions.enabled", true],
+ ["dom.gamepad.extensions.lightindicator", true],
+ ["dom.gamepad.extensions.multitouch", true]] });
+ // Add a gamepad
+ gamepad_index = await GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ GamepadService.leftHand,
+ 4,
+ 2,
+ 1,
+ 1,
+ 2);
+
+ // Simulate button events on the gamepad we added
+ await pressButton();
+
+}
+
+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.hand, "left", "left hand");
+ is(e.gamepad.buttons.length, 4, "correct number of buttons");
+ is(e.gamepad.axes.length, 2, "correct number of axes");
+ is(e.gamepad.hapticActuators.length, 1, "correct number of haptics");
+ is(e.gamepad.lightIndicators.length, 1, "correct number of light indicators");
+ is(e.gamepad.touchEvents.length, 2, "correct number of touches");
+}
+
+function checkValueInFloat32Array(array1, array2) {
+ if (array1.length != array2.length) {
+ return false;
+ }
+ var index = 0;
+ while (index < array2.length) {
+ if (array1[index] != array2[index]) {
+ return false;
+ }
+ ++index;
+ }
+ return true;
+}
+
+async function poseadd() {
+ await GamepadService.newPoseMove(gamepad_index, poseOrient,
+ posePos, poseAngVel, poseAngAcc,
+ poseLinVel, poseLinAcc);
+ await pressButton();
+}
+
+async function posecheck() {
+ var gamepads = navigator.getGamepads();
+ var pose = gamepads[0].pose;
+ is(gamepads[0].pose.hasOrientation, true,
+ "correct gamepadPose hasOrientation");
+ is(gamepads[0].pose.hasPosition, true,
+ "correct gamepadPose hasPosition");
+ is(checkValueInFloat32Array(pose.orientation, poseOrient), true,
+ "correct gamepadPose orientation");
+ is(checkValueInFloat32Array(pose.position, posePos), true,
+ "correct gamepadPose position");
+ is(checkValueInFloat32Array(pose.angularVelocity, poseAngVel), true,
+ "correct gamepadPose angularVelocity");
+ is(checkValueInFloat32Array(pose.angularAcceleration, poseAngAcc), true,
+ "correct gamepadPose angularAcceleration");
+ is(checkValueInFloat32Array(pose.linearVelocity, poseLinVel), true,
+ "correct gamepadPose linearVelocity");
+ is(checkValueInFloat32Array(pose.linearAcceleration, poseLinAcc), true,
+ "correct gamepadPose linearAcceleration");
+ await pressButton();
+}
+
+function setFrameVisible(f, visible) {
+ SpecialPowers.wrap(f.contentWindow).browsingContext.isActive = visible;
+}
+
+function haptictest() {
+ var gamepads = navigator.getGamepads();
+ var hapticActuators = gamepads[0].hapticActuators[0];
+ hapticActuators.pulse(1, 100).then(async function(result) {
+ is(result, true, "gamepad hapticActuators test success.");
+ await GamepadService.removeGamepad(gamepad_index);
+ SimpleTest.finish();
+ });
+ // When page is background, we should stop our haptics and still
+ // can get the promise.
+ var f1 = document.getElementById('f1');
+ setFrameVisible(f1, false);
+}
+
+async function touchadd() {
+ for(var count = 0; count < touchData.length; count++) {
+ const touch = touchData[count];
+ await GamepadService.newTouch(gamepad_index, count, touch.touchId,
+ touch.surfaceId, touch.pos,
+ touch.surf);
+ }
+ await pressButton();
+}
+
+async function touchcheck() {
+ var gamepads = navigator.getGamepads();
+ var touches = gamepads[0].touchEvents;
+
+ is(touches.length, 2, " number of touches");
+
+ var count = 0;
+ touches.forEach(function(touch) {
+ is(touch.touchId, touchData[count].touchId,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, touchData[count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, touchData[count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, touchData[count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ await pressButton();
+}
+
+</script>
+<iframe id="f1" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></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 0000000000..927a4e54bb
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html
@@ -0,0 +1,19 @@
+<!-- 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 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_gamepad_frame_state_sync_iframe.html");
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync_iframe.html b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync_iframe.html
new file mode 100644
index 0000000000..d64197dce0
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync_iframe.html
@@ -0,0 +1,113 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test hidden frames</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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+ // Add a gamepad
+var index;
+
+function setFrameVisible(f, visible) {
+ SpecialPowers.wrap(f.contentWindow).browsingContext.isActive = visible;
+}
+
+var frames_loaded = 0;
+async function startTest() {
+ frames_loaded++;
+ if (frames_loaded != 2) return;
+ index = await GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ GamepadService.noHand,
+ 4, // buttons
+ 2,
+ 0,
+ 0,
+ 0);
+ await gamepad_loaded();
+}
+var f1, f2;
+async 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");
+ ok(!f1.contentWindow.gamepad.buttons[0].touched,
+ "frame 1 no button touched");
+ });
+ w2.addEventListener("gamepadbuttonup", () => {
+ ok(!f2.contentWindow.gamepad.buttons[0].pressed,
+ "frame 2 no button pressed");
+ ok(!f2.contentWindow.gamepad.buttons[0].touched,
+ "frame 2 no button touched");
+ setFrameVisible(f2, false);
+ SpecialPowers.executeSoon(async function() {
+ await GamepadService.newButtonEvent(index, 0, true, true);
+ });
+ })
+ // Now press the button, but don't release it.
+ await GamepadService.newButtonEvent(index, 0, true, 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,
+];
+
+async 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(f1.contentWindow.gamepad.buttons[0].touched, "frame 1 sees button touched");
+ ok(f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 sees button pressed");
+ ok(f2.contentWindow.gamepad.buttons[0].touched, "frame 2 sees button touched");
+
+ // Now release the button, then hide the second frame.
+ await GamepadService.newButtonEvent(index, 0, false, false);
+}
+
+async 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(f1.contentWindow.gamepad.buttons[0].touched, "frame 1 sees button touched");
+ ok(!f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 should not see button pressed");
+ ok(!f2.contentWindow.gamepad.buttons[0].touched, "frame 2 should not see button touched");
+
+ // Now unhide the second frame.
+ setFrameVisible(f2, true);
+ SpecialPowers.executeSoon(async 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");
+ ok(f2.contentWindow.gamepad.buttons[0].touched, "frame 2 sees button touched");
+ // cleanup
+ await 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 0000000000..985f001ca4
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html
@@ -0,0 +1,19 @@
+<!-- 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 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_gamepad_hidden_frame_iframe.html");
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame_iframe.html b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame_iframe.html
new file mode 100644
index 0000000000..60ad2edd01
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame_iframe.html
@@ -0,0 +1,81 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test hidden frames</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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ SpecialPowers.executeSoon(tests[testNum++]);
+});
+
+async function pressButton() {
+ await GamepadService.newButtonEvent(index, 0, true, true);
+ await GamepadService.newButtonEvent(index, 0, false, false);
+}
+
+function setFrameVisible(f, visible) {
+ SpecialPowers.wrap(f.contentWindow).browsingContext.isActive = visible;
+}
+
+var frames_loaded = 0;
+async function startTest() {
+ frames_loaded++;
+ if (frames_loaded != 2) return;
+ index = await GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ GamepadService.noHand,
+ 4, // buttons
+ 2,
+ 0,
+ 0,
+ 0);
+
+ await gamepad_loaded();
+}
+var f1, f2;
+async function gamepad_loaded() {
+ f1 = document.getElementById('f1');
+ f2 = document.getElementById('f2');
+ await 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( async () => { await pressButton(); });
+}
+
+async 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");
+ await 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_gamepad_iframe.html b/dom/tests/mochitest/gamepad/test_gamepad_iframe.html
new file mode 100644
index 0000000000..f7e29c1761
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_iframe.html
@@ -0,0 +1,81 @@
+<!-- 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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+// This should be held for the entire time the testing API is being used to
+// ensure monitoring has been started and that a single GamepadPlatformService
+// instance is used for the entire test
+var GamepadsKungFuDeathGrip = navigator.getGamepads();
+
+// 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);
+
+async function startTest() {
+ // Add a gamepad
+ index = await GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ GamepadService.noHand,
+ 4,
+ 2,
+ 0,
+ 0,
+ 0);
+ await GamepadService.newButtonEvent(index, 0, true, 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");
+}
+
+async function buttontest1() {
+ var gamepads = navigator.getGamepads();
+ is(gamepads[0].buttons[0].pressed, true, "gamepad button should register as pressed");
+ is(gamepads[0].buttons[0].touched, true, "gamepad button should register as touched");
+ await GamepadService.newButtonValueEvent(index, 1, true, true, 0.5);
+}
+
+async function buttontest2() {
+ var gamepads = navigator.getGamepads();
+ is(gamepads[0].buttons[1].pressed, true, "gamepad button should register as pressed");
+ is(gamepads[0].buttons[1].touched, true, "gamepad button should register as touched");
+ is(gamepads[0].buttons[1].value, 0.5, "gamepad button value should be 0.5");
+ await GamepadService.removeGamepad(index);
+ SimpleTest.finish();
+}
+
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin.html b/dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin.html
new file mode 100644
index 0000000000..244b2f18dc
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin.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>Test hidden frames</title>
+ <script 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">
+ // This test loads in an iframe, to ensure that the navigator instance is
+ // loaded with the correct value of the preference.
+ SimpleTest.waitForExplicitFinish();
+ setGamepadPreferenceAndCreateIframe("test_gamepad_multitouch_crossorigin_iframe.html");
+ </script>
+ </body>
+ </html>
+ \ No newline at end of file
diff --git a/dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin_iframe.html b/dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin_iframe.html
new file mode 100644
index 0000000000..de551fac06
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_gamepad_multitouch_crossorigin_iframe.html
@@ -0,0 +1,256 @@
+<!-- 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 SimpleTest = window.parent.SimpleTest;
+let SpecialPowers = window.parent.SpecialPowers;
+
+let tests = [
+ touchAdd,
+ touchcheck1,
+ touchAdd,
+ touchcheck2,
+ touchAdd,
+ touchcheck3
+];
+
+let gamepad_index;
+let testNum = 0;
+let touchData1 = [{touchId: 0, surfaceId: 0, pos: new Float32Array([-0.5, 0.5]), surf: new Float32Array([100, 100])},
+ {touchId: 1, surfaceId: 0, pos: new Float32Array([-0.1, 1.0]), surf: new Float32Array([100, 100])}];
+let touchData2 = [{touchId: 2, surfaceId: 0, pos: new Float32Array([-0.2, 0.3]), surf: new Float32Array([120, 200])},
+ {touchId: 3, surfaceId: 0, pos: new Float32Array([-0.4, 0.7]), surf: new Float32Array([120, 200])}];
+let touchData3 = [{touchId: 4, surfaceId: 0, pos: new Float32Array([-0.5, 0.6]), surf: new Float32Array([150, 100])},
+ {touchId: 5, surfaceId: 0, pos: new Float32Array([-0.3, 0.8]), surf: new Float32Array([150, 100])}];
+
+let data = [
+ touchData1,
+ touchData2,
+ touchData3
+];
+let dataNum = 0;
+
+window.addEventListener("gamepadconnected", connecthandler);
+window.addEventListener("gamepadbuttondown", function() {
+ // Wait to ensure that all frames received the button press as well.
+ ok(true, "gamepadbuttondown");
+ SpecialPowers.executeSoon(tests[testNum++]);
+});
+
+async function pressButton() {
+ await GamepadService.newButtonEvent(gamepad_index, 0, true, true);
+ await GamepadService.newButtonEvent(gamepad_index, 0, false, false);
+}
+
+let frames_loaded = 0;
+async function startTest() {
+ frames_loaded++;
+ let promise = SpecialPowers.pushPrefEnv({ "set": [
+ ["dom.gamepad.extensions.enabled", true],
+ ["dom.gamepad.extensions.lightindicator", true],
+ ["dom.gamepad.extensions.multitouch", true]] });
+ if (frames_loaded == 2) {
+ await promise;
+ // Add a gamepad
+ gamepad_index = await GamepadService.addGamepad("test gamepad", // id
+ GamepadService.standardMapping,
+ GamepadService.leftHand,
+ 4,
+ 2,
+ 1,
+ 1,
+ 2)
+ await gamepad_loaded();
+ }
+}
+
+let f1, f2;
+async function gamepad_loaded() {
+ f1 = document.getElementById('f1');
+ f2 = document.getElementById('f2');
+ await GamepadService.newButtonEvent(gamepad_index, 0, true, 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.hand, "left", "left hand");
+ is(e.gamepad.buttons.length, 4, "correct number of buttons");
+ is(e.gamepad.axes.length, 2, "correct number of axes");
+ is(e.gamepad.hapticActuators.length, 1, "correct number of haptics");
+ is(e.gamepad.lightIndicators.length, 1, "correct number of light indicators");
+ is(e.gamepad.touchEvents.length, 2, "correct number of touches");
+}
+
+function checkValueInFloat32Array(array1, array2) {
+ if (array1.length != array2.length) {
+ return false;
+ }
+ let index = 0;
+ while (index < array2.length) {
+ if (array1[index] != array2[index]) {
+ return false;
+ }
+ ++index;
+ }
+ return true;
+}
+
+function setFrameVisible(f, visible) {
+ SpecialPowers.wrap(f.contentWindow).browsingContext.isActive = visible;
+}
+
+async function touchAdd() {
+ for(let count = 0; count < data[dataNum].length; count++) {
+ const touch = data[dataNum][count];
+ await GamepadService.newTouch(gamepad_index, count, touch.touchId,
+ touch.surfaceId, touch.pos,
+ touch.surf);
+ }
+ ++dataNum;
+ await pressButton();
+}
+
+async function touchcheck1() {
+ let touches = f1.contentWindow.gamepad.touchEvents;
+ is(touches.length, touchData1.length, "f1 number of touches");
+
+ let count = 0;
+ touches.forEach(function(touch) {
+ is(touch.touchId, data[0][count].touchId,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, data[0][count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, data[0][count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, data[0][count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ touches = f2.contentWindow.gamepad.touchEvents;
+ is(touches.length, data[0].length,"f2 number of touches");
+
+ count = 0;
+ touches.forEach(function(touch) {
+ is(touch.touchId, data[0][count].touchId,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, data[0][count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, data[0][count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, data[0][count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ // Making f2 to be at the background.
+ setFrameVisible(f2, false);
+ pressButton();
+}
+
+async function touchcheck2() {
+ let touches = f1.contentWindow.gamepad.touchEvents;
+ is(touches.length, data[1].length, "f1 number of touches");
+
+ let count = 0;
+ touches.forEach(function(touch) {
+ is(touch.touchId, data[1][count].touchId,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, data[1][count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, data[1][count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, data[1][count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ touches = f2.contentWindow.gamepad.touchEvents;
+ is(touches.length, touchData1.length,"f2 number of touches");
+
+ // When f2 is at the background, it will use the previous status.
+ count = 0;
+ touches.forEach(function(touch) {
+ is(touch.touchId, data[0][count].touchId,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, data[0][count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, data[0][count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, data[0][count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ setFrameVisible(f2, true);
+ pressButton();
+}
+
+async function touchcheck3() {
+ let touches = f1.contentWindow.gamepad.touchEvents;
+ is(touches.length, touchData3.length, "f1 number of touches");
+
+ let count = 0;
+ touches.forEach(function(touch) {
+ is(touch.touchId, data[2][count].touchId,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, data[2][count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, data[2][count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, data[2][count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ touches = f2.contentWindow.gamepad.touchEvents;
+ is(touches.length, touchData3.length,"f2 number of touches");
+
+ count = 0;
+ touches.forEach(function(touch) {
+ // f2 was at the background so doesn't add touch events from data[1].
+ is(touch.touchId, data[2][count].touchId - data[1].length,
+ "correct GamepadTouch touchId");
+ is(touch.surfaceId, data[2][count].surfaceId,
+ "correct GamepadTouch surfaceId");
+ is(checkValueInFloat32Array(touch.position, data[2][count].pos), true,
+ "correct touch position");
+ is(checkValueInFloat32Array(touch.surfaceDimensions, data[2][count].surf), true,
+ "correct touch surfaceDimensions");
+
+ ++count;
+ });
+
+ SpecialPowers.executeSoon(cleanup);
+}
+
+function cleanup(){
+ SpecialPowers.executeSoon(async function() {
+ await GamepadService.removeGamepad(gamepad_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_navigator_gamepads.html b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html
new file mode 100644
index 0000000000..06dc214a45
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html
@@ -0,0 +1,19 @@
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test gamepad</title>
+ <script 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">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+setGamepadPreferenceAndCreateIframe("test_navigator_gamepads_iframe.html");
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/gamepad/test_navigator_gamepads_iframe.html b/dom/tests/mochitest/gamepad/test_navigator_gamepads_iframe.html
new file mode 100644
index 0000000000..9c98bac2d3
--- /dev/null
+++ b/dom/tests/mochitest/gamepad/test_navigator_gamepads_iframe.html
@@ -0,0 +1,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>