- Got the DataMatrix decoder compiling again with a quick bandaid.
[zxing.git] / core / test / src / com / google / zxing / common / AbstractBlackBoxTestCase.java
index fec39a0..19dc156 100644 (file)
 package com.google.zxing.common;
 
 import com.google.zxing.BarcodeFormat;
+import com.google.zxing.BinaryBitmap;
 import com.google.zxing.DecodeHintType;
-import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.LuminanceSource;
 import com.google.zxing.Reader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
-import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
+import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
+
 import junit.framework.TestCase;
 
-import javax.imageio.ImageIO;
 import java.awt.geom.AffineTransform;
 import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
@@ -35,10 +36,12 @@ import java.io.FileInputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
-import java.util.ArrayList;
-import java.nio.charset.Charset;
+
+import javax.imageio.ImageIO;
 
 /**
  * @author Sean Owen
@@ -60,6 +63,35 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     }
   };
 
+  public static class SummaryResults {
+    private int totalFound;
+    private int totalMustPass;
+    private int totalTests;
+
+    public SummaryResults() {
+      totalFound = 0;
+      totalMustPass = 0;
+      totalTests = 0;
+    }
+
+    public SummaryResults(int found, int mustPass, int total) {
+      totalFound = found;
+      totalMustPass = mustPass;
+      totalTests = total;
+    }
+
+    public void add(SummaryResults other) {
+      totalFound += other.totalFound;
+      totalMustPass += other.totalMustPass;
+      totalTests += other.totalTests;
+    }
+
+    public String toString() {
+      return "\nSUMMARY RESULTS:\n  Decoded " + totalFound + " images out of " + totalTests +
+        " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)";
+    }
+  }
+
   private static class TestResult {
     private final int mustPassCount;
     private final int tryHarderCount;
@@ -125,7 +157,13 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     return null;
   }
 
+  // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does
+  // not return SummaryResults.
   public void testBlackBox() throws IOException {
+    testBlackBoxCountingResults(true);
+  }
+
+  public SummaryResults testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
     assertFalse(testResults.isEmpty());
 
     File[] imageFiles = getImageFiles();
@@ -145,11 +183,12 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
       for (int x = 0; x < testCount; x++) {
         float rotation = testResults.get(x).getRotation();
         BufferedImage rotatedImage = rotateImage(image, rotation);
-        MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
-        if (decode(source, rotation, expectedText, false)) {
+        LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
+        BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
+        if (decode(bitmap, rotation, expectedText, false)) {
           passedCounts[x]++;
         }
-        if (decode(source, rotation, expectedText, true)) {
+        if (decode(bitmap, rotation, expectedText, true)) {
           tryHarderCounts[x]++;
         }
       }
@@ -179,17 +218,20 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     }
 
     // Then run through again and assert if any failed
-    for (int x = 0; x < testCount; x++) {
-      assertTrue("Rotation " + testResults.get(x).getRotation() +
-          " degrees: Too many images failed",
-          passedCounts[x] >= testResults.get(x).getMustPassCount());
-      assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
-          " degrees: Too many images failed",
-          tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
+    if (assertOnFailure) {
+      for (int x = 0; x < testCount; x++) {
+        assertTrue("Rotation " + testResults.get(x).getRotation() +
+            " degrees: Too many images failed",
+            passedCounts[x] >= testResults.get(x).getMustPassCount());
+        assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
+            " degrees: Too many images failed",
+            tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
+      }
     }
+    return new SummaryResults(totalFound, totalMustPass, totalTests);
   }
 
-  private boolean decode(MonochromeBitmapSource source, float rotation, String expectedText,
+  private boolean decode(BinaryBitmap source, float rotation, String expectedText,
                          boolean tryHarder) {
     Result result;
     String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';