Skip to content

Commit a45ef42

Browse files
committed
Add tests for polygon selector box
Trigger events on canvas in test Test doc fixes
1 parent f228e2b commit a45ef42

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ def check_polygon_selector(event_sequence, expected_result, selections_count,
11321132
selections_count : int
11331133
Wait for the tool to call its `onselect` function `selections_count`
11341134
times, before comparing the result to the `expected_result`
1135-
kwargs :
1135+
**kwargs
11361136
Keyword arguments are passed to PolygonSelector.
11371137
"""
11381138
ax = get_ax()
@@ -1396,6 +1396,56 @@ def onselect(vertices):
13961396
do_event(tool_ref, etype, **event_args)
13971397

13981398

1399+
def test_polygon_selector_box():
1400+
# Create a diamond shape
1401+
verts = [(20, 0), (0, 20), (20, 40), (40, 20)]
1402+
event_sequence = (polygon_place_vertex(*verts[0]) +
1403+
polygon_place_vertex(*verts[1]) +
1404+
polygon_place_vertex(*verts[2]) +
1405+
polygon_place_vertex(*verts[3]) +
1406+
polygon_place_vertex(*verts[0]))
1407+
1408+
ax = get_ax()
1409+
1410+
def onselect(vertices):
1411+
pass
1412+
1413+
# Create selector
1414+
tool = widgets.PolygonSelector(ax, onselect, draw_box=True)
1415+
for (etype, event_args) in event_sequence:
1416+
do_event(tool, etype, **event_args)
1417+
1418+
# In order to trigger the correct callbacks, trigger events on the canvas
1419+
# instead of the individual tools
1420+
t = ax.transData
1421+
canvas = ax.figure.canvas
1422+
1423+
# Scale to half size using the top right corner of the bounding box
1424+
canvas.button_press_event(*t.transform((40, 40)), 1)
1425+
canvas.motion_notify_event(*t.transform((20, 20)))
1426+
canvas.button_release_event(*t.transform((20, 20)), 1)
1427+
np.testing.assert_allclose(
1428+
tool.verts, [(10, 0), (0, 10), (10, 20), (20, 10)])
1429+
1430+
# Move using the center of the bounding box
1431+
canvas.button_press_event(*t.transform((10, 10)), 1)
1432+
canvas.motion_notify_event(*t.transform((30, 30)))
1433+
canvas.button_release_event(*t.transform((30, 30)), 1)
1434+
np.testing.assert_allclose(
1435+
tool.verts, [(30, 20), (20, 30), (30, 40), (40, 30)])
1436+
1437+
# Remove a point from the polygon and check that the box extents update
1438+
np.testing.assert_allclose(
1439+
tool._box.extents, (20.0, 40.0, 20.0, 40.0))
1440+
1441+
canvas.button_press_event(*t.transform((30, 20)), 3)
1442+
canvas.button_release_event(*t.transform((30, 20)), 3)
1443+
np.testing.assert_allclose(
1444+
tool.verts, [(20, 30), (30, 40), (40, 30)])
1445+
np.testing.assert_allclose(
1446+
tool._box.extents, (20.0, 40.0, 30.0, 40.0))
1447+
1448+
13991449
@pytest.mark.parametrize(
14001450
"horizOn, vertOn",
14011451
[(True, True), (True, False), (False, True)],

0 commit comments

Comments
 (0)