Wrote a quick command line app to calculate how many total images are passing our...
[zxing.git] / core / test / src / com / google / zxing / common / AbstractBlackBoxTestCase.java
index 9d4017e..0b3ec6a 100644 (file)
@@ -23,9 +23,9 @@ import com.google.zxing.Reader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
+
 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 +35,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 +62,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;
@@ -86,9 +117,15 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
   private final BarcodeFormat expectedFormat;
   private final List<TestResult> testResults;
 
-  protected AbstractBlackBoxTestCase(File testBase,
+  protected AbstractBlackBoxTestCase(String testBasePathSuffix,
                                      Reader barcodeReader,
                                      BarcodeFormat expectedFormat) {
+    // A little workaround to prevent aggravation in my IDE
+    File testBase = new File(testBasePathSuffix);
+    if (!testBase.exists()) {
+      // try starting with 'core' since the test base is often given as the project root
+      testBase = new File("core/" + testBasePathSuffix);
+    }
     this.testBase = testBase;
     this.barcodeReader = barcodeReader;
     this.expectedFormat = expectedFormat;
@@ -115,7 +152,17 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     return barcodeReader;
   }
 
+  protected Hashtable<DecodeHintType, Object> getHints() {
+    return null;
+  }
+
+  // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does
+  // not return SummaryResults.
   public void testBlackBox() throws IOException {
+    testBlackBoxCountingResults();
+  }
+
+  public SummaryResults testBlackBoxCountingResults() throws IOException {
     assertFalse(testResults.isEmpty());
 
     File[] imageFiles = getImageFiles();
@@ -146,6 +193,8 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     }
 
     // Print the results of all tests first
+    int totalFound = 0;
+    int totalMustPass = 0;
     for (int x = 0; x < testCount; x++) {
       System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:");
       System.out.println("  " + passedCounts[x] + " of " + imageFiles.length + " images passed ("
@@ -153,6 +202,17 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
       System.out.println("  " + tryHarderCounts[x] + " of " + imageFiles.length +
           " images passed with try harder (" + testResults.get(x).getTryHarderCount() +
           " required)");
+      totalFound += passedCounts[x];
+      totalFound += tryHarderCounts[x];
+      totalMustPass += testResults.get(x).getMustPassCount();
+      totalMustPass += testResults.get(x).getTryHarderCount();
+    }
+
+    int totalTests = imageFiles.length * testCount * 2;
+    System.out.println("TOTALS:\n  Decoded " + totalFound + " images out of " + totalTests +
+      " (" + (totalFound * 100 / totalTests) + "%)");
+    if (totalFound > totalMustPass) {
+      System.out.println("  *** Test too lax by " + (totalFound - totalMustPass) + " images");
     }
 
     // Then run through again and assert if any failed
@@ -164,6 +224,7 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
           " 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,
@@ -172,7 +233,15 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
 
     try {
-      result = barcodeReader.decode(source, tryHarder ? TRY_HARDER_HINT : null);
+      Hashtable<DecodeHintType, Object> hints = getHints();
+      if (tryHarder) {
+        if (hints == null) {
+          hints = TRY_HARDER_HINT;
+        } else {
+          hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
+        }
+      }
+      result = barcodeReader.decode(source, hints);
     } catch (ReaderException re) {
       System.out.println(re + suffix);
       return false;
@@ -180,14 +249,14 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
 
     if (!expectedFormat.equals(result.getBarcodeFormat())) {
       System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
-          result.getBarcodeFormat() + "'" + suffix);
+          result.getBarcodeFormat() + '\'' + suffix);
       return false;
     }
 
     String resultText = result.getText();
     if (!expectedText.equals(resultText)) {
       System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
-          "'" +  suffix);
+          '\'' +  suffix);
       return false;
     }
     return true;
@@ -213,7 +282,7 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
       return original;
     } else {
       AffineTransform at = new AffineTransform();
-      at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f);
+      at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0, original.getHeight() / 2.0);
       BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
       return op.filter(original, null);
     }