pub struct Harness<'a, T> { /* private fields */ }
Expand description
A type that tries very hard to provide a comforting and safe environment for widgets who are trying to find their way.
You create a Harness
with some widget and its initial data; then you
can send events to that widget and verify that expected conditions are met.
Harness tries to act like the normal Druid environment; for instance, it will
attempt to dispatch any Command
s that are sent during event handling, and
it will call update
automatically after an event.
That said, it is missing a bunch of logic that would normally be handled
in AppState
: for instance it does not clear the needs_inval
and
children_changed
flags on the window after an update.
In addition, layout and paint are not called automatically. This is
because paint is triggered by druid-shell
, and there is no druid-shell
here;
if you want those functions run you will need to call them yourself.
Also, timers don’t work. ¯_(ツ)_/¯
Implementations§
§impl<T> Harness<'_, T>where
T: Data,
impl<T> Harness<'_, T>where T: Data,
pub fn create_simple(
data: T,
root: impl Widget<T> + 'static,
harness_closure: impl FnMut(&mut Harness<'_, T>)
)
pub fn create_simple( data: T, root: impl Widget<T> + 'static, harness_closure: impl FnMut(&mut Harness<'_, T>) )
Create a new Harness
with the given data and a root widget,
and provide that harness to the passed in function.
For lifetime reasons™, we cannot just make a harness. It’s complicated. I tried my best.
This function is a subset of create_with_render
pub fn create_with_render(
data: T,
root: impl Widget<T> + 'static,
window_size: Size,
harness_closure: impl FnMut(&mut Harness<'_, T>),
render_context_closure: impl FnMut(TargetGuard<'_>)
)
pub fn create_with_render( data: T, root: impl Widget<T> + 'static, window_size: Size, harness_closure: impl FnMut(&mut Harness<'_, T>), render_context_closure: impl FnMut(TargetGuard<'_>) )
Create a new Harness
with the given data and a root widget,
and provide that harness to the harness_closure
callback and then the
render_context to the render_context_closure
callback.
For lifetime reasons™, we cannot just make a harness. It’s complicated. I tried my best.
The with_render version of create
also has a callback that can be used
to save or inspect the painted widget
Usage
The create functions are used to test a widget. The function takes a root
widget
and a data structure and uses them to create a Harness
. The Harness can then be interacted
with via the harness_closure
callback. The final render of
the widget can be inspected with the render_context_closure
callback.
Arguments
-
data
- A structure that matches the type of the widget and that will be passed to theharness_closure
callback via theHarness
structure. -
root
- The widget under test -
shape
- The shape of the render_context in theHarness
structure -
harness_closure
- A closure used to interact with the widget under test through theHarness
structure. -
render_context_closure
- A closure used to inspect the final render_context via theTargetGuard
structure.
pub fn set_initial_size(&mut self, size: Size)
pub fn set_initial_size(&mut self, size: Size)
Set the size without sending a resize event; intended to be used
before calling send_initial_events
pub fn window(&self) -> &Window<T>
pub fn window_mut(&mut self) -> &mut Window<T>
pub fn data(&self) -> &T
pub fn get_state(&mut self, widget: WidgetId) -> WidgetState
pub fn get_state(&mut self, widget: WidgetId) -> WidgetState
Retrieve a copy of this widget’s WidgetState
, or die trying.
pub fn try_get_state(&mut self, widget: WidgetId) -> Option<WidgetState>
pub fn try_get_state(&mut self, widget: WidgetId) -> Option<WidgetState>
Attempt to retrieve a copy of this widget’s WidgetState
.
pub fn get_root_debug_state(&self) -> DebugState
pub fn get_root_debug_state(&self) -> DebugState
Retrieve a copy of the root widget’s DebugState
(and by recursion, all others)
pub fn get_debug_state(&mut self, widget_id: WidgetId) -> DebugState
pub fn get_debug_state(&mut self, widget_id: WidgetId) -> DebugState
Retrieve a copy of this widget’s DebugState
, or die trying.
pub fn try_get_debug_state(&mut self, widget_id: WidgetId) -> Option<DebugState>
pub fn try_get_debug_state(&mut self, widget_id: WidgetId) -> Option<DebugState>
Attempt to retrieve a copy of this widget’s DebugState
.
pub fn inspect_state(&mut self, f: impl Fn(&WidgetState) + 'static)
pub fn inspect_state(&mut self, f: impl Fn(&WidgetState) + 'static)
Inspect the WidgetState
of each widget in the tree.
The provided closure will be called on each widget.
pub fn submit_command(&mut self, cmd: impl Into<Command>)
pub fn submit_command(&mut self, cmd: impl Into<Command>)
Send a command to a target.
pub fn send_initial_events(&mut self)
pub fn send_initial_events(&mut self)
Send the events that would normally be sent when the app starts.
pub fn event(&mut self, event: Event)
pub fn event(&mut self, event: Event)
Send an event to the widget.
If this event triggers lifecycle events, they will also be dispatched,
as will any resulting commands. This will also trigger update
.
Commands dispatched during update
will not be sent?
pub fn just_layout(&mut self)
pub fn just_layout(&mut self)
Only do a layout pass, without painting
pub fn paint_invalid(&mut self)
pub fn paint_invalid(&mut self)
Paints just the part of the window that was invalidated by calls to request_paint
or
request_paint_rect
.
Also resets the invalid region.
pub fn paint(&mut self)
pub fn paint(&mut self)
Paints the entire window and resets the invalid region.