|
| 1 | +"""Tests for RPA.Desktop keyboard and mouse keyword utilities.""" |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +# --- to_key --- |
| 8 | + |
| 9 | + |
| 10 | +def test_to_key_special_keys(): |
| 11 | + pytest.importorskip("pynput") |
| 12 | + from pynput.keyboard import Key |
| 13 | + |
| 14 | + from RPA.Desktop.keywords.keyboard import to_key |
| 15 | + |
| 16 | + assert to_key("ctrl") == Key.ctrl |
| 17 | + assert to_key("shift") == Key.shift |
| 18 | + assert to_key("enter") == Key.enter |
| 19 | + assert to_key("alt") == Key.alt |
| 20 | + assert to_key("tab") == Key.tab |
| 21 | + assert to_key("delete") == Key.delete |
| 22 | + assert to_key("f4") == Key.f4 |
| 23 | + assert to_key("space") == Key.space |
| 24 | + |
| 25 | + |
| 26 | +def test_to_key_single_character(): |
| 27 | + pytest.importorskip("pynput") |
| 28 | + from pynput.keyboard import KeyCode |
| 29 | + |
| 30 | + from RPA.Desktop.keywords.keyboard import to_key |
| 31 | + |
| 32 | + assert isinstance(to_key("a"), KeyCode) |
| 33 | + assert isinstance(to_key("z"), KeyCode) |
| 34 | + assert isinstance(to_key("1"), KeyCode) |
| 35 | + |
| 36 | + |
| 37 | +def test_to_key_case_insensitive(): |
| 38 | + pytest.importorskip("pynput") |
| 39 | + from pynput.keyboard import Key |
| 40 | + |
| 41 | + from RPA.Desktop.keywords.keyboard import to_key |
| 42 | + |
| 43 | + assert to_key("CTRL") == Key.ctrl |
| 44 | + assert to_key("Enter") == Key.enter |
| 45 | + assert to_key("SHIFT") == Key.shift |
| 46 | + |
| 47 | + |
| 48 | +def test_to_key_passthrough(): |
| 49 | + pytest.importorskip("pynput") |
| 50 | + from pynput.keyboard import Key, KeyCode |
| 51 | + |
| 52 | + from RPA.Desktop.keywords.keyboard import to_key |
| 53 | + |
| 54 | + assert to_key(Key.ctrl) is Key.ctrl |
| 55 | + kc = KeyCode.from_char("a") |
| 56 | + assert to_key(kc) is kc |
| 57 | + |
| 58 | + |
| 59 | +def test_to_key_invalid_raises(): |
| 60 | + pytest.importorskip("pynput") |
| 61 | + from RPA.Desktop.keywords.keyboard import to_key |
| 62 | + |
| 63 | + with pytest.raises(ValueError, match="Invalid key"): |
| 64 | + to_key("notakey") |
| 65 | + |
| 66 | + with pytest.raises(ValueError, match="Invalid key"): |
| 67 | + to_key("ab") |
| 68 | + |
| 69 | + |
| 70 | +# --- to_action --- |
| 71 | + |
| 72 | + |
| 73 | +def test_to_action_valid(): |
| 74 | + from RPA.Desktop.keywords.mouse import Action, to_action |
| 75 | + |
| 76 | + assert to_action("click") == Action.click |
| 77 | + assert to_action("double_click") == Action.double_click |
| 78 | + assert to_action("right_click") == Action.right_click |
| 79 | + assert to_action("triple_click") == Action.triple_click |
| 80 | + assert to_action("double click") == Action.double_click |
| 81 | + |
| 82 | + |
| 83 | +def test_to_action_passthrough(): |
| 84 | + from RPA.Desktop.keywords.mouse import Action, to_action |
| 85 | + |
| 86 | + assert to_action(Action.click) is Action.click |
| 87 | + |
| 88 | + |
| 89 | +def test_to_action_invalid_raises(): |
| 90 | + from RPA.Desktop.keywords.mouse import to_action |
| 91 | + |
| 92 | + with pytest.raises(ValueError, match="Unknown mouse action"): |
| 93 | + to_action("spin_click") |
| 94 | + |
| 95 | + |
| 96 | +# --- to_button --- |
| 97 | + |
| 98 | + |
| 99 | +def test_to_button_valid(): |
| 100 | + pytest.importorskip("pynput") |
| 101 | + from pynput.mouse import Button |
| 102 | + |
| 103 | + from RPA.Desktop.keywords.mouse import to_button |
| 104 | + |
| 105 | + assert to_button("left") == Button.left |
| 106 | + assert to_button("right") == Button.right |
| 107 | + assert to_button("middle") == Button.middle |
| 108 | + |
| 109 | + |
| 110 | +def test_to_button_passthrough(): |
| 111 | + pytest.importorskip("pynput") |
| 112 | + from pynput.mouse import Button |
| 113 | + |
| 114 | + from RPA.Desktop.keywords.mouse import to_button |
| 115 | + |
| 116 | + assert to_button(Button.left) is Button.left |
| 117 | + |
| 118 | + |
| 119 | +def test_to_button_invalid_raises(): |
| 120 | + pytest.importorskip("pynput") |
| 121 | + from RPA.Desktop.keywords.mouse import to_button |
| 122 | + |
| 123 | + with pytest.raises(ValueError, match="Unknown mouse button"): |
| 124 | + to_button("side") |
| 125 | + |
| 126 | + |
| 127 | +# --- KeyboardKeywords --- |
| 128 | + |
| 129 | + |
| 130 | +def _make_keyboard_keywords(): |
| 131 | + """Create KeyboardKeywords with a mocked pynput Controller and ctx.""" |
| 132 | + mock_controller = MagicMock() |
| 133 | + ctx = MagicMock() |
| 134 | + |
| 135 | + with patch("pynput.keyboard.Controller", return_value=mock_controller): |
| 136 | + from RPA.Desktop.keywords.keyboard import KeyboardKeywords |
| 137 | + |
| 138 | + kb = KeyboardKeywords(ctx) |
| 139 | + |
| 140 | + return kb, mock_controller |
| 141 | + |
| 142 | + |
| 143 | +def test_press_keys_special(): |
| 144 | + pytest.importorskip("pynput") |
| 145 | + from pynput.keyboard import Key |
| 146 | + |
| 147 | + kb, ctrl = _make_keyboard_keywords() |
| 148 | + kb.press_keys("enter") |
| 149 | + |
| 150 | + ctrl.press.assert_called_once_with(Key.enter) |
| 151 | + ctrl.release.assert_called_once_with(Key.enter) |
| 152 | + |
| 153 | + |
| 154 | +def test_press_keys_character(): |
| 155 | + pytest.importorskip("pynput") |
| 156 | + from pynput.keyboard import KeyCode |
| 157 | + |
| 158 | + kb, ctrl = _make_keyboard_keywords() |
| 159 | + kb.press_keys("a") |
| 160 | + |
| 161 | + pressed = ctrl.press.call_args[0][0] |
| 162 | + assert isinstance(pressed, KeyCode) |
| 163 | + |
| 164 | + |
| 165 | +def test_press_keys_combination_order(): |
| 166 | + """ctrl+a: ctrl pressed first, released last.""" |
| 167 | + pytest.importorskip("pynput") |
| 168 | + from pynput.keyboard import Key, KeyCode |
| 169 | + |
| 170 | + kb, ctrl = _make_keyboard_keywords() |
| 171 | + kb.press_keys("ctrl", "a") |
| 172 | + |
| 173 | + press_calls = [c[0][0] for c in ctrl.press.call_args_list] |
| 174 | + release_calls = [c[0][0] for c in ctrl.release.call_args_list] |
| 175 | + |
| 176 | + assert press_calls[0] == Key.ctrl |
| 177 | + assert isinstance(press_calls[1], KeyCode) |
| 178 | + assert isinstance(release_calls[0], KeyCode) |
| 179 | + assert release_calls[1] == Key.ctrl |
| 180 | + |
| 181 | + |
| 182 | +def test_type_text(): |
| 183 | + pytest.importorskip("pynput") |
| 184 | + kb, ctrl = _make_keyboard_keywords() |
| 185 | + kb.type_text("hello") |
| 186 | + |
| 187 | + ctrl.type.assert_called_once_with("hello") |
0 commit comments