AstrBot-Desktop/src-tauri/tests/windows_dialog_smoke.rs

100 行
3.2 KiB
Rust

#![cfg(windows)]
use std::{thread, time::Duration};
use astrbot_desktop_lib::windows::{
capture::{CaptureLimits, WindowCaptureError, capture_selected_window},
foreground_window::{ForegroundWindowError, inspect_foreground_window},
uia::{UiaReadLimits, read_standard_dialog_text},
};
use windows::{
Win32::{
Foundation::{HWND, LPARAM, WPARAM},
UI::WindowsAndMessaging::{
FindWindowW, GetForegroundWindow, MB_OK, MessageBoxW, PostMessageW,
SetForegroundWindow, WM_CLOSE,
},
},
core::w,
};
const TEST_TITLE: &str = "AstrBot Desktop P00 Safe Provider Test";
const TEST_BODY: &str = "Known non-sensitive UI Automation fixture text.";
struct DialogGuard(HWND);
impl Drop for DialogGuard {
fn drop(&mut self) {
unsafe {
let _ = PostMessageW(Some(self.0), WM_CLOSE, WPARAM(0), LPARAM(0));
}
}
}
fn wait_for_test_dialog() -> HWND {
for _ in 0..100 {
if let Ok(window) =
unsafe { FindWindowW(w!("#32770"), w!("AstrBot Desktop P00 Safe Provider Test")) }
&& !window.0.is_null()
{
return window;
}
thread::sleep(Duration::from_millis(50));
}
panic!("the controlled test dialog did not appear");
}
#[test]
#[ignore = "opens a controlled, non-sensitive Windows dialog for native provider validation"]
fn reads_and_captures_only_the_controlled_standard_dialog() {
let dialog_thread = thread::spawn(|| unsafe {
let _ = MessageBoxW(
None,
w!("Known non-sensitive UI Automation fixture text."),
w!("AstrBot Desktop P00 Safe Provider Test"),
MB_OK,
);
});
let window = wait_for_test_dialog();
let guard = DialogGuard(window);
assert!(unsafe { SetForegroundWindow(window) }.as_bool());
for _ in 0..40 {
if unsafe { GetForegroundWindow() } == window {
break;
}
thread::sleep(Duration::from_millis(50));
}
assert_eq!(unsafe { GetForegroundWindow() }, window);
let selection = inspect_foreground_window(Duration::from_secs(30))
.expect("select the controlled foreground dialog");
assert_eq!(selection.summary().title, TEST_TITLE);
assert_eq!(selection.summary().process_id, std::process::id());
let text = read_standard_dialog_text(&selection, UiaReadLimits::default())
.expect("read the controlled dialog through UI Automation");
assert!(text.text.contains(TEST_TITLE));
assert!(text.text.contains(TEST_BODY));
assert_eq!(text.password_nodes_skipped, 0);
let capture = capture_selected_window(&selection, CaptureLimits::default())
.expect("capture the explicitly selected controlled dialog");
assert!(capture.width > 0);
assert!(capture.height > 0);
assert_eq!(&capture.png_bytes[..8], b"\x89PNG\r\n\x1a\n");
drop(guard);
dialog_thread.join().expect("close the controlled dialog");
assert_eq!(
selection.validate(),
Err(ForegroundWindowError::WindowChanged)
);
assert_eq!(
capture_selected_window(&selection, CaptureLimits::default()).unwrap_err(),
WindowCaptureError::Selection(ForegroundWindowError::WindowChanged)
);
}