use gpui_kit::test::{TestEventExt as _, TestQueryExt as _, TestWindowExt as _};
use gpui_kit::{
AppContext as _, Bounds, Context, ElementId, Entity, IntoElement, MouseButton,
ParentElement as _, Point, Render, Role, Styled as _, TestAppContext, Window, WindowBounds,
WindowOptions, div, point, px, size,
};
use std::time::Duration;
use u0ui::components::{Button, Input, InputState, Tooltip};
use u0ui::icons::CarbonIcon;
struct Page {
state: Entity<InputState>,
}
impl Render for Page {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().p(px(40.)).w(px(400.)).child(
Input::new("key", &self.state)
.password(true)
.password_labels("Show password", "Hide password"),
)
}
}
fn trigger() -> ElementId {
let tip = ElementId::from((ElementId::from("key"), "visibility-tip"));
ElementId::from((tip, "trigger"))
}
fn tooltips(window: &Window) -> Vec<String> {
window
.find_all_by_role(Role::Tooltip)
.into_iter()
.filter(|tip| tip.visible())
.filter_map(|tip| tip.label().map(str::to_string))
.collect()
}
#[gpui_kit::test]
fn press_inside_the_enter_delay_cancels_the_show(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
u0ui::init(cx);
});
let (window, page) = cx.update(|cx| {
gpui_kit::open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(Bounds {
origin: Point::default(),
size: size(px(800.), px(600.)),
})),
..Default::default()
},
cx,
|window, cx| {
let state = cx.new(|cx| InputState::new(window, cx));
cx.new(|_| Page { state })
},
)
.expect("open test window")
});
let wait = |ms: u64, cx: &mut TestAppContext| {
cx.executor().advance_clock(Duration::from_millis(ms));
cx.run_until_parked();
cx.update_window(window, |_, window, cx| window.render_frame(cx))
.unwrap();
};
let masked = |cx: &mut TestAppContext| {
cx.update(|cx| page.read(cx).state.read(cx).presentation().is_masked())
};
wait(10, cx);
assert!(masked(cx), "a password field starts masked");
cx.update_window(window, |_, window, cx| window.hover(trigger(), cx))
.unwrap();
wait(50, cx);
cx.update_window(window, |_, window, cx| window.click(trigger(), cx))
.unwrap();
assert!(!masked(cx), "the press reveals the text");
wait(1000, cx);
let shown = cx
.update_window(window, |_, window, _| tooltips(window))
.unwrap();
assert_eq!(
shown,
Vec::<String>::new(),
"the tooltip showed after the press"
);
cx.update_window(window, |_, window, cx| {
window.pointer_move(point(px(780.), px(580.)), None, cx)
})
.unwrap();
wait(500, cx);
cx.update_window(window, |_, window, cx| window.hover(trigger(), cx))
.unwrap();
wait(200, cx);
let shown = cx
.update_window(window, |_, window, _| tooltips(window))
.unwrap();
assert_eq!(shown, vec!["Hide password".to_string()]);
cx.update_window(window, |_, window, cx| window.click(trigger(), cx))
.unwrap();
wait(500, cx);
assert!(masked(cx));
let shown = cx
.update_window(window, |_, window, _| tooltips(window))
.unwrap();
assert_eq!(shown, Vec::<String>::new());
cx.update_window(window, |_, window, cx| {
window.pointer_move(point(px(780.), px(580.)), None, cx)
})
.unwrap();
wait(500, cx);
cx.update_window(window, |_, window, cx| window.hover(trigger(), cx))
.unwrap();
wait(200, cx);
let shown = cx
.update_window(window, |_, window, _| tooltips(window))
.unwrap();
assert_eq!(shown, vec!["Show password".to_string()]);
cx.update_window(window, |_, window, cx| {
window.pointer_move(point(px(780.), px(580.)), None, cx)
})
.unwrap();
wait(500, cx);
let center = cx
.update_window(window, |_, window, _| {
window.find(trigger()).bounds().center()
})
.unwrap();
cx.update_window(window, |_, window, cx| {
window.pointer_down(center, MouseButton::Left, cx);
window.pointer_up(center, MouseButton::Left, cx);
window.pointer_move(center, None, cx);
})
.unwrap();
assert!(!masked(cx), "the press reveals the text");
wait(1000, cx);
let shown = cx
.update_window(window, |_, window, _| tooltips(window))
.unwrap();
assert_eq!(shown, Vec::<String>::new(), "a late hover report showed it");
}
struct Pair;
impl Render for Pair {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.p(px(80.))
.flex()
.gap(px(200.))
.child(
Tooltip::new(
"plain",
Button::new("plain-b", "Copy")
.icon(CarbonIcon::Copy)
.icon_only(true)
.tooltip(false),
)
.content("Copy to clipboard"),
)
.child(
Button::new("icon", "Delete")
.icon(CarbonIcon::TrashCan)
.icon_only(true),
)
}
}
fn plain_trigger() -> ElementId {
ElementId::from((ElementId::from("plain"), "trigger"))
}
fn icon_trigger() -> ElementId {
let tip = ElementId::from((ElementId::from("icon"), "tooltip"));
ElementId::from((tip, "trigger"))
}
#[gpui_kit::test]
fn only_icon_button_tooltips_close_on_activation(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
u0ui::init(cx);
});
let (window, _pair) = cx.update(|cx| {
gpui_kit::open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(Bounds {
origin: Point::default(),
size: size(px(800.), px(600.)),
})),
..Default::default()
},
cx,
|_, cx| cx.new(|_| Pair),
)
.expect("open test window")
});
let wait = |ms: u64, cx: &mut TestAppContext| {
cx.executor().advance_clock(Duration::from_millis(ms));
cx.run_until_parked();
cx.update_window(window, |_, window, cx| window.render_frame(cx))
.unwrap();
};
let shown = |cx: &mut TestAppContext| {
cx.update_window(window, |_, window, _| tooltips(window))
.unwrap()
};
let away = |cx: &mut TestAppContext| {
cx.update_window(window, |_, window, cx| {
window.pointer_move(point(px(780.), px(580.)), None, cx)
})
.unwrap();
};
wait(10, cx);
cx.update_window(window, |_, window, cx| window.hover(plain_trigger(), cx))
.unwrap();
wait(200, cx);
assert_eq!(shown(cx), vec!["Copy to clipboard".to_string()]);
cx.update_window(window, |_, window, cx| window.click(plain_trigger(), cx))
.unwrap();
wait(500, cx);
assert_eq!(
shown(cx),
vec!["Copy to clipboard".to_string()],
"a standalone tooltip hid on press"
);
cx.update_window(window, |_, window, cx| window.press("escape", cx))
.unwrap();
wait(500, cx);
assert_eq!(shown(cx), Vec::<String>::new(), "Escape did not hide it");
away(cx);
wait(500, cx);
cx.update_window(window, |_, window, cx| window.hover(icon_trigger(), cx))
.unwrap();
wait(200, cx);
assert_eq!(shown(cx), vec!["Delete".to_string()]);
cx.update_window(window, |_, window, cx| window.click(icon_trigger(), cx))
.unwrap();
wait(500, cx);
assert_eq!(
shown(cx),
Vec::<String>::new(),
"an icon button's tooltip stayed up on press"
);
}