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/Cargo.toml | 3 +++ examples/todos/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'examples/todos') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 0d72be86..65c34a8f 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -26,6 +26,9 @@ uuid = { version = "1.0", features = ["js"] } web-sys = { workspace = true, features = ["Window", "Storage"] } wasm-timer.workspace = true +[dev-dependencies] +iced_test.workspace = true + [package.metadata.deb] assets = [ ["target/release-opt/todos", "usr/bin/iced-todos", "755"], 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') 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/snapshots/creates_a_new_task.sha256 | 1 + examples/todos/src/main.rs | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 examples/todos/snapshots/creates_a_new_task.sha256 (limited to 'examples/todos') diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 new file mode 100644 index 00000000..fcfee315 --- /dev/null +++ b/examples/todos/snapshots/creates_a_new_task.sha256 @@ -0,0 +1 @@ +5047c10c4ea050393b686185b8d03b3c3738b47182f9892fb68df589ec46bd4b \ No newline at end of file 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') 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') 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 7683bd201f156c6e80309584a9bdc205810b0e2e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Dec 2024 05:10:04 +0100 Subject: Stop tracking `todos` snapshot hash file Font selection is platform-dependent. --- examples/todos/snapshots/creates_a_new_task.sha256 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 examples/todos/snapshots/creates_a_new_task.sha256 (limited to 'examples/todos') diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 deleted file mode 100644 index fcfee315..00000000 --- a/examples/todos/snapshots/creates_a_new_task.sha256 +++ /dev/null @@ -1 +0,0 @@ -5047c10c4ea050393b686185b8d03b3c3738b47182f9892fb68df589ec46bd4b \ No newline at end of file -- cgit From 6572909ab5b004176f6d261b67b4caa99f1f54bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Dec 2024 03:14:40 +0100 Subject: Embed and use Fira Sans as default font when testing --- examples/todos/Cargo.toml | 2 +- examples/todos/snapshots/creates_a_new_task.sha256 | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 examples/todos/snapshots/creates_a_new_task.sha256 (limited to 'examples/todos') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 65c34a8f..16f4fdd2 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -20,7 +20,7 @@ tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] iced.workspace = true -iced.features = ["debug", "webgl"] +iced.features = ["debug", "webgl", "fira-sans"] uuid = { version = "1.0", features = ["js"] } web-sys = { workspace = true, features = ["Window", "Storage"] } diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 new file mode 100644 index 00000000..75d5f2ce --- /dev/null +++ b/examples/todos/snapshots/creates_a_new_task.sha256 @@ -0,0 +1 @@ +b41c73d214894bf5f94f787e5f265cff6500822b2d4a29a4ac0c847a71db7123 \ No newline at end of file -- 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/snapshots/creates_a_new_task.sha256 | 2 +- examples/todos/src/main.rs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'examples/todos') diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 index 75d5f2ce..15efcd77 100644 --- a/examples/todos/snapshots/creates_a_new_task.sha256 +++ b/examples/todos/snapshots/creates_a_new_task.sha256 @@ -1 +1 @@ -b41c73d214894bf5f94f787e5f265cff6500822b2d4a29a4ac0c847a71db7123 \ No newline at end of file +a7c2ac4b57f84416812e2134e48fe34db55a757d9176beedf5854a2f69532e32 \ No newline at end of file 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') 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/snapshots/creates_a_new_task.sha256 | 2 +- examples/todos/src/main.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'examples/todos') diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 index 15efcd77..75d5f2ce 100644 --- a/examples/todos/snapshots/creates_a_new_task.sha256 +++ b/examples/todos/snapshots/creates_a_new_task.sha256 @@ -1 +1 @@ -a7c2ac4b57f84416812e2134e48fe34db55a757d9176beedf5854a2f69532e32 \ No newline at end of file +b41c73d214894bf5f94f787e5f265cff6500822b2d4a29a4ac0c847a71db7123 \ No newline at end of file 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/snapshots/creates_a_new_task-linux.sha256 | 1 + examples/todos/snapshots/creates_a_new_task.sha256 | 1 - examples/todos/src/main.rs | 3 +-- 3 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 examples/todos/snapshots/creates_a_new_task-linux.sha256 delete mode 100644 examples/todos/snapshots/creates_a_new_task.sha256 (limited to 'examples/todos') diff --git a/examples/todos/snapshots/creates_a_new_task-linux.sha256 b/examples/todos/snapshots/creates_a_new_task-linux.sha256 new file mode 100644 index 00000000..9291711e --- /dev/null +++ b/examples/todos/snapshots/creates_a_new_task-linux.sha256 @@ -0,0 +1 @@ +e0b1f2e0c0af6324eb45fde8e82384d16acc2a80a9e157bdf3f42ac6548181cf \ No newline at end of file diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 deleted file mode 100644 index 75d5f2ce..00000000 --- a/examples/todos/snapshots/creates_a_new_task.sha256 +++ /dev/null @@ -1 +0,0 @@ -b41c73d214894bf5f94f787e5f265cff6500822b2d4a29a4ac0c847a71db7123 \ No newline at end of file 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/snapshots/creates_a_new_task-linux.sha256 | 1 - examples/todos/snapshots/creates_a_new_task.sha256 | 1 + examples/todos/src/main.rs | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 examples/todos/snapshots/creates_a_new_task-linux.sha256 create mode 100644 examples/todos/snapshots/creates_a_new_task.sha256 (limited to 'examples/todos') diff --git a/examples/todos/snapshots/creates_a_new_task-linux.sha256 b/examples/todos/snapshots/creates_a_new_task-linux.sha256 deleted file mode 100644 index 9291711e..00000000 --- a/examples/todos/snapshots/creates_a_new_task-linux.sha256 +++ /dev/null @@ -1 +0,0 @@ -e0b1f2e0c0af6324eb45fde8e82384d16acc2a80a9e157bdf3f42ac6548181cf \ No newline at end of file diff --git a/examples/todos/snapshots/creates_a_new_task.sha256 b/examples/todos/snapshots/creates_a_new_task.sha256 new file mode 100644 index 00000000..193132c5 --- /dev/null +++ b/examples/todos/snapshots/creates_a_new_task.sha256 @@ -0,0 +1 @@ +3160686067cb7c738802009cdf2f3c5f5a5bd8c89ada70517388b7adbe64c313 \ No newline at end of file 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') 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