OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" | 5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 using ::i18n::addressinput::STREET_ADDRESS; | 45 using ::i18n::addressinput::STREET_ADDRESS; |
46 using ::i18n::addressinput::RECIPIENT; | 46 using ::i18n::addressinput::RECIPIENT; |
47 | 47 |
48 using ::i18n::addressinput::INVALID_FORMAT; | 48 using ::i18n::addressinput::INVALID_FORMAT; |
49 using ::i18n::addressinput::MISMATCHING_VALUE; | 49 using ::i18n::addressinput::MISMATCHING_VALUE; |
50 using ::i18n::addressinput::MISSING_REQUIRED_FIELD; | 50 using ::i18n::addressinput::MISSING_REQUIRED_FIELD; |
51 using ::i18n::addressinput::UNEXPECTED_FIELD; | 51 using ::i18n::addressinput::UNEXPECTED_FIELD; |
52 using ::i18n::addressinput::UNKNOWN_VALUE; | 52 using ::i18n::addressinput::UNKNOWN_VALUE; |
53 using ::i18n::addressinput::USES_P_O_BOX; | 53 using ::i18n::addressinput::USES_P_O_BOX; |
54 | 54 |
| 55 // This class should always succeed in getting the rules. |
55 class AddressValidatorTest : public testing::Test, LoadRulesListener { | 56 class AddressValidatorTest : public testing::Test, LoadRulesListener { |
56 protected: | 57 protected: |
57 AddressValidatorTest() | 58 AddressValidatorTest() |
58 : validator_(new AddressValidator( | 59 : validator_(new AddressValidator( |
59 std::unique_ptr<Source>(new TestdataSource(true)), | 60 std::unique_ptr<Source>(new TestdataSource(true)), |
60 std::unique_ptr<Storage>(new NullStorage), | 61 std::unique_ptr<Storage>(new NullStorage), |
61 this)) { | 62 this)) { |
62 validator_->LoadRules("US"); | 63 validator_->LoadRules("US"); |
63 } | 64 } |
64 | 65 |
| 66 void set_expected_status(AddressValidator::Status expected_status) { |
| 67 expected_status_ = expected_status; |
| 68 } |
| 69 |
65 virtual ~AddressValidatorTest() {} | 70 virtual ~AddressValidatorTest() {} |
66 | 71 |
67 const std::unique_ptr<AddressValidator> validator_; | 72 const std::unique_ptr<AddressValidator> validator_; |
68 | 73 |
69 private: | 74 private: |
70 // LoadRulesListener implementation. | 75 // LoadRulesListener implementation. |
71 void OnAddressValidationRulesLoaded(const std::string& country_code, | 76 void OnAddressValidationRulesLoaded(const std::string& country_code, |
72 bool success) override { | 77 bool success) override { |
73 AddressData address_data; | 78 AddressData address_data; |
74 address_data.region_code = country_code; | 79 address_data.region_code = country_code; |
75 FieldProblemMap dummy; | 80 FieldProblemMap dummy; |
76 AddressValidator::Status status = | 81 AddressValidator::Status status = |
77 validator_->ValidateAddress(address_data, NULL, &dummy); | 82 validator_->ValidateAddress(address_data, NULL, &dummy); |
78 ASSERT_EQ(success, status == AddressValidator::SUCCESS); | 83 ASSERT_EQ(expected_status_, status); |
79 } | 84 } |
80 | 85 |
| 86 AddressValidator::Status expected_status_ = AddressValidator::SUCCESS; |
| 87 |
81 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); | 88 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); |
82 }; | 89 }; |
83 | 90 |
84 // Use this test fixture if you're going to use a region with a large set of | 91 // Use this test fixture if you're going to use a region with a large set of |
85 // validation rules. All rules should be loaded in SetUpTestCase(). | 92 // validation rules. All rules should be loaded in SetUpTestCase(). |
86 class LargeAddressValidatorTest : public testing::Test { | 93 class LargeAddressValidatorTest : public testing::Test { |
87 protected: | 94 protected: |
88 LargeAddressValidatorTest() {} | 95 LargeAddressValidatorTest() {} |
89 virtual ~LargeAddressValidatorTest() {} | 96 virtual ~LargeAddressValidatorTest() {} |
90 | 97 |
(...skipping 24 matching lines...) Expand all Loading... |
115 const std::string country_code = "US"; | 122 const std::string country_code = "US"; |
116 const std::string first_state = "AL"; | 123 const std::string first_state = "AL"; |
117 | 124 |
118 validator_->LoadRules(country_code); | 125 validator_->LoadRules(country_code); |
119 std::vector<std::string> sub_keys = | 126 std::vector<std::string> sub_keys = |
120 validator_->GetRegionSubKeys(country_code); | 127 validator_->GetRegionSubKeys(country_code); |
121 ASSERT_FALSE(sub_keys.empty()); | 128 ASSERT_FALSE(sub_keys.empty()); |
122 ASSERT_EQ(sub_keys[0], first_state); | 129 ASSERT_EQ(sub_keys[0], first_state); |
123 } | 130 } |
124 | 131 |
125 TEST_F(AddressValidatorTest, SubKeysNotLoaded) { | 132 TEST_F(AddressValidatorTest, SubKeysNotExist) { |
126 const std::string country_code = "ZZ"; | 133 const std::string country_code = "OZ"; |
| 134 |
| 135 set_expected_status(AddressValidator::RULES_UNAVAILABLE); |
127 | 136 |
128 validator_->LoadRules(country_code); | 137 validator_->LoadRules(country_code); |
129 std::vector<std::string> sub_keys = | 138 std::vector<std::string> sub_keys = |
130 validator_->GetRegionSubKeys(country_code); | 139 validator_->GetRegionSubKeys(country_code); |
131 ASSERT_TRUE(sub_keys.empty()); | 140 ASSERT_TRUE(sub_keys.empty()); |
132 } | 141 } |
133 | 142 |
134 TEST_F(AddressValidatorTest, RegionHasRules) { | 143 TEST_F(AddressValidatorTest, RegionHasRules) { |
135 const std::vector<std::string>& region_codes = GetRegionCodes(); | 144 const std::vector<std::string>& region_codes = GetRegionCodes(); |
136 AddressData address; | 145 AddressData address; |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 EXPECT_FALSE(problems.empty()); | 749 EXPECT_FALSE(problems.empty()); |
741 | 750 |
742 for (FieldProblemMap::const_iterator it = problems.begin(); | 751 for (FieldProblemMap::const_iterator it = problems.begin(); |
743 it != problems.end(); | 752 it != problems.end(); |
744 ++it) { | 753 ++it) { |
745 EXPECT_EQ(MISSING_REQUIRED_FIELD, it->second); | 754 EXPECT_EQ(MISSING_REQUIRED_FIELD, it->second); |
746 } | 755 } |
747 } | 756 } |
748 | 757 |
749 TEST_F(AddressValidatorTest, | 758 TEST_F(AddressValidatorTest, |
750 DoNotValidateRequiredFieldsWithoutRulesWhenErorrIsFiltered) { | 759 DoNotValidateRequiredFieldsWithoutRulesWhenErrorIsFiltered) { |
751 // Do not load the rules for JP. | 760 // Do not load the rules for JP. |
752 AddressData address; | 761 AddressData address; |
753 address.region_code = "JP"; | 762 address.region_code = "JP"; |
754 | 763 |
755 FieldProblemMap filter; | 764 FieldProblemMap filter; |
756 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE)); | 765 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE)); |
757 | 766 |
758 FieldProblemMap problems; | 767 FieldProblemMap problems; |
759 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE, | 768 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE, |
760 validator_->ValidateAddress(address, &filter, &problems)); | 769 validator_->ValidateAddress(address, &filter, &problems)); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 source_->set_failures_number(99); | 919 source_->set_failures_number(99); |
911 validator_->LoadRules("CH"); | 920 validator_->LoadRules("CH"); |
912 validator_->LoadRules("GB"); | 921 validator_->LoadRules("GB"); |
913 base::RunLoop().RunUntilIdle(); | 922 base::RunLoop().RunUntilIdle(); |
914 | 923 |
915 EXPECT_FALSE(load_rules_success_); | 924 EXPECT_FALSE(load_rules_success_); |
916 EXPECT_EQ(16, source_->attempts_number()); | 925 EXPECT_EQ(16, source_->attempts_number()); |
917 } | 926 } |
918 | 927 |
919 } // namespace autofill | 928 } // namespace autofill |
OLD | NEW |