audit select
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use gpui::{
|
||||
Context, IntoElement, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext,
|
||||
VisualTestContext, Window, div, point, px,
|
||||
Context, Entity, IntoElement, MouseButton, ParentElement as _, Render, Styled as _,
|
||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
||||
};
|
||||
use gpui_native_core::input::{Input, InputState};
|
||||
use gpui_native_core::select::Select;
|
||||
use gpui_native_theme::Theme;
|
||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Theme};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Strategy {
|
||||
@@ -200,3 +201,80 @@ fn two_selects_keep_their_popups_independent() {
|
||||
assert_eq!(*first_changes.borrow(), vec![]);
|
||||
assert_eq!(*second_changes.borrow(), vec![Some(Strategy::Curated)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_outside_closes_the_popup() {
|
||||
let (_cx, mut visual, changes) = select_window();
|
||||
|
||||
click(&mut visual, point(px(100.), px(31.)));
|
||||
draw(&mut visual);
|
||||
click(&mut visual, point(px(100.), px(300.)));
|
||||
draw(&mut visual);
|
||||
|
||||
// The popup held keyboard focus while open; a keystroke after the outside
|
||||
// click answers nothing if the popup really closed.
|
||||
visual.simulate_keystrokes("enter");
|
||||
draw(&mut visual);
|
||||
assert_eq!(*changes.borrow(), Vec::<Option<Strategy>>::new());
|
||||
}
|
||||
|
||||
/// Clicking another control while the popup is open dismisses it and still
|
||||
/// acts on the control: dismissing does not claim the event.
|
||||
#[test]
|
||||
fn a_click_on_another_control_dismisses_and_reaches_it() {
|
||||
let (_cx, mut visual, first_changes, second_changes) = pair_window();
|
||||
|
||||
click(&mut visual, point(px(100.), px(31.)));
|
||||
draw(&mut visual);
|
||||
|
||||
// The second select's trigger, below the first — at x=300, past the first
|
||||
// popup's 182px-wide panel: this click must close the first popup AND open
|
||||
// the second.
|
||||
click(&mut visual, point(px(300.), px(69.)));
|
||||
draw(&mut visual);
|
||||
visual.simulate_keystrokes("enter");
|
||||
draw(&mut visual);
|
||||
|
||||
assert_eq!(*first_changes.borrow(), Vec::<Option<Strategy>>::new());
|
||||
assert_eq!(*second_changes.borrow(), vec![Some(Strategy::Curated)]);
|
||||
}
|
||||
|
||||
struct HeightsHarness {
|
||||
state: Entity<InputState>,
|
||||
}
|
||||
|
||||
impl Render for HeightsHarness {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.gap_4()
|
||||
.child(Select::new("heights").options(STRATEGIES))
|
||||
.child(Input::new("heights-field", self.state.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
/// The trigger renders at the same height as the input field: the stated
|
||||
/// metric plus the always-painted hairline border, whatever the platform.
|
||||
#[test]
|
||||
fn the_trigger_matches_the_input_height() {
|
||||
let mut cx = TestAppContext::single();
|
||||
cx.update(Theme::init);
|
||||
let state = cx.update(InputState::new);
|
||||
let state_for_window = state.clone();
|
||||
let window = cx.add_window(move |_, _| HeightsHarness {
|
||||
state: state_for_window,
|
||||
});
|
||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
||||
draw(&mut visual);
|
||||
|
||||
let trigger = visual
|
||||
.debug_bounds("select-trigger")
|
||||
.expect("trigger rendered");
|
||||
let field = visual.debug_bounds("input-field").expect("input rendered");
|
||||
|
||||
// Snapped bounds: allow half a pixel.
|
||||
assert!((trigger.size.height - field.size.height).abs() <= px(0.5));
|
||||
let expected = cx.update(|cx| cx.theme().sizes.medium.input_height) + BORDER_WIDTH * 2.0;
|
||||
assert!((trigger.size.height - expected).abs() <= px(0.5));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user