use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::assert_matches;
use crate::core::{ChildrenIds, Widget, WidgetTag};
use crate::kurbo::Point;
use crate::layout::{AsUnit, LayoutSize, Length};
use crate::properties::Dimensions;
use crate::testing::{ModularWidget, TestHarness};
use crate::theme::test_property_set;
use crate::widgets::{Button, ButtonPress};
#[test]
fn action_source_removed() {
let ok = Arc::new(AtomicBool::new(false));
#[derive(Debug)]
struct ArbitraryAction;
let action_source_tag = WidgetTag::named("action_source");
let action_source = ModularWidget::new(ok.clone())
.pointer_event_fn(|ok, ctx, _, _| {
ctx.submit_untyped_action(Box::new(ArbitraryAction));
ok.store(true, Ordering::Release);
})
.prepare()
.with_props(Dimensions::fixed(50.px(), 50.px()))
.with_tag(action_source_tag)
.to_pod();
let parent = ModularWidget::new(Some(action_source))
.pointer_event_fn(|child, ctx, _, _| {
if let Some(child) = child.take() {
ctx.remove_child(child);
}
})
.action_fn(|_, _, _, _, _| {
panic!("Unexpected action");
})
.register_children_fn(|child, ctx| {
if let Some(child) = child {
ctx.register_child(child);
}
})
.measure_fn(move |child, ctx, _props, axis, len_req, cross_length| {
if let Some(child) = child {
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);
ctx.compute_length(child, auto_length, context_size, axis, cross_length)
} else {
Length::ZERO
}
})
.layout_fn(move |child, ctx, _props, size| {
if let Some(child) = child {
ctx.run_layout(child, size);
ctx.place_child(child, Point::ZERO);
}
})
.children_fn(|child| {
let mut ids = ChildrenIds::new();
if let Some(child) = child {
ids.push(child.id());
}
ids
})
.prepare();
let mut harness = TestHarness::create(test_property_set(), parent);
harness.mouse_move_to(action_source_tag);
assert_matches!(harness.pop_action::<ArbitraryAction>(), None);
assert!(ok.load(Ordering::Acquire), "pointer event didn't happen");
}
#[test]
fn action_propagation() {
#[derive(Debug)]
struct TranslatedAction;
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)
.action_fn(move |_, _, _, action, source| {
assert_eq!(source, button_id, "unexpected action source");
assert!(action.is::<ButtonPress>(), "unexpected action type");
})
.prepare();
let parent2 = ModularWidget::new_parent(parent1)
.action_fn(move |_, ctx, _, action, source| {
assert_eq!(source, button_id, "unexpected action source");
assert!(action.is::<ButtonPress>(), "unexpected action type");
ctx.set_handled();
ctx.submit_untyped_action(Box::new(TranslatedAction));
})
.prepare();
let parent2_id = parent2.id();
let parent3 = ModularWidget::new_parent(parent2)
.action_fn(move |_, _, _, action, source| {
assert_eq!(source, parent2_id, "unexpected action source");
assert!(action.is::<TranslatedAction>(), "unexpected action type");
})
.prepare();
let mut harness = TestHarness::create(test_property_set(), parent3);
harness.mouse_click_on(button_tag, None);
assert_matches!(
harness.pop_action::<TranslatedAction>(),
Some((TranslatedAction, _))
);
assert_matches!(harness.pop_action_erased(), None);
}