diff options
author | 2024-12-17 17:28:46 +0100 | |
---|---|---|
committer | 2024-12-17 17:28:46 +0100 | |
commit | f2c9b6b2ffc50d67d9789e77cb55eeb2a0ebe470 (patch) | |
tree | 4941905adf134468acc079610bb6f25d7461d543 /examples | |
parent | a687a837653a576cb0599f7bc8ecd9c6054213a9 (diff) | |
parent | e5545aaa579f428e45853d125ac86155d8395104 (diff) | |
download | iced-f2c9b6b2ffc50d67d9789e77cb55eeb2a0ebe470.tar.gz iced-f2c9b6b2ffc50d67d9789e77cb55eeb2a0ebe470.tar.bz2 iced-f2c9b6b2ffc50d67d9789e77cb55eeb2a0ebe470.zip |
Merge pull request #2698 from iced-rs/feature/test-crate
Headless Mode Testing
Diffstat (limited to 'examples')
-rw-r--r-- | examples/counter/Cargo.toml | 5 | ||||
-rw-r--r-- | examples/counter/src/main.rs | 28 | ||||
-rw-r--r-- | examples/gradient/src/main.rs | 10 | ||||
-rw-r--r-- | examples/todos/Cargo.toml | 5 | ||||
-rw-r--r-- | examples/todos/snapshots/creates_a_new_task.sha256 | 1 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 53 | ||||
-rw-r--r-- | examples/tour/Cargo.toml | 2 |
7 files changed, 92 insertions, 12 deletions
diff --git a/examples/counter/Cargo.toml b/examples/counter/Cargo.toml index 22f86064..02eac329 100644 --- a/examples/counter/Cargo.toml +++ b/examples/counter/Cargo.toml @@ -10,4 +10,7 @@ iced.workspace = true [target.'cfg(target_arch = "wasm32")'.dependencies] iced.workspace = true -iced.features = ["webgl"] +iced.features = ["webgl", "fira-sans"] + +[dev-dependencies] +iced_test.workspace = true diff --git a/examples/counter/src/main.rs b/examples/counter/src/main.rs index 81684c1c..18bb8cfe 100644 --- a/examples/counter/src/main.rs +++ b/examples/counter/src/main.rs @@ -38,3 +38,31 @@ impl Counter { .align_x(Center) } } + +#[cfg(test)] +mod tests { + use super::*; + use iced_test::selector::text; + use iced_test::{simulator, Error}; + + #[test] + fn it_counts() -> Result<(), Error> { + let mut counter = Counter { value: 0 }; + let mut ui = simulator(counter.view()); + + let _ = ui.click(text("Increment"))?; + let _ = ui.click(text("Increment"))?; + let _ = ui.click(text("Decrement"))?; + + for message in ui.into_messages() { + counter.update(message); + } + + assert_eq!(counter.value, 1); + + let mut ui = simulator(counter.view()); + assert!(ui.find(text("1")).is_ok(), "Counter should display 1!"); + + Ok(()) + } +} diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index b2de069f..910ea9fc 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -1,5 +1,5 @@ -use iced::application; use iced::gradient; +use iced::theme; use iced::widget::{ checkbox, column, container, horizontal_space, row, slider, text, }; @@ -95,16 +95,14 @@ impl Gradient { .into() } - fn style(&self, theme: &Theme) -> application::Appearance { - use application::DefaultStyle; - + fn style(&self, theme: &Theme) -> theme::Style { if self.transparent { - application::Appearance { + theme::Style { background_color: Color::TRANSPARENT, text_color: theme.palette().text, } } else { - Theme::default_style(theme) + theme::default(theme) } } } diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 0d72be86..16f4fdd2 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -20,12 +20,15 @@ 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"] } 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/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 25e3ead2..a5bca235 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: &'static [u8] = include_bytes!("../fonts/icons.ttf"); + fn new() -> (Self, Command<Message>) { ( 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) } @@ -584,3 +585,49 @@ impl SavedState { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + use iced::{Settings, Theme}; + use iced_test::selector::text; + use iced_test::{Error, Simulator}; + + fn simulator(todos: &Todos) -> Simulator<Message> { + Simulator::with_settings( + Settings { + fonts: vec![Todos::ICON_FONT.into()], + ..Settings::default() + }, + todos.view(), + ) + } + + #[test] + fn it_creates_a_new_task() -> Result<(), Error> { + let (mut todos, _command) = Todos::new(); + let _command = todos.update(Message::Loaded(Err(LoadError::File))); + + let mut ui = simulator(&todos); + let _input = ui.click("new-task")?; + + 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); + } + + let mut ui = simulator(&todos); + let _ = ui.find(text("Create the universe"))?; + + let snapshot = ui.snapshot(&Theme::Dark)?; + assert!( + snapshot.matches_hash("snapshots/creates_a_new_task")?, + "snapshots should match!" + ); + + Ok(()) + } +} diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 9e984ad1..719d355f 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -14,7 +14,7 @@ tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] iced.workspace = true -iced.features = ["image", "debug", "webgl"] +iced.features = ["image", "debug", "webgl", "fira-sans"] console_error_panic_hook = "0.1" console_log = "1.0" |