File: joystick_test.py

package info (click to toggle)
pysdl2 0.9.7%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,824 kB
  • sloc: python: 17,244; makefile: 195; sh: 32
file content (293 lines) | stat: -rw-r--r-- 11,363 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
import sys
import pytest
from ctypes import create_string_buffer, byref, c_int
import sdl2
from sdl2 import SDL_Init, SDL_Quit, SDL_INIT_JOYSTICK
from sdl2.events import SDL_QUERY, SDL_ENABLE, SDL_IGNORE
from sdl2.stdinc import SDL_TRUE, SDL_FALSE
from sdl2.error import SDL_GetError, SDL_ClearError
from sdl2 import joystick


def test_SDL_JoystickGetGUIDFromString():
    guid_str = b'030000007e050000060300001c3a0000' # Wiimote on macOS
    expected = [3, 0, 0, 0, 126, 5, 0, 0, 6, 3, 0, 0, 28, 58, 0, 0]
    guid = joystick.SDL_JoystickGetGUIDFromString(guid_str)
    assert list(guid.data) == expected

def test_SDL_JoystickGetGUIDString():
    guid_str = b'030000007e050000060300001c3a0000' # Wiimote on macOS
    guid = joystick.SDL_JoystickGetGUIDFromString(guid_str)
    buff = create_string_buffer(33)
    joystick.SDL_JoystickGetGUIDString(guid, buff, 33) # Get GUID string
    assert guid_str == buff.value

def test_SDL_InitJoystick():
    ret = SDL_Init(SDL_INIT_JOYSTICK)
    SDL_Quit()
    assert ret == 0

def test_SDL_NumJoysticks():
    if SDL_Init(SDL_INIT_JOYSTICK) != 0:
        pytest.skip("joystick subsystem not supported")
    retval = joystick.SDL_NumJoysticks()
    SDL_Quit()
    assert retval >= 0


skipmsg = "joystick subsystem not supported"
@pytest.mark.skipif(SDL_Init(SDL_INIT_JOYSTICK) != 0, reason=skipmsg)
class TestSDLJoystick(object):
    __tags__ = ["sdl"]

    @classmethod
    def setup_class(cls):
        SDL_Init(SDL_INIT_JOYSTICK)
        num = joystick.SDL_NumJoysticks()
        if num < 1:
            pytest.skip("no available joystick devices")
        cls.jcount = num

    @classmethod
    def teardown_class(cls):
        SDL_Quit()

    def setup_method(self):
        SDL_ClearError()

    def test_SDL_JoystickNameForIndex(self):
        for index in range(self.jcount):
            name = joystick.SDL_JoystickNameForIndex(index)
            assert type(name) in (str, bytes)

    def test_SDL_JoystickOpenClose(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            assert isinstance(stick.contents, joystick.SDL_Joystick)
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickName(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            name = joystick.SDL_JoystickName(stick)
            assert type(name) in (str, bytes)
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickGetDeviceGUID(self):
        for index in range(self.jcount):
            guid = joystick.SDL_JoystickGetDeviceGUID(index)
            assert isinstance(guid, joystick.SDL_JoystickGUID)
            guidlist = list(guid.data)
            assert isinstance(guidlist[0], int)

    def test_SDL_JoystickGetGUID(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            guid = joystick.SDL_JoystickGetGUID(stick)
            assert isinstance(guid, joystick.SDL_JoystickGUID)
            guidlist = list(guid.data)
            assert isinstance(guidlist[0], int)
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickGetAttached(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            ret = joystick.SDL_JoystickGetAttached(stick)
            assert ret in [SDL_FALSE, SDL_TRUE]
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickInstanceID(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            ret = joystick.SDL_JoystickInstanceID(stick)
            assert ret > 0
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickNumAxes(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            assert isinstance(stick.contents, joystick.SDL_Joystick)
            axes = joystick.SDL_JoystickNumAxes(stick)
            assert axes >= 0
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickNumBalls(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            assert isinstance(stick.contents, joystick.SDL_Joystick)
            balls = joystick.SDL_JoystickNumBalls(stick)
            assert balls >= 0
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickNumHats(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            assert isinstance(stick.contents, joystick.SDL_Joystick)
            hats = joystick.SDL_JoystickNumHats(stick)
            assert hats >= 0
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickNumButtons(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            assert isinstance(stick.contents, joystick.SDL_Joystick)
            buttons = joystick.SDL_JoystickNumButtons(stick)
            assert buttons >= 0
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickUpdate(self):
        # returns void, not sure what else to test here
        joystick.SDL_JoystickUpdate()

    def test_SDL_JoystickEventState(self):
        for state in (SDL_IGNORE, SDL_ENABLE):
            news = joystick.SDL_JoystickEventState(state)
            assert news == state
            query = joystick.SDL_JoystickEventState(SDL_QUERY)
            assert query == state

    def test_SDL_JoystickGetAxis(self):
        sticks = [joystick.SDL_JoystickOpen(i) for i in range(self.jcount)]
        numaxes = [joystick.SDL_JoystickNumAxes(s) for s in sticks]
        if not any(numaxes):
            pytest.skip("no axis on any connected controller")
        for stick in sticks:
            for axis in range(joystick.SDL_JoystickNumAxes(stick)):
                val = joystick.SDL_JoystickGetAxis(stick, axis)
                assert -32768 <= val <= 32767
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickGetBall(self):
        sticks = [joystick.SDL_JoystickOpen(i) for i in range(self.jcount)]
        numball = [joystick.SDL_JoystickNumBalls(s) for s in sticks]
        if not any(numball):
            pytest.skip("no trackball on any connected controller")
        dx, dy = c_int(0), c_int(0)
        get_ball = joystick.SDL_JoystickGetBall
        for stick in sticks:
            for ball in range(joystick.SDL_JoystickNumBalls(stick)):
                ret = get_ball(stick, ball, byref(dx), byref(dy))
                err = SDL_GetError()
                assert ret == 0
                assert len(err) == 0
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickGetHat(self):
        hatvals = [
            joystick.SDL_HAT_UP, joystick.SDL_HAT_DOWN, joystick.SDL_HAT_LEFT,
            joystick.SDL_HAT_RIGHT, joystick.SDL_HAT_CENTERED,
            joystick.SDL_HAT_LEFTUP, joystick.SDL_HAT_LEFTDOWN,
            joystick.SDL_HAT_RIGHTUP, joystick.SDL_HAT_RIGHTDOWN
        ]
        sticks = [joystick.SDL_JoystickOpen(i) for i in range(self.jcount)]
        numhats = [joystick.SDL_JoystickNumHats(s) for s in sticks]
        if not any(numhats):
            pytest.skip("no POV hat on any connected controller")
        for stick in sticks:
            for hat in range(joystick.SDL_JoystickNumHats(stick)):
                val = joystick.SDL_JoystickGetHat(stick, hat)
                assert val in hatvals
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickGetButton(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            for button in range(joystick.SDL_JoystickNumButtons(stick)):
                val = joystick.SDL_JoystickGetButton(stick, button)
                assert val in [0, 1]
            joystick.SDL_JoystickClose(stick)

    def test_SDL_JoystickCurrentPowerLevel(self):
        levels = [
            joystick.SDL_JOYSTICK_POWER_UNKNOWN,
            joystick.SDL_JOYSTICK_POWER_EMPTY,
            joystick.SDL_JOYSTICK_POWER_LOW,
            joystick.SDL_JOYSTICK_POWER_MEDIUM,
            joystick.SDL_JOYSTICK_POWER_FULL,
            joystick.SDL_JOYSTICK_POWER_WIRED,
            joystick.SDL_JOYSTICK_POWER_MAX,
        ]
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            pwr = joystick.SDL_JoystickCurrentPowerLevel(stick)
            err = SDL_GetError()
            assert pwr in levels
            assert len(err) == 0
            joystick.SDL_JoystickClose(stick)

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickFromInstanceID(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetVendor(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetProduct(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetProductVersion(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetAxisInitialState(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetType(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetDeviceVendor(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetDeviceProduct(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetDeviceProductVersion(self):
        pass

    @pytest.mark.skip("not implemented")
    def test_SDL_JoystickGetDeviceType(self):
        pass

    @pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
    def test_SDL_JoystickGetDeviceInstanceID(self):
        for index in range(self.jcount):
            ret = joystick.SDL_JoystickGetDeviceInstanceID(index)
            assert ret > 0

    @pytest.mark.skipif(sdl2.dll.version < 2007, reason="not available")
    def test_SDL_LockUnlockJoysticks(self):
        # NOTE: not sure how better to test these, since I don't know if
        # they'd even be useful at all in Python given the GIL
        joystick.SDL_LockJoysticks()
        joystick.SDL_UnlockJoysticks()

    @pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
    def test_SDL_JoystickGetPlayerIndex(self):
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            player = joystick.SDL_JoystickGetPlayerIndex(stick)
            assert player in [-1, 0, 1, 2, 3]
            joystick.SDL_JoystickClose(stick)

    @pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
    def test_SDL_JoystickGetDevicePlayerIndex(self):
        for index in range(self.jcount):
            player = joystick.SDL_JoystickGetDevicePlayerIndex(index)
            assert player in [-1, 0, 1, 2, 3]

    @pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
    def test_SDL_JoystickRumble(self):
        # If we ever add an interactive test suite, this should be moved there
        for index in range(self.jcount):
            stick = joystick.SDL_JoystickOpen(index)
            # 50% strength low-frequency, 25% high-frequency rumble for 500ms
            ret = joystick.SDL_JoystickRumble(stick, 32767, 16384, 500)
            assert ret in [-1, 0]
            joystick.SDL_JoystickClose(stick)