1
//! Tab walks a form's fields and then its buttons; Enter presses the focused button.
2

            
3
use super::tests::{alt, key, press};
4
use crate::app::App;
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use ratatui::crossterm::event::KeyCode;
8

            
9
8
fn form_app() -> App {
10
8
    let mut app = crate::test_support::app();
11
8
    app.open_dialog_on_front(Modal::Form(Form::config_set()));
12
8
    app
13
8
}
14

            
15
6
fn on_button(app: &mut App, button: usize) {
16
6
    for _ in 0..2 + button {
17
15
        press(app, key(KeyCode::Tab));
18
    }
19
6
}
20

            
21
4
fn focused_button(app: &App) -> Option<usize> {
22
4
    match app.overlays.top() {
23
4
        Some(Modal::Form(form)) => form.focused_button(),
24
        _ => None,
25
    }
26
4
}
27

            
28
#[test]
29
1
fn tab_moves_from_the_last_field_to_ok_then_close_then_the_first_field() {
30
1
    let mut app = form_app();
31
1
    on_button(&mut app, 0);
32
1
    assert_eq!(focused_button(&app), Some(0));
33
1
    press(&mut app, key(KeyCode::Tab));
34
1
    assert_eq!(focused_button(&app), Some(1));
35
1
    press(&mut app, key(KeyCode::Tab));
36
1
    assert_eq!(focused_button(&app), None);
37
1
}
38

            
39
#[test]
40
1
fn shift_tab_from_the_first_field_lands_on_close() {
41
1
    let mut app = form_app();
42
1
    press(&mut app, key(KeyCode::BackTab));
43
1
    assert_eq!(focused_button(&app), Some(1));
44
1
}
45

            
46
#[test]
47
1
fn enter_on_close_closes_a_clean_form() {
48
1
    let mut app = form_app();
49
1
    on_button(&mut app, 1);
50
1
    press(&mut app, key(KeyCode::Enter));
51
1
    assert!(app.overlays.top().is_none());
52
1
}
53

            
54
#[test]
55
1
fn enter_on_ok_submits_the_form() {
56
1
    let mut app = form_app();
57
1
    on_button(&mut app, 0);
58
1
    press(&mut app, key(KeyCode::Enter));
59
1
    assert_eq!(app.status, "console not connected");
60
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
61
1
}
62

            
63
#[test]
64
1
fn typing_on_a_button_changes_no_field() {
65
1
    let mut app = form_app();
66
1
    on_button(&mut app, 0);
67
1
    press(&mut app, key(KeyCode::Char('x')));
68
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
69
        panic!("the form is open");
70
    };
71
2
    assert!(form.fields.iter().all(|f| !f.widget.value().contains('x')));
72
1
}
73

            
74
#[test]
75
1
fn alt_o_submits_even_with_close_focused() {
76
1
    let mut app = form_app();
77
1
    on_button(&mut app, 1);
78
1
    press(&mut app, alt(KeyCode::Char('o')));
79
1
    assert_eq!(app.status, "console not connected");
80
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
81
1
}
82

            
83
#[test]
84
1
fn enter_on_a_field_still_submits() {
85
1
    let mut app = form_app();
86
1
    press(&mut app, key(KeyCode::Enter));
87
1
    assert_eq!(app.status, "console not connected");
88
1
}
89

            
90
#[test]
91
1
fn the_default_button_loses_its_emphasis_while_another_has_the_focus() {
92
    use tvui::Role;
93
1
    let mut app = form_app();
94
1
    on_button(&mut app, 1);
95
    let mut terminal =
96
1
        ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).expect("test terminal");
97
1
    terminal
98
1
        .draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
99
1
        .expect("draw");
100
1
    let buffer = terminal.backend().buffer();
101
1
    let ok = app.theme.style(Role::ButtonDefault).bg;
102
1
    let seen = buffer
103
1
        .content()
104
1
        .iter()
105
1920
        .any(|cell| cell.bg == ok.unwrap_or_default());
106
1
    assert!(!seen, "no cell keeps the default button background");
107
1
}