use crate::{ AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DevicePixels, DummyKeyboardMapper, ForegroundExecutor, Keymap, NoopTextSystem, PathPromptOptions, Platform, PlatformDisplay, PlatformHeadlessRenderer, PlatformKeyboardLayout, PlatformKeyboardMapper, PlatformTextSystem, PromptButton, ScreenCaptureFrame, ScreenCaptureSource, ScreenCaptureStream, SharedString, SourceMetadata, SystemNotification, SystemNotificationResponse, Task, TestDisplay, TestWindow, ThermalState, WindowAppearance, WindowParams, size, }; use anyhow::Result; use collections::VecDeque; use futures::channel::oneshot; use parking_lot::Mutex; use std::{ cell::RefCell, path::{Path, PathBuf}, rc::{Rc, Weak}, sync::Arc, }; /// TestPlatform implements the Platform trait for use in tests. pub(crate) struct TestPlatform { background_executor: BackgroundExecutor, foreground_executor: ForegroundExecutor, pub(crate) active_window: RefCell>, active_display: Rc, active_cursor: Mutex, current_clipboard_item: Mutex>, #[cfg(any(target_os = "linux", target_os = "freebsd"))] current_primary_item: Mutex>, #[cfg(target_os = "macos")] current_find_pasteboard_item: Mutex>, pub(crate) prompts: RefCell, screen_capture_sources: RefCell>, pub opened_url: RefCell>, pub(crate) system_notifications: RefCell, pub text_system: Arc, pub expect_restart: RefCell>>>, headless_renderer_factory: Option Option>>>, weak: Weak, } #[derive(Clone)] /// A fake screen capture source, used for testing. pub struct TestScreenCaptureSource {} /// A fake screen capture stream, used for testing. pub struct TestScreenCaptureStream {} impl ScreenCaptureSource for TestScreenCaptureSource { fn metadata(&self) -> Result { Ok(SourceMetadata { id: 0, is_main: None, label: None, resolution: size(DevicePixels(1), DevicePixels(1)), }) } fn stream( &self, _foreground_executor: &ForegroundExecutor, _frame_callback: Box, ) -> oneshot::Receiver>> { let (mut tx, rx) = oneshot::channel(); let stream = TestScreenCaptureStream {}; tx.send(Ok(Box::new(stream) as Box)) .ok(); rx } } impl ScreenCaptureStream for TestScreenCaptureStream { fn metadata(&self) -> Result { TestScreenCaptureSource {}.metadata() } } struct TestPrompt { msg: String, detail: Option, answers: Vec, tx: oneshot::Sender, } #[derive(Default)] pub(crate) struct TestSystemNotifications { pub(crate) app_identity: Option<(SharedString, SharedString)>, pub(crate) shown: Vec, pub(crate) delivered: Vec, pub(crate) dismissed: Vec, response_callback: Option>, } #[derive(Default)] pub(crate) struct TestPrompts { multiple_choice: VecDeque, new_path: VecDeque<(PathBuf, oneshot::Sender>>)>, paths: VecDeque<( PathPromptOptions, oneshot::Sender>>>, )>, } impl TestPlatform { pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc { Self::with_platform( executor, foreground_executor, Arc::new(NoopTextSystem), None, ) } pub fn with_text_system( executor: BackgroundExecutor, foreground_executor: ForegroundExecutor, text_system: Arc, ) -> Rc { Self::with_platform(executor, foreground_executor, text_system, None) } pub fn with_platform( executor: BackgroundExecutor, foreground_executor: ForegroundExecutor, text_system: Arc, headless_renderer_factory: Option< Box Option>>, >, ) -> Rc { Rc::new_cyclic(|weak| TestPlatform { background_executor: executor, foreground_executor, prompts: Default::default(), screen_capture_sources: Default::default(), active_cursor: Default::default(), active_display: Rc::new(TestDisplay::new()), active_window: Default::default(), expect_restart: Default::default(), current_clipboard_item: Mutex::new(None), #[cfg(any(target_os = "linux", target_os = "freebsd"))] current_primary_item: Mutex::new(None), #[cfg(target_os = "macos")] current_find_pasteboard_item: Mutex::new(None), weak: weak.clone(), opened_url: Default::default(), system_notifications: Default::default(), text_system, headless_renderer_factory, }) } pub(crate) fn simulate_new_path_selection( &self, select_path: impl FnOnce(&std::path::Path) -> Option, ) { let (path, tx) = self .prompts .borrow_mut() .new_path .pop_front() .expect("no pending new path prompt"); tx.send(Ok(select_path(&path))).ok(); } pub(crate) fn simulate_path_prompt_response( &self, select_paths: impl FnOnce(&PathPromptOptions) -> Option>, ) { let (options, tx) = self .prompts .borrow_mut() .paths .pop_front() .expect("no pending paths prompt"); let selection = select_paths(&options); if let Some(paths) = &selection && !options.multiple && paths.len() > 1 { panic!( "selected {} paths for a prompt that does not allow multiple selection", paths.len() ); } tx.send(Ok(selection)).ok(); } pub(crate) fn did_prompt_for_paths(&self) -> bool { !self.prompts.borrow().paths.is_empty() } #[track_caller] pub(crate) fn simulate_prompt_answer(&self, response: &str) { let prompt = self .prompts .borrow_mut() .multiple_choice .pop_front() .expect("no pending multiple choice prompt"); let Some(ix) = prompt.answers.iter().position(|a| a == response) else { panic!( "PROMPT: {}\n{:?}\n{:?}\nCannot respond with {}", prompt.msg, prompt.detail, prompt.answers, response ) }; prompt.tx.send(ix).ok(); } pub(crate) fn has_pending_prompt(&self) -> bool { !self.prompts.borrow().multiple_choice.is_empty() } pub(crate) fn pending_prompt(&self) -> Option<(String, String)> { let prompts = self.prompts.borrow(); let prompt = prompts.multiple_choice.front()?; Some(( prompt.msg.clone(), prompt.detail.clone().unwrap_or_default(), )) } pub(crate) fn set_screen_capture_sources(&self, sources: Vec) { *self.screen_capture_sources.borrow_mut() = sources; } pub(crate) fn prompt( &self, msg: &str, detail: Option<&str>, answers: &[PromptButton], ) -> oneshot::Receiver { let (tx, rx) = oneshot::channel(); let answers: Vec = answers.iter().map(|s| s.label().to_string()).collect(); self.prompts .borrow_mut() .multiple_choice .push_back(TestPrompt { msg: msg.to_string(), detail: detail.map(|s| s.to_string()), answers, tx, }); rx } pub(crate) fn set_active_window(&self, window: Option) { let executor = self.foreground_executor(); let previous_window = self.active_window.borrow_mut().take(); self.active_window.borrow_mut().clone_from(&window); executor .spawn(async move { if let Some(previous_window) = previous_window { if let Some(window) = window.as_ref() && Rc::ptr_eq(&previous_window.0, &window.0) { return; } previous_window.simulate_active_status_change(false); } if let Some(window) = window { window.simulate_active_status_change(true); } }) .detach(); } pub(crate) fn did_prompt_for_new_path(&self) -> bool { !self.prompts.borrow().new_path.is_empty() } pub(crate) fn app_identity(&self) -> Option<(SharedString, SharedString)> { self.system_notifications.borrow().app_identity.clone() } pub(crate) fn shown_system_notifications(&self) -> Vec { self.system_notifications.borrow().shown.clone() } pub(crate) fn delivered_system_notifications(&self) -> Vec { self.system_notifications.borrow().delivered.clone() } pub(crate) fn dismissed_system_notifications(&self) -> Vec { self.system_notifications.borrow().dismissed.clone() } pub(crate) fn simulate_system_notification_response( &self, response: SystemNotificationResponse, ) { let callback = self .system_notifications .borrow_mut() .response_callback .take(); if let Some(mut callback) = callback { callback(response); self.system_notifications .borrow_mut() .response_callback .get_or_insert(callback); } } } impl Platform for TestPlatform { fn background_executor(&self) -> BackgroundExecutor { self.background_executor.clone() } fn foreground_executor(&self) -> ForegroundExecutor { self.foreground_executor.clone() } fn text_system(&self) -> Arc { self.text_system.clone() } fn keyboard_layout(&self) -> Box { Box::new(TestKeyboardLayout) } fn keyboard_mapper(&self) -> Rc { Rc::new(DummyKeyboardMapper) } fn on_keyboard_layout_change(&self, _: Box) {} fn on_thermal_state_change(&self, _: Box) {} fn thermal_state(&self) -> ThermalState { ThermalState::Nominal } fn run(&self, _on_finish_launching: Box) { unimplemented!() } fn quit(&self) {} fn restart(&self, path: Option) { if let Some(tx) = self.expect_restart.take() { tx.send(path).unwrap(); } } fn activate(&self, _ignoring_other_apps: bool) { // } fn hide(&self) { unimplemented!() } fn hide_other_apps(&self) { unimplemented!() } fn unhide_other_apps(&self) { unimplemented!() } fn displays(&self) -> Vec> { vec![self.active_display.clone()] } fn primary_display(&self) -> Option> { Some(self.active_display.clone()) } fn is_screen_capture_supported(&self) -> bool { true } fn screen_capture_sources( &self, ) -> oneshot::Receiver>>> { let (mut tx, rx) = oneshot::channel(); tx.send(Ok(self .screen_capture_sources .borrow() .iter() .map(|source| Rc::new(source.clone()) as Rc) .collect())) .ok(); rx } fn active_window(&self) -> Option { self.active_window .borrow() .as_ref() .map(|window| window.0.lock().handle) } fn open_window( &self, handle: AnyWindowHandle, params: WindowParams, ) -> anyhow::Result> { let renderer = self.headless_renderer_factory.as_ref().and_then(|f| f()); let window = TestWindow::new( handle, params, self.weak.clone(), self.active_display.clone(), renderer, ); Ok(Box::new(window)) } fn window_appearance(&self) -> WindowAppearance { WindowAppearance::Light } fn open_url(&self, url: &str) { *self.opened_url.borrow_mut() = Some(url.to_string()) } fn on_open_urls(&self, _callback: Box)>) { unimplemented!() } fn prompt_for_paths( &self, options: crate::PathPromptOptions, ) -> oneshot::Receiver>>> { let (tx, rx) = oneshot::channel(); self.prompts.borrow_mut().paths.push_back((options, tx)); rx } fn prompt_for_new_path( &self, directory: &std::path::Path, _suggested_name: Option<&str>, ) -> oneshot::Receiver>> { let (tx, rx) = oneshot::channel(); self.prompts .borrow_mut() .new_path .push_back((directory.to_path_buf(), tx)); rx } fn can_select_mixed_files_and_dirs(&self) -> bool { true } fn reveal_path(&self, _path: &std::path::Path) { unimplemented!() } fn on_quit(&self, _callback: Box) {} fn on_reopen(&self, _callback: Box) { unimplemented!() } fn on_system_wake(&self, _callback: Box) {} fn set_app_identity(&self, identifier: &str, name: &str) { self.system_notifications.borrow_mut().app_identity = Some((identifier.to_string().into(), name.to_string().into())); } fn show_system_notification(&self, notification: SystemNotification) { let mut system_notifications = self.system_notifications.borrow_mut(); if system_notifications.app_identity.is_none() { return; } let delivered = system_notifications .delivered .iter_mut() .find(|delivered| delivered.tag == notification.tag); if let Some(delivered) = delivered { *delivered = notification.clone(); } else { system_notifications.delivered.push(notification.clone()); } system_notifications.shown.push(notification); } fn dismiss_system_notification(&self, tag: &str) { let mut system_notifications = self.system_notifications.borrow_mut(); system_notifications .delivered .retain(|notification| notification.tag != tag); system_notifications .dismissed .push(SharedString::from(tag.to_string())); } fn on_system_notification_response( &self, callback: Box, ) { self.system_notifications.borrow_mut().response_callback = Some(callback); } fn set_menus(&self, _menus: Vec, _keymap: &Keymap) {} fn set_dock_menu(&self, _menu: Vec, _keymap: &Keymap) {} fn add_recent_document(&self, _paths: &Path) {} fn on_app_menu_action(&self, _callback: Box) {} fn on_will_open_app_menu(&self, _callback: Box) {} fn on_validate_app_menu_command(&self, _callback: Box bool>) {} fn app_path(&self) -> Result { unimplemented!() } fn path_for_auxiliary_executable(&self, _name: &str) -> Result { unimplemented!() } fn set_cursor_style(&self, style: crate::CursorStyle) { *self.active_cursor.lock() = style; } fn hide_cursor_until_mouse_moves(&self) {} fn is_cursor_visible(&self) -> bool { true } fn should_auto_hide_scrollbars(&self) -> bool { false } fn read_from_clipboard(&self) -> Option { self.current_clipboard_item.lock().clone() } fn write_to_clipboard(&self, item: ClipboardItem) { *self.current_clipboard_item.lock() = Some(item); } #[cfg(any(target_os = "linux", target_os = "freebsd"))] fn read_from_primary(&self) -> Option { self.current_primary_item.lock().clone() } #[cfg(any(target_os = "linux", target_os = "freebsd"))] fn write_to_primary(&self, item: ClipboardItem) { *self.current_primary_item.lock() = Some(item); } #[cfg(target_os = "macos")] fn read_from_find_pasteboard(&self) -> Option { self.current_find_pasteboard_item.lock().clone() } #[cfg(target_os = "macos")] fn write_to_find_pasteboard(&self, item: ClipboardItem) { *self.current_find_pasteboard_item.lock() = Some(item); } fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Task> { Task::ready(Ok(())) } fn read_credentials(&self, _url: &str) -> Task)>>> { Task::ready(Ok(None)) } fn delete_credentials(&self, _url: &str) -> Task> { Task::ready(Ok(())) } fn register_url_scheme(&self, _: &str) -> Task> { unimplemented!() } fn open_with_system(&self, _path: &Path) { unimplemented!() } } impl TestScreenCaptureSource { /// Create a fake screen capture source, for testing. pub fn new() -> Self { Self {} } } struct TestKeyboardLayout; impl PlatformKeyboardLayout for TestKeyboardLayout { fn id(&self) -> &str { "zed.keyboard.example" } fn name(&self) -> &str { "zed.keyboard.example" } }