Add more unit tests for client.result, and more small code tweaks.
[zxing.git] / core / test / src / com / google / zxing / client / result / AddressBookParsedResultTestCase.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 junit.framework.TestCase;
20 import com.google.zxing.Result;
21 import com.google.zxing.BarcodeFormat;
22
23 import java.util.Arrays;
24
25 /**
26  * Tests {@link AddressBookParsedResult}.
27  *
28  * @author Sean Owen
29  */
30 public final class AddressBookParsedResultTestCase extends TestCase {
31
32   public void testAddressBookDocomo() {
33     doTest("MECARD:N:Sean Owen;;", null, new String[] {"Sean Owen"}, null, null, null, null, null, null, null, null);
34     doTest("MECARD:NOTE:ZXing Team;N:Sean Owen;URL:google.com;EMAIL:srowen@example.org;;",
35         null, new String[] {"Sean Owen"}, null, null, new String[] {"srowen@example.org"}, null, null,
36         "google.com", null, "ZXing Team");
37   }
38
39   public void testAddressBookAU() {
40     doTest("MEMORY:foo\r\nNAME1:Sean\r\nTEL1:+12125551212\r\n",
41         null, new String[] {"Sean"}, null, null, null, new String[] {"+12125551212"}, null, null, null, "foo");
42   }
43
44   public void testVCard() {
45     doTest("BEGIN:VCARD\r\nADR;HOME:123 Main St\r\nVERSION:2.1\r\nN:Owen;Sean\r\nEND:VCARD",
46            null, new String[] {"Sean Owen"}, null, "123 Main St", null, null, null, null, null, null);
47   }
48
49   public void testBizcard() {
50     doTest("BIZCARD:N:Sean;X:Owen;C:Google;A:123 Main St;M:+12125551212;E:srowen@example.org;",
51         null, new String[] {"Sean Owen"}, null, "123 Main St", new String[] {"srowen@example.org"},
52         new String[] {"+12125551212"}, "Google", null, null, null);
53   }
54
55   private static void doTest(String contents,
56                              String title,
57                              String[] names,
58                              String pronunciation,
59                              String address,
60                              String[] emails,
61                              String[] phoneNumbers,
62                              String org,
63                              String url,
64                              String birthday,
65                              String note) {
66     Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
67     ParsedResult result = ResultParser.parseResult(fakeResult);
68     assertEquals(ParsedResultType.ADDRESSBOOK, result.getType());
69     AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
70     assertEquals(title, addressResult.getTitle());
71     assertTrue(Arrays.equals(names, addressResult.getNames()));
72     assertEquals(pronunciation, addressResult.getPronunciation());
73     assertEquals(address, addressResult.getAddress());
74     assertTrue(Arrays.equals(emails, addressResult.getEmails()));
75     assertTrue(Arrays.equals(phoneNumbers, addressResult.getPhoneNumbers()));
76     assertEquals(org, addressResult.getOrg());
77     assertEquals(url, addressResult.getURL());
78     assertEquals(birthday, addressResult.getBirthday());
79     assertEquals(note, addressResult.getNote());
80   }
81
82 }