8000 Add a click_and_move widget test helper · matplotlib/matplotlib@113a0f0 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 113a0f0

Browse files
committed
Add a click_and_move widget test helper
1 parent b0f7b8a commit 113a0f0

File tree

2 files changed

+60
-105
lines changed

2 files changed

+60
-105
lines changed

lib/matplotlib/testing/widgets.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,31 @@ def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
8383
event = mock_event(tool.ax, button, xdata, ydata, key, step)
8484
func = getattr(tool, etype)
8585
func(event)
86+
87+
88+
def click_and_move(tool, start, end, key=None):
89+
"""
90+
Click at *start*, move to end*, and then relese the mouse.
91+
92+
Parameters
93+
----------
94+
tool : matplotlib.widgets.RectangleSelector
95+
start : [Number, Number]
96+
Starting point.
97+
end : [Number, Number]
98+
End point.
99+
key : None or str
100+
The key pressed when the mouse event triggered (see also `.KeyEvent`).
101+
"""
102+
if key is not None:
103+
# Press key
104+
do_event(tool, 'on_key_press', xdata=start[0], ydata=start[1],
105+
button=1, key=key)
106+
# Click, move, and release mouse
107+
do_event(tool, 'press', xdata=start[0], ydata=start[1], button=1)
108+
do_event(tool, 'onmove', xdata=end[0], ydata=end[1], button=1)
109+
do_event(tool, 'release', xdata=end[0], ydata=end[1], button=1)
110+
if key is not None:
111+
# Release key
112+
do_event(tool, 'on_key_release', xdata=end[0], ydata=end[1],
113+
button=1, key=key)

lib/matplotlib/tests/test_widgets.py

Lines changed: 32 additions & 105 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import matplotlib.widgets as widgets
44
import matplotlib.pyplot as plt
55
from matplotlib.testing.decorators import check_figures_equal, image_comparison
6-
from matplotlib.testing.widgets import do_event, get_ax, mock_event
6+
from matplotlib.testing.widgets import (click_and_move, do_event, get_ax,
7+
mock_event)
78

89
import numpy as np
910
from numpy.testing import assert_allclose
@@ -81,9 +82,7 @@ def onselect(epress, erelease):
8182
tool = widgets.RectangleSelector(ax, onselect, interactive=True,
8283
drag_from_anywhere=drag_from_anywhere)
8384
# Create rectangle
84-
do_event(tool, 'press', xdata=0, ydata=10, button=1)
85-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
86-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
85+
click_and_move(tool, start=[0, 10], end=[100, 120])
8786
assert tool.center == (50, 65)
8887
# Drag inside rectangle, but away from centre handle
8988
#
@@ -92,15 +91,11 @@ def onselect(epress, erelease):
9291
#
9392
# If drag_from_anywhere == False, this will create a new rectangle with
9493
# center (30, 20)
95-
do_event(tool, 'press', xdata=25, ydata=15, button=1)
96-
do_event(tool, 'onmove', xdata=35, ydata=25, button=1)
97-
do_event(tool, 'release', xdata=35, ydata=25, button=1)
94+
click_and_move(tool, start=[25, 15], end=[35, 25])
9895
assert tool.center == new_center
9996
# Check that in both cases, dragging outside the rectangle draws a new
10097
# rectangle
101-
do_event(tool, 'press', xdata=175, ydata=185, button=1)
102-
do_event(tool, 'onmove', xdata=185, ydata=195, button=1)
103-
do_event(tool, 'release', xdata=185, ydata=195, button=1)
98+
click_and_move(tool, start=[175, 185], end=[185, 195])
10499
assert tool.center == (180, 190)
105100

106101

@@ -114,9 +109,7 @@ def onselect(epress, erelease):
114109
props=dict(facecolor='b', alpha=0.2),
115110
handle_props=dict(alpha=0.5))
116111
# Create rectangle
117-
do_event(tool, 'press', xdata=0, ydata=10, button=1)
118-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
119-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
112+
click_and_move(tool, start=[0, 10], end=[100, 120])
120113

121114
artist = tool._selection_artist
122115
assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2)
@@ -522,42 +515,20 @@ def onselect(epress, erelease):
522515
tool.extents = (100, 150, 100, 150)
523516

524517
# drag the rectangle
525-
do_event(tool, 'press', xdata=10, ydata=10, button=1,
526-
key=' ')
527-
528-
do_event(tool, 'onmove', xdata=30, ydata=30, button=1)
529-
do_event(tool, 'release', xdata=30, ydata=30, button=1)
518+
click_and_move(tool, start=[125, 125], end=[145, 145])
530519
assert tool.extents == (120, 170, 120, 170)
531520

532521
# create from center
533-
do_event(tool, 'on_key_press', xdata=100, ydata=100, button=1,
534-
key='control')
535-
do_event(tool, 'press', xdata=100, ydata=100, button=1)
536-
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
537-
do_event(tool, 'release', xdata=125, ydata=125, button=1)
538-
do_event(tool, 'on_key_release', xdata=100, ydata=100, button=1,
539-
key='control')
522+
click_and_move(tool, start=[100, 100], end=[125, 125], key='control')
540523
assert tool.extents == (75, 125, 75, 125)
541524

542525
# create a square
543-
do_event(tool, 'on_key_press', xdata=10, ydata=10, button=1,
544-
key='shift')
545-
do_event(tool, 'press', xdata=10, ydata=10, button=1)
546-
do_event(tool, 'onmove', xdata=35, ydata=30, button=1)
547-
do_event(tool, 'release', xdata=35, ydata=30, button=1)
548-
do_event(tool, 'on_key_release', xdata=10, ydata=10, button=1,
549-
key='shift')
526+
click_and_move(tool, start=[10, 10], end=[35, 30], key='shift')
550527
extents = [int(e) for e in tool.extents]
551528
assert extents == [10, 35, 10, 35]
552529

553530
# create a square from center
554-
do_event(tool, 'on_key_press', xdata=100, ydata=100, button=1,
555-
key='ctrl+shift')
556-
do_event(tool, 'press', xdata=100, ydata=100, button=1)
557-
do_event(tool, 'onmove', xdata=125, ydata=130, button=1)
558-
do_event(tool, 'release', xdata=125, ydata=130, button=1)
559-
do_event(tool, 'on_key_release', xdata=100, ydata=100, button=1,
560-
key='ctrl+shift')
531+
click_and_move(tool, start=[100, 100], end=[125, 130], key='ctrl+shift')
561532
extents = [int(e) for e in tool.extents]
562533
assert extents == [70, 130, 70, 130]
563534

@@ -585,21 +556,15 @@ def onselect(epress, erelease):
585556
assert tool.extents == (100, 150, 100, 150)
586557

587558
# grab a corner and move it
588-
do_event(tool, 'press', xdata=100, ydata=100)
589-
do_event(tool, 'onmove', xdata=120, ydata=120)
590-
do_event(tool, 'release', xdata=120, ydata=120)
559+
click_and_move(tool, start=[100, 100], end=[120, 120])
591560
assert tool.extents == (120, 150, 120, 150)
592561

593562
# grab the center and move it
594-
do_event(tool, 'press', xdata=132, ydata=132)
595-
do_event(tool, 'onmove', xdata=120, ydata=120)
596-
do_event(tool, 'release', xdata=120, ydata=120)
563+
click_and_move(tool, start=[132, 132], end=[120, 120])
597564
assert tool.extents == (108, 138, 108, 138)
598565

599566
# create a new rectangle
600-
do_event(tool, 'press', xdata=10, ydata=10)
601-
do_event(tool, 'onmove', xdata=100, ydata=100)
602-
do_event(tool, 'release', xdata=100, ydata=100)
567+
click_and_move(tool, start=[10, 10], end=[100, 100])
603568
assert tool.extents == (10, 100, 10, 100)
604569

605570
# Check that marker_props worked.
@@ -618,19 +583,15 @@ def onselect(vmin, vmax):
618583
ax._got_onselect = True
619584

620585
tool = widgets.RectangleSelector(ax, onselect, interactive=interactive)
621-
do_event(tool, 'press', xdata=100, ydata=110, button=1)
622586
# move outside of axis
623-
do_event(tool, 'onmove', xdata=150, ydata=120, button=1)
624-
do_event(tool, 'release', xdata=150, ydata=120, button=1)
587+
click_and_move(tool, start=[100, 110], end=[150, 120])
625588

626589
assert tool.ax._got_onselect
627590
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
628591

629592
# Reset tool.ax._got_onselect
630593
tool.ax._got_onselect = False
631-
632-
do_event(tool, 'press', xdata=10, ydata=100, button=1)
633-
do_event(tool, 'release', xdata=10, ydata=100, button=1)
594+
click_and_move(tool, start=[10, 100], end=[10, 100])
634595

635596
assert tool.ax._got_onselect
636597

@@ -643,19 +604,14 @@ def onselect(vmin, vmax):
643604

644605
tool = widgets.RectangleSelector(ax, onselect,
645606
ignore_event_outside=ignore_event_outside)
646-
do_event(tool, 'press', xdata=100, ydata=110, button=1)
647-
do_event(tool, 'onmove', xdata=150, ydata=120, button=1)
648-
do_event(tool, 'release', xdata=150, ydata=120, button=1)
649-
607+
click_and_move(tool, start=[100, 110], end=[150, 120])
650608
assert tool.ax._got_onselect
651609
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
652610

653611
# Reset
654612
ax._got_onselect = False
655613
# Trigger event outside of span
656-
do_event(tool, 'press', xdata=150, ydata=150, button=1)
657-
do_event(tool, 'onmove', xdata=160, ydata=160, button=1)
658-
do_event(tool, 'release', xdata=160, ydata=160, button=1)
614+
click_and_move(tool, start=[150, 150], end=[160, 160])
659615
if ignore_event_outside:
660616
# event have been ignored and span haven't changed.
661617
assert not ax._got_onselect
@@ -711,20 +667,14 @@ def onselect(vmin, vmax):
711667

712668
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
713669
interactive=interactive)
714-
do_event(tool, 'press', xdata=100, ydata=100, button=1)
715670
# move outside of axis
716-
do_event(tool, 'onmove', xdata=150, ydata=100, button=1)
717-
do_event(tool, 'release', xdata=150, ydata=100, button=1)
718-
671+
click_and_move(tool, start=[100, 100], end=[150, 100])
719672
assert tool.ax._got_onselect
720673
assert tool.extents == (100, 150)
721674

722675
# Reset tool.ax._got_onselect
723676
tool.ax._got_onselect = False
724-
725-
do_event(tool, 'press', xdata=10, ydata=100, button=1)
726-
do_event(tool, 'release', xdata=10, ydata=100, button=1)
727-
677+
click_and_move(tool, start=[10, 100], end=[10, 100])
728678
assert tool.ax._got_onselect
729679

730680

@@ -740,9 +690,7 @@ def onmove(vmin, vmax):
740690
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
741691
onmove_callback=onmove,
742692
ignore_event_outside=ignore_event_outside)
743-
do_event(tool, 'press', xdata=100, ydata=100, button=1)
744-
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
745-
do_event(tool, 'release', xdata=125, ydata=125, button=1)
693+
click_and_move(tool, start=[100, 100], end=[125, 125])
746694
assert ax._got_onselect
747695
assert ax._got_on_move
748696
assert tool.extents == (100, 125)
@@ -751,9 +699,7 @@ def onmove(vmin, vmax):
751699
ax._got_onselect = False
752700
ax._got_on_move = False
753701
# Trigger event outside of span
754-
do_event(tool, 'press', xdata=150, ydata=150, button=1)
755-
do_event(tool, 'onmove', xdata=160, ydata=160, button=1)
756-
do_event(tool, 'release', xdata=160, ydata=160, button=1)
702+
click_and_move(tool, start=[150, 150], end=[160, 160])
757703
if ignore_event_outside:
758704
# event have been ignored and span haven't changed.
759705
assert not ax._got_onselect
@@ -776,9 +722,7 @@ def onselect(*args):
776722
# Create span
777723
tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True,
778724
drag_from_anywhere=drag_from_anywhere)
779-
do_event(tool, 'press', xdata=10, ydata=10, button=1)
780-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
781-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
725+
click_and_move(tool, start=[10, 10], end=[100, 120])
782726
assert tool.extents == (10, 100)
783727
# Drag inside span
784728
#
@@ -787,18 +731,14 @@ def onselect(*args):
787731
#
788732
# If drag_from_anywhere == False, this will create a new span with
789733
# value extents = 25, 35
790-
do_event(tool, 'press', xdata=25, ydata=15, button=1)
791-
do_event(tool, 'onmove', xdata=35, ydata=25, button=1)
792-
do_event(tool, 'release', xdata=35, ydata=25, button=1)
734+
click_and_move(tool, start=[25, 15], end=[35, 25])
793735
if drag_from_anywhere:
794736
assert tool.extents == (20, 110)
795737
else:
796738
assert tool.extents == (25, 35)
797739

798740
# Check that in both cases, dragging outside the span draws a new span
799-
do_event(tool, 'press', xdata=175, ydata=185, button=1)
800-
do_event(tool, 'onmove', xdata=185, ydata=195, button=1)
801-
do_event(tool, 'release', xdata=185, ydata=195, button=1)
741+
click_and_move(tool, start=[175, 185], end=[185, 195])
802742
assert tool.extents == (175, 185)
803743

804744

@@ -833,9 +773,7 @@ def onselect(epress, erelease):
833773
props=dict(facecolor='b', alpha=0.2),
834774
handle_props=dict(alpha=0.5))
835775
# Create rectangle
836-
do_event(tool, 'press', xdata=0, ydata=10, button=1)
837-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
838-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
776+
click_and_move(tool, start=[0, 10], end=[100, 120])
839777

840778
artist = tool._selection_artist
841779
assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2)
@@ -866,26 +804,20 @@ def onselect(*args):
866804
Selector = widgets.RectangleSelector
867805

868806
tool = Selector(**kwargs)
869-
do_event(tool, 'press', xdata=10, ydata=10, button=1)
870-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
871-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
807+
click_and_move(tool, start=[10, 10], end=[100, 120])
872808

873809
# press-release event outside the selector to clear the selector
874-
do_event(tool, 'press', xdata=130, ydata=130, button=1)
875-
do_event(tool, 'release', xdata=130, ydata=130, button=1)
810+
click_and_move(tool, start=[130, 130], end=[130, 130])
876811
assert not tool._selection_completed
877812

878813
ax = get_ax()
879814
kwargs['ignore_event_outside'] = True
880815
tool = Selector(**kwargs)
881816
assert tool.ignore_event_outside
882-
do_event(tool, 'press', xdata=10, ydata=10, button=1)
883-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
884-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
817+
click_and_move(tool, start=[10, 10], end=[100, 120])
885818

886819
# press-release event outside the selector ignored
887-
do_event(tool, 'press', xdata=130, ydata=130, button=1)
888-
do_event(tool, 'release', xdata=130, ydata=130, button=1)
820+
click_and_move(tool, start=[130, 130], end=[130, 130])
889821
assert tool._selection_completed
890822

891823
do_event(tool, 'on_key_press', key='escape')
@@ -905,9 +837,7 @@ def onselect(*args):
905837
ignore_event_outside=True)
906838
else:
907839
tool = widgets.RectangleSelector(ax, onselect, interactive=True)
908-
do_event(tool, 'press', xdata=10, ydata=10, button=1)
909-
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
910-
do_event(tool, 'release', xdata=100, ydata=120, button=1)
840+
click_and_move(tool, start=[10, 10], end=[100, 120])
911841
assert tool._selection_completed
912842
assert tool.visible
913843
if selector == 'span':
@@ -918,9 +848,7 @@ def onselect(*args):
918848
assert not tool.visible
919849

920850
# Do another cycle of events to make sure we can
921-
do_event(tool, 'press', xdata=10, ydata=10, button=1)
922-
do_event(tool, 'onmove', xdata=50, ydata=120, button=1)
923-
do_event(tool, 'release', xdata=50, ydata=120, button=1)
851+
click_and_move(tool, start=[10, 10], end=[50, 120])
924852
assert tool._selection_completed
925853
assert tool.visible
926854
if selector == 'span':
@@ -985,8 +913,7 @@ def test_span_selector_bound(direction):
985913
press_data = [10.5, 11.5]
986914
move_data = [11, 13] # Updating selector is done in onmove
987915
release_data = move_data
988-
do_event(tool, 'press', xdata=press_data[0], ydata=press_data[1], button=1)
989-
do_event(tool, 'onmove', xdata=move_data[0], ydata=move_data[1], button=1)
916+
click_and_move(tool, start=press_data, end=move_data)
990917

991918
assert ax.get_xbound() == x_bound
992919
assert ax.get_ybound() == y_bound

0 commit comments

Comments
 (0)
0