2020-10-26 22:45:22 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2020-10-26 22:45:22 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-10-26 22:45:22 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper 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.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper 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
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-26 22:45:22 +00:00
|
|
|
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
"""Sets up inputremapper for the tests and runs them.
|
|
|
|
|
|
|
|
This module needs to be imported first in test files.
|
|
|
|
"""
|
|
|
|
|
2022-02-07 16:52:41 +00:00
|
|
|
import argparse
|
2022-07-23 08:53:41 +00:00
|
|
|
import json
|
2020-11-29 15:21:34 +00:00
|
|
|
import os
|
2020-10-26 22:45:22 +00:00
|
|
|
import sys
|
2022-01-17 23:32:13 +00:00
|
|
|
import tempfile
|
2022-07-23 08:53:41 +00:00
|
|
|
import traceback
|
|
|
|
import warnings
|
|
|
|
from multiprocessing.connection import Connection
|
|
|
|
from typing import Dict, Tuple
|
|
|
|
import tracemalloc
|
|
|
|
|
|
|
|
tracemalloc.start()
|
|
|
|
|
|
|
|
try:
|
|
|
|
sys.modules.get("tests.test").main
|
|
|
|
raise AssertionError(
|
|
|
|
"test.py was already imported. "
|
|
|
|
"Always use 'from tests.test import ...' "
|
|
|
|
"not 'from test import ...' to import this"
|
|
|
|
)
|
|
|
|
# have fun debugging infinitely blocking tests without this
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2022-01-14 17:50:57 +00:00
|
|
|
|
2022-02-26 12:26:53 +00:00
|
|
|
|
|
|
|
def get_project_root():
|
|
|
|
"""Find the projects root, i.e. the uppermost directory of the repo."""
|
|
|
|
# when tests are started in pycharm via the green arrow, the working directory
|
|
|
|
# is not the project root. Go up until it is found.
|
|
|
|
root = os.getcwd()
|
|
|
|
for _ in range(10):
|
|
|
|
if "setup.py" in os.listdir(root):
|
|
|
|
return root
|
|
|
|
|
|
|
|
root = os.path.dirname(root)
|
|
|
|
|
|
|
|
raise Exception("Could not find project root")
|
|
|
|
|
2022-01-15 14:03:21 +00:00
|
|
|
|
2022-01-14 17:50:57 +00:00
|
|
|
# make sure the "tests" module visible
|
2022-02-26 12:26:53 +00:00
|
|
|
sys.path.append(get_project_root())
|
2022-01-14 17:50:57 +00:00
|
|
|
if __name__ == "__main__":
|
2022-02-26 12:26:53 +00:00
|
|
|
# import this file to itself to make sure is not run twice and all global
|
|
|
|
# variables end up in sys.modules
|
2022-01-14 17:50:57 +00:00
|
|
|
# https://stackoverflow.com/questions/13181559/importing-modules-main-vs-import-as-module
|
|
|
|
import tests.test
|
|
|
|
|
|
|
|
tests.test.main()
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
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
|
2022-01-18 20:20:42 +00:00
|
|
|
import logging
|
2021-03-27 12:21:35 +00:00
|
|
|
from pickle import UnpicklingError
|
2021-04-23 09:51:21 +00:00
|
|
|
from unittest.mock import patch
|
2020-11-19 10:30:01 +00:00
|
|
|
|
|
|
|
import evdev
|
|
|
|
|
2022-01-15 14:03:21 +00:00
|
|
|
from tests.xmodmap import xmodmap
|
2020-12-02 13:36:17 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
os.environ["UNITTEST"] = "1"
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-01-18 20:20:42 +00:00
|
|
|
logger = logging.getLogger("input-remapper-test")
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
handler.setFormatter(logging.Formatter("\033[90mTest: %(message)s\033[0m"))
|
|
|
|
logger.addHandler(handler)
|
|
|
|
logger.setLevel(logging.INFO)
|
2021-04-26 21:21:52 +00:00
|
|
|
|
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
def is_service_running():
|
|
|
|
"""Check if the daemon is running."""
|
|
|
|
try:
|
2022-01-01 12:00:49 +00:00
|
|
|
subprocess.check_output(["pgrep", "-f", "input-remapper-service"])
|
2020-12-24 00:26:34 +00:00
|
|
|
return True
|
2020-12-02 13:36:17 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2020-12-24 00:26:34 +00:00
|
|
|
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)
|
2021-09-26 10:44:56 +00:00
|
|
|
while len([c for c in children if c.status() != "zombie"]) > 0:
|
2021-03-21 18:15:20 +00:00
|
|
|
for child in children:
|
|
|
|
if i > 10:
|
|
|
|
child.kill()
|
2022-01-18 20:20:42 +00:00
|
|
|
logger.info("Killed pid %s because it didn't finish in time", child.pid)
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
children = this.children(recursive=True)
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if is_service_running():
|
2020-12-02 13:36:17 +00:00
|
|
|
# let tests control daemon existance
|
2021-09-26 10:44:56 +00:00
|
|
|
raise Exception("Expected the service not to be running already.")
|
2020-12-02 13:36:17 +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
|
|
|
|
|
2021-04-02 10:16:34 +00:00
|
|
|
# for joysticks
|
2022-01-31 13:02:03 +00:00
|
|
|
MIN_ABS = -(2**15)
|
|
|
|
MAX_ABS = 2**15
|
2020-11-30 21:42:53 +00:00
|
|
|
|
2022-01-17 23:32:13 +00:00
|
|
|
# When it gets garbage collected it cleans up the temporary directory so it needs to
|
|
|
|
# stay reachable while the tests are ran.
|
|
|
|
temporary_directory = tempfile.TemporaryDirectory(prefix="input-remapper-test")
|
|
|
|
tmp = temporary_directory.name
|
2020-11-29 15:21:34 +00:00
|
|
|
|
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()
|
2022-07-23 08:53:41 +00:00
|
|
|
pending_events: Dict[str, Tuple[Connection, Connection]] = {}
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
|
|
|
|
def read_write_history_pipe():
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Convert the write history from the pipe to some easier to manage list."""
|
2021-01-01 21:20:33 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is only interested in devices that have EV_KEY, add some
|
2020-11-19 10:30:01 +00:00
|
|
|
# random other stuff to test that they are ignored.
|
2021-09-26 10:44:56 +00:00
|
|
|
phys_foo = "usb-0000:03:00.0-1/input2"
|
2021-04-23 09:51:21 +00:00
|
|
|
info_foo = 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]
|
|
|
|
|
2020-11-19 10:30:01 +00:00
|
|
|
fixtures = {
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event1": {
|
|
|
|
"capabilities": {
|
|
|
|
evdev.ecodes.EV_KEY: [evdev.ecodes.KEY_A],
|
2021-04-23 09:51:21 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"phys": "usb-0000:03:00.0-0/input1",
|
|
|
|
"info": info_foo,
|
|
|
|
"name": "Foo Device",
|
2021-04-23 09:51:21 +00:00
|
|
|
},
|
|
|
|
# Another "Foo Device", which will get an incremented key.
|
|
|
|
# If possible write tests using this one, because name != key here and
|
|
|
|
# that would be important to test as well. Otherwise the tests can't
|
|
|
|
# see if the groups correct attribute is used in functions and paths.
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event11": {
|
|
|
|
"capabilities": {
|
2022-07-23 08:53:41 +00:00
|
|
|
evdev.ecodes.EV_KEY: [
|
|
|
|
evdev.ecodes.BTN_LEFT,
|
|
|
|
evdev.ecodes.BTN_TOOL_DOUBLETAP,
|
|
|
|
],
|
2021-03-27 12:21:35 +00:00
|
|
|
evdev.ecodes.EV_REL: [
|
|
|
|
evdev.ecodes.REL_X,
|
|
|
|
evdev.ecodes.REL_Y,
|
|
|
|
evdev.ecodes.REL_WHEEL,
|
2021-09-26 10:44:56 +00:00
|
|
|
evdev.ecodes.REL_HWHEEL,
|
|
|
|
],
|
2021-03-27 12:21:35 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"phys": f"{phys_foo}/input2",
|
|
|
|
"info": info_foo,
|
|
|
|
"name": "Foo Device foo",
|
|
|
|
"group_key": "Foo Device 2", # expected key
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event10": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: keyboard_keys},
|
|
|
|
"phys": f"{phys_foo}/input3",
|
|
|
|
"info": info_foo,
|
|
|
|
"name": "Foo Device",
|
|
|
|
"group_key": "Foo Device 2",
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event13": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: [], evdev.ecodes.EV_SYN: []},
|
|
|
|
"phys": f"{phys_foo}/input1",
|
|
|
|
"info": info_foo,
|
|
|
|
"name": "Foo Device",
|
|
|
|
"group_key": "Foo Device 2",
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event14": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_SYN: []},
|
|
|
|
"phys": f"{phys_foo}/input0",
|
|
|
|
"info": info_foo,
|
|
|
|
"name": "Foo Device qux",
|
|
|
|
"group_key": "Foo Device 2",
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2022-07-23 08:53:41 +00:00
|
|
|
"/dev/input/event15": {
|
|
|
|
"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,
|
|
|
|
evdev.ecodes.ABS_Z,
|
|
|
|
evdev.ecodes.ABS_RZ,
|
|
|
|
evdev.ecodes.ABS_HAT0X,
|
|
|
|
evdev.ecodes.ABS_HAT0Y,
|
|
|
|
],
|
|
|
|
evdev.ecodes.EV_KEY: [evdev.ecodes.BTN_A],
|
|
|
|
},
|
|
|
|
"phys": f"{phys_foo}/input4",
|
|
|
|
"info": info_foo,
|
|
|
|
"name": "Foo Device bar",
|
|
|
|
"group_key": "Foo Device 2",
|
|
|
|
},
|
2021-04-23 09:51:21 +00:00
|
|
|
# Bar Device
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event20": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: keyboard_keys},
|
|
|
|
"phys": "usb-0000:03:00.0-2/input1",
|
|
|
|
"info": evdev.device.DeviceInfo(2, 1, 2, 1),
|
|
|
|
"name": "Bar Device",
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event30": {
|
|
|
|
"capabilities": {
|
2020-12-03 19:03:53 +00:00
|
|
|
evdev.ecodes.EV_SYN: [],
|
|
|
|
evdev.ecodes.EV_ABS: [
|
|
|
|
evdev.ecodes.ABS_X,
|
|
|
|
evdev.ecodes.ABS_Y,
|
2020-12-19 15:04:07 +00:00
|
|
|
evdev.ecodes.ABS_RX,
|
|
|
|
evdev.ecodes.ABS_RY,
|
2021-04-03 10:48:52 +00:00
|
|
|
evdev.ecodes.ABS_Z,
|
|
|
|
evdev.ecodes.ABS_RZ,
|
2021-09-26 10:44:56 +00:00
|
|
|
evdev.ecodes.ABS_HAT0X,
|
2022-04-17 10:19:23 +00:00
|
|
|
evdev.ecodes.ABS_HAT0Y,
|
2020-12-26 15:46:01 +00:00
|
|
|
],
|
2021-09-26 10:44:56 +00:00
|
|
|
evdev.ecodes.EV_KEY: [evdev.ecodes.BTN_A],
|
2020-12-03 19:03:53 +00:00
|
|
|
},
|
2021-09-26 10:44:56 +00:00
|
|
|
"phys": "", # this is empty sometimes
|
|
|
|
"info": evdev.device.DeviceInfo(3, 1, 3, 1),
|
|
|
|
"name": "gamepad",
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2020-11-30 21:42:53 +00:00
|
|
|
# device that is completely ignored
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event31": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_SYN: []},
|
|
|
|
"phys": "usb-0000:03:00.0-4/input1",
|
|
|
|
"info": evdev.device.DeviceInfo(4, 1, 4, 1),
|
|
|
|
"name": "Power Button",
|
2020-11-22 20:54:09 +00:00
|
|
|
},
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper devices are not displayed in the ui, some instance
|
|
|
|
# of input-remapper started injecting apparently.
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event40": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: keyboard_keys},
|
2022-01-01 12:00:49 +00:00
|
|
|
"phys": "input-remapper/input1",
|
2021-09-26 10:44:56 +00:00
|
|
|
"info": evdev.device.DeviceInfo(5, 1, 5, 1),
|
2022-01-01 12:00:49 +00:00
|
|
|
"name": "input-remapper Bar Device",
|
2020-11-19 10:30:01 +00:00
|
|
|
},
|
2021-04-09 18:45:56 +00:00
|
|
|
# denylisted
|
2021-09-26 10:44:56 +00:00
|
|
|
"/dev/input/event51": {
|
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: keyboard_keys},
|
|
|
|
"phys": "usb-0000:03:00.0-5/input1",
|
|
|
|
"info": evdev.device.DeviceInfo(6, 1, 6, 1),
|
|
|
|
"name": "YuBiCofooYuBiKeYbar",
|
2021-04-09 18:45:56 +00:00
|
|
|
},
|
2020-11-19 10:30:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def setup_pipe(group_key):
|
2021-03-21 18:15:20 +00:00
|
|
|
"""Create a pipe that can be used to send events to the helper,
|
|
|
|
which in turn will be sent to the reader
|
|
|
|
"""
|
2021-04-23 09:51:21 +00:00
|
|
|
if pending_events.get(group_key) is None:
|
2022-07-23 08:53:41 +00:00
|
|
|
logger.info("creating Pipe for %s", group_key)
|
2021-04-23 09:51:21 +00:00
|
|
|
pending_events[group_key] = multiprocessing.Pipe()
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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():
|
2021-09-26 10:44:56 +00:00
|
|
|
if "group_key" in fixture:
|
|
|
|
setup_pipe(fixture["group_key"])
|
2021-03-21 18:15:20 +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
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def push_event(group_key, 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
|
|
|
|
----------
|
2021-04-23 09:51:21 +00:00
|
|
|
group_key : string
|
|
|
|
For example 'Foo Device'
|
2020-12-06 13:23:00 +00:00
|
|
|
event : InputEvent
|
2020-11-18 22:00:18 +00:00
|
|
|
"""
|
2021-04-23 09:51:21 +00:00
|
|
|
setup_pipe(group_key)
|
2022-07-23 08:53:41 +00:00
|
|
|
logger.info("Simulating %s for %s", event, group_key)
|
2021-04-23 09:51:21 +00:00
|
|
|
pending_events[group_key][0].send(event)
|
2020-11-18 22:00:18 +00:00
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def push_events(group_key, events):
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Push multiple events."""
|
2021-03-21 18:15:20 +00:00
|
|
|
for event in events:
|
2021-04-23 09:51:21 +00:00
|
|
|
push_event(group_key, event)
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2022-01-31 19:58:37 +00:00
|
|
|
event = InputEvent(sec, usec, type, code, value)
|
2021-01-05 18:33:47 +00:00
|
|
|
return event
|
2021-01-01 21:20:33 +00:00
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
def patch_paths():
|
2022-04-17 10:19:23 +00:00
|
|
|
from inputremapper import user
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
user.HOME = tmp
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
|
2020-12-19 15:04:07 +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
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
def __init__(self, path):
|
2021-09-26 10:44:56 +00:00
|
|
|
if path != "justdoit" and path not in fixtures:
|
2020-12-19 15:04:07 +00:00
|
|
|
raise FileNotFoundError()
|
2020-11-30 13:34:27 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
self.path = path
|
2021-01-05 18:33:47 +00:00
|
|
|
fixture = fixtures.get(path, {})
|
2021-09-26 10:44:56 +00:00
|
|
|
self.phys = fixture.get("phys", "unset")
|
|
|
|
self.info = fixture.get("info", evdev.device.DeviceInfo(None, None, None, None))
|
|
|
|
self.name = fixture.get("name", "unset")
|
2020-12-03 19:37:36 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
# this property exists only for test purposes and is not part of
|
|
|
|
# the original evdev.InputDevice class
|
2021-09-26 10:44:56 +00:00
|
|
|
self.group_key = fixture.get("group_key", self.name)
|
2021-01-05 18:33:47 +00:00
|
|
|
|
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
|
2021-04-23 09:51:21 +00:00
|
|
|
setup_pipe(self.group_key)
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.fd = pending_events[self.group_key][1].fileno()
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
def push_events(self, events):
|
|
|
|
push_events(self.group_key, events)
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def fileno(self):
|
|
|
|
"""Compatibility to select.select."""
|
|
|
|
return self.fd
|
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
def log(self, key, msg):
|
2022-01-18 20:20:42 +00:00
|
|
|
logger.info(f'%s "%s" "%s" %s', msg, self.name, self.path, key)
|
2021-01-01 21:20:33 +00:00
|
|
|
|
2020-12-26 15:46:01 +00:00
|
|
|
def absinfo(self, *args):
|
2021-09-26 10:44:56 +00:00
|
|
|
raise Exception("Ubuntus version of evdev doesn't support .absinfo")
|
2020-11-30 20:16:58 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
def grab(self):
|
2022-01-18 20:20:42 +00:00
|
|
|
logger.info("grab %s %s", self.name, self.path)
|
2021-04-26 21:21:52 +00:00
|
|
|
|
|
|
|
def ungrab(self):
|
2022-01-18 20:20:42 +00:00
|
|
|
logger.info("ungrab %s %s", self.name, self.path)
|
2020-11-30 20:16:58 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
async def async_read_loop(self):
|
2022-07-23 08:53:41 +00:00
|
|
|
logger.info("starting read loop for %s", self.path)
|
|
|
|
new_frame = asyncio.Event()
|
|
|
|
asyncio.get_running_loop().add_reader(self.fd, new_frame.set)
|
|
|
|
while True:
|
|
|
|
await new_frame.wait()
|
|
|
|
new_frame.clear()
|
|
|
|
if not pending_events[self.group_key][1].poll():
|
|
|
|
# todo: why? why do we need this?
|
|
|
|
# sometimes this happens, as if a other process calls recv on
|
|
|
|
# the pipe
|
|
|
|
continue
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
event = pending_events[self.group_key][1].recv()
|
|
|
|
logger.info("got %s at %s", event, self.path)
|
|
|
|
yield event
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
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-04-23 09:51:21 +00:00
|
|
|
if self.group_key not in pending_events:
|
2021-09-26 10:44:56 +00:00
|
|
|
self.log("no events to read", self.group_key)
|
2021-03-21 18:15:20 +00:00
|
|
|
return
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
# consume all of them
|
2021-04-23 09:51:21 +00:00
|
|
|
while pending_events[self.group_key][1].poll():
|
|
|
|
event = pending_events[self.group_key][1].recv()
|
2021-09-26 10:44:56 +00:00
|
|
|
self.log(event, "read")
|
2021-03-21 18:15:20 +00:00
|
|
|
yield event
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT)
|
|
|
|
|
|
|
|
def read_loop(self):
|
|
|
|
"""Endless loop that yields events."""
|
|
|
|
while True:
|
2021-04-23 09:51:21 +00:00
|
|
|
event = pending_events[self.group_key][1].recv()
|
2021-03-21 18:15:20 +00:00
|
|
|
if event is not None:
|
2021-09-26 10:44:56 +00:00
|
|
|
self.log(event, "read_loop")
|
2021-03-21 18:15:20 +00:00
|
|
|
yield event
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT)
|
2020-11-30 17:59:34 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
def read_one(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
"""Read one event or none if nothing available."""
|
2022-07-23 08:53:41 +00:00
|
|
|
if not pending_events.get(self.group_key):
|
2020-12-19 15:04:07 +00:00
|
|
|
return None
|
2020-11-30 17:59:34 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
if not pending_events[self.group_key][1].poll():
|
2020-12-19 15:04:07 +00:00
|
|
|
return None
|
2020-11-22 12:59:28 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
try:
|
2021-04-23 09:51:21 +00:00
|
|
|
event = pending_events[self.group_key][1].recv()
|
2021-04-02 13:08:36 +00:00
|
|
|
except (UnpicklingError, EOFError):
|
2021-03-27 12:21:35 +00:00
|
|
|
# failed in tests sometimes
|
|
|
|
return None
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.log(event, "read_one")
|
2020-12-19 15:04:07 +00:00
|
|
|
return event
|
2020-11-22 12:59:28 +00:00
|
|
|
|
2021-02-13 11:44:50 +00:00
|
|
|
def capabilities(self, absinfo=True, verbose=False):
|
2021-09-26 10:44:56 +00:00
|
|
|
result = copy.deepcopy(fixtures[self.path]["capabilities"])
|
2020-12-26 15:46:01 +00:00
|
|
|
|
|
|
|
if absinfo and evdev.ecodes.EV_ABS in result:
|
|
|
|
absinfo_obj = evdev.AbsInfo(
|
2021-09-26 10:44:56 +00:00
|
|
|
value=None,
|
|
|
|
min=MIN_ABS,
|
|
|
|
fuzz=None,
|
|
|
|
flat=None,
|
|
|
|
resolution=None,
|
|
|
|
max=MAX_ABS,
|
2020-12-26 15:46:01 +00:00
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
ev_abs = []
|
|
|
|
for ev_code in result[evdev.ecodes.EV_ABS]:
|
|
|
|
if ev_code in range(0x10, 0x18): # ABS_HAT0X - ABS_HAT3Y
|
|
|
|
absinfo_obj = evdev.AbsInfo(
|
|
|
|
value=None,
|
|
|
|
min=-1,
|
|
|
|
fuzz=None,
|
|
|
|
flat=None,
|
|
|
|
resolution=None,
|
|
|
|
max=1,
|
|
|
|
)
|
|
|
|
ev_abs.append((ev_code, absinfo_obj))
|
|
|
|
|
|
|
|
result[evdev.ecodes.EV_ABS] = ev_abs
|
2020-12-26 15:46:01 +00:00
|
|
|
|
|
|
|
return result
|
2020-11-28 14:43:24 +00:00
|
|
|
|
2022-02-27 15:11:07 +00:00
|
|
|
def input_props(self):
|
|
|
|
return []
|
|
|
|
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2021-02-18 19:38:14 +00:00
|
|
|
uinputs = {}
|
|
|
|
|
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
class UInput:
|
2021-09-26 10:44:56 +00:00
|
|
|
def __init__(self, events=None, name="unnamed", *args, **kwargs):
|
2020-12-19 15:04:07 +00:00
|
|
|
self.fd = 0
|
|
|
|
self.write_count = 0
|
2021-09-26 10:44:56 +00:00
|
|
|
self.device = InputDevice("justdoit")
|
2021-01-05 18:33:47 +00:00
|
|
|
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
|
|
|
|
2021-02-18 19:38:14 +00:00
|
|
|
global uinputs
|
|
|
|
uinputs[name] = self
|
|
|
|
|
2022-07-25 08:55:26 +00:00
|
|
|
def capabilities(self, verbose=False, absinfo=True):
|
|
|
|
if absinfo or 3 not in self.events:
|
|
|
|
return self.events
|
|
|
|
else:
|
|
|
|
events = self.events.copy()
|
|
|
|
events[3] = [code for code, _ in self.events[3]]
|
|
|
|
return events
|
2020-12-06 18:54:02 +00:00
|
|
|
|
2020-12-19 15:04:07 +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)
|
2020-12-19 15:04:07 +00:00
|
|
|
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)
|
2022-01-18 20:20:42 +00:00
|
|
|
logger.info("%s written", (type, code, value))
|
2020-11-18 22:00:18 +00:00
|
|
|
|
2020-12-19 15:04:07 +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):
|
2021-09-26 10:44:56 +00:00
|
|
|
return InputEvent(self.sec, self.usec, self.type, self.code, self.value)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-01-05 18:33:47 +00:00
|
|
|
|
2020-12-19 15:04:07 +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: (
|
2021-09-26 10:44:56 +00:00
|
|
|
f"InputEvent{(self.type, self.code, self.value)}"
|
2021-01-05 18:33:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def patch_os_system():
|
|
|
|
"""Avoid running pkexec."""
|
|
|
|
original_system = os.system
|
|
|
|
|
|
|
|
def system(command):
|
2021-09-26 10:44:56 +00:00
|
|
|
if "pkexec" in command:
|
2021-03-21 18:15:20 +00:00
|
|
|
# because it
|
|
|
|
# - will open a window for user input
|
|
|
|
# - has no knowledge of the fixtures and patches
|
2021-09-26 10:44:56 +00:00
|
|
|
raise Exception("Write patches to avoid running pkexec stuff")
|
2021-03-21 18:15:20 +00:00
|
|
|
return original_system(command)
|
|
|
|
|
|
|
|
os.system = system
|
|
|
|
|
|
|
|
|
2021-08-22 11:41:52 +00:00
|
|
|
def patch_check_output():
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Xmodmap -pke should always return a fixed set of symbols.
|
2021-08-22 11:41:52 +00:00
|
|
|
|
|
|
|
On some installations the `xmodmap` command might be missig completely,
|
|
|
|
which would break the tests.
|
|
|
|
"""
|
|
|
|
original_check_output = subprocess.check_output
|
|
|
|
|
|
|
|
def check_output(command, *args, **kwargs):
|
2021-09-26 10:44:56 +00:00
|
|
|
if "xmodmap" in command and "-pke" in command:
|
2021-08-22 11:41:52 +00:00
|
|
|
return xmodmap
|
|
|
|
return original_check_output(command, *args, **kwargs)
|
|
|
|
|
|
|
|
subprocess.check_output = check_output
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
|
|
|
|
|
|
|
|
log = file if hasattr(file, "write") else sys.stderr
|
|
|
|
traceback.print_stack(file=log)
|
|
|
|
log.write(warnings.formatwarning(message, category, filename, lineno, line))
|
|
|
|
|
|
|
|
|
|
|
|
def patch_warnings():
|
|
|
|
# show traceback
|
|
|
|
warnings.showwarning = warn_with_traceback
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
|
|
|
|
|
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()
|
2021-08-22 11:41:52 +00:00
|
|
|
patch_check_output()
|
2022-07-23 08:53:41 +00:00
|
|
|
# patch_warnings()
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.logger import update_verbosity
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
update_verbosity(True)
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.daemon import DaemonProxy
|
2022-04-17 10:19:23 +00:00
|
|
|
from inputremapper.input_event import InputEvent as InternalInputEvent
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.injection.injector import Injector, RUNNING, STOPPED
|
|
|
|
from inputremapper.injection.macros.macro import macro_variables
|
|
|
|
from inputremapper.injection.global_uinputs import GlobalUInputs
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.global_config import global_config
|
2022-04-17 10:19:23 +00:00
|
|
|
from inputremapper.configs.mapping import Mapping, UIMapping
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.groups import groups, _Groups
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.system_mapping import system_mapping
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.gui.message_broker import MessageBroker
|
|
|
|
from inputremapper.gui.reader import Reader
|
|
|
|
from inputremapper.configs.paths import get_config_path, get_preset_path
|
|
|
|
from inputremapper.configs.preset import Preset
|
2022-04-17 10:19:23 +00:00
|
|
|
|
2022-01-14 17:50:57 +00:00
|
|
|
from inputremapper.injection.global_uinputs import global_uinputs
|
2020-10-26 22:45:22 +00:00
|
|
|
|
2020-12-06 18:54:02 +00:00
|
|
|
# no need for a high number in tests
|
2021-04-26 21:21:52 +00:00
|
|
|
Injector.regrab_timeout = 0.05
|
2020-12-06 18:54:02 +00:00
|
|
|
|
2020-12-24 00:26:34 +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
|
|
|
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def convert_to_internal_events(events):
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Convert a iterable of InputEvent to a list of inputremapper.InputEvent."""
|
2022-04-17 10:19:23 +00:00
|
|
|
return [InternalInputEvent.from_event(event) for event in events]
|
|
|
|
|
|
|
|
|
|
|
|
def get_key_mapping(
|
|
|
|
combination="99,99,99", target_uinput="keyboard", output_symbol="a"
|
|
|
|
) -> Mapping:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Convenient function to get a valid mapping."""
|
2022-04-17 10:19:23 +00:00
|
|
|
return Mapping(
|
|
|
|
event_combination=combination,
|
|
|
|
target_uinput=target_uinput,
|
|
|
|
output_symbol=output_symbol,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_ui_mapping(
|
|
|
|
combination="99,99,99", target_uinput="keyboard", output_symbol="a"
|
|
|
|
) -> UIMapping:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Convenient function to get a valid mapping."""
|
2022-04-17 10:19:23 +00:00
|
|
|
return UIMapping(
|
|
|
|
event_combination=combination,
|
|
|
|
target_uinput=target_uinput,
|
|
|
|
output_symbol=output_symbol,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-02-12 20:43:40 +00:00
|
|
|
def quick_cleanup(log=True):
|
2020-12-24 00:26:34 +00:00
|
|
|
"""Reset the applications state."""
|
2021-02-12 20:43:40 +00:00
|
|
|
if log:
|
2022-02-27 14:43:01 +00:00
|
|
|
print("Quick cleanup...")
|
2021-02-12 20:43:40 +00:00
|
|
|
|
2021-04-02 10:16:34 +00:00
|
|
|
for device in list(pending_events.keys()):
|
|
|
|
try:
|
|
|
|
while pending_events[device][1].poll():
|
|
|
|
pending_events[device][1].recv()
|
2021-04-02 13:08:36 +00:00
|
|
|
except (UnpicklingError, EOFError):
|
2021-04-02 10:16:34 +00:00
|
|
|
pass
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-04-03 10:48:52 +00:00
|
|
|
# setup new pipes for the next test
|
|
|
|
pending_events[device] = None
|
|
|
|
setup_pipe(device)
|
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
try:
|
|
|
|
if asyncio.get_event_loop().is_running():
|
|
|
|
for task in asyncio.all_tasks():
|
|
|
|
task.cancel()
|
|
|
|
except RuntimeError:
|
|
|
|
# happens when the event loop disappears for magical reasons
|
|
|
|
# create a fresh event loop
|
|
|
|
asyncio.set_event_loop(asyncio.new_event_loop())
|
2020-12-27 18:06:17 +00:00
|
|
|
|
2021-11-20 12:41:34 +00:00
|
|
|
if macro_variables.process is not None and not macro_variables.process.is_alive():
|
|
|
|
# nothing should stop the process during runtime, if it has been started by
|
|
|
|
# the injector once
|
2021-09-26 10:44:56 +00:00
|
|
|
raise AssertionError("the SharedDict manager is not running anymore")
|
2021-04-29 20:22:05 +00:00
|
|
|
|
2021-11-20 12:41:34 +00:00
|
|
|
if macro_variables.process is not None:
|
|
|
|
macro_variables._stop()
|
2021-04-27 22:17:51 +00:00
|
|
|
|
|
|
|
join_children()
|
|
|
|
|
2021-11-20 12:41:34 +00:00
|
|
|
macro_variables.start()
|
2021-04-27 22:17:51 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if os.path.exists(tmp):
|
|
|
|
shutil.rmtree(tmp)
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.path = os.path.join(get_config_path(), "config.json")
|
|
|
|
global_config.clear_config()
|
|
|
|
global_config._save_config()
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
system_mapping.populate()
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
clear_write_history()
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2021-02-20 13:16:05 +00:00
|
|
|
for name in list(uinputs.keys()):
|
|
|
|
del uinputs[name]
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# for device in list(active_macros.keys()):
|
|
|
|
# del active_macros[device]
|
|
|
|
# for device in list(unreleased.keys()):
|
|
|
|
# del unreleased[device]
|
2020-12-31 20:46:57 +00:00
|
|
|
|
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()):
|
2021-04-23 09:51:21 +00:00
|
|
|
fixtures[path] = copy.deepcopy(_fixture_copy[path])
|
2020-12-27 18:06:17 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
os.environ.update(environ_copy)
|
2021-04-02 10:16:34 +00:00
|
|
|
for device in list(os.environ.keys()):
|
|
|
|
if device not in environ_copy:
|
|
|
|
del os.environ[device]
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
for _, pipe in pending_events.values():
|
|
|
|
assert not pipe.poll()
|
|
|
|
|
2021-04-29 20:22:05 +00:00
|
|
|
assert macro_variables.is_alive(1)
|
2022-01-14 17:50:57 +00:00
|
|
|
for uinput in global_uinputs.devices.values():
|
|
|
|
uinput.write_count = 0
|
|
|
|
uinput.write_history = []
|
2021-04-27 22:17:51 +00:00
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
global_uinputs.is_service = True
|
|
|
|
|
2022-02-27 14:43:01 +00:00
|
|
|
if log:
|
|
|
|
print("Quick cleanup done")
|
|
|
|
|
2021-02-12 20:43:40 +00:00
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
"""Reset the applications state.
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
Using this is slower, usually quick_cleanup() is sufficient.
|
2021-02-12 20:43:40 +00:00
|
|
|
"""
|
2022-02-27 14:43:01 +00:00
|
|
|
print("Cleanup...")
|
2021-02-12 20:43:40 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
os.system("pkill -f input-remapper-service")
|
|
|
|
os.system("pkill -f input-remapper-control")
|
2021-02-12 20:43:40 +00:00
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
|
|
quick_cleanup(log=False)
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.refresh()
|
2022-01-14 17:50:57 +00:00
|
|
|
with patch.object(sys, "argv", ["input-remapper-service"]):
|
2022-03-17 09:32:22 +00:00
|
|
|
global_uinputs.prepare_all()
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2022-02-27 14:43:01 +00:00
|
|
|
print("Cleanup done")
|
|
|
|
|
2020-12-06 18:54:02 +00:00
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
def spy(obj, name):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Convenient wrapper for patch.object(..., ..., wraps=...)."""
|
|
|
|
return patch.object(obj, name, wraps=obj.__getattribute__(name))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
class FakeDaemonProxy:
|
|
|
|
def __init__(self):
|
|
|
|
self.calls = {
|
|
|
|
"stop_injecting": [],
|
|
|
|
"get_state": [],
|
|
|
|
"start_injecting": [],
|
|
|
|
"stop_all": 0,
|
|
|
|
"set_config_dir": [],
|
|
|
|
"autoload": 0,
|
|
|
|
"autoload_single": [],
|
|
|
|
"hello": [],
|
|
|
|
}
|
|
|
|
|
|
|
|
def stop_injecting(self, group_key: str) -> None:
|
|
|
|
self.calls["stop_injecting"].append(group_key)
|
|
|
|
|
|
|
|
def get_state(self, group_key: str) -> int:
|
|
|
|
self.calls["get_state"].append(group_key)
|
|
|
|
return STOPPED
|
|
|
|
|
|
|
|
def start_injecting(self, group_key: str, preset: str) -> bool:
|
|
|
|
self.calls["start_injecting"].append((group_key, preset))
|
|
|
|
return True
|
|
|
|
|
|
|
|
def stop_all(self) -> None:
|
|
|
|
self.calls["stop_all"] += 1
|
|
|
|
|
|
|
|
def set_config_dir(self, config_dir: str) -> None:
|
|
|
|
self.calls["set_config_dir"].append(config_dir)
|
|
|
|
|
|
|
|
def autoload(self) -> None:
|
|
|
|
self.calls["autoload"] += 1
|
|
|
|
|
|
|
|
def autoload_single(self, group_key: str) -> None:
|
|
|
|
self.calls["autoload_single"].append(group_key)
|
|
|
|
|
|
|
|
def hello(self, out: str) -> str:
|
|
|
|
self.calls["hello"].append(out)
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_presets():
|
|
|
|
"""prepare a few presets for use in tests
|
|
|
|
"Foo Device 2/preset3" is the newest and "Foo Device 2/preset2" is set to autoload
|
|
|
|
"""
|
|
|
|
preset1 = Preset(get_preset_path("Foo Device", "preset1"))
|
|
|
|
preset1.add(get_key_mapping(combination="1,1,1", output_symbol="b"))
|
|
|
|
preset1.add(get_key_mapping(combination="1,2,1"))
|
|
|
|
preset1.save()
|
|
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
preset2 = Preset(get_preset_path("Foo Device", "preset2"))
|
|
|
|
preset2.add(get_key_mapping(combination="1,3,1"))
|
|
|
|
preset2.add(get_key_mapping(combination="1,4,1"))
|
|
|
|
preset2.save()
|
|
|
|
|
|
|
|
time.sleep(0.05) # make sure the timestamp of preset 3 is the newest
|
|
|
|
preset3 = Preset(get_preset_path("Foo Device", "preset3"))
|
|
|
|
preset3.add(get_key_mapping(combination="1,5,1"))
|
|
|
|
preset3.save()
|
|
|
|
|
|
|
|
with open(get_config_path("config.json"), "w") as file:
|
|
|
|
json.dump({"autoload": {"Foo Device 2": "preset2"}}, file, indent=4)
|
|
|
|
|
|
|
|
global_config.load_config()
|
|
|
|
|
|
|
|
return preset1, preset2, preset3
|
|
|
|
|
|
|
|
|
2022-01-15 14:03:21 +00:00
|
|
|
cleanup()
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2022-01-15 14:03:21 +00:00
|
|
|
|
|
|
|
def main():
|
2022-02-07 16:52:41 +00:00
|
|
|
# https://docs.python.org/3/library/argparse.html
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
# repeated argument 0 or more times with modules
|
|
|
|
parser.add_argument("modules", type=str, nargs="*")
|
|
|
|
# start-dir value if not using modules, allows eg python tests/test.py --start-dir unit
|
|
|
|
parser.add_argument("--start-dir", type=str, default=".")
|
|
|
|
parsed_args = parser.parse_args() # takes from sys.argv by default
|
|
|
|
modules = parsed_args.modules
|
|
|
|
|
2020-10-26 22:45:22 +00:00
|
|
|
# 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
|
2022-01-17 23:55:01 +00:00
|
|
|
# `tests/test.py integration.test_gui.TestGui.test_can_start`
|
|
|
|
# or `tests/test.py integration.test_gui integration.test_daemon`
|
|
|
|
testsuite = unittest.defaultTestLoader.loadTestsFromNames(modules)
|
2020-10-26 22:45:22 +00:00
|
|
|
else:
|
|
|
|
# run all tests by default
|
2022-02-07 16:52:41 +00:00
|
|
|
testsuite = unittest.defaultTestLoader.discover(
|
|
|
|
parsed_args.start_dir, pattern="test_*.py"
|
|
|
|
)
|
2020-11-18 23:28:45 +00:00
|
|
|
|
2020-12-02 13:36:17 +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
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
unittest.TextTestResult.startTest = start_test
|
2022-01-17 23:35:31 +00:00
|
|
|
result = unittest.TextTestRunner(verbosity=2).run(testsuite)
|
|
|
|
sys.exit(not result.wasSuccessful())
|