Skip to content

Commit

Permalink
make find_window and win_get consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Jun 14, 2022
1 parent c17f80f commit cb58732
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ahk/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ def win_get(self, title='', text='', exclude_title='', exclude_text='', encoding
script = self._win_get(title=title, text=text, exclude_title=exclude_title, exclude_text=exclude_text)
encoding = encoding or self.window_encoding
ahk_id = self.run_script(script)
if not ahk_id:
return None
return Window(engine=self, ahk_id=ahk_id, encoding=encoding)

def _win_set(self, subcommand, *args, blocking=True):
Expand Down Expand Up @@ -948,6 +950,8 @@ async def win_get(self, *args, **kwargs):
encoding = kwargs.pop('encoding', self.window_encoding)
script = self._win_get(*args, **kwargs)
ahk_id = await self.a_run_script(script)
if not ahk_id:
return None
return AsyncWindow(engine=self, ahk_id=ahk_id, encoding=encoding)

async def _all_window_ids(self):
Expand Down Expand Up @@ -1004,7 +1008,7 @@ async def find_window(self, func=None, **kwargs):
"""
async for window in self.find_windows(func=func, **kwargs):
return window # return the first result
raise WindowNotFoundError('yikes')
return None

async def find_windows_by_title(self, title, exact=False):
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/unittests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from unittest import TestCase
import os, sys

import pytest

project_root = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../..'))
sys.path.insert(0, project_root)

from ahk import AHK
from ahk.daemon import AHKDaemon
from ahk.window import WindowNotFoundError


class TestWindow(TestCase):
Expand Down Expand Up @@ -99,6 +102,18 @@ def test_title_change(self):
def tearDown(self):
self.p.terminate()

def test_find_window(self):
win = self.ahk.find_window(title=b'Untitled - Notepad')
assert win.id == self.win.id

def test_find_window_nonexistent_is_none(self):
win = self.ahk.find_window(title=b'This should not exist')
assert win is None

def test_winget_nonexistent_window_is_none(self):
win = self.ahk.win_get(title='This should not exist')
assert win is None


class TestWindowDaemon(TestWindow):
def setUp(self):
Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/test_window_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ async def test_title_setter(self):
await self.win.set_title('new title')
assert await self.win.get_title() != starting_title

async def test_find_window(self):
win = await self.ahk.find_window(title=b'Untitled - Notepad')
assert win.id == self.win.id

async def test_find_window_nonexistent_is_none(self):
win = await self.ahk.find_window(title=b'This should not exist')
assert win is None

async def test_winget_nonexistent_window_is_none(self):
win = await self.ahk.win_get(title='This should not exist')
assert win is None

def tearDown(self):
self.p.terminate()
asyncio.run(asyncio.sleep(0.5))

0 comments on commit cb58732

Please sign in to comment.