use std::fmt;
use std::sync::Arc;
use iced_core::window;
use icedwright_locator::{
AnyRole, IntoLocator, IntoTestId, Locator, RoleKind, RoleType, Scope, TextMatch, role,
};
use icedwright_semantics::{Node, NodeId, SemanticTree};
use super::{Harness, TestProgram};
use crate::error::Result;
#[must_use]
pub struct Handle<'h, P: TestProgram, R: RoleKind = AnyRole> {
pub(crate) harness: &'h mut Harness<P>,
pub(crate) locator: Locator<R>,
pub(crate) window: Option<window::Id>,
}
#[derive(Debug, Clone)]
pub struct Resolved {
pub tree: Arc<SemanticTree>,
pub id: NodeId,
}
impl Resolved {
#[must_use]
pub fn node(&self) -> Option<&Node> {
self.tree.get(self.id)
}
}
impl<P: TestProgram, R: RoleKind> fmt::Debug for Handle<'_, P, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Handle")
.field("locator", &self.locator.to_string())
.field("window", &self.window)
.finish_non_exhaustive()
}
}
impl<'h, P: TestProgram, R: RoleKind> Handle<'h, P, R> {
pub(crate) fn new(
harness: &'h mut Harness<P>,
locator: Locator<R>,
window: Option<window::Id>,
) -> Self {
Self {
harness,
locator,
window,
}
}
pub fn locator(&self) -> &Locator<R> {
&self.locator
}
#[must_use]
pub fn window(&self) -> Option<window::Id> {
self.window
}
pub fn harness(&mut self) -> &mut Harness<P> {
self.harness
}
pub(crate) fn current_tree(&mut self) -> Result<Arc<SemanticTree>> {
match self.window {
Some(window) => self.harness.tree_of(window),
None => self.harness.tree(),
}
}
pub fn resolve(&mut self) -> Result<Resolved> {
self.harness.run_until_settled()?;
let tree = self.current_tree()?;
let id = icedwright_locator::resolve(&tree, &self.locator)?;
Ok(Resolved { tree, id })
}
pub fn count(&mut self) -> Result<usize> {
self.harness.run_until_settled()?;
let tree = self.current_tree()?;
Ok(self.locator.count(&tree))
}
}
impl<'h, P: TestProgram, R: RoleKind> Scope for Handle<'h, P, R> {
type Role = R;
type With<R2: RoleKind> = Handle<'h, P, R2>;
fn map_locator<R2: RoleKind>(
self,
f: impl FnOnce(Locator<R>) -> Locator<R2>,
) -> Handle<'h, P, R2> {
Handle {
harness: self.harness,
locator: f(self.locator),
window: self.window,
}
}
fn as_locator(&self) -> &Locator<R> {
&self.locator
}
}
impl<P: TestProgram> Harness<P> {
pub fn locate<R: RoleKind>(&mut self, locator: impl IntoLocator<R>) -> Handle<'_, P, R> {
Handle::new(self, locator.into_locator(), None)
}
pub fn page(&mut self) -> Handle<'_, P, role::Window> {
let window = self.focused();
Handle::new(self, icedwright_locator::page(), window)
}
pub fn get_by_role<R: RoleType>(&mut self, role: R) -> Handle<'_, P, R> {
Handle::new(self, icedwright_locator::get_by_role(role), None)
}
pub fn get_by_text(&mut self, text: impl Into<TextMatch>) -> Handle<'_, P, AnyRole> {
Handle::new(self, icedwright_locator::get_by_text(text), None)
}
pub fn get_by_label(&mut self, text: impl Into<TextMatch>) -> Handle<'_, P, AnyRole> {
Handle::new(self, icedwright_locator::get_by_label(text), None)
}
pub fn get_by_placeholder(&mut self, text: impl Into<TextMatch>) -> Handle<'_, P, AnyRole> {
Handle::new(self, icedwright_locator::get_by_placeholder(text), None)
}
pub fn get_by_tooltip(&mut self, text: impl Into<TextMatch>) -> Handle<'_, P, AnyRole> {
Handle::new(self, icedwright_locator::get_by_tooltip(text), None)
}
pub fn get_by_test_id<'a>(&mut self, id: impl IntoTestId<'a>) -> Handle<'_, P, AnyRole> {
Handle::new(self, icedwright_locator::get_by_test_id(id), None)
}
pub fn get_by_widget_id(&mut self, id: impl Into<String>) -> Handle<'_, P, AnyRole> {
Handle::new(self, icedwright_locator::get_by_widget_id(id), None)
}
}