OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" | |
6 | |
7 #include <cstddef> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" | |
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_pr
oblem.h" | |
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" | |
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" | |
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" | |
21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" | |
22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora
ge.h" | |
23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | |
24 #include "third_party/libaddressinput/src/cpp/test/fake_downloader.h" | |
25 | |
26 namespace { | |
27 | |
28 using ::autofill::AddressValidator; | |
29 using ::autofill::LoadRulesListener; | |
30 using ::i18n::addressinput::AddressData; | |
31 using ::i18n::addressinput::AddressField; | |
32 using ::i18n::addressinput::AddressProblem; | |
33 using ::i18n::addressinput::BuildCallback; | |
34 using ::i18n::addressinput::Downloader; | |
35 using ::i18n::addressinput::FakeDownloader; | |
36 using ::i18n::addressinput::FieldProblemMap; | |
37 using ::i18n::addressinput::GetRegionCodes; | |
38 using ::i18n::addressinput::NullStorage; | |
39 using ::i18n::addressinput::Storage; | |
40 | |
41 using ::i18n::addressinput::COUNTRY; | |
42 using ::i18n::addressinput::ADMIN_AREA; | |
43 using ::i18n::addressinput::LOCALITY; | |
44 using ::i18n::addressinput::DEPENDENT_LOCALITY; | |
45 using ::i18n::addressinput::SORTING_CODE; | |
46 using ::i18n::addressinput::POSTAL_CODE; | |
47 using ::i18n::addressinput::STREET_ADDRESS; | |
48 using ::i18n::addressinput::RECIPIENT; | |
49 | |
50 using ::i18n::addressinput::INVALID_FORMAT; | |
51 using ::i18n::addressinput::MISMATCHING_VALUE; | |
52 using ::i18n::addressinput::MISSING_REQUIRED_FIELD; | |
53 using ::i18n::addressinput::UNEXPECTED_FIELD; | |
54 using ::i18n::addressinput::UNKNOWN_VALUE; | |
55 using ::i18n::addressinput::USES_P_O_BOX; | |
56 | |
57 class AddressValidatorTest : public testing::Test, LoadRulesListener { | |
58 protected: | |
59 AddressValidatorTest() | |
60 : validator_( | |
61 new AddressValidator(FakeDownloader::kFakeAggregateDataUrl, | |
62 scoped_ptr<Downloader>(new FakeDownloader), | |
63 scoped_ptr<Storage>(new NullStorage), | |
64 this)) { | |
65 validator_->LoadRules("US"); | |
66 } | |
67 | |
68 virtual ~AddressValidatorTest() {} | |
69 | |
70 const scoped_ptr<AddressValidator> validator_; | |
71 | |
72 private: | |
73 // LoadRulesListener implementation. | |
74 virtual void OnAddressValidationRulesLoaded(const std::string& country_code, | |
75 bool success) OVERRIDE { | |
76 AddressData address_data; | |
77 address_data.region_code = country_code; | |
78 FieldProblemMap dummy; | |
79 AddressValidator::Status status = | |
80 validator_->ValidateAddress(address_data, NULL, &dummy); | |
81 ASSERT_EQ(success, status == AddressValidator::SUCCESS); | |
82 } | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); | |
85 }; | |
86 | |
87 // Use this text fixture if you're going to use a region with a large set of | |
88 // validation rules. All rules should be loaded in SetUpTestCase(). | |
89 class LargeAddressValidatorTest : public testing::Test { | |
90 protected: | |
91 LargeAddressValidatorTest() {} | |
92 virtual ~LargeAddressValidatorTest() {} | |
93 | |
94 static void SetUpTestCase() { | |
95 validator_ = | |
96 new AddressValidator(FakeDownloader::kFakeAggregateDataUrl, | |
97 scoped_ptr<Downloader>(new FakeDownloader), | |
98 scoped_ptr<Storage>(new NullStorage), | |
99 NULL); | |
100 validator_->LoadRules("CN"); | |
101 validator_->LoadRules("KR"); | |
102 validator_->LoadRules("TW"); | |
103 } | |
104 | |
105 static void TearDownTestcase() { | |
106 delete validator_; | |
107 validator_ = NULL; | |
108 } | |
109 | |
110 // Owned shared instance of validator with large sets validation rules. | |
111 static AddressValidator* validator_; | |
112 | |
113 private: | |
114 DISALLOW_COPY_AND_ASSIGN(LargeAddressValidatorTest); | |
115 }; | |
116 | |
117 AddressValidator* LargeAddressValidatorTest::validator_ = NULL; | |
118 | |
119 TEST_F(AddressValidatorTest, RegionHasRules) { | |
120 const std::vector<std::string>& region_codes = GetRegionCodes(); | |
121 AddressData address; | |
122 for (size_t i = 0; i < region_codes.size(); ++i) { | |
123 SCOPED_TRACE("For region: " + region_codes[i]); | |
124 validator_->LoadRules(region_codes[i]); | |
125 address.region_code = region_codes[i]; | |
126 FieldProblemMap dummy; | |
127 EXPECT_EQ(AddressValidator::SUCCESS, | |
128 validator_->ValidateAddress(address, NULL, &dummy)); | |
129 } | |
130 } | |
131 | |
132 TEST_F(AddressValidatorTest, EmptyAddressNoFatalFailure) { | |
133 AddressData address; | |
134 address.region_code = "US"; | |
135 | |
136 FieldProblemMap dummy; | |
137 EXPECT_EQ(AddressValidator::SUCCESS, | |
138 validator_->ValidateAddress(address, NULL, &dummy)); | |
139 } | |
140 | |
141 TEST_F(AddressValidatorTest, UsStateNamesAreValidEntries) { | |
142 AddressData address; | |
143 address.region_code = "US"; | |
144 address.administrative_area = "California"; | |
145 | |
146 FieldProblemMap filter; | |
147 filter.insert(std::make_pair(ADMIN_AREA, UNKNOWN_VALUE)); | |
148 FieldProblemMap problems; | |
149 EXPECT_EQ(AddressValidator::SUCCESS, | |
150 validator_->ValidateAddress(address, &filter, &problems)); | |
151 EXPECT_TRUE(problems.empty()); | |
152 } | |
153 | |
154 TEST_F(AddressValidatorTest, USZipCode) { | |
155 AddressData address; | |
156 address.recipient = "Mr. Smith"; | |
157 address.address_line.push_back("340 Main St."); | |
158 address.locality = "Venice"; | |
159 address.administrative_area = "CA"; | |
160 address.region_code = "US"; | |
161 | |
162 // Valid Californian zip code. | |
163 address.postal_code = "90291"; | |
164 FieldProblemMap problems; | |
165 EXPECT_EQ(AddressValidator::SUCCESS, | |
166 validator_->ValidateAddress(address, NULL, &problems)); | |
167 EXPECT_TRUE(problems.empty()); | |
168 | |
169 problems.clear(); | |
170 | |
171 // An extended, valid Californian zip code. | |
172 address.postal_code = "90210-1234"; | |
173 EXPECT_EQ(AddressValidator::SUCCESS, | |
174 validator_->ValidateAddress(address, NULL, &problems)); | |
175 EXPECT_TRUE(problems.empty()); | |
176 | |
177 problems.clear(); | |
178 | |
179 // New York zip code (which is invalid for California). | |
180 address.postal_code = "12345"; | |
181 EXPECT_EQ(AddressValidator::SUCCESS, | |
182 validator_->ValidateAddress(address, NULL, &problems)); | |
183 EXPECT_EQ(1U, problems.size()); | |
184 EXPECT_EQ(problems.begin()->first, POSTAL_CODE); | |
185 EXPECT_EQ(problems.begin()->second, MISMATCHING_VALUE); | |
186 | |
187 problems.clear(); | |
188 | |
189 // A zip code with a "90" in the middle. | |
190 address.postal_code = "12903"; | |
191 EXPECT_EQ(AddressValidator::SUCCESS, | |
192 validator_->ValidateAddress(address, NULL, &problems)); | |
193 EXPECT_EQ(1U, problems.size()); | |
194 EXPECT_EQ(problems.begin()->first, POSTAL_CODE); | |
195 EXPECT_EQ(problems.begin()->second, MISMATCHING_VALUE); | |
196 | |
197 problems.clear(); | |
198 | |
199 // Invalid zip code (too many digits). | |
200 address.postal_code = "902911"; | |
201 EXPECT_EQ(AddressValidator::SUCCESS, | |
202 validator_->ValidateAddress(address, NULL, &problems)); | |
203 EXPECT_EQ(1U, problems.size()); | |
204 EXPECT_EQ(problems.begin()->first, POSTAL_CODE); | |
205 EXPECT_EQ(problems.begin()->second, INVALID_FORMAT); | |
206 | |
207 problems.clear(); | |
208 | |
209 // Invalid zip code (too few digits). | |
210 address.postal_code = "9029"; | |
211 EXPECT_EQ(AddressValidator::SUCCESS, | |
212 validator_->ValidateAddress(address, NULL, &problems)); | |
213 EXPECT_EQ(1U, problems.size()); | |
214 EXPECT_EQ(problems.begin()->first, POSTAL_CODE); | |
215 EXPECT_EQ(problems.begin()->second, INVALID_FORMAT); | |
216 } | |
217 | |
218 TEST_F(AddressValidatorTest, BasicValidation) { | |
219 // US rules should always be available, even though this load call fails. | |
220 validator_->LoadRules("US"); | |
221 AddressData address; | |
222 address.region_code = "US"; | |
223 address.language_code = "en"; | |
224 address.administrative_area = "TX"; | |
225 address.locality = "Paris"; | |
226 address.postal_code = "75461"; | |
227 address.address_line.push_back("123 Main St"); | |
228 address.recipient = "Mr. Smith"; | |
229 FieldProblemMap problems; | |
230 EXPECT_EQ(AddressValidator::SUCCESS, | |
231 validator_->ValidateAddress(address, NULL, &problems)); | |
232 EXPECT_TRUE(problems.empty()); | |
233 | |
234 // The display name works as well as the key. | |
235 address.administrative_area = "Texas"; | |
236 problems.clear(); | |
237 EXPECT_EQ(AddressValidator::SUCCESS, | |
238 validator_->ValidateAddress(address, NULL, &problems)); | |
239 EXPECT_TRUE(problems.empty()); | |
240 | |
241 // Ignore capitalization. | |
242 address.administrative_area = "tx"; | |
243 problems.clear(); | |
244 EXPECT_EQ(AddressValidator::SUCCESS, | |
245 validator_->ValidateAddress(address, NULL, &problems)); | |
246 EXPECT_TRUE(problems.empty()); | |
247 | |
248 // Ignore capitalization. | |
249 address.administrative_area = "teXas"; | |
250 problems.clear(); | |
251 EXPECT_EQ(AddressValidator::SUCCESS, | |
252 validator_->ValidateAddress(address, NULL, &problems)); | |
253 EXPECT_TRUE(problems.empty()); | |
254 | |
255 // Ignore diacriticals. | |
256 address.administrative_area = "T\u00E9xas"; | |
257 problems.clear(); | |
258 EXPECT_EQ(AddressValidator::SUCCESS, | |
259 validator_->ValidateAddress(address, NULL, &problems)); | |
260 EXPECT_TRUE(problems.empty()); | |
261 } | |
262 | |
263 TEST_F(AddressValidatorTest, BasicValidationFailure) { | |
264 // US rules should always be available, even though this load call fails. | |
265 validator_->LoadRules("US"); | |
266 AddressData address; | |
267 address.region_code = "US"; | |
268 address.language_code = "en"; | |
269 address.administrative_area = "XT"; | |
270 address.locality = "Paris"; | |
271 address.postal_code = "75461"; | |
272 address.address_line.push_back("123 Main St"); | |
273 address.recipient = "Mr. Smith"; | |
274 FieldProblemMap problems; | |
275 EXPECT_EQ(AddressValidator::SUCCESS, | |
276 validator_->ValidateAddress(address, NULL, &problems)); | |
277 | |
278 ASSERT_EQ(1U, problems.size()); | |
279 EXPECT_EQ(UNKNOWN_VALUE, problems.begin()->second); | |
280 EXPECT_EQ(ADMIN_AREA, problems.begin()->first); | |
281 } | |
282 | |
283 TEST_F(AddressValidatorTest, NoNullSuggestionsCrash) { | |
284 AddressData address; | |
285 address.region_code = "US"; | |
286 EXPECT_EQ(AddressValidator::SUCCESS, | |
287 validator_->GetSuggestions(address, COUNTRY, 1, NULL)); | |
288 } | |
289 | |
290 TEST_F(AddressValidatorTest, SuggestAdminAreaForPostalCode) { | |
291 AddressData address; | |
292 address.region_code = "US"; | |
293 address.postal_code = "90291"; | |
294 | |
295 std::vector<AddressData> suggestions; | |
296 EXPECT_EQ(AddressValidator::SUCCESS, | |
297 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
298 ASSERT_EQ(1U, suggestions.size()); | |
299 EXPECT_EQ("CA", suggestions[0].administrative_area); | |
300 EXPECT_EQ("90291", suggestions[0].postal_code); | |
301 } | |
302 | |
303 TEST_F(LargeAddressValidatorTest, SuggestLocalityForPostalCodeWithAdminArea) { | |
304 AddressData address; | |
305 address.region_code = "TW"; | |
306 address.postal_code = "515"; | |
307 address.administrative_area = "Changhua"; | |
308 address.language_code = "zh-Latn"; | |
309 | |
310 std::vector<AddressData> suggestions; | |
311 EXPECT_EQ(AddressValidator::SUCCESS, | |
312 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
313 ASSERT_EQ(1U, suggestions.size()); | |
314 EXPECT_EQ("Dacun Township", suggestions[0].locality); | |
315 EXPECT_EQ("Changhua County", suggestions[0].administrative_area); | |
316 EXPECT_EQ("515", suggestions[0].postal_code); | |
317 } | |
318 | |
319 TEST_F(LargeAddressValidatorTest, SuggestAdminAreaForPostalCodeWithLocality) { | |
320 AddressData address; | |
321 address.region_code = "TW"; | |
322 address.postal_code = "515"; | |
323 address.locality = "Dacun"; | |
324 address.language_code = "zh-Latn"; | |
325 | |
326 std::vector<AddressData> suggestions; | |
327 EXPECT_EQ(AddressValidator::SUCCESS, | |
328 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
329 ASSERT_EQ(1U, suggestions.size()); | |
330 EXPECT_EQ("Dacun Township", suggestions[0].locality); | |
331 EXPECT_EQ("Changhua County", suggestions[0].administrative_area); | |
332 EXPECT_EQ("515", suggestions[0].postal_code); | |
333 } | |
334 | |
335 TEST_F(AddressValidatorTest, NoSuggestForPostalCodeWithWrongAdminArea) { | |
336 AddressData address; | |
337 address.region_code = "US"; | |
338 address.postal_code = "90066"; | |
339 address.postal_code = "TX"; | |
340 | |
341 std::vector<AddressData> suggestions; | |
342 EXPECT_EQ(AddressValidator::SUCCESS, | |
343 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
344 EXPECT_TRUE(suggestions.empty()); | |
345 } | |
346 | |
347 TEST_F(LargeAddressValidatorTest, SuggestForLocality) { | |
348 AddressData address; | |
349 address.region_code = "CN"; | |
350 address.locality = "Anqin"; | |
351 address.language_code = "zh-Latn"; | |
352 | |
353 std::vector<AddressData> suggestions; | |
354 EXPECT_EQ(AddressValidator::SUCCESS, | |
355 validator_->GetSuggestions(address, LOCALITY, 10, &suggestions)); | |
356 ASSERT_EQ(1U, suggestions.size()); | |
357 EXPECT_EQ("Anqing Shi", suggestions[0].locality); | |
358 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area); | |
359 } | |
360 | |
361 TEST_F(LargeAddressValidatorTest, SuggestForLocalityAndAdminArea) { | |
362 AddressData address; | |
363 address.region_code = "CN"; | |
364 address.locality = "Anqing"; | |
365 address.administrative_area = "Anhui"; | |
366 address.language_code = "zh-Latn"; | |
367 | |
368 std::vector<AddressData> suggestions; | |
369 EXPECT_EQ(AddressValidator::SUCCESS, | |
370 validator_->GetSuggestions(address, LOCALITY, 10, &suggestions)); | |
371 ASSERT_EQ(1U, suggestions.size()); | |
372 EXPECT_TRUE(suggestions[0].dependent_locality.empty()); | |
373 EXPECT_EQ("Anqing Shi", suggestions[0].locality); | |
374 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area); | |
375 } | |
376 | |
377 TEST_F(LargeAddressValidatorTest, SuggestForAdminAreaAndLocality) { | |
378 AddressData address; | |
379 address.region_code = "CN"; | |
380 address.locality = "Anqing"; | |
381 address.administrative_area = "Anhui"; | |
382 address.language_code = "zh-Latn"; | |
383 | |
384 std::vector<AddressData> suggestions; | |
385 EXPECT_EQ(AddressValidator::SUCCESS, | |
386 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions)); | |
387 ASSERT_EQ(1U, suggestions.size()); | |
388 EXPECT_TRUE(suggestions[0].dependent_locality.empty()); | |
389 EXPECT_TRUE(suggestions[0].locality.empty()); | |
390 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area); | |
391 } | |
392 | |
393 TEST_F(LargeAddressValidatorTest, SuggestForDependentLocality) { | |
394 AddressData address; | |
395 address.region_code = "CN"; | |
396 address.dependent_locality = "Zongyang"; | |
397 address.language_code = "zh-Latn"; | |
398 | |
399 std::vector<AddressData> suggestions; | |
400 EXPECT_EQ(AddressValidator::SUCCESS, | |
401 validator_->GetSuggestions( | |
402 address, DEPENDENT_LOCALITY, 10, &suggestions)); | |
403 ASSERT_EQ(1U, suggestions.size()); | |
404 EXPECT_EQ("Zongyang Xian", suggestions[0].dependent_locality); | |
405 EXPECT_EQ("Anqing Shi", suggestions[0].locality); | |
406 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area); | |
407 } | |
408 | |
409 TEST_F(LargeAddressValidatorTest, | |
410 NoSuggestForDependentLocalityWithWrongAdminArea) { | |
411 AddressData address; | |
412 address.region_code = "CN"; | |
413 address.dependent_locality = "Zongyang"; | |
414 address.administrative_area = "Sichuan Sheng"; | |
415 address.language_code = "zh-Latn"; | |
416 | |
417 std::vector<AddressData> suggestions; | |
418 EXPECT_EQ(AddressValidator::SUCCESS, | |
419 validator_->GetSuggestions( | |
420 address, DEPENDENT_LOCALITY, 10, &suggestions)); | |
421 EXPECT_TRUE(suggestions.empty()); | |
422 } | |
423 | |
424 TEST_F(AddressValidatorTest, EmptySuggestionsOverLimit) { | |
425 AddressData address; | |
426 address.region_code = "US"; | |
427 address.administrative_area = "A"; | |
428 | |
429 std::vector<AddressData> suggestions; | |
430 EXPECT_EQ(AddressValidator::SUCCESS, | |
431 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions)); | |
432 EXPECT_TRUE(suggestions.empty()); | |
433 } | |
434 | |
435 TEST_F(AddressValidatorTest, PreferShortSuggestions) { | |
436 AddressData address; | |
437 address.region_code = "US"; | |
438 address.administrative_area = "CA"; | |
439 | |
440 std::vector<AddressData> suggestions; | |
441 EXPECT_EQ(AddressValidator::SUCCESS, | |
442 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions)); | |
443 ASSERT_EQ(1U, suggestions.size()); | |
444 EXPECT_EQ("CA", suggestions[0].administrative_area); | |
445 } | |
446 | |
447 TEST_F(AddressValidatorTest, SuggestTheSingleMatchForFullMatchName) { | |
448 AddressData address; | |
449 address.region_code = "US"; | |
450 address.administrative_area = "Texas"; | |
451 | |
452 std::vector<AddressData> suggestions; | |
453 EXPECT_EQ(AddressValidator::SUCCESS, | |
454 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions)); | |
455 ASSERT_EQ(1U, suggestions.size()); | |
456 EXPECT_EQ("Texas", suggestions[0].administrative_area); | |
457 } | |
458 | |
459 TEST_F(AddressValidatorTest, SuggestAdminArea) { | |
460 AddressData address; | |
461 address.region_code = "US"; | |
462 address.administrative_area = "Cali"; | |
463 | |
464 std::vector<AddressData> suggestions; | |
465 EXPECT_EQ(AddressValidator::SUCCESS, | |
466 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions)); | |
467 ASSERT_EQ(1U, suggestions.size()); | |
468 EXPECT_EQ("California", suggestions[0].administrative_area); | |
469 } | |
470 | |
471 TEST_F(AddressValidatorTest, MultipleSuggestions) { | |
472 AddressData address; | |
473 address.region_code = "US"; | |
474 address.administrative_area = "MA"; | |
475 | |
476 std::vector<AddressData> suggestions; | |
477 EXPECT_EQ(AddressValidator::SUCCESS, | |
478 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions)); | |
479 EXPECT_LT(1U, suggestions.size()); | |
480 | |
481 // Massachusetts should not be a suggestion, because it's already covered | |
482 // under MA. | |
483 std::set<std::string> expected_suggestions; | |
484 expected_suggestions.insert("MA"); | |
485 expected_suggestions.insert("Maine"); | |
486 expected_suggestions.insert("Marshall Islands"); | |
487 expected_suggestions.insert("Maryland"); | |
488 for (std::vector<AddressData>::const_iterator it = suggestions.begin(); | |
489 it != suggestions.end(); | |
490 ++it) { | |
491 expected_suggestions.erase(it->administrative_area); | |
492 } | |
493 EXPECT_TRUE(expected_suggestions.empty()); | |
494 } | |
495 | |
496 TEST_F(LargeAddressValidatorTest, SuggestNonLatinKeyWhenLanguageMatches) { | |
497 AddressData address; | |
498 address.language_code = "ko"; | |
499 address.region_code = "KR"; | |
500 address.postal_code = "210-210"; | |
501 | |
502 std::vector<AddressData> suggestions; | |
503 EXPECT_EQ(AddressValidator::SUCCESS, | |
504 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
505 ASSERT_EQ(1U, suggestions.size()); | |
506 EXPECT_EQ("강원도", suggestions[0].administrative_area); | |
507 EXPECT_EQ("210-210", suggestions[0].postal_code); | |
508 } | |
509 | |
510 TEST_F(LargeAddressValidatorTest, SuggestNonLatinKeyWhenUserInputIsNotLatin) { | |
511 AddressData address; | |
512 address.language_code = "en"; | |
513 address.region_code = "KR"; | |
514 address.administrative_area = "강원"; | |
515 | |
516 std::vector<AddressData> suggestions; | |
517 EXPECT_EQ(AddressValidator::SUCCESS, | |
518 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions)); | |
519 ASSERT_EQ(1U, suggestions.size()); | |
520 EXPECT_EQ("강원도", suggestions[0].administrative_area); | |
521 } | |
522 | |
523 TEST_F(LargeAddressValidatorTest, | |
524 SuggestLatinNameWhenLanguageDiffersAndLatinNameAvailable) { | |
525 AddressData address; | |
526 address.language_code = "ko-Latn"; | |
527 address.region_code = "KR"; | |
528 address.postal_code = "210-210"; | |
529 | |
530 std::vector<AddressData> suggestions; | |
531 EXPECT_EQ(AddressValidator::SUCCESS, | |
532 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
533 ASSERT_EQ(1U, suggestions.size()); | |
534 EXPECT_EQ("Gangwon", suggestions[0].administrative_area); | |
535 EXPECT_EQ("210-210", suggestions[0].postal_code); | |
536 } | |
537 | |
538 TEST_F(AddressValidatorTest, NoSuggestionsForEmptyAddress) { | |
539 AddressData address; | |
540 address.region_code = "US"; | |
541 | |
542 std::vector<AddressData> suggestions; | |
543 EXPECT_EQ( | |
544 AddressValidator::SUCCESS, | |
545 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions)); | |
546 EXPECT_TRUE(suggestions.empty()); | |
547 } | |
548 | |
549 TEST_F(AddressValidatorTest, SuggestionIncludesCountry) { | |
550 AddressData address; | |
551 address.region_code = "US"; | |
552 address.postal_code = "90291"; | |
553 | |
554 std::vector<AddressData> suggestions; | |
555 EXPECT_EQ(AddressValidator::SUCCESS, | |
556 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
557 ASSERT_EQ(1U, suggestions.size()); | |
558 EXPECT_EQ("US", suggestions[0].region_code); | |
559 } | |
560 | |
561 TEST_F(AddressValidatorTest, InvalidPostalCodeNoSuggestions) { | |
562 AddressData address; | |
563 address.region_code = "US"; | |
564 address.postal_code = "0"; | |
565 | |
566 std::vector<AddressData> suggestions; | |
567 EXPECT_EQ( | |
568 AddressValidator::SUCCESS, | |
569 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions)); | |
570 EXPECT_TRUE(suggestions.empty()); | |
571 } | |
572 | |
573 TEST_F(AddressValidatorTest, MismatchedPostalCodeNoSuggestions) { | |
574 AddressData address; | |
575 address.region_code = "US"; | |
576 address.administrative_area = "TX"; | |
577 address.postal_code = "90291"; | |
578 | |
579 std::vector<AddressData> suggestions; | |
580 EXPECT_EQ( | |
581 AddressValidator::SUCCESS, | |
582 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions)); | |
583 EXPECT_TRUE(suggestions.empty()); | |
584 } | |
585 | |
586 TEST_F(AddressValidatorTest, SuggestOnlyForAdministrativeAreasAndPostalCode) { | |
587 AddressData address; | |
588 address.region_code = "US"; | |
589 address.administrative_area = "CA"; | |
590 address.locality = "Los Angeles"; | |
591 address.dependent_locality = "Venice"; | |
592 address.postal_code = "90291"; | |
593 address.sorting_code = "123"; | |
594 address.address_line.push_back("123 Main St"); | |
595 address.recipient = "Jon Smith"; | |
596 | |
597 // Fields that should not have suggestions in US. | |
598 static const AddressField kNoSugestFields[] = { | |
599 COUNTRY, | |
600 LOCALITY, | |
601 DEPENDENT_LOCALITY, | |
602 SORTING_CODE, | |
603 STREET_ADDRESS, | |
604 RECIPIENT | |
605 }; | |
606 | |
607 static const size_t kNumNoSuggestFields = | |
608 sizeof kNoSugestFields / sizeof (AddressField); | |
609 | |
610 for (size_t i = 0; i < kNumNoSuggestFields; ++i) { | |
611 std::vector<AddressData> suggestions; | |
612 EXPECT_EQ(AddressValidator::SUCCESS, | |
613 validator_->GetSuggestions( | |
614 address, kNoSugestFields[i], 999, &suggestions)); | |
615 EXPECT_TRUE(suggestions.empty()); | |
616 } | |
617 } | |
618 | |
619 TEST_F(AddressValidatorTest, SuggestionsAreCleared) { | |
620 AddressData address; | |
621 address.region_code = "US"; | |
622 | |
623 std::vector<AddressData> suggestions(1, address); | |
624 EXPECT_EQ(AddressValidator::SUCCESS, | |
625 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions)); | |
626 EXPECT_TRUE(suggestions.empty()); | |
627 } | |
628 | |
629 TEST_F(AddressValidatorTest, CanonicalizeUsAdminAreaName) { | |
630 AddressData address; | |
631 address.region_code = "US"; | |
632 address.administrative_area = "cALIFORNIa"; | |
633 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address)); | |
634 EXPECT_EQ("CA", address.administrative_area); | |
635 } | |
636 | |
637 TEST_F(AddressValidatorTest, CanonicalizeUsAdminAreaKey) { | |
638 AddressData address; | |
639 address.region_code = "US"; | |
640 address.administrative_area = "CA"; | |
641 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address)); | |
642 EXPECT_EQ("CA", address.administrative_area); | |
643 } | |
644 | |
645 TEST_F(AddressValidatorTest, CanonicalizeJpAdminAreaKey) { | |
646 validator_->LoadRules("JP"); | |
647 AddressData address; | |
648 address.region_code = "JP"; | |
649 address.administrative_area = "東京都"; | |
650 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address)); | |
651 EXPECT_EQ("東京都", address.administrative_area); | |
652 } | |
653 | |
654 TEST_F(AddressValidatorTest, CanonicalizeJpAdminAreaLatinName) { | |
655 validator_->LoadRules("JP"); | |
656 AddressData address; | |
657 address.region_code = "JP"; | |
658 address.administrative_area = "tOKYo"; | |
659 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address)); | |
660 EXPECT_EQ("TOKYO", address.administrative_area); | |
661 } | |
662 | |
663 TEST_F(AddressValidatorTest, TokushimaSuggestionIsValid) { | |
664 validator_->LoadRules("JP"); | |
665 AddressData address; | |
666 address.region_code = "JP"; | |
667 address.administrative_area = "Toku"; | |
668 address.language_code = "ja-Latn"; | |
669 | |
670 std::vector<AddressData> suggestions; | |
671 EXPECT_EQ(AddressValidator::SUCCESS, | |
672 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions)); | |
673 ASSERT_EQ(1U, suggestions.size()); | |
674 EXPECT_EQ("TOKUSHIMA", suggestions[0].administrative_area); | |
675 | |
676 FieldProblemMap filter; | |
677 for (int i = UNEXPECTED_FIELD; i <= USES_P_O_BOX; ++i) | |
678 filter.insert(std::make_pair(ADMIN_AREA, static_cast<AddressProblem>(i))); | |
679 | |
680 FieldProblemMap problems; | |
681 EXPECT_EQ(AddressValidator::SUCCESS, | |
682 validator_->ValidateAddress(suggestions[0], &filter, &problems)); | |
683 EXPECT_TRUE(problems.empty()); | |
684 } | |
685 | |
686 TEST_F(AddressValidatorTest, ValidPostalCodeInSuggestion) { | |
687 validator_->LoadRules("US"); | |
688 AddressData address; | |
689 address.region_code = "US"; | |
690 address.administrative_area = "New"; | |
691 address.postal_code = "13699"; | |
692 | |
693 std::vector<AddressData> suggestions; | |
694 EXPECT_EQ(AddressValidator::SUCCESS, | |
695 validator_->GetSuggestions(address, ADMIN_AREA, 999, &suggestions)); | |
696 ASSERT_EQ(1U, suggestions.size()); | |
697 EXPECT_EQ("New York", suggestions[0].administrative_area); | |
698 | |
699 address.administrative_area = "New"; | |
700 address.postal_code = "03755"; | |
701 | |
702 EXPECT_EQ(AddressValidator::SUCCESS, | |
703 validator_->GetSuggestions(address, ADMIN_AREA, 999, &suggestions)); | |
704 ASSERT_EQ(1U, suggestions.size()); | |
705 EXPECT_EQ("New Hampshire", suggestions[0].administrative_area); | |
706 } | |
707 | |
708 TEST_F(AddressValidatorTest, ValidateRequiredFieldsWithoutRules) { | |
709 // Do not load the rules for JP. | |
710 AddressData address; | |
711 address.region_code = "JP"; | |
712 | |
713 FieldProblemMap problems; | |
714 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE, | |
715 validator_->ValidateAddress(address, NULL, &problems)); | |
716 EXPECT_FALSE(problems.empty()); | |
717 | |
718 for (FieldProblemMap::const_iterator it = problems.begin(); | |
719 it != problems.end(); | |
720 ++it) { | |
721 EXPECT_EQ(MISSING_REQUIRED_FIELD, it->second); | |
722 } | |
723 } | |
724 | |
725 TEST_F(AddressValidatorTest, | |
726 DoNotValidateRequiredFieldsWithoutRulesWhenErorrIsFiltered) { | |
727 // Do not load the rules for JP. | |
728 AddressData address; | |
729 address.region_code = "JP"; | |
730 | |
731 FieldProblemMap filter; | |
732 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE)); | |
733 | |
734 FieldProblemMap problems; | |
735 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE, | |
736 validator_->ValidateAddress(address, &filter, &problems)); | |
737 EXPECT_TRUE(problems.empty()); | |
738 } | |
739 | |
740 } // namespace | |
OLD | NEW |