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
|
import queue
import unittest
import time
from pyglet import window
class EventSequenceFixture:
def __init__(self, event_loop):
self.event_loop = event_loop
self.listen_events = []
self.received_events = queue.Queue()
def create_window(self, **kwargs):
w = self.event_loop.create_window(**kwargs)
w.push_handlers(self)
return w
def wait_for_events(self, expected_events, incorrect_events):
if isinstance(expected_events, (tuple, list)):
self.listen_events = expected_events
else:
self.listen_events = [expected_events]
while True:
# check events
self.event_loop.run_event_loop()
def __getattr__(self, name):
if name.startswith('on_'):
return self._handle_event
else:
raise AttributeError()
def _handle_event(self, name, *args, **kwargs):
if name in self.listen_events:
q.put((name, args, kwargs))
self.event_loop.interrupt_event_loop()
class EventSequenceTest:
"""Base for testing event sequences on a window."""
next_sequence = 0
last_sequence = 0
finished = False
timeout = 2
start_time = time.time()
def check_sequence(self, sequence, name):
if self.next_sequence == 0 and sequence != 0:
print('received event before test start:', name)
return
if sequence == 0:
self.start_time = time.time()
if not self.finished:
if self.next_sequence != sequence:
self.failed = 'ERROR: %s out of order' % name
else:
self.next_sequence += 1
if self.next_sequence > self.last_sequence:
self.finished = True
def check(self):
self.assertTrue(time.time() - self.start_time < self.timeout,
'Did not receive next expected event: %d' % self.next_sequence)
failed = getattr(self, 'failed', None)
if failed:
self.fail(failed)
class WindowShowEventSequenceTest(EventSequenceTest, unittest.TestCase):
"""Event sequence when hidden window is set to visible."""
last_sequence = 3
def on_resize(self, width, height):
self.check_sequence(1, 'on_resize')
def on_show(self):
self.check_sequence(2, 'on_show')
def on_expose(self):
self.check_sequence(3, 'on_expose')
def test_method(self):
window.Window._enable_event_queue = True
win = window.Window(visible=False)
try:
win.dispatch_events()
win.push_handlers(self)
win.set_visible(True)
self.check_sequence(0, 'begin')
while not win.has_exit and not self.finished:
win.dispatch_events()
self.check()
finally:
win.close()
class WindowCreateEventSequenceTest(EventSequenceTest, unittest.TestCase):
last_sequence = 3
def on_resize(self, width, height):
self.check_sequence(1, 'on_resize')
def on_show(self):
self.check_sequence(2, 'on_show')
def on_expose(self):
self.check_sequence(3, 'on_expose')
def test_method(self):
window.Window._enable_event_queue = True
win = window.Window()
try:
win.push_handlers(self)
self.check_sequence(0, 'begin')
while not win.has_exit and not self.finished:
win.dispatch_events()
self.check()
finally:
win.close()
class WindowCreateFullScreenEventSequenceTest(EventSequenceTest, unittest.TestCase):
last_sequence = 3
def on_resize(self, width, height):
self.check_sequence(1, 'on_resize')
def on_show(self):
self.check_sequence(2, 'on_show')
def on_expose(self):
self.check_sequence(3, 'on_expose')
def test_method(self):
window.Window._enable_event_queue = True
win = window.Window(fullscreen=True)
try:
win.push_handlers(self)
self.check_sequence(0, 'begin')
while not win.has_exit and not self.finished:
win.dispatch_events()
self.check()
finally:
win.close()
class WindowSetFullScreenEventSequenceTest(EventSequenceTest, unittest.TestCase):
last_sequence = 2
def on_resize(self, width, height):
self.check_sequence(1, 'on_resize')
def on_expose(self):
self.check_sequence(2, 'on_expose')
def test_method(self):
window.Window._enable_event_queue = True
win = window.Window()
try:
win.dispatch_events()
win.push_handlers(self)
win.set_fullscreen()
self.check_sequence(0, 'begin')
while not win.has_exit and not self.finished:
win.dispatch_events()
self.check()
finally:
win.close()
class WindowUnsetFullScreenEventSequenceTest(EventSequenceTest, unittest.TestCase):
last_sequence = 2
def on_resize(self, width, height):
self.check_sequence(1, 'on_resize')
def on_expose(self):
self.check_sequence(2, 'on_expose')
def test_method(self):
window.Window._enable_event_queue = True
win = window.Window(fullscreen=True)
try:
win.dispatch_events()
win.push_handlers(self)
win.set_fullscreen(False)
self.check_sequence(0, 'begin')
while not win.has_exit and not self.finished:
win.dispatch_events()
self.check()
finally:
win.close()
|