Finally added the beginnings of a decent black-box unit test for QR code decoding
[zxing.git] / core / test / src / com / google / zxing / qrcode / QRCodeReaderTestCase.java
1 /*
2  * Copyright 2007 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.qrcode;
18
19 import com.google.zxing.ReaderException;
20 import com.google.zxing.Result;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.Reader;
23 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
24
25 import junit.framework.TestCase;
26
27 import javax.imageio.ImageIO;
28 import java.awt.image.BufferedImage;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33
34 /**
35  * @author srowen@google.com (Sean Owen)
36  */
37 public final class QRCodeReaderTestCase extends TestCase {
38
39   public void testDecode() throws Exception {
40     doTestURI("http://writerresponsetheory.org/wordpress/wp-content/uploads/qrcode_01.png",
41               "When we at WRT talk about \\\"text,\\\" we are generally talking about a particular kind of readable " +
42               "information encoding - and readable is a complex proposition. Text may be stylized in a way we are " +
43               "unfamiliar with, as in blackletter - it may be interspersed with some markup we don\\'t understand, " +
44               "such as HTML - it may be be a substitution system we aren\\'t familiar with, such as braille or " +
45               "morse code - or it may be a system that, while technically human-readable, isn\\'t particularly " +
46               "optimized for reading by humans, as with barcodes (although barcodes can be read).");
47     doTestURI("http://writerresponsetheory.org/query/poe/qrcode%20outputs/qrcodegen-examples/chunk-from-128-bug-head/" +
48               "encodes-by-nfggames/qr-chunk-from-128-bughead-ground.png",
49               "LANDBYASCARC   PERCEPTIBLEC EEK,OOZI GITSWAYTHROU  AWILDERNESSOFBESUPPOSED,I  " +
50               "CANT,ORATL ASTDWARFISH.NO REESOFANYM  NITUDEARETOBESOMEMISERA  EFRAMEBUIL I GS,TENANTED, U " +
51               "INGSUMMER   THEFUGITIVE;BUTTHEWHO  ISLAND,WI H   EXCEPTIONOFT   W STERNPOI  ,ANDALINEOFLLIAMLEGR   " +
52               ".HEWASOF N    ENTHUGUENOTF    Y ANDHADON  BEENWEALTHTIONCONSE  ENTUPONH SD   STERS,HELEFTNE   " +
53               "LE NS,THEC   OFHISFOREOUTHCARO   A.THISISLA    AVERYSINGULARO    TCONSISTSO  " +
54               "ITTLEELSEDSAQUART   FAMILE. TI   PARATEDFROMTHEMA   AN BYASCAR   YPERCEPTERESORT     " +
55               "MARSH EN   EVEGETATION,ASMIGH   SU POSED,   CANT,ORATREMITY,      " +
56               "ORT OULTRIESTANDS,ANDWHEREARESOM MIS      FRAMEBUIFEVER,MAYBE      " +
57               "INDEED,                TO;BUTT      EISLAND,WITNYYEARSAGO,IC    ACTED                    " +
58               "LLIAM    AND.HEWASOFANNESHADREDUCEDHIM OWA                       " +
59               "IONC NSEQUENTUPONHISDSIDENCEATSULLIVA \\'S                          HC ROLINA.THISISLANOUTTHR    " +
60               "LESLON .        THATN  OINTE        U RTEROF    E.ITISTHROU       ERNE       DSANDSLI  ,AFAVOR        " +
61               "TOFT       HEN.T");
62     doTestURI("http://www.malcolmhall.com/wp-content/uploads/2006/07/200607260214.jpg",
63               "http://www.malcolmhall.com");
64     doTestURI("http://www.qrcodeblog.com/qr/0609/060902_qr_kawasaki_st02.jpg",
65               "http://wwws.keihin.ktr.mlit.go.jp/keitai/");
66     doTestURI("http://mobile.kaywa.com/files/images/2007/4/480/mob181_1175524511.jpg",
67               "2021200000");
68     doTestURI("http://www.smoothplanet.com/files/images/2007/2/mob281_1170754866.jpg",
69               "http://d.kaywa.com/20207100");
70     doTestURI("http://www.mobileviews.com/blog/wp-content/uploads/2006/11/livebarcode.gif",
71               "BIZCARD:N:Todd;X:Ogasawara;T:Tech Geek;C:MobileViews.com;A:MobileTown USA;E:editor@mobileviews.com;;");
72     doTestURI("http://staticrooster.com/tshirts/qr_sm.gif",
73               "http://staticrooster.com");
74     doTestURI("http://www.ihaveanidea.org/blogs/uploads/i/interactive/270.png",
75               "Morden");
76   }
77
78   private static void doTestURI(final String uriString, final String expected)
79       throws URISyntaxException, IOException, ReaderException {
80     URI uri = new URI(uriString);
81     InputStream is = uri.toURL().openStream();
82     try {
83       BufferedImage image = ImageIO.read(is);
84       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
85       Reader reader = new QRCodeReader();
86       Result result = reader.decode(source);
87       assertEquals(expected, result.getText());
88     } finally {
89       is.close();
90     }
91   }
92
93 }