-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Tests for `bleak.backends.winrt.util` package.""" | ||
|
||
import sys | ||
|
||
import pytest | ||
|
||
if not sys.platform.startswith("win"): | ||
pytest.skip("skipping windows-only tests", allow_module_level=True) | ||
|
||
from ctypes import windll, wintypes | ||
|
||
from bleak.backends.winrt.util import _check_hresult, assert_mta | ||
from bleak.exc import BleakError | ||
|
||
# https://learn.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit | ||
COINIT_MULTITHREADED = 0x0 | ||
COINIT_APARTMENTTHREADED = 0x2 | ||
|
||
# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex | ||
_CoInitializeEx = windll.ole32.CoInitializeEx | ||
_CoInitializeEx.restype = wintypes.LONG | ||
_CoInitializeEx.argtypes = [wintypes.LPVOID, wintypes.DWORD] | ||
_CoInitializeEx.errcheck = _check_hresult | ||
|
||
# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize | ||
_CoUninitialize = windll.ole32.CoUninitialize | ||
_CoUninitialize.restype = None | ||
_CoUninitialize.argtypes = [] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_assert_mta_no_init(): | ||
"""Test device_path_from_characteristic_path.""" | ||
|
||
await assert_mta() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_assert_mta_init_mta(): | ||
"""Test device_path_from_characteristic_path.""" | ||
|
||
_CoInitializeEx(None, COINIT_MULTITHREADED) | ||
|
||
try: | ||
await assert_mta() | ||
assert hasattr(assert_mta, "_allowed") | ||
finally: | ||
_CoUninitialize() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_assert_mta_init_sta(): | ||
"""Test device_path_from_characteristic_path.""" | ||
|
||
_CoInitializeEx(None, COINIT_APARTMENTTHREADED) | ||
|
||
try: | ||
with pytest.raises( | ||
BleakError, | ||
match="Thread is configured for Windows GUI but callbacks are not working.", | ||
): | ||
await assert_mta() | ||
finally: | ||
_CoUninitialize() |