Skip to content

Commit

Permalink
Added new tests for apply and crop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Riyad Kalla committed Nov 10, 2011
1 parent a41dadd commit 1f5a126
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/test/java/org/imgscalr/AbstractScalrTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.imgscalr;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;

public abstract class AbstractScalrTest {
protected static BufferedImage src;

@BeforeClass
public static void setup() throws IOException {
src = load("time-square.png");
}

@AfterClass
public static void tearDown() {
src.flush();
}

protected static BufferedImage load(String name) {
BufferedImage i = null;

try {
i = ImageIO.read(AbstractScalrTest.class.getResource(name));
} catch (Exception e) {
e.printStackTrace();
}

return i;
}

protected static void save(BufferedImage image, String name) {
try {
ImageIO.write(image, "PNG", new FileOutputStream(name));
} catch (Exception e) {
e.printStackTrace();
}
}

protected static void assertEquals(BufferedImage orig, BufferedImage tmp)
throws AssertionError {
// Ensure neither image is null.
Assert.assertNotNull(orig);
Assert.assertNotNull(tmp);

// Ensure dimensions are equal.
Assert.assertEquals(orig.getWidth(), tmp.getWidth());
Assert.assertEquals(orig.getHeight(), tmp.getHeight());

int w = orig.getWidth();
int h = orig.getHeight();

// Ensure every pixel RGB is the same.
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Assert.assertEquals(orig.getRGB(i, j), tmp.getRGB(i, j));
}
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/org/imgscalr/ScalrApplyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.imgscalr;

import static org.imgscalr.Scalr.OP_ANTIALIAS;
import static org.imgscalr.Scalr.OP_BRIGHTER;
import static org.imgscalr.Scalr.OP_DARKER;
import static org.imgscalr.Scalr.OP_GRAYSCALE;
import static org.imgscalr.Scalr.apply;

import java.awt.image.BufferedImageOp;

import org.junit.Assert;
import org.junit.Test;

public class ScalrApplyTest extends AbstractScalrTest {
@Test
public void testApplyEX() {
try {
apply(src, (BufferedImageOp[]) null);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}
}

@Test
public void testApply1() {
assertEquals(load("time-square-apply-1.png"), apply(src, OP_ANTIALIAS));
}

@Test
public void testApply4() {
assertEquals(
load("time-square-apply-4.png"),
apply(src, Scalr.OP_ANTIALIAS, OP_BRIGHTER, OP_DARKER,
OP_GRAYSCALE));
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/imgscalr/ScalrCropTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.imgscalr;

import static org.imgscalr.Scalr.crop;

import org.junit.Assert;
import org.junit.Test;

public class ScalrCropTest extends AbstractScalrTest {
@Test
public void testCropEX() {
try {
crop(src, 3200, 2400);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}

try {
crop(src, -8, -10, 100, 100);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}

try {
crop(src, -100, -200, -4, -4);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}
}

@Test
public void testCropWH() {
assertEquals(load("time-square-crop-wh.png"), crop(src, 320, 240));
}

@Test
public void testCropXYWH() {
assertEquals(load("time-square-crop-xywh.png"),
crop(src, 100, 100, 320, 240));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/org/imgscalr/time-square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1f5a126

Please sign in to comment.