File: test_visuals.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 (141 lines) | stat: -rw-r--r-- 4,365 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
import pytest

from vispy.scene import visuals, Node
from vispy.scene.visuals import VisualNode
import vispy.visuals


def test_docstrings():
    # test that docstring insertions worked for all Visual+Node subclasses
    for name in dir(visuals):
        obj = getattr(visuals, name)
        if isinstance(obj, type) and issubclass(obj, Node):
            if obj is Node or obj is VisualNode:
                continue
            assert "This class inherits from visuals." in obj.__doc__
            assert "parent : Node" in obj.__doc__


def test_visual_node_generation():
    # test that all Visual classes also have Visual+Node classes
    visuals = []
    for name in dir(vispy.visuals):
        obj = getattr(vispy.visuals, name)
        if isinstance(obj, type) and issubclass(obj, Node):
            if obj is Node:
                continue
            assert name.endswith('Visual')
            vis_node = getattr(visuals, name[:-6])
            assert issubclass(vis_node, Node)
            assert issubclass(vis_node, obj)


def test_push_gl_state():
    node = vispy.visuals.MeshVisual()
    og_gl_state = node._vshare.gl_state.copy()
    node.push_gl_state(blend=False, depth_test=False)
    assert node._vshare.gl_state != og_gl_state
    # preset is always set, unset kwargs should be absent
    assert node._vshare.gl_state == {
        "preset": None,
        "blend": False,
        "depth_test": False,
    }
    node.pop_gl_state()
    assert node._vshare.gl_state == og_gl_state


def test_push_gl_state_update():
    node = vispy.visuals.MeshVisual()
    og_gl_state = node._vshare.gl_state.copy()
    assert "blend" not in og_gl_state
    assert node._vshare.gl_state["depth_test"]

    node.push_gl_state_update(blend=False, depth_test=False)
    assert node._vshare.gl_state != og_gl_state
    assert not node._vshare.gl_state["blend"]
    assert not node._vshare.gl_state["depth_test"]

    node.pop_gl_state()
    assert node._vshare.gl_state == og_gl_state


def test_pop_empty_gl_state():
    node = vispy.visuals.MeshVisual()
    assert node._prev_gl_state == []
    og_gl_state = node._vshare.gl_state.copy()
    node.pop_gl_state()
    assert node._vshare.gl_state == og_gl_state


def test_update_gl_state():
    node = vispy.visuals.MeshVisual()

    og_gl_state = node._vshare.gl_state.copy()
    assert og_gl_state
    og_gl_state["blend"] = False

    node.update_gl_state(blend=True)

    # check that the state was updated
    assert node._vshare.gl_state.pop("blend") != og_gl_state.pop("blend")
    # the rest of the state should be unchanged
    assert node._vshare.gl_state == og_gl_state


def test_update_gl_state_context_manager():
    node = vispy.visuals.MeshVisual()

    node.set_gl_state(blend=False)
    og_gl_state = node._vshare.gl_state.copy()

    with node.update_gl_state(blend=True):
        # check that the state was updated
        assert node._vshare.gl_state == {**og_gl_state, "blend": True}

    # the update should be reverted once out of the context
    assert node._vshare.gl_state == og_gl_state


def test_set_gl_state():
    node = vispy.visuals.MeshVisual()

    node.set_gl_state(blend=False, depth_test=False)
    # preset is always set, unset kwargs should be absent
    assert node._vshare.gl_state == {
        "preset": None,
        "blend": False,
        "depth_test": False,
    }

    node.set_gl_state(blend=False)
    # preset is always set, unset kwargs should be absent
    assert node._vshare.gl_state == {"preset": None, "blend": False}


def test_set_gl_state_context_manager():
    node = vispy.visuals.MeshVisual()

    node.set_gl_state(blend=False)
    og_gl_state = node._vshare.gl_state.copy()

    with node.set_gl_state(blend=True):
        # preset is always set, unset kwargs should be absent
        assert node._vshare.gl_state == {"preset": None, "blend": True}

    # the update should be reverted once out of the context
    assert node._vshare.gl_state == og_gl_state


@pytest.mark.parametrize("enable_picking", [True, False])
def test_picking_context(enable_picking):
    mesh = visuals.Mesh()
    mesh.picking = not enable_picking

    assert mesh.picking != enable_picking

    with mesh.set_picking(picking=enable_picking) as p:
        assert p == enable_picking
        assert mesh.picking == enable_picking

    assert mesh.picking != enable_picking