8000 gamepad.vibrationActuator.playEffect() should throw for invalid effec… · WebKit/WebKit@f5f6113 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5f6113

Browse files
committed
gamepad.vibrationActuator.playEffect() should throw for invalid effect parameters
https://bugs.webkit.org/show_bug.cgi?id=250409 Reviewed by Brent Fulgham. gamepad.vibrationActuator.playEffect() should throw for invalid effect parameters: - https://w3c.github.io/gamepad/extensions.html#dom-gamepadhapticactuator-playeffect (Step 1) * LayoutTests/gamepad/gamepad-vibrationActuator-playEffect-validation-expected.txt: Added. * LayoutTests/gamepad/gamepad-vibrationActuator-playEffect-validation.html: Added. * Source/WebCore/Modules/gamepad/GamepadHapticActuator.cpp: (WebCore::areEffectParametersValid): (WebCore::GamepadHapticActuator::playEffect): * Source/WebCore/testing/MockGamepad.cpp: (WebCore::MockGamepad::MockGamepad): Canonical link: https://commits.webkit.org/258752@main
1 parent 7e91513 commit f5f6113

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Tests input validation for vibrationActuator.playEffect
2+
3+
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
4+
5+
6+
PASS gamepad.vibrationActuator.canPlayEffectType('dual-rumble') is true
7+
PASS result is "complete"
8+
* Invalid input test: Negative duration
9+
PASS playEffect() threw exception: TypeError: Invalid effect parameter
10+
* Invalid input test: Negative startDelay
11+
PASS playEffect() threw exception: TypeError: Invalid effect parameter
12+
* Invalid input test: Negative weakMagnitude
8000
13+
PASS playEffect() threw exception: TypeError: Invalid effect parameter
14+
* Invalid input test: Negative strongMagnitude
15+
PASS playEffect() threw exception: TypeError: Invalid effect parameter
16+
* Invalid input test: weakMagnitude > 1
17+
PASS playEffect() threw exception: TypeError: Invalid effect parameter
18+
* Invalid input test: strongMagnitude > 1
19+
PASS playEffect() threw exception: TypeError: Invalid effect parameter
20+
PASS successfullyParsed is true
21+
22+
TEST COMPLETE
23+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<head>
2+
<script src="../resources/js-test.js"></script>
3+
<body>
4+
<script>
5+
description("Tests input validation for vibrationActuator.playEffect");
6+
jsTestIsAsync = true;
7+
8+
let gamepad = null;
9+
10+
async function shouldThrowDueToInvalidParameter(description, parameters)
11+
{
12+
debug("* Invalid input test: " + description);
13+
try {
14+
await gamepad.vibrationActuator.playEffect('dual-rumble', parameters);
15+
testFailed("playEffect() did not throw an exception()");
16+
} catch (error) {
17+
testPassed("playEffect() threw exception: " + error);
18+
}
19+
}
20+
21+
function runTest() {
22+
addEventListener("gamepadconnected", async e => {
23+
gamepad = event.gamepad;
24+
shouldBeTrue("gamepad.vibrationActuator.canPlayEffectType('dual-rumble')");
25+
try {
26+
result = await gamepad.vibrationActuator.playEffect('dual-rumble');
27+
shouldBeEqualToString("result", "complete");
28+
} catch (error) {
29+
testFailed("playEffect('dual-rumble') unexpectedly threw exception: " + error);
30+
}
31+
32+
await shouldThrowDueToInvalidParameter("Negative duration", { duration: -1 });
33+
await shouldThrowDueToInvalidParameter("Negative startDelay", { startDelay: -1 });
34+
await shouldThrowDueToInvalidParameter("Negative weakMagnitude", { weakMagnitude: -1 });
35+
await shouldThrowDueToInvalidParameter("Negative strongMagnitude", { strongMagnitude: -1 });
36+
await shouldThrowDueToInvalidParameter("weakMagnitude > 1", { weakMagnitude: 2 });
37+
await shouldThrowDueToInvalidParameter("strongMagnitude > 1", { strongMagnitude: 2 });
38+
39+
finishJSTest();
40+
});
41+
42+
testRunner.setMockGamepadDetails(0, "Test Gamepad", "", 2, 2);
43+
testRunner.setMockGamepadAxisValue(0, 0, 0.7);
44+
testRunner.setMockGamepadAxisValue(0, 1, -1.0);
45+
testRunner.setMockGamepadButtonValue(0, 0, 1.0);
46+
testRunner.setMockGamepadButtonValue(0, 1, 1.0);
47+
testRunner.connectMockGamepad(0);
48+
}
49+
50+
onload = runTest;
51+
52+
</script>
53+
</body>

Source/WebCore/Modules/gamepad/GamepadHapticActuator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ GamepadHapticActuator::GamepadHapticActuator(Gamepad& gamepad)
4949
{
5050
}
5151

52+
static bool areEffectParametersValid(GamepadHapticEffectType effectType, const GamepadEffectParameters& parameters)
53+
{
54+
if (parameters.duration < 0 || parameters.startDelay < 0)
55+
return false;
56+
57+
if (effectType == GamepadHapticEffectType::DualRumble) {
58+
if (parameters.weakMagnitude < 0 || parameters.strongMagnitude < 0 || parameters.weakMagnitude > 1 || parameters.strongMagnitude > 1)
59+
return false;
60+
}
61+
return true;
62+
}
63+
5264
GamepadHapticActuator::~GamepadHapticActuator() = default;
5365

5466
bool GamepadHapticActuator::canPlayEffectType(EffectType effectType) const
@@ -58,6 +70,10 @@ bool GamepadHapticActuator::canPlayEffectType(EffectType effectType) const
5870

5971
void GamepadHapticActuator::playEffect(Document& document, EffectType effectType, GamepadEffectParameters&& effectParameters, Ref<DeferredPromise>&& promise)
6072
{
73+
if (!areEffectParametersValid(effectType, effectParameters)) {
74+
promise->reject(Exception { TypeError, "Invalid effect parameter"_s });
75+
return;
76+
}
6177
if (!document.isFullyActive() || document.hidden() || !m_gamepad) {
6278
promise->resolve<IDLEnumeration<Result>>(Result::Preempted);
6379
return;

Source/WebCore/testing/MockGamepad.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ MockGamepad::MockGamepad(unsigned index, const String& gamepadID, const String&
3434
: PlatformGamepad(index)
3535
{
3636
m_connectTime = m_lastUpdateTime = MonotonicTime::now();
37+
m_supportedEffectTypes.add(GamepadHapticEffectType::DualRumble);
3738
updateDetails(gamepadID, mapping, axisCount, buttonCount);
3839
}
3940

0 commit comments

Comments
 (0)
0