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 Commands 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,

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<'_>) )

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 the harness_closure callback via the Harness structure.

  • root - The widget under test

  • shape - The shape of the render_context in the Harness structure

  • harness_closure - A closure used to interact with the widget under test through the Harness structure.

  • render_context_closure - A closure used to inspect the final render_context via the TargetGuard structure.

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

Retrieve a copy of this widget’s WidgetState, or die trying.

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

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

Retrieve a copy of this widget’s DebugState, or die trying.

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)

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>)

Send a command to a target.

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)

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)

Only do a layout pass, without painting

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)

Paints the entire window and resets the invalid region.

pub fn root_debug_state(&self) -> DebugState

Trait Implementations§

§

impl<T> Drop for Harness<'_, T>

§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for Harness<'a, T>

§

impl<'a, T> !Send for Harness<'a, T>

§

impl<'a, T> !Sync for Harness<'a, T>

§

impl<'a, T> Unpin for Harness<'a, T>where T: Unpin,

§

impl<'a, T> !UnwindSafe for Harness<'a, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more