1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
use crossbeam::channel::{Receiver, Sender};
use cursive::backends::puppet::observed::ObservedScreen;
use cursive::backends::puppet::Backend;
use cursive::event::{Event, Key};
use cursive::view::Nameable;
use cursive::views::TextView;
use cursive::Vec2;
use cursive_tabs::{Align, Placement, TabPanel, TabView};
use insta::assert_display_snapshot;
fn setup_test_environment<F>(cb: F) -> (Receiver<ObservedScreen>, Sender<Option<Event>>)
where
F: FnOnce(&mut cursive::Cursive),
{
let backend = Backend::init(Some(Vec2::new(80, 24)));
let frames = backend.stream();
let input = backend.input();
let mut siv = cursive::Cursive::new().into_runner(backend);
cb(&mut siv);
input
.send(Some(Event::Refresh))
.expect("Refresh not accepted, backend not valid");
siv.step();
(frames, input)
}
struct TestCursive {
siv: cursive::CursiveRunner<cursive::Cursive>,
frames: Receiver<ObservedScreen>,
input: Sender<Option<Event>>,
}
impl TestCursive {
fn new<F>(cb: F) -> Self
where
F: FnOnce(&mut cursive::Cursive),
{
let backend = Backend::init(Some(Vec2::new(80, 24)));
let frames = backend.stream();
let input = backend.input();
let mut siv = cursive::Cursive::new().into_runner(backend);
cb(&mut siv);
input
.send(Some(Event::Refresh))
.expect("Refresh not accepted, backend not valid");
siv.step();
Self { siv, frames, input }
}
fn _call_on<F>(&mut self, cb: F)
where
F: FnOnce(&mut cursive::Cursive),
{
cb(&mut self.siv);
}
fn input(&mut self, event: Event) {
self.input
.send(Some(event))
.expect("Refresh not accepted, backend could not react");
self.step();
}
fn step(&mut self) {
self.input
.send(Some(Event::Refresh))
.expect("Refresh not accepted, backend could not react");
self.siv.step();
}
fn last_screen(&mut self) -> ObservedScreen {
self.frames.try_iter().last().unwrap()
}
}
#[test]
fn test_puppet_screen() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
siv.add_fullscreen_layer(TextView::new(
"This is a smoke test for the puppet cursive backend.",
))
});
assert_display_snapshot!(frames.try_iter().last().unwrap())
}
#[test]
fn end2end_add_at() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let tabs = TabView::new()
.with_tab_at(TextView::new("Third").with_name("0"), 0)
.with_tab_at(TextView::new("First").with_name("1"), 0)
.with_tab_at(TextView::new("Second").with_name("2"), 1);
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_add_at_action_change_tab() {
let mut tsiv = TestCursive::new(|siv: &mut cursive::Cursive| {
let tabs = TabView::new()
.with_tab_at(TextView::new("Third").with_name("0"), 0)
.with_tab_at(TextView::new("First").with_name("1"), 0)
.with_tab_at(TextView::new("Second").with_name("2"), 1);
siv.add_layer(tabs);
});
tsiv.input(Event::Key(Key::Up));
assert_display_snapshot!(tsiv.last_screen());
}
#[test]
fn end2end_add_at_panel() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let tabs = TabPanel::new()
.with_tab(TextView::new("Pshhhh").with_name("Stonks"))
.with_tab_at(TextView::new("Fooooo").with_name("So"), 0)
.with_tab_at(TextView::new("Ahhhhh").with_name("Much"), 1)
.with_bar_alignment(Align::Center);
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_panel_smoke() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let tabs = TabPanel::new()
.with_tab(TextView::new("Pshhhh").with_name("Stronk test"))
.with_active_tab("Stronk test")
.unwrap_or_else(|_| panic!("Setting active tab has failed"))
.with_bar_alignment(Align::Center);
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_remove_active() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let mut tabs = TabView::new()
.with_tab(TextView::new("First").with_name("0"))
.with_tab(TextView::new("Second").with_name("1"));
tabs.remove_tab("1").expect("Removal of active tab failed");
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_remove_inactive() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let mut tabs = TabView::new()
.with_tab(TextView::new("First").with_name("0"))
.with_tab(TextView::new("Second").with_name("1"));
tabs.remove_tab("0").expect("Removal failed.");
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_swap() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let mut tabs = TabPanel::new()
.with_tab(TextView::new("Pshhhh").with_name("Stonks"))
.with_tab_at(TextView::new("Fooooo").with_name("So"), 0)
.with_tab_at(TextView::new("Ahhhhh").with_name("Much"), 1)
.with_bar_alignment(Align::Center);
tabs.swap_tabs("So", "Stonks");
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_switch() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let tabs = TabView::new()
.with_tab(TextView::new("First").with_name("0"))
.with_tab(TextView::new("Second").with_name("1"))
.with_active_tab("0")
.unwrap_or_else(|_| panic!("Setting active tab has failed"));
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_vertical_left() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let tabs = TabPanel::new()
.with_tab(TextView::new("Pshhhh").with_name("Stronk test"))
.with_tab(TextView::new("Pshhhh").with_name("Stronker test"))
.with_active_tab("Stronk test")
.unwrap_or_else(|_| panic!("Setting active tab has failed"))
.with_bar_alignment(Align::Center)
.with_bar_placement(Placement::VerticalLeft);
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
#[test]
fn end2end_vertical_left_with_action_change_tab() {
let mut tsiv = TestCursive::new(|siv: &mut cursive::Cursive| {
let tabs = TabPanel::new()
.with_tab(TextView::new("Pshhhh").with_name("Stronk test"))
.with_tab(TextView::new("Pshhhh").with_name("Stronker test"))
.with_active_tab("Stronk test")
.unwrap_or_else(|_| panic!("Setting active tab has failed"))
.with_bar_alignment(Align::Center)
.with_bar_placement(Placement::VerticalLeft);
siv.add_layer(tabs);
});
tsiv.input(Event::Key(Key::Up));
assert_display_snapshot!(tsiv.last_screen());
}
#[test]
fn end2end_vertical_right() {
let (frames, _) = setup_test_environment(|siv: &mut cursive::Cursive| {
let tabs = TabPanel::new()
.with_tab(TextView::new("Pshhhh").with_name("Stronk test"))
.with_active_tab("Stronk test")
.unwrap_or_else(|_| panic!("Setting active tab has failed"))
.with_bar_alignment(Align::Center)
.with_bar_placement(Placement::VerticalRight);
siv.add_layer(tabs);
});
assert_display_snapshot!(frames.try_iter().last().unwrap());
}
|