You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
input-remapper/tests/unit/test_preset.py

485 lines
18 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2023 sezanzeb <proxima@sezanzeb.de>
#
# 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/>.
import os
import unittest
from unittest.mock import patch
from evdev.ecodes import EV_KEY, EV_ABS
from inputremapper.configs.mapping import Mapping
2 years ago
from inputremapper.configs.mapping import UIMapping
from inputremapper.configs.paths import get_preset_path, get_config_path, CONFIG_PATH
from inputremapper.configs.preset import Preset
from inputremapper.configs.input_config import InputCombination, InputConfig
from tests.lib.cleanup import quick_cleanup
class TestPreset(unittest.TestCase):
def setUp(self):
self.preset = Preset(get_preset_path("foo", "bar2"))
self.assertFalse(self.preset.has_unsaved_changes())
def tearDown(self):
quick_cleanup()
2 years ago
def test_is_mapped_multiple_times(self):
combination = InputCombination(
InputCombination.from_tuples((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4))
)
2 years ago
permutations = combination.get_permutations()
self.assertEqual(len(permutations), 6)
self.preset._mappings[permutations[0]] = Mapping(
input_combination=permutations[0],
2 years ago
target_uinput="keyboard",
output_symbol="a",
)
self.assertFalse(self.preset._is_mapped_multiple_times(permutations[2]))
self.preset._mappings[permutations[1]] = Mapping(
input_combination=permutations[1],
2 years ago
target_uinput="keyboard",
output_symbol="a",
)
self.assertTrue(self.preset._is_mapped_multiple_times(permutations[2]))
def test_has_unsaved_changes(self):
self.preset.path = get_preset_path("foo", "bar2")
self.preset.add(Mapping.from_combination())
self.assertTrue(self.preset.has_unsaved_changes())
self.preset.save()
self.assertFalse(self.preset.has_unsaved_changes())
self.preset.empty()
self.assertEqual(len(self.preset), 0)
# empty preset but non-empty file
self.assertTrue(self.preset.has_unsaved_changes())
# load again from the disc
self.preset.load()
self.assertEqual(
self.preset.get_mapping(InputCombination.empty_combination()),
Mapping.from_combination(),
)
self.assertFalse(self.preset.has_unsaved_changes())
# change the path to a non exiting file
self.preset.path = get_preset_path("bar", "foo")
# the preset has a mapping, the file has not
self.assertTrue(self.preset.has_unsaved_changes())
# change back to the original path
self.preset.path = get_preset_path("foo", "bar2")
# no difference between file and memory
self.assertFalse(self.preset.has_unsaved_changes())
# modify the mapping
mapping = self.preset.get_mapping(InputCombination.empty_combination())
mapping.gain = 0.5
self.assertTrue(self.preset.has_unsaved_changes())
self.preset.load()
self.preset.path = get_preset_path("bar", "foo")
self.preset.remove(Mapping.from_combination().input_combination)
# empty preset and empty file
self.assertFalse(self.preset.has_unsaved_changes())
self.preset.path = get_preset_path("foo", "bar2")
# empty preset, but non-empty file
self.assertTrue(self.preset.has_unsaved_changes())
self.preset.load()
self.assertEqual(len(self.preset), 1)
self.assertFalse(self.preset.has_unsaved_changes())
# delete the preset from the system:
self.preset.empty()
self.preset.save()
self.preset.load()
self.assertFalse(self.preset.has_unsaved_changes())
self.assertEqual(len(self.preset), 0)
def test_save_load(self):
one = InputConfig(type=EV_KEY, code=10)
two = InputConfig(type=EV_KEY, code=11)
three = InputConfig(type=EV_KEY, code=12)
self.preset.add(
Mapping.from_combination(InputCombination([one]), "keyboard", "1")
)
self.preset.add(
Mapping.from_combination(InputCombination([two]), "keyboard", "2")
)
self.preset.add(
Mapping.from_combination(InputCombination((two, three)), "keyboard", "3"),
)
self.preset.path = get_preset_path("Foo Device", "test")
self.preset.save()
path = os.path.join(CONFIG_PATH, "presets", "Foo Device", "test.json")
self.assertTrue(os.path.exists(path))
loaded = Preset(get_preset_path("Foo Device", "test"))
self.assertEqual(len(loaded), 0)
loaded.load()
self.assertEqual(len(loaded), 3)
self.assertRaises(TypeError, loaded.get_mapping, one)
self.assertEqual(
loaded.get_mapping(InputCombination([one])),
Mapping.from_combination(InputCombination([one]), "keyboard", "1"),
)
self.assertEqual(
loaded.get_mapping(InputCombination([two])),
Mapping.from_combination(InputCombination([two]), "keyboard", "2"),
)
self.assertEqual(
loaded.get_mapping(InputCombination([two, three])),
Mapping.from_combination(InputCombination([two, three]), "keyboard", "3"),
)
# load missing file
preset = Preset(get_config_path("missing_file.json"))
self.assertRaises(FileNotFoundError, preset.load)
def test_modify_mapping(self):
ev_1 = InputCombination([InputConfig(type=EV_KEY, code=1)])
ev_3 = InputCombination([InputConfig(type=EV_KEY, code=2)])
# only values between -99 and 99 are allowed as mapping for EV_ABS or EV_REL
ev_4 = InputCombination([InputConfig(type=EV_ABS, code=1, analog_threshold=99)])
# add the first mapping
self.preset.add(Mapping.from_combination(ev_1, "keyboard", "a"))
self.assertTrue(self.preset.has_unsaved_changes())
self.assertEqual(len(self.preset), 1)
# change ev_1 to ev_3 and change a to b
mapping = self.preset.get_mapping(ev_1)
mapping.input_combination = ev_3
mapping.output_symbol = "b"
self.assertIsNone(self.preset.get_mapping(ev_1))
self.assertEqual(
self.preset.get_mapping(ev_3),
Mapping.from_combination(ev_3, "keyboard", "b"),
)
self.assertEqual(len(self.preset), 1)
# add 4
self.preset.add(Mapping.from_combination(ev_4, "keyboard", "c"))
self.assertEqual(
self.preset.get_mapping(ev_3),
Mapping.from_combination(ev_3, "keyboard", "b"),
)
self.assertEqual(
self.preset.get_mapping(ev_4),
Mapping.from_combination(ev_4, "keyboard", "c"),
)
self.assertEqual(len(self.preset), 2)
# change the preset of 4 to d
mapping = self.preset.get_mapping(ev_4)
mapping.output_symbol = "d"
self.assertEqual(
self.preset.get_mapping(ev_4),
Mapping.from_combination(ev_4, "keyboard", "d"),
)
self.assertEqual(len(self.preset), 2)
# try to change combination of 4 to 3
mapping = self.preset.get_mapping(ev_4)
with self.assertRaises(KeyError):
mapping.input_combination = ev_3
self.assertEqual(
self.preset.get_mapping(ev_3),
Mapping.from_combination(ev_3, "keyboard", "b"),
)
self.assertEqual(
self.preset.get_mapping(ev_4),
Mapping.from_combination(ev_4, "keyboard", "d"),
)
self.assertEqual(len(self.preset), 2)
def test_avoids_redundant_saves(self):
with patch.object(self.preset, "has_unsaved_changes", lambda: False):
self.preset.path = get_preset_path("foo", "bar2")
self.preset.add(Mapping.from_combination())
self.preset.save()
with open(get_preset_path("foo", "bar2"), "r") as f:
content = f.read()
self.assertFalse(content)
def test_combinations(self):
ev_1 = InputConfig(type=EV_KEY, code=1, analog_threshold=111)
ev_2 = InputConfig(type=EV_KEY, code=1, analog_threshold=222)
ev_3 = InputConfig(type=EV_KEY, code=2, analog_threshold=111)
ev_4 = InputConfig(type=EV_ABS, code=1, analog_threshold=99)
combi_1 = InputCombination((ev_1, ev_2, ev_3))
combi_2 = InputCombination((ev_2, ev_1, ev_3))
combi_3 = InputCombination((ev_1, ev_2, ev_4))
self.preset.add(Mapping.from_combination(combi_1, "keyboard", "a"))
self.assertEqual(
self.preset.get_mapping(combi_1),
Mapping.from_combination(combi_1, "keyboard", "a"),
)
self.assertEqual(
self.preset.get_mapping(combi_2),
Mapping.from_combination(combi_1, "keyboard", "a"),
)
2 years ago
# since combi_1 and combi_2 are equivalent, this raises a KeyError
self.assertRaises(
KeyError,
self.preset.add,
Mapping.from_combination(combi_2, "keyboard", "b"),
)
self.assertEqual(
self.preset.get_mapping(combi_1),
Mapping.from_combination(combi_1, "keyboard", "a"),
)
self.assertEqual(
self.preset.get_mapping(combi_2),
Mapping.from_combination(combi_1, "keyboard", "a"),
)
self.preset.add(Mapping.from_combination(combi_3, "keyboard", "c"))
self.assertEqual(
self.preset.get_mapping(combi_1),
Mapping.from_combination(combi_1, "keyboard", "a"),
)
self.assertEqual(
self.preset.get_mapping(combi_2),
Mapping.from_combination(combi_1, "keyboard", "a"),
)
self.assertEqual(
self.preset.get_mapping(combi_3),
Mapping.from_combination(combi_3, "keyboard", "c"),
)
mapping = self.preset.get_mapping(combi_1)
mapping.output_symbol = "c"
with self.assertRaises(KeyError):
mapping.input_combination = combi_3
self.assertEqual(
self.preset.get_mapping(combi_1),
Mapping.from_combination(combi_1, "keyboard", "c"),
)
self.assertEqual(
self.preset.get_mapping(combi_2),
Mapping.from_combination(combi_1, "keyboard", "c"),
)
self.assertEqual(
self.preset.get_mapping(combi_3),
Mapping.from_combination(combi_3, "keyboard", "c"),
)
def test_remove(self):
# does nothing
ev_1 = InputCombination([InputConfig(type=EV_KEY, code=40)])
ev_2 = InputCombination([InputConfig(type=EV_KEY, code=30)])
ev_3 = InputCombination([InputConfig(type=EV_KEY, code=20)])
ev_4 = InputCombination([InputConfig(type=EV_KEY, code=10)])
self.assertRaises(TypeError, self.preset.remove, (EV_KEY, 10, 1))
self.preset.remove(ev_1)
self.assertFalse(self.preset.has_unsaved_changes())
self.assertEqual(len(self.preset), 0)
self.preset.add(Mapping.from_combination(input_combination=ev_1))
self.assertEqual(len(self.preset), 1)
self.preset.remove(ev_1)
self.assertEqual(len(self.preset), 0)
self.preset.add(Mapping.from_combination(ev_4, "keyboard", "KEY_KP1"))
self.assertTrue(self.preset.has_unsaved_changes())
self.preset.add(Mapping.from_combination(ev_3, "keyboard", "KEY_KP2"))
self.preset.add(Mapping.from_combination(ev_2, "keyboard", "KEY_KP3"))
self.assertEqual(len(self.preset), 3)
self.preset.remove(ev_3)
self.assertEqual(len(self.preset), 2)
self.assertEqual(
self.preset.get_mapping(ev_4),
Mapping.from_combination(ev_4, "keyboard", "KEY_KP1"),
)
self.assertIsNone(self.preset.get_mapping(ev_3))
self.assertEqual(
self.preset.get_mapping(ev_2),
Mapping.from_combination(ev_2, "keyboard", "KEY_KP3"),
)
def test_empty(self):
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=10)]),
"keyboard",
"1",
),
)
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=11)]),
"keyboard",
"2",
),
)
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=12)]),
"keyboard",
"3",
),
)
self.assertEqual(len(self.preset), 3)
self.preset.path = get_config_path("test.json")
self.preset.save()
self.assertFalse(self.preset.has_unsaved_changes())
self.preset.empty()
self.assertEqual(self.preset.path, get_config_path("test.json"))
self.assertTrue(self.preset.has_unsaved_changes())
self.assertEqual(len(self.preset), 0)
def test_clear(self):
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=10)]),
"keyboard",
"1",
),
)
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=11)]),
"keyboard",
"2",
),
)
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=12)]),
"keyboard",
"3",
),
)
self.assertEqual(len(self.preset), 3)
self.preset.path = get_config_path("test.json")
self.preset.save()
self.assertFalse(self.preset.has_unsaved_changes())
self.preset.clear()
self.assertFalse(self.preset.has_unsaved_changes())
self.assertIsNone(self.preset.path)
self.assertEqual(len(self.preset), 0)
def test_dangerously_mapped_btn_left(self):
# btn left is mapped
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig.btn_left()]),
"keyboard",
"1",
)
)
self.assertTrue(self.preset.dangerously_mapped_btn_left())
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=41)]),
"keyboard",
"2",
)
)
self.assertTrue(self.preset.dangerously_mapped_btn_left())
# another mapping maps to btn_left
self.preset.add(
Mapping.from_combination(
InputCombination([InputConfig(type=EV_KEY, code=42)]),
"mouse",
"btn_left",
)
)
self.assertFalse(self.preset.dangerously_mapped_btn_left())
mapping = self.preset.get_mapping(
InputCombination([InputConfig(type=EV_KEY, code=42)])
)
mapping.output_symbol = "BTN_Left"
self.assertFalse(self.preset.dangerously_mapped_btn_left())
mapping.target_uinput = "keyboard + mouse"
mapping.output_symbol = "3"
self.assertTrue(self.preset.dangerously_mapped_btn_left())
# btn_left is not mapped
self.preset.remove(InputCombination([InputConfig.btn_left()]))
self.assertFalse(self.preset.dangerously_mapped_btn_left())
def test_save_load_with_invalid_mappings(self):
ui_preset = Preset(get_config_path("test.json"), mapping_factory=UIMapping)
Frontend Refactor (#375) * Tests for the GuiEventHandler * Implement GuiEventHandler * tests for data manager * Implemented data_manager * Remove Ellipsis from type hint * workaround for old pydantic version * workaround for old pydantic version * some more tests for data_manager * Updated Data Manager * move DeviceSelection to its own class * Data Manager no longer listens for events * Moved PresetSelection to its own class * MappingListBox and SelectionLable Listen to the EventHandler * DataManager no longer creates its own data objects in the init * removed global reader object * Changed UI startup * created backend Interface * event_handler debug logs show function which emit a event * some cleanup * added target selector to components * created code editor component * adapted autocompletion & some cleanup * black * connected some buttons to the event_handler * tests for data_manager newest_preset and group * cleanup presets and test_presets * migrated confirm delete dialog * backend tests * controller tests * add python3-gi to ci * more dependencies * and more ... * Github-Actions workaround remove this commit * not so many permission denyed errors in test.yml * Fix #404 (hopefully) * revert Github-Actions workaround * More tests * event_handler allows for event supression * more tests * WIP Implement Key recording * Start and Stop Injection * context no longer stores preset * restructured the RelToBtnHandler * Simplified read_loop * Implement async iterator for ipc.pipe * multiple event actions * helper now implements mapping handlers to read inputs all with async * updated and simplified reader the helper uses the mapping handlers, so the reader now can be much simpler * Fixed race condition in tests * implemented DataBus * Fixed a UIMapping bug where the last_error would not be deleted * added a immutable variant of the UIMapping * updated data_manager to use data_bus * Uptdated tests to use the DataBus * Gui uses DataBus * removed EventHandler * Renamed controller methods * Implemented recording toggle * implemented StatusBar * Sending validation errors to status bar * sending injection status to status bar * proper preset renaming * implemented copy preset in the data manager * implemented copy_preset in controller * fixed a bug where a wron selection lable would update * no longer send invalid data over the bus, if the preset or group changes * Implement create and delete mapping * Allow for frontend specific mapping defaults * implemented autoload toggle * cleanup user_interface * removed editor * Docstings renaming and ordering of methods * more simplifications to user_interface * integrated backend into data_manager * removed active preset * transformation tests * controller tests * fix missing uinputs in gui * moved some tests and implemented basic tests for mapping handlers * docstring reformatting Co-authored-by: Tobi <proxima@sezanzeb.de> * allow for empty groups * docstring * fixed TestGroupFromHelper * some work on integration tests * test for annoying import error in tests * testing if test_user_interface works * I feel lucky * not so lucky * some more tests * fixed but where the group_key was used as folder name * Fixed a bug where state=NO_GRAB would never be read from the injector * allow to stop the recorder * working on integration tests * integration tests * fixed more integration tests * updated coveragerc * no longer attempt to record keys when injecting * event_reader cleans up not finished tasks * More integration tests * All tests pass * renamed data_bus * WIP fixing typing issues * more typing fixes * added keyboard+mouse device to tests * cleanup imports * new read loop because the evdev async read loop can not be cancelled * Added field to modify mapping name * created tests for components * even more component tests * do component tests need a screen? * apparently they do :_( * created release_input switch * Don't record relative axis when movement is slow * show delete dialog above main window * wip basic dialog to edit combination * some gui changes to the combination-editor * Simple implementation of CombinationListbox * renamed attach_to_events method and mark as private * shorter str() for UInputsData * moved logic to generate readable event string from combination to event * new mapping parameter force release timeout this helps with the helper when recording multiple relative axis at once * make it possible to rearange the event_combination * more work on the combination editor * tests for DataManager.load_event * simplyfied test_controller * more controller tests * Implement input threshold in gui * greater range for time dependent unit test * implemented a output-axis selector * data_manager now provides injector state * black * mypy * Updated confirm cancel dialog * created release timeout input * implemented transformation graph * Added sliders for gain, expo and deadzone * fix bug where the system_mapping was overridden in each injector thread * updated slider settings * removed debug statement * explicitly checking output code against None (0 is a valid code) * usage * Allow for multiple axis to be activated by same button * readme * only warn about not implemented mapping-handler don't fail to create event-pipelines * More accurate event names * Allow removal of single events from the input-combination * rename callback to notify_callback * rename event message to selected_event * made read_continuisly private * typing for autocompletion * docstrings for message_broker messages * make components methods and propreties private * gui spacings * removed eval * make some controller functions private * move status message generation from data_manager to controller * parse mapping errors in controller for more helpful messages * remove system_mapping from code editor * More component tests * more tests * mypy * make grab_devices less greedy (partial mitigation for #435) only grab one device if there are multiple which can satisfy the same mapping * accumulate more values in test * docstrings * Updated status messages * comments, docstrings, imports Co-authored-by: Tobi <proxima@sezanzeb.de>
2 years ago
ui_preset.add(UIMapping())
self.assertFalse(ui_preset.is_valid())
# make the mapping valid
m = ui_preset.get_mapping(InputCombination.empty_combination())
m.output_symbol = "a"
m.target_uinput = "keyboard"
self.assertTrue(ui_preset.is_valid())
m2 = UIMapping(
input_combination=InputCombination([InputConfig(type=1, code=2)])
)
ui_preset.add(m2)
self.assertFalse(ui_preset.is_valid())
ui_preset.save()
# only the valid preset is loaded
preset = Preset(get_config_path("test.json"))
preset.load()
self.assertEqual(len(preset), 1)
a = preset.get_mapping(m.input_combination).dict()
b = m.dict()
a.pop("mapping_type")
b.pop("mapping_type")
self.assertEqual(a, b)
# self.assertEqual(preset.get_mapping(m.input_combination), m)
# both presets load
ui_preset.clear()
ui_preset.path = get_config_path("test.json")
ui_preset.load()
self.assertEqual(len(ui_preset), 2)
a = ui_preset.get_mapping(m.input_combination).dict()
b = m.dict()
a.pop("mapping_type")
b.pop("mapping_type")
self.assertEqual(a, b)
# self.assertEqual(ui_preset.get_mapping(m.input_combination), m)
self.assertEqual(ui_preset.get_mapping(m2.input_combination), m2)
if __name__ == "__main__":
unittest.main()