Skip to content

Commit 2ce2a7e

Browse files
author
Amir Tocker
committed
Add tests for auto width and original width and height ( "ow", "oh") values
1 parent f68866b commit 2ce2a7e

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

cloudinary-core/src/main/java/com/cloudinary/Transformation.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@ public String generate(Map options) {
469469
String angle = StringUtils.join(ObjectUtils.asArray(options.get("angle")), ".");
470470

471471
boolean noHtmlSizes = hasLayer || StringUtils.isNotBlank(angle) || "fit".equals(crop) || "limit".equals(crop);
472-
if (width != null && (width.equals("auto") || Float.parseFloat(width) < 1 || noHtmlSizes || isResponsive)) {
472+
if (width != null && (width.startsWith("auto") || !isValidAttrValue(width) || noHtmlSizes || isResponsive)) {
473473
this.htmlWidth = null;
474474
}
475-
if (height != null && (Float.parseFloat(height) < 1 || noHtmlSizes || isResponsive)) {
475+
if (height != null && (!isValidAttrValue(height) || noHtmlSizes || isResponsive)) {
476476
this.htmlHeight = null;
477477
}
478478

@@ -607,6 +607,21 @@ public String generate(Map options) {
607607
return StringUtils.join(transformations, "/");
608608
}
609609

610+
/**
611+
* Check if the value is a float >= 1
612+
* @param value
613+
* @return true if the value is a float >= 1
614+
*/
615+
private boolean isValidAttrValue(String value) {
616+
final float parseFloat;
617+
try {
618+
parseFloat = Float.parseFloat(value);
619+
} catch (NumberFormatException e) {
620+
return false;
621+
}
622+
return parseFloat >= 1;
623+
}
624+
610625
public String getHtmlWidth() {
611626
return htmlWidth;
612627
}

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,20 @@ public void testVariousOptions() {
272272
assertEquals(DEFAULT_UPLOAD_PATH + "g_center,p_a,q_0.4,r_3,x_1,y_2/test", result);
273273
}
274274

275+
@Test
276+
public void testQuality() {
277+
// should use x, y, radius, prefix, gravity and quality from options
278+
Transformation transformation = new Transformation().quality(0.4);
279+
String result = cloudinary.url().transformation(transformation).generate("test");
280+
assertEquals(DEFAULT_UPLOAD_PATH + "q_0.4/test", result);
281+
transformation = new Transformation().quality("auto");
282+
result = cloudinary.url().transformation(transformation).generate("test");
283+
assertEquals(DEFAULT_UPLOAD_PATH + "q_auto/test", result);
284+
transformation = new Transformation().quality("auto:good");
285+
result = cloudinary.url().transformation(transformation).generate("test");
286+
assertEquals(DEFAULT_UPLOAD_PATH + "q_auto:good/test", result);
287+
}
288+
275289
@Test
276290
public void testTransformationSimple() {
277291
// should support named transformation
@@ -576,6 +590,28 @@ public void testResponsiveWidth() {
576590
assertEquals(DEFAULT_UPLOAD_PATH + "c_crop,h_100,w_100/c_pad,w_auto/test", result);
577591
Transformation.setResponsiveWidthTransformation(null);
578592
}
593+
@Test
594+
public void testShouldSupportAutoWidth(){
595+
String trans;
596+
597+
trans = new Transformation().width("auto:20").crop("fill").generate();
598+
assertEquals("c_fill,w_auto:20", trans);
599+
trans = new Transformation().width("auto:20:350").crop("fill").generate();
600+
assertEquals("c_fill,w_auto:20:350", trans);
601+
trans = new Transformation().width("auto:breakpoints").crop("fill").generate();
602+
assertEquals("c_fill,w_auto:breakpoints", trans);
603+
trans = new Transformation().width("auto:breakpoints_100_1900_20_15").crop("fill").generate();
604+
assertEquals("c_fill,w_auto:breakpoints_100_1900_20_15", trans);
605+
trans = new Transformation().width("auto:breakpoints:json").crop("fill").generate();
606+
assertEquals("c_fill,w_auto:breakpoints:json", trans);
607+
}
608+
609+
610+
@Test
611+
public void testShouldSupportOhOw(){
612+
String trans = new Transformation().width("ow").height("oh").crop("crop").generate();
613+
assertEquals("c_crop,h_oh,w_ow", trans);
614+
}
579615

580616
@Test
581617
public void testVideoCodec() {

0 commit comments

Comments
 (0)