Lines
99.14 %
Functions
100 %
Branches
//! Opening and closing windows: what a close settles, and the desktop without windows.
use super::report_chart::{chart_requests, png_wire, rendered, show, stripes, text, with_table};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::focus::{FocusTarget, current_focus};
use crate::form::Form;
use crate::route::{Route, RouteCtx};
use crate::tabs::nms::ConsoleState;
use crate::test_support::{alt, ctrl, draw_frame, key, press};
use crate::view::ViewId;
use crate::window::{View, WindowId};
use ratatui::crossterm::event::KeyCode;
use rpc::chart::ChartBackend;
use std::collections::HashSet;
fn window_ids(app: &App) -> HashSet<WindowId> {
app.windows.iter().map(|win| win.id).collect()
}
fn desktop_ids(app: &App) -> HashSet<WindowId> {
app.desktop
.iter_back_to_front()
.map(|slot| slot.id)
.collect()
fn close_all(app: &mut App) {
while app.front_window().is_some() {
app.close_front_window();
fn route_to(app: &mut App, request_id: i64, window: WindowId, ctx: RouteCtx) {
app.pending_routes.insert(
request_id,
Route {
target: Some(window),
ctx,
},
);
#[test]
fn open_and_close_keep_the_windows_and_the_desktop_in_lockstep() {
let mut app = crate::test_support::app();
let opened = app.open_new_window(View::Console(ConsoleState::new()));
assert_eq!(window_ids(&app), desktop_ids(&app));
assert_eq!(app.front_window(), Some(opened));
app.close_window(opened);
assert!(!window_ids(&app).contains(&opened));
let again = app.open_new_window(View::Console(ConsoleState::new()));
assert_ne!(again, opened, "ids are never reused");
#[tokio::test]
async fn closing_the_front_window_fronts_and_loads_the_next_one() {
app.attach_echo();
app.switch_tab(ViewId::Transactions);
apply(&mut app, Intent::CloseWindow);
assert_eq!(app.window_id(ViewId::Transactions), None);
assert_eq!(app.front_kind(), Some(ViewId::Accounts));
let front = app.window_of(ViewId::Accounts);
let loads = app
.pending_routes
.values()
.filter(|route| route.target == Some(front))
.count();
assert_eq!(loads, 1);
fn closing_a_missing_window_changes_nothing() {
app.close_window(WindowId(u32::MAX));
assert_eq!(app.windows.len(), ViewId::ALL.len());
fn a_pending_form_of_the_closed_window_is_released() {
let accounts = app.window_of(ViewId::Accounts);
route_to(&mut app, 5, accounts, RouteCtx::None);
app.lock_pending_form(5, Form::config_set());
assert_eq!(current_focus(&app), FocusTarget::Pending);
app.close_window(accounts);
assert!(app.pending_form.is_none());
assert_ne!(current_focus(&app), FocusTarget::Pending);
assert!(app.dialog_free("a new form"));
assert!(app.status.contains("window closed"), "{}", app.status);
fn a_pending_form_of_another_window_stays_locked() {
let tags = app.window_of(ViewId::Tags);
route_to(&mut app, 5, tags, RouteCtx::None);
app.close_window(app.window_of(ViewId::Accounts));
assert!(app.pending_form.is_some());
async fn a_bulk_delete_of_the_closed_window_is_abandoned() {
app.overlays
.set_origin(app.origin_of(app.window_of(ViewId::Transactions)));
app.submit_delete_marked(vec!["a".into(), "b".into()]);
assert_eq!(app.pending_routes.len(), 2);
app.close_window(app.window_of(ViewId::Transactions));
assert!(
app.status.contains("outcome unknown for 2"),
"{}",
app.status
assert_eq!(app.bulk.abandon(), None);
async fn a_bulk_delete_of_another_window_runs_on() {
app.submit_delete_marked(vec!["a".into()]);
assert!(app.bulk.abandon().is_some());
async fn a_late_reply_for_the_closed_window_is_dropped() {
let [(&request, _)] = app.pending_routes.iter().collect::<Vec<_>>()[..] else {
panic!("one bulk route");
};
let status = app.status.clone();
app.route_reply(request, &format!("(:id {request} :value t)"));
assert_eq!(app.status, status);
assert!(app.pending_routes.is_empty());
fn closing_over_a_modal_cancels_it_and_keeps_the_window() {
crate::test_support::open_overlay(&mut app);
assert!(app.overlays.is_empty());
press(&mut app, alt(KeyCode::F(3)));
assert_eq!(app.windows.len(), ViewId::ALL.len() - 1);
fn m_f3_and_c_f4_close_the_front_window_in_every_preset() {
let chords = [alt(KeyCode::F(3)), ctrl(KeyCode::F(4))];
for name in ["cua", "emacs", "vim"] {
for chord in chords {
let mut app = crate::test_support::app_with_preset(name);
press(&mut app, chord);
assert_eq!(app.windows.len(), ViewId::ALL.len() - 1, "{name} {chord:?}");
assert_eq!(app.window_id(ViewId::Accounts), None, "{name} {chord:?}");
fn m_f3_closes_a_focused_console() {
app.focus_console();
assert_eq!(app.window_id(ViewId::Console), None);
assert!(!app.console_input_focused());
fn the_empty_desktop_draws_only_the_background_and_the_bars() {
close_all(&mut app);
assert!(app.windows.is_empty());
assert_eq!(app.front_kind(), None);
let (buffer, _) = draw_frame(&app, 80, 24);
let body = &buffer.content()[80 * 3..80 * 20];
assert!(body.iter().all(|cell| cell.symbol() == "\u{2591}"));
fn the_menu_opens_on_the_empty_desktop() {
press(&mut app, key(KeyCode::F(10)));
assert_eq!(app.menu_item(), Some(0));
press(&mut app, key(KeyCode::Enter));
fn keys_on_the_empty_desktop_do_nothing_but_say_so() {
for code in [
KeyCode::Tab,
KeyCode::BackTab,
KeyCode::Char('r'),
KeyCode::Char('c'),
KeyCode::Char('e'),
KeyCode::Char('d'),
KeyCode::Enter,
KeyCode::Esc,
KeyCode::Down,
KeyCode::F(5),
] {
app.clear_status();
press(&mut app, key(code));
assert!(app.windows.is_empty(), "{code:?}");
assert!(app.overlays.is_empty(), "{code:?}");
press(&mut app, key(KeyCode::Tab));
assert_eq!(app.status, "no window");
fn a_digit_raises_the_existing_window() {
let transactions = app.window_of(ViewId::Transactions);
press(&mut app, key(KeyCode::Char('2')));
assert_eq!(app.front_window(), Some(transactions));
async fn a_digit_reopens_a_closed_view_zoomed_and_loads_it() {
let closed = app.window_of(ViewId::Transactions);
app.close_window(closed);
let reopened = app.window_of(ViewId::Transactions);
assert_ne!(reopened, closed);
assert_eq!(app.front_window(), Some(reopened));
assert!(app.desktop.iter_back_to_front().all(|slot| slot.zoomed));
.filter(|route| route.target == Some(reopened))
fn a_digit_on_the_empty_desktop_opens_the_view() {
press(&mut app, key(KeyCode::Char('4')));
assert_eq!(app.front_kind(), Some(ViewId::Reports));
assert_eq!(app.windows.len(), 1);
async fn a_kitty_chart_is_deleted_when_its_window_leaves_the_front_or_closes() {
let (mut app, _) = with_table(40, 20, ChartBackend::Kitty);
show(&mut app);
let [id] = chart_requests(&app)[..] else {
panic!("one chart request");
app.route_reply(id, &png_wire(id, stripes(380, 320)));
let reports = app.window_of(ViewId::Reports);
let image = crate::tabs::reports::chart::kitty_image_id(reports);
assert_eq!(
app.win::<crate::tabs::reports::ReportsTab>(reports)
.and_then(|tab| tab.chart.kitty_id()),
Some(image)
app.take_outbound();
let transmits = |app: &App| text(&rendered(app, 40, 20)).contains("a=T");
assert!(transmits(&app));
assert!(!transmits(&app), "a picture transmits once");
let deleted = vec![tvui::kitty_delete(image)];
app.switch_tab(ViewId::Accounts);
assert_eq!(app.take_outbound(), deleted);
app.switch_tab(ViewId::Reports);
assert!(app.take_outbound().is_empty());
assert!(transmits(&app), "the raised chart transmits again");
app.close_window(reports);