File: test_arrows.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (109 lines) | stat: -rw-r--r-- 3,562 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
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

import numpy as np

from vispy.visuals.line.arrow import ARROW_TYPES
from vispy.scene import visuals, transforms
from vispy.testing import (requires_application, TestingCanvas,
                           run_tests_if_main, assert_raises, SkipTest, IS_TRAVIS_CI)
from vispy.testing.image_tester import assert_image_approved


vertices = np.array([
    [25, 25],
    [25, 75],
    [50, 25],
    [50, 75],
    [75, 25],
    [75, 75]
]).astype(np.float32)

vertices += 0.33

arrows = np.array([
    vertices[:2],
    vertices[3:1:-1],
    vertices[4:],
    vertices[-1:-3:-1]
]).reshape((4, 4))


@requires_application()
def test_arrow_draw():
    """Test drawing arrows without transforms"""
    with TestingCanvas() as c:
        if IS_TRAVIS_CI and c.app.backend_name.lower() == 'pyqt4':
            # TODO: Fix this (issue #1042
            raise SkipTest('Travis fails due to FB stack problem')
        for arrow_type in ARROW_TYPES:
            arrow = visuals.Arrow(pos=vertices, arrow_type=arrow_type,
                                  arrows=arrows, arrow_size=10, color='red',
                                  connect="segments", parent=c.scene)

            assert_image_approved(c.render(), 'visuals/arrow_type_%s.png' %
                                  arrow_type)

            arrow.parent = None


@requires_application()
def test_arrow_transform_draw():
    """Tests the ArrowVisual when a transform is applied"""
    with TestingCanvas() as c:
        if IS_TRAVIS_CI and c.app.backend_name.lower() == 'pyqt4':
            # TODO: Fix this (issue #1042
            raise SkipTest('Travis fails due to FB stack problem')
        for arrow_type in ARROW_TYPES:
            arrow = visuals.Arrow(pos=vertices, arrow_type=arrow_type,
                                  arrows=arrows, arrow_size=10, color='red',
                                  connect="segments", parent=c.scene)
            arrow.transform = transforms.STTransform(scale=(0.5, 0.75),
                                                     translate=(-20, -20))

            assert_image_approved(c.render(),
                                  'visuals/arrow_transform_type_%s.png' %
                                  arrow_type)

            arrow.parent = None


@requires_application()
def test_arrow_reactive():
    """Tests the reactive behaviour of the ArrowVisual properties."""
    with TestingCanvas() as c:
        arrow = visuals.Arrow(pos=vertices, arrows=arrows,
                              connect="segments", parent=c.scene)

        arrow.arrow_type = "stealth"
        assert_image_approved(c.render(), 'visuals/arrow_reactive1.png')

        arrow.arrow_size = 20
        assert_image_approved(c.render(), 'visuals/arrow_reactive2.png')


@requires_application()
def test_arrow_attributes():
    """Tests if the ArrowVisual performs the required checks for attributes."""
    with TestingCanvas() as c:
        arrow = visuals.Arrow(pos=vertices, arrow_type="stealth",
                              arrows=arrows, arrow_size=10, color='red',
                              connect="segments", parent=c.scene)

        def size_test():
            arrow.arrow_size = 0.0

        def type_test():
            arrow.arrow_type = "random_non_existent"

        assert_raises(
            ValueError, size_test
        )

        assert_raises(
            ValueError, type_test
        )


run_tests_if_main()