use gpui_kit::test::TestWindowExt as _;
use gpui_kit::{AppContext as _, TestAppContext, WindowHandle};
use subspace_core::state::State;
use subspace_dashboard::prefs;
use subspace_dashboard::workspace::navigator_title;
use super::Settings;
use super::preview_tests::open_settings;
use crate::app::navigation::{COLLAPSED_KEY, NAVIGATION_KEY};
use crate::toolbar_labels::{self, ToolbarControl};
#[gpui_kit::test]
fn saved_toolbar_labels_survive_reloading_preferences(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, window, cx| {
settings.set_toolbar_label(3, true, cx);
assert!(!ToolbarControl::Commands.shows_label(cx));
assert!(!settings.save(window, cx).unwrap(), "no restart needed");
})
.unwrap();
cx.update(|cx| {
prefs::init_file(Some(root.path().join("state.json")), cx);
assert!(ToolbarControl::Commands.shows_label(cx));
assert!(!ToolbarControl::Search.shows_label(cx));
});
let reopened = open_settings(root.path(), cx);
reopened
.update(cx, |settings, _, _| {
assert!(settings.toolbar.labels[3]);
assert!(!settings.toolbar.labels[2]);
})
.unwrap();
}
#[gpui_kit::test]
fn cancelled_toolbar_drafts_do_not_change_preferences(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, window, cx| {
settings.set_toolbar_label(0, true, cx);
settings.restore_preview(window, cx).unwrap();
assert!(!ToolbarControl::Chat.shows_label(cx));
})
.unwrap();
let state = State::load(&root.path().join("state.json"));
assert_eq!(state.get(ToolbarControl::Chat.key()), None);
}
#[gpui_kit::test]
fn saving_an_untouched_draft_keeps_context_menu_changes(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, window, cx| {
ToolbarControl::Help.set_label(true, cx);
settings.set_toolbar_label(3, true, cx);
settings.save(window, cx).unwrap();
assert!(ToolbarControl::Commands.shows_label(cx));
assert!(ToolbarControl::Help.shows_label(cx));
})
.unwrap();
let state = State::load(&root.path().join("state.json"));
assert_eq!(state.get(ToolbarControl::Help.key()), Some("1"));
}
fn label_switches_disabled(settings: WindowHandle<Settings>, cx: &mut TestAppContext) -> Vec<bool> {
cx.update_window(settings.into(), |_, window, cx| {
window.render_frame(cx);
(0..ToolbarControl::ALL.len())
.filter(|index| ToolbarControl::ALL[*index].offered(false, cx))
.map(|index| window.find(("toolbar-label", index)).disabled() == Some(true))
.collect()
})
.unwrap()
}
#[gpui_kit::test]
fn collapsing_the_toolbar_disables_label_switches_and_saves_on_save(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = super::organization_tests::open_settings(root.path(), cx);
cx.update_window(settings.into(), |_, window, cx| {
window.click("settings-app:layout", cx)
})
.unwrap();
cx.run_until_parked();
assert!(label_switches_disabled(settings, cx).iter().all(|off| !off));
cx.update_window(settings.into(), |_, window, cx| {
assert_eq!(window.find("collapse-toolbar").checked(), Some(false));
window.click("collapse-toolbar", cx);
})
.unwrap();
cx.run_until_parked();
assert!(label_switches_disabled(settings, cx).iter().all(|off| *off));
settings
.update(cx, |settings, window, cx| {
assert!(!toolbar_labels::collapsed(cx), "Save applies the draft");
assert!(!settings.save(window, cx).unwrap(), "no restart needed");
})
.unwrap();
cx.update(|cx| {
prefs::init_file(Some(root.path().join("state.json")), cx);
assert!(toolbar_labels::collapsed(cx));
});
let reopened = super::organization_tests::open_settings(root.path(), cx);
reopened
.update(cx, |settings, _, _| assert!(settings.toolbar.menu))
.unwrap();
}
#[gpui_kit::test]
fn cancelled_toolbar_menu_draft_does_not_change_preferences(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, window, cx| {
settings.set_toolbar_menu(true, cx);
settings.restore_preview(window, cx).unwrap();
assert!(!toolbar_labels::collapsed(cx));
})
.unwrap();
let state = State::load(&root.path().join("state.json"));
assert_eq!(state.get("app.toolbar-menu"), None);
}
#[gpui_kit::test]
fn layout_navigation_save_and_reopen_preserves_the_draft(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, window, cx| {
settings.board_navigation.mode.update(cx, |select, cx| {
select.set_selected_value(&"Vertical tabs".to_owned(), window, cx);
});
settings.board_navigation.collapsed = true;
settings.save(window, cx).unwrap();
})
.unwrap();
cx.update(|cx| {
assert_eq!(prefs::get(cx, NAVIGATION_KEY).as_deref(), Some("vertical"));
assert_eq!(prefs::get(cx, COLLAPSED_KEY).as_deref(), Some("1"));
});
let reopened = open_settings(root.path(), cx);
reopened
.update(cx, |settings, _, cx| {
assert_eq!(
settings.board_navigation.mode.read(cx).selected_value(),
Some(&"Vertical tabs".to_owned())
);
assert!(settings.board_navigation.collapsed);
})
.unwrap();
}
#[gpui_kit::test]
fn the_navigator_title_switch_is_a_draft_until_save(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, _, cx| {
assert!(settings.board_navigation.navigator_title);
settings.board_navigation.navigator_title = false;
assert!(navigator_title::shown(cx), "a draft changes nothing");
})
.unwrap();
settings
.update(cx, |settings, window, cx| {
settings.save(window, cx).unwrap()
})
.unwrap();
cx.update(|cx| {
assert!(!navigator_title::shown(cx));
assert_eq!(
prefs::get(cx, "layout.navigator-title").as_deref(),
Some("0")
);
});
let reopened = open_settings(root.path(), cx);
reopened
.update(cx, |settings, _, _| {
assert!(!settings.board_navigation.navigator_title)
})
.unwrap();
}
#[gpui_kit::test]
fn a_failed_save_keeps_the_live_navigator_title_and_can_be_retried(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
let state_file = root.path().join("state.json");
settings
.update(cx, |settings, window, cx| {
settings.save(window, cx).unwrap();
})
.unwrap();
std::fs::remove_file(&state_file).unwrap();
std::fs::create_dir(&state_file).unwrap();
settings
.update(cx, |settings, window, cx| {
settings.board_navigation.navigator_title = false;
assert!(settings.save(window, cx).is_err());
assert!(navigator_title::shown(cx), "the live title is unchanged");
assert_eq!(prefs::get(cx, "layout.navigator-title"), None);
assert!(
!settings.board_navigation.navigator_title,
"the draft is kept"
);
let stage = settings.board_navigation.stage_title();
assert!(super::background_updates::save(&mut [], stage, cx).is_err());
assert!(
navigator_title::shown(cx),
"a failed write restores the title"
);
})
.unwrap();
std::fs::remove_dir(&state_file).unwrap();
settings
.update(cx, |settings, window, cx| {
settings.save(window, cx).unwrap();
assert!(
!navigator_title::shown(cx),
"the retried Save applies the draft"
);
})
.unwrap();
let saved = State::load(&state_file);
assert_eq!(saved.get("layout.navigator-title"), Some("0"));
}
#[gpui_kit::test]
fn cancelled_navigation_draft_does_not_change_preferences(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
settings
.update(cx, |settings, window, cx| {
settings.board_navigation.mode.update(cx, |select, cx| {
select.set_selected_value(&"Vertical tabs".to_owned(), window, cx);
});
settings.board_navigation.collapsed = true;
settings.restore_preview(window, cx).unwrap();
})
.unwrap();
cx.update(|cx| {
assert_eq!(prefs::get(cx, NAVIGATION_KEY), None);
assert_eq!(prefs::get(cx, COLLAPSED_KEY), None);
});
}
#[gpui_kit::test]
fn untouched_navigation_draft_preserves_palette_changes(cx: &mut TestAppContext) {
let root = tempfile::tempdir().unwrap();
let settings = open_settings(root.path(), cx);
cx.update(|cx| {
prefs::set(cx, NAVIGATION_KEY, "vertical");
prefs::set(cx, COLLAPSED_KEY, "1");
});
settings
.update(cx, |settings, window, cx| {
settings.save(window, cx).unwrap();
})
.unwrap();
let state = State::load(&root.path().join("state.json"));
assert_eq!(state.get(NAVIGATION_KEY), Some("vertical"));
assert_eq!(state.get(COLLAPSED_KEY), Some("1"));
}