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