|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import tempfile |
| 4 | +from PIL import Image |
| 5 | + |
1 | 6 | import pytest |
2 | 7 |
|
3 | | -from matplotlib.backend_tools import ToolHelpBase |
| 8 | +import matplotlib |
| 9 | +from matplotlib.backend_tools import ToolHelpBase, ToolToggleBase |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +from matplotlib.testing import subprocess_run_helper |
| 12 | +from .test_backends_interactive import ( |
| 13 | + _get_testable_interactive_backends, _test_timeout, |
| 14 | + ) |
4 | 15 |
|
5 | 16 |
|
6 | 17 | @pytest.mark.parametrize('rc_shortcut,expected', [ |
|
18 | 29 | ]) |
19 | 30 | def test_format_shortcut(rc_shortcut, expected): |
20 | 31 | assert ToolHelpBase.format_shortcut(rc_shortcut) == expected |
| 32 | + |
| 33 | + |
| 34 | +def _test_toolbar_button_la_mode_icon_inside_subprocess(): |
| 35 | + matplotlib.rcParams["toolbar"] = "toolmanager" |
| 36 | + # create an icon in LA mode |
| 37 | + with tempfile.TemporaryDirectory() as tempdir: |
| 38 | + img = Image.new("LA", (26, 26)) |
| 39 | + tmp_img_path = os.path.join(tempdir, "test_la_icon.png") |
| 40 | + img.save(tmp_img_path) |
| 41 | + |
| 42 | + class CustomTool(ToolToggleBase): |
| 43 | + image = tmp_img_path |
| 44 | + description = "" # gtk3 backend does not allow None |
| 45 | + |
| 46 | + fig = plt.figure() |
| 47 | + toolmanager = fig.canvas.manager.toolmanager |
| 48 | + toolbar = fig.canvas.manager.toolbar |
| 49 | + toolmanager.add_tool("test", CustomTool) |
| 50 | + toolbar.add_tool("test", "group") |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "env", |
| 55 | + _get_testable_interactive_backends(), |
| 56 | + ) |
| 57 | +def test_toolbar_button_la_mode_icon(env): |
| 58 | + # test that icon in LA mode can be used for buttons |
| 59 | + # see GH#25164 |
| 60 | + try: |
| 61 | + # run inside subprocess for a self-contained environment |
| 62 | + proc = subprocess_run_helper( |
| 63 | + _test_toolbar_button_la_mode_icon_inside_subprocess, |
| 64 | + timeout=_test_timeout, |
| 65 | + extra_env=env, |
| 66 | + ) |
| 67 | + except subprocess.CalledProcessError as err: |
| 68 | + pytest.fail( |
| 69 | + f"subprocess failed to test intended behavior: {err.stderr}") |
0 commit comments