Skip to content

Commit b24f74a

Browse files
committed
Mouse dragging in unit tests: removed unnecessary qapp argument
1 parent ebd87ce commit b24f74a

11 files changed

+109
-168
lines changed

plotpy/tests/unit/test_annotation_tools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def create_window(tool_classes: tuple[type[InteractiveTool], ...]) -> PlotWindow
8787
def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
8888
"""Generic test for annotation tool. Simulates a mouse drag on the plot and checks
8989
that the tool is activated and deactivated correctly."""
90-
with qt_app_context(exec_loop=False) as qapp:
90+
with qt_app_context(exec_loop=False):
9191
win = create_window(tool_classes)
9292
win.show()
9393
plot = win.manager.get_plot()
@@ -99,7 +99,7 @@ def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
9999
x_path = np.linspace(0, 0.5, 100)
100100
y_path = np.linspace(0, 0.5, 100)
101101
with execenv.context(accept_dialogs=True):
102-
drag_mouse(win, qapp, x_path, y_path)
102+
drag_mouse(win, x_path, y_path)
103103
if hasattr(tool_class, "SWITCH_TO_DEFAULT_TOOL"):
104104
assert win.manager.get_default_tool() == default_tool
105105
plot.unselect_all()
@@ -109,9 +109,9 @@ def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
109109
assert select_tool is not None
110110
select_tool.activate()
111111

112-
drag_mouse(win, qapp, np.linspace(0.2, 0.5, 10), np.linspace(0.2, 0.5, 10))
112+
drag_mouse(win, np.linspace(0.2, 0.5, 10), np.linspace(0.2, 0.5, 10))
113113

114-
undo_redo(qapp, win)
114+
undo_redo(win)
115115

116116
exec_dialog(win)
117117

plotpy/tests/unit/test_cursor_tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def test_cursor_tool(cursor_tool: type[BaseCursorTool]):
3131
Args:
3232
cursor_tool: Cursor tool class to test.
3333
"""
34-
with qt_app_context(exec_loop=True) as qapp:
35-
win, tool = create_window(cursor_tool)
34+
with qt_app_context(exec_loop=True):
35+
win, _tool = create_window(cursor_tool)
3636
win.show()
3737
plot = win.manager.get_plot()
3838

@@ -41,7 +41,7 @@ def test_cursor_tool(cursor_tool: type[BaseCursorTool]):
4141
tool_shape_type = type(active_tool.create_shape())
4242
assert tool_shape_type not in (type(item) for item in plot.get_items())
4343

44-
drag_mouse(win, qapp, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7]))
44+
drag_mouse(win, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7]))
4545
assert tool_shape_type in (type(item) for item in plot.get_items())
4646

4747
exec_dialog(win)

plotpy/tests/unit/test_curve_tools.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
def test_curve_stat_tool():
3333
"""Test the curve stats tool."""
34-
with qt_app_context(exec_loop=False) as qapp:
35-
win, tool = create_window(CurveStatsTool)
34+
with qt_app_context(exec_loop=False):
35+
win, _tool = create_window(CurveStatsTool)
3636
win.show()
3737
plot = win.manager.get_plot()
3838

@@ -42,7 +42,7 @@ def test_curve_stat_tool():
4242
if isinstance(item, (DataInfoLabel, XRangeSelection))
4343
]
4444

45-
drag_mouse(win, qapp, np.array([0.4, 0.6]), np.array([0.5, 0.5]))
45+
drag_mouse(win, np.array([0.4, 0.6]), np.array([0.5, 0.5]))
4646

4747
new_stat_items = [
4848
item
@@ -58,15 +58,15 @@ def test_curve_stat_tool():
5858
win.manager.add_tool(SelectTool).activate()
5959
plot.unselect_all()
6060

61-
mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK)
61+
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
6262
selected_items = plot.get_selected_items()
6363
assert len(selected_items) == 1 and isinstance(
6464
selected_items[0], XRangeSelection
6565
)
6666
range_item: XRangeSelection = selected_items[0]
6767

6868
x00, x01, y0 = range_item.get_handles_pos()
69-
drag_mouse(win, qapp, np.linspace(0.5, 0.9, 100), np.full(100, 0.5))
69+
drag_mouse(win, np.linspace(0.5, 0.9, 100), np.full(100, 0.5))
7070
x10, x11, y1 = range_item.get_handles_pos()
7171

7272
assert x00 < x10 and x01 < x11 and y0 == y1
@@ -84,32 +84,22 @@ def test_curve_stat_tool():
8484

8585
def test_free_select_point_tool():
8686
"""Test the free select point tool."""
87-
with qt_app_context(exec_loop=False) as qapp:
87+
with qt_app_context(exec_loop=False):
8888
win, tool = create_window(SelectPointTool)
8989
win.show()
90-
mouse_event_at_relative_plot_pos(
91-
win,
92-
qapp,
93-
(0.5, 0.5),
94-
CLICK,
95-
)
90+
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
9691
assert tool.get_coordinates() is not None
9792
exec_dialog(win)
9893

9994

10095
def test_contrained_select_point_tool():
10196
"""Test the constrained select point tool contrained to a CurveItem."""
102-
with qt_app_context(exec_loop=False) as qapp:
97+
with qt_app_context(exec_loop=False):
10398
win, tool = create_window(SelectPointTool)
10499
win.show()
105100
tool.on_active_item = True
106101

107-
mouse_event_at_relative_plot_pos(
108-
win,
109-
qapp,
110-
(0.5, 0.5),
111-
CLICK,
112-
)
102+
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
113103
coor = tool.get_coordinates()
114104
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
115105
arr_x, arr_y = curve_item.get_data()
@@ -122,25 +112,25 @@ def test_contrained_select_point_tool():
122112

123113
def test_select_points_tool():
124114
"""Test the select points tool constrained to a CurveItem."""
125-
with qt_app_context(exec_loop=False) as qapp:
115+
with qt_app_context(exec_loop=False):
126116
win, tool = create_window(tool_class=SelectPointsTool)
127117
mod = QC.Qt.KeyboardModifier.ControlModifier
128118

129-
mouse_event_at_relative_plot_pos(win, qapp, (0.4, 0.5), CLICK, mod)
119+
mouse_event_at_relative_plot_pos(win, (0.4, 0.5), CLICK, mod)
130120
assert len(tool.get_coordinates() or ()) == 1
131121

132-
mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK, mod)
133-
mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod)
122+
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK, mod)
123+
mouse_event_at_relative_plot_pos(win, (0.8, 0.8), CLICK, mod)
134124
print(tool.get_coordinates())
135125
assert len(tool.get_coordinates() or ()) == 3
136126

137-
mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod)
127+
mouse_event_at_relative_plot_pos(win, (0.8, 0.8), CLICK, mod)
138128
assert len(tool.get_coordinates() or ()) == 2
139129

140-
mouse_event_at_relative_plot_pos(win, qapp, (0.7, 0.5), CLICK, mod)
130+
mouse_event_at_relative_plot_pos(win, (0.7, 0.5), CLICK, mod)
141131
assert len(tool.get_coordinates() or ()) == 3
142132

143-
mouse_event_at_relative_plot_pos(win, qapp, (0.1, 0.1), CLICK)
133+
mouse_event_at_relative_plot_pos(win, (0.1, 0.1), CLICK)
144134
assert len(tool.get_coordinates() or ()) == 1
145135

146136
coor = tool.get_coordinates()
@@ -155,7 +145,7 @@ def test_select_points_tool():
155145

156146
def test_edit_point_tool():
157147
"""Test the edit point tool for a CurveItem."""
158-
with qt_app_context(exec_loop=False) as qapp:
148+
with qt_app_context(exec_loop=False):
159149
win, tool = create_window(EditPointTool)
160150
win.show()
161151
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
@@ -177,11 +167,11 @@ def test_edit_point_tool():
177167
min_v, max_v = 0, 1
178168
x_path = np.full(n, min_v)
179169
y_path = np.linspace(max_v, min_v, n)
180-
drag_mouse(win, qapp, x_path, y_path)
170+
drag_mouse(win, x_path, y_path)
181171

182172
x_path = np.full(n, max_v)
183173

184-
drag_mouse(win, qapp, x_path, y_path)
174+
drag_mouse(win, x_path, y_path)
185175
curve_changes = tool.get_changes()[curve_item]
186176

187177
x_arr, y_arr = curve_item.get_data()
@@ -196,9 +186,7 @@ def test_edit_point_tool():
196186
assert x_arr is not None and y_arr is not None
197187

198188
# Reset the arrays and deletes the changes
199-
keyboard_event(
200-
win, qapp, QC.Qt.Key.Key_Z, QC.Qt.KeyboardModifier.ControlModifier
201-
)
189+
keyboard_event(win, QC.Qt.Key.Key_Z, QC.Qt.KeyboardModifier.ControlModifier)
202190

203191
assert len(curve_changes) == 0
204192

@@ -207,7 +195,7 @@ def test_edit_point_tool():
207195
assert np.allclose(orig_x, restored_x)
208196
assert np.allclose(orig_y, restored_y)
209197

210-
mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK)
198+
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
211199
tool.trigger_insert_point_at_selection()
212200

213201
new_x, new_y = curve_item.get_data()

plotpy/tests/unit/test_display_coords_tool.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@
1818
@pytest.mark.parametrize("active_item", [ICurveItemType, IImageItemType, None])
1919
def test_display_coords(active_item: type[IItemType] | None):
2020
"""Test display coordinates tool on a curve and on an image."""
21-
with qt_app_context(exec_loop=False) as qapp:
22-
win, tool = create_window(DisplayCoordsTool, active_item_type=active_item)
21+
with qt_app_context(exec_loop=False):
22+
win, _tool = create_window(DisplayCoordsTool, active_item_type=active_item)
2323
plot = win.manager.get_plot()
2424

2525
# The is no way to test a condition while the mouse is moving so it is
2626
# not possible to test the display of the coordinates while the mouse is moving.
2727
assert plot.curve_pointer is False and plot.canvas_pointer is False
28-
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=False)
28+
drag_mouse(win, np.array([0.5]), np.array([0.5]), click=False)
2929
assert plot.curve_pointer is False and plot.canvas_pointer is False
30-
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=True)
30+
drag_mouse(win, np.array([0.5]), np.array([0.5]), click=True)
3131
assert plot.curve_pointer is False and plot.canvas_pointer is False
3232
drag_mouse(
3333
win,
34-
qapp,
3534
np.array([0.5]),
3635
np.array([0.5]),
3736
click=False,
@@ -40,7 +39,6 @@ def test_display_coords(active_item: type[IItemType] | None):
4039
assert plot.curve_pointer is False and plot.canvas_pointer is False
4140
drag_mouse(
4241
win,
43-
qapp,
4442
np.array([0.5]),
4543
np.array([0.5]),
4644
click=False,

plotpy/tests/unit/test_events.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,13 @@ def result_zoom(zoom_type: ZoomType, event_type: ZoomEvent):
5757
btn = QC.Qt.MouseButton.RightButton
5858
x_path = np.array([0.5, 0.2])
5959

60-
def _zoom_with_mouse(qapp: QW.QApplication, win: PlotWindow | PlotDialog):
60+
def _zoom_with_mouse(win: PlotWindow | PlotDialog):
6161
"""Zoom in the plot by dragging the mouse while holding right click.
6262
6363
Args:
64-
qapp: QApplication instance.
6564
win: PlotWindow or PlotDialog instance.
6665
"""
67-
drag_mouse(
68-
win,
69-
qapp,
70-
x_path,
71-
np.array([0.5, 0.5]),
72-
btn=btn,
73-
)
66+
drag_mouse(win, x_path, np.array([0.5, 0.5]), btn=btn)
7467

7568
return _zoom_with_mouse, zoom_type
7669

@@ -84,21 +77,13 @@ def _zoom_with_mouse(qapp: QW.QApplication, win: PlotWindow | PlotDialog):
8477
angle_delta = -360
8578
mod = QC.Qt.KeyboardModifier.ControlModifier
8679

87-
def _zoom_with_wheel(qapp: QW.QApplication, win: PlotWindow | PlotDialog):
80+
def _zoom_with_wheel(win: PlotWindow | PlotDialog):
8881
"""Zoom in the plot by scrolling the mouse wheel while holding control.
8982
9083
Args:
91-
qapp: QApplication instance.
9284
win: PlotWindow or PlotDialog instance.
9385
"""
94-
scroll_wheel(
95-
win,
96-
qapp,
97-
(0.5, 0.5),
98-
angle_delta,
99-
0,
100-
mods=mod,
101-
)
86+
scroll_wheel(win, (0.5, 0.5), angle_delta, 0, mods=mod)
10287

10388
return _zoom_with_wheel, zoom_type
10489

@@ -123,7 +108,7 @@ def test_zoom(
123108
Args:
124109
zoom_func: _description_
125110
"""
126-
with qt_app_context(exec_loop=False) as qapp:
111+
with qt_app_context(exec_loop=False):
127112
win, _ = create_window(SelectTool)
128113
win.show()
129114

@@ -135,7 +120,7 @@ def test_zoom(
135120
x_min0, x_max0 = plot.get_axis_limits(axis_x)
136121
y_min0, y_max0 = plot.get_axis_limits(axis_y)
137122

138-
zoom_func(qapp, win)
123+
zoom_func(win)
139124

140125
x_min1, x_max1 = plot.get_axis_limits(axis_x)
141126
y_min1, y_max1 = plot.get_axis_limits(axis_y)
@@ -151,7 +136,7 @@ def test_zoom(
151136

152137
def test_pan():
153138
"""Test panning the plot by dragging the mouse while holding middle click."""
154-
with qt_app_context(exec_loop=False) as qapp:
139+
with qt_app_context(exec_loop=False):
155140
win, _ = create_window(SelectTool)
156141
win.show()
157142

@@ -163,13 +148,8 @@ def test_pan():
163148
x_min0, x_max0 = plot.get_axis_limits(axis_x)
164149
y_min0, y_max0 = plot.get_axis_limits(axis_y)
165150

166-
drag_mouse(
167-
win,
168-
qapp,
169-
np.linspace(0.5, 0, 100),
170-
np.linspace(0.5, 0, 100),
171-
btn=QC.Qt.MouseButton.MiddleButton,
172-
)
151+
x_path, y_path = np.linspace(0.5, 0, 100), np.linspace(0.5, 0, 100)
152+
drag_mouse(win, x_path, y_path, btn=QC.Qt.MouseButton.MiddleButton)
173153

174154
x_min1, x_max1 = plot.get_axis_limits(axis_x)
175155
y_min1, y_max1 = plot.get_axis_limits(axis_y)

plotpy/tests/unit/test_line_cross_section_tool.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111

1212
def test_line_cross_section():
1313
"""Test the line cross section tool."""
14-
with qt_app_context(exec_loop=False) as qapp:
15-
win, tool = create_window(
14+
with qt_app_context(exec_loop=False):
15+
win, _tool = create_window(
1616
LineCrossSectionTool,
1717
active_item_type=IImageItemType,
1818
panels=[LineCrossSection],
1919
)
2020
n = 100
21-
x_path = np.linspace(0.25, 0.75, n)
22-
y_path = np.linspace(0.25, 0.75, n)
23-
drag_mouse(win, qapp, x_path, y_path)
24-
21+
drag_mouse(win, np.linspace(0.25, 0.75, n), np.linspace(0.25, 0.75, n))
2522
exec_dialog(win)
2623

2724

0 commit comments

Comments
 (0)