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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2025 sezanzeb <b8x45ygc9@mozmail.com>
#
# This file is part of input-remapper.
#
# input-remapper 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.
#
# input-remapper 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 input-remapper. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import time
import unittest
from evdev.ecodes import (
EV_KEY,
)
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.injection.macros.macro import Macro
from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import MacroTestBase, DummyMapping
@test_setup
class TestMacros(MacroTestBase):
async def test_newlines(self):
macro = Parser.parse(
" repeat(2,\nkey(\nr ).key(minus\n )).key(m) ",
self.context,
DummyMapping,
)
r = keyboard_layout.get("r")
minus = keyboard_layout.get("minus")
m = keyboard_layout.get("m")
await macro.run(self.handler)
self.assertListEqual(
self.result,
[
(EV_KEY, r, 1),
(EV_KEY, r, 0),
(EV_KEY, minus, 1),
(EV_KEY, minus, 0),
(EV_KEY, r, 1),
(EV_KEY, r, 0),
(EV_KEY, minus, 1),
(EV_KEY, minus, 0),
(EV_KEY, m, 1),
(EV_KEY, m, 0),
],
)
self.assertEqual(self.count_child_macros(macro), 1)
self.assertEqual(self.count_tasks(macro), 4)
async def test_various(self):
start = time.time()
macro = Parser.parse(
"w(200).repeat(2,modify(w,\nrepeat(2,\tkey(BtN_LeFt))).w(10).key(k))",
self.context,
DummyMapping,
)
self.assertEqual(self.count_child_macros(macro), 3)
self.assertEqual(self.count_tasks(macro), 7)
w = keyboard_layout.get("w")
left = keyboard_layout.get("bTn_lEfT")
k = keyboard_layout.get("k")
await macro.run(self.handler)
num_pauses = 8 + 6 + 4
keystroke_time = num_pauses * DummyMapping.macro_key_sleep_ms
wait_time = 220
total_time = (keystroke_time + wait_time) / 1000
self.assertLess(time.time() - start, total_time * 1.2)
self.assertGreater(time.time() - start, total_time * 0.9)
expected = [(EV_KEY, w, 1)]
expected += [(EV_KEY, left, 1), (EV_KEY, left, 0)] * 2
expected += [(EV_KEY, w, 0)]
expected += [(EV_KEY, k, 1), (EV_KEY, k, 0)]
expected *= 2
self.assertListEqual(self.result, expected)
async def test_not_run(self):
# does nothing without .run
macro = Parser.parse("key(a).repeat(3, key(b))", self.context)
self.assertIsInstance(macro, Macro)
self.assertListEqual(self.result, [])
async def test_duplicate_run(self):
# it won't restart the macro, because that may screw up the
# internal state (in particular the _trigger_release_event).
# I actually don't know at all what kind of bugs that might produce,
# lets just avoid it. It might cause it to be held down forever.
a = keyboard_layout.get("a")
b = keyboard_layout.get("b")
c = keyboard_layout.get("c")
macro = Parser.parse(
"key(a).modify(b, hold()).key(c)", self.context, DummyMapping
)
asyncio.ensure_future(macro.run(self.handler))
self.assertFalse(macro.tasks[1].child_macros[0].tasks[0].is_holding())
asyncio.ensure_future(macro.run(self.handler)) # ignored
self.assertFalse(macro.tasks[1].child_macros[0].tasks[0].is_holding())
macro.press_trigger()
await asyncio.sleep(0.2)
self.assertTrue(macro.tasks[1].child_macros[0].tasks[0].is_holding())
asyncio.ensure_future(macro.run(self.handler)) # ignored
self.assertTrue(macro.tasks[1].child_macros[0].tasks[0].is_holding())
macro.release_trigger()
await asyncio.sleep(0.2)
self.assertFalse(macro.tasks[1].child_macros[0].tasks[0].is_holding())
expected = [
(EV_KEY, a, 1),
(EV_KEY, a, 0),
(EV_KEY, b, 1),
(EV_KEY, b, 0),
(EV_KEY, c, 1),
(EV_KEY, c, 0),
]
self.assertListEqual(self.result, expected)
"""not ignored, since previous run is over"""
asyncio.ensure_future(macro.run(self.handler))
macro.press_trigger()
await asyncio.sleep(0.2)
self.assertTrue(macro.tasks[1].child_macros[0].tasks[0].is_holding())
macro.release_trigger()
await asyncio.sleep(0.2)
self.assertFalse(macro.tasks[1].child_macros[0].tasks[0].is_holding())
expected = [
(EV_KEY, a, 1),
(EV_KEY, a, 0),
(EV_KEY, b, 1),
(EV_KEY, b, 0),
(EV_KEY, c, 1),
(EV_KEY, c, 0),
] * 2
self.assertListEqual(self.result, expected)
if __name__ == "__main__":
unittest.main()
|