input-remapper/tests/testcases/test_daemon.py

509 lines
18 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-02-22 18:48:20 +00:00
# Copyright (C) 2021 sezanzeb <proxima@sezanzeb.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
2021-02-07 14:00:36 +00:00
import json
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
2021-09-29 18:50:32 +00:00
from keymapper.system_mapping import system_mapping
from keymapper.gui.custom_mapping import custom_mapping
2020-11-20 20:38:59 +00:00
from keymapper.config import config
from keymapper.groups import groups
from keymapper.paths import get_config_path, mkdir, get_preset_path
2020-12-31 20:47:56 +00:00
from keymapper.key import Key
2021-02-07 14:00:36 +00:00
from keymapper.mapping import Mapping
2021-02-13 19:34:33 +00:00
from keymapper.injection.injector import STARTING, RUNNING, STOPPED, UNKNOWN
from keymapper.daemon import Daemon, BUS_NAME
2020-11-20 20:38:59 +00:00
2021-09-26 10:44:56 +00:00
from tests.test import (
cleanup,
uinput_write_history_pipe,
new_event,
push_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(
2021-09-26 10:44:56 +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)
2021-03-21 18:15:20 +00:00
# should not use pkexec, but rather connect to the previously
# spawned process
self.interface = Daemon.connect()
def tearDown(self):
2021-02-07 14:00:36 +00:00
self.interface.stop_all()
2021-09-26 10:44:56 +00:00
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))
2021-09-26 10:44:56 +00:00
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
2021-03-21 18:15:20 +00:00
os_system = os.system
2020-12-26 18:36:47 +00:00
dbus_get = type(SystemBus()).get
2020-11-20 20:38:59 +00:00
class TestDaemon(unittest.TestCase):
2021-09-26 10:44:56 +00:00
new_fixture_path = "/dev/input/event9876"
def setUp(self):
self.grab = evdev.InputDevice.grab
self.daemon = None
2021-03-21 18:15:20 +00:00
mkdir(get_config_path())
config.save_config()
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:
2021-02-07 14:00:36 +00:00
self.daemon.stop_all()
2020-11-20 20:38:59 +00:00
self.daemon = None
evdev.InputDevice.grab = self.grab
2020-12-26 18:36:47 +00:00
subprocess.check_output = check_output
2021-03-21 18:15:20 +00:00
os.system = os_system
2020-12-26 18:36:47 +00:00
type(SystemBus()).get = dbus_get
cleanup()
2020-11-20 20:38:59 +00:00
2021-03-21 18:15:20 +00:00
def test_connect(self):
os_system_history = []
os.system = os_system_history.append
2020-12-26 18:36:47 +00:00
2021-03-21 18:15:20 +00:00
self.assertFalse(is_service_running())
# no daemon runs, should try to run it via pkexec instead.
# It fails due to the patch and therefore exits the process
self.assertRaises(SystemExit, Daemon.connect)
self.assertEqual(len(os_system_history), 1)
self.assertIsNone(Daemon.connect(False))
2020-12-26 18:36:47 +00:00
class FakeConnection:
pass
type(SystemBus()).get = lambda *args: FakeConnection()
2021-03-21 18:15:20 +00:00
self.assertIsInstance(Daemon.connect(), FakeConnection)
self.assertIsInstance(Daemon.connect(False), FakeConnection)
2020-12-26 18:36:47 +00:00
2020-11-20 20:38:59 +00:00
def test_daemon(self):
2021-02-07 14:00:36 +00:00
# remove the existing system mapping to force our own into it
2021-09-26 10:44:56 +00:00
if os.path.exists(get_config_path("xmodmap.json")):
os.remove(get_config_path("xmodmap.json"))
2021-02-07 14:00:36 +00:00
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
2021-09-26 10:44:56 +00:00
group = groups.find(name="Bar Device")
# unrelated group that shouldn't be affected at all
2021-09-26 10:44:56 +00:00
group2 = groups.find(name="gamepad")
2021-09-26 10:44: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()
2021-02-07 14:00:36 +00:00
# since this is in the same memory as the daemon, there is no need
# to save it to disk
2021-09-26 10:44:56 +00:00
system_mapping._set("a", keycode_to_1)
system_mapping._set("b", keycode_to_2)
2020-11-20 20:38:59 +00:00
2021-09-26 10:44:56 +00:00
preset = "foo"
2020-12-02 17:07:46 +00:00
custom_mapping.save(group.get_preset_path(preset))
config.set_autoload_preset(group.key, preset)
2020-11-20 20:38:59 +00:00
"""injection 1"""
# should forward the event unchanged
2021-09-26 10:44:56 +00:00
push_events(group.key, [new_event(EV_KEY, 13, 1)])
2020-11-20 20:38:59 +00:00
self.daemon = Daemon()
2021-02-07 14:00:36 +00:00
self.daemon.set_config_dir(get_config_path())
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(group.key, preset)
self.assertEqual(self.daemon.get_state(group.key), STARTING)
self.assertEqual(self.daemon.get_state(group2.key), UNKNOWN)
2020-12-02 17:07:46 +00:00
event = uinput_write_history_pipe[0].recv()
self.assertEqual(self.daemon.get_state(group.key), 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(group.key)
self.assertEqual(self.daemon.get_state(group.key), STOPPED)
2020-12-02 17:07:46 +00:00
2021-02-07 14:00:36 +00:00
time.sleep(0.1)
2021-01-01 21:20:33 +00:00
try:
self.assertFalse(uinput_write_history_pipe[0].poll())
except AssertionError:
2021-09-26 10:44:56 +00:00
print("Unexpected", uinput_write_history_pipe[0].recv())
2021-01-05 18:33:47 +00:00
# possibly a duplicate write!
2021-01-01 21:20:33 +00:00
raise
"""injection 2"""
2021-09-29 18:17:45 +00:00
# -1234 will be classified as -1 by the injector
2021-09-26 10:44:56 +00:00
push_events(group.key, [new_event(*ev_2, -1234)])
2020-12-02 17:07:46 +00:00
self.daemon.start_injecting(group.key, preset)
2021-02-07 14:00:36 +00:00
time.sleep(0.1)
self.assertTrue(uinput_write_history_pipe[0].poll())
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()
2021-02-07 14:00:36 +00:00
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_on_start(self):
2021-09-26 10:44:56 +00:00
if os.path.exists(get_config_path("xmodmap.json")):
os.remove(get_config_path("xmodmap.json"))
2021-02-07 14:00:36 +00:00
ev = (EV_KEY, 9)
keycode_to = 100
2021-09-26 10:44:56 +00:00
group_name = "9876 name"
# expected key of the group
group_key = group_name
group = groups.find(name=group_name)
# this test only makes sense if this device is unknown yet
self.assertIsNone(group)
2021-09-26 10:44:56 +00:00
custom_mapping.change(Key(*ev, 1), "a")
system_mapping.clear()
2021-09-26 10:44:56 +00:00
system_mapping._set("a", keycode_to)
2021-02-07 14:00:36 +00:00
# make the daemon load the file instead
2021-09-26 10:44:56 +00:00
with open(get_config_path("xmodmap.json"), "w") as file:
2021-02-07 14:00:36 +00:00
json.dump(system_mapping._mapping, file, indent=4)
system_mapping.clear()
2021-09-26 10:44:56 +00:00
preset = "foo"
custom_mapping.save(get_preset_path(group_name, preset))
config.set_autoload_preset(group_key, preset)
2021-09-26 10:44:56 +00:00
push_events(group_key, [new_event(*ev, 1)])
self.daemon = Daemon()
# make sure the devices are populated
groups.refresh()
# the daemon is supposed to find this device by calling refresh
fixtures[self.new_fixture_path] = {
2021-09-26 10:44:56 +00:00
"capabilities": {evdev.ecodes.EV_KEY: [ev[1]]},
"phys": "9876 phys",
"info": evdev.device.DeviceInfo(4, 5, 6, 7),
"name": group_name,
}
2021-02-07 14:00:36 +00:00
self.daemon.set_config_dir(get_config_path())
self.daemon.start_injecting(group_key, preset)
# test if the injector called groups.refresh successfully
group = groups.find(key=group_key)
self.assertEqual(group.name, group_name)
self.assertEqual(group.key, group_key)
2021-02-07 14:00:36 +00:00
time.sleep(0.1)
self.assertTrue(uinput_write_history_pipe[0].poll())
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(group_key)
self.assertEqual(self.daemon.get_state(group_key), STOPPED)
def test_refresh_for_unknown_key(self):
2021-09-26 10:44:56 +00:00
device = "9876 name"
2021-02-12 19:29:26 +00:00
# this test only makes sense if this device is unknown yet
self.assertIsNone(groups.find(name=device))
2021-02-12 19:29:26 +00:00
self.daemon = Daemon()
# make sure the devices are populated
groups.refresh()
2021-02-12 19:29:26 +00:00
self.daemon.refresh()
2021-02-12 19:29:26 +00:00
fixtures[self.new_fixture_path] = {
2021-09-26 10:44:56 +00:00
"capabilities": {evdev.ecodes.EV_KEY: [evdev.ecodes.KEY_A]},
"phys": "9876 phys",
"info": evdev.device.DeviceInfo(4, 5, 6, 7),
"name": device,
2021-02-12 19:29:26 +00:00
}
2021-09-26 10:44:56 +00:00
self.daemon._autoload("25v7j9q4vtj")
# this is unknown, so the daemon will scan the devices again
2021-02-12 19:29:26 +00:00
# test if the injector called groups.refresh successfully
self.assertIsNotNone(groups.find(name=device))
2021-02-12 19:29:26 +00:00
def test_xmodmap_file(self):
from_keycode = evdev.ecodes.KEY_A
2021-09-26 10:44:56 +00:00
to_name = "qux"
to_keycode = 100
event = (EV_KEY, from_keycode, 1)
2021-09-26 10:44:56 +00:00
name = "Bar Device"
preset = "foo"
group = groups.find(name=name)
2021-09-26 10:44:56 +00:00
config_dir = os.path.join(tmp, "foo")
2021-02-07 14:00:36 +00:00
2021-09-26 10:44:56 +00:00
path = os.path.join(config_dir, "presets", name, f"{preset}.json")
2020-12-31 20:47:56 +00:00
custom_mapping.change(Key(event), to_name)
custom_mapping.save(path)
system_mapping.clear()
2021-09-26 10:44:56 +00:00
push_events(group.key, [new_event(*event)])
2021-02-07 14:00:36 +00:00
# an existing config file is needed otherwise set_config_dir refuses
# to use the directory
2021-09-26 10:44:56 +00:00
config_path = os.path.join(config_dir, "config.json")
2021-02-07 14:00:36 +00:00
config.path = config_path
config.save_config()
2021-09-26 10:44:56 +00:00
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()
2021-02-07 14:00:36 +00:00
self.daemon.set_config_dir(config_dir)
self.daemon.start_injecting(group.key, preset)
2021-02-07 14:00:36 +00:00
time.sleep(0.1)
self.assertTrue(uinput_write_history_pipe[0].poll())
event = uinput_write_history_pipe[0].recv()
self.assertEqual(event.type, EV_KEY)
self.assertEqual(event.code, to_keycode)
self.assertEqual(event.value, 1)
2021-02-07 14:00:36 +00:00
def test_start_stop(self):
2021-09-26 10:44:56 +00:00
group = groups.find(key="Foo Device 2")
preset = "preset8"
2021-02-07 14:00:36 +00:00
daemon = Daemon()
self.daemon = daemon
mapping = Mapping()
2021-09-26 10:44:56 +00:00
mapping.change(Key(3, 2, 1), "a")
mapping.save(group.get_preset_path(preset))
2021-02-07 14:00:36 +00:00
# the daemon needs set_config_dir first before doing anything
daemon.start_injecting(group.key, preset)
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertNotIn(group.key, daemon.injectors)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-07 14:00:36 +00:00
# start
config.save_config()
2021-02-07 14:00:36 +00:00
daemon.set_config_dir(get_config_path())
daemon.start_injecting(group.key, preset)
2021-02-07 14:00:36 +00:00
# explicit start, not autoload, so the history stays empty
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-07 14:00:36 +00:00
# path got translated to the device name
self.assertIn(group.key, daemon.injectors)
2021-02-07 14:00:36 +00:00
# start again
previous_injector = daemon.injectors[group.key]
2021-02-07 14:00:36 +00:00
self.assertNotEqual(previous_injector.get_state(), STOPPED)
daemon.start_injecting(group.key, preset)
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
self.assertIn(group.key, daemon.injectors)
2021-02-07 14:00:36 +00:00
self.assertEqual(previous_injector.get_state(), STOPPED)
# a different injetor is now running
self.assertNotEqual(previous_injector, daemon.injectors[group.key])
self.assertNotEqual(daemon.injectors[group.key].get_state(), STOPPED)
2021-02-07 14:00:36 +00:00
# trying to inject a non existing preset keeps the previous inejction
# alive
injector = daemon.injectors[group.key]
2021-09-26 10:44:56 +00:00
daemon.start_injecting(group.key, "qux")
self.assertEqual(injector, daemon.injectors[group.key])
self.assertNotEqual(daemon.injectors[group.key].get_state(), STOPPED)
2021-02-07 14:00:36 +00:00
# trying to start injecting for an unknown device also just does
# nothing
2021-09-26 10:44:56 +00:00
daemon.start_injecting("quux", "qux")
self.assertNotEqual(daemon.injectors[group.key].get_state(), STOPPED)
2021-02-07 14:00:36 +00:00
# after all that stuff autoload_history is still unharmed
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-07 14:00:36 +00:00
# stop
daemon.stop_injecting(group.key)
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertEqual(daemon.injectors[group.key].get_state(), STOPPED)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-07 14:00:36 +00:00
def test_autoload(self):
2021-09-26 10:44:56 +00:00
preset = "preset7"
group = groups.find(key="Foo Device 2")
2021-02-07 14:00:36 +00:00
daemon = Daemon()
self.daemon = daemon
self.daemon.set_config_dir(get_config_path())
mapping = Mapping()
2021-09-26 10:44:56 +00:00
mapping.change(Key(3, 2, 1), "a")
mapping.save(group.get_preset_path(preset))
2021-02-07 14:00:36 +00:00
# no autoloading is configured yet
self.daemon._autoload(group.key)
self.assertNotIn(group.key, daemon.autoload_history._autoload_history)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-07 14:00:36 +00:00
config.set_autoload_preset(group.key, preset)
2021-02-07 14:00:36 +00:00
config.save_config()
self.daemon.set_config_dir(get_config_path())
2021-02-12 19:29:26 +00:00
len_before = len(self.daemon.autoload_history._autoload_history)
# now autoloading is configured, so it will autoload
self.daemon._autoload(group.key)
2021-02-12 19:29:26 +00:00
len_after = len(self.daemon.autoload_history._autoload_history)
2021-09-26 10:44:56 +00:00
self.assertEqual(
daemon.autoload_history._autoload_history[group.key][1], preset
)
self.assertFalse(daemon.autoload_history.may_autoload(group.key, preset))
injector = daemon.injectors[group.key]
2021-02-12 19:29:26 +00:00
self.assertEqual(len_before + 1, len_after)
2021-02-07 14:00:36 +00:00
# calling duplicate _autoload does nothing
self.daemon._autoload(group.key)
2021-09-26 10:44:56 +00:00
self.assertEqual(
daemon.autoload_history._autoload_history[group.key][1], preset
)
self.assertEqual(injector, daemon.injectors[group.key])
self.assertFalse(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-07 14:00:36 +00:00
# explicit start_injecting clears the autoload history
self.daemon.start_injecting(group.key, preset)
self.assertTrue(daemon.autoload_history.may_autoload(group.key, preset))
2021-02-12 19:29:26 +00:00
# calling autoload for (yet) unknown devices does nothing
len_before = len(self.daemon.autoload_history._autoload_history)
2021-09-26 10:44:56 +00:00
self.daemon._autoload("unknown-key-1234")
2021-02-12 19:29:26 +00:00
len_after = len(self.daemon.autoload_history._autoload_history)
self.assertEqual(len_before, len_after)
# autoloading key-mapper devices does nothing
len_before = len(self.daemon.autoload_history._autoload_history)
2021-09-26 10:44:56 +00:00
self.daemon.autoload_single("Bar Device")
2021-02-12 19:29:26 +00:00
len_after = len(self.daemon.autoload_history._autoload_history)
self.assertEqual(len_before, len_after)
def test_autoload_2(self):
self.daemon = Daemon()
history = self.daemon.autoload_history._autoload_history
# existing device
2021-09-26 10:44:56 +00:00
preset = "preset7"
group = groups.find(key="Foo Device 2")
mapping = Mapping()
2021-09-26 10:44:56 +00:00
mapping.change(Key(3, 2, 1), "a")
mapping.save(group.get_preset_path(preset))
config.set_autoload_preset(group.key, preset)
# ignored, won't cause problems:
2021-09-26 10:44:56 +00:00
config.set_autoload_preset("non-existant-key", "foo")
# daemon is missing the config directory yet
self.daemon.autoload()
self.assertEqual(len(history), 0)
config.save_config()
self.daemon.set_config_dir(get_config_path())
self.daemon.autoload()
self.assertEqual(len(history), 1)
self.assertEqual(history[group.key][1], preset)
2021-04-29 20:22:05 +00:00
def test_autoload_3(self):
# based on a bug
2021-09-26 10:44:56 +00:00
preset = "preset7"
group = groups.find(key="Foo Device 2")
2021-04-29 20:22:05 +00:00
mapping = Mapping()
2021-09-26 10:44:56 +00:00
mapping.change(Key(3, 2, 1), "a")
2021-04-29 20:22:05 +00:00
mapping.save(group.get_preset_path(preset))
config.set_autoload_preset(group.key, preset)
config.save_config()
self.daemon = Daemon()
self.daemon.set_config_dir(get_config_path())
2021-04-29 20:24:38 +00:00
groups.set_groups([]) # caused the bug
2021-09-26 10:44:56 +00:00
self.assertIsNone(groups.find(key="Foo Device 2"))
2021-04-29 20:22:05 +00:00
self.daemon.autoload()
2021-04-29 20:24:38 +00:00
# it should try to refresh the groups because all the
# group_keys are unknown at the moment
2021-04-29 20:22:05 +00:00
history = self.daemon.autoload_history._autoload_history
self.assertEqual(history[group.key][1], preset)
self.assertEqual(self.daemon.get_state(group.key), STARTING)
2021-09-26 10:44:56 +00:00
self.assertIsNotNone(groups.find(key="Foo Device 2"))
2021-04-29 20:22:05 +00:00
2020-11-20 20:38:59 +00:00
if __name__ == "__main__":
unittest.main()