"""Check minimize/restore against one explicitly identified acceptance DX.""" from __future__ import annotations import argparse import json from pathlib import Path import shlex import time import win32con import win32gui import win32process from dx_productivity.ui.host_window_watcher import WindowsHostWindows from packaged_menu import tools_running, wait_for from toolbar_ui import ToolbarUI, TOOLBAR_TITLE, PANEL_TITLE def main(): parser = argparse.ArgumentParser() parser.add_argument('executable', type=Path) parser.add_argument('--host-pid', required=True, type=int) parser.add_argument('--project', required=True, type=Path) parser.add_argument('--evidence', required=True, type=Path) options = parser.parse_args() exe, project, base = options.executable.resolve(), options.project.resolve(), options.evidence.resolve() assert 'acceptance' in project.parts base.mkdir(parents=True, exist_ok=True) def find_tool(): for process in tools_running(): if process.executable == 'None' or Path(process.executable).resolve() != exe or '--request' not in process.command_line: continue args = shlex.split(process.command_line, posix=False) request = Path(args[args.index('--request') + 1].strip('"')) data = json.loads(request.read_text(encoding='utf-8-sig')) if Path(data['design_path']).resolve() == project: return process, request, data process, request, data = wait_for(find_tool, 30) host = WindowsHostWindows().resolve(data['host_view_handle']) assert host is not None and host.pid == options.host_pid original_minimized = bool(win32gui.IsIconic(host.handle)) def windows(): result = {} win32gui.EnumWindows(lambda handle, _: result.update({win32gui.GetWindowText(handle): handle}) if win32process.GetWindowThreadProcessId(handle)[1] == process.pid and win32gui.GetWindowText(handle) in (TOOLBAR_TITLE, PANEL_TITLE) else None, None) return result cases = [] try: if original_minimized: # A correctly suppressed initial launch need not create native # window handles until the first show. Absence also means hidden. assert all(not win32gui.IsWindowVisible(handle) for handle in windows().values()) cases.append({'case': 'startup-while-minimized', 'tool_hidden': True}) win32gui.ShowWindow(host.handle, win32con.SW_RESTORE) handles = wait_for(lambda: windows() if len(windows()) == 2 else None, 20) bar, panel = handles[TOOLBAR_TITLE], handles[PANEL_TITLE] wait_for(lambda: win32gui.IsWindowVisible(bar) and win32gui.IsWindowVisible(panel), 10) ui = ToolbarUI(process.pid) assert '桥接已连接' in ui.texts(ui.panel) ui.capture(ui.toolbar).save(base / 'restored-toolbar.png') ui.capture(ui.panel).save(base / 'restored-panel.png') cases.append({'case': 'initial-restore', 'toolbar_visible': True, 'panel_visible': True}) for expanded in (True, False): if not expanded: ui.collapse() for repeat in range(2): progress = request.with_name('apply-progress.json') prior = progress.stat().st_mtime win32gui.ShowWindow(host.handle, win32con.SW_MINIMIZE) wait_for(lambda: win32gui.IsIconic(host.handle) and not win32gui.IsWindowVisible(bar) and not win32gui.IsWindowVisible(panel), 10) wait_for(lambda: progress.stat().st_mtime > prior, 10) assert win32gui.IsIconic(host.handle) win32gui.ShowWindow(host.handle, win32con.SW_RESTORE) wait_for(lambda: win32gui.IsWindowVisible(bar) and bool(win32gui.IsWindowVisible(panel)) == expanded, 10) time.sleep(.4) foreground = win32gui.GetForegroundWindow() foreground_pid = win32process.GetWindowThreadProcessId(foreground)[1] if foreground else None assert foreground_pid != process.pid, 'Tool took foreground focus on host restore' cases.append({'case': 'cycle', 'expanded': expanded, 'repeat': repeat + 1, 'hidden_during_minimize': True, 'previous_panel_state_restored': True, 'heartbeat_continued': True, 'tool_did_not_take_focus': True, 'foreground_pid_after_restore': foreground_pid}) finally: if original_minimized: win32gui.ShowWindow(host.handle, win32con.SW_MINIMIZE) wait_for(lambda: all(not win32gui.IsWindowVisible(handle) for handle in windows().values()), 10) result = {'host_pid': host.pid, 'host_hwnd': host.handle, 'tool_pid': process.pid, 'host_view_handle': data['host_view_handle'], 'request': str(request), 'executable': str(exe), 'cases': cases, 'final_dx_minimized': bool(win32gui.IsIconic(host.handle)), 'final_tool_hidden': all(not win32gui.IsWindowVisible(handle) for handle in windows().values()), 'design_values_written': False} (base / 'host-native.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') print(json.dumps(result, ensure_ascii=False, indent=2), flush=True) if __name__ == '__main__': main()