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 "testing/gtest/include/gtest/gtest.h" 20 21 using base::ASCIIToUTF16; 22 using base::UTF8ToUTF16; 23 24 namespace autofill { 25 26 namespace { 27 28 base::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. 37 struct 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. 49 struct 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. 72 void 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. 87 TEST(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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) me.xyz"), summary7); 185 EXPECT_EQ(ASCIIToUTF16( 186 "Marion Mitchell Morrison, 123 Zoo St., marion (at) me.xyz"), summary7a); 187 } 188 189 TEST(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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) hades.com," 335 " 16502111111"), labels[2]); 336 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe (at) 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 (at) styx.com"), 341 labels[4]); 342 } 343 344 TEST(AutofillProfileTest, CreateInferredLabelsI18n_CH) { 345 ScopedVector<AutofillProfile> profiles; 346 profiles.push_back( 347 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 348 test::SetProfileInfo(profiles.back(), 349 "H.", 350 "R.", 351 "Giger", 352 "hrgiger (at) beispiel.com", 353 "Beispiel Inc", 354 "Brandschenkestrasse 110", 355 "", 356 "Zurich", "", 357 "8002", 358 "CH", 359 "+41 44-668-1800"); 360 profiles.back()->set_language_code("de_CH"); 361 static const char* kExpectedLabels[] = { 362 "", 363 "H. R. Giger", 364 "H. R. Giger, Brandschenkestrasse 110", 365 "H. R. Giger, Brandschenkestrasse 110, Zurich", 366 "H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich", 367 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich", 368 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich, " 369 "Switzerland", 370 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich, " 371 "Switzerland, hrgiger (at) beispiel.com", 372 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich, " 373 "Switzerland, hrgiger (at) beispiel.com, +41446681800", 374 }; 375 376 std::vector<base::string16> labels; 377 for (size_t i = 0; i < arraysize(kExpectedLabels); ++i) { 378 AutofillProfile::CreateInferredLabels( 379 profiles.get(), NULL, UNKNOWN_TYPE, i, "en-US", &labels); 380 ASSERT_FALSE(labels.empty()); 381 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels[i]), labels.back()); 382 } 383 } 384 385 386 TEST(AutofillProfileTest, CreateInferredLabelsI18n_FR) { 387 ScopedVector<AutofillProfile> profiles; 388 profiles.push_back( 389 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 390 test::SetProfileInfo(profiles.back(), 391 "Antoine", 392 "", 393 "de Saint-Exupry", 394 "antoine (at) exemple.com", 395 "Exemple Inc", 396 "8 Rue de Londres", 397 "", 398 "Paris", "", 399 "75009", 400 "FR", 401 "+33 (0) 1 42 68 53 00"); 402 profiles.back()->set_language_code("fr_FR"); 403 profiles.back()->SetInfo( 404 AutofillType(ADDRESS_HOME_SORTING_CODE), UTF8ToUTF16("CEDEX"), "en-US"); 405 static const char* kExpectedLabels[] = { 406 "", 407 "Antoine de Saint-Exupry", 408 "Antoine de Saint-Exupry, 8 Rue de Londres", 409 "Antoine de Saint-Exupry, 8 Rue de Londres, Paris", 410 "Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris", 411 "Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris CEDEX", 412 "Exemple Inc, Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris " 413 "CEDEX", 414 "Exemple Inc, Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris " 415 "CEDEX, France", 416 "Exemple Inc, Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris " 417 "CEDEX, France, antoine (at) exemple.com", 418 "Exemple Inc, Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris " 419 "CEDEX, France, antoine (at) exemple.com, +33142685300", 420 "Exemple Inc, Antoine de Saint-Exupry, 8 Rue de Londres, 75009 Paris " 421 "CEDEX, France, antoine (at) exemple.com, +33142685300", 422 }; 423 424 std::vector<base::string16> labels; 425 for (size_t i = 0; i < arraysize(kExpectedLabels); ++i) { 426 AutofillProfile::CreateInferredLabels( 427 profiles.get(), NULL, UNKNOWN_TYPE, i, "en-US", &labels); 428 ASSERT_FALSE(labels.empty()); 429 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels[i]), labels.back()); 430 } 431 } 432 433 TEST(AutofillProfileTest, CreateInferredLabelsI18n_KR) { 434 ScopedVector<AutofillProfile> profiles; 435 profiles.push_back( 436 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 437 test::SetProfileInfo(profiles.back(), 438 "Park", 439 "", 440 "Jae-sang", 441 "park (at) yeleul.com", 442 "Yeleul Inc", 443 "Gangnam Finance Center", 444 "152 Teheran-ro", 445 "Gangnam-Gu", "Seoul", 446 "135-984", 447 "KR", 448 "+82-2-531-9000"); 449 profiles.back()->set_language_code("ko_Latn"); 450 profiles.back()->SetInfo(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY), 451 UTF8ToUTF16("Yeoksam-Dong"), 452 "en-US"); 453 static const char* kExpectedLabels[] = { 454 "", 455 "Park Jae-sang", 456 "Park Jae-sang, Gangnam Finance Center", 457 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro", 458 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong", 459 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong, " 460 "Gangnam-Gu", 461 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong, " 462 "Gangnam-Gu, Seoul", 463 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong, " 464 "Gangnam-Gu, Seoul, 135-984", 465 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, " 466 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984", 467 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, " 468 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984, South Korea", 469 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, " 470 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984, South Korea, " 471 "park (at) yeleul.com", 472 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, " 473 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984, South Korea, " 474 "park (at) yeleul.com, +8225319000", 475 }; 476 477 std::vector<base::string16> labels; 478 for (size_t i = 0; i < arraysize(kExpectedLabels); ++i) { 479 AutofillProfile::CreateInferredLabels( 480 profiles.get(), NULL, UNKNOWN_TYPE, i, "en-US", &labels); 481 ASSERT_FALSE(labels.empty()); 482 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels[i]), labels.back()); 483 } 484 } 485 486 TEST(AutofillProfileTest, CreateInferredLabelsI18n_JP_Latn) { 487 ScopedVector<AutofillProfile> profiles; 488 profiles.push_back( 489 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 490 test::SetProfileInfo(profiles.back(), 491 "Miku", 492 "", 493 "Hatsune", 494 "miku (at) rei.com", 495 "Rei Inc", 496 "Roppongi Hills Mori Tower", 497 "6-10-1 Roppongi", 498 "Minato-ku", "Tokyo", 499 "106-6126", 500 "JP", 501 "+81-3-6384-9000"); 502 profiles.back()->set_language_code("ja_Latn"); 503 static const char* kExpectedLabels[] = { 504 "", 505 "Miku Hatsune", 506 "Miku Hatsune, Roppongi Hills Mori Tower", 507 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi", 508 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi, Minato-ku", 509 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi, Minato-ku, " 510 "Tokyo", 511 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi, Minato-ku, " 512 "Tokyo, 106-6126", 513 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, " 514 "Minato-ku, Tokyo, 106-6126", 515 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, " 516 "Minato-ku, Tokyo, 106-6126, Japan", 517 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, " 518 "Minato-ku, Tokyo, 106-6126, Japan, miku (at) rei.com", 519 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, " 520 "Minato-ku, Tokyo, 106-6126, Japan, miku (at) rei.com, +81363849000", 521 }; 522 523 std::vector<base::string16> labels; 524 for (size_t i = 0; i < arraysize(kExpectedLabels); ++i) { 525 AutofillProfile::CreateInferredLabels( 526 profiles.get(), NULL, UNKNOWN_TYPE, i, "en-US", &labels); 527 ASSERT_FALSE(labels.empty()); 528 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels[i]), labels.back()); 529 } 530 } 531 532 TEST(AutofillProfileTest, CreateInferredLabelsI18n_JP_ja) { 533 ScopedVector<AutofillProfile> profiles; 534 profiles.push_back( 535 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 536 test::SetProfileInfo(profiles.back(), 537 "", 538 "", 539 "", 540 "miku (at) rei.com", 541 "", 542 "", 543 " 6-10-1", 544 "", "", 545 "106-6126", 546 "JP", 547 "03-6384-9000"); 548 profiles.back()->set_language_code("ja_JP"); 549 static const char* kExpectedLabels[] = { 550 "", 551 " ", 552 " ", 553 " 6-10-1 ", 554 " 6-10-1 ", 555 " 6-10-1 ", 556 "106-6126 6-10-1 ", 557 "106-6126 6-10-1 ", 558 "106-6126 6-10-1 , Japan", 559 "106-6126 6-10-1 , Japan, " 560 "miku (at) rei.com", 561 "106-6126 6-10-1 , Japan, " 562 "miku (at) rei.com, 0363849000", 563 }; 564 565 std::vector<base::string16> labels; 566 for (size_t i = 0; i < arraysize(kExpectedLabels); ++i) { 567 AutofillProfile::CreateInferredLabels( 568 profiles.get(), NULL, UNKNOWN_TYPE, i, "en-US", &labels); 569 ASSERT_FALSE(labels.empty()); 570 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels[i]), labels.back()); 571 } 572 } 573 574 TEST(AutofillProfileTest, CreateInferredLabels) { 575 ScopedVector<AutofillProfile> profiles; 576 profiles.push_back( 577 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 578 test::SetProfileInfo(profiles[0], 579 "John", 580 "", 581 "Doe", 582 "johndoe (at) hades.com", 583 "Underworld", 584 "666 Erebus St.", 585 "", 586 "Elysium", "CA", 587 "91111", 588 "US", 589 "16502111111"); 590 profiles.push_back( 591 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 592 test::SetProfileInfo(profiles[1], 593 "Jane", 594 "", 595 "Doe", 596 "janedoe (at) tertium.com", 597 "Pluto Inc.", 598 "123 Letha Shore.", 599 "", 600 "Dis", "CA", 601 "91222", 602 "US", 603 "12345678910"); 604 std::vector<base::string16> labels; 605 // Two fields at least - no filter. 606 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 2, 607 "en-US", &labels); 608 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels[0]); 609 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]); 610 611 // Three fields at least - no filter. 612 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 3, 613 "en-US", &labels); 614 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., Elysium"), 615 labels[0]); 616 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore., Dis"), 617 labels[1]); 618 619 std::vector<ServerFieldType> suggested_fields; 620 suggested_fields.push_back(ADDRESS_HOME_CITY); 621 suggested_fields.push_back(ADDRESS_HOME_STATE); 622 suggested_fields.push_back(ADDRESS_HOME_ZIP); 623 624 // Two fields at least, from suggested fields - no filter. 625 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 626 UNKNOWN_TYPE, 2, "en-US", &labels); 627 EXPECT_EQ(ASCIIToUTF16("Elysium, CA"), labels[0]); 628 EXPECT_EQ(ASCIIToUTF16("Dis, CA"), labels[1]); 629 630 // Three fields at least, from suggested fields - no filter. 631 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 632 UNKNOWN_TYPE, 3, "en-US", &labels); 633 EXPECT_EQ(ASCIIToUTF16("Elysium, CA 91111"), labels[0]); 634 EXPECT_EQ(ASCIIToUTF16("Dis, CA 91222"), labels[1]); 635 636 // Three fields at least, from suggested fields - but filter reduces available 637 // fields to two. 638 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 639 ADDRESS_HOME_STATE, 3, "en-US", 640 &labels); 641 EXPECT_EQ(ASCIIToUTF16("Elysium 91111"), labels[0]); 642 EXPECT_EQ(ASCIIToUTF16("Dis 91222"), labels[1]); 643 644 suggested_fields.clear(); 645 // In our implementation we always display NAME_FULL for all NAME* fields... 646 suggested_fields.push_back(NAME_MIDDLE); 647 // One field at least, from suggested fields - no filter. 648 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 649 UNKNOWN_TYPE, 1, "en-US", &labels); 650 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]); 651 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]); 652 653 // One field at least, from suggested fields - filter the same as suggested 654 // field. 655 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 656 NAME_MIDDLE, 1, "en-US", &labels); 657 EXPECT_EQ(base::string16(), labels[0]); 658 EXPECT_EQ(base::string16(), labels[1]); 659 660 suggested_fields.clear(); 661 // In our implementation we always display NAME_FULL for NAME_MIDDLE_INITIAL 662 suggested_fields.push_back(NAME_MIDDLE_INITIAL); 663 // One field at least, from suggested fields - no filter. 664 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 665 UNKNOWN_TYPE, 1, "en-US", &labels); 666 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]); 667 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]); 668 669 // One field at least, from suggested fields - filter same as the first non- 670 // unknown suggested field. 671 suggested_fields.clear(); 672 suggested_fields.push_back(UNKNOWN_TYPE); 673 suggested_fields.push_back(NAME_FULL); 674 suggested_fields.push_back(ADDRESS_HOME_LINE1); 675 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 676 NAME_FULL, 1, "en-US", &labels); 677 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]); 678 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]); 679 680 // No suggested fields, but non-unknown excluded field. 681 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, 682 NAME_FULL, 1, "en-US", &labels); 683 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]); 684 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]); 685 } 686 687 // Test that we fall back to using the full name if there are no other 688 // distinguishing fields, but only if it makes sense given the suggested fields. 689 TEST(AutofillProfileTest, CreateInferredLabelsFallsBackToFullName) { 690 ScopedVector<AutofillProfile> profiles; 691 profiles.push_back( 692 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 693 test::SetProfileInfo(profiles[0], 694 "John", "", "Doe", "doe (at) example.com", "", 695 "88 Nowhere Ave.", "", "", "", "", "", ""); 696 profiles.push_back( 697 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 698 test::SetProfileInfo(profiles[1], 699 "Johnny", "K", "Doe", "doe (at) example.com", "", 700 "88 Nowhere Ave.", "", "", "", "", "", ""); 701 702 // If the only name field in the suggested fields is the excluded field, we 703 // should not fall back to the full name as a distinguishing field. 704 std::vector<ServerFieldType> suggested_fields; 705 suggested_fields.push_back(NAME_LAST); 706 suggested_fields.push_back(ADDRESS_HOME_LINE1); 707 suggested_fields.push_back(EMAIL_ADDRESS); 708 std::vector<base::string16> labels; 709 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 710 NAME_LAST, 1, "en-US", &labels); 711 ASSERT_EQ(2U, labels.size()); 712 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels[0]); 713 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels[1]); 714 715 // Otherwise, we should. 716 suggested_fields.push_back(NAME_FIRST); 717 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 718 NAME_LAST, 1, "en-US", &labels); 719 ASSERT_EQ(2U, labels.size()); 720 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., John Doe"), labels[0]); 721 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Johnny K Doe"), labels[1]); 722 } 723 724 // Test that we do not show duplicate fields in the labels. 725 TEST(AutofillProfileTest, CreateInferredLabelsNoDuplicatedFields) { 726 ScopedVector<AutofillProfile> profiles; 727 profiles.push_back( 728 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 729 test::SetProfileInfo(profiles[0], 730 "John", "", "Doe", "doe (at) example.com", "", 731 "88 Nowhere Ave.", "", "", "", "", "", ""); 732 profiles.push_back( 733 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 734 test::SetProfileInfo(profiles[1], 735 "John", "", "Doe", "dojo (at) example.com", "", 736 "88 Nowhere Ave.", "", "", "", "", "", ""); 737 738 // If the only name field in the suggested fields is the excluded field, we 739 // should not fall back to the full name as a distinguishing field. 740 std::vector<ServerFieldType> suggested_fields; 741 suggested_fields.push_back(ADDRESS_HOME_LINE1); 742 suggested_fields.push_back(ADDRESS_BILLING_LINE1); 743 suggested_fields.push_back(EMAIL_ADDRESS); 744 std::vector<base::string16> labels; 745 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 746 UNKNOWN_TYPE, 2, "en-US", &labels); 747 ASSERT_EQ(2U, labels.size()); 748 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., doe (at) example.com"), labels[0]); 749 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., dojo (at) example.com"), labels[1]); 750 } 751 752 // Make sure that empty fields are not treated as distinguishing fields. 753 TEST(AutofillProfileTest, CreateInferredLabelsSkipsEmptyFields) { 754 ScopedVector<AutofillProfile> profiles; 755 profiles.push_back( 756 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 757 test::SetProfileInfo(profiles[0], 758 "John", "", "Doe", "doe (at) example.com", 759 "Gogole", "", "", "", "", "", "", ""); 760 profiles.push_back( 761 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 762 test::SetProfileInfo(profiles[1], 763 "John", "", "Doe", "doe (at) example.com", 764 "Ggoole", "", "", "", "", "", "", ""); 765 profiles.push_back( 766 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 767 test::SetProfileInfo(profiles[2], 768 "John", "", "Doe", "john.doe (at) example.com", 769 "Goolge", "", "", "", "", "", "", ""); 770 771 std::vector<base::string16> labels; 772 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 3, 773 "en-US", &labels); 774 ASSERT_EQ(3U, labels.size()); 775 EXPECT_EQ(ASCIIToUTF16("John Doe, doe (at) example.com, Gogole"), labels[0]); 776 EXPECT_EQ(ASCIIToUTF16("John Doe, doe (at) example.com, Ggoole"), labels[1]); 777 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe (at) example.com, Goolge"), labels[2]); 778 779 // A field must have a non-empty value for each profile to be considered a 780 // distinguishing field. 781 profiles[1]->SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("88 Nowhere Ave.")); 782 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 1, 783 "en-US", &labels); 784 ASSERT_EQ(3U, labels.size()); 785 EXPECT_EQ(ASCIIToUTF16("John Doe, doe (at) example.com, Gogole"), labels[0]); 786 EXPECT_EQ(ASCIIToUTF16("John Doe, 88 Nowhere Ave., doe (at) example.com, Ggoole"), 787 labels[1]) << labels[1]; 788 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe (at) example.com"), labels[2]); 789 } 790 791 // Test that labels that would otherwise have multiline values are flattened. 792 TEST(AutofillProfileTest, CreateInferredLabelsFlattensMultiLineValues) { 793 ScopedVector<AutofillProfile> profiles; 794 profiles.push_back( 795 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 796 test::SetProfileInfo(profiles[0], 797 "John", "", "Doe", "doe (at) example.com", "", 798 "88 Nowhere Ave.", "Apt. 42", "", "", "", "", ""); 799 800 // If the only name field in the suggested fields is the excluded field, we 801 // should not fall back to the full name as a distinguishing field. 802 std::vector<ServerFieldType> suggested_fields; 803 suggested_fields.push_back(NAME_FULL); 804 suggested_fields.push_back(ADDRESS_HOME_STREET_ADDRESS); 805 std::vector<base::string16> labels; 806 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields, 807 NAME_FULL, 1, "en-US", &labels); 808 ASSERT_EQ(1U, labels.size()); 809 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Apt. 42"), labels[0]); 810 } 811 812 TEST(AutofillProfileTest, IsSubsetOf) { 813 scoped_ptr<AutofillProfile> a, b; 814 815 // |a| is a subset of |b|. 816 a.reset( 817 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 818 b.reset( 819 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 820 test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson", 821 "declaration_guy (at) gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 822 NULL); 823 test::SetProfileInfo(b.get(), "Thomas", NULL, "Jefferson", 824 "declaration_guy (at) gmail.com", "United States Government", "Monticello", 825 NULL, "Charlottesville", "Virginia", "22902", NULL, NULL); 826 EXPECT_TRUE(a->IsSubsetOf(*b, "en-US")); 827 828 // |b| is not a subset of |a|. 829 EXPECT_FALSE(b->IsSubsetOf(*a, "en-US")); 830 831 // |a| is a subset of |a|. 832 EXPECT_TRUE(a->IsSubsetOf(*a, "en-US")); 833 834 // One field in |b| is different. 835 a.reset( 836 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 837 b.reset( 838 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/")); 839 test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson", 840 "declaration_guy (at) gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 841 NULL); 842 test::SetProfileInfo(a.get(), "Thomas", NULL, "Adams", 843 "declaration_guy (at) gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 844 NULL); 845 EXPECT_FALSE(a->IsSubsetOf(*b, "en-US")); 846 } 847 848 TEST(AutofillProfileTest, OverwriteWithOrAddTo) { 849 AutofillProfile a(base::GenerateGUID(), "https://www.example.com"); 850 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", 851 "marion (at) me.xyz", "Fox", "123 Zoo St.", "unit 5", 852 "Hollywood", "CA", "91601", "US", 853 "12345678910"); 854 std::vector<base::string16> first_names; 855 a.GetRawMultiInfo(NAME_FIRST, &first_names); 856 first_names.push_back(ASCIIToUTF16("Marion")); 857 a.SetRawMultiInfo(NAME_FIRST, first_names); 858 859 std::vector<base::string16> last_names; 860 a.GetRawMultiInfo(NAME_LAST, &last_names); 861 last_names[last_names.size() - 1] = ASCIIToUTF16("Morrison"); 862 a.SetRawMultiInfo(NAME_LAST, last_names); 863 864 // Create an identical profile except that the new profile: 865 // (1) Has a different origin, 866 // (2) Has a different address line 2, 867 // (3) Lacks a company name, 868 // (4) Has a different full name variant, and 869 // (5) Has a language code. 870 AutofillProfile b = a; 871 b.set_guid(base::GenerateGUID()); 872 b.set_origin("Chrome settings"); 873 b.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("area 51")); 874 b.SetRawInfo(COMPANY_NAME, base::string16()); 875 876 std::vector<base::string16> names; 877 b.GetMultiInfo(AutofillType(NAME_FULL), "en-US", &names); 878 names.push_back(ASCIIToUTF16("Marion M. Morrison")); 879 b.SetRawMultiInfo(NAME_FULL, names); 880 b.set_language_code("en"); 881 882 a.OverwriteWithOrAddTo(b, "en-US"); 883 EXPECT_EQ("Chrome settings", a.origin()); 884 EXPECT_EQ(ASCIIToUTF16("area 51"), a.GetRawInfo(ADDRESS_HOME_LINE2)); 885 EXPECT_EQ(ASCIIToUTF16("Fox"), a.GetRawInfo(COMPANY_NAME)); 886 a.GetMultiInfo(AutofillType(NAME_FULL), "en-US", &names); 887 ASSERT_EQ(3U, names.size()); 888 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison"), names[0]); 889 EXPECT_EQ(ASCIIToUTF16("Marion Morrison"), names[1]); 890 EXPECT_EQ(ASCIIToUTF16("Marion M. Morrison"), names[2]); 891 EXPECT_EQ("en", a.language_code()); 892 } 893 894 TEST(AutofillProfileTest, AssignmentOperator) { 895 AutofillProfile a(base::GenerateGUID(), "https://www.example.com/"); 896 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", 897 "marion (at) me.xyz", "Fox", "123 Zoo St.", "unit 5", 898 "Hollywood", "CA", "91601", "US", 899 "12345678910"); 900 901 // Result of assignment should be logically equal to the original profile. 902 AutofillProfile b(base::GenerateGUID(), "http://www.example.com/"); 903 b = a; 904 EXPECT_TRUE(a == b); 905 906 // Assignment to self should not change the profile value. 907 a = a; 908 EXPECT_TRUE(a == b); 909 } 910 911 TEST(AutofillProfileTest, SetMultiInfo) { 912 std::vector<base::string16> full_names; 913 full_names.push_back(ASCIIToUTF16("John Davis")); 914 full_names.push_back(ASCIIToUTF16("Elouise Davis")); 915 AutofillProfile p; 916 p.SetMultiInfo(AutofillType(NAME_FULL), full_names, "en-US"); 917 918 std::vector<base::string16> first_names; 919 p.GetMultiInfo(AutofillType(NAME_FIRST), "en-US", &first_names); 920 ASSERT_EQ(2U, first_names.size()); 921 EXPECT_EQ(ASCIIToUTF16("John"), first_names[0]); 922 EXPECT_EQ(ASCIIToUTF16("Elouise"), first_names[1]); 923 924 std::vector<base::string16> last_names; 925 p.GetMultiInfo(AutofillType(NAME_LAST), "en-US", &last_names); 926 ASSERT_EQ(2U, last_names.size()); 927 EXPECT_EQ(ASCIIToUTF16("Davis"), last_names[0]); 928 EXPECT_EQ(ASCIIToUTF16("Davis"), last_names[1]); 929 } 930 931 TEST(AutofillProfileTest, Copy) { 932 AutofillProfile a(base::GenerateGUID(), "https://www.example.com/"); 933 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", 934 "marion (at) me.xyz", "Fox", "123 Zoo St.", "unit 5", 935 "Hollywood", "CA", "91601", "US", 936 "12345678910"); 937 938 // Clone should be logically equal to the original. 939 AutofillProfile b(a); 940 EXPECT_TRUE(a == b); 941 } 942 943 TEST(AutofillProfileTest, Compare) { 944 AutofillProfile a(base::GenerateGUID(), std::string()); 945 AutofillProfile b(base::GenerateGUID(), std::string()); 946 947 // Empty profiles are the same. 948 EXPECT_EQ(0, a.Compare(b)); 949 950 // GUIDs don't count. 951 a.set_guid(base::GenerateGUID()); 952 b.set_guid(base::GenerateGUID()); 953 EXPECT_EQ(0, a.Compare(b)); 954 955 // Origins don't count. 956 a.set_origin("apple"); 957 b.set_origin("banana"); 958 EXPECT_EQ(0, a.Compare(b)); 959 960 // Different values produce non-zero results. 961 test::SetProfileInfo(&a, "Jimmy", NULL, NULL, NULL, 962 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 963 test::SetProfileInfo(&b, "Ringo", NULL, NULL, NULL, 964 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 965 EXPECT_GT(0, a.Compare(b)); 966 EXPECT_LT(0, b.Compare(a)); 967 968 // Phone numbers are compared by the full number, including the area code. 969 // This is a regression test for http://crbug.com/163024 970 test::SetProfileInfo(&a, NULL, NULL, NULL, NULL, 971 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "650.555.4321"); 972 test::SetProfileInfo(&b, NULL, NULL, NULL, NULL, 973 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "408.555.4321"); 974 EXPECT_GT(0, a.Compare(b)); 975 EXPECT_LT(0, b.Compare(a)); 976 977 // Addresses are compared in full. Regression test for http://crbug.com/375545 978 test::SetProfileInfo(&a, "John", NULL, NULL, NULL, 979 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 980 a.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, 981 ASCIIToUTF16("line one\nline two")); 982 test::SetProfileInfo(&b, "John", NULL, NULL, NULL, 983 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 984 b.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, 985 ASCIIToUTF16("line one\nline two\nline three")); 986 EXPECT_GT(0, a.Compare(b)); 987 EXPECT_LT(0, b.Compare(a)); 988 } 989 990 TEST(AutofillProfileTest, MultiValueNames) { 991 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/"); 992 const base::string16 kJohnDoe(ASCIIToUTF16("John Doe")); 993 const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe")); 994 std::vector<base::string16> set_values; 995 set_values.push_back(kJohnDoe); 996 set_values.push_back(kJohnPDoe); 997 p.SetRawMultiInfo(NAME_FULL, set_values); 998 999 // Expect regular |GetInfo| returns the first element. 1000 EXPECT_EQ(kJohnDoe, p.GetRawInfo(NAME_FULL)); 1001 1002 // Ensure that we get out what we put in. 1003 std::vector<base::string16> get_values; 1004 p.GetRawMultiInfo(NAME_FULL, &get_values); 1005 ASSERT_EQ(2UL, get_values.size()); 1006 EXPECT_EQ(kJohnDoe, get_values[0]); 1007 EXPECT_EQ(kJohnPDoe, get_values[1]); 1008 1009 // Update the values. 1010 AutofillProfile p2 = p; 1011 EXPECT_EQ(0, p.Compare(p2)); 1012 const base::string16 kNoOne(ASCIIToUTF16("No One")); 1013 set_values[1] = kNoOne; 1014 p.SetRawMultiInfo(NAME_FULL, set_values); 1015 p.GetRawMultiInfo(NAME_FULL, &get_values); 1016 ASSERT_EQ(2UL, get_values.size()); 1017 EXPECT_EQ(kJohnDoe, get_values[0]); 1018 EXPECT_EQ(kNoOne, get_values[1]); 1019 EXPECT_NE(0, p.Compare(p2)); 1020 1021 // Delete values. 1022 set_values.clear(); 1023 p.SetRawMultiInfo(NAME_FULL, set_values); 1024 p.GetRawMultiInfo(NAME_FULL, &get_values); 1025 ASSERT_EQ(1UL, get_values.size()); 1026 EXPECT_EQ(base::string16(), get_values[0]); 1027 1028 // Expect regular |GetInfo| returns empty value. 1029 EXPECT_EQ(base::string16(), p.GetRawInfo(NAME_FULL)); 1030 } 1031 1032 TEST(AutofillProfileTest, MultiValueEmails) { 1033 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/"); 1034 const base::string16 kJohnDoe(ASCIIToUTF16("john (at) doe.com")); 1035 const base::string16 kJohnPDoe(ASCIIToUTF16("john_p (at) doe.com")); 1036 std::vector<base::string16> set_values; 1037 set_values.push_back(kJohnDoe); 1038 set_values.push_back(kJohnPDoe); 1039 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 1040 1041 // Expect regular |GetInfo| returns the first element. 1042 EXPECT_EQ(kJohnDoe, p.GetRawInfo(EMAIL_ADDRESS)); 1043 1044 // Ensure that we get out what we put in. 1045 std::vector<base::string16> get_values; 1046 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values); 1047 ASSERT_EQ(2UL, get_values.size()); 1048 EXPECT_EQ(kJohnDoe, get_values[0]); 1049 EXPECT_EQ(kJohnPDoe, get_values[1]); 1050 1051 // Update the values. 1052 AutofillProfile p2 = p; 1053 EXPECT_EQ(0, p.Compare(p2)); 1054 const base::string16 kNoOne(ASCIIToUTF16("no (at) one.com")); 1055 set_values[1] = kNoOne; 1056 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 1057 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values); 1058 ASSERT_EQ(2UL, get_values.size()); 1059 EXPECT_EQ(kJohnDoe, get_values[0]); 1060 EXPECT_EQ(kNoOne, get_values[1]); 1061 EXPECT_NE(0, p.Compare(p2)); 1062 1063 // Delete values. 1064 set_values.clear(); 1065 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 1066 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values); 1067 ASSERT_EQ(1UL, get_values.size()); 1068 EXPECT_EQ(base::string16(), get_values[0]); 1069 1070 // Expect regular |GetInfo| returns empty value. 1071 EXPECT_EQ(base::string16(), p.GetRawInfo(EMAIL_ADDRESS)); 1072 } 1073 1074 TEST(AutofillProfileTest, MultiValuePhone) { 1075 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/"); 1076 const base::string16 kJohnDoe(ASCIIToUTF16("4151112222")); 1077 const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333")); 1078 std::vector<base::string16> set_values; 1079 set_values.push_back(kJohnDoe); 1080 set_values.push_back(kJohnPDoe); 1081 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 1082 1083 // Expect regular |GetInfo| returns the first element. 1084 EXPECT_EQ(kJohnDoe, p.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 1085 1086 // Ensure that we get out what we put in. 1087 std::vector<base::string16> get_values; 1088 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values); 1089 ASSERT_EQ(2UL, get_values.size()); 1090 EXPECT_EQ(kJohnDoe, get_values[0]); 1091 EXPECT_EQ(kJohnPDoe, get_values[1]); 1092 1093 // Update the values. 1094 AutofillProfile p2 = p; 1095 EXPECT_EQ(0, p.Compare(p2)); 1096 const base::string16 kNoOne(ASCIIToUTF16("4152110000")); 1097 set_values[1] = kNoOne; 1098 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 1099 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values); 1100 ASSERT_EQ(2UL, get_values.size()); 1101 EXPECT_EQ(kJohnDoe, get_values[0]); 1102 EXPECT_EQ(kNoOne, get_values[1]); 1103 EXPECT_NE(0, p.Compare(p2)); 1104 1105 // Delete values. 1106 set_values.clear(); 1107 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 1108 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values); 1109 ASSERT_EQ(1UL, get_values.size()); 1110 EXPECT_EQ(base::string16(), get_values[0]); 1111 1112 // Expect regular |GetInfo| returns empty value. 1113 EXPECT_EQ(base::string16(), p.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 1114 } 1115 1116 TEST(AutofillProfileTest, IsPresentButInvalid) { 1117 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 1118 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 1119 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 1120 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 1121 1122 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 1123 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 1124 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 1125 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 1126 1127 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C")); 1128 EXPECT_TRUE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 1129 1130 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1131 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE)); 1132 1133 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90")); 1134 EXPECT_TRUE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 1135 1136 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90210")); 1137 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)); 1138 1139 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("310")); 1140 EXPECT_TRUE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 1141 1142 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("(310) 310-6000")); 1143 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER)); 1144 } 1145 1146 TEST(AutofillProfileTest, SetRawInfoPreservesLineBreaks) { 1147 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 1148 1149 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, 1150 ASCIIToUTF16("123 Super St.\n" 1151 "Apt. #42")); 1152 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n" 1153 "Apt. #42"), 1154 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 1155 } 1156 1157 TEST(AutofillProfileTest, SetInfoPreservesLineBreaks) { 1158 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 1159 1160 profile.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS), 1161 ASCIIToUTF16("123 Super St.\n" 1162 "Apt. #42"), 1163 "en-US"); 1164 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n" 1165 "Apt. #42"), 1166 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 1167 } 1168 1169 TEST(AutofillProfileTest, SetRawInfoDoesntTrimWhitespace) { 1170 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 1171 1172 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("\tuser (at) example.com ")); 1173 EXPECT_EQ(ASCIIToUTF16("\tuser (at) example.com "), 1174 profile.GetRawInfo(EMAIL_ADDRESS)); 1175 } 1176 1177 TEST(AutofillProfileTest, SetInfoTrimsWhitespace) { 1178 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 1179 1180 profile.SetInfo(AutofillType(EMAIL_ADDRESS), 1181 ASCIIToUTF16("\tuser (at) example.com "), 1182 "en-US"); 1183 EXPECT_EQ(ASCIIToUTF16("user (at) example.com"), 1184 profile.GetRawInfo(EMAIL_ADDRESS)); 1185 } 1186 1187 TEST(AutofillProfileTest, FullAddress) { 1188 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 1189 test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison", 1190 "marion (at) me.xyz", "Fox", "123 Zoo St.", "unit 5", 1191 "Hollywood", "CA", "91601", "US", 1192 "12345678910"); 1193 1194 AutofillType full_address(HTML_TYPE_FULL_ADDRESS, HTML_MODE_NONE); 1195 base::string16 formatted_address(ASCIIToUTF16( 1196 "Marion Mitchell Morrison\n" 1197 "Fox\n" 1198 "123 Zoo St.\n" 1199 "unit 5\n" 1200 "Hollywood, CA 91601")); 1201 EXPECT_EQ(formatted_address, profile.GetInfo(full_address, "en-US")); 1202 // This should fail and leave the profile unchanged. 1203 EXPECT_FALSE(profile.SetInfo(full_address, ASCIIToUTF16("foobar"), "en-US")); 1204 EXPECT_EQ(formatted_address, profile.GetInfo(full_address, "en-US")); 1205 1206 // Some things can be missing... 1207 profile.SetInfo(AutofillType(ADDRESS_HOME_LINE2), 1208 base::string16(), 1209 "en-US"); 1210 profile.SetInfo(AutofillType(EMAIL_ADDRESS), 1211 base::string16(), 1212 "en-US"); 1213 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison\n" 1214 "Fox\n" 1215 "123 Zoo St.\n" 1216 "Hollywood, CA 91601"), 1217 profile.GetInfo(full_address, "en-US")); 1218 1219 // ...but nothing comes out if a required field is missing. 1220 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), base::string16(), "en-US"); 1221 EXPECT_TRUE(profile.GetInfo(full_address, "en-US").empty()); 1222 1223 // Restore the state but remove country. This should also fail. 1224 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), 1225 ASCIIToUTF16("CA"), 1226 "en-US"); 1227 EXPECT_FALSE(profile.GetInfo(full_address, "en-US").empty()); 1228 profile.SetInfo(AutofillType(ADDRESS_HOME_COUNTRY), 1229 base::string16(), 1230 "en-US"); 1231 EXPECT_TRUE(profile.GetInfo(full_address, "en-US").empty()); 1232 } 1233 1234 TEST(AutofillProfileTest, OverwriteOrAppendNames) { 1235 std::vector<TestCase> test_cases; 1236 1237 // Identical name. 1238 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1239 NameParts("Marion", "Mitchell", "Morrison"), 1240 NameParts("Marion", "Mitchell", "Morrison"))); 1241 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1242 NameParts("MARION", "MITCHELL", "MORRISON"), 1243 NameParts("Marion", "Mitchell", "Morrison"))); 1244 1245 // A parse that has a two-word last name should take precedence over a 1246 // parse that assumes the two names are a middle and a last name. 1247 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1248 NameParts("Marion", "", "Mitchell Morrison"), 1249 NameParts("Marion", "", "Mitchell Morrison"))); 1250 test_cases.push_back(TestCase(NameParts("Marion", "", "Mitchell Morrison"), 1251 NameParts("Marion", "Mitchell", "Morrison"), 1252 NameParts("Marion", "", "Mitchell Morrison"))); 1253 1254 // A parse that has a two-word first name should take precedence over a 1255 // parse that assumes the two names are a first and a middle name. 1256 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"), 1257 NameParts("Marion Mitchell", "", "Morrison"), 1258 NameParts("Marion Mitchell", "", "Morrison"))); 1259 test_cases.push_back(TestCase(NameParts("Marion Mitchell", "", "Morrison"), 1260 NameParts("Marion", "Mitchell", "Morrison"), 1261 NameParts("Marion Mitchell", "", "Morrison"))); 1262 1263 // Two names that are identical in full, but not in parts: the parse that 1264 // does *not* match the heuristic parse should be preferred. 1265 test_cases.push_back( 1266 TestCase(NameParts("Arthur", "Ignatius Conan", "Doyle"), 1267 // Heurstic parse. 1268 NameParts("Arthur Ignatius", "Conan", "Doyle"), 1269 NameParts("Arthur", "Ignatius Conan", "Doyle"))); 1270 test_cases.push_back( 1271 // Heuristic parse. 1272 TestCase(NameParts("Arthur Ignatius", "Conan", "Doyle"), 1273 NameParts("Arthur", "Ignatius Conan", "Doyle"), 1274 NameParts("Arthur", "Ignatius Conan", "Doyle"))); 1275 1276 // A parse that has a many-word first name and/or last name should take 1277 // precedence over a heuristically parsed name. 1278 test_cases.push_back( 1279 // Heuristic parse. 1280 TestCase(NameParts("Roberto Carlos da", "Silva", "Rocha"), 1281 NameParts("Roberto Carlos da Silva", "", "Rocha"), 1282 NameParts("Roberto Carlos da Silva", "", "Rocha"))); 1283 1284 // Cases where merging 2 profiles with same full names but 1285 // different canonical forms appends instead of overwrites, 1286 // provided they dont form heuristically parsed names. 1287 { 1288 NameParts name1("Marion Mitchell", "", "Morrison"); 1289 NameParts name2("Marion", "", "Mitchell Morrison"); 1290 std::vector<NameParts> starting_names(1, name1); 1291 std::vector<NameParts> additional_names(1, name2); 1292 std::vector<NameParts> expected_result; 1293 expected_result.push_back(name1); 1294 expected_result.push_back(name2); 1295 test_cases.push_back( 1296 TestCase(starting_names, additional_names, expected_result)); 1297 } 1298 1299 // Cases where the names do not have the same full name strings, 1300 // i.e. the list of merged names is longer than either of the incoming 1301 // lists. 1302 { 1303 NameParts name1("Antonio", "Augusto Ribeiro", "Reis Jr."); 1304 NameParts name2("Juninho", "", "Pernambucano"); 1305 NameParts name3("Marion", "Mitchell", "Morrison"); 1306 NameParts name4("Marion", "M.", "Morrison"); 1307 std::vector<NameParts> starting_names; 1308 std::vector<NameParts> additional_names; 1309 std::vector<NameParts> expected_result; 1310 starting_names.push_back(name1); 1311 starting_names.push_back(name2); 1312 additional_names.push_back(name3); 1313 additional_names.push_back(name4); 1314 expected_result.push_back(name1); 1315 expected_result.push_back(name2); 1316 expected_result.push_back(name3); 1317 expected_result.push_back(name4); 1318 test_cases.push_back( 1319 TestCase(starting_names, additional_names, expected_result)); 1320 } 1321 1322 for (std::vector<TestCase>::iterator it = test_cases.begin(); 1323 it != test_cases.end(); 1324 ++it) { 1325 TestCase current_case = *it; 1326 SCOPED_TRACE(current_case.starting_names[0].first + " + " + 1327 current_case.additional_names[0].first + " = " + 1328 current_case.expected_result[0].first); 1329 1330 std::vector<base::string16> first_names, middle_names, last_names; 1331 GetNamePartsList( 1332 current_case.starting_names, &first_names, &middle_names, &last_names); 1333 1334 // Construct the starting_profile. 1335 AutofillProfile starting_profile(base::GenerateGUID(), 1336 "https://www.example.com/"); 1337 1338 starting_profile.SetRawMultiInfo(NAME_FIRST, first_names); 1339 starting_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names); 1340 starting_profile.SetRawMultiInfo(NAME_LAST, last_names); 1341 1342 first_names.clear(); 1343 middle_names.clear(); 1344 last_names.clear(); 1345 GetNamePartsList( 1346 current_case.additional_names, &first_names, &middle_names, 1347 &last_names); 1348 1349 // Construct the additional_profile. 1350 AutofillProfile additional_profile(base::GenerateGUID(), 1351 "https://www.example.com/"); 1352 additional_profile.SetRawMultiInfo(NAME_FIRST, first_names); 1353 additional_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names); 1354 additional_profile.SetRawMultiInfo(NAME_LAST, last_names); 1355 1356 // Merge the names from the |additional_profile| into the |starting_profile| 1357 starting_profile.OverwriteWithOrAddTo(additional_profile, "en-US"); 1358 1359 // Verify the test expectations. 1360 first_names.clear(); 1361 middle_names.clear(); 1362 last_names.clear(); 1363 GetNamePartsList( 1364 current_case.expected_result, &first_names, &middle_names, &last_names); 1365 1366 std::vector<base::string16> merged_first_names, merged_middle_names, 1367 merged_last_names; 1368 starting_profile.GetRawMultiInfo(NAME_FIRST, &merged_first_names); 1369 starting_profile.GetRawMultiInfo(NAME_MIDDLE, &merged_middle_names); 1370 starting_profile.GetRawMultiInfo(NAME_LAST, &merged_last_names); 1371 ASSERT_EQ(current_case.expected_result.size(), merged_first_names.size()); 1372 ASSERT_EQ(current_case.expected_result.size(), merged_middle_names.size()); 1373 ASSERT_EQ(current_case.expected_result.size(), merged_last_names.size()); 1374 1375 for (size_t i = 0; i < current_case.expected_result.size(); ++i) { 1376 EXPECT_EQ(first_names[i], merged_first_names[i]); 1377 EXPECT_EQ(middle_names[i], merged_middle_names[i]); 1378 EXPECT_EQ(last_names[i], merged_last_names[i]); 1379 } 1380 } 1381 } 1382 1383 } // namespace autofill 1384