2020-11-20 20:38:59 +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/>.
|
|
|
|
|
|
|
|
|
2020-11-29 00:44:14 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import multiprocessing
|
2020-11-20 20:38:59 +00:00
|
|
|
import unittest
|
2020-11-29 00:44:14 +00:00
|
|
|
import time
|
|
|
|
from unittest.mock import patch
|
|
|
|
from importlib.util import spec_from_loader, module_from_spec
|
|
|
|
from importlib.machinery import SourceFileLoader
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2020-11-29 00:44:14 +00:00
|
|
|
import dbus
|
2020-11-20 20:38:59 +00:00
|
|
|
import evdev
|
2020-11-29 00:44:14 +00:00
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2020-11-30 15:22:17 +00:00
|
|
|
from keymapper.state import custom_mapping, system_mapping, \
|
|
|
|
clear_system_mapping
|
2020-11-20 20:38:59 +00:00
|
|
|
from keymapper.config import config
|
2020-11-29 00:44:14 +00:00
|
|
|
from keymapper.daemon import Daemon, get_dbus_interface
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
from test import uinput_write_history_pipe, Event, pending_events
|
|
|
|
|
|
|
|
|
2020-11-29 00:44:14 +00:00
|
|
|
def gtk_iteration():
|
|
|
|
"""Iterate while events are pending."""
|
|
|
|
while Gtk.events_pending():
|
|
|
|
Gtk.main_iteration()
|
|
|
|
|
|
|
|
|
|
|
|
class TestDBusDaemon(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.process = multiprocessing.Process(
|
|
|
|
target=os.system,
|
|
|
|
args=('key-mapper-service',)
|
|
|
|
)
|
|
|
|
cls.process.start()
|
|
|
|
time.sleep(0.5)
|
|
|
|
cls.interface = get_dbus_interface()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.interface.stop()
|
|
|
|
time.sleep(0.1)
|
|
|
|
cls.process.terminate()
|
|
|
|
time.sleep(0.1)
|
|
|
|
os.system('pkill -f key-mapper-service')
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
def test_can_connect(self):
|
|
|
|
self.assertIsInstance(self.interface, dbus.Interface)
|
|
|
|
|
|
|
|
|
2020-11-20 20:38:59 +00:00
|
|
|
class TestDaemon(unittest.TestCase):
|
2020-11-25 22:36:03 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.grab = evdev.InputDevice.grab
|
|
|
|
self.daemon = None
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# avoid race conditions with other tests, daemon may run processes
|
|
|
|
if self.daemon is not None:
|
|
|
|
self.daemon.stop()
|
|
|
|
self.daemon = None
|
|
|
|
evdev.InputDevice.grab = self.grab
|
2020-11-25 22:36:03 +00:00
|
|
|
config.clear_config()
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
def test_daemon(self):
|
2020-11-22 20:03:09 +00:00
|
|
|
keycode_from = 9
|
|
|
|
keycode_to = 100
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2020-11-22 20:03:09 +00:00
|
|
|
custom_mapping.change(keycode_from, 'a')
|
2020-11-30 15:22:17 +00:00
|
|
|
clear_system_mapping()
|
|
|
|
system_mapping['a'] = keycode_to
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
custom_mapping.save('device 2', 'foo')
|
|
|
|
config.set_autoload_preset('device 2', 'foo')
|
|
|
|
|
|
|
|
pending_events['device 2'] = [
|
2020-11-22 20:03:09 +00:00
|
|
|
Event(evdev.events.EV_KEY, keycode_from - 8, 0)
|
2020-11-20 20:38:59 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
self.daemon = Daemon()
|
|
|
|
|
2020-11-22 20:03:09 +00:00
|
|
|
event = uinput_write_history_pipe[0].recv()
|
|
|
|
self.assertEqual(event.type, evdev.events.EV_KEY)
|
|
|
|
self.assertEqual(event.code, keycode_to - 8)
|
|
|
|
self.assertEqual(event.value, 0)
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|