1use crate::{Application, BackgroundExecutor, Platform};
8use std::rc::Rc;
9
10pub fn background_executor() -> BackgroundExecutor {
12 current_platform(true).background_executor()
13}
14
15pub fn application() -> Application {
17 #[cfg(target_family = "wasm")]
18 {
19 application_with_web_backend(gpui_web::WebBackendPreference::Auto)
20 }
21
22 #[cfg(not(target_family = "wasm"))]
23 Application::with_platform(current_platform(false))
24}
25
26pub fn headless() -> Application {
28 Application::with_platform(current_platform(true))
29}
30
31#[cfg(target_family = "wasm")]
32pub use gpui_web::WebBackendPreference;
33
34#[cfg(target_family = "wasm")]
35pub fn application_with_web_backend(backend_preference: WebBackendPreference) -> Application {
36 let platform = Rc::new(gpui_web::WebPlatform::new_with_backend(
37 true,
38 backend_preference,
39 ));
40 let http_client = std::sync::Arc::new(platform.fetch_http_client());
41 Application::with_platform(platform).with_http_client(http_client)
42}
43
44#[cfg(target_family = "wasm")]
46pub fn single_threaded_web() -> Application {
47 let platform = Rc::new(gpui_web::WebPlatform::new(false));
48 let http_client = std::sync::Arc::new(platform.fetch_http_client());
49 Application::with_platform(platform).with_http_client(http_client)
50}
51
52#[cfg(target_family = "wasm")]
56pub fn web_init() {
57 gpui_web::init_logging();
58}
59
60pub fn current_platform(headless: bool) -> Rc<dyn Platform> {
62 #[cfg(target_os = "macos")]
63 {
64 Rc::new(gpui_macos::MacPlatform::new(headless))
65 }
66
67 #[cfg(target_os = "windows")]
68 {
69 Rc::new(
70 gpui_windows::WindowsPlatform::new(headless)
71 .expect("failed to initialize Windows platform"),
72 )
73 }
74
75 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
76 {
77 gpui_linux::current_platform(headless)
78 }
79
80 #[cfg(target_family = "wasm")]
81 {
82 let _ = headless;
83 Rc::new(gpui_web::WebPlatform::new(true))
84 }
85}
86
87#[cfg(any(feature = "bench-support", feature = "test-support"))]
89pub fn current_headless_renderer() -> Option<Box<dyn crate::SceneRenderer>> {
90 #[cfg(target_os = "macos")]
91 {
92 Some(Box::new(
93 gpui_macos::metal_renderer::MetalHeadlessRenderer::new(),
94 ))
95 }
96
97 #[cfg(not(target_os = "macos"))]
98 {
99 None
100 }
101}
102
103#[cfg(all(test, target_os = "macos"))]
104mod tests {
105 use super::*;
106 use crate::{AppContext, Empty, VisualTestAppContext};
107 use std::cell::RefCell;
108 use std::time::Duration;
109
110 #[test]
118 #[ignore] fn test_foreground_tasks_run_with_run_until_parked() {
120 let mut cx = VisualTestAppContext::new(current_platform(false));
121
122 let task_ran = Rc::new(RefCell::new(false));
123
124 {
127 let task_ran = task_ran.clone();
128 cx.update(|cx| {
129 cx.spawn(async move |_| {
130 *task_ran.borrow_mut() = true;
131 })
132 .detach();
133 });
134 }
135
136 assert!(!*task_ran.borrow());
138
139 cx.run_until_parked();
141
142 assert!(*task_ran.borrow());
144 }
145
146 #[test]
147 #[ignore] fn test_advance_clock_triggers_delayed_tasks() {
149 let mut cx = VisualTestAppContext::new(current_platform(false));
150
151 let task_ran = Rc::new(RefCell::new(false));
152
153 {
155 let task_ran = task_ran.clone();
156 let executor = cx.background_executor.clone();
157 cx.update(|cx| {
158 cx.spawn(async move |_| {
159 executor.timer(Duration::from_millis(500)).await;
160 *task_ran.borrow_mut() = true;
161 })
162 .detach();
163 });
164 }
165
166 cx.run_until_parked();
168 assert!(!*task_ran.borrow());
169
170 cx.advance_clock(Duration::from_millis(600));
172
173 assert!(*task_ran.borrow());
175 }
176
177 #[test]
178 #[ignore] fn test_window_spawn_uses_test_dispatcher() {
180 let mut cx = VisualTestAppContext::new(current_platform(false));
181
182 let task_ran = Rc::new(RefCell::new(false));
183
184 let window = cx
185 .open_offscreen_window_default(|_, cx| cx.new(|_| Empty))
186 .expect("Failed to open window");
187
188 {
191 let task_ran = task_ran.clone();
192 cx.update_window(window.into(), |_, window, cx| {
193 window
194 .spawn(cx, async move |_| {
195 *task_ran.borrow_mut() = true;
196 })
197 .detach();
198 })
199 .ok();
200 }
201
202 assert!(!*task_ran.borrow());
204
205 cx.run_until_parked();
207
208 assert!(*task_ran.borrow());
210 }
211}