nativeview-engine 0.1.0-rc.1

Framework-agnostic scene engine traits, hot-reload cdylib loader, and Frame-0 snapshot codecs.
Documentation
//! [INV:direct-batch-zero-alloc-and-parity]-style guard for the input extension points: warm
//! interceptor / default-action dispatch (empty, mask-skipped, matching, shared) allocates 0
//! bytes.

nativeview_test_support::install_counting_allocator!();

use std::cell::RefCell;

use nativeview_abi::{EventBatchBuf, EventFlags, InteractionEvent, NodeId, SurfaceId, TimestampUs, ValidatedEvent};
use nativeview_engine::ext::input::{
    DefaultAction, DefaultActionChain, Disposition, InputInterceptor, InputKinds, InputPriority,
    InterceptorChain,
};
use nativeview_test_support::count_allocs;

struct Count;

impl InputInterceptor<u64> for Count {
    fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut u64) -> Disposition {
        *cx += u64::from(ev.key_code());
        Disposition::PassThrough
    }
}

impl DefaultAction<u64> for Count {
    fn run(&mut self, ev: &ValidatedEvent<'_>, cx: &mut u64) -> Disposition {
        *cx += u64::from(ev.key_code());
        Disposition::ConsumedPreventDefault
    }
}

#[test]
fn warm_input_chain_dispatch_allocates_nothing() {
    let mut buf = EventBatchBuf::new();
    buf.push_key_down(SurfaceId::ROOT, NodeId::ROOT, TimestampUs(1), "Escape", "Escape", EventFlags::EMPTY, 27);
    let ev = buf.as_validated().get(0).expect("event");
    let keys = nativeview_engine::ext::input::KEY_DOWN;
    let pointer = InputKinds::from_raw(1 << InteractionEvent::PointerDown as u32);

    let mut empty = InterceptorChain::<u64>::new();
    let mut interceptors = InterceptorChain::<u64>::new();
    interceptors.push(pointer, Box::new(Count));
    interceptors.push(keys, Box::new(Count));
    let shared = RefCell::new(DefaultActionChain::<u64>::new());
    shared.borrow_mut().push(keys, Box::new(Count));
    let mut sum = 0u64;

    let (_, allocs) = count_allocs(|| {
        for _ in 0..1_000 {
            assert_eq!(empty.intercept(&ev, &mut sum), Disposition::PassThrough);
            assert_eq!(interceptors.intercept(&ev, &mut sum), Disposition::PassThrough);
            assert!(DefaultActionChain::run_default_shared(&shared, &ev, &mut sum).prevents_default());
        }
    });
    assert_eq!(allocs, 0);
    assert_eq!(sum, 2 * 27 * 1_000);
}

#[test]
fn warm_priority_chain_mask_fast_exit_and_mixed_shared_allocate_nothing() {
    let mut key_buf = EventBatchBuf::new();
    key_buf.push_key_down(SurfaceId::ROOT, NodeId::ROOT, TimestampUs(1), "a", "KeyA", EventFlags::EMPTY, 65);
    let key_ev = key_buf.as_validated().get(0).expect("key");

    let mut press_buf = EventBatchBuf::new();
    press_buf.push_press(SurfaceId::ROOT, NodeId::ROOT, TimestampUs(2), 10.0, 20.0);
    let press_ev = press_buf.as_validated().get(0).expect("press");

    let keys = nativeview_engine::ext::input::KEY_DOWN;
    let pointer = InputKinds::from_raw(1 << InteractionEvent::PointerDown as u32);

    let mut priority_chain = InterceptorChain::<u64>::new();
    priority_chain.push_with_priority(keys, InputPriority::LOW, Box::new(Count));
    priority_chain.push_with_priority(keys, InputPriority::HIGH, Box::new(Count));
    priority_chain.push_with_priority(keys, InputPriority::NORMAL, Box::new(Count));

    let mut non_matching = InterceptorChain::<u64>::new();
    non_matching.push_with_priority(pointer, InputPriority::HIGH, Box::new(Count));

    struct NestedShared {
        chain: std::rc::Rc<RefCell<InterceptorChain<u64>>>,
    }
    impl InputInterceptor<u64> for NestedShared {
        fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut u64) -> Disposition {
            *cx += 1;
            InterceptorChain::intercept_shared(&self.chain, ev, cx)
        }
    }

    let shared_mixed = std::rc::Rc::new(RefCell::new(InterceptorChain::<u64>::new()));
    shared_mixed.borrow_mut().push_with_priority(keys, InputPriority::LOW, Box::new(Count));
    shared_mixed.borrow_mut().push_with_priority(
        keys,
        InputPriority::NORMAL,
        Box::new(NestedShared {
            chain: std::rc::Rc::clone(&shared_mixed),
        }),
    );
    shared_mixed.borrow_mut().push_with_priority(keys, InputPriority::HIGH, Box::new(Count));

    let mut sum = 0u64;
    let (_, allocs) = count_allocs(|| {
        for _ in 0..500 {
            assert_eq!(priority_chain.intercept(&key_ev, &mut sum), Disposition::PassThrough);
            assert_eq!(non_matching.intercept(&key_ev, &mut sum), Disposition::PassThrough);
            assert_eq!(priority_chain.intercept(&press_ev, &mut sum), Disposition::PassThrough);
            assert_eq!(
                InterceptorChain::intercept_shared(&shared_mixed, &key_ev, &mut sum),
                Disposition::PassThrough
            );
        }
    });
    assert_eq!(allocs, 0);
    // Per iteration: priority_chain (3 * 65) + shared_mixed (HIGH: 65, NORMAL: 1 + nested(HIGH: 65, LOW: 65), LOW: 65) = 7 * 65 + 1
    assert_eq!(sum, (7 * 65 + 1) * 500);
}