Lines
100 %
Functions
Branches
//! The loop wakes for a key's button press and lets it act.
use super::*;
use crate::app::KEY_PRESS;
use crate::modal::{ConfirmAction, Modal};
use crate::transport::tests_support::ScriptedTransport;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::io;
/// The first scripted event, an empty poll that waits out its timeout as a real transport does,
/// then a disconnect; every timeout asked for is kept.
struct Waiting {
inner: ScriptedTransport,
polls: usize,
timeouts: Vec<Duration>,
}
impl Transport for Waiting {
type Backend = ratatui::backend::TestBackend;
fn terminal_mut(&mut self) -> &mut ratatui::Terminal<Self::Backend> {
self.inner.terminal_mut()
fn poll(&mut self, timeout: Duration) -> io::Result<Option<RawEvent>> {
self.polls += 1;
self.timeouts.push(timeout);
match self.polls {
2 => {
std::thread::sleep(timeout);
Ok(None)
3 => Err(io::Error::new(io::ErrorKind::UnexpectedEof, "script over")),
_ => self.inner.poll(timeout),
fn key(code: KeyCode) -> RawEvent {
RawEvent::Key(KeyEvent::new(code, KeyModifiers::NONE))
#[test]
fn the_loop_waits_no_longer_than_the_press_needs_and_then_lets_it_act() {
let mut app = crate::test_support::app();
app.overlays.push(Modal::Confirm {
prompt: "Discard changes?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
let events = vec![key(KeyCode::Enter)];
let mut transport = Waiting {
inner: ScriptedTransport::new(60, 20, events),
polls: 0,
timeouts: Vec::new(),
};
run_loop(&mut transport, &mut app).expect_err("the script ends with a disconnect");
assert!(!app.should_quit);
assert!(app.overlays.is_empty());
assert_eq!(transport.timeouts[0], POLL_INTERVAL);
assert!(transport.timeouts[1] <= KEY_PRESS);
/// The first scripted event, then a disconnect.
struct Disconnecting(ScriptedTransport);
impl Transport for Disconnecting {
self.0.terminal_mut()
match self.0.poll(timeout)? {
Some(event) => Ok(Some(event)),
None => Err(io::Error::new(io::ErrorKind::UnexpectedEof, "peer hung up")),
fn confirm_app() -> App {
app
fn a_press_held_when_the_peer_hangs_up_still_acts() {
let mut app = confirm_app();
let mut transport = Disconnecting(ScriptedTransport::new(60, 20, vec![key(KeyCode::Enter)]));
run_loop(&mut transport, &mut app).expect_err("the peer hung up");
fn a_press_held_when_the_loop_quits_still_acts() {
app.hold_button_by_key(1, crate::event::Intent::CloseTopmost, Instant::now());
app.request_quit();
let mut transport = ScriptedTransport::new(60, 20, Vec::new());
run_loop(&mut transport, &mut app).expect("quits at once");