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