11from __future__ import (absolute_import , division , print_function ,
22 unicode_literals )
3-
43from matplotlib .externals import six
54
65from matplotlib import pyplot as plt
7- from matplotlib .testing .decorators import cleanup
6+ from matplotlib .testing .decorators import cleanup , switch_backend
87from matplotlib .testing .decorators import knownfailureif
98from matplotlib ._pylab_helpers import Gcf
9+ import matplotlib .style as mstyle
1010import copy
1111
1212try :
1616 import mock
1717
1818try :
19- from matplotlib .backends .qt_compat import QtCore
19+ with mstyle .context ({'backend' : 'Qt5Agg' }):
20+ from matplotlib .backends .qt_compat import QtCore , __version__
2021 from matplotlib .backends .backend_qt5 import (MODIFIER_KEYS ,
2122 SUPER , ALT , CTRL , SHIFT )
2223
2324 _ , ControlModifier , ControlKey = MODIFIER_KEYS [CTRL ]
2425 _ , AltModifier , AltKey = MODIFIER_KEYS [ALT ]
2526 _ , SuperModifier , SuperKey = MODIFIER_KEYS [SUPER ]
2627 _ , ShiftModifier , ShiftKey = MODIFIER_KEYS [SHIFT ]
27- HAS_QT = True
28+
29+ py_qt_ver = int (__version__ .split ('.' )[0 ])
30+ HAS_QT = py_qt_ver == 5
31+
2832except ImportError :
2933 HAS_QT = False
3034
3135
3236@cleanup
3337@knownfailureif (not HAS_QT )
38+ @switch_backend ('Qt5Agg' )
3439def test_fig_close ():
35- # force switch to the Qt5 backend
36- plt .switch_backend ('Qt5Agg' )
37-
3840 # save the state of Gcf.figs
3941 init_figs = copy .copy (Gcf .figs )
4042
@@ -50,14 +52,14 @@ def test_fig_close():
5052 assert (init_figs == Gcf .figs )
5153
5254
55+ @switch_backend ('Qt5Agg' )
5356def assert_correct_key (qt_key , qt_mods , answer ):
5457 """
5558 Make a figure
5659 Send a key_press_event event (using non-public, qt5 backend specific api)
5760 Catch the event
5861 Assert sent and caught keys are the same
5962 """
60- plt .switch_backend ('Qt5Agg' )
6163 qt_canvas = plt .figure ().canvas
6264
6365 event = mock .Mock ()
@@ -101,15 +103,15 @@ def test_control():
101103def test_unicode_upper ():
102104 assert_correct_key (QtCore .Qt .Key_Aacute ,
103105 ShiftModifier ,
104- unichr (193 ))
106+ six . unichr (193 ))
105107
106108
107109@cleanup
108110@knownfailureif (not HAS_QT )
109111def test_unicode_lower ():
110112 assert_correct_key (QtCore .Qt .Key_Aacute ,
111113 QtCore .Qt .NoModifier ,
112- unichr (225 ))
114+ six . unichr (225 ))
113115
114116
115117@cleanup
@@ -133,7 +135,7 @@ def test_control_alt():
133135def test_modifier_order ():
134136 assert_correct_key (QtCore .Qt .Key_Aacute ,
135137 (ControlModifier | AltModifier | SuperModifier ),
136- 'ctrl+alt+super+' + unichr (225 ))
138+ 'ctrl+alt+super+' + six . unichr (225 ))
137139
138140
139141@cleanup
0 commit comments