1
#################################### IMPORTS ###################################
4
if __name__ == '__main__':
6
pkg_dir = os.path.split(os.path.abspath(__file__))[0]
7
parent_dir, pkg_name = os.path.split(pkg_dir)
8
is_pygame_pkg = (pkg_name == 'tests' and
9
os.path.split(parent_dir)[1] == 'pygame')
11
sys.path.insert(0, parent_dir)
13
is_pygame_pkg = __name__.startswith('pygame.tests.')
17
from pygame.compat import as_unicode
19
################################################################################
21
class EventTypeTest(unittest.TestCase):
23
# __doc__ (as of 2008-08-02) for pygame.event.Event:
25
# pygame.event.Event(type, dict): return Event
26
# pygame.event.Event(type, **attributes): return Event
27
# create a new event object
29
# Creates a new event with the given type. The event is created with
30
# the given attributes and values. The attributes can come from a
31
# dictionary argument, or as string keys from a dictionary.
33
# The given attributes will be readonly attributes on the new event
34
# object itself. These are the only attributes on the Event object,
35
# there are no methods attached to Event objects.
37
e = pygame.event.Event(pygame.USEREVENT, some_attr=1, other_attr='1')
39
self.assertEquals(e.some_attr, 1)
40
self.assertEquals(e.other_attr, "1")
42
# Event now uses tp_dictoffset and tp_members: request 62
43
# on Motherhamster Bugzilla.
44
self.assertEquals(e.type, pygame.USEREVENT)
45
self.assert_(e.dict is e.__dict__)
47
self.assertEquals(e.some_attr, 12)
49
self.assertEquals(e.new_attr, 15)
51
# For Python 2.x a TypeError is raised for a readonly member;
52
# for Python 3.x it is an AttributeError.
53
self.assertRaises((TypeError, AttributeError), setattr, e, 'type', 0)
54
self.assertRaises((TypeError, AttributeError), setattr, e, 'dict', None)
56
# Ensure attributes are visible to dir(), part of the original
59
self.assert_('type' in d)
60
self.assert_('dict' in d)
61
self.assert_('__dict__' in d)
62
self.assert_('some_attr' in d)
63
self.assert_('other_attr' in d)
64
self.assert_('new_attr' in d)
66
def test_as_str(self):
67
# Bug reported on Pygame mailing list July 24, 2011:
68
# For Python 3.x str(event) to raises an UnicodeEncodeError when
69
# an event attribute is a string with a non-ascii character.
71
str(pygame.event.Event(1, a=as_unicode(r"\xed")))
72
except UnicodeEncodeError:
73
self.fail("Event object raised exception for non-ascii character")
77
race_condition_notification = """
78
This test is dependent on timing. The event queue is cleared in preparation for
79
tests. There is a small window where outside events from the OS may have effected
80
results. Try running the test again.
83
class EventModuleTest(unittest.TestCase):
88
self.assert_(not pygame.event.get())
93
def test_set_blocked(self):
94
# __doc__ (as of 2008-06-25) for pygame.event.set_blocked:
96
# pygame.event.set_blocked(type): return None
97
# pygame.event.set_blocked(typelist): return None
98
# pygame.event.set_blocked(None): return None
99
# control which events are allowed on the queue
101
pygame.event.set_blocked(2)
103
self.assert_(pygame.event.get_blocked(2))
105
pygame.event.post(pygame.event.Event(2))
107
events = pygame.event.get()
108
should_be_blocked = [e for e in events if e.type == 2]
110
self.assertEquals(should_be_blocked, [])
112
def test_post__and_poll(self):
113
# __doc__ (as of 2008-06-25) for pygame.event.post:
115
# pygame.event.post(Event): return None
116
# place a new event on the queue
118
e1 = pygame.event.Event(pygame.USEREVENT, attr1='attr1')
120
pygame.event.post(e1)
122
posted_event = pygame.event.poll()
124
e1.attr1, posted_event.attr1, race_condition_notification
127
# fuzzing event types
128
for i in range(1, 11):
129
pygame.event.post(pygame.event.Event(i))
131
pygame.event.poll().type, i, race_condition_notification
133
def test_post_large_user_event(self):
134
pygame.event.post(pygame.event.Event(pygame.USEREVENT, {'a': "a" * 1024}))
136
e = pygame.event.poll()
137
self.assertEquals(e.type, pygame.USEREVENT)
138
self.assertEquals(e.a, "a" * 1024)
143
# __doc__ (as of 2008-06-25) for pygame.event.get:
145
# pygame.event.get(): return Eventlist
146
# pygame.event.get(type): return Eventlist
147
# pygame.event.get(typelist): return Eventlist
148
# get events from the queue
150
# Put 10 events on the queue
151
for _ in range(1, 11):
152
pygame.event.post(pygame.event.Event(pygame.USEREVENT))
154
self.assert_ ( len(pygame.event.get()) >= 10 )
156
def test_clear(self):
158
# __doc__ (as of 2008-06-25) for pygame.event.clear:
160
# pygame.event.clear(): return None
161
# pygame.event.clear(type): return None
162
# pygame.event.clear(typelist): return None
163
# remove all events from the queue
165
for _ in range(1, 11):
166
pygame.event.post(pygame.event.Event(_))
168
self.assert_(pygame.event.poll()) # there are some events on queue
172
self.assert_(not pygame.event.poll(), race_condition_notification)
174
def test_event_name(self):
176
# __doc__ (as of 2008-06-25) for pygame.event.event_name:
178
# pygame.event.event_name(type): return string
179
# get the string name from and event id
181
self.assertEquals(pygame.event.event_name(2), "KeyDown")
182
self.assertEquals(pygame.event.event_name(24), "UserEvent")
185
# __doc__ (as of 2008-06-25) for pygame.event.wait:
187
# pygame.event.wait(): return Event
188
# wait for a single event from the queue
190
pygame.event.post ( pygame.event.Event(2) )
191
self.assert_(pygame.event.wait())
195
# __doc__ (as of 2008-06-25) for pygame.event.peek:
197
# pygame.event.peek(type): return bool
198
# pygame.event.peek(typelist): return bool
199
# test if event types are waiting on the queue
201
event_types = [2, 3, 4]
203
for event_type in event_types:
205
pygame.event.Event(event_type)
207
self.assert_(pygame.event.peek(event_type))
209
self.assert_(pygame.event.peek(event_types))
211
def test_set_allowed(self):
212
# __doc__ (as of 2008-06-25) for pygame.event.set_allowed:
214
# pygame.event.set_allowed(type): return None
215
# pygame.event.set_allowed(typelist): return None
216
# pygame.event.set_allowed(None): return None
217
# control which events are allowed on the queue
219
pygame.event.set_blocked(2)
220
self.assert_(pygame.event.get_blocked(2))
221
pygame.event.set_allowed(2)
222
self.assert_(not pygame.event.get_blocked(2))
225
# __doc__ (as of 2008-06-25) for pygame.event.pump:
227
# pygame.event.pump(): return None
228
# internally process pygame event handlers
230
# see it doesn't cause an error
233
def test_set_grab__and_get_symmetric(self):
235
# __doc__ (as of 2008-06-25) for pygame.event.set_grab:
237
# pygame.event.set_grab(bool): return None
238
# control the sharing of input devices with other applications
240
# If we don't have a real display, don't do the test.
241
if os.environ.get('SDL_VIDEODRIVER') == 'dummy':
244
pygame.event.set_grab(True)
245
self.assert_(pygame.event.get_grab())
246
pygame.event.set_grab(False)
247
self.assert_(not pygame.event.get_grab())
249
def test_event_equality(self):
250
a = pygame.event.Event(1, a=1)
251
b = pygame.event.Event(1, a=1)
252
c = pygame.event.Event(2, a=1)
253
d = pygame.event.Event(1, a=2)
255
self.failUnless(a == a)
257
self.failUnless(a == b)
259
self.failUnless(a != c)
261
self.failUnless(a != d)
264
def todo_test_get_blocked(self):
266
# __doc__ (as of 2008-08-02) for pygame.event.get_blocked:
268
# pygame.event.get_blocked(type): return bool
269
# test if a type of event is blocked from the queue
271
# Returns true if the given event type is blocked from the queue.
275
def todo_test_get_grab(self):
277
# __doc__ (as of 2008-08-02) for pygame.event.get_grab:
279
# pygame.event.get_grab(): return bool
280
# test if the program is sharing input devices
282
# Returns true when the input events are grabbed for this application.
283
# Use pygame.event.set_grab() to control this state.
288
def todo_test_poll(self):
290
# __doc__ (as of 2008-08-02) for pygame.event.poll:
292
# pygame.event.poll(): return Event
293
# get a single event from the queue
295
# Returns a single event from the queue. If the event queue is empty
296
# an event of type pygame.NOEVENT will be returned immediately. The
297
# returned event is removed from the queue.
302
################################################################################
304
if __name__ == '__main__':