use std::cell::Cell;
use std::rc::Rc;
use std::sync::{Arc, mpsc};
use std::assert_matches;
use crate::app::{RenderRoot, RenderRootOptions, WindowSizePolicy};
use crate::core::pointer::PointerEvent;
use crate::core::{
CollectionWidget, CursorIcon, Ime, NewWidget, PropertySet, TextEvent, TimerKey, Update, Widget,
WidgetId, WidgetPod, WidgetTag,
};
use crate::layout::{AsUnit, Length};
use crate::testing::{
DebugName, ModularWidget, PRIMARY_MOUSE, Record, TestHarness, TestWidgetExt, assert_all,
assert_any, assert_debug_panics,
};
use crate::theme::test_property_set;
use crate::util::{Duration, Instant};
use crate::widgets::{Button, Flex, Label, SizedBox, TextArea};
#[test]
fn app_creation() {
let widget_tag = WidgetTag::named("widget");
let widget = NewWidget::new(SizedBox::empty().record()).with_tag(widget_tag);
let harness = TestHarness::create(test_property_set(), widget);
assert_matches!(
harness.take_records_of(widget_tag)[..],
[
Record::RegisterChildren,
Record::Update(Update::WidgetAdded),
Record::Layout(_),
Record::Compose,
Record::AnimFrame(0),
Record::PrePaint,
Record::Paint,
Record::PostPaint,
Record::Accessibility
]
);
}
#[test]
fn new_widget() {
let flex = NewWidget::new(Flex::column());
let mut harness = TestHarness::create(test_property_set(), flex);
let widget_tag = WidgetTag::named("widget");
harness.edit_root_widget(|mut flex| {
let widget = NewWidget::new(SizedBox::empty().record()).with_tag(widget_tag);
Flex::add_fixed(&mut flex, widget);
});
assert_matches!(
harness.take_records_of(widget_tag)[0..2],
[
Record::RegisterChildren,
Record::Update(Update::WidgetAdded)
]
);
}
#[test]
fn timer_keys_are_local_to_widgets() {
const TIMER: TimerKey = TimerKey::new(0);
let target_tag = WidgetTag::named("target");
let parent_tag = WidgetTag::named("parent");
let child = NewWidget::new(SizedBox::empty().record()).with_tag(target_tag);
let parent = NewWidget::new(ModularWidget::new_parent(child).record()).with_tag(parent_tag);
let mut harness = TestHarness::create(test_property_set(), parent);
harness.edit_widget(target_tag, |mut widget| {
widget.ctx.set_timer(TIMER, Duration::from_millis(10));
});
harness.edit_widget(parent_tag, |mut widget| {
widget.ctx.set_timer(TIMER, Duration::from_millis(20));
});
harness.flush_records_of(target_tag);
harness.flush_records_of(parent_tag);
harness.advance_time(Duration::from_millis(10));
let records = harness.take_records_of(target_tag);
assert_any(records, |record| {
matches!(record, Record::Update(Update::TimerFired(TIMER)))
});
let records = harness.take_records_of(parent_tag);
assert_all(records, |record| {
!matches!(record, Record::Update(Update::TimerFired(_)))
});
harness.advance_time(Duration::from_millis(10));
assert_any(harness.take_records_of(parent_tag), |record| {
matches!(record, Record::Update(Update::TimerFired(TIMER)))
});
}
#[test]
fn timer_uses_the_clock_at_the_point_of_request() {
let start = Instant::now();
let clock = Rc::new(Cell::new(start));
let root_clock = Rc::clone(&clock);
let mut root = RenderRoot::new_with_clock(
SizedBox::empty().prepare(),
|_| {},
RenderRootOptions {
default_properties: Arc::new(test_property_set()),
use_system_fonts: false,
size_policy: WindowSizePolicy::User,
size: (100, 100).into(),
scale_factor: 1.0,
test_font: None,
},
move || root_clock.get(),
);
let delay = Duration::from_millis(10);
let request_time = start + Duration::from_secs(1);
root.edit_main_subwindow(|mut widget| {
clock.set(request_time);
widget.ctx.set_timer(TimerKey::new(0), delay);
});
let deadline = request_time + delay;
root.handle_timers();
assert_eq!(root.next_timer_deadline(), Some(deadline));
clock.set(deadline);
root.handle_timers();
assert_eq!(root.next_timer_deadline(), None);
}
#[test]
fn cancelled_timer_does_not_fire() {
const TIMER: TimerKey = TimerKey::new(0);
let target_tag = WidgetTag::named("target");
let widget = NewWidget::new(SizedBox::empty().record()).with_tag(target_tag);
let mut harness = TestHarness::create(test_property_set(), widget);
harness.edit_widget(target_tag, |mut widget| {
widget.ctx.set_timer(TIMER, Duration::from_millis(10));
});
harness.edit_widget(target_tag, |mut widget| {
widget.ctx.cancel_timer(TIMER);
});
harness.flush_records_of(target_tag);
harness.advance_time(Duration::from_millis(10));
let records = harness.take_records_of(target_tag);
assert_all(records, |record| {
!matches!(record, Record::Update(Update::TimerFired(_)))
});
}
#[test]
fn due_timer_can_be_cancelled_or_replaced_before_delivery() {
const FIRST: TimerKey = TimerKey::new(0);
const SECOND: TimerKey = TimerKey::new(1);
let delay = Duration::from_millis(10);
for replace in [false, true] {
let tag = WidgetTag::named("target");
let widget = ModularWidget::new(()).update_fn(move |_, ctx, _, event| {
if let Update::TimerFired(FIRST) = event {
assert!(!ctx.has_timer(FIRST));
assert!(ctx.has_timer(SECOND));
if replace {
ctx.set_timer(SECOND, Duration::ZERO);
} else {
ctx.cancel_timer(SECOND);
}
}
});
let mut harness = TestHarness::create(
test_property_set(),
NewWidget::new(widget.record()).with_tag(tag),
);
harness.edit_widget(tag, |mut widget| {
widget.ctx.set_timer(FIRST, delay);
widget.ctx.set_timer(SECOND, delay);
});
harness.advance_time(delay);
assert_all(harness.take_records_of(tag), |record| {
!matches!(record, Record::Update(Update::TimerFired(SECOND)))
});
harness.advance_time(Duration::ZERO);
let records = harness.take_records_of(tag);
let second_deliveries = records
.iter()
.filter(|record| matches!(record, Record::Update(Update::TimerFired(SECOND))))
.count();
assert_eq!(second_deliveries, usize::from(replace));
}
}
#[test]
fn removing_a_widget_cancels_its_timers() {
let tag = WidgetTag::named("target");
let child = SizedBox::empty().prepare().with_tag(tag);
let mut harness = TestHarness::create(
test_property_set(),
Flex::column().with_fixed(child).prepare(),
);
harness.edit_widget(tag, |mut widget| {
widget
.ctx
.set_timer(TimerKey::new(0), Duration::from_secs(10));
});
assert!(harness.render_root().next_timer_deadline().is_some());
harness.edit_root_widget(|mut root| Flex::remove(&mut root, 0));
assert_eq!(harness.render_root().next_timer_deadline(), None);
}
#[test]
fn forget_register_child() {
let widget = ModularWidget::new_parent(Flex::row().prepare())
.register_children_fn(|_child, _ctx| {
})
.prepare();
assert_debug_panics!(
TestHarness::create(test_property_set(), widget),
"did not call RegisterCtx::register_child()"
);
}
#[test]
fn register_invalid_child() {
let widget = ModularWidget::new_parent(Flex::row().prepare())
.register_children_fn(|child, ctx| {
ctx.register_child(child);
ctx.register_child(&mut WidgetPod::new(Flex::row()));
})
.prepare();
assert_debug_panics!(
TestHarness::create(test_property_set(), widget),
"in the list returned by children_ids"
);
}
#[test]
fn disabled_widget_gets_no_event() {
let button_tag = WidgetTag::named("button");
let parent_tag = WidgetTag::named("parent");
let child = NewWidget::new(Button::with_text("Click me").record()).with_tag(button_tag);
let parent = NewWidget::new(ModularWidget::new_parent(child)).with_tag(parent_tag);
let mut harness = TestHarness::create(test_property_set(), parent);
harness.focus_on(button_tag);
harness.flush_records_of(button_tag);
harness.set_disabled(button_tag, true);
assert_matches!(
harness.take_records_of(button_tag)[..],
[
Record::Update(Update::DisabledChanged(true)),
Record::Update(Update::ChildFocusChanged(false)),
Record::Update(Update::FocusChanged(false)),
]
);
harness.mouse_click_on(button_tag, None);
assert_matches!(harness.take_records_of(button_tag)[..], []);
assert_matches!(harness.focused_widget_id(), None);
}
#[test]
fn disable_parent() {
let button_tag = WidgetTag::named("button");
let parent_tag = WidgetTag::named("parent");
let grandparent_tag = WidgetTag::named("grandparent_tag");
let child = NewWidget::new(Button::with_text("").record()).with_tag(button_tag);
let parent = NewWidget::new(ModularWidget::new_parent(child)).with_tag(parent_tag);
let grandparent = NewWidget::new(ModularWidget::new_parent(parent)).with_tag(grandparent_tag);
let mut harness = TestHarness::create(test_property_set(), grandparent);
harness.flush_records_of(button_tag);
harness.set_disabled(parent_tag, true);
assert_matches!(
harness.take_records_of(button_tag)[..],
[Record::Update(Update::DisabledChanged(true))]
);
assert!(harness.get_widget(button_tag).ctx().is_disabled());
harness.set_disabled(grandparent_tag, true);
assert_matches!(harness.take_records_of(button_tag)[..], []);
harness.set_disabled(parent_tag, false);
assert_matches!(harness.take_records_of(button_tag)[..], []);
harness.set_disabled(grandparent_tag, false);
assert_matches!(
harness.take_records_of(button_tag)[..],
[Record::Update(Update::DisabledChanged(false))]
);
harness.set_disabled(button_tag, false);
assert_matches!(harness.take_records_of(button_tag)[..], []);
}
#[test]
fn stashed_widget_loses_focus() {
let button_tag = WidgetTag::named("button");
let parent_tag = WidgetTag::named("parent");
let child = NewWidget::new(Button::with_text("").record()).with_tag(button_tag);
let parent = NewWidget::new(ModularWidget::new_parent(child)).with_tag(parent_tag);
let mut harness = TestHarness::create(test_property_set(), parent);
harness.focus_on(button_tag);
harness.flush_records_of(button_tag);
harness.edit_widget(parent_tag, |mut widget| {
widget.ctx.set_stashed(&mut widget.widget.state, true);
});
assert_matches!(
harness.take_records_of(button_tag)[..],
[
Record::Update(Update::StashedChanged(true)),
Record::Update(Update::ChildFocusChanged(false)),
Record::Update(Update::FocusChanged(false)),
]
);
assert_matches!(harness.focused_widget_id(), None);
}
#[test]
fn stash_parent() {
let button_tag = WidgetTag::named("button");
let parent_tag = WidgetTag::named("parent");
let grandparent_tag = WidgetTag::named("grandparent_tag");
let child = NewWidget::new(Button::with_text("").record()).with_tag(button_tag);
let parent = NewWidget::new(ModularWidget::new_parent(child)).with_tag(parent_tag);
let grandparent = NewWidget::new(ModularWidget::new_parent(parent)).with_tag(grandparent_tag);
let mut harness = TestHarness::create(test_property_set(), grandparent);
harness.flush_records_of(button_tag);
harness.edit_widget(parent_tag, |mut widget| {
widget.ctx.set_stashed(&mut widget.widget.state, true);
});
assert_matches!(
harness.take_records_of(button_tag)[..],
[Record::Update(Update::StashedChanged(true))]
);
assert!(harness.get_widget(button_tag).ctx().is_stashed());
harness.edit_widget(grandparent_tag, |mut widget| {
widget.ctx.set_stashed(&mut widget.widget.state, true);
});
assert_matches!(harness.take_records_of(button_tag)[..], []);
harness.edit_widget(parent_tag, |mut widget| {
widget.ctx.set_stashed(&mut widget.widget.state, false);
});
assert_matches!(harness.take_records_of(button_tag)[..], []);
harness.edit_widget(grandparent_tag, |mut widget| {
widget.ctx.set_stashed(&mut widget.widget.state, false);
});
assert_matches!(
harness.take_records_of(button_tag)[..],
[
Record::Update(Update::StashedChanged(false)),
Record::Measure(_),
Record::Measure(_),
Record::Measure(_),
Record::Measure(_),
Record::Layout(_),
Record::Compose
]
);
}
fn focusable_child(name: &'static str) -> NewWidget<impl Widget> {
NewWidget::new(ModularWidget::new(()).accepts_focus(true))
.with_props(PropertySet::one(DebugName(name.to_string())))
}
fn focusable_parent(
name: &'static str,
children: Vec<NewWidget<impl Widget + ?Sized>>,
) -> NewWidget<impl Widget> {
NewWidget::new(ModularWidget::new_multi_parent(children).accepts_focus(true))
.with_props(PropertySet::one(DebugName(name.to_string())))
}
#[test]
fn focus_order() {
let child_a1 = focusable_child("child_a1");
let child_a2 = focusable_child("child_a2");
let child_a3 = focusable_child("child_a3");
let child_b1 = focusable_child("child_b1");
let child_c1 = focusable_child("child_c1");
let child_c2 = NewWidget::new(SizedBox::empty());
let child_d1 = NewWidget::new(SizedBox::empty());
let child_d2 = focusable_child("child_d2");
let parent_a = focusable_parent("parent_a", vec![child_a1, child_a2, child_a3]);
let parent_b = focusable_parent("parent_b", vec![child_b1]);
let parent_c = focusable_parent("parent_c", vec![child_c1.erased(), child_c2.erased()]);
let parent_d = focusable_parent("parent_d", vec![child_d1.erased(), child_d2.erased()]);
let root = focusable_parent(
"root",
vec![
parent_a.erased(),
parent_b.erased(),
parent_c.erased(),
parent_d.erased(),
],
);
let mut harness = TestHarness::create(test_property_set(), root);
fn get_name(harness: &TestHarness<impl Widget>, id: Option<WidgetId>) -> Option<String> {
Some(
harness
.get_widget_with_id(id?)
.get_prop::<DebugName>()
.0
.clone(),
)
}
let mut focusable_widgets = Vec::new();
harness.inspect_widgets(|widget| {
if widget.ctx().accepts_focus() {
let id = widget.ctx().widget_id();
focusable_widgets.push(id);
}
});
let mut next_focusable_widgets = focusable_widgets.clone();
next_focusable_widgets.remove(0);
next_focusable_widgets.push(*focusable_widgets.first().unwrap());
for (&id, &next_id) in std::iter::zip(&focusable_widgets, &next_focusable_widgets) {
let tag = harness.make_dyn_tag_for_widget(id);
harness.focus_on(tag);
harness.press_tab_key(false);
let focused = harness.focused_widget_id();
assert_eq!(
get_name(&harness, focused),
get_name(&harness, Some(next_id)),
"failed to find the right successor for {}",
get_name(&harness, Some(id)).unwrap(),
);
assert_eq!(focused, Some(next_id));
harness.press_tab_key(true);
let focused = harness.focused_widget_id();
assert_eq!(
get_name(&harness, focused),
get_name(&harness, Some(id)),
"failed to find the right predecessor for {}",
get_name(&harness, Some(next_id)).unwrap(),
);
assert_eq!(focused, Some(id));
}
harness.clear_focus();
harness.press_tab_key(false);
let focused = harness.focused_widget_id();
assert_eq!(
get_name(&harness, focused),
get_name(&harness, focusable_widgets.first().copied())
);
assert_eq!(focused, focusable_widgets.first().copied());
harness.clear_focus();
harness.press_tab_key(true);
let focused = harness.focused_widget_id();
assert_eq!(
get_name(&harness, focused),
get_name(&harness, focusable_widgets.last().copied())
);
assert_eq!(focused, focusable_widgets.last().copied());
}
#[test]
fn disable_focusable() {
let button1_tag = WidgetTag::named("button1");
let button2_tag = WidgetTag::named("button2");
let button3_tag = WidgetTag::named("button3");
let button1 = NewWidget::new(Button::with_text("")).with_tag(button1_tag);
let button2 = NewWidget::new(Button::with_text("")).with_tag(button2_tag);
let button3 = NewWidget::new(Button::with_text("")).with_tag(button3_tag);
let parent = NewWidget::new(ModularWidget::new_multi_parent(vec![
button1, button2, button3,
]));
let mut harness = TestHarness::create(test_property_set(), parent);
let button1_id = harness.get_widget(button1_tag).id();
let button3_id = harness.get_widget(button3_tag).id();
harness.focus_on(button2_tag);
harness.set_disabled(button2_tag, true);
harness.focus_on(button1_tag);
harness.press_tab_key(false);
assert_eq!(harness.focused_widget_id(), Some(button3_id));
harness.press_tab_key(true);
assert_eq!(harness.focused_widget_id(), Some(button1_id));
}
#[test]
fn stash_focusable() {
let button1_tag = WidgetTag::named("button1");
let button2_tag = WidgetTag::named("button2");
let button3_tag = WidgetTag::named("button3");
let button1 = NewWidget::new(Button::with_text("")).with_tag(button1_tag);
let button2 = NewWidget::new(Button::with_text("")).with_tag(button2_tag);
let button3 = NewWidget::new(Button::with_text("")).with_tag(button3_tag);
let parent = NewWidget::new(ModularWidget::new_multi_parent(vec![
button1, button2, button3,
]));
let mut harness = TestHarness::create(test_property_set(), parent);
let button1_id = harness.get_widget(button1_tag).id();
let button3_id = harness.get_widget(button3_tag).id();
harness.focus_on(button2_tag);
harness.edit_root_widget(|mut parent| {
parent.ctx.set_stashed(&mut parent.widget.state[1], true);
});
harness.focus_on(button1_tag);
harness.press_tab_key(false);
assert_eq!(harness.focused_widget_id(), Some(button3_id));
harness.press_tab_key(true);
assert_eq!(harness.focused_widget_id(), Some(button1_id));
}
#[test]
fn remove_focusable() {
let button1_tag = WidgetTag::named("button1");
let button2_tag = WidgetTag::named("button2");
let button3_tag = WidgetTag::named("button3");
let button1 = NewWidget::new(Button::with_text("")).with_tag(button1_tag);
let button2 = NewWidget::new(Button::with_text("")).with_tag(button2_tag);
let button3 = NewWidget::new(Button::with_text("")).with_tag(button3_tag);
let parent = NewWidget::new(ModularWidget::new_multi_parent(vec![
button1, button2, button3,
]));
let mut harness = TestHarness::create(test_property_set(), parent);
let button1_id = harness.get_widget(button1_tag).id();
let button3_id = harness.get_widget(button3_tag).id();
harness.focus_on(button2_tag);
harness.edit_root_widget(|mut parent| {
let child = parent.widget.state.remove(1);
parent.ctx.remove_child(child);
});
harness.focus_on(button1_tag);
harness.press_tab_key(false);
assert_eq!(harness.focused_widget_id(), Some(button3_id));
harness.press_tab_key(true);
assert_eq!(harness.focused_widget_id(), Some(button1_id));
}
#[test]
fn ime_commit() {
let textbox_tag = WidgetTag::named("textbox");
let textbox = NewWidget::new(TextArea::new_editable("")).with_tag(textbox_tag);
let mut harness = TestHarness::create(test_property_set(), textbox);
harness.focus_on(textbox_tag);
harness.process_text_event(TextEvent::Ime(Ime::Commit("New Text".to_string())));
assert_eq!(harness.get_widget(textbox_tag).text(), "New Text");
harness.process_text_event(TextEvent::Ime(Ime::Commit(" and more".to_string())));
assert_eq!(harness.get_widget(textbox_tag).text(), "New Text and more");
let ime_area_size = harness.ime_rect().1;
assert!(ime_area_size.width > 0. && ime_area_size.height > 0.);
}
#[test]
fn ime_removed() {
let textbox_tag = WidgetTag::named("textbox");
let textbox = NewWidget::new(TextArea::new_editable("")).with_tag(textbox_tag);
let parent = NewWidget::new(SizedBox::new(textbox));
let mut harness = TestHarness::create(test_property_set(), parent);
harness.focus_on(textbox_tag);
harness.edit_root_widget(|mut sized_box| {
SizedBox::remove_child(&mut sized_box);
});
assert!(!harness.has_ime_session());
assert_matches!(harness.focused_widget_id(), None);
}
#[test]
fn ime_start_stop() {
let textbox_tag = WidgetTag::named("textbox");
let textbox = NewWidget::new(TextArea::new_editable("").record()).with_tag(textbox_tag);
let parent = NewWidget::new(ModularWidget::new_parent(textbox));
let mut harness = TestHarness::create(test_property_set(), parent);
harness.focus_on(textbox_tag);
assert!(harness.has_ime_session());
harness.flush_records_of(textbox_tag);
harness.set_disabled(textbox_tag, true);
let records = harness.take_records_of(textbox_tag);
assert_any(records, |r| {
matches!(r, Record::TextEvent(TextEvent::Ime(Ime::Disabled)))
});
assert!(!harness.has_ime_session());
}
fn create_icon_widget() -> ModularWidget<()> {
ModularWidget::new(())
.pointer_event_fn(|_, ctx, _, event| {
if matches!(event, PointerEvent::Down { .. }) {
ctx.capture_pointer();
}
})
.cursor_icon(CursorIcon::Crosshair)
.measure_fn(|_, _, _, _, _, _| 10.px())
}
#[test]
fn cursor_icon() {
let icon_tag = WidgetTag::named("icon");
let label = NewWidget::new(Button::with_text("hello"));
let icon_widget = NewWidget::new(create_icon_widget()).with_tag(icon_tag);
let parent = NewWidget::new(Flex::row().with_fixed(label).with_fixed(icon_widget));
let mut harness = TestHarness::create(test_property_set(), parent);
assert_eq!(harness.cursor_icon(), CursorIcon::Default);
harness.mouse_move_to(icon_tag);
assert_eq!(harness.cursor_icon(), CursorIcon::Crosshair);
}
#[test]
fn pointer_capture_affects_pointer_icon() {
let label_tag = WidgetTag::named("label");
let icon_tag = WidgetTag::named("icon");
let label = NewWidget::new(Button::with_text("hello")).with_tag(label_tag);
let icon_widget = NewWidget::new(create_icon_widget()).with_tag(icon_tag);
let parent = NewWidget::new(Flex::row().with_fixed(label).with_fixed(icon_widget));
let mut harness = TestHarness::create(test_property_set(), parent);
harness.mouse_move_to(icon_tag);
harness.mouse_button_press(None);
assert_eq!(harness.cursor_icon(), CursorIcon::Crosshair);
harness.mouse_move_to(label_tag);
assert_eq!(harness.cursor_icon(), CursorIcon::Crosshair);
harness.mouse_button_release(None);
assert_eq!(harness.cursor_icon(), CursorIcon::Default);
}
#[test]
fn lose_hovered_on_pointer_leave_or_cancel() {
let button_tag = WidgetTag::named("button");
let button = NewWidget::new(Button::with_text("button").record()).with_tag(button_tag);
let mut harness = TestHarness::create(test_property_set(), button);
harness.mouse_move_to(button_tag);
assert!(harness.get_widget(button_tag).ctx().is_hovered());
harness.flush_records_of(button_tag);
harness.process_pointer_event(PointerEvent::Leave(PRIMARY_MOUSE));
assert!(!harness.get_widget(button_tag).ctx().is_hovered());
let records = harness.take_records_of(button_tag);
assert_any(records, |r| {
matches!(r, Record::Update(Update::HoveredChanged(false)))
});
harness.mouse_move_to(button_tag);
assert!(harness.get_widget(button_tag).ctx().is_hovered());
harness.flush_records_of(button_tag);
harness.process_pointer_event(PointerEvent::Cancel(PRIMARY_MOUSE));
assert!(!harness.get_widget(button_tag).ctx().is_hovered());
let records = harness.take_records_of(button_tag);
assert_any(records, |r| {
matches!(r, Record::Update(Update::HoveredChanged(false)))
});
}
#[test]
fn change_hovered_when_widget_changes() {
const BOX_SIZE: Length = Length::const_px(50.);
let child_tag = WidgetTag::named("child");
let parent_tag = WidgetTag::named("parent");
let child =
NewWidget::new(ModularWidget::new(BOX_SIZE).measure_fn(|size, _, _, _, _, _| *size))
.with_tag(child_tag);
let parent =
NewWidget::new(ModularWidget::new_parent(child).measure_fn(|_, _, _, _, _, _| BOX_SIZE))
.with_tag(parent_tag);
let mut harness = TestHarness::create(test_property_set(), parent);
harness.mouse_move_to(child_tag);
assert!(harness.get_widget(child_tag).ctx().is_hovered());
assert!(!harness.get_widget(parent_tag).ctx().is_hovered());
harness.edit_widget(child_tag, |mut child| {
child.widget.state = Length::ZERO;
child.ctx.request_layout();
});
assert!(!harness.get_widget(child_tag).ctx().is_hovered());
assert!(harness.get_widget(parent_tag).ctx().is_hovered());
harness.edit_widget(child_tag, |mut child| {
child.widget.state = BOX_SIZE;
child.ctx.request_layout();
});
assert!(harness.get_widget(child_tag).ctx().is_hovered());
assert!(!harness.get_widget(parent_tag).ctx().is_hovered());
}
fn make_reporter_parent(
child: NewWidget<impl Widget>,
sender: mpsc::Sender<(String, u32)>,
n: u32,
) -> impl Widget {
ModularWidget::new_parent(child)
.accepts_focus(true)
.pointer_event_fn(|_, ctx, _, event| {
if matches!(event, PointerEvent::Down { .. }) {
ctx.capture_pointer();
ctx.set_handled();
}
})
.measure_fn(|_, _, _, _, _, _| 100.px())
.update_fn(move |_, _, _, event| {
sender.send((event.short_name().to_string(), n)).unwrap();
})
}
#[test]
fn status_flag_update_order() {
let (sender, receiver) = mpsc::channel::<(String, u32)>();
let sender1 = sender.clone();
let sender2 = sender.clone();
let sender3 = sender;
let parent1_tag = WidgetTag::named("parent1");
let child = NewWidget::new(Label::new(""));
let parent1 = NewWidget::new(make_reporter_parent(child, sender1, 1)).with_tag(parent1_tag);
let parent2 = NewWidget::new(make_reporter_parent(parent1, sender2, 2));
let parent3 = NewWidget::new(make_reporter_parent(parent2, sender3, 3));
let mut harness = TestHarness::create(test_property_set(), parent3);
let _ = receiver.try_iter().count();
harness.mouse_move_to(parent1_tag);
let events: Vec<_> = receiver.try_iter().collect();
assert_eq!(
events,
[
("ChildHoveredChanged(true)".into(), 1),
("ChildHoveredChanged(true)".into(), 2),
("ChildHoveredChanged(true)".into(), 3),
("HoveredChanged(true)".into(), 1)
]
);
assert!(harness.get_widget(parent1_tag).ctx().is_hovered());
assert!(harness.get_widget(parent1_tag).ctx().has_hovered());
harness.mouse_button_press(None);
let events: Vec<_> = receiver.try_iter().collect();
assert_eq!(
events,
[
("ChildActiveChanged(true)".into(), 1),
("ChildActiveChanged(true)".into(), 2),
("ChildActiveChanged(true)".into(), 3),
("ActiveChanged(true)".into(), 1)
]
);
assert!(harness.get_widget(parent1_tag).ctx().is_active());
assert!(harness.get_widget(parent1_tag).ctx().has_active());
harness.mouse_button_release(None);
let events: Vec<_> = receiver.try_iter().collect();
assert_eq!(
events,
[
("ChildActiveChanged(false)".into(), 1),
("ChildActiveChanged(false)".into(), 2),
("ChildActiveChanged(false)".into(), 3),
("ActiveChanged(false)".into(), 1)
]
);
assert!(!harness.get_widget(parent1_tag).ctx().is_active());
assert!(!harness.get_widget(parent1_tag).ctx().has_active());
harness.mouse_move((-10., -10.));
let events: Vec<_> = receiver.try_iter().collect();
assert_eq!(
events,
[
("ChildHoveredChanged(false)".into(), 1),
("ChildHoveredChanged(false)".into(), 2),
("ChildHoveredChanged(false)".into(), 3),
("HoveredChanged(false)".into(), 1)
]
);
assert!(!harness.get_widget(parent1_tag).ctx().is_hovered());
assert!(!harness.get_widget(parent1_tag).ctx().has_hovered());
harness.focus_on(parent1_tag);
let events: Vec<_> = receiver.try_iter().collect();
assert_eq!(
events,
[
("ChildFocusChanged(true)".into(), 1),
("ChildFocusChanged(true)".into(), 2),
("ChildFocusChanged(true)".into(), 3),
("FocusChanged(true)".into(), 1)
]
);
assert!(harness.get_widget(parent1_tag).ctx().is_focus_target());
assert!(harness.get_widget(parent1_tag).ctx().has_focus_target());
harness.clear_focus();
let events: Vec<_> = receiver.try_iter().collect();
assert_eq!(
events,
[
("ChildFocusChanged(false)".into(), 1),
("ChildFocusChanged(false)".into(), 2),
("ChildFocusChanged(false)".into(), 3),
("FocusChanged(false)".into(), 1)
]
);
assert!(!harness.get_widget(parent1_tag).ctx().is_focus_target());
assert!(!harness.get_widget(parent1_tag).ctx().has_focus_target());
}
#[test]
fn set_default_properties_refires_property_changed() {
use std::any::TypeId;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use crate::peniko::color::AlphaColor;
use crate::properties::ContentColor;
let changed: Rc<RefCell<Vec<TypeId>>> = Rc::new(RefCell::new(Vec::new()));
let widget = {
let changed = changed.clone();
ModularWidget::new(())
.paint_fn(|_, ctx, props, _painter| {
let cache = ctx.property_cache();
let _ = props.get::<ContentColor>(cache);
})
.property_change_fn(move |_, _ctx, property_type| {
changed.borrow_mut().push(property_type);
})
};
let mut defaults = test_property_set();
defaults.insert::<ModularWidget<()>, ContentColor>(ContentColor::new(AlphaColor::WHITE));
let mut harness = TestHarness::create(defaults, NewWidget::new(widget));
let _ = harness.redraw();
changed.borrow_mut().clear();
let mut new_defaults = test_property_set();
new_defaults.insert::<ModularWidget<()>, ContentColor>(ContentColor::new(AlphaColor::BLACK));
harness.set_default_properties(Arc::new(new_defaults));
let _ = harness.redraw();
assert!(
changed.borrow().contains(&TypeId::of::<ContentColor>()),
"expected property_changed to re-fire for ContentColor after the default swap, got {:?}",
changed.borrow(),
);
}