// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Unit tests for the ImageFactory (loading of images)
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.UnitTests
{
using System;
using System.IO;
using System.Collections.Generic;
using NUnit.Framework;
///
/// Test harness for the image factory
///
[TestFixture]
public class ImageFactoryUnitTests
{
///
/// Lists the input files in the Images folder
///
/// The list of files.
private static IEnumerable ListInputFiles()
{
return Directory.GetFiles("./Images");
}
///
/// Tests the loading of image from a file
///
[Test]
public void TestLoadImageFromFile()
{
foreach (var fileName in ListInputFiles())
{
using (var imageFactory = new ImageFactory())
{
imageFactory.Load(fileName);
Assert.AreEqual(fileName, imageFactory.ImagePath);
Assert.IsNotNull(imageFactory.Image);
}
}
}
/// >
/// Tests the loading of image from a memory stream
///
[Test]
public void TestLoadImageFromMemory()
{
foreach (var fileName in ListInputFiles())
{
byte[] photoBytes = File.ReadAllBytes(fileName);
using (var inStream = new MemoryStream(photoBytes))
{
using (var imageFactory = new ImageFactory())
{
imageFactory.Load(inStream);
Assert.AreEqual(null, imageFactory.ImagePath);
Assert.IsNotNull(imageFactory.Image);
}
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void ApplyEffectAlpha()
{
foreach (var fileName in ListInputFiles())
{
using (var imageFactory = new ImageFactory())
{
imageFactory.Load(fileName);
var original = imageFactory.Image.Clone();
imageFactory.Alpha(50);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void ApplyEffectBrightness()
{
foreach (var fileName in ListInputFiles())
{
using (var imageFactory = new ImageFactory())
{
imageFactory.Load(fileName);
var original = imageFactory.Image.Clone();
imageFactory.Brightness(50);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that the image is well resized using constraints
///
[Test]
public void ApplyConstraints()
{
const int maxSize = 200;
foreach (var fileName in ListInputFiles())
{
using (var imageFactory = new ImageFactory())
{
imageFactory.Load(fileName);
var original = imageFactory.Image.Clone();
imageFactory.Constrain(new System.Drawing.Size(maxSize, maxSize));
Assert.AreNotEqual(original, imageFactory.Image);
Assert.LessOrEqual(imageFactory.Image.Width, maxSize);
Assert.LessOrEqual(imageFactory.Image.Height, maxSize);
}
}
}
}
}