1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GamepadServiceTest.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/GamepadManager.h"
#include "mozilla/dom/GamepadPlatformService.h"
#include "mozilla/dom/GamepadServiceTestBinding.h"
#include "mozilla/dom/GamepadTestChannelChild.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/PBackgroundChild.h"
namespace mozilla::dom {
/*
* Implementation of the test service. This is just to provide a simple binding
* of the GamepadService to JavaScript via WebIDL so that we can write
* Mochitests that add and remove fake gamepads, avoiding the platform-specific
* backends.
*/
constexpr uint32_t kMaxButtons = 20;
constexpr uint32_t kMaxAxes = 10;
constexpr uint32_t kMaxHaptics = 2;
constexpr uint32_t kMaxLightIndicator = 2;
constexpr uint32_t kMaxTouchEvents = 4;
NS_IMPL_CYCLE_COLLECTION_INHERITED(GamepadServiceTest, DOMEventTargetHelper,
mWindow)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadServiceTest)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(GamepadServiceTest, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(GamepadServiceTest, DOMEventTargetHelper)
// static
already_AddRefed<GamepadServiceTest> GamepadServiceTest::CreateTestService(
nsPIDOMWindowInner* aWindow) {
MOZ_ASSERT(aWindow);
RefPtr<GamepadServiceTest> service = new GamepadServiceTest(aWindow);
service->InitPBackgroundActor();
return service.forget();
}
void GamepadServiceTest::Shutdown() {
MOZ_ASSERT(!mShuttingDown);
mShuttingDown = true;
DestroyPBackgroundActor();
mWindow = nullptr;
}
GamepadServiceTest::GamepadServiceTest(nsPIDOMWindowInner* aWindow)
: mService(GamepadManager::GetService()),
mWindow(aWindow),
mEventNumber(0),
mShuttingDown(false),
mChild(nullptr) {}
GamepadServiceTest::~GamepadServiceTest() {
MOZ_ASSERT(mPromiseList.IsEmpty());
}
void GamepadServiceTest::InitPBackgroundActor() {
MOZ_ASSERT(!mChild);
::mozilla::ipc::PBackgroundChild* actor =
::mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
if (NS_WARN_IF(!actor)) {
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
}
mChild = GamepadTestChannelChild::Create(this);
PGamepadTestChannelChild* initedChild =
actor->SendPGamepadTestChannelConstructor(mChild.get());
if (NS_WARN_IF(!initedChild)) {
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
}
}
void GamepadServiceTest::ReplyGamepadHandle(uint32_t aPromiseId,
const GamepadHandle& aHandle) {
uint32_t handleSlot = AddGamepadHandle(aHandle);
RefPtr<Promise> p;
if (!mPromiseList.Get(aPromiseId, getter_AddRefs(p))) {
MOZ_CRASH("We should always have a promise.");
}
p->MaybeResolve(handleSlot);
mPromiseList.Remove(aPromiseId);
}
void GamepadServiceTest::DestroyPBackgroundActor() {
MOZ_ASSERT(mChild);
PGamepadTestChannelChild::Send__delete__(mChild);
mChild = nullptr;
}
already_AddRefed<Promise> GamepadServiceTest::AddGamepad(
const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand,
uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics,
uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv) {
if (aNumButtons > kMaxButtons || aNumAxes > kMaxAxes ||
aNumHaptics > kMaxHaptics || aNumLightIndicator > kMaxLightIndicator ||
aNumTouchEvents > kMaxTouchEvents) {
aRv.ThrowNotSupportedError("exceeded maximum hardware dimensions");
return nullptr;
}
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
// The values here are ignored, the value just can't be zero to avoid an
// assertion
GamepadHandle gamepadHandle{1, GamepadHandleKind::GamepadPlatformManager};
// Only VR controllers has displayID, we give 0 to the general gamepads.
GamepadAdded a(nsString(aID), aMapping, aHand, 0, aNumButtons, aNumAxes,
aNumHaptics, aNumLightIndicator, aNumTouchEvents);
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
uint32_t id = ++mEventNumber;
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
already_AddRefed<Promise> GamepadServiceTest::RemoveGamepad(
uint32_t aHandleSlot, ErrorResult& aRv) {
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
GamepadHandle gamepadHandle = GetHandleInSlot(aHandleSlot);
GamepadRemoved a;
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
uint32_t id = ++mEventNumber;
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
already_AddRefed<Promise> GamepadServiceTest::NewButtonEvent(
uint32_t aHandleSlot, uint32_t aButton, bool aPressed, bool aTouched,
ErrorResult& aRv) {
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
GamepadHandle gamepadHandle = GetHandleInSlot(aHandleSlot);
GamepadButtonInformation a(aButton, aPressed ? 1.0 : 0, aPressed, aTouched);
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
uint32_t id = ++mEventNumber;
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
already_AddRefed<Promise> GamepadServiceTest::NewButtonValueEvent(
uint32_t aHandleSlot, uint32_t aButton, bool aPressed, bool aTouched,
double aValue, ErrorResult& aRv) {
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
GamepadHandle gamepadHandle = GetHandleInSlot(aHandleSlot);
GamepadButtonInformation a(aButton, aValue, aPressed, aTouched);
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
uint32_t id = ++mEventNumber;
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
already_AddRefed<Promise> GamepadServiceTest::NewAxisMoveEvent(
uint32_t aHandleSlot, uint32_t aAxis, double aValue, ErrorResult& aRv) {
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
GamepadHandle gamepadHandle = GetHandleInSlot(aHandleSlot);
GamepadAxisInformation a(aAxis, aValue);
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
uint32_t id = ++mEventNumber;
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
already_AddRefed<Promise> GamepadServiceTest::NewPoseMove(
uint32_t aHandleSlot, const Nullable<Float32Array>& aOrient,
const Nullable<Float32Array>& aPos,
const Nullable<Float32Array>& aAngVelocity,
const Nullable<Float32Array>& aAngAcceleration,
const Nullable<Float32Array>& aLinVelocity,
const Nullable<Float32Array>& aLinAcceleration, ErrorResult& aRv) {
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
GamepadHandle gamepadHandle = GetHandleInSlot(aHandleSlot);
GamepadPoseState poseState;
poseState.flags = GamepadCapabilityFlags::Cap_Orientation |
GamepadCapabilityFlags::Cap_Position |
GamepadCapabilityFlags::Cap_AngularAcceleration |
GamepadCapabilityFlags::Cap_LinearAcceleration;
if (!aOrient.IsNull()) {
DebugOnly<bool> ok = aOrient.Value().CopyDataTo(poseState.orientation);
MOZ_ASSERT(
ok, "aOrient.Value().Length() != ArrayLength(poseState.orientation)");
poseState.isOrientationValid = true;
}
if (!aPos.IsNull()) {
DebugOnly<bool> ok = aPos.Value().CopyDataTo(poseState.position);
MOZ_ASSERT(ok, "aPos.Value().Length() != ArrayLength(poseState.position)");
poseState.isPositionValid = true;
}
if (!aAngVelocity.IsNull()) {
DebugOnly<bool> ok =
aAngVelocity.Value().CopyDataTo(poseState.angularVelocity);
MOZ_ASSERT(ok,
"aAngVelocity.Value().Length() != "
"ArrayLength(poseState.angularVelocity)");
}
if (!aAngAcceleration.IsNull()) {
DebugOnly<bool> ok =
aAngAcceleration.Value().CopyDataTo(poseState.angularAcceleration);
MOZ_ASSERT(ok,
"aAngAcceleration.Value().Length() != "
"ArrayLength(poseState.angularAcceleration)");
}
if (!aLinVelocity.IsNull()) {
DebugOnly<bool> ok =
aLinVelocity.Value().CopyDataTo(poseState.linearVelocity);
MOZ_ASSERT(ok,
"aLinVelocity.Value().Length() != "
"ArrayLength(poseState.linearVelocity)");
}
if (!aLinAcceleration.IsNull()) {
DebugOnly<bool> ok =
aLinAcceleration.Value().CopyDataTo(poseState.linearAcceleration);
MOZ_ASSERT(ok,
"aLinAcceleration.Value().Length() != "
"ArrayLength(poseState.linearAcceleration)");
}
GamepadPoseInformation a(poseState);
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
uint32_t id = ++mEventNumber;
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
already_AddRefed<Promise> GamepadServiceTest::NewTouch(
uint32_t aHandleSlot, uint32_t aTouchArrayIndex, uint32_t aTouchId,
uint8_t aSurfaceId, const Float32Array& aPos,
const Nullable<Float32Array>& aSurfDim, ErrorResult& aRv) {
if (mShuttingDown) {
aRv.ThrowInvalidStateError("Shutting down");
return nullptr;
}
GamepadHandle gamepadHandle = GetHandleInSlot(aHandleSlot);
GamepadTouchState touchState;
touchState.touchId = aTouchId;
touchState.surfaceId = aSurfaceId;
DebugOnly<bool> ok = aPos.CopyDataTo(touchState.position);
MOZ_ASSERT(ok, "aPos.Length() != ArrayLength(touchState.position)");
if (!aSurfDim.IsNull()) {
ok = aSurfDim.Value().CopyDataTo(touchState.surfaceDimensions);
MOZ_ASSERT(
ok, "aSurfDim.Length() != ArrayLength(touchState.surfaceDimensions)");
touchState.isSurfaceDimensionsValid = true;
}
GamepadTouchInformation a(aTouchArrayIndex, touchState);
GamepadChangeEventBody body(a);
GamepadChangeEvent e(gamepadHandle, body);
uint32_t id = ++mEventNumber;
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
if (aRv.Failed()) {
return nullptr;
}
MOZ_ASSERT(!mPromiseList.Contains(id));
mPromiseList.InsertOrUpdate(id, RefPtr{p});
mChild->SendGamepadTestEvent(id, e);
return p.forget();
}
JSObject* GamepadServiceTest::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return GamepadServiceTest_Binding::Wrap(aCx, this, aGivenProto);
}
uint32_t GamepadServiceTest::AddGamepadHandle(GamepadHandle aHandle) {
uint32_t handleSlot = mGamepadHandles.Length();
mGamepadHandles.AppendElement(aHandle);
return handleSlot;
}
void GamepadServiceTest::RemoveGamepadHandle(uint32_t aHandleSlot) {
MOZ_ASSERT(aHandleSlot < mGamepadHandles.Length());
return mGamepadHandles.RemoveElementAt(aHandleSlot);
}
GamepadHandle GamepadServiceTest::GetHandleInSlot(uint32_t aHandleSlot) const {
MOZ_ASSERT(aHandleSlot < mGamepadHandles.Length());
return mGamepadHandles.ElementAt(aHandleSlot);
}
} // namespace mozilla::dom
|