mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 8089c857d0304156db615b3c04a93d4bbc68cf18 Former-commit-id: 3d80374aa22017a633cf27f92376611fb35e57d3af/merge-core
6 changed files with 185 additions and 63 deletions
@ -0,0 +1,56 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="AssertionHelpers.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
namespace ImageProcessor.UnitTests |
|||
{ |
|||
using System.Drawing; |
|||
using System.Drawing.Imaging; |
|||
using System.Linq; |
|||
using System.IO; |
|||
using NUnit.Framework; |
|||
using FluentAssertions; |
|||
|
|||
/// <summary>
|
|||
/// Provides helpers for asserting
|
|||
/// </summary>
|
|||
public class AssertionHelpers |
|||
{ |
|||
/// <summary>
|
|||
/// Asserts that two images are identical
|
|||
/// </summary>
|
|||
/// <param name="expected">The expected result</param>
|
|||
/// <param name="tested">The tested image</param>
|
|||
public static void AssertImagesAreIdentical(Image expected, Image tested, string because, params string[] becauseArgs) |
|||
{ |
|||
ToByteArray(expected).SequenceEqual(ToByteArray(tested)).Should().BeTrue(because, becauseArgs); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Asserts that two images are different
|
|||
/// </summary>
|
|||
/// <param name="expected">The not-expected result</param>
|
|||
/// <param name="tested">The tested image</param>
|
|||
public static void AssertImagesAreDifferent(Image expected, Image tested, string because, params string[] becauseArgs) |
|||
{ |
|||
ToByteArray(expected).SequenceEqual(ToByteArray(tested)).Should().BeFalse(because, becauseArgs); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts an image to a byte array
|
|||
/// </summary>
|
|||
/// <param name="imageIn">The image to convert</param>
|
|||
/// <param name="format">The format to use</param>
|
|||
/// <returns>An array of bytes representing the image</returns>
|
|||
public static byte[] ToByteArray(Image imageIn) |
|||
{ |
|||
using (MemoryStream ms = new MemoryStream()) |
|||
{ |
|||
imageIn.Save(ms, ImageFormat.Jpeg); |
|||
return ms.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="CropLayerUnitTests.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
namespace ImageProcessor.UnitTests.Imaging |
|||
{ |
|||
using ImageProcessor.Imaging; |
|||
using FluentAssertions; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Test harness for the <see cref="CropLayer" /> class
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class CropLayerUnitTests |
|||
{ |
|||
/// <summary>
|
|||
/// Tests that the constructor saves the provided data
|
|||
/// </summary>
|
|||
[Test] |
|||
[TestCase(10.5F, 11.2F, 15.6F, 108.9F, CropMode.Percentage)] |
|||
[TestCase(15.1F, 20.7F, 65.8F, 156.7F, CropMode.Pixels)] |
|||
public void ConstructorSavesData(float left, float top, float right, float bottom, CropMode mode) |
|||
{ |
|||
CropLayer cl = new CropLayer(left, top, right, bottom, mode); |
|||
|
|||
cl.Left.Should().Be(left); |
|||
cl.Top.Should().Be(top); |
|||
cl.Right.Should().Be(right); |
|||
cl.Bottom.Should().Be(bottom); |
|||
cl.CropMode.Should().Be(mode); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="FastBitmapUnitTests.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
namespace ImageProcessor.UnitTests.Imaging |
|||
{ |
|||
using System.Drawing; |
|||
using FluentAssertions; |
|||
using ImageProcessor.Imaging; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Test harness for the <see cref="ImageProcessor.Imaging.FastBitmap"/> class
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class FastBitmapUnitTests |
|||
{ |
|||
/// <summary>
|
|||
/// Tests that the bitmap's data is read by the fast bitmap
|
|||
/// </summary>
|
|||
/// <param name="file">The path to the test file</param>
|
|||
[Test] |
|||
[TestCase(@"Images\format-Penguins.jpg")] |
|||
[TestCase(@"Images\format-Penguins.png")] |
|||
public void BitmapIsRead(string file) |
|||
{ |
|||
Bitmap bmp = new Bitmap(file); |
|||
|
|||
using (FastBitmap fbmp = new FastBitmap(bmp)) |
|||
{ |
|||
fbmp.Width.Should().Be(bmp.Width, "because the bitmap should have been read"); |
|||
fbmp.Height.Should().Be(bmp.Height, "because the bitmap should have been read"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that modifications on the fast bitmap's bitmap are actually done
|
|||
/// </summary>
|
|||
/// <param name="file">The path to the test file</param>
|
|||
[Test] |
|||
[TestCase(@"Images\format-Penguins.jpg")] |
|||
[TestCase(@"Images\format-Penguins.png")] |
|||
public void FastBitmapModificationsAreApplied(string file) |
|||
{ |
|||
Bitmap bmp = new Bitmap(file); |
|||
Bitmap original = (Bitmap)bmp.Clone(); |
|||
|
|||
using (FastBitmap fbmp = new FastBitmap(bmp)) |
|||
{ |
|||
// draw a pink diagonal line
|
|||
for (int i = 0; i < 10; i++) |
|||
{ |
|||
fbmp.SetPixel(i, i, Color.Pink); |
|||
} |
|||
} |
|||
|
|||
AssertionHelpers.AssertImagesAreDifferent(original, bmp, "because modifying the fast bitmap should have modified the original bitmap"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue