use std::f32::consts::FRAC_PI_2;
use frus_core::{Affine, Color, LayerFilter, LayerTransform, Point, Primitive, Rect, Scene};
use frus_test::{render_scene, Snapshot};
fn transformed_layer(inner: Rect, color: Color, m: LayerTransform) -> Scene {
let mut content = Scene::new();
content.fill_rect(inner, color);
let primitives = content.split_off(0);
let mut scene = Scene::new();
scene.push_primitive(Primitive::Layer {
primitives,
opacity: 1.0,
clip: Rect::UNBOUNDED,
clip_shape: frus_core::ClipShape::Rect,
transform: Some(m),
filter: LayerFilter::NONE,
owner: 0,
});
scene
}
fn is_red(px: [u8; 4]) -> bool {
px[0] > 200 && px[1] < 60 && px[2] < 60
}
fn is_clear(px: [u8; 4]) -> bool {
px[0] < 60 && px[1] < 60 && px[2] < 60
}
fn render(scene: &Scene) -> Option<Snapshot> {
let frame = render_scene(scene, 64, 64, Color::BLACK);
if frame.is_none() {
eprintln!("no GPU adapter available: test skipped");
}
frame
}
#[test]
fn rotation_turns_a_horizontal_bar_vertical() {
let red = Color::rgb(1.0, 0.0, 0.0);
let bar = Rect::new(8.0, 28.0, 48.0, 8.0);
let m = LayerTransform::rotation(FRAC_PI_2, Point::new(32.0, 32.0));
let Some(frame) = render(&transformed_layer(bar, red, m)) else {
return;
};
assert!(is_red(frame.pixel(32, 12)), "top of the vertical bar → red");
assert!(
is_red(frame.pixel(32, 52)),
"bottom of the vertical bar → red"
);
assert!(is_red(frame.pixel(32, 32)), "centre → red");
assert!(
is_clear(frame.pixel(12, 32)),
"the old left edge → background, it turned"
);
assert!(
is_clear(frame.pixel(52, 32)),
"the old right edge → background, it turned"
);
}
#[test]
fn uniform_scale_enlarges_about_center() {
let red = Color::rgb(1.0, 0.0, 0.0);
let sq = Rect::new(24.0, 24.0, 16.0, 16.0);
let m = LayerTransform::new(Affine::scale(2.0, 2.0).about(Point::new(32.0, 32.0)));
let Some(frame) = render(&transformed_layer(sq, red, m)) else {
return;
};
assert!(
is_red(frame.pixel(20, 20)),
"inside the enlarged image, outside the original square → red"
);
assert!(is_red(frame.pixel(32, 32)), "centre → red");
assert!(
is_clear(frame.pixel(6, 6)),
"outside the image → background"
);
}
#[test]
fn scaling_down_keeps_every_thin_line() {
let red = Color::rgb(1.0, 0.0, 0.0);
let mut content = Scene::new();
for k in 0..7 {
content.fill_rect(Rect::new(0.0, 4.0 + 8.0 * k as f32, 64.0, 1.0), red);
}
let primitives = content.split_off(0);
let mut scene = Scene::new();
scene.push_primitive(Primitive::Layer {
primitives,
opacity: 1.0,
clip: Rect::UNBOUNDED,
clip_shape: frus_core::ClipShape::Rect,
transform: Some(LayerTransform::new(Affine::scale(6.0 / 7.0, 6.0 / 7.0))),
filter: LayerFilter::NONE,
owner: 0,
});
let Some(frame) = render(&scene) else {
return;
};
let lit: Vec<bool> = (0..64).map(|y| frame.pixel(20, y)[0] > 60).collect();
let bands = lit.windows(2).filter(|w| !w[0] && w[1]).count();
assert_eq!(bands, 7, "one band per line: {lit:?}");
}
#[test]
fn non_uniform_scale_widens_x_only() {
let red = Color::rgb(1.0, 0.0, 0.0);
let sq = Rect::new(24.0, 24.0, 16.0, 16.0); let m = LayerTransform::new(Affine::scale(3.0, 1.0).about(Point::new(32.0, 32.0)));
let Some(frame) = render(&transformed_layer(sq, red, m)) else {
return;
};
assert!(is_red(frame.pixel(12, 32)), "widened in x → red");
assert!(
is_clear(frame.pixel(32, 12)),
"y is not scaled → background above"
);
assert!(
is_clear(frame.pixel(4, 32)),
"beyond the widening → background"
);
}
#[test]
fn scale_then_rotate_composes() {
let red = Color::rgb(1.0, 0.0, 0.0);
let sq = Rect::new(24.0, 28.0, 16.0, 8.0);
let pivot = Point::new(32.0, 32.0);
let m = LayerTransform::new(
Affine::scale(2.0, 2.0)
.about(pivot)
.then(Affine::rotation(FRAC_PI_2).about(pivot)),
);
let Some(frame) = render(&transformed_layer(sq, red, m)) else {
return;
};
assert!(
is_red(frame.pixel(32, 20)),
"top of the composed image → red"
);
assert!(
is_red(frame.pixel(32, 44)),
"bottom of the composed image → red"
);
assert!(
is_clear(frame.pixel(12, 32)),
"outside the narrow image → background"
);
assert!(
is_clear(frame.pixel(52, 32)),
"outside the narrow image → background"
);
}