git-svn-id: http://zxing.googlecode.com/svn/trunk@574 59b500cc-1b3d-0410-9834-0bbf25f...
[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.DecodeHintType;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.Reader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
26 import junit.framework.TestCase;
27
28 import javax.imageio.ImageIO;
29 import java.awt.geom.AffineTransform;
30 import java.awt.image.AffineTransformOp;
31 import java.awt.image.BufferedImage;
32 import java.awt.image.BufferedImageOp;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FilenameFilter;
36 import java.io.IOException;
37 import java.io.InputStreamReader;
38 import java.util.Hashtable;
39 import java.util.List;
40 import java.util.ArrayList;
41
42 /**
43  * @author srowen@google.com (Sean Owen)
44  * @author dswitkin@google.com (Daniel Switkin)
45  */
46 public abstract class AbstractBlackBoxTestCase extends TestCase {
47
48   protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
49   static {
50     TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
51     TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
52   }
53
54   private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
55     public boolean accept(File dir, String name) {
56       String lowerCase = name.toLowerCase();
57       return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
58              lowerCase.endsWith(".gif") || lowerCase.endsWith(".png");
59     }
60   };
61
62   private static class TestResult {
63     private final int mustPassCount;
64     private final int tryHarderCount;
65     private final float rotation;
66
67     TestResult(int mustPassCount, int tryHarderCount, float rotation) {
68       this.mustPassCount = mustPassCount;
69       this.tryHarderCount = tryHarderCount;
70       this.rotation = rotation;
71     }
72     public int getMustPassCount() {
73       return mustPassCount;
74     }
75     public int getTryHarderCount() {
76       return tryHarderCount;
77     }
78     public float getRotation() {
79       return rotation;
80     }
81   }
82
83   private final File testBase;
84   private final Reader barcodeReader;
85   private final BarcodeFormat expectedFormat;
86   private final List<TestResult> testResults;
87
88   protected AbstractBlackBoxTestCase(File testBase,
89                                      Reader barcodeReader,
90                                      BarcodeFormat expectedFormat) {
91     this.testBase = testBase;
92     this.barcodeReader = barcodeReader;
93     this.expectedFormat = expectedFormat;
94     testResults = new ArrayList<TestResult>();
95   }
96
97   /**
98    * Adds a new test for the current directory of images.
99    *
100    * @param mustPassCount The number of images which must decode for the test to pass.
101    * @param tryHarderCount The number of images which must pass using the try harder flag.
102    * @param rotation The rotation in degrees clockwise to use for this test.
103    */
104   protected void addTest(int mustPassCount, int tryHarderCount, float rotation) {
105     testResults.add(new TestResult(mustPassCount, tryHarderCount, rotation));
106   }
107
108   protected File[] getImageFiles() {
109     assertTrue("Please run from the 'core' directory", testBase.exists());
110     return testBase.listFiles(IMAGE_NAME_FILTER);
111   }
112
113   protected Reader getReader() {
114     return barcodeReader;
115   }
116
117   public void testBlackBox() throws IOException {
118     assertFalse(testResults.isEmpty());
119
120     File[] imageFiles = getImageFiles();
121     int testCount = testResults.size();
122     int[] passedCounts = new int[testCount];
123     int[] tryHarderCounts = new int[testCount];
124     for (File testImage : imageFiles) {
125       System.out.println("Starting " + testImage.getAbsolutePath());
126
127       BufferedImage image = ImageIO.read(testImage);
128
129       String testImageFileName = testImage.getName();
130       File expectedTextFile = new File(testBase,
131           testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
132       String expectedText = readFileAsString(expectedTextFile);
133
134       for (int x = 0; x < testCount; x++) {
135         float rotation = testResults.get(x).getRotation();
136         BufferedImage rotatedImage = rotateImage(image, rotation);
137         MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
138         if (decode(source, rotation, expectedText, false)) {
139           passedCounts[x]++;
140         }
141         if (decode(source, rotation, expectedText, true)) {
142           tryHarderCounts[x]++;
143         }
144       }
145     }
146
147     // Print the results of all tests first
148     for (int x = 0; x < testCount; x++) {
149       System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:");
150       System.out.println("  " + passedCounts[x] + " of " + imageFiles.length + " images passed ("
151           + testResults.get(x).getMustPassCount() + " required)");
152       System.out.println("  " + tryHarderCounts[x] + " of " + imageFiles.length +
153           " images passed with try harder (" + testResults.get(x).getTryHarderCount() +
154           " required)");
155     }
156
157     // Then run through again and assert if any failed
158     for (int x = 0; x < testCount; x++) {
159       assertTrue("Rotation " + testResults.get(x).getRotation() +
160           " degrees: Too many images failed",
161           passedCounts[x] >= testResults.get(x).getMustPassCount());
162       assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() +
163           " degrees: Too many images failed",
164           tryHarderCounts[x] >= testResults.get(x).getTryHarderCount());
165     }
166   }
167
168   private boolean decode(MonochromeBitmapSource source, float rotation, String expectedText,
169                          boolean tryHarder) {
170     Result result;
171     String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';
172
173     try {
174       result = barcodeReader.decode(source, tryHarder ? TRY_HARDER_HINT : null);
175     } catch (ReaderException re) {
176       System.out.println(re + suffix);
177       return false;
178     }
179
180     if (!expectedFormat.equals(result.getBarcodeFormat())) {
181       System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
182           result.getBarcodeFormat() + "'" + suffix);
183       return false;
184     }
185
186     String resultText = result.getText();
187     if (!expectedText.equals(resultText)) {
188       System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
189           "'" +  suffix);
190       return false;
191     }
192     return true;
193   }
194
195   private static String readFileAsString(File file) throws IOException {
196     StringBuilder result = new StringBuilder((int) file.length());
197     InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF8");
198     try {
199       char[] buffer = new char[256];
200       int charsRead;
201       while ((charsRead = reader.read(buffer)) > 0) {
202         result.append(buffer, 0, charsRead);
203       }
204     } finally {
205       reader.close();
206     }
207     return result.toString();
208   }
209
210   protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
211     if (degrees == 0.0f) {
212       return original;
213     } else {
214       AffineTransform at = new AffineTransform();
215       at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f);
216       BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
217       return op.filter(original, null);
218     }
219   }
220
221 }