use gpui_kit::test::TestWindowExt as _;
use gpui_kit::{
App, AppContext as _, Context, EventEmitter, FocusHandle, Focusable, InteractiveElement,
IntoElement, Render, TestAppContext, Window, div,
};
use subspace_dashboard::{Dashboard, DashboardEvent};
use super::SubspaceApp;
pub(super) struct Fixture {
pub(super) focus: FocusHandle,
covered: bool,
}
impl EventEmitter<DashboardEvent> for Fixture {}
impl Focusable for Fixture {
fn focus_handle(&self, _: &App) -> FocusHandle {
self.focus.clone()
}
}
impl Render for Fixture {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.id(Self::ID)
.key_context(Self::ID)
.track_focus(&self.focus)
}
}
impl Dashboard for Fixture {
const ID: &'static str = "cover-fixture";
const TITLE: &'static str = "Cover fixture";
fn new(_: &mut Window, cx: &mut Context<Self>) -> Self {
Self {
focus: cx.focus_handle(),
covered: false,
}
}
fn covered(&mut self, covered: bool, _: &mut Window, _: &mut Context<Self>) {
self.covered = covered;
}
fn reload(&mut self, _: &mut Window, _: &mut Context<Self>) {}
}
fn open_fixture(
root: &std::path::Path,
cx: &mut TestAppContext,
) -> (
gpui_kit::WindowHandle<SubspaceApp>,
gpui_kit::Entity<Fixture>,
) {
cx.update(|cx| {
crate::theme::preview_tests::init_app(cx, root, root.join("state.json"));
crate::theme::initialize(cx);
cx.set_global(crate::registry::BoardFactories(vec![
crate::registry::BoardRegistration::new::<Fixture>(),
]));
});
let window = cx.add_window(|window, cx| SubspaceApp::new(Vec::new(), window, cx));
let fixture = window
.update(cx, |app, window, cx| {
app.focus_active(window, cx);
app.dashboard()
.unwrap()
.view()
.downcast::<Fixture>()
.unwrap_or_else(|_| panic!("the active dashboard is the fixture"))
})
.unwrap();
(window, fixture)
}
#[gpui_kit::test]
fn shell_overlays_cover_and_uncover_the_boards(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, fixture) = open_fixture(root.path(), cx);
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_root, window, cx| window.render_frame(cx))
.unwrap();
};
draw(cx);
assert!(!fixture.read_with(cx, |fixture, _| fixture.covered));
window
.update(cx, |app, window, cx| app.set_jig(true, window, cx))
.unwrap();
draw(cx);
assert!(fixture.read_with(cx, |fixture, _| fixture.covered));
window
.update(cx, |app, window, cx| app.set_jig(false, window, cx))
.unwrap();
draw(cx);
assert!(!fixture.read_with(cx, |fixture, _| fixture.covered));
}
#[gpui_kit::test]
fn a_maximized_terminal_covers_the_boards(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
cx.update(crate::app::terminal_fixture::install);
let (window, fixture) = open_fixture(root.path(), cx);
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_root, window, cx| window.render_frame(cx))
.unwrap();
};
window
.update(cx, |app, window, cx| {
app.change_terminal(
super::super::terminal_dock::Change::ToggleMaximized,
window,
cx,
);
})
.unwrap();
draw(cx);
assert!(fixture.read_with(cx, |fixture, _| fixture.covered));
window
.update(cx, |app, window, cx| {
app.change_terminal(super::super::terminal_dock::Change::Restore, window, cx);
})
.unwrap();
draw(cx);
assert!(!fixture.read_with(cx, |fixture, _| fixture.covered));
}
#[gpui_kit::test]
fn sign_in_covers_the_window_only_while_a_required_account_has_no_session(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, fixture) = open_fixture(root.path(), cx);
let gate = |state: &str, cx: &mut TestAppContext| {
cx.update(|cx| crate::account::fixture(state, cx).unwrap());
cx.update_window(window.into(), |_root, window, cx| {
window.refresh();
window.render_frame(cx);
window.try_find("sign-in-gate").is_some()
})
.unwrap()
};
cx.update_window(window.into(), |_root, window, cx| {
window.render_frame(cx);
assert!(window.try_find("sign-in-gate").is_none());
})
.unwrap();
assert!(!gate("signed-out", cx));
assert!(gate("required-signed-out", cx));
assert!(fixture.read_with(cx, |fixture, _| fixture.covered));
cx.update_window(window.into(), |_root, window, cx| {
assert_eq!(
window.find("sign-in-gate-sign-in").label(),
Some("Sign in with Example Identity")
);
assert!(window.try_find("sign-in-gate-quit").is_some());
window.render_frame(cx);
assert_eq!(
window.find("sign-in-gate-sign-in").focused(),
Some(true),
"focus is on sign-in"
);
})
.unwrap();
assert!(gate("required-waiting", cx));
assert!(gate("required-expired", cx));
assert!(!gate("required-signed-in", cx));
assert!(!fixture.read_with(cx, |fixture, _| fixture.covered));
assert!(!gate("required-offline", cx));
assert!(!gate("required-keychain", cx));
}
pub(super) fn settle(window: gpui_kit::WindowHandle<SubspaceApp>, cx: &mut TestAppContext) {
cx.update_window(window.into(), |_root, window, cx| {
window.refresh();
window.render_frame(cx);
})
.unwrap();
cx.run_until_parked();
cx.update_window(window.into(), |_root, window, cx| window.render_frame(cx))
.unwrap();
}
pub(super) fn gated(
state: &str,
root: &std::path::Path,
cx: &mut TestAppContext,
) -> (
gpui_kit::WindowHandle<SubspaceApp>,
gpui_kit::Entity<Fixture>,
) {
cx.update(crate::app::terminal_fixture::install);
let (window, fixture) = open_fixture(root, cx);
cx.update(|cx| crate::account::fixture(state, cx).unwrap());
settle(window, cx);
(window, fixture)
}
#[gpui_kit::test]
fn the_sign_in_gate_refuses_every_action_but_quit(cx: &mut TestAppContext) {
use crate::app::{
NextDashboard, OpenSettings, Quit, ToggleChat, TogglePalette, ToggleTerminal,
};
let root = tempfile::tempdir().unwrap();
let (window, _) = gated("required-signed-out", root.path(), cx);
let refused: [Box<dyn gpui_kit::Action>; 5] = [
Box::new(OpenSettings),
Box::new(TogglePalette),
Box::new(ToggleTerminal),
Box::new(ToggleChat),
Box::new(NextDashboard),
];
for action in refused {
let name = action.name();
window
.update(cx, |_, window, cx| {
assert!(!window.is_action_available(action.as_ref(), cx), "{name}");
window.dispatch_action(action, cx);
})
.unwrap();
settle(window, cx);
window
.update(cx, |app, window, _| {
assert!(app.settings.is_none(), "{name} opened Settings");
assert!(app.session.is_none(), "{name} opened the palette");
assert!(app.shown_terminal().is_none(), "{name} showed the terminal");
assert!(!app.chat_open, "{name} opened chat");
assert!(window.try_find("sign-in-gate").is_some());
})
.unwrap();
}
window
.update(cx, |_, window, cx| {
assert!(window.is_action_available(&Quit, cx))
})
.unwrap();
}
#[gpui_kit::test]
fn the_sign_in_gate_closes_settings_and_gives_focus_back(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let (window, fixture) = gated("required-signed-in", root.path(), cx);
window
.update(cx, |app, window, cx| {
app.settings = Some(crate::settings::Settings::open(window, cx).unwrap());
})
.unwrap();
cx.update(|cx| crate::account::fixture("required-signed-out", cx).unwrap());
settle(window, cx);
window
.update(cx, |app, _, _| {
assert!(app.settings.is_none(), "Settings closed")
})
.unwrap();
cx.update(|cx| crate::account::fixture("required-signed-in", cx).unwrap());
settle(window, cx);
window
.update(cx, |_, window, cx| {
assert!(window.try_find("sign-in-gate").is_none());
assert!(window.is_action_available(&crate::app::OpenSettings, cx));
let space = fixture.read(cx).focus.clone();
assert_eq!(
window.focused(cx),
Some(space),
"focus is back on the Space"
);
})
.unwrap();
}