fix: validate the hidden owner of a late startup tip

This commit is contained in:
阿白叔 committed 2026-11-17 07:14:54 +08:00
1 parent a64636ba0d
commit d5d1a3deea
3 files changed
+127 -6

No files matched your search

+78
View File
@@ -42,6 +42,8 @@ def late_tip_fixture(tmp_path, monkeypatch):
monkeypatch.setattr(sessions, '_post_window_message',
lambda w, hwnd, pid, msg: post(hwnd, msg))
monkeypatch.setattr(sessions, '_window_desktop', lambda *a, **kw: nullcontext())
monkeypatch.setattr(win32gui, 'GetWindow', lambda hwnd, kind:
100 if hwnd == 200 and kind == win32con.GW_OWNER else 0)
# Revalidation reads native controls, independently of the initial snapshot.
from cbb_refresh_verify.dialog_models import WindowSnapshot, ControlSnapshot, DialogPhase
def snapshot(hwnd, pid):
@@ -172,3 +174,79 @@ def test_release_tip_ack_never_stacks_another_close_while_process_still_exits(tm
assert result['release_status'] == 'WAITING' and records
assert messages == [(201, win32con.BM_CLICK)]
assert 3 <= state['ticks'] < 3.2
@pytest.mark.parametrize('entry', ['startup', 'release'])
@pytest.mark.parametrize('obstacle', ['', 'foreign_owner', 'other_desktop', 'document',
'progress', 'changed_owner', 'enabled_owner', 'wrong_title'])
def test_late_tip_direct_hidden_empty_owner_matches_native_capture(tmp_path, monkeypatch, entry, obstacle):
import win32con, win32gui, win32process
job, row, records, messages, windows, state, identity = late_tip_fixture(tmp_path, monkeypatch)
frame, tip = deepcopy(windows)
monkeypatch.setattr(sessions, '_process_windows', lambda pids: {
12: [deepcopy(tip)] if state['tip'] else [dict(frame, enabled=True)]})
monkeypatch.setattr(sessions, '_desktop_windows', lambda desktop:
[200] if obstacle == 'other_desktop' else [100, 200])
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda hwnd: hwnd != 100 if state['tip'] else True)
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda hwnd:
hwnd != 100 or not state['tip'] or obstacle == 'enabled_owner')
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd:
'Loaded.pcb - Expedition PCB' if state['tip'] and obstacle == 'wrong_title'
else 'Expedition PCB')
monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd:
('msctls_progress32' if obstacle == 'progress' else 'MDIClient') if hwnd == 300
else 'Afx:main')
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, fn, arg:
fn(300, arg) if state['tip'] and obstacle in ('document', 'progress') else None)
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd:
(1, 99 if hwnd == 100 and state['tip'] and obstacle == 'foreign_owner' else 12))
reads = []
def owner(hwnd, kind):
if hwnd == 300 and kind == win32con.GW_CHILD: return 301
if hwnd == 200 and kind == win32con.GW_OWNER:
reads.append(hwnd)
return 999 if obstacle == 'changed_owner' and len(reads) > 1 else 100
return 0
monkeypatch.setattr(win32gui, 'GetWindow', owner)
if entry == 'release':
state['tip'] = True
result = sessions._request_close(row)
status = result['release_status']
else:
result = failed_session.close_failed_startup(job, 12, timeout_seconds=1)
status = result['status']
expected = [(100, win32con.WM_CLOSE)] if entry == 'startup' else []
if obstacle:
assert status in ('WAITING', 'RETAINED') and records
else:
expected.append((201, win32con.BM_CLICK))
assert status in ('CLOSED', 'RELEASED') and not records
assert messages == expected
@pytest.mark.parametrize('entry', ['startup_deadline', 'release_stop'])
def test_tip_action_rechecks_budget_or_stop_after_slow_fresh_snapshot(tmp_path, monkeypatch, entry):
from threading import Event
import win32con
job, row, records, messages, windows, state, identity = late_tip_fixture(tmp_path, monkeypatch)
stop = Event()
original = sessions._process_windows
reads = []
def snapshot(pids):
value = original(pids)
if state['tip']:
reads.append(1)
if len(reads) >= 2:
state['ticks'] += 2
stop.set()
return value
monkeypatch.setattr(sessions, '_process_windows', snapshot)
if entry == 'startup_deadline':
result = failed_session.close_failed_startup(job, 12, timeout_seconds=1)
assert result['status'] == 'RETAINED'
assert messages == [(100, win32con.WM_CLOSE)]
else:
state['tip'] = True
assert sessions._request_close(row, stop=stop)['release_status'] in ('WAITING', 'CANCELLED')
assert not messages
assert records