File: controller_test.py

package info (click to toggle)
pygame 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,624 kB
  • sloc: ansic: 66,926; python: 48,742; javascript: 1,153; objc: 224; sh: 121; makefile: 59; cpp: 25
file content (357 lines) | stat: -rw-r--r-- 10,834 bytes parent folder | download | duplicates (2)
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
import unittest
import pygame
import pygame._sdl2.controller as controller
from pygame.tests.test_utils import prompt, question


class ControllerModuleTest(unittest.TestCase):
    def setUp(self):
        controller.init()

    def tearDown(self):
        controller.quit()

    def test_init(self):
        controller.quit()
        controller.init()
        self.assertTrue(controller.get_init())

    def test_init__multiple(self):
        controller.init()
        controller.init()
        self.assertTrue(controller.get_init())

    def test_quit(self):
        controller.quit()
        self.assertFalse(controller.get_init())

    def test_quit__multiple(self):
        controller.quit()
        controller.quit()
        self.assertFalse(controller.get_init())

    def test_get_init(self):
        self.assertTrue(controller.get_init())

    def test_get_eventstate(self):
        controller.set_eventstate(True)
        self.assertTrue(controller.get_eventstate())

        controller.set_eventstate(False)
        self.assertFalse(controller.get_eventstate())

        controller.set_eventstate(True)

    def test_get_count(self):
        self.assertGreaterEqual(controller.get_count(), 0)

    def test_is_controller(self):
        for i in range(controller.get_count()):
            if controller.is_controller(i):
                c = controller.Controller(i)
                self.assertIsInstance(c, controller.Controller)
                c.quit()
            else:
                with self.assertRaises(pygame._sdl2.sdl2.error):
                    c = controller.Controller(i)

        with self.assertRaises(TypeError):
            controller.is_controller("Test")

    def test_name_forindex(self):
        self.assertIsNone(controller.name_forindex(-1))


class ControllerTypeTest(unittest.TestCase):
    def setUp(self):
        controller.init()

    def tearDown(self):
        controller.quit()

    def _get_first_controller(self):
        for i in range(controller.get_count()):
            if controller.is_controller(i):
                return controller.Controller(i)

    def test_construction(self):
        c = self._get_first_controller()
        if c:
            self.assertIsInstance(c, controller.Controller)
        else:
            self.skipTest("No controller connected")

    def test__auto_init(self):
        c = self._get_first_controller()
        if c:
            self.assertTrue(c.get_init())
        else:
            self.skipTest("No controller connected")

    def test_get_init(self):
        c = self._get_first_controller()
        if c:
            self.assertTrue(c.get_init())
            c.quit()
            self.assertFalse(c.get_init())
        else:
            self.skipTest("No controller connected")

    def test_from_joystick(self):
        for i in range(controller.get_count()):
            if controller.is_controller(i):
                joy = pygame.joystick.Joystick(i)
                break
        else:
            self.skipTest("No controller connected")

        c = controller.Controller.from_joystick(joy)
        self.assertIsInstance(c, controller.Controller)

    def test_as_joystick(self):
        c = self._get_first_controller()
        if c:
            joy = c.as_joystick()
            self.assertIsInstance(joy, type(pygame.joystick.Joystick(0)))
        else:
            self.skipTest("No controller connected")

    def test_get_mapping(self):
        c = self._get_first_controller()
        if c:
            mapping = c.get_mapping()
            self.assertIsInstance(mapping, dict)
            self.assertIsNotNone(mapping["a"])
        else:
            self.skipTest("No controller connected")

    def test_set_mapping(self):
        c = self._get_first_controller()
        if c:
            mapping = c.get_mapping()
            mapping["a"] = "b3"
            mapping["y"] = "b0"
            c.set_mapping(mapping)
            new_mapping = c.get_mapping()

            self.assertEqual(len(mapping), len(new_mapping))
            for i in mapping:
                if mapping[i] not in ("a", "y"):
                    self.assertEqual(mapping[i], new_mapping[i])
                else:
                    if i == "a":
                        self.assertEqual(new_mapping[i], mapping["y"])
                    else:
                        self.assertEqual(new_mapping[i], mapping["a"])
        else:
            self.skipTest("No controller connected")


class ControllerInteractiveTest(unittest.TestCase):
    __tags__ = ["interactive"]

    def _get_first_controller(self):
        for i in range(controller.get_count()):
            if controller.is_controller(i):
                return controller.Controller(i)

    def setUp(self):
        controller.init()

    def tearDown(self):
        controller.quit()

    def test__get_count_interactive(self):
        prompt(
            "Please connect at least one controller "
            "before the test for controller.get_count() starts"
        )

        # Reset the number of joysticks counted
        controller.quit()
        controller.init()

        joystick_num = controller.get_count()
        ans = question(
            "get_count() thinks there are {} joysticks "
            "connected. Is that correct?".format(joystick_num)
        )

        self.assertTrue(ans)

    def test_set_eventstate_on_interactive(self):
        c = self._get_first_controller()
        if not c:
            self.skipTest("No controller connected")

        pygame.display.init()
        pygame.font.init()

        screen = pygame.display.set_mode((400, 400))
        font = pygame.font.Font(None, 20)
        running = True

        screen.fill((255, 255, 255))
        screen.blit(
            font.render("Press button 'x' (on ps4) or 'a' (on xbox).", True, (0, 0, 0)),
            (0, 0),
        )
        pygame.display.update()

        controller.set_eventstate(True)

        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                if event.type == pygame.CONTROLLERBUTTONDOWN:
                    running = False

        pygame.display.quit()
        pygame.font.quit()

    def test_set_eventstate_off_interactive(self):
        c = self._get_first_controller()
        if not c:
            self.skipTest("No controller connected")

        pygame.display.init()
        pygame.font.init()

        screen = pygame.display.set_mode((400, 400))
        font = pygame.font.Font(None, 20)
        running = True

        screen.fill((255, 255, 255))
        screen.blit(
            font.render("Press button 'x' (on ps4) or 'a' (on xbox).", True, (0, 0, 0)),
            (0, 0),
        )
        pygame.display.update()

        controller.set_eventstate(False)

        while running:
            for event in pygame.event.get(pygame.QUIT):
                if event:
                    running = False

            if c.get_button(pygame.CONTROLLER_BUTTON_A):
                if pygame.event.peek(pygame.CONTROLLERBUTTONDOWN):
                    pygame.display.quit()
                    pygame.font.quit()
                    self.fail()
                else:
                    running = False

        pygame.display.quit()
        pygame.font.quit()

    def test_get_button_interactive(self):
        c = self._get_first_controller()
        if not c:
            self.skipTest("No controller connected")

        pygame.display.init()
        pygame.font.init()

        screen = pygame.display.set_mode((400, 400))
        font = pygame.font.Font(None, 20)
        running = True

        label1 = font.render(
            "Press button 'x' (on ps4) or 'a' (on xbox).", True, (0, 0, 0)
        )

        label2 = font.render(
            'The two values should match up. Press "y" or "n" to confirm.',
            True,
            (0, 0, 0),
        )

        is_pressed = [False, False]  # event, get_button()
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                if event.type == pygame.CONTROLLERBUTTONDOWN and event.button == 0:
                    is_pressed[0] = True
                if event.type == pygame.CONTROLLERBUTTONUP and event.button == 0:
                    is_pressed[0] = False

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_y:
                        running = False
                    if event.key == pygame.K_n:
                        running = False
                        pygame.display.quit()
                        pygame.font.quit()
                        self.fail()

            is_pressed[1] = c.get_button(pygame.CONTROLLER_BUTTON_A)

            screen.fill((255, 255, 255))
            screen.blit(label1, (0, 0))
            screen.blit(label2, (0, 20))
            screen.blit(font.render(str(is_pressed), True, (0, 0, 0)), (0, 40))
            pygame.display.update()

        pygame.display.quit()
        pygame.font.quit()

    def test_get_axis_interactive(self):
        c = self._get_first_controller()
        if not c:
            self.skipTest("No controller connected")

        pygame.display.init()
        pygame.font.init()

        screen = pygame.display.set_mode((400, 400))
        font = pygame.font.Font(None, 20)
        running = True

        label1 = font.render(
            "Press down the right trigger. The value on-screen should", True, (0, 0, 0)
        )

        label2 = font.render(
            "indicate how far the trigger is pressed down. This value should",
            True,
            (0, 0, 0),
        )

        label3 = font.render(
            'be in the range of 0-32767. Press "y" or "n" to confirm.', True, (0, 0, 0)
        )

        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_y:
                        running = False
                    if event.key == pygame.K_n:
                        running = False
                        pygame.display.quit()
                        pygame.font.quit()
                        self.fail()

            right_trigger = c.get_axis(pygame.CONTROLLER_AXIS_TRIGGERRIGHT)

            screen.fill((255, 255, 255))
            screen.blit(label1, (0, 0))
            screen.blit(label2, (0, 20))
            screen.blit(label3, (0, 40))
            screen.blit(font.render(str(right_trigger), True, (0, 0, 0)), (0, 60))
            pygame.display.update()

        pygame.display.quit()
        pygame.font.quit()


if __name__ == "__main__":
    unittest.main()