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
|
|
|
|
import os
|
2020-10-31 16:00:02 +00:00
|
|
|
import unittest
|
2020-11-22 12:59:28 +00:00
|
|
|
import evdev
|
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-11-18 19:03:37 +00:00
|
|
|
from keymapper.state import custom_mapping
|
2020-11-18 09:33:59 +00:00
|
|
|
from keymapper.paths import CONFIG
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-11-22 12:59:28 +00:00
|
|
|
from test import tmp, pending_events, Event
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
def launch(argv=None, bin_path='bin/key-mapper-gtk'):
|
2020-11-08 20:34:27 +00:00
|
|
|
"""Start key-mapper-gtk with the command line argument array argv."""
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class Integration(unittest.TestCase):
|
|
|
|
"""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
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def setUp(self):
|
2020-11-07 23:54:19 +00:00
|
|
|
if os.path.exists(tmp):
|
|
|
|
shutil.rmtree(tmp)
|
2020-11-18 21:33:01 +00:00
|
|
|
custom_mapping.empty()
|
2020-10-31 16:00:02 +00:00
|
|
|
self.window = launch()
|
|
|
|
|
|
|
|
def tearDown(self):
|
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-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-10-31 16:00:02 +00:00
|
|
|
def test_can_start(self):
|
|
|
|
self.assertIsNotNone(self.window)
|
|
|
|
self.assertTrue(self.window.window.get_visible())
|
|
|
|
|
2020-11-08 17:51:35 +00:00
|
|
|
def test_adds_empty_rows(self):
|
|
|
|
rows = len(self.window.get('key_list').get_children())
|
|
|
|
self.assertEqual(rows, 1)
|
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
custom_mapping.change(13, 'a', None)
|
2020-11-08 17:51:35 +00:00
|
|
|
time.sleep(0.2)
|
|
|
|
gtk_iteration()
|
|
|
|
|
|
|
|
rows = len(self.window.get('key_list').get_children())
|
|
|
|
self.assertEqual(rows, 2)
|
|
|
|
|
2020-11-15 00:35:35 +00:00
|
|
|
def test_rows(self):
|
|
|
|
"""Comprehensive test for rows."""
|
2020-11-15 02:01:11 +00:00
|
|
|
def change_empty_row(keycode, character):
|
2020-11-15 00:35:35 +00:00
|
|
|
"""Modify the one empty row that always exists."""
|
|
|
|
# wait for the window to create a new empty row if needed
|
|
|
|
time.sleep(0.2)
|
|
|
|
gtk_iteration()
|
|
|
|
|
|
|
|
# find the empty row
|
|
|
|
rows = self.get_rows()
|
|
|
|
row = rows[-1]
|
|
|
|
self.assertNotIn('changed', row.get_style_context().list_classes())
|
|
|
|
self.assertIsNone(row.keycode.get_label())
|
|
|
|
self.assertEqual(row.character_input.get_text(), '')
|
|
|
|
|
2020-11-22 12:59:28 +00:00
|
|
|
self.window.window.set_focus(row.keycode)
|
|
|
|
|
|
|
|
pending_events[self.window.selected_device] = [
|
|
|
|
Event(evdev.events.EV_KEY, keycode - 8, 1)
|
|
|
|
]
|
|
|
|
|
2020-11-22 13:47:34 +00:00
|
|
|
self.window.on_window_event(None, None)
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-15 02:01:11 +00:00
|
|
|
self.assertEqual(int(row.keycode.get_label()), keycode)
|
2020-11-15 00:35:35 +00:00
|
|
|
|
|
|
|
# set the character to make the new row complete
|
|
|
|
row.character_input.set_text(character)
|
|
|
|
|
|
|
|
self.assertIn('changed', row.get_style_context().list_classes())
|
|
|
|
|
|
|
|
return row
|
|
|
|
|
2020-11-15 02:01:11 +00:00
|
|
|
# add two rows by modifiying the one empty row that exists
|
|
|
|
change_empty_row(10, 'a')
|
|
|
|
change_empty_row(11, 'b')
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-15 02:01:11 +00:00
|
|
|
# one empty row added automatically again
|
|
|
|
time.sleep(0.2)
|
|
|
|
gtk_iteration()
|
|
|
|
# sleep one more time because it's funny to watch the ui
|
|
|
|
# during the test, how rows turn blue and stuff
|
|
|
|
time.sleep(0.2)
|
|
|
|
self.assertEqual(len(self.get_rows()), 3)
|
|
|
|
|
2020-11-17 13:35:36 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(10), 'a')
|
|
|
|
self.assertEqual(custom_mapping.get_character(11), 'b')
|
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-11-17 13:35:36 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(10), 'c')
|
|
|
|
self.assertEqual(custom_mapping.get_character(11), 'b')
|
2020-11-15 02:01:11 +00:00
|
|
|
self.assertTrue(custom_mapping.changed)
|
2020-11-15 00:35:35 +00:00
|
|
|
|
2020-11-08 17:51:35 +00:00
|
|
|
def test_rename_and_save(self):
|
2020-11-18 23:04:04 +00:00
|
|
|
custom_mapping.change(14, 'a', 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-11-17 13:35:36 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(14), 'a')
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
custom_mapping.change(14, 'b', 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-18 21:33:01 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/asdf'))
|
2020-11-17 13:35:36 +00:00
|
|
|
self.assertEqual(custom_mapping.get_character(14), 'b')
|
2020-11-08 17:51:35 +00:00
|
|
|
|
2020-10-31 17:48:03 +00:00
|
|
|
def test_select_device_and_preset(self):
|
2020-10-31 18:12:27 +00:00
|
|
|
class FakeDropdown(Gtk.ComboBoxText):
|
2020-10-31 18:10:40 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def get_active_text(self):
|
|
|
|
return self.name
|
|
|
|
|
2020-11-14 23:27:45 +00:00
|
|
|
def get_active_id(self):
|
|
|
|
return self.name
|
|
|
|
|
2020-11-07 23:54:19 +00:00
|
|
|
# created on start because the first device is selected and some empty
|
|
|
|
# preset prepared.
|
2020-11-18 21:33:01 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
|
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-18 21:33:01 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset'))
|
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/new preset 2'))
|
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')),
|
|
|
|
['new preset', 'new preset 2']
|
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-18 21:33:01 +00:00
|
|
|
self.assertFalse(os.path.exists(f'{CONFIG}/device 1/abc 123'))
|
2020-11-18 23:04:04 +00:00
|
|
|
custom_mapping.change(10, '1', 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-18 21:33:01 +00:00
|
|
|
self.assertTrue(os.path.exists(f'{CONFIG}/device 1/abc 123'))
|
2020-11-07 23:54:19 +00:00
|
|
|
self.assertListEqual(
|
2020-11-18 09:33:59 +00:00
|
|
|
sorted(os.listdir(CONFIG)),
|
2020-11-18 21:33:01 +00:00
|
|
|
['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')),
|
|
|
|
['abc 123', 'new preset 2']
|
2020-11-07 23:54:19 +00:00
|
|
|
)
|
2020-10-31 17:48:03 +00:00
|
|
|
|
2020-10-31 16:00:02 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|