Skip to content

Commit

Permalink
Robot/Keyboard test doesn't work at first attempt (Bugfix) (#1459)
Browse files Browse the repository at this point in the history
Fix: zapper keyboard test doesn't work at first attempt
  • Loading branch information
p-gentili authored Sep 25, 2024
1 parent da70aef commit 09cf5e3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
21 changes: 18 additions & 3 deletions providers/base/bin/zapper_keyboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@
import struct
import sys
import threading
import time
from enum import Enum
from pathlib import Path

from checkbox_support.scripts.zapper_proxy import zapper_run # noqa: E402

ROBOT_INIT = """
*** Settings ***
Library libraries/ZapperHid.py
*** Test Cases ***
Do nothing
Log Re-configure HID device
"""

ROBOT_TESTCASE_COMBO = """
*** Settings ***
Expand Down Expand Up @@ -124,8 +133,12 @@ def get_zapper_kbd_device():
"""
zapper_kbd = "usb-Canonical_Zapper_main_board_123456*-event-kbd"

for file_path in Path("/dev/input/by-id/").glob(zapper_kbd):
return str(file_path)
start = time.time()
for _ in range(5):
for file_path in Path("/dev/input/by-id/").glob(zapper_kbd):
print(time.time() - start)
return str(file_path)
time.sleep(1)
raise FileNotFoundError("Cannot find Zapper Keyboard.")


Expand All @@ -138,6 +151,9 @@ def main(argv):
if len(argv) != 2:
raise SystemExit("Usage: {} <zapper-ip>".format(argv[0]))

# A simple robot-run to initialize the Zapper HID device
zapper_run(argv[1], "robot_run", ROBOT_INIT.encode(), {}, {})

try:
zapper_kbd = get_zapper_kbd_device()
except FileNotFoundError as exc:
Expand All @@ -150,7 +166,6 @@ def main(argv):
listener = KeyboardListener(zapper_kbd, events.append)
listener.start()

zapper_run(argv[1], "reset_hid_state")
try:
assert_key_combo(argv[1], events)
assert_type_string(argv[1], events)
Expand Down
29 changes: 23 additions & 6 deletions providers/base/tests/test_zapper_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ def test_main_no_args(self):
with self.assertRaises(SystemExit):
zapper_keyboard_test.main([1])

@patch("time.sleep", Mock())
@patch("zapper_keyboard_test.Path")
def test_get_zapper_kbd_device(self, mock_path):
"""
Test whether the function returns a path to the Zapper
keyboard device when it's the only Zapper HID device.
"""

mock_path.return_value.glob.return_value = [
Path(
"/dev/input/by-id/"
"usb-Canonical_Zapper_main_board_123456-event-kbd",
)
mock_path.return_value.glob.side_effect = [
[],
[],
[],
[],
[
Path(
"/dev/input/by-id/"
"usb-Canonical_Zapper_main_board_123456-event-kbd",
)
],
]
device = zapper_keyboard_test.get_zapper_kbd_device()
self.assertEqual(
Expand All @@ -80,6 +87,7 @@ def test_get_zapper_kbd_device(self, mock_path):
"usb-Canonical_Zapper_main_board_123456-event-kbd",
)

@patch("time.sleep", Mock())
@patch("zapper_keyboard_test.Path")
def test_get_zapper_kbd_device_if01(self, mock_path):
"""
Expand All @@ -100,6 +108,7 @@ def test_get_zapper_kbd_device_if01(self, mock_path):
"usb-Canonical_Zapper_main_board_123456-if01-event-kbd",
)

@patch("time.sleep", Mock())
@patch("zapper_keyboard_test.Path")
def test_get_zapper_kbd_device_not_found(self, mock_path):
"""
Expand All @@ -111,13 +120,15 @@ def test_get_zapper_kbd_device_not_found(self, mock_path):
with self.assertRaises(FileNotFoundError):
zapper_keyboard_test.get_zapper_kbd_device()

@patch("zapper_keyboard_test.zapper_run", Mock())
@patch("zapper_keyboard_test.get_zapper_kbd_device")
def test_main_no_keyboard(self, mock_get_dev):
"""Check main exits with failure if Zapper keyboard is missing."""
mock_get_dev.side_effect = FileNotFoundError
with self.assertRaises(SystemExit):
zapper_keyboard_test.main([1, 2])

@patch("zapper_keyboard_test.zapper_run", Mock())
@patch("zapper_keyboard_test.get_zapper_kbd_device")
@patch("os.access")
def test_main_no_file_or_permission(self, mock_access, mock_get_dev):
Expand Down Expand Up @@ -154,7 +165,13 @@ def test_main(
mock_key.return_value.start.assert_called_once_with()
mock_key.return_value.stop.assert_called_once_with()
mock_key.return_value.join.assert_called_once_with()
mock_run.assert_called_once_with("127.0.0.1", "reset_hid_state")
mock_run.assert_called_once_with(
"127.0.0.1",
"robot_run",
zapper_keyboard_test.ROBOT_INIT.encode(),
{},
{},
)

mock_combo.side_effect = None
mock_type.side_effect = AssertionError
Expand Down

0 comments on commit 09cf5e3

Please sign in to comment.