/src/mozilla-central/dom/gamepad/GamepadServiceTest.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "GamepadServiceTest.h" |
8 | | |
9 | | #include "mozilla/ErrorResult.h" |
10 | | #include "mozilla/Unused.h" |
11 | | |
12 | | #include "mozilla/dom/GamepadManager.h" |
13 | | #include "mozilla/dom/GamepadPlatformService.h" |
14 | | #include "mozilla/dom/GamepadServiceTestBinding.h" |
15 | | #include "mozilla/dom/GamepadTestChannelChild.h" |
16 | | |
17 | | #include "mozilla/ipc/BackgroundChild.h" |
18 | | #include "mozilla/ipc/PBackgroundChild.h" |
19 | | |
20 | | #include "mozilla/Unused.h" |
21 | | |
22 | | #include "nsIObserver.h" |
23 | | #include "nsIObserverService.h" |
24 | | |
25 | | namespace mozilla { |
26 | | namespace dom { |
27 | | |
28 | | /* |
29 | | * Implementation of the test service. This is just to provide a simple binding |
30 | | * of the GamepadService to JavaScript via WebIDL so that we can write Mochitests |
31 | | * that add and remove fake gamepads, avoiding the platform-specific backends. |
32 | | */ |
33 | | |
34 | | NS_IMPL_CYCLE_COLLECTION_INHERITED(GamepadServiceTest, DOMEventTargetHelper, |
35 | | mWindow) |
36 | | |
37 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadServiceTest) |
38 | 0 | NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
39 | | |
40 | | NS_IMPL_ADDREF_INHERITED(GamepadServiceTest, DOMEventTargetHelper) |
41 | | NS_IMPL_RELEASE_INHERITED(GamepadServiceTest, DOMEventTargetHelper) |
42 | | |
43 | | // static |
44 | | already_AddRefed<GamepadServiceTest> |
45 | | GamepadServiceTest::CreateTestService(nsPIDOMWindowInner* aWindow) |
46 | 0 | { |
47 | 0 | MOZ_ASSERT(aWindow); |
48 | 0 | RefPtr<GamepadServiceTest> service = new GamepadServiceTest(aWindow); |
49 | 0 | service->InitPBackgroundActor(); |
50 | 0 | return service.forget(); |
51 | 0 | } |
52 | | |
53 | | void |
54 | | GamepadServiceTest::Shutdown() |
55 | 0 | { |
56 | 0 | MOZ_ASSERT(!mShuttingDown); |
57 | 0 | mShuttingDown = true; |
58 | 0 | DestroyPBackgroundActor(); |
59 | 0 | mWindow = nullptr; |
60 | 0 | } |
61 | | |
62 | | GamepadServiceTest::GamepadServiceTest(nsPIDOMWindowInner* aWindow) |
63 | | : mService(GamepadManager::GetService()), |
64 | | mWindow(aWindow), |
65 | | mEventNumber(0), |
66 | | mShuttingDown(false), |
67 | | mChild(nullptr) |
68 | 0 | {} |
69 | | |
70 | 0 | GamepadServiceTest::~GamepadServiceTest() {} |
71 | | |
72 | | void |
73 | | GamepadServiceTest::InitPBackgroundActor() |
74 | 0 | { |
75 | 0 | MOZ_ASSERT(!mChild); |
76 | 0 |
|
77 | 0 | PBackgroundChild* actor = BackgroundChild::GetOrCreateForCurrentThread(); |
78 | 0 | if (NS_WARN_IF(!actor)) { |
79 | 0 | MOZ_CRASH("Failed to create a PBackgroundChild actor!"); |
80 | 0 | } |
81 | 0 |
|
82 | 0 | mChild = new GamepadTestChannelChild(); |
83 | 0 | PGamepadTestChannelChild* initedChild = |
84 | 0 | actor->SendPGamepadTestChannelConstructor(mChild); |
85 | 0 | if (NS_WARN_IF(!initedChild)) { |
86 | 0 | MOZ_CRASH("Failed to create a PBackgroundChild actor!"); |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | | void |
91 | | GamepadServiceTest::DestroyPBackgroundActor() |
92 | 0 | { |
93 | 0 | mChild->SendShutdownChannel(); |
94 | 0 | mChild = nullptr; |
95 | 0 | } |
96 | | |
97 | | already_AddRefed<Promise> |
98 | | GamepadServiceTest::AddGamepad(const nsAString& aID, |
99 | | GamepadMappingType aMapping, |
100 | | GamepadHand aHand, |
101 | | uint32_t aNumButtons, |
102 | | uint32_t aNumAxes, |
103 | | uint32_t aNumHaptics, |
104 | | ErrorResult& aRv) |
105 | 0 | { |
106 | 0 | if (mShuttingDown) { |
107 | 0 | return nullptr; |
108 | 0 | } |
109 | 0 | |
110 | 0 | // Only VR controllers has displayID, we give 0 to the general gamepads. |
111 | 0 | GamepadAdded a(nsString(aID), |
112 | 0 | aMapping, aHand, 0, |
113 | 0 | aNumButtons, aNumAxes, aNumHaptics); |
114 | 0 | GamepadChangeEventBody body(a); |
115 | 0 | GamepadChangeEvent e(0, GamepadServiceType::Standard, body); |
116 | 0 |
|
117 | 0 | RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv); |
118 | 0 | if (aRv.Failed()) { |
119 | 0 | return nullptr; |
120 | 0 | } |
121 | 0 | |
122 | 0 | uint32_t id = ++mEventNumber; |
123 | 0 |
|
124 | 0 | mChild->AddPromise(id, p); |
125 | 0 | mChild->SendGamepadTestEvent(id, e); |
126 | 0 |
|
127 | 0 | return p.forget(); |
128 | 0 | } |
129 | | |
130 | | void |
131 | | GamepadServiceTest::RemoveGamepad(uint32_t aIndex) |
132 | 0 | { |
133 | 0 | if (mShuttingDown) { |
134 | 0 | return; |
135 | 0 | } |
136 | 0 | |
137 | 0 | GamepadRemoved a; |
138 | 0 | GamepadChangeEventBody body(a); |
139 | 0 | GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body); |
140 | 0 |
|
141 | 0 | uint32_t id = ++mEventNumber; |
142 | 0 | mChild->SendGamepadTestEvent(id, e); |
143 | 0 | } |
144 | | |
145 | | void |
146 | | GamepadServiceTest::NewButtonEvent(uint32_t aIndex, |
147 | | uint32_t aButton, |
148 | | bool aTouched, |
149 | | bool aPressed) |
150 | 0 | { |
151 | 0 | if (mShuttingDown) { |
152 | 0 | return; |
153 | 0 | } |
154 | 0 | |
155 | 0 | GamepadButtonInformation a(aButton, aPressed ? 1.0 : 0, aPressed, aTouched); |
156 | 0 | GamepadChangeEventBody body(a); |
157 | 0 | GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body); |
158 | 0 |
|
159 | 0 | uint32_t id = ++mEventNumber; |
160 | 0 | mChild->SendGamepadTestEvent(id, e); |
161 | 0 | } |
162 | | |
163 | | void |
164 | | GamepadServiceTest::NewButtonValueEvent(uint32_t aIndex, |
165 | | uint32_t aButton, |
166 | | bool aPressed, |
167 | | bool aTouched, |
168 | | double aValue) |
169 | 0 | { |
170 | 0 | if (mShuttingDown) { |
171 | 0 | return; |
172 | 0 | } |
173 | 0 | |
174 | 0 | GamepadButtonInformation a(aButton, aValue, aPressed, aTouched); |
175 | 0 | GamepadChangeEventBody body(a); |
176 | 0 | GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body); |
177 | 0 |
|
178 | 0 | uint32_t id = ++mEventNumber; |
179 | 0 | mChild->SendGamepadTestEvent(id, e); |
180 | 0 | } |
181 | | |
182 | | void |
183 | | GamepadServiceTest::NewAxisMoveEvent(uint32_t aIndex, |
184 | | uint32_t aAxis, |
185 | | double aValue) |
186 | 0 | { |
187 | 0 | if (mShuttingDown) { |
188 | 0 | return; |
189 | 0 | } |
190 | 0 | |
191 | 0 | GamepadAxisInformation a(aAxis, aValue); |
192 | 0 | GamepadChangeEventBody body(a); |
193 | 0 | GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body); |
194 | 0 |
|
195 | 0 | uint32_t id = ++mEventNumber; |
196 | 0 | mChild->SendGamepadTestEvent(id, e); |
197 | 0 | } |
198 | | |
199 | | void |
200 | | GamepadServiceTest::NewPoseMove(uint32_t aIndex, |
201 | | const Nullable<Float32Array>& aOrient, |
202 | | const Nullable<Float32Array>& aPos, |
203 | | const Nullable<Float32Array>& aAngVelocity, |
204 | | const Nullable<Float32Array>& aAngAcceleration, |
205 | | const Nullable<Float32Array>& aLinVelocity, |
206 | | const Nullable<Float32Array>& aLinAcceleration) |
207 | 0 | { |
208 | 0 | if (mShuttingDown) { |
209 | 0 | return; |
210 | 0 | } |
211 | 0 | |
212 | 0 | GamepadPoseState poseState; |
213 | 0 | poseState.flags = GamepadCapabilityFlags::Cap_Orientation | |
214 | 0 | GamepadCapabilityFlags::Cap_Position | |
215 | 0 | GamepadCapabilityFlags::Cap_AngularAcceleration | |
216 | 0 | GamepadCapabilityFlags::Cap_LinearAcceleration; |
217 | 0 | if (!aOrient.IsNull()) { |
218 | 0 | const Float32Array& value = aOrient.Value(); |
219 | 0 | value.ComputeLengthAndData(); |
220 | 0 | MOZ_ASSERT(value.Length() == 4); |
221 | 0 | poseState.orientation[0] = value.Data()[0]; |
222 | 0 | poseState.orientation[1] = value.Data()[1]; |
223 | 0 | poseState.orientation[2] = value.Data()[2]; |
224 | 0 | poseState.orientation[3] = value.Data()[3]; |
225 | 0 | poseState.isOrientationValid = true; |
226 | 0 | } |
227 | 0 | if (!aPos.IsNull()) { |
228 | 0 | const Float32Array& value = aPos.Value(); |
229 | 0 | value.ComputeLengthAndData(); |
230 | 0 | MOZ_ASSERT(value.Length() == 3); |
231 | 0 | poseState.position[0] = value.Data()[0]; |
232 | 0 | poseState.position[1] = value.Data()[1]; |
233 | 0 | poseState.position[2] = value.Data()[2]; |
234 | 0 | poseState.isPositionValid = true; |
235 | 0 | } |
236 | 0 | if (!aAngVelocity.IsNull()) { |
237 | 0 | const Float32Array& value = aAngVelocity.Value(); |
238 | 0 | value.ComputeLengthAndData(); |
239 | 0 | MOZ_ASSERT(value.Length() == 3); |
240 | 0 | poseState.angularVelocity[0] = value.Data()[0]; |
241 | 0 | poseState.angularVelocity[1] = value.Data()[1]; |
242 | 0 | poseState.angularVelocity[2] = value.Data()[2]; |
243 | 0 | } |
244 | 0 | if (!aAngAcceleration.IsNull()) { |
245 | 0 | const Float32Array& value = aAngAcceleration.Value(); |
246 | 0 | value.ComputeLengthAndData(); |
247 | 0 | MOZ_ASSERT(value.Length() == 3); |
248 | 0 | poseState.angularAcceleration[0] = value.Data()[0]; |
249 | 0 | poseState.angularAcceleration[1] = value.Data()[1]; |
250 | 0 | poseState.angularAcceleration[2] = value.Data()[2]; |
251 | 0 | } |
252 | 0 | if (!aLinVelocity.IsNull()) { |
253 | 0 | const Float32Array& value = aLinVelocity.Value(); |
254 | 0 | value.ComputeLengthAndData(); |
255 | 0 | MOZ_ASSERT(value.Length() == 3); |
256 | 0 | poseState.linearVelocity[0] = value.Data()[0]; |
257 | 0 | poseState.linearVelocity[1] = value.Data()[1]; |
258 | 0 | poseState.linearVelocity[2] = value.Data()[2]; |
259 | 0 | } |
260 | 0 | if (!aLinAcceleration.IsNull()) { |
261 | 0 | const Float32Array& value = aLinAcceleration.Value(); |
262 | 0 | value.ComputeLengthAndData(); |
263 | 0 | MOZ_ASSERT(value.Length() == 3); |
264 | 0 | poseState.linearAcceleration[0] = value.Data()[0]; |
265 | 0 | poseState.linearAcceleration[1] = value.Data()[1]; |
266 | 0 | poseState.linearAcceleration[2] = value.Data()[2]; |
267 | 0 | } |
268 | 0 |
|
269 | 0 | GamepadPoseInformation a(poseState); |
270 | 0 | GamepadChangeEventBody body(a); |
271 | 0 | GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body); |
272 | 0 |
|
273 | 0 | uint32_t id = ++mEventNumber; |
274 | 0 | mChild->SendGamepadTestEvent(id, e); |
275 | 0 | } |
276 | | |
277 | | JSObject* |
278 | | GamepadServiceTest::WrapObject(JSContext* aCx, JS::HandleObject aGivenProto) |
279 | 0 | { |
280 | 0 | return GamepadServiceTest_Binding::Wrap(aCx, this, aGivenProto); |
281 | 0 | } |
282 | | |
283 | | } // dom |
284 | | } // mozilla |