Lines
98.36 %
Functions
100 %
Branches
use super::wheel::line_intent;
use super::*;
use crate::form::{Field, Form, FormKind};
use crate::hits::Surface;
use crate::modal::Draft;
use crate::tabs::fetch::Fetch;
use crate::test_support::{area_of, draw_into, emacs_app, vim_app};
use crate::transport::RawEvent;
use crate::transport::tests_support::{ScriptedTransport, quit_keys};
use crate::view::Tab;
use crate::widgets::{Editor, SelectOption, SelectWidget, Widget};
use ratatui::crossterm::event::{KeyModifiers, MouseButton};
fn presets() -> [App; 3] {
[crate::test_support::app(), emacs_app(), vim_app()]
}
fn event(kind: MouseEventKind) -> MouseEvent {
MouseEvent {
kind,
column: 3,
row: 3,
modifiers: KeyModifiers::NONE,
fn wheel(app: &mut App, up: bool) {
let kind = if up {
MouseEventKind::ScrollUp
} else {
MouseEventKind::ScrollDown
};
handle_mouse(app, event(kind), Instant::now());
fn accounts(mut app: App) -> App {
app.switch_tab(Tab::Accounts);
let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
let rows: Vec<String> = (0..40).map(row).collect();
app.accounts_mut()
.on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
draw_into(&mut app, 80, 24);
app
fn reports(mut app: App) -> App {
app.switch_tab(Tab::Reports);
app.reports_mut().state = Fetch::Loaded((0..40).map(|n| n.to_string()).collect());
fn select_form(open: bool) -> Modal {
let mut select = SelectWidget::new();
select.set_options(
["alpha", "beta", "gamma"]
.into_iter()
.map(|name| SelectOption {
id: name.to_string(),
label: name.to_string(),
})
.collect(),
);
if open {
select.open_list();
let note = Field {
key: None,
label: "note",
widget: Widget::Text(Editor::default()),
let account = Field {
label: "account",
widget: Widget::Select(select),
Modal::Form(Form {
fields: vec![account, note],
focus: 0,
kind: FormKind::ConfigSet,
..Form::config_set()
fn focused_option(app: &App) -> Option<usize> {
match app.overlays.top() {
Some(Modal::Form(form)) => form.focused_select().map(SelectWidget::focused_in_filtered),
_ => None,
#[test]
fn the_wheel_moves_a_list_selection_one_row_per_event() {
for app in presets() {
let mut app = accounts(app);
wheel(&mut app, false);
assert_eq!(app.accounts().selected, 2);
wheel(&mut app, true);
assert_eq!(app.accounts().selected, 1);
assert_eq!(app.accounts().selected, 0);
fn the_wheel_scrolls_the_report_table_a_line() {
let mut app = reports(app);
assert_eq!(app.reports().scroll.top(), 2);
assert_eq!(app.reports().scroll.top(), 1);
fn the_wheel_scrolls_a_draft_and_not_the_view_below() {
for mut app in presets() {
app = accounts(app);
let lines = (0..60).map(|n| format!("line{n:<70}")).collect();
app.overlays.push(Modal::Draft(Draft::new(lines)));
draw_into(&mut app, 80, 10);
let Some(Modal::Draft(draft)) = app.overlays.top() else {
panic!("the draft is not open");
assert_eq!(draft.scroll.top(), 1);
fn wheel_over_body(app: &mut App) {
let body = area_of(app, Target::Body(Surface::Modal));
wheel_at(app, Position::new(body.x + 1, body.y), false);
fn the_wheel_scrolls_help_and_draft_overlays() {
let lines: Vec<String> = (0..40).map(|n| n.to_string()).collect();
let mut app = crate::test_support::app();
app.overlays.push(Modal::Draft(Draft::new(lines.clone())));
wheel_over_body(&mut app);
let Some(Modal::Draft(help)) = app.overlays.top() else {
panic!("help is not open");
assert_eq!(help.scroll.top(), 1);
panic!("draft is not open");
/// The middle of the first option the open drop-down drew.
fn over_options(app: &mut App) -> Position {
draw_into(app, 80, 24);
let area = area_of(app, Target::Option(0));
Position::new(area.x + area.width / 2, area.y)
fn wheel_at(app: &mut App, position: Position, up: bool) {
let mouse = MouseEvent {
column: position.x,
row: position.y,
handle_mouse(app, mouse, Instant::now());
fn the_wheel_steps_the_options_of_an_open_drop_down() {
app.overlays.push(select_form(true));
let at = over_options(&mut app);
wheel_at(&mut app, at, false);
assert_eq!(focused_option(&app), Some(2));
wheel_at(&mut app, at, true);
assert_eq!(focused_option(&app), Some(1));
fn a_form_without_an_open_drop_down_ignores_the_wheel() {
app.overlays.push(select_form(false));
assert_eq!(focused_option(&app), Some(0));
fn the_line_intent_per_surface() {
let line = |surface, up| line_intent(&app, surface, None, up);
assert_eq!(line(Surface::List, false), Some(Intent::ListSelectNext));
assert_eq!(line(Surface::List, true), Some(Intent::ListSelectPrev));
assert_eq!(line(Surface::Pane, false), Some(Intent::ScrollLineDown));
assert_eq!(line(Surface::Modal, true), Some(Intent::ScrollLineUp));
assert_eq!(line(Surface::Options, false), Some(Intent::SelectNext));
assert_eq!(line(Surface::Options, true), Some(Intent::SelectPrev));
fn console(focused: bool) -> App {
app.switch_tab(Tab::Console);
app.set_console_focused(focused);
for i in 0..30 {
app.console_mut().push_scrollback(format!("line {i}"));
fn the_wheel_scrolls_the_console_scrollback_focused_or_not() {
for focused in [true, false] {
let mut app = console(focused);
assert_eq!(app.console().scroll, 2, "focused {focused}");
assert_eq!(app.console().scroll, 1, "focused {focused}");
fn the_wheel_scrolls_the_list_under_the_pointer_while_the_command_line_is_open() {
app.open_command_line();
assert!(app.cmdline.active);
assert!(app.cmdline.editor.buffer().is_empty());
fn the_wheel_over_the_list_header_scrolls_the_list() {
wheel_at(&mut app, Position::new(3, 2), false);
fn the_wheel_over_the_status_bar_or_the_menu_bar_does_nothing() {
wheel_at(&mut app, Position::new(3, 23), false);
wheel_at(&mut app, Position::new(3, 0), false);
fn the_wheel_does_nothing_in_a_confirm_or_over_the_open_menu() {
let mut app = accounts(crate::test_support::app());
app.open_menu();
app.overlays.push(Modal::Confirm {
prompt: "sure?".to_string(),
action: crate::modal::ConfirmAction::DeleteTransaction("t".to_string()),
focus: None,
});
fn releases_drags_and_moves_change_nothing() {
for kind in [
MouseEventKind::Up(MouseButton::Left),
MouseEventKind::Drag(MouseButton::Left),
MouseEventKind::Moved,
MouseEventKind::ScrollLeft,
MouseEventKind::ScrollRight,
] {
handle_mouse(&mut app, event(kind), Instant::now());
assert_eq!(app.front_kind(), Some(Tab::Accounts));
fn a_transport_wheel_event_reaches_the_same_path() {
let wheel_down = RawEvent::Mouse(event(MouseEventKind::ScrollDown), std::time::Instant::now());
let events = [vec![wheel_down, wheel_down], quit_keys().into()].concat();
let mut transport = ScriptedTransport::new(80, 24, events);
crate::runtime::run_loop(&mut transport, &mut app).expect("the loop runs");