2020-11-07 23:54:19 +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-11-07 23:54:19 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-07 23:54:19 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-07 23:54:19 +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-11-07 23:54:19 +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/>.
|
2022-12-15 13:43:03 +00:00
|
|
|
from inputremapper.utils import get_device_hash
|
2022-11-23 21:55:28 +00:00
|
|
|
|
2022-10-16 12:56:21 +00:00
|
|
|
from inputremapper.gui.messages.message_broker import MessageBroker
|
2022-11-23 21:55:28 +00:00
|
|
|
from tests.lib.fixtures import new_event
|
|
|
|
from tests.lib.cleanup import cleanup, quick_cleanup
|
|
|
|
from tests.lib.constants import EVENT_READ_TIMEOUT, START_READING_DELAY
|
|
|
|
from tests.lib.logger import logger
|
|
|
|
from tests.lib.fixtures import fixtures
|
|
|
|
from tests.lib.pipes import push_events
|
|
|
|
from tests.lib.patches import InputDevice
|
2022-03-03 22:42:37 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
import os
|
2020-11-07 23:54:19 +00:00
|
|
|
import unittest
|
2021-03-21 18:15:20 +00:00
|
|
|
import time
|
|
|
|
import multiprocessing
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2020-12-26 15:46:01 +00:00
|
|
|
import evdev
|
|
|
|
from evdev.ecodes import EV_ABS, EV_KEY
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.groups import groups, _Groups
|
2022-11-11 23:48:59 +00:00
|
|
|
from inputremapper.gui.reader_client import ReaderClient
|
|
|
|
from inputremapper.gui.reader_service import ReaderService
|
2020-11-07 23:54:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTest(unittest.TestCase):
|
|
|
|
def test_stubs(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNotNone(groups.find(key="Foo Device 2"))
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def tearDown(self):
|
|
|
|
quick_cleanup()
|
|
|
|
|
2020-12-26 15:46:01 +00:00
|
|
|
def test_fake_capabilities(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
device = InputDevice("/dev/input/event30")
|
2020-12-26 15:46:01 +00:00
|
|
|
capabilities = device.capabilities(absinfo=False)
|
|
|
|
self.assertIsInstance(capabilities, dict)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS], list)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS][0], int)
|
|
|
|
|
|
|
|
capabilities = device.capabilities()
|
|
|
|
self.assertIsInstance(capabilities, dict)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS], list)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS][0], tuple)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS][0][0], int)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS][0][1], evdev.AbsInfo)
|
|
|
|
self.assertIsInstance(capabilities[EV_ABS][0][1].max, int)
|
|
|
|
self.assertIsInstance(capabilities, dict)
|
|
|
|
self.assertIsInstance(capabilities[EV_KEY], list)
|
|
|
|
self.assertIsInstance(capabilities[EV_KEY][0], int)
|
|
|
|
|
|
|
|
def test_restore_fixtures(self):
|
2022-08-28 10:38:40 +00:00
|
|
|
fixtures["/bar/dev"] = {"name": "bla"}
|
2020-12-26 15:46:01 +00:00
|
|
|
cleanup()
|
2022-08-28 10:38:40 +00:00
|
|
|
self.assertIsNone(fixtures.get("/bar/dev"))
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNotNone(fixtures.get("/dev/input/event11"))
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
def test_restore_os_environ(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
os.environ["foo"] = "bar"
|
|
|
|
del os.environ["USER"]
|
2021-01-07 16:15:12 +00:00
|
|
|
environ = os.environ
|
|
|
|
cleanup()
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIn("USER", environ)
|
|
|
|
self.assertNotIn("foo", environ)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def test_push_events(self):
|
2022-11-11 23:48:59 +00:00
|
|
|
"""Test that push_event works properly between reader service and client.
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
Using push_events after the reader-service is already started should work,
|
2021-03-21 18:15:20 +00:00
|
|
|
as well as using push_event twice
|
|
|
|
"""
|
2022-11-11 23:48:59 +00:00
|
|
|
reader_client = ReaderClient(MessageBroker(), groups)
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
def create_reader_service():
|
|
|
|
# this will cause pending events to be copied over to the reader-service
|
2021-03-21 18:15:20 +00:00
|
|
|
# process
|
2022-11-11 23:48:59 +00:00
|
|
|
def start_reader_service():
|
2022-07-23 08:53:41 +00:00
|
|
|
# there is no point in using the global groups object
|
2022-11-11 23:48:59 +00:00
|
|
|
# because the reader-service runs in a different process
|
|
|
|
reader_service = ReaderService(_Groups())
|
|
|
|
reader_service.run()
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
self.reader_service = multiprocessing.Process(target=start_reader_service)
|
|
|
|
self.reader_service.start()
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
def wait_for_results():
|
2022-11-11 23:48:59 +00:00
|
|
|
# wait for the reader-service to send stuff
|
2021-03-21 18:15:20 +00:00
|
|
|
for _ in range(10):
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT)
|
2022-11-11 23:48:59 +00:00
|
|
|
if reader_client._results_pipe.poll():
|
2021-03-21 18:15:20 +00:00
|
|
|
break
|
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
create_reader_service()
|
|
|
|
reader_client.set_group(groups.find(key="Foo Device 2"))
|
|
|
|
reader_client.start_recorder()
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(START_READING_DELAY)
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
event = new_event(EV_KEY, 102, 1)
|
2022-08-28 10:38:40 +00:00
|
|
|
push_events(fixtures.foo_device_2_keyboard, [event])
|
2021-03-21 18:15:20 +00:00
|
|
|
wait_for_results()
|
2022-11-11 23:48:59 +00:00
|
|
|
self.assertTrue(reader_client._results_pipe.poll())
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
reader_client._read()
|
|
|
|
self.assertFalse(reader_client._results_pipe.poll())
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
# can push more events to the reader-service that is inside a separate
|
2021-03-21 18:15:20 +00:00
|
|
|
# process, which end up being sent to the reader
|
2022-07-23 08:53:41 +00:00
|
|
|
event = new_event(EV_KEY, 102, 0)
|
2022-11-11 23:48:59 +00:00
|
|
|
logger.info("push_events")
|
2022-08-28 10:38:40 +00:00
|
|
|
push_events(fixtures.foo_device_2_keyboard, [event])
|
2021-03-21 18:15:20 +00:00
|
|
|
wait_for_results()
|
2022-11-11 23:48:59 +00:00
|
|
|
logger.info("assert")
|
|
|
|
self.assertTrue(reader_client._results_pipe.poll())
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-11-11 23:48:59 +00:00
|
|
|
reader_client.terminate()
|
2022-07-23 08:53:41 +00:00
|
|
|
|
2022-12-15 13:43:03 +00:00
|
|
|
def test_device_hash_from_fixture_is_correct(self):
|
|
|
|
for fixture in fixtures:
|
|
|
|
self.assertEqual(
|
|
|
|
fixture.get_device_hash(), get_device_hash(InputDevice(fixture.path))
|
|
|
|
)
|
|
|
|
|
2020-11-07 23:54:19 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|