1
use super::open::app_on;
2
use super::report_chart::env;
3
use crate::app::App;
4
use crate::app::windows::desktop_area;
5
use ratatui::layout::Rect;
6
use rpc::chart::ChartBackend;
7
use tvui::{Place, Window};
8

            
9
4
fn place_front(app: &mut App, rect: Rect) {
10
4
    let id = app.front_window().expect("a front window");
11
4
    let window = app.desktop.window(id).expect("it is open");
12
4
    app.desktop.restore(Window {
13
4
        place: Place::from(rect),
14
4
        zoomed: false,
15
4
        ..window
16
4
    });
17
4
}
18

            
19
5
fn front_place(app: &App) -> Place {
20
5
    let id = app.front_window().expect("a front window");
21
5
    let place = app.desktop.restored_place(id, app.desktop_rect());
22
5
    place.expect("it is open")
23
5
}
24

            
25
5
fn resize_to(app: &mut App, width: u16, height: u16) {
26
5
    app.sync_terminal(env(width, height, ChartBackend::Halfblocks));
27
5
}
28

            
29
#[test]
30
1
fn a_shrinking_terminal_scales_the_windows_with_it() {
31
1
    let mut app = app_on(80, 24);
32
1
    let before = desktop_area(env(80, 24, ChartBackend::Halfblocks));
33
1
    place_front(&mut app, Rect::new(40, before.y + 4, 30, 10));
34
1
    resize_to(&mut app, 40, 24);
35
1
    let place = front_place(&app);
36
1
    assert_eq!((place.x, place.width), (20, 16));
37
1
    assert_eq!((place.y, place.height), (i32::from(before.y) + 4, 10));
38
1
}
39

            
40
#[test]
41
1
fn a_shrunk_window_keeps_its_minimum_and_stays_usable() {
42
1
    let mut app = app_on(80, 24);
43
1
    place_front(&mut app, Rect::new(60, 18, 20, 6));
44
1
    resize_to(&mut app, 30, 10);
45
1
    let area = app.desktop_rect();
46
1
    let place = front_place(&app);
47
1
    assert!(place.width >= 16 && place.height >= 3, "{place:?}");
48
1
    assert!(place.visible_in(area).width >= 2, "{place:?}");
49
1
    assert!(!place.visible_in(area).is_empty());
50
1
}
51

            
52
#[test]
53
1
fn growing_the_terminal_again_scales_back() {
54
1
    let mut app = app_on(80, 24);
55
1
    let start = Rect::new(50, 10, 30, 8);
56
1
    place_front(&mut app, start);
57
1
    resize_to(&mut app, 40, 12);
58
1
    resize_to(&mut app, 80, 24);
59
1
    let place = front_place(&app);
60
1
    assert!(place.x >= 40 && place.x <= 55, "{place:?}");
61
1
    assert!(place.width >= 30, "{place:?}");
62
1
}
63

            
64
#[test]
65
1
fn an_unchanged_screen_leaves_the_windows_alone() {
66
1
    let mut app = app_on(80, 24);
67
1
    place_front(&mut app, Rect::new(50, 10, 30, 8));
68
1
    let before = front_place(&app);
69
1
    resize_to(&mut app, 80, 24);
70
1
    assert_eq!(front_place(&app), before);
71
1
}