File: test_image_view.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (135 lines) | stat: -rw-r--r-- 4,471 bytes parent folder | download
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
#------------------------------------------------------------------------------
# Copyright (c) 2019-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
"""Test the image view widget."""
from utils import compile_source, wait_for_window_displayed


SOURCE ="""
import os
from enaml.image import Image
from enaml.widgets.api import Window, Container, ImageView

def image_path(name):
    dirname = os.path.dirname(r"%s")
    return os.path.join(dirname, 'images', name)

with open(image_path('img1.png'), 'rb') as f:
    IMAGES = {
        'None': None,
        'Image A': Image(data=f.read()),
    }

enamldef Main(Window):

    attr img_name = 'None'
    alias view: img

    Container:
        ImageView: img:
            image << IMAGES[img_name]

""" % __file__


def test_displaying_no_image(enaml_qtbot, enaml_sleep):
    """Test that we can set the image attribute to None.

    """
    win = compile_source(SOURCE, 'Main')()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)
    enaml_qtbot.wait(enaml_sleep)

    win.img_name = 'Image A'
    enaml_qtbot.wait(enaml_sleep)


# XXX The following test require to compare to a reference image since we
# cannot access the size of the painted area to ensure that scaling was done
# properly

# @pytest.mark.parametrize('factor', [0.5, 2])
# def test_refuse_to_scale(enaml_qtbot, enaml_sleep, factor):
#     """Test that we do not scale the image if scale_to_fit is False.

#     """
#     win = compile_source(SOURCE, 'Main')()
#     win.img_name = 'Image A'
#     win.show()
#     wait_for_window_displayed(enaml_qtbot, win)

#     img_size = win.view.proxy.widget.size()
#     size = win.size()
#     win.set_size((size.width*factor, size.height*factor))
#     enaml_qtbot.wait(enaml_sleep)

#     new_img_size = win.view.proxy.widget.size()
#     assert new_img_size.width() == img_size.width()
#     assert new_img_size.height() == img_size.height()


# @pytest.mark.parametrize('preserve_aspect_ratio', [False, True])
# def test_downscale(enaml_qtbot, enaml_sleep, preserve_aspect_ratio):
#     """Test that scale_to_fit will allow to downscale the image.

#     """
#     win = compile_source(SOURCE, 'Main')()
#     win.img_name = 'Image A'
#     win.view.scale_to_fit = True
#     win.view.preserve_aspect_ratio = preserve_aspect_ratio
#     win.show()
#     wait_for_window_displayed(enaml_qtbot, win)

#     img_size = win.view.proxy.widget.size()
#     size = win.size()
#     win.set_size((size.width*0.2, size.height*0.5))
#     enaml_qtbot.wait(enaml_sleep)

#     new_img_size = win.view.proxy.widget.size()
#     assert new_img_size.width() < img_size.width()
#     assert new_img_size.height() < img_size.height()
#     if preserve_aspect_ratio:
#         assert new_img_size.width()/new_img_size.height() ==\
#             img_size.width()/img_size.height()
#     else:
#         assert new_img_size.width()/new_img_size.height() !=\
#             img_size.width()/img_size.height()


# @pytest.mark.parametrize('preserve_aspect_ratio', [False, True])
# @pytest.mark.parametrize('allow_upscaling', [False, True])
# def test_upscale(enaml_qtbot, enaml_sleep, preserve_aspect_ratio,
#                  allow_upscaling):
#     """Test that scale_to_fit and allow_upscaling allow to upscale.

#     """
#     win = compile_source(SOURCE, 'Main')()
#     win.img_name = 'Image A'
#     win.view.scale_to_fit = True
#     win.view.preserve_aspect_ratio = preserve_aspect_ratio
#     win.show()
#     wait_for_window_displayed(enaml_qtbot, win)

#     img_size = win.view.proxy.widget.size()
#     size = win.size()
#     win.set_size((size.width*2, size.height*4))
#     enaml_qtbot.wait(enaml_sleep)

#     new_img_size = win.view.proxy.widget.size()
#     if allow_upscaling:
#         assert new_img_size.width() > img_size.width()
#         assert new_img_size.height() > img_size.height()
#     else:
#         assert new_img_size.width() == img_size.width()
#         assert new_img_size.height() == img_size.height()
#     if preserve_aspect_ratio:
#         assert new_img_size.width()/new_img_size.height() ==\
#             img_size.width()/img_size.height()
#     else:
#         assert new_img_size.width()/new_img_size.height() !=\
#             img_size.width()/img_size.height()