1 // Copyright 2013 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 "base/json/json_reader.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "components/autofill/content/browser/wallet/wallet_address.h"
11 #include "components/autofill/core/browser/autofill_common_test.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 const char kAddressMissingObjectId[] =
19 " \"phone_number\":\"phone_number\","
20 " \"postal_address\":"
22 " \"recipient_name\":\"recipient_name\","
25 " \"address_line_1\","
28 " \"locality_name\":\"locality_name\","
29 " \"administrative_area_name\":\"administrative_area_name\","
30 " \"postal_code_number\":\"postal_code_number\","
31 " \"country_name_code\":\"US\""
35 const char kAddressMissingCountryNameCode[] =
38 " \"phone_number\":\"phone_number\","
39 " \"postal_address\":"
41 " \"recipient_name\":\"recipient_name\","
44 " \"address_line_1\","
47 " \"locality_name\":\"locality_name\","
48 " \"administrative_area_name\":\"administrative_area_name\","
49 " \"postal_code_number\":\"postal_code_number\""
53 const char kAddressMissingRecipientName[] =
56 " \"phone_number\":\"phone_number\","
57 " \"postal_address\":"
61 " \"address_line_1\","
64 " \"locality_name\":\"locality_name\","
65 " \"administrative_area_name\":\"administrative_area_name\","
66 " \"postal_code_number\":\"postal_code_number\","
67 " \"country_name_code\":\"US\""
71 const char kAddressMissingPostalCodeNumber[] =
74 " \"phone_number\":\"phone_number\","
75 " \"postal_address\":"
77 " \"recipient_name\":\"recipient_name\","
80 " \"address_line_1\","
83 " \"locality_name\":\"locality_name\","
84 " \"administrative_area_name\":\"administrative_area_name\","
85 " \"country_name_code\":\"US\""
89 const char kValidAddress[] =
92 " \"phone_number\":\"phone_number\","
93 " \"is_minimal_address\":true,"
94 " \"postal_address\":"
96 " \"recipient_name\":\"recipient_name\","
99 " \"address_line_1\","
100 " \"address_line_2\""
102 " \"locality_name\":\"locality_name\","
103 " \"administrative_area_name\":\"administrative_area_name\","
104 " \"country_name_code\":\"US\","
105 " \"postal_code_number\":\"postal_code_number\""
109 const char kClientAddressMissingCountryCode[] =
111 " \"name\":\"name\","
112 " \"address1\":\"address1\","
113 " \"address2\":\"address2\","
114 " \"city\":\"city\","
115 " \"state\":\"state\","
116 " \"postal_code\":\"postal_code\","
117 " \"phone_number\":\"phone_number\""
120 const char kClientAddressMissingPostalCode[] =
122 " \"name\":\"name\","
123 " \"address1\":\"address1\","
124 " \"address2\":\"address2\","
125 " \"city\":\"city\","
126 " \"state\":\"state\","
127 " \"phone_number\":\"phone_number\","
128 " \"country_code\":\"US\""
131 const char kClientAddressMissingName[] =
133 " \"address1\":\"address1\","
134 " \"address2\":\"address2\","
135 " \"city\":\"city\","
136 " \"state\":\"state\","
137 " \"postal_code\":\"postal_code\","
138 " \"phone_number\":\"phone_number\","
139 " \"country_code\":\"US\""
142 const char kClientValidAddress[] =
144 " \"name\":\"name\","
145 " \"address1\":\"address1\","
146 " \"address2\":\"address2\","
147 " \"city\":\"city\","
148 " \"state\":\"state\","
149 " \"postal_code\":\"postal_code\","
150 " \"phone_number\":\"phone_number\","
151 " \"country_code\":\"US\","
155 } // anonymous namespace
160 class WalletAddressTest : public testing::Test {
162 WalletAddressTest() {}
164 void SetUpDictionary(const std::string& json) {
165 scoped_ptr<Value> value(base::JSONReader::Read(json));
167 DCHECK(value->IsType(Value::TYPE_DICTIONARY));
168 dict_.reset(static_cast<DictionaryValue*>(value.release()));
170 scoped_ptr<const DictionaryValue> dict_;
173 TEST_F(WalletAddressTest, AddressEqualsIgnoreID) {
174 Address address1("US",
175 ASCIIToUTF16("recipient_name"),
176 ASCIIToUTF16("address_line_1"),
177 ASCIIToUTF16("address_line_2"),
178 ASCIIToUTF16("locality_name"),
179 ASCIIToUTF16("administrative_area_name"),
180 ASCIIToUTF16("postal_code_number"),
181 ASCIIToUTF16("phone_number"),
183 // Same as address1, only id is different.
184 Address address2("US",
185 ASCIIToUTF16("recipient_name"),
186 ASCIIToUTF16("address_line_1"),
187 ASCIIToUTF16("address_line_2"),
188 ASCIIToUTF16("locality_name"),
189 ASCIIToUTF16("administrative_area_name"),
190 ASCIIToUTF16("postal_code_number"),
191 ASCIIToUTF16("phone_number"),
193 // Has same id as address1, but name is different.
194 Address address3("US",
195 ASCIIToUTF16("a_different_name"),
196 ASCIIToUTF16("address_line_1"),
197 ASCIIToUTF16("address_line_2"),
198 ASCIIToUTF16("locality_name"),
199 ASCIIToUTF16("administrative_area_name"),
200 ASCIIToUTF16("postal_code_number"),
201 ASCIIToUTF16("phone_number"),
203 // Same as address1, but no id.
204 Address address4("US",
205 ASCIIToUTF16("recipient_name"),
206 ASCIIToUTF16("address_line_1"),
207 ASCIIToUTF16("address_line_2"),
208 ASCIIToUTF16("locality_name"),
209 ASCIIToUTF16("administrative_area_name"),
210 ASCIIToUTF16("postal_code_number"),
211 ASCIIToUTF16("phone_number"),
214 // Compare the address has id field to itself.
215 EXPECT_EQ(address1, address1);
216 EXPECT_TRUE(address1.EqualsIgnoreID(address1));
218 // Compare the address has no id field to itself
219 EXPECT_EQ(address4, address4);
220 EXPECT_TRUE(address4.EqualsIgnoreID(address4));
222 // Compare two addresses with different id.
223 EXPECT_NE(address1, address2);
224 EXPECT_TRUE(address1.EqualsIgnoreID(address2));
225 EXPECT_TRUE(address2.EqualsIgnoreID(address1));
227 // Compare two different addresses.
228 EXPECT_NE(address1, address3);
229 EXPECT_FALSE(address1.EqualsIgnoreID(address3));
230 EXPECT_FALSE(address3.EqualsIgnoreID(address1));
232 // Compare two same addresses, one has id, the other doesn't.
233 EXPECT_NE(address1, address4);
234 EXPECT_TRUE(address1.EqualsIgnoreID(address4));
235 EXPECT_TRUE(address4.EqualsIgnoreID(address1));
238 TEST_F(WalletAddressTest, CreateAddressMissingObjectId) {
239 SetUpDictionary(kAddressMissingObjectId);
240 Address address("US",
241 ASCIIToUTF16("recipient_name"),
242 ASCIIToUTF16("address_line_1"),
243 ASCIIToUTF16("address_line_2"),
244 ASCIIToUTF16("locality_name"),
245 ASCIIToUTF16("administrative_area_name"),
246 ASCIIToUTF16("postal_code_number"),
247 ASCIIToUTF16("phone_number"),
249 EXPECT_EQ(address, *Address::CreateAddress(*dict_));
252 TEST_F(WalletAddressTest, CreateAddressWithIDMissingObjectId) {
253 SetUpDictionary(kAddressMissingObjectId);
254 EXPECT_EQ(NULL, Address::CreateAddressWithID(*dict_).get());
257 TEST_F(WalletAddressTest, CreateAddressMissingCountryNameCode) {
258 SetUpDictionary(kAddressMissingCountryNameCode);
259 EXPECT_EQ(NULL, Address::CreateAddress(*dict_).get());
260 EXPECT_EQ(NULL, Address::CreateAddressWithID(*dict_).get());
263 TEST_F(WalletAddressTest, CreateAddressMissingRecipientName) {
264 SetUpDictionary(kAddressMissingRecipientName);
265 EXPECT_EQ(NULL, Address::CreateAddress(*dict_).get());
266 EXPECT_EQ(NULL, Address::CreateAddressWithID(*dict_).get());
269 TEST_F(WalletAddressTest, CreateAddressMissingPostalCodeNumber) {
270 SetUpDictionary(kAddressMissingPostalCodeNumber);
271 EXPECT_EQ(NULL, Address::CreateAddress(*dict_).get());
272 EXPECT_EQ(NULL, Address::CreateAddressWithID(*dict_).get());
275 TEST_F(WalletAddressTest, CreateAddressWithID) {
276 SetUpDictionary(kValidAddress);
277 Address address("US",
278 ASCIIToUTF16("recipient_name"),
279 ASCIIToUTF16("address_line_1"),
280 ASCIIToUTF16("address_line_2"),
281 ASCIIToUTF16("locality_name"),
282 ASCIIToUTF16("administrative_area_name"),
283 ASCIIToUTF16("postal_code_number"),
284 ASCIIToUTF16("phone_number"),
286 address.set_is_complete_address(false);
287 EXPECT_EQ(address, *Address::CreateAddress(*dict_));
288 EXPECT_EQ(address, *Address::CreateAddressWithID(*dict_));
291 TEST_F(WalletAddressTest, CreateDisplayAddressMissingCountryNameCode) {
292 SetUpDictionary(kClientAddressMissingCountryCode);
293 EXPECT_EQ(NULL, Address::CreateDisplayAddress(*dict_).get());
296 TEST_F(WalletAddressTest, CreateDisplayAddressMissingName) {
297 SetUpDictionary(kClientAddressMissingName);
298 EXPECT_EQ(NULL, Address::CreateDisplayAddress(*dict_).get());
301 TEST_F(WalletAddressTest, CreateDisplayAddressMissingPostalCode) {
302 SetUpDictionary(kClientAddressMissingPostalCode);
303 EXPECT_EQ(NULL, Address::CreateDisplayAddress(*dict_).get());
306 TEST_F(WalletAddressTest, CreateDisplayAddress) {
307 SetUpDictionary(kClientValidAddress);
308 Address address("US",
309 ASCIIToUTF16("name"),
310 ASCIIToUTF16("address1"),
311 ASCIIToUTF16("address2"),
312 ASCIIToUTF16("city"),
313 ASCIIToUTF16("state"),
314 ASCIIToUTF16("postal_code"),
315 ASCIIToUTF16("phone_number"),
317 EXPECT_EQ(address, *Address::CreateDisplayAddress(*dict_));
320 TEST_F(WalletAddressTest, ToDictionaryWithoutID) {
321 base::DictionaryValue expected;
322 expected.SetString("country_name_code",
324 expected.SetString("recipient_name",
326 expected.SetString("locality_name",
328 expected.SetString("administrative_area_name",
329 "administrative_area_name");
330 expected.SetString("postal_code_number",
331 "postal_code_number");
332 base::ListValue* address_lines = new base::ListValue();
333 address_lines->AppendString("address_line_1");
334 address_lines->AppendString("address_line_2");
335 expected.Set("address_line", address_lines);
337 Address address("US",
338 ASCIIToUTF16("recipient_name"),
339 ASCIIToUTF16("address_line_1"),
340 ASCIIToUTF16("address_line_2"),
341 ASCIIToUTF16("locality_name"),
342 ASCIIToUTF16("administrative_area_name"),
343 ASCIIToUTF16("postal_code_number"),
344 ASCIIToUTF16("phone_number"),
347 EXPECT_TRUE(expected.Equals(address.ToDictionaryWithoutID().get()));
350 TEST_F(WalletAddressTest, ToDictionaryWithID) {
351 base::DictionaryValue expected;
352 expected.SetString("id", "id");
353 expected.SetString("phone_number", "phone_number");
354 expected.SetString("postal_address.country_name_code",
356 expected.SetString("postal_address.recipient_name",
358 expected.SetString("postal_address.locality_name",
360 expected.SetString("postal_address.administrative_area_name",
361 "administrative_area_name");
362 expected.SetString("postal_address.postal_code_number",
363 "postal_code_number");
364 base::ListValue* address_lines = new base::ListValue();
365 address_lines->AppendString("address_line_1");
366 address_lines->AppendString("address_line_2");
367 expected.Set("postal_address.address_line", address_lines);
369 Address address("US",
370 ASCIIToUTF16("recipient_name"),
371 ASCIIToUTF16("address_line_1"),
372 ASCIIToUTF16("address_line_2"),
373 ASCIIToUTF16("locality_name"),
374 ASCIIToUTF16("administrative_area_name"),
375 ASCIIToUTF16("postal_code_number"),
376 ASCIIToUTF16("phone_number"),
379 EXPECT_TRUE(expected.Equals(address.ToDictionaryWithID().get()));
382 TEST_F(WalletAddressTest, FromAutofillProfile) {
384 AutofillProfile profile(test::GetFullProfile());
385 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("tx"));
386 Address address(profile);
387 EXPECT_EQ(ASCIIToUTF16("TX"), address.administrative_area_name());
391 AutofillProfile profile(test::GetFullProfile());
392 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("Texas"));
393 Address address(profile);
394 EXPECT_EQ(ASCIIToUTF16("TX"), address.administrative_area_name());
398 AutofillProfile profile(test::GetFullProfile());
399 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("TX"));
400 Address address(profile);
401 EXPECT_EQ(ASCIIToUTF16("TX"), address.administrative_area_name());
405 AutofillProfile profile(test::GetFullProfile());
406 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("txeas"));
407 Address address(profile);
408 EXPECT_TRUE(address.administrative_area_name().empty());
412 // Verifies that WalletAddress::GetInfo() can correctly return both country
413 // codes and localized country names.
414 TEST_F(WalletAddressTest, GetCountryInfo) {
415 Address address("FR",
416 ASCIIToUTF16("recipient_name"),
417 ASCIIToUTF16("address_line_1"),
418 ASCIIToUTF16("address_line_2"),
419 ASCIIToUTF16("locality_name"),
420 ASCIIToUTF16("administrative_area_name"),
421 ASCIIToUTF16("postal_code_number"),
422 ASCIIToUTF16("phone_number"),
425 AutofillType type = AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE);
426 EXPECT_EQ(ASCIIToUTF16("FR"), address.GetInfo(type, "en-US"));
428 type = AutofillType(HTML_TYPE_COUNTRY_NAME, HTML_MODE_NONE);
429 EXPECT_EQ(ASCIIToUTF16("France"), address.GetInfo(type, "en-US"));
431 type = AutofillType(ADDRESS_HOME_COUNTRY);
432 EXPECT_EQ(ASCIIToUTF16("France"), address.GetInfo(type, "en-US"));
435 // Verifies that WalletAddress::GetInfo() can correctly return a concatenated
436 // full street address.
437 TEST_F(WalletAddressTest, GetStreetAddress) {
438 // Address has both lines 1 and 2.
439 Address address1("FR",
440 ASCIIToUTF16("recipient_name"),
441 ASCIIToUTF16("address_line_1"),
442 ASCIIToUTF16("address_line_2"),
443 ASCIIToUTF16("locality_name"),
444 ASCIIToUTF16("administrative_area_name"),
445 ASCIIToUTF16("postal_code_number"),
446 ASCIIToUTF16("phone_number"),
448 AutofillType type = AutofillType(HTML_TYPE_STREET_ADDRESS, HTML_MODE_NONE);
449 EXPECT_EQ(ASCIIToUTF16("address_line_1, address_line_2"),
450 address1.GetInfo(type, "en-US"));
452 // Address has only line 1.
453 Address address2("FR",
454 ASCIIToUTF16("recipient_name"),
455 ASCIIToUTF16("address_line_1"),
457 ASCIIToUTF16("locality_name"),
458 ASCIIToUTF16("administrative_area_name"),
459 ASCIIToUTF16("postal_code_number"),
460 ASCIIToUTF16("phone_number"),
462 EXPECT_EQ(ASCIIToUTF16("address_line_1"), address2.GetInfo(type, "en-US"));
464 // Address has no address lines.
465 Address address3("FR",
466 ASCIIToUTF16("recipient_name"),
469 ASCIIToUTF16("locality_name"),
470 ASCIIToUTF16("administrative_area_name"),
471 ASCIIToUTF16("postal_code_number"),
472 ASCIIToUTF16("phone_number"),
474 EXPECT_EQ(base::string16(), address3.GetInfo(type, "en-US"));
477 } // namespace wallet
478 } // namespace autofill