File: test_backends.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (164 lines) | stat: -rw-r--r-- 6,394 bytes parent folder | download | duplicates (2)
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
"""Tests to quickly see if the backends look good.
This tests only to see if all the necessary methods are implemented,
whether all the right events are mentioned, and whether the keymap
contains all keys that should be supported.

This test basically checks whether nothing was forgotten, not that the
implementation is corect.

"""

import vispy
from vispy import keys
from vispy.testing import (requires_application, assert_in, run_tests_if_main,
                           assert_raises)
from vispy.app import use_app, Application
from vispy.app.backends import _template
from vispy.util import _get_args


class DummyApplication(Application):
    def _use(self, backend_namd):
        pass


def _test_module_properties(_module=None):
    """Test application module"""
    if _module is None:
        app = use_app()
        _module = app.backend_module

    # Test that the keymap contains all keys supported by vispy.
    module_fname = _module.__name__.split('.')[-1]
    if module_fname not in ('_egl', '_osmesa'):  # skip keys for EGL, osmesa
        keymap = _module.KEYMAP
        vispy_keys = keymap.values()
        for keyname in dir(keys):
            if keyname.upper() != keyname:
                continue
            key = getattr(keys, keyname)
            assert_in(key, vispy_keys)

    # For Qt backend, we have a common implementation
    alt_modname = ''
    if module_fname in ('_pyside', '_pyqt4', '_pyqt5', '_pyqt6', '_pyside2', '_pyside6'):
        alt_modname = _module.__name__.rsplit('.', 1)[0] + '._qt'

    # Test that all _vispy_x methods are there.
    exceptions = (
        '_vispy_get_native_canvas',
        '_vispy_get_native_timer',
        '_vispy_get_native_app',
        '_vispy_reuse',
        '_vispy_mouse_move',
        '_vispy_mouse_press',
        '_vispy_mouse_release',
        '_vispy_mouse_double_click',
        '_vispy_detect_double_click',
        '_vispy_get_fb_bind_location',
        '_vispy_get_geometry',
        '_vispy_get_physical_size',
        '_vispy_sleep',
        '_process_backend_kwargs')  # defined in base class

    class KlassRef(vispy.app.base.BaseCanvasBackend):
        def __init__(self, *args, **kwargs):
            pass  # Do not call the base class, since it will check for Canvas
    Klass = _module.CanvasBackend
    base = KlassRef()
    for key in dir(KlassRef):
        if not key.startswith('__'):
            method = getattr(Klass, key)
            if key not in exceptions:
                print(key)
                args = [None] * (len(_get_args(method)) - 1)
                assert_raises(NotImplementedError, getattr(base, key), *args)
                if hasattr(method, '__module__'):
                    mod_str = method.__module__  # Py3k
                else:
                    mod_str = method.im_func.__module__
                assert_in(mod_str, (_module.__name__, alt_modname),
                          "Method %s.%s not defined in %s"
                          % (Klass, key, _module.__name__))

    Klass = _module.TimerBackend
    KlassRef = vispy.app.timer.TimerBackend
    for key in dir(KlassRef):
        if not key.startswith('__'):
            method = getattr(Klass, key)
            if key not in exceptions:
                if hasattr(method, '__module__'):
                    # Py3k
                    assert_in(method.__module__,
                              (_module.__name__, alt_modname))
                else:
                    t = method.im_func.__module__ == _module.__name__
                    assert t

    Klass = _module.ApplicationBackend
    KlassRef = vispy.app.application.ApplicationBackend
    for key in dir(KlassRef):
        if not key.startswith('__'):
            method = getattr(Klass, key)
            if key not in exceptions:
                if hasattr(method, '__module__'):
                    # Py3k
                    assert_in(method.__module__,
                              (_module.__name__, alt_modname))
                else:
                    t = method.im_func.__module__ == _module.__name__
                    assert t

    # Test that all events seem to be emitted.
    # Get text
    fname = _module.__file__.rstrip('c')  # "strip" will break windows!
    with open(fname, 'rb') as fid:
        text = fid.read().decode('utf-8')

    canvas = vispy.app.Canvas(create_native=False, app=DummyApplication())
    # Stylus and touch are ignored because they are not yet implemented.
    # Mouse events are emitted from the CanvasBackend base class.
    ignore = set(['stylus', 'touch', 'mouse_press', 'paint',
                  'mouse_move', 'mouse_release', 'mouse_double_click',
                  'detect_double_click', 'close'])
    if module_fname in ('_egl', '_osmesa'):
        ignore = ignore.union(['mouse_wheel', 'key_release', 'key_press'])
    eventNames = set(canvas.events._emitters.keys()) - ignore

    if not alt_modname:  # Only check for non-proxy modules
        for name in eventNames:
            assert_in('events.%s' % name, text,
                      'events.%s does not appear in %s' % (name, fname))


def test_template():
    """Test application module template"""
    _test_module_properties(_template)
    assert_raises(NotImplementedError, _template._set_config, dict())
    a = _template.ApplicationBackend()
    print(a._vispy_get_backend_name())
    for method in (a._vispy_process_events, a._vispy_run, a._vispy_quit,
                   a._vispy_get_native_app):
        assert_raises(NotImplementedError, method)

    class TemplateCanvasBackend(_template.CanvasBackend):
        def __init__(self, *args, **kwargs):
            pass  # Do not call the base class, since it will check for Canvas
    c = TemplateCanvasBackend()  # _template.CanvasBackend(None)
    print(c._vispy_get_native_canvas())
    for method in (c._vispy_set_current, c._vispy_swap_buffers, c._vispy_close,
                   c._vispy_update, c._vispy_get_size, c._vispy_get_position):
        assert_raises(NotImplementedError, method)
    for method in (c._vispy_set_title, c._vispy_set_visible):
        assert_raises(NotImplementedError, method, 0)
    for method in (c._vispy_set_size, c._vispy_set_position):
        assert_raises(NotImplementedError, method, 0, 0)


@requires_application()
def test_actual():
    """Test actual application module"""
    _test_module_properties(None)


run_tests_if_main()