-
Notifications
You must be signed in to change notification settings - Fork 65
/
test_hotkeys.py
69 lines (56 loc) · 2.18 KB
/
test_hotkeys.py
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
import asyncio
import subprocess
import time
from unittest import IsolatedAsyncioTestCase # unasync: remove
from unittest import mock
from unittest import TestCase
from ahk import AsyncAHK
from ahk import AsyncWindow
async_sleep = asyncio.sleep # unasync: remove
sleep = time.sleep
class TestHotkeysAsync(IsolatedAsyncioTestCase):
win: AsyncWindow
async def asyncSetUp(self) -> None:
self.ahk = AsyncAHK()
async def asyncTearDown(self) -> None:
self.ahk.stop_hotkeys()
self.ahk._transport._proc.kill()
subprocess.run(['TASKKILL', '/F', '/IM', 'AutoHotkey.exe'], capture_output=True)
time.sleep(0.2)
async def test_hotkey(self):
with mock.MagicMock(return_value=None) as m:
self.ahk.add_hotkey('a', callback=m)
self.ahk.start_hotkeys()
await self.ahk.key_down('a')
await self.ahk.key_press('a')
await async_sleep(1)
m.assert_called()
async def test_hotkey_ex_handler(self):
def side_effect():
raise Exception('oh no')
with mock.MagicMock() as mock_cb, mock.MagicMock() as mock_ex_handler:
mock_cb.side_effect = side_effect
self.ahk.add_hotkey('a', callback=mock_cb, ex_handler=mock_ex_handler)
self.ahk.start_hotkeys()
await self.ahk.key_down('a')
await self.ahk.key_press('a')
await async_sleep(1)
mock_ex_handler.assert_called()
async def test_remove_hotkey(self):
with mock.MagicMock(return_value=None) as m:
self.ahk.add_hotkey('a', callback=m)
self.ahk.start_hotkeys()
self.ahk.remove_hotkey('a')
await self.ahk.key_down('a')
await self.ahk.key_press('a')
await async_sleep(1)
m.assert_not_called()
async def test_clear_hotkeys(self):
with mock.MagicMock(return_value=None) as m:
self.ahk.add_hotkey('a', callback=m)
self.ahk.start_hotkeys()
self.ahk.clear_hotkeys()
await self.ahk.key_down('a')
await self.ahk.key_press('a')
await async_sleep(1)
m.assert_not_called()