Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ImageProcessors/CropWebProcessorTests.cs
Ronald Barendse 1a82e0854a v10: Update to ImageSharp v2 (#12185)
* Update to ImageSharp 2.1.0 and ImageSharp.Web 2.0.0-alpha.0.23

* Rename CachedNameLength to CacheHashLength and add CacheFolderDepth setting

* Replace PhysicalFileSystemProvider with WebRootImageProvider

* Support EXIF-orientation in image dimention extractor

* Remove virtual methods on FileProviderImageProvider

* Simplify FileInfoImageResolver

* Update to SixLabors.ImageSharp.Web 2.0.0-alpha.0.25 and remove custom providers

* Make CropWebProcessor EXIF orientation-aware

* Improve width/height sanitization

* Also use 'v' as cache buster value

* Add WebP to supported image file types

* Update to SixLabors.ImageSharp.Web 2.0.0-alpha.0.27 and fix test

* Fix rounding error and add test cases

* Update to newest and stable releases

* Move ImageSharpImageUrlGenerator to Umbraco.Web.Common

* Use IConfigureOptions to configure ImageSharp options

* Implement IEquatable on ImageUrlGenerationOptions classes

* Fix empty/null values in image URL generation and corresponding tests

* Use IsSupportedImageFormat extension method

* Remove unneeded reflection
2022-04-29 13:16:24 +02:00

56 lines
2.0 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Globalization;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Web;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.Commands.Converters;
using SixLabors.ImageSharp.Web.Middleware;
using Umbraco.Cms.Web.Common.ImageProcessors;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.ImageProcessors
{
[TestFixture]
public class CropWebProcessorTests
{
[Test]
// Coordinates are percentages to crop from the left, top, right and bottom sides
[TestCase("0,0,0,0", 50, 90)]
[TestCase("0.1,0.0,0.0,0.0", 45, 90)]
[TestCase("0.0,0.1,0.0,0.0", 50, 81)]
[TestCase("0.0,0.0,0.1,0.0", 45, 90)]
[TestCase("0.0,0.0,0.0,0.1", 50, 81)]
[TestCase("0.1,0.0,0.1,0.0", 40, 90)]
[TestCase("0.0,0.1,0.0,0.1", 50, 72)]
[TestCase("0.1,0.1,0.1,0.1", 40, 72)]
[TestCase("0.25,0.25,0.25,0.25", 25, 45)]
public void CropWebProcessor_CropsImage(string coordinates, int width, int height)
{
using var image = new Image<Rgba32>(50, 90);
using var formattedImage = new FormattedImage(image, PngFormat.Instance);
var logger = new NullLogger<ImageSharpMiddleware>();
var commands = new CommandCollection
{
{ CropWebProcessor.Coordinates, coordinates },
};
var parser = new CommandParser(new ICommandConverter[]
{
new ArrayConverter<float>(),
new SimpleCommandConverter<float>()
});
var culture = CultureInfo.InvariantCulture;
new CropWebProcessor().Process(formattedImage, logger, commands, parser, culture);
Assert.AreEqual(width, image.Width);
Assert.AreEqual(height, image.Height);
}
}
}