Lines
99.22 %
Functions
100 %
Branches
//! TurboVision frames: double-line active windows, single-line passive ones, shadowed dialogs.
use super::draw;
use crate::event::Intent;
use crate::modal::{ConfirmAction, Draft, Modal};
use crate::view::ViewId;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use tvui::{Dialog, Role};
const SCREEN: Rect = Rect::new(0, 0, 80, 24);
fn rendered(app: &crate::app::App) -> Buffer {
let mut terminal =
Terminal::new(TestBackend::new(SCREEN.width, SCREEN.height)).expect("test terminal");
terminal
.draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
.expect("draw");
terminal.backend().buffer().clone()
}
fn confirm_app() -> crate::app::App {
let mut app = crate::test_support::app();
let prompt = "Delete it?".to_string();
app.overlays.push(Modal::Confirm {
prompt,
action: ConfirmAction::DiscardForm,
focus: None,
});
app
#[test]
fn the_active_view_has_a_double_frame_with_a_centred_title() {
let buffer = rendered(&crate::test_support::app());
assert_eq!(buffer[(0, 1)].symbol(), "╔");
assert_eq!(buffer[(79, 1)].symbol(), "╗");
let top: String = (0..80).map(|x| buffer[(x, 1)].symbol()).collect();
let at = top.find("Accounts").expect("the title is in the border");
assert!((20..40).contains(&top[..at].chars().count()), "{top}");
fn a_view_under_a_dialog_is_passive() {
let buffer = rendered(&confirm_app());
assert_eq!(buffer[(0, 1)].symbol(), "┌");
assert_eq!(buffer[(79, 1)].symbol(), "┐");
/// The box between the first `╔` and the last `╝`, the only double frame of a covered view.
fn dialog_area(buffer: &Buffer) -> Rect {
let corners = |symbol: &'static str| -> Vec<_> {
let all = buffer.area.positions();
all.filter(|p| buffer[*p].symbol() == symbol).collect()
};
let (first, last) = (corners("╔")[0], *corners("╝").last().expect("a corner"));
Rect::new(first.x, first.y, last.x + 1 - first.x, last.y + 1 - first.y)
fn a_dialog_has_a_double_frame_and_a_shadow() {
let app = confirm_app();
let buffer = rendered(&app);
let area = dialog_area(&buffer);
assert_eq!(buffer[(area.x, area.y)].symbol(), "╔");
let shadow = app.theme.style(Role::Shadow);
for position in [
(area.right(), area.y + 1),
(area.right() + 1, area.bottom()),
(area.x + 2, area.bottom()),
] {
assert_eq!(buffer[position].style().bg, shadow.bg, "{position:?}");
assert_eq!(buffer[position].style().fg, shadow.fg, "{position:?}");
assert_ne!(buffer[(area.right(), area.y)].style().bg, shadow.bg);
assert_ne!(buffer[(area.x + 1, area.bottom())].style().bg, shadow.bg);
fn the_tab_bar_and_status_line_are_one_row_each() {
let app = crate::test_support::app();
let fill = |y: u16, roles: &[Role]| {
let backgrounds: Vec<_> = roles.iter().map(|role| app.theme.style(*role).bg).collect();
(0..80).all(|x| backgrounds.contains(&buffer[(x, y)].style().bg))
assert!(fill(0, &[Role::Menu, Role::MenuSelected]));
assert!(fill(23, &[Role::Status]));
assert!(!fill(1, &[Role::Menu, Role::MenuSelected]));
assert!(!fill(22, &[Role::Status]));
assert_eq!(buffer[(0, 22)].symbol(), "╚");
fn row_text(buffer: &Buffer, y: u16) -> String {
(0..buffer.area.width)
.map(|x| buffer[(x, y)].symbol())
.collect()
fn button_row(buffer: &Buffer) -> String {
let area = dialog_area(buffer);
row_text(buffer, area.bottom() - 3)
fn assert_labels_in_order(row: &str, labels: &[&str]) {
let at: Vec<Option<usize>> = labels.iter().map(|label| row.find(label)).collect();
assert!(at.iter().all(Option::is_some), "{labels:?} in {row}");
assert!(at.is_sorted(), "{labels:?} in order in {row}");
fn a_confirm_shows_yes_and_no_in_the_bottom_row() {
assert_labels_in_order(&button_row(&buffer), &["Yes", "No"]);
fn a_draft_shows_a_close_button() {
app.overlays
.push(Modal::Draft(crate::modal::Draft::new(Vec::new())));
assert_labels_in_order(&button_row(&rendered(&app)), &["Close"]);
fn the_transaction_form_fits_80x24_with_ok_and_cancel() {
.push(Modal::Form(crate::form::Form::transaction_create()));
assert!(SCREEN.contains(ratatui::layout::Position::new(
area.right() - 1,
area.bottom() - 1
)));
assert_labels_in_order(&button_row(&buffer), &["OK", "Close"]);
let body = Dialog::body_area(area);
assert!(row_text(&buffer, body.y).contains("Date:"));
fn an_eighteen_split_form_scrolls_to_the_focused_row_between_the_bars() {
use crate::widgets::Widget;
let mut form = crate::form::Form::transaction_create();
let Widget::Splits(ref mut splits) = form.fields[3].widget else {
panic!("the splits field is the fourth");
for _ in 0..17 {
splits.add_row();
form.focus = 3;
app.overlays.push(Modal::Form(form));
assert!(area.y >= 1 && area.bottom() < SCREEN.height, "{area:?}");
let focus = crate::test_support::dn_theme().style(Role::InputFocused);
let shown = area.positions().any(|at| {
(buffer[at].fg, buffer[at].bg)
== (focus.fg.unwrap_or_default(), focus.bg.unwrap_or_default())
assert!(shown, "the focused split cell is shown");
assert!(row_text(&buffer, 0).contains("View"));
assert!(row_text(&buffer, SCREEN.height - 1).contains("[cua]"));
fn the_filter_builder_offers_apply_and_close_without_a_default() {
let builder = crate::modal::FilterBuilder::new("(and)");
app.overlays.push(Modal::FilterBuilder(builder));
let row = button_row(&rendered(&app));
assert_labels_in_order(&row, &["Apply", "Close"]);
fn a_form_draws_its_focused_button_in_the_focus_role_and_shows_no_cursor() {
let mut form = crate::form::Form::config_set();
.draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
let buffer = terminal.backend().buffer();
let row = area.bottom() - 3;
let close = row_text(buffer, row)
.find("Close")
.expect("the Close button");
let focus = app.theme.style(Role::ButtonFocus);
let col = u16::try_from(row_text(buffer, row)[..close].chars().count()).unwrap_or(0);
assert_eq!(buffer[(col, row)].style().bg, focus.bg);
assert!(!terminal.backend().cursor_visible());
fn detail_app(lines: usize) -> crate::app::App {
let lines = (0..lines).map(|i| format!("line{i:02}")).collect();
app.overlays.push(Modal::Draft(Draft::new(lines)));
fn page_down(app: &mut crate::app::App) {
if let Some(Modal::Draft(detail)) = app.overlays.top_mut() {
detail.scroll_by(Intent::ScrollPageDown);
fn a_long_modal_body_has_a_scrollbar_along_its_rows_on_the_right_frame() {
let app = detail_app(60);
let x = area.right() - 1;
assert_eq!(buffer[(x, body.y)].symbol(), "\u{25B2}");
assert_eq!(buffer[(x, body.bottom() - 1)].symbol(), "\u{25BC}");
assert_eq!(buffer[(x, body.bottom())].symbol(), "\u{2551}");
assert_eq!(buffer[(x, area.y)].symbol(), "\u{2557}");
fn a_modal_body_that_fits_has_no_scrollbar() {
let buffer = rendered(&detail_app(3));
assert_eq!(buffer[(area.right() - 1, body.y)].symbol(), "\u{2551}");
fn paging_past_the_end_of_a_modal_shows_the_last_page() {
let mut app = detail_app(60);
rendered(&app);
for _ in 0..10 {
page_down(&mut app);
let body = Dialog::body_area(dialog_area(&buffer));
let row = |y: u16| -> String {
(body.x..body.right())
assert!(
row(body.bottom() - 1).contains("line59"),
"{}",
row(body.bottom() - 1)
);
assert!(row(body.y).contains("line"), "{}", row(body.y));
let thumb = app.theme.style(Role::ScrollbarThumb);
let cell = buffer[(body.right(), body.bottom() - 2)].style();
assert_eq!((cell.fg, cell.bg), (thumb.fg, thumb.bg));
fn a_detail_with_a_multi_line_note_scrolls_to_its_last_line() {
let mut lines: Vec<String> = (0..30).map(|i| format!("line{i:02}")).collect();
lines[3] = "Note: a\nb\nc\nd\ne\nf\ng\nh".to_string();
let last: String = (body.x..body.right())
.map(|x| buffer[(x, body.bottom() - 1)].symbol())
.collect();
assert!(last.contains("line29"), "{last}");
fn the_zoomed_front_window_shows_unzoom_and_its_number() {
let top = row_text(&rendered(&app), 1);
assert!(top.starts_with("╔═[■]"), "{top}");
assert!(top.ends_with("1═[↕]═╗"), "{top}");
app.switch_tab(ViewId::Scripts);
let head = top.strip_suffix("═[↕]═╗").unwrap_or_default();
assert!(head.ends_with('═'), "{top}");
fn a_rear_window_draws_passive_beside_the_active_front_one() {
app.toggle_zoom_of(ViewId::Accounts);
let row = row_text(&buffer, 1);
let front: String = row.chars().take(60).collect();
assert!(front.starts_with("╔═[■]"), "{front}");
assert!(front.ends_with("1═[↑]═╗"), "{front}");
assert!(row.ends_with('┐'), "{row}");
let passive = app.theme.style(Role::PanelFramePassive);
let active = app.theme.style(Role::PanelFrame);
assert_eq!(buffer[(60, 1)].style().fg, passive.fg);
assert_eq!(buffer[(0, 1)].style().fg, active.fg);
fn a_modal_makes_the_front_window_passive_too() {
let mut app = confirm_app();
fn the_desktop_shows_where_no_window_is() {
for kind in ViewId::ALL {
app.toggle_zoom_of(kind);
let shade = app.theme.style(Role::Desktop);
assert_eq!(buffer[(79, 1)].symbol(), "░");
assert_eq!(buffer[(79, 22)].symbol(), "░");
assert_eq!(buffer[(79, 22)].style().bg, shade.bg);
assert_eq!(buffer[(0, 0)].symbol(), " ");