| 1 | use crate::{ |
| 2 | Action, AnyView, AnyWindowHandle, App, AppCell, AppContext, AssetSource, BackgroundExecutor, |
| 3 | Bounds, ClipboardItem, Context, Entity, EntityId, ForegroundExecutor, Global, InputEvent, |
| 4 | Keystroke, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, |
| 5 | Platform, Point, Render, Result, Size, Task, TestDispatcher, TextSystem, VisualTestPlatform, |
| 6 | Window, WindowBounds, WindowHandle, WindowOptions, app::GpuiMode, |
| 7 | }; |
| 8 | use anyhow::anyhow; |
| 9 | use image::RgbaImage; |
| 10 | use std::{future::Future, rc::Rc, sync::Arc, time::Duration}; |
| 11 | |
| 12 | /// A test context that uses real macOS rendering instead of mocked rendering. |
| 13 | /// This is used for visual tests that need to capture actual screenshots. |
| 14 | /// |
| 15 | /// Unlike `TestAppContext` which uses `TestPlatform` with mocked rendering, |
| 16 | /// `VisualTestAppContext` uses the real `MacPlatform` to produce actual rendered output. |
| 17 | /// |
| 18 | /// Windows created through this context are positioned off-screen (at coordinates like -10000, -10000) |
| 19 | /// so they are invisible to the user but still fully rendered by the compositor. |
| 20 | #[derive(Clone)] |
| 21 | pub struct VisualTestAppContext { |
| 22 | /// The underlying app cell |
| 23 | pub app: Rc<AppCell>, |
| 24 | /// The background executor for running async tasks |
| 25 | pub background_executor: BackgroundExecutor, |
| 26 | /// The foreground executor for running tasks on the main thread |
| 27 | pub foreground_executor: ForegroundExecutor, |
| 28 | /// The test dispatcher for deterministic task scheduling |
| 29 | dispatcher: TestDispatcher, |
| 30 | platform: Rc<dyn Platform>, |
| 31 | text_system: Arc<TextSystem>, |
| 32 | } |
| 33 | |
| 34 | impl VisualTestAppContext { |
| 35 | /// Creates a new `VisualTestAppContext` with real macOS platform rendering |
| 36 | /// but deterministic task scheduling via TestDispatcher. |
| 37 | /// |
| 38 | /// This provides: |
| 39 | /// - Real Metal/compositor rendering for accurate screenshots |
| 40 | /// - Deterministic task scheduling via TestDispatcher |
| 41 | /// - Controllable time via `advance_clock` |
| 42 | /// |
| 43 | /// Note: This uses a no-op asset source, so SVG icons won't render. |
| 44 | /// Use `with_asset_source` to provide real assets for icon rendering. |
| 45 | pub fn new(platform: Rc<dyn Platform>) -> Self { |
| 46 | Self::with_asset_source(platform, Arc::new(())) |
| 47 | } |
| 48 | |
| 49 | /// Creates a new `VisualTestAppContext` with a custom asset source. |
| 50 | /// |
| 51 | /// Use this when you need SVG icons to render properly in visual tests. |
| 52 | /// Pass the real `Assets` struct to enable icon rendering. |
| 53 | pub fn with_asset_source( |
| 54 | platform: Rc<dyn Platform>, |
| 55 | asset_source: Arc<dyn AssetSource>, |
| 56 | ) -> Self { |
| 57 | // Use a seeded RNG for deterministic behavior |
| 58 | let seed = std::env::var("SEED") |
| 59 | .ok() |
| 60 | .and_then(|s| s.parse().ok()) |
| 61 | .unwrap_or(0); |
| 62 | |
| 63 | // Create a visual test platform that combines real Mac rendering |
| 64 | // with controllable TestDispatcher for deterministic task scheduling |
| 65 | let platform = Rc::new(VisualTestPlatform::new(platform, seed)); |
| 66 | |
| 67 | // Get the dispatcher and executors from the platform |
| 68 | let dispatcher = platform.dispatcher().clone(); |
| 69 | let background_executor = platform.background_executor(); |
| 70 | let foreground_executor = platform.foreground_executor(); |
| 71 | |
| 72 | let text_system = Arc::new(TextSystem::new(platform.text_system())); |
| 73 | |
| 74 | let http_client = http_client::FakeHttpClient::with_404_response(); |
| 75 | |
| 76 | let mut app = App::new_app(platform.clone(), asset_source, http_client); |
| 77 | app.borrow_mut().mode = GpuiMode::test(); |
| 78 | |
| 79 | Self { |
| 80 | app, |
| 81 | background_executor, |
| 82 | foreground_executor, |
| 83 | dispatcher, |
| 84 | platform, |
| 85 | text_system, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Opens a window positioned off-screen for invisible rendering. |
| 90 | /// |
| 91 | /// The window is positioned at (-10000, -10000) so it's not visible on any display, |
| 92 | /// but it's still fully rendered by the compositor and can be captured via ScreenCaptureKit. |
| 93 | /// |
| 94 | /// # Arguments |
| 95 | /// * `size` - The size of the window to create |
| 96 | /// * `build_root` - A closure that builds the root view for the window |
| 97 | pub fn open_offscreen_window<V: Render + 'static>( |
| 98 | &mut self, |
| 99 | size: Size<Pixels>, |
| 100 | build_root: impl FnOnce(&mut Window, &mut App) -> Entity<V>, |
| 101 | ) -> Result<WindowHandle<V>> { |
| 102 | use crate::{point, px}; |
| 103 | |
| 104 | let bounds = Bounds { |
| 105 | origin: point(px(-10000.0), px(-10000.0)), |
| 106 | size, |
| 107 | }; |
| 108 | |
| 109 | let mut cx = self.app.borrow_mut(); |
| 110 | cx.open_window( |
| 111 | WindowOptions { |
| 112 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 113 | focus: false, |
| 114 | show: true, |
| 115 | ..Default::default() |
| 116 | }, |
| 117 | build_root, |
| 118 | ) |
| 119 | } |
| 120 | |
| 121 | /// Opens an off-screen window with default size (1280x800). |
| 122 | pub fn open_offscreen_window_default<V: Render + 'static>( |
| 123 | &mut self, |
| 124 | build_root: impl FnOnce(&mut Window, &mut App) -> Entity<V>, |
| 125 | ) -> Result<WindowHandle<V>> { |
| 126 | use crate::{px, size}; |
| 127 | self.open_offscreen_window(size(px(1280.0), px(800.0)), build_root) |
| 128 | } |
| 129 | |
| 130 | /// Returns whether screen capture is supported on this platform. |
| 131 | pub fn is_screen_capture_supported(&self) -> bool { |
| 132 | self.platform.is_screen_capture_supported() |
| 133 | } |
| 134 | |
| 135 | /// Returns the text system used by this context. |
| 136 | pub fn text_system(&self) -> &Arc<TextSystem> { |
| 137 | &self.text_system |
| 138 | } |
| 139 | |
| 140 | /// Returns the background executor. |
| 141 | pub fn executor(&self) -> BackgroundExecutor { |
| 142 | self.background_executor.clone() |
| 143 | } |
| 144 | |
| 145 | /// Returns the foreground executor. |
| 146 | pub fn foreground_executor(&self) -> ForegroundExecutor { |
| 147 | self.foreground_executor.clone() |
| 148 | } |
| 149 | |
| 150 | /// Runs all pending foreground and background tasks until there's nothing left to do. |
| 151 | /// This is essential for processing async operations like tooltip timers. |
| 152 | pub fn run_until_parked(&self) { |
| 153 | self.dispatcher.run_until_parked(); |
| 154 | } |
| 155 | |
| 156 | /// Advances the simulated clock by the given duration and processes any tasks |
| 157 | /// that become ready. This is essential for testing time-based behaviors like |
| 158 | /// tooltip delays. |
| 159 | pub fn advance_clock(&self, duration: Duration) { |
| 160 | self.dispatcher.advance_clock(duration); |
| 161 | } |
| 162 | |
| 163 | /// Updates the app state. |
| 164 | pub fn update<R>(&mut self, f: impl FnOnce(&mut App) -> R) -> R { |
| 165 | let mut app = self.app.borrow_mut(); |
| 166 | f(&mut app) |
| 167 | } |
| 168 | |
| 169 | /// Reads from the app state. |
| 170 | pub fn read<R>(&self, f: impl FnOnce(&App) -> R) -> R { |
| 171 | let app = self.app.borrow(); |
| 172 | f(&app) |
| 173 | } |
| 174 | |
| 175 | /// Updates a window. |
| 176 | pub fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T> |
| 177 | where |
| 178 | F: FnOnce(AnyView, &mut Window, &mut App) -> T, |
| 179 | { |
| 180 | let mut lock = self.app.borrow_mut(); |
| 181 | lock.update_window(window, f) |
| 182 | } |
| 183 | |
| 184 | /// Spawns a task on the foreground executor. |
| 185 | pub fn spawn<F, R>(&self, f: F) -> Task<R> |
| 186 | where |
| 187 | F: Future<Output = R> + 'static, |
| 188 | R: 'static, |
| 189 | { |
| 190 | self.foreground_executor.spawn(f) |
| 191 | } |
| 192 | |
| 193 | /// Checks if a global of type G exists. |
| 194 | pub fn has_global<G: Global>(&self) -> bool { |
| 195 | let app = self.app.borrow(); |
| 196 | app.has_global::<G>() |
| 197 | } |
| 198 | |
| 199 | /// Reads a global value. |
| 200 | pub fn read_global<G: Global, R>(&self, f: impl FnOnce(&G, &App) -> R) -> R { |
| 201 | let app = self.app.borrow(); |
| 202 | f(app.global::<G>(), &app) |
| 203 | } |
| 204 | |
| 205 | /// Sets a global value. |
| 206 | pub fn set_global<G: Global>(&mut self, global: G) { |
| 207 | let mut app = self.app.borrow_mut(); |
| 208 | app.set_global(global); |
| 209 | } |
| 210 | |
| 211 | /// Updates a global value. |
| 212 | pub fn update_global<G: Global, R>(&mut self, f: impl FnOnce(&mut G, &mut App) -> R) -> R { |
| 213 | let mut lock = self.app.borrow_mut(); |
| 214 | lock.update(|cx| { |
| 215 | let mut global = cx.lease_global::<G>(); |
| 216 | let result = f(&mut global, cx); |
| 217 | cx.end_global_lease(global); |
| 218 | result |
| 219 | }) |
| 220 | } |
| 221 | |
| 222 | /// Simulates a sequence of keystrokes on the given window. |
| 223 | /// |
| 224 | /// Keystrokes are specified as a space-separated string, e.g., "cmd-p escape". |
| 225 | pub fn simulate_keystrokes(&mut self, window: AnyWindowHandle, keystrokes: &str) { |
| 226 | for keystroke_text in keystrokes.split_whitespace() { |
| 227 | let keystroke = Keystroke::parse(keystroke_text) |
| 228 | .unwrap_or_else(|_| panic!("Invalid keystroke: {}", keystroke_text)); |
| 229 | self.dispatch_keystroke(window, keystroke); |
| 230 | } |
| 231 | self.run_until_parked(); |
| 232 | } |
| 233 | |
| 234 | /// Dispatches a single keystroke to a window. |
| 235 | pub fn dispatch_keystroke(&mut self, window: AnyWindowHandle, keystroke: Keystroke) { |
| 236 | self.update_window(window, |_, window, cx| { |
| 237 | window.dispatch_keystroke(keystroke, cx); |
| 238 | }) |
| 239 | .ok(); |
| 240 | } |
| 241 | |
| 242 | /// Simulates typing text input on the given window. |
| 243 | pub fn simulate_input(&mut self, window: AnyWindowHandle, input: &str) { |
| 244 | for char in input.chars() { |
| 245 | let key = char.to_string(); |
| 246 | let keystroke = Keystroke { |
| 247 | modifiers: Modifiers::default(), |
| 248 | key: key.clone(), |
| 249 | key_char: Some(key), |
| 250 | }; |
| 251 | self.dispatch_keystroke(window, keystroke); |
| 252 | } |
| 253 | self.run_until_parked(); |
| 254 | } |
| 255 | |
| 256 | /// Simulates a mouse move event. |
| 257 | pub fn simulate_mouse_move( |
| 258 | &mut self, |
| 259 | window: AnyWindowHandle, |
| 260 | position: Point<Pixels>, |
| 261 | button: impl Into<Option<MouseButton>>, |
| 262 | modifiers: Modifiers, |
| 263 | ) { |
| 264 | self.simulate_event( |
| 265 | window, |
| 266 | MouseMoveEvent { |
| 267 | position, |
| 268 | modifiers, |
| 269 | pressed_button: button.into(), |
| 270 | }, |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | /// Simulates a mouse down event. |
| 275 | pub fn simulate_mouse_down( |
| 276 | &mut self, |
| 277 | window: AnyWindowHandle, |
| 278 | position: Point<Pixels>, |
| 279 | button: MouseButton, |
| 280 | modifiers: Modifiers, |
| 281 | ) { |
| 282 | self.simulate_event( |
| 283 | window, |
| 284 | MouseDownEvent { |
| 285 | position, |
| 286 | modifiers, |
| 287 | button, |
| 288 | click_count: 1, |
| 289 | first_mouse: false, |
| 290 | }, |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | /// Simulates a mouse up event. |
| 295 | pub fn simulate_mouse_up( |
| 296 | &mut self, |
| 297 | window: AnyWindowHandle, |
| 298 | position: Point<Pixels>, |
| 299 | button: MouseButton, |
| 300 | modifiers: Modifiers, |
| 301 | ) { |
| 302 | self.simulate_event( |
| 303 | window, |
| 304 | MouseUpEvent { |
| 305 | position, |
| 306 | modifiers, |
| 307 | button, |
| 308 | click_count: 1, |
| 309 | }, |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | /// Simulates a click (mouse down followed by mouse up). |
| 314 | pub fn simulate_click( |
| 315 | &mut self, |
| 316 | window: AnyWindowHandle, |
| 317 | position: Point<Pixels>, |
| 318 | modifiers: Modifiers, |
| 319 | ) { |
| 320 | self.simulate_mouse_down(window, position, MouseButton::Left, modifiers); |
| 321 | self.simulate_mouse_up(window, position, MouseButton::Left, modifiers); |
| 322 | } |
| 323 | |
| 324 | /// Simulates an input event on the given window. |
| 325 | pub fn simulate_event<E: InputEvent>(&mut self, window: AnyWindowHandle, event: E) { |
| 326 | self.update_window(window, |_, window, cx| { |
| 327 | window.dispatch_event(event.to_platform_input(), cx); |
| 328 | }) |
| 329 | .ok(); |
| 330 | self.run_until_parked(); |
| 331 | } |
| 332 | |
| 333 | /// Dispatches an action to the given window. |
| 334 | pub fn dispatch_action(&mut self, window: AnyWindowHandle, action: impl Action) { |
| 335 | self.update_window(window, |_, window, cx| { |
| 336 | window.dispatch_action(action.boxed_clone(), cx); |
| 337 | }) |
| 338 | .ok(); |
| 339 | self.run_until_parked(); |
| 340 | } |
| 341 | |
| 342 | /// Writes to the clipboard. |
| 343 | pub fn write_to_clipboard(&self, item: ClipboardItem) { |
| 344 | self.platform.write_to_clipboard(item); |
| 345 | } |
| 346 | |
| 347 | /// Reads from the clipboard. |
| 348 | pub fn read_from_clipboard(&self) -> Option<ClipboardItem> { |
| 349 | self.platform.read_from_clipboard() |
| 350 | } |
| 351 | |
| 352 | /// Waits for a condition to become true, with a timeout. |
| 353 | pub async fn wait_for<T: 'static>( |
| 354 | &mut self, |
| 355 | entity: &Entity<T>, |
| 356 | predicate: impl Fn(&T) -> bool, |
| 357 | timeout: Duration, |
| 358 | ) -> Result<()> { |
| 359 | let start = web_time::Instant::now(); |
| 360 | loop { |
| 361 | { |
| 362 | let app = self.app.borrow(); |
| 363 | if predicate(entity.read(&app)) { |
| 364 | return Ok(()); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if start.elapsed() > timeout { |
| 369 | return Err(anyhow!("Timed out waiting for condition")); |
| 370 | } |
| 371 | |
| 372 | self.run_until_parked(); |
| 373 | self.background_executor |
| 374 | .timer(Duration::from_millis(10)) |
| 375 | .await; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /// Captures a screenshot of the specified window using direct texture capture. |
| 380 | /// |
| 381 | /// This renders the scene to a Metal texture and reads the pixels directly, |
| 382 | /// which does not require the window to be visible on screen. |
| 383 | #[cfg(any(test, feature = "test-support"))] |
| 384 | pub fn capture_screenshot(&mut self, window: AnyWindowHandle) -> Result<RgbaImage> { |
| 385 | self.update_window(window, |_, window, _cx| window.render_to_image())? |
| 386 | } |
| 387 | |
| 388 | /// Waits for animations to complete by waiting a couple of frames. |
| 389 | pub async fn wait_for_animations(&self) { |
| 390 | self.background_executor |
| 391 | .timer(Duration::from_millis(32)) |
| 392 | .await; |
| 393 | self.run_until_parked(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | impl AppContext for VisualTestAppContext { |
| 398 | fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T> { |
| 399 | let mut app = self.app.borrow_mut(); |
| 400 | app.new(build_entity) |
| 401 | } |
| 402 | |
| 403 | fn reserve_entity<T: 'static>(&mut self) -> crate::Reservation<T> { |
| 404 | let mut app = self.app.borrow_mut(); |
| 405 | app.reserve_entity() |
| 406 | } |
| 407 | |
| 408 | fn insert_entity<T: 'static>( |
| 409 | &mut self, |
| 410 | reservation: crate::Reservation<T>, |
| 411 | build_entity: impl FnOnce(&mut Context<T>) -> T, |
| 412 | ) -> Entity<T> { |
| 413 | let mut app = self.app.borrow_mut(); |
| 414 | app.insert_entity(reservation, build_entity) |
| 415 | } |
| 416 | |
| 417 | fn update_entity<T: 'static, R>( |
| 418 | &mut self, |
| 419 | handle: &Entity<T>, |
| 420 | update: impl FnOnce(&mut T, &mut Context<T>) -> R, |
| 421 | ) -> R { |
| 422 | let mut app = self.app.borrow_mut(); |
| 423 | app.update_entity(handle, update) |
| 424 | } |
| 425 | |
| 426 | fn as_mut<'a, T>(&'a mut self, _: &Entity<T>) -> crate::GpuiBorrow<'a, T> |
| 427 | where |
| 428 | T: 'static, |
| 429 | { |
| 430 | panic!("Cannot use as_mut with a visual test app context. Try calling update() first") |
| 431 | } |
| 432 | |
| 433 | fn read_entity<T, R>(&self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R) -> R |
| 434 | where |
| 435 | T: 'static, |
| 436 | { |
| 437 | let app = self.app.borrow(); |
| 438 | app.read_entity(handle, read) |
| 439 | } |
| 440 | |
| 441 | fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T> |
| 442 | where |
| 443 | F: FnOnce(AnyView, &mut Window, &mut App) -> T, |
| 444 | { |
| 445 | let mut lock = self.app.borrow_mut(); |
| 446 | lock.update_window(window, f) |
| 447 | } |
| 448 | |
| 449 | fn with_window<R>( |
| 450 | &mut self, |
| 451 | entity_id: EntityId, |
| 452 | f: impl FnOnce(&mut Window, &mut App) -> R, |
| 453 | ) -> Option<R> { |
| 454 | let mut lock = self.app.borrow_mut(); |
| 455 | lock.with_window(entity_id, f) |
| 456 | } |
| 457 | |
| 458 | fn read_window<T, R>( |
| 459 | &self, |
| 460 | window: &WindowHandle<T>, |
| 461 | read: impl FnOnce(Entity<T>, &App) -> R, |
| 462 | ) -> Result<R> |
| 463 | where |
| 464 | T: 'static, |
| 465 | { |
| 466 | let app = self.app.borrow(); |
| 467 | app.read_window(window, read) |
| 468 | } |
| 469 | |
| 470 | fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R> |
| 471 | where |
| 472 | R: Send + 'static, |
| 473 | { |
| 474 | self.background_executor.spawn(future) |
| 475 | } |
| 476 | |
| 477 | fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R |
| 478 | where |
| 479 | G: Global, |
| 480 | { |
| 481 | let app = self.app.borrow(); |
| 482 | callback(app.global::<G>(), &app) |
| 483 | } |
| 484 | } |