1
use ratatui::Terminal;
2
use ratatui::backend::TestBackend;
3
use ratatui::buffer::Buffer;
4
use tvui::Role;
5

            
6
use crate::app::{App, MenuId};
7
use crate::draw::draw;
8
use crate::view::Tab;
9

            
10
14
fn drawn(app: &App, width: u16) -> Buffer {
11
14
    let mut terminal = Terminal::new(TestBackend::new(width, 24)).expect("test terminal");
12
14
    terminal
13
14
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
14
14
        .expect("draw");
15
14
    terminal.backend().buffer().clone()
16
14
}
17

            
18
112
fn row(buf: &Buffer, y: u16) -> String {
19
9040
    (0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect()
20
112
}
21

            
22
4
fn open_on(tab: Tab) -> App {
23
4
    let mut app = crate::test_support::app();
24
4
    app.switch_tab(tab);
25
4
    app.open_menu();
26
4
    app
27
4
}
28

            
29
#[test]
30
1
fn the_top_row_is_the_view_and_window_menu_bar_at_any_width() {
31
1
    let app = crate::test_support::app();
32
1
    for width in [80, 120] {
33
2
        let buf = drawn(&app, width);
34
2
        assert!(row(&buf, 0).starts_with("  View  Window "), "{width}");
35
2
        assert_eq!(buf[(2, 0)].style().fg, app.theme.style(Role::MenuHotkey).fg);
36
2
        assert_eq!(buf[(8, 0)].style().fg, app.theme.style(Role::MenuHotkey).fg);
37
2
        assert_eq!(buf[(3, 0)].style().fg, app.theme.style(Role::Menu).fg);
38
2
        assert!(!row(&buf, 0).contains("Accounts"), "{width}");
39
    }
40
1
}
41

            
42
#[test]
43
1
fn the_open_menu_lists_each_view_without_a_key_and_marks_the_open_ones() {
44
1
    let mut app = open_on(Tab::Tags);
45
1
    app.close_window(app.window_of(Tab::Config));
46
1
    let buf = drawn(&app, 80);
47
1
    for (offset, tab) in Tab::ALL.iter().enumerate() {
48
10
        let line = row(&buf, 2 + u16::try_from(offset).expect("small"));
49
10
        let mark = if *tab == Tab::Config { " " } else { "•" };
50
10
        assert!(line.contains(&format!("│ {mark} ")), "{line}");
51
10
        assert!(line.contains(tab.label()), "{line}");
52
    }
53
1
}
54

            
55
#[test]
56
1
fn the_window_menu_lists_the_commands_with_their_chords_under_its_title() {
57
1
    let mut app = crate::test_support::app();
58
1
    app.open_menu_of(MenuId::Window);
59
1
    let buf = drawn(&app, 80);
60
8
    let lines: Vec<String> = (2..10).map(|y| row(&buf, y)).collect();
61
1
    for (line, (label, chord)) in lines.iter().zip([
62
1
        ("Size/move", "C-F5"),
63
1
        ("Zoom", "F5"),
64
1
        ("Tile", ""),
65
1
        ("Cascade", ""),
66
1
        ("Next", "F6"),
67
1
        ("Previous", "S-F6"),
68
1
        ("Close", "C-F4"),
69
1
        ("Close all", ""),
70
1
    ]) {
71
8
        assert_eq!(line.chars().nth(6), Some('│'), "{line}");
72
8
        assert!(line.contains(label), "{line}");
73
8
        assert!(line.contains(&format!("{chord} │")), "{line}");
74
    }
75
1
    assert_eq!(
76
1
        buf[(8, 0)].style().bg,
77
1
        app.theme.style(Role::MenuSelected).bg
78
    );
79
1
}
80

            
81
#[test]
82
1
fn the_window_commands_are_dimmed_on_the_empty_desktop_and_the_views_are_not() {
83
1
    let mut app = crate::test_support::app();
84
11
    while app.front_window().is_some() {
85
10
        app.close_front_window();
86
    }
87
1
    app.open_menu_of(MenuId::Window);
88
1
    let disabled = app.theme.style(Role::MenuDisabled);
89
1
    let selected = app.theme.style(Role::MenuSelectedDisabled);
90
1
    let buf = drawn(&app, 80);
91
1
    assert_eq!(buf[(10, 2)].style().fg, selected.fg);
92
1
    assert_eq!(buf[(10, 2)].style().bg, selected.bg);
93
1
    assert!(
94
1
        (3..9)
95
6
            .all(|y| buf[(10, y)].style().fg == disabled.fg
96
6
                && buf[(10, y)].style().bg == disabled.bg)
97
    );
98
1
    app.open_menu_of(MenuId::View);
99
1
    let buf = drawn(&app, 80);
100
9
    assert!((3..12).all(|y| buf[(5, y)].style().fg != disabled.fg));
101
1
}
102

            
103
#[test]
104
1
fn the_selected_item_and_the_title_use_the_selected_roles() {
105
1
    let mut app = open_on(Tab::Accounts);
106
1
    app.menu_next();
107
1
    let buf = drawn(&app, 80);
108
1
    let selected = app.theme.style(Role::MenuSelected);
109
1
    assert_eq!(
110
1
        buf[(2, 0)].style().bg,
111
1
        app.theme.style(Role::MenuSelectedHotkey).bg
112
    );
113
1
    assert_eq!(buf[(5, 3)].style().bg, selected.bg);
114
1
    assert_eq!(buf[(5, 2)].style().bg, app.theme.style(Role::Menu).bg);
115
1
}
116

            
117
#[test]
118
1
fn the_open_menu_casts_a_shadow() {
119
1
    let app = open_on(Tab::Accounts);
120
1
    let buf = drawn(&app, 80);
121
1
    assert_eq!(buf[(21, 3)].style().bg, app.theme.style(Role::Shadow).bg);
122
1
    assert_eq!(buf[(5, 13)].style().bg, app.theme.style(Role::Shadow).bg);
123
1
}
124

            
125
#[test]
126
1
fn the_popup_frame_sits_over_the_window_frame_column() {
127
1
    let buf = drawn(&open_on(Tab::Accounts), 80);
128
1
    assert!(row(&buf, 1).starts_with('┌'));
129
1
    assert!(row(&buf, 2).starts_with("│ "));
130
1
}
131

            
132
2
fn crowded_window_menu() -> App {
133
2
    let mut app = crate::test_support::app();
134
2
    for _ in 0..12 {
135
24
        app.open_extra(Tab::Tags);
136
    }
137
2
    app.open_menu_of(MenuId::Window);
138
2
    app
139
2
}
140

            
141
5
fn shown(buf: &Buffer, label: &str) -> Option<u16> {
142
88
    (2..buf.area.height).find(|y| row(buf, *y).contains(label))
143
5
}
144

            
145
#[test]
146
1
fn a_long_window_list_scrolls_to_keep_the_selection_on_screen() {
147
1
    let mut app = crowded_window_menu();
148
1
    let last = app.menu_entries().pop().map(|entry| entry.label);
149
1
    let last = last.unwrap_or_default();
150
1
    assert!(shown(&drawn(&app, 80), &last).is_none());
151
1
    app.menu_last();
152
1
    let buf = drawn(&app, 80);
153
1
    let at = shown(&buf, &last);
154
1
    assert!(at.is_some_and(|y| y < 23), "{at:?}");
155
1
    assert!(shown(&buf, "Zoom").is_none());
156
1
    app.menu_first();
157
1
    let buf = drawn(&app, 80);
158
1
    assert!(shown(&buf, "Zoom").is_some());
159
1
    assert!(shown(&buf, &last).is_none());
160
1
}
161

            
162
#[test]
163
1
fn moving_up_inside_the_view_does_not_scroll_the_list() {
164
1
    let mut app = crowded_window_menu();
165
1
    app.menu_last();
166
1
    let _ = drawn(&app, 80);
167
1
    let top = app.menu_window.top();
168
1
    app.menu_prev();
169
1
    let _ = drawn(&app, 80);
170
1
    assert_eq!(app.menu_window.top(), top);
171
1
    assert!(top > 0);
172
1
}