71 lines
2.2 KiB
Rust
71 lines
2.2 KiB
Rust
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
use gpui::{
|
|
AnyWindowHandle, AppContext as _, InteractiveElement as _, IntoElement, Modifiers,
|
|
ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window, div, point,
|
|
px,
|
|
};
|
|
use gpui_native_core::alert::{Alert, AlertSeverity};
|
|
use gpui_native_theme::Theme;
|
|
|
|
struct Harness {
|
|
severity: AlertSeverity,
|
|
dismissed: Rc<RefCell<bool>>,
|
|
}
|
|
|
|
impl Render for Harness {
|
|
fn render(&mut self, _window: &mut Window, _cx: &mut gpui::Context<Self>) -> impl IntoElement {
|
|
let dismissed = self.dismissed.clone();
|
|
div().size_full().p_4().child(
|
|
div().debug_selector(|| "test-alert".to_string()).child(
|
|
Alert::new("test-alert")
|
|
.title("Heads up")
|
|
.severity(self.severity)
|
|
.message("Secondary line.")
|
|
.on_dismiss(move |_| *dismissed.borrow_mut() = true),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
/// The alert stretches across the top of the content area; its close button
|
|
/// sits in the banner's top-right corner.
|
|
fn alert_window(
|
|
severity: AlertSeverity,
|
|
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<bool>>) {
|
|
let mut cx = TestAppContext::single();
|
|
cx.update(Theme::init);
|
|
let dismissed = Rc::new(RefCell::new(false));
|
|
let harness_dismissed = dismissed.clone();
|
|
let window = cx.add_window(move |_, _| Harness {
|
|
severity,
|
|
dismissed: harness_dismissed,
|
|
});
|
|
let window = window.into();
|
|
cx.update_window(window, |_, window, cx| {
|
|
window.draw(cx).clear(cx);
|
|
})
|
|
.unwrap();
|
|
(
|
|
VisualTestContext::from_window(window, &cx),
|
|
window,
|
|
dismissed,
|
|
)
|
|
}
|
|
|
|
/// Clicking the close button is how the user acts on the alert; the banner
|
|
/// reports the intent instead of hiding itself.
|
|
#[test]
|
|
fn closing_the_alert_reports_dismissal() {
|
|
let (mut cx, _window, dismissed) = alert_window(AlertSeverity::Danger);
|
|
|
|
let bounds = cx.debug_bounds("test-alert").expect("alert rendered");
|
|
cx.simulate_click(
|
|
point(bounds.right() - px(14.), bounds.top() + px(14.)),
|
|
Modifiers::default(),
|
|
);
|
|
|
|
assert!(*dismissed.borrow(), "the close button reported dismissal");
|
|
}
|