Skip to content

Commit

Permalink
Mouse dragging in unit tests: removed unnecessary qapp argument
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Jun 25, 2024
1 parent ebd87ce commit b24f74a
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 168 deletions.
8 changes: 4 additions & 4 deletions plotpy/tests/unit/test_annotation_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def create_window(tool_classes: tuple[type[InteractiveTool], ...]) -> PlotWindow
def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
"""Generic test for annotation tool. Simulates a mouse drag on the plot and checks
that the tool is activated and deactivated correctly."""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win = create_window(tool_classes)
win.show()
plot = win.manager.get_plot()
Expand All @@ -99,7 +99,7 @@ def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
x_path = np.linspace(0, 0.5, 100)
y_path = np.linspace(0, 0.5, 100)
with execenv.context(accept_dialogs=True):
drag_mouse(win, qapp, x_path, y_path)
drag_mouse(win, x_path, y_path)
if hasattr(tool_class, "SWITCH_TO_DEFAULT_TOOL"):
assert win.manager.get_default_tool() == default_tool
plot.unselect_all()
Expand All @@ -109,9 +109,9 @@ def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]):
assert select_tool is not None
select_tool.activate()

drag_mouse(win, qapp, np.linspace(0.2, 0.5, 10), np.linspace(0.2, 0.5, 10))
drag_mouse(win, np.linspace(0.2, 0.5, 10), np.linspace(0.2, 0.5, 10))

undo_redo(qapp, win)
undo_redo(win)

exec_dialog(win)

Expand Down
6 changes: 3 additions & 3 deletions plotpy/tests/unit/test_cursor_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_cursor_tool(cursor_tool: type[BaseCursorTool]):
Args:
cursor_tool: Cursor tool class to test.
"""
with qt_app_context(exec_loop=True) as qapp:
win, tool = create_window(cursor_tool)
with qt_app_context(exec_loop=True):
win, _tool = create_window(cursor_tool)
win.show()
plot = win.manager.get_plot()

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

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

exec_dialog(win)
Expand Down
54 changes: 21 additions & 33 deletions plotpy/tests/unit/test_curve_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

def test_curve_stat_tool():
"""Test the curve stats tool."""
with qt_app_context(exec_loop=False) as qapp:
win, tool = create_window(CurveStatsTool)
with qt_app_context(exec_loop=False):
win, _tool = create_window(CurveStatsTool)
win.show()
plot = win.manager.get_plot()

Expand All @@ -42,7 +42,7 @@ def test_curve_stat_tool():
if isinstance(item, (DataInfoLabel, XRangeSelection))
]

drag_mouse(win, qapp, np.array([0.4, 0.6]), np.array([0.5, 0.5]))
drag_mouse(win, np.array([0.4, 0.6]), np.array([0.5, 0.5]))

new_stat_items = [
item
Expand All @@ -58,15 +58,15 @@ def test_curve_stat_tool():
win.manager.add_tool(SelectTool).activate()
plot.unselect_all()

mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK)
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
selected_items = plot.get_selected_items()
assert len(selected_items) == 1 and isinstance(
selected_items[0], XRangeSelection
)
range_item: XRangeSelection = selected_items[0]

x00, x01, y0 = range_item.get_handles_pos()
drag_mouse(win, qapp, np.linspace(0.5, 0.9, 100), np.full(100, 0.5))
drag_mouse(win, np.linspace(0.5, 0.9, 100), np.full(100, 0.5))
x10, x11, y1 = range_item.get_handles_pos()

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

def test_free_select_point_tool():
"""Test the free select point tool."""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win, tool = create_window(SelectPointTool)
win.show()
mouse_event_at_relative_plot_pos(
win,
qapp,
(0.5, 0.5),
CLICK,
)
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
assert tool.get_coordinates() is not None
exec_dialog(win)


def test_contrained_select_point_tool():
"""Test the constrained select point tool contrained to a CurveItem."""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win, tool = create_window(SelectPointTool)
win.show()
tool.on_active_item = True

mouse_event_at_relative_plot_pos(
win,
qapp,
(0.5, 0.5),
CLICK,
)
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
coor = tool.get_coordinates()
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
arr_x, arr_y = curve_item.get_data()
Expand All @@ -122,25 +112,25 @@ def test_contrained_select_point_tool():

def test_select_points_tool():
"""Test the select points tool constrained to a CurveItem."""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win, tool = create_window(tool_class=SelectPointsTool)
mod = QC.Qt.KeyboardModifier.ControlModifier

mouse_event_at_relative_plot_pos(win, qapp, (0.4, 0.5), CLICK, mod)
mouse_event_at_relative_plot_pos(win, (0.4, 0.5), CLICK, mod)
assert len(tool.get_coordinates() or ()) == 1

mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK, mod)
mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod)
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK, mod)
mouse_event_at_relative_plot_pos(win, (0.8, 0.8), CLICK, mod)
print(tool.get_coordinates())
assert len(tool.get_coordinates() or ()) == 3

mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod)
mouse_event_at_relative_plot_pos(win, (0.8, 0.8), CLICK, mod)
assert len(tool.get_coordinates() or ()) == 2

mouse_event_at_relative_plot_pos(win, qapp, (0.7, 0.5), CLICK, mod)
mouse_event_at_relative_plot_pos(win, (0.7, 0.5), CLICK, mod)
assert len(tool.get_coordinates() or ()) == 3

mouse_event_at_relative_plot_pos(win, qapp, (0.1, 0.1), CLICK)
mouse_event_at_relative_plot_pos(win, (0.1, 0.1), CLICK)
assert len(tool.get_coordinates() or ()) == 1

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

def test_edit_point_tool():
"""Test the edit point tool for a CurveItem."""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win, tool = create_window(EditPointTool)
win.show()
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
Expand All @@ -177,11 +167,11 @@ def test_edit_point_tool():
min_v, max_v = 0, 1
x_path = np.full(n, min_v)
y_path = np.linspace(max_v, min_v, n)
drag_mouse(win, qapp, x_path, y_path)
drag_mouse(win, x_path, y_path)

x_path = np.full(n, max_v)

drag_mouse(win, qapp, x_path, y_path)
drag_mouse(win, x_path, y_path)
curve_changes = tool.get_changes()[curve_item]

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

# Reset the arrays and deletes the changes
keyboard_event(
win, qapp, QC.Qt.Key.Key_Z, QC.Qt.KeyboardModifier.ControlModifier
)
keyboard_event(win, QC.Qt.Key.Key_Z, QC.Qt.KeyboardModifier.ControlModifier)

assert len(curve_changes) == 0

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

mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK)
mouse_event_at_relative_plot_pos(win, (0.5, 0.5), CLICK)
tool.trigger_insert_point_at_selection()

new_x, new_y = curve_item.get_data()
Expand Down
10 changes: 4 additions & 6 deletions plotpy/tests/unit/test_display_coords_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
@pytest.mark.parametrize("active_item", [ICurveItemType, IImageItemType, None])
def test_display_coords(active_item: type[IItemType] | None):
"""Test display coordinates tool on a curve and on an image."""
with qt_app_context(exec_loop=False) as qapp:
win, tool = create_window(DisplayCoordsTool, active_item_type=active_item)
with qt_app_context(exec_loop=False):
win, _tool = create_window(DisplayCoordsTool, active_item_type=active_item)
plot = win.manager.get_plot()

# The is no way to test a condition while the mouse is moving so it is
# not possible to test the display of the coordinates while the mouse is moving.
assert plot.curve_pointer is False and plot.canvas_pointer is False
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=False)
drag_mouse(win, np.array([0.5]), np.array([0.5]), click=False)
assert plot.curve_pointer is False and plot.canvas_pointer is False
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=True)
drag_mouse(win, np.array([0.5]), np.array([0.5]), click=True)
assert plot.curve_pointer is False and plot.canvas_pointer is False
drag_mouse(
win,
qapp,
np.array([0.5]),
np.array([0.5]),
click=False,
Expand All @@ -40,7 +39,6 @@ def test_display_coords(active_item: type[IItemType] | None):
assert plot.curve_pointer is False and plot.canvas_pointer is False
drag_mouse(
win,
qapp,
np.array([0.5]),
np.array([0.5]),
click=False,
Expand Down
38 changes: 9 additions & 29 deletions plotpy/tests/unit/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,13 @@ def result_zoom(zoom_type: ZoomType, event_type: ZoomEvent):
btn = QC.Qt.MouseButton.RightButton
x_path = np.array([0.5, 0.2])

def _zoom_with_mouse(qapp: QW.QApplication, win: PlotWindow | PlotDialog):
def _zoom_with_mouse(win: PlotWindow | PlotDialog):
"""Zoom in the plot by dragging the mouse while holding right click.
Args:
qapp: QApplication instance.
win: PlotWindow or PlotDialog instance.
"""
drag_mouse(
win,
qapp,
x_path,
np.array([0.5, 0.5]),
btn=btn,
)
drag_mouse(win, x_path, np.array([0.5, 0.5]), btn=btn)

return _zoom_with_mouse, zoom_type

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

def _zoom_with_wheel(qapp: QW.QApplication, win: PlotWindow | PlotDialog):
def _zoom_with_wheel(win: PlotWindow | PlotDialog):
"""Zoom in the plot by scrolling the mouse wheel while holding control.
Args:
qapp: QApplication instance.
win: PlotWindow or PlotDialog instance.
"""
scroll_wheel(
win,
qapp,
(0.5, 0.5),
angle_delta,
0,
mods=mod,
)
scroll_wheel(win, (0.5, 0.5), angle_delta, 0, mods=mod)

return _zoom_with_wheel, zoom_type

Expand All @@ -123,7 +108,7 @@ def test_zoom(
Args:
zoom_func: _description_
"""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win, _ = create_window(SelectTool)
win.show()

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

zoom_func(qapp, win)
zoom_func(win)

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

def test_pan():
"""Test panning the plot by dragging the mouse while holding middle click."""
with qt_app_context(exec_loop=False) as qapp:
with qt_app_context(exec_loop=False):
win, _ = create_window(SelectTool)
win.show()

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

drag_mouse(
win,
qapp,
np.linspace(0.5, 0, 100),
np.linspace(0.5, 0, 100),
btn=QC.Qt.MouseButton.MiddleButton,
)
x_path, y_path = np.linspace(0.5, 0, 100), np.linspace(0.5, 0, 100)
drag_mouse(win, x_path, y_path, btn=QC.Qt.MouseButton.MiddleButton)

x_min1, x_max1 = plot.get_axis_limits(axis_x)
y_min1, y_max1 = plot.get_axis_limits(axis_y)
Expand Down
9 changes: 3 additions & 6 deletions plotpy/tests/unit/test_line_cross_section_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@

def test_line_cross_section():
"""Test the line cross section tool."""
with qt_app_context(exec_loop=False) as qapp:
win, tool = create_window(
with qt_app_context(exec_loop=False):
win, _tool = create_window(
LineCrossSectionTool,
active_item_type=IImageItemType,
panels=[LineCrossSection],
)
n = 100
x_path = np.linspace(0.25, 0.75, n)
y_path = np.linspace(0.25, 0.75, n)
drag_mouse(win, qapp, x_path, y_path)

drag_mouse(win, np.linspace(0.25, 0.75, n), np.linspace(0.25, 0.75, n))
exec_dialog(win)


Expand Down
Loading

0 comments on commit b24f74a

Please sign in to comment.