input-remapper/tests/testcases/macros.py

121 lines
3.9 KiB
Python
Raw Normal View History

2020-11-28 16:49:32 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
# 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/>.
2020-11-28 19:59:24 +00:00
import time
2020-11-28 16:49:32 +00:00
import unittest
2020-11-28 19:59:24 +00:00
import asyncio
2020-11-28 16:49:32 +00:00
2020-11-28 19:59:24 +00:00
from keymapper.dev.macros import parse, _Macro
2020-11-28 21:54:22 +00:00
from keymapper.config import config
2020-11-28 16:49:32 +00:00
class TestMacros(unittest.TestCase):
2020-11-28 17:27:28 +00:00
def setUp(self):
self.result = []
2020-11-28 19:59:24 +00:00
self.loop = asyncio.get_event_loop()
2020-11-28 17:27:28 +00:00
def tearDown(self):
self.result = []
2020-11-28 16:49:32 +00:00
def handler(self, char, value):
"""Where macros should write characters to."""
self.result.append((char, value))
2020-11-28 17:27:28 +00:00
def test_0(self):
2020-11-28 19:59:24 +00:00
self.loop.run_until_complete(parse('k(1)', self.handler).run())
2020-11-28 17:27:28 +00:00
self.assertListEqual(self.result, [(1, 1), (1, 0)])
def test_1(self):
2020-11-28 22:44:40 +00:00
macro = 'k(1).k(a).k(3)'
2020-11-28 19:59:24 +00:00
self.loop.run_until_complete(parse(macro, self.handler).run())
2020-11-28 17:27:28 +00:00
self.assertListEqual(self.result, [
2020-11-28 22:44:40 +00:00
(1, 1), (1, 0),
2020-11-28 17:27:28 +00:00
('a', 1), ('a', 0),
(3, 1), (3, 0),
])
2020-11-28 16:49:32 +00:00
def test_2(self):
2020-11-28 19:59:24 +00:00
start = time.time()
2020-11-28 21:54:22 +00:00
repeats = 20
macro = f'r({repeats}, k(k))'
2020-11-28 19:59:24 +00:00
self.loop.run_until_complete(parse(macro, self.handler).run())
2020-11-28 21:54:22 +00:00
sleep_time = 2 * repeats * config.get_keystroke_sleep() / 1000
self.assertGreater(time.time() - start, sleep_time * 0.9)
self.assertLess(time.time() - start, sleep_time * 1.1)
self.assertListEqual(self.result, [('k', 1), ('k', 0)] * repeats)
2020-11-28 16:49:32 +00:00
def test_3(self):
2020-11-28 19:59:24 +00:00
start = time.time()
2020-11-28 21:54:22 +00:00
macro = 'r(3, k(m).w(100))'
2020-11-28 19:59:24 +00:00
self.loop.run_until_complete(parse(macro, self.handler).run())
2020-11-28 21:54:22 +00:00
keystroke_time = 6 * config.get_keystroke_sleep()
total_time = keystroke_time + 300
total_time /= 1000
self.assertGreater(time.time() - start, total_time * 0.9)
self.assertLess(time.time() - start, total_time * 1.1)
2020-11-28 17:27:28 +00:00
self.assertListEqual(self.result, [
('m', 1), ('m', 0),
('m', 1), ('m', 0),
('m', 1), ('m', 0),
])
2020-11-28 16:49:32 +00:00
def test_4(self):
2020-11-28 22:44:40 +00:00
macro = ' r(2,\nk(\nr ).k(-\n )).k(m) '
2020-11-28 19:59:24 +00:00
self.loop.run_until_complete(parse(macro, self.handler).run())
2020-11-28 17:27:28 +00:00
self.assertListEqual(self.result, [
('r', 1), ('r', 0),
('-', 1), ('-', 0),
('r', 1), ('r', 0),
('-', 1), ('-', 0),
('m', 1), ('m', 0),
])
def test_5(self):
2020-11-28 19:59:24 +00:00
start = time.time()
2020-11-28 22:44:40 +00:00
macro = 'w(200).r(2,m(w,\nr(2,\tk(r))).w(10).k(k))'
2020-11-28 19:59:24 +00:00
self.loop.run_until_complete(parse(macro, self.handler).run())
2020-11-28 21:54:22 +00:00
num_pauses = 8 + 6 + 4
keystroke_time = num_pauses * config.get_keystroke_sleep()
wait_time = 220
total_time = (keystroke_time + wait_time) / 1000
self.assertLess(time.time() - start, total_time * 1.1)
self.assertGreater(time.time() - start, total_time * 0.9)
2020-11-28 17:27:28 +00:00
expected = [('w', 1)]
expected += [('r', 1), ('r', 0)] * 2
expected += [('w', 0)]
expected += [('k', 1), ('k', 0)]
expected *= 2
self.assertListEqual(self.result, expected)
2020-11-28 16:49:32 +00:00
2020-11-28 17:27:28 +00:00
def test_6(self):
2020-11-28 19:59:24 +00:00
# does nothing without .run
ret = parse('k(a).r(3, k(b))', self.handler)
self.assertIsInstance(ret, _Macro)
2020-11-28 17:27:28 +00:00
self.assertListEqual(self.result, [])
2020-11-28 16:49:32 +00:00
2020-11-28 17:27:28 +00:00
if __name__ == '__main__':
2020-11-28 16:49:32 +00:00
unittest.main()