| 1 |
import queue |
| 2 |
import unittest |
| 3 |
import time |
| 4 |
|
| 5 |
from pyglet import window |
| 6 |
|
| 7 |
from ...annotations import skip_platform, Platform |
| 8 |
|
| 9 |
|
| 10 |
class EventSequenceFixture: |
| 11 |
def __init__(self, event_loop): |
| 12 |
self.event_loop = event_loop |
| 13 |
self.listen_events = [] |
| 14 |
self.received_events = queue.Queue() |
| 15 |
|
| 16 |
def create_window(self, **kwargs): |
| 17 |
w = self.event_loop.create_window(**kwargs) |
| 18 |
w.push_handlers(self) |
| 19 |
return w |
| 20 |
|
| 21 |
def wait_for_events(self, expected_events, incorrect_events): |
| 22 |
if isinstance(expected_events, (tuple, list)): |
| 23 |
self.listen_events = expected_events |
| 24 |
else: |
| 25 |
self.listen_events = [expected_events] |
| 26 |
|
| 27 |
while True: |
| 28 |
|
| 29 |
self.event_loop.run_event_loop() |
| 30 |
|
| 31 |
def __getattr__(self, name): |
| 32 |
if name.startswith('on_'): |
| 33 |
return self._handle_event |
| 34 |
else: |
| 35 |
raise AttributeError() |
| 36 |
|
| 37 |
def _handle_event(self, name, *args, **kwargs): |
| 38 |
if name in self.listen_events: |
| 39 |
q.put((name, args, kwargs)) |
| 40 |
self.event_loop.interrupt_event_loop() |
| 41 |
|
| 42 |
|
| 43 |
class EventSequenceTest: |
| 44 |
"""Base for testing event sequences on a window.""" |
| 45 |
next_sequence = 0 |
| 46 |
last_sequence = 0 |
| 47 |
finished = False |
| 48 |
timeout = 2 |
| 49 |
start_time = time.time() |
| 50 |
|
| 51 |
def check_sequence(self, sequence, name): |
| 52 |
if self.next_sequence == 0 and sequence != 0: |
| 53 |
print('received event before test start:', name) |
| 54 |
return |
| 55 |
if sequence == 0: |
| 56 |
self.start_time = time.time() |
| 57 |
if not self.finished: |
| 58 |
if self.next_sequence != sequence: |
| 59 |
self.failed = 'ERROR: %s out of order' % name |
| 60 |
else: |
| 61 |
self.next_sequence += 1 |
| 62 |
if self.next_sequence > self.last_sequence: |
| 63 |
self.finished = True |
| 64 |
|
| 65 |
def check(self): |
| 66 |
self.assertTrue(time.time() - self.start_time < self.timeout, |
| 67 |
'Did not receive next expected event: %d' % self.next_sequence) |
| 68 |
failed = getattr(self, 'failed', None) |
| 69 |
if failed: |
| 70 |
self.fail(failed) |
| 71 |
|
| 72 |
|
| 73 |
class WindowShowEventSequenceTest(EventSequenceTest, unittest.TestCase): |
| 74 |
"""Event sequence when hidden window is set to visible.""" |
| 75 |
last_sequence = 3 |
| 76 |
|
| 77 |
def _on_internal_resize(self, width, height): |
| 78 |
print("ON INTERNAL") |
| 79 |
self.check_sequence(1, 'on_resize') |
| 80 |
|
| 81 |
def on_show(self): |
| 82 |
print("ON SHOW") |
| 83 |
self.check_sequence(2, 'on_show') |
| 84 |
|
| 85 |
def on_expose(self): |
| 86 |
print("ON EXPOSE") |
| 87 |
self.check_sequence(3, 'on_expose') |
| 88 |
|
| 89 |
def test_method(self): |
| 90 |
window.Window._enable_event_queue = True |
| 91 |
win = window.Window(visible=False) |
| 92 |
try: |
| 93 |
win.dispatch_events() |
| 94 |
win.push_handlers(self) |
| 95 |
|
| 96 |
win.set_visible(True) |
| 97 |
self.check_sequence(0, 'begin') |
| 98 |
while not win.has_exit and not self.finished: |
| 99 |
win.dispatch_events() |
| 100 |
self.check() |
| 101 |
finally: |
| 102 |
win.close() |
| 103 |
|
| 104 |
|
| 105 |
class WindowCreateEventSequenceTest(EventSequenceTest, unittest.TestCase): |
| 106 |
last_sequence = 3 |
| 107 |
|
| 108 |
def _on_internal_resize(self, width, height): |
| 109 |
self.check_sequence(1, 'on_resize') |
| 110 |
|
| 111 |
def on_show(self): |
| 112 |
self.check_sequence(2, 'on_show') |
| 113 |
|
| 114 |
def on_expose(self): |
| 115 |
self.check_sequence(3, 'on_expose') |
| 116 |
|
| 117 |
def test_method(self): |
| 118 |
window.Window._enable_event_queue = True |
| 119 |
win = window.Window() |
| 120 |
try: |
| 121 |
win.push_handlers(self) |
| 122 |
self.check_sequence(0, 'begin') |
| 123 |
while not win.has_exit and not self.finished: |
| 124 |
win.dispatch_events() |
| 125 |
self.check() |
| 126 |
finally: |
| 127 |
win.close() |
| 128 |
|
| 129 |
|
| 130 |
@skip_platform(Platform.WINDOWS) |
| 131 |
class WindowCreateFullScreenEventSequenceTest(EventSequenceTest, unittest.TestCase): |
| 132 |
last_sequence = 3 |
| 133 |
|
| 134 |
def _on_internal_resize(self, width, height): |
| 135 |
self.check_sequence(1, 'on_resize') |
| 136 |
|
| 137 |
def on_show(self): |
| 138 |
self.check_sequence(2, 'on_show') |
| 139 |
|
| 140 |
def on_expose(self): |
| 141 |
self.check_sequence(3, 'on_expose') |
| 142 |
|
| 143 |
def test_method(self): |
| 144 |
window.Window._enable_event_queue = True |
| 145 |
win = window.Window(fullscreen=True) |
| 146 |
try: |
| 147 |
win.push_handlers(self) |
| 148 |
self.check_sequence(0, 'begin') |
| 149 |
while not win.has_exit and not self.finished: |
| 150 |
win.dispatch_events() |
| 151 |
self.check() |
| 152 |
finally: |
| 153 |
win.close() |
| 154 |
|
| 155 |
|
| 156 |
@skip_platform(Platform.WINDOWS) |
| 157 |
class WindowSetFullScreenEventSequenceTest(EventSequenceTest, unittest.TestCase): |
| 158 |
last_sequence = 2 |
| 159 |
|
| 160 |
def _on_internal_resize(self, width, height): |
| 161 |
self.check_sequence(1, 'on_resize') |
| 162 |
|
| 163 |
def on_expose(self): |
| 164 |
self.check_sequence(2, 'on_expose') |
| 165 |
|
| 166 |
def test_method(self): |
| 167 |
window.Window._enable_event_queue = True |
| 168 |
win = window.Window() |
| 169 |
try: |
| 170 |
win.dispatch_events() |
| 171 |
|
| 172 |
win.push_handlers(self) |
| 173 |
win.set_fullscreen() |
| 174 |
self.check_sequence(0, 'begin') |
| 175 |
while not win.has_exit and not self.finished: |
| 176 |
win.dispatch_events() |
| 177 |
self.check() |
| 178 |
finally: |
| 179 |
win.close() |
| 180 |
|
| 181 |
|
| 182 |
@skip_platform(Platform.WINDOWS) |
| 183 |
class WindowUnsetFullScreenEventSequenceTest(EventSequenceTest, unittest.TestCase): |
| 184 |
last_sequence = 2 |
| 185 |
|
| 186 |
def _on_internal_resize(self, width, height): |
| 187 |
self.check_sequence(1, 'on_resize') |
| 188 |
|
| 189 |
def on_expose(self): |
| 190 |
self.check_sequence(2, 'on_expose') |
| 191 |
|
| 192 |
def test_method(self): |
| 193 |
window.Window._enable_event_queue = True |
| 194 |
win = window.Window(fullscreen=True) |
| 195 |
try: |
| 196 |
win.dispatch_events() |
| 197 |
win.push_handlers(self) |
| 198 |
|
| 199 |
win.set_fullscreen(False) |
| 200 |
self.check_sequence(0, 'begin') |
| 201 |
while not win.has_exit and not self.finished: |
| 202 |
win.dispatch_events() |
| 203 |
self.check() |
| 204 |
finally: |
| 205 |
win.close() |