1/*
2 * Copyright (C) 2010 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
17package com.android.i18n.addressinput;
18
19import com.android.i18n.addressinput.testing.AddressDataMapLoader;
20
21import junit.framework.TestCase;
22
23/**
24 * Spot check the standard data set for various cases of interest. This is not an exhaustive test.
25 */
26public class FieldVerifierTest extends TestCase {
27
28    private static final StandardAddressVerifier VERIFIER =
29            new StandardAddressVerifier(new FieldVerifier(
30                    new AddressVerificationData(AddressDataMapLoader.DATA)));
31
32    private AddressProblems problems = new AddressProblems();
33
34    @Override
35    protected void setUp() {
36        problems.clear();
37    }
38
39    public void testUnitedStatesOk() {
40        AddressData addr = new AddressData.Builder().setCountry("US")
41                                                    .setAdminArea("CA")
42                                                    .setLocality("Mountain View")
43                                                    .setAddress("1234 Somewhere")
44                                                    .setPostalCode("94025").build();
45        VERIFIER.verify(addr, problems);
46        assertTrue(problems.toString(), problems.isEmpty());  // no mismatch
47    }
48
49    public void testUnitedStatesZipMismatch() {
50        AddressData addr = new AddressData.Builder().setCountry("US")
51                                                    .setAdminArea("CA")
52                                                    .setLocality("Mountain View")
53                                                    .setPostalCode("12345").build();
54        VERIFIER.verify(addr, problems);
55
56        assertEquals(AddressProblemType.MISMATCHING_VALUE,
57                     problems.getProblem(AddressField.POSTAL_CODE));
58    }
59
60    public void testUnitedStatesNotOk() {
61        AddressData addr = new AddressData.Builder().setCountry("US")
62                                                    .setAdminArea("CA")
63                                                    .setLocality(null)
64                                                    .setDependentLocality("Foo Bar")
65                                                    .setPostalCode("12345").build();
66        VERIFIER.verify(addr, problems);
67
68        assertEquals(AddressProblemType.MISMATCHING_VALUE,
69                     problems.getProblem(AddressField.POSTAL_CODE));
70        assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
71                     problems.getProblem(AddressField.LOCALITY));
72    }
73
74    public void testChinaOk() {
75        AddressData addr = new AddressData.Builder().setCountry("CN")
76                                                    .setAdminArea("Beijing Shi")
77                                                    .setLocality("Xicheng Qu")
78                                                    .setAddress("Yitiao Lu")
79                                                    .setPostalCode("123456").build();
80        VERIFIER.verify(addr, problems);
81        assertTrue(problems.isEmpty());
82    }
83
84    public void testGermanAddress() {
85        AddressData addr = new AddressData.Builder().setCountry("DE")
86                                                    .setLocality("Berlin")
87                                                    .setAddress("Huttenstr. 50")
88                                                    .setPostalCode("10553")
89                                                    .setOrganization("BMW AG Niederkassung Berlin")
90                                                    .setRecipient("Herr Diefendorf").build();
91
92        VERIFIER.verify(addr, problems);
93        assertTrue(problems.isEmpty());
94
95        // Clones address but leave city empty.
96        addr = new AddressData.Builder().set(addr).setLocality(null).build();
97
98        VERIFIER.verify(addr, problems);
99        assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
100                     problems.getProblem(AddressField.LOCALITY));
101    }
102
103    public void testIrishAddress() {
104        AddressData addr = new AddressData.Builder().setCountry("IE")
105                                                    .setLocality("Dublin")
106                                                    .setAdminArea("Co. Dublin")
107                                                    .setAddress("7424 118 Avenue NW")
108                                                    .setRecipient("Conan O'Brien").build();
109
110        VERIFIER.verify(addr, problems);
111        assertTrue(problems.toString(), problems.isEmpty());
112
113        // Clones address but leave county empty. This address should be valid
114        // since county is not required.
115        addr = new AddressData.Builder().set(addr).setAdminArea(null).build();
116
117        VERIFIER.verify(addr, problems);
118        assertTrue(problems.toString(), problems.isEmpty());
119    }
120
121    public void testChinaPostalCodeBadFormat() {
122        AddressData addr = new AddressData.Builder().setCountry("CN")
123                                                    .setAdminArea("Beijing Shi")
124                                                    .setLocality("Xicheng Qu")
125                                                    .setPostalCode("12345").build();
126        VERIFIER.verify(addr, problems);
127
128        assertEquals(AddressProblemType.UNRECOGNIZED_FORMAT,
129                     problems.getProblem(AddressField.POSTAL_CODE));
130    }
131
132    /**
133     * If there is a postal code pattern for a certain country, and the input postal code is empty,
134     * it should not be reported as bad postal code format. Whether empty postal code is ok should
135     * be determined by checks for required fields.
136     */
137    public void testEmptyPostalCodeReportedAsGoodFormat() {
138        // Chilean address has a postal code format pattern, but does not require
139        // postal code. The following address is valid.
140        AddressData addr = new AddressData.Builder().setCountry("CL")
141                                                    .setAddressLine1("GUSTAVO LE PAIGE ST #159")
142                                                    .setAdminArea("Atacama")
143                                                    .setLocality("San Pedro")
144                                                    .setPostalCode("")
145                                                    .build();
146        VERIFIER.verify(addr, problems);
147        assertTrue(problems.toString(), problems.isEmpty());
148
149        problems.clear();
150
151        // Now check for US addresses, which require a postal code. The following
152        // address's postal code is wrong because it is missing a required field, not
153        // because it doesn't match the expected postal code pattern.
154        addr = new AddressData.Builder().setCountry("US").setPostalCode("").build();
155        problems.clear();
156        VERIFIER.verify(addr, problems);
157
158        assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
159                     problems.getProblem(AddressField.POSTAL_CODE));
160    }
161
162    public void testChinaTaiwanOk() {
163        AddressData addr = new AddressData.Builder().setCountry("CN")
164                                                    .setAdminArea("Taiwan")
165                                                    .setLocality("Taichung City")
166                                                    .setDependentLocality("Situn District")
167                                                    .setAddress("12345 Yitiao Lu")
168                                                    .setPostalCode("407").build();
169        VERIFIER.verify(addr, problems);
170        assertTrue(problems.isEmpty());
171    }
172
173    public void testChinaTaiwanUnknownDistrict() {
174        AddressData addr = new AddressData.Builder().setCountry("CN")
175                                                    .setAdminArea("Taiwan")
176                                                    .setLocality("Taichung City")
177                                                    .setDependentLocality("Foo Bar")
178                                                    .setPostalCode("400").build();
179        VERIFIER.verify(addr, problems);
180
181        assertEquals(AddressProblemType.UNKNOWN_VALUE,
182                     problems.getProblem(AddressField.DEPENDENT_LOCALITY));
183    }
184
185    public void testStreetVerification() {
186        // Missing street address
187        AddressData addr = new AddressData.Builder().setCountry("US")
188                                                    .setAdminArea("CA")
189                                                    .setLocality("Mountain View")
190                                                    .setPostalCode("94025").build();
191
192        assertNull(addr.getAddressLine1());
193        assertNull(addr.getAddressLine2());
194
195        VERIFIER.verify(addr, problems);
196
197        assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
198                     problems.getProblem(AddressField.STREET_ADDRESS));
199    }
200
201    // Tests The Bahamas' address
202    public void failingtestBahamas() {
203        final AddressData address =
204                new AddressData.Builder().setAddress("Abaco Beach Resort & Boat Habour")
205                                         .setLocality("Treasure Cay")
206                                         .setAdminArea("Abaco")
207                                         .setCountry("BS").build();
208        VERIFIER.verify(address, problems);
209        assertTrue(problems.isEmpty());
210    }
211
212    public void testJapan() {
213        // Added AdminArea since address verification can't infer it from Kyoto City
214        // Commented out dependent locality since we don't have the data for this and in fact say
215        // that it shouldn't be used for Japan.
216        // TODO: support inference of higher levels from lower ones
217        final AddressData address = new AddressData.Builder()
218                .setRecipient("\u5BAE\u672C \u8302")  // SHIGERU_MIYAMOTO
219                .setAddress("\u4E0A\u9CE5\u7FBD\u927E\u7ACB\u753A11\u756A\u5730")
220                .setAdminArea("\u4eac\u90fd\u5e9c")  // Kyoto prefecture, added
221                .setLocality("\u4EAC\u90FD\u5E02")  // Kyoto city
222                // .setDependentLocality("\u5357\u533A")
223                .setCountry("JP")
224                .setPostalCode("601-8501").build();
225        VERIFIER.verify(address, problems);
226        assertTrue(problems.toString(), problems.isEmpty());
227    }
228
229    public void testJapanLatin() {
230        // added AdminArea since address verification can't infer it from Kyoto City
231        // commented out dependent locality since address verification doesn't use it
232        final AddressData address = new AddressData.Builder()
233            .setRecipient("Shigeru Miyamoto")  // SHIGERU_MIYAMOTO_ENGLISH
234            .setAddress("11-1 Kamitoba-hokotate-cho")
235            .setAdminArea("KYOTO")  // added
236            .setLocality("Kyoto")
237            // .setDependentLocality("Minami-ku")
238            .setLanguageCode("ja_Latn")
239            .setCountry("JP")
240            .setPostalCode("601-8501").build();
241        VERIFIER.verify(address, problems);
242        assertTrue(problems.isEmpty());
243    }
244
245    public void testJapanLatinInvalidAdmin() {
246        final AddressData address = new AddressData.Builder()
247                .setRecipient("Shigeru Miyamoto")  // SHIGERU_MIYAMOTO_ENGLISH
248                .setAddress("11-1 Kamitoba-hokotate-cho")
249                .setAdminArea("Fake Admin")
250                .setLocality("Kyoto")
251                .setLanguageCode("ja_Latn")
252                .setCountry("JP")
253                .setPostalCode("601-8501").build();
254        VERIFIER.verify(address, problems);
255        assertFalse(problems.isEmpty());
256        assertEquals(AddressProblemType.UNKNOWN_VALUE,
257                     problems.getProblem(AddressField.ADMIN_AREA));
258    }
259
260    public void testCanadaMixedCasePostcode() {
261      final AddressData address = new AddressData.Builder()
262              .setRecipient("Joe Bloggs")
263              .setAddress("11 East St")
264              .setLocality("Montreal")
265              .setAdminArea("Quebec")
266              .setCountry("CA")
267              .setPostalCode("H2b 2y5").build();
268      VERIFIER.verify(address, problems);
269      assertTrue(problems.isEmpty());
270  }
271}
272