Files
cbb-refresh-verify-795/tests/test_native_management_fields.py
T

59 lines
3.1 KiB
Python

import ctypes
import json
from types import SimpleNamespace
import pytest
@pytest.mark.parametrize('expected,actual', [('S:\\source\\项目.prj', 'S:\\source\\项目.prj'), ('', '')])
def test_field_uses_bounded_set_and_remote_gettext_with_exact_readback(tmp_path, monkeypatch, expected, actual):
from cbb_refresh_verify import native_management as native
calls = []
class Send:
def __call__(self, hwnd, message, wparam, lparam, flags, timeout, result):
calls.append((hwnd, message, flags, timeout))
if message == 0x000C:
assert ctypes.wstring_at(lparam) == expected
result._obj.value = 1
elif message == 0x000D:
buffer = ctypes.create_unicode_buffer(actual)
ctypes.memmove(lparam, ctypes.addressof(buffer), ctypes.sizeof(buffer))
result._obj.value = len(actual)
else:
pytest.fail('unproven field message')
return 1
monkeypatch.setattr(native.ctypes, 'WinDLL', lambda *a, **k: SimpleNamespace(SendMessageTimeoutW=Send()))
assert native._set_probe_field(123, 1217, expected, tmp_path) == actual
assert [item[1] for item in calls] == [0x000C, 0x000D]
assert all(item[0] == 123 and item[2] == 0x0002 and 0 < item[3] <= 1000 for item in calls)
record = json.loads((tmp_path/'native-field-readback.jsonl').read_text(encoding='utf-8'))
assert record['expected'] == expected and record['actual'] == actual and record['matched']
@pytest.mark.parametrize('failure', ['empty', 'mismatch', 'set_timeout', 'read_timeout', 'set_rejected', 'truncated'])
def test_field_failure_is_recorded_before_rejection_without_foreground_fallback(tmp_path, monkeypatch, failure):
from cbb_refresh_verify import native_management as native
calls = []
class Send:
def __call__(self, hwnd, message, wparam, lparam, flags, timeout, result):
calls.append(message)
if message == 0x000C:
result._obj.value = 0 if failure == 'set_rejected' else 1
return 0 if failure == 'set_timeout' else 1
if failure == 'read_timeout': return 0
actual = '' if failure == 'empty' else 'other project'
buffer = ctypes.create_unicode_buffer(actual)
ctypes.memmove(lparam, ctypes.addressof(buffer), ctypes.sizeof(buffer))
result._obj.value = wparam - 1 if failure == 'truncated' else len(actual)
return 1
monkeypatch.setattr(native.ctypes, 'WinDLL', lambda *a, **k: SimpleNamespace(SendMessageTimeoutW=Send()))
with pytest.raises((RuntimeError, TimeoutError)):
native._set_probe_field(123, 1217, 'expected project', tmp_path)
record = json.loads((tmp_path/'native-field-readback.jsonl').read_text(encoding='utf-8'))
assert record['expected'] == 'expected project' and not record['matched']
assert record['control_id'] == 1217 and record['exception']['type']
if failure in ('empty', 'mismatch'):
assert record['actual'] == ('' if failure == 'empty' else 'other project')
if failure in ('set_timeout', 'set_rejected'):
assert calls == [0x000C]