2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
2022-04-29 13:16:24 +02:00
|
|
|
using Umbraco.Cms.Web.Common.Media;
|
2020-02-08 11:05:14 -08:00
|
|
|
|
2022-04-29 13:16:24 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.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-12-20 08:36:11 +01:00
|
|
|
private static readonly ImageUrlGenerationOptions.CropCoordinates s_crop = new ImageUrlGenerationOptions.CropCoordinates(0.58729977382575338m, 0.055768992440203169m, 0m, 0.32457553600198386m);
|
2021-08-10 17:20:03 +02:00
|
|
|
private static readonly ImageUrlGenerationOptions.FocalPointPosition s_focus1 = new ImageUrlGenerationOptions.FocalPointPosition(0.96m, 0.80827067669172936m);
|
|
|
|
|
private static readonly ImageUrlGenerationOptions.FocalPointPosition s_focus2 = new ImageUrlGenerationOptions.FocalPointPosition(0.4275m, 0.41m);
|
2021-08-13 15:53:36 +02:00
|
|
|
private static readonly ImageSharpImageUrlGenerator s_generator = new ImageSharpImageUrlGenerator(new string[0]);
|
2020-02-08 11:05:14 -08:00
|
|
|
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_CropAliasTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Crop = s_crop, Width = 100, Height = 100 });
|
2021-08-10 17:17:40 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?cc=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&width=100&height=100", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_WidthHeightTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus1, Width = 200, Height = 300 });
|
2021-08-10 17:17:40 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=200&height=300", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_FocalPointTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus1, Width = 100, Height = 100 });
|
2021-08-10 17:17:40 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=100&height=100", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrlFurtherOptionsTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus1, Width = 200, Height = 300, FurtherOptions = "&filter=comic&roundedcorners=radius-26|bgcolor-fff" });
|
2022-04-29 13:16:24 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=200&height=300&filter=comic&roundedcorners=radius-26%7Cbgcolor-fff", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-09 15:52:55 +02:00
|
|
|
/// Test that if options is null, the generated image URL is also null.
|
2020-02-08 11:05:14 -08:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrlNullOptionsTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(null);
|
2020-02-08 11:05:14 -08:00
|
|
|
Assert.AreEqual(null, urlString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-04-29 13:16:24 +02:00
|
|
|
/// Test that if the image URL is null, the generated image URL is also null.
|
2020-02-08 11:05:14 -08:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrlNullTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(null));
|
2022-04-29 13:16:24 +02:00
|
|
|
Assert.AreEqual(null, urlString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test that if the image URL is empty, the generated image URL is empty.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetImageUrlEmptyTest()
|
|
|
|
|
{
|
|
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty));
|
2021-08-09 15:52:55 +02:00
|
|
|
Assert.AreEqual(string.Empty, urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-04-29 13:16:24 +02:00
|
|
|
/// Test the GetImageUrl method on the ImageCropDataSet Model
|
2020-02-08 11:05:14 -08:00
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetBaseCropUrlFromModelTest()
|
|
|
|
|
{
|
2022-04-29 13:16:24 +02:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty) { Crop = s_crop, Width = 100, Height = 100 });
|
2021-08-10 17:20:03 +02:00
|
|
|
Assert.AreEqual("?cc=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&width=100&height=100", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test that if Crop mode is specified as anything other than Crop the image doesn't use the crop
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_SpecifiedCropModeTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlStringMin = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Min, Width = 300, Height = 150 });
|
|
|
|
|
var urlStringBoxPad = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.BoxPad, Width = 300, Height = 150 });
|
|
|
|
|
var urlStringPad = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 300, Height = 150 });
|
|
|
|
|
var urlStringMax = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Max, Width = 300, Height = 150 });
|
|
|
|
|
var urlStringStretch = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Stretch, Width = 300, Height = 150 });
|
2020-02-08 11:05:14 -08:00
|
|
|
|
2021-08-06 14:44:16 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rmode=min&width=300&height=150", urlStringMin);
|
|
|
|
|
Assert.AreEqual(MediaPath + "?rmode=boxpad&width=300&height=150", urlStringBoxPad);
|
|
|
|
|
Assert.AreEqual(MediaPath + "?rmode=pad&width=300&height=150", urlStringPad);
|
|
|
|
|
Assert.AreEqual(MediaPath + "?rmode=max&width=300&height=150", urlStringMax);
|
|
|
|
|
Assert.AreEqual(MediaPath + "?rmode=stretch&width=300&height=150", urlStringStretch);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test for upload property type
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_UploadTypeTest()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Crop, ImageCropAnchor = ImageCropAnchor.Center, Width = 100, Height = 270 });
|
2021-08-06 14:44:16 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rmode=crop&ranchor=center&width=100&height=270", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test for preferFocalPoint when focal point is centered
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_PreferFocalPointCenter()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2021-08-09 15:52:55 +02:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Width = 300, Height = 150 });
|
|
|
|
|
Assert.AreEqual(MediaPath + "?width=300&height=150", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test to check if crop ratio is ignored if useCropDimensions is true
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus2, Width = 270, Height = 161 });
|
2021-08-10 17:17:40 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rxy=0.4275,0.41&width=270&height=161", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test to check result when only a width parameter is passed, effectivly a resize only
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_WidthOnlyParameter()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2021-08-09 15:52:55 +02:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Width = 200 });
|
|
|
|
|
Assert.AreEqual(MediaPath + "?width=200", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test to check result when only a height parameter is passed, effectivly a resize only
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_HeightOnlyParameter()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2021-08-09 15:52:55 +02:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Height = 200 });
|
|
|
|
|
Assert.AreEqual(MediaPath + "?height=200", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test to check result when using a background color with padding
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
2022-04-29 13:16:24 +02:00
|
|
|
public void GetImageUrl_BackgroundColorParameter()
|
2020-02-08 11:05:14 -08:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 400, Height = 400, FurtherOptions = "&bgcolor=fff" });
|
2021-08-06 14:44:16 +02:00
|
|
|
Assert.AreEqual(MediaPath + "?rmode=pad&width=400&height=400&bgcolor=fff", urlString);
|
2020-02-08 11:05:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|