summaryrefslogtreecommitdiffstats
path: root/java/test
diff options
context:
space:
mode:
Diffstat (limited to 'java/test')
-rw-r--r--java/test/TestImage.java175
-rw-r--r--java/test/TestImageScanner.java53
-rw-r--r--java/test/TestScanImage.java183
3 files changed, 411 insertions, 0 deletions
diff --git a/java/test/TestImage.java b/java/test/TestImage.java
new file mode 100644
index 0000000..9666f2b
--- /dev/null
+++ b/java/test/TestImage.java
@@ -0,0 +1,175 @@
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.After;
+import static org.junit.Assert.*;
+
+import net.sourceforge.zbar.Image;
+
+public class TestImage
+{
+ protected Image image;
+
+ @Before public void setUp ()
+ {
+ image = new Image();
+ }
+
+ @After public void tearDown ()
+ {
+ image.destroy();
+ image = null;
+ }
+
+
+ @Test public void creation ()
+ {
+ Image img0 = new Image(123, 456);
+ Image img1 = new Image("BGR3");
+ Image img2 = new Image(987, 654, "UYVY");
+
+ assertEquals(123, img0.getWidth());
+ assertEquals(456, img0.getHeight());
+ assertEquals(null, img0.getFormat());
+
+ assertEquals(0, img1.getWidth());
+ assertEquals(0, img1.getHeight());
+ assertEquals("BGR3", img1.getFormat());
+
+ assertEquals(987, img2.getWidth());
+ assertEquals(654, img2.getHeight());
+ assertEquals("UYVY", img2.getFormat());
+ }
+
+ @Test public void sequence ()
+ {
+ assertEquals(0, image.getSequence());
+ image.setSequence(42);
+ assertEquals(42, image.getSequence());
+ }
+
+ @Test public void size ()
+ {
+ assertEquals(0, image.getWidth());
+ assertEquals(0, image.getHeight());
+
+ image.setSize(640, 480);
+ int[] size0 = { 640, 480 };
+ assertArrayEquals(size0, image.getSize());
+
+ int[] size1 = { 320, 240 };
+ image.setSize(size1);
+ assertEquals(320, image.getWidth());
+ assertEquals(240, image.getHeight());
+ }
+
+ @Test public void crop ()
+ {
+ int[] zeros = { 0, 0, 0, 0 };
+ assertArrayEquals(zeros, image.getCrop());
+
+ image.setSize(123, 456);
+ int[] crop0 = { 0, 0, 123, 456 };
+ assertArrayEquals(crop0, image.getCrop());
+
+ image.setCrop(1, 2, 34, 56);
+ int[] crop1 = { 1, 2, 34, 56 };
+ assertArrayEquals(crop1, image.getCrop());
+
+ image.setCrop(-20, -20, 200, 500);
+ assertArrayEquals(crop0, image.getCrop());
+
+ int[] crop2 = { 7, 8, 90, 12};
+ image.setCrop(crop2);
+ assertArrayEquals(crop2, image.getCrop());
+
+ image.setSize(654, 321);
+ int[] crop3 = { 0, 0, 654, 321 };
+ assertArrayEquals(crop3, image.getCrop());
+
+ int[] crop4 = { -10, -10, 700, 400 };
+ image.setCrop(crop4);
+ assertArrayEquals(crop3, image.getCrop());
+ }
+
+ @Test public void format ()
+ {
+ assertNull(image.getFormat());
+ image.setFormat("Y800");
+ assertEquals("Y800", image.getFormat());
+ boolean gotException = false;
+ try {
+ image.setFormat("[]");
+ }
+ catch(IllegalArgumentException e) {
+ // expected
+ gotException = true;
+ }
+ assertTrue("Expected exception", gotException);
+ assertEquals("Y800", image.getFormat());
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void setFormatInvalid0 ()
+ {
+ image.setFormat(null);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void setFormatInvalid1 ()
+ {
+ image.setFormat("");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void setFormatInvalid2 ()
+ {
+ image.setFormat("YOMAMA");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void setFormatInvalid3 ()
+ {
+ image.setFormat("foo");
+ }
+
+ @Test public void data ()
+ {
+ assertNull(image.getData());
+
+ int[] ints = new int[24];
+ image.setData(ints);
+ assertSame(ints, image.getData());
+
+ byte[] bytes = new byte[280];
+ image.setData(bytes);
+ assertSame(bytes, image.getData());
+
+ image.setData((byte[])null);
+ assertNull(image.getData());
+ }
+
+ @Test public void convert ()
+ {
+ image.setSize(4, 4);
+ image.setFormat("RGB4");
+ int[] rgb4 = new int[16];
+ byte[] exp = new byte[16];
+ for(int i = 0; i < 16; i++) {
+ int c = i * 15;
+ rgb4[i] = c | (c << 8) | (c << 16) | (c << 24);
+ exp[i] = (byte)c;
+ }
+ image.setData(rgb4);
+
+ Image gray = image.convert("Y800");
+ assertEquals(4, gray.getWidth());
+ assertEquals(4, gray.getHeight());
+ assertEquals("Y800", gray.getFormat());
+
+ byte[] y800 = gray.getData();
+ assertEquals(16, y800.length);
+
+ assertArrayEquals(exp, y800);
+ }
+}
diff --git a/java/test/TestImageScanner.java b/java/test/TestImageScanner.java
new file mode 100644
index 0000000..0d8eccc
--- /dev/null
+++ b/java/test/TestImageScanner.java
@@ -0,0 +1,53 @@
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.After;
+import org.junit.Assert.*;
+
+import net.sourceforge.zbar.ImageScanner;
+import net.sourceforge.zbar.Config;
+
+public class TestImageScanner
+{
+ protected ImageScanner scanner;
+
+ @Before public void setUp ()
+ {
+ scanner = new ImageScanner();
+ }
+
+ @After public void tearDown ()
+ {
+ scanner.destroy();
+ scanner = null;
+ }
+
+
+ @Test public void creation ()
+ {
+ // create/destroy
+ }
+
+ @Test public void callSetConfig ()
+ {
+ scanner.setConfig(0, Config.X_DENSITY, 2);
+ scanner.setConfig(0, Config.Y_DENSITY, 4);
+ }
+
+ @Test public void callParseConfig ()
+ {
+ scanner.parseConfig("disable");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void callParseConfigInvalid ()
+ {
+ scanner.parseConfig("yomama");
+ }
+
+ @Test public void callEnableCache ()
+ {
+ scanner.enableCache(true);
+ scanner.enableCache(false);
+ }
+}
diff --git a/java/test/TestScanImage.java b/java/test/TestScanImage.java
new file mode 100644
index 0000000..86ed024
--- /dev/null
+++ b/java/test/TestScanImage.java
@@ -0,0 +1,183 @@
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.After;
+import static org.junit.Assert.*;
+
+import net.sourceforge.zbar.*;
+
+import java.text.CharacterIterator;
+import java.text.StringCharacterIterator;
+import java.util.Iterator;
+
+public class TestScanImage
+{
+ protected ImageScanner scanner;
+ protected Image image;
+
+ @Before public void setUp ()
+ {
+ scanner = new ImageScanner();
+ image = new Image();
+ }
+
+ @After public void tearDown ()
+ {
+ image = null;
+ scanner = null;
+ System.gc();
+ }
+
+ public static final String encoded_widths =
+ "9 111 212241113121211311141132 11111 311213121312121332111132 111 9";
+
+ protected void generateY800 ()
+ {
+ int width = 114, height = 85;
+ image.setSize(width, height);
+ image.setFormat("Y800");
+ int datalen = width * height;
+ byte[] data = new byte[datalen];
+
+ int y = 0;
+ int p = 0;
+ for(; y < 10 && y < height; y++)
+ for(int x = 0; x < width; x++)
+ data[p++] = -1;
+
+ for(; y < height - 10; y++) {
+ int x = 0;
+ byte color = -1;
+ CharacterIterator it = new StringCharacterIterator(encoded_widths);
+ for(char c = it.first();
+ c != CharacterIterator.DONE;
+ c = it.next())
+ {
+ if(c == ' ')
+ continue;
+ for(int dx = (int)c - 0x30; dx > 0; dx--) {
+ data[p++] = color;
+ x++;
+ }
+ color = (byte)~color;
+ }
+ for(; x < width; x++)
+ data[p++] = (byte)~color;
+ }
+
+ for(; y < height; y++)
+ for(int x = 0; x < width; x++)
+ data[p++] = -1;
+ assert(p == datalen);
+
+ image.setData(data);
+ }
+
+ protected void checkResults (SymbolSet syms)
+ {
+ assertNotNull(syms);
+ assert(syms.size() == 1);
+ Iterator<Symbol> it = syms.iterator();
+ assertTrue(it.hasNext());
+ Symbol sym = it.next();
+ assertNotNull(sym);
+ assertFalse(it.hasNext());
+
+ assertEquals(Symbol.EAN13, sym.getType());
+ assertEquals(sym.EAN13, sym.getType()); // cached
+
+ assertTrue(sym.getQuality() > 1);
+ assertEquals(0, sym.getCount());
+
+ SymbolSet comps = sym.getComponents();
+ assertNotNull(comps);
+ assertEquals(0, comps.size());
+ it = comps.iterator();
+ assertNotNull(it);
+ assertFalse(it.hasNext());
+
+ String data = sym.getData();
+ assertEquals("6268964977804", data);
+
+ byte[] bytes = sym.getDataBytes();
+ byte[] exp = { '6','2','6','8','9','6','4','9','7','7','8','0','4' };
+ assertArrayEquals(exp, bytes);
+
+ int[] r = sym.getBounds();
+ assertTrue(r[0] > 6);
+ assertTrue(r[1] > 6);
+ assertTrue(r[2] < 102);
+ assertTrue(r[3] < 73);
+
+ assertEquals(Orientation.UP, sym.getOrientation());
+ }
+
+ @Test public void generated ()
+ {
+ generateY800();
+ int n = scanner.scanImage(image);
+ assertEquals(1, n);
+
+ checkResults(image.getSymbols());
+ checkResults(scanner.getResults());
+ }
+
+ @Test public void config ()
+ {
+ generateY800();
+ scanner.setConfig(Symbol.EAN13, Config.ENABLE, 0);
+ int n = scanner.scanImage(image);
+ assertEquals(0, n);
+ }
+
+ @Test public void cache ()
+ {
+ generateY800();
+ scanner.enableCache(true);
+
+ int n = 0;
+ for(int i = 0; i < 10; i++) {
+ n = scanner.scanImage(image);
+ if(n > 0) {
+ assertTrue(i > 1);
+ break;
+ }
+ }
+
+ assertEquals(1, n);
+ checkResults(scanner.getResults());
+ }
+
+ @Test public void orientation()
+ {
+ generateY800();
+
+ // flip the image
+ int width = image.getWidth();
+ int height = image.getHeight();
+ byte[] data = image.getData();
+ int p = 0;
+ for(int y = 0; y < height; y++) {
+ for(int x0 = 0; x0 < width / 2; x0++) {
+ int x1 = width - x0 - 1;
+ assert(x0 < x1);
+ byte b = data[p + x0];
+ data[p + x0] = data[p + x1];
+ data[p + x1] = b;
+ }
+ p += width;
+ }
+ image.setData(data);
+
+ int n = scanner.scanImage(image);
+ assertEquals(1, n);
+
+ SymbolSet syms = scanner.getResults();
+ assert(syms.size() == 1);
+ for(Symbol sym : syms) {
+ assertEquals(Symbol.EAN13, sym.getType());
+ assertEquals("6268964977804", sym.getData());
+ assertEquals(Orientation.DOWN, sym.getOrientation());
+ }
+ }
+}