Skip to main content

sui_testing/
app.rs

1use std::{cell::RefCell, rc::Rc};
2
3use sui_core::{Error, Result};
4use sui_render_wgpu::WgpuExternalTextureRegistry;
5use sui_runtime::{Runtime, set_window_render_options};
6
7use crate::{harness::Harness, window::TestWindow};
8
9pub trait IntoTestRuntime {
10    fn into_test_runtime(self) -> Result<Runtime>;
11}
12
13impl IntoTestRuntime for Runtime {
14    fn into_test_runtime(self) -> Result<Runtime> {
15        Ok(self)
16    }
17}
18
19impl IntoTestRuntime for sui_runtime::Application {
20    fn into_test_runtime(self) -> Result<Runtime> {
21        self.build()
22    }
23}
24
25impl IntoTestRuntime for sui::Application {
26    fn into_test_runtime(self) -> Result<Runtime> {
27        let initial_window_render_options = self.initial_window_render_options();
28        let runtime = self.build()?;
29        if let Some(options) = initial_window_render_options {
30            for window_id in runtime.window_ids() {
31                set_window_render_options(window_id, options);
32            }
33        }
34        Ok(runtime)
35    }
36}
37
38impl IntoTestRuntime for Result<Runtime> {
39    fn into_test_runtime(self) -> Result<Runtime> {
40        self
41    }
42}
43
44#[derive(Clone)]
45pub struct TestApp {
46    pub(crate) harness: Rc<RefCell<Harness>>,
47}
48
49impl TestApp {
50    pub fn new<F, A>(build: F) -> Result<Self>
51    where
52        F: FnOnce() -> A + Send + 'static,
53        A: IntoTestRuntime,
54    {
55        Self::new_with_options(build, true, false)
56    }
57
58    pub fn new_with_vsync<F, A>(build: F, vsync_enabled: bool) -> Result<Self>
59    where
60        F: FnOnce() -> A + Send + 'static,
61        A: IntoTestRuntime,
62    {
63        Self::new_with_options(build, vsync_enabled, false)
64    }
65
66    pub fn new_with_options<F, A>(build: F, vsync_enabled: bool, visible: bool) -> Result<Self>
67    where
68        F: FnOnce() -> A + Send + 'static,
69        A: IntoTestRuntime,
70    {
71        let harness = if live_backend_available() {
72            Harness::new_live_with_options(
73                move || build().into_test_runtime(),
74                vsync_enabled,
75                visible,
76            )?
77        } else {
78            Harness::new_headless_with_timeout(build().into_test_runtime()?, 30.0)?
79        };
80        let harness = Rc::new(RefCell::new(harness));
81        Ok(Self { harness })
82    }
83
84    pub fn new_no_vsync<F, A>(build: F) -> Result<Self>
85    where
86        F: FnOnce() -> A + Send + 'static,
87        A: IntoTestRuntime,
88    {
89        Self::new_with_vsync(build, false)
90    }
91
92    pub fn new_visible_no_vsync<F, A>(build: F) -> Result<Self>
93    where
94        F: FnOnce() -> A + Send + 'static,
95        A: IntoTestRuntime,
96    {
97        Self::new_with_options(build, false, true)
98    }
99
100    pub fn from_runtime(runtime: Runtime) -> Result<Self> {
101        let harness = Rc::new(RefCell::new(Harness::new_headless(runtime)?));
102        Ok(Self { harness })
103    }
104
105    /// Build a headless test harness that can resolve app-owned WGPU textures.
106    pub fn from_runtime_with_external_texture_registry(
107        runtime: Runtime,
108        registry: WgpuExternalTextureRegistry,
109    ) -> Result<Self> {
110        let harness = Rc::new(RefCell::new(
111            Harness::new_headless_with_external_texture_registry(runtime, registry)?,
112        ));
113        Ok(Self { harness })
114    }
115
116    pub fn from_runtime_with_frame_budget(runtime: Runtime, initial_frames: usize) -> Result<Self> {
117        let harness = Rc::new(RefCell::new(Harness::new_headless_with_frame_budget(
118            runtime,
119            initial_frames,
120        )?));
121        Ok(Self { harness })
122    }
123
124    pub fn set_default_timeout(&self, timeout: f64) -> Result<()> {
125        if timeout.is_sign_negative() {
126            return Err(Error::new("default timeout must be >= 0"));
127        }
128
129        self.harness.borrow_mut().set_default_timeout(timeout);
130        Ok(())
131    }
132
133    pub fn default_timeout(&self) -> f64 {
134        self.harness.borrow().default_timeout()
135    }
136
137    pub fn run_until_idle(&self) -> Result<()> {
138        self.harness.borrow_mut().run_until_idle()
139    }
140
141    pub fn pump_frames(&self, frames: usize) -> Result<()> {
142        self.harness.borrow_mut().pump_frames(frames)
143    }
144
145    pub fn advance_time(&self, delta: f64) -> Result<()> {
146        self.harness.borrow_mut().advance_time(delta)
147    }
148
149    pub fn windows(&self) -> Result<Vec<TestWindow>> {
150        let window_ids = self.harness.borrow().window_ids();
151        Ok(window_ids
152            .into_iter()
153            .map(|window_id| TestWindow::new(Rc::clone(&self.harness), window_id))
154            .collect())
155    }
156
157    pub fn main_window(&self) -> Result<TestWindow> {
158        let window_id = self
159            .harness
160            .borrow()
161            .window_ids()
162            .into_iter()
163            .next()
164            .ok_or_else(|| Error::new("test app did not create any windows"))?;
165        Ok(TestWindow::new(Rc::clone(&self.harness), window_id))
166    }
167
168    pub fn window_by_title(&self, title: &str) -> Result<TestWindow> {
169        let window_id = self
170            .harness
171            .borrow()
172            .window_id_by_title(title)
173            .ok_or_else(|| Error::new(format!("no test window found with title \"{title}\"")))?;
174        Ok(TestWindow::new(Rc::clone(&self.harness), window_id))
175    }
176}
177
178fn live_backend_available() -> bool {
179    #[cfg(target_os = "linux")]
180    {
181        std::env::var_os("WAYLAND_DISPLAY").is_some()
182            || std::env::var_os("WAYLAND_SOCKET").is_some()
183            || std::env::var_os("DISPLAY").is_some()
184    }
185    #[cfg(not(target_os = "linux"))]
186    {
187        true
188    }
189}