testing a short macro in injector tests

This commit is contained in:
sezanzeb 2020-11-29 00:13:13 +01:00
parent c63796bcb7
commit 68f2de3978
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.write(evdev.ecodes.EV_KEY, keycode - KEYCODE_OFFSET, value)
device.syn() 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): async def _injection_loop(self, device, keymapper_device):
"""Inject keycodes for one of the virtual devices. """Inject keycodes for one of the virtual devices.
@ -240,13 +246,7 @@ class KeycodeInjector:
# probably a macro # probably a macro
macros[keycode] = parse( macros[keycode] = parse(
output, output,
lambda char, value: ( lambda *args: self._macro_write(*args, keymapper_device)
self._write(
keymapper_device,
system_mapping.get_keycode(char),
value
)
)
) )
logger.debug( logger.debug(

View File

@ -121,10 +121,6 @@ class _Macro:
def keycode(self, character): def keycode(self, character):
"""Write the character.""" """Write the character."""
self.tasks.append((KEYSTROKE, lambda: self.handler(character, 1))) 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.add_keycode_pause()
self.tasks.append((KEYSTROKE, lambda: self.handler(character, 0))) self.tasks.append((KEYSTROKE, lambda: self.handler(character, 0)))
self.add_keycode_pause() self.add_keycode_pause()

View File

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

View File

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