mirror of https://github.com/SixLabors/ImageSharp
17 changed files with 368 additions and 28 deletions
@ -0,0 +1,38 @@ |
|||||
|
// <copyright file="IImageOperationsProvider.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp |
||||
|
{ |
||||
|
using System; |
||||
|
using ImageSharp.PixelFormats; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Represents an interface that will create IImageOperations
|
||||
|
/// </summary>
|
||||
|
internal interface IImageOperationsProvider |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Called during Mutate operations to generate the imageoperations provider.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TPixel">The pixel format</typeparam>
|
||||
|
/// <param name="source">The source image.</param>
|
||||
|
/// <returns>A new IImageOPeration</returns>
|
||||
|
IImageOperations<TPixel> CreateMutator<TPixel>(Image<TPixel> source) |
||||
|
where TPixel : struct, IPixel<TPixel>; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The default implmentation of IImageOperationsProvider
|
||||
|
/// </summary>
|
||||
|
internal class DefaultImageOperationsProvider : IImageOperationsProvider |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public IImageOperations<TPixel> CreateMutator<TPixel>(Image<TPixel> source) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
return new ImageOperations<TPixel>(source); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using ImageSharp.Processing; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace ImageSharp.Tests |
||||
|
{ |
||||
|
public abstract class BaseImageOperationsExtensionTest |
||||
|
{ |
||||
|
protected readonly FakeImageOperationsProvider.FakeImageOperations<Rgba32> operations; |
||||
|
|
||||
|
public BaseImageOperationsExtensionTest() |
||||
|
{ |
||||
|
this.operations = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(null); |
||||
|
} |
||||
|
|
||||
|
public T Verify<T>(int index = 0) |
||||
|
{ |
||||
|
var operation = this.operations.applied[index]; |
||||
|
|
||||
|
return Assert.IsType<T>(operation.Processor); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
namespace ImageSharp.Tests |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using ImageSharp.PixelFormats; |
||||
|
using ImageSharp.Processing; |
||||
|
using SixLabors.Primitives; |
||||
|
|
||||
|
public class FakeImageOperationsProvider : IImageOperationsProvider |
||||
|
{ |
||||
|
private List<object> ImageOperators = new List<object>(); |
||||
|
|
||||
|
public bool HasCreated<TPixel>(Image<TPixel> source) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
return Created(source).Any(); |
||||
|
} |
||||
|
public IEnumerable<FakeImageOperations<TPixel>> Created<TPixel>(Image<TPixel> source) where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
return this.ImageOperators.OfType<FakeImageOperations<TPixel>>() |
||||
|
.Where(x => x.source == source); |
||||
|
} |
||||
|
|
||||
|
public IEnumerable<FakeImageOperations<TPixel>.AppliedOpperation> AppliedOperations<TPixel>(Image<TPixel> source) where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
return Created(source) |
||||
|
.SelectMany(x => x.applied); |
||||
|
} |
||||
|
|
||||
|
public IImageOperations<TPixel> CreateMutator<TPixel>(Image<TPixel> source) where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
var op = new FakeImageOperations<TPixel>(source); |
||||
|
this.ImageOperators.Add(op); |
||||
|
return op; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public class FakeImageOperations<TPixel> : IImageOperations<TPixel> |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
public Image<TPixel> source; |
||||
|
|
||||
|
public List<AppliedOpperation> applied = new List<AppliedOpperation>(); |
||||
|
|
||||
|
public FakeImageOperations(Image<TPixel> source) |
||||
|
{ |
||||
|
this.source = source; |
||||
|
} |
||||
|
|
||||
|
public IImageOperations<TPixel> ApplyProcessor(IImageProcessor<TPixel> processor, Rectangle rectangle) |
||||
|
{ |
||||
|
applied.Add(new AppliedOpperation |
||||
|
{ |
||||
|
Processor = processor, |
||||
|
Rectangle = rectangle |
||||
|
}); |
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
public IImageOperations<TPixel> ApplyProcessor(IImageProcessor<TPixel> processor) |
||||
|
{ |
||||
|
applied.Add(new AppliedOpperation |
||||
|
{ |
||||
|
Processor = processor |
||||
|
}); |
||||
|
return this; |
||||
|
} |
||||
|
public struct AppliedOpperation |
||||
|
{ |
||||
|
public Rectangle? Rectangle { get; set; } |
||||
|
public IImageProcessor<TPixel> Processor { get; set; } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,120 @@ |
|||||
|
// <copyright file="ConfigurationTests.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Tests |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
|
||||
|
using ImageSharp.Formats; |
||||
|
using ImageSharp.IO; |
||||
|
using ImageSharp.PixelFormats; |
||||
|
using ImageSharp.Processing; |
||||
|
using Moq; |
||||
|
using SixLabors.Primitives; |
||||
|
using Xunit; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Tests the configuration class.
|
||||
|
/// </summary>
|
||||
|
public class ImageOperationTests : IDisposable |
||||
|
{ |
||||
|
private readonly Image<Rgba32> image; |
||||
|
private readonly FakeImageOperationsProvider provider; |
||||
|
private readonly IImageProcessor<Rgba32> processor; |
||||
|
|
||||
|
public Configuration Configuration { get; private set; } |
||||
|
|
||||
|
public ImageOperationTests() |
||||
|
{ |
||||
|
this.provider = new FakeImageOperationsProvider(); |
||||
|
this.processor = new Mock<IImageProcessor<Rgba32>>().Object; |
||||
|
this.image = new Image<Rgba32>(new Configuration() |
||||
|
{ |
||||
|
ImageOperationsProvider = this.provider |
||||
|
}, 1, 1); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MutateCallsImageOperationsProvider_Func_OriginalImage() |
||||
|
{ |
||||
|
this.image.Mutate(x => x.ApplyProcessor(this.processor)); |
||||
|
|
||||
|
Assert.True(this.provider.HasCreated(this.image)); |
||||
|
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x=>x.Processor)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MutateCallsImageOperationsProvider_ListOfProcessors_OriginalImage() |
||||
|
{ |
||||
|
this.image.Mutate(this.processor); |
||||
|
|
||||
|
Assert.True(this.provider.HasCreated(this.image)); |
||||
|
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CloneCallsImageOperationsProvider_Func_WithDuplicateImage() |
||||
|
{ |
||||
|
var returned = this.image.Clone(x => x.ApplyProcessor(this.processor)); |
||||
|
|
||||
|
Assert.True(this.provider.HasCreated(returned)); |
||||
|
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.Processor)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CloneCallsImageOperationsProvider_ListOfProcessors_WithDuplicateImage() |
||||
|
{ |
||||
|
var returned = this.image.Clone(this.processor); |
||||
|
|
||||
|
Assert.True(this.provider.HasCreated(returned)); |
||||
|
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.Processor)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CloneCallsImageOperationsProvider_Func_NotOnOrigional() |
||||
|
{ |
||||
|
var returned = this.image.Clone(x => x.ApplyProcessor(this.processor)); |
||||
|
Assert.False(this.provider.HasCreated(this.image)); |
||||
|
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CloneCallsImageOperationsProvider_ListOfProcessors_NotOnOrigional() |
||||
|
{ |
||||
|
var returned = this.image.Clone(this.processor); |
||||
|
Assert.False(this.provider.HasCreated(this.image)); |
||||
|
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.Processor)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ApplyProcessors_ListOfProcessors_AppliesALlProcessorsToOperation() |
||||
|
{ |
||||
|
var operations = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(null); |
||||
|
operations.ApplyProcessors(this.processor); |
||||
|
Assert.Contains(this.processor, operations.applied.Select(x => x.Processor)); |
||||
|
} |
||||
|
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
this.image.Dispose(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class RunImageOperation : BaseImageOperationsExtensionTest |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Run_CreatedDelegateProcessor() |
||||
|
{ |
||||
|
Action<Image<Rgba32>> action = (i) => { }; |
||||
|
this.operations.Run(action); |
||||
|
|
||||
|
DelegateImageProcessor<Rgba32> processor = this.Verify<DelegateImageProcessor<Rgba32>>(); |
||||
|
Assert.Equal(action, processor.Action); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue