//! Live check that `ui.screenshot` never takes the foreground (Windows). //! //! Starts a built desktop app (`LUTRA_DESKTOP_EXE`, built with //! `--features bundled-ui`) with `--control` and an isolated data directory, //! takes several screenshots over the control channel and samples the //! foreground window every few milliseconds the whole time. The test fails //! if the foreground window ever belongs to the app's process, or if a //! screenshot is not a PNG. Ignored by default because it needs a desktop //! session and a built app: //! //! `LUTRA_DESKTOP_EXE=… LUTRA_CONTROL_PORT=7962 cargo test -p lutra-desktop --test screenshot_focus -- --ignored` #![cfg(windows)] #![allow(unsafe_code)] use std::process::{Child, Command}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::thread; use std::time::{Duration, Instant}; use lutra_control::client::Client; use serde_json::json; use windows::Win32::UI::WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId}; /// Kills the app when the test ends, also on failure. struct App(Child); impl Drop for App { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } } /// Process id owning the current foreground window, 0 if there is none. fn foreground_pid() -> u32 { let mut pid = 0_u32; unsafe { let window = GetForegroundWindow(); GetWindowThreadProcessId(window, Some(&mut pid)); } pid } #[test] #[ignore = "starts the desktop app; set LUTRA_DESKTOP_EXE and run with --ignored"] fn screenshots_never_take_the_foreground() { let exe = std::env::var("LUTRA_DESKTOP_EXE").expect("LUTRA_DESKTOP_EXE"); let port: u16 = std::env::var("LUTRA_CONTROL_PORT") .unwrap_or_else(|_| "7962".into()) .parse() .unwrap(); let data = tempfile::tempdir().unwrap(); let shots = tempfile::tempdir().unwrap(); let app = App(Command::new(exe) .arg("--control") .arg(port.to_string()) .arg("--data-dir") .arg(data.path()) .spawn() .unwrap()); let pid = app.0.id(); let stop = Arc::new(AtomicBool::new(false)); let stolen = Arc::new(AtomicU32::new(0)); let sampler = { let (stop, stolen) = (stop.clone(), stolen.clone()); thread::spawn(move || { while !stop.load(Ordering::Relaxed) { if foreground_pid() == pid { stolen.fetch_add(1, Ordering::Relaxed); } thread::sleep(Duration::from_millis(5)); } }) }; let deadline = Instant::now() + Duration::from_secs(90); let mut client = loop { match Client::connect(port, Duration::from_secs(2)) { Ok(client) => break client, Err(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(250)), Err(e) => panic!("the app did not open its control port: {e:?}"), } }; client.request("app.ping", json!({})).unwrap(); thread::sleep(Duration::from_secs(3)); for i in 0..5 { let path = shots.path().join(format!("shot-{i}.png")); client .request("ui.screenshot", json!({ "path": path })) .unwrap(); let png = std::fs::read(&path).unwrap(); assert!( png.starts_with(b"\x89PNG\r\n\x1a\n"), "shot {i} is not a PNG" ); assert!(png.len() > 1000, "shot {i} is suspiciously small"); thread::sleep(Duration::from_millis(200)); } stop.store(true, Ordering::Relaxed); sampler.join().unwrap(); assert_eq!( stolen.load(Ordering::Relaxed), 0, "the app's window was in the foreground during the check" ); }