|
7 | 7 | import signal
|
8 | 8 | import subprocess
|
9 | 9 | import sys
|
| 10 | +import tempfile |
10 | 11 | import time
|
11 | 12 | import urllib.request
|
12 | 13 |
|
| 14 | +from PIL import Image |
| 15 | + |
13 | 16 | import pytest
|
14 | 17 |
|
15 | 18 | import matplotlib as mpl
|
16 | 19 | from matplotlib import _c_internal_utils
|
| 20 | +from matplotlib.backend_tools import ToolToggleBase |
17 | 21 | from matplotlib.testing import subprocess_run_helper as _run_helper
|
18 | 22 |
|
19 | 23 |
|
@@ -71,6 +75,24 @@ def _get_testable_interactive_backends():
|
71 | 75 | _test_timeout = 60 # A reasonably safe value for slower architectures.
|
72 | 76 |
|
73 | 77 |
|
| 78 | +def _test_toolbar_button_la_mode_icon(fig): |
| 79 | + # test a toolbar button icon using an image in LA mode (GH issue 25174) |
| 80 | + # create an icon in LA mode |
| 81 | + with tempfile.TemporaryDirectory() as tempdir: |
| 82 | + img = Image.new("LA", (26, 26)) |
| 83 | + tmp_img_path = os.path.join(tempdir, "test_la_icon.png") |
| 84 | + img.save(tmp_img_path) |
| 85 | + |
| 86 | + class CustomTool(ToolToggleBase): |
| 87 | + image = tmp_img_path |
| 88 | + description = "" # gtk3 backend does not allow None |
| 89 | + |
| 90 | + toolmanager = fig.canvas.manager.toolmanager |
| 91 | + toolbar = fig.canvas.manager.toolbar |
| 92 | + toolmanager.add_tool("test", CustomTool) |
| 93 | + toolbar.add_tool("test", "group") |
| 94 | + |
| 95 | + |
74 | 96 | # The source of this function gets extracted and run in another process, so it
|
75 | 97 | # must be fully self-contained.
|
76 | 98 | # Using a timer not only allows testing of timers (on other backends), but is
|
@@ -122,14 +144,17 @@ def check_alt_backend(alt_backend):
|
122 | 144 | if importlib.util.find_spec("cairocffi"):
|
123 | 145 | check_alt_backend(backend[:-3] + "cairo")
|
124 | 146 | check_alt_backend("svg")
|
125 |
| - |
126 | 147 | mpl.use(backend, force=True)
|
127 | 148 |
|
128 | 149 | fig, ax = plt.subplots()
|
129 | 150 | assert_equal(
|
130 | 151 | type(fig.canvas).__module__,
|
131 | 152 | "matplotlib.backends.backend_{}".format(backend))
|
132 | 153 |
|
| 154 | + if mpl.rcParams["toolbar"] == "toolmanager": |
| 155 | + # test toolbar button icon LA mode see GH issue 25174 |
| 156 | + _test_toolbar_button_la_mode_icon(fig) |
| 157 | + |
133 | 158 | ax.plot([0, 1], [2, 3])
|
134 | 159 | if fig.canvas.toolbar: # i.e toolbar2.
|
135 | 160 | fig.canvas.toolbar.draw_rubberband(None, 1., 1, 2., 2)
|
@@ -168,11 +193,17 @@ def test_interactive_backend(env, toolbar):
|
168 | 193 | pytest.skip("toolmanager is not implemented for macosx.")
|
169 | 194 | if env["MPLBACKEND"] == "wx":
|
170 | 195 | pytest.skip("wx backend is deprecated; tests failed on appveyor")
|
171 |
| - proc = _run_helper(_test_interactive_impl, |
172 |
| - json.dumps({"toolbar": toolbar}), |
173 |
| - timeout=_test_timeout, |
174 |
| - extra_env=env) |
175 |
| - |
| 196 | + try: |
| 197 | + proc = _run_helper( |
| 198 | + _test_interactive_impl, |
| 199 | + json.dumps({"toolbar": toolbar}), |
| 200 | + timeout=_test_timeout, |
| 201 | + extra_env=env, |
| 202 | + ) |
| 203 | + except subprocess.CalledProcessError as err: |
| 204 | + pytest.fail( |
| 205 | + "Subprocess failed to test intended behavior\n" |
| 206 | + + str(err.stderr)) |
176 | 207 | assert proc.stdout.count("CloseEvent") == 1
|
177 | 208 |
|
178 | 209 |
|
|
0 commit comments