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"
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"
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
{
54 AddressValidatorTest()
56 new AddressValidator(scoped_ptr
<Source
>(new TestdataSource(true)),
57 scoped_ptr
<Storage
>(new NullStorage
),
59 validator_
->LoadRules("US");
62 virtual ~AddressValidatorTest() {}
64 const scoped_ptr
<AddressValidator
> validator_
;
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
{
85 LargeAddressValidatorTest() {}
86 virtual ~LargeAddressValidatorTest() {}
88 static void SetUpTestCase() {
90 new AddressValidator(scoped_ptr
<Source
>(new TestdataSource(true)),
91 scoped_ptr
<Storage
>(new NullStorage
),
93 validator_
->LoadRules("CN");
94 validator_
->LoadRules("KR");
95 validator_
->LoadRules("TW");
98 static void TearDownTestcase() {
103 // Owned shared instance of validator with large sets validation rules.
104 static AddressValidator
* validator_
;
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();
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
) {
127 address
.region_code
= "US";
129 FieldProblemMap dummy
;
130 EXPECT_EQ(AddressValidator::SUCCESS
,
131 validator_
->ValidateAddress(address
, NULL
, &dummy
));
134 TEST_F(AddressValidatorTest
, UsStateNamesAreValidEntries
) {
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
) {
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());
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());
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
);
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
);
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
);
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");
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";
230 EXPECT_EQ(AddressValidator::SUCCESS
,
231 validator_
->ValidateAddress(address
, NULL
, &problems
));
232 EXPECT_TRUE(problems
.empty());
234 // Ignore capitalization.
235 address
.administrative_area
= "tx";
237 EXPECT_EQ(AddressValidator::SUCCESS
,
238 validator_
->ValidateAddress(address
, NULL
, &problems
));
239 EXPECT_TRUE(problems
.empty());
241 // Ignore capitalization.
242 address
.administrative_area
= "teXas";
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");
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");
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
) {
278 address
.region_code
= "US";
279 EXPECT_EQ(AddressValidator::SUCCESS
,
280 validator_
->GetSuggestions(address
, COUNTRY
, 1, NULL
));
283 TEST_F(AddressValidatorTest
, SuggestAdminAreaForPostalCode
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
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();
484 expected_suggestions
.erase(it
->administrative_area
);
486 EXPECT_TRUE(expected_suggestions
.empty());
489 TEST_F(LargeAddressValidatorTest
, SuggestNonLatinKeyWhenLanguageMatches
) {
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
) {
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
) {
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
) {
533 address
.region_code
= "US";
535 std::vector
<AddressData
> suggestions
;
537 AddressValidator::SUCCESS
,
538 validator_
->GetSuggestions(address
, POSTAL_CODE
, 999, &suggestions
));
539 EXPECT_TRUE(suggestions
.empty());
542 TEST_F(AddressValidatorTest
, SuggestionIncludesCountry
) {
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
) {
556 address
.region_code
= "US";
557 address
.postal_code
= "0";
559 std::vector
<AddressData
> suggestions
;
561 AddressValidator::SUCCESS
,
562 validator_
->GetSuggestions(address
, POSTAL_CODE
, 999, &suggestions
));
563 EXPECT_TRUE(suggestions
.empty());
566 TEST_F(AddressValidatorTest
, MismatchedPostalCodeNoSuggestions
) {
568 address
.region_code
= "US";
569 address
.administrative_area
= "TX";
570 address
.postal_code
= "90291";
572 std::vector
<AddressData
> suggestions
;
574 AddressValidator::SUCCESS
,
575 validator_
->GetSuggestions(address
, POSTAL_CODE
, 999, &suggestions
));
576 EXPECT_TRUE(suggestions
.empty());
579 TEST_F(AddressValidatorTest
, SuggestOnlyForAdministrativeAreasAndPostalCode
) {
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
[] = {
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
) {
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
) {
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
) {
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");
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");
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");
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");
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.
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();
714 EXPECT_EQ(MISSING_REQUIRED_FIELD
, it
->second
);
718 TEST_F(AddressValidatorTest
,
719 DoNotValidateRequiredFieldsWithoutRulesWhenErorrIsFiltered
) {
720 // Do not load the rules for JP.
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
735 class FailingAddressValidatorTest
: public testing::Test
, LoadRulesListener
{
737 // A validator that retries loading rules without delay.
738 class TestAddressValidator
: public AddressValidator
{
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(),
747 load_rules_listener
) {}
749 virtual ~TestAddressValidator() {}
752 virtual base::TimeDelta
GetBaseRetryPeriod() const override
{
753 return base::TimeDelta::FromSeconds(0);
757 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator
);
760 // A source that always fails |failures_number| times before downloading
762 class FailingSource
: public Source
{
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
{
778 // |callback| takes ownership of the |new std::string|.
779 if (failures_number_
-- > 0)
780 callback(false, url
, new std::string
);
782 actual_source_
.Get(url
, callback
);
785 // Returns the number of download attempts.
786 int attempts_number() const { return attempts_number_
; }
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
),
804 new TestAddressValidator(scoped_ptr
<Source
>(source_
),
805 scoped_ptr
<Storage
>(new NullStorage
),
807 load_rules_success_(false) {}
809 virtual ~FailingAddressValidatorTest() {}
811 FailingSource
* source_
; // Owned by |validator_|.
812 scoped_ptr
<AddressValidator
> validator_
;
813 bool load_rules_success_
;
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.
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