File: gamecontroller_test.py

package info (click to toggle)
pysdl2 0.9.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: python: 24,685; makefile: 36; sh: 8
file content (465 lines) | stat: -rw-r--r-- 17,360 bytes parent folder | download
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
import sys
import pytest
import sdl2
from sdl2 import (
    SDL_Init, SDL_Quit, SDL_ClearError, SDL_GetError,
    SDL_FALSE, SDL_TRUE, SDL_IGNORE, SDL_ENABLE, SDL_QUERY
)
from sdl2 import joystick
from .conftest import _check_error_msg

# Get status of gamepad support/availability before running tests
SDL_ClearError()
ret = SDL_Init(sdl2.SDL_INIT_GAMECONTROLLER)
gamepad_works = ret == 0
SDL_Quit()

pytestmark = pytest.mark.skipif(not gamepad_works, reason="system unsupported")

gamepad_types = [
    sdl2.SDL_CONTROLLER_TYPE_UNKNOWN,
    sdl2.SDL_CONTROLLER_TYPE_XBOX360,
    sdl2.SDL_CONTROLLER_TYPE_XBOXONE,
    sdl2.SDL_CONTROLLER_TYPE_PS3,
    sdl2.SDL_CONTROLLER_TYPE_PS4,
    sdl2.SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO,
    sdl2.SDL_CONTROLLER_TYPE_VIRTUAL,
    sdl2.SDL_CONTROLLER_TYPE_PS5,
    sdl2.SDL_CONTROLLER_TYPE_AMAZON_LUNA,
    sdl2.SDL_CONTROLLER_TYPE_GOOGLE_STADIA,
]


# Overrides global fixture with one that initializes the joystick system
@pytest.fixture(scope="module")
def with_sdl():
    sdl2.SDL_ClearError()
    sdl2.SDL_SetHint(b"SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", b"1")
    ret = sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER)
    assert ret == 0, _check_error_msg()
    # Also initialize a virtual joystick (if supported)
    if sdl2.dll.version >= 2014:
        virt_type = joystick.SDL_JOYSTICK_TYPE_GAMECONTROLLER
        virt_index = joystick.SDL_JoystickAttachVirtual(virt_type, 2, 4, 1)
    yield
    sdl2.SDL_Quit()

@pytest.fixture()
def gamepads(with_sdl):
    devices = []
    count = joystick.SDL_NumJoysticks()
    for i in range(count):
        if sdl2.SDL_IsGameController(i) == SDL_TRUE:
            pad = sdl2.SDL_GameControllerOpen(i)
            assert pad, _check_error_msg()
            assert isinstance(pad.contents, sdl2.SDL_GameController)
            devices.append(pad)
    yield devices
    for pad in devices:
        sdl2.SDL_GameControllerClose(pad)

def is_virtual(pad):
    stick = sdl2.SDL_GameControllerGetJoystick(pad)
    name = joystick.SDL_JoystickName(stick)
    return b"Virtual " in name


# Test if SDL_GameControllerMappingForGUID is able to be tested
if sys.version_info >= (3, 8, 0) or sdl2.dll.version >= 2006:
    has_mapping_for_guid = True
else:
    has_mapping_for_guid = False


@pytest.mark.skip("not implemented")
def test_SDL_GameControllerAddMappingsFromRW(gamepads):
    pass

@pytest.mark.skip("not implemented")
def test_SDL_GameControllerAddMappingsFromFile(gamepads):
    pass

def test_SDL_GameControllerAddMapping(with_sdl):
    newmap = (
        b"030000005e0400002700000006010000,Microsoft SideWinder,"
        b"platform:Mac OS X,a:b0,b:b1,x:b2,y:b3,dpup:-a1,dpdown:+a1,"
        b"dpleft:-a0,dpright:+a0,lefttrigger:b4,righttrigger:b5"
    )
    if sdl2.dll.version >= 2006:
        n1 = sdl2.SDL_GameControllerNumMappings()
        ret = sdl2.SDL_GameControllerAddMapping(newmap)
        assert ret >= 0
        n2 = sdl2.SDL_GameControllerNumMappings()
        assert n2 == n1 + 1
    else:
        # NumMappings not available before 2.0.6
        ret = sdl2.SDL_GameControllerAddMapping(newmap)
        assert ret != -1

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_GameControllerNumMappings(with_sdl):
    num = sdl2.SDL_GameControllerNumMappings()
    assert num > 0

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_GameControllerMappingForIndex(with_sdl):
    newmap = (
        b"030000005e0400002700000006010000,Microsoft SideWinder,"
        b"platform:Mac OS X,a:b0,b:b1,x:b2,y:b3,dpup:-a1,dpdown:+a1,"
        b"dpleft:-a0,dpright:+a0,lefttrigger:b4,righttrigger:b5"
    )
    ret = sdl2.SDL_GameControllerAddMapping(newmap)
    assert ret >= 0
    num = sdl2.SDL_GameControllerNumMappings()
    retmap = sdl2.SDL_GameControllerMappingForIndex(num - 1)
    assert newmap == retmap

@pytest.mark.skipif(not has_mapping_for_guid, reason="not available")
def test_SDL_GameControllerMappingForGUID(with_sdl):
    newmap = (
        b"030000005e0400002700000006010000,Microsoft SideWinder,"
        b"platform:Mac OS X,a:b0,b:b1,x:b2,y:b3,dpup:-a1,dpdown:+a1,"
        b"dpleft:-a0,dpright:+a0,lefttrigger:b4,righttrigger:b5"
    )
    ret = sdl2.SDL_GameControllerAddMapping(newmap)
    assert ret >= 0
    # Get GUID for new mapping
    guid_str = newmap.split(b",")[0]
    guid = joystick.SDL_JoystickGetGUIDFromString(guid_str)
    # Get mapping for GUID
    retmap = sdl2.SDL_GameControllerMappingForGUID(guid)
    assert retmap == newmap

def test_SDL_GameControllerMapping(gamepads):
    for pad in gamepads:
        mapping = sdl2.SDL_GameControllerMapping(pad)
        assert mapping == None or type(mapping) in (str, bytes)

def test_SDL_IsGameController(with_sdl):
    count = joystick.SDL_NumJoysticks()
    for index in range(count):
        ret = sdl2.SDL_IsGameController(index)
        assert ret in [SDL_TRUE, SDL_FALSE]

def test_SDL_GameControllerNameForIndex(with_sdl):
    count = joystick.SDL_NumJoysticks()
    for index in range(count):
        name = sdl2.SDL_GameControllerNameForIndex(index)
        assert name == None or type(name) in (str, bytes)

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_GameControllerPathForIndex(with_sdl):
    count = joystick.SDL_NumJoysticks()
    for index in range(count):
        name = sdl2.SDL_GameControllerPathForIndex(index)
        assert name == None or type(name) in (str, bytes)

@pytest.mark.skipif(sdl2.dll.version < 2012, reason="not available")
def test_SDL_GameControllerTypeForIndex(with_sdl):
    count = joystick.SDL_NumJoysticks()
    for index in range(count):
        padtype = sdl2.SDL_GameControllerTypeForIndex(index)
        assert padtype in gamepad_types

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
def test_SDL_GameControllerMappingForDeviceIndex(with_sdl):
    pass

def test_SDL_GameControllerOpenClose(with_sdl):
    count = joystick.SDL_NumJoysticks()
    for index in range(count):
        if sdl2.SDL_IsGameController(index) == SDL_TRUE:
            pad = sdl2.SDL_GameControllerOpen(index)
            assert pad, _check_error_msg()
            assert isinstance(pad.contents, sdl2.SDL_GameController)
            sdl2.SDL_GameControllerClose(pad)

def test_SDL_GameControllerFromInstanceID(gamepads):
    for pad in gamepads:
        stick = sdl2.SDL_GameControllerGetJoystick(pad)
        iid = joystick.SDL_JoystickInstanceID(stick)
        assert iid >= 0
        pad2 = sdl2.SDL_GameControllerFromInstanceID(iid)
        name = sdl2.SDL_GameControllerName(pad)
        assert sdl2.SDL_GameControllerName(pad2) == name

@pytest.mark.skipif(sdl2.dll.version < 2012, reason="not available")
def test_SDL_GameControllerFromPlayerIndex(gamepads):
    i = 0
    for pad in gamepads:
        sdl2.SDL_GameControllerSetPlayerIndex(pad, i)
        pad2 = sdl2.SDL_GameControllerFromPlayerIndex(i)
        name = sdl2.SDL_GameControllerName(pad)
        assert sdl2.SDL_GameControllerName(pad2) == name
        i += 1

def test_SDL_GameControllerName(gamepads):
    names = []
    for pad in gamepads:
        name = sdl2.SDL_GameControllerName(pad)
        assert type(name) in (str, bytes)
        names.append(name.decode('utf-8'))
    print(names)

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_GameControllerPath(gamepads):
    paths = []
    for pad in gamepads:
        path = sdl2.SDL_GameControllerPath(pad)
        if not (is_virtual(pad) or sys.platform == "darwin"):
            assert type(path) in (str, bytes)
            paths.append(path.decode('utf-8'))
    if len(paths):
        print(paths)

@pytest.mark.skipif(sdl2.dll.version < 2012, reason="not available")
def test_SDL_GameControllerGetType(gamepads):
    for pad in gamepads:
        padtype = sdl2.SDL_GameControllerGetType(pad)
        assert padtype in gamepad_types
        if is_virtual(pad):
            assert padtype == sdl2.SDL_CONTROLLER_TYPE_VIRTUAL

@pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
def test_SDL_GameControllerGetPlayerIndex(gamepads):
    for pad in gamepads:
        player = sdl2.SDL_GameControllerGetPlayerIndex(pad)
        assert player in [-1, 0, 1, 2, 3]

@pytest.mark.skipif(sdl2.dll.version < 2012, reason="not available")
def test_SDL_GameControllerSetPlayerIndex(gamepads):
    i = 0
    for pad in gamepads:
        sdl2.SDL_GameControllerSetPlayerIndex(pad, i)
        player = sdl2.SDL_GameControllerGetPlayerIndex(pad)
        assert player == i
        i += 1

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_GameControllerGetVendor(gamepads):
    for pad in gamepads:
        vid = sdl2.SDL_GameControllerGetVendor(pad)
        if not is_virtual(pad):
            assert vid > 0

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_GameControllerGetProduct(gamepads):
    for pad in gamepads:
        pid = sdl2.SDL_GameControllerGetProduct(pad)
        if not is_virtual(pad):
            assert pid > 0

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_GameControllerGetProductVersion(gamepads):
    for pad in gamepads:
        pver = sdl2.SDL_GameControllerGetProductVersion(pad)
        assert pver >= 0

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_GameControllerGetFirmwareVersion(gamepads):
    for pad in gamepads:
        fw_ver = sdl2.SDL_GameControllerGetFirmwareVersion(pad)
        assert fw_ver >= 0

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerGetSerial(gamepads):
    for pad in gamepads:
        serial = sdl2.SDL_GameControllerGetSerial(pad)
        assert serial == None or type(serial) in (str, bytes)

@pytest.mark.skipif(sdl2.dll.version < 2300, reason="not available")
def test_SDL_GameControllerGetSteamHandle(gamepads):
    for pad in gamepads:
        handle = sdl2.SDL_GameControllerGetSteamHandle(pad)
        assert handle >= 0

def test_SDL_GameControllerGetAttached(gamepads):
    for pad in gamepads:
        ret = sdl2.SDL_GameControllerGetAttached(pad)
        assert ret in [SDL_FALSE, SDL_TRUE]

def test_SDL_GameControllerGetJoystick(gamepads):
    for pad in gamepads:
        stick = sdl2.SDL_GameControllerGetJoystick(pad)
        assert stick, _check_error_msg()
        assert isinstance(stick.contents, joystick.SDL_Joystick)

def test_SDL_GameControllerEventState(with_sdl):
    for state in (SDL_IGNORE, SDL_ENABLE):
        news = sdl2.SDL_GameControllerEventState(state)
        assert news == state
        query = sdl2.SDL_GameControllerEventState(SDL_QUERY)
        assert query == state

def test_SDL_GameControllerUpdate(with_sdl):
    # NOTE: Returns void, can't really test anything else
    sdl2.SDL_GameControllerUpdate()

def test_SDL_GameControllerGetAxisFromString(with_sdl):
    expected = {
        b'lefty': sdl2.SDL_CONTROLLER_AXIS_LEFTY,
        b'lefttrigger': sdl2.SDL_CONTROLLER_AXIS_TRIGGERLEFT,
        b'notanaxis': sdl2.SDL_CONTROLLER_AXIS_INVALID
    }
    for string in expected.keys():
        a = sdl2.SDL_GameControllerGetAxisFromString(string)
        assert a == expected[string]

def test_SDL_GameControllerGetStringForAxis(with_sdl):
    expected = {
        sdl2.SDL_CONTROLLER_AXIS_LEFTY: b'lefty',
        sdl2.SDL_CONTROLLER_AXIS_TRIGGERLEFT: b'lefttrigger',
        sdl2.SDL_CONTROLLER_AXIS_INVALID: None
    }
    for axis in expected.keys():
        s = sdl2.SDL_GameControllerGetStringForAxis(axis)
        assert s == expected[axis]

@pytest.mark.skip("not implemented")
def test_SDL_GameControllerGetBindForAxis(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerHasAxis(gamepads):
    pass

@pytest.mark.skip("not implemented")
def test_SDL_GameControllerGetAxis(gamepads):
    pass

def test_SDL_GameControllerGetButtonFromString(with_sdl):
    expected = {
        b'x': sdl2.SDL_CONTROLLER_BUTTON_X,
        b'dpup': sdl2.SDL_CONTROLLER_BUTTON_DPAD_UP,
        b'notabutton': sdl2.SDL_CONTROLLER_BUTTON_INVALID
    }
    for string in expected.keys():
        b = sdl2.SDL_GameControllerGetButtonFromString(string)
        assert b == expected[string]

def test_SDL_GameControllerGetStringForButton(with_sdl):
    expected = {
        sdl2.SDL_CONTROLLER_BUTTON_X: b'x',
        sdl2.SDL_CONTROLLER_BUTTON_DPAD_UP: b'dpup',
        sdl2.SDL_CONTROLLER_BUTTON_INVALID: None
    }
    for button in expected.keys():
        s = sdl2.SDL_GameControllerGetStringForButton(button)
        assert s == expected[button]

@pytest.mark.skip("not implemented")
def test_SDL_GameControllerGetBindForButton(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerHasButton(gamepads):
    pass

@pytest.mark.skip("not implemented")
def test_SDL_GameControllerGetButton(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerGetNumTouchpads(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerGetNumTouchpadFingers(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerGetTouchpadFinger(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerHasSensor(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerSetSensorEnabled(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerIsSensorEnabled(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2016, reason="not available")
def test_SDL_GameControllerGetSensorDataRate(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerGetSensorData(gamepads):
    pass

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2260, reason="not available")
def test_SDL_GameControllerGetSensorDataWithTimestamp(gamepads):
    pass

@pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
def test_SDL_GameControllerRumble(gamepads):
    # If we ever add an interactive test suite, this should be moved there
    for pad in gamepads:
        # 50% strength low-frequency, 25% high-frequency rumble for 500ms
        ret = sdl2.SDL_GameControllerRumble(pad, 32767, 16384, 500)
        assert ret in [-1, 0]

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerRumbleTriggers(gamepads):
    # If we ever add an interactive test suite, this should be moved there
    for pad in gamepads:
        # 50% strength left trigger, 25% right trigger rumble for 500ms
        ret = sdl2.SDL_GameControllerRumbleTriggers(pad, 32767, 16384, 500)
        assert ret in [-1, 0]

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_GameControllerHasSetLED(gamepads):
    # If we ever add an interactive test suite, this should be moved there
    for pad in gamepads:
        has_led = sdl2.SDL_GameControllerHasLED(pad)
        assert has_led in [SDL_FALSE, SDL_TRUE]
        expected = -1 if has_led == SDL_FALSE else 0
        cols = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
        for r, g, b in cols:
            ret = sdl2.SDL_GameControllerSetLED(pad, r, g, b)
            assert ret == expected

@pytest.mark.skipif(sdl2.dll.version < 2018, reason="not available")
def test_SDL_GameControllerHasRumble(gamepads):
    # If we ever add an interactive test suite, this should be moved there
    for pad in gamepads:
        has_rumble = sdl2.SDL_GameControllerHasRumble(pad)
        assert has_rumble in [SDL_FALSE, SDL_TRUE]

@pytest.mark.skipif(sdl2.dll.version < 2018, reason="not available")
def test_SDL_GameControllerHasRumbleTriggers(gamepads):
    # If we ever add an interactive test suite, this should be moved there
    for pad in gamepads:
        has_rumble_triggers = sdl2.SDL_GameControllerHasRumbleTriggers(pad)
        assert has_rumble_triggers in [SDL_FALSE, SDL_TRUE]

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2016, reason="not available")
def test_SDL_GameControllerSendEffect(gamepads):
    # Probably impossible to test since effect data would be specific
    # to each controller type?
    pass

@pytest.mark.skip("Only relevant on iOS, not testable by PySDL2")
@pytest.mark.skipif(sdl2.dll.version < 2018, reason="not available")
def test_SDL_GameControllerGetAppleSFSymbolsNameForButtonAxis(gamepads):
    # The following two functions are deliberatly ignored:
    # - SDL_GameControllerGetAppleSFSymbolsNameForButton
    # - SDL_GameControllerGetAppleSFSymbolsNameForAxis
    pass