icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! The harness as an [`icedwright_check::Driver`]: assertions settle, read the tree and wait
//! on virtual time, with the real clock only bounding a wait on declared external sources.

use std::sync::Arc;

use iced_core::window;
use icedwright_check::{Driver, Waited};
use icedwright_semantics::SemanticTree;
use icedwright_types::{VirtualDuration, VirtualInstant};

use crate::error::{Error, HarnessError, Result};
use crate::harness::{Harness, TestProgram};

impl<P: TestProgram> Driver for Harness<P> {
    type Error = Error;
    type Instant = VirtualInstant;
    type Duration = VirtualDuration;
    type RealInstant = std::time::Instant;

    fn settle(&mut self) -> Result {
        self.run_until_settled()
    }

    fn tree(&mut self) -> Result<&SemanticTree> {
        let _ = Harness::tree(self)?;
        self.trees
            .as_ref()
            .map(|(_, _, tree)| &**tree)
            .ok_or_else(|| HarnessError::NoWindow.into())
    }

    fn now(&self) -> VirtualInstant {
        Harness::now(self)
    }

    fn deadline(&self, after: VirtualDuration) -> VirtualInstant {
        Harness::now(self) + after
    }

    fn real_now(&self) -> std::time::Instant {
        Harness::real_now(self)
    }

    fn wait(
        &mut self,
        deadline: VirtualInstant,
        began: std::time::Instant,
    ) -> Result<Waited<VirtualInstant>> {
        Harness::wait(self, deadline, began)
    }

    fn wait_external(&mut self, began: std::time::Instant) -> Result<Waited<VirtualInstant>> {
        Harness::wait_external(self, began)
    }
}

/// The harness seen through one window, or every window, for a handle's assertions.
pub(crate) struct Scoped<'a, P: TestProgram> {
    pub(crate) harness: &'a mut Harness<P>,
    pub(crate) window: Option<window::Id>,
    tree: Option<Arc<SemanticTree>>,
}

impl<'a, P: TestProgram> Scoped<'a, P> {
    pub(crate) fn new(harness: &'a mut Harness<P>, window: Option<window::Id>) -> Self {
        Self {
            harness,
            window,
            tree: None,
        }
    }

    /// The tree of the last attempt.
    pub(crate) fn last_tree(&self) -> Option<Arc<SemanticTree>> {
        self.tree.clone()
    }
}

impl<P: TestProgram> Driver for Scoped<'_, P> {
    type Error = Error;
    type Instant = VirtualInstant;
    type Duration = VirtualDuration;
    type RealInstant = std::time::Instant;

    fn settle(&mut self) -> Result {
        self.harness.run_until_settled()
    }

    fn tree(&mut self) -> Result<&SemanticTree> {
        let tree = match self.window {
            Some(window) => self.harness.tree_of(window)?,
            None => Harness::tree(self.harness)?,
        };
        Ok(&**self.tree.insert(tree))
    }

    fn now(&self) -> VirtualInstant {
        self.harness.now()
    }

    fn deadline(&self, after: VirtualDuration) -> VirtualInstant {
        self.harness.now() + after
    }

    fn real_now(&self) -> std::time::Instant {
        self.harness.real_now()
    }

    fn wait(
        &mut self,
        deadline: VirtualInstant,
        began: std::time::Instant,
    ) -> Result<Waited<VirtualInstant>> {
        self.harness.wait(deadline, began)
    }

    fn wait_external(&mut self, began: std::time::Instant) -> Result<Waited<VirtualInstant>> {
        self.harness.wait_external(began)
    }
}