Minor style tweaks
[zxing.git] / core / test / src / com / google / zxing / common / AbstractBlackBoxTestCase.java
1 /*
2  * Copyright 2008 Google Inc.
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.net.URL;
39 import java.util.Hashtable;
40 import java.util.Vector;
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   private 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              lowerCase.endsWith(".url");
60     }
61   };
62
63   private static class TestResult {
64     private final int mustPassCount;
65     private final float rotation;
66     TestResult(int mustPassCount, float rotation) {
67       this.mustPassCount = mustPassCount;
68       this.rotation = rotation;
69     }
70     public int getMustPassCount() {
71       return mustPassCount;
72     }
73     public float getRotation() {
74       return rotation;
75     }
76   }
77
78   private final File testBase;
79   private final Reader barcodeReader;
80   private final BarcodeFormat expectedFormat;
81   private Vector<TestResult> testResults;
82
83   protected AbstractBlackBoxTestCase(File testBase,
84                                      Reader barcodeReader,
85                                      BarcodeFormat expectedFormat) {
86     this.testBase = testBase;
87     this.barcodeReader = barcodeReader;
88     this.expectedFormat = expectedFormat;
89     testResults = new Vector<TestResult>();
90   }
91
92   /**
93    * Adds a new test for the current directory of images.
94    *
95    * @param mustPassCount The number of images which must decode for the test to pass.
96    * @param rotation The rotation in degrees clockwise to use for this test.
97    */
98   protected void addTest(int mustPassCount, float rotation) {
99     testResults.add(new TestResult(mustPassCount, rotation));
100   }
101
102   public void testBlackBox() throws IOException {
103     assertFalse(testResults.isEmpty());
104     assertTrue("Please run from the 'core' directory", testBase.exists());
105
106     File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
107     int[] passedCounts = new int[testResults.size()];
108     for (File testImage : imageFiles) {
109       System.out.println("Starting " + testImage.getAbsolutePath());
110
111       BufferedImage image;
112       if (testImage.getName().endsWith(".url")) {
113         String urlString = readFileAsString(testImage);
114         image = ImageIO.read(new URL(urlString));
115       } else {
116         image = ImageIO.read(testImage);
117       }
118
119       String testImageFileName = testImage.getName();
120       File expectedTextFile = new File(testBase,
121           testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
122       String expectedText = readFileAsString(expectedTextFile);
123
124       for (int x = 0; x < testResults.size(); x++) {
125         if (doTestOneImage(image, testResults.get(x).getRotation(), expectedText)) {
126           passedCounts[x]++;
127         }
128       }
129     }
130
131     for (int x = 0; x < testResults.size(); x++) {
132       System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " + passedCounts[x] +
133           " of " + imageFiles.length + " images passed (" + testResults.get(x).getMustPassCount() +
134           " required)");
135       assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: Too many images failed",
136           passedCounts[x] >= testResults.get(x).getMustPassCount());
137     }
138   }
139
140   private boolean doTestOneImage(BufferedImage image, float rotationInDegrees, String expectedText) {
141     BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
142     MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
143     Result result;
144     try {
145       result = barcodeReader.decode(source);
146     } catch (ReaderException re) {
147       System.out.println(re);
148       return false;
149     }
150
151     if (!expectedFormat.equals(result.getBarcodeFormat())) {
152       System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
153           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
154       return false;
155     }
156
157     String resultText = result.getText();
158     if (!expectedText.equals(resultText)) {
159       System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
160           "' (rotation: " + rotationInDegrees + ')');
161       return false;
162     }
163
164     // Try "try harder" mode
165     try {
166       result = barcodeReader.decode(source, TRY_HARDER_HINT);
167     } catch (ReaderException re) {
168       fail("Normal mode succeeded but \"try harder\" failed");
169       return false;
170     }
171     if (!expectedFormat.equals(result.getBarcodeFormat())) {
172       System.out.println("Try Harder Format mismatch: expected '" + expectedFormat + "' but got '" +
173           result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
174     } else if (!expectedText.equals(resultText)) {
175       System.out.println("Try Harder Mismatch: expected '" + expectedText + "' but got '" +
176           resultText + "' (rotation: " + rotationInDegrees + ')');
177     }
178     return true;
179   }
180
181   private static String readFileAsString(File file) throws IOException {
182     StringBuilder result = new StringBuilder((int) file.length());
183     InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
184     try {
185       char[] buffer = new char[256];
186       int charsRead;
187       while ((charsRead = reader.read(buffer)) > 0) {
188         result.append(buffer, 0, charsRead);
189       }
190     } finally {
191       reader.close();
192     }
193     return result.toString();
194   }
195
196   private static BufferedImage rotateImage(BufferedImage original, float degrees) {
197     if (degrees == 0.0f) {
198       return original;
199     } else {
200       AffineTransform at = new AffineTransform();
201       at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f);
202       BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
203       return op.filter(original, null);
204     }
205   }
206
207 }