diff --git a/src/cbb_refresh_verify/residual_sessions.py b/src/cbb_refresh_verify/residual_sessions.py index f5958c8..a3ea28f 100644 --- a/src/cbb_refresh_verify/residual_sessions.py +++ b/src/cbb_refresh_verify/residual_sessions.py @@ -252,7 +252,7 @@ def _post_window_message(window, hwnd, pid, message): win32gui.PostMessage(hwnd, message, 0, 0) -def _process_windows(pids): +def _process_windows(pids, *, include_hidden_documents=False): result = _WindowSnapshot(pids) if not pids: return result import win32api, win32con, win32gui, win32process @@ -271,10 +271,13 @@ def _process_windows(pids): kind = win32gui.GetClassName(handle) if kind == 'MDIClient' and win32gui.GetWindow(handle, win32con.GW_CHILD): documents.append(handle) - if win32gui.IsWindowVisible(handle): + visible = bool(win32gui.IsWindowVisible(handle)) + if visible or (include_hidden_documents and kind.casefold().startswith('afx:') + and win32gui.GetClassName(win32gui.GetParent(handle)) == 'MDIClient'): children.append({'hwnd': int(handle), 'class': kind, 'id': win32gui.GetDlgCtrlID(handle) & 0xffff, - 'text': win32gui.GetWindowText(handle), 'enabled': bool(win32gui.IsWindowEnabled(handle))}) + 'text': win32gui.GetWindowText(handle), 'enabled': bool(win32gui.IsWindowEnabled(handle)), + 'visible': visible}) win32api.SetLastError(0) win32gui.EnumChildWindows(hwnd, child, None) window = {'hwnd': int(hwnd), 'title': win32gui.GetWindowText(hwnd), @@ -651,7 +654,7 @@ def _cancelled_diagnostic_record(row): def _legacy_diagnostic_windows(row): """Adapt the existing complete private-desktop snapshot for diagnostic policy.""" import win32con, win32gui - snapshot = _process_windows({row['pid']}) + snapshot = _process_windows({row['pid']}, include_hidden_documents=True) if _window_error(snapshot, row['pid']): raise ValueError('STARTUP_WINDOW_UNCONFIRMED') result = [] @@ -664,7 +667,7 @@ def _legacy_diagnostic_windows(row): if benign: continue with _window_desktop(window['desktop']): - children = [dict(c, parent=win32gui.GetParent(c['hwnd']), visible=True) for c in window['children']] + children = [dict(c, parent=win32gui.GetParent(c['hwnd'])) for c in window['children']] result.append(dict(hwnd=window['hwnd'], pid=row['pid'], **{'class':window['class']}, text=window['title'], enabled=window['enabled'], children=children, owner=int(win32gui.GetWindow(window['hwnd'], win32con.GW_OWNER) or 0))) @@ -711,6 +714,8 @@ def _close_registered_startup(row, handle, proof, *, stop=None): has_tip = any(w['title'] == 'Tip of the Day' for w in snapshot.get(row['pid'],[])) if has_tip and confirmed() and _dismiss_startup_tip(tip_row,handle,snapshot.get(row['pid'],[]), incomplete=_window_error(snapshot,row['pid']),deadline=deadline,stop=stop) is not None: + if closed is None: + continue # Tip may precede loading; the same intent still owns its one Close. while time.monotonic() < deadline: if win32event.WaitForSingleObject(handle,100) == win32con.WAIT_OBJECT_0: return {'release_status':'RELEASED','detail':'已关闭启动提示并正常退出'} diff --git a/tests/test_registered_startup_release.py b/tests/test_registered_startup_release.py index 9299fbb..01737b0 100644 --- a/tests/test_registered_startup_release.py +++ b/tests/test_registered_startup_release.py @@ -226,3 +226,59 @@ def test_registered_cleanup_preserves_frame_with_an_additional_mdi_document(tmp_ monkeypatch.setattr(sessions,'_legacy_diagnostic_windows',windows) assert sessions._request_close(row)['release_status']=='WAITING' assert records and not messages + + +@pytest.mark.parametrize('hidden_sibling',[None,False,True]) +def test_real_legacy_adapter_keeps_hidden_documents_before_any_native_action(tmp_path,monkeypatch,hidden_sibling): + import win32con,win32gui,win32process + snapshot,adapter=sessions._process_windows,sessions._legacy_diagnostic_windows + job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch) + monkeypatch.setattr(sessions,'_process_windows',snapshot) + monkeypatch.setattr(sessions,'_legacy_diagnostic_windows',adapter) + monkeypatch.setattr(sessions,'_process_threads',lambda pids:{12:[1]}) + monkeypatch.setattr(sessions,'_thread_desktop_name',lambda tid:job['desktop']) + monkeypatch.setattr(sessions,'_desktop_windows',lambda desktop:[100,200] if not state['declined'] else [100]) + classes={100:'Afx:main',101:'MDIClient',102:'Afx:document',103:'Afx:document', + 200:'#32770',30:'Static',31:'Button',32:'Button'} + texts={100:'Expedition PCB - Reusable Block - [IP101GR]',101:'',102:'IP101GR',103:'Other', + 200:'Expedition PCB',30:'New changes are ready for Forward Annotation. Do you want to Forward Annotate now?',31:'&No',32:'&Yes'} + parents={101:100,102:101,103:101,30:200,31:200,32:200} + monkeypatch.setattr(win32process,'GetWindowThreadProcessId',lambda h:(1,12)) + monkeypatch.setattr(win32gui,'GetClassName',lambda h:classes[h]) + monkeypatch.setattr(win32gui,'GetWindowText',lambda h:texts[h]) + monkeypatch.setattr(win32gui,'GetParent',lambda h:parents.get(h,0)) + monkeypatch.setattr(win32gui,'GetDlgCtrlID',lambda h:{30:6172,31:7,32:6}.get(h,0)) + monkeypatch.setattr(win32gui,'GetWindowRect',lambda h:(0,0,300,200)) + monkeypatch.setattr(win32gui,'IsWindowVisible',lambda h:not (h==103 and hidden_sibling)) + monkeypatch.setattr(win32gui,'IsWindowEnabled',lambda h:h!=100 or state['declined']) + monkeypatch.setattr(win32gui,'GetWindow',lambda h,k:102 if h==101 and k==win32con.GW_CHILD else 100 if h==200 and k==win32con.GW_OWNER else 0) + monkeypatch.setattr(win32gui,'EnumChildWindows',lambda h,fn,arg:[fn(c,arg) for c in ([101,102]+([103] if hidden_sibling is not None else []) if h==100 else [30,31,32])]) + result=sessions._request_close(row) + if hidden_sibling is None: + assert result['release_status']=='RELEASED',result + assert not records and messages==[(31,245),(100,16)] + else: + assert result['release_status']=='WAITING',result + assert records and not messages + + +@pytest.mark.parametrize('closed',[False,True]) +def test_legacy_tip_continues_to_loaded_cleanup_only_without_prior_close(tmp_path,monkeypatch,closed): + import win32con,win32gui + from copy import deepcopy + job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch,closed=closed) + state['tip']=True + tip_windows=[dict(hwnd=100,**{'class':'Afx:main'},title='Expedition PCB',enabled=False, + desktop=job['desktop'],children=[],documents=[]), + dict(hwnd=200,**{'class':'#32770'},title='Tip of the Day',enabled=True, + desktop=job['desktop'],documents=[],children=[dict(hwnd=201,**{'class':'Button'},id=1,text='OK',enabled=True)])] + monkeypatch.setattr(sessions,'_process_windows',lambda pids:{12:deepcopy(tip_windows) if state['tip'] else []}) + monkeypatch.setattr(win32gui,'GetWindow',lambda h,k:100 if h==200 and k==win32con.GW_OWNER else 0) + original=win32gui.PostMessage + def post(hwnd,message,*args): + original(hwnd,message,*args) + if closed and hwnd==201:records.clear() + monkeypatch.setattr(win32gui,'PostMessage',post) + assert sessions._request_close(row)['release_status']=='RELEASED' + assert messages==([(201,245)] if closed else [(201,245),(31,245),(100,16)]) + assert not records and state['ticks']<30