Files
gpui-native-kit/crates/core/tests/radio.rs
T
2026-10-28 19:17:35 +07:00

122 lines
3.9 KiB
Rust

use std::{cell::RefCell, rc::Rc};
use gpui::{
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
Modifiers, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window,
div, point, px,
};
use gpui_native_core::radio::{Radio, RadioGroup};
use gpui_native_theme::Theme;
struct Harness {
changes: Rc<RefCell<Vec<&'static str>>>,
selected: Option<&'static str>,
}
impl Render for Harness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let changes = self.changes.clone();
let view = cx.entity();
div().size_full().flex().flex_col().child(
RadioGroup::new("test-group")
.selected(self.selected)
.on_change(move |value, _, cx| {
changes.borrow_mut().push(value);
view.update(cx, |harness, cx| {
harness.selected = Some(value);
cx.notify();
});
})
.child(Radio::new("radio-a", "a").label("Option A"))
.child(Radio::new("radio-b", "b").label("Option B"))
.child(
Radio::new("radio-off", "c")
.label("Disabled")
.disabled(true),
),
)
}
}
/// A maximized window whose radio rows sit at the top-left: row 1 spans
/// roughly y 0..21, row 2 y 29..50, so `(8, 10)` and `(8, 40)` hit them.
fn group_window(
selected: Option<&'static str>,
) -> (
VisualTestContext,
AnyWindowHandle,
Rc<RefCell<Vec<&'static str>>>,
) {
let mut cx = TestAppContext::single();
cx.update(Theme::init);
let changes = Rc::new(RefCell::new(Vec::new()));
let harness_changes = changes.clone();
let window = cx.add_window(move |_, _| Harness {
changes: harness_changes,
selected,
});
let window = window.into();
cx.update_window(window, |_, window, cx| {
window.draw(cx).clear(cx);
})
.unwrap();
(VisualTestContext::from_window(window, &cx), window, changes)
}
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
cx.update_window(window, |_, window, cx| {
window.draw(cx).clear(cx);
})
.unwrap();
}
#[test]
fn clicking_reports_values_through_the_controlled_loop() {
let (mut cx, _window, changes) = group_window(None);
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
assert_eq!(*changes.borrow(), vec!["a"]);
cx.simulate_click(point(px(8.), px(40.)), Modifiers::default());
assert_eq!(*changes.borrow(), vec!["a", "b"]);
}
#[test]
fn clicking_the_selected_radio_never_unchecks_it() {
let (mut cx, _window, changes) = group_window(Some("a"));
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
assert_eq!(*changes.borrow(), vec!["a"], "re-reports the same value");
}
#[test]
fn disabled_radio_does_not_report_changes() {
let (mut cx, _window, changes) = group_window(None);
cx.simulate_click(point(px(8.), px(70.)), Modifiers::default());
assert!(changes.borrow().is_empty());
}
#[test]
fn enter_and_space_report_the_focused_radio_value() {
let (mut cx, window, changes) = group_window(None);
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
assert_eq!(
*changes.borrow(),
vec!["a"],
"the pointer click itself selects"
);
// Repaint with the first radio focused so its Enter/Space handlers register.
draw(&mut cx, window);
for key in ["enter", "space"] {
cx.simulate_event(KeyDownEvent {
keystroke: Keystroke::parse(key).unwrap(),
is_held: false,
prefer_character_input: false,
});
cx.simulate_event(KeyUpEvent {
keystroke: Keystroke::parse(key).unwrap(),
});
}
assert_eq!(*changes.borrow(), vec!["a", "a", "a"]);
}