use super::visible_box;
use pretty_assertions::assert_eq;
use rho_browser::tab::Viewport;
use rho_protocol::BrowserRect;
const VIEWPORT: Viewport = Viewport {
width: 100,
height: 50,
};
fn rect(left: f64, top: f64, right: f64, bottom: f64) -> [f64; 8] {
[left, top, right, top, right, bottom, left, bottom]
}
type Case = (
&'static str,
Vec<[f64; 8]>,
Option<((f64, f64), BrowserRect)>,
);
#[test]
fn a_click_lands_on_and_outlines_the_part_of_the_box_in_the_viewport() {
let cases: [Case; 7] = [
(
"inside",
vec![rect(10.0, 10.0, 30.0, 20.0)],
Some(((20.0, 15.0), BrowserRect::new(10, 10, 20, 10))),
),
(
"at fractional pixels",
vec![rect(10.5, 20.25, 30.5, 25.75)],
Some(((20.5, 23.0), BrowserRect::new(10, 20, 21, 6))),
),
(
"partly off the right and bottom",
vec![rect(80.0, 40.0, 120.0, 60.0)],
Some(((90.0, 45.0), BrowserRect::new(80, 40, 20, 10))),
),
(
"partly off the left and top",
vec![rect(-20.0, -10.0, 10.0, 10.0)],
Some(((5.0, 5.0), BrowserRect::new(0, 0, 10, 10))),
),
("wholly below", vec![rect(10.0, 60.0, 30.0, 70.0)], None),
(
"under a pixel wide",
vec![rect(10.0, 10.0, 10.5, 20.0)],
None,
),
(
"the first quad that shows, of a wrapped inline",
vec![rect(10.0, -20.0, 30.0, -10.0), rect(0.0, 0.0, 20.0, 10.0)],
Some(((10.0, 5.0), BrowserRect::new(0, 0, 20, 10))),
),
];
for (case, quads, expected) in cases {
let drawn = visible_box(&quads, VIEWPORT).map(|drawn| (drawn.center(), drawn.rect()));
assert_eq!(drawn, expected, "{case}");
}
}