Lines
100 %
Functions
Branches
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Buffer;
use tvui::Role;
use crate::app::{App, MenuId};
use crate::draw::draw;
use crate::view::Tab;
fn drawn(app: &App, width: u16) -> Buffer {
let mut terminal = Terminal::new(TestBackend::new(width, 24)).expect("test terminal");
terminal
.draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
.expect("draw");
terminal.backend().buffer().clone()
}
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect()
fn open_on(tab: Tab) -> App {
let mut app = crate::test_support::app();
app.switch_tab(tab);
app.open_menu();
app
#[test]
fn the_top_row_is_the_view_and_window_menu_bar_at_any_width() {
let app = crate::test_support::app();
for width in [80, 120] {
let buf = drawn(&app, width);
assert!(row(&buf, 0).starts_with(" View Window "), "{width}");
assert_eq!(buf[(2, 0)].style().fg, app.theme.style(Role::MenuHotkey).fg);
assert_eq!(buf[(8, 0)].style().fg, app.theme.style(Role::MenuHotkey).fg);
assert_eq!(buf[(3, 0)].style().fg, app.theme.style(Role::Menu).fg);
assert!(!row(&buf, 0).contains("Accounts"), "{width}");
fn the_open_menu_lists_each_view_without_a_key_and_marks_the_open_ones() {
let mut app = open_on(Tab::Tags);
app.close_window(app.window_of(Tab::Config));
let buf = drawn(&app, 80);
for (offset, tab) in Tab::ALL.iter().enumerate() {
let line = row(&buf, 2 + u16::try_from(offset).expect("small"));
let mark = if *tab == Tab::Config { " " } else { "•" };
assert!(line.contains(&format!("│ {mark} ")), "{line}");
assert!(line.contains(tab.label()), "{line}");
fn the_window_menu_lists_the_commands_with_their_chords_under_its_title() {
app.open_menu_of(MenuId::Window);
let lines: Vec<String> = (2..10).map(|y| row(&buf, y)).collect();
for (line, (label, chord)) in lines.iter().zip([
("Size/move", "C-F5"),
("Zoom", "F5"),
("Tile", ""),
("Cascade", ""),
("Next", "F6"),
("Previous", "S-F6"),
("Close", "C-F4"),
("Close all", ""),
]) {
assert_eq!(line.chars().nth(6), Some('│'), "{line}");
assert!(line.contains(label), "{line}");
assert!(line.contains(&format!("{chord} │")), "{line}");
assert_eq!(
buf[(8, 0)].style().bg,
app.theme.style(Role::MenuSelected).bg
);
fn the_window_commands_are_dimmed_on_the_empty_desktop_and_the_views_are_not() {
while app.front_window().is_some() {
app.close_front_window();
let disabled = app.theme.style(Role::MenuDisabled);
let selected = app.theme.style(Role::MenuSelectedDisabled);
assert_eq!(buf[(10, 2)].style().fg, selected.fg);
assert_eq!(buf[(10, 2)].style().bg, selected.bg);
assert!(
(3..9)
.all(|y| buf[(10, y)].style().fg == disabled.fg
&& buf[(10, y)].style().bg == disabled.bg)
app.open_menu_of(MenuId::View);
assert!((3..12).all(|y| buf[(5, y)].style().fg != disabled.fg));
fn the_selected_item_and_the_title_use_the_selected_roles() {
let mut app = open_on(Tab::Accounts);
app.menu_next();
let selected = app.theme.style(Role::MenuSelected);
buf[(2, 0)].style().bg,
app.theme.style(Role::MenuSelectedHotkey).bg
assert_eq!(buf[(5, 3)].style().bg, selected.bg);
assert_eq!(buf[(5, 2)].style().bg, app.theme.style(Role::Menu).bg);
fn the_open_menu_casts_a_shadow() {
let app = open_on(Tab::Accounts);
assert_eq!(buf[(21, 3)].style().bg, app.theme.style(Role::Shadow).bg);
assert_eq!(buf[(5, 13)].style().bg, app.theme.style(Role::Shadow).bg);
fn the_popup_frame_sits_over_the_window_frame_column() {
let buf = drawn(&open_on(Tab::Accounts), 80);
assert!(row(&buf, 1).starts_with('┌'));
assert!(row(&buf, 2).starts_with("│ "));
fn crowded_window_menu() -> App {
for _ in 0..12 {
app.open_extra(Tab::Tags);
fn shown(buf: &Buffer, label: &str) -> Option<u16> {
(2..buf.area.height).find(|y| row(buf, *y).contains(label))
fn a_long_window_list_scrolls_to_keep_the_selection_on_screen() {
let mut app = crowded_window_menu();
let last = app.menu_entries().pop().map(|entry| entry.label);
let last = last.unwrap_or_default();
assert!(shown(&drawn(&app, 80), &last).is_none());
app.menu_last();
let at = shown(&buf, &last);
assert!(at.is_some_and(|y| y < 23), "{at:?}");
assert!(shown(&buf, "Zoom").is_none());
app.menu_first();
assert!(shown(&buf, "Zoom").is_some());
assert!(shown(&buf, &last).is_none());
fn moving_up_inside_the_view_does_not_scroll_the_list() {
let _ = drawn(&app, 80);
let top = app.menu_window.top();
app.menu_prev();
assert_eq!(app.menu_window.top(), top);
assert!(top > 0);