Skip to main content

sui_testing/
window.rs

1use std::{cell::RefCell, rc::Rc};
2
3use sui_core::{Event, Result, SemanticsRole, WindowId};
4use sui_render_wgpu::{DebugCaptureArtifact, DebugCaptureRequest};
5use sui_runtime::WindowPerformanceSnapshot;
6
7use crate::{
8    harness::Harness,
9    locator::Locator,
10    screenshot::{ArtifactBundle, Screenshot},
11    selector::Selector,
12    snapshot::WindowSnapshot,
13};
14
15#[derive(Clone)]
16pub struct TestWindow {
17    harness: Rc<RefCell<Harness>>,
18    window_id: WindowId,
19}
20
21impl TestWindow {
22    pub(crate) fn new(harness: Rc<RefCell<Harness>>, window_id: WindowId) -> Self {
23        Self { harness, window_id }
24    }
25
26    pub fn id(&self) -> WindowId {
27        self.window_id
28    }
29
30    pub fn snapshot(&self) -> Result<WindowSnapshot> {
31        let mut harness = self.harness.borrow_mut();
32        harness.run_until_idle()?;
33        harness.snapshot(self.window_id)
34    }
35
36    pub fn snapshot_now(&self) -> Result<WindowSnapshot> {
37        self.harness.borrow().snapshot(self.window_id)
38    }
39
40    pub fn run_until_idle(&self) -> Result<()> {
41        self.harness.borrow_mut().run_until_idle()
42    }
43
44    pub fn pump_frames(&self, frames: usize) -> Result<()> {
45        self.harness.borrow_mut().pump_frames(frames)
46    }
47
48    pub fn advance_time(&self, delta: f64) -> Result<()> {
49        self.harness.borrow_mut().advance_time(delta)
50    }
51
52    pub fn dispatch_event_now(&self, event: Event) -> Result<()> {
53        self.harness
54            .borrow_mut()
55            .dispatch_event_now(self.window_id, event)
56    }
57
58    pub fn capture_screenshot(&self) -> Result<Screenshot> {
59        let mut harness = self.harness.borrow_mut();
60        harness.run_until_idle()?;
61        harness.capture_screenshot(self.window_id)
62    }
63
64    pub fn capture_screenshot_now(&self) -> Result<Screenshot> {
65        self.harness.borrow().capture_screenshot(self.window_id)
66    }
67
68    pub fn capture_artifacts(&self) -> Result<ArtifactBundle> {
69        let mut harness = self.harness.borrow_mut();
70        harness.run_until_idle()?;
71        harness.capture_artifacts(self.window_id)
72    }
73
74    pub fn capture_debug_frame(
75        &self,
76        request: DebugCaptureRequest,
77    ) -> Result<DebugCaptureArtifact> {
78        let mut harness = self.harness.borrow_mut();
79        harness.run_until_idle()?;
80        harness.capture_debug_frame(self.window_id, request)
81    }
82
83    pub fn performance_snapshot(&self) -> Result<WindowPerformanceSnapshot> {
84        let mut harness = self.harness.borrow_mut();
85        harness.run_until_idle()?;
86        harness.performance_snapshot(self.window_id)
87    }
88
89    pub fn locator(&self, selector: Selector) -> Locator {
90        Locator::new(Rc::clone(&self.harness), self.window_id, selector)
91    }
92
93    pub fn root(&self) -> Locator {
94        self.locator(Selector::root())
95    }
96
97    pub fn focused(&self) -> Locator {
98        self.locator(Selector::focused())
99    }
100
101    pub fn get_by_role(&self, role: SemanticsRole) -> Locator {
102        self.locator(Selector::by_role(role))
103    }
104
105    pub fn get_by_text(&self, text: impl Into<String>) -> Locator {
106        self.locator(Selector::by_text(text))
107    }
108
109    pub fn get_by_description(&self, text: impl Into<String>) -> Locator {
110        self.locator(Selector::by_description(text))
111    }
112}