2020-11-22 14:14:43 +00:00
|
|
|
#!/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
|
|
|
|
|
2020-12-02 19:08:55 +00:00
|
|
|
from evdev.events import EV_KEY
|
2020-11-30 17:59:34 +00:00
|
|
|
import time
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2020-11-25 22:36:03 +00:00
|
|
|
from keymapper.dev.reader import keycode_reader
|
2020-12-02 20:24:35 +00:00
|
|
|
from keymapper.state import KEYCODE_OFFSET
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
from tests.test import Event, pending_events, EVENT_READ_TIMEOUT
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
CODE_1 = 100
|
|
|
|
CODE_2 = 101
|
|
|
|
CODE_3 = 102
|
|
|
|
|
|
|
|
|
|
|
|
class TestReader(unittest.TestCase):
|
2020-11-30 17:59:34 +00:00
|
|
|
def setUp(self):
|
|
|
|
# verify that tearDown properly cleared the reader
|
2020-12-02 20:20:18 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (None, None))
|
2020-11-30 17:59:34 +00:00
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
def tearDown(self):
|
2020-11-30 17:59:34 +00:00
|
|
|
keycode_reader.stop_reading()
|
|
|
|
keys = list(pending_events.keys())
|
|
|
|
for key in keys:
|
|
|
|
del pending_events[key]
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
def test_reading(self):
|
|
|
|
pending_events['device 1'] = [
|
2020-12-02 19:08:55 +00:00
|
|
|
Event(EV_KEY, CODE_1, 1),
|
|
|
|
Event(EV_KEY, CODE_2, 1),
|
|
|
|
Event(EV_KEY, CODE_3, 1)
|
2020-11-22 14:14:43 +00:00
|
|
|
]
|
2020-11-30 17:59:34 +00:00
|
|
|
keycode_reader.start_reading('device 1')
|
2020-12-01 22:53:32 +00:00
|
|
|
|
|
|
|
# sending anything arbitrary does not stop the pipe
|
2020-12-02 19:08:55 +00:00
|
|
|
keycode_reader._pipe[0].send((EV_KEY, 1234))
|
2020-12-01 22:53:32 +00:00
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-12-02 20:24:35 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (EV_KEY, CODE_3 + KEYCODE_OFFSET))
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (None, None))
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
def test_wrong_device(self):
|
2020-11-22 14:14:43 +00:00
|
|
|
pending_events['device 1'] = [
|
2020-12-02 19:08:55 +00:00
|
|
|
Event(EV_KEY, CODE_1, 1),
|
|
|
|
Event(EV_KEY, CODE_2, 1),
|
|
|
|
Event(EV_KEY, CODE_3, 1)
|
2020-11-22 14:14:43 +00:00
|
|
|
]
|
2020-11-30 17:59:34 +00:00
|
|
|
keycode_reader.start_reading('device 2')
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (None, None))
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2020-11-22 16:30:06 +00:00
|
|
|
def test_keymapper_devices(self):
|
2020-12-01 22:53:32 +00:00
|
|
|
# Don't read from keymapper devices, their keycodes are not
|
|
|
|
# representative for the original key. As long as this is not
|
|
|
|
# intentionally programmed it won't even do that. But it was at some
|
|
|
|
# point.
|
2020-11-22 16:30:06 +00:00
|
|
|
pending_events['key-mapper device 2'] = [
|
2020-12-02 19:08:55 +00:00
|
|
|
Event(EV_KEY, CODE_1, 1),
|
|
|
|
Event(EV_KEY, CODE_2, 1),
|
|
|
|
Event(EV_KEY, CODE_3, 1)
|
2020-11-22 16:30:06 +00:00
|
|
|
]
|
2020-11-30 17:59:34 +00:00
|
|
|
keycode_reader.start_reading('device 2')
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (None, None))
|
2020-11-22 16:30:06 +00:00
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
def test_clear(self):
|
|
|
|
pending_events['device 1'] = [
|
2020-12-02 19:08:55 +00:00
|
|
|
Event(EV_KEY, CODE_1, 1),
|
|
|
|
Event(EV_KEY, CODE_2, 1),
|
|
|
|
Event(EV_KEY, CODE_3, 1)
|
2020-11-22 14:14:43 +00:00
|
|
|
]
|
2020-11-30 17:59:34 +00:00
|
|
|
keycode_reader.start_reading('device 1')
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-11-22 14:14:43 +00:00
|
|
|
keycode_reader.clear()
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (None, None))
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
def test_switch_device(self):
|
2020-12-02 19:08:55 +00:00
|
|
|
pending_events['device 2'] = [Event(EV_KEY, CODE_1, 1)]
|
|
|
|
pending_events['device 1'] = [Event(EV_KEY, CODE_3, 1)]
|
2020-11-30 17:59:34 +00:00
|
|
|
|
|
|
|
keycode_reader.start_reading('device 2')
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
keycode_reader.start_reading('device 1')
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2020-12-02 20:24:35 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (EV_KEY, CODE_3 + KEYCODE_OFFSET))
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(keycode_reader.read(), (None, None))
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|