Lines
100 %
Functions
Branches
//! The number and the cascade slot a new window gets.
use super::report_chart::env;
use super::*;
use crate::app::windows::{desktop_area, new_view};
use crate::window::Placement;
use ratatui::layout::Rect;
use rpc::chart::ChartBackend;
fn number_of(app: &App, id: WindowId) -> Option<u8> {
app.frame_state(id).number
}
fn rect_of(app: &App, id: WindowId) -> Option<Rect> {
let slot = app.desktop.iter_back_to_front().find(|slot| slot.id == id);
slot.map(|slot| crate::test_support::flat(slot.place))
pub(super) fn app_on(width: u16, height: u16) -> App {
let mut app = crate::test_support::app();
app.terminal
.replace(env(width, height, ChartBackend::Halfblocks));
app
#[test]
fn the_startup_windows_are_numbered_in_creation_order_and_the_tenth_has_none() {
let app = crate::test_support::app();
let numbers: Vec<_> = app.windows.iter().map(|win| win.number).collect();
let expected: Vec<_> = (1..=9).map(Some).chain([None]).collect();
assert_eq!(numbers, expected);
fn a_new_window_takes_the_lowest_free_number() {
let (third, first) = (app.windows[2].id, app.windows[0].id);
app.close_window(third);
app.close_window(first);
let (one, three) = (app.open_extra(ViewId::Tags), app.open_extra(ViewId::Tags));
assert_eq!(number_of(&app, one), Some(1));
assert_eq!(number_of(&app, three), Some(3));
fn a_window_past_the_ninth_has_no_number() {
let tenth = app.open_extra(ViewId::Tags);
assert_eq!(number_of(&app, tenth), None);
fn the_startup_frame_numbers_are_the_digits_of_the_views() {
for (index, kind) in ViewId::ALL.into_iter().enumerate() {
let digit = u8::try_from(index + 1).ok().filter(|digit| *digit <= 9);
assert_eq!(
app.frame_state(app.window_of(kind)).number,
digit,
"{kind:?}"
);
fn a_reopened_view_shows_the_number_that_was_freed() {
let first = app.windows[0].id;
let again = app.open_extra(ViewId::Accounts);
assert_eq!(app.frame_state(again).number, Some(1));
fn a_new_window_opens_one_step_down_and_right_of_the_front_one() {
let mut app = app_on(120, 36);
let front = app.window_of(ViewId::Accounts);
let previous = rect_of(&app, front).unwrap_or_default();
let next = app.open_extra(ViewId::Tags);
let rect = rect_of(&app, next).unwrap_or_default();
assert_eq!((rect.x, rect.y), (previous.x + 1, previous.y + 1));
assert_eq!((rect.width, rect.height), (60, 16));
fn the_cascade_wraps_to_the_desktop_corner_before_leaving_the_desktop() {
let mut app = app_on(80, 24);
let area = desktop_area(env(80, 24, ChartBackend::Halfblocks));
let mut rects = vec![];
for _ in 0..16 {
let id = app.open_extra(ViewId::Tags);
rects.extend(rect_of(&app, id));
let slots: Vec<u16> = rects.iter().map(|rect| rect.x - area.x).collect();
assert!(slots.iter().all(|slot| *slot < 7), "{slots:?}");
assert!(rects.iter().all(|rect| rect.y - area.y == rect.x - area.x));
assert!(rects.iter().all(|rect| area.contains(rect.as_position())));
assert!(slots.windows(2).any(|pair| pair == [6, 0]), "{slots:?}");
assert!(rects.windows(2).all(|pair| pair[0] != pair[1]));
fn move_front_to(app: &mut App, rect: Rect) {
let id = app.front_window().expect("a front window");
let window = app.desktop.window(id).expect("it is open");
app.desktop.restore(tvui::Window {
place: tvui::Place::from(rect),
zoomed: false,
..window
});
fn a_window_opens_one_step_from_the_moved_front_window_not_on_the_diagonal() {
move_front_to(&mut app, Rect::new(10, 5, 30, 10));
let rect = rect_of(&app, id).expect("the window is open");
assert_eq!((rect.x, rect.y), (11, 6));
fn a_window_wraps_to_the_corner_when_one_step_from_the_front_leaves_the_desktop() {
for front in [
Rect::new(20, area.bottom() - 10, 30, 10),
Rect::new(area.right() - 1, area.y, 30, 10),
] {
move_front_to(&mut app, front);
assert_eq!((rect.x, rect.y), (area.x, area.y), "{front:?}");
fn a_new_window_is_zoomed_when_the_front_one_is() {
let zoomed = app.open_extra(ViewId::Tags);
assert!(app.frame_state(zoomed).zoomed);
app.toggle_zoom();
let plain = app.open_extra(ViewId::Tags);
assert!(!app.frame_state(plain).zoomed);
fn the_first_window_of_an_empty_desktop_is_zoomed() {
for win in app.windows.iter().map(|win| win.id).collect::<Vec<_>>() {
app.close_window(win);
assert!(app.frame_state(id).zoomed);
#[tokio::test]
async fn a_window_a_same_rect_window_covers_is_not_loaded() {
app.attach_echo();
let placement = Placement {
rect: Rect::new(3, 3, 40, 10),
};
let under = app.open_window(new_view(ViewId::Tags), placement, None);
let over = app.open_window(new_view(ViewId::Tags), placement, None);
app.activate_window(over);
let loading = |id| app.win_by(id).is_some_and(|win| win.view.loading());
assert!(loading(over));
assert!(!loading(under));
fn a_small_desktop_still_cascades_away_from_the_front_rect() {
for (width, height) in [(70, 17), (40, 12)] {
let mut app = app_on(width, height);
let area = desktop_area(env(width, height, ChartBackend::Halfblocks));
let mut slots = std::collections::BTreeSet::new();
for _ in 0..10 {
let before = app
.front_window()
.and_then(|front| app.desktop.restored_place(front, area));
let after = app.desktop.restored_place(id, area);
assert_ne!(after, before, "{width}x{height}");
assert!(
after.is_some_and(
|rect| area.contains(crate::test_support::flat(rect).as_position())
)
slots.extend(after.map(|place| place.x));
assert!(slots.len() >= 3, "{width}x{height}: {slots:?}");