2020-11-08 15:27:26 +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 os
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
|
2020-11-17 22:48:30 +00:00
|
|
|
from keymapper.archive.xkb import custom_mapping, generate_symbols, \
|
2020-11-15 11:11:35 +00:00
|
|
|
create_identity_mapping, create_setxkbmap_config, \
|
2020-11-18 09:33:59 +00:00
|
|
|
get_preset_name, create_default_symbols, parse_symbols_file, \
|
|
|
|
get_usr_path, KEYCODES_PATH, USERS_SYMBOLS
|
2020-11-08 15:27:26 +00:00
|
|
|
|
|
|
|
from test import tmp
|
|
|
|
|
|
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-11-10 22:14:28 +00:00
|
|
|
custom_mapping.empty()
|
|
|
|
custom_mapping.change(None, 10, 'a')
|
|
|
|
custom_mapping.change(None, 11, 'KP_1')
|
|
|
|
custom_mapping.change(None, 12, 3)
|
2020-11-16 15:18:37 +00:00
|
|
|
custom_mapping.change(None, 13, ['a', 'A', 'NoSymbol', 3])
|
2020-11-17 13:35:36 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(12), '3')
|
|
|
|
self.assertEqual(custom_mapping.get_character(13), 'a, A, NoSymbol, 3')
|
2020-11-08 15:27:26 +00:00
|
|
|
if os.path.exists(tmp):
|
|
|
|
shutil.rmtree(tmp)
|
|
|
|
|
|
|
|
def test_create_setxkbmap_config(self):
|
2020-11-08 18:50:30 +00:00
|
|
|
create_setxkbmap_config('device a', 'preset b')
|
2020-11-08 15:27:26 +00:00
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(
|
2020-11-10 22:14:28 +00:00
|
|
|
USERS_SYMBOLS,
|
2020-11-08 15:27:26 +00:00
|
|
|
'device_a',
|
|
|
|
'preset_b'
|
|
|
|
)))
|
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(KEYCODES_PATH))
|
|
|
|
|
2020-11-15 19:27:06 +00:00
|
|
|
with open(get_usr_path('device_a', 'preset_b'), 'r') as f:
|
2020-11-08 15:27:26 +00:00
|
|
|
content = f.read()
|
|
|
|
self.assertIn('key <10> { [ a ] };', content)
|
2020-11-08 17:51:35 +00:00
|
|
|
self.assertIn('key <11> { [ KP_1 ] };', content)
|
2020-11-08 15:27:26 +00:00
|
|
|
self.assertIn('key <12> { [ 3 ] };', content)
|
2020-11-16 15:52:43 +00:00
|
|
|
self.assertIn('key <13> { [ a, A, NoSymbol, 3 ] };', content)
|
2020-11-08 15:27:26 +00:00
|
|
|
|
2020-11-15 11:11:35 +00:00
|
|
|
self.assertIn('include "key-mapper/user/default"', content)
|
|
|
|
self.assertIn(get_preset_name('device_a', 'preset_b'), content)
|
|
|
|
|
2020-11-16 15:52:43 +00:00
|
|
|
# it should be loaded correctly after saving
|
|
|
|
parse_symbols_file('device_a', 'preset_b')
|
2020-11-17 13:35:36 +00:00
|
|
|
self.assertIsNone(custom_mapping.get_character(9))
|
|
|
|
self.assertEqual(custom_mapping.get_character(10), 'a')
|
|
|
|
self.assertEqual(custom_mapping.get_character(11), 'KP_1')
|
|
|
|
self.assertEqual(custom_mapping.get_character(12), '3')
|
|
|
|
self.assertEqual(custom_mapping.get_character(13), 'a, A, NoSymbol, 3')
|
|
|
|
self.assertIsNone(custom_mapping.get_character(14))
|
2020-11-16 15:52:43 +00:00
|
|
|
|
2020-11-15 11:11:35 +00:00
|
|
|
def test_default_symbols(self):
|
|
|
|
# keycodes are missing
|
|
|
|
self.assertRaises(
|
|
|
|
FileNotFoundError,
|
|
|
|
create_default_symbols
|
|
|
|
)
|
|
|
|
create_identity_mapping()
|
|
|
|
create_default_symbols()
|
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(get_usr_path('default')))
|
|
|
|
|
2020-11-15 19:27:06 +00:00
|
|
|
with open(get_usr_path('default'), 'r') as f:
|
2020-11-15 11:11:35 +00:00
|
|
|
content = f.read()
|
|
|
|
self.assertNotIn('include', content)
|
2020-11-16 15:18:37 +00:00
|
|
|
# this is pretty much the same on every keyboard.
|
|
|
|
# The default config should also contain the modified keys, but
|
|
|
|
# since they are different in every country just check for the
|
|
|
|
# comma.
|
|
|
|
self.assertIn('key <10> { [ 1, ', content)
|
|
|
|
self.assertIn('key <11> { [ 2, ', content)
|
|
|
|
self.assertIn('key <12> { [ 3, ', content)
|
|
|
|
self.assertIn('key <13> { [ 4, ', content)
|
|
|
|
self.assertIn('key <65> { [ space, ', content)
|
2020-11-15 11:11:35 +00:00
|
|
|
|
|
|
|
def test_get_preset_name(self):
|
|
|
|
self.assertEqual(get_preset_name('a', 'b'), 'key-mapper/user/a/b')
|
|
|
|
self.assertEqual(get_preset_name('a'), 'key-mapper/user/a')
|
|
|
|
|
2020-11-08 15:27:26 +00:00
|
|
|
def test_generate_content(self):
|
|
|
|
self.assertRaises(
|
|
|
|
FileNotFoundError,
|
2020-11-10 22:14:28 +00:00
|
|
|
generate_symbols,
|
2020-11-08 18:50:30 +00:00
|
|
|
'device', 'preset'
|
2020-11-08 15:27:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# create the identity mapping, because it is required for
|
2020-11-10 22:14:28 +00:00
|
|
|
# generate_symbols
|
2020-11-08 15:27:26 +00:00
|
|
|
create_identity_mapping()
|
|
|
|
|
2020-11-10 22:14:28 +00:00
|
|
|
content = generate_symbols('device/preset')
|
2020-11-08 15:27:26 +00:00
|
|
|
self.assertIn('key <10> { [ a ] };', content)
|
2020-11-08 17:51:35 +00:00
|
|
|
self.assertIn('key <11> { [ KP_1 ] };', content)
|
2020-11-08 15:27:26 +00:00
|
|
|
self.assertIn('key <12> { [ 3 ] };', content)
|
2020-11-16 15:18:37 +00:00
|
|
|
self.assertIn('key <13> { [ a, A, NoSymbol, 3 ] };', content)
|
2020-11-08 15:27:26 +00:00
|
|
|
|
2020-11-15 11:11:35 +00:00
|
|
|
def test_identity_mapping(self):
|
|
|
|
create_identity_mapping()
|
|
|
|
self.assertTrue(os.path.exists(KEYCODES_PATH))
|
|
|
|
with open(KEYCODES_PATH, 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
self.assertIn('minimum = 8;', content)
|
|
|
|
# whatever the maximum is, might change if mouse buttons
|
|
|
|
# can be supported at some point as well (no idea how)
|
|
|
|
self.assertIn('maximum =', content)
|
|
|
|
self.assertIn('<8> = 8;', content)
|
|
|
|
self.assertIn('<255> = 255;', content)
|
|
|
|
# this is stuff that should only be found in symbol files
|
|
|
|
self.assertNotIn('include', content)
|
|
|
|
self.assertNotIn('name', content)
|
|
|
|
# to make sure they are used together with format, which
|
|
|
|
# changes {{ to {.
|
|
|
|
self.assertNotIn('{{', content)
|
|
|
|
self.assertNotIn('}}', content)
|
2020-11-15 14:47:22 +00:00
|
|
|
self.assertIn('{', content)
|
|
|
|
self.assertIn('}', content)
|
2020-11-15 11:11:35 +00:00
|
|
|
|
2020-11-08 15:27:26 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|