forked from openstenoproject/plover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_keyboard.py
99 lines (84 loc) · 2.69 KB
/
test_keyboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pytest
from plover import system
from plover.machine.keyboard import Keyboard
from plover.machine.keymap import Keymap
from plover.oslayer.keyboardcontrol import KeyboardCapture
from .py37compat import mock
def send_input(capture, key_events):
for evt in key_events.strip().split():
if evt.startswith('+'):
capture.key_down(evt[1:])
elif evt.startswith('-'):
capture.key_up(evt[1:])
else:
capture.key_down(evt)
capture.key_up(evt)
@pytest.fixture
def capture():
capture = mock.MagicMock(spec=KeyboardCapture)
with mock.patch('plover.machine.keyboard.KeyboardCapture', new=lambda: capture):
yield capture
@pytest.fixture(params=[False])
def machine(request, capture):
machine = Keyboard({'arpeggiate': request.param})
keymap = Keymap(Keyboard.KEYS_LAYOUT.split(),
system.KEYS + Keyboard.ACTIONS)
keymap.set_mappings(system.KEYMAPS['Keyboard'])
machine.set_keymap(keymap)
return machine
def arpeggiate(func):
return pytest.mark.parametrize('machine', [True], indirect=True)(func)
@pytest.fixture
def strokes(machine):
strokes = []
machine.add_stroke_callback(strokes.append)
return strokes
def test_lifecycle(capture, machine, strokes):
# Start machine.
machine.start_capture()
assert capture.mock_calls == [
mock.call.start(),
mock.call.suppress(()),
]
capture.reset_mock()
machine.set_suppression(True)
suppressed_keys = dict(machine.keymap.get_bindings())
del suppressed_keys['space']
assert strokes == []
assert capture.mock_calls == [
mock.call.suppress(suppressed_keys.keys()),
]
# Trigger some strokes.
capture.reset_mock()
send_input(capture, '+a +h -a -h space w')
assert strokes == [
{'S-', '*'},
{'T-'},
]
assert capture.mock_calls == []
# Stop machine.
del strokes[:]
machine.stop_capture()
assert strokes == []
assert capture.mock_calls == [
mock.call.suppress(()),
mock.call.cancel(),
]
def test_unfinished_stroke_1(capture, machine, strokes):
machine.start_capture()
send_input(capture, '+a +q -a')
assert strokes == []
def test_unfinished_stroke_2(capture, machine, strokes):
machine.start_capture()
send_input(capture, '+a +r -a +a -r')
assert strokes == []
@arpeggiate
def test_arpeggiate_1(capture, machine, strokes):
machine.start_capture()
send_input(capture, 'a h space w')
assert strokes == [{'S-', '*'}]
@arpeggiate
def test_arpeggiate_2(capture, machine, strokes):
machine.start_capture()
send_input(capture, 'a +h +space -space -h w')
assert strokes == [{'S-', '*'}]