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