using System.Buffers.Binary;
using Febe.Desktop.Core;
using Febe.Tests.Desktop.Support;
using SkiaSharp;
namespace Febe.Tests.Desktop;
///
/// The icon of the Windows application (spec ยง12, appendix C): the brand mark drawn with SkiaSharp and written by the
/// hand-made ICO writer, at 16, 24, 32, 48, 64, 128 and 256 pixels. The committed src/Febe.Desktop/Febe.ico must be
/// what the code draws: FEBE_RIGENERA_ICONA=1 dotnet test --filter IconTests writes it again.
///
[TestClass]
public sealed class IconTests
{
[TestMethod]
public void TheIconHasEverySizeDibBelow256AndPngAt256()
{
var ico = BrandMark.RenderIcon();
var entries = IcoWriter.ReadDirectory(ico);
CollectionAssert.AreEqual(new[] { 16, 24, 32, 48, 64, 128, 256 }, entries.Select(e => e.Size).ToArray());
Assert.IsTrue(entries.All(e => e.BitsPerPixel == 32));
Assert.IsTrue(entries.Where(e => e.Size < 256).All(e => !e.IsPng), "DIB below 256");
Assert.IsTrue(entries.Single(e => e.Size == 256).IsPng, "PNG at 256");
// Entries follow the directory and do not overlap.
var end = 6 + (16 * entries.Count);
foreach (var entry in entries.OrderBy(e => e.Offset))
{
Assert.AreEqual(end, entry.Offset);
end += entry.Length;
}
Assert.AreEqual(ico.Length, end);
// A DIB has the doubled height of the XOR + AND images and the right length.
foreach (var entry in entries.Where(e => !e.IsPng))
{
var header = ico.AsSpan(entry.Offset, 40);
Assert.AreEqual(40u, BinaryPrimitives.ReadUInt32LittleEndian(header));
Assert.AreEqual(entry.Size, BinaryPrimitives.ReadInt32LittleEndian(header[4..]));
Assert.AreEqual(entry.Size * 2, BinaryPrimitives.ReadInt32LittleEndian(header[8..]));
var maskStride = ((entry.Size + 31) / 32) * 4;
Assert.AreEqual(40 + (entry.Size * entry.Size * 4) + (maskStride * entry.Size), entry.Length);
}
}
[TestMethod]
public void EveryImageShowsTheMarkTransparentCornersAccentSquareWhiteMoon()
{
var ico = BrandMark.RenderIcon();
foreach (var entry in IcoWriter.ReadDirectory(ico))
{
using var image = IcoWriter.Decode(ico, entry);
var s = entry.Size;
Assert.AreEqual(0, image.GetPixel(0, 0).Alpha, $"{s}: transparent corner");
Assert.AreEqual(0, image.GetPixel(s - 1, s - 1).Alpha, $"{s}: transparent corner");
// Inside the square, away from ring and moon (x=4/32, y=28/32): the accent colour #0F5C8C.
var accent = image.GetPixel(Scale(4, s), Scale(28, s));
if (s >= 24)
{
AssertClose(new SKColor(0x0F, 0x5C, 0x8C, 0xFF), accent, 6, $"{s}: accent");
}
// The centre of the moon (23.2, 8.8) is white.
var moon = image.GetPixel(Scale(23.2, s), Scale(8.8, s));
if (s >= 32)
{
AssertClose(SKColors.White, moon, 10, $"{s}: moon");
}
// The centre of the ring (14.5, 17.5) is the accent, not white: it is a ring, not a disc.
var hole = image.GetPixel(Scale(14.5, s), Scale(17.5, s));
if (s >= 32)
{
AssertClose(new SKColor(0x0F, 0x5C, 0x8C, 0xFF), hole, 6, $"{s}: ring hole");
}
}
}
[TestMethod]
public void TheWriterRefusesImagesThatAreNotSquareOrTooLarge()
{
using var wide = new SKBitmap(32, 16);
using var huge = new SKBitmap(512, 512);
Assert.ThrowsExactly(() => IcoWriter.Write([wide]));
Assert.ThrowsExactly(() => IcoWriter.Write([huge]));
Assert.ThrowsExactly(() => IcoWriter.Write([]));
Assert.ThrowsExactly(() => IcoWriter.ReadDirectory([1, 2, 3, 4, 5, 6]));
}
[TestMethod]
public void TheCommittedIconIsWhatTheCodeDraws()
{
var path = Repository.PathOf("src", "Febe.Desktop", "Febe.ico");
var fresh = BrandMark.RenderIcon();
if (Environment.GetEnvironmentVariable("FEBE_RIGENERA_ICONA") == "1")
{
File.WriteAllBytes(path, fresh);
}
Assert.IsTrue(File.Exists(path), "src/Febe.Desktop/Febe.ico manca: FEBE_RIGENERA_ICONA=1 lo genera.");
var committed = File.ReadAllBytes(path);
var a = IcoWriter.ReadDirectory(committed);
var b = IcoWriter.ReadDirectory(fresh);
CollectionAssert.AreEqual(b.Select(e => e.Size).ToArray(), a.Select(e => e.Size).ToArray());
// Pixel by pixel, with a small tolerance (antialiasing may differ by a unit between SkiaSharp builds).
for (var i = 0; i < a.Count; i++)
{
using var left = IcoWriter.Decode(committed, a[i]);
using var right = IcoWriter.Decode(fresh, b[i]);
for (var y = 0; y < left.Height; y++)
{
for (var x = 0; x < left.Width; x++)
{
AssertClose(right.GetPixel(x, y), left.GetPixel(x, y), 3, $"{a[i].Size} px ({x},{y})");
}
}
}
}
private static int Scale(double coordinate, int size) => Math.Clamp((int)Math.Floor(coordinate * size / 32), 0, size - 1);
private static void AssertClose(SKColor expected, SKColor actual, int tolerance, string what)
{
var ok = Math.Abs(expected.Red - actual.Red) <= tolerance
&& Math.Abs(expected.Green - actual.Green) <= tolerance
&& Math.Abs(expected.Blue - actual.Blue) <= tolerance
&& Math.Abs(expected.Alpha - actual.Alpha) <= tolerance;
Assert.IsTrue(ok, $"{what}: atteso {expected}, trovato {actual}");
}
}