// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using System; using System.Collections.Generic; using System.Linq; using ImageSharp.Formats; using Xunit; /// /// Tests the configuration class. /// public class ConfigurationTests { /// /// Test that the default configuration is not null. /// [Fact] public void TestDefultConfigurationIsNotNull() { Assert.True(Configuration.Default != null); } /// /// Test that the default configuration parallel options is not null. /// [Fact] public void TestDefultConfigurationParallelOptionsIsNotNull() { Assert.True(Configuration.Default.ParallelOptions != null); } /// /// Test that the default configuration parallel options max degrees of parallelism matches the /// environment processor count. /// [Fact] public void TestDefultConfigurationMaxDegreeOfParallelism() { Assert.True(Configuration.Default.ParallelOptions.MaxDegreeOfParallelism == Environment.ProcessorCount); } /// /// Test that the default configuration parallel options is not null. /// [Fact] public void TestDefultConfigurationImageFormatsIsNotNull() { Assert.True(Configuration.Default.ImageFormats != null); } /// /// Tests the method throws an exception /// when the format is null. /// [Fact] public void TestAddImageFormatThrowsWithNullFormat() { Assert.Throws(() => { Configuration.Default.AddImageFormat(null); }); } /// /// Tests the method throws an exception /// when the encoder is null. /// [Fact] public void TestAddImageFormatThrowsWithNullEncoder() { TestFormat format = new TestFormat { Encoder = null }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Tests the method throws an exception /// when the decoder is null. /// [Fact] public void TestAddImageFormatThrowsWithNullDecoder() { TestFormat format = new TestFormat { Decoder = null }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Tests the method throws an exception /// when the mime type is null or an empty string. /// [Fact] public void TestAddImageFormatThrowsWithNullOrEmptyMimeType() { TestFormat format = new TestFormat { MimeType = null }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); format = new TestFormat { MimeType = string.Empty }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Tests the method throws an exception /// when the extension is null or an empty string. /// [Fact] public void TestAddImageFormatThrowsWithNullOrEmptyExtension() { TestFormat format = new TestFormat { Extension = null }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); format = new TestFormat { Extension = string.Empty }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Tests the method throws an exception /// when the supported extensions list is null or empty. /// [Fact] public void TestAddImageFormatThrowsWenSupportedExtensionsIsNullOrEmpty() { TestFormat format = new TestFormat { SupportedExtensions = null }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); format = new TestFormat { SupportedExtensions = Enumerable.Empty() }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Tests the method throws an exception /// when the supported extensions list does not contain the default extension. /// [Fact] public void TestAddImageFormatThrowsWithoutDefaultExtension() { TestFormat format = new TestFormat { Extension = "test" }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Tests the method throws an exception /// when the supported extensions list contains an empty string. /// [Fact] public void TestAddImageFormatThrowsWithEmptySupportedExtension() { TestFormat format = new TestFormat { Extension = "test", SupportedExtensions = new[] { "test", string.Empty } }; Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); } /// /// Test that the method ignores adding duplicate image formats. /// [Fact] public void TestConfigurationIgnoresDuplicateImageFormats() { Configuration.Default.AddImageFormat(new PngFormat()); Configuration.Default.AddImageFormat(new PngFormat()); Assert.True(Configuration.Default.ImageFormats.Count(i => i.GetType() == typeof(PngFormat)) == 1); } /// /// Test that the default image constructors use default configuration. /// [Fact] public void TestImageUsesDefaultConfiguration() { Configuration.Default.AddImageFormat(new PngFormat()); Image image = new Image(1, 1); Assert.Equal(image.Configuration.ParallelOptions, Configuration.Default.ParallelOptions); Assert.Equal(image.Configuration.ImageFormats, Configuration.Default.ImageFormats); } /// /// Test that the default image constructor copies the configuration. /// [Fact] public void TestImageCopiesConfiguration() { Configuration.Default.AddImageFormat(new PngFormat()); Image image = new Image(1, 1); Image image2 = new Image(image); Assert.Equal(image2.Configuration.ParallelOptions, image.Configuration.ParallelOptions); Assert.True(image2.Configuration.ImageFormats.SequenceEqual(image.Configuration.ImageFormats)); } /// /// A test image format for testing the configuration. /// private class TestFormat : IImageFormat { /// /// Initializes a new instance of the class. /// public TestFormat() { this.Decoder = new JpegDecoder(); this.Encoder = new JpegEncoder(); this.Extension = "jpg"; this.MimeType = "image/test"; this.SupportedExtensions = new[] { "jpg" }; } /// public IImageDecoder Decoder { get; set; } /// public IImageEncoder Encoder { get; set; } /// public string MimeType { get; set; } /// public string Extension { get; set; } /// public IEnumerable SupportedExtensions { get; set; } /// public int HeaderSize { get { throw new NotImplementedException(); } } /// public bool IsSupportedFileFormat(byte[] header) { throw new NotImplementedException(); } } } }