input-remapper/tests/unit/test_daemon.py

464 lines
16 KiB
Python
Raw Normal View History

2020-11-20 20:38:59 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
2022-01-01 12:00:49 +00:00
# input-remapper - GUI for device specific keyboard mappings
2022-01-01 12:52:33 +00:00
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
2020-11-20 20:38:59 +00:00
#
2022-01-01 12:00:49 +00:00
# This file is part of input-remapper.
2020-11-20 20:38:59 +00:00
#
2022-01-01 12:00:49 +00:00
# input-remapper is free software: you can redistribute it and/or modify
2020-11-20 20:38:59 +00:00
# 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.
#
2022-01-01 12:00:49 +00:00
# input-remapper is distributed in the hope that it will be useful,
2020-11-20 20:38:59 +00:00
# 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
2022-01-01 12:00:49 +00:00
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
2020-11-20 20:38:59 +00:00
from tests.test import (
cleanup,
uinput_write_history_pipe,
new_event,
push_events,
is_service_running,
fixtures,
tmp,
)
2020-11-29 00:44:14 +00:00
import os
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, KEY_B, KEY_A
2020-12-26 18:36:47 +00:00
from pydbus import SystemBus
2020-11-20 20:38:59 +00:00
2022-01-31 19:58:37 +00:00
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.gui.active_preset import active_preset
from inputremapper.configs.global_config import global_config
2022-01-01 12:00:49 +00:00
from inputremapper.groups import groups
2022-01-31 19:58:37 +00:00
from inputremapper.configs.paths import get_config_path, mkdir, get_preset_path
from inputremapper.event_combination import EventCombination
from inputremapper.configs.preset import Preset
2022-01-01 12:00:49 +00:00
from inputremapper.injection.injector import STARTING, RUNNING, STOPPED, UNKNOWN
2022-01-17 23:55:01 +00:00
from inputremapper.daemon import Daemon
2020-11-20 20:38:59 +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())
2022-01-31 19:58:37 +00:00
global_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())
2021-03-21 18:15:20 +00:00
# no daemon runs, should try to run it via pkexec instead.
# It fails due to the patch on os.system and therefore exits the process
2021-03-21 18:15:20 +00:00
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
# make the connect command work this time by acting like a connection is
# available:
set_config_dir_callcount = 0
2020-12-26 18:36:47 +00:00
class FakeConnection:
def set_config_dir(self, *args, **kwargs):
nonlocal set_config_dir_callcount
set_config_dir_callcount += 1
2020-12-26 18:36:47 +00:00
type(SystemBus()).get = lambda *args, **kwargs: FakeConnection()
2021-03-21 18:15:20 +00:00
self.assertIsInstance(Daemon.connect(), FakeConnection)
self.assertEqual(set_config_dir_callcount, 1)
2021-03-21 18:15:20 +00:00
self.assertIsInstance(Daemon.connect(False), FakeConnection)
self.assertEqual(set_config_dir_callcount, 2)
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)
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")
2022-01-31 19:58:37 +00:00
active_preset.change(EventCombination([*ev_1, 1]), "keyboard", "a")
active_preset.change(EventCombination([*ev_2, -1]), "keyboard", "b")
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
2022-01-31 19:58:37 +00:00
active_preset.save(group.get_preset_path(preset))
global_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()
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, KEY_B)
2020-12-02 17:07:46 +00:00
self.assertEqual(event.value, 1)
2020-11-20 20:38:59 +00:00
def test_config_dir(self):
2022-01-31 19:58:37 +00:00
global_config.set("foo", "bar")
self.assertEqual(global_config.get("foo"), "bar")
# freshly loads the config and therefore removes the previosly added key.
# This is important so that if the service is started via sudo or pkexec
# it knows where to look for configuration files.
self.daemon = Daemon()
self.assertEqual(self.daemon.config_dir, get_config_path())
2022-01-31 19:58:37 +00:00
self.assertIsNone(global_config.get("foo"))
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)
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)
2022-01-31 19:58:37 +00:00
active_preset.change(EventCombination([*ev, 1]), "keyboard", "a")
system_mapping.clear()
system_mapping._set("a", KEY_A)
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"
2022-01-31 19:58:37 +00:00
active_preset.save(get_preset_path(group_name, preset))
global_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,
}
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()
self.assertEqual(event.t, (EV_KEY, KEY_A, 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
2022-01-14 17:50:57 +00:00
target = "keyboard"
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")
2022-01-31 19:58:37 +00:00
active_preset.change(EventCombination(event), target, to_name)
active_preset.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")
2022-01-31 19:58:37 +00:00
global_config.path = config_path
global_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
2022-01-31 19:58:37 +00:00
mapping = Preset()
mapping.change(EventCombination([3, 2, 1]), "keyboard", "a")
mapping.save(group.get_preset_path(preset))
2021-02-07 14:00:36 +00:00
# start
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
2022-01-31 19:58:37 +00:00
mapping = Preset()
mapping.change(EventCombination([3, 2, 1]), "keyboard", "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
2022-01-31 19:58:37 +00:00
global_config.set_autoload_preset(group.key, preset)
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)
2022-01-01 12:00:49 +00:00
# autoloading input-remapper devices does nothing
2021-02-12 19:29:26 +00:00
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")
2022-01-31 19:58:37 +00:00
mapping = Preset()
mapping.change(EventCombination([3, 2, 1]), "keyboard", "a")
mapping.save(group.get_preset_path(preset))
2022-01-31 19:58:37 +00:00
global_config.set_autoload_preset(group.key, preset)
# ignored, won't cause problems:
2022-01-31 19:58:37 +00:00
global_config.set_autoload_preset("non-existant-key", "foo")
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
2022-01-31 19:58:37 +00:00
mapping = Preset()
mapping.change(EventCombination([3, 2, 1]), "keyboard", "a")
2021-04-29 20:22:05 +00:00
mapping.save(group.get_preset_path(preset))
2022-01-31 19:58:37 +00:00
global_config.set_autoload_preset(group.key, preset)
2021-04-29 20:22:05 +00:00
self.daemon = Daemon()
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()