1
use ratatui::buffer::Buffer;
2
use ratatui::layout::Rect;
3
use ratatui::widgets::Widget;
4

            
5
use super::*;
6

            
7
12
fn theme() -> Theme {
8
12
    Theme::preset("dn").expect("the dn preset parses")
9
12
}
10

            
11
4
fn row(buf: &Buffer, y: u16) -> String {
12
4
    (0..buf.area.width)
13
110
        .map(|x| buf[(x, y)].symbol().to_string())
14
        .collect()
15
4
}
16

            
17
2
fn bar(selected: Option<usize>) -> Buffer {
18
2
    let area = Rect::new(0, 0, 20, 1);
19
2
    let mut buf = Buffer::empty(area);
20
2
    MenuBar::new(&theme(), &["~M~ode", "~H~elp"], selected).render(area, &mut buf);
21
2
    buf
22
2
}
23

            
24
8
fn items() -> Vec<MenuItem<'static>> {
25
8
    Vec::from([
26
8
        MenuItem {
27
8
            label: "~A~ccounts",
28
8
            key: "1",
29
8
            marked: true,
30
8
            enabled: true,
31
8
        },
32
8
        MenuItem {
33
8
            label: "Co~n~sole",
34
8
            key: "F10",
35
8
            marked: false,
36
8
            enabled: false,
37
8
        },
38
8
    ])
39
8
}
40

            
41
5
fn popup(selected: usize) -> Buffer {
42
5
    let area = Rect::new(0, 0, 30, 8);
43
5
    let mut buf = Buffer::empty(area);
44
5
    for position in area.positions() {
45
1200
        buf[position].set_char('x');
46
    }
47
5
    let items = items();
48
5
    MenuPopup::new(&theme(), &items, selected).render(Rect::new(1, 0, 8, 1), &mut buf);
49
5
    buf
50
5
}
51

            
52
#[test]
53
1
fn the_bar_pads_each_title_one_column_in() {
54
1
    assert_eq!(row(&bar(None), 0), "  Mode  Help        ");
55
1
}
56

            
57
#[test]
58
1
fn title_areas_cover_the_padded_titles_and_clip_to_the_row() {
59
1
    let titles = ["~M~ode", "~H~elp"];
60
1
    let areas = MenuBar::title_areas(Rect::new(0, 0, 20, 1), &titles);
61
1
    assert_eq!(areas, [Rect::new(1, 0, 6, 1), Rect::new(7, 0, 6, 1)]);
62
1
    let clipped = MenuBar::title_areas(Rect::new(0, 0, 9, 1), &titles);
63
1
    assert_eq!(clipped[1], Rect::new(7, 0, 2, 1));
64
1
}
65

            
66
#[test]
67
1
fn the_bar_colours_hotkeys_and_the_selected_title() {
68
1
    let theme = theme();
69
1
    let buf = bar(Some(0));
70
1
    assert_eq!(
71
1
        buf[(2, 0)].style().fg,
72
1
        theme.style(Role::MenuSelectedHotkey).fg
73
    );
74
1
    assert_eq!(buf[(3, 0)].style().bg, theme.style(Role::MenuSelected).bg);
75
1
    assert_eq!(buf[(1, 0)].style().bg, theme.style(Role::MenuSelected).bg);
76
1
    assert_eq!(buf[(8, 0)].style().fg, theme.style(Role::MenuHotkey).fg);
77
1
    assert_eq!(buf[(9, 0)].style().fg, theme.style(Role::Menu).fg);
78
1
    assert_eq!(buf[(19, 0)].style().bg, theme.style(Role::Menu).bg);
79
1
}
80

            
81
#[test]
82
1
fn the_popup_box_fits_the_widest_label_and_key() {
83
1
    let area = MenuPopup::area(Rect::new(1, 0, 8, 1), &items());
84
1
    assert_eq!(area, Rect::new(0, 1, 19, 4));
85
1
    let rows = MenuPopup::item_areas(Rect::new(1, 0, 8, 1), &items());
86
1
    assert_eq!(rows, [Rect::new(1, 2, 17, 1), Rect::new(1, 3, 17, 1)]);
87
1
}
88

            
89
#[test]
90
1
fn the_popup_lists_labels_left_keys_right_and_a_bullet_on_the_marked() {
91
1
    let buf = popup(0);
92
1
    assert_eq!(row(&buf, 1), "┌─────────────────┐xxxxxxxxxxx");
93
1
    assert_eq!(row(&buf, 2), "│ • Accounts    1 │xxxxxxxxxxx");
94
1
    assert_eq!(row(&buf, 4), "└─────────────────┘xxxxxxxxxxx");
95
1
}
96

            
97
#[test]
98
1
fn the_popup_shades_the_selected_item_and_the_hotkeys() {
99
1
    let theme = theme();
100
1
    let buf = popup(1);
101
1
    assert_eq!(buf[(3, 2)].style().bg, theme.style(Role::Menu).bg);
102
1
    assert_eq!(buf[(3, 3)].style().bg, theme.style(Role::MenuSelected).bg);
103
1
    assert_eq!(buf[(4, 2)].style().fg, theme.style(Role::MenuHotkey).fg);
104
1
}
105

            
106
#[test]
107
1
fn a_disabled_item_uses_the_disabled_roles_in_every_cell_of_its_row() {
108
1
    let theme = theme();
109
1
    let plain = popup(0);
110
1
    let chosen = popup(1);
111
1
    for (x, y, buf, role) in [
112
1
        (5, 3, &plain, Role::MenuDisabled),
113
1
        (7, 3, &plain, Role::MenuDisabled),
114
1
        (5, 3, &chosen, Role::MenuSelectedDisabled),
115
1
        (7, 3, &chosen, Role::MenuSelectedDisabled),
116
1
        (5, 2, &plain, Role::MenuSelected),
117
1
    ] {
118
5
        let style = buf[(x, y)].style();
119
5
        assert_eq!(style.fg, theme.style(role).fg, "{x},{y} fg");
120
5
        assert_eq!(style.bg, theme.style(role).bg, "{x},{y} bg");
121
    }
122
1
}
123

            
124
#[test]
125
1
fn the_popup_casts_a_shadow_right_and_below() {
126
1
    let theme = theme();
127
1
    let buf = popup(0);
128
1
    assert_eq!(buf[(19, 2)].style().bg, theme.style(Role::Shadow).bg);
129
1
    assert_eq!(buf[(20, 3)].style().bg, theme.style(Role::Shadow).bg);
130
1
    assert_eq!(buf[(4, 5)].style().bg, theme.style(Role::Shadow).bg);
131
1
    assert_ne!(buf[(22, 3)].style().bg, theme.style(Role::Shadow).bg);
132
1
}
133

            
134
#[test]
135
1
fn a_popup_past_the_buffer_edge_is_clipped_not_a_panic() {
136
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 10, 3));
137
1
    let items = items();
138
1
    MenuPopup::new(&theme(), &items, 0).render(Rect::new(5, 0, 8, 1), &mut buf);
139
1
    assert_eq!(buf[(4, 1)].symbol(), "┌");
140
1
}