2021-12-05 15:24:51 +01:00
|
|
|
import time
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
|
|
from kmk.hid import HIDModes
|
2022-07-03 17:31:39 +02:00
|
|
|
from kmk.keys import KC, ModifierKey
|
2021-12-05 15:24:51 +01:00
|
|
|
from kmk.kmk_keyboard import KMKKeyboard
|
2022-04-09 02:07:38 +02:00
|
|
|
from kmk.scanners import DiodeOrientation
|
2022-04-10 14:46:40 +02:00
|
|
|
from kmk.scanners.digitalio import MatrixScanner
|
2023-03-29 22:02:29 +02:00
|
|
|
from kmk.scheduler import _task_queue
|
2021-12-05 15:24:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DigitalInOut(Mock):
|
|
|
|
value = False
|
|
|
|
|
|
|
|
|
2022-07-03 17:31:39 +02:00
|
|
|
def code2name(code):
|
|
|
|
for name in KC:
|
|
|
|
try:
|
|
|
|
if KC[name].code == code:
|
|
|
|
return name
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
class KeyboardTest:
|
2023-03-08 21:14:24 +01:00
|
|
|
loop_delay_ms = 2
|
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
def __init__(
|
2022-04-29 20:21:16 +02:00
|
|
|
self,
|
|
|
|
modules,
|
|
|
|
keymap,
|
|
|
|
keyboard_debug_enabled=False,
|
|
|
|
debug_enabled=False,
|
|
|
|
extensions={},
|
2021-12-05 15:24:51 +01:00
|
|
|
):
|
|
|
|
self.debug_enabled = debug_enabled
|
|
|
|
|
|
|
|
self.keyboard = KMKKeyboard()
|
|
|
|
self.keyboard.debug_enabled = keyboard_debug_enabled
|
|
|
|
|
|
|
|
self.keyboard.modules = modules
|
2022-04-29 20:21:16 +02:00
|
|
|
self.keyboard.extensions = extensions
|
2021-12-05 15:24:51 +01:00
|
|
|
|
|
|
|
self.pins = tuple(DigitalInOut() for k in keymap[0])
|
|
|
|
|
|
|
|
self.keyboard.col_pins = (DigitalInOut(),)
|
|
|
|
self.keyboard.row_pins = self.pins
|
|
|
|
self.keyboard.diode_orientation = DiodeOrientation.COL2ROW
|
2022-04-09 21:34:06 +02:00
|
|
|
self.keyboard.matrix = MatrixScanner(
|
|
|
|
cols=self.keyboard.col_pins,
|
|
|
|
rows=self.keyboard.row_pins,
|
|
|
|
diode_orientation=self.keyboard.diode_orientation,
|
|
|
|
)
|
2021-12-05 15:24:51 +01:00
|
|
|
self.keyboard.keymap = keymap
|
|
|
|
|
|
|
|
self.keyboard._init(hid_type=HIDModes.NOOP)
|
|
|
|
|
|
|
|
@patch('kmk.hid.AbstractHID.hid_send')
|
2022-07-03 17:31:39 +02:00
|
|
|
def test(self, testname, key_events, assert_reports, hid_send):
|
2021-12-05 15:24:51 +01:00
|
|
|
if self.debug_enabled:
|
2022-07-03 17:31:39 +02:00
|
|
|
print(testname)
|
2021-12-05 15:24:51 +01:00
|
|
|
|
2022-07-03 17:31:39 +02:00
|
|
|
# setup report recording
|
|
|
|
hid_reports = []
|
|
|
|
hid_send.side_effect = lambda report: hid_reports.append(report[1:])
|
2021-12-05 15:24:51 +01:00
|
|
|
|
2022-07-03 17:31:39 +02:00
|
|
|
# inject key switch events
|
2022-07-04 22:21:57 +02:00
|
|
|
self.keyboard._main_loop()
|
2021-12-05 15:24:51 +01:00
|
|
|
for e in key_events:
|
|
|
|
if isinstance(e, int):
|
|
|
|
starttime_ms = time.time_ns() // 1_000_000
|
|
|
|
while time.time_ns() // 1_000_000 - starttime_ms < e:
|
|
|
|
self.do_main_loop()
|
|
|
|
else:
|
|
|
|
key_pos = e[0]
|
|
|
|
is_pressed = e[1]
|
|
|
|
self.pins[key_pos].value = is_pressed
|
|
|
|
self.do_main_loop()
|
2023-02-16 07:02:06 +01:00
|
|
|
|
|
|
|
# wait up to 10s for delayed actions to resolve, if there are any
|
|
|
|
timeout = time.time_ns() + 10 * 1_000_000_000
|
|
|
|
while timeout > time.time_ns():
|
|
|
|
self.do_main_loop()
|
2023-03-29 22:02:29 +02:00
|
|
|
if not _task_queue.peek() and not self.keyboard._resume_buffer:
|
2023-02-16 07:02:06 +01:00
|
|
|
break
|
|
|
|
assert timeout > time.time_ns(), 'infinite loop detected'
|
2021-12-05 15:24:51 +01:00
|
|
|
|
2022-07-03 17:31:39 +02:00
|
|
|
matching = True
|
|
|
|
for i in range(max(len(hid_reports), len(assert_reports))):
|
|
|
|
# prepare the generated report codes
|
|
|
|
try:
|
|
|
|
hid_report = hid_reports[i]
|
|
|
|
except IndexError:
|
|
|
|
report_mods = None
|
|
|
|
report_keys = [None]
|
|
|
|
else:
|
|
|
|
report_mods = hid_report[0]
|
|
|
|
report_keys = {code for code in hid_report[2:] if code != 0}
|
|
|
|
|
|
|
|
# prepare the desired report codes
|
|
|
|
try:
|
|
|
|
hid_assert = assert_reports[i]
|
|
|
|
except IndexError:
|
|
|
|
assert_mods = None
|
|
|
|
assert_keys = [None]
|
|
|
|
else:
|
|
|
|
assert_mods = 0
|
|
|
|
assert_keys = set()
|
|
|
|
for k in hid_assert:
|
|
|
|
if isinstance(k, ModifierKey):
|
|
|
|
assert_mods |= k.code
|
|
|
|
else:
|
|
|
|
assert_keys.add(k.code)
|
|
|
|
|
|
|
|
# accumulate assertion for late evalution, -- makes for a more
|
|
|
|
# helpfull debug output.
|
|
|
|
matching = matching and report_mods == assert_mods
|
|
|
|
matching = matching and report_keys == assert_keys
|
|
|
|
|
2021-12-05 15:24:51 +01:00
|
|
|
if self.debug_enabled:
|
2022-07-03 17:31:39 +02:00
|
|
|
report_keys_names = {code2name(c) for c in report_keys}
|
|
|
|
assert_keys_names = {code2name(c) for c in assert_keys}
|
2021-12-05 15:24:51 +01:00
|
|
|
print(
|
2022-07-03 17:31:39 +02:00
|
|
|
f'assert '
|
|
|
|
f'mods: {report_mods} == {assert_mods} '
|
|
|
|
f'keys: {report_keys_names} == {assert_keys_names} '
|
2021-12-05 15:24:51 +01:00
|
|
|
)
|
2022-07-03 17:31:39 +02:00
|
|
|
|
|
|
|
assert matching, "reports don't match up"
|
2021-12-05 15:24:51 +01:00
|
|
|
|
|
|
|
def do_main_loop(self):
|
2022-07-03 17:31:39 +02:00
|
|
|
self.keyboard._main_loop()
|
2023-03-08 21:14:24 +01:00
|
|
|
time.sleep(self.loop_delay_ms / 1000)
|