1
//! Left clicks act on what the last frame drew, under every key preset.
2

            
3
use super::button_tests::click_button;
4
use super::*;
5
use crate::app::MenuId;
6
use crate::form::Form;
7
use crate::hits::Target;
8
use crate::modal::Draft;
9
use crate::test_support::{area_of, emacs_app, redraw, vim_app};
10
use crate::view::Tab;
11
use ratatui::crossterm::event::{KeyModifiers, MouseEvent};
12
use std::time::Duration;
13

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

            
18
12
fn config(mut app: App) -> App {
19
12
    app.switch_tab(Tab::Config);
20
12
    let rows = [("a", "1"), ("b", "2"), ("c", "3")];
21
36
    let row = |(key, value): &(&str, &str)| format!(r#"(:KEY "{key}" :VALUE "{value}")"#);
22
12
    let rows: Vec<String> = rows.iter().map(row).collect();
23
12
    app.config_mut()
24
12
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
25
12
    app
26
12
}
27

            
28
27
fn accounts(mut app: App) -> App {
29
27
    app.switch_tab(Tab::Accounts);
30
81
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
31
27
    let rows: Vec<String> = (0..3).map(row).collect();
32
27
    app.accounts_mut()
33
27
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
34
27
    app
35
27
}
36

            
37
386
pub(super) fn press(app: &mut App, position: Position, kind: MouseEventKind, at: Instant) {
38
386
    let mouse = MouseEvent {
39
386
        kind,
40
386
        column: position.x,
41
386
        row: position.y,
42
386
        modifiers: KeyModifiers::NONE,
43
386
    };
44
386
    handle_mouse(app, mouse, at);
45
386
}
46

            
47
/// Draws the frame, then clicks the left button in the middle of `target`'s area.
48
229
pub(super) fn click_on(app: &mut App, target: Target, at: Instant) {
49
229
    redraw(app);
50
229
    let area = area_of(app, target);
51
229
    let position = Position::new(area.x + area.width / 2, area.y);
52
229
    press(app, position, MouseEventKind::Down(MouseButton::Left), at);
53
229
}
54

            
55
288
pub(super) fn now() -> Instant {
56
288
    Instant::now()
57
288
}
58

            
59
6
fn form_focus(app: &App) -> Option<usize> {
60
6
    match app.overlays.top() {
61
6
        Some(Modal::Form(form)) => Some(form.focus),
62
        _ => None,
63
    }
64
6
}
65

            
66
#[test]
67
1
fn a_click_on_the_menu_title_opens_the_menu_and_another_closes_it() {
68
1
    for mut app in presets() {
69
3
        click_on(&mut app, Target::MenuTitle(0), now());
70
3
        assert!(app.menu.is_some());
71
3
        click_on(&mut app, Target::MenuTitle(0), now());
72
3
        assert!(app.menu.is_none());
73
    }
74
1
}
75

            
76
#[test]
77
1
fn a_click_on_a_title_opens_its_menu_a_click_on_the_other_switches_and_the_same_closes() {
78
1
    for mut app in presets() {
79
3
        click_on(&mut app, Target::MenuTitle(1), now());
80
3
        assert_eq!(app.menu_title(), Some(MenuId::Window));
81
3
        click_on(&mut app, Target::MenuTitle(0), now());
82
3
        assert_eq!(app.menu_title(), Some(MenuId::View));
83
3
        click_on(&mut app, Target::MenuTitle(0), now());
84
3
        assert!(app.menu.is_none());
85
    }
86
1
}
87

            
88
#[test]
89
1
fn a_click_on_a_title_opens_no_menu_where_open_menu_does_not_act() {
90
1
    for mut app in presets() {
91
3
        app.open_command_line();
92
3
        click_on(&mut app, Target::MenuTitle(1), now());
93
3
        assert!(app.menu.is_none());
94
3
        app.close_command_line();
95
3
        app.overlays.push(Modal::Form(Form::config_set()));
96
3
        click_on(&mut app, Target::MenuTitle(1), now());
97
3
        assert!(app.menu.is_none());
98
    }
99
1
}
100

            
101
#[test]
102
1
fn a_click_on_a_window_menu_item_applies_it() {
103
1
    for mut app in presets() {
104
3
        app.open_menu_of(MenuId::Window);
105
3
        click_on(&mut app, Target::MenuItem(4), now());
106
3
        assert_eq!(app.front_kind(), Some(Tab::Transactions));
107
3
        assert!(app.menu.is_none());
108
    }
109
1
}
110

            
111
#[test]
112
1
fn a_click_on_a_menu_item_switches_to_that_view() {
113
5
    let index = Tab::ALL.iter().position(|tab| *tab == Tab::Config);
114
1
    for mut app in presets() {
115
3
        app.open_menu();
116
3
        click_on(&mut app, Target::MenuItem(index.unwrap_or_default()), now());
117
3
        assert_eq!(app.front_kind(), Some(Tab::Config));
118
3
        assert!(app.menu.is_none());
119
    }
120
1
}
121

            
122
#[test]
123
1
fn a_click_outside_the_open_menu_closes_it_and_reaches_nothing_below() {
124
1
    for app in presets() {
125
3
        let mut app = accounts(app);
126
3
        redraw(&mut app);
127
3
        let row = area_of(&app, Target::Row(2));
128
3
        app.open_menu();
129
3
        redraw(&mut app);
130
3
        let at = Position::new(row.x, row.y);
131
3
        press(&mut app, at, MouseEventKind::Down(MouseButton::Left), now());
132
3
        assert!(app.menu.is_none());
133
3
        assert_eq!(app.accounts().selected, 0);
134
    }
135
1
}
136

            
137
#[test]
138
1
fn a_click_on_a_row_selects_it() {
139
1
    for app in presets() {
140
3
        let mut app = accounts(app);
141
3
        click_on(&mut app, Target::Row(2), now());
142
3
        assert_eq!(app.accounts().selected, 2);
143
3
        click_on(&mut app, Target::Row(1), now());
144
3
        assert_eq!(app.accounts().selected, 1);
145
    }
146
1
}
147

            
148
12
fn double_click(app: &mut App, first: Target, second: Target, gap: Duration) {
149
12
    let start = now();
150
12
    click_on(app, first, start);
151
12
    click_on(app, second, start + gap);
152
12
}
153

            
154
#[test]
155
1
fn two_quick_clicks_on_a_row_do_what_enter_does_there() {
156
1
    for app in presets() {
157
3
        let mut app = config(app);
158
3
        double_click(
159
3
            &mut app,
160
3
            Target::Row(1),
161
3
            Target::Row(1),
162
3
            Duration::from_millis(100),
163
3
        );
164
3
        assert_eq!(app.config().selected, 1);
165
3
        assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
166
3
        assert!(app.last_click.is_none());
167
    }
168
1
}
169

            
170
#[test]
171
1
fn two_slow_clicks_on_a_row_only_select_it() {
172
1
    for app in presets() {
173
3
        let mut app = config(app);
174
3
        double_click(
175
3
            &mut app,
176
3
            Target::Row(1),
177
3
            Target::Row(1),
178
3
            Duration::from_millis(900),
179
3
        );
180
3
        assert_eq!(app.config().selected, 1);
181
3
        assert!(app.overlays.top().is_none());
182
    }
183
1
}
184

            
185
#[test]
186
1
fn two_quick_clicks_on_different_rows_are_two_selections() {
187
1
    for app in presets() {
188
3
        let mut app = config(app);
189
3
        double_click(
190
3
            &mut app,
191
3
            Target::Row(0),
192
3
            Target::Row(2),
193
3
            Duration::from_millis(100),
194
3
        );
195
3
        assert_eq!(app.config().selected, 2);
196
3
        assert!(app.overlays.top().is_none());
197
    }
198
1
}
199

            
200
#[test]
201
1
fn a_third_quick_click_is_not_a_second_double_click() {
202
1
    let mut app = config(crate::test_support::app());
203
1
    let start = now();
204
1
    click_on(&mut app, Target::Row(1), start);
205
1
    click_on(&mut app, Target::Row(1), start + Duration::from_millis(50));
206
1
    app.overlays.pop();
207
1
    click_on(&mut app, Target::Row(1), start + Duration::from_millis(100));
208
1
    assert!(app.overlays.top().is_none());
209
1
}
210

            
211
#[test]
212
1
fn a_click_elsewhere_between_two_clicks_on_a_row_breaks_the_double() {
213
1
    let mut app = config(crate::test_support::app());
214
1
    let start = now();
215
1
    click_on(&mut app, Target::Row(1), start);
216
1
    click_on(
217
1
        &mut app,
218
1
        Target::MenuTitle(0),
219
1
        start + Duration::from_millis(50),
220
1
    );
221
1
    click_on(
222
1
        &mut app,
223
1
        Target::MenuTitle(0),
224
1
        start + Duration::from_millis(80),
225
1
    );
226
1
    click_on(&mut app, Target::Row(1), start + Duration::from_millis(100));
227
1
    assert!(app.overlays.top().is_none());
228
1
}
229

            
230
#[test]
231
1
fn a_status_hint_applies_its_intent() {
232
1
    for app in presets() {
233
3
        let mut app = accounts(app);
234
3
        click_on(&mut app, Target::Hint(Intent::OpenMenu), now());
235
3
        assert!(app.menu.is_some());
236
    }
237
1
}
238

            
239
#[test]
240
1
fn a_click_in_a_field_focuses_it_by_label_or_value() {
241
1
    for mut app in presets() {
242
3
        app.overlays.push(Modal::Form(Form::transaction_create()));
243
3
        click_on(&mut app, Target::Field(2), now());
244
3
        assert_eq!(form_focus(&app), Some(2));
245
3
        let area = area_of(&app, Target::Field(1));
246
3
        let label = Position::new(area.x, area.y);
247
3
        press(
248
3
            &mut app,
249
3
            label,
250
3
            MouseEventKind::Down(MouseButton::Left),
251
3
            now(),
252
3
        );
253
3
        assert_eq!(form_focus(&app), Some(1));
254
    }
255
1
}
256

            
257
#[test]
258
1
fn the_ok_button_submits_and_close_closes_as_their_intents_do() {
259
1
    let makers: [fn() -> App; 3] = [crate::test_support::app, emacs_app, vim_app];
260
1
    for make in makers {
261
3
        let (mut clicked, mut keyed) = (make(), make());
262
3
        for app in [&mut clicked, &mut keyed] {
263
6
            app.overlays.push(Modal::Form(Form::config_set()));
264
        }
265
3
        click_button(&mut clicked, 0, now());
266
3
        apply(&mut keyed, Intent::Submit);
267
3
        assert!(!clicked.status.is_empty());
268
3
        assert_eq!(clicked.status, keyed.status);
269
3
        assert!(matches!(clicked.overlays.top(), Some(Modal::Form(_))));
270
    }
271
1
    let mut app = crate::test_support::app();
272
1
    app.overlays.push(Modal::Form(Form::config_set()));
273
1
    click_button(&mut app, 1, now());
274
1
    assert!(app.overlays.top().is_none());
275
1
}
276

            
277
6
fn confirm_over_a_form(app: &mut App) {
278
6
    app.overlays.push(Modal::Form(Form::config_set()));
279
6
    app.overlays.push(Modal::Confirm {
280
6
        prompt: "Discard changes?".to_string(),
281
6
        action: crate::modal::ConfirmAction::DiscardForm,
282
6
        focus: None,
283
6
    });
284
6
}
285

            
286
#[test]
287
1
fn yes_confirms_and_no_keeps_the_form() {
288
1
    for mut app in presets() {
289
3
        confirm_over_a_form(&mut app);
290
3
        click_button(&mut app, 0, now());
291
3
        assert!(app.overlays.top().is_none());
292
    }
293
1
    for mut app in presets() {
294
3
        confirm_over_a_form(&mut app);
295
3
        click_button(&mut app, 1, now());
296
3
        assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
297
    }
298
1
}
299

            
300
#[test]
301
1
fn a_click_outside_a_modal_does_nothing() {
302
1
    for app in presets() {
303
3
        let mut app = accounts(app);
304
3
        redraw(&mut app);
305
3
        let row = area_of(&app, Target::Row(2));
306
3
        crate::test_support::open_overlay(&mut app);
307
3
        redraw(&mut app);
308
3
        let at = Position::new(row.x, row.y);
309
3
        press(&mut app, at, MouseEventKind::Down(MouseButton::Left), now());
310
3
        assert_eq!(app.accounts().selected, 0);
311
3
        assert!(matches!(app.overlays.top(), Some(Modal::Confirm { .. })));
312
    }
313
1
}
314

            
315
#[test]
316
1
fn only_the_left_press_clicks() {
317
1
    let mut app = accounts(crate::test_support::app());
318
1
    redraw(&mut app);
319
1
    let row = area_of(&app, Target::Row(2));
320
1
    let at = Position::new(row.x, row.y);
321
1
    for kind in [
322
1
        MouseEventKind::Down(MouseButton::Right),
323
1
        MouseEventKind::Down(MouseButton::Middle),
324
1
        MouseEventKind::Up(MouseButton::Left),
325
1
        MouseEventKind::Drag(MouseButton::Left),
326
1
    ] {
327
4
        press(&mut app, at, kind, now());
328
    }
329
1
    assert_eq!(app.accounts().selected, 0);
330
1
}
331

            
332
#[test]
333
1
fn a_click_on_nothing_selects_nothing() {
334
1
    let mut app = accounts(crate::test_support::app());
335
1
    redraw(&mut app);
336
1
    press(
337
1
        &mut app,
338
1
        Position::new(79, 12),
339
1
        MouseEventKind::Down(MouseButton::Left),
340
1
        now(),
341
1
    );
342
1
    assert_eq!(app.accounts().selected, 0);
343
1
}
344

            
345
#[test]
346
1
fn selecting_a_row_the_list_does_not_hold_changes_nothing() {
347
1
    let mut app = config(accounts(crate::test_support::app()));
348
1
    app.accounts_mut().select_row(1);
349
1
    app.accounts_mut().select_row(3);
350
1
    app.config_mut().select_row(2);
351
1
    app.config_mut().select_row(3);
352
1
    assert_eq!((app.accounts().selected, app.config().selected), (1, 2));
353
1
    let mut empty = crate::test_support::app();
354
1
    empty.accounts_mut().select_row(0);
355
1
    assert_eq!(empty.accounts().selected, 0);
356
1
}
357

            
358
#[test]
359
1
fn focusing_a_field_moves_the_focus_and_ignores_an_index_past_the_fields() {
360
1
    let mut form = Form::transaction_create();
361
1
    let fields = form.fields.len();
362
1
    form.focus_field(1);
363
1
    form.focus_field(fields);
364
1
    assert_eq!(form.focus, 1);
365
1
    form.focus_field(0);
366
1
    assert_eq!(form.focus, 0);
367
1
}
368

            
369
#[test]
370
1
fn a_hint_under_a_modal_is_still_live() {
371
1
    for mut app in presets() {
372
12
        let lines: Vec<String> = (0..4).map(|n| n.to_string()).collect();
373
3
        app.overlays.push(Modal::Draft(Draft::new(lines)));
374
3
        click_on(&mut app, Target::Hint(Intent::CloseTopmost), now());
375
3
        assert!(app.overlays.top().is_none());
376
    }
377
1
}
378

            
379
#[test]
380
1
fn row_clicks_with_the_command_line_open_select_the_row_and_submit_nothing() {
381
1
    for app in presets() {
382
3
        let mut app = accounts(app);
383
3
        app.open_command_line();
384
3
        for c in "help".chars() {
385
12
            apply(&mut app, Intent::InsertChar(c));
386
        }
387
3
        let start = now();
388
3
        click_on(&mut app, Target::Row(2), start);
389
3
        click_on(&mut app, Target::Row(2), start + Duration::from_millis(50));
390
3
        assert!(app.cmdline.active);
391
3
        assert_eq!(app.cmdline.editor.buffer(), "help");
392
3
        assert_eq!(app.accounts().selected, 2);
393
3
        assert!(app.status.is_empty(), "{}", app.status);
394
    }
395
1
}
396

            
397
#[test]
398
1
fn a_double_click_on_an_account_does_what_the_key_for_its_transactions_does() {
399
1
    for make in [crate::test_support::app, emacs_app, vim_app] {
400
3
        let (mut clicked, mut keyed) = (accounts(make()), accounts(make()));
401
3
        double_click(
402
3
            &mut clicked,
403
3
            Target::Row(1),
404
3
            Target::Row(1),
405
3
            Duration::from_millis(100),
406
3
        );
407
3
        keyed.accounts_mut().select_row(1);
408
3
        apply(&mut keyed, Intent::ListAccountTransactions);
409
6
        let seen = |app: &App| {
410
6
            (
411
6
                app.front_kind(),
412
6
                app.status.clone(),
413
6
                app.accounts().selected,
414
6
            )
415
6
        };
416
3
        assert_eq!(seen(&clicked), seen(&keyed));
417
3
        assert!(!clicked.front_is(Tab::Accounts) || !clicked.status.is_empty());
418
    }
419
1
}
420

            
421
#[test]
422
1
fn enter_is_bound_on_accounts_and_commodities_and_shown() {
423
1
    for mut app in presets() {
424
3
        for (tab, intent) in [
425
3
            (Tab::Accounts, Intent::ListAccountTransactions),
426
3
            (Tab::Commodities, Intent::EditRow),
427
3
        ] {
428
6
            app.switch_tab(tab);
429
6
            assert_eq!(crate::keymap::in_view(&app).bound("Enter"), Some(intent));
430
6
            let hint = crate::keymap::hinted(&app)
431
6
                .into_iter()
432
19
                .find(|hint| hint.intent == intent);
433
6
            let shown = hint.as_ref().map(|hint| hint.key.contains("Enter"));
434
6
            assert!(shown.is_some(), "{tab:?}");
435
6
            assert!(
436
6
                shown == Some(true) || tab == Tab::Commodities,
437
                "{tab:?} {hint:?}"
438
            );
439
6
            let help = crate::keymap::help_lines(&app).join("\n");
440
6
            assert!(help.contains("Enter"), "{tab:?}");
441
        }
442
    }
443
1
}
444

            
445
#[test]
446
1
fn a_scroll_between_two_clicks_makes_the_second_a_different_row() {
447
1
    let mut app = crate::test_support::app();
448
1
    app.switch_tab(Tab::Accounts);
449
60
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
450
1
    let rows: Vec<String> = (0..60).map(row).collect();
451
1
    app.accounts_mut()
452
1
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
453
1
    let start = now();
454
1
    click_on(&mut app, Target::Row(0), start);
455
1
    let first = area_of(&app, Target::Row(0));
456
1
    for _ in 0..40 {
457
40
        press(
458
40
            &mut app,
459
40
            Position::new(first.x, first.y),
460
40
            MouseEventKind::ScrollDown,
461
40
            start,
462
40
        );
463
    }
464
1
    redraw(&mut app);
465
1
    let again = Position::new(first.x, first.y);
466
1
    press(
467
1
        &mut app,
468
1
        again,
469
1
        MouseEventKind::Down(MouseButton::Left),
470
1
        start + Duration::from_millis(50),
471
1
    );
472
1
    assert_ne!(app.accounts().selected, 0);
473
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
474
1
}
475

            
476
#[test]
477
1
fn a_click_and_the_wheel_do_nothing_in_the_size_move_mode() {
478
1
    for app in presets() {
479
3
        let mut app = accounts(app);
480
3
        redraw(&mut app);
481
3
        let row = area_of(&app, Target::Row(2));
482
3
        app.begin_size_move();
483
3
        let at = Position::new(row.x, row.y);
484
3
        press(&mut app, at, MouseEventKind::Down(MouseButton::Left), now());
485
3
        assert_eq!(app.accounts().selected, 0);
486
3
        press(&mut app, at, MouseEventKind::ScrollDown, now());
487
3
        assert_eq!(app.accounts().selected, 0);
488
3
        assert!(app.dragging());
489
    }
490
1
}