1
//! Clicks on grid cells and on the rows of an open drop-down, under every key preset.
2

            
3
use super::click_tests::{click_on, now, presets, press};
4
use super::*;
5
use crate::form::Form;
6
use crate::test_support::{area_of, draw_into, redraw};
7
use crate::widgets::grid::CellId;
8
use crate::widgets::splits::{COL_FROM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE};
9
use crate::widgets::{FocusedSubWidget, SelectOption, SelectWidget, SplitsWidget, Widget};
10
use std::time::Duration;
11
use tvui::BarPart;
12

            
13
const SPLITS: usize = 3;
14

            
15
87
fn options(n: usize) -> Vec<SelectOption> {
16
87
    (0..n)
17
        .map(|i| SelectOption {
18
603
            id: format!("id{i}"),
19
603
            label: format!("label{i}"),
20
603
        })
21
        .collect()
22
87
}
23

            
24
33
fn cell(col: usize) -> Target {
25
33
    let cell = CellId {
26
33
        tag: false,
27
33
        row: 0,
28
33
        col,
29
33
    };
30
33
    Target::Cell {
31
33
        field: SPLITS,
32
33
        cell,
33
33
    }
34
33
}
35

            
36
42
fn transaction(mut app: App, n: usize) -> App {
37
42
    let mut form = Form::transaction_create();
38
42
    if let Some(Widget::Splits(splits)) = form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
39
42
        splits.set_account_options(options(n));
40
42
        splits.set_commodity_options(options(n));
41
    }
42
42
    app.overlays.push(Modal::Form(form));
43
42
    app
44
42
}
45

            
46
57
fn splits(app: &mut App) -> Option<&mut SplitsWidget> {
47
57
    match app.overlays.top_mut() {
48
57
        Some(Modal::Form(form)) => match form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
49
57
            Some(Widget::Splits(splits)) => Some(splits),
50
            _ => None,
51
        },
52
        _ => None,
53
    }
54
57
}
55

            
56
105
fn form(app: &App) -> &Form {
57
105
    match app.overlays.top() {
58
105
        Some(Modal::Form(form)) => form,
59
        _ => panic!("no form is open"),
60
    }
61
105
}
62

            
63
48
fn open(app: &App) -> bool {
64
48
    form(app).focused_select().is_some_and(|select| select.open)
65
48
}
66

            
67
9
fn value(app: &App) -> Option<String> {
68
9
    form(app).focused_select().map(|s| s.value().to_string())
69
9
}
70

            
71
9
fn middle(rect: Rect) -> Position {
72
9
    Position::new(rect.x + rect.width / 2, rect.y)
73
9
}
74

            
75
#[test]
76
1
fn a_click_on_a_cell_focuses_it_without_opening_a_select() {
77
1
    for app in presets() {
78
3
        let mut app = transaction(app, 3);
79
3
        click_on(&mut app, cell(COL_TO), now());
80
3
        assert!(form(&app).cell_focused(SPLITS, cell_id(COL_TO)));
81
3
        assert!(!open(&app));
82
    }
83
1
}
84

            
85
21
fn cell_id(col: usize) -> CellId {
86
21
    CellId {
87
21
        tag: false,
88
21
        row: 0,
89
21
        col,
90
21
    }
91
21
}
92

            
93
#[test]
94
1
fn a_click_on_the_focused_select_cell_opens_it() {
95
1
    for app in presets() {
96
3
        let mut app = transaction(app, 3);
97
3
        let start = now();
98
3
        click_on(&mut app, cell(COL_TO), start);
99
3
        click_on(&mut app, cell(COL_TO), start + Duration::from_secs(2));
100
3
        assert!(open(&app));
101
    }
102
1
}
103

            
104
#[test]
105
1
fn a_click_on_a_cell_leaves_a_list_that_was_closed_closed_on_another_cell() {
106
1
    for app in presets() {
107
3
        let mut app = transaction(app, 3);
108
3
        let start = now();
109
3
        click_on(&mut app, cell(COL_TO), start);
110
3
        click_on(&mut app, cell(COL_FROM), start + Duration::from_secs(2));
111
3
        assert!(form(&app).cell_focused(SPLITS, cell_id(COL_FROM)));
112
3
        assert!(!open(&app));
113
    }
114
1
}
115

            
116
#[test]
117
1
fn a_quick_second_click_is_tested_against_the_cell_the_first_one_hit() {
118
1
    for app in presets() {
119
3
        let mut app = transaction(app, 3);
120
3
        focus_splits(&mut app, COL_TO_AMOUNT);
121
3
        draw_into(&mut app, 70, 24);
122
3
        let first = middle(area_of(&app, cell(COL_TO_COMM)));
123
3
        let start = now();
124
3
        press(
125
3
            &mut app,
126
3
            first,
127
3
            MouseEventKind::Down(MouseButton::Left),
128
3
            start,
129
3
        );
130
3
        draw_into(&mut app, 70, 24);
131
3
        let under = app.hits.hit_at(first).map(|(_, target, _)| target);
132
3
        assert_ne!(under, Some(cell(COL_TO_COMM)), "the grid did not shift");
133
3
        let later = start + Duration::from_millis(100);
134
3
        press(
135
3
            &mut app,
136
3
            first,
137
3
            MouseEventKind::Down(MouseButton::Left),
138
3
            later,
139
3
        );
140
3
        assert!(form(&app).cell_focused(SPLITS, cell_id(COL_TO_COMM)));
141
3
        assert!(open(&app));
142
    }
143
1
}
144

            
145
#[test]
146
1
fn a_slow_second_click_lands_on_what_is_under_the_pointer_now() {
147
1
    for app in presets() {
148
3
        let mut app = transaction(app, 3);
149
3
        focus_splits(&mut app, COL_TO_AMOUNT);
150
3
        draw_into(&mut app, 70, 24);
151
3
        let first = middle(area_of(&app, cell(COL_TO_COMM)));
152
3
        let start = now();
153
3
        press(
154
3
            &mut app,
155
3
            first,
156
3
            MouseEventKind::Down(MouseButton::Left),
157
3
            start,
158
3
        );
159
3
        draw_into(&mut app, 70, 24);
160
3
        let later = start + Duration::from_secs(2);
161
3
        press(
162
3
            &mut app,
163
3
            first,
164
3
            MouseEventKind::Down(MouseButton::Left),
165
3
            later,
166
3
        );
167
3
        assert!(!open(&app));
168
    }
169
1
}
170

            
171
/// Puts the focus on column `col` of the first split.
172
24
fn focus_splits(app: &mut App, col: usize) {
173
24
    if let Some(Modal::Form(form)) = app.overlays.top_mut() {
174
24
        form.focus = SPLITS;
175
    }
176
24
    if let Some(splits) = splits(app) {
177
24
        splits.col_focus = col;
178
    }
179
24
}
180

            
181
15
fn open_from(app: &mut App) {
182
15
    focus_splits(app, COL_FROM);
183
15
    if let Some(splits) = splits(app) {
184
15
        splits.open_select();
185
    }
186
15
}
187

            
188
#[test]
189
1
fn a_click_on_an_option_picks_it_and_closes_the_list() {
190
1
    for app in presets() {
191
3
        let mut app = transaction(app, 4);
192
3
        open_from(&mut app);
193
3
        click_on(&mut app, Target::Option(2), now());
194
3
        assert!(!open(&app));
195
3
        assert_eq!(value(&app).as_deref(), Some("id2"));
196
    }
197
1
}
198

            
199
#[test]
200
1
fn a_click_elsewhere_closes_the_list_and_does_nothing_else() {
201
1
    for app in presets() {
202
3
        let mut app = transaction(app, 4);
203
3
        open_from(&mut app);
204
3
        if let Some(splits) = splits(&mut app) {
205
3
            splits.set_account_fills(fills());
206
        }
207
3
        apply(&mut app, Intent::SelectNext);
208
3
        assert_eq!(value(&app).as_deref(), Some("id1"));
209
3
        assert_eq!(from_commodity(&mut app), "id2");
210
3
        let focus = form(&app).focus;
211
3
        click_on(&mut app, Target::Field(0), now());
212
3
        assert!(!open(&app));
213
3
        assert_eq!(form(&app).focus, focus);
214
3
        assert_eq!(value(&app).as_deref(), Some("id0"));
215
3
        assert_eq!(from_commodity(&mut app), "");
216
    }
217
1
}
218

            
219
/// Picking account `id1` fills the commodity `id2`.
220
3
fn fills() -> Vec<cli_core::forms::FormOption> {
221
3
    let fill = cli_core::forms::FormOption {
222
3
        id: "id1".to_string(),
223
3
        label: String::new(),
224
3
        alias: String::new(),
225
3
        commodity: Some("id2".to_string()),
226
3
    };
227
3
    vec![fill]
228
3
}
229

            
230
6
fn from_commodity(app: &mut App) -> String {
231
6
    let first = splits(app).and_then(|splits| splits.rows().first());
232
6
    first.map_or_else(String::new, |row| row.from_commodity.value().to_string())
233
6
}
234

            
235
#[test]
236
1
fn a_click_on_a_dropdown_border_keeps_the_list_open() {
237
1
    for app in presets() {
238
3
        let mut app = transaction(app, 4);
239
3
        open_from(&mut app);
240
3
        redraw(&mut app);
241
3
        let area = area_of(&app, Target::Body(crate::hits::Surface::Options));
242
3
        let at = Position::new(area.x, area.y);
243
3
        press(&mut app, at, MouseEventKind::Down(MouseButton::Left), now());
244
3
        assert!(open(&app));
245
    }
246
1
}
247

            
248
30
fn focused_index(app: &App) -> Option<usize> {
249
30
    form(app)
250
30
        .focused_select()
251
30
        .map(SelectWidget::focused_in_filtered)
252
30
}
253

            
254
#[test]
255
1
fn the_dropdown_bar_steps_a_line_and_a_page_and_the_thumb_does_nothing() {
256
1
    for app in presets() {
257
3
        let mut app = transaction(app, 30);
258
3
        open_from(&mut app);
259
21
        let bar = |part| Target::Scroll(crate::hits::Surface::Options, part);
260
3
        click_on(&mut app, bar(BarPart::Down), now());
261
3
        assert_eq!(focused_index(&app), Some(1));
262
3
        for expected in [9, 17, 25] {
263
9
            click_on(&mut app, bar(BarPart::PageDown), now());
264
9
            assert_eq!(focused_index(&app), Some(expected));
265
        }
266
3
        click_on(&mut app, bar(BarPart::Thumb), now());
267
3
        assert_eq!(focused_index(&app), Some(25));
268
3
        click_on(&mut app, bar(BarPart::PageUp), now());
269
3
        assert_eq!(focused_index(&app), Some(17));
270
3
        click_on(&mut app, bar(BarPart::Up), now());
271
3
        assert_eq!(focused_index(&app), Some(16));
272
3
        assert!(open(&app));
273
    }
274
1
}
275

            
276
#[test]
277
1
fn page_keys_move_the_open_list_a_page_under_every_preset() {
278
    use crate::test_support::{key, press};
279
    use ratatui::crossterm::event::KeyCode;
280
1
    for app in presets() {
281
3
        let mut app = transaction(app, 30);
282
3
        open_from(&mut app);
283
3
        draw_into(&mut app, 80, 24);
284
3
        press(&mut app, key(KeyCode::PageDown));
285
3
        assert_eq!(focused_index(&app), Some(8));
286
3
        press(&mut app, key(KeyCode::PageDown));
287
3
        assert_eq!(focused_index(&app), Some(16));
288
3
        press(&mut app, key(KeyCode::PageUp));
289
3
        assert_eq!(focused_index(&app), Some(8));
290
3
        assert!(open(&app));
291
    }
292
1
}
293

            
294
#[test]
295
1
fn a_second_click_on_the_focused_plain_select_field_opens_it() {
296
1
    for app in presets() {
297
3
        let mut app = transaction(app, 3);
298
3
        if let Some(Modal::Form(form)) = app.overlays.top_mut() {
299
3
            form.fields.insert(0, select_field());
300
3
            form.focus = 1;
301
        }
302
3
        let start = now();
303
3
        click_on(&mut app, Target::Field(0), start);
304
3
        assert!(!open(&app));
305
3
        click_on(&mut app, Target::Field(0), start + Duration::from_secs(2));
306
3
        assert!(open(&app));
307
    }
308
1
}
309

            
310
3
fn select_field() -> crate::form::Field {
311
3
    let mut select = SelectWidget::new();
312
3
    select.set_options(options(3));
313
3
    crate::form::Field {
314
3
        key: None,
315
3
        label: "pick",
316
3
        widget: Widget::Select(select),
317
3
    }
318
3
}
319

            
320
#[test]
321
1
fn a_quick_second_click_does_not_close_the_list_the_first_one_opened() {
322
1
    for app in presets() {
323
3
        let mut app = transaction(app, 3);
324
3
        let start = now();
325
3
        click_on(&mut app, cell(COL_TO), start);
326
3
        click_on(&mut app, cell(COL_TO), start + Duration::from_millis(50));
327
3
        assert!(open(&app));
328
3
        click_on(&mut app, cell(COL_TO), start + Duration::from_millis(100));
329
3
        assert!(open(&app));
330
3
        click_on(&mut app, Target::Field(0), start + Duration::from_secs(3));
331
3
        assert!(!open(&app));
332
    }
333
1
}
334

            
335
6
fn amount_text(col: usize) -> Target {
336
6
    Target::Caret {
337
6
        field: SPLITS,
338
6
        cell: cell_id(col).into(),
339
6
    }
340
6
}
341

            
342
#[test]
343
1
fn a_quick_second_click_on_a_text_cell_is_tested_against_the_cell_the_first_one_hit() {
344
1
    for app in presets() {
345
3
        let mut app = transaction(app, 3);
346
3
        focus_splits(&mut app, COL_TO_AMOUNT);
347
3
        if let Some(splits) = splits(&mut app)
348
3
            && let Some(FocusedSubWidget::Amount(amount)) = splits.focused_subwidget_mut()
349
        {
350
3
            amount.as_editor_mut().replace_buffer("42");
351
        }
352
3
        draw_into(&mut app, 60, 24);
353
3
        let first = middle(area_of(&app, amount_text(COL_VALUE)));
354
3
        let start = now();
355
3
        press(
356
3
            &mut app,
357
3
            first,
358
3
            MouseEventKind::Down(MouseButton::Left),
359
3
            start,
360
3
        );
361
3
        draw_into(&mut app, 60, 24);
362
3
        let under = app.hits.hit_at(first).map(|(_, target, _)| target);
363
3
        assert_ne!(
364
3
            under,
365
3
            Some(amount_text(COL_VALUE)),
366
            "the grid did not shift"
367
        );
368
3
        let later = start + Duration::from_millis(100);
369
3
        press(
370
3
            &mut app,
371
3
            first,
372
3
            MouseEventKind::Down(MouseButton::Left),
373
3
            later,
374
3
        );
375
3
        assert!(form(&app).cell_focused(SPLITS, cell_id(COL_VALUE)));
376
    }
377
1
}
378

            
379
#[test]
380
1
fn a_click_on_the_mark_of_a_tail_clipped_account_opens_it_and_focus_shows_its_start() {
381
1
    let long = "Expenses:Food:Groceries:Weekly";
382
1
    for app in presets() {
383
3
        let mut app = transaction(app, 3);
384
3
        if let Some(splits) = splits(&mut app) {
385
3
            splits.set_account_options(vec![SelectOption {
386
3
                id: "long".to_string(),
387
3
                label: long.to_string(),
388
3
            }]);
389
3
            splits.col_focus = COL_FROM;
390
3
            splits.edit_select(|select| select.select_id("long"));
391
3
            splits.col_focus = COL_VALUE;
392
        }
393
3
        redraw(&mut app);
394
3
        let mark = Target::Mark {
395
3
            field: SPLITS,
396
3
            cell: Some(cell_id(COL_FROM)),
397
3
        };
398
3
        click_on(&mut app, mark, now());
399
3
        assert!(open(&app));
400
3
        let Some(splits) = splits(&mut app) else {
401
            continue;
402
        };
403
3
        let grid = splits.grid(true, None, None);
404
3
        let item = &crate::test_support::cells(&grid, 0)[2];
405
3
        assert_eq!(item.shown(Rect::new(0, 0, 12, 1)), long);
406
    }
407
1
}