File: windowed_test_base.py

package info (click to toggle)
pyglet 2.0.17%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,560 kB
  • sloc: python: 80,579; xml: 50,988; ansic: 171; makefile: 146
file content (81 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (3)
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
"""Base class for interactive tests spawning a basic window."""

from inspect import cleandoc

from pyglet import gl
from pyglet.window import Window

from tests.base.interactive import InteractiveTestCase


class WindowedTestCase(InteractiveTestCase):
    """
    Base class for tests that show a window, render something in that window and then ask a 
    question to the user whether the contents are correct. Also takes a screenshot when the
    test is passed, so it can run without interaction afterwards.
    """

    # Defaults
    window_size = 200, 200
    window_options = None
    window = None
    question = None
    take_screenshot = True

    # Methods to override in implementations
    def on_expose(self):
        pass

    def render(self):
        pass

    def draw(self):
        pass

    # Implementation of the base test class
    @classmethod
    def create_test_case(cls, name, description=None, decorators=None, **kwargs):
        def run_test(self):
            for name, value in kwargs.items():
                setattr(self, name, value)
            self._test_main()

        run_test.__name__ = name
        if description:
            run_test.__doc__ = cleandoc(description)
        if decorators:
            for decorator in decorators:
                run_test = decorator(run_test)
        setattr(cls, name, run_test)

    def _test_main(self):
        assert self.question

        self.window = w = Window(**self._get_window_options())
        try:
            w.push_handlers(self)
            self.render()
            w.set_visible()
            w.dispatch_events()

            self.user_verify(cleandoc(self.question), self.take_screenshot)

        finally:
            w.close()

    def _get_window_options(self):
        if self.window_options:
            options = self.window_options
        else:
            options = {}

        if 'width' not in options:
            options['width'] = self.window_size[0]
        if 'height' not in options:
            options['height'] = self.window_size[1]
        if 'visible' not in options:
            options['visible'] = False
        if 'resizable' not in options:
            options['resizable'] = True

        return options