input-remapper/tests/test.py

641 lines
17 KiB
Python
Raw Normal View History

2020-10-26 22:45:22 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
2021-02-22 18:48:20 +00:00
# Copyright (C) 2021 sezanzeb <proxima@sezanzeb.de>
2020-10-26 22:45:22 +00:00
#
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
2020-11-29 15:21:34 +00:00
import os
2020-10-26 22:45:22 +00:00
import sys
import shutil
2020-11-19 10:40:15 +00:00
import time
2020-12-03 19:03:53 +00:00
import copy
2020-10-26 22:45:22 +00:00
import unittest
2020-11-29 00:44:14 +00:00
import subprocess
2020-11-20 20:38:59 +00:00
import multiprocessing
2020-11-28 14:43:24 +00:00
import asyncio
2021-03-21 18:15:20 +00:00
import psutil
2021-03-27 12:21:35 +00:00
from pickle import UnpicklingError
import evdev
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GLib', '2.0')
2020-11-16 14:39:15 +00:00
2020-11-29 15:21:34 +00:00
assert not os.getcwd().endswith('tests')
2021-03-21 18:15:20 +00:00
os.environ['UNITTEST'] = '1'
def is_service_running():
"""Check if the daemon is running."""
try:
subprocess.check_output(['pgrep', '-f', 'key-mapper-service'])
return True
except subprocess.CalledProcessError:
return False
2021-03-21 18:15:20 +00:00
def join_children():
"""Wait for child processes to exit. Stop them if it takes too long."""
this = psutil.Process(os.getpid())
i = 0
time.sleep(EVENT_READ_TIMEOUT)
children = this.children(recursive=True)
while len([c for c in children if c.status() != 'zombie']) > 0:
for child in children:
if i > 10:
child.kill()
print(
f'\033[90m' # color
f'Killed pid {child.pid} because it didn\'t finish in time'
'\033[0m' # end style
)
children = this.children(recursive=True)
time.sleep(EVENT_READ_TIMEOUT)
i += 1
if is_service_running():
# let tests control daemon existance
raise Exception('Expected the service not to be running already.')
# make sure the "tests" module visible
sys.path.append(os.getcwd())
2020-11-29 15:21:34 +00:00
2020-11-30 17:59:34 +00:00
# give tests some time to test stuff while the process
# is still running
EVENT_READ_TIMEOUT = 0.01
2021-03-21 18:15:20 +00:00
# based on experience how much time passes at most until
# the helper starts receiving previously pushed events after a
# call to start_reading
START_READING_DELAY = 0.05
2020-11-30 21:42:53 +00:00
MAX_ABS = 2 ** 15
2020-11-29 15:21:34 +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-20 20:38:59 +00:00
# for tests that makes the injector create its processes
uinput_write_history_pipe = multiprocessing.Pipe()
2020-11-18 23:04:04 +00:00
pending_events = {}
2020-11-18 22:00:18 +00:00
2021-01-01 21:20:33 +00:00
2021-02-13 13:11:49 +00:00
if os.path.exists(tmp):
shutil.rmtree(tmp)
2021-01-01 21:20:33 +00:00
def read_write_history_pipe():
"""convert the write history from the pipe to some easier to manage list"""
history = []
while uinput_write_history_pipe[0].poll():
event = uinput_write_history_pipe[0].recv()
history.append((event.type, event.code, event.value))
return history
# key-mapper is only interested in devices that have EV_KEY, add some
# random other stuff to test that they are ignored.
2020-12-27 10:26:07 +00:00
phys_1 = 'usb-0000:03:00.0-1/input2'
2021-01-08 16:21:51 +00:00
info_1 = evdev.device.DeviceInfo(1, 1, 1, 1)
2020-12-27 10:26:07 +00:00
2021-03-27 12:21:35 +00:00
keyboard_keys = sorted(evdev.ecodes.keys.keys())[:255]
fixtures = {
# device 1
'/dev/input/event11': {
2021-03-27 12:21:35 +00:00
'capabilities': {
evdev.ecodes.EV_KEY: [
evdev.ecodes.BTN_LEFT
],
evdev.ecodes.EV_REL: [
evdev.ecodes.REL_X,
evdev.ecodes.REL_Y,
evdev.ecodes.REL_WHEEL,
evdev.ecodes.REL_HWHEEL
]
},
2020-12-27 10:26:07 +00:00
'phys': f'{phys_1}/input2',
'info': info_1,
2021-01-05 18:33:47 +00:00
'name': 'device 1 foo',
'group': 'device 1'
},
'/dev/input/event10': {
2021-03-27 12:21:35 +00:00
'capabilities': {evdev.ecodes.EV_KEY: keyboard_keys},
2020-12-27 10:26:07 +00:00
'phys': f'{phys_1}/input3',
'info': info_1,
2021-01-05 18:33:47 +00:00
'name': 'device 1',
'group': 'device 1'
},
'/dev/input/event13': {
'capabilities': {evdev.ecodes.EV_KEY: [], evdev.ecodes.EV_SYN: []},
2020-12-27 10:26:07 +00:00
'phys': f'{phys_1}/input1',
'info': info_1,
2021-01-05 18:33:47 +00:00
'name': 'device 1',
'group': 'device 1'
},
'/dev/input/event14': {
'capabilities': {evdev.ecodes.EV_SYN: []},
2020-12-27 10:26:07 +00:00
'phys': f'{phys_1}/input0',
'info': info_1,
2021-01-05 18:33:47 +00:00
'name': 'device 1 qux',
'group': 'device 1'
},
# device 2
'/dev/input/event20': {
2021-03-27 12:21:35 +00:00
'capabilities': {evdev.ecodes.EV_KEY: keyboard_keys},
'phys': 'usb-0000:03:00.0-2/input1',
2021-01-08 16:21:51 +00:00
'info': evdev.device.DeviceInfo(2, 1, 2, 1),
'name': 'device 2'
},
'/dev/input/event30': {
2020-12-03 19:03:53 +00:00
'capabilities': {
evdev.ecodes.EV_SYN: [],
evdev.ecodes.EV_ABS: [
evdev.ecodes.ABS_X,
evdev.ecodes.ABS_Y,
evdev.ecodes.ABS_RX,
evdev.ecodes.ABS_RY,
2020-12-03 19:03:53 +00:00
evdev.ecodes.ABS_HAT0X
2020-12-26 15:46:01 +00:00
],
evdev.ecodes.EV_KEY: [
evdev.ecodes.BTN_A
2020-12-03 19:03:53 +00:00
]
},
'phys': 'usb-0000:03:00.0-3/input1',
2021-01-08 16:21:51 +00:00
'info': evdev.device.DeviceInfo(3, 1, 3, 1),
2020-11-30 21:42:53 +00:00
'name': 'gamepad'
},
2020-11-30 21:42:53 +00:00
# device that is completely ignored
2020-11-22 20:54:09 +00:00
'/dev/input/event31': {
'capabilities': {evdev.ecodes.EV_SYN: []},
'phys': 'usb-0000:03:00.0-4/input1',
2021-01-08 16:21:51 +00:00
'info': evdev.device.DeviceInfo(4, 1, 4, 1),
2020-11-22 20:54:09 +00:00
'name': 'Power Button'
},
# key-mapper devices are not displayed in the ui, some instance
# of key-mapper started injecting apparently.
'/dev/input/event40': {
2021-03-27 12:21:35 +00:00
'capabilities': {evdev.ecodes.EV_KEY: keyboard_keys},
'phys': 'key-mapper/input1',
2021-01-08 16:21:51 +00:00
'info': evdev.device.DeviceInfo(5, 1, 5, 1),
'name': 'key-mapper device 2'
},
}
2020-11-18 22:00:18 +00:00
2021-03-21 18:15:20 +00:00
def setup_pipe(device):
"""Create a pipe that can be used to send events to the helper,
which in turn will be sent to the reader
"""
if pending_events.get(device) is None:
pending_events[device] = multiprocessing.Pipe()
# make sure those pipes exist before any process (the helper) gets forked,
# so that events can be pushed after the fork.
for fixture in fixtures.values():
if 'group' in fixture:
setup_pipe(fixture['group'])
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):
2021-03-21 18:15:20 +00:00
"""Make a device act like it is reading events from evdev.
push_event is like hitting a key on a keyboard for stuff that reads from
evdev.InputDevice (which is patched in test.py to work that way)
2020-11-18 22:00:18 +00:00
Parameters
----------
device : string
2020-11-18 23:04:04 +00:00
For example 'device 1'
2020-12-06 13:23:00 +00:00
event : InputEvent
2020-11-18 22:00:18 +00:00
"""
2021-03-21 18:15:20 +00:00
setup_pipe(device)
pending_events[device][0].send(event)
2020-11-18 22:00:18 +00:00
2021-03-21 18:15:20 +00:00
def push_events(device, events):
"""Push multiple events"""
for event in events:
push_event(device, event)
def new_event(type, code, value, timestamp=None, offset=0):
2021-01-05 18:33:47 +00:00
"""Create a new input_event."""
if timestamp is None:
2021-03-21 18:15:20 +00:00
timestamp = time.time() + offset
2020-12-05 10:58:29 +00:00
2021-01-05 18:33:47 +00:00
sec = int(timestamp)
usec = timestamp % 1 * 1000000
event = evdev.InputEvent(sec, usec, type, code, value)
return event
2021-01-01 21:20:33 +00:00
2020-11-16 14:39:15 +00:00
def patch_paths():
from keymapper import paths
paths.CONFIG_PATH = '/tmp/key-mapper-test'
2020-11-16 14:39:15 +00:00
class InputDevice:
# expose as existing attribute, otherwise the patch for
# evdev < 1.0.0 will crash the test
path = None
2020-11-14 23:27:45 +00:00
def __init__(self, path):
2021-01-05 18:33:47 +00:00
if path != 'justdoit' and path not in fixtures:
raise FileNotFoundError()
2020-11-30 13:34:27 +00:00
self.path = path
2021-01-05 18:33:47 +00:00
fixture = fixtures.get(path, {})
self.phys = fixture.get('phys', 'unset')
2021-01-08 16:21:51 +00:00
self.info = fixture.get('info', evdev.device.DeviceInfo(None, None, None, None))
2021-01-05 18:33:47 +00:00
self.name = fixture.get('name', 'unset')
2020-12-03 19:37:36 +00:00
2021-01-05 18:33:47 +00:00
# properties that exists for test purposes and are not part of
# the original object
self.group = fixture.get('group', self.name)
2021-03-21 18:15:20 +00:00
# ensure a pipe exists to make this object act like
# it is reading events from a device
setup_pipe(self.group)
self.fd = pending_events[self.group][1].fileno()
def fileno(self):
"""Compatibility to select.select."""
return self.fd
2021-01-01 21:20:33 +00:00
def log(self, key, msg):
print(
f'\033[90m' # color
2021-01-05 18:33:47 +00:00
f'{msg} "{self.name}" "{self.path}" {key}'
2021-01-01 21:20:33 +00:00
'\033[0m' # end style
)
2020-12-26 15:46:01 +00:00
def absinfo(self, *args):
raise Exception('Ubuntus version of evdev doesn\'t support .absinfo')
2020-11-30 20:16:58 +00:00
def grab(self):
pass
2020-11-30 20:16:58 +00:00
2021-03-21 18:15:20 +00:00
async def async_read_loop(self):
if pending_events.get(self.group) is None:
self.log('no events to read', self.group)
return
# consume all of them
while pending_events[self.group][1].poll():
result = pending_events[self.group][1].recv()
self.log(result, 'async_read_loop')
yield result
await asyncio.sleep(0.01)
# doesn't loop endlessly in order to run tests for the injector in
# the main process
def read(self):
2021-01-05 18:33:47 +00:00
# the patched fake InputDevice objects read anything pending from
2021-03-21 18:15:20 +00:00
# that group.
# To be realistic it would have to check if the provided
2021-01-05 18:33:47 +00:00
# element is in its capabilities.
2021-03-21 18:15:20 +00:00
if self.group not in pending_events:
self.log('no events to read', self.group)
return
2020-11-18 22:00:18 +00:00
2021-03-21 18:15:20 +00:00
# consume all of them
while pending_events[self.group][1].poll():
event = pending_events[self.group][1].recv()
self.log(event, 'read')
yield event
time.sleep(EVENT_READ_TIMEOUT)
def read_loop(self):
"""Endless loop that yields events."""
while True:
event = pending_events[self.group][1].recv()
if event is not None:
self.log(event, 'read_loop')
yield event
time.sleep(EVENT_READ_TIMEOUT)
2020-11-30 17:59:34 +00:00
def read_one(self):
2021-03-21 18:15:20 +00:00
"""Read one event or none if nothing available."""
2021-01-05 18:33:47 +00:00
if pending_events.get(self.group) is None:
return None
2020-11-30 17:59:34 +00:00
2021-01-05 18:33:47 +00:00
if len(pending_events[self.group]) == 0:
return None
2021-03-21 18:15:20 +00:00
time.sleep(EVENT_READ_TIMEOUT)
2021-03-27 12:21:35 +00:00
try:
event = pending_events[self.group][1].recv()
except UnpicklingError as error:
# failed in tests sometimes
print(error)
return None
2021-01-01 21:20:33 +00:00
self.log(event, 'read_one')
return event
2021-02-13 11:44:50 +00:00
def capabilities(self, absinfo=True, verbose=False):
2020-12-26 15:46:01 +00:00
result = copy.deepcopy(fixtures[self.path]['capabilities'])
if absinfo and evdev.ecodes.EV_ABS in result:
absinfo_obj = evdev.AbsInfo(
value=None, min=None, fuzz=None, flat=None,
resolution=None, max=MAX_ABS
)
result[evdev.ecodes.EV_ABS] = [
(stuff, absinfo_obj) for stuff in result[evdev.ecodes.EV_ABS]
]
return result
2020-11-28 14:43:24 +00:00
2020-11-07 23:54:19 +00:00
uinputs = {}
class UInput:
2021-01-05 18:33:47 +00:00
def __init__(self, events=None, name='unnamed', *args, **kwargs):
self.fd = 0
self.write_count = 0
2021-01-05 18:33:47 +00:00
self.device = InputDevice('justdoit')
self.name = name
self.events = events
2021-02-13 19:19:31 +00:00
self.write_history = []
2020-11-18 23:04:04 +00:00
global uinputs
uinputs[name] = self
def capabilities(self, *args, **kwargs):
2021-01-05 18:33:47 +00:00
return self.events
2020-12-06 18:54:02 +00:00
def write(self, type, code, value):
self.write_count += 1
2021-01-05 18:33:47 +00:00
event = new_event(type, code, value)
uinput_write_history.append(event)
uinput_write_history_pipe[1].send(event)
2021-02-13 19:19:31 +00:00
self.write_history.append(event)
2021-01-05 18:33:47 +00:00
print(
f'\033[90m' # color
f'{(type, code, value)} written'
'\033[0m' # end style
)
2020-11-18 22:00:18 +00:00
def syn(self):
pass
2021-01-05 18:33:47 +00:00
class InputEvent(evdev.InputEvent):
def __init__(self, sec, usec, type, code, value):
self.t = (type, code, value)
super().__init__(sec, usec, type, code, value)
2021-01-07 16:15:12 +00:00
def copy(self):
return InputEvent(
self.sec,
self.usec,
self.type,
self.code,
self.value
)
2021-01-05 18:33:47 +00:00
def patch_evdev():
def list_devices():
return fixtures.keys()
2020-11-18 22:00:18 +00:00
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
2021-01-05 18:33:47 +00:00
evdev.InputEvent = InputEvent
2020-11-16 14:39:15 +00:00
2021-01-05 18:33:47 +00:00
def patch_events():
# improve logging of stuff
evdev.InputEvent.__str__ = lambda self: (
f'InputEvent{(self.type, self.code, self.value)}'
)
2021-03-21 18:15:20 +00:00
def patch_os_system():
"""Avoid running pkexec."""
original_system = os.system
def system(command):
if 'pkexec' in command:
# because it
# - will open a window for user input
# - has no knowledge of the fixtures and patches
raise Exception('Write patches to avoid running pkexec stuff')
return original_system(command)
os.system = system
2020-11-28 23:28:46 +00:00
def clear_write_history():
"""Empty the history in preparation for the next test."""
while len(uinput_write_history) > 0:
uinput_write_history.pop()
while uinput_write_history_pipe[0].poll():
uinput_write_history_pipe[0].recv()
2020-12-03 19:03:53 +00:00
2020-11-16 14:39:15 +00:00
# quickly fake some stuff before any other file gets a chance to import
# the original versions
patch_paths()
patch_evdev()
2021-01-05 18:33:47 +00:00
patch_events()
2021-03-21 18:15:20 +00:00
patch_os_system()
2020-11-07 23:54:19 +00:00
from keymapper.logger import update_verbosity
2021-03-21 18:15:20 +00:00
update_verbosity(True)
2021-02-13 19:34:33 +00:00
from keymapper.injection.injector import Injector
from keymapper.config import config
2021-03-21 18:15:20 +00:00
from keymapper.gui.reader import reader
2020-12-26 15:46:01 +00:00
from keymapper.getdevices import refresh_devices
from keymapper.state import system_mapping, custom_mapping
2021-01-09 16:44:24 +00:00
from keymapper.paths import get_config_path
2021-02-13 19:34:33 +00:00
from keymapper.injection.keycode_mapper import active_macros, unreleased
2020-10-26 22:45:22 +00:00
2020-12-06 18:54:02 +00:00
# no need for a high number in tests
Injector.regrab_timeout = 0.15
2020-12-06 18:54:02 +00:00
2020-12-26 15:46:01 +00:00
_fixture_copy = copy.deepcopy(fixtures)
2021-01-07 16:15:12 +00:00
environ_copy = copy.deepcopy(os.environ)
2020-12-26 15:46:01 +00:00
2021-03-21 18:15:20 +00:00
def send_event_to_reader(event):
"""Act like the helper and send input events to the reader."""
reader._results._unread.append({
'type': 'event',
'message': (
event.sec, event.usec,
event.type, event.code, event.value
)
})
def quick_cleanup(log=True):
"""Reset the applications state."""
if log:
print('quick cleanup')
2021-03-21 18:15:20 +00:00
for key in list(pending_events.keys()):
while pending_events[key][1].poll():
pending_events[key][1].recv()
try:
reader.terminate()
except (BrokenPipeError, OSError):
pass
2020-12-31 20:46:57 +00:00
if asyncio.get_event_loop().is_running():
for task in asyncio.all_tasks():
task.cancel()
2020-12-27 18:06:17 +00:00
if os.path.exists(tmp):
shutil.rmtree(tmp)
2020-12-26 15:46:01 +00:00
2021-01-09 16:44:24 +00:00
config.path = os.path.join(get_config_path(), 'config.json')
config.clear_config()
config.save_config()
2020-12-26 15:46:01 +00:00
system_mapping.populate()
custom_mapping.empty()
2020-12-27 18:06:17 +00:00
custom_mapping.clear_config()
2021-01-09 16:44:24 +00:00
custom_mapping.changed = False
2020-12-26 15:46:01 +00:00
clear_write_history()
2020-12-26 15:46:01 +00:00
for name in list(uinputs.keys()):
del uinputs[name]
for key in list(active_macros.keys()):
del active_macros[key]
2020-12-31 20:46:57 +00:00
for key in list(unreleased.keys()):
del unreleased[key]
2020-12-26 15:46:01 +00:00
for path in list(fixtures.keys()):
if path not in _fixture_copy:
del fixtures[path]
for path in list(_fixture_copy.keys()):
if path not in fixtures:
fixtures[path] = _fixture_copy[path]
2020-12-27 18:06:17 +00:00
2021-01-07 16:15:12 +00:00
os.environ.update(environ_copy)
for key in list(os.environ.keys()):
if key not in environ_copy:
del os.environ[key]
2021-03-21 18:15:20 +00:00
join_children()
reader.clear()
for _, pipe in pending_events.values():
assert not pipe.poll()
def cleanup():
"""Reset the applications state.
2021-03-21 18:15:20 +00:00
Using this is slower, usually quick_cleanup() is sufficient.
"""
print('cleanup')
os.system('pkill -f key-mapper-service')
2021-03-21 18:15:20 +00:00
os.system('pkill -f key-mapper-control')
time.sleep(0.05)
quick_cleanup(log=False)
2020-12-26 15:46:01 +00:00
refresh_devices()
2020-12-06 18:54:02 +00:00
2021-02-07 14:00:36 +00:00
def spy(obj, name):
"""Keep track of arguments and callcount.
Get a list of the call history that keeps getting updated.
"""
original_func = obj.__getattribute__(name)
history = []
def new_func(*args, **kwargs):
history.append((args, kwargs))
original_func(*args, **kwargs)
obj.__setattr__(name, new_func)
return history
2020-11-26 22:34:52 +00:00
def main():
cleanup()
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-12-02 15:17:52 +00:00
# for example
# `tests/test.py test_integration.TestIntegration.test_can_start`
# or `tests/test.py test_integration test_daemon`
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 before each test,
# because the first log will be on the same line otherwise
original_start_test = unittest.TextTestResult.startTest
def start_test(self, test):
original_start_test(self, test)
2020-11-22 14:17:55 +00:00
print()
2020-11-18 23:28:45 +00:00
unittest.TextTestResult.startTest = start_test
unittest.TextTestRunner(verbosity=2).run(testsuite)
2020-11-26 22:34:52 +00:00
if __name__ == "__main__":
main()