1
use super::wheel::line_intent;
2
use super::*;
3
use crate::form::{Field, Form, FormKind};
4
use crate::hits::Surface;
5
use crate::modal::Draft;
6
use crate::tabs::fetch::Fetch;
7
use crate::test_support::{area_of, draw_into, emacs_app, vim_app};
8
use crate::transport::RawEvent;
9
use crate::transport::tests_support::{ScriptedTransport, quit_keys};
10
use crate::view::Tab;
11
use crate::widgets::{Editor, SelectOption, SelectWidget, Widget};
12
use ratatui::crossterm::event::{KeyModifiers, MouseButton};
13

            
14
9
fn presets() -> [App; 3] {
15
9
    [crate::test_support::app(), emacs_app(), vim_app()]
16
9
}
17

            
18
56
fn event(kind: MouseEventKind) -> MouseEvent {
19
56
    MouseEvent {
20
56
        kind,
21
56
        column: 3,
22
56
        row: 3,
23
56
        modifiers: KeyModifiers::NONE,
24
56
    }
25
56
}
26

            
27
50
fn wheel(app: &mut App, up: bool) {
28
50
    let kind = if up {
29
19
        MouseEventKind::ScrollUp
30
    } else {
31
31
        MouseEventKind::ScrollDown
32
    };
33
50
    handle_mouse(app, event(kind), Instant::now());
34
50
}
35

            
36
19
fn accounts(mut app: App) -> App {
37
19
    app.switch_tab(Tab::Accounts);
38
760
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
39
19
    let rows: Vec<String> = (0..40).map(row).collect();
40
19
    app.accounts_mut()
41
19
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
42
19
    draw_into(&mut app, 80, 24);
43
19
    app
44
19
}
45

            
46
3
fn reports(mut app: App) -> App {
47
3
    app.switch_tab(Tab::Reports);
48
120
    app.reports_mut().state = Fetch::Loaded((0..40).map(|n| n.to_string()).collect());
49
3
    draw_into(&mut app, 80, 24);
50
3
    app
51
3
}
52

            
53
6
fn select_form(open: bool) -> Modal {
54
6
    let mut select = SelectWidget::new();
55
6
    select.set_options(
56
6
        ["alpha", "beta", "gamma"]
57
6
            .into_iter()
58
            .map(|name| SelectOption {
59
18
                id: name.to_string(),
60
18
                label: name.to_string(),
61
18
            })
62
            .collect(),
63
    );
64
6
    if open {
65
3
        select.open_list();
66
3
    }
67
6
    let note = Field {
68
6
        key: None,
69
6
        label: "note",
70
6
        widget: Widget::Text(Editor::default()),
71
6
    };
72
6
    let account = Field {
73
6
        key: None,
74
6
        label: "account",
75
6
        widget: Widget::Select(select),
76
6
    };
77
6
    Modal::Form(Form {
78
6
        fields: vec![account, note],
79
6
        focus: 0,
80
6
        kind: FormKind::ConfigSet,
81
6
        ..Form::config_set()
82
6
    })
83
6
}
84

            
85
9
fn focused_option(app: &App) -> Option<usize> {
86
9
    match app.overlays.top() {
87
9
        Some(Modal::Form(form)) => form.focused_select().map(SelectWidget::focused_in_filtered),
88
        _ => None,
89
    }
90
9
}
91

            
92
#[test]
93
1
fn the_wheel_moves_a_list_selection_one_row_per_event() {
94
1
    for app in presets() {
95
3
        let mut app = accounts(app);
96
3
        wheel(&mut app, false);
97
3
        wheel(&mut app, false);
98
3
        assert_eq!(app.accounts().selected, 2);
99
3
        wheel(&mut app, true);
100
3
        assert_eq!(app.accounts().selected, 1);
101
3
        wheel(&mut app, true);
102
3
        wheel(&mut app, true);
103
3
        assert_eq!(app.accounts().selected, 0);
104
    }
105
1
}
106

            
107
#[test]
108
1
fn the_wheel_scrolls_the_report_table_a_line() {
109
1
    for app in presets() {
110
3
        let mut app = reports(app);
111
3
        wheel(&mut app, false);
112
3
        wheel(&mut app, false);
113
3
        assert_eq!(app.reports().scroll.top(), 2);
114
3
        wheel(&mut app, true);
115
3
        assert_eq!(app.reports().scroll.top(), 1);
116
    }
117
1
}
118

            
119
#[test]
120
1
fn the_wheel_scrolls_a_draft_and_not_the_view_below() {
121
1
    for mut app in presets() {
122
3
        app = accounts(app);
123
180
        let lines = (0..60).map(|n| format!("line{n:<70}")).collect();
124
3
        app.overlays.push(Modal::Draft(Draft::new(lines)));
125
3
        draw_into(&mut app, 80, 10);
126
3
        wheel(&mut app, false);
127
3
        wheel(&mut app, false);
128
3
        wheel(&mut app, true);
129
3
        let Some(Modal::Draft(draft)) = app.overlays.top() else {
130
            panic!("the draft is not open");
131
        };
132
3
        assert_eq!(draft.scroll.top(), 1);
133
3
        assert_eq!(app.accounts().selected, 0);
134
    }
135
1
}
136

            
137
2
fn wheel_over_body(app: &mut App) {
138
2
    let body = area_of(app, Target::Body(Surface::Modal));
139
2
    wheel_at(app, Position::new(body.x + 1, body.y), false);
140
2
}
141

            
142
#[test]
143
1
fn the_wheel_scrolls_help_and_draft_overlays() {
144
40
    let lines: Vec<String> = (0..40).map(|n| n.to_string()).collect();
145
1
    let mut app = crate::test_support::app();
146
1
    app.overlays.push(Modal::Draft(Draft::new(lines.clone())));
147
1
    draw_into(&mut app, 80, 10);
148
1
    wheel_over_body(&mut app);
149
1
    let Some(Modal::Draft(help)) = app.overlays.top() else {
150
        panic!("help is not open");
151
    };
152
1
    assert_eq!(help.scroll.top(), 1);
153
1
    let mut app = crate::test_support::app();
154
1
    app.overlays.push(Modal::Draft(Draft::new(lines)));
155
1
    draw_into(&mut app, 80, 10);
156
1
    wheel_over_body(&mut app);
157
1
    let Some(Modal::Draft(draft)) = app.overlays.top() else {
158
        panic!("draft is not open");
159
    };
160
1
    assert_eq!(draft.scroll.top(), 1);
161
1
}
162

            
163
/// The middle of the first option the open drop-down drew.
164
3
fn over_options(app: &mut App) -> Position {
165
3
    draw_into(app, 80, 24);
166
3
    let area = area_of(app, Target::Option(0));
167
3
    Position::new(area.x + area.width / 2, area.y)
168
3
}
169

            
170
20
fn wheel_at(app: &mut App, position: Position, up: bool) {
171
20
    let kind = if up {
172
3
        MouseEventKind::ScrollUp
173
    } else {
174
17
        MouseEventKind::ScrollDown
175
    };
176
20
    let mouse = MouseEvent {
177
20
        kind,
178
20
        column: position.x,
179
20
        row: position.y,
180
20
        modifiers: KeyModifiers::NONE,
181
20
    };
182
20
    handle_mouse(app, mouse, Instant::now());
183
20
}
184

            
185
#[test]
186
1
fn the_wheel_steps_the_options_of_an_open_drop_down() {
187
1
    for mut app in presets() {
188
3
        app.overlays.push(select_form(true));
189
3
        let at = over_options(&mut app);
190
3
        wheel_at(&mut app, at, false);
191
3
        wheel_at(&mut app, at, false);
192
3
        assert_eq!(focused_option(&app), Some(2));
193
3
        wheel_at(&mut app, at, true);
194
3
        assert_eq!(focused_option(&app), Some(1));
195
    }
196
1
}
197

            
198
#[test]
199
1
fn a_form_without_an_open_drop_down_ignores_the_wheel() {
200
1
    for mut app in presets() {
201
3
        app.overlays.push(select_form(false));
202
3
        draw_into(&mut app, 80, 24);
203
3
        wheel(&mut app, false);
204
3
        assert_eq!(focused_option(&app), Some(0));
205
    }
206
1
}
207

            
208
#[test]
209
1
fn the_line_intent_per_surface() {
210
1
    for app in presets() {
211
18
        let line = |surface, up| line_intent(&app, surface, None, up);
212
3
        assert_eq!(line(Surface::List, false), Some(Intent::ListSelectNext));
213
3
        assert_eq!(line(Surface::List, true), Some(Intent::ListSelectPrev));
214
3
        assert_eq!(line(Surface::Pane, false), Some(Intent::ScrollLineDown));
215
3
        assert_eq!(line(Surface::Modal, true), Some(Intent::ScrollLineUp));
216
3
        assert_eq!(line(Surface::Options, false), Some(Intent::SelectNext));
217
3
        assert_eq!(line(Surface::Options, true), Some(Intent::SelectPrev));
218
    }
219
1
}
220

            
221
2
fn console(focused: bool) -> App {
222
2
    let mut app = crate::test_support::app();
223
2
    app.switch_tab(Tab::Console);
224
2
    app.set_console_focused(focused);
225
2
    for i in 0..30 {
226
60
        app.console_mut().push_scrollback(format!("line {i}"));
227
    }
228
2
    draw_into(&mut app, 80, 24);
229
2
    app
230
2
}
231

            
232
#[test]
233
1
fn the_wheel_scrolls_the_console_scrollback_focused_or_not() {
234
1
    for focused in [true, false] {
235
2
        let mut app = console(focused);
236
2
        wheel(&mut app, true);
237
2
        wheel(&mut app, true);
238
2
        assert_eq!(app.console().scroll, 2, "focused {focused}");
239
2
        wheel(&mut app, false);
240
2
        assert_eq!(app.console().scroll, 1, "focused {focused}");
241
    }
242
1
}
243

            
244
#[test]
245
1
fn the_wheel_scrolls_the_list_under_the_pointer_while_the_command_line_is_open() {
246
1
    for app in presets() {
247
3
        let mut app = accounts(app);
248
3
        app.open_command_line();
249
3
        draw_into(&mut app, 80, 24);
250
3
        wheel(&mut app, false);
251
3
        wheel(&mut app, false);
252
3
        assert_eq!(app.accounts().selected, 2);
253
3
        assert!(app.cmdline.active);
254
3
        assert!(app.cmdline.editor.buffer().is_empty());
255
    }
256
1
}
257

            
258
#[test]
259
1
fn the_wheel_over_the_list_header_scrolls_the_list() {
260
1
    for app in presets() {
261
3
        let mut app = accounts(app);
262
3
        wheel_at(&mut app, Position::new(3, 2), false);
263
3
        assert_eq!(app.accounts().selected, 1);
264
    }
265
1
}
266

            
267
#[test]
268
1
fn the_wheel_over_the_status_bar_or_the_menu_bar_does_nothing() {
269
1
    for app in presets() {
270
3
        let mut app = accounts(app);
271
3
        wheel_at(&mut app, Position::new(3, 23), false);
272
3
        wheel_at(&mut app, Position::new(3, 0), false);
273
3
        assert_eq!(app.accounts().selected, 0);
274
    }
275
1
}
276

            
277
#[test]
278
1
fn the_wheel_does_nothing_in_a_confirm_or_over_the_open_menu() {
279
1
    let mut app = accounts(crate::test_support::app());
280
1
    app.open_menu();
281
1
    draw_into(&mut app, 80, 24);
282
1
    wheel(&mut app, false);
283
1
    assert_eq!(app.accounts().selected, 0);
284
1
    let mut app = accounts(crate::test_support::app());
285
1
    app.overlays.push(Modal::Confirm {
286
1
        prompt: "sure?".to_string(),
287
1
        action: crate::modal::ConfirmAction::DeleteTransaction("t".to_string()),
288
1
        focus: None,
289
1
    });
290
1
    draw_into(&mut app, 80, 24);
291
1
    wheel(&mut app, false);
292
1
    assert_eq!(app.accounts().selected, 0);
293
1
}
294

            
295
#[test]
296
1
fn releases_drags_and_moves_change_nothing() {
297
1
    let mut app = accounts(crate::test_support::app());
298
1
    for kind in [
299
1
        MouseEventKind::Up(MouseButton::Left),
300
1
        MouseEventKind::Drag(MouseButton::Left),
301
1
        MouseEventKind::Moved,
302
1
        MouseEventKind::ScrollLeft,
303
1
        MouseEventKind::ScrollRight,
304
1
    ] {
305
5
        handle_mouse(&mut app, event(kind), Instant::now());
306
    }
307
1
    assert_eq!(app.accounts().selected, 0);
308
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
309
1
}
310

            
311
#[test]
312
1
fn a_transport_wheel_event_reaches_the_same_path() {
313
1
    let wheel_down = RawEvent::Mouse(event(MouseEventKind::ScrollDown), std::time::Instant::now());
314
1
    let events = [vec![wheel_down, wheel_down], quit_keys().into()].concat();
315
1
    let mut app = accounts(crate::test_support::app());
316
1
    let mut transport = ScriptedTransport::new(80, 24, events);
317
1
    crate::runtime::run_loop(&mut transport, &mut app).expect("the loop runs");
318
1
    assert_eq!(app.accounts().selected, 2);
319
1
}