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