input-remapper/tests/testcases/mapping.py

156 lines
5.7 KiB
Python
Raw Normal View History

2020-11-01 20:32:06 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
# This file is part of key-mapper.
#
# key-mapper 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.
#
# key-mapper 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 key-mapper. If not, see <https://www.gnu.org/licenses/>.
import unittest
2020-11-09 22:16:30 +00:00
from keymapper.mapping import Mapping
from keymapper.state import populate_system_mapping
2020-11-01 20:32:06 +00:00
2020-11-08 11:48:50 +00:00
class TestMapping(unittest.TestCase):
def setUp(self):
self.mapping = Mapping()
2020-11-08 17:51:35 +00:00
self.assertFalse(self.mapping.changed)
2020-11-08 11:48:50 +00:00
def test_populate_system_mapping(self):
2020-11-30 17:59:34 +00:00
mapping = populate_system_mapping()
self.assertGreater(len(mapping), 100)
2020-11-23 22:41:13 +00:00
# keycode 10 is typically mapped to '1'
2020-11-30 17:59:34 +00:00
self.assertEqual(mapping['1'], 10)
2020-11-23 22:41:13 +00:00
2020-11-28 14:48:57 +00:00
def test_clone(self):
mapping1 = Mapping()
mapping1.change(1, 'a')
mapping2 = mapping1.clone()
mapping1.change(2, 'b')
self.assertEqual(mapping1.get_character(1), 'a')
self.assertEqual(mapping1.get_keycode('a'), 1)
self.assertEqual(mapping1.get_character(2), 'b')
self.assertEqual(mapping1.get_keycode('b'), 2)
self.assertEqual(mapping2.get_character(1), 'a')
self.assertEqual(mapping2.get_keycode('a'), 1)
self.assertIsNone(mapping2.get_character(2))
self.assertIsNone(mapping2.get_keycode('b'))
2020-11-18 23:10:42 +00:00
def test_save_load(self):
self.mapping.change(10, '1')
self.mapping.change(11, '2')
self.mapping.change(12, '3')
self.mapping.save('device 1', 'test')
loaded = Mapping()
self.assertEqual(len(loaded), 0)
loaded.load('device 1', 'test')
2020-11-18 23:10:42 +00:00
self.assertEqual(len(loaded), 3)
self.assertEqual(loaded.get_character(10), '1')
self.assertEqual(loaded.get_character(11), '2')
self.assertEqual(loaded.get_character(12), '3')
self.assertEqual(loaded.get_keycode('1'), 10)
self.assertEqual(loaded.get_keycode('2'), 11)
self.assertEqual(loaded.get_keycode('3'), 12)
2020-11-23 20:43:31 +00:00
def test_split(self):
# mapping supports the xmodmap/xkb syntax for modified buttons
self.mapping.change(10, 'a, A')
self.assertEqual(self.mapping.get_keycode('a'), 10)
self.assertEqual(self.mapping.get_keycode('A'), 10)
2020-11-08 11:48:50 +00:00
def test_change(self):
2020-11-08 15:27:26 +00:00
# 1 is not assigned yet, ignore it
2020-11-18 23:04:04 +00:00
self.mapping.change(2, 'a', 1)
2020-11-08 17:51:35 +00:00
self.assertTrue(self.mapping.changed)
self.assertIsNone(self.mapping.get_character(1))
self.assertEqual(self.mapping.get_character(2), 'a')
self.assertEqual(self.mapping.get_keycode('a'), 2)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 1)
# change 2 to 3 and change a to b
2020-11-18 23:04:04 +00:00
self.mapping.change(3, 'b', 2)
self.assertIsNone(self.mapping.get_character(2))
self.assertEqual(self.mapping.get_character(3), 'b')
self.assertEqual(self.mapping.get_keycode('b'), 3)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 1)
# add 4
2020-11-18 23:04:04 +00:00
self.mapping.change(4, 'c', None)
self.assertEqual(self.mapping.get_character(3), 'b')
self.assertEqual(self.mapping.get_character(4), 'c')
self.assertEqual(self.mapping.get_keycode('c'), 4)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
# change the mapping of 4 to d
2020-11-18 23:04:04 +00:00
self.mapping.change(4, 'd', None)
self.assertEqual(self.mapping.get_character(4), 'd')
self.assertEqual(self.mapping.get_keycode('d'), 4)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
# this also works in the same way
2020-11-18 23:04:04 +00:00
self.mapping.change(4, 'e', 4)
self.assertEqual(self.mapping.get_character(4), 'e')
self.assertEqual(self.mapping.get_keycode('e'), 4)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
2020-11-08 20:01:58 +00:00
# and this
2020-11-18 23:04:04 +00:00
self.mapping.change('4', 'f', '4')
self.assertEqual(self.mapping.get_character(4), 'f')
self.assertEqual(self.mapping.get_keycode('f'), 4)
2020-11-08 20:01:58 +00:00
self.assertEqual(len(self.mapping), 2)
2020-11-16 15:18:37 +00:00
# non-int keycodes are ignored
2020-11-18 23:04:04 +00:00
self.mapping.change('b', 'c', 'a')
2020-11-08 20:01:58 +00:00
self.assertEqual(len(self.mapping), 2)
2020-11-08 11:48:50 +00:00
def test_clear(self):
2020-11-08 17:51:35 +00:00
# does nothing
self.mapping.clear(40)
self.assertFalse(self.mapping.changed)
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 0)
2020-11-08 17:51:35 +00:00
self.mapping._mapping[40] = 'b'
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 1)
2020-11-08 17:51:35 +00:00
self.mapping.clear(40)
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 0)
2020-11-08 17:51:35 +00:00
self.assertTrue(self.mapping.changed)
2020-11-18 23:04:04 +00:00
self.mapping.change(10, 'KP_1', None)
2020-11-08 17:51:35 +00:00
self.assertTrue(self.mapping.changed)
2020-11-18 23:04:04 +00:00
self.mapping.change(20, 'KP_2', None)
self.mapping.change(30, 'KP_3', None)
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 3)
2020-11-08 11:48:50 +00:00
self.mapping.clear(20)
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 2)
self.assertEqual(self.mapping.get_character(10), 'KP_1')
self.assertIsNone(self.mapping.get_character(20))
self.assertEqual(self.mapping.get_character(30), 'KP_3')
2020-11-01 20:32:06 +00:00
2020-11-18 23:10:42 +00:00
def test_empty(self):
self.mapping.change(10, '1')
self.mapping.change(11, '2')
self.mapping.change(12, '3')
self.assertEqual(len(self.mapping), 3)
self.mapping.empty()
self.assertEqual(len(self.mapping), 0)
2020-11-01 20:32:06 +00:00
if __name__ == "__main__":
unittest.main()