reader tests

This commit is contained in:
sezanzeb 2020-11-22 15:14:43 +01:00
parent d785e5bb00
commit 76cc37b867
3 changed files with 91 additions and 4 deletions

View File

@ -28,7 +28,7 @@ from keymapper.logger import logger
from keymapper.getdevices import get_devices from keymapper.getdevices import get_devices
class KeycodeReader: class _KeycodeReader:
"""Keeps reading keycodes in the background for the UI to use. """Keeps reading keycodes in the background for the UI to use.
When a button was pressed, the newest keycode can be obtained from this When a button was pressed, the newest keycode can be obtained from this
@ -73,6 +73,12 @@ class KeycodeReader:
event = virtual_device.read_one() event = virtual_device.read_one()
if event is None: if event is None:
break break
logger.spam(
'got code:%s value:%s',
event.code + 8, event.value
)
if event.type == evdev.ecodes.EV_KEY and event.value == 1: if event.type == evdev.ecodes.EV_KEY and event.value == 1:
# value: 1 for down, 0 for up, 2 for hold. # value: 1 for down, 0 for up, 2 for hold.
# this happens to report key codes that are 8 lower # this happens to report key codes that are 8 lower
@ -81,4 +87,4 @@ class KeycodeReader:
return newest_keycode return newest_keycode
keycode_reader = KeycodeReader() keycode_reader = _KeycodeReader()

View File

@ -53,6 +53,7 @@ class TestInjector(unittest.TestCase):
self.injector.stop_injecting() self.injector.stop_injecting()
self.injector = None self.injector = None
evdev.InputDevice.grab = self.grab evdev.InputDevice.grab = self.grab
pending_events['device 2'] = []
def test_modify_capabilities(self): def test_modify_capabilities(self):
class FakeDevice: class FakeDevice:
@ -99,10 +100,9 @@ class TestInjector(unittest.TestCase):
self.assertEqual(before, is_numlock_on()) self.assertEqual(before, is_numlock_on())
def test_injector_constructor(self): def test_injector_constructor(self):
global pending_events
# this buys some time to test if the process is alive. 2 (20ms) would # this buys some time to test if the process is alive. 2 (20ms) would
# already be enough # already be enough
pending_events['device 2'] = [Event(1, 1, 1)] * 10 pending_events['device 2'] = [Event(1, 31, 1)] * 10
injector2 = None injector2 = None
try: try:

81
tests/testcases/reader.py Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
# This file is part of key-mapper.
#
# key-mapper is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# key-mapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
import unittest
import evdev
from keymapper.reader import keycode_reader
from test import Event, pending_events
CODE_1 = 100
CODE_2 = 101
CODE_3 = 102
class TestReader(unittest.TestCase):
def tearDown(self):
keycode_reader.clear()
def test_reading(self):
keycode_reader.start_reading('device 1')
pending_events['device 1'] = [
Event(evdev.events.EV_KEY, CODE_1, 1),
Event(evdev.events.EV_KEY, CODE_2, 1),
Event(evdev.events.EV_KEY, CODE_3, 1)
]
self.assertEqual(keycode_reader.read(), CODE_3 + 8)
self.assertIsNone(keycode_reader.read())
def test_specific_device(self):
keycode_reader.start_reading('device 2')
pending_events['device 1'] = [
Event(evdev.events.EV_KEY, CODE_1, 1),
Event(evdev.events.EV_KEY, CODE_2, 1),
Event(evdev.events.EV_KEY, CODE_3, 1)
]
self.assertIsNone(keycode_reader.read())
def test_clear(self):
keycode_reader.start_reading('device 1')
pending_events['device 1'] = [
Event(evdev.events.EV_KEY, CODE_1, 1),
Event(evdev.events.EV_KEY, CODE_2, 1),
Event(evdev.events.EV_KEY, CODE_3, 1)
]
keycode_reader.clear()
self.assertIsNone(keycode_reader.read())
def test_switch_device(self):
keycode_reader.start_reading('device 2')
pending_events['device 2'] = [Event(evdev.events.EV_KEY, CODE_1, 1)]
keycode_reader.start_reading('device 1')
pending_events['device 1'] = [Event(evdev.events.EV_KEY, CODE_3, 1)]
self.assertEqual(keycode_reader.read(), CODE_3 + 8)
self.assertIsNone(keycode_reader.read())
if __name__ == "__main__":
unittest.main()