testing a short macro in injector tests

This commit is contained in:
sezanzeb 2020-11-29 00:13:13 +01:00 committed by sezanzeb
parent cc04f991a9
commit d3a1b16d6b
4 changed files with 55 additions and 24 deletions

View File

@ -222,6 +222,12 @@ class KeycodeInjector:
device.write(evdev.ecodes.EV_KEY, keycode - KEYCODE_OFFSET, value)
device.syn()
def _macro_write(self, character, value, keymapper_device):
"""Handler for macros."""
keycode = system_mapping.get_keycode(character)
logger.spam('macro writes code:%s char:%s', keycode, character)
self._write(keymapper_device, keycode, value)
async def _injection_loop(self, device, keymapper_device):
"""Inject keycodes for one of the virtual devices.
@ -240,13 +246,7 @@ class KeycodeInjector:
# probably a macro
macros[keycode] = parse(
output,
lambda char, value: (
self._write(
keymapper_device,
system_mapping.get_keycode(char),
value
)
)
lambda *args: self._macro_write(*args, keymapper_device)
)
logger.debug(

View File

@ -121,10 +121,6 @@ class _Macro:
def keycode(self, character):
"""Write the character."""
self.tasks.append((KEYSTROKE, lambda: self.handler(character, 1)))
self.tasks.append((DEBUG, lambda: logger.spam(
'macro writes character %s',
character
)))
self.add_keycode_pause()
self.tasks.append((KEYSTROKE, lambda: self.handler(character, 0)))
self.add_keycode_pause()

View File

@ -24,7 +24,7 @@ import unittest
import evdev
from keymapper.dev.injector import is_numlock_on, toggle_numlock,\
ensure_numlock, KeycodeInjector
ensure_numlock, KeycodeInjector, KEYCODE_OFFSET
from keymapper.state import custom_mapping, system_mapping
from keymapper.mapping import Mapping
@ -127,16 +127,26 @@ class TestInjector(unittest.TestCase):
self.assertEqual(before, is_numlock_on())
def test_injector(self):
custom_mapping.change(8, 'k(q).k(w)')
custom_mapping.change(9, 'a')
# one mapping that is unknown in the system_mapping on purpose
custom_mapping.change(10, 'b')
system_mapping.empty()
system_mapping.change(100, 'a')
code_a = 100
code_q = 101
code_w = 102
system_mapping.change(code_a, 'a')
system_mapping.change(code_q, 'q')
system_mapping.change(code_w, 'w')
# the second arg of those event objects is 8 lower than the
# keycode used in X and in the mappings
pending_events['device 2'] = [
# should execute a macro
Event(evdev.events.EV_KEY, 0, 1),
Event(evdev.events.EV_KEY, 0, 0),
# normal keystroke
Event(evdev.events.EV_KEY, 1, 1),
Event(evdev.events.EV_KEY, 1, 0),
# ignored because unknown to the system
@ -150,19 +160,41 @@ class TestInjector(unittest.TestCase):
# don't start the process for coverage testing purposes
self.injector._start_injecting()
self.assertEqual(len(uinput_write_history), 3)
self.assertEqual(len(uinput_write_history), 7)
self.assertEqual(uinput_write_history[0].type, evdev.events.EV_KEY)
self.assertEqual(uinput_write_history[0].code, 92)
self.assertEqual(uinput_write_history[0].value, 1)
# convert the write history to some easier to manage list
history = [
(event.type, event.code, event.value)
for event in uinput_write_history
]
self.assertEqual(uinput_write_history[1].type, evdev.events.EV_KEY)
self.assertEqual(uinput_write_history[1].code, 92)
self.assertEqual(uinput_write_history[1].value, 0)
# since the macro takes a little bit of time to execute, its
# keystrokes are all over the place.
# just check if they are there and if so, remove them from the list.
ev_key = evdev.events.EV_KEY
self.assertIn((ev_key, code_q - KEYCODE_OFFSET, 1), history)
self.assertIn((ev_key, code_q - KEYCODE_OFFSET, 0), history)
self.assertIn((ev_key, code_w - KEYCODE_OFFSET, 1), history)
self.assertIn((ev_key, code_w - KEYCODE_OFFSET, 0), history)
index_q_1 = history.index((ev_key, code_q - KEYCODE_OFFSET, 1))
index_q_0 = history.index((ev_key, code_q - KEYCODE_OFFSET, 0))
index_w_1 = history.index((ev_key, code_w - KEYCODE_OFFSET, 1))
index_w_0 = history.index((ev_key, code_w - KEYCODE_OFFSET, 0))
self.assertGreater(index_q_0, index_q_1)
self.assertGreater(index_w_1, index_q_0)
self.assertGreater(index_w_0, index_w_1)
del history[index_q_1]
index_q_0 = history.index((ev_key, code_q - KEYCODE_OFFSET, 0))
del history[index_q_0]
index_w_1 = history.index((ev_key, code_w - KEYCODE_OFFSET, 1))
del history[index_w_1]
index_w_0 = history.index((ev_key, code_w - KEYCODE_OFFSET, 0))
del history[index_w_0]
self.assertEqual(uinput_write_history[2].type, 3124)
self.assertEqual(uinput_write_history[2].code, 3564)
self.assertEqual(uinput_write_history[2].value, 6542)
# the rest should be in order.
self.assertEqual(history[0], (ev_key, code_a - KEYCODE_OFFSET, 1))
self.assertEqual(history[1], (ev_key, code_a - KEYCODE_OFFSET, 0))
self.assertEqual(history[2], (3124, 3564, 6542))
if __name__ == "__main__":

View File

@ -30,12 +30,15 @@ from keymapper.config import config
class TestMacros(unittest.TestCase):
def setUp(self):
self.result = []
self.handler = lambda char, value: self.result.append((char, value))
self.loop = asyncio.get_event_loop()
def tearDown(self):
self.result = []
def handler(self, char, value):
"""Where macros should write characters to."""
self.result.append((char, value))
def test_0(self):
self.loop.run_until_complete(parse('k(1)', self.handler).run())
self.assertListEqual(self.result, [(1, 1), (1, 0)])