Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / chrome_address_validator_unittest.cc
blobf10bfee2e2dbae7dcbf7bacd0ad1610ca1e73be2
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.
5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
7 #include <cstddef>
8 #include <string>
9 #include <vector>
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_problem.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_storage.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
21 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h"
23 namespace autofill {
25 using ::i18n::addressinput::AddressData;
26 using ::i18n::addressinput::AddressField;
27 using ::i18n::addressinput::AddressProblem;
28 using ::i18n::addressinput::BuildCallback;
29 using ::i18n::addressinput::FieldProblemMap;
30 using ::i18n::addressinput::GetRegionCodes;
31 using ::i18n::addressinput::NullStorage;
32 using ::i18n::addressinput::Source;
33 using ::i18n::addressinput::Storage;
34 using ::i18n::addressinput::TestdataSource;
36 using ::i18n::addressinput::COUNTRY;
37 using ::i18n::addressinput::ADMIN_AREA;
38 using ::i18n::addressinput::LOCALITY;
39 using ::i18n::addressinput::DEPENDENT_LOCALITY;
40 using ::i18n::addressinput::SORTING_CODE;
41 using ::i18n::addressinput::POSTAL_CODE;
42 using ::i18n::addressinput::STREET_ADDRESS;
43 using ::i18n::addressinput::RECIPIENT;
45 using ::i18n::addressinput::INVALID_FORMAT;
46 using ::i18n::addressinput::MISMATCHING_VALUE;
47 using ::i18n::addressinput::MISSING_REQUIRED_FIELD;
48 using ::i18n::addressinput::UNEXPECTED_FIELD;
49 using ::i18n::addressinput::UNKNOWN_VALUE;
50 using ::i18n::addressinput::USES_P_O_BOX;
52 class AddressValidatorTest : public testing::Test, LoadRulesListener {
53 protected:
54 AddressValidatorTest()
55 : validator_(
56 new AddressValidator(scoped_ptr<Source>(new TestdataSource(true)),
57 scoped_ptr<Storage>(new NullStorage),
58 this)) {
59 validator_->LoadRules("US");
62 virtual ~AddressValidatorTest() {}
64 const scoped_ptr<AddressValidator> validator_;
66 private:
67 // LoadRulesListener implementation.
68 virtual void OnAddressValidationRulesLoaded(const std::string& country_code,
69 bool success) override {
70 AddressData address_data;
71 address_data.region_code = country_code;
72 FieldProblemMap dummy;
73 AddressValidator::Status status =
74 validator_->ValidateAddress(address_data, NULL, &dummy);
75 ASSERT_EQ(success, status == AddressValidator::SUCCESS);
78 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest);
81 // Use this test fixture if you're going to use a region with a large set of
82 // validation rules. All rules should be loaded in SetUpTestCase().
83 class LargeAddressValidatorTest : public testing::Test {
84 protected:
85 LargeAddressValidatorTest() {}
86 virtual ~LargeAddressValidatorTest() {}
88 static void SetUpTestCase() {
89 validator_ =
90 new AddressValidator(scoped_ptr<Source>(new TestdataSource(true)),
91 scoped_ptr<Storage>(new NullStorage),
92 NULL);
93 validator_->LoadRules("CN");
94 validator_->LoadRules("KR");
95 validator_->LoadRules("TW");
98 static void TearDownTestcase() {
99 delete validator_;
100 validator_ = NULL;
103 // Owned shared instance of validator with large sets validation rules.
104 static AddressValidator* validator_;
106 private:
107 DISALLOW_COPY_AND_ASSIGN(LargeAddressValidatorTest);
110 AddressValidator* LargeAddressValidatorTest::validator_ = NULL;
112 TEST_F(AddressValidatorTest, RegionHasRules) {
113 const std::vector<std::string>& region_codes = GetRegionCodes();
114 AddressData address;
115 for (size_t i = 0; i < region_codes.size(); ++i) {
116 SCOPED_TRACE("For region: " + region_codes[i]);
117 validator_->LoadRules(region_codes[i]);
118 address.region_code = region_codes[i];
119 FieldProblemMap dummy;
120 EXPECT_EQ(AddressValidator::SUCCESS,
121 validator_->ValidateAddress(address, NULL, &dummy));
125 TEST_F(AddressValidatorTest, EmptyAddressNoFatalFailure) {
126 AddressData address;
127 address.region_code = "US";
129 FieldProblemMap dummy;
130 EXPECT_EQ(AddressValidator::SUCCESS,
131 validator_->ValidateAddress(address, NULL, &dummy));
134 TEST_F(AddressValidatorTest, UsStateNamesAreValidEntries) {
135 AddressData address;
136 address.region_code = "US";
137 address.administrative_area = "California";
139 FieldProblemMap filter;
140 filter.insert(std::make_pair(ADMIN_AREA, UNKNOWN_VALUE));
141 FieldProblemMap problems;
142 EXPECT_EQ(AddressValidator::SUCCESS,
143 validator_->ValidateAddress(address, &filter, &problems));
144 EXPECT_TRUE(problems.empty());
147 TEST_F(AddressValidatorTest, USZipCode) {
148 AddressData address;
149 address.recipient = "Mr. Smith";
150 address.address_line.push_back("340 Main St.");
151 address.locality = "Venice";
152 address.administrative_area = "CA";
153 address.region_code = "US";
155 // Valid Californian zip code.
156 address.postal_code = "90291";
157 FieldProblemMap problems;
158 EXPECT_EQ(AddressValidator::SUCCESS,
159 validator_->ValidateAddress(address, NULL, &problems));
160 EXPECT_TRUE(problems.empty());
162 problems.clear();
164 // An extended, valid Californian zip code.
165 address.postal_code = "90210-1234";
166 EXPECT_EQ(AddressValidator::SUCCESS,
167 validator_->ValidateAddress(address, NULL, &problems));
168 EXPECT_TRUE(problems.empty());
170 problems.clear();
172 // New York zip code (which is invalid for California).
173 address.postal_code = "12345";
174 EXPECT_EQ(AddressValidator::SUCCESS,
175 validator_->ValidateAddress(address, NULL, &problems));
176 EXPECT_EQ(1U, problems.size());
177 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
178 EXPECT_EQ(problems.begin()->second, MISMATCHING_VALUE);
180 problems.clear();
182 // A zip code with a "90" in the middle.
183 address.postal_code = "12903";
184 EXPECT_EQ(AddressValidator::SUCCESS,
185 validator_->ValidateAddress(address, NULL, &problems));
186 EXPECT_EQ(1U, problems.size());
187 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
188 EXPECT_EQ(problems.begin()->second, MISMATCHING_VALUE);
190 problems.clear();
192 // Invalid zip code (too many digits).
193 address.postal_code = "902911";
194 EXPECT_EQ(AddressValidator::SUCCESS,
195 validator_->ValidateAddress(address, NULL, &problems));
196 EXPECT_EQ(1U, problems.size());
197 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
198 EXPECT_EQ(problems.begin()->second, INVALID_FORMAT);
200 problems.clear();
202 // Invalid zip code (too few digits).
203 address.postal_code = "9029";
204 EXPECT_EQ(AddressValidator::SUCCESS,
205 validator_->ValidateAddress(address, NULL, &problems));
206 EXPECT_EQ(1U, problems.size());
207 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
208 EXPECT_EQ(problems.begin()->second, INVALID_FORMAT);
211 TEST_F(AddressValidatorTest, BasicValidation) {
212 // US rules should always be available, even though this load call fails.
213 validator_->LoadRules("US");
214 AddressData address;
215 address.region_code = "US";
216 address.language_code = "en";
217 address.administrative_area = "TX";
218 address.locality = "Paris";
219 address.postal_code = "75461";
220 address.address_line.push_back("123 Main St");
221 address.recipient = "Mr. Smith";
222 FieldProblemMap problems;
223 EXPECT_EQ(AddressValidator::SUCCESS,
224 validator_->ValidateAddress(address, NULL, &problems));
225 EXPECT_TRUE(problems.empty());
227 // The display name works as well as the key.
228 address.administrative_area = "Texas";
229 problems.clear();
230 EXPECT_EQ(AddressValidator::SUCCESS,
231 validator_->ValidateAddress(address, NULL, &problems));
232 EXPECT_TRUE(problems.empty());
234 // Ignore capitalization.
235 address.administrative_area = "tx";
236 problems.clear();
237 EXPECT_EQ(AddressValidator::SUCCESS,
238 validator_->ValidateAddress(address, NULL, &problems));
239 EXPECT_TRUE(problems.empty());
241 // Ignore capitalization.
242 address.administrative_area = "teXas";
243 problems.clear();
244 EXPECT_EQ(AddressValidator::SUCCESS,
245 validator_->ValidateAddress(address, NULL, &problems));
246 EXPECT_TRUE(problems.empty());
248 // Ignore diacriticals.
249 address.administrative_area = base::WideToUTF8(L"T\u00E9xas");
250 problems.clear();
251 EXPECT_EQ(AddressValidator::SUCCESS,
252 validator_->ValidateAddress(address, NULL, &problems));
253 EXPECT_TRUE(problems.empty());
256 TEST_F(AddressValidatorTest, BasicValidationFailure) {
257 // US rules should always be available, even though this load call fails.
258 validator_->LoadRules("US");
259 AddressData address;
260 address.region_code = "US";
261 address.language_code = "en";
262 address.administrative_area = "XT";
263 address.locality = "Paris";
264 address.postal_code = "75461";
265 address.address_line.push_back("123 Main St");
266 address.recipient = "Mr. Smith";
267 FieldProblemMap problems;
268 EXPECT_EQ(AddressValidator::SUCCESS,
269 validator_->ValidateAddress(address, NULL, &problems));
271 ASSERT_EQ(1U, problems.size());
272 EXPECT_EQ(UNKNOWN_VALUE, problems.begin()->second);
273 EXPECT_EQ(ADMIN_AREA, problems.begin()->first);
276 TEST_F(AddressValidatorTest, NoNullSuggestionsCrash) {
277 AddressData address;
278 address.region_code = "US";
279 EXPECT_EQ(AddressValidator::SUCCESS,
280 validator_->GetSuggestions(address, COUNTRY, 1, NULL));
283 TEST_F(AddressValidatorTest, SuggestAdminAreaForPostalCode) {
284 AddressData address;
285 address.region_code = "US";
286 address.postal_code = "90291";
288 std::vector<AddressData> suggestions;
289 EXPECT_EQ(AddressValidator::SUCCESS,
290 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
291 ASSERT_EQ(1U, suggestions.size());
292 EXPECT_EQ("CA", suggestions[0].administrative_area);
293 EXPECT_EQ("90291", suggestions[0].postal_code);
296 TEST_F(LargeAddressValidatorTest, SuggestLocalityForPostalCodeWithAdminArea) {
297 AddressData address;
298 address.region_code = "TW";
299 address.postal_code = "515";
300 address.administrative_area = "Changhua";
301 address.language_code = "zh-Latn";
303 std::vector<AddressData> suggestions;
304 EXPECT_EQ(AddressValidator::SUCCESS,
305 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
306 ASSERT_EQ(1U, suggestions.size());
307 EXPECT_EQ("Dacun Township", suggestions[0].locality);
308 EXPECT_EQ("Changhua County", suggestions[0].administrative_area);
309 EXPECT_EQ("515", suggestions[0].postal_code);
312 TEST_F(LargeAddressValidatorTest, SuggestAdminAreaForPostalCodeWithLocality) {
313 AddressData address;
314 address.region_code = "TW";
315 address.postal_code = "515";
316 address.locality = "Dacun";
317 address.language_code = "zh-Latn";
319 std::vector<AddressData> suggestions;
320 EXPECT_EQ(AddressValidator::SUCCESS,
321 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
322 ASSERT_EQ(1U, suggestions.size());
323 EXPECT_EQ("Dacun Township", suggestions[0].locality);
324 EXPECT_EQ("Changhua County", suggestions[0].administrative_area);
325 EXPECT_EQ("515", suggestions[0].postal_code);
328 TEST_F(AddressValidatorTest, NoSuggestForPostalCodeWithWrongAdminArea) {
329 AddressData address;
330 address.region_code = "US";
331 address.postal_code = "90066";
332 address.postal_code = "TX";
334 std::vector<AddressData> suggestions;
335 EXPECT_EQ(AddressValidator::SUCCESS,
336 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
337 EXPECT_TRUE(suggestions.empty());
340 TEST_F(LargeAddressValidatorTest, SuggestForLocality) {
341 AddressData address;
342 address.region_code = "CN";
343 address.locality = "Anqin";
344 address.language_code = "zh-Latn";
346 std::vector<AddressData> suggestions;
347 EXPECT_EQ(AddressValidator::SUCCESS,
348 validator_->GetSuggestions(address, LOCALITY, 10, &suggestions));
349 ASSERT_EQ(1U, suggestions.size());
350 EXPECT_EQ("Anqing Shi", suggestions[0].locality);
351 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
354 TEST_F(LargeAddressValidatorTest, SuggestForLocalityAndAdminArea) {
355 AddressData address;
356 address.region_code = "CN";
357 address.locality = "Anqing";
358 address.administrative_area = "Anhui";
359 address.language_code = "zh-Latn";
361 std::vector<AddressData> suggestions;
362 EXPECT_EQ(AddressValidator::SUCCESS,
363 validator_->GetSuggestions(address, LOCALITY, 10, &suggestions));
364 ASSERT_EQ(1U, suggestions.size());
365 EXPECT_TRUE(suggestions[0].dependent_locality.empty());
366 EXPECT_EQ("Anqing Shi", suggestions[0].locality);
367 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
370 TEST_F(LargeAddressValidatorTest, SuggestForAdminAreaAndLocality) {
371 AddressData address;
372 address.region_code = "CN";
373 address.locality = "Anqing";
374 address.administrative_area = "Anhui";
375 address.language_code = "zh-Latn";
377 std::vector<AddressData> suggestions;
378 EXPECT_EQ(AddressValidator::SUCCESS,
379 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
380 ASSERT_EQ(1U, suggestions.size());
381 EXPECT_TRUE(suggestions[0].dependent_locality.empty());
382 EXPECT_TRUE(suggestions[0].locality.empty());
383 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
386 TEST_F(LargeAddressValidatorTest, SuggestForDependentLocality) {
387 AddressData address;
388 address.region_code = "CN";
389 address.dependent_locality = "Zongyang";
390 address.language_code = "zh-Latn";
392 std::vector<AddressData> suggestions;
393 EXPECT_EQ(AddressValidator::SUCCESS,
394 validator_->GetSuggestions(
395 address, DEPENDENT_LOCALITY, 10, &suggestions));
396 ASSERT_EQ(1U, suggestions.size());
397 EXPECT_EQ("Zongyang Xian", suggestions[0].dependent_locality);
398 EXPECT_EQ("Anqing Shi", suggestions[0].locality);
399 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
402 TEST_F(LargeAddressValidatorTest,
403 NoSuggestForDependentLocalityWithWrongAdminArea) {
404 AddressData address;
405 address.region_code = "CN";
406 address.dependent_locality = "Zongyang";
407 address.administrative_area = "Sichuan Sheng";
408 address.language_code = "zh-Latn";
410 std::vector<AddressData> suggestions;
411 EXPECT_EQ(AddressValidator::SUCCESS,
412 validator_->GetSuggestions(
413 address, DEPENDENT_LOCALITY, 10, &suggestions));
414 EXPECT_TRUE(suggestions.empty());
417 TEST_F(AddressValidatorTest, EmptySuggestionsOverLimit) {
418 AddressData address;
419 address.region_code = "US";
420 address.administrative_area = "A";
422 std::vector<AddressData> suggestions;
423 EXPECT_EQ(AddressValidator::SUCCESS,
424 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions));
425 EXPECT_TRUE(suggestions.empty());
428 TEST_F(AddressValidatorTest, PreferShortSuggestions) {
429 AddressData address;
430 address.region_code = "US";
431 address.administrative_area = "CA";
433 std::vector<AddressData> suggestions;
434 EXPECT_EQ(AddressValidator::SUCCESS,
435 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
436 ASSERT_EQ(1U, suggestions.size());
437 EXPECT_EQ("CA", suggestions[0].administrative_area);
440 TEST_F(AddressValidatorTest, SuggestTheSingleMatchForFullMatchName) {
441 AddressData address;
442 address.region_code = "US";
443 address.administrative_area = "Texas";
445 std::vector<AddressData> suggestions;
446 EXPECT_EQ(AddressValidator::SUCCESS,
447 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
448 ASSERT_EQ(1U, suggestions.size());
449 EXPECT_EQ("Texas", suggestions[0].administrative_area);
452 TEST_F(AddressValidatorTest, SuggestAdminArea) {
453 AddressData address;
454 address.region_code = "US";
455 address.administrative_area = "Cali";
457 std::vector<AddressData> suggestions;
458 EXPECT_EQ(AddressValidator::SUCCESS,
459 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
460 ASSERT_EQ(1U, suggestions.size());
461 EXPECT_EQ("California", suggestions[0].administrative_area);
464 TEST_F(AddressValidatorTest, MultipleSuggestions) {
465 AddressData address;
466 address.region_code = "US";
467 address.administrative_area = "MA";
469 std::vector<AddressData> suggestions;
470 EXPECT_EQ(AddressValidator::SUCCESS,
471 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
472 EXPECT_LT(1U, suggestions.size());
474 // Massachusetts should not be a suggestion, because it's already covered
475 // under MA.
476 std::set<std::string> expected_suggestions;
477 expected_suggestions.insert("MA");
478 expected_suggestions.insert("Maine");
479 expected_suggestions.insert("Marshall Islands");
480 expected_suggestions.insert("Maryland");
481 for (std::vector<AddressData>::const_iterator it = suggestions.begin();
482 it != suggestions.end();
483 ++it) {
484 expected_suggestions.erase(it->administrative_area);
486 EXPECT_TRUE(expected_suggestions.empty());
489 TEST_F(LargeAddressValidatorTest, SuggestNonLatinKeyWhenLanguageMatches) {
490 AddressData address;
491 address.language_code = "ko";
492 address.region_code = "KR";
493 address.postal_code = "210-210";
495 std::vector<AddressData> suggestions;
496 EXPECT_EQ(AddressValidator::SUCCESS,
497 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
498 ASSERT_EQ(1U, suggestions.size());
499 EXPECT_EQ("강원도", suggestions[0].administrative_area);
500 EXPECT_EQ("210-210", suggestions[0].postal_code);
503 TEST_F(LargeAddressValidatorTest, SuggestNonLatinKeyWhenUserInputIsNotLatin) {
504 AddressData address;
505 address.language_code = "en";
506 address.region_code = "KR";
507 address.administrative_area = "강원";
509 std::vector<AddressData> suggestions;
510 EXPECT_EQ(AddressValidator::SUCCESS,
511 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions));
512 ASSERT_EQ(1U, suggestions.size());
513 EXPECT_EQ("강원도", suggestions[0].administrative_area);
516 TEST_F(LargeAddressValidatorTest,
517 SuggestLatinNameWhenLanguageDiffersAndLatinNameAvailable) {
518 AddressData address;
519 address.language_code = "ko-Latn";
520 address.region_code = "KR";
521 address.postal_code = "210-210";
523 std::vector<AddressData> suggestions;
524 EXPECT_EQ(AddressValidator::SUCCESS,
525 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
526 ASSERT_EQ(1U, suggestions.size());
527 EXPECT_EQ("Gangwon", suggestions[0].administrative_area);
528 EXPECT_EQ("210-210", suggestions[0].postal_code);
531 TEST_F(AddressValidatorTest, NoSuggestionsForEmptyAddress) {
532 AddressData address;
533 address.region_code = "US";
535 std::vector<AddressData> suggestions;
536 EXPECT_EQ(
537 AddressValidator::SUCCESS,
538 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions));
539 EXPECT_TRUE(suggestions.empty());
542 TEST_F(AddressValidatorTest, SuggestionIncludesCountry) {
543 AddressData address;
544 address.region_code = "US";
545 address.postal_code = "90291";
547 std::vector<AddressData> suggestions;
548 EXPECT_EQ(AddressValidator::SUCCESS,
549 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
550 ASSERT_EQ(1U, suggestions.size());
551 EXPECT_EQ("US", suggestions[0].region_code);
554 TEST_F(AddressValidatorTest, InvalidPostalCodeNoSuggestions) {
555 AddressData address;
556 address.region_code = "US";
557 address.postal_code = "0";
559 std::vector<AddressData> suggestions;
560 EXPECT_EQ(
561 AddressValidator::SUCCESS,
562 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions));
563 EXPECT_TRUE(suggestions.empty());
566 TEST_F(AddressValidatorTest, MismatchedPostalCodeNoSuggestions) {
567 AddressData address;
568 address.region_code = "US";
569 address.administrative_area = "TX";
570 address.postal_code = "90291";
572 std::vector<AddressData> suggestions;
573 EXPECT_EQ(
574 AddressValidator::SUCCESS,
575 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions));
576 EXPECT_TRUE(suggestions.empty());
579 TEST_F(AddressValidatorTest, SuggestOnlyForAdministrativeAreasAndPostalCode) {
580 AddressData address;
581 address.region_code = "US";
582 address.administrative_area = "CA";
583 address.locality = "Los Angeles";
584 address.dependent_locality = "Venice";
585 address.postal_code = "90291";
586 address.sorting_code = "123";
587 address.address_line.push_back("123 Main St");
588 address.recipient = "Jon Smith";
590 // Fields that should not have suggestions in US.
591 static const AddressField kNoSugestFields[] = {
592 COUNTRY,
593 LOCALITY,
594 DEPENDENT_LOCALITY,
595 SORTING_CODE,
596 STREET_ADDRESS,
597 RECIPIENT
600 static const size_t kNumNoSuggestFields =
601 sizeof kNoSugestFields / sizeof (AddressField);
603 for (size_t i = 0; i < kNumNoSuggestFields; ++i) {
604 std::vector<AddressData> suggestions;
605 EXPECT_EQ(AddressValidator::SUCCESS,
606 validator_->GetSuggestions(
607 address, kNoSugestFields[i], 999, &suggestions));
608 EXPECT_TRUE(suggestions.empty());
612 TEST_F(AddressValidatorTest, SuggestionsAreCleared) {
613 AddressData address;
614 address.region_code = "US";
616 std::vector<AddressData> suggestions(1, address);
617 EXPECT_EQ(AddressValidator::SUCCESS,
618 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
619 EXPECT_TRUE(suggestions.empty());
622 TEST_F(AddressValidatorTest, CanonicalizeUsAdminAreaName) {
623 AddressData address;
624 address.region_code = "US";
625 address.administrative_area = "cALIFORNIa";
626 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
627 EXPECT_EQ("CA", address.administrative_area);
630 TEST_F(AddressValidatorTest, CanonicalizeUsAdminAreaKey) {
631 AddressData address;
632 address.region_code = "US";
633 address.administrative_area = "CA";
634 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
635 EXPECT_EQ("CA", address.administrative_area);
638 TEST_F(AddressValidatorTest, CanonicalizeJpAdminAreaKey) {
639 validator_->LoadRules("JP");
640 AddressData address;
641 address.region_code = "JP";
642 address.administrative_area = "東京都";
643 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
644 EXPECT_EQ("東京都", address.administrative_area);
647 TEST_F(AddressValidatorTest, CanonicalizeJpAdminAreaLatinName) {
648 validator_->LoadRules("JP");
649 AddressData address;
650 address.region_code = "JP";
651 address.administrative_area = "tOKYo";
652 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
653 EXPECT_EQ("TOKYO", address.administrative_area);
656 TEST_F(AddressValidatorTest, TokushimaSuggestionIsValid) {
657 validator_->LoadRules("JP");
658 AddressData address;
659 address.region_code = "JP";
660 address.administrative_area = "Toku";
661 address.language_code = "ja-Latn";
663 std::vector<AddressData> suggestions;
664 EXPECT_EQ(AddressValidator::SUCCESS,
665 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions));
666 ASSERT_EQ(1U, suggestions.size());
667 EXPECT_EQ("TOKUSHIMA", suggestions[0].administrative_area);
669 FieldProblemMap filter;
670 for (int i = UNEXPECTED_FIELD; i <= USES_P_O_BOX; ++i)
671 filter.insert(std::make_pair(ADMIN_AREA, static_cast<AddressProblem>(i)));
673 FieldProblemMap problems;
674 EXPECT_EQ(AddressValidator::SUCCESS,
675 validator_->ValidateAddress(suggestions[0], &filter, &problems));
676 EXPECT_TRUE(problems.empty());
679 TEST_F(AddressValidatorTest, ValidPostalCodeInSuggestion) {
680 validator_->LoadRules("US");
681 AddressData address;
682 address.region_code = "US";
683 address.administrative_area = "New";
684 address.postal_code = "13699";
686 std::vector<AddressData> suggestions;
687 EXPECT_EQ(AddressValidator::SUCCESS,
688 validator_->GetSuggestions(address, ADMIN_AREA, 999, &suggestions));
689 ASSERT_EQ(1U, suggestions.size());
690 EXPECT_EQ("New York", suggestions[0].administrative_area);
692 address.administrative_area = "New";
693 address.postal_code = "03755";
695 EXPECT_EQ(AddressValidator::SUCCESS,
696 validator_->GetSuggestions(address, ADMIN_AREA, 999, &suggestions));
697 ASSERT_EQ(1U, suggestions.size());
698 EXPECT_EQ("New Hampshire", suggestions[0].administrative_area);
701 TEST_F(AddressValidatorTest, ValidateRequiredFieldsWithoutRules) {
702 // Do not load the rules for JP.
703 AddressData address;
704 address.region_code = "JP";
706 FieldProblemMap problems;
707 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE,
708 validator_->ValidateAddress(address, NULL, &problems));
709 EXPECT_FALSE(problems.empty());
711 for (FieldProblemMap::const_iterator it = problems.begin();
712 it != problems.end();
713 ++it) {
714 EXPECT_EQ(MISSING_REQUIRED_FIELD, it->second);
718 TEST_F(AddressValidatorTest,
719 DoNotValidateRequiredFieldsWithoutRulesWhenErorrIsFiltered) {
720 // Do not load the rules for JP.
721 AddressData address;
722 address.region_code = "JP";
724 FieldProblemMap filter;
725 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE));
727 FieldProblemMap problems;
728 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE,
729 validator_->ValidateAddress(address, &filter, &problems));
730 EXPECT_TRUE(problems.empty());
733 // Use this test fixture for configuring the number of failed attempts to load
734 // rules.
735 class FailingAddressValidatorTest : public testing::Test, LoadRulesListener {
736 protected:
737 // A validator that retries loading rules without delay.
738 class TestAddressValidator : public AddressValidator {
739 public:
740 // Takes ownership of |source| and |storage|.
741 TestAddressValidator(
742 scoped_ptr< ::i18n::addressinput::Source> source,
743 scoped_ptr< ::i18n::addressinput::Storage> storage,
744 LoadRulesListener* load_rules_listener)
745 : AddressValidator(source.Pass(),
746 storage.Pass(),
747 load_rules_listener) {}
749 virtual ~TestAddressValidator() {}
751 protected:
752 virtual base::TimeDelta GetBaseRetryPeriod() const override {
753 return base::TimeDelta::FromSeconds(0);
756 private:
757 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator);
760 // A source that always fails |failures_number| times before downloading
761 // data.
762 class FailingSource : public Source {
763 public:
764 explicit FailingSource()
765 : failures_number_(0), attempts_number_(0), actual_source_(true) {}
766 virtual ~FailingSource() {}
768 // Sets the number of times to fail before downloading data.
769 void set_failures_number(int failures_number) {
770 failures_number_ = failures_number;
773 // Source implementation.
774 // Always fails for the first |failures_number| times.
775 virtual void Get(const std::string& url,
776 const Callback& callback) const override {
777 ++attempts_number_;
778 // |callback| takes ownership of the |new std::string|.
779 if (failures_number_-- > 0)
780 callback(false, url, new std::string);
781 else
782 actual_source_.Get(url, callback);
785 // Returns the number of download attempts.
786 int attempts_number() const { return attempts_number_; }
788 private:
789 // The number of times to fail before downloading data.
790 mutable int failures_number_;
792 // The number of times Get was called.
793 mutable int attempts_number_;
795 // The source to use for successful downloads.
796 TestdataSource actual_source_;
798 DISALLOW_COPY_AND_ASSIGN(FailingSource);
801 FailingAddressValidatorTest()
802 : source_(new FailingSource),
803 validator_(
804 new TestAddressValidator(scoped_ptr<Source>(source_),
805 scoped_ptr<Storage>(new NullStorage),
806 this)),
807 load_rules_success_(false) {}
809 virtual ~FailingAddressValidatorTest() {}
811 FailingSource* source_; // Owned by |validator_|.
812 scoped_ptr<AddressValidator> validator_;
813 bool load_rules_success_;
815 private:
816 // LoadRulesListener implementation.
817 virtual void OnAddressValidationRulesLoaded(const std::string&,
818 bool success) override {
819 load_rules_success_ = success;
822 base::MessageLoop ui_;
824 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest);
827 // The validator will attempt to load rules at most 8 times.
828 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) {
829 source_->set_failures_number(99);
830 validator_->LoadRules("CH");
831 base::RunLoop().RunUntilIdle();
833 EXPECT_FALSE(load_rules_success_);
834 EXPECT_EQ(8, source_->attempts_number());
837 // The validator will load rules successfully if the source returns data
838 // before the maximum number of retries.
839 TEST_F(FailingAddressValidatorTest, RuleRetryingWillSucceed) {
840 source_->set_failures_number(4);
841 validator_->LoadRules("CH");
842 base::RunLoop().RunUntilIdle();
844 EXPECT_TRUE(load_rules_success_);
845 EXPECT_EQ(5, source_->attempts_number());
848 // The delayed task to retry loading rules should stop (instead of crashing) if
849 // the validator is destroyed before it fires.
850 TEST_F(FailingAddressValidatorTest, DestroyedValidatorStopsRetries) {
851 source_->set_failures_number(4);
852 validator_->LoadRules("CH");
854 // Destroy the validator.
855 validator_.reset();
857 // Fire the delayed task to retry loading rules.
858 EXPECT_NO_FATAL_FAILURE(base::RunLoop().RunUntilIdle());
861 // Each call to LoadRules should reset the number of retry attempts. If the
862 // first call to LoadRules exceeded the maximum number of retries, the second
863 // call to LoadRules should start counting the retries from zero.
864 TEST_F(FailingAddressValidatorTest, LoadingRulesSecondTimeSucceeds) {
865 source_->set_failures_number(11);
866 validator_->LoadRules("CH");
867 base::RunLoop().RunUntilIdle();
869 EXPECT_FALSE(load_rules_success_);
870 EXPECT_EQ(8, source_->attempts_number());
872 validator_->LoadRules("CH");
873 base::RunLoop().RunUntilIdle();
875 EXPECT_TRUE(load_rules_success_);
876 EXPECT_EQ(12, source_->attempts_number());
879 // Calling LoadRules("CH") and LoadRules("GB") simultaneously should attempt to
880 // load both rules up to the maximum number of attempts for each region.
881 TEST_F(FailingAddressValidatorTest, RegionsShouldRetryIndividually) {
882 source_->set_failures_number(99);
883 validator_->LoadRules("CH");
884 validator_->LoadRules("GB");
885 base::RunLoop().RunUntilIdle();
887 EXPECT_FALSE(load_rules_success_);
888 EXPECT_EQ(16, source_->attempts_number());
891 } // namespace autofill