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