summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/gamepad
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/gamepad')
-rw-r--r--testing/web-platform/tests/gamepad/META.yml5
-rw-r--r--testing/web-platform/tests/gamepad/events-manual.html34
-rw-r--r--testing/web-platform/tests/gamepad/feature-policy-gamepad.html8
-rw-r--r--testing/web-platform/tests/gamepad/gamepad-default-feature-policy.https.sub.html44
-rw-r--r--testing/web-platform/tests/gamepad/gamepad-secure-context.html25
-rw-r--r--testing/web-platform/tests/gamepad/gamepad-supported-by-feature-policy.html11
-rw-r--r--testing/web-platform/tests/gamepad/getgamepads-polling-manual.html30
-rw-r--r--testing/web-platform/tests/gamepad/idlharness-extensions.https.window.js12
-rw-r--r--testing/web-platform/tests/gamepad/idlharness-manual.html37
-rw-r--r--testing/web-platform/tests/gamepad/idlharness.https.window.js18
-rw-r--r--testing/web-platform/tests/gamepad/not-fully-active.https.html21
-rw-r--r--testing/web-platform/tests/gamepad/timestamp-manual.html58
12 files changed, 303 insertions, 0 deletions
diff --git a/testing/web-platform/tests/gamepad/META.yml b/testing/web-platform/tests/gamepad/META.yml
new file mode 100644
index 0000000000..f9acc8b9cf
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/META.yml
@@ -0,0 +1,5 @@
+spec: https://w3c.github.io/gamepad/
+suggested_reviewers:
+ - marcoscaceres
+ - nondebug
+ - reillyeon
diff --git a/testing/web-platform/tests/gamepad/events-manual.html b/testing/web-platform/tests/gamepad/events-manual.html
new file mode 100644
index 0000000000..cbade65a8f
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/events-manual.html
@@ -0,0 +1,34 @@
+<!doctype html>
+<html>
+<head>
+<title>Manual Gamepad events tests</title>
+<link rel="help" href="https://w3c.github.io/gamepad/#the-gamepadconnected-event">
+<link rel="help" href="https://w3c.github.io/gamepad/#the-gamepaddisconnected-event">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+setup({explicit_timeout: true});
+
+function set_instructions(text) {
+ document.getElementById("instructions").textContent = text;
+}
+
+var index = -1;
+addEventListener("gamepadconnected", function (e) {
+ assert_equals(index, -1, "Too many connected events?");
+ assert_class_string(e, "GamepadEvent");
+ assert_class_string(e.gamepad, "Gamepad");
+ index = e.gamepad.index;
+ set_instructions("Found a gamepad. Now disconnect the gamepad.");
+});
+addEventListener("gamepaddisconnected", function (e) {
+ assert_class_string(e, "GamepadEvent");
+ assert_equals(e.gamepad.index, index);
+ done();
+});
+</script>
+</head>
+<body>
+<p id="instructions">This test requires a gamepad. Connect one and press any button to start the test.</p>
+</body>
+</html>
diff --git a/testing/web-platform/tests/gamepad/feature-policy-gamepad.html b/testing/web-platform/tests/gamepad/feature-policy-gamepad.html
new file mode 100644
index 0000000000..2747a04b34
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/feature-policy-gamepad.html
@@ -0,0 +1,8 @@
+<script>
+ try {
+ const test = navigator.getGamepads();
+ if (test) window.parent.postMessage({ enabled: true }, "*");
+ } catch (err) {
+ window.parent.postMessage({ enabled: false }, "*");
+ }
+</script>
diff --git a/testing/web-platform/tests/gamepad/gamepad-default-feature-policy.https.sub.html b/testing/web-platform/tests/gamepad/gamepad-default-feature-policy.https.sub.html
new file mode 100644
index 0000000000..f4b4d75947
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/gamepad-default-feature-policy.https.sub.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<body>
+ <script src=/resources/testharness.js></script>
+ <script src=/resources/testharnessreport.js></script>
+ <script src=/feature-policy/resources/featurepolicy.js></script>
+ <script src=util/utils.js></script>
+ <script>
+ "use strict";
+ const relative_path = "/gamepad/feature-policy-gamepad.html";
+ const base_src = "/feature-policy/resources/redirect-on-load.html#";
+ const sub = "https://{{domains[www]}}:{{ports[https][0]}}";
+ const same_origin_src = base_src + relative_path;
+ const cross_origin_src = sub + relative_path;
+ const header = 'Feature-Policy allow="gamepad"';
+
+ async_test((t) => {
+ test_feature_availability(
+ "getGamepads()",
+ t,
+ same_origin_src,
+ expect_feature_available_default
+ );
+ }, header + " allows same-origin by default.");
+
+ async_test((t) => {
+ test_feature_availability(
+ "getGamepads()",
+ t,
+ cross_origin_src,
+ expect_feature_available_default,
+ "gamepad"
+ );
+ }, header + " allows cross-origin with feature allowed.");
+
+ async_test((t) => {
+ test_feature_availability(
+ "getGamepads()",
+ t,
+ cross_origin_src,
+ expect_feature_available_default
+ );
+ }, header + " allows cross-origin by default.");
+ </script>
+</body>
diff --git a/testing/web-platform/tests/gamepad/gamepad-secure-context.html b/testing/web-platform/tests/gamepad/gamepad-secure-context.html
new file mode 100644
index 0000000000..28641220fd
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/gamepad-secure-context.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>Gamepad Test: non-secure contexts</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+ test(() => {
+ // To avoid breaking legacy apps getGamepads() should always be exposed on
+ // the navigator object, even in non-secure contexts.
+ assert_true("getGamepads" in Navigator.prototype, "in prototype");
+ assert_true("getGamepads" in navigator, "on instance");
+
+ // Calling navigator.getGamepads() should return an empty array even if
+ // there are connected gamepads.
+ assert_array_equals(navigator.getGamepads(), [],
+ "getGamepads returns an empty array");
+
+ // Gamepad API types should not be exposed in non-secure contexts.
+ assert_equals(typeof(Gamepad), "undefined", "Gamepad type is undefined");
+ assert_equals(typeof(GamepadButton), "undefined",
+ "GamepadButton type is undefined");
+ assert_equals(typeof(GamepadEvent), "undefined",
+ "GamepadEvent type is undefined");
+ }, "Gamepad API is not exposed in non-secure contexts");
+</script>
diff --git a/testing/web-platform/tests/gamepad/gamepad-supported-by-feature-policy.html b/testing/web-platform/tests/gamepad/gamepad-supported-by-feature-policy.html
new file mode 100644
index 0000000000..a688a380a7
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/gamepad-supported-by-feature-policy.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<title>Test that gamepad is advertised in the feature list</title>
+<link rel="help" href="https://w3c.github.io/webappsec-feature-policy/#dom-featurepolicy-features">
+<link rel="help" href="https://github.com/w3c/encrypted-media/pull/432">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+test(() => {
+ assert_in_array('gamepad', document.featurePolicy.features());
+}, 'document.featurePolicy.features should advertise gamepad.');
+</script> \ No newline at end of file
diff --git a/testing/web-platform/tests/gamepad/getgamepads-polling-manual.html b/testing/web-platform/tests/gamepad/getgamepads-polling-manual.html
new file mode 100644
index 0000000000..4330fec5a5
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/getgamepads-polling-manual.html
@@ -0,0 +1,30 @@
+<!doctype html>
+<html>
+<head>
+<title>Manual Gamepad getGamepads polling tests</title>
+<link rel="help" href="https://w3c.github.io/gamepad/#navigator-interface-extension">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+setup({explicit_timeout: true});
+// Poll until we see a gamepad.
+var id = setInterval(function() {
+ var gamepads = navigator.getGamepads();
+ var found = null;
+ for (var i = 0; i < gamepads.length; i++) {
+ if (gamepads[i]) {
+ found = gamepads[i];
+ break;
+ }
+ }
+ if (found) {
+ clearInterval(id);
+ done();
+ }
+}, 15);
+</script>
+</head>
+<body>
+<p>This test requires a gamepad. Connect one and press any button to start the test.</p>
+</body>
+</html>
diff --git a/testing/web-platform/tests/gamepad/idlharness-extensions.https.window.js b/testing/web-platform/tests/gamepad/idlharness-extensions.https.window.js
new file mode 100644
index 0000000000..dcf76878fc
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/idlharness-extensions.https.window.js
@@ -0,0 +1,12 @@
+// META: script=/resources/WebIDLParser.js
+// META: script=/resources/idlharness.js
+// META: timeout=long
+
+// https://w3c.github.io/gamepad/extensions.html
+
+'use strict';
+
+idl_test(
+ ['gamepad-extensions'],
+ ['gamepad'],
+);
diff --git a/testing/web-platform/tests/gamepad/idlharness-manual.html b/testing/web-platform/tests/gamepad/idlharness-manual.html
new file mode 100644
index 0000000000..2964ee550d
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/idlharness-manual.html
@@ -0,0 +1,37 @@
+<!doctype html>
+<title>Manual Gamepad IDL tests</title>
+<link rel="help" href="https://w3c.github.io/gamepad/">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/resources/WebIDLParser.js"></script>
+<script src="/resources/idlharness.js"></script>
+<p id="instructions">This test requires a gamepad. Connect one and press any button to start the test.</p>
+<script>
+'use strict';
+
+idl_test(
+ ['gamepad'],
+ ['dom', 'html'],
+ (idl_array, t) => {
+ idl_array.add_objects({
+ Gamepad: ['gamepad'],
+ GamepadButton: ['gamepadButton'],
+ GamepadEvent: ['event'],
+ Navigator: ['navigator']
+ });
+
+ const eventWatcher = new EventWatcher(t, window, "gamepadconnected");
+ const gamepadConnected = eventWatcher.wait_for("gamepadconnected")
+ .then(e => {
+ self.event = e;
+ self.gamepad = e.gamepad;
+ self.gamepadButton = e.gamepad.buttons[0];
+ });
+
+ const timeout = new Promise((_, reject) => {
+ t.step_timeout(() => { reject('Timed out waiting for gamepad'); }, 5000);
+ });
+ return Promise.race([gamepadConnected, timeout]);
+ }
+);
+</script>
diff --git a/testing/web-platform/tests/gamepad/idlharness.https.window.js b/testing/web-platform/tests/gamepad/idlharness.https.window.js
new file mode 100644
index 0000000000..b15bf1f0ae
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/idlharness.https.window.js
@@ -0,0 +1,18 @@
+// META: script=/resources/WebIDLParser.js
+// META: script=/resources/idlharness.js
+// META: timeout=long
+
+// https://w3c.github.io/gamepad/
+
+'use strict';
+
+idl_test(
+ ['gamepad'],
+ ['html', 'dom'],
+ idl_array => {
+ idl_array.add_objects({
+ GamepadEvent: ['new GamepadEvent("gamepad")'],
+ Navigator: ['navigator']
+ });
+ }
+);
diff --git a/testing/web-platform/tests/gamepad/not-fully-active.https.html b/testing/web-platform/tests/gamepad/not-fully-active.https.html
new file mode 100644
index 0000000000..500afef3d2
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/not-fully-active.https.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>Gamepad tests for non-fully-active document</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<body>
+<script>
+ promise_test(async () => {
+ const iframe = document.createElement('iframe');
+ iframe.srcdoc = `<h1>test</h1>`
+ await new Promise(resolve => {
+ iframe.onload = resolve;
+ document.body.appendChild(iframe);
+ });
+ const navigator = iframe.contentWindow.navigator;
+ iframe.remove();
+ const gamepads = navigator.getGamepads();
+ assert_true(Array.isArray(gamepads), 'getGamepads should return an array');
+ assert_equals(gamepads.length, 0, "No gamepads should be available");
+ }, "calling getGamepads() in a non-fully-active document");
+</script>
diff --git a/testing/web-platform/tests/gamepad/timestamp-manual.html b/testing/web-platform/tests/gamepad/timestamp-manual.html
new file mode 100644
index 0000000000..a2808dd0e0
--- /dev/null
+++ b/testing/web-platform/tests/gamepad/timestamp-manual.html
@@ -0,0 +1,58 @@
+<!doctype html>
+<html>
+<head>
+<title>Manual Gamepad timestamp tests</title>
+<link rel="help" href="https://w3c.github.io/gamepad/#dom-gamepad-timestamp">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+setup({explicit_timeout: true});
+
+function set_instructions(text) {
+ document.getElementById("instructions").textContent = text;
+}
+
+var index = -1;
+var lastTimestamp = performance.now();
+var id = -1;
+addEventListener("gamepadconnected", function (e) {
+ assert_equals(index, -1, "Too many connected events?");
+ index = e.gamepad.index;
+ assert_greater_than(e.gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
+ lastTimestamp = e.gamepad.timestamp;
+ set_instructions("Found a gamepad. Now release the button you pressed and press it again.");
+ // There may not be a button pressed here, so handle it cleanly either way.
+ if (e.gamepad.buttons.some(function (b) { return b.pressed; })) {
+ id = setInterval(waitForButtonRelease, 15);
+ } else {
+ id = setInterval(waitForButtonPress, 15);
+ }
+});
+
+function waitForButtonRelease() {
+ var gamepad = navigator.getGamepads()[index];
+ assert_true(!!gamepad);
+ if (gamepad.buttons.every(function (b) { return !b.pressed; })) {
+ assert_greater_than(gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
+ lastTimestamp = gamepad.timestamp;
+ clearInterval(id);
+ id = setInterval(waitForButtonPress, 15);
+ }
+}
+
+function waitForButtonPress() {
+ var gamepad = navigator.getGamepads()[index];
+ assert_true(!!gamepad);
+ if (gamepad.buttons.some(function (b) { return b.pressed; })) {
+ assert_greater_than(gamepad.timestamp, lastTimestamp, "timestamp should be increasing");
+ clearInterval(id);
+ done();
+ }
+}
+
+</script>
+</head>
+<body>
+<p id="instructions">This test requires a gamepad. Connect one and press any button to start the test.</p>
+</body>
+</html>