mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-30 11:37:16 +02:00
LibWeb: Add tests for Gamepad API by utilising virtual SDL3 joysticks
This commit is contained in:
committed by
Andreas Kling
parent
74e0483ea5
commit
9adf27f009
Notes:
github-actions[bot]
2025-09-01 19:11:51 +00:00
Author: https://github.com/Lubrsi Commit: https://github.com/LadybirdBrowser/ladybird/commit/9adf27f0099 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5902 Reviewed-by: https://github.com/gmta Reviewed-by: https://github.com/trflynn89
68
Tests/LibWeb/Text/input/GamepadAPI/gamepad-iframe.html
Normal file
68
Tests/LibWeb/Text/input/GamepadAPI/gamepad-iframe.html
Normal file
@@ -0,0 +1,68 @@
|
||||
<!DOCTYPE html>
|
||||
<iframe id="test-iframe" srcdoc="
|
||||
<script src='gamepad-helper.js'></script>
|
||||
<script>
|
||||
window.onmessage = async ({ data }) => {
|
||||
switch (data) {
|
||||
case 'getGamepads':
|
||||
window.parent.postMessage(getStringifiedGamepads(), '*');
|
||||
break;
|
||||
case 'rumbleGamepad': {
|
||||
const navGamepad = navigator.getGamepads()[0];
|
||||
await navGamepad.vibrationActuator.playEffect('dual-rumble', { duration: 1, weakMagnitude: 0.5, strongMagnitude: 0.5 });
|
||||
await handleSDLInputEvents();
|
||||
await navGamepad.vibrationActuator.playEffect('trigger-rumble', { duration: 1, leftTrigger: 0.5, rightTrigger: 0.5 });
|
||||
await handleSDLInputEvents();
|
||||
window.parent.postMessage('done', '*');
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
"></iframe>
|
||||
<script src="../include.js"></script>
|
||||
<script src="gamepad-helper.js"></script>
|
||||
<script>
|
||||
asyncTest(async (done) => {
|
||||
const testIframe = document.getElementById("test-iframe");
|
||||
|
||||
const sendMessageAndWait = (message) => {
|
||||
return new Promise((resolve) => {
|
||||
window.onmessage = ({ data }) => {
|
||||
resolve(data);
|
||||
};
|
||||
|
||||
testIframe.contentWindow.postMessage(message, "*");
|
||||
});
|
||||
};
|
||||
|
||||
const gamepad = internals.connectVirtualGamepad();
|
||||
await handleSDLInputEvents();
|
||||
listenForGamepadConnected();
|
||||
|
||||
println(`Before pressing a button: ${await sendMessageAndWait("getGamepads")}`);
|
||||
gamepad.setButton(gamepad.buttons[0], true);
|
||||
await handleSDLInputEvents();
|
||||
println(`After pressing a button: ${await sendMessageAndWait("getGamepads")}`);
|
||||
|
||||
const shortMax = 32628;
|
||||
const axisValueAsInt = Math.floor(0.8 * shortMax);
|
||||
println(`Before moving axis ${gamepad.axes[0]} to 0.8 (${axisValueAsInt}): ${await sendMessageAndWait("getGamepads")}`);
|
||||
gamepad.setAxis(gamepad.axes[0], axisValueAsInt);
|
||||
await handleSDLInputEvents();
|
||||
println(`After moving axis ${gamepad.axes[0]} to 0.8 (${axisValueAsInt}): ${await sendMessageAndWait("getGamepads")}`);
|
||||
|
||||
await sendMessageAndWait("rumbleGamepad");
|
||||
println(`Received dual rumble effects: ${JSON.stringify(gamepad.getReceivedRumbleEffects())}`);
|
||||
println(`Received trigger rumble effects: ${JSON.stringify(gamepad.getReceivedRumbleTriggerEffects())}`);
|
||||
|
||||
listenForGamepadDisconnected();
|
||||
gamepad.disconnect();
|
||||
await handleSDLInputEvents();
|
||||
println(`After disconnecting gamepad: ${await sendMessageAndWait("getGamepads")}`);
|
||||
|
||||
done();
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user