.
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
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::checkbox::Checkbox;
|
||||
use gpui_native_theme::Theme;
|
||||
|
||||
struct Harness {
|
||||
changes: Rc<RefCell<Vec<bool>>>,
|
||||
checked: bool,
|
||||
disabled: bool,
|
||||
}
|
||||
|
||||
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(
|
||||
Checkbox::new("test-checkbox")
|
||||
.label("Telemetry")
|
||||
.checked(self.checked)
|
||||
.disabled(self.disabled)
|
||||
.on_change(move |next, _, cx| {
|
||||
changes.borrow_mut().push(next);
|
||||
view.update(cx, |harness, cx| {
|
||||
harness.checked = next;
|
||||
cx.notify();
|
||||
});
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A maximized window whose checkbox sits in the top-left corner, so
|
||||
/// `point(px(8.), px(10.))` lands on it.
|
||||
fn checkbox_window(
|
||||
checked: bool,
|
||||
disabled: bool,
|
||||
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<Vec<bool>>>) {
|
||||
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,
|
||||
checked,
|
||||
disabled,
|
||||
});
|
||||
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_the_next_value_through_the_controlled_loop() {
|
||||
let (mut cx, _window, changes) = checkbox_window(false, false);
|
||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
||||
assert_eq!(*changes.borrow(), vec![true]);
|
||||
|
||||
// The second click must report `false`: only if the owner applied the
|
||||
// first change does the checkbox know it is now checked.
|
||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabled_checkbox_does_not_report_changes() {
|
||||
let (mut cx, _window, changes) = checkbox_window(false, true);
|
||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
||||
assert!(changes.borrow().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enter_and_space_report_the_next_value_while_focused() {
|
||||
let (mut cx, window, changes) = checkbox_window(false, false);
|
||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
||||
assert_eq!(
|
||||
*changes.borrow(),
|
||||
vec![true],
|
||||
"the pointer click itself toggles"
|
||||
);
|
||||
|
||||
// Repaint with the checkbox 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![true, false, true]);
|
||||
}
|
||||
Reference in New Issue
Block a user