File: test_parallel.py

package info (click to toggle)
input-remapper 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,856 kB
  • sloc: python: 27,277; sh: 191; xml: 33; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 3,888 bytes parent folder | download | duplicates (2)
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
#!/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 unittest

from evdev.ecodes import (
    EV_KEY,
    KEY_A,
    KEY_B,
    KEY_C,
    KEY_D,
)

from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import DummyMapping, MacroTestBase


@test_setup
class TestParallel(MacroTestBase):
    async def test_1_child_macro(self):
        macro = Parser.parse(
            "parallel(key(a))",
            self.context,
            DummyMapping(),
            True,
        )
        self.assertEqual(len(macro.tasks[0].child_macros), 1)
        await macro.run(self.handler)
        self.assertEqual(self.result, [(EV_KEY, KEY_A, 1), (EV_KEY, KEY_A, 0)])

    async def test_4_child_macros(self):
        macro = Parser.parse(
            "parallel(key(a), key(b), key(c), key(d))",
            self.context,
            DummyMapping(),
            True,
        )
        self.assertEqual(len(macro.tasks[0].child_macros), 4)
        await macro.run(self.handler)
        self.assertIn((EV_KEY, KEY_A, 0), self.result)
        self.assertIn((EV_KEY, KEY_B, 0), self.result)
        self.assertIn((EV_KEY, KEY_C, 0), self.result)
        self.assertIn((EV_KEY, KEY_D, 0), self.result)

    async def test_one_wait_takes_longer(self):
        mapping = DummyMapping()
        mapping.macro_key_sleep_ms = 0
        macro = Parser.parse(
            "parallel(wait(100), wait(10).key(b)).key(c)",
            self.context,
            mapping,
            True,
        )

        asyncio.ensure_future(macro.run(self.handler))
        await asyncio.sleep(0.06)
        # The wait(10).key(b) macro is already done, but KEY_C is not yet injected
        self.assertEqual(len(self.result), 2)
        self.assertIn((EV_KEY, KEY_B, 1), self.result)
        self.assertIn((EV_KEY, KEY_B, 0), self.result)

        # Both need to complete for it to continue to key(c)
        await asyncio.sleep(0.06)
        self.assertEqual(len(self.result), 4)
        self.assertIn((EV_KEY, KEY_C, 1), self.result)
        self.assertIn((EV_KEY, KEY_C, 0), self.result)

    async def test_parallel_hold(self):
        mapping = DummyMapping()
        mapping.macro_key_sleep_ms = 0
        macro = Parser.parse(
            "parallel(hold_keys(a), hold_keys(b)).key(c)",
            self.context,
            mapping,
            True,
        )

        macro.press_trigger()
        asyncio.ensure_future(macro.run(self.handler))
        await asyncio.sleep(0.05)
        self.assertIn((EV_KEY, KEY_A, 1), self.result)
        self.assertIn((EV_KEY, KEY_B, 1), self.result)
        self.assertEqual(len(self.result), 2)

        macro.release_trigger()
        await asyncio.sleep(0.05)
        self.assertIn((EV_KEY, KEY_A, 0), self.result)
        self.assertIn((EV_KEY, KEY_B, 0), self.result)
        self.assertIn((EV_KEY, KEY_C, 1), self.result)
        self.assertIn((EV_KEY, KEY_C, 0), self.result)
        self.assertEqual(len(self.result), 6)


if __name__ == "__main__":
    unittest.main()