From 4695b65a1bc20c39f4fdddab341fe971683c270b Mon Sep 17 00:00:00 2001 From: Fritiof Hedman Date: Tue, 3 Nov 2026 19:04:25 +0100 Subject: [PATCH] Add unit tests for SaliencySmartCropper The golden fixtures only prove determinism; these assert the crop actually biases toward the edge-rich side on both axes, reaches the source edge for an edge-hugging subject, and always returns an in-bounds rect at the target aspect. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../SaliencySmartCropperTests.cs | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/FrameProcessor.Tests/SaliencySmartCropperTests.cs diff --git a/tests/FrameProcessor.Tests/SaliencySmartCropperTests.cs b/tests/FrameProcessor.Tests/SaliencySmartCropperTests.cs new file mode 100644 index 0000000..1f8e184 --- /dev/null +++ b/tests/FrameProcessor.Tests/SaliencySmartCropperTests.cs @@ -0,0 +1,112 @@ +using FrameProcessor.ImagePipeline.SmartCrop; +using Microsoft.Extensions.Logging.Abstractions; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace FrameProcessor.Tests; + +public class SaliencySmartCropperTests +{ + public enum Side { Left, Right, Top, Bottom } + + private static readonly SaliencySmartCropper Cropper = new(NullLogger.Instance); + + [Fact] + public void SameAspect_ReturnsFullBounds() + { + using var image = CreateHalfCheckerboard(1600, 1200, Side.Right); + var rect = Cropper.ComputeCropRect(image, 800, 600); + Assert.Equal(image.Bounds, rect); + } + + [Theory] + [InlineData(Side.Right)] + [InlineData(Side.Left)] + public void WideSource_BiasesTowardEdgeRichSide(Side side) + { + using var image = CreateHalfCheckerboard(1600, 900, side); + var rect = Cropper.ComputeCropRect(image, 1600, 1200); + + var centeredX = (image.Width - rect.Width) / 2; + if (side == Side.Right) + { + Assert.True(rect.X > centeredX, $"Expected crop right of center, got X={rect.X}"); + } + else + { + Assert.True(rect.X < centeredX, $"Expected crop left of center, got X={rect.X}"); + } + AssertValidCrop(image, rect, 1600, 1200); + } + + [Theory] + [InlineData(Side.Bottom)] + [InlineData(Side.Top)] + public void TallSource_BiasesTowardEdgeRichSide(Side side) + { + using var image = CreateHalfCheckerboard(900, 1600, side); + var rect = Cropper.ComputeCropRect(image, 1600, 1200); + + var centeredY = (image.Height - rect.Height) / 2; + if (side == Side.Bottom) + { + Assert.True(rect.Y > centeredY, $"Expected crop below center, got Y={rect.Y}"); + } + else + { + Assert.True(rect.Y < centeredY, $"Expected crop above center, got Y={rect.Y}"); + } + AssertValidCrop(image, rect, 1600, 1200); + } + + [Fact] + public void EdgeHuggingSubject_CropReachesSourceEdge() + { + using var image = CreateHalfCheckerboard(1600, 900, Side.Right); + var rect = Cropper.ComputeCropRect(image, 1600, 1200); + Assert.Equal(image.Width, rect.Right); + } + + [Fact] + public void PortraitTargetOnLandscapeSource_ReturnsValidRect() + { + using var image = CreateHalfCheckerboard(1600, 1200, Side.Right); + var rect = Cropper.ComputeCropRect(image, 1200, 1600); + AssertValidCrop(image, rect, 1200, 1600); + } + + private static void AssertValidCrop(Image image, Rectangle rect, int targetWidth, int targetHeight) + { + Assert.True(image.Bounds.Contains(rect), $"Crop {rect} outside source bounds {image.Bounds}"); + var expectedAspect = (double)targetWidth / targetHeight; + var actualAspect = (double)rect.Width / rect.Height; + Assert.InRange(actualAspect, expectedAspect * 0.99, expectedAspect * 1.01); + } + + private static Image CreateHalfCheckerboard(int width, int height, Side side) + { + var image = new Image(width, height); + var gray = new Rgba32(0x80, 0x80, 0x80, 0xFF); + var black = new Rgba32(0x00, 0x00, 0x00, 0xFF); + var white = new Rgba32(0xFF, 0xFF, 0xFF, 0xFF); + image.ProcessPixelRows(accessor => + { + for (var y = 0; y < accessor.Height; y++) + { + var row = accessor.GetRowSpan(y); + for (var x = 0; x < row.Length; x++) + { + var edgy = side switch + { + Side.Left => x < width / 2, + Side.Right => x >= width / 2, + Side.Top => y < height / 2, + _ => y >= height / 2, + }; + row[x] = !edgy ? gray : (((x / 8) + (y / 8)) & 1) == 0 ? black : white; + } + } + }); + return image; + } +}