Files
ladybird/Tests/LibWeb/Text/input/GamepadAPI/gamepad-is-available-in-new-navigables.html
Zaggy1024 e8c9a08efe Tests: Disconnect the virtual gamepad in the navigables gamepad test
Failing to disconnect the gamepad would cause subsequent gamepad tests
to fail if they ran in the same WebView.
2026-01-06 12:14:22 +01:00

58 lines
2.0 KiB
HTML

<!DOCTYPE html>
<script src="../include.js"></script>
<script src="gamepad-helper.js"></script>
<script>
asyncTest(async (done) => {
const gamepad = internals.connectVirtualGamepad();
await handleSDLInputEvents();
listenForGamepadConnected();
println(`Before pressing button in top level: ${getStringifiedGamepads()}`);
gamepad.setButton(gamepad.buttons[0], true);
await handleSDLInputEvents();
println(`After pressing button in top level: ${getStringifiedGamepads()}`);
gamepad.setButton(gamepad.buttons[0], false);
await handleSDLInputEvents();
const iframe = document.createElement("iframe");
iframe.onload = async () => {
const sendMessageAndWait = (message) => {
return new Promise((resolve) => {
window.onmessage = ({ data }) => {
resolve(data);
};
iframe.contentWindow.postMessage(message, "*");
});
};
println(`Gamepad should not be immediately exposed in the new navigable: ${await sendMessageAndWait("getGamepads")}`);
// New navigables will automatically connect to available gamepads, otherwise this wouldn't do anything.
gamepad.setButton(gamepad.buttons[0], true);
await handleSDLInputEvents();
println(`Gamepad is now exposed in the new navigable: ${await sendMessageAndWait("getGamepads")}`);
gamepad.disconnect();
done();
};
iframe.srcdoc = `
\u003cscript src="gamepad-helper.js"\u003e\u003c/script\u003e
\u003cscript\u003e
window.onmessage = async ({ data }) => {
switch (data) {
case 'getGamepads':
window.parent.postMessage(getStringifiedGamepads(), '*');
break;
default:
break;
}
};
\u003c/script\u003e`;
document.body.appendChild(iframe);
});
</script>