| 1 | import pytest |
| 2 | from unittest.mock import Mock |
| 3 | |
| 4 | # Skip this entire test module if on a non-win32 platform. |
| 5 | # Although it's ugly, it's easier than refactoring platform restriction |
| 6 | # code to allow exceptions to enable mock-backed testing. |
| 7 | try: |
| 8 | from pyglet.libs.win32 import context_managers |
| 9 | from ctypes.wintypes import HANDLE |
| 10 | except ImportError: |
| 11 | context_managers = HANDLE = None |
| 12 | |
| 13 | pytestmark = pytest.mark.skipif( |
| 14 | context_managers is None, |
| 15 | reason='Win32 context managers only available on Windows...') |
| 16 | |
| 17 | |
| 18 | # = Fixtures & Constants = # |
| 19 | |
| 20 | GET_DC_SUCCESS = 1 |
| 21 | GET_DC_FAILURE = None |
| 22 | RELEASE_DC_SUCCESS = 1 |
| 23 | RELEASE_DC_FAILURE = 0 |
| 24 | USER32_MODPATH = 'pyglet.libs.win32.context_managers.user32' |
| 25 | |
| 26 | |
| 27 | # === Fixtures === # |
| 28 | |
| 29 | # Indirectly parametrize via pytest.mark.parametrize |
| 30 | # https://docs.pytest.org/en/stable/example/parametrize.html#indirect-parametrization |
| 31 | @pytest.fixture |
| 32 | def dc_handle(request) -> "HANDLE": |
| 33 | return HANDLE(request.param) |
| 34 | |
| 35 | |
| 36 | @pytest.fixture |
| 37 | def release_result(request) -> int: |
| 38 | return request.param |
| 39 | |
| 40 | |
| 41 | @pytest.fixture |
| 42 | def patched_get_dc(monkeypatch, dc_handle): |
| 43 | mock = Mock(return_value=dc_handle) |
| 44 | monkeypatch.setattr(context_managers.user32, 'GetDC', mock) |
| 45 | return mock |
| 46 | |
| 47 | |
| 48 | @pytest.fixture |
| 49 | def patched_release_dc(monkeypatch, release_result): |
| 50 | mock = Mock(return_value=release_result) |
| 51 | monkeypatch.setattr(context_managers.user32, 'ReleaseDC', mock) |
| 52 | return mock |
| 53 | |
| 54 | |
| 55 | # Kludge to avoid IDE typing issues or ugly cast / type: ignore |
| 56 | @pytest.fixture |
| 57 | def patched_win_error_type(monkeypatch): |
| 58 | monkeypatch.setattr(context_managers, 'WinError', OSError) |
| 59 | return OSError |
| 60 | |
| 61 | |
| 62 | # = Tests = # |
| 63 | |
| 64 | |
| 65 | @pytest.mark.parametrize('dc_handle', [GET_DC_SUCCESS], indirect=True) |
| 66 | @pytest.mark.parametrize('release_result', [RELEASE_DC_SUCCESS], indirect=True) |
| 67 | def test_device_context_yields_correct_value_when_get_dc_succeeds( |
| 68 | patched_get_dc, patched_release_dc, dc_handle |
| 69 | ): |
| 70 | with context_managers.device_context() as dc: |
| 71 | assert dc is dc_handle |
| 72 | |
| 73 | |
| 74 | # TODO: Review the following tests to see why they are failing. |
| 75 | |
| 76 | # @pytest.mark.parametrize('dc_handle', [GET_DC_FAILURE], indirect=True) |
| 77 | # @pytest.mark.parametrize('release_result', [RELEASE_DC_SUCCESS], indirect=True) |
| 78 | # def test_device_context_raises_winerror_when_get_dc_fails( |
| 79 | # patched_win_error_type, patched_get_dc, patched_release_dc |
| 80 | # ): |
| 81 | # with pytest.raises(patched_win_error_type): |
| 82 | # with context_managers.device_context() as dc: |
| 83 | # pass |
| 84 | # |
| 85 | # |
| 86 | # @pytest.mark.parametrize('dc_handle', [GET_DC_SUCCESS], indirect=True) |
| 87 | # @pytest.mark.parametrize('release_result', [RELEASE_DC_FAILURE], indirect=True) |
| 88 | # def test_device_context_raises_winerror_when_cleanup_fails( |
| 89 | # patched_win_error_type, |
| 90 | # patched_get_dc, patched_release_dc |
| 91 | # ): |
| 92 | # with pytest.raises(patched_win_error_type): |
| 93 | # with context_managers.device_context() as dc: |
| 94 | # pass |
| 95 | # |
| 96 | # |
| 97 | # # Nasty whitebox tests |
| 98 | # @pytest.mark.parametrize('dc_handle', [GET_DC_SUCCESS, GET_DC_FAILURE], indirect=True) |
| 99 | # @pytest.mark.parametrize('release_result', [RELEASE_DC_SUCCESS], indirect=True) |
| 100 | # def test_device_context_calls_get_dc( |
| 101 | # patched_get_dc, |
| 102 | # dc_handle, |
| 103 | # patched_release_dc |
| 104 | # ): |
| 105 | # try: |
| 106 | # with context_managers.device_context() as dc: |
| 107 | # pass |
| 108 | # finally: |
| 109 | # patched_get_dc.assert_called_with(dc_handle.value) |
| 110 | # |
| 111 | # |
| 112 | # @pytest.mark.parametrize('dc_handle', [GET_DC_SUCCESS], indirect=True) |
| 113 | # @pytest.mark.parametrize('release_result', |
| 114 | # [RELEASE_DC_FAILURE, RELEASE_DC_SUCCESS], indirect=True) |
| 115 | # def test_device_context_calls_release_dc( |
| 116 | # patched_get_dc, |
| 117 | # dc_handle, |
| 118 | # patched_release_dc |
| 119 | # ): |
| 120 | # try: |
| 121 | # with context_managers.device_context() as dc: |
| 122 | # pass |
| 123 | # finally: |
| 124 | # patched_release_dc.assert_called_with(dc_handle) |