Skip to content

Commit 2c1d715

Browse files
committed
moving unit test util functions to a new unit.utils module
1 parent 4eeb9ab commit 2c1d715

8 files changed

Lines changed: 153 additions & 138 deletions

plotpy/tests/unit/test_aspect_ratio_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from guidata.qthelpers import exec_dialog, qt_app_context
55

66
from plotpy.interfaces.items import IImageItemType
7-
from plotpy.tests.unit.test_point_tools import create_window
7+
from plotpy.tests.unit.utils import create_window
88
from plotpy.tools import AspectRatioTool
99

1010

plotpy/tests/unit/test_display_coords_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from guidata.qthelpers import qt_app_context
55

66
from plotpy.interfaces.items import IImageItemType
7-
from plotpy.tests.unit.test_point_tools import create_window, drag_mouse
7+
from plotpy.tests.unit.utils import create_window, drag_mouse
88
from plotpy.tools import DisplayCoordsTool
99

1010

plotpy/tests/unit/test_multiline_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import qtpy.QtCore as QC
55
from guidata.qthelpers import exec_dialog, qt_app_context
66

7-
from plotpy.tests.unit.test_point_tools import (
7+
from plotpy.tests.unit.utils import (
88
CLICK,
99
create_window,
1010
keyboard_event,

plotpy/tests/unit/test_oblique_cross_section_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from plotpy.interfaces.items import IImageItemType
77
from plotpy.panels.csection.cswidget import ObliqueCrossSection
8-
from plotpy.tests.unit.test_point_tools import create_window, drag_mouse
8+
from plotpy.tests.unit.utils import create_window, drag_mouse
99
from plotpy.tools import ObliqueCrossSectionTool
1010

1111

plotpy/tests/unit/test_point_tools.py

Lines changed: 8 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,24 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, TypeVar, Union
3+
from typing import TYPE_CHECKING
44

55
import numpy as np
66
import qtpy.QtCore as QC
7-
import qtpy.QtGui as QG
8-
import qtpy.QtWidgets as QW
97
from guidata.qthelpers import exec_dialog, qt_app_context
108

11-
from plotpy.interfaces.items import ICurveItemType, IItemType
12-
from plotpy.panels.base import PanelWidget
13-
from plotpy.plot.plotwidget import PlotDialog, PlotWindow
14-
from plotpy.tests import vistools as ptv
15-
from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend
16-
from plotpy.tools import (
17-
CommandTool,
18-
EditPointTool,
19-
InteractiveTool,
20-
SelectPointsTool,
21-
SelectPointTool,
9+
from plotpy.tests.unit.utils import (
10+
CLICK,
11+
create_window,
12+
drag_mouse,
13+
keyboard_event,
14+
mouse_event_at_relative_plot_pos,
2215
)
16+
from plotpy.tools import EditPointTool, SelectPointsTool, SelectPointTool
2317
from plotpy.tools.curve import DownSamplingTool
2418

2519
if TYPE_CHECKING:
2620

2721
from plotpy.items.curve.base import CurveItem
28-
from plotpy.items.image.base import BaseImageItem
29-
30-
CLICK = (QC.QEvent.Type.MouseButtonPress, QC.QEvent.Type.MouseButtonRelease)
31-
ToolT = TypeVar("ToolT", bound=Union[InteractiveTool, CommandTool])
32-
33-
34-
def keyboard_event(
35-
win: PlotDialog,
36-
qapp: QW.QApplication,
37-
key: QC.Qt.Key,
38-
mod=QC.Qt.KeyboardModifier.NoModifier,
39-
):
40-
"""Simulates a keyboard event on the plot.
41-
42-
Args:
43-
win: window containing the plot
44-
qapp: Main QApplication instance
45-
key: Key to simulate
46-
mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
47-
48-
Returns:
49-
None
50-
"""
51-
plot = win.manager.get_plot()
52-
canva: QW.QWidget = plot.canvas() # type: ignore
53-
key_event = QG.QKeyEvent(QC.QEvent.Type.KeyPress, key, mod)
54-
qapp.sendEvent(canva, key_event)
55-
56-
57-
def mouse_event_at_relative_plot_pos(
58-
win: PlotDialog,
59-
qapp: QW.QApplication,
60-
relative_xy: tuple[float, float],
61-
click_types: tuple[QC.QEvent.Type, ...] = (QC.QEvent.Type.MouseButtonPress,),
62-
mod=QC.Qt.KeyboardModifier.NoModifier,
63-
) -> None:
64-
"""Simulates a click on the plot at the given relative position.
65-
66-
Args:
67-
win: window containing the plot
68-
qapp: Main QApplication instance
69-
relative_xy: Relative position of the click on the plot
70-
click_types: Different mouse button event types to send.
71-
Defaults to (QC.QEvent.Type.MouseButtonPress,).
72-
mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
73-
74-
Returns:
75-
None
76-
"""
77-
plot = win.manager.get_plot()
78-
79-
plot = win.manager.get_plot()
80-
canva: QW.QWidget = plot.canvas() # type: ignore
81-
size = canva.size()
82-
pos_x, pos_y = (
83-
relative_xy[0] * size.width(),
84-
relative_xy[1] * size.height(),
85-
)
86-
# pos_x, pos_y = axes_to_canvas(plot.get_active_item(), x, y)
87-
canva_pos = QC.QPointF(pos_x, pos_y).toPoint()
88-
glob_pos = canva.mapToGlobal(canva_pos)
89-
# QTest.mouseClick(canva, QC.Qt.MouseButton.LeftButton, mod, canva_pos)
90-
91-
for type_ in click_types:
92-
mouse_event_press = QG.QMouseEvent(
93-
type_,
94-
canva_pos,
95-
glob_pos,
96-
QC.Qt.MouseButton.LeftButton,
97-
QC.Qt.MouseButton.LeftButton,
98-
mod,
99-
)
100-
qapp.sendEvent(canva, mouse_event_press)
101-
102-
103-
def drag_mouse(
104-
win: PlotWindow,
105-
qapp: QW.QApplication,
106-
x_path: np.ndarray,
107-
y_path: np.ndarray,
108-
mod=QC.Qt.KeyboardModifier.NoModifier,
109-
click=True,
110-
) -> None:
111-
x0, y0 = x_path[0], y_path[0]
112-
xn, yn = x_path[-1], y_path[-1]
113-
press = (QC.QEvent.Type.MouseButtonPress,)
114-
move = (QC.QEvent.Type.MouseMove,)
115-
release = (QC.QEvent.Type.MouseButtonRelease,)
116-
117-
if click:
118-
mouse_event_at_relative_plot_pos(win, qapp, (x0, y0), press, mod)
119-
for x, y in zip(x_path, y_path):
120-
mouse_event_at_relative_plot_pos(win, qapp, (x, y), move, mod)
121-
if click:
122-
mouse_event_at_relative_plot_pos(win, qapp, (xn, yn), release, mod)
123-
124-
125-
def create_window(
126-
tool_class: type[ToolT],
127-
active_item_type: type[IItemType] = ICurveItemType,
128-
panels: list[type[PanelWidget]] | None = None,
129-
) -> tuple[PlotWindow, ToolT]:
130-
131-
items: list[CurveItem | BaseImageItem] = make_curve_image_legend()
132-
win = ptv.show_items(items, wintitle="Unit test plot", auto_tools=False)
133-
plot = win.manager.get_plot()
134-
for item in win.manager.get_plot().get_items()[::-1]:
135-
plot.set_active_item(item)
136-
last_active_item = plot.get_last_active_item(active_item_type)
137-
plot.set_active_item(last_active_item)
138-
139-
if panels is not None:
140-
for panel in panels:
141-
win.manager.add_panel(panel())
142-
143-
tool = win.manager.add_tool(tool_class)
144-
tool.activate()
145-
return win, tool
14622

14723

14824
def test_free_select_point_tool():

plotpy/tests/unit/test_rect_zoom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from numpy import linspace
88

99
from plotpy.interfaces.items import ICurveItemType
10-
from plotpy.tests.unit.test_point_tools import create_window, drag_mouse
10+
from plotpy.tests.unit.utils import create_window, drag_mouse
1111
from plotpy.tools import RectZoomTool
1212

1313

plotpy/tests/unit/test_shape_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from plotpy.plot.plotwidget import PlotWindow
3232
from plotpy.tools.base import RectangularActionTool
3333

34-
from plotpy.tests.unit.test_point_tools import drag_mouse
34+
from plotpy.tests.unit.utils import drag_mouse
3535

3636
P0 = QC.QPointF(10, 10)
3737
P1 = QC.QPointF(100, 100)

plotpy/tests/unit/utils.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, TypeVar, Union
4+
5+
import qtpy.QtCore as QC
6+
import qtpy.QtGui as QG
7+
import qtpy.QtWidgets as QW
8+
9+
from plotpy.interfaces.items import ICurveItemType, IItemType
10+
from plotpy.tests import vistools as ptv
11+
from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend
12+
from plotpy.tools import CommandTool, InteractiveTool
13+
14+
if TYPE_CHECKING:
15+
16+
import numpy as np
17+
18+
from plotpy.items.curve.base import CurveItem
19+
from plotpy.items.image.base import BaseImageItem
20+
from plotpy.panels.base import PanelWidget
21+
from plotpy.plot.plotwidget import PlotDialog, PlotWindow
22+
23+
24+
CLICK = (QC.QEvent.Type.MouseButtonPress, QC.QEvent.Type.MouseButtonRelease)
25+
ToolT = TypeVar("ToolT", bound=Union[InteractiveTool, CommandTool])
26+
27+
28+
def keyboard_event(
29+
win: PlotDialog,
30+
qapp: QW.QApplication,
31+
key: QC.Qt.Key,
32+
mod=QC.Qt.KeyboardModifier.NoModifier,
33+
):
34+
"""Simulates a keyboard event on the plot.
35+
36+
Args:
37+
win: window containing the plot
38+
qapp: Main QApplication instance
39+
key: Key to simulate
40+
mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
41+
42+
Returns:
43+
None
44+
"""
45+
plot = win.manager.get_plot()
46+
canva: QW.QWidget = plot.canvas() # type: ignore
47+
key_event = QG.QKeyEvent(QC.QEvent.Type.KeyPress, key, mod)
48+
qapp.sendEvent(canva, key_event)
49+
50+
51+
def mouse_event_at_relative_plot_pos(
52+
win: PlotDialog,
53+
qapp: QW.QApplication,
54+
relative_xy: tuple[float, float],
55+
click_types: tuple[QC.QEvent.Type, ...] = (QC.QEvent.Type.MouseButtonPress,),
56+
mod=QC.Qt.KeyboardModifier.NoModifier,
57+
) -> None:
58+
"""Simulates a click on the plot at the given relative position.
59+
60+
Args:
61+
win: window containing the plot
62+
qapp: Main QApplication instance
63+
relative_xy: Relative position of the click on the plot
64+
click_types: Different mouse button event types to send.
65+
Defaults to (QC.QEvent.Type.MouseButtonPress,).
66+
mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
67+
68+
Returns:
69+
None
70+
"""
71+
plot = win.manager.get_plot()
72+
73+
plot = win.manager.get_plot()
74+
canva: QW.QWidget = plot.canvas() # type: ignore
75+
size = canva.size()
76+
pos_x, pos_y = (
77+
relative_xy[0] * size.width(),
78+
relative_xy[1] * size.height(),
79+
)
80+
# pos_x, pos_y = axes_to_canvas(plot.get_active_item(), x, y)
81+
canva_pos = QC.QPointF(pos_x, pos_y).toPoint()
82+
glob_pos = canva.mapToGlobal(canva_pos)
83+
# QTest.mouseClick(canva, QC.Qt.MouseButton.LeftButton, mod, canva_pos)
84+
85+
for type_ in click_types:
86+
mouse_event_press = QG.QMouseEvent(
87+
type_,
88+
canva_pos,
89+
glob_pos,
90+
QC.Qt.MouseButton.LeftButton,
91+
QC.Qt.MouseButton.LeftButton,
92+
mod,
93+
)
94+
qapp.sendEvent(canva, mouse_event_press)
95+
96+
97+
def drag_mouse(
98+
win: PlotWindow,
99+
qapp: QW.QApplication,
100+
x_path: np.ndarray,
101+
y_path: np.ndarray,
102+
mod=QC.Qt.KeyboardModifier.NoModifier,
103+
click=True,
104+
) -> None:
105+
x0, y0 = x_path[0], y_path[0]
106+
xn, yn = x_path[-1], y_path[-1]
107+
press = (QC.QEvent.Type.MouseButtonPress,)
108+
move = (QC.QEvent.Type.MouseMove,)
109+
release = (QC.QEvent.Type.MouseButtonRelease,)
110+
111+
if click:
112+
mouse_event_at_relative_plot_pos(win, qapp, (x0, y0), press, mod)
113+
for x, y in zip(x_path, y_path):
114+
mouse_event_at_relative_plot_pos(win, qapp, (x, y), move, mod)
115+
if click:
116+
mouse_event_at_relative_plot_pos(win, qapp, (xn, yn), release, mod)
117+
118+
119+
def create_window(
120+
tool_class: type[ToolT],
121+
active_item_type: type[IItemType] = ICurveItemType,
122+
panels: list[type[PanelWidget]] | None = None,
123+
) -> tuple[PlotWindow, ToolT]:
124+
125+
items: list[CurveItem | BaseImageItem] = make_curve_image_legend()
126+
win = ptv.show_items(items, wintitle="Unit test plot", auto_tools=False)
127+
plot = win.manager.get_plot()
128+
for item in win.manager.get_plot().get_items()[::-1]:
129+
plot.set_active_item(item)
130+
last_active_item = plot.get_last_active_item(active_item_type)
131+
plot.set_active_item(last_active_item)
132+
133+
if panels is not None:
134+
for panel in panels:
135+
win.manager.add_panel(panel())
136+
137+
tool = win.manager.add_tool(tool_class)
138+
tool.activate()
139+
return win, tool

0 commit comments

Comments
 (0)