Skip to content

Commit 5e3383a

Browse files
authored
Improve documentation of TestHarness (#809)
Add scale_factor parameter. Add links to doc module.
1 parent c1e0c83 commit 5e3383a

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

masonry/src/testing/harness.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,19 @@ pub struct TestHarness {
112112
title: String,
113113
}
114114

115+
/// Parameters for creating a [`TestHarness`].
116+
#[derive(Debug, Clone, Copy)]
117+
#[non_exhaustive]
115118
pub struct TestHarnessParams {
119+
/// The size of the virtual window the harness renders into for snapshot testing.
120+
/// Defaults to [`Self::DEFAULT_SIZE`].
116121
pub window_size: Size,
122+
/// The background color of the virtual window.
123+
/// Defaults to [`Self::DEFAULT_BACKGROUND_COLOR`].
117124
pub background_color: Color,
125+
/// The scale factor widgets are rendered at.
126+
/// Defaults to 1.0.
127+
pub scale_factor: f64,
118128
}
119129

120130
/// Assert a snapshot of a rendered frame of your app.
@@ -153,6 +163,7 @@ impl Default for TestHarnessParams {
153163
Self {
154164
window_size: Self::DEFAULT_SIZE,
155165
background_color: Self::DEFAULT_BACKGROUND_COLOR,
166+
scale_factor: 1.0,
156167
}
157168
}
158169
}
@@ -177,7 +188,7 @@ impl TestHarness {
177188
)
178189
}
179190

180-
/// Builds harness with given root widget, canvas size and background color.
191+
/// Builds harness with given root widget and additional parameters.
181192
pub fn create_with(root_widget: impl Widget, params: TestHarnessParams) -> Self {
182193
let mouse_state = PointerState::empty();
183194
let window_size = PhysicalSize::new(
@@ -204,7 +215,7 @@ impl TestHarness {
204215
RenderRootOptions {
205216
use_system_fonts: false,
206217
size_policy: WindowSizePolicy::User,
207-
scale_factor: 1.0,
218+
scale_factor: params.scale_factor,
208219
test_font: Some(data),
209220
},
210221
),
@@ -222,12 +233,10 @@ impl TestHarness {
222233
}
223234

224235
// --- MARK: PROCESS EVENTS ---
225-
// FIXME - The docs for these three functions are copy-pasted. Rewrite them.
226236

227237
/// Send a [`WindowEvent`] to the simulated window.
228238
///
229-
/// If this event triggers rewrite passes, they will also run as normal.
230-
// TODO - Link to tutorial about rewrite passes - See #632
239+
/// This will run [rewrite passes](crate::doc::doc_05_pass_system#rewrite-passes) after the event is processed.
231240
pub fn process_window_event(&mut self, event: WindowEvent) -> Handled {
232241
let handled = self.render_root.handle_window_event(event);
233242
self.process_signals();
@@ -236,8 +245,7 @@ impl TestHarness {
236245

237246
/// Send a [`PointerEvent`] to the simulated window.
238247
///
239-
/// If this event triggers rewrite passes, they will also run as normal.
240-
// TODO - Link to tutorial about rewrite passes - See #632
248+
/// This will run [rewrite passes](crate::doc::doc_05_pass_system#rewrite-passes) after the event is processed.
241249
pub fn process_pointer_event(&mut self, event: PointerEvent) -> Handled {
242250
let handled = self.render_root.handle_pointer_event(event);
243251
self.process_signals();
@@ -246,8 +254,7 @@ impl TestHarness {
246254

247255
/// Send a [`TextEvent`] to the simulated window.
248256
///
249-
/// If this event triggers rewrite passes, they will also run as normal.
250-
// TODO - Link to tutorial about rewrite passes - See #632
257+
/// This will run [rewrite passes](crate::doc::doc_05_pass_system#rewrite-passes) after the event is processed.
251258
pub fn process_text_event(&mut self, event: TextEvent) -> Handled {
252259
let handled = self.render_root.handle_text_event(event);
253260
self.process_signals();
@@ -490,12 +497,11 @@ impl TestHarness {
490497
}
491498
}
492499

493-
/// Sets the focused widget.
500+
/// Sets the [focused widget](crate::doc::doc_06_masonry_concepts#text-focus).
494501
///
495502
/// ## Panics
496503
///
497504
/// If the widget is not found in the tree or can't be focused.
498-
// TODO - Link to focus definition in tutorial
499505
#[track_caller]
500506
pub fn focus_on(&mut self, id: Option<WidgetId>) {
501507
if let Some(id) = id {
@@ -547,20 +553,19 @@ impl TestHarness {
547553
}
548554

549555
// TODO - Link to focus definition in tutorial
550-
/// Return a [`WidgetRef`] to the widget that receives keyboard events.
556+
/// Return a [`WidgetRef`] to the [focused widget](crate::doc::doc_06_masonry_concepts#text-focus).
551557
pub fn focused_widget(&self) -> Option<WidgetRef<'_, dyn Widget>> {
552558
self.root_widget()
553559
.find_widget_by_id(self.render_root.global_state.focused_widget?)
554560
}
555561

556-
/// Return a [`WidgetRef`] to the widget which captures pointer events.
557-
// TODO - Link to pointer capture definition in tutorial
562+
/// Return a [`WidgetRef`] to the widget which [captures pointer events](crate::doc::doc_06_masonry_concepts#pointer-capture).
558563
pub fn pointer_capture_target(&self) -> Option<WidgetRef<'_, dyn Widget>> {
559564
self.render_root
560565
.get_widget(self.render_root.global_state.pointer_capture_target?)
561566
}
562567

563-
/// Return the id of the widget which captures pointer events.
568+
/// Return the id of the widget which [captures pointer events](crate::doc::doc_06_masonry_concepts#pointer-capture).
564569
// TODO - This is kinda redundant with the above
565570
pub fn pointer_capture_target_id(&self) -> Option<WidgetId> {
566571
self.render_root.global_state.pointer_capture_target

0 commit comments

Comments
 (0)