|
|
|
@ -6,7 +6,9 @@ |
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Squidex.Infrastructure.Assets.ImageSharp; |
|
|
|
using Xunit; |
|
|
|
@ -18,10 +20,43 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
private readonly ImageSharpAssetThumbnailGenerator sut = new ImageSharpAssetThumbnailGenerator(); |
|
|
|
private readonly MemoryStream target = new MemoryStream(); |
|
|
|
|
|
|
|
public static IEnumerable<object[]> GetConversions() |
|
|
|
{ |
|
|
|
var allFormats = Enum.GetValues(typeof(ImageFormat)).OfType<ImageFormat>().Where(x => x != ImageFormat.Auto); |
|
|
|
|
|
|
|
foreach (var source in allFormats) |
|
|
|
{ |
|
|
|
foreach (var target in allFormats) |
|
|
|
{ |
|
|
|
if (!Equals(target, source)) |
|
|
|
{ |
|
|
|
yield return new object[] { target, source }; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(GetConversions))] |
|
|
|
public async Task Should_convert_between_formats(ImageFormat sourceFormat, ImageFormat targetFormat) |
|
|
|
{ |
|
|
|
var source = GetImage(sourceFormat); |
|
|
|
|
|
|
|
var options = new ResizeOptions { Format = targetFormat }; |
|
|
|
|
|
|
|
await sut.CreateThumbnailAsync(source, target, options); |
|
|
|
|
|
|
|
target.Position = 0; |
|
|
|
|
|
|
|
var imageInfo = await sut.GetImageInfoAsync(target); |
|
|
|
|
|
|
|
Assert.Equal(targetFormat.ToString(), imageInfo?.Format); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_return_same_image_if_no_size_and_quality_is_passed_for_thumbnail() |
|
|
|
{ |
|
|
|
var source = GetPng(); |
|
|
|
var source = GetImage(ImageFormat.PNG); |
|
|
|
|
|
|
|
await sut.CreateThumbnailAsync(source, target, new ResizeOptions()); |
|
|
|
|
|
|
|
@ -31,7 +66,7 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
[Fact] |
|
|
|
public async Task Should_resize_image_to_target() |
|
|
|
{ |
|
|
|
var source = GetPng(); |
|
|
|
var source = GetImage(ImageFormat.PNG); |
|
|
|
|
|
|
|
var options = new ResizeOptions { Width = 1000, Height = 1000, Mode = ResizeMode.BoxPad }; |
|
|
|
|
|
|
|
@ -43,7 +78,7 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
[Fact] |
|
|
|
public async Task Should_change_jpeg_quality_and_write_to_target() |
|
|
|
{ |
|
|
|
var source = GetJpeg(); |
|
|
|
var source = GetImage(ImageFormat.JPEG); |
|
|
|
|
|
|
|
var options = new ResizeOptions { Quality = 10 }; |
|
|
|
|
|
|
|
@ -55,7 +90,7 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
[Fact] |
|
|
|
public async Task Should_change_png_quality_and_write_to_target() |
|
|
|
{ |
|
|
|
var source = GetPng(); |
|
|
|
var source = GetImage(ImageFormat.PNG); |
|
|
|
|
|
|
|
var options = new ResizeOptions { Quality = 10 }; |
|
|
|
|
|
|
|
@ -73,18 +108,20 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
|
|
|
|
Assert.Equal(135, imageInfo.PixelHeight); |
|
|
|
Assert.Equal(600, imageInfo.PixelWidth); |
|
|
|
|
|
|
|
Assert.False(imageInfo.IsRotatedOrSwapped); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_return_image_information_if_image_is_valid() |
|
|
|
{ |
|
|
|
var source = GetPng(); |
|
|
|
var source = GetImage(ImageFormat.PNG); |
|
|
|
|
|
|
|
var imageInfo = await sut.GetImageInfoAsync(source); |
|
|
|
|
|
|
|
Assert.Equal(600, imageInfo!.PixelHeight); |
|
|
|
Assert.Equal(600, imageInfo!.PixelWidth); |
|
|
|
|
|
|
|
Assert.False(imageInfo.IsRotatedOrSwapped); |
|
|
|
} |
|
|
|
|
|
|
|
@ -97,6 +134,7 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
|
|
|
|
Assert.Equal(600, imageInfo!.PixelHeight); |
|
|
|
Assert.Equal(135, imageInfo!.PixelWidth); |
|
|
|
|
|
|
|
Assert.True(imageInfo.IsRotatedOrSwapped); |
|
|
|
} |
|
|
|
|
|
|
|
@ -110,19 +148,18 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
Assert.Null(imageInfo); |
|
|
|
} |
|
|
|
|
|
|
|
private Stream GetPng() |
|
|
|
private Stream GetImage(ImageFormat format) |
|
|
|
{ |
|
|
|
return GetType().Assembly.GetManifestResourceStream("Squidex.Infrastructure.Assets.Images.logo.png")!; |
|
|
|
} |
|
|
|
var name = $"Squidex.Infrastructure.Assets.Images.logo.{format.ToString().ToLowerInvariant()}"; |
|
|
|
|
|
|
|
private Stream GetJpeg() |
|
|
|
{ |
|
|
|
return GetType().Assembly.GetManifestResourceStream("Squidex.Infrastructure.Assets.Images.logo.jpg")!; |
|
|
|
return GetType().Assembly.GetManifestResourceStream(name)!; |
|
|
|
} |
|
|
|
|
|
|
|
private Stream GetRotatedJpeg() |
|
|
|
{ |
|
|
|
return GetType().Assembly.GetManifestResourceStream("Squidex.Infrastructure.Assets.Images.logo-wide-rotated.jpg")!; |
|
|
|
var name = "Squidex.Infrastructure.Assets.Images.logo-wide-rotated.jpg"; |
|
|
|
|
|
|
|
return GetType().Assembly.GetManifestResourceStream(name)!; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|