-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Qt4 keys #2273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Qt4 keys #2273
Changes from 1 commit
26e8b9f
62f564e
db86c40
1226094
b454f32
b80a8e7
41fdfae
b22193b
16ed6e8
60407dc
bf4e43d
32ddf1e
c9721fe
5cfbeb4
6770646
2bea38e
8f68252
ec40639
989ce39
2359f09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,17 @@ | |
from matplotlib._pylab_helpers import Gcf | ||
import copy | ||
|
||
import mock | ||
|
||
try: | ||
import matplotlib.backends.qt4_compat | ||
from matplotlib.backends.qt4_compat import QtCore | ||
from matplotlib.backends.backend_qt import MODIFIER_KEYS | ||
HAS_QT = True | ||
|
||
ShiftModifier = QtCore.Qt.ShiftModifier | ||
ControlModifier, ControlKey = MODIFIER_KEYS['ctrl'] | ||
AltModifier, AltKey = MODIFIER_KEYS['alt'] | ||
SuperModifier, SuperKey = MODIFIER_KEYS['super'] | ||
except ImportError: | ||
HAS_QT = False | ||
|
||
|
@@ -34,3 +42,92 @@ def test_fig_close(): | |
# assert that we have removed the reference to the FigureManager | ||
# that got added by plt.figure() | ||
assert(init_figs == Gcf.figs) | ||
|
||
|
||
def assert_correct_key(qt_key, qt_mods, qt_text, answer): | ||
plt.switch_backend('Qt4Agg') | ||
qt_canvas = plt.figure().canvas | ||
|
||
event = mock.Mock() | ||
event.isAutoRepeat.return_value = False | ||
event.key.return_value = qt_key | ||
event.modifiers.return_value = qt_mods | ||
event.text.return_value = qt_text | ||
|
||
def receive(event): | ||
assert event.key == answer | ||
|
||
qt_canvas.mpl_connect('key_press_event', receive) | ||
qt_canvas.keyPressEvent(event) | ||
|
||
|
||
@cleanup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the cleanup decorator is only for when we are plotting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I missed that. Yes, that does count as "plotting". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be easier to go with a test class that has a setup and teardown so we don't have to do cleanup for each test function here? Don't know if it would make a difference or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this way is more robust. A test class would presumably have a single, shared figure that all the callback test use. I don't fully trust key_press_events cough_macos_cough to actually get fired. In the event that one doesn't fire, the test will eventually time out. I don't know what cascade gets launched if I'm using a class based test with a shared figure and one of the tests times out. That and I like functions : ) |
||
@knownfailureif(not HAS_QT) | ||
def test_shift(): | ||
assert_correct_key(QtCore.Qt.Key_A, | ||
ShiftModifier, | ||
u'A', | ||
u'A') | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_noshift(): | ||
assert_correct_key(QtCore.Qt.Key_A, | ||
QtCore.Qt.NoModifier, | ||
u'a', | ||
u'a') | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_control(): | ||
assert_correct_key(QtCore.Qt.Key_A, | ||
ControlModifier, | ||
u'', | ||
u'ctrl+a') | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_unicode(): | ||
assert_correct_key(QtCore.Qt.Key_Aacute, | ||
ShiftModifier, | ||
unichr(193), | ||
unichr(193)) | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_unicode_noshift(): | ||
assert_correct_key(QtCore.Qt.Key_Aacute, | ||
QtCore.Qt.NoModifier, | ||
unichr(193), | ||
unichr(225)) | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_alt_control(): | ||
assert_correct_key(QtCore.Qt.Key_Control, | ||
AltModifier, | ||
u'', | ||
u'alt+control') | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_control_alt(): | ||
assert_correct_key(QtCore.Qt.Key_Alt, | ||
ControlModifier, | ||
u'', | ||
u'ctrl+alt') | ||
|
||
|
||
@cleanup | ||
@knownfailureif(not HAS_QT) | ||
def test_modifier_order(): | ||
assert_correct_key(QtCore.Qt.Key_Aacute, | ||
(ControlModifier & AltModifier & SuperModifier), | ||
u'', | ||
u'ctrl+alt+super+' + unichr(225)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a new mpl dependency? I've no problem using mock (it is the go to for things like this), just wanted to clarify whether we need to update docs etc. ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah -- seeing this, I can see the advantage of this, and I wouldn't want to reinvent the mock wheel. (It it might prove to be useful elsewhere).
We should update the docs. Since we now have automatic dependency resolution, we should update the
setupext.py
configuration forTests
as well. And maybe, for good measure, in case all that fails, mark these tests as "skip" or "knownfail" if mock is not installed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mock is convenient, but not necessary for my tests. i can get the same effect by attaching several
lambda self: return_value
to anObject()
The total line count will be essentially the same.give me a yea or nay on mock, and I'll add the needed things to
setupext.py