[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / chrome_address_validator_unittest.cc
blobf72cbd988ee7b81d058c6110c4956442e668a868
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 "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_problem.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_storage.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
20 #include "third_party/libaddressinput/src/cpp/test/fake_downloader.h"
22 namespace autofill {
24 using ::i18n::addressinput::AddressData;
25 using ::i18n::addressinput::AddressField;
26 using ::i18n::addressinput::AddressProblem;
27 using ::i18n::addressinput::BuildCallback;
28 using ::i18n::addressinput::Downloader;
29 using ::i18n::addressinput::FakeDownloader;
30 using ::i18n::addressinput::FieldProblemMap;
31 using ::i18n::addressinput::GetRegionCodes;
32 using ::i18n::addressinput::NullStorage;
33 using ::i18n::addressinput::Storage;
35 using ::i18n::addressinput::COUNTRY;
36 using ::i18n::addressinput::ADMIN_AREA;
37 using ::i18n::addressinput::LOCALITY;
38 using ::i18n::addressinput::DEPENDENT_LOCALITY;
39 using ::i18n::addressinput::SORTING_CODE;
40 using ::i18n::addressinput::POSTAL_CODE;
41 using ::i18n::addressinput::STREET_ADDRESS;
42 using ::i18n::addressinput::RECIPIENT;
44 using ::i18n::addressinput::INVALID_FORMAT;
45 using ::i18n::addressinput::MISMATCHING_VALUE;
46 using ::i18n::addressinput::MISSING_REQUIRED_FIELD;
47 using ::i18n::addressinput::UNEXPECTED_FIELD;
48 using ::i18n::addressinput::UNKNOWN_VALUE;
49 using ::i18n::addressinput::USES_P_O_BOX;
51 class AddressValidatorTest : public testing::Test, LoadRulesListener {
52 protected:
53 AddressValidatorTest()
54 : validator_(
55 new AddressValidator(FakeDownloader::kFakeAggregateDataUrl,
56 scoped_ptr<Downloader>(new FakeDownloader),
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(FakeDownloader::kFakeAggregateDataUrl,
91 scoped_ptr<Downloader>(new FakeDownloader),
92 scoped_ptr<Storage>(new NullStorage),
93 NULL);
94 validator_->LoadRules("CN");
95 validator_->LoadRules("KR");
96 validator_->LoadRules("TW");
99 static void TearDownTestcase() {
100 delete validator_;
101 validator_ = NULL;
104 // Owned shared instance of validator with large sets validation rules.
105 static AddressValidator* validator_;
107 private:
108 DISALLOW_COPY_AND_ASSIGN(LargeAddressValidatorTest);
111 AddressValidator* LargeAddressValidatorTest::validator_ = NULL;
113 TEST_F(AddressValidatorTest, RegionHasRules) {
114 const std::vector<std::string>& region_codes = GetRegionCodes();
115 AddressData address;
116 for (size_t i = 0; i < region_codes.size(); ++i) {
117 SCOPED_TRACE("For region: " + region_codes[i]);
118 validator_->LoadRules(region_codes[i]);
119 address.region_code = region_codes[i];
120 FieldProblemMap dummy;
121 EXPECT_EQ(AddressValidator::SUCCESS,
122 validator_->ValidateAddress(address, NULL, &dummy));
126 TEST_F(AddressValidatorTest, EmptyAddressNoFatalFailure) {
127 AddressData address;
128 address.region_code = "US";
130 FieldProblemMap dummy;
131 EXPECT_EQ(AddressValidator::SUCCESS,
132 validator_->ValidateAddress(address, NULL, &dummy));
135 TEST_F(AddressValidatorTest, UsStateNamesAreValidEntries) {
136 AddressData address;
137 address.region_code = "US";
138 address.administrative_area = "California";
140 FieldProblemMap filter;
141 filter.insert(std::make_pair(ADMIN_AREA, UNKNOWN_VALUE));
142 FieldProblemMap problems;
143 EXPECT_EQ(AddressValidator::SUCCESS,
144 validator_->ValidateAddress(address, &filter, &problems));
145 EXPECT_TRUE(problems.empty());
148 TEST_F(AddressValidatorTest, USZipCode) {
149 AddressData address;
150 address.recipient = "Mr. Smith";
151 address.address_line.push_back("340 Main St.");
152 address.locality = "Venice";
153 address.administrative_area = "CA";
154 address.region_code = "US";
156 // Valid Californian zip code.
157 address.postal_code = "90291";
158 FieldProblemMap problems;
159 EXPECT_EQ(AddressValidator::SUCCESS,
160 validator_->ValidateAddress(address, NULL, &problems));
161 EXPECT_TRUE(problems.empty());
163 problems.clear();
165 // An extended, valid Californian zip code.
166 address.postal_code = "90210-1234";
167 EXPECT_EQ(AddressValidator::SUCCESS,
168 validator_->ValidateAddress(address, NULL, &problems));
169 EXPECT_TRUE(problems.empty());
171 problems.clear();
173 // New York zip code (which is invalid for California).
174 address.postal_code = "12345";
175 EXPECT_EQ(AddressValidator::SUCCESS,
176 validator_->ValidateAddress(address, NULL, &problems));
177 EXPECT_EQ(1U, problems.size());
178 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
179 EXPECT_EQ(problems.begin()->second, MISMATCHING_VALUE);
181 problems.clear();
183 // A zip code with a "90" in the middle.
184 address.postal_code = "12903";
185 EXPECT_EQ(AddressValidator::SUCCESS,
186 validator_->ValidateAddress(address, NULL, &problems));
187 EXPECT_EQ(1U, problems.size());
188 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
189 EXPECT_EQ(problems.begin()->second, MISMATCHING_VALUE);
191 problems.clear();
193 // Invalid zip code (too many digits).
194 address.postal_code = "902911";
195 EXPECT_EQ(AddressValidator::SUCCESS,
196 validator_->ValidateAddress(address, NULL, &problems));
197 EXPECT_EQ(1U, problems.size());
198 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
199 EXPECT_EQ(problems.begin()->second, INVALID_FORMAT);
201 problems.clear();
203 // Invalid zip code (too few digits).
204 address.postal_code = "9029";
205 EXPECT_EQ(AddressValidator::SUCCESS,
206 validator_->ValidateAddress(address, NULL, &problems));
207 EXPECT_EQ(1U, problems.size());
208 EXPECT_EQ(problems.begin()->first, POSTAL_CODE);
209 EXPECT_EQ(problems.begin()->second, INVALID_FORMAT);
212 TEST_F(AddressValidatorTest, BasicValidation) {
213 // US rules should always be available, even though this load call fails.
214 validator_->LoadRules("US");
215 AddressData address;
216 address.region_code = "US";
217 address.language_code = "en";
218 address.administrative_area = "TX";
219 address.locality = "Paris";
220 address.postal_code = "75461";
221 address.address_line.push_back("123 Main St");
222 address.recipient = "Mr. Smith";
223 FieldProblemMap problems;
224 EXPECT_EQ(AddressValidator::SUCCESS,
225 validator_->ValidateAddress(address, NULL, &problems));
226 EXPECT_TRUE(problems.empty());
228 // The display name works as well as the key.
229 address.administrative_area = "Texas";
230 problems.clear();
231 EXPECT_EQ(AddressValidator::SUCCESS,
232 validator_->ValidateAddress(address, NULL, &problems));
233 EXPECT_TRUE(problems.empty());
235 // Ignore capitalization.
236 address.administrative_area = "tx";
237 problems.clear();
238 EXPECT_EQ(AddressValidator::SUCCESS,
239 validator_->ValidateAddress(address, NULL, &problems));
240 EXPECT_TRUE(problems.empty());
242 // Ignore capitalization.
243 address.administrative_area = "teXas";
244 problems.clear();
245 EXPECT_EQ(AddressValidator::SUCCESS,
246 validator_->ValidateAddress(address, NULL, &problems));
247 EXPECT_TRUE(problems.empty());
249 // Ignore diacriticals.
250 address.administrative_area = "T\u00E9xas";
251 problems.clear();
252 EXPECT_EQ(AddressValidator::SUCCESS,
253 validator_->ValidateAddress(address, NULL, &problems));
254 EXPECT_TRUE(problems.empty());
257 TEST_F(AddressValidatorTest, BasicValidationFailure) {
258 // US rules should always be available, even though this load call fails.
259 validator_->LoadRules("US");
260 AddressData address;
261 address.region_code = "US";
262 address.language_code = "en";
263 address.administrative_area = "XT";
264 address.locality = "Paris";
265 address.postal_code = "75461";
266 address.address_line.push_back("123 Main St");
267 address.recipient = "Mr. Smith";
268 FieldProblemMap problems;
269 EXPECT_EQ(AddressValidator::SUCCESS,
270 validator_->ValidateAddress(address, NULL, &problems));
272 ASSERT_EQ(1U, problems.size());
273 EXPECT_EQ(UNKNOWN_VALUE, problems.begin()->second);
274 EXPECT_EQ(ADMIN_AREA, problems.begin()->first);
277 TEST_F(AddressValidatorTest, NoNullSuggestionsCrash) {
278 AddressData address;
279 address.region_code = "US";
280 EXPECT_EQ(AddressValidator::SUCCESS,
281 validator_->GetSuggestions(address, COUNTRY, 1, NULL));
284 TEST_F(AddressValidatorTest, SuggestAdminAreaForPostalCode) {
285 AddressData address;
286 address.region_code = "US";
287 address.postal_code = "90291";
289 std::vector<AddressData> suggestions;
290 EXPECT_EQ(AddressValidator::SUCCESS,
291 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
292 ASSERT_EQ(1U, suggestions.size());
293 EXPECT_EQ("CA", suggestions[0].administrative_area);
294 EXPECT_EQ("90291", suggestions[0].postal_code);
297 TEST_F(LargeAddressValidatorTest, SuggestLocalityForPostalCodeWithAdminArea) {
298 AddressData address;
299 address.region_code = "TW";
300 address.postal_code = "515";
301 address.administrative_area = "Changhua";
302 address.language_code = "zh-Latn";
304 std::vector<AddressData> suggestions;
305 EXPECT_EQ(AddressValidator::SUCCESS,
306 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
307 ASSERT_EQ(1U, suggestions.size());
308 EXPECT_EQ("Dacun Township", suggestions[0].locality);
309 EXPECT_EQ("Changhua County", suggestions[0].administrative_area);
310 EXPECT_EQ("515", suggestions[0].postal_code);
313 TEST_F(LargeAddressValidatorTest, SuggestAdminAreaForPostalCodeWithLocality) {
314 AddressData address;
315 address.region_code = "TW";
316 address.postal_code = "515";
317 address.locality = "Dacun";
318 address.language_code = "zh-Latn";
320 std::vector<AddressData> suggestions;
321 EXPECT_EQ(AddressValidator::SUCCESS,
322 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
323 ASSERT_EQ(1U, suggestions.size());
324 EXPECT_EQ("Dacun Township", suggestions[0].locality);
325 EXPECT_EQ("Changhua County", suggestions[0].administrative_area);
326 EXPECT_EQ("515", suggestions[0].postal_code);
329 TEST_F(AddressValidatorTest, NoSuggestForPostalCodeWithWrongAdminArea) {
330 AddressData address;
331 address.region_code = "US";
332 address.postal_code = "90066";
333 address.postal_code = "TX";
335 std::vector<AddressData> suggestions;
336 EXPECT_EQ(AddressValidator::SUCCESS,
337 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
338 EXPECT_TRUE(suggestions.empty());
341 TEST_F(LargeAddressValidatorTest, SuggestForLocality) {
342 AddressData address;
343 address.region_code = "CN";
344 address.locality = "Anqin";
345 address.language_code = "zh-Latn";
347 std::vector<AddressData> suggestions;
348 EXPECT_EQ(AddressValidator::SUCCESS,
349 validator_->GetSuggestions(address, LOCALITY, 10, &suggestions));
350 ASSERT_EQ(1U, suggestions.size());
351 EXPECT_EQ("Anqing Shi", suggestions[0].locality);
352 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
355 TEST_F(LargeAddressValidatorTest, SuggestForLocalityAndAdminArea) {
356 AddressData address;
357 address.region_code = "CN";
358 address.locality = "Anqing";
359 address.administrative_area = "Anhui";
360 address.language_code = "zh-Latn";
362 std::vector<AddressData> suggestions;
363 EXPECT_EQ(AddressValidator::SUCCESS,
364 validator_->GetSuggestions(address, LOCALITY, 10, &suggestions));
365 ASSERT_EQ(1U, suggestions.size());
366 EXPECT_TRUE(suggestions[0].dependent_locality.empty());
367 EXPECT_EQ("Anqing Shi", suggestions[0].locality);
368 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
371 TEST_F(LargeAddressValidatorTest, SuggestForAdminAreaAndLocality) {
372 AddressData address;
373 address.region_code = "CN";
374 address.locality = "Anqing";
375 address.administrative_area = "Anhui";
376 address.language_code = "zh-Latn";
378 std::vector<AddressData> suggestions;
379 EXPECT_EQ(AddressValidator::SUCCESS,
380 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
381 ASSERT_EQ(1U, suggestions.size());
382 EXPECT_TRUE(suggestions[0].dependent_locality.empty());
383 EXPECT_TRUE(suggestions[0].locality.empty());
384 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
387 TEST_F(LargeAddressValidatorTest, SuggestForDependentLocality) {
388 AddressData address;
389 address.region_code = "CN";
390 address.dependent_locality = "Zongyang";
391 address.language_code = "zh-Latn";
393 std::vector<AddressData> suggestions;
394 EXPECT_EQ(AddressValidator::SUCCESS,
395 validator_->GetSuggestions(
396 address, DEPENDENT_LOCALITY, 10, &suggestions));
397 ASSERT_EQ(1U, suggestions.size());
398 EXPECT_EQ("Zongyang Xian", suggestions[0].dependent_locality);
399 EXPECT_EQ("Anqing Shi", suggestions[0].locality);
400 EXPECT_EQ("Anhui Sheng", suggestions[0].administrative_area);
403 TEST_F(LargeAddressValidatorTest,
404 NoSuggestForDependentLocalityWithWrongAdminArea) {
405 AddressData address;
406 address.region_code = "CN";
407 address.dependent_locality = "Zongyang";
408 address.administrative_area = "Sichuan Sheng";
409 address.language_code = "zh-Latn";
411 std::vector<AddressData> suggestions;
412 EXPECT_EQ(AddressValidator::SUCCESS,
413 validator_->GetSuggestions(
414 address, DEPENDENT_LOCALITY, 10, &suggestions));
415 EXPECT_TRUE(suggestions.empty());
418 TEST_F(AddressValidatorTest, EmptySuggestionsOverLimit) {
419 AddressData address;
420 address.region_code = "US";
421 address.administrative_area = "A";
423 std::vector<AddressData> suggestions;
424 EXPECT_EQ(AddressValidator::SUCCESS,
425 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions));
426 EXPECT_TRUE(suggestions.empty());
429 TEST_F(AddressValidatorTest, PreferShortSuggestions) {
430 AddressData address;
431 address.region_code = "US";
432 address.administrative_area = "CA";
434 std::vector<AddressData> suggestions;
435 EXPECT_EQ(AddressValidator::SUCCESS,
436 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
437 ASSERT_EQ(1U, suggestions.size());
438 EXPECT_EQ("CA", suggestions[0].administrative_area);
441 TEST_F(AddressValidatorTest, SuggestTheSingleMatchForFullMatchName) {
442 AddressData address;
443 address.region_code = "US";
444 address.administrative_area = "Texas";
446 std::vector<AddressData> suggestions;
447 EXPECT_EQ(AddressValidator::SUCCESS,
448 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
449 ASSERT_EQ(1U, suggestions.size());
450 EXPECT_EQ("Texas", suggestions[0].administrative_area);
453 TEST_F(AddressValidatorTest, SuggestAdminArea) {
454 AddressData address;
455 address.region_code = "US";
456 address.administrative_area = "Cali";
458 std::vector<AddressData> suggestions;
459 EXPECT_EQ(AddressValidator::SUCCESS,
460 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
461 ASSERT_EQ(1U, suggestions.size());
462 EXPECT_EQ("California", suggestions[0].administrative_area);
465 TEST_F(AddressValidatorTest, MultipleSuggestions) {
466 AddressData address;
467 address.region_code = "US";
468 address.administrative_area = "MA";
470 std::vector<AddressData> suggestions;
471 EXPECT_EQ(AddressValidator::SUCCESS,
472 validator_->GetSuggestions(address, ADMIN_AREA, 10, &suggestions));
473 EXPECT_LT(1U, suggestions.size());
475 // Massachusetts should not be a suggestion, because it's already covered
476 // under MA.
477 std::set<std::string> expected_suggestions;
478 expected_suggestions.insert("MA");
479 expected_suggestions.insert("Maine");
480 expected_suggestions.insert("Marshall Islands");
481 expected_suggestions.insert("Maryland");
482 for (std::vector<AddressData>::const_iterator it = suggestions.begin();
483 it != suggestions.end();
484 ++it) {
485 expected_suggestions.erase(it->administrative_area);
487 EXPECT_TRUE(expected_suggestions.empty());
490 TEST_F(LargeAddressValidatorTest, SuggestNonLatinKeyWhenLanguageMatches) {
491 AddressData address;
492 address.language_code = "ko";
493 address.region_code = "KR";
494 address.postal_code = "210-210";
496 std::vector<AddressData> suggestions;
497 EXPECT_EQ(AddressValidator::SUCCESS,
498 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
499 ASSERT_EQ(1U, suggestions.size());
500 EXPECT_EQ("강원도", suggestions[0].administrative_area);
501 EXPECT_EQ("210-210", suggestions[0].postal_code);
504 TEST_F(LargeAddressValidatorTest, SuggestNonLatinKeyWhenUserInputIsNotLatin) {
505 AddressData address;
506 address.language_code = "en";
507 address.region_code = "KR";
508 address.administrative_area = "강원";
510 std::vector<AddressData> suggestions;
511 EXPECT_EQ(AddressValidator::SUCCESS,
512 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions));
513 ASSERT_EQ(1U, suggestions.size());
514 EXPECT_EQ("강원도", suggestions[0].administrative_area);
517 TEST_F(LargeAddressValidatorTest,
518 SuggestLatinNameWhenLanguageDiffersAndLatinNameAvailable) {
519 AddressData address;
520 address.language_code = "ko-Latn";
521 address.region_code = "KR";
522 address.postal_code = "210-210";
524 std::vector<AddressData> suggestions;
525 EXPECT_EQ(AddressValidator::SUCCESS,
526 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
527 ASSERT_EQ(1U, suggestions.size());
528 EXPECT_EQ("Gangwon", suggestions[0].administrative_area);
529 EXPECT_EQ("210-210", suggestions[0].postal_code);
532 TEST_F(AddressValidatorTest, NoSuggestionsForEmptyAddress) {
533 AddressData address;
534 address.region_code = "US";
536 std::vector<AddressData> suggestions;
537 EXPECT_EQ(
538 AddressValidator::SUCCESS,
539 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions));
540 EXPECT_TRUE(suggestions.empty());
543 TEST_F(AddressValidatorTest, SuggestionIncludesCountry) {
544 AddressData address;
545 address.region_code = "US";
546 address.postal_code = "90291";
548 std::vector<AddressData> suggestions;
549 EXPECT_EQ(AddressValidator::SUCCESS,
550 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
551 ASSERT_EQ(1U, suggestions.size());
552 EXPECT_EQ("US", suggestions[0].region_code);
555 TEST_F(AddressValidatorTest, InvalidPostalCodeNoSuggestions) {
556 AddressData address;
557 address.region_code = "US";
558 address.postal_code = "0";
560 std::vector<AddressData> suggestions;
561 EXPECT_EQ(
562 AddressValidator::SUCCESS,
563 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions));
564 EXPECT_TRUE(suggestions.empty());
567 TEST_F(AddressValidatorTest, MismatchedPostalCodeNoSuggestions) {
568 AddressData address;
569 address.region_code = "US";
570 address.administrative_area = "TX";
571 address.postal_code = "90291";
573 std::vector<AddressData> suggestions;
574 EXPECT_EQ(
575 AddressValidator::SUCCESS,
576 validator_->GetSuggestions(address, POSTAL_CODE, 999, &suggestions));
577 EXPECT_TRUE(suggestions.empty());
580 TEST_F(AddressValidatorTest, SuggestOnlyForAdministrativeAreasAndPostalCode) {
581 AddressData address;
582 address.region_code = "US";
583 address.administrative_area = "CA";
584 address.locality = "Los Angeles";
585 address.dependent_locality = "Venice";
586 address.postal_code = "90291";
587 address.sorting_code = "123";
588 address.address_line.push_back("123 Main St");
589 address.recipient = "Jon Smith";
591 // Fields that should not have suggestions in US.
592 static const AddressField kNoSugestFields[] = {
593 COUNTRY,
594 LOCALITY,
595 DEPENDENT_LOCALITY,
596 SORTING_CODE,
597 STREET_ADDRESS,
598 RECIPIENT
601 static const size_t kNumNoSuggestFields =
602 sizeof kNoSugestFields / sizeof (AddressField);
604 for (size_t i = 0; i < kNumNoSuggestFields; ++i) {
605 std::vector<AddressData> suggestions;
606 EXPECT_EQ(AddressValidator::SUCCESS,
607 validator_->GetSuggestions(
608 address, kNoSugestFields[i], 999, &suggestions));
609 EXPECT_TRUE(suggestions.empty());
613 TEST_F(AddressValidatorTest, SuggestionsAreCleared) {
614 AddressData address;
615 address.region_code = "US";
617 std::vector<AddressData> suggestions(1, address);
618 EXPECT_EQ(AddressValidator::SUCCESS,
619 validator_->GetSuggestions(address, POSTAL_CODE, 1, &suggestions));
620 EXPECT_TRUE(suggestions.empty());
623 TEST_F(AddressValidatorTest, CanonicalizeUsAdminAreaName) {
624 AddressData address;
625 address.region_code = "US";
626 address.administrative_area = "cALIFORNIa";
627 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
628 EXPECT_EQ("CA", address.administrative_area);
631 TEST_F(AddressValidatorTest, CanonicalizeUsAdminAreaKey) {
632 AddressData address;
633 address.region_code = "US";
634 address.administrative_area = "CA";
635 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
636 EXPECT_EQ("CA", address.administrative_area);
639 TEST_F(AddressValidatorTest, CanonicalizeJpAdminAreaKey) {
640 validator_->LoadRules("JP");
641 AddressData address;
642 address.region_code = "JP";
643 address.administrative_area = "東京都";
644 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
645 EXPECT_EQ("東京都", address.administrative_area);
648 TEST_F(AddressValidatorTest, CanonicalizeJpAdminAreaLatinName) {
649 validator_->LoadRules("JP");
650 AddressData address;
651 address.region_code = "JP";
652 address.administrative_area = "tOKYo";
653 EXPECT_TRUE(validator_->CanonicalizeAdministrativeArea(&address));
654 EXPECT_EQ("TOKYO", address.administrative_area);
657 TEST_F(AddressValidatorTest, TokushimaSuggestionIsValid) {
658 validator_->LoadRules("JP");
659 AddressData address;
660 address.region_code = "JP";
661 address.administrative_area = "Toku";
662 address.language_code = "ja-Latn";
664 std::vector<AddressData> suggestions;
665 EXPECT_EQ(AddressValidator::SUCCESS,
666 validator_->GetSuggestions(address, ADMIN_AREA, 1, &suggestions));
667 ASSERT_EQ(1U, suggestions.size());
668 EXPECT_EQ("TOKUSHIMA", suggestions[0].administrative_area);
670 FieldProblemMap filter;
671 for (int i = UNEXPECTED_FIELD; i <= USES_P_O_BOX; ++i)
672 filter.insert(std::make_pair(ADMIN_AREA, static_cast<AddressProblem>(i)));
674 FieldProblemMap problems;
675 EXPECT_EQ(AddressValidator::SUCCESS,
676 validator_->ValidateAddress(suggestions[0], &filter, &problems));
677 EXPECT_TRUE(problems.empty());
680 TEST_F(AddressValidatorTest, ValidPostalCodeInSuggestion) {
681 validator_->LoadRules("US");
682 AddressData address;
683 address.region_code = "US";
684 address.administrative_area = "New";
685 address.postal_code = "13699";
687 std::vector<AddressData> suggestions;
688 EXPECT_EQ(AddressValidator::SUCCESS,
689 validator_->GetSuggestions(address, ADMIN_AREA, 999, &suggestions));
690 ASSERT_EQ(1U, suggestions.size());
691 EXPECT_EQ("New York", suggestions[0].administrative_area);
693 address.administrative_area = "New";
694 address.postal_code = "03755";
696 EXPECT_EQ(AddressValidator::SUCCESS,
697 validator_->GetSuggestions(address, ADMIN_AREA, 999, &suggestions));
698 ASSERT_EQ(1U, suggestions.size());
699 EXPECT_EQ("New Hampshire", suggestions[0].administrative_area);
702 TEST_F(AddressValidatorTest, ValidateRequiredFieldsWithoutRules) {
703 // Do not load the rules for JP.
704 AddressData address;
705 address.region_code = "JP";
707 FieldProblemMap problems;
708 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE,
709 validator_->ValidateAddress(address, NULL, &problems));
710 EXPECT_FALSE(problems.empty());
712 for (FieldProblemMap::const_iterator it = problems.begin();
713 it != problems.end();
714 ++it) {
715 EXPECT_EQ(MISSING_REQUIRED_FIELD, it->second);
719 TEST_F(AddressValidatorTest,
720 DoNotValidateRequiredFieldsWithoutRulesWhenErorrIsFiltered) {
721 // Do not load the rules for JP.
722 AddressData address;
723 address.region_code = "JP";
725 FieldProblemMap filter;
726 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE));
728 FieldProblemMap problems;
729 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE,
730 validator_->ValidateAddress(address, &filter, &problems));
731 EXPECT_TRUE(problems.empty());
734 // Use this test fixture for configuring the number of failed attempts to load
735 // rules.
736 class FailingAddressValidatorTest : public testing::Test, LoadRulesListener {
737 protected:
738 // A validator that retries loading rules without delay.
739 class TestAddressValidator : public AddressValidator {
740 public:
741 // Takes ownership of |downloader| and |storage|.
742 TestAddressValidator(
743 const std::string& validation_data_url,
744 scoped_ptr< ::i18n::addressinput::Downloader> downloader,
745 scoped_ptr< ::i18n::addressinput::Storage> storage,
746 LoadRulesListener* load_rules_listener)
747 : AddressValidator(validation_data_url,
748 downloader.Pass(),
749 storage.Pass(),
750 load_rules_listener) {}
752 virtual ~TestAddressValidator() {}
754 protected:
755 virtual base::TimeDelta GetBaseRetryPeriod() const OVERRIDE {
756 return base::TimeDelta::FromSeconds(0);
759 private:
760 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator);
763 // A downloader that always fails |failures_number| times before downloading
764 // data.
765 class FailingDownloader : public Downloader {
766 public:
767 explicit FailingDownloader() : failures_number_(0), attempts_number_(0) {}
768 virtual ~FailingDownloader() {}
770 // Sets the number of times to fail before downloading data.
771 void set_failures_number(int failures_number) {
772 failures_number_ = failures_number;
775 // Downloader implementation.
776 // Always fails for the first |failures_number| times.
777 virtual void Download(const std::string& url,
778 const Callback& callback) const OVERRIDE {
779 ++attempts_number_;
780 // |callback| takes ownership of the |new std::string|.
781 if (failures_number_-- > 0)
782 callback(false, url, new std::string);
783 else
784 actual_downloader_.Download(url, callback);
787 // Returns the number of download attempts.
788 int attempts_number() const { return attempts_number_; }
790 private:
791 // The number of times to fail before downloading data.
792 mutable int failures_number_;
794 // The number of times Download was called.
795 mutable int attempts_number_;
797 // The downloader to use for successful downloads.
798 FakeDownloader actual_downloader_;
800 DISALLOW_COPY_AND_ASSIGN(FailingDownloader);
803 FailingAddressValidatorTest()
804 : downloader_(new FailingDownloader),
805 validator_(
806 new TestAddressValidator(FakeDownloader::kFakeAggregateDataUrl,
807 scoped_ptr<Downloader>(downloader_),
808 scoped_ptr<Storage>(new NullStorage),
809 this)),
810 load_rules_success_(false) {}
812 virtual ~FailingAddressValidatorTest() {}
814 FailingDownloader* downloader_; // Owned by |validator_|.
815 scoped_ptr<AddressValidator> validator_;
816 bool load_rules_success_;
818 private:
819 // LoadRulesListener implementation.
820 virtual void OnAddressValidationRulesLoaded(const std::string&,
821 bool success) OVERRIDE {
822 load_rules_success_ = success;
825 base::MessageLoop ui_;
827 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest);
830 // The validator will attempt to load rules at most 8 times.
831 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) {
832 downloader_->set_failures_number(99);
833 validator_->LoadRules("CH");
834 base::RunLoop().RunUntilIdle();
836 EXPECT_FALSE(load_rules_success_);
837 EXPECT_EQ(8, downloader_->attempts_number());
840 // The validator will load rules successfully if the downloader returns data
841 // before the maximum number of retries.
842 TEST_F(FailingAddressValidatorTest, RuleRetryingWillSucceed) {
843 downloader_->set_failures_number(4);
844 validator_->LoadRules("CH");
845 base::RunLoop().RunUntilIdle();
847 EXPECT_TRUE(load_rules_success_);
848 EXPECT_EQ(5, downloader_->attempts_number());
851 // The delayed task to retry loading rules should stop (instead of crashing) if
852 // the validator is destroyed before it fires.
853 TEST_F(FailingAddressValidatorTest, DestroyedValidatorStopsRetries) {
854 downloader_->set_failures_number(4);
855 validator_->LoadRules("CH");
857 // Destroy the validator.
858 validator_.reset();
860 // Fire the delayed task to retry loading rules.
861 EXPECT_NO_FATAL_FAILURE(base::RunLoop().RunUntilIdle());
864 // Each call to LoadRules should reset the number of retry attempts. If the
865 // first call to LoadRules exceeded the maximum number of retries, the second
866 // call to LoadRules should start counting the retries from zero.
867 TEST_F(FailingAddressValidatorTest, LoadingRulesSecondTimeSucceeds) {
868 downloader_->set_failures_number(11);
869 validator_->LoadRules("CH");
870 base::RunLoop().RunUntilIdle();
872 EXPECT_FALSE(load_rules_success_);
873 EXPECT_EQ(8, downloader_->attempts_number());
875 validator_->LoadRules("CH");
876 base::RunLoop().RunUntilIdle();
878 EXPECT_TRUE(load_rules_success_);
879 EXPECT_EQ(12, downloader_->attempts_number());
882 // Calling LoadRules("CH") and LoadRules("GB") simultaneously should attempt to
883 // load both rules up to the maximum number of attempts for each region.
884 TEST_F(FailingAddressValidatorTest, RegionsShouldRetryIndividually) {
885 downloader_->set_failures_number(99);
886 validator_->LoadRules("CH");
887 validator_->LoadRules("GB");
888 base::RunLoop().RunUntilIdle();
890 EXPECT_FALSE(load_rules_success_);
891 EXPECT_EQ(16, downloader_->attempts_number());
894 } // namespace autofill