use crate::event::{Key, KeyEvent, MouseButton, MouseEvent, MouseEventKind};
use crate::testing::mock::types::SimulatedEvent;
use std::collections::VecDeque;
use std::time::Duration;
#[derive(Debug, Default)]
pub struct EventSimulator {
events: VecDeque<SimulatedEvent>,
}
impl EventSimulator {
pub fn new() -> Self {
Self::default()
}
pub fn key(mut self, key: Key) -> Self {
self.events
.push_back(SimulatedEvent::Key(KeyEvent::new(key)));
self
}
pub fn ctrl(mut self, key: Key) -> Self {
self.events
.push_back(SimulatedEvent::Key(KeyEvent::ctrl(key)));
self
}
pub fn ctrl_char(self, c: char) -> Self {
self.ctrl(Key::Char(c))
}
pub fn alt(mut self, key: Key) -> Self {
self.events
.push_back(SimulatedEvent::Key(KeyEvent::alt(key)));
self
}
pub fn alt_char(self, c: char) -> Self {
self.alt(Key::Char(c))
}
pub fn shift(self, c: char) -> Self {
self.key(Key::Char(c.to_ascii_uppercase()))
}
pub fn type_text(mut self, text: &str) -> Self {
for ch in text.chars() {
self.events
.push_back(SimulatedEvent::Key(KeyEvent::new(Key::Char(ch))));
}
self
}
pub fn enter(self) -> Self {
self.key(Key::Enter)
}
pub fn escape(self) -> Self {
self.key(Key::Escape)
}
pub fn tab(self) -> Self {
self.key(Key::Tab)
}
pub fn backspace(self) -> Self {
self.key(Key::Backspace)
}
pub fn delete(self) -> Self {
self.key(Key::Delete)
}
pub fn up(self) -> Self {
self.key(Key::Up)
}
pub fn down(self) -> Self {
self.key(Key::Down)
}
pub fn left(self) -> Self {
self.key(Key::Left)
}
pub fn right(self) -> Self {
self.key(Key::Right)
}
pub fn click(mut self, x: u16, y: u16) -> Self {
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Down(MouseButton::Left),
)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Up(MouseButton::Left),
)));
self
}
pub fn right_click(mut self, x: u16, y: u16) -> Self {
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Down(MouseButton::Right),
)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Up(MouseButton::Right),
)));
self
}
pub fn double_click(mut self, x: u16, y: u16) -> Self {
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Down(MouseButton::Left),
)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Up(MouseButton::Left),
)));
self.events
.push_back(SimulatedEvent::Wait(Duration::from_millis(50)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Down(MouseButton::Left),
)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::Up(MouseButton::Left),
)));
self
}
pub fn drag(mut self, from: (u16, u16), to: (u16, u16)) -> Self {
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
from.0,
from.1,
MouseEventKind::Down(MouseButton::Left),
)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
to.0,
to.1,
MouseEventKind::Drag(MouseButton::Left),
)));
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
to.0,
to.1,
MouseEventKind::Up(MouseButton::Left),
)));
self
}
pub fn scroll_up(mut self, x: u16, y: u16) -> Self {
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::ScrollUp,
)));
self
}
pub fn scroll_down(mut self, x: u16, y: u16) -> Self {
self.events.push_back(SimulatedEvent::Mouse(MouseEvent::new(
x,
y,
MouseEventKind::ScrollDown,
)));
self
}
pub fn wait(mut self, duration: Duration) -> Self {
self.events.push_back(SimulatedEvent::Wait(duration));
self
}
pub fn wait_ms(self, ms: u64) -> Self {
self.wait(Duration::from_millis(ms))
}
pub fn custom(mut self, name: impl Into<String>) -> Self {
self.events.push_back(SimulatedEvent::Custom(name.into()));
self
}
pub fn poll_event(&mut self) -> Option<SimulatedEvent> {
self.events.pop_front()
}
pub fn has_events(&self) -> bool {
!self.events.is_empty()
}
pub fn len(&self) -> usize {
self.events.len()
}
pub fn is_empty(&self) -> bool {
self.events.is_empty()
}
pub fn clear(&mut self) {
self.events.clear();
}
pub fn into_vec(self) -> Vec<SimulatedEvent> {
self.events.into_iter().collect()
}
pub fn repeat(mut self, n: usize) -> Self {
let events: Vec<_> = self.events.iter().cloned().collect();
for _ in 1..n {
for event in &events {
self.events.push_back(event.clone());
}
}
self
}
}