From d09d5d45ae4697eef277dfe30756b91c7d802a94 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 3 Dec 2024 22:03:06 +0100 Subject: Draft `iced_test` crate and test `todos` example --- examples/todos/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 25e3ead2..8772bb80 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -584,3 +584,36 @@ impl SavedState { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + use iced::test; + use iced::test::selector; + + #[test] + fn it_creates_a_new_task() { + let (mut todos, _command) = Todos::new(); + let _command = todos.update(Message::Loaded(Err(LoadError::File))); + + let mut interface = test::interface(todos.view()); + + let _input = interface + .click("new-task") + .expect("new-task input must be present"); + + interface.typewrite("Create the universe"); + interface.press_key(keyboard::key::Named::Enter); + + for message in interface.into_messages() { + let _command = todos.update(message); + } + + let mut interface = test::interface(todos.view()); + + let _ = interface + .find(selector::text("Create the universe")) + .expect("New task must be present"); + } +} -- cgit From 8e3636d769d96ab5ba49a9647b72c59ae2226dd0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 4 Dec 2024 00:29:36 +0100 Subject: Return `Result` in `todos` test --- examples/todos/src/main.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 8772bb80..51d09962 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -593,15 +593,12 @@ mod tests { use iced::test::selector; #[test] - fn it_creates_a_new_task() { + fn it_creates_a_new_task() -> Result<(), test::Error> { let (mut todos, _command) = Todos::new(); let _command = todos.update(Message::Loaded(Err(LoadError::File))); let mut interface = test::interface(todos.view()); - - let _input = interface - .click("new-task") - .expect("new-task input must be present"); + let _input = interface.click("new-task")?; interface.typewrite("Create the universe"); interface.press_key(keyboard::key::Named::Enter); @@ -611,9 +608,8 @@ mod tests { } let mut interface = test::interface(todos.view()); + let _ = interface.find(selector::text("Create the universe"))?; - let _ = interface - .find(selector::text("Create the universe")) - .expect("New task must be present"); + Ok(()) } } -- cgit From 1aeb317f2dbfb63215e6226073e67878ffa6503b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 6 Dec 2024 04:06:41 +0100 Subject: Add image and hash snapshot-based testing to `iced_test` --- examples/todos/src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 51d09962..ff38c6ce 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -15,7 +15,7 @@ pub fn main() -> iced::Result { iced::application(Todos::title, Todos::update, Todos::view) .subscription(Todos::subscription) - .font(include_bytes!("../fonts/icons.ttf").as_slice()) + .font(Todos::ICON_FONT) .window_size((500.0, 800.0)) .run_with(Todos::new) } @@ -48,6 +48,8 @@ enum Message { } impl Todos { + const ICON_FONT: &[u8] = include_bytes!("../fonts/icons.ttf"); + fn new() -> (Self, Command) { ( Self::Loading, @@ -449,11 +451,10 @@ fn empty_message(message: &str) -> Element<'_, Message> { } // Fonts -const ICONS: Font = Font::with_name("Iced-Todos-Icons"); fn icon(unicode: char) -> Text<'static> { text(unicode.to_string()) - .font(ICONS) + .font(Font::with_name("Iced-Todos-Icons")) .width(20) .align_x(Center) } @@ -594,6 +595,8 @@ mod tests { #[test] fn it_creates_a_new_task() -> Result<(), test::Error> { + test::load_font(Todos::ICON_FONT)?; + let (mut todos, _command) = Todos::new(); let _command = todos.update(Message::Loaded(Err(LoadError::File))); @@ -610,6 +613,12 @@ mod tests { let mut interface = test::interface(todos.view()); let _ = interface.find(selector::text("Create the universe"))?; + let snapshot = interface.snapshot()?; + assert!( + snapshot.matches_hash("snapshots/creates_a_new_task")?, + "snapshots should match!" + ); + Ok(()) } } -- cgit From 1713ac49f2ae794f78f24c01d6c21625b2c2879c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Dec 2024 04:56:37 +0100 Subject: Decouple `iced_test` from `iced` crate --- examples/todos/src/main.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index ff38c6ce..f4b8d786 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -590,30 +590,29 @@ impl SavedState { mod tests { use super::*; - use iced::test; - use iced::test::selector; + use iced_test::{interface, load_font, selector, Error}; #[test] - fn it_creates_a_new_task() -> Result<(), test::Error> { - test::load_font(Todos::ICON_FONT)?; + fn it_creates_a_new_task() -> Result<(), Error> { + load_font(Todos::ICON_FONT)?; let (mut todos, _command) = Todos::new(); let _command = todos.update(Message::Loaded(Err(LoadError::File))); - let mut interface = test::interface(todos.view()); - let _input = interface.click("new-task")?; + let mut ui = interface(todos.view()); + let _input = ui.click("new-task")?; - interface.typewrite("Create the universe"); - interface.press_key(keyboard::key::Named::Enter); + ui.typewrite("Create the universe"); + ui.press_key(keyboard::key::Named::Enter); - for message in interface.into_messages() { + for message in ui.into_messages() { let _command = todos.update(message); } - let mut interface = test::interface(todos.view()); - let _ = interface.find(selector::text("Create the universe"))?; + let mut ui = interface(todos.view()); + let _ = ui.find(selector::text("Create the universe"))?; - let snapshot = interface.snapshot()?; + let snapshot = ui.snapshot()?; assert!( snapshot.matches_hash("snapshots/creates_a_new_task")?, "snapshots should match!" -- cgit From 9bc29e5347c42fb53ac0a99b761cada6bc48a4e8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Dec 2024 05:00:33 +0100 Subject: Add explicit `'static` in `todos` for Rust 1.80 --- examples/todos/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index f4b8d786..1d4aa8a4 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -48,7 +48,7 @@ enum Message { } impl Todos { - const ICON_FONT: &[u8] = include_bytes!("../fonts/icons.ttf"); + const ICON_FONT: &'static [u8] = include_bytes!("../fonts/icons.ttf"); fn new() -> (Self, Command) { ( -- cgit From 2cf4abf25bb5702635c19a22353399db8cef7be3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2024 03:49:24 +0100 Subject: Support custom renderers in `iced_test` through `renderer::Headless` trait --- examples/todos/src/main.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 1d4aa8a4..fe23cbc9 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -590,16 +590,25 @@ impl SavedState { mod tests { use super::*; - use iced_test::{interface, load_font, selector, Error}; + use iced::Settings; + use iced_test::{selector, Error, Simulator}; + + fn simulator(todos: &Todos) -> Simulator { + Simulator::with_settings( + Settings { + fonts: vec![Todos::ICON_FONT.into()], + ..Settings::default() + }, + todos.view(), + ) + } #[test] fn it_creates_a_new_task() -> Result<(), Error> { - load_font(Todos::ICON_FONT)?; - let (mut todos, _command) = Todos::new(); let _command = todos.update(Message::Loaded(Err(LoadError::File))); - let mut ui = interface(todos.view()); + let mut ui = simulator(&todos); let _input = ui.click("new-task")?; ui.typewrite("Create the universe"); @@ -609,7 +618,7 @@ mod tests { let _command = todos.update(message); } - let mut ui = interface(todos.view()); + let mut ui = simulator(&todos); let _ = ui.find(selector::text("Create the universe"))?; let snapshot = ui.snapshot()?; -- cgit From 869b44db4ec8c946c8e5eab2498bbc3a501418b1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 17 Dec 2024 01:37:00 +0100 Subject: Implement `Simulator::simulate` and polish naming --- examples/todos/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index fe23cbc9..1cc47782 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -611,8 +611,8 @@ mod tests { let mut ui = simulator(&todos); let _input = ui.click("new-task")?; - ui.typewrite("Create the universe"); - ui.press_key(keyboard::key::Named::Enter); + let _ = ui.typewrite("Create the universe"); + let _ = ui.tap_key(keyboard::key::Named::Enter); for message in ui.into_messages() { let _command = todos.update(message); -- cgit From 0ad40d03387a8127b445305a1c63fa3d2ac45ed7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 17 Dec 2024 01:53:39 +0100 Subject: Reduce size of `Simulator` in `todos` test --- examples/todos/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 1cc47782..0b80f002 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -594,11 +594,12 @@ mod tests { use iced_test::{selector, Error, Simulator}; fn simulator(todos: &Todos) -> Simulator { - Simulator::with_settings( + Simulator::with_size( Settings { fonts: vec![Todos::ICON_FONT.into()], ..Settings::default() }, + (512.0, 512.0), todos.view(), ) } -- cgit From 2f98a7e2032715409891e6d2c9f8529cfed59569 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 17 Dec 2024 02:17:07 +0100 Subject: Append `env::consts::OS` to snapshot filenames --- examples/todos/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 0b80f002..1cc47782 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -594,12 +594,11 @@ mod tests { use iced_test::{selector, Error, Simulator}; fn simulator(todos: &Todos) -> Simulator { - Simulator::with_size( + Simulator::with_settings( Settings { fonts: vec![Todos::ICON_FONT.into()], ..Settings::default() }, - (512.0, 512.0), todos.view(), ) } -- cgit From 41a822c6fb6dd15c9e2246a6f0d136d83c6c7d00 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 17 Dec 2024 02:27:13 +0100 Subject: Use proper hash for `creates_a_new_task` snapshot --- examples/todos/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 1cc47782..45034d6c 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -590,7 +590,7 @@ impl SavedState { mod tests { use super::*; - use iced::Settings; + use iced::{Settings, Theme}; use iced_test::{selector, Error, Simulator}; fn simulator(todos: &Todos) -> Simulator { @@ -621,7 +621,7 @@ mod tests { let mut ui = simulator(&todos); let _ = ui.find(selector::text("Create the universe"))?; - let snapshot = ui.snapshot()?; + let snapshot = ui.snapshot(&Theme::Dark)?; assert!( snapshot.matches_hash("snapshots/creates_a_new_task")?, "snapshots should match!" -- cgit From 5220a064c5054e872fd2f8922aa83838bf066949 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 17 Dec 2024 04:13:19 +0100 Subject: Write documentation for `iced_test` --- examples/todos/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples/todos/src') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 45034d6c..a5bca235 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -591,7 +591,8 @@ mod tests { use super::*; use iced::{Settings, Theme}; - use iced_test::{selector, Error, Simulator}; + use iced_test::selector::text; + use iced_test::{Error, Simulator}; fn simulator(todos: &Todos) -> Simulator { Simulator::with_settings( @@ -619,7 +620,7 @@ mod tests { } let mut ui = simulator(&todos); - let _ = ui.find(selector::text("Create the universe"))?; + let _ = ui.find(text("Create the universe"))?; let snapshot = ui.snapshot(&Theme::Dark)?; assert!( -- cgit