-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepad-helper.test.js
More file actions
512 lines (442 loc) · 20.2 KB
/
Copy pathgamepad-helper.test.js
File metadata and controls
512 lines (442 loc) · 20.2 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import {
beforeEach,
describe,
expect,
jest,
test,
} from '@jest/globals';
const GamepadHelper = require('../src/js/gamepad-helper');
describe('GamepadHelper', () => {
let helper;
let mockGamepad;
beforeEach(() => {
// Setup basic mock of navigator.getGamepads
globalThis.navigator.getGamepads = jest.fn();
// Create a new helper instance before each test
helper = new GamepadHelper();
// Create a basic mock gamepad
mockGamepad = {
id: "Xbox Wireless Controller",
index: 0,
connected: true,
buttons: new Array(17).fill({ pressed: false, value: 0 }),
axes: [0, 0, 0, 0],
mapping: "standard",
vibrationActuator: {
type: "dual-rumble",
playEffect: jest.fn().mockResolvedValue({ success: true })
}
};
});
describe('Constructor and initialization', () => {
test('initializes with correct controller types', () => {
expect(helper.CONTROLLER_TYPES).toEqual({
XBOX: 'xbox',
PLAYSTATION: 'playstation',
SWITCH: 'switch',
STANDARD: 'standard'
});
});
test('initializes exactIdLookup correctly', () => {
// Check a few known mappings
expect(helper.exactIdLookup['xinput'].type).toBe(helper.CONTROLLER_TYPES.XBOX);
expect(helper.exactIdLookup['PLAYSTATION(R)3 Controller (Vendor: 054c Product: 0268)'].type).toBe(helper.CONTROLLER_TYPES.PLAYSTATION);
expect(helper.exactIdLookup['Pro Controller (STANDARD GAMEPAD Vendor: 057e Product: 2009)'].type).toBe(helper.CONTROLLER_TYPES.SWITCH);
});
test('initializes vendorProductLookup correctly', () => {
expect(helper.vendorProductLookup['054c:05c4'].name).toBe('Sony DualShock (PS4)');
expect(helper.vendorProductLookup['054c:0ce6'].name).toBe('Sony DualSense (PS5)');
expect(helper.vendorProductLookup['057e:2009'].name).toBe('Nintendo Switch Pro Controller');
expect(helper.vendorProductLookup['1209:0001'].name).toBe('Generic Gamepad');
expect(helper.vendorProductLookup['045e:028e'].name).toBe('Xbox 360');
expect(helper.vendorProductLookup['045e:02ea'].name).toBe('Xbox One/Series');
expect(helper.vendorProductLookup['045e:0b12'].name).toBe('Xbox One/Series');
expect(helper.vendorProductLookup['045e:0b13'].name).toBe('Xbox One/Series');
expect(helper.vendorProductLookup['045e:0b20'].name).toBe('Xbox One/Series');
});
test('initializes controller mappings correctly', () => {
expect(helper.controllerMappings).toBeDefined();
expect(helper.controllerMappings[helper.CONTROLLER_TYPES.XBOX].buttonMap[0]).toBe('A');
expect(helper.controllerMappings[helper.CONTROLLER_TYPES.PLAYSTATION].buttonMap[0]).toBe('×');
expect(helper.controllerMappings[helper.CONTROLLER_TYPES.SWITCH].buttonMap[0]).toBe('B');
});
});
describe('isSupported', () => {
test('returns true when Gamepad API is supported', () => {
expect(helper.isSupported()).toBe(true);
});
test('returns false when Gamepad API is not supported', () => {
delete globalThis.navigator.getGamepads;
expect(helper.isSupported()).toBe(false);
});
});
describe('getGamepadInfo', () => {
test('returns generic info for null gamepadId', () => {
const result = helper.getGamepadInfo(null);
expect(result).toEqual({
type: helper.CONTROLLER_TYPES.STANDARD,
name: 'Generic Controller'
});
});
test('returns exact match for known gamepadId', () => {
const result = helper.getGamepadInfo('xinput');
expect(result).toEqual({
type: helper.CONTROLLER_TYPES.XBOX,
name: 'Xbox'
});
});
test('preserves browser-provided XInput labels', () => {
expect(helper.getGamepadInfo('xinput')).toEqual({
type: helper.CONTROLLER_TYPES.XBOX,
name: 'Xbox'
});
expect(helper.getGamepadInfo('Xbox 360 Controller (XInput STANDARD GAMEPAD)')).toEqual({
type: helper.CONTROLLER_TYPES.XBOX,
name: 'Xbox 360'
});
});
test.each([
{
browserController: 'Firefox DualShock 4',
gamepadId: '054c-05c4-HID VHF Driver',
type: 'playstation',
name: 'Sony DualShock (PS4)'
},
{
browserController: 'Firefox DualSense',
gamepadId: '054c-0ce6-HID VHF Driver',
type: 'playstation',
name: 'Sony DualSense (PS5)'
},
{
browserController: 'Firefox Switch Pro',
gamepadId: '057e-2009-HID VHF Driver',
type: 'switch',
name: 'Nintendo Switch Pro Controller'
},
{
browserController: 'Firefox Generic',
gamepadId: '1209-0001-HID VHF Driver',
type: 'standard',
name: 'Generic Gamepad'
},
{
browserController: 'Firefox Xbox 360',
gamepadId: '045e-028e-Sunshine (libvirtualhid) X-Box 360 Controller',
type: 'xbox',
name: 'Xbox 360'
},
{
browserController: 'Firefox Xbox Series',
gamepadId: '045e-0b13-Sunshine (libvirtualhid) X-Box Series Controller',
type: 'xbox',
name: 'Xbox One/Series'
},
{
browserController: 'Chrome DualShock 4',
gamepadId: 'HID VHF Driver (STANDARD GAMEPAD Vendor: 054c Product: 05c4)',
type: 'playstation',
name: 'Sony DualShock (PS4)'
},
{
browserController: 'Chrome DualSense',
gamepadId: 'HID VHF Driver (STANDARD GAMEPAD Vendor: 054c Product: 0ce6)',
type: 'playstation',
name: 'Sony DualSense (PS5)'
},
{
browserController: 'Chrome Switch Pro',
gamepadId: 'HID VHF Driver (STANDARD GAMEPAD Vendor: 057e Product: 2009)',
type: 'switch',
name: 'Nintendo Switch Pro Controller'
},
{
browserController: 'Chrome Generic',
gamepadId: 'HID VHF Driver (Vendor: 1209 Product: 0001)',
type: 'standard',
name: 'Generic Gamepad'
},
{
browserController: 'Chrome Xbox 360',
gamepadId: 'Sunshine (libvirtualhid) X-Box 360 Controller (STANDARD GAMEPAD Vendor: 045e Product: 028e)',
type: 'xbox',
name: 'Xbox 360'
},
{
browserController: 'Chrome Xbox One',
gamepadId: 'Sunshine (libvirtualhid) X-Box One Controller (STANDARD GAMEPAD Vendor: 045e Product: 0b20)',
type: 'xbox',
name: 'Xbox One/Series'
},
{
browserController: 'Chrome Xbox Series',
gamepadId: 'Sunshine (libvirtualhid) X-Box Series Controller (STANDARD GAMEPAD Vendor: 045e Product: 0b13)',
type: 'xbox',
name: 'Xbox One/Series'
},
])('returns a VID/PID match for $browserController', ({ gamepadId, type, name }) => {
expect(helper.getGamepadInfo(gamepadId)).toEqual({ type, name });
});
test('normalizes shortened and uppercase VID/PID values', () => {
expect(helper.getGamepadInfo('54C-5C4-HID VHF Driver')).toEqual({
type: helper.CONTROLLER_TYPES.PLAYSTATION,
name: 'Sony DualShock (PS4)'
});
});
test('does not extract a VID/PID from a missing ID', () => {
expect(helper.extractVendorProductId(null)).toBeNull();
});
test('returns generic info for unknown gamepadId', () => {
const result = helper.getGamepadInfo('unknown-controller-id');
expect(result).toEqual({
type: helper.CONTROLLER_TYPES.STANDARD,
name: 'Generic Controller'
});
});
});
describe('detectControllerType', () => {
test('detects Xbox controller', () => {
expect(helper.detectControllerType('xinput')).toBe(helper.CONTROLLER_TYPES.XBOX);
});
test('detects PlayStation controller', () => {
expect(helper.detectControllerType('054c-05c4-Wireless Controller')).toBe(helper.CONTROLLER_TYPES.PLAYSTATION);
});
test('detects Switch controller', () => {
expect(helper.detectControllerType('Pro Controller (STANDARD GAMEPAD Vendor: 057e Product: 2009)')).toBe(helper.CONTROLLER_TYPES.SWITCH);
});
test('returns standard for unknown controllers', () => {
expect(helper.detectControllerType('Unknown Controller')).toBe(helper.CONTROLLER_TYPES.STANDARD);
});
});
describe('getButtonName', () => {
test('returns correct button names for Xbox controller', () => {
expect(helper.getButtonName(helper.CONTROLLER_TYPES.XBOX, 0)).toBe('A');
expect(helper.getButtonName(helper.CONTROLLER_TYPES.XBOX, 4)).toBe('LB');
expect(helper.getButtonName(helper.CONTROLLER_TYPES.XBOX, 12)).toBe('DUp');
expect(helper.getButtonName(helper.CONTROLLER_TYPES.XBOX, 17)).toBe('Share');
expect(helper.getButtonName(helper.CONTROLLER_TYPES.XBOX, 18)).toBe('B18');
});
test('returns correct button names for PlayStation controller', () => {
expect(helper.getButtonName(helper.CONTROLLER_TYPES.PLAYSTATION, 0)).toBe('×');
expect(helper.getButtonName(helper.CONTROLLER_TYPES.PLAYSTATION, 4)).toBe('L1');
expect(helper.getButtonName(helper.CONTROLLER_TYPES.PLAYSTATION, 17)).toBe('TouchPad');
});
test('returns fallback for unknown button indices', () => {
expect(helper.getButtonName(helper.CONTROLLER_TYPES.XBOX, 99)).toBe('B99');
});
test('falls back to standard controller for unknown type', () => {
expect(helper.getButtonName('unknown-type', 0)).toBe('B0');
});
});
describe('getAxisName', () => {
test('returns correct axis names for known controller types', () => {
expect(helper.getAxisName(helper.CONTROLLER_TYPES.XBOX, 0)).toBe('Left Stick X');
expect(helper.getAxisName(helper.CONTROLLER_TYPES.PLAYSTATION, 1)).toBe('Left Stick Y');
expect(helper.getAxisName(helper.CONTROLLER_TYPES.SWITCH, 3)).toBe('Right Stick Y');
});
test('returns generic axis name for unknown indices', () => {
expect(helper.getAxisName(helper.CONTROLLER_TYPES.XBOX, 99)).toBe('Axis 99');
});
test('falls back to standard controller for unknown type', () => {
expect(helper.getAxisName('unknown-type', 0)).toBe('Axis 0');
});
});
describe('isVibrationSupported', () => {
test('returns true when vibration actuator exists', () => {
expect(helper.isVibrationSupported(mockGamepad)).toBe(true);
});
test('returns false when gamepad is null', () => {
expect(helper.isVibrationSupported(null)).toBe(false);
});
test('returns false when vibration actuator does not exist', () => {
delete mockGamepad.vibrationActuator;
expect(helper.isVibrationSupported(mockGamepad)).toBe(false);
});
});
describe('getVibrationCapabilities', () => {
test('returns correct capabilities for supported gamepad', () => {
const result = helper.getVibrationCapabilities(mockGamepad);
expect(result).toEqual({
supported: true,
type: 'dual-rumble'
});
});
test('returns unsupported for null gamepad', () => {
const result = helper.getVibrationCapabilities(null);
expect(result).toEqual({
supported: false,
type: null
});
});
test('handles missing actuator type', () => {
mockGamepad.vibrationActuator = { playEffect: jest.fn() };
const result = helper.getVibrationCapabilities(mockGamepad);
expect(result).toEqual({
supported: true,
type: 'unknown'
});
});
});
describe('vibrate', () => {
test('calls playEffect with correct parameters for dual-rumble', async () => {
await helper.vibrate(mockGamepad, {
weakMagnitude: 0.3,
strongMagnitude: 0.7,
duration: 500,
startDelay: 100
});
expect(mockGamepad.vibrationActuator.playEffect).toHaveBeenCalledWith(
'dual-rumble',
{
weakMagnitude: 0.3,
strongMagnitude: 0.7,
duration: 500,
startDelay: 100
}
);
});
test('uses default parameters when not provided', async () => {
await helper.vibrate(mockGamepad);
expect(mockGamepad.vibrationActuator.playEffect).toHaveBeenCalledWith(
'dual-rumble',
{
weakMagnitude: 0.5,
strongMagnitude: 0.5,
duration: 1000,
startDelay: 0
}
);
});
test('handles vibration actuator type correctly', async () => {
mockGamepad.vibrationActuator.type = 'vibration';
await helper.vibrate(mockGamepad, {
weakMagnitude: 0.3,
strongMagnitude: 0.7
});
expect(mockGamepad.vibrationActuator.playEffect).toHaveBeenCalledWith(
'vibration',
expect.objectContaining({
magnitude: 0.7 // Should use the max of weakMagnitude and strongMagnitude
})
);
});
test('rejects promise when vibration not supported', async () => {
delete mockGamepad.vibrationActuator;
await expect(helper.vibrate(mockGamepad)).rejects.toThrow('Vibration not supported');
});
test('tries fallback for unknown actuator types', async () => {
console.warn = jest.fn(); // report generation fails if this is not mocked
mockGamepad.vibrationActuator.type = 'unknown-type';
// Mock implementation that throws on first call but succeeds on second
mockGamepad.vibrationActuator.playEffect = jest.fn()
.mockImplementationOnce(() => { throw new Error('Unknown type'); })
.mockImplementationOnce(() => Promise.resolve({ success: true }));
await helper.vibrate(mockGamepad);
// Should have tried with unknown type first, then fallback
expect(mockGamepad.vibrationActuator.playEffect).toHaveBeenCalledTimes(2);
expect(mockGamepad.vibrationActuator.playEffect).toHaveBeenLastCalledWith(
'dual-rumble',
expect.anything()
);
});
});
describe('stopVibration', () => {
test('calls vibrate with zero magnitudes', async () => {
const vibrateSpy = jest.spyOn(helper, 'vibrate').mockResolvedValue({ success: true });
await helper.stopVibration(mockGamepad);
expect(vibrateSpy).toHaveBeenCalledWith(
mockGamepad,
{ weakMagnitude: 0, strongMagnitude: 0 }
);
});
test('rejects when vibration not supported', async () => {
delete mockGamepad.vibrationActuator;
await expect(helper.stopVibration(mockGamepad)).rejects.toThrow('Vibration not supported');
});
});
describe('getConnectedGamepads', () => {
test('returns empty array when API not supported', () => {
delete globalThis.navigator.getGamepads;
expect(helper.getConnectedGamepads()).toEqual([]);
});
test('returns array of connected gamepads', () => {
const mockGamepads = [mockGamepad, null, { id: 'gamepad-2', index: 2 }, null];
globalThis.navigator.getGamepads.mockReturnValue(mockGamepads);
const result = helper.getConnectedGamepads();
expect(result).toEqual([mockGamepad, { id: 'gamepad-2', index: 2 }]);
expect(result.length).toBe(2);
});
});
describe('getButtonImagePath', () => {
test('returns correct path for Xbox controller buttons', () => {
const path = helper.getButtonImagePath(helper.CONTROLLER_TYPES.XBOX, 0);
expect(path).toBe('/assets/img/gamepads/xbox/Buttons Outline/White/SVG/A.svg');
});
test('returns the Share icon for Xbox button 17', () => {
const path = helper.getButtonImagePath(helper.CONTROLLER_TYPES.XBOX, 17);
expect(path).toBe('/assets/img/gamepads/xbox/Buttons Outline/White/SVG/Share.svg');
});
test('returns correct path for PlayStation controller buttons', () => {
const path = helper.getButtonImagePath(helper.CONTROLLER_TYPES.PLAYSTATION, 3);
expect(path).toBe('/assets/img/gamepads/playstation/Buttons Outline/White/SVG/Triangle.svg');
});
test('returns correct path for Switch controller buttons', () => {
const path = helper.getButtonImagePath(helper.CONTROLLER_TYPES.SWITCH, 1);
expect(path).toBe('/assets/img/gamepads/switch/Buttons Outline/White/SVG/A.svg');
});
test('returns null for unknown button index', () => {
const path = helper.getButtonImagePath(helper.CONTROLLER_TYPES.XBOX, 99);
expect(path).toBe(null);
});
test('customizes base path when provided', () => {
const path = helper.getButtonImagePath(
helper.CONTROLLER_TYPES.XBOX,
0,
'/custom/path/'
);
expect(path).toBe('/custom/path/xbox/Buttons Outline/White/SVG/A.svg');
});
test('handles different button colors', () => {
const path = helper.getButtonImagePath(
helper.CONTROLLER_TYPES.XBOX,
0,
'/assets/img/gamepads/',
'Black'
);
expect(path).toBe('/assets/img/gamepads/xbox/Buttons Outline/Black/SVG/A.svg');
});
test('handles different button types', () => {
const path = helper.getButtonImagePath(
helper.CONTROLLER_TYPES.XBOX,
0,
'/assets/img/gamepads/',
'White',
'Solid'
);
expect(path).toBe('/assets/img/gamepads/xbox/Buttons Solid/White/SVG/A.svg');
});
test('handles invalid button color', () => {
console.warn = jest.fn();
const path = helper.getButtonImagePath(
helper.CONTROLLER_TYPES.XBOX,
0,
'/assets/img/gamepads/',
'Invalid'
);
expect(console.warn).toHaveBeenCalledWith("Invalid buttonColor: Invalid. Using 'White' instead.");
expect(path).toBe('/assets/img/gamepads/xbox/Buttons Outline/White/SVG/A.svg');
});
test('handles invalid button type', () => {
console.warn = jest.fn();
const path = helper.getButtonImagePath(
helper.CONTROLLER_TYPES.XBOX,
0,
'/assets/img/gamepads/',
'White',
'Invalid'
);
expect(console.warn).toHaveBeenCalledWith("Invalid buttonType: Invalid. Using 'Outline' instead.");
expect(path).toBe('/assets/img/gamepads/xbox/Buttons Outline/White/SVG/A.svg');
});
});
});