dc28f71f95a341cd58675ed5a8e59011c3682be2
[zxing.git] / core / test / src / com / google / zxing / client / result / ParsedReaderResultTestCase.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.client.result;
18
19 import junit.framework.TestCase;
20
21 /**
22  * Tests {@link ParsedReaderResult}.
23  *
24  * @author srowen@google.com (Sean Owen)
25  */
26 public final class ParsedReaderResultTestCase extends TestCase {
27
28   public void testTextType() {
29     doTestResult("foo", ParsedReaderResultType.TEXT);
30     doTestResult("", ParsedReaderResultType.TEXT);
31     doTestResult("This is a test", ParsedReaderResultType.TEXT);
32   }
33
34   public void testBookmarkType() {
35     doTestResult("MEBKM:URL:google.com;;", ParsedReaderResultType.BOOKMARK);
36     doTestResult("MEBKM:URL:google.com;TITLE:Google;;", ParsedReaderResultType.BOOKMARK);
37     doTestResult("MEBKM:TITLE:Google;URL:google.com;;", ParsedReaderResultType.BOOKMARK);
38     doTestResult("MEBKM:URL:http://google.com;;", ParsedReaderResultType.BOOKMARK);
39     doTestResult("MEBKM:URL:HTTPS://google.com;;", ParsedReaderResultType.BOOKMARK);
40   }
41
42   public void testURLTOType() {
43     doTestResult("URLTO:foo:bar.com", ParsedReaderResultType.URLTO);
44     doTestResult("URLTO::bar.com", ParsedReaderResultType.URLTO);
45     doTestResult("URLTO::http://bar.com", ParsedReaderResultType.URLTO);
46   }
47
48   public void testEmailType() {
49     doTestResult("MATMSG:TO:srowen@example.org;;", ParsedReaderResultType.EMAIL);
50     doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;;", ParsedReaderResultType.EMAIL);
51     doTestResult("MATMSG:TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedReaderResultType.EMAIL);
52     doTestResult("MATMSG:SUB:Stuff;BODY:This is some text;TO:srowen@example.org;;", ParsedReaderResultType.EMAIL);
53     doTestResult("TO:srowen@example.org;SUB:Stuff;BODY:This is some text;;", ParsedReaderResultType.TEXT);
54   }
55
56   public void testEmailAddressType() {
57     doTestResult("srowen@example.org", ParsedReaderResultType.EMAIL_ADDRESS);
58     doTestResult("mailto:srowen@example.org", ParsedReaderResultType.EMAIL_ADDRESS);
59     doTestResult("srowen@example", ParsedReaderResultType.TEXT);
60     doTestResult("srowen", ParsedReaderResultType.TEXT);
61     doTestResult("Let's meet @ 2", ParsedReaderResultType.TEXT);
62   }
63
64   public void testAddressBookType() {
65     doTestResult("MECARD:N:Sean Owen;;", ParsedReaderResultType.ADDRESSBOOK);
66     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;;", ParsedReaderResultType.ADDRESSBOOK);
67     doTestResult("MECARD:TEL:+12125551212;N:Sean Owen;URL:google.com;;", ParsedReaderResultType.ADDRESSBOOK);
68     doTestResult("TEL:+12125551212;N:Sean Owen;;", ParsedReaderResultType.TEXT);
69   }
70
71   public void testAddressBookAUType() {
72     doTestResult("MEMORY:\r\n", ParsedReaderResultType.ADDRESSBOOK_AU);
73     doTestResult("MEMORY:foo\r\nNAME1:Sean\r\n", ParsedReaderResultType.ADDRESSBOOK_AU);
74     doTestResult("TEL1:+12125551212\r\nMEMORY:\r\n", ParsedReaderResultType.ADDRESSBOOK_AU);
75   }
76
77   public void testUPC() {
78     doTestResult("123456789012", ParsedReaderResultType.UPC);
79     doTestResult("1234567890123", ParsedReaderResultType.UPC);
80     doTestResult("12345678901", ParsedReaderResultType.TEXT);
81   }
82
83   public void testURI() {
84     doTestResult("http://google.com", ParsedReaderResultType.URI);
85     doTestResult("google.com", ParsedReaderResultType.URI);
86     doTestResult("https://google.com", ParsedReaderResultType.URI);
87     doTestResult("HTTP://google.com", ParsedReaderResultType.URI);
88     doTestResult("http://google.com/foobar", ParsedReaderResultType.URI);
89     doTestResult("https://google.com:443/foobar", ParsedReaderResultType.URI);
90     doTestResult("google.com:443/foobar", ParsedReaderResultType.URI);
91   }
92
93   private static void doTestResult(String text, ParsedReaderResultType type) {
94     ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
95     assertNotNull(result);
96     assertEquals(type, result.getType());
97   }
98
99 }