icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! Raw-event helpers for the harness's own end-to-end tests. The actions with actionability
//! checks live in a later module; these tests only need a press in the middle of a node.

use iced::mouse;
use iced::{Event, Point};
use icedwright::prelude::*;
use icedwright::{Resolved, RoleKind};

/// The centre of a resolved node, in window coordinates.
pub fn centre(resolved: &Resolved) -> icedwright::Result<Point> {
    let node = resolved
        .node()
        .ok_or_else(|| icedwright::Error::other(std::io::Error::other("node not in its tree")))?;
    let centre = node.bounds.center();
    Ok(Point::new(centre.x, centre.y))
}

/// Moves the pointer to the centre of the handle's node and presses and releases the left
/// button there.
pub fn click<P: TestProgram, R: RoleKind>(mut handle: Handle<'_, P, R>) -> icedwright::Result {
    let resolved = handle.resolve()?;
    let position = centre(&resolved)?;
    let window = handle.window();
    let harness = handle.harness();
    let events = [
        Event::Mouse(mouse::Event::CursorMoved { position }),
        Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
        Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)),
    ];
    match window {
        Some(window) => harness.send_to(window, &events)?,
        None => harness.send(&events)?,
    };
    Ok(())
}

/// Whether some node in the tree has exactly this text.
pub fn shows<P: TestProgram>(harness: &mut Harness<P>, text: &str) -> icedwright::Result<bool> {
    Ok(harness.get_by_text(text).count()? > 0)
}