2014-03-20 12:22:50 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2014-04-21 08:53:40 +01:00
|
|
|
|
using Umbraco.Web.Models;
|
2014-03-20 12:22:50 +00:00
|
|
|
|
using Umbraco.Web.PropertyEditors;
|
|
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.PropertyEditors
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class ImageCropperTest
|
|
|
|
|
|
{
|
2014-03-24 16:32:50 +00:00
|
|
|
|
private const string cropperJson = "{\"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}}]}";
|
|
|
|
|
|
|
|
|
|
|
|
private const string mediaPath = "/media/1005/img_0671.jpg";
|
|
|
|
|
|
|
2014-03-20 12:22:50 +00:00
|
|
|
|
[Test]
|
2014-03-24 16:32:50 +00:00
|
|
|
|
public void GetCropUrl_CropAliasTest()
|
2014-03-20 12:22:50 +00:00
|
|
|
|
{
|
2014-03-24 19:27:00 +00:00
|
|
|
|
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "Thumb", useCropDimensions: true);
|
2014-03-24 16:32:50 +00:00
|
|
|
|
Assert.AreEqual(mediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
|
|
|
|
|
|
}
|
2014-03-20 12:22:50 +00:00
|
|
|
|
|
2014-03-24 16:32:50 +00:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_WidthHeightTest()
|
|
|
|
|
|
{
|
2014-03-24 19:27:00 +00:00
|
|
|
|
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300);
|
2014-03-24 16:32:50 +00:00
|
|
|
|
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300", urlString);
|
|
|
|
|
|
}
|
2014-03-20 12:22:50 +00:00
|
|
|
|
|
2014-03-24 16:32:50 +00:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_FocalPointTest()
|
|
|
|
|
|
{
|
2014-03-24 19:27:00 +00:00
|
|
|
|
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "thumb", preferFocalPoint: true, useCropDimensions: true);
|
2014-03-24 16:32:50 +00:00
|
|
|
|
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=100&height=100", urlString);
|
2014-03-20 12:22:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-24 17:51:52 +00:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrlFurtherOptionsTest()
|
|
|
|
|
|
{
|
2014-03-24 19:27:00 +00:00
|
|
|
|
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff");
|
2014-03-24 17:51:52 +00:00
|
|
|
|
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-04-21 08:38:53 +01:00
|
|
|
|
/// <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(imageCropperValue: cropperJson, cropAlias: "Banner", useCropDimensions: true);
|
|
|
|
|
|
Assert.AreEqual(null, urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the GetCropUrl method on the ImageCropDataSet Model
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetBaseCropUrlFromModelTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
var cropDataSet = cropperJson.SerializeToCropDataSet();
|
|
|
|
|
|
var urlString = cropDataSet.GetCropUrl("thumb");
|
|
|
|
|
|
Assert.AreEqual("?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=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(imageCropperValue: cropperJson, cropAlias: "Thumb", useCropDimensions: true, ratioMode:ImageCropRatioMode.Height);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&heightratio=1", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the height ratio mode with manual width/height dimensions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_WidthHeightRatioModeTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 300, height: 150, ratioMode:ImageCropRatioMode.Height);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=300&heightratio=0.5", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test the height ratio mode with width/height dimensions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_HeightWidthRatioModeTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 300, height: 150, ratioMode: ImageCropRatioMode.Width);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&height=150&widthratio=2", urlString);
|
|
|
|
|
|
}
|
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 urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 300, height: 150, imageCropMode:ImageCropMode.Max);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?mode=max&width=300&height=150", urlString);
|
|
|
|
|
|
}
|
2014-04-27 22:17:24 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test for upload property type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_UploadTypeTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
var urlString = mediaPath.GetCropUrl(width: 100, height: 270, imageCropMode: ImageCropMode.Crop, imageCropAnchor: ImageCropAnchor.Center);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?mode=crop&anchor=center&width=100&height=270", urlString);
|
|
|
|
|
|
}
|
2015-02-27 14:42:00 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test for preferFocalPoint when focal point is centered
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_PreferFocalPointCenter()
|
|
|
|
|
|
{
|
|
|
|
|
|
var 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(imageCropperValue: cropperJson, width: 300, height: 150, preferFocalPoint:true);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?anchor=center&mode=crop&width=300&height=150", urlString);
|
|
|
|
|
|
}
|
2015-08-19 16:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidth()
|
|
|
|
|
|
{
|
|
|
|
|
|
var 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(imageCropperValue: cropperJson, cropAlias: "home", width: 200);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?anchor=center&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPoint()
|
|
|
|
|
|
{
|
|
|
|
|
|
var 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(imageCropperValue: cropperJson, cropAlias: "home", width: 200);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?center=0.41,0.4275&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetCropUrl_PreDefinedCropNoCoordinatesWithHeight()
|
|
|
|
|
|
{
|
|
|
|
|
|
var 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(imageCropperValue: cropperJson, cropAlias: "home", height: 200);
|
|
|
|
|
|
Assert.AreEqual(mediaPath + "?anchor=center&mode=crop&widthratio=1.6770186335403726708074534161&height=200", urlString);
|
|
|
|
|
|
}
|
2014-03-20 12:22:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|