2020-11-07 23:54:19 +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-11-07 23:54:19 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
from keymapper.groups import groups
|
2021-03-21 18:15:20 +00:00
|
|
|
from keymapper.gui.reader import reader
|
|
|
|
from keymapper.gui.helper import RootHelper
|
2020-11-07 23:54:19 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
from tests.test import (
|
|
|
|
InputDevice,
|
|
|
|
quick_cleanup,
|
|
|
|
cleanup,
|
|
|
|
fixtures,
|
|
|
|
new_event,
|
|
|
|
push_events,
|
|
|
|
EVENT_READ_TIMEOUT,
|
|
|
|
START_READING_DELAY,
|
|
|
|
)
|
2020-12-26 15:46:01 +00:00
|
|
|
|
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):
|
|
|
|
fixtures[1] = [1234]
|
2021-09-26 10:44:56 +00:00
|
|
|
del fixtures["/dev/input/event11"]
|
2020-12-26 15:46:01 +00:00
|
|
|
cleanup()
|
|
|
|
self.assertIsNone(fixtures.get(1))
|
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):
|
|
|
|
"""Test that push_event works properly between helper and reader.
|
|
|
|
|
|
|
|
Using push_events after the helper is already forked should work,
|
|
|
|
as well as using push_event twice
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def create_helper():
|
|
|
|
# this will cause pending events to be copied over to the helper
|
|
|
|
# process
|
|
|
|
def start_helper():
|
|
|
|
helper = RootHelper()
|
|
|
|
helper.run()
|
|
|
|
|
|
|
|
self.helper = multiprocessing.Process(target=start_helper)
|
|
|
|
self.helper.start()
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
def wait_for_results():
|
|
|
|
# wait for the helper to send stuff
|
|
|
|
for _ in range(10):
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT)
|
|
|
|
if reader._results.poll():
|
|
|
|
break
|
|
|
|
|
|
|
|
event = new_event(EV_KEY, 102, 1)
|
|
|
|
create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(START_READING_DELAY)
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("Foo Device 2", [event])
|
2021-03-21 18:15:20 +00:00
|
|
|
wait_for_results()
|
|
|
|
self.assertTrue(reader._results.poll())
|
|
|
|
|
|
|
|
reader.clear()
|
|
|
|
self.assertFalse(reader._results.poll())
|
|
|
|
|
|
|
|
# can push more events to the helper that is inside a separate
|
|
|
|
# process, which end up being sent to the reader
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("Foo Device 2", [event])
|
2021-03-21 18:15:20 +00:00
|
|
|
wait_for_results()
|
|
|
|
self.assertTrue(reader._results.poll())
|
|
|
|
|
2020-11-07 23:54:19 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|