-
Notifications
You must be signed in to change notification settings - Fork 7
/
gamepad.spec.ts
264 lines (197 loc) · 7.44 KB
/
gamepad.spec.ts
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
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { IGamepad, IGamepadButton, INavigator, IWindow } from '../apis'
import store from '../store'
import { Vector2 } from '../utils/math'
import { MockEventTarget } from '../utils/mock'
import { Gamepad } from './gamepad'
class MockWindow extends MockEventTarget implements IWindow {}
class MockNavigator extends MockEventTarget implements INavigator {
public gamepads: IGamepad[] = []
public getGamepads(): IGamepad[] {
return this.gamepads
}
}
class MockGamepad implements IGamepad {
public index = 0
public buttons: IGamepadButton[] = []
public axes: number[] = []
public connected = true
public timestamp = 1000
public mapping = 'standard'
}
interface MockPack {
win: MockWindow
nav: MockNavigator
gamepad: Gamepad
}
function mockPack(): MockPack {
const win = new MockWindow()
const nav = new MockNavigator()
const gamepad = new Gamepad({ win, nav })
return { win, nav, gamepad }
}
describe('The `Gamepad` class', () => {
it('should register the required listeners on the window', () => {
const { win } = mockPack()
expect(Object.keys(win.listeners).sort()).to.deep.equal([
'gamepadconnected',
'gamepaddisconnected',
].sort())
})
describe('should have an `isConnected()` method that', () => {
const { win, nav, gamepad } = mockPack()
it('returns `false` before any gamepad was connected', () => {
expect(gamepad.isConnected()).to.equal(false)
})
it('returns `true` after a gamepad was connected', () => {
nav.gamepads[0] = new MockGamepad()
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
expect(gamepad.isConnected()).to.equal(true)
})
it('returns `false` after the gamepad was disconnected', () => {
nav.gamepads.pop()
win.listeners.gamepaddisconnected({ gamepad: { index: 0 } })
expect(gamepad.isConnected()).to.equal(false)
})
it('returns `false` after a non-standard gamepad was connected', () => {
nav.gamepads[0] = new MockGamepad()
nav.gamepads[0].mapping = 'non-standard'
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
expect(gamepad.isConnected()).to.equal(false)
})
it('returns `true` again after a gamepad was connected', () => {
nav.gamepads[0] = new MockGamepad()
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
expect(gamepad.isConnected()).to.equal(true)
})
after(() => {
nav.gamepads.pop()
win.listeners.gamepaddisconnected({ gamepad: { index: 0 } })
})
})
describe('in its disconnected state', () => {
const { gamepad } = mockPack()
it('should set `store.preferGamepad` to `false`', () => {
expect(store.preferGamepad).to.equal(false)
})
describe('should have an `button()` method that returns a component that', () => {
it('when queried `false`', () => {
expect(gamepad.button(0).query()).to.equal(false)
})
it('when queried in `trigger` moder returns `false`', () => {
expect(gamepad.button(0).trigger.query()).to.equal(false)
})
})
describe('should have a `stick()` method that returns a component that', () => {
it('returns a zero vector when queried', () => {
expect(gamepad.stick('left').query()).to.deep.equal({ x: 0, y: 0 })
})
})
describe('should have a `vibrate()` method that', () => {
it("doesn't throw an error when called", async () => {
await gamepad.vibrate(1000)
})
})
})
describe('in its connected state', () => {
const { win, nav, gamepad } = mockPack()
before(() => {
nav.gamepads[0] = {
index: 0,
buttons: [
{ pressed: false },
],
axes: [0, 0, 0, 0],
connected: true,
timestamp: 0,
mapping: 'standard',
}
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
})
it('should not allow another gamepad to be connected', () => {
nav.gamepads[1] = {
index: 1,
buttons: [
{ pressed: true },
],
axes: [0, 0, 0, 0],
connected: true,
timestamp: 0,
mapping: 'standard',
}
win.listeners.gamepadconnected({ gamepad: nav.gamepads[1] })
expect(gamepad.button(0).query()).to.equal(false)
})
it('should stay connected gamepad if another gamepad is disconnected', () => {
win.listeners.gamepaddisconnected({ gamepad: nav.gamepads[1] })
expect(gamepad.isConnected()).to.equal(true)
})
it('should set `store.preferGamepad` to `true`', () => {
expect(store.preferGamepad).to.equal(true)
})
describe('should have a `button()` method that returns a component that', () => {
it('returns `false` when the button is not pressed', () => {
expect(gamepad.button(0).query()).to.equal(false)
})
it('returns `true` when the button is pressed', () => {
nav.gamepads[0].buttons[0].pressed = true
expect(gamepad.button(0).query()).to.equal(true)
})
describe('when queried in `trigger` mode', () => {
it('returns `false` when the key is not pressed', () => {
nav.gamepads[0].buttons[0].pressed = false
expect(gamepad.button(0).trigger.query()).to.equal(false)
})
it('returns `true` once after the key was pressed', () => {
nav.gamepads[0].buttons[0].pressed = true
expect(gamepad.button(0).trigger.query()).to.equal(true)
})
it('returns `false` after the key state was queried', () => {
expect(gamepad.button(0).trigger.query()).to.equal(false)
})
})
})
describe('should have a `stick()` method that returns a component that', () => {
it('throws an error when initialized with an invalid stick', () => {
expect(() => gamepad.stick('lol')).to.throw(Error, 'Gamepad stick "lol" not found!')
})
it('returns a (0, 0) vector when initially queried', () => {
expect(gamepad.stick('left').query()).to.deep.equal(new Vector2())
})
it('returns the correct vector when queried after change', () => {
nav.gamepads[0].axes[0] = .56
nav.gamepads[0].axes[1] = .31
expect(gamepad.stick('left').query()).to.deep.equal(new Vector2(.56, .31))
})
it('also works with custom axis numbers', () => {
nav.gamepads[0].axes[2] = .42
nav.gamepads[0].axes[3] = .69
expect(gamepad.stick({ label: '', xAxis: 2, yAxis: 3 }).query()).to.deep.equal(new Vector2(.42, .69))
})
})
describe('should have a `vibrate()` method that', () => {
it('correctly passes along the provided options', () => {
let playEffectArgs: any[] = []
nav.gamepads[0].vibrationActuator = {
type: 'dual-rumble',
playEffect: async (...args) => { playEffectArgs = args },
}
gamepad.vibrate(1000, { weakMagnitude: 0.5, strongMagnitude: 1 })
expect(playEffectArgs).to.deep.equal([
'dual-rumble',
{ duration: 1000, weakMagnitude: 0.5, strongMagnitude: 1 },
])
})
it('ignores actuators that are not of type `dual-rumble`', () => {
let playEffectCalled = false
nav.gamepads[0].vibrationActuator = {
type: 'not-dual-rumble',
playEffect: async () => { playEffectCalled = true },
}
gamepad.vibrate(1000)
expect(playEffectCalled).to.equal(false)
})
})
})
})