removed stuff from tests that was removed

pull/14/head
sezanzeb 4 years ago
parent f18b013513
commit 3d3136ed8b

@ -1,4 +0,0 @@
default
xkb_symbols "basic" {
name[Group1]="key-mapper/empty";
};

@ -1,7 +0,0 @@
// keycodes configuration for key-mapper presets
default xkb_keycodes "basic" {{
minimum = {minimum};
maximum = {maximum};
{xkb_keycodes}
}};

@ -1,9 +0,0 @@
// key-mapper symbols config file.
// the corresponding keycodes configuration is in
// /usr/share/X11/xkb/keycodes/key-mapper
default xkb_symbols "basic" {{
name[Group1] = "{name}";
{xkb_symbols}
}};

@ -103,14 +103,30 @@ class KeycodeInjector:
device = evdev.InputDevice(path)
try:
# grab to avoid e.g. the disabled keycode of 10 to confuse X,
# especially when one of the buttons of your mouse also uses 10
# especially when one of the buttons of your mouse also uses 10.
# This also avoids having to load an empty xkb symbols file
# to prevent writing any unwanted keys.
device.grab()
except IOError:
logger.error('Cannot grab %s', path)
# copy the capabilities because the keymapper_device is going
# to act like the mouse
keymapper_device = evdev.UInput.from_device(device)
# to act like the device.
capabilities = device.capabilities(absinfo=False)
# However, make sure that it supports all keycodes, not just some
# random ones. That's why I avoid from_device for this
capabilities[evdev.ecodes.EV_KEY] = evdev.ecodes.keys.keys()
# just like what python-evdev does in from_device
if evdev.ecodes.EV_SYN in capabilities:
del capabilities[evdev.ecodes.EV_SYN]
if evdev.ecodes.EV_FF in capabilities:
del capabilities[evdev.ecodes.EV_FF]
keymapper_device = evdev.UInput(
name=f'key-mapper {device.name}',
events=capabilities
)
pipe.send(DEVICE_CREATED)
@ -141,7 +157,7 @@ class KeycodeInjector:
if character is None:
# unknown keycode, forward it
continue
target_keycode = input_keycode
else:
target_keycode = system_mapping.get_keycode(character)
if target_keycode is None:

@ -64,7 +64,7 @@ def get_presets(device):
]
# the highest timestamp to the front
presets.reverse()
return [preset.replace('_', ' ') for preset in presets]
return presets
def get_any_preset():
@ -72,10 +72,10 @@ def get_any_preset():
devices = get_devices().keys()
if len(devices) == 0:
return None, None
any_device = list(devices)[0].replace('_', ' ')
any_device = list(devices)[0]
any_preset = (get_presets(any_device) or [None])[0]
if any_preset is not None:
any_preset = any_preset.replace('_', ' ')
any_preset = any_preset
return any_device, any_preset
@ -105,10 +105,7 @@ def find_newest_preset(device=None):
logger.debug('No presets found')
return get_any_preset()
online_devices = [
device.replace(' ', '_')
for device in get_devices().keys()
]
online_devices = get_devices().keys()
newest_path = None
while len(paths) > 0:
@ -124,10 +121,6 @@ def find_newest_preset(device=None):
logger.debug('None of the configured devices is currently online')
return get_any_preset()
# ui: no underscores, filesystem: no whitespaces
device = device and device.replace('_', ' ')
preset = preset and preset.replace('_', ' ')
logger.debug('The newest preset is "%s", "%s"', device, preset)
return device, preset

@ -29,8 +29,6 @@ DistUtilsExtra.auto.setup(
license='GPL-3.0',
data_files=[
('share/applications/', ['data/key-mapper.desktop']),
('share/key-mapper/', ['data/xkb_symbols_template']),
('share/polkit-1/actions/', ['data/org.key-mapper.policy']),
('share/X11/xkb/symbols/', ['data/key-mapper-empty']),
],
)

@ -34,15 +34,12 @@ def patch_paths():
from keymapper import paths
prefix = '/tmp/key-mapper-test/X11/'
paths.X11_SYMBOLS = prefix + 'symbols'
paths.USERS_SYMBOLS = prefix + 'symbols/key-mapper/user'
paths.CONFIG = prefix + 'symbols/key-mapper/user'
paths.DEFAULT_SYMBOLS = prefix + 'symbols/key-mapper/user/default'
paths.KEYCODES_PATH = prefix + 'keycodes/key-mapper'
def patch_linux():
from keymapper import linux
linux.KeycodeInjector.start_reading = lambda *args: None
linux.KeycodeInjector.read = lambda *args: None
# TODO patch for the injector or something idk?
def patch_evdev():
@ -113,7 +110,6 @@ def patch_unsaved():
# the original versions
patch_paths()
patch_evdev()
patch_linux()
patch_unsaved()

@ -1,39 +0,0 @@
#!/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 re
import unittest
from keymapper.cli import get_system_layout_locale
class Test(unittest.TestCase):
def test_get_system_layout_locale(self):
layout = get_system_layout_locale()
self.assertGreater(len(layout), 0)
# should be all alphanumeric
match = re.findall(r'\w+', layout)
self.assertEqual(len(match), 1)
self.assertEqual(match[0], layout)
if __name__ == "__main__":
unittest.main()

@ -1,144 +0,0 @@
#!/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
from keymapper.archive.xkb import custom_mapping, generate_symbols, \
create_identity_mapping, create_setxkbmap_config, \
get_preset_name, create_default_symbols, parse_symbols_file, \
get_usr_path, KEYCODES_PATH, USERS_SYMBOLS
from test import tmp
class TestConfig(unittest.TestCase):
def setUp(self):
custom_mapping.empty()
custom_mapping.change(None, 10, 'a')
custom_mapping.change(None, 11, 'KP_1')
custom_mapping.change(None, 12, 3)
custom_mapping.change(None, 13, ['a', 'A', 'NoSymbol', 3])
self.assertEqual(custom_mapping.get_character(12), '3')
self.assertEqual(custom_mapping.get_character(13), 'a, A, NoSymbol, 3')
if os.path.exists(tmp):
shutil.rmtree(tmp)
def test_create_setxkbmap_config(self):
create_setxkbmap_config('device a', 'preset b')
self.assertTrue(os.path.exists(os.path.join(
USERS_SYMBOLS,
'device_a',
'preset_b'
)))
self.assertTrue(os.path.exists(KEYCODES_PATH))
with open(get_usr_path('device_a', 'preset_b'), 'r') as f:
content = f.read()
self.assertIn('key <10> { [ a ] };', content)
self.assertIn('key <11> { [ KP_1 ] };', content)
self.assertIn('key <12> { [ 3 ] };', content)
self.assertIn('key <13> { [ a, A, NoSymbol, 3 ] };', content)
self.assertIn('include "key-mapper/user/default"', content)
self.assertIn(get_preset_name('device_a', 'preset_b'), content)
# it should be loaded correctly after saving
parse_symbols_file('device_a', 'preset_b')
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))
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')))
with open(get_usr_path('default'), 'r') as f:
content = f.read()
self.assertNotIn('include', content)
# 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)
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')
def test_generate_content(self):
self.assertRaises(
FileNotFoundError,
generate_symbols,
'device', 'preset'
)
# create the identity mapping, because it is required for
# generate_symbols
create_identity_mapping()
content = generate_symbols('device/preset')
self.assertIn('key <10> { [ a ] };', content)
self.assertIn('key <11> { [ KP_1 ] };', content)
self.assertIn('key <12> { [ 3 ] };', content)
self.assertIn('key <13> { [ a, A, NoSymbol, 3 ] };', content)
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)
self.assertIn('{', content)
self.assertIn('}', content)
if __name__ == "__main__":
unittest.main()

@ -79,6 +79,7 @@ class Integration(unittest.TestCase):
def setUp(self):
if os.path.exists(tmp):
shutil.rmtree(tmp)
custom_mapping.empty()
self.window = launch()
def tearDown(self):
@ -188,7 +189,7 @@ class Integration(unittest.TestCase):
self.window.get('preset_name_input').set_text('asdf')
self.window.on_save_preset_clicked(None)
self.assertEqual(self.window.selected_preset, 'asdf')
self.assertTrue(os.path.exists(f'{CONFIG}/device_1/asdf'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/asdf'))
self.assertEqual(custom_mapping.get_character(14), 'b')
def test_select_device_and_preset(self):
@ -204,15 +205,15 @@ class Integration(unittest.TestCase):
# created on start because the first device is selected and some empty
# preset prepared.
self.assertTrue(os.path.exists(f'{CONFIG}/device_1/new_preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
self.assertEqual(self.window.selected_device, 'device 1')
self.assertEqual(self.window.selected_preset, 'new preset')
# create another one
self.window.on_create_preset_clicked(None)
gtk_iteration()
self.assertTrue(os.path.exists(f'{CONFIG}/device_1/new_preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device_1/new_preset_2'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset 2'))
self.assertEqual(self.window.selected_preset, 'new preset 2')
self.window.on_select_preset(FakeDropdown('new preset'))
@ -220,27 +221,27 @@ class Integration(unittest.TestCase):
self.assertEqual(self.window.selected_preset, 'new preset')
self.assertListEqual(
sorted(os.listdir(f'{CONFIG}/device_1')),
['new_preset', 'new_preset_2']
sorted(os.listdir(f'{CONFIG}/device 1')),
['new preset', 'new preset 2']
)
# now try to change the name
self.window.get('preset_name_input').set_text('abc 123')
gtk_iteration()
self.assertEqual(self.window.selected_preset, 'new preset')
self.assertFalse(os.path.exists(f'{CONFIG}/device_1/abc_123'))
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/abc 123'))
custom_mapping.change(None, 10, '1')
self.window.on_save_preset_clicked(None)
gtk_iteration()
self.assertEqual(self.window.selected_preset, 'abc 123')
self.assertTrue(os.path.exists(f'{CONFIG}/device_1/abc_123'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/abc 123'))
self.assertListEqual(
sorted(os.listdir(CONFIG)),
['default', 'device_1']
['device 1']
)
self.assertListEqual(
sorted(os.listdir(f'{CONFIG}/device_1')),
['abc_123', 'new_preset_2']
sorted(os.listdir(f'{CONFIG}/device 1')),
['abc 123', 'new preset 2']
)

@ -21,7 +21,7 @@
import unittest
from keymapper.injector import GetDevicesProcess
from keymapper.getdevices import _GetDevicesProcess
class TestLinux(unittest.TestCase):
@ -35,7 +35,7 @@ class TestLinux(unittest.TestCase):
# don't actually start the process, just use the `run` function.
# otherwise the coverage tool can't keep track.
pipe = FakePipe()
GetDevicesProcess(pipe).run()
_GetDevicesProcess(pipe).run()
self.assertDictEqual(pipe.devices, {
'device 1': {
'paths': [

@ -35,56 +35,45 @@ class TestMapping(unittest.TestCase):
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(2), 258)
self.assertEqual(self.mapping.get_keycode('a'), 2)
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')
self.assertEqual(self.mapping.get_keycode(3), 259)
self.assertEqual(self.mapping.get_keycode('b'), 3)
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')
self.assertEqual(self.mapping.get_keycode(4), 260)
self.assertEqual(self.mapping.get_keycode('c'), 4)
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')
self.assertEqual(self.mapping.get_keycode(4), 260)
self.assertEqual(self.mapping.get_keycode('d'), 4)
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')
self.assertEqual(self.mapping.get_keycode(4), 260)
self.assertEqual(self.mapping.get_keycode('e'), 4)
self.assertEqual(len(self.mapping), 2)
# and this
self.mapping.change('4', '4', 'f')
self.assertEqual(self.mapping.get_character(4), 'f')
self.assertEqual(self.mapping.get_keycode(4), 260)
self.assertEqual(self.mapping.get_keycode('f'), 4)
self.assertEqual(len(self.mapping), 2)
# non-int keycodes are ignored
self.mapping.change('a', 'b', 'c')
self.assertEqual(len(self.mapping), 2)
def test_change_multiple(self):
self.mapping.change(None, 1, ['a', 'A'])
self.assertEqual(self.mapping.get_character(1), 'a, A')
self.assertEqual(self.mapping.get_keycode(1), 257)
self.mapping.change(1, 2, ['b', 'B'])
self.assertEqual(self.mapping.get_character(2), 'b, B')
self.assertEqual(self.mapping.get_keycode(2), 258)
self.assertIsNone(self.mapping.get_character(1))
self.assertIsNone(self.mapping.get_keycode(1))
def test_clear(self):
# does nothing
self.mapping.clear(40)
@ -103,15 +92,6 @@ class TestMapping(unittest.TestCase):
self.assertIsNone(self.mapping.get_character(20))
self.assertEqual(self.mapping.get_character(30), 'KP_3')
def test_iterate_and_convert_to_string(self):
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'))]
)
if __name__ == "__main__":
unittest.main()

@ -25,12 +25,19 @@ import shutil
import time
from keymapper.presets import find_newest_preset, rename_preset, \
get_any_preset, delete_preset
from keymapper.archive.xkb import create_preset, USERS_SYMBOLS
get_any_preset, delete_preset, get_available_preset_name
from keymapper.paths import CONFIG
from keymapper.state import custom_mapping
from test import tmp
def create_preset(device, name='new preset'):
name = get_available_preset_name(device, name)
custom_mapping.empty()
custom_mapping.save(device, name)
class TestCreatePreset(unittest.TestCase):
def setUp(self):
if os.path.exists(tmp):
@ -40,21 +47,21 @@ class TestCreatePreset(unittest.TestCase):
self.assertEqual(get_any_preset(), ('device 1', None))
create_preset('device 1')
self.assertEqual(get_any_preset(), ('device 1', 'new preset'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
def test_create_preset_2(self):
create_preset('device 1')
create_preset('device 1')
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset_2'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset 2'))
def test_create_preset_3(self):
create_preset('device 1', 'pre set')
create_preset('device 1', 'pre set')
create_preset('device 1', 'pre set')
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/pre_set'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/pre_set_2'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/pre_set_3'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/pre set'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/pre set 2'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/pre set 3'))
class TestDeletePreset(unittest.TestCase):
@ -65,15 +72,15 @@ class TestDeletePreset(unittest.TestCase):
def test_delete_preset(self):
create_preset('device 1')
create_preset('device 1')
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
delete_preset('device 1', 'new preset')
self.assertFalse(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1'))
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/new preset'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1'))
delete_preset('device 1', 'new preset 2')
self.assertFalse(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset'))
self.assertFalse(os.path.exists(f'{USERS_SYMBOLS}/device_1/new_preset_2'))
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/new preset'))
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/new preset 2'))
# if no preset in the directory, remove the directory
self.assertFalse(os.path.exists(f'{USERS_SYMBOLS}/device_1'))
self.assertFalse(os.path.exists(f'{CONFIG}/device 1'))
class TestRenamePreset(unittest.TestCase):
@ -87,10 +94,10 @@ class TestRenamePreset(unittest.TestCase):
create_preset('device 1', 'foobar')
rename_preset('device 1', 'preset 1', 'foobar')
rename_preset('device 1', 'preset 2', 'foobar')
self.assertFalse(os.path.exists(f'{USERS_SYMBOLS}/device_1/preset_1'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/foobar'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/foobar_2'))
self.assertTrue(os.path.exists(f'{USERS_SYMBOLS}/device_1/foobar_3'))
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/preset 1'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/foobar'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/foobar 2'))
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/foobar 3'))
class TestFindPresets(unittest.TestCase):
@ -105,14 +112,14 @@ class TestFindPresets(unittest.TestCase):
self.assertEqual(find_newest_preset(), ('device 2', 'preset 2'))
def test_find_newest_preset_2(self):
os.makedirs(f'{USERS_SYMBOLS}/device_1')
os.makedirs(f'{CONFIG}/device 1')
time.sleep(0.01)
os.makedirs(f'{USERS_SYMBOLS}/device_2')
os.makedirs(f'{CONFIG}/device_2')
# takes the first one that the test-fake returns
self.assertEqual(find_newest_preset(), ('device 1', None))
def test_find_newest_preset_3(self):
os.makedirs(f'{USERS_SYMBOLS}/device_1')
os.makedirs(f'{CONFIG}/device 1')
self.assertEqual(find_newest_preset(), ('device 1', None))
def test_find_newest_preset_4(self):

@ -22,19 +22,12 @@
import unittest
from keymapper.getdevices import get_devices
from keymapper.archive.xkb import USERS_SYMBOLS, X11_SYMBOLS, \
DEFAULT_SYMBOLS
class TestTest(unittest.TestCase):
def test_stubs(self):
self.assertIn('device 1', get_devices())
def test_paths(self):
# stuff in /usr
self.assertTrue(USERS_SYMBOLS.startswith(X11_SYMBOLS))
self.assertTrue(DEFAULT_SYMBOLS.startswith(X11_SYMBOLS))
if __name__ == "__main__":
unittest.main()

Loading…
Cancel
Save