Skip to content

Commit fac4ad5

Browse files
authored
Merge pull request #14506 from anntzer/testqt
TST: Simplify Qt tests.
2 parents 476e0c2 + a4c504c commit fac4ad5

File tree

1 file changed

+18
-53
lines changed

1 file changed

+18
-53
lines changed

lib/matplotlib/tests/test_backend_qt.py

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@
1010

1111

1212
@pytest.fixture(autouse=True)
13-
def mpl_test_settings(qt_module, mpl_test_settings):
13+
def mpl_test_settings(qt_core, mpl_test_settings):
1414
"""
15-
Ensure qt_module fixture is *first* fixture.
15+
Ensure qt_core fixture is *first* fixture.
1616
17-
We override the `mpl_test_settings` fixture and depend on the `qt_module`
17+
We override the `mpl_test_settings` fixture and depend on the `qt_core`
1818
fixture first. It is very important that it is first, because it skips
1919
tests when Qt is not available, and if not, then the main
2020
`mpl_test_settings` fixture will try to switch backends before the skip can
2121
be triggered.
2222
"""
23-
pass
2423

2524

2625
@pytest.fixture
27-
def qt_module(request):
26+
def qt_core(request):
2827
backend, = request.node.get_closest_marker('backend').args
2928
if backend == 'Qt4Agg':
3029
if any(k in sys.modules for k in ('PyQt5', 'PySide2')):
@@ -59,43 +58,10 @@ def qt_module(request):
5958
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
6059
except AttributeError:
6160
py_qt_ver = QtCore.__version_info__[0]
62-
6361
if py_qt_ver != 4:
6462
pytest.skip('Qt4 is not available')
6563

66-
from matplotlib.backends.backend_qt4 import (
67-
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT)
68-
elif backend == 'Qt5Agg':
69-
from matplotlib.backends.backend_qt5 import (
70-
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT)
71-
72-
mods = {}
73-
keys = {}
74-
for name, index in zip(['Alt', 'Control', 'Shift', 'Super'],
75-
[ALT, CTRL, SHIFT, SUPER]):
76-
_, mod, key = MODIFIER_KEYS[index]
77-
mods[name + 'Modifier'] = mod
78-
keys[name + 'Key'] = key
79-
80-
return QtCore, mods, keys
81-
82-
83-
@pytest.fixture
84-
def qt_key(request):
85-
QtCore, _, keys = request.getfixturevalue('qt_module')
86-
if request.param.startswith('Key'):
87-
return getattr(QtCore.Qt, request.param)
88-
else:
89-
return keys[request.param]
90-
91-
92-
@pytest.fixture
93-
def qt_mods(request):
94-
QtCore, mods, _ = request.getfixturevalue('qt_module')
95-
result = QtCore.Qt.NoModifier
96-
for mod in request.param:
97-
result |= mods[mod]
98-
return result
64+
return QtCore
9965

10066

10167
@pytest.mark.parametrize('backend', [
@@ -120,13 +86,10 @@ def test_fig_close(backend):
12086

12187

12288
@pytest.mark.backend('Qt5Agg')
123-
def test_fig_signals(qt_module):
89+
def test_fig_signals(qt_core):
12490
# Create a figure
12591
plt.figure()
12692

127-
# Access QtCore
128-
QtCore = qt_module[0]
129-
13093
# Access signals
13194
import signal
13295
event_loop_signal = None
@@ -138,10 +101,10 @@ def fire_signal_and_quit():
138101
event_loop_signal = signal.getsignal(signal.SIGINT)
139102

140103
# Request event loop exit
141-
QtCore.QCoreApplication.exit()
104+
qt_core.QCoreApplication.exit()
142105

143106
# Timer to exit event loop
144-
QtCore.QTimer.singleShot(0, fire_signal_and_quit)
107+
qt_core.QTimer.singleShot(0, fire_signal_and_quit)
145108

146109
# Save original SIGINT handler
147110
original_signal = signal.getsignal(signal.SIGINT)
@@ -176,15 +139,14 @@ def CustomHandler(signum, frame):
176139
'\N{LATIN CAPITAL LETTER A WITH ACUTE}'),
177140
('Key_Aacute', [],
178141
'\N{LATIN SMALL LETTER A WITH ACUTE}'),
179-
('ControlKey', ['AltModifier'], 'alt+control'),
180-
('AltKey', ['ControlModifier'], 'ctrl+alt'),
181-
('Key_Aacute', ['ControlModifier', 'AltModifier', 'SuperModifier'],
142+
('Key_Control', ['AltModifier'], 'alt+control'),
143+
('Key_Alt', ['ControlModifier'], 'ctrl+alt'),
144+
('Key_Aacute', ['ControlModifier', 'AltModifier', 'MetaModifier'],
182145
'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE}'),
183146
('Key_Backspace', [], 'backspace'),
184147
('Key_Backspace', ['ControlModifier'], 'ctrl+backspace'),
185148
('Key_Play', [], None),
186149
],
187-
indirect=['qt_key', 'qt_mods'],
188150
ids=[
189151
'shift',
190152
'lower',
@@ -204,23 +166,26 @@ def CustomHandler(signum, frame):
204166
pytest.param('Qt4Agg', marks=pytest.mark.backend('Qt4Agg')),
205167
pytest.param('Qt5Agg', marks=pytest.mark.backend('Qt5Agg')),
206168
])
207-
def test_correct_key(backend, qt_key, qt_mods, answer):
169+
def test_correct_key(backend, qt_core, qt_key, qt_mods, answer):
208170
"""
209171
Make a figure.
210172
Send a key_press_event event (using non-public, qtX backend specific api).
211173
Catch the event.
212174
Assert sent and caught keys are the same.
213175
"""
214-
qt_canvas = plt.figure().canvas
176+
qt_mod = qt_core.Qt.NoModifier
177+
for mod in qt_mods:
178+
qt_mod |= getattr(qt_core.Qt, mod)
215179

216180
class _Event:
217181
def isAutoRepeat(self): return False
218-
def key(self): return qt_key
219-
def modifiers(self): return qt_mods
182+
def key(self): return getattr(qt_core.Qt, qt_key)
183+
def modifiers(self): return qt_mod
220184

221185
def receive(event):
222186
assert event.key == answer
223187

188+
qt_canvas = plt.figure().canvas
224189
qt_canvas.mpl_connect('key_press_event', receive)
225190
qt_canvas.keyPressEvent(_Event())
226191

0 commit comments

Comments
 (0)