autofill_profile_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 4 5#include "base/basictypes.h" 6#include "base/format_macros.h" 7#include "base/guid.h" 8#include "base/memory/scoped_ptr.h" 9#include "base/memory/scoped_vector.h" 10#include "base/stl_util.h" 11#include "base/strings/string16.h" 12#include "base/strings/stringprintf.h" 13#include "base/strings/utf_string_conversions.h" 14#include "components/autofill/core/browser/autofill_profile.h" 15#include "components/autofill/core/browser/autofill_test_utils.h" 16#include "components/autofill/core/browser/autofill_type.h" 17#include "components/autofill/core/browser/field_types.h" 18#include "components/autofill/core/common/form_field_data.h" 19#include "grit/components_strings.h" 20#include "testing/gtest/include/gtest/gtest.h" 21 22using base::ASCIIToUTF16; 23 24namespace autofill { 25 26namespace { 27 28base::string16 GetLabel(AutofillProfile* profile) { 29 std::vector<AutofillProfile*> profiles; 30 profiles.push_back(profile); 31 std::vector<base::string16> labels; 32 AutofillProfile::CreateDifferentiatingLabels(profiles, "en-US", &labels); 33 return labels[0]; 34} 35 36// Holds the autofill profile |first|, |middle| and |last| names. 37struct NameParts { 38 NameParts(const std::string& first, 39 const std::string& middle, 40 const std::string& last) 41 : first(first), middle(middle), last(last) {} 42 43 std::string first; 44 std::string middle; 45 std::string last; 46}; 47 48// Test case to be executed to validate OverwriteOrAppendNames. 49struct TestCase { 50 TestCase(const NameParts& starting_name, 51 const NameParts& additional_name, 52 const NameParts& expected_result) 53 : starting_names(std::vector<NameParts>(1, starting_name)), 54 additional_names(std::vector<NameParts>(1, additional_name)), 55 expected_result(std::vector<NameParts>(1, expected_result)) {} 56 57 TestCase(const std::vector<NameParts>& starting_names, 58 const std::vector<NameParts>& additional_names, 59 const std::vector<NameParts>& expected_result) 60 : starting_names(starting_names), 61 additional_names(additional_names), 62 expected_result(expected_result) {} 63 64 std::vector<NameParts> starting_names; 65 std::vector<NameParts> additional_names; 66 std::vector<NameParts> expected_result; 67}; 68 69// Populates |first_names|, |middle_names| and |last_names| from the list of 70// NameParts from |starting_names|, |additional_names| or |expected_result| 71// from the testcase to create and verify the autofill profile. 72void GetNamePartsList(const std::vector<NameParts>& names, 73 std::vector<base::string16>* first_names, 74 std::vector<base::string16>* middle_names, 75 std::vector<base::string16>* last_names) { 76 for (size_t i = 0; i < names.size(); ++i) { 77 first_names->push_back(ASCIIToUTF16(names[i].first)); 78 middle_names->push_back(ASCIIToUTF16(names[i].middle)); 79 last_names->push_back(ASCIIToUTF16(names[i].last)); 80 } 81} 82 83} // namespace 84 85// Tests different possibilities for summary string generation. 86// Based on existence of first name, last name, and address line 1. 87TEST(AutofillProfileTest, PreviewSummaryString) { 88 // Case 0/null: "" 89 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com/"); 90 // Empty profile - nothing to update. 91 base::string16 summary0 = GetLabel(&profile0); 92 EXPECT_EQ(base::string16(), summary0); 93 94 // Case 0a/empty name and address, so the first two fields of the rest of the 95 // data is used: "Hollywood, CA" 96 AutofillProfile profile00(base::GenerateGUID(), "https://www.example.com/"); 97 test::SetProfileInfo(&profile00, "", "", "", 98 "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", "91601", "US", 99 "16505678910"); 100 base::string16 summary00 = GetLabel(&profile00); 101 EXPECT_EQ(ASCIIToUTF16("Hollywood, CA"), summary00); 102 103 // Case 1: "<address>" without line 2. 104 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com/"); 105 test::SetProfileInfo(&profile1, "", "", "", 106 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "", "Hollywood", "CA", 107 "91601", "US", "16505678910"); 108 base::string16 summary1 = GetLabel(&profile1); 109 EXPECT_EQ(ASCIIToUTF16("123 Zoo St., Hollywood"), summary1); 110 111 // Case 1a: "<address>" with line 2. 112 AutofillProfile profile1a(base::GenerateGUID(), "https://www.example.com/"); 113 test::SetProfileInfo(&profile1a, "", "", "", 114 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA", 115 "91601", "US", "16505678910"); 116 base::string16 summary1a = GetLabel(&profile1a); 117 EXPECT_EQ(ASCIIToUTF16("123 Zoo St., unit 5"), summary1a); 118 119 // Case 2: "<lastname>" 120 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com/"); 121 test::SetProfileInfo(&profile2, "", "Mitchell", 122 "Morrison", "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", 123 "91601", "US", "16505678910"); 124 base::string16 summary2 = GetLabel(&profile2); 125 // Summary includes full name, to the maximal extent available. 126 EXPECT_EQ(ASCIIToUTF16("Mitchell Morrison, Hollywood"), summary2); 127 128 // Case 3: "<lastname>, <address>" 129 AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com/"); 130 test::SetProfileInfo(&profile3, "", "Mitchell", 131 "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "", 132 "Hollywood", "CA", "91601", "US", "16505678910"); 133 base::string16 summary3 = GetLabel(&profile3); 134 EXPECT_EQ(ASCIIToUTF16("Mitchell Morrison, 123 Zoo St."), summary3); 135 136 // Case 4: "<firstname>" 137 AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com/"); 138 test::SetProfileInfo(&profile4, "Marion", "Mitchell", "", 139 "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", "91601", "US", 140 "16505678910"); 141 base::string16 summary4 = GetLabel(&profile4); 142 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, Hollywood"), summary4); 143 144 // Case 5: "<firstname>, <address>" 145 AutofillProfile profile5(base::GenerateGUID(), "https://www.example.com/"); 146 test::SetProfileInfo(&profile5, "Marion", "Mitchell", "", 147 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA", 148 "91601", "US", "16505678910"); 149 base::string16 summary5 = GetLabel(&profile5); 150 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, 123 Zoo St."), summary5); 151 152 // Case 6: "<firstname> <lastname>" 153 AutofillProfile profile6(base::GenerateGUID(), "https://www.example.com/"); 154 test::SetProfileInfo(&profile6, "Marion", "Mitchell", 155 "Morrison", "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", 156 "91601", "US", "16505678910"); 157 base::string16 summary6 = GetLabel(&profile6); 158 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, Hollywood"), 159 summary6); 160 161 // Case 7: "<firstname> <lastname>, <address>" 162 AutofillProfile profile7(base::GenerateGUID(), "https://www.example.com/"); 163 test::SetProfileInfo(&profile7, "Marion", "Mitchell", 164 "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", 165 "Hollywood", "CA", "91601", "US", "16505678910"); 166 base::string16 summary7 = GetLabel(&profile7); 167 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, 123 Zoo St."), summary7); 168 169 // Case 7a: "<firstname> <lastname>, <address>" - same as #7, except for 170 // e-mail. 171 AutofillProfile profile7a(base::GenerateGUID(), "https://www.example.com/"); 172 test::SetProfileInfo(&profile7a, "Marion", "Mitchell", 173 "Morrison", "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5", 174 "Hollywood", "CA", "91601", "US", "16505678910"); 175 std::vector<AutofillProfile*> profiles; 176 profiles.push_back(&profile7); 177 profiles.push_back(&profile7a); 178 std::vector<base::string16> labels; 179 AutofillProfile::CreateDifferentiatingLabels(profiles, "en-US", &labels); 180 ASSERT_EQ(profiles.size(), labels.size()); 181 summary7 = labels[0]; 182 base::string16 summary7a = labels[1]; 183 EXPECT_EQ(ASCIIToUTF16( 184 "Marion Mitchell Morrison, 123 Zoo St., johnwayne@me.xyz"), summary7); 185 EXPECT_EQ(ASCIIToUTF16( 186 "Marion Mitchell Morrison, 123 Zoo St., marion@me.xyz"), summary7a); 187} 188 189TEST(AutofillProfileTest, AdjustInferredLabels) { 190 ScopedVector<AutofillProfile> profiles; 191 profiles.push_back( 192 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 193 test::SetProfileInfo( 194 profiles[0], 195 "John", 196 "", 197 "Doe", 198 "johndoe@hades.com", 199 "Underworld", 200 "666 Erebus St.", 201 "", 202 "Elysium", "CA", 203 "91111", 204 "US", 205 "16502111111"); 206 profiles.push_back( 207 new AutofillProfile(base::GenerateGUID(), "http://www.example.com/")); 208 test::SetProfileInfo( 209 profiles[1], 210 "Jane", 211 "", 212 "Doe", 213 "janedoe@tertium.com", 214 "Pluto Inc.", 215 "123 Letha Shore.", 216 "", 217 "Dis", "CA", 218 "91222", 219 "US", 220 "12345678910"); 221 std::vector<base::string16> labels; 222 AutofillProfile::CreateDifferentiatingLabels( 223 profiles.get(), "en-US", &labels); 224 ASSERT_EQ(2U, labels.size()); 225 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels[0]); 226 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 227 228 profiles.push_back( 229 new AutofillProfile(base::GenerateGUID(), "Chrome settings")); 230 test::SetProfileInfo( 231 profiles[2], 232 "John", 233 "", 234 "Doe", 235 "johndoe@tertium.com", 236 "Underworld", 237 "666 Erebus St.", 238 "", 239 "Elysium", "CA", 240 "91111", 241 "US", 242 "16502111111"); 243 labels.clear(); 244 AutofillProfile::CreateDifferentiatingLabels( 245 profiles.get(), "en-US", &labels); 246 247 // Profile 0 and 2 inferred label now includes an e-mail. 248 ASSERT_EQ(3U, labels.size()); 249 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com"), 250 labels[0]); 251 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 252 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@tertium.com"), 253 labels[2]); 254 255 profiles.resize(2); 256 257 profiles.push_back( 258 new AutofillProfile(base::GenerateGUID(), std::string())); 259 test::SetProfileInfo( 260 profiles[2], 261 "John", 262 "", 263 "Doe", 264 "johndoe@hades.com", 265 "Underworld", 266 "666 Erebus St.", 267 "", 268 "Elysium", "CO", // State is different 269 "91111", 270 "US", 271 "16502111111"); 272 273 labels.clear(); 274 AutofillProfile::CreateDifferentiatingLabels( 275 profiles.get(), "en-US", &labels); 276 277 // Profile 0 and 2 inferred label now includes a state. 278 ASSERT_EQ(3U, labels.size()); 279 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels[0]); 280 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 281 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO"), labels[2]); 282 283 profiles.push_back( 284 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 285 test::SetProfileInfo( 286 profiles[3], 287 "John", 288 "", 289 "Doe", 290 "johndoe@hades.com", 291 "Underworld", 292 "666 Erebus St.", 293 "", 294 "Elysium", "CO", // State is different for some. 295 "91111", 296 "US", 297 "16504444444"); // Phone is different for some. 298 299 labels.clear(); 300 AutofillProfile::CreateDifferentiatingLabels( 301 profiles.get(), "en-US", &labels); 302 ASSERT_EQ(4U, labels.size()); 303 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels[0]); 304 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 305 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, 16502111111"), 306 labels[2]); 307 // This one differs from other ones by unique phone, so no need for extra 308 // information. 309 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, 16504444444"), 310 labels[3]); 311 312 profiles.push_back( 313 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 314 test::SetProfileInfo( 315 profiles[4], 316 "John", 317 "", 318 "Doe", 319 "johndoe@styx.com", // E-Mail is different for some. 320 "Underworld", 321 "666 Erebus St.", 322 "", 323 "Elysium", "CO", // State is different for some. 324 "91111", 325 "US", 326 "16504444444"); // Phone is different for some. 327 328 labels.clear(); 329 AutofillProfile::CreateDifferentiatingLabels( 330 profiles.get(), "en-US", &labels); 331 ASSERT_EQ(5U, labels.size()); 332 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels[0]); 333 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 334 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@hades.com," 335 " 16502111111"), labels[2]); 336 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@hades.com," 337 " 16504444444"), labels[3]); 338 // This one differs from other ones by unique e-mail, so no need for extra 339 // information. 340 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@styx.com"), 341 labels[4]); 342} 343 344TEST(AutofillProfileTest, CreateInferredLabels) { 345 ScopedVector<AutofillProfile> profiles; 346 profiles.push_back( 347 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 348 test::SetProfileInfo(profiles[0], 349 "John", 350 "", 351 "Doe", 352 "johndoe@hades.com", 353 "Underworld", 354 "666 Erebus St.", 355 "", 356 "Elysium", "CA", 357 "91111", 358 "US", 359 "16502111111"); 360 profiles.push_back( 361 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 362 test::SetProfileInfo(profiles[1], 363 "Jane", 364 "", 365 "Doe", 366 "janedoe@tertium.com", 367 "Pluto Inc.", 368 "123 Letha Shore.", 369 "", 370 "Dis", "CA", 371 "91222", 372 "US", 373 "12345678910"); 374 std::vector<base::string16> labels; 375 // Two fields at least - no filter. 376 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 2, 377 "en-US", &labels); 378 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels[0]); 379 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 380 381 // Three fields at least - no filter. 382 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 3, 383 "en-US", &labels); 384 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., Elysium"), 385 labels[0]); 386 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore., Dis"), 387 labels[1]); 388 389 std::vector<ServerFieldType> suggested_fields; 390 suggested_fields.push_back(ADDRESS_HOME_CITY); 391 suggested_fields.push_back(ADDRESS_HOME_STATE); 392 suggested_fields.push_back(ADDRESS_HOME_ZIP); 393 394 // Two fields at least, from suggested fields - no filter. 395 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 396 UNKNOWN_TYPE, 2, "en-US", &labels); 397 EXPECT_EQ(ASCIIToUTF16("Elysium, CA"), labels[0]); 398 EXPECT_EQ(ASCIIToUTF16("Dis, CA"), labels[1]); 399 400 // Three fields at least, from suggested fields - no filter. 401 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 402 UNKNOWN_TYPE, 3, "en-US", &labels); 403 EXPECT_EQ(ASCIIToUTF16("Elysium, CA, 91111"), labels[0]); 404 EXPECT_EQ(ASCIIToUTF16("Dis, CA, 91222"), labels[1]); 405 406 // Three fields at least, from suggested fields - but filter reduces available 407 // fields to two. 408 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 409 ADDRESS_HOME_STATE, 3, "en-US", 410 &labels); 411 EXPECT_EQ(ASCIIToUTF16("Elysium, 91111"), labels[0]); 412 EXPECT_EQ(ASCIIToUTF16("Dis, 91222"), labels[1]); 413 414 suggested_fields.clear(); 415 // In our implementation we always display NAME_FULL for all NAME* fields... 416 suggested_fields.push_back(NAME_MIDDLE); 417 // One field at least, from suggested fields - no filter. 418 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 419 UNKNOWN_TYPE, 1, "en-US", &labels); 420 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]); 421 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]); 422 423 // One field at least, from suggested fields - filter the same as suggested 424 // field. 425 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 426 NAME_MIDDLE, 1, "en-US", &labels); 427 EXPECT_EQ(base::string16(), labels[0]); 428 EXPECT_EQ(base::string16(), labels[1]); 429 430 suggested_fields.clear(); 431 // In our implementation we always display NAME_FULL for NAME_MIDDLE_INITIAL 432 suggested_fields.push_back(NAME_MIDDLE_INITIAL); 433 // One field at least, from suggested fields - no filter. 434 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 435 UNKNOWN_TYPE, 1, "en-US", &labels); 436 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]); 437 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]); 438 439 // One field at least, from suggested fields - filter same as the first non- 440 // unknown suggested field. 441 suggested_fields.clear(); 442 suggested_fields.push_back(UNKNOWN_TYPE); 443 suggested_fields.push_back(NAME_FULL); 444 suggested_fields.push_back(ADDRESS_HOME_LINE1); 445 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 446 NAME_FULL, 1, "en-US", &labels); 447 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]); 448 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]); 449 450 // No suggested fields, but non-unknown excluded field. 451 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, 452 NAME_FULL, 1, "en-US", &labels); 453 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]); 454 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]); 455} 456 457// Test that we fall back to using the full name if there are no other 458// distinguishing fields, but only if it makes sense given the suggested fields. 459TEST(AutofillProfileTest, CreateInferredLabelsFallsBackToFullName) { 460 ScopedVector<AutofillProfile> profiles; 461 profiles.push_back( 462 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 463 test::SetProfileInfo(profiles[0], 464 "John", "", "Doe", "doe@example.com", "", 465 "88 Nowhere Ave.", "", "", "", "", "", ""); 466 profiles.push_back( 467 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 468 test::SetProfileInfo(profiles[1], 469 "Johnny", "K", "Doe", "doe@example.com", "", 470 "88 Nowhere Ave.", "", "", "", "", "", ""); 471 472 // If the only name field in the suggested fields is the excluded field, we 473 // should not fall back to the full name as a distinguishing field. 474 std::vector<ServerFieldType> suggested_fields; 475 suggested_fields.push_back(NAME_LAST); 476 suggested_fields.push_back(ADDRESS_HOME_LINE1); 477 suggested_fields.push_back(EMAIL_ADDRESS); 478 std::vector<base::string16> labels; 479 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 480 NAME_LAST, 1, "en-US", &labels); 481 ASSERT_EQ(2U, labels.size()); 482 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels[0]); 483 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels[1]); 484 485 // Otherwise, we should. 486 suggested_fields.push_back(NAME_FIRST); 487 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 488 NAME_LAST, 1, "en-US", &labels); 489 ASSERT_EQ(2U, labels.size()); 490 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., John Doe"), labels[0]); 491 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Johnny K Doe"), labels[1]); 492} 493 494// Test that we do not show duplicate fields in the labels. 495TEST(AutofillProfileTest, CreateInferredLabelsNoDuplicatedFields) { 496 ScopedVector<AutofillProfile> profiles; 497 profiles.push_back( 498 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 499 test::SetProfileInfo(profiles[0], 500 "John", "", "Doe", "doe@example.com", "", 501 "88 Nowhere Ave.", "", "", "", "", "", ""); 502 profiles.push_back( 503 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 504 test::SetProfileInfo(profiles[1], 505 "John", "", "Doe", "dojo@example.com", "", 506 "88 Nowhere Ave.", "", "", "", "", "", ""); 507 508 // If the only name field in the suggested fields is the excluded field, we 509 // should not fall back to the full name as a distinguishing field. 510 std::vector<ServerFieldType> suggested_fields; 511 suggested_fields.push_back(ADDRESS_HOME_LINE1); 512 suggested_fields.push_back(ADDRESS_BILLING_LINE1); 513 suggested_fields.push_back(EMAIL_ADDRESS); 514 std::vector<base::string16> labels; 515 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 516 UNKNOWN_TYPE, 2, "en-US", &labels); 517 ASSERT_EQ(2U, labels.size()); 518 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., doe@example.com"), labels[0]); 519 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., dojo@example.com"), labels[1]); 520} 521 522// Make sure that empty fields are not treated as distinguishing fields. 523TEST(AutofillProfileTest, CreateInferredLabelsSkipsEmptyFields) { 524 ScopedVector<AutofillProfile> profiles; 525 profiles.push_back( 526 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 527 test::SetProfileInfo(profiles[0], 528 "John", "", "Doe", "doe@example.com", 529 "Gogole", "", "", "", "", "", "", ""); 530 profiles.push_back( 531 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 532 test::SetProfileInfo(profiles[1], 533 "John", "", "Doe", "doe@example.com", 534 "Ggoole", "", "", "", "", "", "", ""); 535 profiles.push_back( 536 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 537 test::SetProfileInfo(profiles[2], 538 "John", "", "Doe", "john.doe@example.com", 539 "Goolge", "", "", "", "", "", "", ""); 540 541 std::vector<base::string16> labels; 542 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 3, 543 "en-US", &labels); 544 ASSERT_EQ(3U, labels.size()); 545 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels[0]); 546 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Ggoole"), labels[1]); 547 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com, Goolge"), labels[2]); 548 549 // A field must have a non-empty value for each profile to be considered a 550 // distinguishing field. 551 profiles[1]->SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("88 Nowhere Ave.")); 552 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 1, 553 "en-US", &labels); 554 ASSERT_EQ(3U, labels.size()); 555 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels[0]); 556 EXPECT_EQ(ASCIIToUTF16("John Doe, 88 Nowhere Ave., doe@example.com, Ggoole"), 557 labels[1]) << labels[1]; 558 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com"), labels[2]); 559} 560 561// Test that labels that would otherwise have multiline values are flattened. 562TEST(AutofillProfileTest, CreateInferredLabelsFlattensMultiLineValues) { 563 ScopedVector<AutofillProfile> profiles; 564 profiles.push_back( 565 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 566 test::SetProfileInfo(profiles[0], 567 "John", "", "Doe", "doe@example.com", "", 568 "88 Nowhere Ave.", "Apt. 42", "", "", "", "", ""); 569 570 // If the only name field in the suggested fields is the excluded field, we 571 // should not fall back to the full name as a distinguishing field. 572 std::vector<ServerFieldType> suggested_fields; 573 suggested_fields.push_back(NAME_FULL); 574 suggested_fields.push_back(ADDRESS_HOME_STREET_ADDRESS); 575 std::vector<base::string16> labels; 576 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 577 NAME_FULL, 1, "en-US", &labels); 578 ASSERT_EQ(1U, labels.size()); 579 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Apt. 42"), labels[0]); 580} 581 582TEST(AutofillProfileTest, IsSubsetOf) { 583 scoped_ptr<AutofillProfile> a, b; 584 585 // |a| is a subset of |b|. 586 a.reset( 587 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 588 b.reset( 589 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 590 test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson", 591 "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 592 NULL); 593 test::SetProfileInfo(b.get(), "Thomas", NULL, "Jefferson", 594 "declaration_guy@gmail.com", "United States Government", "Monticello", 595 NULL, "Charlottesville", "Virginia", "22902", NULL, NULL); 596 EXPECT_TRUE(a->IsSubsetOf(*b, "en-US")); 597 598 // |b| is not a subset of |a|. 599 EXPECT_FALSE(b->IsSubsetOf(*a, "en-US")); 600 601 // |a| is a subset of |a|. 602 EXPECT_TRUE(a->IsSubsetOf(*a, "en-US")); 603 604 // One field in |b| is different. 605 a.reset( 606 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 607 b.reset( 608 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 609 test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson", 610 "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 611 NULL); 612 test::SetProfileInfo(a.get(), "Thomas", NULL, "Adams", 613 "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 614 NULL); 615 EXPECT_FALSE(a->IsSubsetOf(*b, "en-US")); 616} 617 618TEST(AutofillProfileTest, OverwriteWithOrAddTo) { 619 AutofillProfile a(base::GenerateGUID(), "https://www.example.com"); 620 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", 621 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5", 622 "Hollywood", "CA", "91601", "US", 623 "12345678910"); 624 std::vector<base::string16> first_names; 625 a.GetRawMultiInfo(NAME_FIRST, &first_names); 626 first_names.push_back(ASCIIToUTF16("Marion")); 627 a.SetRawMultiInfo(NAME_FIRST, first_names); 628 629 std::vector<base::string16> last_names; 630 a.GetRawMultiInfo(NAME_LAST, &last_names); 631 last_names[last_names.size() - 1] = ASCIIToUTF16("Morrison"); 632 a.SetRawMultiInfo(NAME_LAST, last_names); 633 634 // Create an identical profile except that the new profile: 635 // (1) Has a different origin, 636 // (2) Has a different address line 2, 637 // (3) Lacks a company name, 638 // (4) Has a different full name variant, and 639 // (5) Has a language code. 640 AutofillProfile b = a; 641 b.set_guid(base::GenerateGUID()); 642 b.set_origin("Chrome settings"); 643 b.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("area 51")); 644 b.SetRawInfo(COMPANY_NAME, base::string16()); 645 646 std::vector<base::string16> names; 647 b.GetMultiInfo(AutofillType(NAME_FULL), "en-US", &names); 648 names.push_back(ASCIIToUTF16("Marion M. Morrison")); 649 b.SetRawMultiInfo(NAME_FULL, names); 650 b.set_language_code("en"); 651 652 a.OverwriteWithOrAddTo(b, "en-US"); 653 EXPECT_EQ("Chrome settings", a.origin()); 654 EXPECT_EQ(ASCIIToUTF16("area 51"), a.GetRawInfo(ADDRESS_HOME_LINE2)); 655 EXPECT_EQ(ASCIIToUTF16("Fox"), a.GetRawInfo(COMPANY_NAME)); 656 a.GetMultiInfo(AutofillType(NAME_FULL), "en-US", &names); 657 ASSERT_EQ(3U, names.size()); 658 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison"), names[0]); 659 EXPECT_EQ(ASCIIToUTF16("Marion Morrison"), names[1]); 660 EXPECT_EQ(ASCIIToUTF16("Marion M. Morrison"), names[2]); 661 EXPECT_EQ("en", a.language_code()); 662} 663 664TEST(AutofillProfileTest, AssignmentOperator) { 665 AutofillProfile a(base::GenerateGUID(), "https://www.example.com/"); 666 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", 667 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5", 668 "Hollywood", "CA", "91601", "US", 669 "12345678910"); 670 671 // Result of assignment should be logically equal to the original profile. 672 AutofillProfile b(base::GenerateGUID(), "http://www.example.com/"); 673 b = a; 674 EXPECT_TRUE(a == b); 675 676 // Assignment to self should not change the profile value. 677 a = a; 678 EXPECT_TRUE(a == b); 679} 680 681TEST(AutofillProfileTest, SetMultiInfo) { 682 std::vector<base::string16> full_names; 683 full_names.push_back(ASCIIToUTF16("John Davis")); 684 full_names.push_back(ASCIIToUTF16("Elouise Davis")); 685 AutofillProfile p; 686 p.SetMultiInfo(AutofillType(NAME_FULL), full_names, "en-US"); 687 688 std::vector<base::string16> first_names; 689 p.GetMultiInfo(AutofillType(NAME_FIRST), "en-US", &first_names); 690 ASSERT_EQ(2U, first_names.size()); 691 EXPECT_EQ(ASCIIToUTF16("John"), first_names[0]); 692 EXPECT_EQ(ASCIIToUTF16("Elouise"), first_names[1]); 693 694 std::vector<base::string16> last_names; 695 p.GetMultiInfo(AutofillType(NAME_LAST), "en-US", &last_names); 696 ASSERT_EQ(2U, last_names.size()); 697 EXPECT_EQ(ASCIIToUTF16("Davis"), last_names[0]); 698 EXPECT_EQ(ASCIIToUTF16("Davis"), last_names[1]); 699} 700 701TEST(AutofillProfileTest, Copy) { 702 AutofillProfile a(base::GenerateGUID(), "https://www.example.com/"); 703 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", 704 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5", 705 "Hollywood", "CA", "91601", "US", 706 "12345678910"); 707 708 // Clone should be logically equal to the original. 709 AutofillProfile b(a); 710 EXPECT_TRUE(a == b); 711} 712 713TEST(AutofillProfileTest, Compare) { 714 AutofillProfile a(base::GenerateGUID(), std::string()); 715 AutofillProfile b(base::GenerateGUID(), std::string()); 716 717 // Empty profiles are the same. 718 EXPECT_EQ(0, a.Compare(b)); 719 720 // GUIDs don't count. 721 a.set_guid(base::GenerateGUID()); 722 b.set_guid(base::GenerateGUID()); 723 EXPECT_EQ(0, a.Compare(b)); 724 725 // Origins don't count. 726 a.set_origin("apple"); 727 b.set_origin("banana"); 728 EXPECT_EQ(0, a.Compare(b)); 729 730 // Different values produce non-zero results. 731 test::SetProfileInfo(&a, "Jimmy", NULL, NULL, NULL, 732 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 733 test::SetProfileInfo(&b, "Ringo", NULL, NULL, NULL, 734 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 735 EXPECT_GT(0, a.Compare(b)); 736 EXPECT_LT(0, b.Compare(a)); 737 738 // Phone numbers are compared by the full number, including the area code. 739 // This is a regression test for http://crbug.com/163024 740 test::SetProfileInfo(&a, NULL, NULL, NULL, NULL, 741 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "650.555.4321"); 742 test::SetProfileInfo(&b, NULL, NULL, NULL, NULL, 743 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "408.555.4321"); 744 EXPECT_GT(0, a.Compare(b)); 745 EXPECT_LT(0, b.Compare(a)); 746 747 // Addresses are compared in full. Regression test for http://crbug.com/375545 748 test::SetProfileInfo(&a, "John", NULL, NULL, NULL, 749 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 750 a.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, 751 ASCIIToUTF16("line one\nline two")); 752 test::SetProfileInfo(&b, "John", NULL, NULL, NULL, 753 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 754 b.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, 755 ASCIIToUTF16("line one\nline two\nline three")); 756 EXPECT_GT(0, a.Compare(b)); 757 EXPECT_LT(0, b.Compare(a)); 758} 759 760TEST(AutofillProfileTest, MultiValueNames) { 761 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/"); 762 const base::string16 kJohnDoe(ASCIIToUTF16("John Doe")); 763 const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe")); 764 std::vector<base::string16> set_values; 765 set_values.push_back(kJohnDoe); 766 set_values.push_back(kJohnPDoe); 767 p.SetRawMultiInfo(NAME_FULL, set_values); 768 769 // Expect regular |GetInfo| returns the first element. 770 EXPECT_EQ(kJohnDoe, p.GetRawInfo(NAME_FULL)); 771 772 // Ensure that we get out what we put in. 773 std::vector<base::string16> get_values; 774 p.GetRawMultiInfo(NAME_FULL, &get_values); 775 ASSERT_EQ(2UL, get_values.size()); 776 EXPECT_EQ(kJohnDoe, get_values[0]); 777 EXPECT_EQ(kJohnPDoe, get_values[1]); 778 779 // Update the values. 780 AutofillProfile p2 = p; 781 EXPECT_EQ(0, p.Compare(p2)); 782 const base::string16 kNoOne(ASCIIToUTF16("No One")); 783 set_values[1] = kNoOne; 784 p.SetRawMultiInfo(NAME_FULL, set_values); 785 p.GetRawMultiInfo(NAME_FULL, &get_values); 786 ASSERT_EQ(2UL, get_values.size()); 787 EXPECT_EQ(kJohnDoe, get_values[0]); 788 EXPECT_EQ(kNoOne, get_values[1]); 789 EXPECT_NE(0, p.Compare(p2)); 790 791 // Delete values. 792 set_values.clear(); 793 p.SetRawMultiInfo(NAME_FULL, set_values); 794 p.GetRawMultiInfo(NAME_FULL, &get_values); 795 ASSERT_EQ(1UL, get_values.size()); 796 EXPECT_EQ(base::string16(), get_values[0]); 797 798 // Expect regular |GetInfo| returns empty value. 799 EXPECT_EQ(base::string16(), p.GetRawInfo(NAME_FULL)); 800} 801 802TEST(AutofillProfileTest, MultiValueEmails) { 803 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/"); 804 const base::string16 kJohnDoe(ASCIIToUTF16("john@doe.com")); 805 const base::string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com")); 806 std::vector<base::string16> set_values; 807 set_values.push_back(kJohnDoe); 808 set_values.push_back(kJohnPDoe); 809 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 810 811 // Expect regular |GetInfo| returns the first element. 812 EXPECT_EQ(kJohnDoe, p.GetRawInfo(EMAIL_ADDRESS)); 813 814 // Ensure that we get out what we put in. 815 std::vector<base::string16> get_values; 816 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values); 817 ASSERT_EQ(2UL, get_values.size()); 818 EXPECT_EQ(kJohnDoe, get_values[0]); 819 EXPECT_EQ(kJohnPDoe, get_values[1]); 820 821 // Update the values. 822 AutofillProfile p2 = p; 823 EXPECT_EQ(0, p.Compare(p2)); 824 const base::string16 kNoOne(ASCIIToUTF16("no@one.com")); 825 set_values[1] = kNoOne; 826 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 827 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values); 828 ASSERT_EQ(2UL, get_values.size()); 829 EXPECT_EQ(kJohnDoe, get_values[0]); 830 EXPECT_EQ(kNoOne, get_values[1]); 831 EXPECT_NE(0, p.Compare(p2)); 832 833 // Delete values. 834 set_values.clear(); 835 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 836 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values); 837 ASSERT_EQ(1UL, get_values.size()); 838 EXPECT_EQ(base::string16(), get_values[0]); 839 840 // Expect regular |GetInfo| returns empty value. 841 EXPECT_EQ(base::string16(), p.GetRawInfo(EMAIL_ADDRESS)); 842} 843 844TEST(AutofillProfileTest, MultiValuePhone) { 845 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/"); 846 const base::string16 kJohnDoe(ASCIIToUTF16("4151112222")); 847 const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333")); 848 std::vector<base::string16> set_values; 849 set_values.push_back(kJohnDoe); 850 set_values.push_back(kJohnPDoe); 851 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 852 853 // Expect regular |GetInfo| returns the first element. 854 EXPECT_EQ(kJohnDoe, p.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 855 856 // Ensure that we get out what we put in. 857 std::vector<base::string16> get_values; 858 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values); 859 ASSERT_EQ(2UL, get_values.size()); 860 EXPECT_EQ(kJohnDoe, get_values[0]); 861 EXPECT_EQ(kJohnPDoe, get_values[1]); 862 863 // Update the values. 864 AutofillProfile p2 = p; 865 EXPECT_EQ(0, p.Compare(p2)); 866 const base::string16 kNoOne(ASCIIToUTF16("4152110000")); 867 set_values[1] = kNoOne; 868 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 869 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values); 870 ASSERT_EQ(2UL, get_values.size()); 871 EXPECT_EQ(kJohnDoe, get_values[0]); 872 EXPECT_EQ(kNoOne, get_values[1]); 873 EXPECT_NE(0, p.Compare(p2)); 874 875 // Delete values. 876 set_values.clear(); 877 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 878 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values); 879 ASSERT_EQ(1UL, get_values.size()); 880 EXPECT_EQ(base::string16(), get_values[0]); 881 882 // Expect regular |GetInfo| returns empty value. 883 EXPECT_EQ(base::string16(), p.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 884} 885 886TEST(AutofillProfileTest, IsPresentButInvalid) { 887 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 888 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 889 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 890 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 891 892 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 893 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 894 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 895 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 896 897 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C")); 898 EXPECT_TRUE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 899 900 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 901 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 902 903 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90")); 904 EXPECT_TRUE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 905 906 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90210")); 907 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 908 909 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("310")); 910 EXPECT_TRUE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 911 912 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("(310) 310-6000")); 913 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 914} 915 916TEST(AutofillProfileTest, SetRawInfoPreservesLineBreaks) { 917 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 918 919 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, 920 ASCIIToUTF16("123 Super St.\n" 921 "Apt. #42")); 922 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n" 923 "Apt. #42"), 924 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 925} 926 927TEST(AutofillProfileTest, SetInfoPreservesLineBreaks) { 928 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 929 930 profile.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS), 931 ASCIIToUTF16("123 Super St.\n" 932 "Apt. #42"), 933 "en-US"); 934 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n" 935 "Apt. #42"), 936 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 937} 938 939TEST(AutofillProfileTest, SetRawInfoDoesntTrimWhitespace) { 940 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 941 942 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("\tuser@example.com ")); 943 EXPECT_EQ(ASCIIToUTF16("\tuser@example.com "), 944 profile.GetRawInfo(EMAIL_ADDRESS)); 945} 946 947TEST(AutofillProfileTest, SetInfoTrimsWhitespace) { 948 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 949 950 profile.SetInfo(AutofillType(EMAIL_ADDRESS), 951 ASCIIToUTF16("\tuser@example.com "), 952 "en-US"); 953 EXPECT_EQ(ASCIIToUTF16("user@example.com"), 954 profile.GetRawInfo(EMAIL_ADDRESS)); 955} 956 957TEST(AutofillProfileTest, FullAddress) { 958 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 959 test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison", 960 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5", 961 "Hollywood", "CA", "91601", "US", 962 "12345678910"); 963 964 AutofillType full_address(HTML_TYPE_FULL_ADDRESS, HTML_MODE_NONE); 965 base::string16 formatted_address(ASCIIToUTF16( 966 "Marion Mitchell Morrison\n" 967 "123 Zoo St.\n" 968 "unit 5\n" 969 "Hollywood, CA 91601")); 970 EXPECT_EQ(formatted_address, profile.GetInfo(full_address, "en-US")); 971 // This should fail and leave the profile unchanged. 972 EXPECT_FALSE(profile.SetInfo(full_address, ASCIIToUTF16("foobar"), "en-US")); 973 EXPECT_EQ(formatted_address, profile.GetInfo(full_address, "en-US")); 974 975 // Some things can be missing... 976 profile.SetInfo(AutofillType(ADDRESS_HOME_LINE2), 977 base::string16(), 978 "en-US"); 979 profile.SetInfo(AutofillType(EMAIL_ADDRESS), 980 base::string16(), 981 "en-US"); 982 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison\n" 983 "123 Zoo St.\n" 984 "Hollywood, CA 91601"), 985 profile.GetInfo(full_address, "en-US")); 986 987 // ...but nothing comes out if a required field is missing. 988 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), base::string16(), "en-US"); 989 EXPECT_TRUE(profile.GetInfo(full_address, "en-US").empty()); 990 991 // Restore the state but remove country. This should also fail. 992 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), 993 ASCIIToUTF16("CA"), 994 "en-US"); 995 EXPECT_FALSE(profile.GetInfo(full_address, "en-US").empty()); 996 profile.SetInfo(AutofillType(ADDRESS_HOME_COUNTRY), 997 base::string16(), 998 "en-US"); 999 EXPECT_TRUE(profile.GetInfo(full_address, "en-US").empty()); 1000} 1001 1002TEST(AutofillProfileTest, OverwriteOrAppendNames) { 1003 std::vector<TestCase> test_cases; 1004 1005 // Identical name. 1006 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1007 NameParts("Marion", "Mitchell", "Morrison"), 1008 NameParts("Marion", "Mitchell", "Morrison"))); 1009 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1010 NameParts("MARION", "MITCHELL", "MORRISON"), 1011 NameParts("Marion", "Mitchell", "Morrison"))); 1012 1013 // A parse that has a two-word last name should take precedence over a 1014 // parse that assumes the two names are a middle and a last name. 1015 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1016 NameParts("Marion", "", "Mitchell Morrison"), 1017 NameParts("Marion", "", "Mitchell Morrison"))); 1018 test_cases.push_back(TestCase(NameParts("Marion", "", "Mitchell Morrison"), 1019 NameParts("Marion", "Mitchell", "Morrison"), 1020 NameParts("Marion", "", "Mitchell Morrison"))); 1021 1022 // A parse that has a two-word first name should take precedence over a 1023 // parse that assumes the two names are a first and a middle name. 1024 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1025 NameParts("Marion Mitchell", "", "Morrison"), 1026 NameParts("Marion Mitchell", "", "Morrison"))); 1027 test_cases.push_back(TestCase(NameParts("Marion Mitchell", "", "Morrison"), 1028 NameParts("Marion", "Mitchell", "Morrison"), 1029 NameParts("Marion Mitchell", "", "Morrison"))); 1030 1031 // Two names that are identical in full, but not in parts: the parse that 1032 // does *not* match the heuristic parse should be preferred. 1033 test_cases.push_back( 1034 TestCase(NameParts("Arthur", "Ignatius Conan", "Doyle"), 1035 // Heurstic parse. 1036 NameParts("Arthur Ignatius", "Conan", "Doyle"), 1037 NameParts("Arthur", "Ignatius Conan", "Doyle"))); 1038 test_cases.push_back( 1039 // Heuristic parse. 1040 TestCase(NameParts("Arthur Ignatius", "Conan", "Doyle"), 1041 NameParts("Arthur", "Ignatius Conan", "Doyle"), 1042 NameParts("Arthur", "Ignatius Conan", "Doyle"))); 1043 1044 // A parse that has a many-word first name and/or last name should take 1045 // precedence over a heuristically parsed name. 1046 test_cases.push_back( 1047 // Heuristic parse. 1048 TestCase(NameParts("Roberto Carlos da", "Silva", "Rocha"), 1049 NameParts("Roberto Carlos da Silva", "", "Rocha"), 1050 NameParts("Roberto Carlos da Silva", "", "Rocha"))); 1051 1052 // Cases where merging 2 profiles with same full names but 1053 // different canonical forms appends instead of overwrites, 1054 // provided they dont form heuristically parsed names. 1055 { 1056 NameParts name1("Marion Mitchell", "", "Morrison"); 1057 NameParts name2("Marion", "", "Mitchell Morrison"); 1058 std::vector<NameParts> starting_names(1, name1); 1059 std::vector<NameParts> additional_names(1, name2); 1060 std::vector<NameParts> expected_result; 1061 expected_result.push_back(name1); 1062 expected_result.push_back(name2); 1063 test_cases.push_back( 1064 TestCase(starting_names, additional_names, expected_result)); 1065 } 1066 1067 // Cases where the names do not have the same full name strings, 1068 // i.e. the list of merged names is longer than either of the incoming 1069 // lists. 1070 { 1071 NameParts name1("Antonio", "Augusto Ribeiro", "Reis Jr."); 1072 NameParts name2("Juninho", "", "Pernambucano"); 1073 NameParts name3("Marion", "Mitchell", "Morrison"); 1074 NameParts name4("Marion", "M.", "Morrison"); 1075 std::vector<NameParts> starting_names; 1076 std::vector<NameParts> additional_names; 1077 std::vector<NameParts> expected_result; 1078 starting_names.push_back(name1); 1079 starting_names.push_back(name2); 1080 additional_names.push_back(name3); 1081 additional_names.push_back(name4); 1082 expected_result.push_back(name1); 1083 expected_result.push_back(name2); 1084 expected_result.push_back(name3); 1085 expected_result.push_back(name4); 1086 test_cases.push_back( 1087 TestCase(starting_names, additional_names, expected_result)); 1088 } 1089 1090 for (std::vector<TestCase>::iterator it = test_cases.begin(); 1091 it != test_cases.end(); 1092 ++it) { 1093 TestCase current_case = *it; 1094 SCOPED_TRACE(current_case.starting_names[0].first + " + " + 1095 current_case.additional_names[0].first + " = " + 1096 current_case.expected_result[0].first); 1097 1098 std::vector<base::string16> first_names, middle_names, last_names; 1099 GetNamePartsList( 1100 current_case.starting_names, &first_names, &middle_names, &last_names); 1101 1102 // Construct the starting_profile. 1103 AutofillProfile starting_profile(base::GenerateGUID(), 1104 "https://www.example.com/"); 1105 1106 starting_profile.SetRawMultiInfo(NAME_FIRST, first_names); 1107 starting_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names); 1108 starting_profile.SetRawMultiInfo(NAME_LAST, last_names); 1109 1110 first_names.clear(); 1111 middle_names.clear(); 1112 last_names.clear(); 1113 GetNamePartsList( 1114 current_case.additional_names, &first_names, &middle_names, 1115 &last_names); 1116 1117 // Construct the additional_profile. 1118 AutofillProfile additional_profile(base::GenerateGUID(), 1119 "https://www.example.com/"); 1120 additional_profile.SetRawMultiInfo(NAME_FIRST, first_names); 1121 additional_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names); 1122 additional_profile.SetRawMultiInfo(NAME_LAST, last_names); 1123 1124 // Merge the names from the |additional_profile| into the |starting_profile| 1125 starting_profile.OverwriteWithOrAddTo(additional_profile, "en-US"); 1126 1127 // Verify the test expectations. 1128 first_names.clear(); 1129 middle_names.clear(); 1130 last_names.clear(); 1131 GetNamePartsList( 1132 current_case.expected_result, &first_names, &middle_names, &last_names); 1133 1134 std::vector<base::string16> merged_first_names, merged_middle_names, 1135 merged_last_names; 1136 starting_profile.GetRawMultiInfo(NAME_FIRST, &merged_first_names); 1137 starting_profile.GetRawMultiInfo(NAME_MIDDLE, &merged_middle_names); 1138 starting_profile.GetRawMultiInfo(NAME_LAST, &merged_last_names); 1139 ASSERT_EQ(current_case.expected_result.size(), merged_first_names.size()); 1140 ASSERT_EQ(current_case.expected_result.size(), merged_middle_names.size()); 1141 ASSERT_EQ(current_case.expected_result.size(), merged_last_names.size()); 1142 1143 for (size_t i = 0; i < current_case.expected_result.size(); ++i) { 1144 EXPECT_EQ(first_names[i], merged_first_names[i]); 1145 EXPECT_EQ(middle_names[i], merged_middle_names[i]); 1146 EXPECT_EQ(last_names[i], merged_last_names[i]); 1147 } 1148 } 1149} 1150 1151} // namespace autofill 1152