Add iCal support, plus many small changes suggested by code inspection -- mostly...
[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 srowen@google.com (Sean Owen)
27  */
28 public final class ParsedReaderResultTestCase extends TestCase {
29
30   public void testTextType() {
31     doTestResult("foo", ParsedResultType.TEXT);
32     doTestResult("", ParsedResultType.TEXT);
33     doTestResult("This is a test", ParsedResultType.TEXT);
34   }
35
36   public void testBookmarkType() {
37     doTestResult("MEBKM:URL:google.com;;", ParsedResultType.URI);
38     doTestResult("MEBKM:URL:google.com;TITLE:Google;;", ParsedResultType.URI);
39     doTestResult("MEBKM:TITLE:Google;URL:google.com;;", ParsedResultType.URI);
40     doTestResult("MEBKM:URL:http://google.com;;", ParsedResultType.URI);
41     doTestResult("MEBKM:URL:HTTPS://google.com;;", ParsedResultType.URI);
42   }
43
44   public void testURLTOType() {
45     doTestResult("URLTO:foo:bar.com", ParsedResultType.URI);
46     doTestResult("URLTO::bar.com", ParsedResultType.URI);
47     doTestResult("URLTO::http://bar.com", ParsedResultType.URI);
48   }
49
50   public void testEmailType() {
51     doTestResult("MATMSG:TO:srowen@example.org;;", ParsedResultType.EMAIL_ADDRESS);
52     doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;;", ParsedResultType.EMAIL_ADDRESS);
53     doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedResultType.EMAIL_ADDRESS);
54     doTestResult("MATMSG:SUB:Stuff;BODY:This is some text;TO:srowen@example.org;;", ParsedResultType.EMAIL_ADDRESS);
55     doTestResult("TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedResultType.TEXT);
56   }
57
58   public void testEmailAddressType() {
59     doTestResult("srowen@example.org", ParsedResultType.EMAIL_ADDRESS);
60     doTestResult("mailto:srowen@example.org", ParsedResultType.EMAIL_ADDRESS);
61     doTestResult("srowen@example", ParsedResultType.TEXT);
62     doTestResult("srowen", ParsedResultType.TEXT);
63     doTestResult("Let's meet @ 2", ParsedResultType.TEXT);
64   }
65
66   public void testAddressBookType() {
67     doTestResult("MECARD:N:Sean Owen;;", ParsedResultType.ADDRESSBOOK);
68     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;;", ParsedResultType.ADDRESSBOOK);
69     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;URL:google.com;;", ParsedResultType.ADDRESSBOOK);
70     doTestResult("TEL:+12125551212;N:Sean Owen;;", ParsedResultType.TEXT);
71   }
72
73   public void testAddressBookAUType() {
74     doTestResult("MEMORY:\r\n", ParsedResultType.ADDRESSBOOK);
75     doTestResult("MEMORY:foo\r\nNAME1:Sean\r\n", ParsedResultType.ADDRESSBOOK);
76     doTestResult("TEL1:+12125551212\r\nMEMORY:\r\n", ParsedResultType.ADDRESSBOOK);
77   }
78
79   public void testUPC() {
80     doTestResult("123456789012", ParsedResultType.UPC, BarcodeFormat.UPC_A);
81     doTestResult("1234567890123", ParsedResultType.UPC, BarcodeFormat.UPC_A);
82     doTestResult("12345678901", ParsedResultType.TEXT);
83   }
84
85   public void testURI() {
86     doTestResult("http://google.com", ParsedResultType.URI);
87     doTestResult("google.com", ParsedResultType.URI);
88     doTestResult("https://google.com", ParsedResultType.URI);
89     doTestResult("HTTP://google.com", ParsedResultType.URI);
90     doTestResult("http://google.com/foobar", ParsedResultType.URI);
91     doTestResult("https://google.com:443/foobar", ParsedResultType.URI);
92     doTestResult("google.com:443/foobar", ParsedResultType.URI);
93   }
94
95   public void testGeo() {
96     doTestResult("geo:1,2", ParsedResultType.GEO);
97     doTestResult("geo:1,2,3", ParsedResultType.GEO);
98     doTestResult("geo:100.33,-32.3344,3.35", ParsedResultType.GEO);
99     doTestResult("geography", ParsedResultType.TEXT);
100   }
101
102   public void testTel() {
103     doTestResult("tel:+15551212", ParsedResultType.TEL);
104     doTestResult("tel:212 555 1212", ParsedResultType.TEL);
105     doTestResult("tel:2125551212", ParsedResultType.TEL);
106     doTestResult("telephone", ParsedResultType.TEXT);
107   }
108
109   public void testVCard() {
110     doTestResult("BEGIN:VCARD\r\nEND:VCARD", ParsedResultType.ADDRESSBOOK);
111     doTestResult("BEGIN:VCARD\r\nN:Owen;Sean\r\nEND:VCARD", ParsedResultType.ADDRESSBOOK);
112     doTestResult("BEGIN:VCARD\r\nVERSION:2.1\r\nN:Owen;Sean\r\nEND:VCARD", ParsedResultType.ADDRESSBOOK);
113     doTestResult("BEGIN:VCARD\r\nADR;HOME:123 Main St\r\nVERSION:2.1\r\nN:Owen;Sean\r\nEND:VCARD", ParsedResultType.ADDRESSBOOK);    
114     doTestResult("BEGIN:VCARD", ParsedResultType.URI); // yeah we end up guessing "URI" here
115   }
116
117   public void testVEvent() {
118     doTestResult("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\nDTEND:20080505T234555Z\r\nEND:VEVENT\r\nEND:VCALENDAR", ParsedResultType.CALENDAR);
119     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\nDTEND:20080505T234555Z\r\nEND:VEVENT", ParsedResultType.CALENDAR);
120     doTestResult("BEGIN:VEVENT\r\nDTEND:20080505T\r\nEND:VEVENT", ParsedResultType.TEXT);
121     doTestResult("BEGIN:VEVENT", ParsedResultType.URI); // See above note on why this is URI
122   }
123
124   public void testSMS() {
125     doTestResult("sms:+15551212", ParsedResultType.SMS);
126     doTestResult("SMSTO:+15551212", ParsedResultType.SMS);
127     doTestResult("smsto:+15551212", ParsedResultType.SMS);
128     doTestResult("sms:+15551212;via=999333", ParsedResultType.SMS);
129     doTestResult("sms:+15551212?subject=foo&body=bar", ParsedResultType.SMS);
130   }
131
132   public void testMMS() {
133     doTestResult("mms:+15551212", ParsedResultType.SMS);
134     doTestResult("MMSTO:+15551212", ParsedResultType.SMS);
135     doTestResult("mmsto:+15551212", ParsedResultType.SMS);
136     doTestResult("mms:+15551212;via=999333", ParsedResultType.SMS);
137     doTestResult("mms:+15551212?subject=foo&body=bar", ParsedResultType.SMS);
138   }
139
140   /*
141   public void testNDEFText() {
142     doTestResult(new byte[] {(byte)0xD1,(byte)0x01,(byte)0x05,(byte)0x54,
143                              (byte)0x02,(byte)0x65,(byte)0x6E,(byte)0x68,
144                              (byte)0x69},
145                  ParsedResultType.TEXT);
146   }
147
148   public void testNDEFURI() {
149     doTestResult(new byte[] {(byte)0xD1,(byte)0x01,(byte)0x08,(byte)0x55,
150                              (byte)0x01,(byte)0x6E,(byte)0x66,(byte)0x63,
151                              (byte)0x2E,(byte)0x63,(byte)0x6F,(byte)0x6D},
152                  ParsedResultType.URI);
153   }
154
155   public void testNDEFSmartPoster() {
156     doTestResult(new byte[] {(byte)0xD1,(byte)0x02,(byte)0x2F,(byte)0x53,
157                              (byte)0x70,(byte)0x91,(byte)0x01,(byte)0x0E,
158                              (byte)0x55,(byte)0x01,(byte)0x6E,(byte)0x66,
159                              (byte)0x63,(byte)0x2D,(byte)0x66,(byte)0x6F,
160                              (byte)0x72,(byte)0x75,(byte)0x6D,(byte)0x2E,
161                              (byte)0x6F,(byte)0x72,(byte)0x67,(byte)0x11,
162                              (byte)0x03,(byte)0x01,(byte)0x61,(byte)0x63,
163                              (byte)0x74,(byte)0x00,(byte)0x51,(byte)0x01,
164                              (byte)0x12,(byte)0x54,(byte)0x05,(byte)0x65,
165                              (byte)0x6E,(byte)0x2D,(byte)0x55,(byte)0x53,
166                              (byte)0x48,(byte)0x65,(byte)0x6C,(byte)0x6C,
167                              (byte)0x6F,(byte)0x2C,(byte)0x20,(byte)0x77,
168                              (byte)0x6F,(byte)0x72,(byte)0x6C,(byte)0x64},
169                  ParsedResultType.NDEF_SMART_POSTER);
170   }
171    */
172
173   private static void doTestResult(String text, ParsedResultType type) {
174     doTestResult(text, type, null);
175   }
176
177   private static void doTestResult(String text, ParsedResultType type, BarcodeFormat format) {
178     Result fakeResult = new Result(text, null, null, format);
179     ParsedResult result = ResultParser.parseResult(fakeResult);
180     assertNotNull(result);
181     assertEquals(type, result.getType());
182   }
183
184   /*
185   private static void doTestResult(byte[] rawBytes, ParsedResultType type) {
186     Result fakeResult = new Result(null, rawBytes, null, null);
187     ParsedResult result = ResultParser.parseResult(fakeResult);
188     assertNotNull(result);
189     assertEquals(type, result.getType());
190   }
191    */
192
193 }