Skip to content

Commit 5436408

Browse files
committed
Have TestHarness methods use WidgetTag instead of WidgetId
This is part of an effort to make WidgetTag the main primitive used in tests.
1 parent 9274fcc commit 5436408

21 files changed

Lines changed: 259 additions & 193 deletions

masonry/src/doc/color_rectangle.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ mod tests {
282282
let widget = ColorRectangle::new(BLUE).with_props(Dimensions::fixed(20.px(), 20.px()));
283283

284284
let mut harness = TestHarness::create(default_property_set(), widget);
285-
let rect_id = harness.root_id();
285+
let rect_tag = harness.root_tag();
286286

287287
// Computes the rect's layout and sends an PointerEvent
288288
// placing the mouse at its center.
289-
harness.mouse_move_to(rect_id);
289+
harness.mouse_move_to(rect_tag);
290290
assert_render_snapshot!(harness, "rect_hovered_rectangle");
291291
}
292292

@@ -314,9 +314,9 @@ mod tests {
314314
let widget = ColorRectangle::new(BLUE).with_props(Dimensions::fixed(20.px(), 20.px()));
315315

316316
let mut harness = TestHarness::create(default_property_set(), widget);
317-
let rect_id = harness.root_id();
317+
let rect_tag = harness.root_tag();
318318

319-
harness.mouse_click_on(rect_id);
319+
harness.mouse_click_on(rect_tag);
320320
assert!(matches!(
321321
harness.pop_action::<ColorRectanglePress>(),
322322
Some((ColorRectanglePress, _))

masonry/src/doc/testing_widget.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ Let's create another snapshot test to check that our widget correctly changes co
116116
let widget = ColorRectangle::new(BLUE).with_props(Dimensions::fixed(20.px(), 20.px()));
117117
118118
let mut harness = TestHarness::create(default_property_set(), widget);
119-
let rect_id = harness.root_id();
119+
let rect_tag = harness.root_tag();
120120
121121
// Computes the rect's layout and sends an PointerEvent
122122
// placing the mouse at its center.
123-
harness.mouse_move_to(rect_id);
123+
harness.mouse_move_to(rect_tag);
124124
assert_render_snapshot!(harness, "rect_hovered_rectangle");
125125
}
126126
```
@@ -179,7 +179,7 @@ The `TestHarness` is also capable of reading actions emitted by our widget with
179179
let mut harness = TestHarness::create(default_property_set(), widget);
180180
let rect_id = harness.root_id();
181181
182-
harness.mouse_click_on(rect_id);
182+
harness.mouse_click_on_with_id(rect_id);
183183
assert!(matches!(
184184
harness.pop_action::<ColorRectanglePress>(),
185185
Some((ColorRectanglePress, _))

masonry/src/tests/accessibility.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ fn access_node_children() {
5959
let _ = harness.render();
6060

6161
let parent_ref = harness.get_widget(parent_tag);
62-
let parent_node_id = parent_ref.id();
6362
let [id_1, id_2, id_3] = parent_ref.inner().children_ids()[..] else {
6463
unreachable!()
6564
};
6665

67-
let parent_node = harness.access_node(parent_node_id).unwrap();
66+
let parent_node = harness.access_node(parent_tag).unwrap();
6867
assert_eq!(
6968
Vec::<u64>::from_iter(parent_node.child_ids().map(node_local_id_to_u64)),
7069
vec![id_1.to_raw(), id_2.to_raw(), id_3.to_raw()]
@@ -78,7 +77,7 @@ fn access_node_children() {
7877
let _ = harness.render();
7978

8079
// Stash child is not included
81-
let parent_node = harness.access_node(parent_node_id).unwrap();
80+
let parent_node = harness.access_node(parent_tag).unwrap();
8281
assert_eq!(
8382
Vec::<u64>::from_iter(parent_node.child_ids().map(node_local_id_to_u64)),
8483
vec![id_1.to_raw(), id_3.to_raw()]

masonry/src/tests/action.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
66

77
use assert_matches::assert_matches;
88

9-
use crate::core::{ChildrenIds, Widget};
9+
use crate::core::{ChildrenIds, Widget, WidgetTag};
1010
use crate::kurbo::Point;
1111
use crate::layout::{AsUnit, LayoutSize};
1212
use crate::properties::Dimensions;
@@ -25,6 +25,7 @@ fn action_source_removed() {
2525
#[derive(Debug)]
2626
struct ArbitraryAction;
2727

28+
let action_source_tag = WidgetTag::named("action_source");
2829
let action_source = ModularWidget::new(ok.clone())
2930
.pointer_event_fn(|ok, ctx, _, _| {
3031
// Send an action but crucially don't mark the pointer event as handled,
@@ -33,8 +34,8 @@ fn action_source_removed() {
3334
ok.store(true, Ordering::Release);
3435
})
3536
.with_props(Dimensions::fixed(50.px(), 50.px()))
37+
.with_tag(action_source_tag)
3638
.to_pod();
37-
let action_source_id = action_source.id();
3839

3940
let parent = ModularWidget::new(Some(action_source))
4041
.pointer_event_fn(|child, ctx, _, _| {
@@ -80,7 +81,7 @@ fn action_source_removed() {
8081

8182
let mut harness = TestHarness::create(test_property_set(), parent);
8283

83-
harness.mouse_move_to(action_source_id);
84+
harness.mouse_move_to(action_source_tag);
8485

8586
// We don't expect the action to make it to the app driver,
8687
// because we deleted the child before it got there.
@@ -94,7 +95,10 @@ fn action_propagation() {
9495
#[derive(Debug)]
9596
struct TranslatedAction;
9697

97-
let button = Button::with_text("Click me!").with_auto_id();
98+
let button_tag = WidgetTag::named("button");
99+
let button = Button::with_text("Click me!")
100+
.with_auto_id()
101+
.with_tag(button_tag);
98102
let button_id = button.id();
99103

100104
let parent1 = ModularWidget::new_parent(button)
@@ -128,7 +132,7 @@ fn action_propagation() {
128132

129133
let mut harness = TestHarness::create(test_property_set(), parent3);
130134

131-
harness.mouse_click_on(button_id);
135+
harness.mouse_click_on(button_tag);
132136

133137
// Only the translated action should reach the app driver
134138
assert_matches!(

masonry/src/tests/event.rs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ fn pointer_event() {
3838
let button = NewWidget::new(Button::with_text("button").record()).with_tag(button_tag);
3939

4040
let mut harness = TestHarness::create(test_property_set(), button);
41-
let button_id = harness.get_widget(button_tag).id();
4241

4342
harness.flush_records_of(button_tag);
44-
harness.mouse_move_to(button_id);
43+
harness.mouse_move_to(button_tag);
4544

4645
let records = harness.take_records_of(button_tag);
4746
assert_any(records, |r| {
@@ -61,10 +60,9 @@ fn pointer_event_bubbling() {
6160
NewWidget::new(ModularWidget::new_parent(parent).record()).with_tag(grandparent_tag);
6261

6362
let mut harness = TestHarness::create(test_property_set(), grandparent);
64-
let button_id = harness.get_widget(button_tag).id();
6563

6664
harness.flush_records_of(button_tag);
67-
harness.mouse_click_on(button_id);
65+
harness.mouse_click_on(button_tag);
6866

6967
fn is_pointer_down(record: Record) -> bool {
7068
matches!(record, Record::PointerEvent(PointerEvent::Down { .. }))
@@ -86,7 +84,7 @@ fn pointer_capture_and_cancel() {
8684

8785
let target_id = harness.get_widget(target_tag).id();
8886

89-
harness.mouse_move_to(target_id);
87+
harness.mouse_move_to(target_tag);
9088
harness.mouse_button_press(PointerButton::Primary);
9189
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));
9290

@@ -109,7 +107,7 @@ fn synthetic_cancel() {
109107

110108
let target_id = harness.get_widget(target_tag).id();
111109

112-
harness.mouse_move_to(target_id);
110+
harness.mouse_move_to(target_tag);
113111
harness.mouse_button_press(PointerButton::Primary);
114112
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));
115113

@@ -143,15 +141,14 @@ fn pointer_capture_suppresses_neighbors() {
143141
harness.flush_records_of(other_tag);
144142

145143
let target_id = harness.get_widget(target_tag).id();
146-
let other_id = harness.get_widget(other_tag).id();
147144

148-
harness.mouse_move_to(target_id);
145+
harness.mouse_move_to(target_tag);
149146
harness.mouse_button_press(PointerButton::Primary);
150147

151148
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));
152149

153150
// As long as 'target' is captured, 'other' doesn't get pointer events, even when the cursor is on it.
154-
harness.mouse_move_to(other_id);
151+
harness.mouse_move_to(other_tag);
155152
assert_matches!(harness.take_records_of(other_tag)[..], []);
156153

157154
// 'other' is not considered hovered either.
@@ -191,8 +188,7 @@ fn try_capture_pointer_on_text_event() {
191188
.with_auto_id();
192189

193190
let mut harness = TestHarness::create(test_property_set(), widget);
194-
let id = harness.root_id();
195-
harness.focus_on(Some(id));
191+
harness.focus_on(harness.root_tag());
196192

197193
assert_debug_panics!(
198194
harness.keyboard_type_chars("a"),
@@ -211,7 +207,7 @@ fn pointer_cancel_on_window_blur() {
211207

212208
let target_id = harness.get_widget(target_tag).id();
213209

214-
harness.mouse_move_to(target_id);
210+
harness.mouse_move_to(target_tag);
215211
harness.mouse_button_press(PointerButton::Primary);
216212
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));
217213
harness.flush_records_of(target_tag);
@@ -241,13 +237,11 @@ fn click_anchors_focus() {
241237

242238
let mut harness = TestHarness::create(test_property_set(), parent);
243239

244-
let child_3_id = harness.get_widget(child_3).id();
245240
let child_4_id = harness.get_widget(child_4).id();
246-
let other_id = harness.get_widget(other).id();
247241

248242
// Clicking a disabled button doesn't focus it.
249243
harness.set_disabled(child_3, true);
250-
harness.mouse_click_on(child_3_id);
244+
harness.mouse_click_on(child_3);
251245
assert_eq!(harness.focused_widget_id(), None);
252246

253247
// But the next tab event focuses its neighbor.
@@ -259,7 +253,7 @@ fn click_anchors_focus() {
259253
// is resolved.
260254

261255
// Clicking another non-focusable widget clears focus.
262-
harness.mouse_move_to_unchecked(other_id);
256+
harness.mouse_move_to_unchecked(other);
263257
harness.mouse_button_press(PointerButton::Primary);
264258
harness.mouse_button_release(PointerButton::Primary);
265259
assert_eq!(harness.focused_widget_id(), None);
@@ -433,7 +427,7 @@ fn multi_pointers_capture() {
433427

434428
// Move mouse to button 1, mouse press
435429
// Check mouse is captured, button 1 is active
436-
harness.mouse_move_to(button_1_id);
430+
harness.mouse_move_to(button_1_tag);
437431
harness.mouse_button_press(PointerButton::Primary);
438432

439433
assert_captured_by(&harness, PointerId::PRIMARY, button_1_id);
@@ -484,15 +478,14 @@ fn text_event() {
484478
let target = NewWidget::new(TextArea::new_editable("").record()).with_tag(target_tag);
485479

486480
let mut harness = TestHarness::create(test_property_set(), target);
487-
let target_id = harness.get_widget(target_tag).id();
488481
harness.flush_records_of(target_tag);
489482

490483
// The widget isn't focused, it doesn't get text events.
491484
harness.keyboard_type_chars("A");
492485
assert_matches!(harness.take_records_of(target_tag)[..], []);
493486

494487
// We focus on the widget, now it gets text events.
495-
harness.focus_on(Some(target_id));
488+
harness.focus_on(target_tag);
496489
harness.keyboard_type_chars("A");
497490
let records = harness.take_records_of(target_tag);
498491
assert_any(records, |r| matches!(r, Record::TextEvent(_)));
@@ -511,9 +504,8 @@ fn text_event_bubbling() {
511504
NewWidget::new(ModularWidget::new_parent(parent).record()).with_tag(grandparent_tag);
512505

513506
let mut harness = TestHarness::create(test_property_set(), grandparent);
514-
let target_id = harness.get_widget(target_tag).id();
515507

516-
harness.focus_on(Some(target_id));
508+
harness.focus_on(target_tag);
517509
harness.process_text_event(TextEvent::key_down(Key::Character("A".into())));
518510

519511
fn is_keyboard_event(record: Record) -> bool {
@@ -538,16 +530,14 @@ fn text_event_fallback() {
538530
.with_auto_id();
539531

540532
let mut harness = TestHarness::create(test_property_set(), parent);
541-
let target_id = harness.get_widget(target_tag).id();
542-
let other_id = harness.get_widget(other_tag).id();
543533
harness.flush_records_of(target_tag);
544-
harness.set_focus_fallback(Some(target_id));
534+
harness.set_focus_fallback(target_tag);
545535

546-
harness.focus_on(Some(other_id));
536+
harness.focus_on(other_tag);
547537
assert_matches!(harness.take_records_of(target_tag)[..], []);
548538

549539
// If a widget is set as focus fallback, that widget gets text events when no widget is focused.
550-
harness.focus_on(None);
540+
harness.clear_focus();
551541
harness.keyboard_type_chars("A");
552542
let records = harness.take_records_of(target_tag);
553543
assert_any(records, |r| matches!(r, Record::TextEvent(_)));
@@ -578,30 +568,28 @@ fn tab_focus() {
578568
let mut harness = TestHarness::create(test_property_set(), parent);
579569

580570
let child_1_id = harness.get_widget(child_1).id();
581-
let child_2_id = harness.get_widget(child_2).id();
582571
let child_3_id = harness.get_widget(child_3).id();
583-
let child_4_id = harness.get_widget(child_4).id();
584572
let child_5_id = harness.get_widget(child_5).id();
585573

586574
assert_eq!(harness.focused_widget_id(), None);
587575

588576
// Tab moves focus to the next focusable widget in the tree.
589-
harness.focus_on(Some(child_2_id));
577+
harness.focus_on(child_2);
590578
harness.press_tab_key(false);
591579
assert_eq!(harness.focused_widget_id(), Some(child_3_id));
592580

593581
// Shift+Tab moves focus to the previous focusable widget in the tree.
594-
harness.focus_on(Some(child_4_id));
582+
harness.focus_on(child_4);
595583
harness.press_tab_key(true);
596584
assert_eq!(harness.focused_widget_id(), Some(child_3_id));
597585

598586
// When nothing is focused, Tab focuses the first focusable widget in the tree.
599-
harness.focus_on(None);
587+
harness.clear_focus();
600588
harness.press_tab_key(false);
601589
assert_eq!(harness.focused_widget_id(), Some(child_1_id));
602590

603591
// When nothing is focused, Shift+Tab focuses the last focusable widget in the tree.
604-
harness.focus_on(None);
592+
harness.clear_focus();
605593
harness.press_tab_key(true);
606594
assert_eq!(harness.focused_widget_id(), Some(child_5_id));
607595
}
@@ -703,9 +691,8 @@ fn downcast_untyped_action() {
703691
let widget = NewWidget::new(arbitrary_submitter).with_tag(target_tag);
704692

705693
let mut harness = TestHarness::create(test_property_set(), widget);
706-
let target_id = harness.get_widget(target_tag).id();
707694

708-
harness.mouse_move_to(target_id);
695+
harness.mouse_move_to(target_tag);
709696

710697
assert_matches!(
711698
harness.pop_action::<ArbitraryAction>(),

0 commit comments

Comments
 (0)