1
use super::*;
2
use crate::{close_area, title_area, zoom_area};
3
use ratatui::buffer::Buffer;
4

            
5
const WIDTHS: [Constraint; 2] = [Constraint::Length(4), Constraint::Fill(1)];
6

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

            
11
13
fn drawn(panel: Panel, area: Rect) -> Buffer {
12
13
    let mut buf = Buffer::empty(area);
13
13
    panel.render(area, &mut buf);
14
13
    buf
15
13
}
16

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

            
23
29
fn state(active: bool) -> FrameState {
24
29
    let mode = match active {
25
18
        true => FrameMode::Active,
26
11
        false => FrameMode::Passive,
27
    };
28
29
    FrameState {
29
29
        mode,
30
29
        ..FrameState::default()
31
29
    }
32
29
}
33

            
34
#[test]
35
1
fn an_active_panel_joins_its_dividers_to_the_double_frame() {
36
1
    let theme = theme();
37
1
    let panel = Panel::new(&theme, "", state(true)).widths(&WIDTHS);
38
1
    let buf = drawn(panel, Rect::new(0, 0, 12, 4));
39
1
    assert_eq!(row(&buf, 0), "╔═[■]╤═[↑]═╗");
40
1
    assert_eq!(row(&buf, 1), "║    │     ║");
41
1
    assert_eq!(row(&buf, 2), "║    │     ║");
42
1
    assert_eq!(row(&buf, 3), "╚════╧════─┘");
43
1
}
44

            
45
#[test]
46
1
fn a_passive_panel_joins_its_dividers_to_the_single_frame() {
47
1
    let theme = theme();
48
1
    let panel = Panel::new(&theme, "", state(false)).widths(&WIDTHS);
49
1
    let buf = drawn(panel, Rect::new(0, 0, 12, 3));
50
1
    assert_eq!(row(&buf, 0), "┌────┬─────┐");
51
1
    assert_eq!(row(&buf, 2), "└────┴─────┘");
52
1
}
53

            
54
#[test]
55
1
fn columns_are_the_inner_widths_with_a_gap_for_the_divider() {
56
1
    let theme = theme();
57
1
    let panel = Panel::new(&theme, "", state(true)).widths(&WIDTHS);
58
1
    let area = Rect::new(2, 1, 12, 4);
59
1
    let columns = panel.columns(area);
60
1
    assert_eq!(columns, [Rect::new(3, 2, 4, 2), Rect::new(8, 2, 5, 2)]);
61
1
    assert_eq!(Panel::inner(area), Rect::new(3, 2, 10, 2));
62
1
}
63

            
64
#[test]
65
1
fn a_panel_without_widths_has_no_dividers() {
66
1
    let theme = theme();
67
1
    let buf = drawn(Panel::new(&theme, "", state(true)), Rect::new(0, 0, 8, 3));
68
1
    assert_eq!(row(&buf, 0), "╔══════╗");
69
1
    assert_eq!(row(&buf, 1), "║      ║");
70
1
}
71

            
72
#[test]
73
1
fn the_info_sits_right_aligned_in_the_bottom_border_in_the_header_role() {
74
1
    let theme = theme();
75
1
    let area = Rect::new(0, 0, 24, 3);
76
1
    let mut buf = drawn(Panel::new(&theme, "", state(true)).widths(&WIDTHS), area);
77
1
    render_info(&theme, area, &mut buf, state(true), "1–20 of 48");
78
1
    assert_eq!(row(&buf, 2), "╚════╧════ 1–20 of 48 ─┘");
79
1
    let header = theme.style(Role::PanelHeader);
80
1
    assert_eq!(buf[(11, 2)].style().fg, header.fg);
81
1
    assert_eq!(buf[(11, 2)].style().bg, header.bg);
82
1
}
83

            
84
#[test]
85
1
fn the_info_styles_only_its_own_cells_not_the_border() {
86
1
    let theme = theme();
87
1
    let area = Rect::new(0, 0, 24, 3);
88
1
    let mut buf = drawn(Panel::new(&theme, "", state(true)), area);
89
1
    let frame = buf[(2, 2)].style();
90
1
    render_info(&theme, area, &mut buf, state(true), "1–20 of 48");
91
1
    assert_eq!(buf[(2, 2)].style(), frame);
92
1
    assert_eq!(buf[(11, 2)].style().fg, theme.style(Role::PanelHeader).fg);
93
1
    assert_ne!(buf[(2, 2)].style().fg, buf[(11, 2)].style().fg);
94
1
}
95

            
96
#[test]
97
1
fn a_long_info_is_clipped_inside_the_frame() {
98
1
    let theme = theme();
99
1
    let area = Rect::new(0, 0, 12, 3);
100
1
    let mut buf = drawn(Panel::new(&theme, "", state(true)), area);
101
1
    render_info(
102
1
        &theme,
103
1
        area,
104
1
        &mut buf,
105
1
        state(true),
106
1
        "1–20 of 48 and a very long tail",
107
1
    );
108
1
    assert_eq!(row(&buf, 2).chars().count(), 12);
109
1
    assert!(row(&buf, 2).starts_with('╚'));
110
1
    assert!(row(&buf, 2).ends_with('┘'));
111
1
    assert!(row(&buf, 1).ends_with('║'));
112
1
}
113

            
114
5
fn bar(rows: usize, view: usize, top: usize, height: u16) -> Option<String> {
115
5
    let theme = theme();
116
5
    let area = Rect::new(0, 0, 1, height + 2);
117
5
    let mut buf = Buffer::empty(area);
118
5
    render_scrollbar(&theme, area, &mut buf, (rows, view, top));
119
5
    let column: String = (1..=height)
120
60
        .map(|y| buf[(0, y)].symbol().to_string())
121
        .collect();
122
5
    (rows > view).then_some(column)
123
5
}
124

            
125
#[test]
126
1
fn the_scrollbar_is_absent_while_the_rows_fit() {
127
1
    assert_eq!(bar(10, 10, 0, 12), None);
128
1
    assert_eq!(bar(3, 10, 0, 12), None);
129
1
    let area = Rect::new(0, 0, 1, 5);
130
1
    let mut buf = Buffer::empty(area);
131
1
    render_scrollbar(&theme(), area, &mut buf, (3, 10, 0));
132
1
    assert_eq!(buf, Buffer::empty(area));
133
1
}
134

            
135
#[test]
136
1
fn the_track_is_the_right_border_inside_the_corners() {
137
1
    assert_eq!(
138
1
        scrollbar_track(Rect::new(0, 0, 20, 8)),
139
1
        Rect::new(19, 1, 1, 6)
140
    );
141
1
    assert_eq!(scrollbar_track(Rect::new(4, 2, 0, 8)).width, 0);
142
1
}
143

            
144
#[test]
145
1
fn the_thumb_follows_the_top_row_from_the_first_end_to_the_last() {
146
1
    assert_eq!(bar(40, 10, 0, 12).as_deref(), Some("▲■■■▒▒▒▒▒▒▒▼"));
147
1
    assert_eq!(bar(40, 10, 15, 12).as_deref(), Some("▲▒▒▒▒■■■▒▒▒▼"));
148
1
    assert_eq!(bar(40, 10, 30, 12).as_deref(), Some("▲▒▒▒▒▒▒▒■■■▼"));
149
1
}
150

            
151
#[test]
152
1
fn the_scrollbar_roles_style_the_track_and_the_thumb() {
153
1
    let theme = theme();
154
1
    let area = Rect::new(0, 0, 1, 14);
155
1
    let mut buf = Buffer::empty(area);
156
1
    render_scrollbar(&theme, area, &mut buf, (40, 10, 0));
157
1
    let (page, thumb) = (
158
1
        theme.style(Role::Scrollbar),
159
1
        theme.style(Role::ScrollbarThumb),
160
1
    );
161
3
    let colours = |y| (buf[(0, y)].style().fg, buf[(0, y)].style().bg);
162
1
    assert_eq!(colours(1), (page.fg, page.bg));
163
1
    assert_eq!(colours(2), (thumb.fg, thumb.bg));
164
1
    assert_eq!(colours(8), (page.fg, page.bg));
165
1
}
166

            
167
#[test]
168
1
fn dividers_and_junctions_take_the_frame_role_in_every_preset() {
169
1
    for preset in ["dn", "turbo"] {
170
2
        let theme = Theme::preset(preset).expect("preset parses");
171
2
        for (active, role) in [(true, Role::PanelFrame), (false, Role::PanelFramePassive)] {
172
4
            let panel = Panel::new(&theme, "", state(active)).widths(&WIDTHS);
173
4
            let buf = drawn(panel, Rect::new(0, 0, 12, 4));
174
4
            let frame = theme.style(role);
175
4
            for y in 0..4 {
176
16
                let style = buf[(5, y)].style();
177
16
                assert_eq!(
178
16
                    (style.fg, style.bg),
179
16
                    (frame.fg, frame.bg),
180
                    "{preset} {active} {y}"
181
                );
182
            }
183
        }
184
    }
185
1
}
186

            
187
#[test]
188
1
fn the_rows_are_inside_the_frame_below_the_header() {
189
1
    assert_eq!(Panel::rows(Rect::new(2, 1, 20, 8)), Rect::new(3, 3, 18, 5));
190
1
    assert_eq!(Panel::rows(Rect::new(0, 0, 20, 2)).height, 0);
191
1
}
192

            
193
#[test]
194
1
fn the_range_counts_from_one_and_is_absent_without_rows() {
195
1
    assert_eq!(range_text(0..20, 48).as_deref(), Some("1\u{2013}20 of 48"));
196
1
    assert_eq!(
197
1
        range_text(22..30, 30).as_deref(),
198
1
        Some("23\u{2013}30 of 30")
199
    );
200
1
    assert_eq!(range_text(5..5, 30), None);
201
1
}
202

            
203
1240
fn bar_symbol(part: BarPart) -> &'static str {
204
1240
    match part {
205
120
        BarPart::Up => "\u{25B2}",
206
120
        BarPart::Down => "\u{25BC}",
207
436
        BarPart::Thumb => "\u{25A0}",
208
564
        BarPart::PageUp | BarPart::PageDown => "\u{2592}",
209
    }
210
1240
}
211

            
212
#[test]
213
1
fn the_bar_parts_cover_the_cells_the_rendered_bar_draws() {
214
1
    let theme = theme();
215
1
    let shapes = [(30, 10), (100, 7), (11, 10), (40, 1), (9, 0)];
216
1
    for height in [1u16, 2, 3, 4, 5, 8, 12, 30] {
217
8
        for (rows, view) in shapes {
218
40
            for top in [0, 1, (rows - view) / 2, rows - view] {
219
160
                let area = Rect::new(0, 0, 6, height + 2);
220
160
                let mut buf = Buffer::empty(area);
221
160
                render_scrollbar(&theme, area, &mut buf, (rows, view, top));
222
160
                let parts = scrollbar_parts(area, (rows, view, top));
223
160
                let track = scrollbar_track(area);
224
160
                assert_eq!(parts.is_empty(), height <= 2, "{height} {rows}/{view}");
225
160
                let drawn = if parts.is_empty() { 0 } else { track.height };
226
160
                for y in track.top()..track.top() + drawn {
227
1240
                    let hits: Vec<_> = parts
228
1240
                        .iter()
229
5174
                        .filter(|(cell, _)| cell.contains((track.x, y).into()))
230
                        .collect();
231
1240
                    let [(_, part)] = hits[..] else {
232
                        panic!("{height} {rows}/{view}@{top}: row {y} holds {hits:?}");
233
                    };
234
1240
                    assert_eq!(
235
1240
                        buf[(track.x, y)].symbol(),
236
1240
                        bar_symbol(*part),
237
                        "{height} {rows}/{view}@{top} {y} {part:?}"
238
                    );
239
                }
240
            }
241
        }
242
    }
243
1
}
244

            
245
#[test]
246
1
fn a_bar_is_split_into_arrows_page_areas_and_a_thumb_in_order() {
247
1
    let area = Rect::new(0, 0, 4, 12);
248
1
    let parts: Vec<_> = scrollbar_parts(area, (20, 10, 5)).into_iter().collect();
249
5
    let kinds: Vec<_> = parts.iter().map(|(_, part)| *part).collect();
250
1
    assert_eq!(
251
1
        kinds,
252
1
        [
253
1
            BarPart::Up,
254
1
            BarPart::PageUp,
255
1
            BarPart::Thumb,
256
1
            BarPart::PageDown,
257
1
            BarPart::Down
258
1
        ]
259
    );
260
1
    let track = scrollbar_track(area);
261
1
    assert_eq!(parts.first().map(|(cell, _)| cell.y), Some(track.y));
262
1
    assert_eq!(
263
1
        parts.last().map(|(cell, _)| cell.bottom()),
264
1
        Some(track.bottom())
265
    );
266
1
}
267

            
268
#[test]
269
1
fn a_bar_without_hidden_rows_has_no_parts() {
270
1
    assert!(scrollbar_parts(Rect::new(0, 0, 4, 12), (10, 10, 0)).is_empty());
271
1
    assert!(scrollbar_parts(Rect::new(0, 0, 4, 12), (3, 10, 0)).is_empty());
272
1
}
273

            
274
#[test]
275
1
fn a_passive_panel_info_runs_to_the_corner_margin() {
276
1
    let theme = theme();
277
1
    let area = Rect::new(0, 0, 24, 3);
278
1
    let mut buf = drawn(Panel::new(&theme, "", state(false)), area);
279
1
    render_info(&theme, area, &mut buf, state(false), "1–20 of 48");
280
1
    assert_eq!(row(&buf, 2), "└────────── 1–20 of 48 ┘");
281
1
}
282

            
283
#[test]
284
1
fn a_narrow_info_row_draws_nothing_and_does_not_panic() {
285
1
    let theme = theme();
286
1
    for width in 0..6 {
287
6
        let area = Rect::new(0, 0, width, 3);
288
6
        let mut buf = Buffer::empty(area);
289
6
        render_info(&theme, area, &mut buf, state(false), "info");
290
6
        render_info(&theme, area, &mut buf, state(true), "info");
291
    }
292
1
}
293

            
294
#[test]
295
1
fn a_dragging_panel_draws_single_dividers_in_the_icon_colour() {
296
1
    let theme = theme();
297
1
    let dragging = FrameState {
298
1
        mode: FrameMode::Dragging,
299
1
        ..FrameState::default()
300
1
    };
301
1
    let panel = Panel::new(&theme, "", dragging).widths(&WIDTHS);
302
1
    let buf = drawn(panel, Rect::new(0, 0, 12, 4));
303
1
    assert_eq!(row(&buf, 0), "┌─[■]┬─[↑]─┐");
304
1
    let icon = theme.style(Role::FrameIcon);
305
1
    for y in 0..4 {
306
4
        let style = buf[(5, y)].style();
307
4
        assert_eq!((style.fg, style.bg), (icon.fg, icon.bg), "{y}");
308
    }
309
1
}
310

            
311
#[test]
312
1
fn the_dividers_never_land_on_the_icons_title_or_number() {
313
1
    let theme = theme();
314
1
    let area = Rect::new(0, 0, 16, 4);
315
1
    let widths = [Constraint::Fill(1); 4];
316
1
    let numbered = FrameState {
317
1
        number: Some(5),
318
1
        ..state(true)
319
1
    };
320
1
    let panel = Panel::new(&theme, "A", numbered).widths(&widths);
321
1
    let columns = panel.columns(area);
322
4
    let gaps: Vec<u16> = columns.iter().map(|column| column.right()).collect();
323
1
    let buf = drawn(panel, area);
324
1
    let close = close_area(area, numbered).expect("fits");
325
1
    let zoom = zoom_area(area, numbered).expect("fits");
326
1
    assert!(
327
1
        gaps.iter().any(|x| close.contains((*x, 0).into())),
328
        "{gaps:?}"
329
    );
330
1
    assert!(
331
3
        gaps.iter().any(|x| zoom.contains((*x, 0).into())),
332
        "{gaps:?}"
333
    );
334
3
    let cells = |rect: Rect| -> String {
335
3
        rect.positions()
336
7
            .map(|p| buf[p].symbol().to_string())
337
            .collect()
338
3
    };
339
1
    assert_eq!(cells(close), "[■]");
340
1
    assert_eq!(cells(zoom), "[↑]");
341
1
    assert_eq!(buf[(9, 0)].symbol(), "5");
342
1
    assert_eq!(cells(title_area(area, "A").expect("fits")), "A");
343
1
}