diff options
Diffstat (limited to 'examples/todos/src')
-rw-r--r-- | examples/todos/src/main.rs | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 25e3ead2..7759552c 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,6 +1,6 @@ use iced::keyboard; use iced::widget::{ - self, button, center, checkbox, column, container, keyed_column, row, + self, button, center, center_x, checkbox, column, keyed_column, row, scrollable, text, text_input, Text, }; use iced::window; @@ -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, @@ -147,9 +149,7 @@ impl Todos { } } Message::ToggleFullscreen(mode) => window::get_latest() - .and_then(move |window| { - window::change_mode(window, mode) - }), + .and_then(move |window| window::set_mode(window, mode)), Message::Loaded(_) => Command::none(), }; @@ -237,7 +237,7 @@ impl Todos { .spacing(20) .max_width(800); - scrollable(container(content).center_x(Fill).padding(40)).into() + scrollable(center_x(content).padding(40)).into() } } } @@ -449,11 +449,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 +583,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(()) + } +} |