2020-09-28 15:43:28 +02:00
|
|
|
|
using NUnit.Framework;
|
2020-02-08 11:05:14 -08:00
|
|
|
|
using Umbraco.Core.Models;
|
2020-03-24 09:37:46 +01:00
|
|
|
|
using Umbraco.Infrastructure.Media;
|
2020-05-20 17:39:07 +02:00
|
|
|
|
using Umbraco.Web.Models;
|
2020-02-08 11:05:14 -08:00
|
|
|
|
|
2020-10-20 14:49:52 +02:00
|
|
|
|
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Media
|
2020-02-08 11:05:14 -08:00
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2020-10-20 14:49:52 +02:00
|
|
|
|
public class ImageSharpImageUrlGeneratorTests
|
2020-02-08 11:05:14 -08:00
|
|
|
|
{
|
|
|
|
|
|
private const string MediaPath = "/media/1005/img_0671.jpg";
|
2020-02-09 11:12:29 -08:00
|
|
|
|
private static readonly ImageUrlGenerationOptions.CropCoordinates Crop = new ImageUrlGenerationOptions.CropCoordinates(0.58729977382575338m, 0.055768992440203169m, 0m, 0.32457553600198386m);
|
|
|
|
|
|
private static readonly ImageUrlGenerationOptions.FocalPointPosition Focus1 = new ImageUrlGenerationOptions.FocalPointPosition(0.80827067669172936m, 0.96m);
|
|
|
|
|
|
private static readonly ImageUrlGenerationOptions.FocalPointPosition Focus2 = new ImageUrlGenerationOptions.FocalPointPosition(0.41m, 0.4275m);
|
2020-03-30 21:27:35 +02:00
|
|
|
|
private static readonly ImageSharpImageUrlGenerator Generator = new ImageSharpImageUrlGenerator();
|
2020-02-08 11:05:14 -08:00
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_CropAliasTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Crop = Crop, Width = 100, Height = 100 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_WidthHeightTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus1, Width = 200, Height = 300 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_FocalPointTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus1, Width = 100, Height = 100 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=100&height=100", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrlFurtherOptionsTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus1, Width = 200, Height = 300, FurtherOptions = "&filter=comic&roundedcorners=radius-26|bgcolor-fff" });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=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 = Generator.GetImageUrl(null);
|
|
|
|
|
|
Assert.AreEqual(null, urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test that if a crop alias has been specified that doesn't exist the method returns null
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrlEmptyTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(null));
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual("?mode=crop", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the GetCropUrl method on the ImageCropDataSet Model
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetBaseCropUrlFromModelTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(null) { Crop = Crop, Width = 100, Height = 100 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual("?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the height ratio mode with predefined crop dimensions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_CropAliasHeightRatioModeTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Crop = Crop, Width = 100, HeightRatio = 1 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&heightratio=1&width=100", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the height ratio mode with manual width/height dimensions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_WidthHeightRatioModeTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus1, Width = 300, HeightRatio = 0.5m });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&heightratio=0.5&width=300", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the height ratio mode with width/height dimensions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_HeightWidthRatioModeTest()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus1, Height = 150, WidthRatio = 2 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.80827067669172936,0.96&mode=crop&widthratio=2&height=150", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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()
|
|
|
|
|
|
{
|
2020-05-20 17:39:07 +02:00
|
|
|
|
var urlStringMin = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Min, Width = 300, Height = 150 });
|
|
|
|
|
|
var urlStringBoxPad = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.BoxPad, Width = 300, Height = 150 });
|
|
|
|
|
|
var urlStringPad = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 300, Height = 150 });
|
|
|
|
|
|
var urlStringMax = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Max, Width = 300, Height = 150 });
|
|
|
|
|
|
var urlStringStretch = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Stretch, Width = 300, Height = 150 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=min&width=300&height=150", urlStringMin);
|
|
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=boxpad&width=300&height=150", urlStringBoxPad);
|
|
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=pad&width=300&height=150", urlStringPad);
|
|
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=max&width=300&height=150", urlStringMax);
|
|
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=stretch&width=300&height=150", urlStringStretch);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test for upload property type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_UploadTypeTest()
|
|
|
|
|
|
{
|
2020-05-20 17:39:07 +02:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Crop, ImageCropAnchor = ImageCropAnchor.Center, Width = 100, Height = 270 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=crop&anchor=center&width=100&height=270", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test for preferFocalPoint when focal point is centered
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_PreferFocalPointCenter()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { DefaultCrop = true, Width = 300, Height = 150 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&width=300&height=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()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { DefaultCrop = true, Width = 200, HeightRatio = 0.5962962962962962962962962963m });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&heightratio=0.5962962962962962962962962963&width=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()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus2, Width = 200, HeightRatio = 0.5962962962962962962962962963m });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.41,0.4275&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test to check if crop ratio is ignored if useCropDimensions is true
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = Focus2, Width = 270, Height = 161 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?center=0.41,0.4275&mode=crop&width=270&height=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()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { DefaultCrop = true, Height = 200, WidthRatio = 1.6770186335403726708074534161m });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&widthratio=1.6770186335403726708074534161&height=200", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test to check result when only a width parameter is passed, effectivly a resize only
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_WidthOnlyParameter()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { DefaultCrop = true, Width = 200 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&width=200", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test to check result when only a height parameter is passed, effectivly a resize only
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_HeightOnlyParameter()
|
|
|
|
|
|
{
|
2020-02-09 11:12:29 -08:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { DefaultCrop = true, Height = 200 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?anchor=center&mode=crop&height=200", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test to check result when using a background color with padding
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_BackgroundColorParameter()
|
|
|
|
|
|
{
|
2020-05-20 17:39:07 +02:00
|
|
|
|
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 400, Height = 400, FurtherOptions = "&bgcolor=fff" });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
Assert.AreEqual(MediaPath + "?mode=pad&width=400&height=400&bgcolor=fff", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|