diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index 3d86a5b0c..38baf91d3 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using SixLabors.ImageSharp.Processing; namespace SixLabors.ImageSharp @@ -43,7 +44,9 @@ namespace SixLabors.ImageSharp } var configOptions = context.Configuration.GetDefaultGraphicsOptions(); - context.Properties[typeof(GraphicsOptions)] = configOptions; + + // do not cache the fall back to config into the the processing context + // in case someone want to change the value on the config and expects it re trflow thru return configOptions; } @@ -60,6 +63,8 @@ namespace SixLabors.ImageSharp } var configOptions = new GraphicsOptions(); + + // capture the fallback so the same instance will always be returned in case its mutated context.Properties[typeof(GraphicsOptions)] = configOptions; return configOptions; } diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs b/tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs new file mode 100644 index 000000000..3f90412ae --- /dev/null +++ b/tests/ImageSharp.Tests/Drawing/DrawImageExtensionsTests.cs @@ -0,0 +1,83 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Linq; +using Moq; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors.Drawing; +using SixLabors.ImageSharp.Tests.Processing; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Drawing +{ + public class DrawImageExtensionsTests : BaseImageOperationsExtensionTest + { + + [Fact] + public void DrawImage_OpacityOnly_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(this.options.ColorBlendingMode, dip.ColorBlendingMode); + } + + [Fact] + public void DrawImage_OpacityAndBlending_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, PixelColorBlendingMode.Multiply, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(PixelColorBlendingMode.Multiply, dip.ColorBlendingMode); + } + + [Fact] + public void DrawImage_LocationAndOpacity_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, Point.Empty, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(this.options.ColorBlendingMode, dip.ColorBlendingMode); + } + + [Fact] + public void DrawImage_LocationAndOpacityAndBlending_VerifyGraphicOptionsTakenFromContext() + { + // non-default values as we cant easly defect usage otherwise + this.options.AlphaCompositionMode = PixelAlphaCompositionMode.Xor; + this.options.ColorBlendingMode = PixelColorBlendingMode.Screen; + + this.operations.DrawImage(null, Point.Empty, PixelColorBlendingMode.Multiply, 0.5f); + var dip = this.Verify(); + + Assert.Equal(0.5, dip.Opacity); + Assert.Equal(this.options.AlphaCompositionMode, dip.AlphaCompositionMode); + Assert.Equal(PixelColorBlendingMode.Multiply, dip.ColorBlendingMode); + } + } +} diff --git a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs new file mode 100644 index 000000000..6707341d2 --- /dev/null +++ b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs @@ -0,0 +1,128 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests.Processing; +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public class GraphicOptionsDefaultsExtensionsTests + { + [Fact] + public void SetDefaultOptionsOnProcessingContext() + { + var option = new GraphicsOptions(); + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + context.SetDefaultOptions(option); + + // sets the prop on the processing context not on the configuration + Assert.Equal(option, context.Properties[typeof(GraphicsOptions)]); + Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); + } + + [Fact] + public void SetDefaultOptionsOnConfiguration() + { + var option = new GraphicsOptions(); + var config = new Configuration(); + + config.SetDefaultOptions(option); + + Assert.Equal(option, config.Properties[typeof(GraphicsOptions)]); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_SettingNullThenReturnsNewInstance() + { + var config = new Configuration(); + + var options = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + config.SetDefaultOptions((GraphicsOptions)null); + + var options2 = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options2); + + // we set it to null should now be a new instance + Assert.NotEqual(options, options2); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_IgnoreIncorectlyTypesDictionEntry() + { + var config = new Configuration(); + + config.Properties[typeof(GraphicsOptions)] = "wronge type"; + var options = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + Assert.IsType(options); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_AlwaysReturnsInstance() + { + var config = new Configuration(); + + Assert.DoesNotContain(typeof(GraphicsOptions), config.Properties.Keys); + var options = config.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + } + + [Fact] + public void GetDefaultOptionsFromConfiguration_AlwaysReturnsSameValue() + { + var config = new Configuration(); + + var options = config.GetDefaultGraphicsOptions(); + var options2 = config.GetDefaultGraphicsOptions(); + Assert.Equal(options, options2); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_AlwaysReturnsInstance() + { + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + var ctxOptions = context.GetDefaultGraphicsOptions(); + Assert.NotNull(ctxOptions); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_AlwaysReturnsInstanceEvenIfSetToNull() + { + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + context.SetDefaultOptions((GraphicsOptions)null); + var ctxOptions = context.GetDefaultGraphicsOptions(); + Assert.NotNull(ctxOptions); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_FallbackToConfigsInstance() + { + var option = new GraphicsOptions(); + var config = new Configuration(); + config.SetDefaultOptions(option); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + + var ctxOptions = context.GetDefaultGraphicsOptions(); + Assert.Equal(option, ctxOptions); + } + + [Fact] + public void GetDefaultOptionsFromProcessingContext_IgnoreIncorectlyTypesDictionEntry() + { + var config = new Configuration(); + var context = new FakeImageOperationsProvider.FakeImageOperations(config, null, true); + context.Properties[typeof(GraphicsOptions)] = "wronge type"; + var options = context.GetDefaultGraphicsOptions(); + Assert.NotNull(options); + Assert.IsType(options); + } + } +} diff --git a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs index 82c22245d..da2040131 100644 --- a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs +++ b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs @@ -25,6 +25,7 @@ namespace SixLabors.ImageSharp.Tests.Processing this.source = new Image(91 + 324, 123 + 56); this.rect = new Rectangle(91, 123, 324, 56); // make this random? this.internalOperations = new FakeImageOperationsProvider.FakeImageOperations(this.source.GetConfiguration(), this.source, false); + this.internalOperations.SetDefaultOptions(this.options); this.operations = this.internalOperations; } diff --git a/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs b/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs index 37bd2e87a..34b99461d 100644 --- a/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs +++ b/tests/ImageSharp.Tests/Processing/Effects/BackgroundColorTest.cs @@ -10,15 +10,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects { public class BackgroundColorTest : BaseImageOperationsExtensionTest { - private static readonly GraphicsOptionsComparer GraphicsOptionsComparer = new GraphicsOptionsComparer(); - [Fact] public void BackgroundColor_amount_BackgroundColorProcessorDefaultsSet() { this.operations.BackgroundColor(Color.BlanchedAlmond); BackgroundColorProcessor processor = this.Verify(); - Assert.Equal(new GraphicsOptions(), processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } @@ -28,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.BackgroundColor(Color.BlanchedAlmond, this.rect); BackgroundColorProcessor processor = this.Verify(this.rect); - Assert.Equal(new GraphicsOptions(), processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } @@ -38,7 +36,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.BackgroundColor(this.options, Color.BlanchedAlmond); BackgroundColorProcessor processor = this.Verify(); - Assert.Equal(this.options, processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } @@ -48,7 +46,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.BackgroundColor(this.options, Color.BlanchedAlmond, this.rect); BackgroundColorProcessor processor = this.Verify(this.rect); - Assert.Equal(this.options, processor.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, processor.GraphicsOptions); Assert.Equal(Color.BlanchedAlmond, processor.Color); } } diff --git a/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs b/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs index 65e04fbcc..6cb38e2fe 100644 --- a/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs +++ b/tests/ImageSharp.Tests/Processing/Filters/LomographTest.cs @@ -16,14 +16,16 @@ namespace SixLabors.ImageSharp.Tests public void Lomograph_amount_LomographProcessorDefaultsSet() { this.operations.Lomograph(); - this.Verify(); + var processor = this.Verify(); + Assert.Equal(processor.GraphicsOptions, this.options); } [Fact] public void Lomograph_amount_rect_LomographProcessorDefaultsSet() { this.operations.Lomograph(this.rect); - this.Verify(this.rect); + var processor = this.Verify(this.rect); + Assert.Equal(processor.GraphicsOptions, this.options); } } } diff --git a/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs b/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs index c3e2c3c50..346df0379 100644 --- a/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs +++ b/tests/ImageSharp.Tests/Processing/Filters/PolaroidTest.cs @@ -14,14 +14,16 @@ namespace SixLabors.ImageSharp.Tests.Processing.Filters public void Polaroid_amount_PolaroidProcessorDefaultsSet() { this.operations.Polaroid(); - this.Verify(); + var processor = this.Verify(); + Assert.Equal(processor.GraphicsOptions, this.options); } [Fact] public void Polaroid_amount_rect_PolaroidProcessorDefaultsSet() { this.operations.Polaroid(this.rect); - this.Verify(this.rect); + var processor = this.Verify(this.rect); + Assert.Equal(processor.GraphicsOptions, this.options); } } } diff --git a/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs b/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs index ea000ae2a..0336b231b 100644 --- a/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs +++ b/tests/ImageSharp.Tests/Processing/Overlays/GlowTest.cs @@ -11,15 +11,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays { public class GlowTest : BaseImageOperationsExtensionTest { - private static readonly GraphicsOptionsComparer GraphicsOptionsComparer = new GraphicsOptionsComparer(); - [Fact] public void Glow_GlowProcessorWithDefaultValues() { this.operations.Glow(); GlowProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.GlowColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.Radius); } @@ -30,7 +28,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Glow(Color.Aquamarine); GlowProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Aquamarine, p.GlowColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.Radius); } @@ -41,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Glow(3.5f); GlowProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.GlowColor); Assert.Equal(ValueSize.Absolute(3.5f), p.Radius); } @@ -53,7 +51,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Glow(rect); GlowProcessor p = this.Verify(rect); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.GlowColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.Radius); } diff --git a/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs b/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs index 8e5eb7207..5d41c58ce 100644 --- a/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs +++ b/tests/ImageSharp.Tests/Processing/Overlays/VignetteTest.cs @@ -10,15 +10,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays { public class VignetteTest : BaseImageOperationsExtensionTest { - private static readonly GraphicsOptionsComparer GraphicsOptionsComparer = new GraphicsOptionsComparer(); - [Fact] public void Vignette_VignetteProcessorWithDefaultValues() { this.operations.Vignette(); VignetteProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.VignetteColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.RadiusX); Assert.Equal(ValueSize.PercentageOfHeight(.5f), p.RadiusY); @@ -30,7 +28,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Vignette(Color.Aquamarine); VignetteProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Aquamarine, p.VignetteColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.RadiusX); Assert.Equal(ValueSize.PercentageOfHeight(.5f), p.RadiusY); @@ -42,7 +40,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Vignette(3.5f, 12123f); VignetteProcessor p = this.Verify(); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.VignetteColor); Assert.Equal(ValueSize.Absolute(3.5f), p.RadiusX); Assert.Equal(ValueSize.Absolute(12123f), p.RadiusY); @@ -55,7 +53,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Overlays this.operations.Vignette(rect); VignetteProcessor p = this.Verify(rect); - Assert.Equal(new GraphicsOptions(), p.GraphicsOptions, GraphicsOptionsComparer); + Assert.Equal(this.options, p.GraphicsOptions); Assert.Equal(Color.Black, p.VignetteColor); Assert.Equal(ValueSize.PercentageOfWidth(.5f), p.RadiusX); Assert.Equal(ValueSize.PercentageOfHeight(.5f), p.RadiusY);