#![allow(dead_code)]
use futures::executor::block_on;
use iced::keyboard::{self, Key, Modifiers, key};
use iced::{Element, Event, Font, Pixels, Point, Size};
use iced_test::core::clipboard::{Clipboard, Kind};
use iced_test::core::renderer::Headless;
use iced_test::core::widget::Operation;
use iced_test::core::widget::operation::{Outcome, focusable};
use iced_test::renderer::Renderer;
use iced_test::runtime::user_interface::{Cache, UserInterface};
#[derive(Debug, Default)]
pub struct Memory(pub Option<String>);
impl Clipboard for Memory {
fn read(&self, _kind: Kind) -> Option<String> {
self.0.clone()
}
fn write(&mut self, _kind: Kind, contents: String) {
self.0 = Some(contents);
}
}
pub struct Harness<S, M> {
pub state: S,
pub clipboard: Memory,
renderer: Renderer,
cache: Option<Cache>,
view: for<'a> fn(&'a S) -> Element<'a, M>,
update: fn(&mut S, M),
}
impl<S, M> Harness<S, M> {
pub fn new(state: S, view: for<'a> fn(&'a S) -> Element<'a, M>, update: fn(&mut S, M)) -> Self {
let Some(renderer) = block_on(<Renderer as Headless>::new(
Font::with_name("Fira Sans"),
Pixels(16.0),
Some("tiny-skia"),
)) else {
panic!("a headless renderer");
};
Self {
state,
clipboard: Memory::default(),
renderer,
cache: Some(Cache::default()),
view,
update,
}
}
pub fn focus(&mut self, id: impl Into<iced::widget::Id>) {
let cache = self.cache.take().unwrap_or_default();
let mut ui = build((self.view)(&self.state), cache, &mut self.renderer);
ui.operate(&self.renderer, &mut focusable::focus(id.into()));
self.cache = Some(ui.into_cache());
}
pub fn focus_next(&mut self) {
let cache = self.cache.take().unwrap_or_default();
let mut ui = build((self.view)(&self.state), cache, &mut self.renderer);
let mut operation: Box<dyn Operation> = Box::new(focusable::focus_next());
loop {
ui.operate(&self.renderer, operation.as_mut());
match operation.finish() {
Outcome::Chain(next) => operation = next,
_ => break,
}
}
self.cache = Some(ui.into_cache());
}
pub fn batch(&mut self, events: impl IntoIterator<Item = Event>) -> Vec<bool> {
self.batch_with(iced::mouse::Cursor::Unavailable, events)
}
pub fn click(&mut self, position: Point) {
self.batch_with(
iced::mouse::Cursor::Available(position),
[
Event::Mouse(iced::mouse::Event::CursorMoved { position }),
Event::Mouse(iced::mouse::Event::ButtonPressed(iced::mouse::Button::Left)),
],
);
self.batch_with(
iced::mouse::Cursor::Available(position),
[Event::Mouse(iced::mouse::Event::ButtonReleased(
iced::mouse::Button::Left,
))],
);
}
pub fn batch_with(
&mut self,
cursor: iced::mouse::Cursor,
events: impl IntoIterator<Item = Event>,
) -> Vec<bool> {
let events: Vec<Event> = events.into_iter().collect();
let mut messages = Vec::new();
let cache = self.cache.take().unwrap_or_default();
let mut ui = build((self.view)(&self.state), cache, &mut self.renderer);
let (_, statuses) = ui.update(
&events,
cursor,
&mut self.renderer,
&mut self.clipboard,
&mut messages,
);
self.cache = Some(ui.into_cache());
for message in messages {
(self.update)(&mut self.state, message);
}
statuses
.into_iter()
.map(|status| status == iced::event::Status::Captured)
.collect()
}
pub fn type_text(&mut self, text: &str) {
for event in iced_test::simulator::typewrite(text) {
self.batch([event]);
}
}
pub fn press(&mut self, modifiers: Modifiers, key: impl Into<Key>) -> bool {
let key = key.into();
self.batch([Event::Keyboard(keyboard::Event::ModifiersChanged(
modifiers,
))]);
let captured = self.batch([press(modifiers, key.clone())]);
self.batch([
release(modifiers, key),
Event::Keyboard(keyboard::Event::ModifiersChanged(Modifiers::empty())),
]);
captured.first().copied().unwrap_or(false)
}
pub fn press_with_text(&mut self, modifiers: Modifiers, c: char) -> bool {
let key = letter(c);
self.batch([Event::Keyboard(keyboard::Event::ModifiersChanged(
modifiers,
))]);
let captured = self.batch([Event::Keyboard(keyboard::Event::KeyPressed {
key: key.clone(),
modified_key: key.clone(),
physical_key: key::Physical::Unidentified(key::NativeCode::Unidentified),
location: keyboard::Location::Standard,
modifiers,
repeat: false,
text: Some(c.to_string().into()),
})]);
self.batch([
release(modifiers, key),
Event::Keyboard(keyboard::Event::ModifiersChanged(Modifiers::empty())),
]);
captured.first().copied().unwrap_or(false)
}
pub fn tap(&mut self, key: impl Into<Key>) -> bool {
self.press(Modifiers::empty(), key)
}
}
fn build<'a, M>(
element: Element<'a, M>,
cache: Cache,
renderer: &mut Renderer,
) -> UserInterface<'a, M, iced::Theme, Renderer> {
UserInterface::build(element, Size::new(800.0, 600.0), cache, renderer)
}
pub fn press(modifiers: Modifiers, key: Key) -> Event {
let text = match &key {
Key::Character(c) if !modifiers.command() => Some(c.clone()),
_ => None,
};
Event::Keyboard(keyboard::Event::KeyPressed {
key: key.clone(),
modified_key: key,
physical_key: key::Physical::Unidentified(key::NativeCode::Unidentified),
location: keyboard::Location::Standard,
modifiers,
repeat: false,
text,
})
}
fn release(modifiers: Modifiers, key: Key) -> Event {
Event::Keyboard(keyboard::Event::KeyReleased {
key: key.clone(),
modified_key: key,
physical_key: key::Physical::Unidentified(key::NativeCode::Unidentified),
location: keyboard::Location::Standard,
modifiers,
})
}
pub fn letter(c: char) -> Key {
Key::Character(c.to_string().into())
}
pub const COMMAND: Modifiers = Modifiers::COMMAND;
pub fn jump() -> Modifiers {
if cfg!(target_os = "macos") {
Modifiers::ALT
} else {
Modifiers::CTRL
}
}