input-remapper/tests/unit/test_control.py

315 lines
12 KiB
Python
Raw Normal View History

#!/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>
#
2022-01-01 12:00:49 +00:00
# This file is part of input-remapper.
#
2022-01-01 12:00:49 +00:00
# input-remapper 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.
#
2022-01-01 12:00:49 +00:00
# input-remapper 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
2022-01-01 12:00:49 +00:00
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
2022-01-01 12:00:49 +00:00
"""Testing the input-remapper-control command"""
import os
2021-02-07 14:00:36 +00:00
import time
import unittest
2021-03-21 18:15:20 +00:00
from unittest import mock
import collections
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
2022-01-31 19:58:37 +00:00
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.daemon import Daemon
2022-01-31 19:58:37 +00:00
from inputremapper.configs.preset import Preset
from inputremapper.configs.paths import get_preset_path
2022-01-01 12:00:49 +00:00
from inputremapper.groups import groups
from tests.test import quick_cleanup, tmp
def import_control():
2022-01-01 12:00:49 +00:00
"""Import the core function of the input-remapper-control command."""
2022-01-31 19:58:37 +00:00
active_preset.empty()
2022-01-01 12:00:49 +00:00
bin_path = os.path.join(os.getcwd(), "bin", "input-remapper-control")
2021-09-26 10:44:56 +00:00
loader = SourceFileLoader("__not_main_idk__", bin_path)
spec = spec_from_loader("__not_main_idk__", loader)
module = module_from_spec(spec)
spec.loader.exec_module(module)
return module.communicate, module.utils, module.internals
communicate, utils, internals = import_control()
options = collections.namedtuple(
2021-09-26 10:44:56 +00:00
"options",
2021-09-29 18:17:45 +00:00
["command", "config_dir", "preset", "device", "list_devices", "key_names", "debug"],
)
class TestControl(unittest.TestCase):
def tearDown(self):
quick_cleanup()
def test_autoload(self):
2021-09-26 10:44:56 +00:00
device_keys = ["Foo Device 2", "Bar Device"]
groups_ = [groups.find(key=key) for key in device_keys]
2021-09-26 10:44:56 +00:00
presets = ["bar0", "bar", "bar2"]
paths = [
get_preset_path(groups_[0].name, presets[0]),
get_preset_path(groups_[1].name, presets[1]),
2021-09-26 10:44:56 +00:00
get_preset_path(groups_[1].name, presets[2]),
]
2021-01-09 16:44:24 +00:00
2022-01-31 19:58:37 +00:00
Preset().save(paths[0])
Preset().save(paths[1])
Preset().save(paths[2])
2021-01-09 16:44:24 +00:00
daemon = Daemon()
start_history = []
2021-02-07 14:00:36 +00:00
stop_counter = 0
# using an actual injector is not within the scope of this test
class Injector:
def stop_injecting(self, *args, **kwargs):
nonlocal stop_counter
stop_counter += 1
2021-09-26 10:44:56 +00:00
2021-02-07 14:00:36 +00:00
def start_injecting(device, preset):
print(f'\033[90mstart_injecting "{device}" "{preset}"\033[0m')
2021-02-07 14:00:36 +00:00
start_history.append((device, preset))
daemon.injectors[device] = Injector()
2021-09-26 10:44:56 +00:00
2021-02-07 14:00:36 +00:00
daemon.start_injecting = start_injecting
2021-01-09 16:44:24 +00:00
2022-01-31 19:58:37 +00:00
global_config.set_autoload_preset(groups_[0].key, presets[0])
global_config.set_autoload_preset(groups_[1].key, presets[1])
2021-01-09 16:44:24 +00:00
2021-09-26 10:44:56 +00:00
communicate(options("autoload", None, None, None, False, False, False), daemon)
2021-01-09 16:44:24 +00:00
self.assertEqual(len(start_history), 2)
self.assertEqual(start_history[0], (groups_[0].key, presets[0]))
self.assertEqual(start_history[1], (groups_[1].key, presets[1]))
self.assertIn(groups_[0].key, daemon.injectors)
self.assertIn(groups_[1].key, daemon.injectors)
2021-09-26 10:44:56 +00:00
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
2021-02-07 14:00:36 +00:00
# calling autoload again doesn't load redundantly
2021-09-26 10:44:56 +00:00
communicate(options("autoload", None, None, None, False, False, False), daemon)
2021-02-07 14:00:36 +00:00
self.assertEqual(len(start_history), 2)
self.assertEqual(stop_counter, 0)
2021-09-26 10:44:56 +00:00
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
2021-02-07 14:00:36 +00:00
# unless the injection in question ist stopped
2021-09-26 10:44:56 +00:00
communicate(
2021-09-29 18:17:45 +00:00
options("stop", None, None, groups_[0].key, False, False, False), daemon
2021-09-26 10:44:56 +00:00
)
2021-02-07 14:00:36 +00:00
self.assertEqual(stop_counter, 1)
2021-09-26 10:44:56 +00:00
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
communicate(options("autoload", None, None, None, False, False, False), daemon)
2021-02-07 14:00:36 +00:00
self.assertEqual(len(start_history), 3)
self.assertEqual(start_history[2], (groups_[0].key, presets[0]))
2021-09-26 10:44:56 +00:00
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
2021-02-07 14:00:36 +00:00
# if a device name is passed, will only start injecting for that one
2021-09-26 10:44:56 +00:00
communicate(options("stop-all", None, None, None, False, False, False), daemon)
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
2021-02-07 14:00:36 +00:00
self.assertEqual(stop_counter, 3)
2022-01-31 19:58:37 +00:00
global_config.set_autoload_preset(groups_[1].key, presets[2])
2021-09-26 10:44:56 +00:00
communicate(
2021-09-29 18:17:45 +00:00
options("autoload", None, None, groups_[1].key, False, False, False), daemon
2021-09-26 10:44:56 +00:00
)
2021-02-07 14:00:36 +00:00
self.assertEqual(len(start_history), 4)
self.assertEqual(start_history[3], (groups_[1].key, presets[2]))
2021-09-26 10:44:56 +00:00
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[2])
)
2021-02-07 14:00:36 +00:00
# autoloading for the same device again redundantly will not autoload
# again
2021-09-26 10:44:56 +00:00
communicate(
2021-09-29 18:17:45 +00:00
options("autoload", None, None, groups_[1].key, False, False, False), daemon
2021-09-26 10:44:56 +00:00
)
2021-02-07 14:00:36 +00:00
self.assertEqual(len(start_history), 4)
self.assertEqual(stop_counter, 3)
2021-09-26 10:44:56 +00:00
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[2])
)
2021-02-07 14:00:36 +00:00
# any other arbitrary preset may be autoloaded
2021-09-26 10:44:56 +00:00
self.assertTrue(daemon.autoload_history.may_autoload(groups_[1].key, "quuuux"))
2021-02-07 14:00:36 +00:00
# after 15 seconds it may be autoloaded again
2021-09-26 10:44:56 +00:00
daemon.autoload_history._autoload_history[groups_[1].key] = (
time.time() - 16,
presets[2],
)
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[1].key, presets[2])
)
2021-01-09 16:44:24 +00:00
def test_autoload_other_path(self):
2021-09-26 10:44:56 +00:00
device_names = ["Foo Device", "Bar Device"]
groups_ = [groups.find(name=name) for name in device_names]
2021-09-26 10:44:56 +00:00
presets = ["bar123", "bar2"]
config_dir = os.path.join(tmp, "qux", "quux")
2021-01-09 16:44:24 +00:00
paths = [
2021-09-26 10:44:56 +00:00
os.path.join(config_dir, "presets", device_names[0], presets[0] + ".json"),
os.path.join(config_dir, "presets", device_names[1], presets[1] + ".json"),
2021-01-09 16:44:24 +00:00
]
2022-01-31 19:58:37 +00:00
Preset().save(paths[0])
Preset().save(paths[1])
daemon = Daemon()
start_history = []
daemon.start_injecting = lambda *args: start_history.append(args)
2022-01-31 19:58:37 +00:00
global_config.path = os.path.join(config_dir, "config.json")
global_config.load_config()
global_config.set_autoload_preset(device_names[0], presets[0])
global_config.set_autoload_preset(device_names[1], presets[1])
2021-09-26 10:44:56 +00:00
communicate(
options("autoload", config_dir, None, None, False, False, False),
daemon,
2021-09-26 10:44:56 +00:00
)
self.assertEqual(len(start_history), 2)
self.assertEqual(start_history[0], (groups_[0].key, presets[0]))
self.assertEqual(start_history[1], (groups_[1].key, presets[1]))
def test_start_stop(self):
2021-09-26 10:44:56 +00:00
group = groups.find(key="Foo Device 2")
preset = "preset9"
daemon = Daemon()
start_history = []
stop_history = []
2021-01-09 16:44:24 +00:00
stop_all_history = []
daemon.start_injecting = lambda *args: start_history.append(args)
daemon.stop_injecting = lambda *args: stop_history.append(args)
2021-02-07 14:00:36 +00:00
daemon.stop_all = lambda *args: stop_all_history.append(args)
2021-09-26 10:44:56 +00:00
communicate(
options("start", None, preset, group.paths[0], False, False, False),
daemon,
2021-09-26 10:44:56 +00:00
)
self.assertEqual(len(start_history), 1)
self.assertEqual(start_history[0], (group.key, preset))
2021-01-09 16:44:24 +00:00
2021-09-26 10:44:56 +00:00
communicate(
options("stop", None, None, group.paths[1], False, False, False),
daemon,
2021-09-26 10:44:56 +00:00
)
2021-01-09 16:44:24 +00:00
self.assertEqual(len(stop_history), 1)
# provided any of the groups paths as --device argument, figures out
# the correct group.key to use here
self.assertEqual(stop_history[0], (group.key,))
2021-09-26 10:44:56 +00:00
communicate(options("stop-all", None, None, None, False, False, False), daemon)
2021-01-09 16:44:24 +00:00
self.assertEqual(len(stop_all_history), 1)
self.assertEqual(stop_all_history[0], ())
def test_config_not_found(self):
2021-09-26 10:44:56 +00:00
key = "Foo Device 2"
path = "~/a/preset.json"
config_dir = "/foo/bar"
2021-01-09 16:44:24 +00:00
daemon = Daemon()
start_history = []
stop_history = []
daemon.start_injecting = lambda *args: start_history.append(args)
daemon.stop_injecting = lambda *args: stop_history.append(args)
2021-09-26 10:44:56 +00:00
options_1 = options("start", config_dir, path, key, False, False, False)
self.assertRaises(SystemExit, lambda: communicate(options_1, daemon))
2021-01-09 16:44:24 +00:00
2021-09-26 10:44:56 +00:00
options_2 = options("stop", config_dir, None, key, False, False, False)
self.assertRaises(SystemExit, lambda: communicate(options_2, daemon))
2021-01-09 16:44:24 +00:00
2021-02-07 14:00:36 +00:00
def test_autoload_config_dir(self):
daemon = Daemon()
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "foo")
2021-02-07 14:00:36 +00:00
os.makedirs(path)
2021-09-26 10:44:56 +00:00
with open(os.path.join(path, "config.json"), "w") as file:
2021-02-07 14:00:36 +00:00
file.write('{"foo":"bar"}')
2022-01-31 19:58:37 +00:00
self.assertIsNone(global_config.get("foo"))
2021-02-07 14:00:36 +00:00
daemon.set_config_dir(path)
2022-01-31 19:58:37 +00:00
# since daemon and this test share the same memory, the global_config
2021-02-07 14:00:36 +00:00
# object that this test can access will be modified
2022-01-31 19:58:37 +00:00
self.assertEqual(global_config.get("foo"), "bar")
2021-02-07 14:00:36 +00:00
# passing a path that doesn't exist or a path that doesn't contain
# a config.json file won't do anything
2021-09-26 10:44:56 +00:00
os.makedirs(os.path.join(tmp, "bar"))
daemon.set_config_dir(os.path.join(tmp, "bar"))
2022-01-31 19:58:37 +00:00
self.assertEqual(global_config.get("foo"), "bar")
2021-09-26 10:44:56 +00:00
daemon.set_config_dir(os.path.join(tmp, "qux"))
2022-01-31 19:58:37 +00:00
self.assertEqual(global_config.get("foo"), "bar")
2021-02-07 14:00:36 +00:00
2021-03-21 18:15:20 +00:00
def test_internals(self):
2021-09-26 10:44:56 +00:00
with mock.patch("os.system") as os_system_patch:
internals(options("helper", None, None, None, False, False, False))
2021-03-22 08:59:16 +00:00
os_system_patch.assert_called_once()
2022-01-01 12:00:49 +00:00
self.assertIn("input-remapper-helper", os_system_patch.call_args.args[0])
2021-09-26 10:44:56 +00:00
self.assertNotIn("-d", os_system_patch.call_args.args[0])
2021-03-21 18:15:20 +00:00
2021-09-26 10:44:56 +00:00
with mock.patch("os.system") as os_system_patch:
internals(options("start-daemon", None, None, None, False, False, True))
2021-03-22 08:59:16 +00:00
os_system_patch.assert_called_once()
2022-01-01 12:00:49 +00:00
self.assertIn("input-remapper-service", os_system_patch.call_args.args[0])
2021-09-26 10:44:56 +00:00
self.assertIn("-d", os_system_patch.call_args.args[0])
2021-03-21 18:15:20 +00:00
if __name__ == "__main__":
unittest.main()