1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2025 sezanzeb <b8x45ygc9@mozmail.com>
#
# This file is part of input-remapper.
#
# 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.
#
# 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
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
"""Testing the input-remapper-control command"""
import collections
import os
import time
import unittest
from unittest.mock import patch, MagicMock
from inputremapper.bin.input_remapper_control import InputRemapperControlBin
from inputremapper.configs.global_config import GlobalConfig
from inputremapper.configs.migrations import Migrations
from inputremapper.configs.paths import PathUtils
from inputremapper.configs.preset import Preset
from inputremapper.daemon import Daemon
from inputremapper.groups import groups
from inputremapper.injection.global_uinputs import GlobalUInputs, FrontendUInput
from inputremapper.injection.mapping_handlers.mapping_parser import MappingParser
from tests.lib.test_setup import test_setup
from tests.lib.tmp import tmp
options = collections.namedtuple(
"options",
["command", "config_dir", "preset", "device", "list_devices", "key_names", "debug"],
)
@test_setup
class TestControl(unittest.TestCase):
def setUp(self):
self.global_config = GlobalConfig()
self.global_uinputs = GlobalUInputs(FrontendUInput)
self.migrations = Migrations(self.global_uinputs)
self.mapping_parser = MappingParser(self.global_uinputs)
self.input_remapper_control = InputRemapperControlBin(
self.global_config, self.migrations
)
def test_autoload(self):
device_keys = ["Foo Device 2", "Bar Device"]
groups_ = [groups.find(key=key) for key in device_keys]
presets = ["bar0", "bar", "bar2"]
paths = [
PathUtils.get_preset_path(groups_[0].name, presets[0]),
PathUtils.get_preset_path(groups_[1].name, presets[1]),
PathUtils.get_preset_path(groups_[1].name, presets[2]),
]
Preset(paths[0]).save()
Preset(paths[1]).save()
Preset(paths[2]).save()
daemon = Daemon(self.global_config, self.global_uinputs, self.mapping_parser)
self.input_remapper_control.set_daemon(daemon)
start_history = []
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
def start_injecting(device: str, preset: str):
print(f'\033[90mstart_injecting "{device}" "{preset}"\033[0m')
start_history.append((device, preset))
daemon.injectors[device] = Injector()
patch.object(daemon, "start_injecting", start_injecting).start()
self.global_config.set_autoload_preset(groups_[0].key, presets[0])
self.global_config.set_autoload_preset(groups_[1].key, presets[1])
self.input_remapper_control.communicate(
command="autoload",
config_dir=None,
preset=None,
device=None,
)
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)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
# calling autoload again doesn't load redundantly
self.input_remapper_control.communicate(
command="autoload",
config_dir=None,
preset=None,
device=None,
)
self.assertEqual(len(start_history), 2)
self.assertEqual(stop_counter, 0)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
# unless the injection in question ist stopped
self.input_remapper_control.communicate(
command="stop",
config_dir=None,
preset=None,
device=groups_[0].key,
)
self.assertEqual(stop_counter, 1)
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
self.input_remapper_control.communicate(
command="autoload",
config_dir=None,
preset=None,
device=None,
)
self.assertEqual(len(start_history), 3)
self.assertEqual(start_history[2], (groups_[0].key, presets[0]))
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
# if a device name is passed, will only start injecting for that one
self.input_remapper_control.communicate(
command="stop-all",
config_dir=None,
preset=None,
device=None,
)
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[1].key, presets[1])
)
self.assertEqual(stop_counter, 3)
self.global_config.set_autoload_preset(groups_[1].key, presets[2])
self.input_remapper_control.communicate(
command="autoload",
config_dir=None,
preset=None,
device=groups_[1].key,
)
self.assertEqual(len(start_history), 4)
self.assertEqual(start_history[3], (groups_[1].key, presets[2]))
self.assertTrue(
daemon.autoload_history.may_autoload(groups_[0].key, presets[0])
)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[2])
)
# autoloading for the same device again redundantly will not autoload
# again
self.input_remapper_control.communicate(
command="autoload",
config_dir=None,
preset=None,
device=groups_[1].key,
)
self.assertEqual(len(start_history), 4)
self.assertEqual(stop_counter, 3)
self.assertFalse(
daemon.autoload_history.may_autoload(groups_[1].key, presets[2])
)
# any other arbitrary preset may be autoloaded
self.assertTrue(daemon.autoload_history.may_autoload(groups_[1].key, "quuuux"))
# after 15 seconds it may be autoloaded again
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])
)
def test_autoload_other_path(self):
device_names = ["Foo Device", "Bar Device"]
groups_ = [groups.find(name=name) for name in device_names]
presets = ["bar123", "bar2"]
config_dir = os.path.join(tmp, "qux", "quux")
paths = [
os.path.join(config_dir, "presets", device_names[0], presets[0] + ".json"),
os.path.join(config_dir, "presets", device_names[1], presets[1] + ".json"),
]
Preset(paths[0]).save()
Preset(paths[1]).save()
daemon = Daemon(self.global_config, self.global_uinputs, self.mapping_parser)
self.input_remapper_control.set_daemon(daemon)
start_history = []
daemon.start_injecting = lambda *args: start_history.append(args)
self.global_config.path = os.path.join(config_dir, "config.json")
self.global_config.load_config()
self.global_config.set_autoload_preset(device_names[0], presets[0])
self.global_config.set_autoload_preset(device_names[1], presets[1])
self.input_remapper_control.communicate(
command="autoload",
config_dir=config_dir,
preset=None,
device=None,
)
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):
group = groups.find(key="Foo Device 2")
preset = "preset9"
daemon = Daemon(self.global_config, self.global_uinputs, self.mapping_parser)
self.input_remapper_control.set_daemon(daemon)
start_history = []
stop_history = []
stop_all_history = []
daemon.start_injecting = lambda *args: start_history.append(args)
daemon.stop_injecting = lambda *args: stop_history.append(args)
daemon.stop_all = lambda *args: stop_all_history.append(args)
self.input_remapper_control.communicate(
command="start",
config_dir=None,
preset=preset,
device=group.paths[0],
)
self.assertEqual(len(start_history), 1)
self.assertEqual(start_history[0], (group.key, preset))
self.input_remapper_control.communicate(
command="stop",
config_dir=None,
preset=None,
device=group.paths[1],
)
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,))
self.input_remapper_control.communicate(
command="stop-all",
config_dir=None,
preset=None,
device=None,
)
self.assertEqual(len(stop_all_history), 1)
self.assertEqual(stop_all_history[0], ())
@patch.object(Daemon, "quit")
def test_quit(self, quit_mock: MagicMock) -> None:
group = groups.find(key="Foo Device 2")
assert group is not None
preset = "preset9"
daemon = Daemon(self.global_config, self.global_uinputs, self.mapping_parser)
self.input_remapper_control.set_daemon(daemon)
self.input_remapper_control.communicate(
command="quit",
config_dir=None,
preset=preset,
device=group.paths[0],
)
quit_mock.assert_called_once()
def test_config_not_found(self):
key = "Foo Device 2"
path = "~/a/preset.json"
config_dir = "/foo/bar"
daemon = Daemon(self.global_config, self.global_uinputs, self.mapping_parser)
self.input_remapper_control.set_daemon(daemon)
start_history = []
stop_history = []
daemon.start_injecting = lambda *args: start_history.append(args)
daemon.stop_injecting = lambda *args: stop_history.append(args)
self.assertRaises(
SystemExit,
lambda: self.input_remapper_control.communicate(
command="start",
config_dir=config_dir,
preset=path,
device=key,
),
)
self.assertRaises(
SystemExit,
lambda: self.input_remapper_control.communicate(
command="stop",
config_dir=config_dir,
preset=None,
device=key,
),
)
def test_autoload_config_dir(self):
daemon = Daemon(self.global_config, self.global_uinputs, self.mapping_parser)
path = os.path.join(tmp, "foo")
os.makedirs(path)
with open(os.path.join(path, "config.json"), "w") as file:
file.write('{"autoload":{"foo": "bar"}}')
self.assertIsNone(self.global_config.get_autoload_preset("foo"))
daemon.set_config_dir(path)
# since daemon and this test share the same memory, the global_config
# object that this test can access will be modified
self.assertEqual(self.global_config.get_autoload_preset("foo"), "bar")
# passing a path that doesn't exist or a path that doesn't contain
# a config.json file won't do anything
os.makedirs(os.path.join(tmp, "bar"))
daemon.set_config_dir(os.path.join(tmp, "bar"))
self.assertEqual(self.global_config.get_autoload_preset("foo"), "bar")
daemon.set_config_dir(os.path.join(tmp, "qux"))
self.assertEqual(self.global_config.get_autoload_preset("foo"), "bar")
def test_internals_reader(self):
with patch.object(os, "system") as os_system_patch:
self.input_remapper_control.internals("start-reader-service", False)
os_system_patch.assert_called_once()
self.assertIn(
"input-remapper-reader-service", os_system_patch.call_args.args[0]
)
self.assertNotIn("-d", os_system_patch.call_args.args[0])
def test_internals_daemon(self):
with patch.object(os, "system") as os_system_patch:
self.input_remapper_control.internals("start-daemon", True)
os_system_patch.assert_called_once()
self.assertIn("input-remapper-service", os_system_patch.call_args.args[0])
self.assertIn("-d", os_system_patch.call_args.args[0])
if __name__ == "__main__":
unittest.main()
|