Catalog/MaddoShared.Tests/ImageCreatorSharpTests.cs

63 lines
1.7 KiB
C#
Raw Permalink Normal View History

2026-02-04 19:01:00 +01:00
using System.Reflection;
2026-05-28 20:27:05 +02:00
using MaddoShared;
2026-02-04 19:01:00 +01:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
2026-05-28 20:27:05 +02:00
using SixLabors.ImageSharp;
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
namespace MaddoShared.Tests;
[TestClass]
public class ImageCreatorSharpTests
2026-02-04 19:01:00 +01:00
{
2026-05-28 20:27:05 +02:00
[TestMethod]
public void CalculateThumbnailSize_Larghezza_UsesWidthScaling()
2026-02-04 19:01:00 +01:00
{
2026-05-28 20:27:05 +02:00
var size = CalculateThumbnailSize(400, 200, 200, "Larghezza");
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
size.Width.ShouldBe(200);
size.Height.ShouldBe(100);
}
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
[TestMethod]
public void CalculateThumbnailSize_Altezza_UsesHeightScaling()
{
var size = CalculateThumbnailSize(200, 400, 200, "Altezza");
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
size.Width.ShouldBe(100);
size.Height.ShouldBe(200);
}
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
[TestMethod]
public void FindBestFontSize_ConstrainsTextToBounds()
{
const string text = "A very long text that will not fit at the requested size";
var method = typeof(ImageCreatorImageSharp).GetMethod(
"FindBestFontSize",
BindingFlags.NonPublic | BindingFlags.Static);
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
method.ShouldNotBeNull();
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
var size = (float)method.Invoke(null, new object[]
2026-02-04 19:01:00 +01:00
{
2026-05-28 20:27:05 +02:00
text,
"Arial",
40,
50f,
20f,
6
})!;
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
size.ShouldBeInRange(6f, 40f);
(size * text.Length * 0.6f <= 50f || size <= 6f).ShouldBeTrue();
}
2026-05-28 20:27:05 +02:00
private static Size CalculateThumbnailSize(int width, int height, int maxPixel, string sizeMode)
{
var method = typeof(ImageCreatorImageSharp).GetMethod(
"CalculateThumbnailSize",
BindingFlags.NonPublic | BindingFlags.Static);
2026-02-04 19:01:00 +01:00
2026-05-28 20:27:05 +02:00
method.ShouldNotBeNull();
return (Size)method.Invoke(null, new object[] { width, height, maxPixel, sizeMode })!;
2026-02-04 19:01:00 +01:00
}
}