Files
Umbraco-CMS/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ImageCropperTest.cs

406 lines
21 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
2018-01-26 17:55:20 +01:00
using Umbraco.Core.PropertyEditors.ValueConverters;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
{
[TestFixture]
public class ImageCropperTest
{
2017-07-20 11:21:28 +02:00
private const string CropperJson1 = "{\"focalPoint\": {\"left\": 0.96,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}";
2016-06-30 16:39:05 +02:00
private const string CropperJson2 = "{\"focalPoint\": {\"left\": 0.98,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0672.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}";
private const string CropperJson3 = "{\"focalPoint\": {\"left\": 0.98,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0672.jpg\",\"crops\": []}";
private const string MediaPath = "/media/1005/img_0671.jpg";
[Test]
public void CanConvertImageCropperDataSetSrcToString()
{
// cropperJson3 - has not crops
ImageCropperValue cropperValue = CropperJson3.DeserializeImageCropperValue();
Attempt<string> serialized = cropperValue.TryConvertTo<string>();
Assert.IsTrue(serialized.Success);
Assert.AreEqual("/media/1005/img_0672.jpg", serialized.Result);
}
[Test]
public void CanConvertImageCropperDataSetJObject()
{
// cropperJson3 - has not crops
ImageCropperValue cropperValue = CropperJson3.DeserializeImageCropperValue();
Attempt<JObject> serialized = cropperValue.TryConvertTo<JObject>();
Assert.IsTrue(serialized.Success);
Assert.AreEqual(cropperValue, serialized.Result.ToObject<ImageCropperValue>());
}
[Test]
public void CanConvertImageCropperDataSetJsonToString()
{
ImageCropperValue cropperValue = CropperJson1.DeserializeImageCropperValue();
Attempt<string> serialized = cropperValue.TryConvertTo<string>();
Assert.IsTrue(serialized.Success);
Assert.IsTrue(serialized.Result.DetectIsJson());
ImageCropperValue obj = JsonConvert.DeserializeObject<ImageCropperValue>(CropperJson1, new JsonSerializerSettings { Culture = CultureInfo.InvariantCulture, FloatParseHandling = FloatParseHandling.Decimal });
Assert.AreEqual(cropperValue, obj);
}
//// [TestCase(CropperJson1, CropperJson1, true)]
//// [TestCase(CropperJson1, CropperJson2, false)]
//// public void CanConvertImageCropperPropertyEditor(string val1, string val2, bool expected)
//// {
//// try
//// {
//// var container = TestHelper.GetRegister();
//// var composition = new Composition(container, TestHelper.GetMockedTypeLoader(), Mock.Of<IProfilingLogger>(), ComponentTests.MockRuntimeState(RuntimeLevel.Run), TestHelper.GetConfigs(), TestHelper.IOHelper, AppCaches.NoCache);
////
//// composition.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>();
////
//// Current.Factory = composition.CreateFactory();
////
//// var logger = Mock.Of<ILogger>();
//// var scheme = Mock.Of<IMediaPathScheme>();
//// var shortStringHelper = Mock.Of<IShortStringHelper>();
////
//// var mediaFileSystem = new MediaFileSystem(Mock.Of<IFileSystem>(), scheme, logger, shortStringHelper);
////
//// var dataTypeService = new TestObjects.TestDataTypeService(
//// new DataType(new ImageCropperPropertyEditor(Mock.Of<ILogger>(), mediaFileSystem, Mock.Of<IContentSettings>(), Mock.Of<IDataTypeService>(), Mock.Of<ILocalizationService>(), TestHelper.IOHelper, TestHelper.ShortStringHelper, Mock.Of<ILocalizedTextService>())) { Id = 1 });
////
//// var factory = new PublishedContentTypeFactory(Mock.Of<IPublishedModelFactory>(), new PropertyValueConverterCollection(Array.Empty<IPropertyValueConverter>()), dataTypeService);
////
//// var converter = new ImageCropperValueConverter();
//// var result = converter.ConvertSourceToIntermediate(null, factory.CreatePropertyType("test", 1), val1, false); // does not use type for conversion
////
//// var resultShouldMatch = val2.DeserializeImageCropperValue();
//// if (expected)
//// {
//// Assert.AreEqual(resultShouldMatch, result);
//// }
//// else
//// {
//// Assert.AreNotEqual(resultShouldMatch, result);
//// }
//// }
//// finally
//// {
//// Current.Reset();
//// }
//// }
[Test]
public void GetCropUrl_CropAliasTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
/// <summary>
/// Test to ensure useCropDimensions is observed
/// </summary>
[Test]
public void GetCropUrl_CropAliasIgnoreWidthHeightTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true, width: 50, height: 50);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
[Test]
public void GetCropUrl_WidthHeightTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&w=200&h=300", urlString);
}
[Test]
public void GetCropUrl_FocalPointTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "thumb", preferFocalPoint: true, useCropDimensions: true);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&w=100&h=100", urlString);
}
[Test]
public void GetCropUrlFurtherOptionsTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff");
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&w=200&h=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
}
/// <summary>
/// Test that if a crop alias has been specified that doesn't exist the method returns null
/// </summary>
[Test]
public void GetCropUrlNullTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Banner", useCropDimensions: true);
Assert.AreEqual(null, urlString);
}
/// <summary>
/// Test the GetCropUrl method on the ImageCropDataSet Model
/// </summary>
[Test]
public void GetBaseCropUrlFromModelTest()
{
ImageCropperValue cropDataSet = CropperJson1.DeserializeImageCropperValue();
var urlString = cropDataSet.GetCropUrl("thumb", new TestImageUrlGenerator());
2020-02-08 11:05:14 -08:00
Assert.AreEqual("?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
2014-04-21 08:53:40 +01:00
/// <summary>
/// Test the height ratio mode with predefined crop dimensions
/// </summary>
[Test]
public void GetCropUrl_CropAliasHeightRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true, ratioMode: ImageCropRatioMode.Height);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&hr=1&w=100", urlString);
2014-04-21 08:53:40 +01:00
}
/// <summary>
/// Test the height ratio mode with manual width/height dimensions
/// </summary>
[Test]
public void GetCropUrl_WidthHeightRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Height);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&hr=0.5&w=300", urlString);
2014-04-21 08:53:40 +01:00
}
/// <summary>
/// Test the height ratio mode with width/height dimensions
/// </summary>
[Test]
public void GetCropUrl_HeightWidthRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Width);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.80827067669172936x0.96&wr=2&h=150", urlString);
2014-04-21 08:53:40 +01:00
}
2014-04-27 22:06:48 +01:00
/// <summary>
/// Test that if Crop mode is specified as anything other than Crop the image doesn't use the crop
/// </summary>
[Test]
public void GetCropUrl_SpecifiedCropModeTest()
{
var urlStringMin = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode: ImageCropMode.Min);
var urlStringBoxPad = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode: ImageCropMode.BoxPad);
var urlStringPad = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode: ImageCropMode.Pad);
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode: ImageCropMode.Max);
var urlStringStretch = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, imageCropMode: ImageCropMode.Stretch);
2017-07-20 11:21:28 +02:00
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=min&w=300&h=150", urlStringMin);
Assert.AreEqual(MediaPath + "?m=boxpad&w=300&h=150", urlStringBoxPad);
Assert.AreEqual(MediaPath + "?m=pad&w=300&h=150", urlStringPad);
Assert.AreEqual(MediaPath + "?m=max&w=300&h=150", urlString);
Assert.AreEqual(MediaPath + "?m=stretch&w=300&h=150", urlStringStretch);
2014-04-27 22:06:48 +01:00
}
/// <summary>
/// Test for upload property type
/// </summary>
[Test]
public void GetCropUrl_UploadTypeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), width: 100, height: 270, imageCropMode: ImageCropMode.Crop, imageCropAnchor: ImageCropAnchor.Center);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=crop&a=center&w=100&h=270", urlString);
}
/// <summary>
/// Test for preferFocalPoint when focal point is centered
/// </summary>
[Test]
public void GetCropUrl_PreferFocalPointCenter()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, width: 300, height: 150, preferFocalPoint: true);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=defaultcrop&w=300&h=150", urlString);
}
/// <summary>
/// Test to check if height ratio is returned for a predefined crop without coordinates and focal point in centre when a width parameter is passed
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidth()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", width: 200);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=defaultcrop&hr=0.5962962962962962962962962963&w=200", urlString);
}
/// <summary>
/// Test to check if height ratio is returned for a predefined crop without coordinates and focal point is custom when a width parameter is passed
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPoint()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", width: 200);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.41x0.4275&hr=0.5962962962962962962962962963&w=200", urlString);
}
/// <summary>
/// Test to check if crop ratio is ignored if useCropDimensions is true
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", width: 200, useCropDimensions: true);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?f=0.41x0.4275&w=270&h=161", urlString);
}
/// <summary>
/// Test to check if width ratio is returned for a predefined crop without coordinates and focal point in centre when a height parameter is passed
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithHeight()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, cropAlias: "home", height: 200);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=defaultcrop&wr=1.6770186335403726708074534161&h=200", urlString);
}
/// <summary>
/// Test to check result when only a width parameter is passed, effectivly a resize only
/// </summary>
[Test]
public void GetCropUrl_WidthOnlyParameter()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, width: 200);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=defaultcrop&w=200", urlString);
}
/// <summary>
/// Test to check result when only a height parameter is passed, effectivly a resize only
/// </summary>
[Test]
public void GetCropUrl_HeightOnlyParameter()
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: cropperJson, height: 200);
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=defaultcrop&h=200", urlString);
}
2017-05-12 14:49:44 +02:00
/// <summary>
/// Test to check result when using a background color with padding
/// </summary>
[Test]
public void GetCropUrl_BackgroundColorParameter()
{
var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"" + MediaPath + "\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), 400, 400, cropperJson, imageCropMode: ImageCropMode.Pad, furtherOptions: "&bgcolor=fff");
2020-02-08 11:05:14 -08:00
Assert.AreEqual(MediaPath + "?m=pad&w=400&h=400&bgcolor=fff", urlString);
2017-05-12 14:49:44 +02:00
}
2020-02-08 11:05:14 -08:00
internal class TestImageUrlGenerator : IImageUrlGenerator
{
public IEnumerable<string> SupportedImageFileTypes => new[] { "jpeg", "jpg", "gif", "bmp", "png", "tiff", "tif" };
public string GetImageUrl(ImageUrlGenerationOptions options)
{
var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);
if (options.FocalPoint != null)
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl.Append("?f=");
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture));
2020-02-08 11:05:14 -08:00
imageProcessorUrl.Append("x");
imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
}
else if (options.Crop != null)
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl.Append("?c=");
imageProcessorUrl.Append(options.Crop.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.Crop.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.Crop.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.Crop.Y2.ToString(CultureInfo.InvariantCulture));
}
else if (options.DefaultCrop)
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl.Append("?m=defaultcrop");
}
else
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl.Append("?m=" + options.ImageCropMode.ToString().ToLower());
if (options.ImageCropAnchor != null)
{
imageProcessorUrl.Append("&a=" + options.ImageCropAnchor.ToString().ToLower());
}
}
2020-02-08 11:05:14 -08:00
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&f=");
if (options.Quality != null && hasFormat == false)
{
imageProcessorUrl.Append("&q=" + options.Quality);
}
if (options.HeightRatio != null)
{
imageProcessorUrl.Append("&hr=" + options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (options.WidthRatio != null)
{
imageProcessorUrl.Append("&wr=" + options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (options.Width != null)
{
imageProcessorUrl.Append("&w=" + options.Width);
}
if (options.Height != null)
{
imageProcessorUrl.Append("&h=" + options.Height);
}
if (options.UpScale == false)
{
imageProcessorUrl.Append("&u=no");
}
if (options.AnimationProcessMode != null)
{
imageProcessorUrl.Append("&apm=" + options.AnimationProcessMode);
}
if (options.FurtherOptions != null)
{
imageProcessorUrl.Append(options.FurtherOptions);
}
if (options.Quality != null && hasFormat)
{
imageProcessorUrl.Append("&q=" + options.Quality);
}
if (options.CacheBusterValue != null)
{
imageProcessorUrl.Append("&r=").Append(options.CacheBusterValue);
}
return imageProcessorUrl.ToString();
}
2017-05-12 14:49:44 +02:00
}
}
2017-07-20 11:21:28 +02:00
}