1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
import asyncio
import time
import unittest
from unittest.mock import patch
import evdev
from evdev.ecodes import KEY_A, EV_KEY, KEY_B, KEY_LEFTSHIFT, KEY_C
from inputremapper.configs.input_config import InputConfig
from inputremapper.configs.mapping import Mapping
from inputremapper.configs.preset import Preset
from inputremapper.injection.context import Context
from inputremapper.injection.event_reader import EventReader
from inputremapper.injection.global_uinputs import GlobalUInputs, UInput
from inputremapper.injection.macros.parse import Parser
from inputremapper.injection.mapping_handlers.mapping_parser import MappingParser
from inputremapper.input_event import InputEvent
from tests.lib.fixtures import fixtures
from tests.lib.patches import InputDevice
from tests.lib.pipes import uinput_write_history
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import MacroTestBase, DummyMapping
@test_setup
class TestModTapIntegration(unittest.IsolatedAsyncioTestCase):
# Testcases are from https://github.com/qmk/qmk_firmware/blob/78a0adfbb4d2c4e12f93f2a62ded0020d406243e/docs/tap_hold.md#nested-tap-abba-nested-tap
# This test-setup is a bit more involved, because I needed to modify the
# EventReader as well for this to work.
def setUp(self):
self.origin_hash = fixtures.bar_device.get_device_hash()
self.forward_uinput = evdev.UInput(name="test-forward-uinput")
self.source_device = InputDevice(fixtures.bar_device.path)
self.stop_event = asyncio.Event()
self.global_uinputs = GlobalUInputs(UInput)
self.global_uinputs.prepare_all()
self.target_uinput = self.global_uinputs.get_uinput("keyboard")
self.mapping_parser = MappingParser(self.global_uinputs)
self.mapping = Mapping.from_combination(
input_combination=[
InputConfig(
type=EV_KEY,
code=KEY_A,
origin_hash=self.origin_hash,
)
],
output_symbol="mod_tap(a, Shift_L)",
)
self.preset = Preset()
self.preset.add(self.mapping)
self.bootstrap_event_reader()
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def bootstrap_event_reader(self):
self.context = Context(
self.preset,
source_devices={self.origin_hash: self.source_device},
forward_devices={self.origin_hash: self.forward_uinput},
mapping_parser=self.mapping_parser,
)
self.event_reader = EventReader(
self.context,
self.source_device,
self.stop_event,
)
def write(self, type_, code, value):
self.target_uinput.write_event(InputEvent.from_tuple((type_, code, value)))
async def input(self, type_, code, value):
asyncio.ensure_future(
self.event_reader.handle(
InputEvent.from_tuple(
(
type_,
code,
value,
),
origin_hash=self.origin_hash,
)
)
)
# Make the main_loop iterate a bit for the event_reader to do its thing.
await asyncio.sleep(0)
async def test_distinct_taps_1(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.190)
await self.input(EV_KEY, KEY_A, 0)
await asyncio.sleep(0.020) # exceeds tapping_term here
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 0)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_A, 1)),
InputEvent.from_tuple((EV_KEY, KEY_A, 0)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
],
)
self.assertEqual(
self.target_uinput.write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_A, 1)),
InputEvent.from_tuple((EV_KEY, KEY_A, 0)),
],
)
self.assertEqual(
self.forward_uinput.write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
],
)
async def test_distinct_taps_2(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.220) # exceeds tapping_term here
await self.input(EV_KEY, KEY_A, 0)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 0)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 1)),
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 0)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
],
)
self.assertEqual(
self.target_uinput.write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 1)),
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 0)),
],
)
self.assertEqual(
self.forward_uinput.write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
],
)
async def test_nested_tap_1(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.100)
self.assertEqual(uinput_write_history, [])
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.020)
self.assertEqual(uinput_write_history, [])
await self.input(EV_KEY, KEY_B, 0)
await asyncio.sleep(0.050)
self.assertEqual(uinput_write_history, [])
await self.input(EV_KEY, KEY_A, 0)
# everything happened within the tapping_term, so the modifier is not activated.
# "ab" should be written, in the exact order of the input.
await asyncio.sleep(0.040)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_A, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
InputEvent.from_tuple((EV_KEY, KEY_A, 0)),
],
)
async def test_nested_tap_2(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.100)
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 0)
await asyncio.sleep(0.100) # exceeds tapping_term here
await self.input(EV_KEY, KEY_A, 0)
await asyncio.sleep(0.020)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 0)),
],
)
async def test_nested_tap_3(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.220) # exceeds tapping_term here
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 0)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_A, 0)
await asyncio.sleep(0.020)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 0)),
],
)
async def test_rolling_keys_1(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.100)
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_A, 0)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 0)
# everything happened within the tapping_term, so the modifier is not activated.
# "ab" should be written, in the exact order of the input.
await asyncio.sleep(0.100)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_A, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_A, 0)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
],
)
async def test_rolling_keys_2(self):
await self.input(EV_KEY, KEY_A, 1)
await asyncio.sleep(0.100)
await self.input(EV_KEY, KEY_B, 1)
await asyncio.sleep(0.100) # exceeds tapping_term here
await self.input(EV_KEY, KEY_A, 0)
await asyncio.sleep(0.020)
await self.input(EV_KEY, KEY_B, 0)
await asyncio.sleep(0.020)
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 1)),
InputEvent.from_tuple((EV_KEY, KEY_B, 1)),
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 0)),
InputEvent.from_tuple((EV_KEY, KEY_B, 0)),
],
)
async def test_many_keys_correct_order_without_sleep(self):
self.mapping.macro_key_sleep_ms = 0
await self.many_keys_correct_order()
async def test_many_keys_correct_order_with_sleep(self):
self.mapping.macro_key_sleep_ms = 20
await self.many_keys_correct_order()
async def many_keys_correct_order(self):
await self.input(EV_KEY, KEY_A, 1)
# Send many events to the listener. It has to make all of them wait.
for i in range(30):
await self.input(EV_KEY, i, 1)
# exceed tapping_term. mod_tap will inject the modifier and replay all the
# previous events.
await asyncio.sleep(0.201)
# mod_tap is busy replaying events. While it does that, inject this
await self.input(EV_KEY, 100, 1)
start = time.time()
timeout = 2
while len(uinput_write_history) < 32 and (time.time() - start) < timeout:
# Wait for it to complete
await asyncio.sleep(0.1)
self.assertEqual(len(uinput_write_history), 32)
# Expect it to cleanly handle all events before injecting 100. Expect
# everything to be in the correct order.
self.assertEqual(
uinput_write_history,
[
InputEvent.from_tuple((EV_KEY, KEY_LEFTSHIFT, 1)),
*[InputEvent.from_tuple((EV_KEY, i, 1)) for i in range(30)],
InputEvent.from_tuple((EV_KEY, 100, 1)),
],
)
async def test_mapped_second_key(self):
# Map b to c.
# While mod_tap is waiting for the timeout to happen, press b.
# We expect c to be written, because b goes through the handlers and
# gets mapped.
# The event_reader has to wait for listeners to complete for mod_tap to work, so
# that it hands them over to the other handlers when the time comes.
# That means however, that the event_readers loop blocks. Therefore, it was turned
# into a fire-and-forget kind of thing. When an event arrives, it just schedules
# asyncio to do that stuff later, and continues reading.
self.preset.add(
Mapping.from_combination(
input_combination=[
InputConfig(
type=EV_KEY,
code=KEY_B,
origin_hash=self.origin_hash,
)
],
output_symbol="c",
),
)
self.bootstrap_event_reader()
async def async_generator():
events = [
InputEvent(0, 0, EV_KEY, KEY_A, 1),
InputEvent(0, 0, EV_KEY, KEY_B, 1),
InputEvent(0, 0, EV_KEY, KEY_A, 0),
InputEvent(0, 0, EV_KEY, KEY_B, 0),
]
for event in events:
yield event
# Wait a bit. During runtime, events don't come in that quickly
# and the mod_tap macro needs some loop iterations until it adds
# the listener to the context.
await asyncio.sleep(0.010)
with patch.object(self.event_reader, "read_loop", async_generator):
await self.event_reader.run()
await asyncio.sleep(0.020)
self.assertIn(InputEvent(0, 0, EV_KEY, KEY_C, 1), uinput_write_history)
self.assertIn(InputEvent(0, 0, EV_KEY, KEY_C, 0), uinput_write_history)
self.assertIn(InputEvent(0, 0, EV_KEY, KEY_A, 1), uinput_write_history)
self.assertIn(InputEvent(0, 0, EV_KEY, KEY_A, 0), uinput_write_history)
self.assertNotIn(InputEvent(0, 0, EV_KEY, KEY_B, 1), uinput_write_history)
self.assertNotIn(InputEvent(0, 0, EV_KEY, KEY_B, 0), uinput_write_history)
@test_setup
class TestModTapUnit(MacroTestBase):
async def wait_for_timeout(self, macro):
macro = Parser.parse(macro, self.context, DummyMapping, True)
start = time.time()
# Awaiting macro.run will cause it to wait for the tapping_term.
# When it injects the modifier, release the trigger.
macro.press_trigger()
await macro.run(lambda *_, **__: macro.release_trigger())
return time.time() - start
async def test_tapping_term_configuration_default(self):
time_ = await self.wait_for_timeout("mod_tap(a, b)")
# + 2 times 10ms of keycode_pause
self.assertAlmostEqual(time_, 0.22, delta=0.02)
async def test_tapping_term_configuration_100(self):
time_ = await self.wait_for_timeout("mod_tap(a, b, 100)")
self.assertAlmostEqual(time_, 0.12, delta=0.02)
async def test_tapping_term_configuration_100_kwarg(self):
time_ = await self.wait_for_timeout("mod_tap(a, b, tapping_term=100)")
self.assertAlmostEqual(time_, 0.12, delta=0.02)
|