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