2020-10-26 22:45:22 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2020-10-31 13:43:09 +00:00
|
|
|
# key-mapper - GUI for device specific keyboard mappings
|
2020-10-26 22:45:22 +00:00
|
|
|
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
|
|
|
#
|
2020-10-31 13:02:59 +00:00
|
|
|
# This file is part of key-mapper.
|
2020-10-26 22:45:22 +00:00
|
|
|
#
|
2020-10-31 13:02:59 +00:00
|
|
|
# key-mapper is free software: you can redistribute it and/or modify
|
2020-10-26 22:45:22 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2020-10-31 13:02:59 +00:00
|
|
|
# key-mapper is distributed in the hope that it will be useful,
|
2020-10-26 22:45:22 +00:00
|
|
|
# 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
|
2020-10-31 13:02:59 +00:00
|
|
|
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-26 22:45:22 +00:00
|
|
|
|
|
|
|
|
2020-10-31 13:02:59 +00:00
|
|
|
"""Sets up key-mapper for the tests and runs them."""
|
2020-10-26 22:45:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
import sys
|
2020-11-19 10:40:15 +00:00
|
|
|
import time
|
2020-10-26 22:45:22 +00:00
|
|
|
import unittest
|
2020-11-19 10:30:01 +00:00
|
|
|
|
|
|
|
import evdev
|
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
from keymapper.logger import update_verbosity
|
2020-10-26 22:45:22 +00:00
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
tmp = '/tmp/key-mapper-test'
|
2020-11-18 22:00:18 +00:00
|
|
|
uinput_write_history = []
|
2020-11-18 23:04:04 +00:00
|
|
|
pending_events = {}
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2020-11-19 10:30:01 +00:00
|
|
|
# key-mapper is only interested in devices that have EV_KEY, add some
|
|
|
|
# random other stuff to test that they are ignored.
|
|
|
|
fixtures = {
|
|
|
|
# device 1
|
|
|
|
'/dev/input/event11': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_KEY: [], evdev.ecodes.EV_ABS: []},
|
|
|
|
'phys': 'usb-0000:03:00.0-1/input2',
|
|
|
|
'name': 'device 1 foo'
|
|
|
|
},
|
|
|
|
'/dev/input/event10': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_KEY: []},
|
|
|
|
'phys': 'usb-0000:03:00.0-1/input3',
|
|
|
|
'name': 'device 1'
|
|
|
|
},
|
|
|
|
'/dev/input/event13': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_KEY: [], evdev.ecodes.EV_SYN: []},
|
|
|
|
'phys': 'usb-0000:03:00.0-1/input1',
|
|
|
|
'name': 'device 1'
|
|
|
|
},
|
|
|
|
'/dev/input/event14': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_SYN: []},
|
|
|
|
'phys': 'usb-0000:03:00.0-1/input0',
|
|
|
|
'name': 'device 1 qux'
|
|
|
|
},
|
|
|
|
|
|
|
|
# device 2
|
|
|
|
'/dev/input/event20': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_KEY: []},
|
|
|
|
'phys': 'usb-0000:03:00.0-2/input1',
|
|
|
|
'name': 'device 2'
|
|
|
|
},
|
|
|
|
|
|
|
|
# something that is completely ignored
|
|
|
|
'/dev/input/event30': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_SYN: []},
|
|
|
|
'phys': 'usb-0000:03:00.0-3/input1',
|
|
|
|
'name': 'device 3'
|
|
|
|
},
|
|
|
|
|
|
|
|
# key-mapper devices are also ignored, another instance of key-mapper
|
|
|
|
# started injecting apparently.
|
|
|
|
'/dev/input/event40': {
|
|
|
|
'capabilities': {evdev.ecodes.EV_KEY: []},
|
|
|
|
'phys': 'key-mapper/input1',
|
|
|
|
'name': 'key-mapper device 2'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
def get_events():
|
|
|
|
"""Get all events written by the injector."""
|
|
|
|
return uinput_write_history
|
2020-11-18 22:00:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def push_event(device, event):
|
2020-11-18 23:04:04 +00:00
|
|
|
"""Emit a fake event for a device.
|
2020-11-18 22:00:18 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
device : string
|
2020-11-18 23:04:04 +00:00
|
|
|
For example 'device 1'
|
2020-11-18 22:00:18 +00:00
|
|
|
event : Event
|
|
|
|
"""
|
2020-11-18 23:04:04 +00:00
|
|
|
if pending_events.get(device) is None:
|
|
|
|
pending_events[device] = []
|
|
|
|
pending_events[device].append(event)
|
2020-11-18 22:00:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Event:
|
|
|
|
"""Event to put into the injector for tests."""
|
|
|
|
def __init__(self, type, code, value):
|
|
|
|
"""
|
|
|
|
Paramaters
|
|
|
|
----------
|
|
|
|
type : int
|
|
|
|
one of evdev.ecodes.EV_*
|
|
|
|
code : int
|
|
|
|
keyboard event code as known to linux. E.g. 2 for the '1' button
|
|
|
|
value : int
|
|
|
|
1 for down, 0 for up, 2 for hold
|
|
|
|
"""
|
|
|
|
self.type = type
|
|
|
|
self.code = code
|
|
|
|
self.value = value
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def patch_paths():
|
|
|
|
from keymapper import paths
|
2020-11-18 23:10:42 +00:00
|
|
|
paths.CONFIG = '/tmp/key-mapper-test/'
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def patch_evdev():
|
|
|
|
def list_devices():
|
|
|
|
return fixtures.keys()
|
2020-11-14 23:27:45 +00:00
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
class InputDevice:
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.phys = fixtures[path]['phys']
|
|
|
|
self.name = fixtures[path]['name']
|
2020-11-02 00:25:43 +00:00
|
|
|
|
2020-11-18 22:00:18 +00:00
|
|
|
def grab(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def read_loop(self):
|
2020-11-18 23:04:04 +00:00
|
|
|
"""Read all prepared events at once."""
|
|
|
|
if pending_events.get(self.name) is None:
|
|
|
|
return
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
while len(pending_events[self.name]) > 0:
|
|
|
|
yield pending_events[self.name].pop(0)
|
2020-11-19 10:40:15 +00:00
|
|
|
# give tests some time to test stuff while the process
|
|
|
|
# is still running
|
|
|
|
time.sleep(0.01)
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
def capabilities(self, absinfo=True):
|
2020-11-16 14:39:15 +00:00
|
|
|
return fixtures[self.path]['capabilities']
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2020-11-18 22:00:18 +00:00
|
|
|
class UInput:
|
2020-11-18 23:04:04 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.fd = 0
|
|
|
|
pass
|
|
|
|
|
|
|
|
def write(self, type, code, value):
|
|
|
|
uinput_write_history.append(Event(type, code, value))
|
2020-11-18 22:00:18 +00:00
|
|
|
|
|
|
|
def syn(self):
|
|
|
|
pass
|
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
evdev.list_devices = list_devices
|
|
|
|
evdev.InputDevice = InputDevice
|
2020-11-18 22:00:18 +00:00
|
|
|
evdev.UInput = UInput
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def patch_unsaved():
|
|
|
|
# don't block tests
|
|
|
|
from keymapper.gtk import unsaved
|
|
|
|
unsaved.unsaved_changes_dialog = lambda: unsaved.CONTINUE
|
|
|
|
|
|
|
|
|
|
|
|
# quickly fake some stuff before any other file gets a chance to import
|
|
|
|
# the original versions
|
|
|
|
patch_paths()
|
|
|
|
patch_evdev()
|
|
|
|
patch_unsaved()
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2020-10-26 22:45:22 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-11-02 00:25:43 +00:00
|
|
|
update_verbosity(True)
|
|
|
|
|
2020-10-26 22:45:22 +00:00
|
|
|
modules = sys.argv[1:]
|
|
|
|
# discoverer is really convenient, but it can't find a specific test
|
|
|
|
# in all of the available tests like unittest.main() does...,
|
|
|
|
# so provide both options.
|
|
|
|
if len(modules) > 0:
|
2020-11-14 23:27:45 +00:00
|
|
|
# for example `tests/test.py integration.Integration.test_can_start`
|
2020-10-26 22:45:22 +00:00
|
|
|
testsuite = unittest.defaultTestLoader.loadTestsFromNames(
|
|
|
|
[f'testcases.{module}' for module in modules]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# run all tests by default
|
|
|
|
testsuite = unittest.defaultTestLoader.discover(
|
|
|
|
'testcases', pattern='*.py'
|
|
|
|
)
|
2020-11-18 23:28:45 +00:00
|
|
|
|
|
|
|
# add a newline to each "qux (foo.bar)..." output, because the logs
|
|
|
|
# will be on the same line otherwise
|
|
|
|
originalStartTest = unittest.TextTestResult.startTest
|
|
|
|
def startTest(self, test):
|
|
|
|
originalStartTest(self, test)
|
|
|
|
print()
|
|
|
|
unittest.TextTestResult.startTest = startTest
|
|
|
|
|
|
|
|
testrunner = unittest.TextTestRunner(verbosity=2).run(testsuite)
|