input-remapper/tests/testcases/test_daemon.py

279 lines
8.7 KiB
Python
Raw Normal View History

2020-11-20 20:38:59 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
2021-01-02 23:08:33 +00:00
# Copyright (C) 2021 sezanzeb <proxima@hip70890b.de>
2020-11-20 20:38:59 +00:00
#
# 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/>.
2020-11-29 00:44:14 +00:00
import os
import multiprocessing
2020-11-20 20:38:59 +00:00
import unittest
2020-11-29 00:44:14 +00:00
import time
2020-12-26 18:36:47 +00:00
import subprocess
2020-11-20 20:38:59 +00:00
import evdev
from evdev.ecodes import EV_KEY, EV_ABS
2020-11-29 00:44:14 +00:00
from gi.repository import Gtk
2020-12-26 18:36:47 +00:00
from pydbus import SystemBus
2020-11-20 20:38:59 +00:00
2020-12-04 13:38:41 +00:00
from keymapper.state import custom_mapping, system_mapping
2020-11-20 20:38:59 +00:00
from keymapper.config import config
2020-12-26 18:36:47 +00:00
from keymapper.getdevices import get_devices
from keymapper.paths import get_preset_path
2020-12-31 20:47:56 +00:00
from keymapper.key import Key
2021-01-07 16:15:12 +00:00
from keymapper.dev.injector import STARTING, RUNNING, STOPPED, UNKNOWN
from keymapper.daemon import Daemon, get_dbus_interface, BUS_NAME
2020-11-20 20:38:59 +00:00
2021-01-05 18:33:47 +00:00
from tests.test import cleanup, uinput_write_history_pipe, new_event, \
pending_events, is_service_running, fixtures, tmp
2020-11-20 20:38:59 +00:00
2020-11-29 00:44:14 +00:00
def gtk_iteration():
"""Iterate while events are pending."""
while Gtk.events_pending():
Gtk.main_iteration()
class TestDBusDaemon(unittest.TestCase):
def setUp(self):
self.process = multiprocessing.Process(
2020-11-29 00:44:14 +00:00
target=os.system,
args=('key-mapper-service -d',)
2020-11-29 00:44:14 +00:00
)
self.process.start()
2020-11-29 00:44:14 +00:00
time.sleep(0.5)
self.interface = get_dbus_interface()
def tearDown(self):
self.interface.stop()
os.system('pkill -f key-mapper-service')
2020-11-29 00:44:14 +00:00
for _ in range(10):
time.sleep(0.1)
if not is_service_running():
break
self.assertFalse(is_service_running())
2020-11-29 00:44:14 +00:00
def test_can_connect(self):
# it's a remote dbus object
self.assertEqual(self.interface._bus_name, BUS_NAME)
self.assertFalse(isinstance(self.interface, Daemon))
self.assertEqual(self.interface.hello('foo'), 'foo')
2020-11-29 00:44:14 +00:00
2020-12-26 18:36:47 +00:00
check_output = subprocess.check_output
dbus_get = type(SystemBus()).get
2020-11-20 20:38:59 +00:00
class TestDaemon(unittest.TestCase):
new_fixture = '/dev/input/event9876'
def setUp(self):
self.grab = evdev.InputDevice.grab
self.daemon = None
2020-11-20 20:38:59 +00:00
def tearDown(self):
# avoid race conditions with other tests, daemon may run processes
if self.daemon is not None:
self.daemon.stop()
self.daemon = None
evdev.InputDevice.grab = self.grab
2020-12-26 18:36:47 +00:00
subprocess.check_output = check_output
type(SystemBus()).get = dbus_get
cleanup()
2020-11-20 20:38:59 +00:00
2020-12-26 18:36:47 +00:00
def test_get_dbus_interface(self):
# no daemon runs, should return an instance of the object instead
self.assertFalse(is_service_running())
self.assertIsInstance(get_dbus_interface(), Daemon)
self.assertIsNone(get_dbus_interface(False))
subprocess.check_output = lambda *args: None
self.assertTrue(is_service_running())
# now it actually tries to use the dbus, but it fails
# because none exists, so it returns an instance again
self.assertIsInstance(get_dbus_interface(), Daemon)
self.assertIsNone(get_dbus_interface(False))
class FakeConnection:
pass
type(SystemBus()).get = lambda *args: FakeConnection()
self.assertIsInstance(get_dbus_interface(), FakeConnection)
self.assertIsInstance(get_dbus_interface(False), FakeConnection)
2020-11-20 20:38:59 +00:00
def test_daemon(self):
ev_1 = (EV_KEY, 9)
ev_2 = (EV_ABS, 12)
2020-12-02 17:07:46 +00:00
keycode_to_1 = 100
keycode_to_2 = 101
device = 'device 2'
2020-12-31 20:47:56 +00:00
custom_mapping.change(Key(*ev_1, 1), 'a')
custom_mapping.change(Key(*ev_2, -1), 'b')
2020-11-20 20:38:59 +00:00
2020-12-04 13:38:41 +00:00
system_mapping.clear()
system_mapping._set('a', keycode_to_1)
system_mapping._set('b', keycode_to_2)
2020-11-20 20:38:59 +00:00
2020-12-02 17:07:46 +00:00
preset = 'foo'
custom_mapping.save(get_preset_path(device, preset))
config.set_autoload_preset(device, preset)
2020-11-20 20:38:59 +00:00
"""injection 1"""
# should forward the event unchanged
pending_events[device] = [
2021-01-05 18:33:47 +00:00
new_event(EV_KEY, 13, 1)
2020-11-20 20:38:59 +00:00
]
self.daemon = Daemon()
preset_path = get_preset_path(device, preset)
2020-12-02 17:07:46 +00:00
2021-01-01 21:20:33 +00:00
self.assertFalse(uinput_write_history_pipe[0].poll())
self.daemon.start_injecting(device, preset_path)
2021-01-07 16:15:12 +00:00
self.assertEqual(self.daemon.get_state(device), STARTING)
self.assertEqual(self.daemon.get_state('device 1'), UNKNOWN)
2020-12-02 17:07:46 +00:00
event = uinput_write_history_pipe[0].recv()
2021-01-07 16:15:12 +00:00
self.assertEqual(self.daemon.get_state(device), RUNNING)
self.assertEqual(event.type, EV_KEY)
self.assertEqual(event.code, 13)
self.assertEqual(event.value, 1)
2020-12-02 17:07:46 +00:00
self.daemon.stop_injecting(device)
2021-01-07 16:15:12 +00:00
self.assertEqual(self.daemon.get_state(device), STOPPED)
2020-12-02 17:07:46 +00:00
2021-01-01 21:20:33 +00:00
time.sleep(0.2)
try:
self.assertFalse(uinput_write_history_pipe[0].poll())
except AssertionError:
2021-01-05 18:33:47 +00:00
print('Unexpected', uinput_write_history_pipe[0].recv())
# possibly a duplicate write!
2021-01-01 21:20:33 +00:00
raise
"""injection 2"""
# -1234 will be normalized to -1 by the injector
pending_events[device] = [
2021-01-05 18:33:47 +00:00
new_event(*ev_2, -1234)
2020-12-02 17:07:46 +00:00
]
path = get_preset_path(device, preset)
self.daemon.start_injecting(device, path)
2020-12-02 17:07:46 +00:00
# the written key is a key-down event, not the original
2021-01-05 18:33:47 +00:00
# event value of -1234
2020-12-02 17:07:46 +00:00
event = uinput_write_history_pipe[0].recv()
self.assertEqual(event.type, EV_KEY)
self.assertEqual(event.code, keycode_to_2)
2020-12-02 17:07:46 +00:00
self.assertEqual(event.value, 1)
2020-11-20 20:38:59 +00:00
def test_refresh_devices_on_start(self):
ev = (EV_KEY, 9)
keycode_to = 100
device = '9876 name'
# this test only makes sense if this device is unknown yet
self.assertIsNone(get_devices().get(device))
2020-12-31 20:47:56 +00:00
custom_mapping.change(Key(*ev, 1), 'a')
system_mapping.clear()
system_mapping._set('a', keycode_to)
preset = 'foo'
custom_mapping.save(get_preset_path(device, preset))
config.set_autoload_preset(device, preset)
pending_events[device] = [
2021-01-05 18:33:47 +00:00
new_event(*ev, 1)
]
self.daemon = Daemon()
preset_path = get_preset_path(device, preset)
# make sure the devices are populated
get_devices()
fixtures[self.new_fixture] = {
'capabilities': {evdev.ecodes.EV_KEY: [ev[1]]},
'phys': '9876 phys',
2021-01-08 16:21:51 +00:00
'info': evdev.device.DeviceInfo(4, 5, 6, 7),
'name': device
}
self.daemon.start_injecting(device, preset_path)
# test if the injector called refresh_devices successfully
self.assertIsNotNone(get_devices().get(device))
event = uinput_write_history_pipe[0].recv()
2021-01-07 16:15:12 +00:00
self.assertEqual(event.t, (EV_KEY, keycode_to, 1))
self.daemon.stop_injecting(device)
2021-01-07 16:15:12 +00:00
self.assertEqual(self.daemon.get_state(device), STOPPED)
def test_xmodmap_file(self):
from_keycode = evdev.ecodes.KEY_A
to_name = 'qux'
to_keycode = 100
event = (EV_KEY, from_keycode, 1)
device = 'device 2'
preset = 'foo'
path = get_preset_path(device, preset)
2020-12-31 20:47:56 +00:00
custom_mapping.change(Key(event), to_name)
custom_mapping.save(path)
system_mapping.clear()
config.set_autoload_preset(device, preset)
pending_events[device] = [
2021-01-05 18:33:47 +00:00
new_event(*event)
]
config_dir = os.path.join(tmp, 'foo')
os.makedirs(config_dir, exist_ok=True)
config_path = os.path.join(config_dir, 'config.json')
with open(config_path, 'w') as file:
file.write('{"bar":1234}')
xmodmap_path = os.path.join(config_dir, 'xmodmap.json')
with open(xmodmap_path, 'w') as file:
file.write(f'{{"{to_name}":{to_keycode}}}')
self.daemon = Daemon()
self.daemon.start_injecting(device, path, config_dir)
event = uinput_write_history_pipe[0].recv()
self.assertEqual(event.type, EV_KEY)
self.assertEqual(event.code, to_keycode)
self.assertEqual(event.value, 1)
# since the daemon is running in the same process, the config
# that the test knows will be overwritten
self.assertEqual(config.get('bar'), 1234)
2020-11-20 20:38:59 +00:00
if __name__ == "__main__":
unittest.main()