1
//! The number and the cascade slot a new window gets.
2
use super::report_chart::env;
3
use super::*;
4
use crate::app::windows::{desktop_area, new_view};
5
use crate::window::Placement;
6
use ratatui::layout::Rect;
7
use rpc::chart::ChartBackend;
8

            
9
3
fn number_of(app: &App, id: WindowId) -> Option<u8> {
10
3
    app.frame_state(id).number
11
3
}
12

            
13
21
fn rect_of(app: &App, id: WindowId) -> Option<Rect> {
14
350
    let slot = app.desktop.iter_back_to_front().find(|slot| slot.id == id);
15
21
    slot.map(|slot| crate::test_support::flat(slot.place))
16
21
}
17

            
18
19
pub(super) fn app_on(width: u16, height: u16) -> App {
19
19
    let mut app = crate::test_support::app();
20
19
    app.terminal
21
19
        .replace(env(width, height, ChartBackend::Halfblocks));
22
19
    app
23
19
}
24

            
25
#[test]
26
1
fn the_startup_windows_are_numbered_in_creation_order_and_the_tenth_has_none() {
27
1
    let app = crate::test_support::app();
28
10
    let numbers: Vec<_> = app.windows.iter().map(|win| win.number).collect();
29
1
    let expected: Vec<_> = (1..=9).map(Some).chain([None]).collect();
30
1
    assert_eq!(numbers, expected);
31
1
}
32

            
33
#[test]
34
1
fn a_new_window_takes_the_lowest_free_number() {
35
1
    let mut app = crate::test_support::app();
36
1
    let (third, first) = (app.windows[2].id, app.windows[0].id);
37
1
    app.close_window(third);
38
1
    app.close_window(first);
39
1
    let (one, three) = (app.open_extra(ViewId::Tags), app.open_extra(ViewId::Tags));
40
1
    assert_eq!(number_of(&app, one), Some(1));
41
1
    assert_eq!(number_of(&app, three), Some(3));
42
1
}
43

            
44
#[test]
45
1
fn a_window_past_the_ninth_has_no_number() {
46
1
    let mut app = crate::test_support::app();
47
1
    let tenth = app.open_extra(ViewId::Tags);
48
1
    assert_eq!(number_of(&app, tenth), None);
49
1
}
50

            
51
#[test]
52
1
fn the_startup_frame_numbers_are_the_digits_of_the_views() {
53
1
    let app = crate::test_support::app();
54
1
    for (index, kind) in ViewId::ALL.into_iter().enumerate() {
55
10
        let digit = u8::try_from(index + 1).ok().filter(|digit| *digit <= 9);
56
10
        assert_eq!(
57
10
            app.frame_state(app.window_of(kind)).number,
58
10
            digit,
59
            "{kind:?}"
60
        );
61
    }
62
1
}
63

            
64
#[test]
65
1
fn a_reopened_view_shows_the_number_that_was_freed() {
66
1
    let mut app = crate::test_support::app();
67
1
    let first = app.windows[0].id;
68
1
    app.close_window(first);
69
1
    let again = app.open_extra(ViewId::Accounts);
70
1
    assert_eq!(app.frame_state(again).number, Some(1));
71
1
}
72

            
73
#[test]
74
1
fn a_new_window_opens_one_step_down_and_right_of_the_front_one() {
75
1
    let mut app = app_on(120, 36);
76
1
    let front = app.window_of(ViewId::Accounts);
77
1
    let previous = rect_of(&app, front).unwrap_or_default();
78
1
    let next = app.open_extra(ViewId::Tags);
79
1
    let rect = rect_of(&app, next).unwrap_or_default();
80
1
    assert_eq!((rect.x, rect.y), (previous.x + 1, previous.y + 1));
81
1
    assert_eq!((rect.width, rect.height), (60, 16));
82
1
}
83

            
84
#[test]
85
1
fn the_cascade_wraps_to_the_desktop_corner_before_leaving_the_desktop() {
86
1
    let mut app = app_on(80, 24);
87
1
    let area = desktop_area(env(80, 24, ChartBackend::Halfblocks));
88
1
    let mut rects = vec![];
89
1
    for _ in 0..16 {
90
16
        let id = app.open_extra(ViewId::Tags);
91
16
        rects.extend(rect_of(&app, id));
92
    }
93
16
    let slots: Vec<u16> = rects.iter().map(|rect| rect.x - area.x).collect();
94
16
    assert!(slots.iter().all(|slot| *slot < 7), "{slots:?}");
95
16
    assert!(rects.iter().all(|rect| rect.y - area.y == rect.x - area.x));
96
16
    assert!(rects.iter().all(|rect| area.contains(rect.as_position())));
97
6
    assert!(slots.windows(2).any(|pair| pair == [6, 0]), "{slots:?}");
98
15
    assert!(rects.windows(2).all(|pair| pair[0] != pair[1]));
99
1
}
100

            
101
3
fn move_front_to(app: &mut App, rect: Rect) {
102
3
    let id = app.front_window().expect("a front window");
103
3
    let window = app.desktop.window(id).expect("it is open");
104
3
    app.desktop.restore(tvui::Window {
105
3
        place: tvui::Place::from(rect),
106
3
        zoomed: false,
107
3
        ..window
108
3
    });
109
3
}
110

            
111
#[test]
112
1
fn a_window_opens_one_step_from_the_moved_front_window_not_on_the_diagonal() {
113
1
    let mut app = app_on(80, 24);
114
1
    move_front_to(&mut app, Rect::new(10, 5, 30, 10));
115
1
    let id = app.open_extra(ViewId::Tags);
116
1
    let rect = rect_of(&app, id).expect("the window is open");
117
1
    assert_eq!((rect.x, rect.y), (11, 6));
118
1
}
119

            
120
#[test]
121
1
fn a_window_wraps_to_the_corner_when_one_step_from_the_front_leaves_the_desktop() {
122
1
    let area = desktop_area(env(80, 24, ChartBackend::Halfblocks));
123
1
    for front in [
124
1
        Rect::new(20, area.bottom() - 10, 30, 10),
125
1
        Rect::new(area.right() - 1, area.y, 30, 10),
126
1
    ] {
127
2
        let mut app = app_on(80, 24);
128
2
        move_front_to(&mut app, front);
129
2
        let id = app.open_extra(ViewId::Tags);
130
2
        let rect = rect_of(&app, id).expect("the window is open");
131
2
        assert_eq!((rect.x, rect.y), (area.x, area.y), "{front:?}");
132
    }
133
1
}
134

            
135
#[test]
136
1
fn a_new_window_is_zoomed_when_the_front_one_is() {
137
1
    let mut app = crate::test_support::app();
138
1
    let zoomed = app.open_extra(ViewId::Tags);
139
1
    assert!(app.frame_state(zoomed).zoomed);
140
1
    app.toggle_zoom();
141
1
    let plain = app.open_extra(ViewId::Tags);
142
1
    assert!(!app.frame_state(plain).zoomed);
143
1
}
144

            
145
#[test]
146
1
fn the_first_window_of_an_empty_desktop_is_zoomed() {
147
1
    let mut app = crate::test_support::app();
148
10
    for win in app.windows.iter().map(|win| win.id).collect::<Vec<_>>() {
149
10
        app.close_window(win);
150
    }
151
1
    let id = app.open_extra(ViewId::Tags);
152
1
    assert!(app.frame_state(id).zoomed);
153
1
}
154

            
155
#[tokio::test]
156
1
async fn a_window_a_same_rect_window_covers_is_not_loaded() {
157
1
    let mut app = app_on(80, 24);
158
1
    app.attach_echo();
159
1
    let placement = Placement {
160
1
        rect: Rect::new(3, 3, 40, 10),
161
1
        zoomed: false,
162
1
    };
163
1
    let under = app.open_window(new_view(ViewId::Tags), placement, None);
164
1
    let over = app.open_window(new_view(ViewId::Tags), placement, None);
165
1
    app.activate_window(over);
166
2
    let loading = |id| app.win_by(id).is_some_and(|win| win.view.loading());
167
1
    assert!(loading(over));
168
1
    assert!(!loading(under));
169
1
}
170

            
171
#[test]
172
1
fn a_small_desktop_still_cascades_away_from_the_front_rect() {
173
1
    for (width, height) in [(70, 17), (40, 12)] {
174
2
        let mut app = app_on(width, height);
175
2
        let area = desktop_area(env(width, height, ChartBackend::Halfblocks));
176
2
        let mut slots = std::collections::BTreeSet::new();
177
2
        for _ in 0..10 {
178
20
            let before = app
179
20
                .front_window()
180
20
                .and_then(|front| app.desktop.restored_place(front, area));
181
20
            let id = app.open_extra(ViewId::Tags);
182
20
            let after = app.desktop.restored_place(id, area);
183
20
            assert_ne!(after, before, "{width}x{height}");
184
20
            assert!(
185
20
                after.is_some_and(
186
20
                    |rect| area.contains(crate::test_support::flat(rect).as_position())
187
                )
188
            );
189
20
            slots.extend(after.map(|place| place.x));
190
        }
191
2
        assert!(slots.len() >= 3, "{width}x{height}: {slots:?}");
192
    }
193
1
}