77
88import pytest
99
10- try :
11- import PyQt4
12- except (ImportError , RuntimeError ): # RuntimeError if PyQt5 already imported.
10+
11+ @pytest .fixture (autouse = True )
12+ def mpl_test_settings (qt4_module , mpl_test_settings ):
13+ """
14+ Ensure qt4_module fixture is *first* fixture.
15+
16+ We override the `mpl_test_settings` fixture and depend on the `qt4_module`
17+ fixture first. It is very important that it is first, because it skips
18+ tests when Qt4 is not available, and if not, then the main
19+ `mpl_test_settings` fixture will try to switch backends before the skip can
20+ be triggered.
21+ """
22+ pass
23+
24+
25+ @pytest .fixture
26+ def qt4_module ():
1327 try :
14- import PySide
15- except ImportError :
16- pytestmark = pytest .mark .skip ("Failed to import a Qt4 binding." )
28+ import PyQt4
29+ # RuntimeError if PyQt5 already imported.
30+ except (ImportError , RuntimeError ):
31+ try :
32+ import PySide
33+ except ImportError :
34+ pytest .skip ("Failed to import a Qt4 binding." )
35+
36+ qt_compat = pytest .importorskip ('matplotlib.backends.qt_compat' )
37+ QtCore = qt_compat .QtCore
38+
39+ try :
40+ py_qt_ver = int (QtCore .PYQT_VERSION_STR .split ('.' )[0 ])
41+ except AttributeError :
42+ py_qt_ver = QtCore .__version_info__ [0 ]
43+
44+ if py_qt_ver != 4 :
45+ pytest .skip (reason = 'Qt4 is not available' )
46+
47+ from matplotlib .backends .backend_qt4 import (
48+ MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT ) # noqa
49+
50+ mods = {}
51+ keys = {}
52+ for name , index in zip (['Alt' , 'Control' , 'Shift' , 'Super' ],
53+ [ALT , CTRL , SHIFT , SUPER ]):
54+ _ , mod , key = MODIFIER_KEYS [index ]
55+ mods [name + 'Modifier' ] = mod
56+ keys [name + 'Key' ] = key
1757
18- qt_compat = pytest .importorskip ('matplotlib.backends.qt_compat' )
19- QtCore = qt_compat .QtCore
58+ return QtCore , mods , keys
2059
21- from matplotlib .backends .backend_qt4 import (
22- MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT ) # noqa
2360
24- _ , ControlModifier , ControlKey = MODIFIER_KEYS [CTRL ]
25- _ , AltModifier , AltKey = MODIFIER_KEYS [ALT ]
26- _ , SuperModifier , SuperKey = MODIFIER_KEYS [SUPER ]
27- _ , ShiftModifier , ShiftKey = MODIFIER_KEYS [SHIFT ]
61+ @pytest .fixture
62+ def qt_key (request ):
63+ QtCore , _ , keys = request .getfixturevalue ('qt4_module' )
64+ if request .param .startswith ('Key' ):
65+ return getattr (QtCore .Qt , request .param )
66+ else :
67+ return keys [request .param ]
2868
29- try :
30- py_qt_ver = int (QtCore .PYQT_VERSION_STR .split ('.' )[0 ])
31- except AttributeError :
32- py_qt_ver = QtCore .__version_info__ [0 ]
3369
34- if py_qt_ver != 4 :
35- pytestmark = pytest .mark .skipif (reason = 'Qt4 is not available' )
70+ @pytest .fixture
71+ def qt_mods (request ):
72+ QtCore , mods , _ = request .getfixturevalue ('qt4_module' )
73+ result = QtCore .Qt .NoModifier
74+ for mod in request .param :
75+ result |= mods [mod ]
76+ return result
3677
3778
3879@pytest .mark .backend ('Qt4Agg' )
@@ -55,21 +96,22 @@ def test_fig_close():
5596@pytest .mark .parametrize (
5697 'qt_key, qt_mods, answer' ,
5798 [
58- (QtCore . Qt . Key_A , ShiftModifier , 'A' ),
59- (QtCore . Qt . Key_A , QtCore . Qt . NoModifier , 'a' ),
60- (QtCore . Qt . Key_A , ControlModifier , 'ctrl+a' ),
61- (QtCore . Qt . Key_Aacute , ShiftModifier ,
99+ (' Key_A' , [ ' ShiftModifier' ] , 'A' ),
100+ (' Key_A' , [] , 'a' ),
101+ (' Key_A' , [ ' ControlModifier' ] , 'ctrl+a' ),
102+ (' Key_Aacute' , [ ' ShiftModifier' ] ,
62103 '\N{LATIN CAPITAL LETTER A WITH ACUTE} ' ),
63- (QtCore . Qt . Key_Aacute , QtCore . Qt . NoModifier ,
104+ (' Key_Aacute' , [] ,
64105 '\N{LATIN SMALL LETTER A WITH ACUTE} ' ),
65- (ControlKey , AltModifier , 'alt+control' ),
66- (AltKey , ControlModifier , 'ctrl+alt' ),
67- (QtCore . Qt . Key_Aacute , ( ControlModifier | AltModifier | SuperModifier ) ,
106+ (' ControlKey' , [ ' AltModifier' ] , 'alt+control' ),
107+ (' AltKey' , [ ' ControlModifier' ] , 'ctrl+alt' ),
108+ (' Key_Aacute' , [ ' ControlModifier' , ' AltModifier' , ' SuperModifier' ] ,
68109 'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE} ' ),
69- (QtCore . Qt . Key_Backspace , QtCore . Qt . NoModifier , 'backspace' ),
70- (QtCore . Qt . Key_Backspace , ControlModifier , 'ctrl+backspace' ),
71- (QtCore . Qt . Key_Play , QtCore . Qt . NoModifier , None ),
110+ (' Key_Backspace' , [] , 'backspace' ),
111+ (' Key_Backspace' , [ ' ControlModifier' ] , 'ctrl+backspace' ),
112+ (' Key_Play' , [] , None ),
72113 ],
114+ indirect = ['qt_key' , 'qt_mods' ],
73115 ids = [
74116 'shift' ,
75117 'lower' ,
0 commit comments