|
4 | 4 | from matplotlib.backend_bases import (
|
5 | 5 | FigureCanvasBase, LocationEvent, MouseButton, MouseEvent,
|
6 | 6 | NavigationToolbar2, RendererBase)
|
| 7 | +from matplotlib.backend_tools import RubberbandBase |
7 | 8 | from matplotlib.figure import Figure
|
8 | 9 | from matplotlib.testing._markers import needs_pgf_xelatex
|
9 | 10 | import matplotlib.pyplot as plt
|
@@ -349,3 +350,56 @@ def test_interactive_pan(key, mouseend, expectedxlim, expectedylim):
|
349 | 350 | # Should be close, but won't be exact due to screen integer resolution
|
350 | 351 | assert tuple(ax.get_xlim()) == pytest.approx(expectedxlim, abs=0.02)
|
351 | 352 | assert tuple(ax.get_ylim()) == pytest.approx(expectedylim, abs=0.02)
|
| 353 | + |
| 354 | + |
| 355 | +def test_toolmanager_remove(): |
| 356 | + expected_warning_regex = ( |
| 357 | + r"Treat the new Tool classes introduced in " |
| 358 | + r"v[0-9]*.[0-9]* as experimental for now; " |
| 359 | + "the API and rcParam may change in future versions.") |
| 360 | + with pytest.warns(UserWarning, match=expected_warning_regex): |
| 361 | + plt.rcParams['toolbar'] = 'toolmanager' |
| 362 | + fig = plt.gcf() |
| 363 | + initial_len = len(fig.canvas.manager.toolmanager.tools) |
| 364 | + assert 'forward' in fig.canvas.manager.toolmanager.tools.keys() |
| 365 | + fig.canvas.manager.toolmanager.remove_tool('forward') |
| 366 | + assert len(fig.canvas.manager.toolmanager.tools) == initial_len - 1 |
| 367 | + assert 'forward' not in fig.canvas.manager.toolmanager.tools.keys() |
| 368 | + |
| 369 | + |
| 370 | +def test_toolmanager_get_tool(): |
| 371 | + expected_warning_regex = ( |
| 372 | + r"Treat the new Tool classes introduced in " |
| 373 | + r"v[0-9]*.[0-9]* as experimental for now; " |
| 374 | + "the API and rcParam may change in future versions.") |
| 375 | + with pytest.warns(UserWarning, match=expected_warning_regex): |
| 376 | + plt.rcParams['toolbar'] = 'toolmanager' |
| 377 | + fig = plt.gcf() |
| 378 | + rubberband = fig.canvas.manager.toolmanager.get_tool('rubberband') |
| 379 | + assert isinstance(rubberband, RubberbandBase) |
| 380 | + assert fig.canvas.manager.toolmanager.get_tool(rubberband) is rubberband |
| 381 | + with pytest.warns(UserWarning, |
| 382 | + match="ToolManager does not control tool 'foo'"): |
| 383 | + assert fig.canvas.manager.toolmanager.get_tool('foo') is None |
| 384 | + assert fig.canvas.manager.toolmanager.get_tool('foo', warn=False) is None |
| 385 | + |
| 386 | + with pytest.warns(UserWarning, |
| 387 | + match="ToolManager does not control tool 'foo'"): |
| 388 | + assert fig.canvas.manager.toolmanager.trigger_tool('foo') is None |
| 389 | + |
| 390 | + |
| 391 | +def test_toolmanager_update_keymap(): |
| 392 | + expected_warning_regex = ( |
| 393 | + r"Treat the new Tool classes introduced in " |
| 394 | + r"v[0-9]*.[0-9]* as experimental for now; " |
| 395 | + "the API and rcParam may change in future versions.") |
| 396 | + with pytest.warns(UserWarning, match=expected_warning_regex): |
| 397 | + plt.rcParams['toolbar'] = 'toolmanager' |
| 398 | + fig = plt.gcf() |
| 399 | + assert 'v' in fig.canvas.manager.toolmanager.get_tool_keymap('forward') |
| 400 | + with pytest.warns(UserWarning, |
| 401 | + match="Key c changed from back to forward"): |
| 402 | + fig.canvas.manager.toolmanager.update_keymap('forward', 'c') |
| 403 | + assert fig.canvas.manager.toolmanager.get_tool_keymap('forward') == ['c'] |
| 404 | + with pytest.raises(KeyError, match="'foo' not in Tools"): |
| 405 | + fig.canvas.manager.toolmanager.update_keymap('foo', 'c') |
0 commit comments