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