use std::ops::Not as _;
use iced::widget::{button, column, text};
use iced::{Element, Subscription, Theme};
use icedwright_check::expectations::Pattern;
use icedwright_locator::Scope as _;
use icedwright_probe::semantic;
use icedwright_types::units::{Logical, ScaleFactor};
use icedwright_types::{Frame, Rect, Size, VirtualDuration, role};
use super::report::{ExpectState, outlined};
use super::{ExpectError, MessagePattern, expect};
use crate::Error;
use crate::harness::{Harness, TestProgram};
#[derive(Debug, Clone, PartialEq)]
enum Message {
Show,
Hide,
Count(u32),
}
#[derive(Debug, Default)]
struct App {
shown: bool,
count: u32,
}
impl App {
fn update(&mut self, message: Message) {
match message {
Message::Show => self.shown = true,
Message::Hide => self.shown = false,
Message::Count(n) => self.count = n,
}
}
fn view(&self) -> Element<'_, Message> {
let mut page = column![
semantic(button(text("Show")).on_press(Message::Show))
.role(role::Button)
.name("Show")
];
if self.shown {
page = page.push(text("Shown"));
}
page.into()
}
fn subscription(&self) -> Subscription<Message> {
if !self.shown {
return Subscription::none();
}
iced::time::every(std::time::Duration::from_secs(1)).map(|_| Message::Hide)
}
}
fn harness() -> crate::Result<Harness<impl TestProgram<State = App, Message = Message>>> {
Harness::new(
iced::application(App::default, App::update, App::view)
.subscription(App::subscription)
.theme(Theme::Light),
)
}
#[test]
fn a_pass_on_the_first_attempt_moves_no_time() -> crate::Result {
let mut ui = harness()?;
let before = ui.now();
expect(ui.get_by_role(role::Button).name("Show")).to_be_visible()?;
assert_eq!(ui.now(), before);
Ok(())
}
#[test]
fn an_idle_failure_reaches_the_deadline_in_one_jump() -> crate::Result {
let mut ui = harness()?;
let before = ui.now();
let result = expect(ui.get_by_text("Shown"))
.timeout(VirtualDuration::from_secs(2))
.to_be_visible();
let Err(Error::Expect(error)) = result else {
panic!("{result:?}");
};
assert_eq!(ui.now() - before, VirtualDuration::from_secs(2));
assert!(error.to_string().contains("2 attempts"), "{error}");
Ok(())
}
#[test]
fn not_twice_is_the_matcher_again() -> crate::Result {
let mut ui = harness()?;
expect(ui.get_by_text("Shown")).not().to_be_visible()?;
expect(ui.get_by_role(role::Button))
.not()
.not()
.to_be_visible()?;
Ok(())
}
#[test]
fn a_wait_wakes_at_the_apps_own_timer() -> crate::Result {
let mut ui = harness()?;
ui.dispatch(Message::Show)?;
let shown = ui.now();
expect(ui.get_by_text("Shown")).to_be_hidden()?;
assert_eq!(ui.now() - shown, VirtualDuration::from_secs(1));
Ok(())
}
#[test]
fn every_non_widget_expectation_runs_through_the_loop() -> crate::Result {
let mut ui = harness()?;
let mark = ui.mark();
ui.dispatch(Message::Count(3))?;
ui.expect_messages()
.since(mark)
.to_contain(Message::Count(3))?;
ui.expect_state(|app| app.count).to_equal(3)?;
ui.expect_clipboard().to_be_empty()?;
ui.expect_tasks().to_be_idle()?;
ui.expect_windows().to_have_count(1)?;
let before = ui.now();
let failed = ui
.expect_state(|app| app.count)
.timeout(VirtualDuration::from_millis(300))
.to_equal(4);
assert!(matches!(failed, Err(Error::Expect(_))), "{failed:?}");
assert_eq!(ui.now() - before, VirtualDuration::from_millis(300));
Ok(())
}
#[test]
fn every_non_widget_expectation_takes_during() -> crate::Result {
let mut ui = harness()?;
let window = VirtualDuration::from_millis(400);
let before = ui.now();
ui.expect_tasks().during(window).to_be_idle()?;
ui.expect_clipboard().during(window).to_be_empty()?;
ui.expect_windows().during(window).to_have_count(1)?;
ui.expect_state(|app| app.count)
.during(window)
.to_equal(0)?;
ui.expect_messages().during(window).to_be_empty()?;
assert_eq!(ui.now() - before, VirtualDuration::from_millis(2000));
ui.dispatch(Message::Show)?;
let broken = ui
.expect_state(|app| app.shown)
.during(VirtualDuration::from_secs(2))
.to_equal(true);
assert!(matches!(broken, Err(Error::Expect(_))), "{broken:?}");
Ok(())
}
#[test]
fn a_message_pattern_keeps_its_source_and_its_captures() {
let wanted = 3;
let pattern: MessagePattern<Message> = crate::message!(Message::Count(n) if *n == wanted);
assert!(pattern.matches(&Message::Count(3)));
assert!(!pattern.matches(&Message::Count(4)));
assert!(!pattern.matches(&Message::Show));
assert_eq!(pattern.text(), "Message::Count(n) if *n == wanted");
let either: MessagePattern<Message> = crate::message!(Message::Show | Message::Hide);
assert!(either.matches(&Message::Hide));
}
#[test]
fn soft_failures_are_taken_once() {
let mut state = ExpectState::default();
assert!(state.take_soft().is_none());
state.record_soft(ExpectError::Soft(Vec::new()));
state.record_soft(ExpectError::Soft(Vec::new()));
let Some(ExpectError::Soft(all)) = state.take_soft() else {
panic!("two soft failures were recorded");
};
assert_eq!(all.len(), 2);
assert!(state.take_soft().is_none());
state.report_unfinished();
}
#[test]
fn the_screenshot_outlines_the_node_at_the_scale_factor() -> crate::Result {
let scale = ScaleFactor::new(2.0)?;
let size = Logical(Size::new(10.0, 10.0)).to_physical_pixels(scale);
let frame = Frame::new(vec![0; 20 * 20 * 4], size, scale)?;
let bounds = Rect::new(icedwright_types::Point::new(2.0, 2.0), Size::new(5.0, 5.0));
let outlined = outlined(frame, Logical(bounds))?;
assert_eq!(
outlined.pixel(4, 4),
Some([230, 20, 60, 255]),
"the corner is drawn"
);
assert_eq!(
outlined.pixel(8, 8),
Some([0, 0, 0, 0]),
"the inside is left alone"
);
assert_eq!(
outlined.pixel(0, 0),
Some([0, 0, 0, 0]),
"outside is left alone"
);
Ok(())
}