input-remapper/tests/unit/test_context.py

131 lines
5.1 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/>.
from tests.test import quick_cleanup
import unittest
2022-01-01 12:00:49 +00:00
from inputremapper.injection.context import Context
2022-01-31 19:58:37 +00:00
from inputremapper.configs.preset import Preset
from inputremapper.event_combination import EventCombination
from inputremapper.configs.global_config import NONE, MOUSE, WHEEL, BUTTONS
from inputremapper.configs.system_mapping import system_mapping
class TestContext(unittest.TestCase):
2021-09-29 18:17:45 +00:00
@classmethod
def setUpClass(cls):
quick_cleanup()
def setUp(self):
2022-01-31 19:58:37 +00:00
self.mapping = Preset()
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", WHEEL)
self.mapping.set("gamepad.joystick.right_purpose", WHEEL)
2022-01-31 19:58:37 +00:00
self.mapping.change(EventCombination([1, 31, 1]), "keyboard", "k(a)")
self.mapping.change(EventCombination([1, 32, 1]), "keyboard", "b")
self.mapping.change(
EventCombination((1, 33, 1), (1, 34, 1), (1, 35, 1)), "keyboard", "c"
)
self.context = Context(self.mapping)
def test_update_purposes(self):
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", BUTTONS)
self.mapping.set("gamepad.joystick.right_purpose", MOUSE)
self.context.update_purposes()
self.assertEqual(self.context.left_purpose, BUTTONS)
self.assertEqual(self.context.right_purpose, MOUSE)
def test_parse_macros(self):
self.assertEqual(len(self.context.macros), 1)
2022-01-14 17:50:57 +00:00
self.assertEqual(self.context.macros[((1, 31, 1),)][1], "keyboard")
self.assertEqual(self.context.macros[((1, 31, 1),)][0].code, "k(a)")
def test_map_keys_to_codes(self):
2021-09-26 10:44:56 +00:00
b = system_mapping.get("b")
c = system_mapping.get("c")
self.assertEqual(len(self.context.key_to_code), 3)
2022-01-14 17:50:57 +00:00
self.assertEqual(self.context.key_to_code[((1, 32, 1),)], (b, "keyboard"))
2021-09-26 10:44:56 +00:00
self.assertEqual(
2022-01-14 17:50:57 +00:00
self.context.key_to_code[(1, 33, 1), (1, 34, 1), (1, 35, 1)],
(c, "keyboard"),
2021-09-26 10:44:56 +00:00
)
self.assertEqual(
2022-01-14 17:50:57 +00:00
self.context.key_to_code[(1, 34, 1), (1, 33, 1), (1, 35, 1)],
(c, "keyboard"),
2021-09-26 10:44:56 +00:00
)
def test_is_mapped(self):
2021-09-26 10:44:56 +00:00
self.assertTrue(self.context.is_mapped(((1, 32, 1),)))
self.assertTrue(self.context.is_mapped(((1, 33, 1), (1, 34, 1), (1, 35, 1))))
self.assertTrue(self.context.is_mapped(((1, 34, 1), (1, 33, 1), (1, 35, 1))))
self.assertFalse(self.context.is_mapped(((1, 34, 1), (1, 35, 1), (1, 33, 1))))
self.assertFalse(self.context.is_mapped(((1, 36, 1),)))
def test_maps_joystick(self):
self.assertTrue(self.context.maps_joystick())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", NONE)
self.mapping.set("gamepad.joystick.right_purpose", NONE)
self.context.update_purposes()
self.assertFalse(self.context.maps_joystick())
def test_joystick_as_dpad(self):
self.assertTrue(self.context.maps_joystick())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", WHEEL)
self.mapping.set("gamepad.joystick.right_purpose", MOUSE)
self.context.update_purposes()
self.assertFalse(self.context.joystick_as_dpad())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", BUTTONS)
self.mapping.set("gamepad.joystick.right_purpose", NONE)
self.context.update_purposes()
self.assertTrue(self.context.joystick_as_dpad())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", MOUSE)
self.mapping.set("gamepad.joystick.right_purpose", BUTTONS)
self.context.update_purposes()
self.assertTrue(self.context.joystick_as_dpad())
def test_joystick_as_mouse(self):
self.assertTrue(self.context.maps_joystick())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.right_purpose", MOUSE)
self.context.update_purposes()
self.assertTrue(self.context.joystick_as_mouse())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.left_purpose", NONE)
self.mapping.set("gamepad.joystick.right_purpose", NONE)
self.context.update_purposes()
self.assertFalse(self.context.joystick_as_mouse())
2021-09-26 10:44:56 +00:00
self.mapping.set("gamepad.joystick.right_purpose", BUTTONS)
self.context.update_purposes()
self.assertFalse(self.context.joystick_as_mouse())
def test_writes_keys(self):
self.assertTrue(self.context.writes_keys())
2022-01-31 19:58:37 +00:00
self.assertFalse(Context(Preset()).writes_keys())
if __name__ == "__main__":
unittest.main()