Skip to content

Commit 66a8dd5

Browse files
committed
Moved LA file mode for toolbar icon test in test_backends_interactive.py
1 parent 6ff693a commit 66a8dd5

File tree

2 files changed

+38
-56
lines changed

2 files changed

+38
-56
lines changed
Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
import os
2-
import subprocess
3-
import tempfile
4-
from PIL import Image
5-
61
import pytest
72

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-
)
3+
from matplotlib.backend_tools import ToolHelpBase
154

165

176
@pytest.mark.parametrize('rc_shortcut,expected', [
@@ -29,41 +18,3 @@
2918
])
3019
def test_format_shortcut(rc_shortcut, expected):
3120
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}")

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import signal
88
import subprocess
99
import sys
10+
import tempfile
1011
import time
1112
import urllib.request
1213

14+
from PIL import Image
15+
1316
import pytest
1417

1518
import matplotlib as mpl
1619
from matplotlib import _c_internal_utils
20+
from matplotlib.backend_tools import ToolToggleBase
1721
from matplotlib.testing import subprocess_run_helper as _run_helper
1822

1923

@@ -71,6 +75,24 @@ def _get_testable_interactive_backends():
7175
_test_timeout = 60 # A reasonably safe value for slower architectures.
7276

7377

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+
7496
# The source of this function gets extracted and run in another process, so it
7597
# must be fully self-contained.
7698
# Using a timer not only allows testing of timers (on other backends), but is
@@ -122,14 +144,17 @@ def check_alt_backend(alt_backend):
122144
if importlib.util.find_spec("cairocffi"):
123145
check_alt_backend(backend[:-3] + "cairo")
124146
check_alt_backend("svg")
125-
126147
mpl.use(backend, force=True)
127148

128149
fig, ax = plt.subplots()
129150
assert_equal(
130151
type(fig.canvas).__module__,
131152
f"matplotlib.backends.backend_{backend}")
132153

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+
133158
ax.plot([0, 1], [2, 3])
134159
if fig.canvas.toolbar: # i.e toolbar2.
135160
fig.canvas.toolbar.draw_rubberband(None, 1., 1, 2., 2)
@@ -168,11 +193,17 @@ def test_interactive_backend(env, toolbar):
168193
pytest.skip("toolmanager is not implemented for macosx.")
169194
if env["MPLBACKEND"] == "wx":
170195
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))
176207
assert proc.stdout.count("CloseEvent") == 1
177208

178209

0 commit comments

Comments
 (0)