Files
gpui-native-kit/crates/core/tests/field_bench.rs
T
2026-11-09 10:09:56 +00:00

109 lines
3.4 KiB
Rust

use std::{cell::Cell, rc::Rc, time::Duration};
use gpui::{
AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, SharedString,
Styled as _, TestAppContext, VisualTestContext, Window, div,
};
use gpui_native_core::common::v_flex;
use gpui_native_core::field::Field;
use gpui_native_core::init;
use gpui_native_core::input::{Input, InputState};
struct FieldHost {
url: Entity<InputState>,
renders: Rc<Cell<usize>>,
}
impl Render for FieldHost {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.renders.set(self.renders.get() + 1);
let url_error = self
.url
.read(cx)
.value()
.is_empty()
.then_some("Required")
.map(SharedString::from);
div().size_full().p_4().child(
v_flex().gap_3().child(
Field::new("bench-url", "Server URL")
.description("Where your patches are sent.")
.error(url_error)
.child(Input::new("bench-url-input", self.url.clone())),
),
)
}
}
/// A field page in a real window; the url input is focused unless
/// `focus` is false, which leaves the page truly idle.
fn field_host(
focus: bool,
) -> (
TestAppContext,
Entity<InputState>,
VisualTestContext,
Rc<Cell<usize>>,
) {
let cx = TestAppContext::single();
cx.update(init);
let renders = Rc::new(Cell::new(0));
let renders_for_host = renders.clone();
let (url, window) = cx.update(|cx| {
let url = InputState::new(cx);
let url_for_host = url.clone();
let window = cx
.open_window(gpui::WindowOptions::default(), |_, cx| {
cx.new(|_| FieldHost {
url: url_for_host,
renders: renders_for_host.clone(),
})
})
.unwrap();
(url, window)
});
let mut visual = VisualTestContext::from_window(window.into(), &cx);
visual.update(|window, cx| {
if focus {
let focus = url.read(cx).focus_handle();
window.focus(&focus, cx);
}
window.draw(cx).clear(cx);
});
(cx, url, visual, renders)
}
#[test]
fn an_idle_field_page_schedules_no_frames() {
let (_cx, _url, mut visual, _renders) = field_host(false);
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
assert_eq!(
frames, 0,
"an idle unfocused field page must not request animation frames"
);
}
/// The caret blink is fps-capped: it must never demand a next frame, and its
/// redraws arrive on the executor timer at the blink cadence.
#[test]
fn a_focused_field_blink_is_fps_capped_and_still_redraws() {
let (cx, _url, mut visual, renders) = field_host(true);
let renders_after_focus = renders.get();
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
assert_eq!(
frames, 0,
"the blink must be fps-capped, not per-frame scheduled"
);
// The blink redraws through the executor timer (4 fps cap).
cx.executor().advance_clock(Duration::from_millis(251));
cx.run_until_parked();
visual.update(|window, cx| window.draw(cx).clear(cx));
assert!(
renders.get() > renders_after_focus,
"the capped blink must still redraw its page"
);
}