use std::cell::RefCell; use std::rc::Rc; use std::time::Duration; use gpui::{AppContext as _, TestAppContext}; use gpui_native_core::toast::{Toast, ToastSeverity, Toasts}; use gpui_native_theme::Theme; fn toasts_store() -> (TestAppContext, gpui::Entity) { let cx = TestAppContext::single(); cx.update(Theme::init); let toasts = cx.update(|cx| cx.new(|_| Toasts::default())); (cx, toasts) } #[test] fn push_raises_a_toast_and_the_timer_dismisses_it() { let (cx, toasts) = toasts_store(); let id = cx.update(|cx| { toasts.update(cx, |toasts, cx| { toasts.push(Toast::new("Saved").severity(ToastSeverity::Success), cx) }) }); assert_eq!(cx.update(|cx| toasts.read(cx).count()), 1); // The default lifetime is five seconds. cx.executor().advance_clock(Toast::DURATION); cx.run_until_parked(); assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0); let _ = id; } #[test] fn sticky_toasts_ignore_the_timer_until_dismissed() { let (cx, toasts) = toasts_store(); let id = cx.update(|cx| { toasts.update(cx, |toasts, cx| { toasts.push(Toast::new("Sticky").duration(Duration::ZERO), cx) }) }); cx.executor().advance_clock(Toast::DURATION * 4); cx.run_until_parked(); assert_eq!(cx.update(|cx| toasts.read(cx).count()), 1); cx.update(|cx| { toasts.update(cx, |toasts, cx| toasts.dismiss(id, cx)); }); assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0); } #[test] fn dismiss_runs_the_handler_and_clear_removes_everything() { let (cx, toasts) = toasts_store(); let dismissed = Rc::new(std::cell::RefCell::new(Vec::<&'static str>::new())); let ids = cx.update(|cx| { toasts.update(cx, |toasts, cx| { let first = dismissed.clone(); let second = dismissed.clone(); vec![ toasts.push( Toast::new("first").on_dismiss(move |_| record(&first, "first")), cx, ), toasts.push( Toast::new("second").on_dismiss(move |_| record(&second, "second")), cx, ), ] }) }); cx.update(|cx| { toasts.update(cx, |toasts, cx| toasts.dismiss(ids[0], cx)); }); assert_eq!(*dismissed.borrow(), vec!["first"]); cx.update(|cx| { toasts.update(cx, |toasts, cx| toasts.clear(cx)); }); assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0); fn record(sink: &Rc>>, what: &'static str) { sink.borrow_mut().push(what); } }