Move fake_keyevent to test.helpers

This commit is contained in:
Florian Bruhin 2014-07-04 07:35:04 +02:00
parent 734ee74e8a
commit 539325db90
2 changed files with 47 additions and 44 deletions

View File

@ -21,6 +21,9 @@
import os
from contextlib import contextmanager
from unittest.mock import Mock, create_autospec
from PyQt5.QtGui import QKeyEvent
@contextmanager
@ -36,3 +39,12 @@ def environ_set_temp(name, value):
os.environ[name] = oldval
else:
del os.environ[name]
def fake_keyevent(key, modifiers=0, text=''):
"""Generate a new fake QKeyPressEvent."""
mock = create_autospec(QKeyEvent, instance=True)
mock.key.return_value = key
mock.modifiers.return_value = modifiers
mock.text.return_value = text
return mock

View File

@ -26,9 +26,9 @@ from unittest.mock import Mock, create_autospec, patch
import qutebrowser.keyinput.basekeyparser as basekeyparser
from qutebrowser.test.stubs import ConfigStub
from qutebrowser.test.helpers import fake_keyevent
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent
CONFIG = {'test': {'<Ctrl-a>': 'ctrla',
'a': 'a',
@ -39,15 +39,6 @@ CONFIG = {'test': {'<Ctrl-a>': 'ctrla',
'test2': {'foo': 'bar', '<Ctrl+X>': 'ctrlx'}}
def _fake_keyevent(key, modifiers=0, text=''):
"""Generate a new fake QKeyPressEvent."""
mock = create_autospec(QKeyEvent, instance=True)
mock.key.return_value = key
mock.modifiers.return_value = modifiers
mock.text.return_value = text
return mock
def setUpModule():
"""Mock out some imports in basekeyparser."""
basekeyparser.QObject = Mock()
@ -145,20 +136,20 @@ class SpecialKeysTests(unittest.TestCase):
def test_valid_key(self):
"""Test a valid special keyevent."""
self.kp.handle(_fake_keyevent(Qt.Key_A, Qt.ControlModifier))
self.kp.handle(_fake_keyevent(Qt.Key_X, Qt.ControlModifier))
self.kp.handle(fake_keyevent(Qt.Key_A, Qt.ControlModifier))
self.kp.handle(fake_keyevent(Qt.Key_X, Qt.ControlModifier))
self.kp.execute.assert_called_once_with('ctrla', self.kp.Type.special)
def test_invalid_key(self):
"""Test an invalid special keyevent."""
self.kp.handle(_fake_keyevent(Qt.Key_A, (Qt.ControlModifier |
self.kp.handle(fake_keyevent(Qt.Key_A, (Qt.ControlModifier |
Qt.AltModifier)))
self.assertFalse(self.kp.execute.called)
def test_keychain(self):
"""Test a keychain."""
self.kp.handle(_fake_keyevent(Qt.Key_B))
self.kp.handle(_fake_keyevent(Qt.Key_A))
self.kp.handle(fake_keyevent(Qt.Key_B))
self.kp.handle(fake_keyevent(Qt.Key_A))
self.assertFalse(self.kp.execute.called)
@ -183,14 +174,14 @@ class KeyChainTests(unittest.TestCase):
def test_valid_special_key(self):
"""Test valid special key."""
self.kp.handle(_fake_keyevent(Qt.Key_A, Qt.ControlModifier))
self.kp.handle(_fake_keyevent(Qt.Key_X, Qt.ControlModifier))
self.kp.handle(fake_keyevent(Qt.Key_A, Qt.ControlModifier))
self.kp.handle(fake_keyevent(Qt.Key_X, Qt.ControlModifier))
self.kp.execute.assert_called_once_with('ctrla', self.kp.Type.special)
self.assertEqual(self.kp._keystring, '')
def test_invalid_special_key(self):
"""Test invalid special key."""
self.kp.handle(_fake_keyevent(Qt.Key_A, (Qt.ControlModifier |
self.kp.handle(fake_keyevent(Qt.Key_A, (Qt.ControlModifier |
Qt.AltModifier)))
self.assertFalse(self.kp.execute.called)
self.assertEqual(self.kp._keystring, '')
@ -198,10 +189,10 @@ class KeyChainTests(unittest.TestCase):
def test_keychain(self):
"""Test valid keychain."""
# Press 'x' which is ignored because of no match
self.kp.handle(_fake_keyevent(Qt.Key_X, text='x'))
self.kp.handle(fake_keyevent(Qt.Key_X, text='x'))
# Then start the real chain
self.kp.handle(_fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='a'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='a'))
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None)
self.assertEqual(self.kp._keystring, '')
@ -209,7 +200,7 @@ class KeyChainTests(unittest.TestCase):
"""Test ambigious keychain."""
# We start with 'a' where the keychain gives us an ambigious result.
# Then we check if the timer has been set up correctly
self.kp.handle(_fake_keyevent(Qt.Key_A, text='a'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='a'))
self.assertFalse(self.kp.execute.called)
basekeyparser.Timer.assert_called_once_with(self.kp, 'ambigious_match')
self.timermock.setSingleShot.assert_called_once_with(True)
@ -219,15 +210,15 @@ class KeyChainTests(unittest.TestCase):
self.timermock.start.assert_called_once_with()
# Now we type an 'x' and check 'ax' has been executed and the timer
# stopped.
self.kp.handle(_fake_keyevent(Qt.Key_X, text='x'))
self.kp.handle(fake_keyevent(Qt.Key_X, text='x'))
self.kp.execute.assert_called_once_with('ax', self.kp.Type.chain, None)
self.timermock.stop.assert_called_once_with()
self.assertEqual(self.kp._keystring, '')
def test_invalid_keychain(self):
"""Test invalid keychain."""
self.kp.handle(_fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(_fake_keyevent(Qt.Key_C, text='c'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent(Qt.Key_C, text='c'))
self.assertEqual(self.kp._keystring, '')
@ -245,44 +236,44 @@ class CountTests(unittest.TestCase):
def test_no_count(self):
"""Test with no count added."""
self.kp.handle(_fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='a'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='a'))
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None)
self.assertEqual(self.kp._keystring, '')
def test_count_0(self):
"""Test with count=0."""
self.kp.handle(_fake_keyevent(Qt.Key_0, text='0'))
self.kp.handle(_fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='a'))
self.kp.handle(fake_keyevent(Qt.Key_0, text='0'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='a'))
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, 0)
self.assertEqual(self.kp._keystring, '')
def test_count_42(self):
"""Test with count=42."""
self.kp.handle(_fake_keyevent(Qt.Key_4, text='4'))
self.kp.handle(_fake_keyevent(Qt.Key_2, text='2'))
self.kp.handle(_fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='a'))
self.kp.handle(fake_keyevent(Qt.Key_4, text='4'))
self.kp.handle(fake_keyevent(Qt.Key_2, text='2'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='a'))
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, 42)
self.assertEqual(self.kp._keystring, '')
def test_count_42_invalid(self):
"""Test with count=42 and invalid command."""
# Invalid call with ccx gets ignored
self.kp.handle(_fake_keyevent(Qt.Key_4, text='4'))
self.kp.handle(_fake_keyevent(Qt.Key_2, text='2'))
self.kp.handle(_fake_keyevent(Qt.Key_B, text='c'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='x'))
self.kp.handle(fake_keyevent(Qt.Key_4, text='4'))
self.kp.handle(fake_keyevent(Qt.Key_2, text='2'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='c'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='x'))
self.assertFalse(self.kp.execute.called)
self.assertEqual(self.kp._keystring, '')
# Valid call with ccc gets the correct count
self.kp.handle(_fake_keyevent(Qt.Key_4, text='2'))
self.kp.handle(_fake_keyevent(Qt.Key_2, text='3'))
self.kp.handle(_fake_keyevent(Qt.Key_B, text='c'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(_fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(fake_keyevent(Qt.Key_4, text='2'))
self.kp.handle(fake_keyevent(Qt.Key_2, text='3'))
self.kp.handle(fake_keyevent(Qt.Key_B, text='c'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(fake_keyevent(Qt.Key_A, text='c'))
self.kp.execute.assert_called_once_with('ccc', self.kp.Type.chain, 23)
self.assertEqual(self.kp._keystring, '')