From 847b21d44f2a4249c61995131106327ea89e3f8e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 1 May 2018 18:33:44 +0200 Subject: [PATCH] use overloading in ImageFrameCollection + more tests for API-s affected by the PR --- src/ImageSharp/ImageFrameCollection.cs | 20 ++++++- .../Image/ImageFramesCollectionTests.cs | 13 +++- tests/ImageSharp.Tests/Image/ImageTests.cs | 60 +++++++++++++++++-- .../TestUtilities/TestImageExtensions.cs | 15 ++++- 4 files changed, 98 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/ImageFrameCollection.cs b/src/ImageSharp/ImageFrameCollection.cs index 97a8df875d..0318a7068d 100644 --- a/src/ImageSharp/ImageFrameCollection.cs +++ b/src/ImageSharp/ImageFrameCollection.cs @@ -198,6 +198,17 @@ namespace SixLabors.ImageSharp return new Image(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { clonedFrame }); } + /// + /// Creates a new and appends it to the end of the collection. + /// + /// + /// The new . + /// + public ImageFrame CreateFrame() + { + return this.CreateFrame(default); + } + /// /// Creates a new and appends it to the end of the collection. /// @@ -205,8 +216,13 @@ namespace SixLabors.ImageSharp /// /// The new . /// - public ImageFrame CreateFrame(TPixel backgroundColor = default) { - var frame = new ImageFrame(this.parent.GetConfiguration(), this.RootFrame.Width, this.RootFrame.Height, backgroundColor); + public ImageFrame CreateFrame(TPixel backgroundColor) + { + var frame = new ImageFrame( + this.parent.GetConfiguration(), + this.RootFrame.Width, + this.RootFrame.Height, + backgroundColor); this.frames.Add(frame); return frame; } diff --git a/tests/ImageSharp.Tests/Image/ImageFramesCollectionTests.cs b/tests/ImageSharp.Tests/Image/ImageFramesCollectionTests.cs index cb185d9773..c2ebf83ba7 100644 --- a/tests/ImageSharp.Tests/Image/ImageFramesCollectionTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageFramesCollectionTests.cs @@ -225,10 +225,21 @@ namespace SixLabors.ImageSharp.Tests } [Fact] - public void CreateFrame() + public void CreateFrame_Default() { this.image.Frames.CreateFrame(); + + Assert.Equal(2, this.image.Frames.Count); + this.image.Frames[1].ComparePixelBufferTo(default(Rgba32)); + } + + [Fact] + public void CreateFrame_CustomFillColor() + { + this.image.Frames.CreateFrame(Rgba32.HotPink); + Assert.Equal(2, this.image.Frames.Count); + this.image.Frames[1].ComparePixelBufferTo(Rgba32.HotPink); } [Fact] diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index da813f4280..8234df24ef 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -13,10 +13,60 @@ namespace SixLabors.ImageSharp.Tests /// /// Tests the class. /// - public class ImageTests : FileTestBase + public class ImageTests { + public class Constructor + { + [Fact] + public void Width_Height() + { + using (var image = new Image(11, 23)) + { + Assert.Equal(11, image.Width); + Assert.Equal(23, image.Height); + Assert.Equal(11*23, image.GetPixelSpan().Length); + image.ComparePixelBufferTo(default(Rgba32)); + + Assert.Equal(Configuration.Default, image.GetConfiguration()); + } + } + + [Fact] + public void Configuration_Width_Height() + { + Configuration configuration = Configuration.Default.ShallowCopy(); + + using (var image = new Image(configuration, 11, 23)) + { + Assert.Equal(11, image.Width); + Assert.Equal(23, image.Height); + Assert.Equal(11 * 23, image.GetPixelSpan().Length); + image.ComparePixelBufferTo(default(Rgba32)); + + Assert.Equal(configuration, image.GetConfiguration()); + } + } + + [Fact] + public void Configuration_Width_Height_BackroundColor() + { + Configuration configuration = Configuration.Default.ShallowCopy(); + Rgba32 color = Rgba32.Aquamarine; + + using (var image = new Image(configuration, 11, 23, color)) + { + Assert.Equal(11, image.Width); + Assert.Equal(23, image.Height); + Assert.Equal(11 * 23, image.GetPixelSpan().Length); + image.ComparePixelBufferTo(color); + + Assert.Equal(configuration, image.GetConfiguration()); + } + } + } + [Fact] - public void ConstructorByteArray() + public void Load_ByteArray() { Assert.Throws(() => { @@ -32,7 +82,7 @@ namespace SixLabors.ImageSharp.Tests } [Fact] - public void ConstructorFileSystem() + public void Load_FileSystemPath() { TestFile file = TestFile.Create(TestImages.Bmp.Car); using (Image image = Image.Load(file.FullPath)) @@ -43,7 +93,7 @@ namespace SixLabors.ImageSharp.Tests } [Fact] - public void ConstructorFileSystem_FileNotFound() + public void Load_FileSystemPath_FileNotFound() { System.IO.FileNotFoundException ex = Assert.Throws( () => @@ -53,7 +103,7 @@ namespace SixLabors.ImageSharp.Tests } [Fact] - public void ConstructorFileSystem_NullPath() + public void Load_FileSystemPath_NullPath() { ArgumentNullException ex = Assert.Throws( () => diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index f37df48dce..8955eeb63c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -361,14 +361,25 @@ namespace SixLabors.ImageSharp.Tests public static Image ComparePixelBufferTo(this Image image, TPixel expectedPixel) where TPixel : struct, IPixel { - Span actualPixels = image.GetPixelSpan(); + foreach (ImageFrame imageFrame in image.Frames) + { + imageFrame.ComparePixelBufferTo(expectedPixel); + } + + return image; + } + + public static ImageFrame ComparePixelBufferTo(this ImageFrame imageFrame, TPixel expectedPixel) + where TPixel : struct, IPixel + { + Span actualPixels = imageFrame.GetPixelSpan(); for (int i = 0; i < actualPixels.Length; i++) { Assert.True(expectedPixel.Equals(actualPixels[i]), $"Pixels are different on position {i}!"); } - return image; + return imageFrame; } public static ImageFrame ComparePixelBufferTo(