A rendering claim about iris was verified by hand from another checkout, because the compositor script and the input replay lived in ai-app's submodule and not here. `scripts/run-headless.sh` starts a headless sway on its own socket, runs an example against it and screenshots the result. `rig-input`'s `replay-touch` drives a recorded gesture in through Wayland's virtual pointer, since a headless compositor has no input device to move. `iris::harness` gains the `.touch` parser, so a recording means the same thing replayed into a harness as into a window rather than being read twice by two parsers. The ai-app copy's `--phone` became `--mode`, since which phone is not iris's business; its `IRIS_SCALE` has nothing to hand a density to here, so it waits for one.
86 lines
2.5 KiB
Rust
86 lines
2.5 KiB
Rust
//! Which widget an input reaches.
|
|
|
|
use std::{cell::RefCell, rc::Rc};
|
|
|
|
use iris::harness::{Harness, TouchScript};
|
|
use iris::prelude::*;
|
|
|
|
#[test]
|
|
fn a_press_reaches_only_the_widget_under_the_cursor() {
|
|
let mut h = Harness::new((400, 200));
|
|
let clicks = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
let (on_left, on_right) = (clicks.clone(), clicks.clone());
|
|
let left = rect(Color::RED)
|
|
.width(100)
|
|
.on(CursorSense::click(), move |_, _| {
|
|
on_left.borrow_mut().push("left")
|
|
})
|
|
.add(&mut h.rsc);
|
|
let right = rect(Color::BLUE)
|
|
.on(CursorSense::click(), move |_, _| {
|
|
on_right.borrow_mut().push("right")
|
|
})
|
|
.add(&mut h.rsc);
|
|
h.set_root((left, right).span(Dir::RIGHT));
|
|
|
|
h.click((50, 100));
|
|
assert_eq!(*clicks.borrow(), ["left"]);
|
|
|
|
h.click((300, 100));
|
|
assert_eq!(*clicks.borrow(), ["left", "right"]);
|
|
}
|
|
|
|
#[test]
|
|
fn hover_ends_when_the_cursor_leaves_the_window() {
|
|
let mut h = Harness::new((400, 200));
|
|
let hovered = Rc::new(RefCell::new(0));
|
|
let ended = Rc::new(RefCell::new(0));
|
|
|
|
let (h_count, e_count) = (hovered.clone(), ended.clone());
|
|
let widget = rect(Color::RED)
|
|
.on(CursorSense::HoverStart, move |_, _| {
|
|
*h_count.borrow_mut() += 1
|
|
})
|
|
.on(CursorSense::HoverEnd, move |_, _| {
|
|
*e_count.borrow_mut() += 1
|
|
})
|
|
.add(&mut h.rsc);
|
|
h.set_root(widget);
|
|
|
|
h.move_to((200, 100));
|
|
assert_eq!((*hovered.borrow(), *ended.borrow()), (1, 0));
|
|
|
|
// A second sample inside the same widget is not a second hover.
|
|
h.move_to((210, 100));
|
|
assert_eq!((*hovered.borrow(), *ended.borrow()), (1, 0));
|
|
|
|
h.leave();
|
|
assert_eq!((*hovered.borrow(), *ended.borrow()), (1, 1));
|
|
}
|
|
|
|
#[test]
|
|
fn a_recorded_gesture_presses_where_it_says() {
|
|
let mut h = Harness::new((400, 200));
|
|
let clicks = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
let (on_left, on_right) = (clicks.clone(), clicks.clone());
|
|
let left = rect(Color::RED)
|
|
.width(100)
|
|
.on(CursorSense::click(), move |_, _| {
|
|
on_left.borrow_mut().push("left")
|
|
})
|
|
.add(&mut h.rsc);
|
|
let right = rect(Color::BLUE)
|
|
.on(CursorSense::click(), move |_, _| {
|
|
on_right.borrow_mut().push("right")
|
|
})
|
|
.add(&mut h.rsc);
|
|
h.set_root((left, right).span(Dir::RIGHT));
|
|
|
|
let script = TouchScript::parse("0 down 300 100\n80 up 300 100").unwrap();
|
|
h.replay(&script);
|
|
|
|
assert_eq!(*clicks.borrow(), ["right"]);
|
|
}
|