using Titano.Core.Imaging; using Titano.Core.Windows.Codecs; using Titano.Core.Windows.Wic; using Titano.Testing; namespace Titano.Core.Windows.Tests.Codecs; public sealed class CodecInventoryTests { private static readonly WicDecoderInfo Jpeg = Decoder("JPEG Decoder", Guid.NewGuid(), WicGuids.VendorMicrosoftBuiltIn, ".jpeg,.jpe,.jpg,.jfif,.exif"); private static readonly WicDecoderInfo InBoxDng = Decoder("DNG Decoder", Guid.NewGuid(), WicGuids.VendorMicrosoftBuiltIn, ".dng"); private static readonly WicDecoderInfo Raw = Decoder("Microsoft Raw Image Decoder", WicGuids.RawDecoder, WicGuids.VendorMicrosoft, ".arw,.cr2,.cr3,.dng,.nef,.raf"); private static readonly WicDecoderInfo Heif = Decoder("HEIF Decoder", WicGuids.HeifDecoder, WicGuids.VendorMicrosoft, ".heic,.heif,.hif"); private static readonly WicDecoderInfo Webp = Decoder("WebP Decoder", WicGuids.WebpDecoder, WicGuids.VendorMicrosoft, ".webp"); [Test] [WindowsOnly] public void DetectFindsTheBuiltInDecoders() { var inventory = CodecInventory.Detect(); Assert.NotEmpty(inventory.Decoders); foreach (var format in new[] { ImageFileFormat.Jpeg, ImageFileFormat.Png, ImageFileFormat.Tiff }) { var support = inventory.For(format); Assert.True(support.IsNative, $"{format} should decode natively through WIC"); var decoder = Assert.NotNull(support.Decoder); Assert.False(string.IsNullOrWhiteSpace(decoder.FriendlyName), $"{format} decoder has no name"); Assert.NotEqual(Guid.Empty, decoder.ContainerFormat); Assert.Empty(support.MissingExtensions); } Assert.Equal(ImageFormatInfo.Known.Count, inventory.Formats.Count); } [Test] public void RawNeedsTheRawImageExtension() { var inventory = Inventory([Jpeg, InBoxDng, Classified(Raw, rawExtensionInstalled: false)]); var dng = inventory.For(ImageFileFormat.Dng); Assert.False(dng.IsNative); Assert.Equal("DNG Decoder", dng.Decoder?.FriendlyName); Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.RawImage)], dng.MissingExtensions); Assert.False(inventory.IsNative(ImageFileFormat.Cr3)); Assert.Contains(StoreExtension.Get(StoreExtensionKind.RawImage), inventory.MissingExtensions); } [Test] public void RawIsNativeThroughTheExtension() { var inventory = Inventory([Jpeg, InBoxDng, Classified(Raw, rawExtensionInstalled: true)]); Assert.True(inventory.IsNative(ImageFileFormat.Dng)); Assert.Equal(StoreExtensionKind.RawImage, inventory.For(ImageFileFormat.Dng).Decoder?.Extension); Assert.True(inventory.IsNative(ImageFileFormat.Cr3)); // Installed but without this extension registered: nothing to install, still not native. var gpr = inventory.For(ImageFileFormat.Gpr); Assert.False(gpr.IsNative); Assert.Empty(gpr.MissingExtensions); } [Test] public void HeifNeedsAnHevcDecoder() { var withoutHevc = Inventory([Jpeg, Classified(Heif)]).For(ImageFileFormat.Heif); Assert.False(withoutHevc.IsNative); Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.HevcVideo)], withoutHevc.MissingExtensions); Assert.True(Inventory([Jpeg, Classified(Heif)], hevc: true).IsNative(ImageFileFormat.Heif)); var nothing = Inventory([Jpeg]).For(ImageFileFormat.Heif); Assert.SequenceEqual( [StoreExtension.Get(StoreExtensionKind.HeifImage), StoreExtension.Get(StoreExtensionKind.HevcVideo)], nothing.MissingExtensions); } [Test] public void AvifUsesTheHeifDecoderWithAnAv1Decoder() { var withoutAv1 = Inventory([Classified(Heif)], hevc: true).For(ImageFileFormat.Avif); Assert.False(withoutAv1.IsNative); Assert.Equal("HEIF Decoder", withoutAv1.Decoder?.FriendlyName); Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.Av1Video)], withoutAv1.MissingExtensions); Assert.True(Inventory([Classified(Heif)], av1: true).IsNative(ImageFileFormat.Avif)); } [Test] public void WebpNeedsTheWebpExtension() { Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.WebpImage)], Inventory([Jpeg]).For(ImageFileFormat.WebP).MissingExtensions); Assert.True(Inventory([Jpeg, Classified(Webp)]).IsNative(ImageFileFormat.WebP)); } [Test] public void BuiltInFormatsNeverAskForExtensions() { var inventory = Inventory([Jpeg]); Assert.True(inventory.IsNative(ImageFileFormat.Jpeg)); Assert.False(inventory.IsNative(ImageFileFormat.Png)); Assert.Empty(inventory.For(ImageFileFormat.Png).MissingExtensions); Assert.False(inventory.IsNative(ImageFileFormat.Unknown)); } [Test] public void ClassificationUsesClsidPackageAndVendor() { Assert.Null(CodecInventory.Classify(Raw, rawExtensionInstalled: false)); Assert.Equal(StoreExtensionKind.RawImage, CodecInventory.Classify(Raw, rawExtensionInstalled: true)); Assert.Equal(StoreExtensionKind.HeifImage, CodecInventory.Classify(Heif, false)); Assert.Equal(StoreExtensionKind.WebpImage, CodecInventory.Classify(Webp, false)); var avif = Decoder("AVIF Decoder", Guid.NewGuid(), WicGuids.VendorMicrosoft, ".avif"); Assert.Equal(StoreExtensionKind.Av1Video, CodecInventory.Classify(avif, false)); var thirdParty = Decoder("Other AVIF", Guid.NewGuid(), Guid.NewGuid(), ".avif"); Assert.Null(CodecInventory.Classify(thirdParty, false)); Assert.Null(CodecInventory.Classify(Jpeg, false)); } [Test] public void StoreLinksUseTheProductIds() { Assert.Equal(5, StoreExtension.All.Count); var raw = StoreExtension.Get(StoreExtensionKind.RawImage); Assert.Equal("ms-windows-store://pdp/?ProductId=9NCTDW2W1BH8", raw.StoreUri.AbsoluteUri); Assert.Equal("https://apps.microsoft.com/detail/9nctdw2w1bh8", raw.WebUri.AbsoluteUri); Assert.Equal("9PMMSR1CGPWG", StoreExtension.Get(StoreExtensionKind.HeifImage).ProductId); Assert.Equal("9NMZLZ57R3T7", StoreExtension.Get(StoreExtensionKind.HevcVideo).ProductId); Assert.Equal("9MVZQVXJBQ9V", StoreExtension.Get(StoreExtensionKind.Av1Video).ProductId); Assert.Equal("9PG2DK419DRG", StoreExtension.Get(StoreExtensionKind.WebpImage).ProductId); } [Test] public void ExtensionListsAreNormalised() { Assert.SequenceEqual(["jpg", "jpeg", "jfif"], WicDecoderInfo.ParseExtensions(".JPG, .jpeg;.jfif,,.jpg")); Assert.True(Jpeg.Handles(".JPG")); Assert.True(Jpeg.Handles("jfif")); Assert.False(Jpeg.Handles("png")); } private static CodecInventory Inventory(WicDecoderInfo[] decoders, bool hevc = false, bool av1 = false) => new(decoders, hevc, av1); private static WicDecoderInfo Classified(WicDecoderInfo decoder, bool rawExtensionInstalled = false) => decoder with { Extension = CodecInventory.Classify(decoder, rawExtensionInstalled) }; private static WicDecoderInfo Decoder(string name, Guid clsid, Guid vendor, string extensions) => new(name, clsid, Guid.NewGuid(), vendor, "1.0", WicDecoderInfo.ParseExtensions(extensions), null); }