| 1 | #[cfg(not(windows))] |
| 2 | fn main() { |
| 3 | eprintln!("embed_probe runs on Windows only"); |
| 4 | std::process::exit(2); |
| 5 | } |
| 6 | |
| 7 | #[cfg(windows)] |
| 8 | fn main() { |
| 9 | probe::run(); |
| 10 | } |
| 11 | |
| 12 | #[cfg(windows)] |
| 13 | mod probe { |
| 14 | use std::time::{Duration, Instant}; |
| 15 | |
| 16 | use tokio::sync::mpsc::UnboundedReceiver; |
| 17 | use webview::{EmbedBounds, EmbedEvent, EmbedHost, EmbedSpec, MainEmbed}; |
| 18 | use windows::Win32::Foundation::{HINSTANCE, HWND, RECT}; |
| 19 | use windows::Win32::Graphics::Gdi::{CreateRectRgn, DeleteObject, GetWindowRgn, HGDIOBJ}; |
| 20 | use windows::Win32::System::LibraryLoader::GetModuleHandleW; |
| 21 | use windows::Win32::UI::WindowsAndMessaging::*; |
| 22 | use windows::core::{PCWSTR, w}; |
| 23 | |
| 24 | const GLOBAL_TIMEOUT: Duration = Duration::from_secs(20); |
| 25 | const STEP_TIMEOUT: Duration = Duration::from_secs(15); |
| 26 | const PAGE_URL: &str = "data:text/html,%3Ctitle%3Eprobe%3C%2Ftitle%3E%3Ch1%3Eprobe%3C%2Fh1%3E"; |
| 27 | const PARENT_CLASS: PCWSTR = w!("EmbedProbeParent"); |
| 28 | const CONTAINER_CLASS: PCWSTR = w!("RustyTeamsEmbedContainer"); |
| 29 | const ERROR_REGION: i32 = 0; |
| 30 | const COMPLEX_REGION: i32 = 3; |
| 31 | |
| 32 | struct Probe { |
| 33 | started: Instant, |
| 34 | events: UnboundedReceiver<EmbedEvent>, |
| 35 | seen: Vec<EmbedEvent>, |
| 36 | } |
| 37 | |
| 38 | impl Probe { |
| 39 | fn wait_for( |
| 40 | &mut self, |
| 41 | label: &str, |
| 42 | mut done: impl FnMut(&[EmbedEvent]) -> bool, |
| 43 | ) -> Result<Duration, String> { |
| 44 | let step_start = Instant::now(); |
| 45 | let deadline = step_start + STEP_TIMEOUT; |
| 46 | loop { |
| 47 | pump_messages(); |
| 48 | while let Ok(event) = self.events.try_recv() { |
| 49 | self.seen.push(event); |
| 50 | } |
| 51 | if self.seen.contains(&EmbedEvent::Closed) { |
| 52 | return Err(format!("{label}: embed closed itself")); |
| 53 | } |
| 54 | if done(&self.seen) { |
| 55 | return Ok(step_start.elapsed()); |
| 56 | } |
| 57 | if self.started.elapsed() > GLOBAL_TIMEOUT || Instant::now() > deadline { |
| 58 | return Err(format!("{label}: timed out")); |
| 59 | } |
| 60 | std::thread::sleep(Duration::from_millis(5)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn settle(&mut self, label: &str, duration: Duration) -> Result<(), String> { |
| 65 | let end = Instant::now() + duration; |
| 66 | self.wait_for(label, |_| Instant::now() >= end).map(|_| ()) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | pub fn run() { |
| 71 | let started = Instant::now(); |
| 72 | let outcome = execute(started); |
| 73 | match outcome { |
| 74 | Ok(()) => { |
| 75 | println!("PASS total={:?}", started.elapsed()); |
| 76 | std::process::exit(0); |
| 77 | } |
| 78 | Err(reason) => { |
| 79 | println!("FAIL {reason} total={:?}", started.elapsed()); |
| 80 | std::process::exit(1); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn execute(started: Instant) -> Result<(), String> { |
| 86 | let parent = create_hidden_parent().map_err(|error| format!("parent window: {error}"))?; |
| 87 | let folder = std::env::temp_dir().join("rusty-teams-embed-probe-data"); |
| 88 | let host = EmbedHost::new(folder); |
| 89 | let (embed, events) = host |
| 90 | .open(EmbedSpec { |
| 91 | parent: parent.0 as isize, |
| 92 | url: PAGE_URL.to_owned(), |
| 93 | background: 0x1f1f1f, |
| 94 | }) |
| 95 | .ok_or("open returned no embed")?; |
| 96 | let mut probe = Probe { |
| 97 | started, |
| 98 | events, |
| 99 | seen: Vec::new(), |
| 100 | }; |
| 101 | let container = find_container(parent).ok_or("container window missing after open")?; |
| 102 | embed.place(bounds(0, 0, 640, 480), true); |
| 103 | let loaded = probe.wait_for("navigation", |seen| seen.contains(&EmbedEvent::Loaded))?; |
| 104 | println!("navigation_completed after {loaded:?}"); |
| 105 | |
| 106 | embed.place(bounds(10, 20, 800, 600), true); |
| 107 | probe.settle("resize settle", Duration::from_millis(200))?; |
| 108 | let rect = window_rect(container); |
| 109 | expect( |
| 110 | (rect.right - rect.left, rect.bottom - rect.top) == (800, 600), |
| 111 | format!( |
| 112 | "resize: container is {}x{}", |
| 113 | rect.right - rect.left, |
| 114 | rect.bottom - rect.top |
| 115 | ), |
| 116 | )?; |
| 117 | println!("resize ok 800x600"); |
| 118 | |
| 119 | embed.place(bounds(10, 20, 800, 600), false); |
| 120 | probe.settle("hide settle", Duration::from_millis(100))?; |
| 121 | expect( |
| 122 | !style_visible(container), |
| 123 | "visibility: container still shown after hide".into(), |
| 124 | )?; |
| 125 | embed.place(bounds(10, 20, 800, 600), true); |
| 126 | probe.settle("show settle", Duration::from_millis(100))?; |
| 127 | expect( |
| 128 | style_visible(container), |
| 129 | "visibility: container hidden after show".into(), |
| 130 | )?; |
| 131 | println!("visibility toggle ok"); |
| 132 | |
| 133 | embed.set_cutouts(vec![bounds(500, 400, 300, 200)]); |
| 134 | expect( |
| 135 | region_kind(container) == COMPLEX_REGION, |
| 136 | format!( |
| 137 | "region: expected complex region, got {}", |
| 138 | region_kind(container) |
| 139 | ), |
| 140 | )?; |
| 141 | embed.set_cutouts(Vec::new()); |
| 142 | expect( |
| 143 | region_kind(container) == ERROR_REGION, |
| 144 | format!( |
| 145 | "region: expected cleared region, got {}", |
| 146 | region_kind(container) |
| 147 | ), |
| 148 | )?; |
| 149 | println!("region apply/clear ok"); |
| 150 | |
| 151 | close(&embed, &mut probe, parent)?; |
| 152 | println!("close ok"); |
| 153 | unsafe { |
| 154 | let _ = DestroyWindow(parent); |
| 155 | } |
| 156 | Ok(()) |
| 157 | } |
| 158 | |
| 159 | fn close(embed: &MainEmbed, probe: &mut Probe, parent: HWND) -> Result<(), String> { |
| 160 | embed.close(); |
| 161 | probe.settle("close settle", Duration::from_millis(100))?; |
| 162 | expect( |
| 163 | find_container(parent).is_none(), |
| 164 | "close: container still exists".into(), |
| 165 | ) |
| 166 | } |
| 167 | |
| 168 | fn expect(condition: bool, failure: String) -> Result<(), String> { |
| 169 | condition.then_some(()).ok_or(failure) |
| 170 | } |
| 171 | |
| 172 | fn bounds(x: i32, y: i32, width: i32, height: i32) -> EmbedBounds { |
| 173 | EmbedBounds { |
| 174 | x, |
| 175 | y, |
| 176 | width, |
| 177 | height, |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn pump_messages() { |
| 182 | let mut message = MSG::default(); |
| 183 | while unsafe { PeekMessageW(&mut message, None, 0, 0, PM_REMOVE) }.as_bool() { |
| 184 | unsafe { |
| 185 | let _ = TranslateMessage(&message); |
| 186 | DispatchMessageW(&message); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | fn create_hidden_parent() -> windows::core::Result<HWND> { |
| 192 | let instance = HINSTANCE(unsafe { GetModuleHandleW(None) }?.0); |
| 193 | let class = WNDCLASSW { |
| 194 | lpfnWndProc: Some(parent_procedure), |
| 195 | hInstance: instance, |
| 196 | lpszClassName: PARENT_CLASS, |
| 197 | ..Default::default() |
| 198 | }; |
| 199 | unsafe { RegisterClassW(&class) }; |
| 200 | unsafe { |
| 201 | CreateWindowExW( |
| 202 | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE, |
| 203 | PARENT_CLASS, |
| 204 | w!("embed probe"), |
| 205 | WS_OVERLAPPEDWINDOW, |
| 206 | -32000, |
| 207 | -32000, |
| 208 | 1000, |
| 209 | 800, |
| 210 | None, |
| 211 | None, |
| 212 | Some(instance), |
| 213 | None, |
| 214 | ) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | extern "system" fn parent_procedure( |
| 219 | window: HWND, |
| 220 | message: u32, |
| 221 | wparam: windows::Win32::Foundation::WPARAM, |
| 222 | lparam: windows::Win32::Foundation::LPARAM, |
| 223 | ) -> windows::Win32::Foundation::LRESULT { |
| 224 | unsafe { DefWindowProcW(window, message, wparam, lparam) } |
| 225 | } |
| 226 | |
| 227 | fn find_container(parent: HWND) -> Option<HWND> { |
| 228 | let mut after = None; |
| 229 | loop { |
| 230 | let window = |
| 231 | unsafe { FindWindowExW(None, after, CONTAINER_CLASS, PCWSTR::null()) }.ok()?; |
| 232 | if window.is_invalid() { |
| 233 | return None; |
| 234 | } |
| 235 | if unsafe { GetWindow(window, GW_OWNER) }.is_ok_and(|owner| owner == parent) { |
| 236 | return Some(window); |
| 237 | } |
| 238 | after = Some(window); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | fn window_rect(window: HWND) -> RECT { |
| 243 | let mut rect = RECT::default(); |
| 244 | unsafe { |
| 245 | let _ = GetWindowRect(window, &mut rect); |
| 246 | } |
| 247 | rect |
| 248 | } |
| 249 | |
| 250 | fn style_visible(window: HWND) -> bool { |
| 251 | unsafe { GetWindowLongPtrW(window, GWL_STYLE) as u32 & WS_VISIBLE.0 != 0 } |
| 252 | } |
| 253 | |
| 254 | fn region_kind(window: HWND) -> i32 { |
| 255 | unsafe { |
| 256 | let probe_region = CreateRectRgn(0, 0, 0, 0); |
| 257 | let kind = GetWindowRgn(window, probe_region); |
| 258 | let _ = DeleteObject(HGDIOBJ(probe_region.0)); |
| 259 | kind.0 |
| 260 | } |
| 261 | } |
| 262 | } |