Browse Source

use overloading in ImageFrameCollection + more tests for API-s affected by the PR

pull/552/head
Anton Firszov 8 years ago
parent
commit
a9816fd937
  1. 20
      src/ImageSharp/ImageFrameCollection.cs
  2. 13
      tests/ImageSharp.Tests/Image/ImageFramesCollectionTests.cs
  3. 60
      tests/ImageSharp.Tests/Image/ImageTests.cs
  4. 15
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

20
src/ImageSharp/ImageFrameCollection.cs

@ -198,6 +198,17 @@ namespace SixLabors.ImageSharp
return new Image<TPixel>(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { clonedFrame });
}
/// <summary>
/// Creates a new <seealso cref="ImageFrame{TPixel}" /> and appends it to the end of the collection.
/// </summary>
/// <returns>
/// The new <see cref="ImageFrame{TPixel}" />.
/// </returns>
public ImageFrame<TPixel> CreateFrame()
{
return this.CreateFrame(default);
}
/// <summary>
/// Creates a new <seealso cref="ImageFrame{TPixel}" /> and appends it to the end of the collection.
/// </summary>
@ -205,8 +216,13 @@ namespace SixLabors.ImageSharp
/// <returns>
/// The new <see cref="ImageFrame{TPixel}" />.
/// </returns>
public ImageFrame<TPixel> CreateFrame(TPixel backgroundColor = default) {
var frame = new ImageFrame<TPixel>(this.parent.GetConfiguration(), this.RootFrame.Width, this.RootFrame.Height, backgroundColor);
public ImageFrame<TPixel> CreateFrame(TPixel backgroundColor)
{
var frame = new ImageFrame<TPixel>(
this.parent.GetConfiguration(),
this.RootFrame.Width,
this.RootFrame.Height,
backgroundColor);
this.frames.Add(frame);
return frame;
}

13
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]

60
tests/ImageSharp.Tests/Image/ImageTests.cs

@ -13,10 +13,60 @@ namespace SixLabors.ImageSharp.Tests
/// <summary>
/// Tests the <see cref="Image"/> class.
/// </summary>
public class ImageTests : FileTestBase
public class ImageTests
{
public class Constructor
{
[Fact]
public void Width_Height()
{
using (var image = new Image<Rgba32>(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<Rgba32>(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<Rgba32>(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<ArgumentNullException>(() =>
{
@ -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<Rgba32> image = Image.Load<Rgba32>(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<System.IO.FileNotFoundException>(
() =>
@ -53,7 +103,7 @@ namespace SixLabors.ImageSharp.Tests
}
[Fact]
public void ConstructorFileSystem_NullPath()
public void Load_FileSystemPath_NullPath()
{
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
() =>

15
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -361,14 +361,25 @@ namespace SixLabors.ImageSharp.Tests
public static Image<TPixel> ComparePixelBufferTo<TPixel>(this Image<TPixel> image, TPixel expectedPixel)
where TPixel : struct, IPixel<TPixel>
{
Span<TPixel> actualPixels = image.GetPixelSpan();
foreach (ImageFrame<TPixel> imageFrame in image.Frames)
{
imageFrame.ComparePixelBufferTo(expectedPixel);
}
return image;
}
public static ImageFrame<TPixel> ComparePixelBufferTo<TPixel>(this ImageFrame<TPixel> imageFrame, TPixel expectedPixel)
where TPixel : struct, IPixel<TPixel>
{
Span<TPixel> 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<TPixel> ComparePixelBufferTo<TPixel>(

Loading…
Cancel
Save