nativeview-engine 0.1.0-rc.1

Framework-agnostic scene engine traits, hot-reload cdylib loader, and Frame-0 snapshot codecs.
Documentation
//! `CountingAlloc` zero-allocation steady-state guard plus ancestor hover/active chain,
//! W3C enter/exit ordering, pointer capture survival across `pointer_leave`, `pointer_cancel`,
//! and `:focus-visible` modality tests for [`InteractionTracker`].


use nativeview_abi::{NodeId, TimestampUs};
use nativeview_engine::tracker::{PointerKey, PointerType, MAX_ACTIVE_POINTERS};
use nativeview_engine::InteractionTracker;

const M: PointerKey = PointerKey::new(PointerType::Mouse, 1);
const AT: [f32; 2] = [0.0, 0.0];
const TS: TimestampUs = TimestampUs(1);
use nativeview_tables::interaction::{FocusModality, InteractionState};

use nativeview_test_support::count_allocs;
nativeview_test_support::install_counting_allocator!();

#[test]
fn interaction_tracker_ancestor_chains_capture_and_zero_alloc_steady_state() {
    // Tree: root (1) -> card (2) -> btn_a (3) -> label_a (4)
    //                            -> btn_b (5)
    let root = NodeId::new(1, 0);
    let card = NodeId::new(2, 0);
    let btn_a = NodeId::new(3, 0);
    let label_a = NodeId::new(4, 0);
    let btn_b = NodeId::new(5, 0);

    let parent_of = |id: NodeId| -> Option<NodeId> {
        if id == label_a {
            Some(btn_a)
        } else if id == btn_a || id == btn_b {
            Some(card)
        } else if id == card {
            Some(root)
        } else {
            None
        }
    };

    let mut t = InteractionTracker::new();

    // 1. Hovering label_a activates :hover on the entire leaf-to-root ancestor chain.
    let dirty = t.pointer_move_with(M, Some(label_a), parent_of);
    assert_eq!(dirty, &[root, card, btn_a, label_a]);
    assert_eq!(t.hovered_chain(), &[label_a, btn_a, card, root]);
    assert_eq!(t.entered(), &[root, card, btn_a, label_a]); // top-down
    assert!(t.exited().is_empty());
    for id in [root, card, btn_a, label_a] {
        assert_eq!(t.state(id), InteractionState::HOVER);
    }

    // 2. Moving from label_a -> btn_b exits [label_a, btn_a] bottom-up and enters [btn_b] top-down;
    //    shared ancestors card & root stay continuously hovered.
    let dirty = t.pointer_move_with(M, Some(btn_b), parent_of);
    assert_eq!(dirty, &[btn_a, label_a, btn_b]);
    assert_eq!(t.exited(), &[label_a, btn_a]); // bottom-up
    assert_eq!(t.entered(), &[btn_b]); // top-down
    assert_eq!(t.state(card), InteractionState::HOVER);
    assert_eq!(t.state(btn_a), InteractionState::NONE);
    assert_eq!(t.state(btn_b), InteractionState::HOVER);

    // 3. Down on label_a, drag to btn_a (still inside btn_a): label_a loses :active, btn_a keeps :active,
    //    releasing on btn_a clicks btn_a (deepest common non-disabled ancestor).
    let _ = t.pointer_down_with(M, AT, TS, Some(label_a), parent_of);
    assert_eq!(t.active_chain(), &[label_a, btn_a, card, root]);
    let _ = t.pointer_move_with(M, Some(btn_a), parent_of);
    assert_eq!(t.active_chain(), &[btn_a, card, root]);
    let end = t.pointer_up_with(M, Some(btn_a), parent_of);
    assert_eq!(end.clicked, Some(btn_a));
    assert!(t.active_chain().is_empty());

    // 4. Pointer capture survives drag-off AND pointer_leave; pointer_cancel clears capture & active.
    let _ = t.pointer_down_with(M, AT, TS, Some(btn_a), parent_of);
    t.pointers_mut().capture_now(M, btn_a, true).unwrap();
    assert_eq!(t.captured(), Some((M, btn_a)));
    let _ = t.pointer_move_with(M, None, parent_of);
    assert_eq!(t.captured(), Some((M, btn_a)));
    assert_eq!(t.active_chain(), &[btn_a, card, root]);
    let _ = t.pointer_leave_slice();
    assert_eq!(t.captured(), Some((M, btn_a)), "capture must survive pointer_leave");
    assert_eq!(t.active_chain(), &[btn_a, card, root]);
    let _ = t.pointer_cancel_all();
    assert_eq!(t.captured(), None);
    assert!(t.active_chain().is_empty());
    assert!(t.hovered_chain().is_empty());

    // 5. Focus modality (:focus vs :focus-visible).
    let _ = t.set_focus(Some(btn_a), FocusModality::Pointer);
    assert_eq!(t.state(btn_a), InteractionState::FOCUS);
    let _ = t.set_focus(Some(btn_a), FocusModality::Keyboard);
    assert_eq!(
        t.state(btn_a),
        InteractionState::FOCUS | InteractionState::FOCUS_VISIBLE
    );
    let _ = t.set_focus(None, FocusModality::Pointer);

    // 6. Positive probe proving `CountingAlloc` actually intercepts heap allocations, followed by
    //    the steady-state zero-allocation guard across 200 full interaction cycles.
    let (probe_box, probe_allocs) = count_allocs(|| std::hint::black_box(Box::new(0xDEAD_BEEFu64)));
    assert!(
        probe_allocs >= 1,
        "CountingAlloc positive probe must observe >= 1 allocation, saw {probe_allocs}"
    );
    drop(probe_box);

    let (_, allocs) = count_allocs(|| {
        for _ in 0..200 {
            let _ = t.pointer_move_with(M, Some(label_a), parent_of);
            let _ = t.pointer_down_with(M, AT, TS, Some(label_a), parent_of);
            t.pointers_mut().capture_now(M, btn_a, true).unwrap();
            let _ = t.pointer_move_with(M, Some(btn_b), parent_of);
            let _ = t.pointer_leave_slice();
            let _ = t.pointer_cancel_all();
            let _ = t.pointer_down_with(M, AT, TS, Some(btn_a), parent_of);
            let _ = t.pointer_up_with(M, Some(btn_a), parent_of);
            let _ = t.set_focus(Some(btn_a), FocusModality::Keyboard);
            let _ = t.set_focus(Some(btn_b), FocusModality::Pointer);
            let _ = t.set_focus(None, FocusModality::Pointer);
        }
    });
    assert_eq!(
        allocs, 0,
        "steady-state InteractionTracker transitions must perform 0 heap allocations, saw {allocs}"
    );
}

#[test]
fn interaction_tracker_breaks_cyclic_parent_chains_without_duplicates() {
    let a = NodeId::new(10, 0);
    let b = NodeId::new(11, 0);
    let cyclic_parent = |id: NodeId| -> Option<NodeId> {
        if id == a {
            Some(b)
        } else if id == b {
            Some(a)
        } else {
            None
        }
    };

    let mut t = InteractionTracker::new();
    let dirty = t.pointer_move_with(M, Some(a), cyclic_parent);
    assert_eq!(dirty, &[a, b]);
    assert_eq!(t.entered(), &[b, a]);
    assert_eq!(t.hovered_chain(), &[a, b]);
    let _ = t.pointer_down_with(M, AT, TS, Some(a), cyclic_parent);
    assert_eq!(t.active_chain(), &[a, b]);
}

#[test]
fn interaction_tracker_prune_disconnected_performs_zero_heap_allocations() {
    let root = NodeId::new(1, 0);
    let card = NodeId::new(2, 0);
    let btn = NodeId::new(3, 0);
    let label = NodeId::new(4, 0);

    let parent_of = |id: NodeId| -> Option<NodeId> {
        if id == label {
            Some(btn)
        } else if id == btn {
            Some(card)
        } else if id == card {
            Some(root)
        } else {
            None
        }
    };

    let mut t = InteractionTracker::new();
    // Warm all internal chains & scratch buffers across two populate/prune cycles.
    for _ in 0..2 {
        let _ = t.pointer_move_with(M, Some(label), parent_of);
        let _ = t.pointer_down_with(M, AT, TS, Some(label), parent_of);
        let _ = t.set_drag_over_with(Some(label), parent_of);
        t.pointers_mut().capture_now(M, btn, true).unwrap();
        assert!(t.prune_disconnected(|id| id == root || id == card));
        assert_eq!(t.hovered_chain(), &[card, root]);
        assert_eq!(t.active_chain(), &[card, root]);
        assert_eq!(t.drag_over_chain(), &[card, root]);
        assert_eq!(t.captured(), None);
        assert!(!t.prune_disconnected(|id| id == root || id == card));
        let _ = t.pointer_up_with(M, Some(label), parent_of);
    }

    let (probe_box, probe_allocs) = count_allocs(|| std::hint::black_box(Box::new(0xBEEFu64)));
    assert!(probe_allocs >= 1, "CountingAlloc positive probe must observe >= 1 allocation");
    drop(probe_box);

    let (pruned_runs, allocs) = count_allocs(|| {
        let mut pruned = 0usize;
        for _ in 0..200 {
            let _ = t.pointer_move_with(M, Some(label), parent_of);
            let _ = t.pointer_down_with(M, AT, TS, Some(label), parent_of);
            let _ = t.set_drag_over_with(Some(label), parent_of);
            t.pointers_mut().capture_now(M, btn, true).unwrap();
            if t.prune_disconnected(|id| id == root || id == card) {
                pruned += 1;
            }
            assert!(!t.prune_disconnected(|id| id == root || id == card));
            let _ = t.pointer_up_with(M, Some(label), parent_of);
        }
        pruned
    });
    assert_eq!(pruned_runs, 200);
    assert_eq!(
        allocs, 0,
        "InteractionTracker::prune_disconnected must perform 0 heap allocations, saw {allocs}"
    );
}

#[test]
fn ten_pointer_down_move_capture_up_cancel_and_overflow_allocate_nothing() {
    let root = NodeId::new(1, 0);
    let leaves: Vec<NodeId> = (0..MAX_ACTIVE_POINTERS as u32).map(|i| NodeId::new(10 + i, 0)).collect();
    let parent_of = |id: NodeId| (id != root).then_some(root);
    let touch = |i: usize| PointerKey::new(PointerType::Touch, i as u32);
    let mut t = InteractionTracker::new();
    let cycle = |t: &mut InteractionTracker| {
        for (i, &n) in leaves.iter().enumerate() {
            assert!(t.pointer_down_with(touch(i), AT, TS, Some(n), parent_of).is_ok());
            t.pointers_mut().capture_now(touch(i), n, false).unwrap();
        }
        assert!(t.pointer_down_with(touch(99), AT, TS, Some(root), parent_of).is_err());
        for (i, &n) in leaves.iter().enumerate() {
            let _ = t.pointer_move_with(touch(i), Some(n), parent_of);
            t.pointers_mut().set_capture(touch(i), root).unwrap();
            let _ = t.pointers_mut().process_pending_capture(touch(i));
        }
        for (i, &n) in leaves.iter().enumerate().take(leaves.len() / 2) {
            let _ = t.pointer_up_with(touch(i), Some(n), parent_of);
        }
        for i in leaves.len() / 2..leaves.len() {
            let _ = t.pointer_cancel(touch(i));
        }
        assert!(t.pointers().is_empty());
    };
    cycle(&mut t);
    let (_, allocs) = count_allocs(|| {
        for _ in 0..100 {
            cycle(&mut t);
        }
    });
    assert_eq!(allocs, 0, "10-pointer down/move/capture/up/cancel + overflow allocated {allocs} times");
}