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)
}
}
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,
}
}
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)
}
}