/
event_test.py
679 lines (506 loc) 路 21.5 KB
/
event_test.py
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import os
import sys
import unittest
import pygame
from pygame.compat import as_unicode
PY3 = sys.version_info >= (3, 0, 0)
################################################################################
EVENT_TYPES = (
# pygame.NOEVENT,
# pygame.ACTIVEEVENT,
pygame.KEYDOWN,
pygame.KEYUP,
pygame.MOUSEMOTION,
pygame.MOUSEBUTTONDOWN,
pygame.MOUSEBUTTONUP,
pygame.JOYAXISMOTION,
pygame.JOYBALLMOTION,
pygame.JOYHATMOTION,
pygame.JOYBUTTONDOWN,
pygame.JOYBUTTONUP,
pygame.VIDEORESIZE,
pygame.VIDEOEXPOSE,
pygame.QUIT,
pygame.SYSWMEVENT,
pygame.USEREVENT,
# pygame.NUMEVENTS,
)
NAMES_AND_EVENTS = (
("NoEvent", pygame.NOEVENT),
("ActiveEvent", pygame.ACTIVEEVENT),
("KeyDown", pygame.KEYDOWN),
("KeyUp", pygame.KEYUP),
("MouseMotion", pygame.MOUSEMOTION),
("MouseButtonDown", pygame.MOUSEBUTTONDOWN),
("MouseButtonUp", pygame.MOUSEBUTTONUP),
("JoyAxisMotion", pygame.JOYAXISMOTION),
("JoyBallMotion", pygame.JOYBALLMOTION),
("JoyHatMotion", pygame.JOYHATMOTION),
("JoyButtonDown", pygame.JOYBUTTONDOWN),
("JoyButtonUp", pygame.JOYBUTTONUP),
("VideoResize", pygame.VIDEORESIZE),
("VideoExpose", pygame.VIDEOEXPOSE),
("Quit", pygame.QUIT),
("SysWMEvent", pygame.SYSWMEVENT),
("MidiIn", pygame.MIDIIN),
("MidiOut", pygame.MIDIOUT),
("UserEvent", pygame.USEREVENT),
("Unknown", 0xFFFF),
)
# Add in any SDL 2 specific events.
if pygame.get_sdl_version()[0] >= 2:
NAMES_AND_EVENTS += (
("FingerMotion", pygame.FINGERMOTION),
("FingerDown", pygame.FINGERDOWN),
("FingerUp", pygame.FINGERUP),
("MultiGesture", pygame.MULTIGESTURE),
("MouseWheel", pygame.MOUSEWHEEL),
("TextInput", pygame.TEXTINPUT),
("TextEditing", pygame.TEXTEDITING),
("WindowEvent", pygame.WINDOWEVENT),
("ControllerAxisMotion", pygame.CONTROLLERAXISMOTION),
("ControllerButtonDown", pygame.CONTROLLERBUTTONDOWN),
("ControllerButtonUp", pygame.CONTROLLERBUTTONUP),
("ControllerDeviceAdded", pygame.CONTROLLERDEVICEADDED),
("ControllerDeviceRemoved", pygame.CONTROLLERDEVICEREMOVED),
("ControllerDeviceMapped", pygame.CONTROLLERDEVICEREMAPPED),
("DropFile", pygame.DROPFILE),
)
# Add in any SDL 2.0.4 specific events.
if pygame.get_sdl_version() >= (2, 0, 4):
NAMES_AND_EVENTS += (
("AudioDeviceAdded", pygame.AUDIODEVICEADDED),
("AudioDeviceRemoved", pygame.AUDIODEVICEREMOVED),
)
# Add in any SDL 2.0.5 specific events.
if pygame.get_sdl_version() >= (2, 0, 5):
NAMES_AND_EVENTS += (
("DropText", pygame.DROPTEXT),
("DropBegin", pygame.DROPBEGIN),
("DropComplete", pygame.DROPCOMPLETE),
)
class EventTypeTest(unittest.TestCase):
def test_Event(self):
"""Ensure an Event object can be created."""
e = pygame.event.Event(pygame.USEREVENT, some_attr=1, other_attr="1")
self.assertEqual(e.some_attr, 1)
self.assertEqual(e.other_attr, "1")
# Event now uses tp_dictoffset and tp_members: request 62
# on Motherhamster Bugzilla.
self.assertEqual(e.type, pygame.USEREVENT)
self.assertIs(e.dict, e.__dict__)
e.some_attr = 12
self.assertEqual(e.some_attr, 12)
e.new_attr = 15
self.assertEqual(e.new_attr, 15)
# For Python 2.x a TypeError is raised for a readonly member;
# for Python 3.x it is an AttributeError.
self.assertRaises((TypeError, AttributeError), setattr, e, "type", 0)
self.assertRaises((TypeError, AttributeError), setattr, e, "dict", None)
# Ensure attributes are visible to dir(), part of the original
# posted request.
d = dir(e)
attrs = ("type", "dict", "__dict__", "some_attr", "other_attr", "new_attr")
for attr in attrs:
self.assertIn(attr, d)
def test_as_str(self):
# Bug reported on Pygame mailing list July 24, 2011:
# For Python 3.x str(event) to raises an UnicodeEncodeError when
# an event attribute is a string with a non-ascii character.
try:
str(pygame.event.Event(EVENT_TYPES[0], a=as_unicode(r"\xed")))
except UnicodeEncodeError:
self.fail("Event object raised exception for non-ascii character")
# Passed.
race_condition_notification = """
This test is dependent on timing. The event queue is cleared in preparation for
tests. There is a small window where outside events from the OS may have effected
results. Try running the test again.
"""
class EventModuleArgsTest(unittest.TestCase):
def setUp(self):
pygame.display.init()
pygame.event.clear()
def tearDown(self):
pygame.display.quit()
def test_get(self):
pygame.event.get()
pygame.event.get(None)
pygame.event.get(None, True)
pygame.event.get(pump=False)
pygame.event.get(pump=True)
pygame.event.get(eventtype=None)
pygame.event.get(eventtype=[pygame.KEYUP, pygame.KEYDOWN])
pygame.event.get(eventtype=pygame.USEREVENT, pump=False)
def test_clear(self):
pygame.event.clear()
pygame.event.clear(None)
pygame.event.clear(None, True)
pygame.event.clear(pump=False)
pygame.event.clear(pump=True)
pygame.event.clear(eventtype=None)
pygame.event.clear(eventtype=[pygame.KEYUP, pygame.KEYDOWN])
pygame.event.clear(eventtype=pygame.USEREVENT, pump=False)
def test_peek(self):
pygame.event.peek()
pygame.event.peek(None)
pygame.event.peek(None, True)
pygame.event.peek(pump=False)
pygame.event.peek(pump=True)
pygame.event.peek(eventtype=None)
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN])
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False)
class EventModuleTest(unittest.TestCase):
def _assertCountEqual(self, *args, **kwargs):
# Handle method name differences between Python versions.
if PY3:
self.assertCountEqual(*args, **kwargs)
else:
self.assertItemsEqual(*args, **kwargs)
def setUp(self):
pygame.display.init()
pygame.event.clear() # flush events
def tearDown(self):
pygame.event.clear() # flush events
pygame.display.quit()
def test_event_numevents(self):
"""Ensures NUMEVENTS does not exceed the maximum SDL number of events.
"""
# Ref: https://www.libsdl.org/tmp/SDL/include/SDL_events.h
MAX_SDL_EVENTS = 0xFFFF + 1 # SDL_LASTEVENT = 0xFFFF
self.assertLessEqual(pygame.NUMEVENTS, MAX_SDL_EVENTS)
def test_event_attribute(self):
e1 = pygame.event.Event(pygame.USEREVENT, attr1="attr1")
self.assertEqual(e1.attr1, "attr1")
def test_set_blocked(self):
"""Ensure events can be blocked from the queue."""
event = EVENT_TYPES[0]
pygame.event.set_blocked(event)
self.assertTrue(pygame.event.get_blocked(event))
pygame.event.post(pygame.event.Event(event))
ret = pygame.event.get()
should_be_blocked = [e for e in ret if e.type == event]
self.assertEqual(should_be_blocked, [])
def test_set_blocked__event_sequence(self):
"""Ensure a sequence of event types can be blocked."""
event_types = [
pygame.KEYDOWN,
pygame.KEYUP,
pygame.MOUSEMOTION,
pygame.MOUSEBUTTONDOWN,
pygame.MOUSEBUTTONUP,
]
pygame.event.set_blocked(event_types)
for etype in event_types:
self.assertTrue(pygame.event.get_blocked(etype))
def test_set_blocked_all(self):
"""Ensure all events can be unblocked at once."""
pygame.event.set_blocked(None)
for e in EVENT_TYPES:
self.assertTrue(pygame.event.get_blocked(e))
def test_post__and_poll(self):
"""Ensure events can be posted to the queue."""
e1 = pygame.event.Event(pygame.USEREVENT, attr1="attr1")
pygame.event.post(e1)
posted_event = pygame.event.poll()
self.assertEqual(e1.attr1, posted_event.attr1, race_condition_notification)
# fuzzing event types
for i in range(1, 11):
pygame.event.post(pygame.event.Event(EVENT_TYPES[i]))
self.assertEqual(
pygame.event.poll().type, EVENT_TYPES[i], race_condition_notification
)
def test_post_large_user_event(self):
pygame.event.post(pygame.event.Event(pygame.USEREVENT, {"a": "a" * 1024}))
e = pygame.event.poll()
self.assertEqual(e.type, pygame.USEREVENT)
self.assertEqual(e.a, "a" * 1024)
def test_get(self):
"""Ensure get() retrieves all the events on the queue."""
event_cnt = 10
for _ in range(event_cnt):
pygame.event.post(pygame.event.Event(pygame.USEREVENT))
queue = pygame.event.get()
self.assertEqual(len(queue), event_cnt)
self.assertTrue(all(e.type == pygame.USEREVENT for e in queue))
def test_get_type(self):
ev = pygame.event.Event(pygame.USEREVENT)
pygame.event.post(ev)
queue = pygame.event.get(pygame.USEREVENT)
self.assertEqual(len(queue), 1)
self.assertEqual(queue[0].type, pygame.USEREVENT)
def test_get__empty_queue(self):
"""Ensure get() works correctly on an empty queue."""
expected_events = []
pygame.event.clear()
# Ensure all events can be checked.
retrieved_events = pygame.event.get()
self.assertListEqual(retrieved_events, expected_events)
# Ensure events can be checked individually.
for event_type in EVENT_TYPES:
retrieved_events = pygame.event.get(event_type)
self.assertListEqual(retrieved_events, expected_events)
# Ensure events can be checked as a sequence.
retrieved_events = pygame.event.get(EVENT_TYPES)
self.assertListEqual(retrieved_events, expected_events)
def test_get__event_sequence(self):
"""Ensure get() can handle a sequence of event types."""
event_types = [pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEMOTION]
other_event_type = pygame.MOUSEBUTTONUP
# Test when no events in the queue.
expected_events = []
pygame.event.clear()
retrieved_events = pygame.event.get(event_types)
self._assertCountEqual(retrieved_events, expected_events)
# Test when an event type not in the list is in the queue.
expected_events = []
pygame.event.clear()
pygame.event.post(pygame.event.Event(other_event_type))
retrieved_events = pygame.event.get(event_types)
self._assertCountEqual(retrieved_events, expected_events)
# Test when 1 event type in the list is in the queue.
expected_events = [pygame.event.Event(event_types[0])]
pygame.event.clear()
pygame.event.post(expected_events[0])
retrieved_events = pygame.event.get(event_types)
self._assertCountEqual(retrieved_events, expected_events)
# Test all events in the list are in the queue.
pygame.event.clear()
expected_events = []
for etype in event_types:
expected_events.append(pygame.event.Event(etype))
pygame.event.post(expected_events[-1])
retrieved_events = pygame.event.get(event_types)
self._assertCountEqual(retrieved_events, expected_events)
def test_clear(self):
"""Ensure clear() removes all the events on the queue."""
for e in EVENT_TYPES:
pygame.event.post(pygame.event.Event(e))
poll_event = pygame.event.poll()
self.assertNotEqual(poll_event.type, pygame.NOEVENT)
pygame.event.clear()
poll_event = pygame.event.poll()
self.assertEqual(poll_event.type, pygame.NOEVENT, race_condition_notification)
def test_clear__empty_queue(self):
"""Ensure clear() works correctly on an empty queue."""
expected_events = []
pygame.event.clear()
# Test calling clear() on an already empty queue.
pygame.event.clear()
retrieved_events = pygame.event.get()
self.assertListEqual(retrieved_events, expected_events)
def test_clear__event_sequence(self):
"""Ensure a sequence of event types can be cleared from the queue."""
cleared_event_types = EVENT_TYPES[:5]
expected_event_types = EVENT_TYPES[5:10]
expected_events = []
# Add the events to the queue.
for etype in cleared_event_types:
pygame.event.post(pygame.event.Event(etype))
for etype in expected_events:
expected_events.append(pygame.event.Event(etype))
pygame.event.post(expected_events[-1])
# Clear the cleared_events from the queue.
pygame.event.clear(cleared_event_types)
# Check the rest of the events in the queue.
remaining_events = pygame.event.get()
self._assertCountEqual(remaining_events, expected_events)
def test_event_name(self):
"""Ensure event_name() returns the correct event name."""
for expected_name, event in NAMES_AND_EVENTS:
self.assertEqual(
pygame.event.event_name(event), expected_name, "0x{:X}".format(event)
)
def test_event_name__userevent_range(self):
"""Ensures event_name() returns the correct name for user events.
Tests the full range of user events.
"""
expected_name = "UserEvent"
for event in range(pygame.USEREVENT, pygame.NUMEVENTS):
self.assertEqual(
pygame.event.event_name(event), expected_name, "0x{:X}".format(event)
)
def test_event_name__userevent_boundary(self):
"""Ensures event_name() does not return 'UserEvent' for events
just outside the user event range.
"""
unexpected_name = "UserEvent"
for event in (pygame.USEREVENT - 1, pygame.NUMEVENTS):
self.assertNotEqual(
pygame.event.event_name(event), unexpected_name, "0x{:X}".format(event)
)
def test_wait(self):
"""Ensure wait() waits for an event on the queue."""
event = pygame.event.Event(EVENT_TYPES[0])
pygame.event.post(event)
wait_event = pygame.event.wait()
self.assertEqual(wait_event.type, event.type)
def test_peek(self):
"""Ensure queued events can be peeked at."""
event_types = [pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEMOTION]
for event_type in event_types:
pygame.event.post(pygame.event.Event(event_type))
# Ensure events can be checked individually.
for event_type in event_types:
self.assertTrue(pygame.event.peek(event_type))
# Ensure events can be checked as a sequence.
self.assertTrue(pygame.event.peek(event_types))
def test_peek__event_sequence(self):
"""Ensure peek() can handle a sequence of event types."""
event_types = [pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEMOTION]
other_event_type = pygame.MOUSEBUTTONUP
# Test when no events in the queue.
pygame.event.clear()
peeked = pygame.event.peek(event_types)
self.assertFalse(peeked)
# Test when an event type not in the list is in the queue.
pygame.event.clear()
pygame.event.post(pygame.event.Event(other_event_type))
peeked = pygame.event.peek(event_types)
self.assertFalse(peeked)
# Test when 1 event type in the list is in the queue.
pygame.event.clear()
pygame.event.post(pygame.event.Event(event_types[0]))
peeked = pygame.event.peek(event_types)
self.assertTrue(peeked)
# Test all events in the list are in the queue.
pygame.event.clear()
for etype in event_types:
pygame.event.post(pygame.event.Event(etype))
peeked = pygame.event.peek(event_types)
self.assertTrue(peeked)
def test_peek__empty_queue(self):
"""Ensure peek() works correctly on an empty queue."""
pygame.event.clear()
# Ensure all events can be checked.
peeked = pygame.event.peek()
self.assertFalse(peeked)
# Ensure events can be checked individually.
for event_type in EVENT_TYPES:
peeked = pygame.event.peek(event_type)
self.assertFalse(peeked)
# Ensure events can be checked as a sequence.
peeked = pygame.event.peek(EVENT_TYPES)
self.assertFalse(peeked)
def test_set_allowed(self):
"""Ensure a blocked event type can be unblocked/allowed."""
event = EVENT_TYPES[0]
pygame.event.set_blocked(event)
self.assertTrue(pygame.event.get_blocked(event))
pygame.event.set_allowed(event)
self.assertFalse(pygame.event.get_blocked(event))
def test_set_allowed__event_sequence(self):
"""Ensure a sequence of blocked event types can be unblocked/allowed.
"""
event_types = [
pygame.KEYDOWN,
pygame.KEYUP,
pygame.MOUSEMOTION,
pygame.MOUSEBUTTONDOWN,
pygame.MOUSEBUTTONUP,
]
pygame.event.set_blocked(event_types)
pygame.event.set_allowed(event_types)
for etype in event_types:
self.assertFalse(pygame.event.get_blocked(etype))
def test_set_allowed_all(self):
"""Ensure all events can be unblocked/allowed at once."""
pygame.event.set_blocked(None)
for e in EVENT_TYPES:
self.assertTrue(pygame.event.get_blocked(e))
pygame.event.set_allowed(None)
for e in EVENT_TYPES:
self.assertFalse(pygame.event.get_blocked(e))
def test_pump(self):
"""Ensure pump() functions properly."""
pygame.event.pump()
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
'requires the SDL_VIDEODRIVER to be a non "dummy" value',
)
def test_set_grab__and_get_symmetric(self):
"""Ensure event grabbing can be enabled and disabled.
WARNING: Moving the mouse off the display during this test can cause it
to fail.
"""
surf = pygame.display.set_mode((10, 10))
pygame.event.set_grab(True)
self.assertTrue(pygame.event.get_grab())
pygame.event.set_grab(False)
self.assertFalse(pygame.event.get_grab())
def test_event_equality(self):
a = pygame.event.Event(EVENT_TYPES[0], a=1)
b = pygame.event.Event(EVENT_TYPES[0], a=1)
c = pygame.event.Event(EVENT_TYPES[1], a=1)
d = pygame.event.Event(EVENT_TYPES[0], a=2)
self.assertTrue(a == a)
self.assertFalse(a != a)
self.assertTrue(a == b)
self.assertFalse(a != b)
self.assertTrue(a != c)
self.assertFalse(a == c)
self.assertTrue(a != d)
self.assertFalse(a == d)
def test_custom_type(self):
atype = pygame.event.custom_type()
atype2 = pygame.event.custom_type()
self.assertEqual(atype, atype2 - 1)
ev = pygame.event.Event(atype)
pygame.event.post(ev)
queue = pygame.event.get(atype)
self.assertEqual(len(queue), 1)
self.assertEqual(queue[0].type, atype)
def test_get_blocked(self):
"""Ensure an event's blocked state can be retrieved."""
# Test each event is not blocked.
pygame.event.set_allowed(None)
for etype in EVENT_TYPES:
blocked = pygame.event.get_blocked(etype)
self.assertFalse(blocked)
# Test each event type is blocked.
pygame.event.set_blocked(None)
for etype in EVENT_TYPES:
blocked = pygame.event.get_blocked(etype)
self.assertTrue(blocked)
def test_get_blocked__event_sequence(self):
"""Ensure get_blocked() can handle a sequence of event types."""
event_types = [
pygame.KEYDOWN,
pygame.KEYUP,
pygame.MOUSEMOTION,
pygame.MOUSEBUTTONDOWN,
pygame.MOUSEBUTTONUP,
]
# Test no event types in the list are blocked.
blocked = pygame.event.get_blocked(event_types)
self.assertFalse(blocked)
# Test when 1 event type in the list is blocked.
pygame.event.set_blocked(event_types[2])
blocked = pygame.event.get_blocked(event_types)
self.assertTrue(blocked)
# Test all event types in the list are blocked.
pygame.event.set_blocked(event_types)
blocked = pygame.event.get_blocked(event_types)
self.assertTrue(blocked)
def todo_test_get_grab(self):
# __doc__ (as of 2008-08-02) for pygame.event.get_grab:
# pygame.event.get_grab(): return bool
# test if the program is sharing input devices
#
# Returns true when the input events are grabbed for this application.
# Use pygame.event.set_grab() to control this state.
#
self.fail()
def todo_test_poll(self):
# __doc__ (as of 2008-08-02) for pygame.event.poll:
# pygame.event.poll(): return Event
# get a single event from the queue
#
# Returns a single event from the queue. If the event queue is empty
# an event of type pygame.NOEVENT will be returned immediately. The
# returned event is removed from the queue.
#
self.fail()
################################################################################
if __name__ == "__main__":
unittest.main()