Skip to main content

fenestra_shell/
synthetic.rs

1//! Synthetic event injection for headless testing: agents drive an [`App`]
2//! with scripted input and look at the resulting pixels.
3
4use fenestra_core::{App, InputEvent, KeyInput, Theme};
5use image::RgbaImage;
6
7/// A scripted input event for [`render_app`].
8#[derive(Debug, Clone, PartialEq)]
9pub enum SyntheticEvent {
10    /// Move the pointer to logical coordinates.
11    MouseMove {
12        /// Logical x.
13        x: f32,
14        /// Logical y.
15        y: f32,
16    },
17    /// Press the primary button.
18    MouseDown,
19    /// Release the primary button.
20    MouseUp,
21    /// Press the secondary (right) button.
22    RightDown,
23    /// Release the secondary (right) button.
24    RightUp,
25    /// Drop an OS file at the current pointer position.
26    FileDrop(std::path::PathBuf),
27    /// Press a key.
28    Key(KeyInput),
29    /// Commit text (M5).
30    Text(String),
31    /// Scroll (winit convention: positive `dy` moves content down).
32    Wheel {
33        /// Vertical delta in logical px.
34        dy: f32,
35    },
36    /// Focus next.
37    Tab,
38    /// Focus previous.
39    ShiftTab,
40}
41
42impl From<&SyntheticEvent> for InputEvent {
43    fn from(ev: &SyntheticEvent) -> Self {
44        match ev {
45            SyntheticEvent::MouseMove { x, y } => Self::PointerMove { x: *x, y: *y },
46            SyntheticEvent::MouseDown => Self::PointerDown,
47            SyntheticEvent::MouseUp => Self::PointerUp,
48            SyntheticEvent::RightDown => Self::RightDown,
49            SyntheticEvent::RightUp => Self::RightUp,
50            SyntheticEvent::FileDrop(p) => Self::FileDrop(p.clone()),
51            SyntheticEvent::Key(k) => Self::Key(*k),
52            SyntheticEvent::Text(s) => Self::Text(s.clone()),
53            SyntheticEvent::Wheel { dy } => Self::Wheel { dy: *dy },
54            SyntheticEvent::Tab => Self::Tab,
55            SyntheticEvent::ShiftTab => Self::ShiftTab,
56        }
57    }
58}
59
60/// Drives an app headlessly: dispatches each event against the current
61/// view, applies the emitted messages, then renders one settle frame.
62/// Deterministic: scale 1.0, reduced motion, embedded fonts only. The
63/// requested size is clamped to the device-supported range (at least 1x1,
64/// at most the maximum texture dimension).
65///
66/// [`App::init`] runs first with a collecting [`Proxy`]; proxied messages
67/// are applied at deterministic points (before each event and before the
68/// settle frame). Messages sent from spawned threads race those drain
69/// points — keep proxy use synchronous in tests.
70///
71/// # Panics
72/// If no compute-capable GPU adapter exists or rendering fails.
73pub fn render_app<A: App>(
74    app: &mut A,
75    events: &[SyntheticEvent],
76    size: (u32, u32),
77    theme: &Theme,
78) -> RgbaImage
79where
80    A::Msg: Send,
81{
82    let mut harness = crate::Harness::new(&mut *app, theme.clone(), size);
83    for ev in events {
84        harness.input(ev.into());
85    }
86    harness.render()
87}