1
use ratatui::style::Style;
2

            
3
use super::*;
4
use crate::{Role, scrollbar_track};
5

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

            
10
const BUTTONS: [Button<'static>; 2] = [
11
    Button {
12
        label: "~O~K",
13
        default: true,
14
    },
15
    Button {
16
        label: "~C~ancel",
17
        default: false,
18
    },
19
];
20

            
21
7
fn row(buf: &Buffer, y: u16) -> String {
22
7
    (0..buf.area.width)
23
238
        .map(|x| buf[(x, y)].symbol().to_string())
24
        .collect()
25
7
}
26

            
27
3
fn drawn(area: Rect, screen: Rect) -> Buffer {
28
3
    let mut buf = Buffer::empty(screen);
29
3
    Dialog::new(&theme(), "Form", &BUTTONS).render(area, &mut buf);
30
3
    buf
31
3
}
32

            
33
#[test]
34
1
fn the_dialog_draws_frame_title_and_buttons_bottom_centred() {
35
1
    let buf = drawn(Rect::new(0, 0, 30, 7), Rect::new(0, 0, 34, 8));
36
1
    assert_eq!(row(&buf, 0), "╔═[■]═══════ Form ═══════════╗    ");
37
1
    assert_eq!(row(&buf, 3), "║                            ║    ");
38
1
    assert_eq!(row(&buf, 4), "║       OK   ▄    Cancel ▄   ║    ");
39
1
    assert_eq!(row(&buf, 5), "║     ▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀   ║    ");
40
1
    assert_eq!(row(&buf, 6), "╚════════════════════════════╝    ");
41
1
}
42

            
43
#[test]
44
1
fn the_default_button_and_hotkeys_use_their_roles() {
45
1
    let theme = theme();
46
1
    let buf = drawn(Rect::new(0, 0, 30, 7), Rect::new(0, 0, 34, 8));
47
1
    let ok = theme.style(Role::ButtonDefault);
48
1
    let cancel = theme.style(Role::Button);
49
1
    let hot = theme.style(Role::ButtonHotkey);
50
1
    assert_eq!(buf[(5, 4)].style().bg, ok.bg);
51
1
    assert_eq!(buf[(9, 4)].style().fg, ok.fg);
52
1
    assert_eq!(buf[(8, 4)].style().fg, hot.fg);
53
1
    assert_eq!(buf[(17, 4)].style().bg, cancel.bg);
54
1
    assert_eq!(buf[(18, 4)].style().fg, hot.fg);
55
1
    assert_eq!(buf[(1, 4)].style().bg, theme.style(Role::Dialog).bg);
56
1
}
57

            
58
#[test]
59
1
fn the_focused_button_is_drawn_in_the_focus_role() {
60
1
    let theme = theme();
61
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 34, 8));
62
1
    let dialog = Dialog::new(&theme, "Form", &BUTTONS).focused(Some(1));
63
1
    dialog.render(Rect::new(0, 0, 30, 7), &mut buf);
64
1
    let focus = theme.style(Role::ButtonFocus);
65
1
    assert_eq!(buf[(17, 4)].style().bg, focus.bg);
66
1
    assert_eq!(buf[(5, 4)].style().bg, theme.style(Role::Button).bg);
67
1
}
68

            
69
#[test]
70
1
fn the_pressed_button_is_drawn_down_and_the_others_stay_up() {
71
1
    let theme = theme();
72
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 34, 8));
73
1
    let dialog = Dialog::new(&theme, "Form", &BUTTONS).pressed(Some(1));
74
1
    dialog.render(Rect::new(0, 0, 30, 7), &mut buf);
75
1
    assert_eq!(row(&buf, 4), "║       OK   ▄     Cancel    ║    ");
76
1
    assert_eq!(row(&buf, 5), "║     ▀▀▀▀▀▀▀▀               ║    ");
77
1
}
78

            
79
#[test]
80
1
fn the_dialog_casts_the_shadow() {
81
1
    let theme = theme();
82
1
    let buf = drawn(Rect::new(0, 0, 30, 7), Rect::new(0, 0, 34, 8));
83
1
    let shadow = Style::reset().patch(theme.style(Role::Shadow));
84
1
    assert_eq!(buf[(30, 3)].style().bg, shadow.bg);
85
1
    assert_eq!(buf[(31, 3)].style().bg, shadow.bg);
86
1
    assert_eq!(buf[(10, 7)].style().bg, shadow.bg);
87
1
}
88

            
89
#[test]
90
1
fn the_body_excludes_the_frame_and_the_button_row() {
91
1
    let area = Rect::new(2, 1, 30, 10);
92
1
    assert_eq!(Dialog::body_area(area), Rect::new(3, 2, 28, 5));
93
1
    assert_eq!(
94
1
        Dialog::body_area(Rect::new(0, 0, 4, 3)),
95
1
        Rect::new(1, 1, 2, 0)
96
    );
97
1
}
98

            
99
#[test]
100
1
fn the_buttons_are_centred_in_the_last_two_inner_rows() {
101
1
    let areas = Dialog::button_areas(Rect::new(0, 0, 30, 7), &BUTTONS, ButtonRow::Centred);
102
1
    assert_eq!(areas, [Rect::new(4, 4, 10, 2), Rect::new(16, 4, 10, 2)]);
103
1
}
104

            
105
#[test]
106
1
fn a_dialog_too_short_for_buttons_has_none() {
107
1
    assert!(Dialog::button_areas(Rect::new(0, 0, 30, 3), &BUTTONS, ButtonRow::Centred).is_empty());
108
1
}
109

            
110
27
fn button(label: &'static str) -> Button<'static> {
111
27
    Button {
112
27
        label,
113
27
        default: false,
114
27
    }
115
27
}
116

            
117
9
fn xs(area: Rect, count: usize, row: ButtonRow) -> Vec<(u16, u16, u16)> {
118
9
    let all = [button("~O~K"), button("~C~ancel"), button("~H~elp")];
119
9
    Dialog::button_areas(area, &all[..count], row)
120
9
        .iter()
121
20
        .map(|rect| (rect.x, rect.width, rect.y))
122
        .collect()
123
9
}
124

            
125
#[test]
126
1
fn a_centred_row_puts_one_two_and_three_buttons_in_the_middle() {
127
1
    let area = Rect::new(0, 0, 44, 8);
128
1
    let centred = ButtonRow::Centred;
129
1
    assert_eq!(xs(area, 1, centred), [(17, 10, 5)]);
130
1
    assert_eq!(xs(area, 2, centred), [(11, 10, 5), (23, 10, 5)]);
131
1
    assert_eq!(xs(area, 3, centred), [(5, 10, 5), (17, 10, 5), (29, 10, 5)]);
132
1
}
133

            
134
#[test]
135
1
fn a_bottom_right_row_ends_one_column_before_the_frame() {
136
1
    let area = Rect::new(0, 0, 44, 8);
137
1
    let right = ButtonRow::BottomRight;
138
1
    assert_eq!(xs(area, 1, right), [(32, 10, 5)]);
139
1
    assert_eq!(xs(area, 2, right), [(20, 10, 5), (32, 10, 5)]);
140
1
    assert_eq!(xs(area, 3, right), [(8, 10, 5), (20, 10, 5), (32, 10, 5)]);
141
1
}
142

            
143
#[test]
144
1
fn a_row_wider_than_the_dialog_is_cut_off_at_the_frame() {
145
1
    let area = Rect::new(0, 0, 30, 8);
146
1
    let cut = [(1, 10, 5), (13, 10, 5), (25, 4, 5)];
147
1
    assert_eq!(xs(area, 3, ButtonRow::Centred), cut);
148
1
    assert_eq!(xs(area, 3, ButtonRow::BottomRight), cut);
149
1
    assert_eq!(
150
1
        xs(area, 2, ButtonRow::BottomRight),
151
1
        [(6, 10, 5), (18, 10, 5)]
152
    );
153
1
    let areas = Dialog::button_areas(Rect::new(0, 0, 12, 8), &BUTTONS, ButtonRow::Centred);
154
1
    assert_eq!(areas[0], Rect::new(1, 5, 10, 2));
155
1
    assert_eq!(areas[1].area(), 0);
156
1
}
157

            
158
#[test]
159
1
fn the_row_width_is_the_buttons_and_the_gaps() {
160
1
    assert_eq!(Dialog::button_row_width(&BUTTONS), 22);
161
1
    assert_eq!(Dialog::button_row_width(&BUTTONS[..1]), 10);
162
1
    assert_eq!(Dialog::button_row_width(&[]), 0);
163
1
}
164

            
165
#[test]
166
1
fn a_click_hits_the_face_without_the_blank_column_the_shadow_column_and_the_shadow_row() {
167
1
    let face = Button::face(Rect::new(4, 4, 10, 2));
168
1
    assert_eq!(face, Rect::new(5, 4, 8, 1));
169
1
    assert_eq!(Button::face(Rect::new(0, 0, 1, 2)).width, 0);
170
1
}
171

            
172
#[test]
173
1
fn the_scrollbar_runs_along_the_body_rows_of_the_right_frame() {
174
1
    let area = Rect::new(0, 0, 30, 10);
175
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 34, 11));
176
1
    Dialog::new(&theme(), "Form", &BUTTONS)
177
1
        .scrollbar(40, 5, 0)
178
1
        .render(area, &mut buf);
179
1
    let body = Dialog::body_area(area);
180
1
    assert_eq!(body.height, 5);
181
1
    assert_eq!(buf[(29, 1)].symbol(), "\u{25B2}");
182
1
    assert_eq!(buf[(29, 5)].symbol(), "\u{25BC}");
183
1
    assert_eq!(buf[(29, 6)].symbol(), "\u{2551}");
184
1
    assert_eq!(buf[(29, 0)].symbol(), "\u{2557}");
185
1
    let thumb = theme().style(Role::ScrollbarThumb);
186
1
    assert_eq!(buf[(29, 2)].style().bg, thumb.bg);
187
1
    assert_eq!(buf[(29, 2)].style().fg, thumb.fg);
188
1
}
189

            
190
#[test]
191
1
fn no_scrollbar_when_the_body_fits() {
192
1
    let area = Rect::new(0, 0, 30, 10);
193
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 34, 11));
194
1
    Dialog::new(&theme(), "Form", &BUTTONS)
195
1
        .scrollbar(6, 6, 0)
196
1
        .render(area, &mut buf);
197
1
    assert_eq!(buf[(29, 1)].symbol(), "\u{2551}");
198
1
}
199

            
200
#[test]
201
1
fn the_scrollbar_frame_is_the_right_frame_from_the_corner_row_to_the_one_below_the_body() {
202
1
    let frame = Dialog::scrollbar_frame(Rect::new(0, 0, 30, 10));
203
1
    assert_eq!((frame.y, frame.bottom()), (0, 7));
204
1
    assert_eq!(scrollbar_track(frame), Rect::new(29, 1, 1, 5));
205
1
}
206

            
207
#[test]
208
1
fn a_row_as_wide_as_a_u16_places_without_overflow() {
209
1
    let label = "x".repeat(70_000);
210
1
    let huge = [Button {
211
1
        label: &label,
212
1
        default: false,
213
1
    }];
214
1
    assert_eq!(Dialog::button_row_width(&huge), u16::MAX);
215
1
    let area = Rect::new(0, 0, 30, 8);
216
1
    for row in [ButtonRow::Centred, ButtonRow::BottomRight] {
217
2
        let areas = Dialog::button_areas(area, &huge, row);
218
2
        assert_eq!(areas, [Rect::new(1, 5, 28, 2)]);
219
    }
220
1
}
221

            
222
#[test]
223
1
fn the_dialog_frame_has_a_close_icon_and_no_zoom_grow_or_number_in_both_presets() {
224
1
    for name in ["dn", "turbo"] {
225
2
        let theme = Theme::preset(name).expect("the preset parses");
226
2
        let mut buf = Buffer::empty(Rect::new(0, 0, 30, 7));
227
2
        Dialog::new(&theme, "Form", &BUTTONS).render(Rect::new(0, 0, 30, 7), &mut buf);
228
60
        let top: String = (0..30).map(|x| buf[(x, 0)].symbol().to_string()).collect();
229
2
        assert_eq!(top, "╔═[■]═══════ Form ═══════════╗", "{name}");
230
2
        assert_eq!(
231
2
            buf[(2, 0)].style().fg,
232
2
            theme.style(Role::DialogFrame).fg,
233
            "{name}"
234
        );
235
2
        assert_eq!(
236
2
            buf[(3, 0)].style().fg,
237
2
            theme.style(Role::DialogIcon).fg,
238
            "{name}"
239
        );
240
2
        assert_eq!(
241
2
            buf[(3, 0)].style().bg,
242
2
            theme.style(Role::Dialog).bg,
243
            "{name}"
244
        );
245
2
        assert_eq!(
246
2
            buf[(4, 0)].style().fg,
247
2
            theme.style(Role::DialogFrame).fg,
248
            "{name}"
249
        );
250
2
        assert_eq!(buf[(25, 0)].symbol(), "═", "{name}: no zoom");
251
2
        assert_eq!(buf[(28, 6)].symbol(), "═", "{name}: no grow mark");
252
2
        assert_eq!(buf[(23, 0)].symbol(), "═", "{name}: no number");
253
    }
254
1
}
255

            
256
#[test]
257
1
fn the_close_area_is_the_icon_and_min_size_holds_the_buttons_and_a_body_row() {
258
1
    assert_eq!(
259
1
        Dialog::close_area(Rect::new(4, 2, 30, 7)),
260
1
        Some(Rect::new(6, 2, 3, 1))
261
    );
262
1
    assert_eq!(Dialog::close_area(Rect::new(0, 0, 11, 7)), None);
263
1
    let (width, height) = Dialog::min_size(&BUTTONS, ButtonRow::BottomRight);
264
1
    assert_eq!(height, 6);
265
1
    let buttons = Dialog::button_areas(
266
1
        Rect::new(0, 0, width, height),
267
1
        &BUTTONS,
268
1
        ButtonRow::BottomRight,
269
1
    );
270
2
    assert!(buttons.iter().all(|area| area.width > 0));
271
1
}
272

            
273
#[test]
274
1
fn min_size_is_one_column_wider_for_a_right_hand_row() {
275
1
    let row = Dialog::button_row_width(&BUTTONS);
276
1
    assert_eq!(Dialog::min_size(&BUTTONS, ButtonRow::Centred).0, row + 2);
277
1
    assert_eq!(
278
1
        Dialog::min_size(&BUTTONS, ButtonRow::BottomRight).0,
279
1
        row + 3
280
    );
281
1
}
282

            
283
#[test]
284
1
fn a_dragging_dialog_has_a_single_line_frame_and_its_icon_in_one_colour() {
285
1
    let theme = theme();
286
1
    let mut buf = Buffer::empty(Rect::new(0, 0, 30, 7));
287
1
    Dialog::new(&theme, "Form", &BUTTONS)
288
1
        .dragging(true)
289
1
        .render(Rect::new(0, 0, 30, 7), &mut buf);
290
1
    assert_eq!(buf[(0, 0)].symbol(), "┌");
291
1
    assert_eq!(buf[(2, 0)].symbol(), "[");
292
1
    assert_eq!(buf[(2, 0)].style().fg, theme.style(Role::DialogIcon).fg);
293
1
}
294

            
295
const FULL: Rect = Rect::new(0, 0, 100, 40);
296

            
297
#[test]
298
1
fn fitted_is_as_tall_as_its_rows_and_centred() {
299
1
    let area = Dialog::fitted((3, 2), &BUTTONS, ButtonRow::Centred, FULL);
300
1
    assert_eq!(area.height, 2 + 2 + Dialog::BUTTON_ROWS);
301
1
    assert_eq!(area.x, (FULL.width - area.width) / 2);
302
1
    assert_eq!(area.y, (FULL.height - area.height) / 2);
303
1
}
304

            
305
#[test]
306
1
fn fitted_follows_the_host_offset() {
307
1
    let host = Rect::new(10, 5, 100, 40);
308
1
    let area = Dialog::fitted((3, 2), &BUTTONS, ButtonRow::Centred, host);
309
1
    assert_eq!(area.x, host.x + (host.width - area.width) / 2);
310
1
    assert_eq!(area.y, host.y + (host.height - area.height) / 2);
311
1
}
312

            
313
#[test]
314
1
fn fitted_widens_to_a_long_line_and_stays_as_narrow_as_its_buttons_otherwise() {
315
1
    let wide = Dialog::fitted((80, 1), &BUTTONS, ButtonRow::Centred, FULL);
316
1
    assert_eq!(wide.width, 84);
317
1
    let short = Dialog::fitted((3, 1), &BUTTONS, ButtonRow::Centred, FULL);
318
1
    assert_eq!(short.width, 24);
319
1
}
320

            
321
#[test]
322
1
fn fitted_is_content_sized_on_a_wide_screen() {
323
1
    let screen = Rect::new(0, 0, 200, 50);
324
1
    let area = Dialog::fitted((10, 3), &BUTTONS, ButtonRow::Centred, screen);
325
1
    assert_eq!(area.width, 24);
326
1
}
327

            
328
#[test]
329
1
fn fitted_never_exceeds_the_host_even_when_it_is_too_small() {
330
1
    let tall = Dialog::fitted((3, 100), &BUTTONS, ButtonRow::Centred, FULL);
331
1
    assert_eq!(tall.height, FULL.height);
332
1
    let narrow = Rect::new(0, 0, 10, 40);
333
1
    let area = Dialog::fitted((3, 1), &BUTTONS, ButtonRow::Centred, narrow);
334
1
    assert_eq!(area.width, narrow.width);
335
1
    assert_eq!(area.x, 0);
336
1
}
337

            
338
#[test]
339
1
fn fitted_gives_a_right_hand_row_one_more_column() {
340
1
    let host = Rect::new(0, 0, 30, 40);
341
1
    let centred = Dialog::fitted((0, 1), &BUTTONS, ButtonRow::Centred, host);
342
1
    let right = Dialog::fitted((0, 1), &BUTTONS, ButtonRow::BottomRight, host);
343
1
    assert_eq!(centred.width, 24);
344
1
    assert_eq!(right.width, 25);
345
1
}