use auralis_signal::Signal;
use burin::core::ElementId;
use burin::style::Point;
use burin::testing::TestHarness;
use burin::widgets::display::{ColumnWidth, List, Table, TableColumn};
use burin::widgets::layout::SizedBox;
const ROW_H: f32 = 28.0;
fn cols() -> Vec<TableColumn<String>> {
vec![
TableColumn::new("A", ColumnWidth::Fixed(200.0)).render(|r: &String, _, _| r.clone()),
TableColumn::new("B", ColumnWidth::Fixed(100.0))
.render(|_: &String, ri, _| format!("{ri}")),
]
}
fn mount_table(n: usize) -> (TestHarness, ElementId, ElementId) {
let (h, mounted, target, _rows) = mount_table_sig(n);
(h, mounted, target)
}
fn mount_table_sig(n: usize) -> (TestHarness, ElementId, ElementId, Signal<Vec<String>>) {
let rows = Signal::new((0..n).map(|i| format!("Row {i}")).collect::<Vec<_>>());
let mut h = TestHarness::new(700.0, 400.0);
let mounted = h.mount(
SizedBox::new().width(700.0).height(400.0).child(
Table::new(rows.clone())
.columns(cols())
.row_height(ROW_H)
.virtual_threshold(16),
),
);
for _ in 0..5 {
h.run_frame();
}
let target = scroll_container(&h, mounted);
(h, mounted, target, rows)
}
fn scroll_container(h: &TestHarness, root: ElementId) -> ElementId {
let mut best: Option<(ElementId, f32)> = None;
let mut stack = vec![root];
while let Some(id) = stack.pop() {
if let Some(sc) = h.root().comp_scroll(id) {
let cb = sc.content_bounds.get().height;
if best.map_or(true, |(_, b)| cb > b) {
best = Some((id, cb));
}
}
if let Some(el) = h.find(id) {
for &c in &el.children {
stack.push(c);
}
}
}
best.expect("scroll container").0
}
fn active_rows(
h: &TestHarness,
root: ElementId,
scroll_id: ElementId,
) -> Vec<(ElementId, burin::style::Rect)> {
let is_inside_scroll = |id: ElementId| -> bool {
let mut cur = burin::core::dirty_registry::parent_of(id);
while let Some(p) = cur {
if p == scroll_id {
return true;
}
cur = burin::core::dirty_registry::parent_of(p);
}
false
};
let mut out = Vec::new();
let mut stack = vec![root];
while let Some(id) = stack.pop() {
if let Some(el) = h.find(id) {
if el.accessible_role() == Some(accesskit::Role::Row)
&& !el.slot_inactive.get()
&& is_inside_scroll(id)
{
out.push((id, el.screen_bounds));
}
for &c in &el.children {
stack.push(c);
}
}
}
out.sort_by(|a, b| a.1.y.partial_cmp(&b.1.y).unwrap());
out
}
#[test]
fn table_rows_reposition_to_virtual_space_after_scroll() {
let (mut h, mounted, target) = mount_table(200);
h.scroll(target, 0.0, -(10.0 * ROW_H));
h.run_frame();
h.run_frame();
h.run_frame();
let sc = h.root().comp_scroll(target).unwrap();
assert_eq!(sc.scroll_offset.get().y, 10.0 * ROW_H, "offset applied");
let rows = active_rows(&h, mounted, target);
let body_rows: Vec<_> = rows.iter().filter(|(_, r)| r.height == ROW_H).collect();
assert!(body_rows.len() >= 10, "pool rows present");
let min_y = body_rows.iter().map(|(_, r)| r.y).fold(f32::MAX, f32::min);
let max_y = body_rows.iter().map(|(_, r)| r.y).fold(f32::MIN, f32::max);
assert!(
max_y - min_y >= 9.0 * ROW_H - 0.5,
"rows span the pool window: min_y={min_y} max_y={max_y}"
);
assert!(
min_y >= 10.0 * ROW_H - 1.0,
"row window must slide down in content space, got min_y={min_y}"
);
}
#[test]
fn table_rows_hittable_inside_viewport_after_scroll() {
let (mut h, _mounted, target) = mount_table(200);
h.scroll(target, 0.0, -(10.0 * ROW_H));
h.run_frame();
h.run_frame();
h.run_frame();
let vp = h.find(target).unwrap().screen_bounds;
for frac in [0.2f32, 0.5, 0.8] {
let p = Point::new(vp.x + 100.0, vp.y + vp.height * frac);
let hit = burin::core::dirty_registry::hit_test_with_fallback(h.root(), p)
.expect("hit something");
let mut cur = Some(hit);
let mut found_row = false;
while let Some(id) = cur {
if h.find(id).map(|e| e.accessible_role()) == Some(Some(accesskit::Role::Row)) {
found_row = true;
break;
}
cur = burin::core::dirty_registry::parent_of(id);
}
assert!(
found_row,
"probe at frac={frac} must hit a row subtree, hit {hit:?} (sb={:?}, role={:?}) instead",
h.find(hit).map(|e| e.screen_bounds),
h.find(hit).and_then(|e| e.accessible_role())
);
}
}
#[test]
fn table_scroll_back_to_top_restores_rows() {
let (mut h, mounted, target) = mount_table(200);
h.scroll(target, 0.0, -(50.0 * ROW_H));
h.run_frames(3);
h.scroll(target, 0.0, 50.0 * ROW_H);
h.run_frames(3);
let sc = h.root().comp_scroll(target).unwrap();
assert_eq!(sc.scroll_offset.get().y, 0.0);
let rows = active_rows(&h, mounted, target);
let body_rows: Vec<_> = rows.iter().filter(|(_, r)| r.height == ROW_H).collect();
let min_y = body_rows.iter().map(|(_, r)| r.y).fold(f32::MAX, f32::min);
assert!(
min_y < 2.0 * ROW_H,
"back at top the first row must be near content origin, got min_y={min_y}"
);
}
#[test]
fn list_items_reposition_after_scroll() {
let items = Signal::new((0..300).map(|i| format!("Item {i}")).collect::<Vec<_>>());
let mut h = TestHarness::new(400.0, 300.0);
const IH: f32 = 30.0;
let mounted = h.mount(
SizedBox::new()
.width(400.0)
.height(300.0)
.child(List::new(items).item_height(IH).virtual_threshold(12)),
);
for _ in 0..5 {
h.run_frame();
}
let target = scroll_container(&h, mounted);
h.scroll(target, 0.0, -(20.0 * IH));
h.run_frames(3);
let sc = h.root().comp_scroll(target).unwrap();
assert_eq!(sc.scroll_offset.get().y, 20.0 * IH);
let mut min_y = f32::MAX;
let mut stack = vec![mounted];
let mut n_active = 0;
while let Some(id) = stack.pop() {
if let Some(el) = h.find(id) {
if el.accessible_role() == Some(accesskit::Role::ListBoxOption)
&& !el.slot_inactive.get()
{
min_y = min_y.min(el.screen_bounds.y);
n_active += 1;
}
for &c in &el.children {
stack.push(c);
}
}
}
assert!(n_active > 0, "active items present");
assert!(
min_y >= 20.0 * IH - 1.0,
"item window must slide down, got min_y={min_y}"
);
}
#[test]
fn table_data_shrink_deactivates_stale_slots_and_clamps_offset() {
let (mut h, mounted, target, rows) = mount_table_sig(200);
h.scroll(target, 0.0, -(150.0 * ROW_H));
h.run_frames(3);
h.set_signal(
&rows,
(0..30).map(|i| format!("Row {i}")).collect::<Vec<_>>(),
);
h.run_frames(4);
let sc = h.root().comp_scroll(target).unwrap();
let vp_h = h.find(target).unwrap().screen_bounds.height;
let max_y = (30.0 * ROW_H - vp_h).max(0.0);
assert!(
sc.scroll_offset.get().y <= max_y + 0.5,
"offset must clamp to {} after shrink, got {}",
max_y,
sc.scroll_offset.get().y
);
let rows_now = active_rows(&h, mounted, target);
let body_rows: Vec<_> = rows_now.iter().filter(|(_, r)| r.height == ROW_H).collect();
assert!(!body_rows.is_empty(), "some rows remain active");
for (id, r) in &body_rows {
let vi = (r.y / ROW_H).round() as usize;
assert!(
vi <= 30,
"active row {id:?} at y={} maps to vi={vi} beyond the shrunken data",
r.y
);
}
}
#[test]
fn table_data_replace_same_len_updates_visible_texts() {
let (mut h, _mounted, target, rows) = mount_table_sig(100);
h.scroll(target, 0.0, -(40.0 * ROW_H));
h.run_frames(3);
h.set_signal(
&rows,
(0..100).map(|i| format!("NEW {i}")).collect::<Vec<_>>(),
);
h.run_frames(3);
let sc = h.root().comp_scroll(target).unwrap();
assert_eq!(sc.scroll_offset.get().y, 40.0 * ROW_H);
}
#[test]
fn list_data_shrink_clamps_and_deactivates() {
let items = Signal::new((0..300).map(|i| format!("Item {i}")).collect::<Vec<_>>());
let mut h = TestHarness::new(400.0, 300.0);
const IH: f32 = 30.0;
let mounted = h.mount(
SizedBox::new().width(400.0).height(300.0).child(
List::new(items.clone())
.item_height(IH)
.virtual_threshold(12),
),
);
for _ in 0..5 {
h.run_frame();
}
let target = scroll_container(&h, mounted);
h.scroll(target, 0.0, -(200.0 * IH));
h.run_frames(3);
h.set_signal(
&items,
(0..20).map(|i| format!("Item {i}")).collect::<Vec<_>>(),
);
h.run_frames(4);
let sc = h.root().comp_scroll(target).unwrap();
let vp_h = h.find(target).unwrap().screen_bounds.height;
let max_y = (20.0 * IH - vp_h).max(0.0);
assert!(
sc.scroll_offset.get().y <= max_y + 0.5,
"list offset must clamp to {max_y}, got {}",
sc.scroll_offset.get().y
);
let mut stack = vec![mounted];
while let Some(id) = stack.pop() {
if let Some(el) = h.find(id) {
if el.accessible_role() == Some(accesskit::Role::ListBoxOption)
&& !el.slot_inactive.get()
{
let vi = (el.screen_bounds.y / IH).round() as usize;
assert!(
vi <= 20,
"active item {id:?} beyond shrunken data (vi={vi})"
);
}
for &c in &el.children {
stack.push(c);
}
}
}
}
#[test]
fn table_pool_grows_to_cover_viewport() {
let rows = Signal::new((0..500).map(|i| format!("Row {i}")).collect::<Vec<_>>());
let mut h = TestHarness::new(700.0, 760.0);
let mounted = h.mount(
SizedBox::new().width(700.0).height(760.0).child(
Table::new(rows)
.columns(cols())
.row_height(ROW_H)
.virtual_threshold(8),
),
);
for _ in 0..6 {
h.run_frame();
}
let target = scroll_container(&h, mounted);
let vp_h = h.find(target).unwrap().screen_bounds.height;
let needed = (vp_h / ROW_H).ceil() as usize;
let rows_now = active_rows(&h, mounted, target);
let body_rows: Vec<_> = rows_now.iter().filter(|(_, r)| r.height == ROW_H).collect();
assert!(
body_rows.len() >= needed,
"pool must cover the viewport: needed {needed} rows for {vp_h}px, got {}",
body_rows.len()
);
let mut ys: Vec<f32> = body_rows.iter().map(|(_, r)| r.y).collect();
ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
for w in ys.windows(2) {
assert!(
(w[1] - w[0] - ROW_H).abs() < 0.5,
"rows must be contiguous, gap between {} and {}",
w[0],
w[1]
);
}
h.scroll(target, 0.0, -(50.0 * ROW_H));
h.run_frames(3);
let sc = h.root().comp_scroll(target).unwrap();
assert_eq!(sc.scroll_offset.get().y, 50.0 * ROW_H);
let rows_after = active_rows(&h, mounted, target);
let min_y = rows_after
.iter()
.filter(|(_, r)| r.height == ROW_H)
.map(|(_, r)| r.y)
.fold(f32::MAX, f32::min);
assert!(
min_y >= 50.0 * ROW_H - 1.0,
"window slid down after growth, min_y={min_y}"
);
}
#[test]
fn list_pool_covers_viewport() {
let items = Signal::new((0..300).map(|i| format!("Item {i}")).collect::<Vec<_>>());
let mut h = TestHarness::new(400.0, 620.0);
const IH: f32 = 30.0;
let mounted = h.mount(
SizedBox::new()
.width(400.0)
.height(620.0)
.child(List::new(items).item_height(IH).virtual_threshold(8)),
);
for _ in 0..5 {
h.run_frame();
}
let target = scroll_container(&h, mounted);
let vp_h = h.find(target).unwrap().screen_bounds.height;
let needed = (vp_h / IH).ceil() as usize;
let mut n_active = 0;
let mut stack = vec![mounted];
while let Some(id) = stack.pop() {
if let Some(el) = h.find(id) {
if el.accessible_role() == Some(accesskit::Role::ListBoxOption)
&& !el.slot_inactive.get()
{
n_active += 1;
}
for &c in &el.children {
stack.push(c);
}
}
}
assert!(
n_active >= needed,
"list pool must cover the viewport: needed {needed}, got {n_active}"
);
}
#[test]
fn table_checkbox_glyphs_follow_virtual_index_after_scroll() {
use std::collections::HashSet;
let rows = Signal::new((0..200).map(|i| format!("Row {i}")).collect::<Vec<_>>());
let multi = Signal::new(HashSet::from([0usize, 25]));
let mut h = TestHarness::new(700.0, 400.0);
let mounted = h.mount(
SizedBox::new().width(700.0).height(400.0).child(
Table::new(rows)
.columns(cols())
.row_height(ROW_H)
.virtual_threshold(16)
.multi_select(multi.clone()),
),
);
for _ in 0..6 {
h.run_frame();
}
let target = scroll_container(&h, mounted);
h.scroll(target, 0.0, -(20.0 * ROW_H));
h.run_frames(3);
let read_label = |h: &TestHarness, id: ElementId| -> String {
let el = h.find(id).unwrap();
let cell = el.lazy_label().expect("checkbox cell has lazy label");
let s = cell.take();
cell.set(s.clone());
s
};
let rows_now = active_rows(&h, mounted, target);
let body_rows: Vec<_> = rows_now.iter().filter(|(_, r)| r.height == ROW_H).collect();
let min_y = body_rows.iter().map(|(_, r)| r.y).fold(f32::MAX, f32::min);
let mut checked_seen = false;
let mut unchecked_seen = false;
for (id, r) in &body_rows {
let vi = 20 + ((r.y - min_y) / ROW_H).round() as usize;
let row_el = h.find(*id).unwrap();
let cb_cell = *row_el.children.first().expect("row has checkbox cell");
let glyph = read_label(&h, cb_cell);
let expected = if vi == 25 { "\u{2611}" } else { "\u{2610}" };
assert_eq!(
glyph, expected,
"row vi={vi}: checkbox glyph must follow the data row"
);
if vi == 25 {
checked_seen = true;
} else {
unchecked_seen = true;
}
}
assert!(checked_seen && unchecked_seen, "both states exercised");
}