use strata::prelude::*;
use strata_platform::{KeyCode, Modifiers, PlatformEvent};
#[test]
fn test_scroll_container_offset_clamping_and_translation() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="scroll-root" class="flex flex-col w-[800px] h-[400px] overflow-y-auto bg-black">
<div class="h-[300px] w-full bg-red-500" />
<div class="h-[300px] w-full bg-blue-500" />
<div class="h-[300px] w-full bg-green-500" />
</div>
},
800.0,
400.0,
1.0,
);
assert_eq!(runtime.active_scrollbars.len(), 1);
let bar = &runtime.active_scrollbars[0];
assert_eq!(bar.scroll_y, 0.0);
assert!((bar.max_scroll_y - 500.0).abs() < 2.0);
let child0_y = runtime.layout_root.children[0].geometry.border_box.y;
assert_eq!(child0_y, 0.0);
let scrolled = runtime.dispatch_scroll(400.0, 200.0, 0.0, 120.0);
assert!(scrolled);
assert_eq!(runtime.active_scrollbars[0].scroll_y, 120.0);
let child0_scrolled_y = runtime.layout_root.children[0].geometry.border_box.y;
assert_eq!(child0_scrolled_y, -120.0);
runtime.dispatch_scroll(400.0, 200.0, 0.0, 1000.0);
assert_eq!(runtime.active_scrollbars[0].scroll_y, 500.0);
let child0_clamped_bottom_y = runtime.layout_root.children[0].geometry.border_box.y;
assert_eq!(child0_clamped_bottom_y, -500.0);
runtime.dispatch_scroll(400.0, 200.0, 0.0, -2000.0);
assert_eq!(runtime.active_scrollbars[0].scroll_y, 0.0);
let child0_clamped_top_y = runtime.layout_root.children[0].geometry.border_box.y;
assert_eq!(child0_clamped_top_y, 0.0);
}
#[test]
fn test_keyboard_navigation_scrolling() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="keyboard-scroll" class="flex flex-col w-[600px] h-[300px] overflow-y-auto">
<div class="h-[200px] w-full" />
<div class="h-[200px] w-full" />
<div class="h-[200px] w-full" />
<div class="h-[200px] w-full" />
</div>
},
600.0,
300.0,
1.0,
);
runtime.last_mouse_pos = (300.0, 150.0);
let down_ev = PlatformEvent::KeyDown {
key: KeyCode::ArrowDown,
modifiers: Modifiers::default(),
};
assert!(runtime.handle_key_event(&down_ev));
assert_eq!(runtime.active_scrollbars[0].scroll_y, 40.0);
let pagedown_ev = PlatformEvent::KeyDown {
key: KeyCode::PageDown,
modifiers: Modifiers::default(),
};
assert!(runtime.handle_key_event(&pagedown_ev));
assert_eq!(runtime.active_scrollbars[0].scroll_y, 440.0);
let pageup_ev = PlatformEvent::KeyDown {
key: KeyCode::PageUp,
modifiers: Modifiers::default(),
};
assert!(runtime.handle_key_event(&pageup_ev));
assert_eq!(runtime.active_scrollbars[0].scroll_y, 40.0);
let end_ev = PlatformEvent::KeyDown {
key: KeyCode::End,
modifiers: Modifiers::default(),
};
assert!(runtime.handle_key_event(&end_ev));
assert_eq!(runtime.active_scrollbars[0].scroll_y, 500.0);
let home_ev = PlatformEvent::KeyDown {
key: KeyCode::Home,
modifiers: Modifiers::default(),
};
assert!(runtime.handle_key_event(&home_ev));
assert_eq!(runtime.active_scrollbars[0].scroll_y, 0.0);
}
#[test]
fn test_scrollbar_thumb_press_and_drag() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="thumb-drag" class="flex flex-col w-[500px] h-[200px] overflow-y-auto">
<div class="h-[1000px] w-full" />
</div>
},
500.0,
200.0,
1.0,
);
assert_eq!(runtime.active_scrollbars.len(), 1);
let thumb = runtime.active_scrollbars[0].thumb_bounds;
assert!(!runtime.check_scrollbar_thumb_press(10.0, 10.0));
assert!(runtime.scrollbar_drag.is_none());
let thumb_center_x = thumb.x + thumb.width / 2.0;
let thumb_center_y = thumb.y + thumb.height / 2.0;
assert!(runtime.check_scrollbar_thumb_press(thumb_center_x, thumb_center_y));
assert!(runtime.scrollbar_drag.is_some());
assert!(runtime.poll_scrollbar_drag(thumb_center_x, thumb_center_y + 50.0));
assert!(runtime.active_scrollbars[0].scroll_y > 0.0);
}
#[test]
fn test_scroll_multiple_frames() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="scroll-root" class="flex flex-col w-[800px] h-[400px] overflow-y-auto bg-black">
<div class="h-[300px] w-full bg-red-500" />
<div class="h-[300px] w-full bg-blue-500" />
<div class="h-[300px] w-full bg-green-500" />
</div>
},
800.0,
400.0,
1.0,
);
assert_eq!(runtime.active_scrollbars[0].content_h, 900.0);
assert_eq!(runtime.active_scrollbars[0].max_scroll_y, 500.0);
let initial_thumb_h = runtime.active_scrollbars[0].thumb_bounds.height;
runtime.dispatch_scroll(400.0, 200.0, 0.0, 120.0);
assert_eq!(runtime.active_scrollbars[0].scroll_y, 120.0);
assert_eq!(runtime.active_scrollbars[0].content_h, 900.0);
assert_eq!(runtime.active_scrollbars[0].max_scroll_y, 500.0);
assert_eq!(
runtime.layout_root.children[0].geometry.border_box.y,
-120.0
);
assert!((runtime.active_scrollbars[0].thumb_bounds.height - initial_thumb_h).abs() < 0.01);
for frame in 1..=25 {
runtime.tick_frame();
assert_eq!(
runtime.active_scrollbars[0].scroll_y, 120.0,
"Frame {frame}: scroll_y must remain 120.0"
);
assert_eq!(
runtime.active_scrollbars[0].content_h, 900.0,
"Frame {frame}: content_h must remain 900.0"
);
assert_eq!(
runtime.active_scrollbars[0].max_scroll_y, 500.0,
"Frame {frame}: max_scroll_y must remain 500.0"
);
assert_eq!(
runtime.layout_root.children[0].geometry.border_box.y, -120.0,
"Frame {frame}: child0 border_box.y must remain -120.0"
);
assert!(
(runtime.active_scrollbars[0].thumb_bounds.height - initial_thumb_h).abs() < 0.01,
"Frame {frame}: thumb height must remain constant"
);
}
}
#[test]
fn test_smooth_scroll_critically_damped_convergence() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="smooth-scroll" class="flex flex-col w-[800px] h-[400px] overflow-y-auto">
<div class="h-[300px] w-full" />
<div class="h-[300px] w-full" />
<div class="h-[300px] w-full" />
</div>
},
800.0,
400.0,
1.0,
);
let start_t = std::time::Instant::now();
let initiated = runtime.dispatch_smooth_scroll(400.0, 200.0, 0.0, 120.0);
assert!(initiated, "Smooth scroll must initiate successfully");
assert!(
runtime.active_smooth_scroll.is_some(),
"Tracker must be active"
);
if let Some(ref mut tr) = runtime.active_smooth_scroll {
tr.start_time = start_t;
tr.last_event_time = start_t;
}
let mut prev_y = 0.0f32;
for step in 1..=15 {
let tick_t = start_t + std::time::Duration::from_millis(step * 20);
let updated = runtime.poll_smooth_scroll_at(tick_t);
let cur_y = runtime.active_scrollbars[0].scroll_y;
assert!(updated || cur_y >= 119.5);
assert!(
cur_y >= prev_y,
"Scroll offset must be monotonically non-decreasing: cur={cur_y}, prev={prev_y}"
);
prev_y = cur_y;
}
let settle_t = start_t + std::time::Duration::from_millis(450);
runtime.poll_smooth_scroll_at(settle_t);
assert_eq!(
runtime.active_scrollbars[0].scroll_y, 120.0,
"Must settle exactly at 120.0px target"
);
assert!(
runtime.active_smooth_scroll.is_none(),
"Tracker must be cleared on settle"
);
let idle_updated =
runtime.poll_smooth_scroll_at(settle_t + std::time::Duration::from_millis(50));
assert!(!idle_updated, "Idle tick must return false to sleep CPU");
}
#[test]
fn test_smooth_scroll_flick_velocity_acceleration() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="flick-scroll" class="flex flex-col w-[800px] h-[400px] overflow-y-auto">
<div class="h-[2000px] w-full" />
</div>
},
800.0,
400.0,
1.0,
);
let t0 = std::time::Instant::now();
runtime.dispatch_smooth_scroll(400.0, 200.0, 0.0, 110.0);
if let Some(ref mut tr) = runtime.active_smooth_scroll {
tr.start_time = t0;
tr.last_event_time = t0;
}
let t1 = t0 + std::time::Duration::from_millis(40);
runtime.poll_smooth_scroll_at(t1);
runtime.dispatch_smooth_scroll(400.0, 200.0, 0.0, 110.0);
let target = runtime.active_smooth_scroll.as_ref().unwrap().target_y;
assert!(
target > 220.0,
"Rapid flick acceleration must accumulate dynamic momentum: target was {target}"
);
let t_end = t1 + std::time::Duration::from_millis(500);
runtime.poll_smooth_scroll_at(t_end);
assert_eq!(runtime.active_scrollbars[0].scroll_y, target);
assert!(runtime.active_smooth_scroll.is_none());
}
#[test]
fn test_smooth_scroll_boundary_clamping() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="boundary-scroll" class="flex flex-col w-[800px] h-[400px] overflow-y-auto">
<div class="h-[1000px] w-full" />
</div>
},
800.0,
400.0,
1.0,
);
assert_eq!(runtime.active_scrollbars[0].max_scroll_y, 600.0);
let t0 = std::time::Instant::now();
runtime.dispatch_smooth_scroll(400.0, 200.0, 0.0, 5000.0);
if let Some(ref mut tr) = runtime.active_smooth_scroll {
tr.start_time = t0;
tr.last_event_time = t0;
}
runtime.poll_smooth_scroll_at(t0 + std::time::Duration::from_millis(500));
assert_eq!(
runtime.active_scrollbars[0].scroll_y, 600.0,
"Must clamp to max_scroll_y exactly"
);
let t1 = t0 + std::time::Duration::from_millis(600);
runtime.dispatch_smooth_scroll(400.0, 200.0, 0.0, -8000.0);
if let Some(ref mut tr) = runtime.active_smooth_scroll {
tr.start_time = t1;
tr.last_event_time = t1;
}
runtime.poll_smooth_scroll_at(t1 + std::time::Duration::from_millis(500));
assert_eq!(
runtime.active_scrollbars[0].scroll_y, 0.0,
"Must clamp to 0.0 exactly"
);
}
#[test]
fn test_smooth_scroll_interrupted_by_manual_thumb_drag() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="interrupted-scroll" class="flex flex-col w-[800px] h-[400px] overflow-y-auto">
<div class="h-[1200px] w-full" />
</div>
},
800.0,
400.0,
1.0,
);
runtime.dispatch_smooth_scroll(400.0, 200.0, 0.0, 200.0);
assert!(runtime.active_smooth_scroll.is_some());
let thumb = runtime.active_scrollbars[0].thumb_bounds;
let thumb_center_x = thumb.x + thumb.width / 2.0;
let thumb_center_y = thumb.y + thumb.height / 2.0;
assert!(runtime.check_scrollbar_thumb_press(thumb_center_x, thumb_center_y));
assert!(
runtime.active_smooth_scroll.is_none(),
"Manual thumb press must immediately cancel kinetic scroll"
);
assert!(runtime.scrollbar_drag.is_some());
}
#[test]
fn test_scrollbar_right_edge_pixel_perfect_hit_testing() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="edge-scroll" class="flex flex-col w-[800px] h-[400px] overflow-y-auto">
<div class="h-[2000px] w-full" />
</div>
},
800.0,
400.0,
1.0,
);
assert_eq!(runtime.active_scrollbars.len(), 1);
let bar = runtime.active_scrollbars[0].clone();
let expected_right = 800.0;
assert_eq!(
bar.track_bounds.x + bar.track_bounds.width,
expected_right,
"Track right edge must be exactly at 800.0px"
);
assert_eq!(
bar.thumb_bounds.x + bar.thumb_bounds.width,
expected_right,
"Thumb right edge must be exactly at 800.0px"
);
assert_eq!(
bar.track_bounds.y, 0.0,
"Track top edge must be exactly at 0.0px"
);
assert_eq!(
bar.track_bounds.height, 400.0,
"Track height must match container height 400.0px"
);
let thumb_center_y = bar.thumb_bounds.y + bar.thumb_bounds.height / 2.0;
assert!(
runtime.check_scrollbar_thumb_press(800.0, thumb_center_y),
"Must pick scrollbar at x = 800.0px (very edge right)"
);
assert!(runtime.scrollbar_drag.is_some());
runtime.scrollbar_drag = None;
assert!(runtime.check_scrollbar_thumb_press(799.5, thumb_center_y));
assert!(runtime.scrollbar_drag.is_some());
runtime.scrollbar_drag = None;
assert!(runtime.check_scrollbar_thumb_press(799.0, thumb_center_y));
assert!(runtime.scrollbar_drag.is_some());
runtime.scrollbar_drag = None;
assert!(
runtime.check_scrollbar_thumb_press(800.0, 350.0),
"Must pick scrollbar track at x = 800.0px"
);
assert!(runtime.scrollbar_drag.is_some());
assert!(
runtime.active_scrollbars[0].scroll_y > 0.0,
"Clicking track must scroll towards click point"
);
runtime.scrollbar_drag = None;
assert!(
!runtime.check_scrollbar_thumb_press(780.0, thumb_center_y),
"Coordinates left of track must return false"
);
assert!(runtime.scrollbar_drag.is_none());
}
#[test]
fn test_smooth_scroll_consecutive_impulses_preserve_exact_layout_offset() {
let mut runtime = AppRuntime::new(
rsx! {
<div id="rapid-wheel" class="flex flex-col w-[440px] h-[400px] p-5 overflow-y-auto">
<div class="h-[300px] w-full shrink-0" />
<div class="h-[300px] w-full shrink-0" />
</div>
},
440.0,
400.0,
1.0,
);
let initial_y0 = runtime.layout_root.children[0].geometry.border_box.y;
let max_scroll = runtime.active_scrollbars[0].max_scroll_y;
assert!(max_scroll > 200.0);
assert!(runtime.dispatch_smooth_scroll(220.0, 200.0, 0.0, 110.0));
if let Some(ref mut tr) = runtime.active_smooth_scroll {
tr.start_time = std::time::Instant::now() - std::time::Duration::from_millis(25);
tr.last_event_time = tr.start_time;
}
assert!(runtime.dispatch_smooth_scroll(220.0, 200.0, 0.0, 110.0));
if let Some(ref mut tr) = runtime.active_smooth_scroll {
tr.start_time = std::time::Instant::now() - std::time::Duration::from_millis(25);
tr.last_event_time = tr.start_time;
}
assert!(runtime.dispatch_smooth_scroll(220.0, 200.0, 0.0, 500.0));
let settle_t = std::time::Instant::now() + std::time::Duration::from_millis(600);
runtime.poll_smooth_scroll_at(settle_t);
assert!(
(runtime.active_scrollbars[0].scroll_y - max_scroll).abs() < 0.01,
"scroll_y must reach exact max_scroll_y"
);
let actual_y0 = runtime.layout_root.children[0].geometry.border_box.y;
let expected_y0 = initial_y0 - max_scroll;
assert!(
(actual_y0 - expected_y0).abs() < 0.05,
"layout child must scroll all the way to {expected_y0}, got {actual_y0} (lost {}px)",
actual_y0 - expected_y0
);
}
#[test]
fn test_onscroll_event_and_programmatic_scroll_to_bottom_and_scroll_into_view() {
use std::cell::RefCell;
use std::rc::Rc;
let observed_scroll = Rc::new(RefCell::new((0.0_f64, 0.0_f64, 0.0_f64, 0.0_f64)));
let observed_clone = Rc::clone(&observed_scroll);
let on_scroll = move |e: Event| {
*observed_clone.borrow_mut() = (
e.scroll_top,
e.scroll_height,
e.client_height,
e.delta_y,
);
};
let mut runtime = AppRuntime::new(
rsx! {
<div
id="log-scroller"
class="flex flex-col w-[400px] h-[200px] overflow-y-auto bg-neutral-900"
onscroll=on_scroll
>
<div id="item-0" class="h-[150px] w-full shrink-0" />
<div id="item-1" class="h-[150px] w-full shrink-0" />
<div id="item-2" class="h-[150px] w-full shrink-0" />
<div id="item-3" class="h-[150px] w-full shrink-0" />
</div>
},
400.0,
200.0,
1.0,
);
assert_eq!(runtime.scroll_offset_by_id("log-scroller"), (0.0, 0.0));
assert_eq!(get_scroll_offset("log-scroller"), (0.0, 0.0));
assert!(runtime.dispatch_scroll(100.0, 100.0, 0.0, 80.0));
let (top, sh, ch, dy) = *observed_scroll.borrow();
assert!((top - 80.0).abs() < 0.01);
assert!((sh - 600.0).abs() < 0.01);
assert!((ch - 200.0).abs() < 0.01);
assert!((dy - 80.0).abs() < 0.01);
assert_eq!(get_scroll_offset("log-scroller"), (0.0, 80.0));
scroll_to_bottom("log-scroller");
assert!(runtime.flush_pending_scroll_commands());
assert_eq!(runtime.scroll_offset_by_id("log-scroller"), (0.0, 400.0));
assert_eq!(get_scroll_offset("log-scroller"), (0.0, 400.0));
assert!((observed_scroll.borrow().0 - 400.0).abs() < 0.01);
scroll_to_top("log-scroller");
assert!(runtime.flush_pending_scroll_commands());
assert_eq!(runtime.scroll_offset_by_id("log-scroller"), (0.0, 0.0));
scroll_into_view("item-2");
assert!(runtime.flush_pending_scroll_commands());
assert!(
(runtime.scroll_offset_by_id("log-scroller").1 - 250.0).abs() < 0.05,
"scroll_into_view(item-2) should scroll to 250.0, got {}",
runtime.scroll_offset_by_id("log-scroller").1
);
}
#[test]
fn test_canvas2d_component_emits_draw_canvas2d_and_compiles_gpu_and_rasterizer() {
use std::rc::Rc;
use strata::style::types::Color;
use strata_render::compositor::DrawCommand;
use strata_ui::canvas::{Canvas2D, Canvas2DContext};
let draw_cb: Rc<dyn Fn(&mut Canvas2DContext)> = Rc::new(|ctx: &mut Canvas2DContext| {
ctx.set_fill_style(Color::rgba(255, 0, 0, 255));
ctx.fill_rect(10.0, 10.0, 60.0, 40.0);
ctx.set_stroke_style(Color::rgba(0, 255, 0, 255));
ctx.set_line_width(2.0);
ctx.stroke_rect(10.0, 10.0, 60.0, 40.0);
ctx.begin_path();
ctx.move_to(20.0, 80.0);
ctx.line_to(160.0, 80.0);
ctx.stroke();
ctx.begin_path();
ctx.arc(120.0, 50.0, 15.0, 0.0, std::f32::consts::TAU, false);
ctx.fill();
});
let view = Canvas2D(Some(200.0), Some(120.0), None, Some(draw_cb));
let runtime = AppRuntime::new(view, 240.0, 160.0, 1.0);
let has_canvas_cmd = runtime.display_list.commands.iter().any(|cmd| {
matches!(
cmd,
DrawCommand::DrawCanvas2D { commands, .. } if commands.len() == 4
)
});
assert!(
has_canvas_cmd,
"<Canvas2D> must emit DrawCommand::DrawCanvas2D with 4 recorded commands"
);
assert!(
!runtime.gpu_path_segments().is_empty(),
"GpuPathPipeline must compile path segments from DrawCommand::DrawCanvas2D"
);
let quad_batch =
strata_render::GpuBatch::from_display_list(&runtime.display_list, 240.0, 160.0);
assert!(
quad_batch.len() >= 2,
"GpuBatch must compile FillRect and StrokeRect from DrawCommand::DrawCanvas2D"
);
let mut fb = strata_render::FrameBuffer::new(240, 160);
fb.execute_display_list(&runtime.display_list);
let px = fb.get_pixel(30, 30);
assert_eq!(px, Color::rgba(255, 0, 0, 255));
}
#[test]
fn test_svg_text_and_stroke_dasharray_in_rasterizer_and_gpu_pipeline() {
use strata_render::compositor::DrawCommand;
let runtime = AppRuntime::new(
rsx! {
<div class="w-[200px] h-[100px] bg-black">
<svg viewBox="0 0 200 100" class="w-[200px] h-[100px]">
<path
d="M 20 30 L 180 30"
stroke="#ff0000"
stroke-width="6"
stroke-dasharray="20,20"
stroke-linecap="butt"
fill="none"
/>
<text x="100" y="75" font-size="16" text-anchor="middle" fill="#00ff00">
"ALT 120m"
</text>
</svg>
</div>
},
200.0,
100.0,
1.0,
);
let has_dashed_path = runtime.display_list.commands.iter().any(|cmd| {
matches!(
cmd,
DrawCommand::DrawSvgPath { stroke_dasharray, .. }
if stroke_dasharray.as_slice() == [20.0, 20.0]
)
});
assert!(has_dashed_path, "<svg> must propagate stroke-dasharray");
let has_svg_text = runtime.display_list.commands.iter().any(|cmd| {
matches!(
cmd,
DrawCommand::DrawText { text, .. } if text == "ALT 120m"
)
});
assert!(
has_svg_text,
"<svg> must parse <text> and emit DrawCommand::DrawText"
);
let mut fb = strata_render::FrameBuffer::new(200, 100);
fb.execute_display_list(&runtime.display_list);
let dash_on = fb.get_pixel(30, 30);
let dash_gap = fb.get_pixel(50, 30);
assert_eq!(
dash_on.r, 255,
"Pixel inside first dash [20..40] must be painted red"
);
assert_eq!(
dash_gap.r, 0,
"Pixel inside first dash gap [40..60] must remain background black"
);
}
#[test]
fn test_platform_event_file_drop_and_uri_list_parsing() {
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;
let parsed = strata_platform::parse_dropped_uri_list_to_paths(
"# comment\r\nfile:///tmp/mission%20plan.json\r\nfile://localhost/var/data/track.gpx\r\n",
);
assert_eq!(
parsed,
vec![
PathBuf::from("/tmp/mission plan.json"),
PathBuf::from("/var/data/track.gpx"),
]
);
let dropped_files = Rc::new(RefCell::new(Vec::<PathBuf>::new()));
let dropped_clone = Rc::clone(&dropped_files);
let on_drop = move |e: Event| {
*dropped_clone.borrow_mut() = e.files.clone();
};
let mut runtime = AppRuntime::new(
rsx! {
<div
class="w-[300px] h-[200px] bg-neutral-900"
ondrop=on_drop
>
"Drop Zone"
</div>
},
300.0,
200.0,
1.0,
);
runtime.step_event(&PlatformEvent::MouseMove { x: 100.0, y: 100.0 });
let redraw = runtime.step_event(&PlatformEvent::FileDrop(parsed.clone()));
assert!(redraw);
assert_eq!(*dropped_files.borrow(), parsed);
}
#[test]
fn test_use_click_outside_invoked_by_runtime_on_outside_pointer_down() {
let is_open = Signal::new(true);
let _handle = use_click_outside(is_open.clone(), {
let is_open = is_open.clone();
move || is_open.set(false)
});
let on_down = move |e: Event| {
e.stop_propagation();
};
let mut runtime = AppRuntime::new(
rsx! {
<div class="w-[400px] h-[300px] bg-black">
<div
class="w-[150px] h-[100px] bg-neutral-800"
onmousedown=on_down
>
"Inside Popover"
</div>
</div>
},
400.0,
300.0,
1.0,
);
runtime.dispatch_pointer_down(50.0, 50.0);
assert!(
is_open.get(),
"Clicking inside with stop_propagation must not trigger use_click_outside"
);
runtime.dispatch_pointer_down(300.0, 200.0);
assert!(
!is_open.get(),
"Clicking outside must automatically dismiss use_click_outside"
);
}
#[test]
fn test_native_dom_drag_and_drop_lifecycle_with_data_transfer() {
use std::cell::RefCell;
use std::rc::Rc;
let log = Rc::new(RefCell::new(Vec::<String>::new()));
let l1 = Rc::clone(&log);
let l2 = Rc::clone(&log);
let l3 = Rc::clone(&log);
let l4 = Rc::clone(&log);
let l5 = Rc::clone(&log);
let l6 = Rc::clone(&log);
let on_drag_start = move |e: Event| {
e.set_data("text/plain", "node-42");
l1.borrow_mut().push("dragstart".to_string());
};
let on_drag_end = move |_e: Event| {
l2.borrow_mut().push("dragend".to_string());
};
let on_drag_enter = move |_e: Event| {
l3.borrow_mut().push("dragenter".to_string());
};
let on_drag_over = move |e: Event| {
e.prevent_default();
l4.borrow_mut().push("dragover".to_string());
};
let on_drag_leave = move |_e: Event| {
l5.borrow_mut().push("dragleave".to_string());
};
let on_drop = move |e: Event| {
let payload = e.get_data("text/plain").unwrap_or_default();
l6.borrow_mut().push(format!("drop:{payload}"));
};
let mut runtime = AppRuntime::new(
rsx! {
<div class="flex flex-row w-[400px] h-[200px] bg-black">
<div
class="w-[150px] h-[150px] bg-blue-600"
draggable="true"
ondragstart=on_drag_start
ondragend=on_drag_end
>
"Draggable"
</div>
<div
class="w-[150px] h-[150px] bg-neutral-800"
ondragenter=on_drag_enter
ondragover=on_drag_over
ondragleave=on_drag_leave
ondrop=on_drop
>
"Drop Target"
</div>
</div>
},
400.0,
200.0,
1.0,
);
runtime.dispatch_pointer_down(50.0, 50.0);
runtime.dispatch_pointer_move(60.0, 50.0);
runtime.dispatch_pointer_move(220.0, 50.0);
runtime.dispatch_pointer_up(220.0, 50.0);
let events = log.borrow().clone();
assert!(events.contains(&"dragstart".to_string()));
assert!(events.contains(&"dragenter".to_string()));
assert!(events.contains(&"dragover".to_string()));
assert!(events.contains(&"drop:node-42".to_string()));
assert_eq!(events.last().map(String::as_str), Some("dragend"));
}