1
//! Clicks on scrollbar parts: an arrow steps a line, a page area a page, the thumb nothing.
2

            
3
use super::click_tests::{click_on, now, presets};
4
use super::*;
5
use crate::modal::Draft;
6
use crate::tabs::fetch::Fetch;
7
use crate::test_support::redraw;
8
use crate::view::Tab;
9
use tvui::BarPart;
10

            
11
57
fn bar(surface: Surface, part: BarPart) -> Target {
12
57
    Target::Scroll(surface, part)
13
57
}
14

            
15
9
fn accounts(mut app: App) -> App {
16
9
    app.switch_tab(Tab::Accounts);
17
720
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
18
9
    let rows: Vec<String> = (0..80).map(row).collect();
19
9
    app.accounts_mut()
20
9
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
21
9
    app
22
9
}
23

            
24
#[test]
25
1
fn a_list_bar_steps_the_selection_a_row_and_a_page() {
26
1
    for app in presets() {
27
3
        let mut app = accounts(app);
28
3
        redraw(&mut app);
29
3
        click_on(&mut app, bar(Surface::List, BarPart::Down), now());
30
3
        assert_eq!(app.accounts().selected, 1);
31
3
        let page = app.accounts().window.page();
32
3
        click_on(&mut app, bar(Surface::List, BarPart::PageDown), now());
33
3
        click_on(&mut app, bar(Surface::List, BarPart::PageDown), now());
34
3
        assert_eq!(app.accounts().selected, 1 + 2 * page);
35
3
        click_on(&mut app, bar(Surface::List, BarPart::Thumb), now());
36
3
        assert_eq!(app.accounts().selected, 1 + 2 * page);
37
3
        click_on(&mut app, bar(Surface::List, BarPart::PageUp), now());
38
3
        click_on(&mut app, bar(Surface::List, BarPart::PageUp), now());
39
3
        assert_eq!(app.accounts().selected, 1);
40
3
        click_on(&mut app, bar(Surface::List, BarPart::Up), now());
41
3
        assert_eq!(app.accounts().selected, 0);
42
    }
43
1
}
44

            
45
#[test]
46
1
fn a_list_bar_works_while_the_command_line_has_the_focus() {
47
1
    for app in presets() {
48
3
        let mut app = accounts(app);
49
3
        app.open_command_line();
50
3
        click_on(&mut app, bar(Surface::List, BarPart::Down), now());
51
3
        assert_eq!(app.accounts().selected, 1);
52
3
        assert!(app.cmdline.active);
53
    }
54
1
}
55

            
56
#[test]
57
1
fn a_report_bar_scrolls_the_table() {
58
1
    for mut app in presets() {
59
3
        app.switch_tab(Tab::Reports);
60
240
        app.reports_mut().state = Fetch::Loaded((0..80).map(|n| n.to_string()).collect());
61
3
        click_on(&mut app, bar(Surface::Pane, BarPart::Down), now());
62
3
        assert_eq!(app.reports().scroll.top(), 1);
63
3
        let page = app.reports().scroll.page();
64
3
        click_on(&mut app, bar(Surface::Pane, BarPart::PageDown), now());
65
3
        assert_eq!(app.reports().scroll.top(), 1 + page);
66
3
        click_on(&mut app, bar(Surface::Pane, BarPart::Thumb), now());
67
3
        assert_eq!(app.reports().scroll.top(), 1 + page);
68
3
        click_on(&mut app, bar(Surface::Pane, BarPart::Up), now());
69
3
        assert_eq!(app.reports().scroll.top(), page);
70
3
        click_on(&mut app, bar(Surface::Pane, BarPart::PageUp), now());
71
3
        assert_eq!(app.reports().scroll.top(), 0);
72
    }
73
1
}
74

            
75
#[test]
76
1
fn a_modal_bar_scrolls_the_overlay_body() {
77
1
    for mut app in presets() {
78
240
        let lines: Vec<String> = (0..80).map(|n| n.to_string()).collect();
79
3
        app.overlays.push(Modal::Draft(Draft::new(lines)));
80
3
        click_on(&mut app, bar(Surface::Modal, BarPart::Down), now());
81
12
        let top = |app: &App| match app.overlays.top() {
82
12
            Some(Modal::Draft(detail)) => (detail.scroll.top(), detail.scroll.page()),
83
            _ => panic!("the detail is not open"),
84
12
        };
85
3
        assert_eq!(top(&app).0, 1);
86
3
        click_on(&mut app, bar(Surface::Modal, BarPart::PageDown), now());
87
3
        let (scrolled, page) = top(&app);
88
3
        assert_eq!(scrolled, 1 + page);
89
3
        click_on(&mut app, bar(Surface::Modal, BarPart::Thumb), now());
90
3
        assert_eq!(top(&app).0, 1 + page);
91
3
        click_on(&mut app, bar(Surface::Modal, BarPart::Up), now());
92
3
        click_on(&mut app, bar(Surface::Modal, BarPart::PageUp), now());
93
3
        assert_eq!(top(&app).0, 0);
94
    }
95
1
}
96

            
97
#[test]
98
1
fn a_click_on_a_modal_body_does_nothing() {
99
1
    for mut app in presets() {
100
240
        let lines: Vec<String> = (0..80).map(|n| n.to_string()).collect();
101
3
        app.overlays.push(Modal::Draft(Draft::new(lines)));
102
3
        click_on(&mut app, Target::Body(Surface::Modal), now());
103
3
        assert!(matches!(app.overlays.top(), Some(Modal::Draft(_))));
104
    }
105
1
}
106

            
107
#[test]
108
1
fn a_row_click_selects_the_row_while_the_command_line_is_open_but_never_opens_it() {
109
1
    for app in presets() {
110
3
        let mut app = accounts(app);
111
3
        app.open_command_line();
112
3
        click_on(&mut app, Target::Row(2), now());
113
3
        assert_eq!(app.accounts().selected, 2);
114
3
        click_on(&mut app, Target::Row(2), now());
115
3
        assert!(app.overlays.top().is_none());
116
3
        assert!(app.cmdline.active);
117
3
        assert!(app.last_click.is_none());
118
    }
119
1
}
120

            
121
4
fn detail_lines() -> Vec<String> {
122
160
    (0..40).map(|n| format!("line {n:0>70}")).collect()
123
4
}
124

            
125
4
fn detail_top(app: &App, id: crate::window::WindowId) -> usize {
126
4
    app.win::<crate::viewer::Viewer>(id)
127
4
        .map_or(usize::MAX, |viewer| viewer.scroll.top())
128
4
}
129

            
130
#[test]
131
1
fn a_detail_bar_scrolls_the_front_detail() {
132
1
    for mut app in presets() {
133
3
        app.show_viewer(
134
3
            crate::viewer::Source::Detail { id: String::new() },
135
3
            detail_lines(),
136
3
        );
137
3
        let id = app.front_window().expect("the detail is in front");
138
3
        click_on(&mut app, bar(Surface::Pane, BarPart::Down), now());
139
3
        assert_eq!(detail_top(&app, id), 1);
140
    }
141
1
}
142

            
143
#[test]
144
1
fn the_wheel_scrolls_a_rear_detail() {
145
1
    let mut app = crate::test_support::app();
146
1
    app.toggle_zoom();
147
1
    app.show_viewer(
148
1
        crate::viewer::Source::Detail { id: String::new() },
149
1
        detail_lines(),
150
1
    );
151
1
    let id = app.front_window().expect("the detail is in front");
152
1
    app.open_extra(Tab::Accounts);
153
1
    redraw(&mut app);
154
1
    let desktop = crate::draw::body_area(ratatui::layout::Rect::new(0, 0, 80, 24));
155
1
    let shown = app.visible_windows(desktop);
156
1
    let place = shown
157
1
        .iter()
158
3
        .find(|(shown, ..)| *shown == id)
159
1
        .map(|(.., place)| *place);
160
1
    let rect = crate::test_support::flat(place.expect("the detail is drawn"));
161
1
    let mouse = MouseEvent {
162
1
        kind: MouseEventKind::ScrollDown,
163
1
        column: rect.right() - 3,
164
1
        row: rect.y + 2,
165
1
        modifiers: ratatui::crossterm::event::KeyModifiers::NONE,
166
1
    };
167
1
    handle_mouse(&mut app, mouse, now());
168
1
    assert!(detail_top(&app, id) > 0);
169
1
}