Skip to content

Commit d8457ed

Browse files
committed
test_plot_image unit test: fixed ColormapTool test
1 parent af2ff51 commit d8457ed

1 file changed

Lines changed: 63 additions & 58 deletions

File tree

plotpy/tests/unit/test_plot_image.py

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
import numpy as np
1111
import pytest
12-
from guidata.qthelpers import exec_dialog
13-
from qtpy.QtCore import Qt
12+
from guidata.qthelpers import exec_dialog, qt_app_context
1413

1514
from plotpy.builder import make
1615
from plotpy.plot import BasePlot
@@ -42,9 +41,10 @@ def plot_image(item):
4241

4342
def test_plot_image():
4443
"""Test plotting of an Image"""
45-
item = make.image(compute_image())
46-
with plot_image(item) as plot:
47-
pass
44+
with qt_app_context(exec_loop=False):
45+
item = make.image(compute_image())
46+
with plot_image(item):
47+
pass
4848

4949

5050
def compute_image_xy():
@@ -62,9 +62,10 @@ def compute_image_xy():
6262

6363
def test_plot_image_xy():
6464
"""Test plotting of an Image with custom XY coordinates"""
65-
item = make.xyimage(*compute_image_xy())
66-
with plot_image(item) as plot:
67-
...
65+
with qt_app_context(exec_loop=False):
66+
item = make.xyimage(*compute_image_xy())
67+
with plot_image(item):
68+
pass
6869

6970

7071
def test_plot_quad_image():
@@ -74,68 +75,72 @@ def test_plot_quad_image():
7475
y = np.arange(-2.0, 2.0, delta)
7576
X, Y = np.meshgrid(x, y)
7677
Z = X * Y
77-
item = make.quadgrid(X, Y, Z)
78-
with plot_image(item) as plot:
79-
...
78+
with qt_app_context(exec_loop=False):
79+
item = make.quadgrid(X, Y, Z)
80+
with plot_image(item):
81+
pass
8082

8183

8284
def test_plot_tr_image():
8385
"""Test plotting of a TrImageItem"""
84-
img = compute_image()
85-
item = make.trimage(img)
86-
with plot_image(item) as plot:
87-
# translate the image and check the new bounds
88-
bounds1 = item.bounds
89-
item.set_transform(50, 25, 0)
90-
bounds2 = item.bounds
91-
assert bounds1.width() == bounds2.width()
92-
assert bounds1.height() == bounds2.height()
93-
assert bounds2.left() == bounds1.left() + 50
94-
assert bounds2.top() == bounds1.top() + 25
95-
96-
# rescale the image
97-
item.set_transform(0, 0, 0, 2.0, 3.0)
98-
plot.do_autoscale()
99-
bounds3 = item.bounds
100-
assert bounds3.width() == bounds2.width() * 2.0
101-
assert bounds3.height() == bounds2.height() * 3.0
86+
with qt_app_context(exec_loop=False):
87+
img = compute_image()
88+
item = make.trimage(img)
89+
with plot_image(item) as plot:
90+
# translate the image and check the new bounds
91+
bounds1 = item.bounds
92+
item.set_transform(50, 25, 0)
93+
bounds2 = item.bounds
94+
assert bounds1.width() == bounds2.width()
95+
assert bounds1.height() == bounds2.height()
96+
assert bounds2.left() == bounds1.left() + 50
97+
assert bounds2.top() == bounds1.top() + 25
98+
99+
# rescale the image
100+
item.set_transform(0, 0, 0, 2.0, 3.0)
101+
plot.do_autoscale()
102+
bounds3 = item.bounds
103+
assert bounds3.width() == bounds2.width() * 2.0
104+
assert bounds3.height() == bounds2.height() * 3.0
102105

103106

104107
@pytest.mark.parametrize("ratio", [1.0, 0.75, 1.5, 2.0, 3.0])
105108
def test_set_aspect_ratio(ratio):
106109
"""Test BasePlot.set_aspect_ratio method()
107110
108111
It ensures that the new height is correctly set."""
109-
win = make.dialog(type="image")
110-
item = make.image(compute_image())
111-
plot = win.manager.get_plot()
112-
plot.add_item(item, autoscale=False)
113-
win.show()
114-
x0, x1, y0, y1 = plot.get_plot_limits()
115-
plot.set_aspect_ratio(ratio, True)
116-
assert plot.get_aspect_ratio() == ratio
117-
exec_dialog(win)
112+
with qt_app_context(exec_loop=False):
113+
win = make.dialog(type="image")
114+
item = make.image(compute_image())
115+
plot = win.manager.get_plot()
116+
plot.add_item(item, autoscale=False)
117+
win.show()
118+
x0, x1, y0, y1 = plot.get_plot_limits()
119+
plot.set_aspect_ratio(ratio, True)
120+
assert plot.get_aspect_ratio() == ratio
121+
exec_dialog(win)
118122

119123

120124
def test_colormap_tool():
121125
"""Test ColorMapTool on an image"""
122-
win = make.dialog(type="image", toolbar=True)
123-
item = make.image(compute_image())
124-
plot = win.manager.get_plot()
125-
plot.add_item(item)
126-
win.show()
127-
128-
# default color map should be "jet"
129-
color_map_tool = win.manager.get_tool(ColormapTool)
130-
assert item.get_color_map_name() == "jet"
131-
jet_img = plot.grab().toImage()
132-
133-
# change the colormap
134-
plot.select_item(item)
135-
cmap_name = "Accent"
136-
color_map_tool.activate_cmap(cmap_name)
137-
assert item.get_color_map_name() == cmap_name
138-
accent_img = plot.grab().toImage()
139-
assert jet_img != accent_img
140-
141-
exec_dialog(win)
126+
with qt_app_context(exec_loop=False):
127+
win = make.dialog(type="image", toolbar=True)
128+
item = make.image(compute_image())
129+
plot = win.manager.get_plot()
130+
plot.add_item(item)
131+
win.show()
132+
133+
# default color map should be "jet"
134+
color_map_tool = win.manager.get_tool(ColormapTool)
135+
assert item.get_color_map_name() == "jet"
136+
jet_img = plot.grab().toImage()
137+
138+
# change the colormap
139+
plot.select_item(item)
140+
cmap_name = "accent"
141+
color_map_tool.activate_cmap(cmap_name)
142+
assert item.get_color_map_name() == cmap_name
143+
accent_img = plot.grab().toImage()
144+
assert jet_img != accent_img
145+
146+
exec_dialog(win)

0 commit comments

Comments
 (0)