1/*
2 * Copyright (C) 2009 The Android Open Source Project
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
17package com.android.providers.contacts;
18
19import android.test.suitebuilder.annotation.SmallTest;
20
21import com.android.providers.contacts.PostalSplitter.Postal;
22
23import junit.framework.TestCase;
24
25import java.util.Locale;
26
27/**
28 * Tests for {@link PostalSplitter}, especially for en_US locale.
29 *
30 * Run the test like this:
31 * <code>
32 * adb shell am instrument -e class com.android.providers.contacts.PostalSplitterTest -w \
33 *         com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
34 * </code>
35 */
36@SmallTest
37public class PostalSplitterTest extends TestCase {
38    private PostalSplitter mPostalSplitter;
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43
44        mPostalSplitter = new PostalSplitter(Locale.US);
45    }
46
47    public void testNull() {
48        assertSplitPostal(null, null, null, null, null, null, null, null);
49        assertJoinedPostal(null, null, null, null, null, null, null, null);
50    }
51
52    public void testEmpty() {
53        assertSplitPostal("", null, null, null, null, null, null, null);
54        assertJoinedPostal(null, null, null, null, null, null, null, null);
55    }
56
57    public void testSpaces() {
58        assertSplitPostal(" ", " ", null, null, null, null, null, null);
59        assertJoinedPostal(" ", " ", null, null, null, null, null, null);
60    }
61
62    public void testPobox() {
63        assertJoinedPostal("PO Box 2600\nImaginationland", null, "PO Box 2600", null,
64                "Imaginationland", null, null, null);
65    }
66
67    public void testNormal() {
68        assertJoinedPostal("1600 Amphitheatre Parkway\nMountain View, CA 94043",
69                "1600 Amphitheatre Parkway", null, null, "Mountain View", "CA", "94043", null);
70    }
71
72    public void testMissingRegion() {
73        assertJoinedPostal("1600 Amphitheatre Parkway\nMountain View 94043",
74                "1600 Amphitheatre Parkway", null, null, "Mountain View", null, "94043", null);
75
76        assertJoinedPostal("1600 Amphitheatre Parkway\n94043",
77                "1600 Amphitheatre Parkway", null, null, null, null, "94043", null);
78
79        assertJoinedPostal("1600 Amphitheatre Parkway\n94043\nUSA",
80                "1600 Amphitheatre Parkway", null, null, null, null, "94043", "USA");
81    }
82
83    public void testMissingPostcode() {
84        assertJoinedPostal("1600 Amphitheatre Parkway\nMountain View, CA",
85                "1600 Amphitheatre Parkway", null, null, "Mountain View", "CA", null, null);
86
87        assertJoinedPostal("1600 Amphitheatre Parkway\nMountain View, CA\nUSA",
88                "1600 Amphitheatre Parkway", null, null, "Mountain View", "CA", null, "USA");
89
90        assertJoinedPostal("1600 Amphitheatre Parkway\nUSA",
91                "1600 Amphitheatre Parkway", null, null, null, null, null, "USA");
92    }
93
94    public void testMissingStreet() {
95        assertJoinedPostal("Mr. Rogers\nUSA", null, null, "Mr. Rogers", null, null, null, "USA");
96    }
97
98    private void assertSplitPostal(String formattedPostal, String street, String pobox,
99            String neighborhood, String city, String region, String postcode, String country) {
100        final Postal postal = new Postal();
101        mPostalSplitter.split(postal, formattedPostal);
102        assertEquals(street, postal.street);
103        assertEquals(pobox, postal.pobox);
104        assertEquals(neighborhood, postal.neighborhood);
105        assertEquals(city, postal.city);
106        assertEquals(region, postal.region);
107        assertEquals(postcode, postal.postcode);
108        assertEquals(country, postal.country);
109    }
110
111    private void assertJoinedPostal(String formattedPostal, String street, String pobox,
112            String neighborhood, String city, String region, String postcode, String country) {
113        final Postal postal = new Postal();
114        postal.street = street;
115        postal.pobox = pobox;
116        postal.neighborhood = neighborhood;
117        postal.city = city;
118        postal.region = region;
119        postal.postcode = postcode;
120        postal.country = country;
121
122        final String joined = mPostalSplitter.join(postal);
123        assertEquals(formattedPostal, joined);
124    }
125}
126