input-remapper/tests/testcases/mapping.py

118 lines
4.3 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
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_change(self):
2020-11-08 15:27:26 +00:00
# 1 is not assigned yet, ignore it
2020-11-08 11:48:50 +00:00
self.mapping.change(1, 2, 'a')
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')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(2), 258)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 1)
# change 2 to 3 and change a to b
self.mapping.change(2, 3, 'b')
self.assertIsNone(self.mapping.get_character(2))
self.assertEqual(self.mapping.get_character(3), 'b')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(3), 259)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 1)
# add 4
self.mapping.change(None, 4, 'c')
self.assertEqual(self.mapping.get_character(3), 'b')
self.assertEqual(self.mapping.get_character(4), 'c')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(4), 260)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
# change the mapping of 4 to d
self.mapping.change(None, 4, 'd')
self.assertEqual(self.mapping.get_character(4), 'd')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(4), 260)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
# this also works in the same way
self.mapping.change(4, 4, 'e')
self.assertEqual(self.mapping.get_character(4), 'e')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(4), 260)
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
2020-11-08 20:01:58 +00:00
# and this
self.mapping.change('4', '4', 'f')
self.assertEqual(self.mapping.get_character(4), 'f')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(4), 260)
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-08 20:01:58 +00:00
self.mapping.change('a', 'b', 'c')
self.assertEqual(len(self.mapping), 2)
2020-11-16 15:18:37 +00:00
def test_change_multiple(self):
self.mapping.change(None, 1, ['a', 'A'])
self.assertEqual(self.mapping.get_character(1), 'a, A')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(1), 257)
2020-11-16 15:18:37 +00:00
self.mapping.change(1, 2, ['b', 'B'])
self.assertEqual(self.mapping.get_character(2), 'b, B')
2020-11-17 16:56:59 +00:00
self.assertEqual(self.mapping.get_keycode(2), 258)
self.assertIsNone(self.mapping.get_character(1))
2020-11-17 16:56:59 +00:00
self.assertIsNone(self.mapping.get_keycode(1))
2020-11-16 15:18:37 +00:00
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)
self.mapping._mapping[40] = 'b'
self.mapping.clear(40)
self.assertTrue(self.mapping.changed)
self.mapping.change(None, 10, 'KP_1')
self.assertTrue(self.mapping.changed)
self.mapping.change(None, 20, 'KP_2')
self.mapping.change(None, 30, 'KP_3')
2020-11-08 11:48:50 +00:00
self.mapping.clear(20)
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-17 16:56:59 +00:00
def test_iterate_and_convert_to_string(self):
2020-11-08 15:27:26 +00:00
self.mapping.change(None, 10, 1)
self.mapping.change(None, 20, 2)
self.mapping.change(None, 30, 3)
self.assertListEqual(
list(self.mapping),
[(10, (266, '1')), (20, (276, '2')), (30, (286, '3'))]
2020-11-08 15:27:26 +00:00
)
2020-11-01 20:32:06 +00:00
if __name__ == "__main__":
unittest.main()