Add support for UPC/EAN +5 extensions, plus basic tests, and display the content...
[zxing.git] / core / test / src / com / google / zxing / common / AbstractBlackBoxTestCase.java
index e24f5fb..5048343 100644 (file)
@@ -23,10 +23,11 @@ import com.google.zxing.LuminanceSource;
 import com.google.zxing.Reader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
+import com.google.zxing.ResultMetadataType;
 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;
@@ -40,8 +41,8 @@ import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
-
-import javax.imageio.ImageIO;
+import java.util.Map;
+import java.util.Properties;
 
 /**
  * @author Sean Owen
@@ -160,10 +161,10 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
   // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does
   // not return SummaryResults.
   public void testBlackBox() throws IOException {
-    testBlackBoxCountingResults();
+    testBlackBoxCountingResults(true);
   }
 
-  public SummaryResults testBlackBoxCountingResults() throws IOException {
+  public SummaryResults testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
     assertFalse(testResults.isEmpty());
 
     File[] imageFiles = getImageFiles();
@@ -176,19 +177,25 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
       BufferedImage image = ImageIO.read(testImage);
 
       String testImageFileName = testImage.getName();
-      File expectedTextFile = new File(testBase,
-          testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
+      String fileBaseName = testImageFileName.substring(0, testImageFileName.indexOf('.'));
+      File expectedTextFile = new File(testBase, fileBaseName + ".txt");
       String expectedText = readFileAsString(expectedTextFile);
 
+      File expectedMetadataFile = new File(testBase, fileBaseName + ".metadata.txt");
+      Properties expectedMetadata = new Properties();
+      if (expectedMetadataFile.exists()) {
+        expectedMetadata.load(new FileInputStream(expectedMetadataFile));
+      }
+
       for (int x = 0; x < testCount; x++) {
         float rotation = testResults.get(x).getRotation();
         BufferedImage rotatedImage = rotateImage(image, rotation);
         LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
-        BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
-        if (decode(bitmap, rotation, expectedText, false)) {
+        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
+        if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) {
           passedCounts[x]++;
         }
-        if (decode(bitmap, rotation, expectedText, true)) {
+        if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
           tryHarderCounts[x]++;
         }
       }
@@ -212,24 +219,31 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
 
     int totalTests = imageFiles.length * testCount * 2;
     System.out.println("TOTALS:\n  Decoded " + totalFound + " images out of " + totalTests +
-      " (" + (totalFound * 100 / totalTests) + "%)");
+      " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)");
     if (totalFound > totalMustPass) {
       System.out.println("  *** Test too lax by " + (totalFound - totalMustPass) + " images");
+    } else if (totalFound < totalMustPass) {
+      System.out.println("  *** Test failed by " + (totalMustPass - totalFound) + " images");
     }
 
     // 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(BinaryBitmap source, float rotation, String expectedText,
+  private boolean decode(BinaryBitmap source,
+                         float rotation,
+                         String expectedText,
+                         Properties expectedMetadata,
                          boolean tryHarder) {
     Result result;
     String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
@@ -261,6 +275,19 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
           '\'' +  suffix);
       return false;
     }
+
+    Hashtable resultMetadata = result.getResultMetadata();
+    for (Map.Entry<Object,Object> metadatum : expectedMetadata.entrySet()) {
+      ResultMetadataType key = ResultMetadataType.valueOf(metadatum.getKey().toString());
+      Object expectedValue = metadatum.getValue();
+      Object actualValue = resultMetadata == null ? null : resultMetadata.get(key);
+      if (!expectedValue.equals(actualValue)) {
+        System.out.println("Metadata mismatch: for key '" + key + "' expected '" + expectedValue +
+            "' but got '" + actualValue + '\'');
+        return false;
+      }
+    }
+
     return true;
   }
 
@@ -290,4 +317,4 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
     }
   }
 
-}
\ No newline at end of file
+}