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 ja_JP locale.
29 * This class depends on the assumption that all the tests in {@link NameSplitterTest} pass.
30 *
31 * Run the test like this:
32 * <code>
33 * adb shell am instrument -e class com.android.providers.contacts.PostalSplitterForJapaneseTest -w
34 *         com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
35 * </code>
36 */
37@SmallTest
38public class PostalSplitterForJapaneseTest extends TestCase {
39    private PostalSplitter mPostalSplitter;
40
41    // Postal address for Tokyo Metropolitan City Hall (Tokyo-Tocho) as of 2009 + pseudo PO box.
42    // Japanese don't use neighborhood, so it is not used in this test suite.
43    //
44    // "Nihon" in Kanji
45    private static final String COUNTRY = "\u65E5\u672C";
46    private static final String POSTCODE = "163-8001";
47    // "Tokyo-to" in Kanji
48    private static final String REGION = "\u6771\u4EAC\u90FD";
49    // "Sinjuku-ku" in Kanji
50    private static final String CITY = "\u65B0\u5BBF\u533A";
51    // Nishi-Sinjuku 2-8-1
52    private static final String STREET = "\u897F\u65B0\u5BBF 2-8-1";
53    // Pseudo PO box for test: "Sisyobako 404"
54    private static final String POBOX = "\u79C1\u66F8\u7BB1";
55
56    @Override
57    protected void setUp() throws Exception {
58        super.setUp();
59
60        mPostalSplitter = new PostalSplitter(Locale.JAPAN);
61    }
62
63    public void testNull() {
64        assertSplitPostal(null, null, null, null, null, null, null, null);
65        assertJoinedPostal(null, null, null, null, null, null, null, null);
66    }
67
68    public void testEmpty() {
69        assertSplitPostal("", null, null, null, null, null, null, null);
70        assertJoinedPostal(null, null, null, null, null, null, null, null);
71    }
72
73    public void testSpaces() {
74        assertSplitPostal(" ", " ", null, null, null, null, null, null);
75        assertJoinedPostal(" ", " ", null, null, null, null, null, null);
76    }
77
78    public void testPobox() {
79        assertJoinedPostal(CITY + "\n" + POBOX, null, POBOX, null, CITY, null, null, null);
80    }
81
82    public void testNormal() {
83        assertJoinedPostal(POSTCODE + "\n" + REGION + " " + CITY + "\n" + STREET,
84                STREET, null, null, CITY, REGION, POSTCODE, null);
85    }
86
87    public void testMissingRegion() {
88        assertJoinedPostal(POSTCODE + "\n" + REGION + "\n" + STREET,
89                STREET, null, null, REGION, null, POSTCODE, null);
90
91        assertJoinedPostal(POSTCODE + "\n" + STREET,
92                STREET, null, null, null, null, POSTCODE, null);
93
94        assertJoinedPostal(COUNTRY + " " + POSTCODE + "\n" + STREET,
95                STREET, null, null, null, null, POSTCODE, COUNTRY);
96    }
97
98    public void testMissingPostcode() {
99        assertJoinedPostal(REGION + " " + CITY + "\n" + STREET,
100                STREET, null, null, CITY, REGION, null, null);
101
102        assertJoinedPostal(COUNTRY + "\n" + REGION + " " + CITY + "\n" + STREET,
103                STREET, null, null, CITY, REGION, null, COUNTRY);
104
105        assertJoinedPostal(COUNTRY + "\n" + STREET,
106                STREET, null, null, null, null, null, COUNTRY);
107    }
108
109    public void testMissingStreet() {
110        assertJoinedPostal(COUNTRY + "\n" + STREET,
111                null, null, STREET, null, null, null, COUNTRY);
112    }
113
114    private void assertSplitPostal(String formattedPostal, String street, String pobox,
115            String neighborhood, String city, String region, String postcode, String country) {
116        final Postal postal = new Postal();
117        mPostalSplitter.split(postal, formattedPostal);
118        assertEquals(street, postal.street);
119        assertEquals(pobox, postal.pobox);
120        assertEquals(neighborhood, postal.neighborhood);
121        assertEquals(city, postal.city);
122        assertEquals(region, postal.region);
123        assertEquals(postcode, postal.postcode);
124        assertEquals(country, postal.country);
125    }
126
127    private void assertJoinedPostal(String formattedPostal, String street, String pobox,
128            String neighborhood, String city, String region, String postcode, String country) {
129        final Postal postal = new Postal();
130        postal.street = street;
131        postal.pobox = pobox;
132        postal.neighborhood = neighborhood;
133        postal.city = city;
134        postal.region = region;
135        postal.postcode = postcode;
136        postal.country = country;
137
138        final String joined = mPostalSplitter.join(postal);
139        assertEquals(formattedPostal, joined);
140    }
141}
142