icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! The option builders actions take: [`Click`], [`Hover`], [`Drag`], [`Type`] and
//! [`OptionMatch`].

use std::num::NonZeroU16;

use iced_core::mouse;
use icedwright_check::ClickCount;
use icedwright_locator::TextMatch;
use icedwright_types::{Logical, ModifierSet, Point, VirtualDuration};

/// How a click is sent. The default is a single left click at the centre of the node's
/// visible part, with every actionability check, waiting for the app to settle afterwards.
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct Click {
    pub(crate) button: mouse::Button,
    pub(crate) count: ClickCount,
    pub(crate) position: Option<Logical<Point>>,
    pub(crate) modifiers: ModifierSet,
    pub(crate) force: bool,
    pub(crate) trial: bool,
    pub(crate) no_wait_after: bool,
    pub(crate) timeout: Option<VirtualDuration>,
    pub(crate) steps: NonZeroU16,
}

impl Default for Click {
    fn default() -> Self {
        Self {
            button: mouse::Button::Left,
            count: ClickCount::Single,
            position: None,
            modifiers: ModifierSet::NONE,
            force: false,
            trial: false,
            no_wait_after: false,
            timeout: None,
            steps: NonZeroU16::MIN,
        }
    }
}

impl Click {
    /// A single left click at the centre.
    pub fn new() -> Self {
        Self::default()
    }

    /// The mouse button.
    pub fn button(mut self, button: mouse::Button) -> Self {
        self.button = button;
        self
    }

    /// How many presses.
    pub fn count(mut self, count: ClickCount) -> Self {
        self.count = count;
        self
    }

    /// Where to press, relative to the node's top-left corner. The press is then never moved
    /// by click separation.
    pub fn position(mut self, at: impl Into<Logical<Point>>) -> Self {
        self.position = Some(at.into());
        self
    }

    /// Modifiers held for the whole click.
    pub fn modifiers(mut self, modifiers: ModifierSet) -> Self {
        self.modifiers = modifiers;
        self
    }

    /// Skips every actionability check except that the node is attached; still scrolls.
    pub fn force(mut self) -> Self {
        self.force = true;
        self
    }

    /// Runs the checks and the scroll and sends no input.
    pub fn trial(mut self) -> Self {
        self.trial = true;
        self
    }

    /// Returns as soon as the input is delivered, before the app settles.
    pub fn no_wait_after(mut self) -> Self {
        self.no_wait_after = true;
        self
    }

    /// How long to wait for the node to become actionable, in virtual time.
    pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
        self.timeout = Some(timeout.into());
        self
    }

    /// `CursorMoved` events on the way to the point, one per frame.
    pub fn steps(mut self, steps: NonZeroU16) -> Self {
        self.steps = steps;
        self
    }
}

/// How a hover is sent.
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct Hover {
    pub(crate) position: Option<Logical<Point>>,
    pub(crate) force: bool,
    pub(crate) trial: bool,
    pub(crate) timeout: Option<VirtualDuration>,
    pub(crate) steps: NonZeroU16,
}

impl Default for Hover {
    fn default() -> Self {
        Self {
            position: None,
            force: false,
            trial: false,
            timeout: None,
            steps: NonZeroU16::MIN,
        }
    }
}

impl Hover {
    /// A hover at the centre.
    pub fn new() -> Self {
        Self::default()
    }

    /// Where to rest, relative to the node's top-left corner.
    pub fn position(mut self, at: impl Into<Logical<Point>>) -> Self {
        self.position = Some(at.into());
        self
    }

    /// Skips every actionability check except that the node is attached.
    pub fn force(mut self) -> Self {
        self.force = true;
        self
    }

    /// Runs the checks and sends no input.
    pub fn trial(mut self) -> Self {
        self.trial = true;
        self
    }

    /// How long to wait for the node, in virtual time.
    pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
        self.timeout = Some(timeout.into());
        self
    }

    /// `CursorMoved` events on the way, one per frame.
    pub fn steps(mut self, steps: NonZeroU16) -> Self {
        self.steps = steps;
        self
    }
}

/// The drag steps crossed by default, enough for the drag thresholds iced-cube uses.
const DRAG_STEPS: NonZeroU16 = match NonZeroU16::new(5) {
    Some(steps) => steps,
    None => NonZeroU16::MIN,
};

/// How a drag is sent.
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct Drag {
    pub(crate) source: Option<Logical<Point>>,
    pub(crate) target: Option<Logical<Point>>,
    pub(crate) steps: NonZeroU16,
    pub(crate) modifiers: ModifierSet,
    pub(crate) force: bool,
    pub(crate) timeout: Option<VirtualDuration>,
}

impl Default for Drag {
    fn default() -> Self {
        Self {
            source: None,
            target: None,
            steps: DRAG_STEPS,
            modifiers: ModifierSet::NONE,
            force: false,
            timeout: None,
        }
    }
}

impl Drag {
    /// A drag from centre to centre in five steps.
    pub fn new() -> Self {
        Self::default()
    }

    /// Where to press, relative to the source's top-left corner.
    pub fn source_position(mut self, at: impl Into<Logical<Point>>) -> Self {
        self.source = Some(at.into());
        self
    }

    /// Where to release, relative to the target's top-left corner.
    pub fn target_position(mut self, at: impl Into<Logical<Point>>) -> Self {
        self.target = Some(at.into());
        self
    }

    /// `CursorMoved` events between press and release, one per frame.
    pub fn steps(mut self, steps: NonZeroU16) -> Self {
        self.steps = steps;
        self
    }

    /// Modifiers held for the whole drag.
    pub fn modifiers(mut self, modifiers: ModifierSet) -> Self {
        self.modifiers = modifiers;
        self
    }

    /// Skips every actionability check except that both nodes are attached.
    pub fn force(mut self) -> Self {
        self.force = true;
        self
    }

    /// How long to wait for both nodes, in virtual time.
    pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
        self.timeout = Some(timeout.into());
        self
    }
}

/// How text is typed key by key.
#[derive(Debug, Clone, PartialEq, Default)]
#[must_use]
pub struct Type {
    pub(crate) delay: VirtualDuration,
    pub(crate) layout: Option<icedwright_check::KeyboardLayout>,
    pub(crate) timeout: Option<VirtualDuration>,
}

impl Type {
    /// No delay between keys, on the harness's keyboard layout.
    pub fn new() -> Self {
        Self::default()
    }

    /// Virtual time between keys.
    pub fn delay(mut self, delay: impl Into<VirtualDuration>) -> Self {
        self.delay = delay.into();
        self
    }

    /// A layout for this call only.
    pub fn layout(mut self, layout: icedwright_check::KeyboardLayout) -> Self {
        self.layout = Some(layout);
        self
    }

    /// How long to wait for the field, in virtual time.
    pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
        self.timeout = Some(timeout.into());
        self
    }
}

/// Which option `select_option` picks.
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub enum OptionMatch {
    /// The option whose accessible name, or else whole text, matches.
    Name(TextMatch),
    /// The option at this position, counted from 0.
    Index(usize),
    /// The annotated option whose value is this.
    Value(String),
}

impl OptionMatch {
    /// The option at `index`, counted from 0.
    pub fn index(index: usize) -> Self {
        Self::Index(index)
    }

    /// The option whose annotated value is `value`.
    pub fn value(value: impl Into<String>) -> Self {
        Self::Value(value.into())
    }

    /// Whether the option with this name, text and value, at this position, is the one.
    #[must_use]
    pub(crate) fn picks(
        &self,
        index: usize,
        name: Option<&str>,
        text: &str,
        value: Option<&str>,
    ) -> bool {
        match self {
            OptionMatch::Name(wanted) => name.map_or_else(
                || wanted.matches(text),
                |n| wanted.matches(n) || wanted.matches(text),
            ),
            OptionMatch::Index(wanted) => *wanted == index,
            OptionMatch::Value(wanted) => value == Some(wanted.as_str()),
        }
    }
}

impl From<&str> for OptionMatch {
    fn from(name: &str) -> Self {
        Self::Name(name.into())
    }
}

impl From<String> for OptionMatch {
    fn from(name: String) -> Self {
        Self::Name(name.into())
    }
}

impl From<TextMatch> for OptionMatch {
    fn from(name: TextMatch) -> Self {
        Self::Name(name)
    }
}

impl std::fmt::Display for OptionMatch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OptionMatch::Name(name) => write!(f, "the option named {name}"),
            OptionMatch::Index(index) => write!(f, "option {index}"),
            OptionMatch::Value(value) => write!(f, "the option with value {value:?}"),
        }
    }
}