toddy-iced-test 0.6.2

Vendored fork of iced_test for Toddy UI -- A library for testing iced applications in headless mode
Documentation

Test your iced applications in headless mode.

Basic Usage

Let's assume we want to test the classical counter interface.

First, we will want to create a [Simulator] of our interface:

# struct Counter { value: i64 }
# impl Counter {
#    pub fn view(&self) -> iced_runtime::core::Element<(), iced_runtime::core::Theme, iced_renderer::Renderer> { unimplemented!() }
# }
use iced_test::simulator;

let mut counter = Counter { value: 0 };
let mut ui = simulator(counter.view());

Now we can simulate a user interacting with our interface. Let's use [Simulator::click] to click the counter buttons:

# struct Counter { value: i64 }
# impl Counter {
#    pub fn view(&self) -> iced_runtime::core::Element<(), iced_runtime::core::Theme, iced_renderer::Renderer> { unimplemented!() }
# }
# use iced_test::simulator;
#
# let mut counter = Counter { value: 0 };
# let mut ui = simulator(counter.view());
#
let _ = ui.click("+");
let _ = ui.click("+");
let _ = ui.click("-");

[Simulator::click] takes a type implementing the [Selector] trait. A [Selector] describes a way to query the widgets of an interface. In this case, we leverage the [Selector] implementation of &str, which selects a widget by the text it contains.

We can now process any messages produced by these interactions and then assert that the final value of our counter is indeed 1!

# struct Counter { value: i64 }
# impl Counter {
#    pub fn update(&mut self, message: ()) {}
#    pub fn view(&self) -> iced_runtime::core::Element<(), iced_runtime::core::Theme, iced_renderer::Renderer> { unimplemented!() }
# }
# use iced_test::simulator;
#
# let mut counter = Counter { value: 0 };
# let mut ui = simulator(counter.view());
#
# let _ = ui.click("+");
# let _ = ui.click("+");
# let _ = ui.click("-");
#
for message in ui.into_messages() {
    counter.update(message);
}

assert_eq!(counter.value, 1);

We can even rebuild the interface to make sure the counter displays the proper value with [Simulator::find]:

# struct Counter { value: i64 }
# impl Counter {
#    pub fn view(&self) -> iced_runtime::core::Element<(), iced_runtime::core::Theme, iced_renderer::Renderer> { unimplemented!() }
# }
# use iced_test::simulator;
#
# let mut counter = Counter { value: 0 };
let mut ui = simulator(counter.view());

assert!(ui.find("1").is_ok(), "Counter should display 1!");

And that's it! That's the gist of testing iced applications!

[Simulator] contains additional operations you can use to simulate more interactions—like tap_key or typewrite—and even perform snapshot testing!