| 1 |
"""Base class for interactive tests spawning a basic window.""" |
| 2 |
|
| 3 |
from inspect import cleandoc |
| 4 |
|
| 5 |
from pyglet import gl |
| 6 |
from pyglet.window import Window |
| 7 |
|
| 8 |
from tests.base.interactive import InteractiveTestCase |
| 9 |
|
| 10 |
|
| 11 |
class WindowedTestCase(InteractiveTestCase): |
| 12 |
""" |
| 13 |
Base class for tests that show a window, render something in that window and then ask a |
| 14 |
question to the user whether the contents are correct. Also takes a screenshot when the |
| 15 |
test is passed, so it can run without interaction afterwards. |
| 16 |
""" |
| 17 |
|
| 18 |
|
| 19 |
window_size = 200, 200 |
| 20 |
window_options = None |
| 21 |
window = None |
| 22 |
question = None |
| 23 |
take_screenshot = True |
| 24 |
|
| 25 |
|
| 26 |
def on_expose(self): |
| 27 |
pass |
| 28 |
|
| 29 |
def render(self): |
| 30 |
pass |
| 31 |
|
| 32 |
def draw(self): |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
@classmethod |
| 37 |
def create_test_case(cls, name, description=None, decorators=None, **kwargs): |
| 38 |
def run_test(self): |
| 39 |
for name, value in kwargs.items(): |
| 40 |
setattr(self, name, value) |
| 41 |
self._test_main() |
| 42 |
|
| 43 |
run_test.__name__ = name |
| 44 |
if description: |
| 45 |
run_test.__doc__ = cleandoc(description) |
| 46 |
if decorators: |
| 47 |
for decorator in decorators: |
| 48 |
run_test = decorator(run_test) |
| 49 |
setattr(cls, name, run_test) |
| 50 |
|
| 51 |
def _test_main(self): |
| 52 |
assert self.question |
| 53 |
|
| 54 |
self.window = w = Window(**self._get_window_options()) |
| 55 |
try: |
| 56 |
w.push_handlers(self) |
| 57 |
self.render() |
| 58 |
w.set_visible() |
| 59 |
w.dispatch_events() |
| 60 |
|
| 61 |
self.user_verify(cleandoc(self.question), self.take_screenshot) |
| 62 |
|
| 63 |
finally: |
| 64 |
w.close() |
| 65 |
|
| 66 |
def _get_window_options(self): |
| 67 |
if self.window_options: |
| 68 |
options = self.window_options |
| 69 |
else: |
| 70 |
options = {} |
| 71 |
|
| 72 |
if 'width' not in options: |
| 73 |
options['width'] = self.window_size[0] |
| 74 |
if 'height' not in options: |
| 75 |
options['height'] = self.window_size[1] |
| 76 |
if 'visible' not in options: |
| 77 |
options['visible'] = False |
| 78 |
if 'resizable' not in options: |
| 79 |
options['resizable'] = True |
| 80 |
|
| 81 |
return options |