Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions masonry/src/doc/color_rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ mod tests {
.with_props(Dimensions::MIN);

let mut harness = TestHarness::create(default_property_set(), widget);
let rect_id = harness.root_id();
let rect_tag = harness.root_tag();

// Computes the rect's layout and sends an PointerEvent
// placing the mouse at its center.
harness.mouse_move_to(rect_id);
harness.mouse_move_to(rect_tag);
assert_render_snapshot!(harness, "rect_hovered_rectangle");
}

Expand Down Expand Up @@ -322,9 +322,9 @@ mod tests {
.with_props(Dimensions::MIN);

let mut harness = TestHarness::create(default_property_set(), widget);
let rect_id = harness.root_id();
let rect_tag = harness.root_tag();

harness.mouse_click_on(rect_id, None);
harness.mouse_click_on(rect_tag, None);
assert!(matches!(
harness.pop_action::<ColorRectanglePress>(),
Some((ColorRectanglePress, _))
Expand Down
8 changes: 4 additions & 4 deletions masonry/src/doc/testing_widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ Let's create another snapshot test to check that our widget correctly changes co
.with_props(Dimensions::MIN);

let mut harness = TestHarness::create(default_property_set(), widget);
let rect_id = harness.root_id();
let rect_tag = harness.root_tag();

// Computes the rect's layout and sends an PointerEvent
// placing the mouse at its center.
harness.mouse_move_to(rect_id);
harness.mouse_move_to(rect_tag);
assert_render_snapshot!(harness, "rect_hovered_rectangle");
}
```
Expand Down Expand Up @@ -187,9 +187,9 @@ The `TestHarness` is also capable of reading actions emitted by our widget with
.with_props(Dimensions::MIN);

let mut harness = TestHarness::create(default_property_set(), widget);
let rect_id = harness.root_id();
let rect_tag = harness.root_tag();

harness.mouse_click_on(rect_id, None);
harness.mouse_click_on(rect_tag, None);
assert!(matches!(
harness.pop_action::<ColorRectanglePress>(),
Some((ColorRectanglePress, _))
Expand Down
5 changes: 2 additions & 3 deletions masonry/src/tests/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ fn access_node_children() {
let _ = harness.render();

let parent_ref = harness.get_widget(parent_tag);
let parent_node_id = parent_ref.id();
let [id_1, id_2, id_3] = parent_ref.inner().children_ids()[..] else {
unreachable!()
};

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

// Stash child is not included
let parent_node = harness.access_node(parent_node_id).unwrap();
let parent_node = harness.access_node(parent_tag).unwrap();
assert_eq!(
Vec::<u64>::from_iter(parent_node.child_ids().map(node_local_id_to_u64)),
vec![id_1.to_raw(), id_3.to_raw()]
Expand Down
14 changes: 9 additions & 5 deletions masonry/src/tests/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering};

use assert_matches::assert_matches;

use crate::core::{ChildrenIds, Widget};
use crate::core::{ChildrenIds, Widget, WidgetTag};
use crate::kurbo::Point;
use crate::layout::{AsUnit, LayoutSize};
use crate::properties::Dimensions;
Expand All @@ -25,6 +25,7 @@ fn action_source_removed() {
#[derive(Debug)]
struct ArbitraryAction;

let action_source_tag = WidgetTag::named("action_source");
let action_source = ModularWidget::new(ok.clone())
.pointer_event_fn(|ok, ctx, _, _| {
// Send an action but crucially don't mark the pointer event as handled,
Expand All @@ -34,8 +35,8 @@ fn action_source_removed() {
})
.prepare()
.with_props(Dimensions::fixed(50.px(), 50.px()))
.with_tag(action_source_tag)
.to_pod();
let action_source_id = action_source.id();

let parent = ModularWidget::new(Some(action_source))
.pointer_event_fn(|child, ctx, _, _| {
Expand Down Expand Up @@ -81,7 +82,7 @@ fn action_source_removed() {

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

harness.mouse_move_to(action_source_id);
harness.mouse_move_to(action_source_tag);

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

let button = Button::with_text("Click me!").prepare();
let button_tag = WidgetTag::named("button");
let button = Button::with_text("Click me!")
.prepare()
.with_tag(button_tag);
let button_id = button.id();

let parent1 = ModularWidget::new_parent(button)
Expand Down Expand Up @@ -129,7 +133,7 @@ fn action_propagation() {

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

harness.mouse_click_on(button_id, None);
harness.mouse_click_on(button_tag, None);

// Only the translated action should reach the app driver
assert_matches!(
Expand Down
55 changes: 21 additions & 34 deletions masonry/src/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ fn pointer_event() {
let button = NewWidget::new(Button::with_text("button").record()).with_tag(button_tag);

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

harness.flush_records_of(button_tag);
harness.mouse_move_to(button_id);
harness.mouse_move_to(button_tag);

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

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

harness.flush_records_of(button_tag);
harness.mouse_click_on(button_id, None);
harness.mouse_click_on(button_tag, None);

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

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

harness.mouse_move_to(target_id);
harness.mouse_move_to(target_tag);
harness.mouse_button_press(None);
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));

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

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

harness.mouse_move_to(target_id);
harness.mouse_move_to(target_tag);
harness.mouse_button_press(None);
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));

Expand Down Expand Up @@ -143,15 +141,14 @@ fn pointer_capture_suppresses_neighbors() {
harness.flush_records_of(other_tag);

let target_id = harness.get_widget(target_tag).id();
let other_id = harness.get_widget(other_tag).id();

harness.mouse_move_to(target_id);
harness.mouse_move_to(target_tag);
harness.mouse_button_press(None);

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

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

// 'other' is not considered hovered either.
Expand Down Expand Up @@ -191,8 +188,7 @@ fn try_capture_pointer_on_text_event() {
.prepare();

let mut harness = TestHarness::create(test_property_set(), widget);
let id = harness.root_id();
harness.focus_on(Some(id));
harness.focus_on(harness.root_tag());

assert_debug_panics!(
harness.keyboard_type_chars("a"),
Expand All @@ -211,7 +207,7 @@ fn pointer_cancel_on_window_blur() {

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

harness.mouse_move_to(target_id);
harness.mouse_move_to(target_tag);
harness.mouse_button_press(None);
assert_eq!(harness.pointer_capture_target_id(), Some(target_id));
harness.flush_records_of(target_tag);
Expand Down Expand Up @@ -241,13 +237,11 @@ fn click_anchors_focus() {

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

let child_3_id = harness.get_widget(child_3).id();
let child_4_id = harness.get_widget(child_4).id();
let other_id = harness.get_widget(other).id();

// Clicking a disabled button doesn't focus it.
harness.set_disabled(child_3, true);
harness.mouse_click_on(child_3_id, None);
harness.mouse_click_on(child_3, None);
assert_eq!(harness.focused_widget_id(), None);

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

// Clicking another non-focusable widget clears focus.
harness.mouse_move_to_unchecked(other_id);
harness.mouse_move_to_unchecked(other);
harness.mouse_button_press(None);
harness.mouse_button_release(None);
assert_eq!(harness.focused_widget_id(), None);
Expand Down Expand Up @@ -433,7 +427,7 @@ fn multi_pointers_capture() {

// Move mouse to button 1, mouse press
// Check mouse is captured, button 1 is active
harness.mouse_move_to(button_1_id);
harness.mouse_move_to(button_1_tag);
harness.mouse_button_press(None);

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

let mut harness = TestHarness::create(test_property_set(), target);
let target_id = harness.get_widget(target_tag).id();
harness.flush_records_of(target_tag);

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

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

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

harness.focus_on(Some(target_id));
harness.focus_on(target_tag);
harness.process_text_event(TextEvent::key_down(Key::Character("A".into())));

fn is_keyboard_event(record: Record) -> bool {
Expand All @@ -535,16 +527,14 @@ fn text_event_fallback() {
let parent = Flex::row().with_fixed(target).with_fixed(other).prepare();

let mut harness = TestHarness::create(test_property_set(), parent);
let target_id = harness.get_widget(target_tag).id();
let other_id = harness.get_widget(other_tag).id();
harness.flush_records_of(target_tag);
harness.set_focus_fallback(Some(target_id));
harness.set_focus_fallback(target_tag);

harness.focus_on(Some(other_id));
harness.focus_on(other_tag);
assert_matches!(harness.take_records_of(target_tag)[..], []);

// If a widget is set as focus fallback, that widget gets text events when no widget is focused.
harness.focus_on(None);
harness.clear_focus();
harness.keyboard_type_chars("A");
let records = harness.take_records_of(target_tag);
assert_any(records, |r| matches!(r, Record::TextEvent(_)));
Expand Down Expand Up @@ -575,30 +565,28 @@ fn tab_focus() {
let mut harness = TestHarness::create(test_property_set(), parent);

let child_1_id = harness.get_widget(child_1).id();
let child_2_id = harness.get_widget(child_2).id();
let child_3_id = harness.get_widget(child_3).id();
let child_4_id = harness.get_widget(child_4).id();
let child_5_id = harness.get_widget(child_5).id();

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

// Tab moves focus to the next focusable widget in the tree.
harness.focus_on(Some(child_2_id));
harness.focus_on(child_2);
harness.press_tab_key(false);
assert_eq!(harness.focused_widget_id(), Some(child_3_id));

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

// When nothing is focused, Tab focuses the first focusable widget in the tree.
harness.focus_on(None);
harness.clear_focus();
harness.press_tab_key(false);
assert_eq!(harness.focused_widget_id(), Some(child_1_id));

// When nothing is focused, Shift+Tab focuses the last focusable widget in the tree.
harness.focus_on(None);
harness.clear_focus();
harness.press_tab_key(true);
assert_eq!(harness.focused_widget_id(), Some(child_5_id));
}
Expand Down Expand Up @@ -700,9 +688,8 @@ fn downcast_untyped_action() {
let widget = NewWidget::new(arbitrary_submitter).with_tag(target_tag);

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

harness.mouse_move_to(target_id);
harness.mouse_move_to(target_tag);

assert_matches!(
harness.pop_action::<ArbitraryAction>(),
Expand Down
Loading
Loading