You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.0 KiB
Python
106 lines
3.0 KiB
Python
4 years ago
|
#!/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/>.
|
||
|
|
||
|
|
||
4 years ago
|
import os
|
||
|
import multiprocessing
|
||
4 years ago
|
import unittest
|
||
4 years ago
|
import time
|
||
4 years ago
|
|
||
|
import evdev
|
||
4 years ago
|
import gi
|
||
|
gi.require_version('Gtk', '3.0')
|
||
|
from gi.repository import Gtk
|
||
4 years ago
|
|
||
4 years ago
|
from keymapper.state import custom_mapping, system_mapping, \
|
||
|
clear_system_mapping
|
||
4 years ago
|
from keymapper.config import config
|
||
4 years ago
|
from keymapper.daemon import Daemon, get_dbus_interface, BUS_NAME
|
||
4 years ago
|
|
||
4 years ago
|
from tests.test import uinput_write_history_pipe, Event, pending_events
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
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,
|
||
4 years ago
|
args=('key-mapper-service -d',)
|
||
4 years ago
|
)
|
||
|
cls.process.start()
|
||
|
time.sleep(0.5)
|
||
|
cls.interface = get_dbus_interface()
|
||
|
|
||
|
@classmethod
|
||
|
def tearDownClass(cls):
|
||
4 years ago
|
cls.interface.stop(True)
|
||
4 years ago
|
|
||
|
def test_can_connect(self):
|
||
4 years ago
|
# it's a remote dbus object
|
||
|
self.assertEqual(self.interface._bus_name, BUS_NAME)
|
||
|
self.assertFalse(isinstance(self.interface, Daemon))
|
||
|
self.assertEqual(self.interface.hello('foo'), 'foo')
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
class TestDaemon(unittest.TestCase):
|
||
4 years ago
|
def setUp(self):
|
||
|
self.grab = evdev.InputDevice.grab
|
||
|
self.daemon = None
|
||
4 years ago
|
|
||
|
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
|
||
4 years ago
|
config.clear_config()
|
||
4 years ago
|
|
||
|
def test_daemon(self):
|
||
4 years ago
|
keycode_from = 9
|
||
|
keycode_to = 100
|
||
4 years ago
|
|
||
4 years ago
|
custom_mapping.change(keycode_from, 'a')
|
||
4 years ago
|
clear_system_mapping()
|
||
|
system_mapping['a'] = keycode_to
|
||
4 years ago
|
|
||
|
custom_mapping.save('device 2', 'foo')
|
||
|
config.set_autoload_preset('device 2', 'foo')
|
||
|
|
||
|
pending_events['device 2'] = [
|
||
4 years ago
|
Event(evdev.events.EV_KEY, keycode_from - 8, 0)
|
||
4 years ago
|
]
|
||
|
|
||
|
self.daemon = Daemon()
|
||
|
|
||
4 years ago
|
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)
|
||
4 years ago
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
unittest.main()
|