more tests

This commit is contained in:
sezanzeb 2020-12-06 15:11:13 +01:00 committed by sezanzeb
parent 66a78bb3fb
commit 9a60ae17ff
3 changed files with 48 additions and 59 deletions

View File

@ -38,7 +38,7 @@ from keymapper.dev.reader import keycode_reader
from keymapper.daemon import get_dbus_interface
from keymapper.config import config
from keymapper.dev.macros import is_this_a_macro, parse
from keymapper.dev.permissions import can_read_devices
from keymapper.dev import permissions
def gtk_iteration():
@ -110,14 +110,13 @@ class Window:
# already visible (without content) to make it look more responsive.
gtk_iteration()
permission_errors = can_read_devices()
permission_errors = permissions.can_read_devices()
if len(permission_errors) > 0:
# TODO test
cmd = 'sudo key-mapper-service --setup-permissions'
permission_errors = [(
'You can also try `sudo key-mapper-service --setup-permiss'
'ions` to setup all needed permissions for you. All '
'commands mentioned here are also printed to the console '
'for you to copy.'
f'You can also try `{cmd}` to setup all needed permissions '
f'for you. All commands mentioned here are also printed to '
f'the console for you to copy.'
)] + permission_errors
self.show_status(
CTX_ERROR,
@ -294,7 +293,6 @@ class Window:
def check_macro_syntax(self):
"""Check if the programmed macros are allright."""
# TODO test
for (ev_type, keycode), output in custom_mapping:
if not is_this_a_macro(output):
continue

View File

@ -37,10 +37,11 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from keymapper.state import custom_mapping, system_mapping
from keymapper.paths import CONFIG, get_config_path, USER
from keymapper.paths import CONFIG, get_config_path
from keymapper.config import config
from keymapper.dev.reader import keycode_reader
from keymapper.gtk.row import to_string
from keymapper.dev import permissions
from tests.test import tmp, pending_events, InputEvent, uinput_write_history_pipe, \
clear_write_history
@ -382,6 +383,22 @@ class TestIntegration(unittest.TestCase):
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/asdf.json'))
self.assertEqual(custom_mapping.get_character(EV_KEY, 14), 'b')
def test_check_macro_syntax(self):
status = self.window.get('status_bar')
custom_mapping.change((EV_KEY, 9), 'k(1))', (None, None))
self.window.on_save_preset_clicked(None)
tooltip = status.get_tooltip_text().lower()
self.assertIn('brackets', tooltip)
custom_mapping.change((EV_KEY, 9), 'k(1)', (None, None))
self.window.on_save_preset_clicked(None)
tooltip = status.get_tooltip_text().lower()
self.assertNotIn('brackets', tooltip)
self.assertIn('saved', tooltip)
self.assertEqual(custom_mapping.get_character(EV_KEY, 9), 'k(1)')
def test_select_device_and_preset(self):
# created on start because the first device is selected and some empty
# preset prepared.
@ -505,76 +522,48 @@ class TestIntegration(unittest.TestCase):
original_access = os.access
original_getgrnam = grp.getgrnam
original_can_read_devices = permissions.can_read_devices
class TestPermissions(unittest.TestCase):
def tearDown(self):
os.access = original_access
os.getgrnam = original_getgrnam
permissions.can_read_devices = original_can_read_devices
if self.window is not None:
self.window.on_close()
self.window.window.destroy()
gtk_iteration()
self.window = None
self.window.on_close()
self.window.window.destroy()
gtk_iteration()
shutil.rmtree('/tmp/key-mapper-test')
def test_check_groups_missing(self):
# TODO modify test
class Grnam:
def __init__(self, group):
self.gr_mem = []
def test_fails(self):
def fake():
return ['error1', 'error2', 'error3']
grp.getgrnam = Grnam
permissions.can_read_devices = fake
self.window = launch()
status = self.window.get('status_bar')
labels = ''
for label in status.get_message_area():
labels += label.get_text()
self.assertIn('input', labels)
self.assertIn('plugdev', labels)
tooltip = status.get_tooltip_text()
self.assertIn('sudo key-mapper-service --setup-permissions', tooltip)
self.assertIn('error1', tooltip)
self.assertIn('error2', tooltip)
self.assertIn('error3', tooltip)
def test_check_plugdev_missing(self):
# TODO modify test
class Grnam:
def __init__(self, group):
if group == 'input':
self.gr_mem = [USER]
else:
self.gr_mem = []
def test_good(self):
def fake():
return []
grp.getgrnam = Grnam
permissions.can_read_devices = fake
self.window = launch()
status = self.window.get('status_bar')
labels = ''
for label in status.get_message_area():
labels += label.get_text()
self.assertNotIn('input', labels)
self.assertIn('plugdev', labels)
def test_check_write_uinput(self):
# TODO modify test
class Grnam:
def __init__(self, group):
self.gr_mem = [USER]
grp.getgrnam = Grnam
def access(path, mode):
return False
os.access = access
self.window = launch()
status = self.window.get('status_bar')
labels = ''
for label in status.get_message_area():
labels += label.get_text()
self.assertNotIn('plugdev', labels)
self.assertIn('Insufficient permissions on /dev/uinput', labels)
self.assertIsNone(status.get_tooltip_text())
if __name__ == "__main__":

View File

@ -47,6 +47,8 @@ class TestKeycodeMapper(unittest.TestCase):
for key in keys:
del active_macros[key]
system_mapping.populate()
def test_should_map_event_as_btn(self):
self.assertTrue(should_map_event_as_btn(EV_ABS, ABS_HAT0X))
self.assertTrue(should_map_event_as_btn(EV_KEY, KEY_A))