80 lines
2.5 KiB
Rust
80 lines
2.5 KiB
Rust
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>,
|
|
}
|
|
|
|
impl Render for FieldHost {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
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) {
|
|
let cx = TestAppContext::single();
|
|
cx.update(init);
|
|
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 })
|
|
})
|
|
.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)
|
|
}
|
|
|
|
#[test]
|
|
fn an_idle_field_page_schedules_no_frames() {
|
|
let (_cx, _url, mut visual) = 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"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_focused_field_page_schedules_frames_for_its_blink() {
|
|
let (_cx, _url, mut visual) = field_host(true);
|
|
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
|
|
assert!(
|
|
frames > 0,
|
|
"the focused caret's blink needs frames to redraw at the blink cadence"
|
|
);
|
|
}
|