Add more unit tests for client.result, and more small code tweaks.
[zxing.git] / core / test / src / com / google / zxing / client / result / ParsedReaderResultTestCase.java
1 /*
2  * Copyright 2007 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.client.result;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import junit.framework.TestCase;
22
23 /**
24  * Tests {@link ParsedResult}.
25  *
26  * @author Sean Owen
27  * @author dswitkin@google.com (Daniel Switkin)
28  */
29 public final class ParsedReaderResultTestCase extends TestCase {
30
31   public void testTextType() {
32     doTestResult("", "", ParsedResultType.TEXT);
33     doTestResult("foo", "foo", ParsedResultType.TEXT);
34     doTestResult("This is a test", "This is a test", ParsedResultType.TEXT);
35     doTestResult("This is a test\nwith newlines", "This is a test\nwith newlines",
36         ParsedResultType.TEXT);
37     doTestResult("This: a test with lots of @ nearly-random punctuation! No? OK then.",
38         "This: a test with lots of @ nearly-random punctuation! No? OK then.",
39         ParsedResultType.TEXT);
40   }
41
42   public void testBookmarkType() {
43     doTestResult("MEBKM:URL:google.com;;", "http://google.com", ParsedResultType.URI);
44     doTestResult("MEBKM:URL:google.com;TITLE:Google;;", "Google\nhttp://google.com",
45         ParsedResultType.URI);
46     doTestResult("MEBKM:TITLE:Google;URL:google.com;;", "Google\nhttp://google.com",
47         ParsedResultType.URI);
48     doTestResult("MEBKM:URL:http://google.com;;", "http://google.com", ParsedResultType.URI);
49     doTestResult("MEBKM:URL:HTTPS://google.com;;", "https://google.com", ParsedResultType.URI);
50   }
51
52   public void testURLTOType() {
53     doTestResult("urlto:foo:bar.com", "foo\nhttp://bar.com", ParsedResultType.URI);
54     doTestResult("URLTO:foo:bar.com", "foo\nhttp://bar.com", ParsedResultType.URI);
55     doTestResult("URLTO::bar.com", "http://bar.com", ParsedResultType.URI);
56     doTestResult("URLTO::http://bar.com", "http://bar.com", ParsedResultType.URI);
57   }
58
59   public void testEmailType() {
60     doTestResult("MATMSG:TO:srowen@example.org;;",
61         "srowen@example.org", ParsedResultType.EMAIL_ADDRESS);
62     doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;;", "srowen@example.org\nStuff",
63         ParsedResultType.EMAIL_ADDRESS);
64     doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;",
65         "srowen@example.org\nStuff\nThis is some text", ParsedResultType.EMAIL_ADDRESS);
66     doTestResult("MATMSG:SUB:Stuff;BODY:This is some text;TO:srowen@example.org;;",
67         "srowen@example.org\nStuff\nThis is some text", ParsedResultType.EMAIL_ADDRESS);
68     doTestResult("TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;",
69         "TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedResultType.TEXT);
70   }
71
72   public void testEmailAddressType() {
73     doTestResult("srowen@example.org", "srowen@example.org", ParsedResultType.EMAIL_ADDRESS);
74     doTestResult("mailto:srowen@example.org", "srowen@example.org", ParsedResultType.EMAIL_ADDRESS);
75     doTestResult("MAILTO:srowen@example.org", "srowen@example.org", ParsedResultType.EMAIL_ADDRESS);
76     doTestResult("srowen@example", "srowen@example", ParsedResultType.TEXT);
77     doTestResult("srowen", "srowen", ParsedResultType.TEXT);
78     doTestResult("Let's meet @ 2", "Let's meet @ 2", ParsedResultType.TEXT);
79   }
80
81   public void testAddressBookType() {
82     doTestResult("MECARD:N:Sean Owen;;", "Sean Owen", ParsedResultType.ADDRESSBOOK);
83     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;;", "Sean Owen\n+12125551212",
84         ParsedResultType.ADDRESSBOOK);
85     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;URL:google.com;;",
86         "Sean Owen\n+12125551212\ngoogle.com", ParsedResultType.ADDRESSBOOK);
87     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;URL:google.com;EMAIL:srowen@example.org;",
88         "Sean Owen\n+12125551212\nsrowen@example.org\ngoogle.com", ParsedResultType.ADDRESSBOOK);
89     doTestResult("MECARD:ADR:76 9th Ave;N:Sean Owen;URL:google.com;EMAIL:srowen@example.org;",
90         "Sean Owen\n76 9th Ave\nsrowen@example.org\ngoogle.com", ParsedResultType.ADDRESSBOOK);
91     doTestResult("MECARD:BDAY:19760520;N:Sean Owen;URL:google.com;EMAIL:srowen@example.org;",
92         "Sean Owen\nsrowen@example.org\ngoogle.com\n19760520", ParsedResultType.ADDRESSBOOK);
93     doTestResult("MECARD:ORG:Google;N:Sean Owen;URL:google.com;EMAIL:srowen@example.org;",
94         "Sean Owen\nGoogle\nsrowen@example.org\ngoogle.com", ParsedResultType.ADDRESSBOOK);
95     doTestResult("MECARD:NOTE:ZXing Team;N:Sean Owen;URL:google.com;EMAIL:srowen@example.org;",
96         "Sean Owen\nsrowen@example.org\ngoogle.com\nZXing Team", ParsedResultType.ADDRESSBOOK);
97     doTestResult("N:Sean Owen;TEL:+12125551212;;", "N:Sean Owen;TEL:+12125551212;;",
98         ParsedResultType.TEXT);
99   }
100
101   public void testAddressBookAUType() {
102     doTestResult("MEMORY:\r\n", "", ParsedResultType.ADDRESSBOOK);
103     doTestResult("MEMORY:foo\r\nNAME1:Sean\r\n", "Sean\nfoo", ParsedResultType.ADDRESSBOOK);
104     doTestResult("TEL1:+12125551212\r\nMEMORY:\r\n", "+12125551212", ParsedResultType.ADDRESSBOOK);
105   }
106
107   public void testBizcard() {
108     doTestResult("BIZCARD:N:Sean;X:Owen;C:Google;A:123 Main St;M:+12225551212;E:srowen@example.org;",
109         "Sean Owen\nGoogle\n123 Main St\n+12225551212\nsrowen@example.org", ParsedResultType.ADDRESSBOOK);
110   }
111
112   public void testUPCA() {
113     doTestResult("123456789012", "123456789012", ParsedResultType.PRODUCT, BarcodeFormat.UPC_A);
114     doTestResult("1234567890123", "1234567890123", ParsedResultType.PRODUCT, BarcodeFormat.UPC_A);
115     doTestResult("12345678901", "12345678901", ParsedResultType.TEXT);
116   }
117
118   public void testUPCE() {
119     doTestResult("01234565", "01234565", ParsedResultType.PRODUCT, BarcodeFormat.UPC_E);
120   }
121
122   public void testEAN() {
123     doTestResult("00393157", "00393157", ParsedResultType.PRODUCT, BarcodeFormat.EAN_8);
124     doTestResult("00393158", "00393158", ParsedResultType.TEXT);
125     doTestResult("5051140178499", "5051140178499", ParsedResultType.PRODUCT, BarcodeFormat.EAN_13);
126     doTestResult("5051140178490", "5051140178490", ParsedResultType.TEXT);
127   }
128
129   public void testISBN() {
130     doTestResult("9784567890123", "9784567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
131     doTestResult("9794567890123", "9794567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
132     doTestResult("97845678901", "97845678901", ParsedResultType.TEXT);
133     doTestResult("97945678901", "97945678901", ParsedResultType.TEXT);
134   }
135
136   public void testURI() {
137     doTestResult("http://google.com", "http://google.com", ParsedResultType.URI);
138     doTestResult("google.com", "http://google.com", ParsedResultType.URI);
139     doTestResult("https://google.com", "https://google.com", ParsedResultType.URI);
140     doTestResult("HTTP://google.com", "http://google.com", ParsedResultType.URI);
141     doTestResult("http://google.com/foobar", "http://google.com/foobar", ParsedResultType.URI);
142     doTestResult("https://google.com:443/foobar", "https://google.com:443/foobar",
143         ParsedResultType.URI);
144     doTestResult("google.com:443", "http://google.com:443", ParsedResultType.URI);
145     doTestResult("google.com:443/", "http://google.com:443/", ParsedResultType.URI);
146     doTestResult("google.com:443/foobar", "http://google.com:443/foobar", ParsedResultType.URI);
147     doTestResult("http://google.com:443/foobar", "http://google.com:443/foobar",
148         ParsedResultType.URI);
149     doTestResult("https://google.com:443/foobar", "https://google.com:443/foobar",
150         ParsedResultType.URI);
151     doTestResult("ftp://google.com/fake", "ftp://google.com/fake", ParsedResultType.URI);
152     doTestResult("gopher://google.com/obsolete", "gopher://google.com/obsolete",
153         ParsedResultType.URI);
154   }
155
156   public void testGeo() {
157     doTestResult("geo:1,2", "1.0, 2.0", ParsedResultType.GEO);
158     doTestResult("GEO:1,2", "1.0, 2.0", ParsedResultType.GEO);
159     doTestResult("geo:1,2,3", "1.0, 2.0, 3.0m", ParsedResultType.GEO);
160     doTestResult("geo:100.33,-32.3344,3.35", "100.33, -32.3344, 3.35m", ParsedResultType.GEO);
161     doTestResult("geo", "geo", ParsedResultType.TEXT);
162     doTestResult("geography", "geography", ParsedResultType.TEXT);
163   }
164
165   public void testTel() {
166     doTestResult("tel:+15551212", "+15551212", ParsedResultType.TEL);
167     doTestResult("TEL:+15551212", "+15551212", ParsedResultType.TEL);
168     doTestResult("tel:212 555 1212", "212 555 1212", ParsedResultType.TEL);
169     doTestResult("tel:2125551212", "2125551212", ParsedResultType.TEL);
170     doTestResult("tel:212-555-1212", "212-555-1212", ParsedResultType.TEL);
171     doTestResult("tel", "tel", ParsedResultType.TEXT);
172     doTestResult("telephone", "telephone", ParsedResultType.TEXT);
173   }
174
175   public void testVCard() {
176     doTestResult("BEGIN:VCARD\r\nEND:VCARD", "", ParsedResultType.ADDRESSBOOK);
177     doTestResult("BEGIN:VCARD\r\nN:Owen;Sean\r\nEND:VCARD", "Sean Owen",
178         ParsedResultType.ADDRESSBOOK);
179     doTestResult("BEGIN:VCARD\r\nVERSION:2.1\r\nN:Owen;Sean\r\nEND:VCARD", "Sean Owen",
180         ParsedResultType.ADDRESSBOOK);
181     doTestResult("BEGIN:VCARD\r\nADR;HOME:123 Main St\r\nVERSION:2.1\r\nN:Owen;Sean\r\nEND:VCARD",
182         "Sean Owen\n123 Main St", ParsedResultType.ADDRESSBOOK);
183     doTestResult("BEGIN:VCARD", "begin:VCARD",
184         ParsedResultType.URI); // yeah we end up guessing "URI" here
185   }
186
187   public void testVEvent() {
188     // UTC times
189     doTestResult("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" +
190         "DTEND:20080505T234555Z\r\nEND:VEVENT\r\nEND:VCALENDAR",
191         "foo\n20080504T123456Z\n20080505T234555Z",
192         ParsedResultType.CALENDAR);
193     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" +
194         "DTEND:20080505T234555Z\r\nEND:VEVENT", "foo\n20080504T123456Z\n20080505T234555Z",
195         ParsedResultType.CALENDAR);
196     // Local times
197     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456\r\n" +
198         "DTEND:20080505T234555\r\nEND:VEVENT", "foo\n20080504T123456\n20080505T234555",
199         ParsedResultType.CALENDAR);
200     // Date only (all day event)
201     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504\r\n" +
202         "DTEND:20080505\r\nEND:VEVENT", "foo\n20080504\n20080505", ParsedResultType.CALENDAR);
203     // Start time only
204     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\nEND:VEVENT",
205         "foo\n20080504T123456Z", ParsedResultType.CALENDAR);
206     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456\r\nEND:VEVENT",
207         "foo\n20080504T123456", ParsedResultType.CALENDAR);
208     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504\r\nEND:VEVENT",
209         "foo\n20080504", ParsedResultType.CALENDAR);
210     doTestResult("BEGIN:VEVENT\r\nDTEND:20080505T\r\nEND:VEVENT",
211         "BEGIN:VEVENT\r\nDTEND:20080505T\r\nEND:VEVENT", ParsedResultType.TEXT);
212     // Make sure illegal entries without newlines don't crash
213     doTestResult(
214         "BEGIN:VEVENTSUMMARY:EventDTSTART:20081030T122030ZDTEND:20081030T132030ZEND:VEVENT",
215         "begin:VEVENTSUMMARY:EventDTSTART:20081030T122030ZDTEND:20081030T132030ZEND:VEVENT",
216         ParsedResultType.URI);
217     // See above note on why this is URI
218     doTestResult("BEGIN:VEVENT", "begin:VEVENT", ParsedResultType.URI);
219   }
220
221   public void testSMS() {
222     doTestResult("sms:+15551212", "+15551212", ParsedResultType.SMS);
223     doTestResult("SMS:+15551212", "+15551212", ParsedResultType.SMS);
224     doTestResult("SMSTO:+15551212", "+15551212", ParsedResultType.SMS);
225     doTestResult("smsto:+15551212", "+15551212", ParsedResultType.SMS);
226     doTestResult("sms:+15551212;via=999333", "+15551212\n999333", ParsedResultType.SMS);
227     doTestResult("sms:+15551212?subject=foo&body=bar", "+15551212\nfoo\nbar", ParsedResultType.SMS);
228     doTestResult("sms:+15551212:subject", "+15551212\nsubject", ParsedResultType.SMS);
229     doTestResult("sms:+15551212:My message", "+15551212\nMy message", ParsedResultType.SMS);
230     // Need to handle question mark in the subject
231     doTestResult("sms:+15551212:What's up?", "+15551212\nWhat's up?", ParsedResultType.SMS);
232     // Need to handle colon in the subject
233     doTestResult("sms:+15551212:Directions: Do this", "+15551212\nDirections: Do this",
234         ParsedResultType.SMS);
235     doTestResult("sms:212-555-1212:Here's a longer message. Should be fine.",
236         "212-555-1212\nHere's a longer message. Should be fine.",
237         ParsedResultType.SMS);
238   }
239
240   public void testMMS() {
241     doTestResult("mms:+15551212", "+15551212", ParsedResultType.SMS);
242     doTestResult("MMS:+15551212", "+15551212", ParsedResultType.SMS);
243     doTestResult("MMSTO:+15551212", "+15551212", ParsedResultType.SMS);
244     doTestResult("mmsto:+15551212", "+15551212", ParsedResultType.SMS);
245     doTestResult("mms:+15551212;via=999333", "+15551212\n999333", ParsedResultType.SMS);
246     doTestResult("mms:+15551212?subject=foo&body=bar", "+15551212\nfoo\nbar", ParsedResultType.SMS);
247     doTestResult("mms:+15551212:subject", "+15551212\nsubject", ParsedResultType.SMS);
248     doTestResult("mms:+15551212:My message", "+15551212\nMy message", ParsedResultType.SMS);
249     doTestResult("mms:+15551212:What's up?", "+15551212\nWhat's up?", ParsedResultType.SMS);
250     doTestResult("mms:+15551212:Directions: Do this", "+15551212\nDirections: Do this",
251         ParsedResultType.SMS);
252     doTestResult("mms:212-555-1212:Here's a longer message. Should be fine.",
253         "212-555-1212\nHere's a longer message. Should be fine.", ParsedResultType.SMS);
254   }
255
256   /*
257   public void testNDEFText() {
258     doTestResult(new byte[] {(byte)0xD1,(byte)0x01,(byte)0x05,(byte)0x54,
259                              (byte)0x02,(byte)0x65,(byte)0x6E,(byte)0x68,
260                              (byte)0x69},
261                  ParsedResultType.TEXT);
262   }
263
264   public void testNDEFURI() {
265     doTestResult(new byte[] {(byte)0xD1,(byte)0x01,(byte)0x08,(byte)0x55,
266                              (byte)0x01,(byte)0x6E,(byte)0x66,(byte)0x63,
267                              (byte)0x2E,(byte)0x63,(byte)0x6F,(byte)0x6D},
268                  ParsedResultType.URI);
269   }
270
271   public void testNDEFSmartPoster() {
272     doTestResult(new byte[] {(byte)0xD1,(byte)0x02,(byte)0x2F,(byte)0x53,
273                              (byte)0x70,(byte)0x91,(byte)0x01,(byte)0x0E,
274                              (byte)0x55,(byte)0x01,(byte)0x6E,(byte)0x66,
275                              (byte)0x63,(byte)0x2D,(byte)0x66,(byte)0x6F,
276                              (byte)0x72,(byte)0x75,(byte)0x6D,(byte)0x2E,
277                              (byte)0x6F,(byte)0x72,(byte)0x67,(byte)0x11,
278                              (byte)0x03,(byte)0x01,(byte)0x61,(byte)0x63,
279                              (byte)0x74,(byte)0x00,(byte)0x51,(byte)0x01,
280                              (byte)0x12,(byte)0x54,(byte)0x05,(byte)0x65,
281                              (byte)0x6E,(byte)0x2D,(byte)0x55,(byte)0x53,
282                              (byte)0x48,(byte)0x65,(byte)0x6C,(byte)0x6C,
283                              (byte)0x6F,(byte)0x2C,(byte)0x20,(byte)0x77,
284                              (byte)0x6F,(byte)0x72,(byte)0x6C,(byte)0x64},
285                  ParsedResultType.NDEF_SMART_POSTER);
286   }
287    */
288
289   private static void doTestResult(String contents, String goldenResult, ParsedResultType type) {
290     doTestResult(contents, goldenResult, type, null);
291   }
292
293   private static void doTestResult(String contents, String goldenResult, ParsedResultType type,
294                                    BarcodeFormat format) {
295     Result fakeResult = new Result(contents, null, null, format);
296     ParsedResult result = ResultParser.parseResult(fakeResult);
297     assertNotNull(result);
298     assertEquals(type, result.getType());
299
300     String displayResult = result.getDisplayResult();
301     assertTrue("Expected:\n" + goldenResult + "\nbut got:\n" + displayResult,
302         goldenResult.equals(displayResult));
303   }
304
305   /*
306   private static void doTestResult(byte[] rawBytes, ParsedResultType type) {
307     Result fakeResult = new Result(null, rawBytes, null, null);
308     ParsedResult result = ResultParser.parseResult(fakeResult);
309     assertNotNull(result);
310     assertEquals(type, result.getType());
311   }
312    */
313
314 }