2021-01-01 13:09:28 +00:00
|
|
|
#!/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>
|
2021-01-01 13:09:28 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2021-01-01 13:09:28 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2021-01-01 13:09:28 +00:00
|
|
|
# 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,
|
2021-01-01 13:09:28 +00:00
|
|
|
# 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/>.
|
2021-01-01 13:09:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from evdev.ecodes import KEY_LEFTSHIFT, KEY_RIGHTALT, KEY_LEFTCTRL
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.event_combination import EventCombination
|
|
|
|
from inputremapper.input_event import InputEvent
|
2021-01-01 13:09:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestKey(unittest.TestCase):
|
|
|
|
def test_key(self):
|
|
|
|
# its very similar to regular tuples, but with some extra stuff
|
2022-01-31 19:58:37 +00:00
|
|
|
key_1 = EventCombination((1, 3, 1), (1, 5, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_1), 2)
|
|
|
|
self.assertEqual(key_1[0], (1, 3, 1))
|
|
|
|
self.assertEqual(key_1[1], (1, 5, 1))
|
2021-01-25 23:15:30 +00:00
|
|
|
self.assertEqual(hash(key_1), hash(((1, 3, 1), (1, 5, 1))))
|
2021-01-01 13:09:28 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_2 = EventCombination((1, 3, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_2), 1)
|
|
|
|
self.assertNotEqual(key_2, key_1)
|
|
|
|
self.assertNotEqual(hash(key_2), hash(key_1))
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_3 = EventCombination((1, 3, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_3), 1)
|
|
|
|
self.assertEqual(key_3, key_2)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertNotEqual(key_3, (1, 3, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(hash(key_3), hash(key_2))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(hash(key_3), hash(((1, 3, 1),)))
|
2021-01-01 13:09:28 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_4 = EventCombination(*key_3)
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_4), 1)
|
|
|
|
self.assertEqual(key_4, key_3)
|
|
|
|
self.assertEqual(hash(key_4), hash(key_3))
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_5 = EventCombination(*key_4, *key_4, (1, 7, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_5), 3)
|
|
|
|
self.assertNotEqual(key_5, key_4)
|
|
|
|
self.assertNotEqual(hash(key_5), hash(key_4))
|
|
|
|
self.assertEqual(key_5, ((1, 3, 1), (1, 3, 1), (1, 7, 1)))
|
|
|
|
self.assertEqual(hash(key_5), hash(((1, 3, 1), (1, 3, 1), (1, 7, 1))))
|
|
|
|
|
|
|
|
def test_get_permutations(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
key_1 = EventCombination((1, 3, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_1.get_permutations()), 1)
|
|
|
|
self.assertEqual(key_1.get_permutations()[0], key_1)
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_2 = EventCombination((1, 3, 1), (1, 5, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_2.get_permutations()), 1)
|
|
|
|
self.assertEqual(key_2.get_permutations()[0], key_2)
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_3 = EventCombination((1, 3, 1), (1, 5, 1), (1, 7, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertEqual(len(key_3.get_permutations()), 2)
|
|
|
|
self.assertEqual(
|
2022-01-31 19:58:37 +00:00
|
|
|
key_3.get_permutations()[0],
|
|
|
|
EventCombination((1, 3, 1), (1, 5, 1), (1, 7, 1)),
|
2021-01-01 13:09:28 +00:00
|
|
|
)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(key_3.get_permutations()[1], ((1, 5, 1), (1, 3, 1), (1, 7, 1)))
|
2021-01-01 13:09:28 +00:00
|
|
|
|
|
|
|
def test_is_problematic(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
key_1 = EventCombination((1, KEY_LEFTSHIFT, 1), (1, 5, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertTrue(key_1.is_problematic())
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_2 = EventCombination((1, KEY_RIGHTALT, 1), (1, 5, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertTrue(key_2.is_problematic())
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_3 = EventCombination((1, 3, 1), (1, KEY_LEFTCTRL, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertTrue(key_3.is_problematic())
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_4 = EventCombination((1, 3, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertFalse(key_4.is_problematic())
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
key_5 = EventCombination((1, 3, 1), (1, 5, 1))
|
2021-01-01 13:09:28 +00:00
|
|
|
self.assertFalse(key_5.is_problematic())
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
def test_init(self):
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination(1))
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination(None))
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination([1]))
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination((1,)))
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination((1, 2)))
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination("1"))
|
|
|
|
self.assertRaises(ValueError, lambda: EventCombination("(1,2,3)"))
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, lambda: EventCombination((1, 2, 3), (1, 2, 3), None)
|
|
|
|
)
|
2021-04-02 14:33:20 +00:00
|
|
|
|
|
|
|
# those don't raise errors
|
2022-01-31 19:58:37 +00:00
|
|
|
EventCombination((1, 2, 3), (1, 2, 3))
|
|
|
|
EventCombination((1, 2, 3))
|
|
|
|
EventCombination(("1", "2", "3"))
|
|
|
|
EventCombination("1, 2, 3")
|
|
|
|
EventCombination("1, 2, 3", (1, 3, 4), InputEvent.from_string(" 1,5 , 1 "))
|
|
|
|
EventCombination((1, 2, 3), (1, 2, "3"))
|
|
|
|
|
|
|
|
def test_json_str(self):
|
|
|
|
c1 = EventCombination((1, 2, 3))
|
|
|
|
c2 = EventCombination((1, 2, 3), (4, 5, 6))
|
|
|
|
self.assertEqual(c1.json_str(), "1,2,3")
|
|
|
|
self.assertEqual(c2.json_str(), "1,2,3+4,5,6")
|
2021-04-02 14:33:20 +00:00
|
|
|
|
2021-01-01 13:09:28 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|