2020-10-31 16:00:02 +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 sys
|
2020-11-07 23:54:19 +00:00
|
|
|
import time
|
2020-12-04 16:16:39 +00:00
|
|
|
import grp
|
2020-11-07 23:54:19 +00:00
|
|
|
import os
|
2020-10-31 16:00:02 +00:00
|
|
|
import unittest
|
2020-11-22 12:59:28 +00:00
|
|
|
import evdev
|
2020-12-02 18:33:31 +00:00
|
|
|
from evdev.events import EV_KEY
|
2020-11-30 13:34:27 +00:00
|
|
|
import json
|
2020-10-31 16:00:02 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
from importlib.util import spec_from_loader, module_from_spec
|
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
|
|
|
|
import gi
|
2020-11-02 01:17:58 +00:00
|
|
|
import shutil
|
2020-10-31 16:00:02 +00:00
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
2020-12-04 13:38:41 +00:00
|
|
|
from keymapper.state import custom_mapping, system_mapping
|
2020-12-06 14:11:13 +00:00
|
|
|
from keymapper.paths import CONFIG, get_config_path
|
2020-11-26 23:23:03 +00:00
|
|
|
from keymapper.config import config
|
2020-11-30 17:59:34 +00:00
|
|
|
from keymapper.dev.reader import keycode_reader
|
2020-12-03 19:03:53 +00:00
|
|
|
from keymapper.gtk.row import to_string
|
2020-12-06 14:11:13 +00:00
|
|
|
from keymapper.dev import permissions
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-12-06 13:23:00 +00:00
|
|
|
from tests.test import tmp, pending_events, InputEvent, uinput_write_history_pipe, \
|
2020-11-28 23:28:46 +00:00
|
|
|
clear_write_history
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2020-10-31 16:00:02 +00:00
|
|
|
|
|
|
|
def gtk_iteration():
|
|
|
|
"""Iterate while events are pending."""
|
|
|
|
while Gtk.events_pending():
|
|
|
|
Gtk.main_iteration()
|
|
|
|
|
|
|
|
|
2020-12-04 16:16:39 +00:00
|
|
|
# iterate a few times when Gtk.main() is called, but don't block
|
|
|
|
# there and just continue to the tests while the UI becomes
|
|
|
|
# unresponsive
|
|
|
|
Gtk.main = gtk_iteration
|
|
|
|
|
|
|
|
# doesn't do much except avoid some Gtk assertion error, whatever:
|
|
|
|
Gtk.main_quit = lambda: None
|
|
|
|
|
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
def launch(argv=None):
|
2020-11-08 20:34:27 +00:00
|
|
|
"""Start key-mapper-gtk with the command line argument array argv."""
|
2020-12-04 16:16:39 +00:00
|
|
|
if os.path.exists(tmp):
|
|
|
|
shutil.rmtree(tmp)
|
|
|
|
custom_mapping.empty()
|
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
bin_path = os.path.join(os.getcwd(), 'bin', 'key-mapper-gtk')
|
|
|
|
|
2020-10-31 16:00:02 +00:00
|
|
|
if not argv:
|
|
|
|
argv = ['-d']
|
|
|
|
|
|
|
|
with patch.object(sys, 'argv', [''] + [str(arg) for arg in argv]):
|
|
|
|
loader = SourceFileLoader('__main__', bin_path)
|
|
|
|
spec = spec_from_loader('__main__', loader)
|
|
|
|
module = module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
|
|
gtk_iteration()
|
|
|
|
|
|
|
|
return module.window
|
|
|
|
|
|
|
|
|
2020-11-26 23:23:03 +00:00
|
|
|
class FakeDropdown(Gtk.ComboBoxText):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def get_active_text(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_active_id(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2020-11-29 15:21:34 +00:00
|
|
|
class TestIntegration(unittest.TestCase):
|
2020-10-31 16:00:02 +00:00
|
|
|
"""For tests that use the window.
|
|
|
|
|
2020-11-08 20:34:27 +00:00
|
|
|
Try to modify the configuration only by calling functions of the window.
|
2020-10-31 16:00:02 +00:00
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
self.window = launch()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2020-11-22 20:02:16 +00:00
|
|
|
self.window.on_apply_system_layout_clicked(None)
|
2020-11-15 00:35:35 +00:00
|
|
|
gtk_iteration()
|
2020-10-31 16:00:02 +00:00
|
|
|
self.window.on_close()
|
|
|
|
self.window.window.destroy()
|
|
|
|
gtk_iteration()
|
2020-11-02 01:17:58 +00:00
|
|
|
shutil.rmtree('/tmp/key-mapper-test')
|
2020-11-28 23:28:46 +00:00
|
|
|
clear_write_history()
|
2020-12-04 13:38:41 +00:00
|
|
|
system_mapping.populate()
|
2020-10-31 16:00:02 +00:00
|
|
|
|
2020-11-15 00:35:35 +00:00
|
|
|
def get_rows(self):
|
|
|
|
return self.window.get('key_list').get_children()
|
|
|
|
|
2020-12-02 17:07:46 +00:00
|
|
|
def test_show_device_mapping_status(self):
|
|
|
|
# this function may not return True, otherwise the timeout
|
|
|
|
# runs forever
|
|
|
|
self.assertFalse(self.window.show_device_mapping_status())
|
|
|
|
|
2020-11-26 23:23:03 +00:00
|
|
|
def test_autoload(self):
|
|
|
|
self.window.on_preset_autoload_switch_activate(None, False)
|
|
|
|
self.assertFalse(config.is_autoloaded(
|
|
|
|
self.window.selected_device,
|
|
|
|
self.window.selected_preset
|
|
|
|
))
|
|
|
|
|
|
|
|
# select a preset for the first device
|
|
|
|
self.window.on_select_device(FakeDropdown('device 1'))
|
|
|
|
self.window.on_preset_autoload_switch_activate(None, True)
|
|
|
|
self.assertTrue(config.is_autoloaded('device 1', 'new preset'))
|
|
|
|
self.assertFalse(config.is_autoloaded('device 2', 'new preset'))
|
|
|
|
self.assertListEqual(
|
|
|
|
list(config.iterate_autoload_presets()),
|
|
|
|
[('device 1', 'new preset')]
|
|
|
|
)
|
|
|
|
|
|
|
|
# select a preset for the second device
|
|
|
|
self.window.on_select_device(FakeDropdown('device 2'))
|
|
|
|
self.window.on_preset_autoload_switch_activate(None, True)
|
|
|
|
self.assertTrue(config.is_autoloaded('device 1', 'new preset'))
|
|
|
|
self.assertTrue(config.is_autoloaded('device 2', 'new preset'))
|
|
|
|
self.assertListEqual(
|
|
|
|
list(config.iterate_autoload_presets()),
|
|
|
|
[('device 1', 'new preset'), ('device 2', 'new preset')]
|
|
|
|
)
|
|
|
|
|
|
|
|
# disable autoloading for the second device
|
|
|
|
self.window.on_preset_autoload_switch_activate(None, False)
|
|
|
|
self.assertTrue(config.is_autoloaded('device 1', 'new preset'))
|
|
|
|
self.assertFalse(config.is_autoloaded('device 2', 'new preset'))
|
|
|
|
self.assertListEqual(
|
|
|
|
list(config.iterate_autoload_presets()),
|
|
|
|
[('device 1', 'new preset')]
|
|
|
|
)
|
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
def test_select_device(self):
|
|
|
|
# creates a new empty preset when no preset exists for the device
|
|
|
|
self.window.on_select_device(FakeDropdown('device 1'))
|
2020-12-02 22:27:32 +00:00
|
|
|
custom_mapping.change((EV_KEY, 50), 'q')
|
|
|
|
custom_mapping.change((EV_KEY, 51), 'u')
|
|
|
|
custom_mapping.change((EV_KEY, 52), 'x')
|
2020-11-30 13:34:27 +00:00
|
|
|
self.assertEqual(len(custom_mapping), 3)
|
|
|
|
self.window.on_select_device(FakeDropdown('device 2'))
|
|
|
|
self.assertEqual(len(custom_mapping), 0)
|
|
|
|
# it creates the file for that right away. It may have been possible
|
|
|
|
# to write it such that it doesn't (its empty anyway), but it does,
|
|
|
|
# so use that to test it in more detail.
|
|
|
|
path = get_config_path('device 2', 'new preset')
|
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
with open(path, 'r') as file:
|
|
|
|
preset = json.load(file)
|
|
|
|
self.assertEqual(len(preset['mapping']), 0)
|
|
|
|
|
2020-10-31 16:00:02 +00:00
|
|
|
def test_can_start(self):
|
|
|
|
self.assertIsNotNone(self.window)
|
|
|
|
self.assertTrue(self.window.window.get_visible())
|
|
|
|
|
2020-12-03 19:03:53 +00:00
|
|
|
def test_row_keycode_to_string(self):
|
|
|
|
# not an integration test, but I have all the row tests here already
|
|
|
|
self.assertEqual(to_string(EV_KEY, 10), '9')
|
|
|
|
self.assertEqual(to_string(EV_KEY, 39), 'SEMICOLON')
|
|
|
|
|
2020-11-29 15:21:34 +00:00
|
|
|
def test_row_simple(self):
|
|
|
|
rows = self.window.get('key_list').get_children()
|
|
|
|
self.assertEqual(len(rows), 1)
|
|
|
|
|
|
|
|
row = rows[0]
|
|
|
|
|
2020-12-02 19:08:55 +00:00
|
|
|
row.set_new_keycode(None, None)
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertIsNone(row.get_keycode())
|
|
|
|
self.assertEqual(len(custom_mapping), 0)
|
2020-12-03 19:03:53 +00:00
|
|
|
self.assertEqual(row.keycode_input.get_label(), None)
|
|
|
|
|
2020-12-02 19:08:55 +00:00
|
|
|
row.set_new_keycode(EV_KEY, 30)
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(len(custom_mapping), 0)
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(row.get_keycode(), (EV_KEY, 30))
|
2020-12-03 19:03:53 +00:00
|
|
|
# this is KEY_A in linux/input-event-codes.h,
|
|
|
|
# but KEY_ is removed from the text
|
|
|
|
self.assertEqual(row.keycode_input.get_label(), 'A')
|
|
|
|
|
2020-12-02 19:08:55 +00:00
|
|
|
row.set_new_keycode(EV_KEY, 30)
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(len(custom_mapping), 0)
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(row.get_keycode(), (EV_KEY, 30))
|
2020-11-29 15:21:34 +00:00
|
|
|
|
2020-12-01 23:13:54 +00:00
|
|
|
time.sleep(0.1)
|
2020-11-29 15:21:34 +00:00
|
|
|
gtk_iteration()
|
|
|
|
self.assertEqual(len(self.window.get('key_list').get_children()), 1)
|
|
|
|
|
|
|
|
row.character_input.set_text('Shift_L')
|
|
|
|
self.assertEqual(len(custom_mapping), 1)
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-12-01 23:13:54 +00:00
|
|
|
time.sleep(0.1)
|
2020-11-08 17:51:35 +00:00
|
|
|
gtk_iteration()
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(len(self.window.get('key_list').get_children()), 2)
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 30), 'Shift_L')
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(row.get_character(), 'Shift_L')
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(row.get_keycode(), (EV_KEY, 30))
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-11-29 15:21:34 +00:00
|
|
|
def change_empty_row(self, code, char, code_first=True, success=True):
|
2020-12-03 19:03:53 +00:00
|
|
|
"""Modify the one empty row that always exists.
|
|
|
|
|
|
|
|
Utility function for other tests.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
code_first : boolean
|
|
|
|
If True, the code is entered and then the character.
|
|
|
|
If False, the character is entered first.
|
|
|
|
success : boolean
|
|
|
|
If this change on the empty row is going to result in a change
|
|
|
|
in the mapping eventually. False if this change is going to
|
|
|
|
cause a duplicate.
|
|
|
|
"""
|
2020-11-22 20:02:16 +00:00
|
|
|
# wait for the window to create a new empty row if needed
|
2020-12-01 23:13:54 +00:00
|
|
|
time.sleep(0.1)
|
2020-11-22 20:02:16 +00:00
|
|
|
gtk_iteration()
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-22 20:02:16 +00:00
|
|
|
# find the empty row
|
|
|
|
rows = self.get_rows()
|
|
|
|
row = rows[-1]
|
|
|
|
self.assertNotIn('changed', row.get_style_context().list_classes())
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertIsNone(row.keycode_input.get_label())
|
2020-11-22 20:02:16 +00:00
|
|
|
self.assertEqual(row.character_input.get_text(), '')
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-29 15:21:34 +00:00
|
|
|
if char and not code_first:
|
|
|
|
# set the character to make the new row complete
|
|
|
|
self.assertIsNone(row.get_character())
|
|
|
|
row.character_input.set_text(char)
|
|
|
|
self.assertEqual(row.get_character(), char)
|
|
|
|
|
|
|
|
self.window.window.set_focus(row.keycode_input)
|
|
|
|
|
|
|
|
if code:
|
|
|
|
# modifies the keycode in the row not by writing into the input,
|
|
|
|
# but by sending an event
|
2020-12-02 19:08:55 +00:00
|
|
|
keycode_reader._pipe[1].send((EV_KEY, code))
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
gtk_iteration()
|
2020-11-29 15:21:34 +00:00
|
|
|
if success:
|
2020-12-02 19:08:55 +00:00
|
|
|
self.assertEqual(row.get_keycode(), (EV_KEY, code))
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertIn(
|
|
|
|
'changed',
|
|
|
|
row.get_style_context().list_classes()
|
|
|
|
)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
self.assertIsNone(row.get_keycode())
|
|
|
|
self.assertIsNone(row.get_character())
|
|
|
|
self.assertNotIn('changed', row.get_style_context().list_classes())
|
|
|
|
|
|
|
|
if char and code_first:
|
|
|
|
# set the character to make the new row complete
|
|
|
|
self.assertIsNone(row.get_character())
|
|
|
|
row.character_input.set_text(char)
|
|
|
|
self.assertEqual(row.get_character(), char)
|
2020-11-22 20:02:16 +00:00
|
|
|
|
|
|
|
return row
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-22 20:02:16 +00:00
|
|
|
def test_rows(self):
|
|
|
|
"""Comprehensive test for rows."""
|
2020-11-29 15:21:34 +00:00
|
|
|
# how many rows there should be in the end
|
|
|
|
num_rows_target = 3
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-15 02:01:11 +00:00
|
|
|
# add two rows by modifiying the one empty row that exists
|
2020-11-29 15:21:34 +00:00
|
|
|
self.change_empty_row(10, 'a', code_first=False)
|
|
|
|
self.change_empty_row(11, 'k(b).k(c)')
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-15 02:01:11 +00:00
|
|
|
# one empty row added automatically again
|
2020-12-01 23:13:54 +00:00
|
|
|
time.sleep(0.1)
|
2020-11-15 02:01:11 +00:00
|
|
|
gtk_iteration()
|
|
|
|
# sleep one more time because it's funny to watch the ui
|
|
|
|
# during the test, how rows turn blue and stuff
|
2020-12-01 23:13:54 +00:00
|
|
|
time.sleep(0.1)
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(len(self.get_rows()), num_rows_target)
|
2020-11-15 02:01:11 +00:00
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 10), 'a')
|
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 11), 'k(b).k(c)')
|
2020-11-15 02:01:11 +00:00
|
|
|
self.assertTrue(custom_mapping.changed)
|
|
|
|
|
|
|
|
self.window.on_save_preset_clicked(None)
|
|
|
|
for row in self.get_rows():
|
|
|
|
self.assertNotIn(
|
|
|
|
'changed',
|
|
|
|
row.get_style_context().list_classes()
|
|
|
|
)
|
|
|
|
self.assertFalse(custom_mapping.changed)
|
|
|
|
|
|
|
|
# now change the first row and it should turn blue,
|
|
|
|
# but the other should remain unhighlighted
|
|
|
|
row = self.get_rows()[0]
|
|
|
|
row.character_input.set_text('c')
|
|
|
|
self.assertIn('changed', row.get_style_context().list_classes())
|
|
|
|
for row in self.get_rows()[1:]:
|
|
|
|
self.assertNotIn(
|
|
|
|
'changed',
|
|
|
|
row.get_style_context().list_classes()
|
|
|
|
)
|
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 10), 'c')
|
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 11), 'k(b).k(c)')
|
2020-11-15 02:01:11 +00:00
|
|
|
self.assertTrue(custom_mapping.changed)
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-29 15:21:34 +00:00
|
|
|
# try to add a duplicate keycode, it should be ignored
|
|
|
|
self.change_empty_row(11, 'd', success=False)
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 11), 'k(b).k(c)')
|
2020-11-29 15:21:34 +00:00
|
|
|
# and the number of rows shouldn't change
|
|
|
|
self.assertEqual(len(self.get_rows()), num_rows_target)
|
|
|
|
|
|
|
|
def test_remove_row(self):
|
|
|
|
"""Comprehensive test for rows 2."""
|
|
|
|
# sleeps are added to be able to visually follow and debug the test
|
|
|
|
# add two rows by modifiying the one empty row that exists
|
|
|
|
row_1 = self.change_empty_row(10, 'a')
|
|
|
|
row_2 = self.change_empty_row(11, 'b')
|
|
|
|
row_3 = self.change_empty_row(None, 'c')
|
|
|
|
|
|
|
|
# no empty row added because one is unfinished
|
|
|
|
time.sleep(0.2)
|
|
|
|
gtk_iteration()
|
|
|
|
self.assertEqual(len(self.get_rows()), 3)
|
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 11), 'b')
|
2020-11-29 15:21:34 +00:00
|
|
|
|
|
|
|
def remove(row, code, char, num_rows_after):
|
|
|
|
if code is not None and char is not None:
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, code), char)
|
2020-12-02 19:08:55 +00:00
|
|
|
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(row.get_character(), char)
|
2020-12-02 19:08:55 +00:00
|
|
|
if code is None:
|
|
|
|
self.assertIsNone(row.get_keycode())
|
|
|
|
else:
|
|
|
|
self.assertEqual(row.get_keycode(), (EV_KEY, code))
|
2020-11-29 15:21:34 +00:00
|
|
|
row.on_delete_button_clicked()
|
|
|
|
time.sleep(0.2)
|
|
|
|
gtk_iteration()
|
|
|
|
self.assertIsNone(row.get_keycode())
|
|
|
|
self.assertIsNone(row.get_character())
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertIsNone(custom_mapping.get_character(EV_KEY, code))
|
2020-11-29 15:21:34 +00:00
|
|
|
self.assertEqual(len(self.get_rows()), num_rows_after)
|
|
|
|
|
|
|
|
remove(row_1, 10, 'a', 2)
|
|
|
|
remove(row_2, 11, 'b', 1)
|
|
|
|
# there is no empty row at the moment, so after removing that one,
|
|
|
|
# which is the only row, one empty row will be there. So the number
|
|
|
|
# of rows won't change.
|
|
|
|
remove(row_3, None, 'c', 1)
|
|
|
|
|
2020-11-08 17:51:35 +00:00
|
|
|
def test_rename_and_save(self):
|
2020-12-02 22:27:32 +00:00
|
|
|
custom_mapping.change((EV_KEY, 14), 'a', (None, None))
|
2020-11-08 17:51:35 +00:00
|
|
|
self.assertEqual(self.window.selected_preset, 'new preset')
|
|
|
|
self.window.on_save_preset_clicked(None)
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 14), 'a')
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-12-02 22:27:32 +00:00
|
|
|
custom_mapping.change((EV_KEY, 14), 'b', (None, None))
|
2020-11-08 17:51:35 +00:00
|
|
|
self.window.get('preset_name_input').set_text('asdf')
|
|
|
|
self.window.on_save_preset_clicked(None)
|
|
|
|
self.assertEqual(self.window.selected_preset, 'asdf')
|
2020-11-24 23:23:34 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/asdf.json'))
|
2020-12-02 18:33:31 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(EV_KEY, 14), 'b')
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
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)')
|
|
|
|
|
2020-10-31 17:48:03 +00:00
|
|
|
def test_select_device_and_preset(self):
|
2020-11-07 23:54:19 +00:00
|
|
|
# created on start because the first device is selected and some empty
|
|
|
|
# preset prepared.
|
2020-11-24 23:23:34 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset.json'))
|
2020-11-07 23:54:19 +00:00
|
|
|
self.assertEqual(self.window.selected_device, 'device 1')
|
|
|
|
self.assertEqual(self.window.selected_preset, 'new preset')
|
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
# create another one
|
2020-11-07 23:54:19 +00:00
|
|
|
self.window.on_create_preset_clicked(None)
|
|
|
|
gtk_iteration()
|
2020-11-24 23:23:34 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset.json'))
|
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset 2.json'))
|
2020-11-07 23:54:19 +00:00
|
|
|
self.assertEqual(self.window.selected_preset, 'new preset 2')
|
|
|
|
|
|
|
|
self.window.on_select_preset(FakeDropdown('new preset'))
|
|
|
|
gtk_iteration()
|
|
|
|
self.assertEqual(self.window.selected_preset, 'new preset')
|
|
|
|
|
2020-11-08 00:02:04 +00:00
|
|
|
self.assertListEqual(
|
2020-11-18 21:33:01 +00:00
|
|
|
sorted(os.listdir(f'{CONFIG}/device 1')),
|
2020-11-24 23:23:34 +00:00
|
|
|
sorted(['new preset.json', 'new preset 2.json'])
|
2020-11-08 00:02:04 +00:00
|
|
|
)
|
|
|
|
|
2020-11-07 23:54:19 +00:00
|
|
|
# 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')
|
2020-11-24 23:23:34 +00:00
|
|
|
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/abc 123.json'))
|
2020-12-02 22:27:32 +00:00
|
|
|
custom_mapping.change((EV_KEY, 10), '1', (None, None))
|
2020-11-07 23:54:19 +00:00
|
|
|
self.window.on_save_preset_clicked(None)
|
|
|
|
gtk_iteration()
|
|
|
|
self.assertEqual(self.window.selected_preset, 'abc 123')
|
2020-11-24 23:23:34 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/abc 123.json'))
|
2020-11-07 23:54:19 +00:00
|
|
|
self.assertListEqual(
|
2020-11-18 09:33:59 +00:00
|
|
|
sorted(os.listdir(CONFIG)),
|
2020-11-24 23:23:34 +00:00
|
|
|
sorted(['device 1'])
|
2020-11-07 23:54:19 +00:00
|
|
|
)
|
|
|
|
self.assertListEqual(
|
2020-11-18 21:33:01 +00:00
|
|
|
sorted(os.listdir(f'{CONFIG}/device 1')),
|
2020-11-24 23:23:34 +00:00
|
|
|
sorted(['abc 123.json', 'new preset 2.json'])
|
2020-11-07 23:54:19 +00:00
|
|
|
)
|
2020-10-31 17:48:03 +00:00
|
|
|
|
2020-11-22 20:02:16 +00:00
|
|
|
def test_start_injecting(self):
|
|
|
|
keycode_from = 9
|
2020-11-28 23:28:46 +00:00
|
|
|
keycode_to = 200
|
2020-11-22 20:02:16 +00:00
|
|
|
|
|
|
|
self.change_empty_row(keycode_from, 'a')
|
2020-12-04 13:38:41 +00:00
|
|
|
system_mapping.clear()
|
|
|
|
system_mapping._set('a', keycode_to)
|
2020-11-22 20:02:16 +00:00
|
|
|
|
|
|
|
pending_events['device 2'] = [
|
2020-12-06 13:23:00 +00:00
|
|
|
InputEvent(evdev.events.EV_KEY, keycode_from, 1),
|
|
|
|
InputEvent(evdev.events.EV_KEY, keycode_from, 0)
|
2020-11-22 20:02:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
custom_mapping.save('device 2', 'foo preset')
|
|
|
|
|
|
|
|
self.window.selected_device = 'device 2'
|
|
|
|
self.window.selected_preset = 'foo preset'
|
|
|
|
self.window.on_apply_preset_clicked(None)
|
|
|
|
|
|
|
|
# the integration tests will cause the injection to be started as
|
|
|
|
# processes, as intended. Luckily, recv will block until the events
|
|
|
|
# are handled and pushed.
|
|
|
|
|
|
|
|
# Note, that pushing events to pending_events won't work anymore
|
|
|
|
# from here on because the injector processes memory cannot be
|
|
|
|
# modified from here.
|
|
|
|
|
|
|
|
event = uinput_write_history_pipe[0].recv()
|
|
|
|
self.assertEqual(event.type, evdev.events.EV_KEY)
|
2020-12-02 20:36:54 +00:00
|
|
|
self.assertEqual(event.code, keycode_to)
|
2020-11-22 20:02:16 +00:00
|
|
|
self.assertEqual(event.value, 1)
|
|
|
|
|
|
|
|
event = uinput_write_history_pipe[0].recv()
|
|
|
|
self.assertEqual(event.type, evdev.events.EV_KEY)
|
2020-12-02 20:36:54 +00:00
|
|
|
self.assertEqual(event.code, keycode_to)
|
2020-11-22 20:02:16 +00:00
|
|
|
self.assertEqual(event.value, 0)
|
|
|
|
|
|
|
|
def test_stop_injecting(self):
|
|
|
|
keycode_from = 16
|
|
|
|
keycode_to = 90
|
|
|
|
|
|
|
|
self.change_empty_row(keycode_from, 't')
|
2020-12-04 13:38:41 +00:00
|
|
|
system_mapping.clear()
|
|
|
|
system_mapping._set('t', keycode_to)
|
2020-11-22 20:02:16 +00:00
|
|
|
|
|
|
|
# not all of those events should be processed, since that takes some
|
|
|
|
# time due to time.sleep in the fakes and the injection is stopped.
|
2020-12-06 13:23:00 +00:00
|
|
|
pending_events['device 2'] = [InputEvent(1, keycode_from, 1)] * 100
|
2020-11-22 20:02:16 +00:00
|
|
|
|
|
|
|
custom_mapping.save('device 2', 'foo preset')
|
|
|
|
|
|
|
|
self.window.selected_device = 'device 2'
|
|
|
|
self.window.selected_preset = 'foo preset'
|
|
|
|
self.window.on_apply_preset_clicked(None)
|
|
|
|
|
|
|
|
pipe = uinput_write_history_pipe[0]
|
|
|
|
# block until the first event is available, indicating that
|
|
|
|
# the injector is ready
|
|
|
|
write_history = [pipe.recv()]
|
|
|
|
|
|
|
|
# stop
|
|
|
|
self.window.on_apply_system_layout_clicked(None)
|
|
|
|
|
|
|
|
# try to receive a few of the events
|
|
|
|
time.sleep(0.2)
|
|
|
|
while pipe.poll():
|
|
|
|
write_history.append(pipe.recv())
|
|
|
|
|
|
|
|
len_before = len(write_history)
|
|
|
|
self.assertLess(len(write_history), 50)
|
|
|
|
|
|
|
|
# since the injector should not be running anymore, no more events
|
|
|
|
# should be received after waiting even more time
|
|
|
|
time.sleep(0.2)
|
|
|
|
while pipe.poll():
|
|
|
|
write_history.append(pipe.recv())
|
|
|
|
self.assertEqual(len(write_history), len_before)
|
|
|
|
|
2020-10-31 16:00:02 +00:00
|
|
|
|
2020-12-04 16:16:39 +00:00
|
|
|
original_access = os.access
|
|
|
|
original_getgrnam = grp.getgrnam
|
2020-12-06 14:11:13 +00:00
|
|
|
original_can_read_devices = permissions.can_read_devices
|
2020-12-04 16:16:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestPermissions(unittest.TestCase):
|
|
|
|
def tearDown(self):
|
|
|
|
os.access = original_access
|
|
|
|
os.getgrnam = original_getgrnam
|
2020-12-06 14:11:13 +00:00
|
|
|
permissions.can_read_devices = original_can_read_devices
|
2020-12-04 16:16:39 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
if self.window is not None:
|
|
|
|
self.window.on_close()
|
|
|
|
self.window.window.destroy()
|
|
|
|
gtk_iteration()
|
|
|
|
self.window = None
|
2020-12-04 16:16:39 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
shutil.rmtree('/tmp/key-mapper-test')
|
2020-12-04 16:16:39 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
def test_fails(self):
|
|
|
|
def fake():
|
|
|
|
return ['error1', 'error2', 'error3']
|
2020-12-04 16:16:39 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
permissions.can_read_devices = fake
|
2020-12-04 16:16:39 +00:00
|
|
|
|
|
|
|
self.window = launch()
|
|
|
|
status = self.window.get('status_bar')
|
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
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)
|
2020-12-04 16:16:39 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
def test_good(self):
|
|
|
|
def fake():
|
|
|
|
return []
|
2020-12-04 16:16:39 +00:00
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
permissions.can_read_devices = fake
|
2020-12-04 16:16:39 +00:00
|
|
|
|
|
|
|
self.window = launch()
|
|
|
|
status = self.window.get('status_bar')
|
|
|
|
|
2020-12-06 14:11:13 +00:00
|
|
|
self.assertIsNone(status.get_tooltip_text())
|
2020-12-04 16:16:39 +00:00
|
|
|
|
|
|
|
|
2020-10-31 16:00:02 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|