1
//! The loop wakes for a key's button press and lets it act.
2

            
3
use super::*;
4
use crate::app::KEY_PRESS;
5
use crate::modal::{ConfirmAction, Modal};
6
use crate::transport::tests_support::ScriptedTransport;
7
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
8
use std::io;
9

            
10
/// The first scripted event, an empty poll that waits out its timeout as a real transport does,
11
/// then a disconnect; every timeout asked for is kept.
12
struct Waiting {
13
    inner: ScriptedTransport,
14
    polls: usize,
15
    timeouts: Vec<Duration>,
16
}
17

            
18
impl Transport for Waiting {
19
    type Backend = ratatui::backend::TestBackend;
20

            
21
6
    fn terminal_mut(&mut self) -> &mut ratatui::Terminal<Self::Backend> {
22
6
        self.inner.terminal_mut()
23
6
    }
24

            
25
3
    fn poll(&mut self, timeout: Duration) -> io::Result<Option<RawEvent>> {
26
3
        self.polls += 1;
27
3
        self.timeouts.push(timeout);
28
3
        match self.polls {
29
            2 => {
30
1
                std::thread::sleep(timeout);
31
1
                Ok(None)
32
            }
33
1
            3 => Err(io::Error::new(io::ErrorKind::UnexpectedEof, "script over")),
34
1
            _ => self.inner.poll(timeout),
35
        }
36
3
    }
37
}
38

            
39
2
fn key(code: KeyCode) -> RawEvent {
40
2
    RawEvent::Key(KeyEvent::new(code, KeyModifiers::NONE))
41
2
}
42

            
43
#[test]
44
1
fn the_loop_waits_no_longer_than_the_press_needs_and_then_lets_it_act() {
45
1
    let mut app = crate::test_support::app();
46
1
    app.overlays.push(Modal::Confirm {
47
1
        prompt: "Discard changes?".to_string(),
48
1
        action: ConfirmAction::DiscardForm,
49
1
        focus: None,
50
1
    });
51
1
    let events = vec![key(KeyCode::Enter)];
52
1
    let mut transport = Waiting {
53
1
        inner: ScriptedTransport::new(60, 20, events),
54
1
        polls: 0,
55
1
        timeouts: Vec::new(),
56
1
    };
57
1
    run_loop(&mut transport, &mut app).expect_err("the script ends with a disconnect");
58
1
    assert!(!app.should_quit);
59
1
    assert!(app.overlays.is_empty());
60
1
    assert_eq!(transport.timeouts[0], POLL_INTERVAL);
61
1
    assert!(transport.timeouts[1] <= KEY_PRESS);
62
1
}
63

            
64
/// The first scripted event, then a disconnect.
65
struct Disconnecting(ScriptedTransport);
66

            
67
impl Transport for Disconnecting {
68
    type Backend = ratatui::backend::TestBackend;
69

            
70
4
    fn terminal_mut(&mut self) -> &mut ratatui::Terminal<Self::Backend> {
71
4
        self.0.terminal_mut()
72
4
    }
73

            
74
2
    fn poll(&mut self, timeout: Duration) -> io::Result<Option<RawEvent>> {
75
2
        match self.0.poll(timeout)? {
76
1
            Some(event) => Ok(Some(event)),
77
1
            None => Err(io::Error::new(io::ErrorKind::UnexpectedEof, "peer hung up")),
78
        }
79
2
    }
80
}
81

            
82
2
fn confirm_app() -> App {
83
2
    let mut app = crate::test_support::app();
84
2
    app.overlays.push(Modal::Confirm {
85
2
        prompt: "Discard changes?".to_string(),
86
2
        action: ConfirmAction::DiscardForm,
87
2
        focus: None,
88
2
    });
89
2
    app
90
2
}
91

            
92
#[test]
93
1
fn a_press_held_when_the_peer_hangs_up_still_acts() {
94
1
    let mut app = confirm_app();
95
1
    let mut transport = Disconnecting(ScriptedTransport::new(60, 20, vec![key(KeyCode::Enter)]));
96
1
    run_loop(&mut transport, &mut app).expect_err("the peer hung up");
97
1
    assert!(app.overlays.is_empty());
98
1
}
99

            
100
#[test]
101
1
fn a_press_held_when_the_loop_quits_still_acts() {
102
1
    let mut app = confirm_app();
103
1
    app.hold_button_by_key(1, crate::event::Intent::CloseTopmost, Instant::now());
104
1
    app.request_quit();
105
1
    let mut transport = ScriptedTransport::new(60, 20, Vec::new());
106
1
    run_loop(&mut transport, &mut app).expect("quits at once");
107
1
    assert!(app.overlays.is_empty());
108
1
}