2022-01-31 19:58:37 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2023-02-27 16:07:42 +00:00
|
|
|
# Copyright (C) 2023 sezanzeb <proxima@sezanzeb.de>
|
2022-01-31 19:58:37 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
2022-11-05 13:48:23 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2022-11-20 19:36:33 +00:00
|
|
|
from evdev.ecodes import EV_KEY, EV_ABS
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-11-20 19:36:33 +00:00
|
|
|
from inputremapper.configs.mapping import Mapping
|
2022-11-01 11:07:12 +00:00
|
|
|
from inputremapper.configs.mapping import UIMapping
|
|
|
|
from inputremapper.configs.paths import get_preset_path, get_config_path, CONFIG_PATH
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.preset import Preset
|
2022-12-15 13:43:03 +00:00
|
|
|
from inputremapper.configs.input_config import InputCombination, InputConfig
|
2022-11-23 21:55:28 +00:00
|
|
|
from tests.lib.cleanup import quick_cleanup
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
class TestPreset(unittest.TestCase):
|
2022-01-31 19:58:37 +00:00
|
|
|
def setUp(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset = Preset(get_preset_path("foo", "bar2"))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
quick_cleanup()
|
|
|
|
|
2022-11-01 11:07:12 +00:00
|
|
|
def test_is_mapped_multiple_times(self):
|
2022-12-15 13:43:03 +00:00
|
|
|
combination = InputCombination(
|
2023-02-19 20:19:29 +00:00
|
|
|
InputCombination.from_tuples((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4))
|
2022-12-15 13:43:03 +00:00
|
|
|
)
|
2022-11-01 11:07:12 +00:00
|
|
|
permutations = combination.get_permutations()
|
|
|
|
self.assertEqual(len(permutations), 6)
|
|
|
|
|
|
|
|
self.preset._mappings[permutations[0]] = Mapping(
|
2022-12-15 13:43:03 +00:00
|
|
|
input_combination=permutations[0],
|
2022-11-01 11:07:12 +00:00
|
|
|
target_uinput="keyboard",
|
|
|
|
output_symbol="a",
|
|
|
|
)
|
|
|
|
self.assertFalse(self.preset._is_mapped_multiple_times(permutations[2]))
|
|
|
|
|
|
|
|
self.preset._mappings[permutations[1]] = Mapping(
|
2022-12-15 13:43:03 +00:00
|
|
|
input_combination=permutations[1],
|
2022-11-01 11:07:12 +00:00
|
|
|
target_uinput="keyboard",
|
|
|
|
output_symbol="a",
|
|
|
|
)
|
|
|
|
self.assertTrue(self.preset._is_mapped_multiple_times(permutations[2]))
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def test_has_unsaved_changes(self):
|
|
|
|
self.preset.path = get_preset_path("foo", "bar2")
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination())
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
|
|
|
self.preset.save()
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.empty()
|
|
|
|
self.assertEqual(len(self.preset), 0)
|
2022-11-12 16:45:32 +00:00
|
|
|
# empty preset but non-empty file
|
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# load again from the disc
|
|
|
|
self.preset.load()
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(
|
2022-12-15 13:43:03 +00:00
|
|
|
self.preset.get_mapping(InputCombination.empty_combination()),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# change the path to a non exiting file
|
|
|
|
self.preset.path = get_preset_path("bar", "foo")
|
2022-11-12 16:45:32 +00:00
|
|
|
# the preset has a mapping, the file has not
|
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
# change back to the original path
|
|
|
|
self.preset.path = get_preset_path("foo", "bar2")
|
2022-11-12 16:45:32 +00:00
|
|
|
# no difference between file and memory
|
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
# modify the mapping
|
2022-12-15 13:43:03 +00:00
|
|
|
mapping = self.preset.get_mapping(InputCombination.empty_combination())
|
2022-04-17 10:19:23 +00:00
|
|
|
mapping.gain = 0.5
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.load()
|
|
|
|
|
|
|
|
self.preset.path = get_preset_path("bar", "foo")
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.remove(Mapping.from_combination().input_combination)
|
2022-11-12 16:45:32 +00:00
|
|
|
# empty preset and empty file
|
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
self.preset.path = get_preset_path("foo", "bar2")
|
2022-11-12 16:45:32 +00:00
|
|
|
# empty preset, but non-empty file
|
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.load()
|
|
|
|
self.assertEqual(len(self.preset), 1)
|
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# 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)
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
def test_save_load(self):
|
2022-12-15 13:43:03 +00:00
|
|
|
one = InputConfig(type=EV_KEY, code=10)
|
|
|
|
two = InputConfig(type=EV_KEY, code=11)
|
|
|
|
three = InputConfig(type=EV_KEY, code=12)
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
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"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.preset.path = get_preset_path("Foo Device", "test")
|
|
|
|
self.preset.save()
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
path = os.path.join(CONFIG_PATH, "presets", "Foo Device", "test.json")
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
loaded = Preset(get_preset_path("Foo Device", "test"))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(loaded), 0)
|
2022-04-17 10:19:23 +00:00
|
|
|
loaded.load()
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(loaded), 3)
|
|
|
|
self.assertRaises(TypeError, loaded.get_mapping, one)
|
|
|
|
self.assertEqual(
|
2023-02-19 20:19:29 +00:00
|
|
|
loaded.get_mapping(InputCombination([one])),
|
|
|
|
Mapping.from_combination(InputCombination([one]), "keyboard", "1"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2023-02-19 20:19:29 +00:00
|
|
|
loaded.get_mapping(InputCombination([two])),
|
|
|
|
Mapping.from_combination(InputCombination([two]), "keyboard", "2"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2023-02-19 20:19:29 +00:00
|
|
|
loaded.get_mapping(InputCombination([two, three])),
|
|
|
|
Mapping.from_combination(InputCombination([two, three]), "keyboard", "3"),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# load missing file
|
|
|
|
preset = Preset(get_config_path("missing_file.json"))
|
|
|
|
self.assertRaises(FileNotFoundError, preset.load)
|
|
|
|
|
|
|
|
def test_modify_mapping(self):
|
2023-02-19 20:19:29 +00:00
|
|
|
ev_1 = InputCombination([InputConfig(type=EV_KEY, code=1)])
|
|
|
|
ev_3 = InputCombination([InputConfig(type=EV_KEY, code=2)])
|
2022-04-17 10:19:23 +00:00
|
|
|
# only values between -99 and 99 are allowed as mapping for EV_ABS or EV_REL
|
2023-02-19 20:19:29 +00:00
|
|
|
ev_4 = InputCombination([InputConfig(type=EV_ABS, code=1, analog_threshold=99)])
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# add the first mapping
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(ev_1, "keyboard", "a"))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
|
|
|
self.assertEqual(len(self.preset), 1)
|
|
|
|
|
|
|
|
# change ev_1 to ev_3 and change a to b
|
2022-04-17 10:19:23 +00:00
|
|
|
mapping = self.preset.get_mapping(ev_1)
|
2022-12-15 13:43:03 +00:00
|
|
|
mapping.input_combination = ev_3
|
2022-04-17 10:19:23 +00:00
|
|
|
mapping.output_symbol = "b"
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertIsNone(self.preset.get_mapping(ev_1))
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_3),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_3, "keyboard", "b"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 1)
|
|
|
|
|
|
|
|
# add 4
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(ev_4, "keyboard", "c"))
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_3),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_3, "keyboard", "b"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_4),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_4, "keyboard", "c"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 2)
|
|
|
|
|
|
|
|
# change the preset of 4 to d
|
2022-04-17 10:19:23 +00:00
|
|
|
mapping = self.preset.get_mapping(ev_4)
|
|
|
|
mapping.output_symbol = "d"
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_4),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_4, "keyboard", "d"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 2)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# try to change combination of 4 to 3
|
|
|
|
mapping = self.preset.get_mapping(ev_4)
|
|
|
|
with self.assertRaises(KeyError):
|
2022-12-15 13:43:03 +00:00
|
|
|
mapping.input_combination = ev_3
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_3),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_3, "keyboard", "b"),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_4),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_4, "keyboard", "d"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(len(self.preset), 2)
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def test_avoids_redundant_saves(self):
|
|
|
|
with patch.object(self.preset, "has_unsaved_changes", lambda: False):
|
|
|
|
self.preset.path = get_preset_path("foo", "bar2")
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination())
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.save()
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
with open(get_preset_path("foo", "bar2"), "r") as f:
|
|
|
|
content = f.read()
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertFalse(content)
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
def test_combinations(self):
|
2022-12-15 13:43:03 +00:00
|
|
|
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))
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(combi_1, "keyboard", "a"))
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_1),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_2),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-11-19 10:29:14 +00:00
|
|
|
# since combi_1 and combi_2 are equivalent, this raises a KeyError
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertRaises(
|
2022-04-18 11:52:59 +00:00
|
|
|
KeyError,
|
|
|
|
self.preset.add,
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_2, "keyboard", "b"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_1),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_2),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(combi_3, "keyboard", "c"))
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_1),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_2),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_3),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_3, "keyboard", "c"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mapping = self.preset.get_mapping(combi_1)
|
|
|
|
mapping.output_symbol = "c"
|
|
|
|
with self.assertRaises(KeyError):
|
2022-12-15 13:43:03 +00:00
|
|
|
mapping.input_combination = combi_3
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_1),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "c"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_2),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_1, "keyboard", "c"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(combi_3),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(combi_3, "keyboard", "c"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_remove(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
# does nothing
|
2023-02-19 20:19:29 +00:00
|
|
|
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)])
|
2022-01-31 19:58:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertRaises(TypeError, self.preset.remove, (EV_KEY, 10, 1))
|
|
|
|
self.preset.remove(ev_1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
|
|
|
self.assertEqual(len(self.preset), 0)
|
|
|
|
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(input_combination=ev_1))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 1)
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.remove(ev_1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 0)
|
|
|
|
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(ev_4, "keyboard", "KEY_KP1"))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(self.preset.has_unsaved_changes())
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.add(Mapping.from_combination(ev_3, "keyboard", "KEY_KP2"))
|
|
|
|
self.preset.add(Mapping.from_combination(ev_2, "keyboard", "KEY_KP3"))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 3)
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.remove(ev_3)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 2)
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_4),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_4, "keyboard", "KEY_KP1"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertIsNone(self.preset.get_mapping(ev_3))
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
self.preset.get_mapping(ev_2),
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(ev_2, "keyboard", "KEY_KP3"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
def test_empty(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=10)]),
|
2022-12-15 13:43:03 +00:00
|
|
|
"keyboard",
|
|
|
|
"1",
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=11)]),
|
2022-12-15 13:43:03 +00:00
|
|
|
"keyboard",
|
|
|
|
"2",
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=12)]),
|
2022-12-15 13:43:03 +00:00
|
|
|
"keyboard",
|
|
|
|
"3",
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 3)
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.path = get_config_path("test.json")
|
|
|
|
self.preset.save()
|
|
|
|
self.assertFalse(self.preset.has_unsaved_changes())
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
self.preset.empty()
|
2022-04-17 10:19:23 +00:00
|
|
|
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(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=10)]),
|
2022-12-15 13:43:03 +00:00
|
|
|
"keyboard",
|
|
|
|
"1",
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=11)]),
|
2022-12-15 13:43:03 +00:00
|
|
|
"keyboard",
|
|
|
|
"2",
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=12)]),
|
2022-12-15 13:43:03 +00:00
|
|
|
"keyboard",
|
|
|
|
"3",
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
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)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(len(self.preset), 0)
|
|
|
|
|
|
|
|
def test_dangerously_mapped_btn_left(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
# btn left is mapped
|
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig.btn_left()]),
|
2022-04-18 11:52:59 +00:00
|
|
|
"keyboard",
|
|
|
|
"1",
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(self.preset.dangerously_mapped_btn_left())
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=41)]),
|
2022-04-18 11:52:59 +00:00
|
|
|
"keyboard",
|
|
|
|
"2",
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(self.preset.dangerously_mapped_btn_left())
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# another mapping maps to btn_left
|
|
|
|
self.preset.add(
|
2023-02-19 20:19:29 +00:00
|
|
|
Mapping.from_combination(
|
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=42)]),
|
2022-04-18 11:52:59 +00:00
|
|
|
"mouse",
|
|
|
|
"btn_left",
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertFalse(self.preset.dangerously_mapped_btn_left())
|
|
|
|
|
2022-12-15 13:43:03 +00:00
|
|
|
mapping = self.preset.get_mapping(
|
2023-02-19 20:19:29 +00:00
|
|
|
InputCombination([InputConfig(type=EV_KEY, code=42)])
|
2022-12-15 13:43:03 +00:00
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
mapping.output_symbol = "BTN_Left"
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertFalse(self.preset.dangerously_mapped_btn_left())
|
|
|
|
|
2023-02-26 20:19:56 +00:00
|
|
|
mapping.target_uinput = "keyboard + mouse"
|
2022-04-17 10:19:23 +00:00
|
|
|
mapping.output_symbol = "3"
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertTrue(self.preset.dangerously_mapped_btn_left())
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# btn_left is not mapped
|
2023-02-19 20:19:29 +00:00
|
|
|
self.preset.remove(InputCombination([InputConfig.btn_left()]))
|
2022-04-17 10:19:23 +00:00
|
|
|
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)
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
ui_preset.add(UIMapping())
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertFalse(ui_preset.is_valid())
|
|
|
|
|
|
|
|
# make the mapping valid
|
2022-12-15 13:43:03 +00:00
|
|
|
m = ui_preset.get_mapping(InputCombination.empty_combination())
|
2022-04-17 10:19:23 +00:00
|
|
|
m.output_symbol = "a"
|
|
|
|
m.target_uinput = "keyboard"
|
|
|
|
self.assertTrue(ui_preset.is_valid())
|
|
|
|
|
2023-02-19 20:19:29 +00:00
|
|
|
m2 = UIMapping(
|
|
|
|
input_combination=InputCombination([InputConfig(type=1, code=2)])
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
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)
|
2022-10-02 15:33:52 +00:00
|
|
|
|
2022-12-15 13:43:03 +00:00
|
|
|
a = preset.get_mapping(m.input_combination).dict()
|
2022-10-02 15:33:52 +00:00
|
|
|
b = m.dict()
|
|
|
|
a.pop("mapping_type")
|
|
|
|
b.pop("mapping_type")
|
|
|
|
self.assertEqual(a, b)
|
2022-12-15 13:43:03 +00:00
|
|
|
# self.assertEqual(preset.get_mapping(m.input_combination), m)
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
# both presets load
|
|
|
|
ui_preset.clear()
|
|
|
|
ui_preset.path = get_config_path("test.json")
|
|
|
|
ui_preset.load()
|
|
|
|
self.assertEqual(len(ui_preset), 2)
|
2022-10-02 15:33:52 +00:00
|
|
|
|
2022-12-15 13:43:03 +00:00
|
|
|
a = ui_preset.get_mapping(m.input_combination).dict()
|
2022-10-02 15:33:52 +00:00
|
|
|
b = m.dict()
|
|
|
|
a.pop("mapping_type")
|
|
|
|
b.pop("mapping_type")
|
|
|
|
self.assertEqual(a, b)
|
2022-12-15 13:43:03 +00:00
|
|
|
# self.assertEqual(ui_preset.get_mapping(m.input_combination), m)
|
|
|
|
self.assertEqual(ui_preset.get_mapping(m2.input_combination), m2)
|
2022-04-17 10:19:23 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|