5048343968b60e2da499b57ca977932299835d42
[zxing.git] / core / test / src / com / google / zxing / common / AbstractBlackBoxTestCase.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.common;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.DecodeHintType;
22 import com.google.zxing.LuminanceSource;
23 import com.google.zxing.Reader;
24 import com.google.zxing.ReaderException;
25 import com.google.zxing.Result;
26 import com.google.zxing.ResultMetadataType;
27 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
28 import junit.framework.TestCase;
29
30 import javax.imageio.ImageIO;
31 import java.awt.geom.AffineTransform;
32 import java.awt.image.AffineTransformOp;
33 import java.awt.image.BufferedImage;
34 import java.awt.image.BufferedImageOp;
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.FilenameFilter;
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 import java.nio.charset.Charset;
41 import java.util.ArrayList;
42 import java.util.Hashtable;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Properties;
46
47 /**
48  * @author Sean Owen
49  * @author dswitkin@google.com (Daniel Switkin)
50  */
51 public abstract class AbstractBlackBoxTestCase extends TestCase {
52
53   protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
54   static {
55     TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
56     TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
57   }
58
59   private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
60     public boolean accept(File dir, String name) {
61       String lowerCase = name.toLowerCase();
62       return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
63              lowerCase.endsWith(".gif") || lowerCase.endsWith(".png");
64     }
65   };
66
67   public static class SummaryResults {
68     private int totalFound;
69     private int totalMustPass;
70     private int totalTests;
71
72     public SummaryResults() {
73       totalFound = 0;
74       totalMustPass = 0;
75       totalTests = 0;
76     }
77
78     public SummaryResults(int found, int mustPass, int total) {
79       totalFound = found;
80       totalMustPass = mustPass;
81       totalTests = total;
82     }
83
84     public void add(SummaryResults other) {
85       totalFound += other.totalFound;
86       totalMustPass += other.totalMustPass;
87       totalTests += other.totalTests;
88     }
89
90     public String toString() {
91       return "\nSUMMARY RESULTS:\n  Decoded " + totalFound + " images out of " + totalTests +
92         " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)";
93     }
94   }
95
96   private static class TestResult {
97     private final int mustPassCount;
98     private final int tryHarderCount;
99     private final float rotation;
100
101     TestResult(int mustPassCount, int tryHarderCount, float rotation) {
102       this.mustPassCount = mustPassCount;
103       this.tryHarderCount = tryHarderCount;
104       this.rotation = rotation;
105     }
106     public int getMustPassCount() {
107       return mustPassCount;
108     }
109     public int getTryHarderCount() {
110       return tryHarderCount;
111     }
112     public float getRotation() {
113       return rotation;
114     }
115   }
116
117   private final File testBase;
118   private final Reader barcodeReader;
119   private final BarcodeFormat expectedFormat;
120   private final List<TestResult> testResults;
121
122   protected AbstractBlackBoxTestCase(String testBasePathSuffix,
123                                      Reader barcodeReader,
124                                      BarcodeFormat expectedFormat) {
125     // A little workaround to prevent aggravation in my IDE
126     File testBase = new File(testBasePathSuffix);
127     if (!testBase.exists()) {
128       // try starting with 'core' since the test base is often given as the project root
129       testBase = new File("core/" + testBasePathSuffix);
130     }
131     this.testBase = testBase;
132     this.barcodeReader = barcodeReader;
133     this.expectedFormat = expectedFormat;
134     testResults = new ArrayList<TestResult>();
135   }
136
137   /**
138    * Adds a new test for the current directory of images.
139    *
140    * @param mustPassCount The number of images which must decode for the test to pass.
141    * @param tryHarderCount The number of images which must pass using the try harder flag.
142    * @param rotation The rotation in degrees clockwise to use for this test.
143    */
144   protected void addTest(int mustPassCount, int tryHarderCount, float rotation) {
145     testResults.add(new TestResult(mustPassCount, tryHarderCount, rotation));
146   }
147
148   protected File[] getImageFiles() {
149     assertTrue("Please run from the 'core' directory", testBase.exists());
150     return testBase.listFiles(IMAGE_NAME_FILTER);
151   }
152
153   protected Reader getReader() {
154     return barcodeReader;
155   }
156
157   protected Hashtable<DecodeHintType, Object> getHints() {
158     return null;
159   }
160
161   // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does
162   // not return SummaryResults.
163   public void testBlackBox() throws IOException {
164     testBlackBoxCountingResults(true);
165   }
166
167   public SummaryResults testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
168     assertFalse(testResults.isEmpty());
169
170     File[] imageFiles = getImageFiles();
171     int testCount = testResults.size();
172     int[] passedCounts = new int[testCount];
173     int[] tryHarderCounts = new int[testCount];
174     for (File testImage : imageFiles) {
175       System.out.println("Starting " + testImage.getAbsolutePath());
176
177       BufferedImage image = ImageIO.read(testImage);
178
179       String testImageFileName = testImage.getName();
180       String fileBaseName = testImageFileName.substring(0, testImageFileName.indexOf('.'));
181       File expectedTextFile = new File(testBase, fileBaseName + ".txt");
182       String expectedText = readFileAsString(expectedTextFile);
183
184       File expectedMetadataFile = new File(testBase, fileBaseName + ".metadata.txt");
185       Properties expectedMetadata = new Properties();
186       if (expectedMetadataFile.exists()) {
187         expectedMetadata.load(new FileInputStream(expectedMetadataFile));
188       }
189
190       for (int x = 0; x < testCount; x++) {
191         float rotation = testResults.get(x).getRotation();
192         BufferedImage rotatedImage = rotateImage(image, rotation);
193         LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
194         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
195         if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) {
196           passedCounts[x]++;
197         }
198         if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
199           tryHarderCounts[x]++;
200         }
201       }
202     }
203
204     // Print the results of all tests first
205     int totalFound = 0;
206     int totalMustPass = 0;
207     for (int x = 0; x < testCount; x++) {
208       System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:");
209       System.out.println("  " + passedCounts[x] + " of " + imageFiles.length + " images passed ("
210           + testResults.get(x).getMustPassCount() + " required)");
211       System.out.println("  " + tryHarderCounts[x] + " of " + imageFiles.length +
212           " images passed with try harder (" + testResults.get(x).getTryHarderCount() +
213           " required)");
214       totalFound += passedCounts[x];
215       totalFound += tryHarderCounts[x];
216       totalMustPass += testResults.get(x).getMustPassCount();
217       totalMustPass += testResults.get(x).getTryHarderCount();
218     }
219
220     int totalTests = imageFiles.length * testCount * 2;
221     System.out.println("TOTALS:\n  Decoded " + totalFound + " images out of " + totalTests +
222       " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)");
223     if (totalFound > totalMustPass) {
224       System.out.println("  *** Test too lax by " + (totalFound - totalMustPass) + " images");
225     } else if (totalFound < totalMustPass) {
226       System.out.println("  *** Test failed by " + (totalMustPass - totalFound) + " images");
227     }
228
229     // Then run through again and assert if any failed
230     if (assertOnFailure) {
231       for (int x = 0; x < testCount; x++) {
232         assertTrue("Rotation " + testResults.get(x).getRotation() +
233             " degrees: Too many images failed",
234             passedCounts[x] >= testResults.get(x).getMustPassCount());
235         assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
236             " degrees: Too many images failed",
237             tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
238       }
239     }
240     return new SummaryResults(totalFound, totalMustPass, totalTests);
241   }
242
243   private boolean decode(BinaryBitmap source,
244                          float rotation,
245                          String expectedText,
246                          Properties expectedMetadata,
247                          boolean tryHarder) {
248     Result result;
249     String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
250
251     try {
252       Hashtable<DecodeHintType, Object> hints = getHints();
253       if (tryHarder) {
254         if (hints == null) {
255           hints = TRY_HARDER_HINT;
256         } else {
257           hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
258         }
259       }
260       result = barcodeReader.decode(source, hints);
261     } catch (ReaderException re) {
262       System.out.println(re + suffix);
263       return false;
264     }
265
266     if (!expectedFormat.equals(result.getBarcodeFormat())) {
267       System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
268           result.getBarcodeFormat() + '\'' + suffix);
269       return false;
270     }
271
272     String resultText = result.getText();
273     if (!expectedText.equals(resultText)) {
274       System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
275           '\'' +  suffix);
276       return false;
277     }
278
279     Hashtable resultMetadata = result.getResultMetadata();
280     for (Map.Entry<Object,Object> metadatum : expectedMetadata.entrySet()) {
281       ResultMetadataType key = ResultMetadataType.valueOf(metadatum.getKey().toString());
282       Object expectedValue = metadatum.getValue();
283       Object actualValue = resultMetadata == null ? null : resultMetadata.get(key);
284       if (!expectedValue.equals(actualValue)) {
285         System.out.println("Metadata mismatch: for key '" + key + "' expected '" + expectedValue +
286             "' but got '" + actualValue + '\'');
287         return false;
288       }
289     }
290
291     return true;
292   }
293
294   private static String readFileAsString(File file) throws IOException {
295     StringBuilder result = new StringBuilder((int) file.length());
296     InputStreamReader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF8"));
297     try {
298       char[] buffer = new char[256];
299       int charsRead;
300       while ((charsRead = reader.read(buffer)) > 0) {
301         result.append(buffer, 0, charsRead);
302       }
303     } finally {
304       reader.close();
305     }
306     return result.toString();
307   }
308
309   protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
310     if (degrees == 0.0f) {
311       return original;
312     } else {
313       AffineTransform at = new AffineTransform();
314       at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0, original.getHeight() / 2.0);
315       BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
316       return op.filter(original, null);
317     }
318   }
319
320 }