1use crate::{
2 AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId, ForegroundExecutor,
3 Platform, PlatformDisplay, PlatformTextSystem, TestDisplay, TestWindow, WindowOptions,
4};
5use anyhow::{anyhow, Result};
6use collections::VecDeque;
7use futures::channel::oneshot;
8use parking_lot::Mutex;
9use std::{
10 cell::RefCell,
11 path::PathBuf,
12 rc::{Rc, Weak},
13 sync::Arc,
14};
15
16pub struct TestPlatform {
17 background_executor: BackgroundExecutor,
18 foreground_executor: ForegroundExecutor,
19
20 active_window: Arc<Mutex<Option<AnyWindowHandle>>>,
21 active_display: Rc<dyn PlatformDisplay>,
22 active_cursor: Mutex<CursorStyle>,
23 current_clipboard_item: Mutex<Option<ClipboardItem>>,
24 pub(crate) prompts: RefCell<TestPrompts>,
25 weak: Weak<Self>,
26}
27
28#[derive(Default)]
29pub(crate) struct TestPrompts {
30 multiple_choice: VecDeque<oneshot::Sender<usize>>,
31 new_path: VecDeque<(PathBuf, oneshot::Sender<Option<PathBuf>>)>,
32}
33
34impl TestPlatform {
35 pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc<Self> {
36 Rc::new_cyclic(|weak| TestPlatform {
37 background_executor: executor,
38 foreground_executor,
39 prompts: Default::default(),
40 active_cursor: Default::default(),
41 active_display: Rc::new(TestDisplay::new()),
42 active_window: Default::default(),
43 current_clipboard_item: Mutex::new(None),
44 weak: weak.clone(),
45 })
46 }
47
48 pub(crate) fn simulate_new_path_selection(
49 &self,
50 select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>,
51 ) {
52 let (path, tx) = self
53 .prompts
54 .borrow_mut()
55 .new_path
56 .pop_front()
57 .expect("no pending new path prompt");
58 tx.send(select_path(&path)).ok();
59 }
60
61 pub(crate) fn simulate_prompt_answer(&self, response_ix: usize) {
62 let tx = self
63 .prompts
64 .borrow_mut()
65 .multiple_choice
66 .pop_front()
67 .expect("no pending multiple choice prompt");
68 tx.send(response_ix).ok();
69 }
70
71 pub(crate) fn has_pending_prompt(&self) -> bool {
72 !self.prompts.borrow().multiple_choice.is_empty()
73 }
74
75 pub(crate) fn prompt(&self) -> oneshot::Receiver<usize> {
76 let (tx, rx) = oneshot::channel();
77 self.prompts.borrow_mut().multiple_choice.push_back(tx);
78 rx
79 }
80}
81
82// todo!("implement out what our tests needed in GPUI 1")
83impl Platform for TestPlatform {
84 fn background_executor(&self) -> BackgroundExecutor {
85 self.background_executor.clone()
86 }
87
88 fn foreground_executor(&self) -> ForegroundExecutor {
89 self.foreground_executor.clone()
90 }
91
92 fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
93 Arc::new(crate::platform::mac::MacTextSystem::new())
94 }
95
96 fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
97 unimplemented!()
98 }
99
100 fn quit(&self) {}
101
102 fn restart(&self) {
103 unimplemented!()
104 }
105
106 fn activate(&self, _ignoring_other_apps: bool) {
107 unimplemented!()
108 }
109
110 fn hide(&self) {
111 unimplemented!()
112 }
113
114 fn hide_other_apps(&self) {
115 unimplemented!()
116 }
117
118 fn unhide_other_apps(&self) {
119 unimplemented!()
120 }
121
122 fn displays(&self) -> Vec<std::rc::Rc<dyn crate::PlatformDisplay>> {
123 vec![self.active_display.clone()]
124 }
125
126 fn display(&self, id: DisplayId) -> Option<std::rc::Rc<dyn crate::PlatformDisplay>> {
127 self.displays().iter().find(|d| d.id() == id).cloned()
128 }
129
130 fn main_window(&self) -> Option<crate::AnyWindowHandle> {
131 unimplemented!()
132 }
133
134 fn open_window(
135 &self,
136 handle: AnyWindowHandle,
137 options: WindowOptions,
138 ) -> Box<dyn crate::PlatformWindow> {
139 *self.active_window.lock() = Some(handle);
140 Box::new(TestWindow::new(
141 options,
142 self.weak.clone(),
143 self.active_display.clone(),
144 ))
145 }
146
147 fn set_display_link_output_callback(
148 &self,
149 _display_id: DisplayId,
150 mut callback: Box<dyn FnMut(&crate::VideoTimestamp, &crate::VideoTimestamp) + Send>,
151 ) {
152 let timestamp = crate::VideoTimestamp {
153 version: 0,
154 video_time_scale: 0,
155 video_time: 0,
156 host_time: 0,
157 rate_scalar: 0.0,
158 video_refresh_period: 0,
159 smpte_time: crate::SmtpeTime::default(),
160 flags: 0,
161 reserved: 0,
162 };
163 callback(×tamp, ×tamp)
164 }
165
166 fn start_display_link(&self, _display_id: DisplayId) {}
167
168 fn stop_display_link(&self, _display_id: DisplayId) {}
169
170 fn open_url(&self, _url: &str) {
171 unimplemented!()
172 }
173
174 fn on_open_urls(&self, _callback: Box<dyn FnMut(Vec<String>)>) {
175 unimplemented!()
176 }
177
178 fn prompt_for_paths(
179 &self,
180 _options: crate::PathPromptOptions,
181 ) -> oneshot::Receiver<Option<Vec<std::path::PathBuf>>> {
182 unimplemented!()
183 }
184
185 fn prompt_for_new_path(
186 &self,
187 directory: &std::path::Path,
188 ) -> oneshot::Receiver<Option<std::path::PathBuf>> {
189 let (tx, rx) = oneshot::channel();
190 self.prompts
191 .borrow_mut()
192 .new_path
193 .push_back((directory.to_path_buf(), tx));
194 rx
195 }
196
197 fn reveal_path(&self, _path: &std::path::Path) {
198 unimplemented!()
199 }
200
201 fn on_become_active(&self, _callback: Box<dyn FnMut()>) {}
202
203 fn on_resign_active(&self, _callback: Box<dyn FnMut()>) {}
204
205 fn on_quit(&self, _callback: Box<dyn FnMut()>) {}
206
207 fn on_reopen(&self, _callback: Box<dyn FnMut()>) {
208 unimplemented!()
209 }
210
211 fn on_event(&self, _callback: Box<dyn FnMut(crate::InputEvent) -> bool>) {
212 unimplemented!()
213 }
214
215 fn os_name(&self) -> &'static str {
216 "test"
217 }
218
219 fn os_version(&self) -> Result<crate::SemanticVersion> {
220 Err(anyhow!("os_version called on TestPlatform"))
221 }
222
223 fn app_version(&self) -> Result<crate::SemanticVersion> {
224 Err(anyhow!("app_version called on TestPlatform"))
225 }
226
227 fn app_path(&self) -> Result<std::path::PathBuf> {
228 unimplemented!()
229 }
230
231 fn local_timezone(&self) -> time::UtcOffset {
232 unimplemented!()
233 }
234
235 fn path_for_auxiliary_executable(&self, _name: &str) -> Result<std::path::PathBuf> {
236 unimplemented!()
237 }
238
239 fn set_cursor_style(&self, style: crate::CursorStyle) {
240 *self.active_cursor.lock() = style;
241 }
242
243 fn should_auto_hide_scrollbars(&self) -> bool {
244 // todo()
245 true
246 }
247
248 fn write_to_clipboard(&self, item: ClipboardItem) {
249 *self.current_clipboard_item.lock() = Some(item);
250 }
251
252 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
253 self.current_clipboard_item.lock().clone()
254 }
255
256 fn write_credentials(&self, _url: &str, _username: &str, _password: &[u8]) -> Result<()> {
257 Ok(())
258 }
259
260 fn read_credentials(&self, _url: &str) -> Result<Option<(String, Vec<u8>)>> {
261 Ok(None)
262 }
263
264 fn delete_credentials(&self, _url: &str) -> Result<()> {
265 Ok(())
266 }
267}