input-remapper/tests/testcases/test_mapping.py

188 lines
7.0 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-12-03 19:03:53 +00:00
from evdev.events import EV_KEY, EV_ABS
2020-11-01 20:32:06 +00:00
2020-11-09 22:16:30 +00:00
from keymapper.mapping import Mapping
2020-12-04 13:38:41 +00:00
from keymapper.state import SystemMapping
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
2020-12-04 13:38:41 +00:00
def test_system_mapping(self):
system_mapping = SystemMapping()
self.assertGreater(len(system_mapping._mapping), 100)
self.assertEqual(system_mapping.get('1'), 2)
self.assertEqual(system_mapping.get('KeY_1'), 2)
2020-12-03 00:32:36 +00:00
2020-12-04 13:38:41 +00:00
self.assertEqual(system_mapping.get('AlT_L'), 56)
self.assertEqual(system_mapping.get('KEy_LEFtALT'), 56)
2020-12-03 00:32:36 +00:00
2020-12-04 13:38:41 +00:00
self.assertEqual(system_mapping.get('kEY_LeFTSHIFT'), 42)
self.assertEqual(system_mapping.get('ShiFt_L'), 42)
2020-12-06 11:54:14 +00:00
self.assertEqual(system_mapping.get('BTN_left'), 272)
2020-12-04 13:38:41 +00:00
self.assertIsNotNone(system_mapping.get('kp_1'))
self.assertIsNotNone(system_mapping.get('KP_1'))
self.assertEqual(
system_mapping.get('KP_Left'),
system_mapping.get('KP_4')
)
self.assertEqual(
system_mapping.get('KP_Left'),
system_mapping.get('KEY_KP4')
)
2020-11-23 22:41:13 +00:00
2020-12-06 11:54:14 +00:00
names = system_mapping.list_names()
self.assertIn('key_kp1', names)
self.assertIn('key_nextsong', names)
self.assertIn('2', names)
self.assertIn('key_3', names)
self.assertIn('c', names)
self.assertIn('key_d', names)
self.assertIn('f4', names)
self.assertIn('key_f5', names)
self.assertIn('minus', names)
self.assertIn('btn_left', names)
self.assertIn('btn_right', names)
2020-11-28 14:48:57 +00:00
def test_clone(self):
mapping1 = Mapping()
mapping1.change((EV_KEY, 1), 'a')
2020-11-28 14:48:57 +00:00
mapping2 = mapping1.clone()
mapping1.change((EV_KEY, 2), 'b')
2020-11-28 14:48:57 +00:00
2020-12-02 18:33:31 +00:00
self.assertEqual(mapping1.get_character(EV_KEY, 1), 'a')
self.assertEqual(mapping1.get_character(EV_KEY, 2), 'b')
2020-11-28 14:48:57 +00:00
2020-12-02 18:33:31 +00:00
self.assertEqual(mapping2.get_character(EV_KEY, 1), 'a')
self.assertIsNone(mapping2.get_character(EV_KEY, 2))
2020-11-28 14:48:57 +00:00
2020-11-18 23:10:42 +00:00
def test_save_load(self):
self.mapping.change((EV_KEY, 10), '1')
self.mapping.change((EV_KEY, 11), '2')
self.mapping.change((EV_KEY, 12), '3')
self.mapping.config['foo'] = 'bar'
2020-11-18 23:10:42 +00:00
self.mapping.save('device 1', 'test')
2020-11-18 23:10:42 +00:00
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)
2020-12-02 18:33:31 +00:00
self.assertEqual(loaded.get_character(EV_KEY, 10), '1')
self.assertEqual(loaded.get_character(EV_KEY, 11), '2')
self.assertEqual(loaded.get_character(EV_KEY, 12), '3')
self.assertEqual(loaded.config['foo'], 'bar')
2020-11-23 20:43:31 +00:00
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
self.mapping.change((EV_KEY, 2), 'a', (EV_KEY, 1))
2020-11-08 17:51:35 +00:00
self.assertTrue(self.mapping.changed)
2020-12-02 18:33:31 +00:00
self.assertIsNone(self.mapping.get_character(EV_KEY, 1))
self.assertEqual(self.mapping.get_character(EV_KEY, 2), 'a')
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 1)
2020-12-03 19:03:53 +00:00
# change KEY 2 to ABS 16 and change a to b
self.mapping.change((EV_ABS, 16), 'b', (EV_KEY, 2))
2020-12-02 18:33:31 +00:00
self.assertIsNone(self.mapping.get_character(EV_KEY, 2))
2020-12-03 19:03:53 +00:00
self.assertEqual(self.mapping.get_character(EV_ABS, 16), 'b')
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 1)
# add 4
self.mapping.change((EV_KEY, 4), 'c', (None, None))
2020-12-03 19:03:53 +00:00
self.assertEqual(self.mapping.get_character(EV_ABS, 16), 'b')
2020-12-02 18:33:31 +00:00
self.assertEqual(self.mapping.get_character(EV_KEY, 4), 'c')
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
# change the mapping of 4 to d
self.mapping.change((EV_KEY, 4), 'd', (None, None))
2020-12-02 18:33:31 +00:00
self.assertEqual(self.mapping.get_character(EV_KEY, 4), 'd')
2020-11-08 11:48:50 +00:00
self.assertEqual(len(self.mapping), 2)
# this also works in the same way
self.mapping.change((EV_KEY, 4), 'e', (EV_KEY, 4))
2020-12-02 18:33:31 +00:00
self.assertEqual(self.mapping.get_character(EV_KEY, 4), 'e')
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((EV_KEY, '4'), 'f', (str(EV_KEY), '4'))
2020-12-02 18:33:31 +00:00
self.assertEqual(self.mapping.get_character(EV_KEY, 4), 'f')
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
self.mapping.change((EV_KEY, 'b'), 'c', (EV_KEY, 'a'))
2020-12-03 19:03:53 +00:00
self.mapping.change((EV_KEY, 'b'), 'c')
self.mapping.change(('foo', 1), 'c', ('foo', 2))
self.mapping.change(('foo', 1), 'c')
2020-11-08 20:01:58 +00:00
self.assertEqual(len(self.mapping), 2)
2020-12-03 19:03:53 +00:00
def test_change_2(self):
self.mapping.change((EV_KEY, 2), 'a')
self.mapping.change((None, 2), 'b', (EV_KEY, 2))
self.assertEqual(self.mapping.get_character(EV_KEY, 2), 'a')
self.mapping.change((EV_KEY, None), 'c', (EV_KEY, 2))
self.assertEqual(self.mapping.get_character(EV_KEY, 2), 'a')
self.assertEqual(len(self.mapping), 1)
2020-11-08 11:48:50 +00:00
def test_clear(self):
2020-11-08 17:51:35 +00:00
# does nothing
2020-12-02 18:33:31 +00:00
self.mapping.clear(EV_KEY, 40)
2020-11-08 17:51:35 +00:00
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
2020-12-02 18:33:31 +00:00
self.mapping._mapping[(EV_KEY, 40)] = 'b'
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 1)
2020-12-02 18:33:31 +00:00
self.mapping.clear(EV_KEY, 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)
self.mapping.change((EV_KEY, 10), 'KP_1', (None, None))
2020-11-08 17:51:35 +00:00
self.assertTrue(self.mapping.changed)
self.mapping.change((EV_KEY, 20), 'KP_2', (None, None))
self.mapping.change((EV_KEY, 30), 'KP_3', (None, None))
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 3)
2020-12-02 18:33:31 +00:00
self.mapping.clear(EV_KEY, 20)
2020-11-18 23:10:42 +00:00
self.assertEqual(len(self.mapping), 2)
2020-12-02 18:33:31 +00:00
self.assertEqual(self.mapping.get_character(EV_KEY, 10), 'KP_1')
self.assertIsNone(self.mapping.get_character(EV_KEY, 20))
self.assertEqual(self.mapping.get_character(EV_KEY, 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((EV_KEY, 10), '1')
self.mapping.change((EV_KEY, 11), '2')
self.mapping.change((EV_KEY, 12), '3')
2020-11-18 23:10:42 +00:00
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()