Per dominik.wild, added support for "sms:number:body" format URIs
[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\n" +
114                  "END:VCARD", ParsedResultType.ADDRESSBOOK);    
115     doTestResult("BEGIN:VCARD", ParsedResultType.URI); // yeah we end up guessing "URI" here
116   }
117
118   public void testVEvent() {
119     doTestResult("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" +
120                  "DTEND:20080505T234555Z\r\nEND:VEVENT\r\nEND:VCALENDAR", ParsedResultType.CALENDAR);
121     doTestResult("BEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" +
122                  "DTEND:20080505T234555Z\r\nEND:VEVENT", ParsedResultType.CALENDAR);
123     doTestResult("BEGIN:VEVENT\r\nDTEND:20080505T\r\nEND:VEVENT", ParsedResultType.TEXT);
124     doTestResult("BEGIN:VEVENT", ParsedResultType.URI); // See above note on why this is URI
125   }
126
127   public void testSMS() {
128     doTestResult("sms:+15551212", ParsedResultType.SMS);
129     doTestResult("SMSTO:+15551212", ParsedResultType.SMS);
130     doTestResult("smsto:+15551212", ParsedResultType.SMS);
131     doTestResult("sms:+15551212;via=999333", ParsedResultType.SMS);
132     doTestResult("sms:+15551212?subject=foo&body=bar", ParsedResultType.SMS);
133     doTestResult("sms:+15551212:subject", ParsedResultType.SMS);
134   }
135
136   public void testMMS() {
137     doTestResult("mms:+15551212", ParsedResultType.SMS);
138     doTestResult("MMSTO:+15551212", ParsedResultType.SMS);
139     doTestResult("mmsto:+15551212", ParsedResultType.SMS);
140     doTestResult("mms:+15551212;via=999333", ParsedResultType.SMS);
141     doTestResult("mms:+15551212?subject=foo&body=bar", ParsedResultType.SMS);
142     doTestResult("mms:+15551212:subject", ParsedResultType.SMS);
143   }
144
145   /*
146   public void testNDEFText() {
147     doTestResult(new byte[] {(byte)0xD1,(byte)0x01,(byte)0x05,(byte)0x54,
148                              (byte)0x02,(byte)0x65,(byte)0x6E,(byte)0x68,
149                              (byte)0x69},
150                  ParsedResultType.TEXT);
151   }
152
153   public void testNDEFURI() {
154     doTestResult(new byte[] {(byte)0xD1,(byte)0x01,(byte)0x08,(byte)0x55,
155                              (byte)0x01,(byte)0x6E,(byte)0x66,(byte)0x63,
156                              (byte)0x2E,(byte)0x63,(byte)0x6F,(byte)0x6D},
157                  ParsedResultType.URI);
158   }
159
160   public void testNDEFSmartPoster() {
161     doTestResult(new byte[] {(byte)0xD1,(byte)0x02,(byte)0x2F,(byte)0x53,
162                              (byte)0x70,(byte)0x91,(byte)0x01,(byte)0x0E,
163                              (byte)0x55,(byte)0x01,(byte)0x6E,(byte)0x66,
164                              (byte)0x63,(byte)0x2D,(byte)0x66,(byte)0x6F,
165                              (byte)0x72,(byte)0x75,(byte)0x6D,(byte)0x2E,
166                              (byte)0x6F,(byte)0x72,(byte)0x67,(byte)0x11,
167                              (byte)0x03,(byte)0x01,(byte)0x61,(byte)0x63,
168                              (byte)0x74,(byte)0x00,(byte)0x51,(byte)0x01,
169                              (byte)0x12,(byte)0x54,(byte)0x05,(byte)0x65,
170                              (byte)0x6E,(byte)0x2D,(byte)0x55,(byte)0x53,
171                              (byte)0x48,(byte)0x65,(byte)0x6C,(byte)0x6C,
172                              (byte)0x6F,(byte)0x2C,(byte)0x20,(byte)0x77,
173                              (byte)0x6F,(byte)0x72,(byte)0x6C,(byte)0x64},
174                  ParsedResultType.NDEF_SMART_POSTER);
175   }
176    */
177
178   private static void doTestResult(String text, ParsedResultType type) {
179     doTestResult(text, type, null);
180   }
181
182   private static void doTestResult(String text, ParsedResultType type, BarcodeFormat format) {
183     Result fakeResult = new Result(text, null, null, format);
184     ParsedResult result = ResultParser.parseResult(fakeResult);
185     assertNotNull(result);
186     assertEquals(type, result.getType());
187   }
188
189   /*
190   private static void doTestResult(byte[] rawBytes, ParsedResultType type) {
191     Result fakeResult = new Result(null, rawBytes, null, null);
192     ParsedResult result = ResultParser.parseResult(fakeResult);
193     assertNotNull(result);
194     assertEquals(type, result.getType());
195   }
196    */
197
198 }