import pywintypes from cbb_refresh_verify.dialog_models import DialogPhase from cbb_refresh_verify.dialog_probe import WindowProbe PHASE = DialogPhase.PCB_STARTING class FakeDesktop: def __init__(self, handles=()): self.handles = tuple(handles) def EnumDesktopWindows(self): return self.handles class FakeGui: def __init__(self, windows, children=None, disappearing=()): self.windows = windows self.children = children or {} self.disappearing = set(disappearing) self.callback_results = [] def IsWindowVisible(self, hwnd): return self.windows[hwnd].get("visible", True) def GetClassName(self, hwnd): return self.windows[hwnd].get("class_name", "") def GetWindowText(self, hwnd): if hwnd in self.disappearing: raise pywintypes.error(1400, "GetWindowText", "invalid window") return self.windows[hwnd].get("text", "") def GetDlgCtrlID(self, hwnd): return self.windows[hwnd].get("control_id", 0) def IsWindowEnabled(self, hwnd): return self.windows[hwnd].get("enabled", True) def GetWindow(self, hwnd, _command): return self.windows[hwnd].get("owner", 0) def EnumChildWindows(self, hwnd, callback, context): for child in self.children.get(hwnd, ()): self.callback_results.append(callback(child, context)) def SendMessage(self, *_args): return 0 class FakeProcess: @staticmethod def GetWindowThreadProcessId(hwnd): return 1, 1000 + hwnd class FakeUia: def __init__(self, records=None, error=None): self.records = records or {} self.error = error def controls(self, hwnd): if self.error: raise self.error return self.records.get(hwnd, ()) def make_probe(gui, handles, uia=None): return WindowProbe( desktop=FakeDesktop(handles), win32gui=gui, win32process=FakeProcess(), process_name_reader=lambda pid: f"proc-{pid}.exe", uia=uia or FakeUia(), ) def test_probe_keeps_visible_non_dialog_classes(): gui = FakeGui({10: {"class_name": "GXWND", "text": "License chooser"}}) snapshots = make_probe(gui, (10,)).capture(PHASE) assert [item.window_class for item in snapshots] == ["GXWND"] assert snapshots[0].process_name == "proc-1010.exe" def test_disappearing_window_is_skipped_without_hiding_other_windows(): gui = FakeGui({ 10: {"class_name": "GXWND", "text": "gone"}, 20: {"class_name": "#32770", "text": "still here"}, }, disappearing=(10,)) snapshots = make_probe(gui, (10, 20)).capture(PHASE) assert [item.hwnd for item in snapshots] == [20] def test_uia_enrichment_records_patterns_and_automation_id(): gui = FakeGui({10: {"class_name": "GXWND", "text": "License chooser"}}) uia = FakeUia({10: ({ "handle": 101, "control_id": 1522, "class_name": "GXWND", "name": "Product license", "automation_id": "licenseList", "control_type": "ListItem", "enabled": True, "selected": True, "checked": False, "patterns": ("SelectionItem",), },)}) control = make_probe(gui, (10,), uia).capture(PHASE)[0].controls[0] assert control.automation_id == "licenseList" assert control.patterns == ("SelectionItem",) assert control.selected is True def test_uia_failure_keeps_win32_evidence_and_adds_diagnostic(): windows = { 10: {"class_name": "#32770", "text": "Prompt"}, 11: {"class_name": "Button", "text": "OK", "control_id": 1}, } gui = FakeGui(windows, children={10: (11,)}) snapshot = make_probe(gui, (10,), FakeUia(error=RuntimeError("uia down"))).capture(PHASE)[0] assert snapshot.controls[0].name == "OK" assert snapshot.diagnostics == ("UIA_ERROR: uia down",) assert gui.callback_results == [True] def test_empty_private_desktop_winerror_two_is_transient(): class EmptyDesktop: @staticmethod def EnumDesktopWindows(): raise pywintypes.error(2, "EnumDesktopWindows", "file not found") probe = WindowProbe( desktop=EmptyDesktop(), win32gui=object(), win32process=object(), process_name_reader=lambda _pid: "", uia=FakeUia(), ) assert probe.capture(PHASE) == () def test_probe_ignores_input_indicator_afx_and_hidden_ie_windows(): gui = FakeGui({ 10: {"class_name": "UAC_InputIndicatorOverlayWnd", "text": ""}, 20: {"class_name": "UAC Input Indicator", "text": ""}, 30: {"class_name": "Afx:00400000:0:00010003:00000000:00000000", "text": ""}, 35: {"class_name": "Internet Explorer_Hidden", "text": ""}, 40: {"class_name": "#32770", "text": "real dialog"}, }) snapshots = make_probe(gui, (10, 20, 30, 35, 40)).capture(PHASE) assert [item.hwnd for item in snapshots] == [40] def test_probe_skips_project_integration_panel_during_com_phase(): gui = FakeGui({10: {"class_name": "#32770", "text": "Project Integration"}}) snapshots = make_probe(gui, (10,)).capture(DialogPhase.PROJECT_INTEGRATION) assert snapshots == ()