Skip to content

Commit 304abbd

Browse files
committed
Bug 1523351 - Part 2: Add tests for GamepadTouch and GamepadLightIndicator. r=baku
Differential Revision: https://phabricator.services.mozilla.com/D29289 --HG-- extra : moz-landing-system : lando
1 parent f1193f9 commit 304abbd

15 files changed

+407
-12
lines changed

browser/components/resistfingerprinting/test/mochitest/test_hide_gamepad_info_iframe.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
GamepadService.noHand,
2020
4, // buttons
2121
2,
22+
0,
23+
0,
2224
0).then((aIndex) => new Promise((aResolve) => {
2325
// Press a button to make the gamepad visible to the page.
2426
GamepadService.newButtonEvent(aIndex, 0, true, true);

dom/gamepad/GamepadServiceTest.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ void GamepadServiceTest::DestroyPBackgroundActor() {
9090
already_AddRefed<Promise> GamepadServiceTest::AddGamepad(
9191
const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand,
9292
uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics,
93-
ErrorResult& aRv) {
93+
uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv) {
9494
if (mShuttingDown) {
9595
return nullptr;
9696
}
9797

9898
// Only VR controllers has displayID, we give 0 to the general gamepads.
9999
GamepadAdded a(nsString(aID), aMapping, aHand, 0, aNumButtons, aNumAxes,
100-
aNumHaptics);
100+
aNumHaptics, aNumLightIndicator, aNumTouchEvents);
101101
GamepadChangeEventBody body(a);
102102
GamepadChangeEvent e(0, GamepadServiceType::Standard, body);
103103

@@ -246,6 +246,40 @@ void GamepadServiceTest::NewPoseMove(
246246
mChild->SendGamepadTestEvent(id, e);
247247
}
248248

249+
void GamepadServiceTest::NewTouch(uint32_t aIndex, uint32_t aTouchArrayIndex,
250+
uint32_t aTouchId, uint8_t aSurfaceId,
251+
const Float32Array& aPos,
252+
const Nullable<Float32Array>& aSurfDim) {
253+
if (mShuttingDown) {
254+
return;
255+
}
256+
257+
GamepadTouchState touchState;
258+
touchState.touchId = aTouchId;
259+
touchState.surfaceId = aSurfaceId;
260+
const Float32Array& value = aPos;
261+
value.ComputeLengthAndData();
262+
MOZ_ASSERT(value.Length() == 2);
263+
touchState.position[0] = value.Data()[0];
264+
touchState.position[1] = value.Data()[1];
265+
266+
if (!aSurfDim.IsNull()) {
267+
const Float32Array& value = aSurfDim.Value();
268+
value.ComputeLengthAndData();
269+
MOZ_ASSERT(value.Length() == 2);
270+
touchState.surfaceDimensions[0] = value.Data()[0];
271+
touchState.surfaceDimensions[1] = value.Data()[1];
272+
touchState.isSurfaceDimensionsValid = true;
273+
}
274+
275+
GamepadTouchInformation a(aTouchArrayIndex, touchState);
276+
GamepadChangeEventBody body(a);
277+
GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body);
278+
279+
uint32_t id = ++mEventNumber;
280+
mChild->SendGamepadTestEvent(id, e);
281+
}
282+
249283
JSObject* GamepadServiceTest::WrapObject(JSContext* aCx,
250284
JS::HandleObject aGivenProto) {
251285
return GamepadServiceTest_Binding::Wrap(aCx, this, aGivenProto);

dom/gamepad/GamepadServiceTest.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ class GamepadServiceTest final : public DOMEventTargetHelper {
3333
GamepadHand LeftHand() const { return GamepadHand::Left; }
3434
GamepadHand RightHand() const { return GamepadHand::Right; }
3535

36-
already_AddRefed<Promise> AddGamepad(const nsAString& aID,
37-
GamepadMappingType aMapping,
38-
GamepadHand aHand, uint32_t aNumButtons,
39-
uint32_t aNumAxes, uint32_t aNumHaptics,
40-
ErrorResult& aRv);
36+
already_AddRefed<Promise> AddGamepad(
37+
const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand,
38+
uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics,
39+
uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv);
4140
void RemoveGamepad(uint32_t aIndex);
4241
void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
4342
bool aTouched);
@@ -50,6 +49,9 @@ class GamepadServiceTest final : public DOMEventTargetHelper {
5049
const Nullable<Float32Array>& aAngAcceleration,
5150
const Nullable<Float32Array>& aLinVelocity,
5251
const Nullable<Float32Array>& aLinAcceleration);
52+
void NewTouch(uint32_t aIndex, uint32_t aTouchArrayIndex, uint32_t aTouchId,
53+
uint8_t aSurfaceId, const Float32Array& aPos,
54+
const Nullable<Float32Array>& aSurfDim);
5355
void Shutdown();
5456

5557
static already_AddRefed<GamepadServiceTest> CreateTestService(

dom/gamepad/ipc/GamepadTestChannelParent.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ mozilla::ipc::IPCResult GamepadTestChannelParent::RecvGamepadTestEvent(
2626
LossyCopyUTF16toASCII(a.id(), gamepadID);
2727
uint32_t index = service->AddGamepad(
2828
gamepadID.get(), static_cast<GamepadMappingType>(a.mapping()), a.hand(),
29-
a.num_buttons(), a.num_axes(), a.num_haptics());
29+
a.num_buttons(), a.num_axes(), a.num_haptics(), a.num_lights(),
30+
a.num_touches());
3031
if (!mShuttingdown) {
3132
Unused << SendReplyGamepadIndex(aID, index);
3233
}
@@ -52,6 +53,11 @@ mozilla::ipc::IPCResult GamepadTestChannelParent::RecvGamepadTestEvent(
5253
service->NewPoseEvent(index, a.pose_state());
5354
return IPC_OK();
5455
}
56+
if (body.type() == GamepadChangeEventBody::TGamepadTouchInformation) {
57+
const GamepadTouchInformation& a = body.get_GamepadTouchInformation();
58+
service->NewMultiTouchEvent(index, a.index(), a.touch_state());
59+
return IPC_OK();
60+
}
5561

5662
NS_WARNING("Unknown event type.");
5763
return IPC_FAIL_NO_REASON(this);

dom/tests/mochitest/gamepad/mochitest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ support-files = test_gamepad_iframe.html
1212
support-files = test_gamepad_connect_events_iframe.html
1313
[test_gamepad_extensions.html]
1414
support-files = test_gamepad_extensions_iframe.html
15+
scheme = https
1516
[test_gamepad_frame_state_sync.html]
1617
support-files = test_gamepad_frame_state_sync_iframe.html
1718
[test_gamepad_hidden_frame.html]
1819
support-files = test_gamepad_hidden_frame_iframe.html
20+
[test_gamepad_multitouch_crossorigin.html]
21+
support-files = test_gamepad_multitouch_crossorigin_iframe.html
22+
scheme = https
1923
[test_navigator_gamepads.html]
2024
support-files = test_navigator_gamepads_iframe.html

dom/tests/mochitest/gamepad/test_check_timestamp_iframe.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
GamepadService.noHand,
3131
4,
3232
2,
33+
0,
34+
0,
3335
0).then(function(i) {
3436
index = i;
3537
// Press a button to make the gamepad visible

dom/tests/mochitest/gamepad/test_gamepad_connect_events_iframe.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
GamepadService.noHand,
3838
4, // buttons
3939
2,
40+
0,
41+
0,
4042
0).then(function(i) {
4143
gamepad_index = i;
4244
gamepad_connected()

dom/tests/mochitest/gamepad/test_gamepad_extensions_iframe.html

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
var tests = [
1818
poseadd,
1919
posecheck,
20-
haptictest
20+
touchadd,
21+
touchcheck,
22+
haptictest // Keep haptictest at the last because it will test removing gamepad from the service.
2123
];
2224
var gamepad_index = 0;
2325
var testNum = 0;
@@ -27,6 +29,8 @@
2729
var poseAngAcc = new Float32Array([-0.494, 0.476, -0.241]);
2830
var poseLinVel = new Float32Array([0.003,0.024,-0.068]);
2931
var poseLinAcc = new Float32Array([-1.211,21.427,-2.348]);
32+
var touchData = [{touchId: 0, surfaceId: 0, pos: new Float32Array([-0.5, 0.5]), surf: new Float32Array([100, 100])},
33+
{touchId: 1, surfaceId: 0, pos: new Float32Array([-0.1, 1.0]), surf: new Float32Array([100, 100])}];
3034

3135
window.addEventListener("gamepadconnected", connecthandler);
3236
window.addEventListener("gamepadbuttondown", function() {
@@ -40,14 +44,19 @@
4044
}
4145

4246
function startTest() {
43-
SpecialPowers.pushPrefEnv({ "set": [["dom.gamepad.extensions.enabled", true]] });
47+
SpecialPowers.pushPrefEnv({ "set": [
48+
["dom.gamepad.extensions.enabled", true],
49+
["dom.gamepad.extensions.lightindicator", true],
50+
["dom.gamepad.extensions.multitouch", true]] });
4451
// Add a gamepad
4552
GamepadService.addGamepad("test gamepad", // id
4653
GamepadService.standardMapping,
4754
GamepadService.leftHand,
4855
4,
4956
2,
50-
1).then(function(i) {
57+
1,
58+
1,
59+
2).then(function(i) {
5160
gamepad_index = i;
5261
// Simulate button events on the gamepad we added
5362
pressButton();
@@ -64,6 +73,8 @@
6473
is(e.gamepad.buttons.length, 4, "correct number of buttons");
6574
is(e.gamepad.axes.length, 2, "correct number of axes");
6675
is(e.gamepad.hapticActuators.length, 1, "correct number of haptics");
76+
is(e.gamepad.lightIndicators.length, 1, "correct number of light indicators");
77+
is(e.gamepad.touchEvents.length, 2, "correct number of touches");
6778
}
6879

6980
function checkValueInFloat32Array(array1, array2) {
@@ -129,6 +140,41 @@
129140
setFrameVisible(f1, false);
130141
}
131142

143+
function touchadd() {
144+
var count = 0;
145+
touchData.forEach(function(touch) {
146+
GamepadService.newTouch(gamepad_index, count, touch.touchId,
147+
touch.surfaceId, touch.pos,
148+
touch.surf);
149+
++count;
150+
});
151+
152+
pressButton();
153+
}
154+
155+
function touchcheck() {
156+
var gamepads = navigator.getGamepads();
157+
var touches = gamepads[0].touchEvents;
158+
159+
is(touches.length, 2, " number of touches");
160+
161+
var count = 0;
162+
touches.forEach(function(touch) {
163+
is(touch.touchId, touchData[count].touchId,
164+
"correct GamepadTouch touchId");
165+
is(touch.surfaceId, touchData[count].surfaceId,
166+
"correct GamepadTouch surfaceId");
167+
is(checkValueInFloat32Array(touch.position, touchData[count].pos), true,
168+
"correct touch position");
169+
is(checkValueInFloat32Array(touch.surfaceDimensions, touchData[count].surf), true,
170+
"correct touch surfaceDimensions");
171+
172+
++count;
173+
});
174+
175+
pressButton();
176+
}
177+
132178
</script>
133179
<iframe id="f1" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></iframe>
134180
</body>

dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync_iframe.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
GamepadService.noHand,
3333
4, // buttons
3434
2,
35+
0,
36+
0,
3537
0).then(function(i) {
3638
index = i;
3739
gamepad_loaded();

dom/tests/mochitest/gamepad/test_gamepad_hidden_frame_iframe.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
GamepadService.noHand,
4040
4, // buttons
4141
2,
42+
0,
43+
0,
4244
0).then(function(i) {
4345
index = i;
4446
gamepad_loaded();

dom/tests/mochitest/gamepad/test_gamepad_iframe.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
GamepadService.noHand,
4040
4,
4141
2,
42+
0,
43+
0,
4244
0).then(function(i) {
4345
index = i;
4446
// Simulate button events on the gamepad we added
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- Any copyright is dedicated to the Public Domain.
2+
- http://creativecommons.org/publicdomain/zero/1.0/ -->
3+
<!DOCTYPE HTML>
4+
<html>
5+
<head>
6+
<title>Test hidden frames</title>
7+
<script src="/tests/SimpleTest/SimpleTest.js"></script>
8+
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
9+
</head>
10+
<body>
11+
<script type="text/javascript" src="mock_gamepad.js"></script>
12+
<script class="testbody" type="text/javascript">
13+
// This test loads in an iframe, to ensure that the navigator instance is
14+
// loaded with the correct value of the preference.
15+
SimpleTest.waitForExplicitFinish();
16+
setGamepadPreferenceAndCreateIframe("test_gamepad_multitouch_crossorigin_iframe.html");
17+
</script>
18+
</body>
19+
</html>
20+

0 commit comments

Comments
 (0)