1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/browser/autofill_type.h"
15 #include "components/autofill/core/common/form_field_data.h"
16 #include "grit/component_strings.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using base::ASCIIToUTF16;
25 base::string16 GetLabel(AutofillProfile* profile) {
26 std::vector<AutofillProfile*> profiles;
27 profiles.push_back(profile);
28 std::vector<base::string16> labels;
29 AutofillProfile::CreateDifferentiatingLabels(profiles, &labels);
35 // Tests different possibilities for summary string generation.
36 // Based on existence of first name, last name, and address line 1.
37 TEST(AutofillProfileTest, PreviewSummaryString) {
39 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com/");
40 // Empty profile - nothing to update.
41 base::string16 summary0 = GetLabel(&profile0);
42 EXPECT_EQ(base::string16(), summary0);
44 // Case 0a/empty name and address, so the first two fields of the rest of the
45 // data is used: "Hollywood, CA"
46 AutofillProfile profile00(base::GenerateGUID(), "https://www.example.com/");
47 test::SetProfileInfo(&profile00, "", "", "",
48 "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", "91601", "US",
50 base::string16 summary00 = GetLabel(&profile00);
51 EXPECT_EQ(ASCIIToUTF16("Hollywood, CA"), summary00);
53 // Case 1: "<address>" without line 2.
54 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com/");
55 test::SetProfileInfo(&profile1, "", "", "",
56 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "", "Hollywood", "CA",
57 "91601", "US", "16505678910");
58 base::string16 summary1 = GetLabel(&profile1);
59 EXPECT_EQ(ASCIIToUTF16("123 Zoo St., Hollywood"), summary1);
61 // Case 1a: "<address>" with line 2.
62 AutofillProfile profile1a(base::GenerateGUID(), "https://www.example.com/");
63 test::SetProfileInfo(&profile1a, "", "", "",
64 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
65 "91601", "US", "16505678910");
66 base::string16 summary1a = GetLabel(&profile1a);
67 EXPECT_EQ(ASCIIToUTF16("123 Zoo St., unit 5"), summary1a);
69 // Case 2: "<lastname>"
70 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com/");
71 test::SetProfileInfo(&profile2, "", "Mitchell",
72 "Morrison", "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA",
73 "91601", "US", "16505678910");
74 base::string16 summary2 = GetLabel(&profile2);
75 // Summary includes full name, to the maximal extent available.
76 EXPECT_EQ(ASCIIToUTF16("Mitchell Morrison, Hollywood"), summary2);
78 // Case 3: "<lastname>, <address>"
79 AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com/");
80 test::SetProfileInfo(&profile3, "", "Mitchell",
81 "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "",
82 "Hollywood", "CA", "91601", "US", "16505678910");
83 base::string16 summary3 = GetLabel(&profile3);
84 EXPECT_EQ(ASCIIToUTF16("Mitchell Morrison, 123 Zoo St."), summary3);
86 // Case 4: "<firstname>"
87 AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com/");
88 test::SetProfileInfo(&profile4, "Marion", "Mitchell", "",
89 "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", "91601", "US",
91 base::string16 summary4 = GetLabel(&profile4);
92 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, Hollywood"), summary4);
94 // Case 5: "<firstname>, <address>"
95 AutofillProfile profile5(base::GenerateGUID(), "https://www.example.com/");
96 test::SetProfileInfo(&profile5, "Marion", "Mitchell", "",
97 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
98 "91601", "US", "16505678910");
99 base::string16 summary5 = GetLabel(&profile5);
100 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, 123 Zoo St."), summary5);
102 // Case 6: "<firstname> <lastname>"
103 AutofillProfile profile6(base::GenerateGUID(), "https://www.example.com/");
104 test::SetProfileInfo(&profile6, "Marion", "Mitchell",
105 "Morrison", "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA",
106 "91601", "US", "16505678910");
107 base::string16 summary6 = GetLabel(&profile6);
108 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, Hollywood"),
111 // Case 7: "<firstname> <lastname>, <address>"
112 AutofillProfile profile7(base::GenerateGUID(), "https://www.example.com/");
113 test::SetProfileInfo(&profile7, "Marion", "Mitchell",
114 "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
115 "Hollywood", "CA", "91601", "US", "16505678910");
116 base::string16 summary7 = GetLabel(&profile7);
117 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, 123 Zoo St."), summary7);
119 // Case 7a: "<firstname> <lastname>, <address>" - same as #7, except for
121 AutofillProfile profile7a(base::GenerateGUID(), "https://www.example.com/");
122 test::SetProfileInfo(&profile7a, "Marion", "Mitchell",
123 "Morrison", "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
124 "Hollywood", "CA", "91601", "US", "16505678910");
125 std::vector<AutofillProfile*> profiles;
126 profiles.push_back(&profile7);
127 profiles.push_back(&profile7a);
128 std::vector<base::string16> labels;
129 AutofillProfile::CreateDifferentiatingLabels(profiles, &labels);
130 ASSERT_EQ(profiles.size(), labels.size());
131 summary7 = labels[0];
132 base::string16 summary7a = labels[1];
133 EXPECT_EQ(ASCIIToUTF16(
134 "Marion Mitchell Morrison, 123 Zoo St., johnwayne@me.xyz"), summary7);
135 EXPECT_EQ(ASCIIToUTF16(
136 "Marion Mitchell Morrison, 123 Zoo St., marion@me.xyz"), summary7a);
139 TEST(AutofillProfileTest, AdjustInferredLabels) {
140 ScopedVector<AutofillProfile> profiles;
142 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
143 test::SetProfileInfo(
157 new AutofillProfile(base::GenerateGUID(), "http://www.example.com/"));
158 test::SetProfileInfo(
163 "janedoe@tertium.com",
171 std::vector<base::string16> labels;
172 AutofillProfile::CreateDifferentiatingLabels(profiles.get(), &labels);
173 ASSERT_EQ(2U, labels.size());
174 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels[0]);
175 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
178 new AutofillProfile(base::GenerateGUID(), "Chrome settings"));
179 test::SetProfileInfo(
184 "johndoe@tertium.com",
193 AutofillProfile::CreateDifferentiatingLabels(profiles.get(), &labels);
195 // Profile 0 and 2 inferred label now includes an e-mail.
196 ASSERT_EQ(3U, labels.size());
197 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com"),
199 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
200 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@tertium.com"),
206 new AutofillProfile(base::GenerateGUID(), std::string()));
207 test::SetProfileInfo(
216 "Elysium", "CO", // State is different
222 AutofillProfile::CreateDifferentiatingLabels(profiles.get(), &labels);
224 // Profile 0 and 2 inferred label now includes a state.
225 ASSERT_EQ(3U, labels.size());
226 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels[0]);
227 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
228 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO"), labels[2]);
231 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
232 test::SetProfileInfo(
241 "Elysium", "CO", // State is different for some.
244 "16504444444"); // Phone is different for some.
247 AutofillProfile::CreateDifferentiatingLabels(profiles.get(), &labels);
248 ASSERT_EQ(4U, labels.size());
249 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels[0]);
250 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
251 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, 16502111111"),
253 // This one differs from other ones by unique phone, so no need for extra
255 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, 16504444444"),
259 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
260 test::SetProfileInfo(
265 "johndoe@styx.com", // E-Mail is different for some.
269 "Elysium", "CO", // State is different for some.
272 "16504444444"); // Phone is different for some.
275 AutofillProfile::CreateDifferentiatingLabels(profiles.get(), &labels);
276 ASSERT_EQ(5U, labels.size());
277 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels[0]);
278 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
279 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@hades.com,"
280 " 16502111111"), labels[2]);
281 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@hades.com,"
282 " 16504444444"), labels[3]);
283 // This one differs from other ones by unique e-mail, so no need for extra
285 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@styx.com"),
289 TEST(AutofillProfileTest, CreateInferredLabels) {
290 ScopedVector<AutofillProfile> profiles;
292 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
293 test::SetProfileInfo(profiles[0],
306 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
307 test::SetProfileInfo(profiles[1],
311 "janedoe@tertium.com",
319 std::vector<base::string16> labels;
320 // Two fields at least - no filter.
321 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 2,
323 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels[0]);
324 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
326 // Three fields at least - no filter.
327 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 3,
329 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., Elysium"),
331 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore., Dis"),
334 std::vector<ServerFieldType> suggested_fields;
335 suggested_fields.push_back(ADDRESS_HOME_CITY);
336 suggested_fields.push_back(ADDRESS_HOME_STATE);
337 suggested_fields.push_back(ADDRESS_HOME_ZIP);
339 // Two fields at least, from suggested fields - no filter.
340 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
341 UNKNOWN_TYPE, 2, &labels);
342 EXPECT_EQ(ASCIIToUTF16("Elysium, CA"), labels[0]);
343 EXPECT_EQ(ASCIIToUTF16("Dis, CA"), labels[1]);
345 // Three fields at least, from suggested fields - no filter.
346 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
347 UNKNOWN_TYPE, 3, &labels);
348 EXPECT_EQ(ASCIIToUTF16("Elysium, CA, 91111"), labels[0]);
349 EXPECT_EQ(ASCIIToUTF16("Dis, CA, 91222"), labels[1]);
351 // Three fields at least, from suggested fields - but filter reduces available
353 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
354 ADDRESS_HOME_STATE, 3, &labels);
355 EXPECT_EQ(ASCIIToUTF16("Elysium, 91111"), labels[0]);
356 EXPECT_EQ(ASCIIToUTF16("Dis, 91222"), labels[1]);
358 suggested_fields.clear();
359 // In our implementation we always display NAME_FULL for all NAME* fields...
360 suggested_fields.push_back(NAME_MIDDLE);
361 // One field at least, from suggested fields - no filter.
362 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
363 UNKNOWN_TYPE, 1, &labels);
364 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]);
365 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]);
367 // One field at least, from suggested fields - filter the same as suggested
369 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
370 NAME_MIDDLE, 1, &labels);
371 EXPECT_EQ(base::string16(), labels[0]);
372 EXPECT_EQ(base::string16(), labels[1]);
374 suggested_fields.clear();
375 // In our implementation we always display NAME_FULL for NAME_MIDDLE_INITIAL
376 suggested_fields.push_back(NAME_MIDDLE_INITIAL);
377 // One field at least, from suggested fields - no filter.
378 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
379 UNKNOWN_TYPE, 1, &labels);
380 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]);
381 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]);
383 // One field at least, from suggested fields - filter same as the first non-
384 // unknown suggested field.
385 suggested_fields.clear();
386 suggested_fields.push_back(UNKNOWN_TYPE);
387 suggested_fields.push_back(NAME_FULL);
388 suggested_fields.push_back(ADDRESS_HOME_LINE1);
389 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
390 NAME_FULL, 1, &labels);
391 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels[0]);
392 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]);
395 // Test that we fall back to using the full name if there are no other
396 // distinguishing fields, but only if it makes sense given the suggested fields.
397 TEST(AutofillProfileTest, CreateInferredLabelsFallsBackToFullName) {
398 ScopedVector<AutofillProfile> profiles;
400 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
401 test::SetProfileInfo(profiles[0],
402 "John", "", "Doe", "doe@example.com", "",
403 "88 Nowhere Ave.", "", "", "", "", "", "");
405 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
406 test::SetProfileInfo(profiles[1],
407 "Johnny", "K", "Doe", "doe@example.com", "",
408 "88 Nowhere Ave.", "", "", "", "", "", "");
410 // If the only name field in the suggested fields is the excluded field, we
411 // should not fall back to the full name as a distinguishing field.
412 std::vector<ServerFieldType> suggested_fields;
413 suggested_fields.push_back(NAME_LAST);
414 suggested_fields.push_back(ADDRESS_HOME_LINE1);
415 suggested_fields.push_back(EMAIL_ADDRESS);
416 std::vector<base::string16> labels;
417 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
418 NAME_LAST, 1, &labels);
419 ASSERT_EQ(2U, labels.size());
420 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels[0]);
421 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels[1]);
423 // Otherwise, we should.
424 suggested_fields.push_back(NAME_FIRST);
425 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
426 NAME_LAST, 1, &labels);
427 ASSERT_EQ(2U, labels.size());
428 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., John Doe"), labels[0]);
429 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Johnny K Doe"), labels[1]);
432 // Test that we do not show duplicate fields in the labels.
433 TEST(AutofillProfileTest, CreateInferredLabelsNoDuplicatedFields) {
434 ScopedVector<AutofillProfile> profiles;
436 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
437 test::SetProfileInfo(profiles[0],
438 "John", "", "Doe", "doe@example.com", "",
439 "88 Nowhere Ave.", "", "", "", "", "", "");
441 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
442 test::SetProfileInfo(profiles[1],
443 "John", "", "Doe", "dojo@example.com", "",
444 "88 Nowhere Ave.", "", "", "", "", "", "");
446 // If the only name field in the suggested fields is the excluded field, we
447 // should not fall back to the full name as a distinguishing field.
448 std::vector<ServerFieldType> suggested_fields;
449 suggested_fields.push_back(ADDRESS_HOME_LINE1);
450 suggested_fields.push_back(ADDRESS_BILLING_LINE1);
451 suggested_fields.push_back(EMAIL_ADDRESS);
452 std::vector<base::string16> labels;
453 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
454 UNKNOWN_TYPE, 2, &labels);
455 ASSERT_EQ(2U, labels.size());
456 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., doe@example.com"), labels[0]);
457 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., dojo@example.com"), labels[1]);
460 // Make sure that empty fields are not treated as distinguishing fields.
461 TEST(AutofillProfileTest, CreateInferredLabelsSkipsEmptyFields) {
462 ScopedVector<AutofillProfile> profiles;
464 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
465 test::SetProfileInfo(profiles[0],
466 "John", "", "Doe", "doe@example.com",
467 "Gogole", "", "", "", "", "", "", "");
469 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
470 test::SetProfileInfo(profiles[1],
471 "John", "", "Doe", "doe@example.com",
472 "Ggoole", "", "", "", "", "", "", "");
474 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
475 test::SetProfileInfo(profiles[2],
476 "John", "", "Doe", "john.doe@example.com",
477 "Goolge", "", "", "", "", "", "", "");
479 std::vector<base::string16> labels;
480 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 3,
482 ASSERT_EQ(3U, labels.size());
483 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels[0]);
484 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Ggoole"), labels[1]);
485 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com, Goolge"), labels[2]);
487 // A field must have a non-empty value for each profile to be considered a
488 // distinguishing field.
489 profiles[1]->SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("88 Nowhere Ave."));
490 AutofillProfile::CreateInferredLabels(profiles.get(), NULL, UNKNOWN_TYPE, 1,
492 ASSERT_EQ(3U, labels.size());
493 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels[0]);
494 EXPECT_EQ(ASCIIToUTF16("John Doe, 88 Nowhere Ave., doe@example.com, Ggoole"),
495 labels[1]) << labels[1];
496 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com"), labels[2]);
499 // Test that labels that would otherwise have multiline values are flattened.
500 TEST(AutofillProfileTest, CreateInferredLabelsFlattensMultiLineValues) {
501 ScopedVector<AutofillProfile> profiles;
503 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
504 test::SetProfileInfo(profiles[0],
505 "John", "", "Doe", "doe@example.com", "",
506 "88 Nowhere Ave.", "Apt. 42", "", "", "", "", "");
508 // If the only name field in the suggested fields is the excluded field, we
509 // should not fall back to the full name as a distinguishing field.
510 std::vector<ServerFieldType> suggested_fields;
511 suggested_fields.push_back(NAME_FULL);
512 suggested_fields.push_back(ADDRESS_HOME_STREET_ADDRESS);
513 std::vector<base::string16> labels;
514 AutofillProfile::CreateInferredLabels(profiles.get(), &suggested_fields,
515 NAME_FULL, 1, &labels);
516 ASSERT_EQ(1U, labels.size());
517 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Apt. 42"), labels[0]);
520 TEST(AutofillProfileTest, IsSubsetOf) {
521 scoped_ptr<AutofillProfile> a, b;
523 // |a| is a subset of |b|.
525 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
527 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
528 test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
529 "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
531 test::SetProfileInfo(b.get(), "Thomas", NULL, "Jefferson",
532 "declaration_guy@gmail.com", "United States Government", "Monticello",
533 NULL, "Charlottesville", "Virginia", "22902", NULL, NULL);
534 EXPECT_TRUE(a->IsSubsetOf(*b, "en-US"));
536 // |b| is not a subset of |a|.
537 EXPECT_FALSE(b->IsSubsetOf(*a, "en-US"));
539 // |a| is a subset of |a|.
540 EXPECT_TRUE(a->IsSubsetOf(*a, "en-US"));
542 // One field in |b| is different.
544 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
546 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
547 test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
548 "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
550 test::SetProfileInfo(a.get(), "Thomas", NULL, "Adams",
551 "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
553 EXPECT_FALSE(a->IsSubsetOf(*b, "en-US"));
556 TEST(AutofillProfileTest, OverwriteWithOrAddTo) {
557 AutofillProfile a(base::GenerateGUID(), "https://www.example.com");
558 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison",
559 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
560 "Hollywood", "CA", "91601", "US",
562 std::vector<base::string16> names;
563 a.GetRawMultiInfo(NAME_FULL, &names);
564 names.push_back(ASCIIToUTF16("Marion Morrison"));
565 a.SetRawMultiInfo(NAME_FULL, names);
567 // Create an identical profile except that the new profile:
568 // (1) Has a different origin,
569 // (2) Has a different address line 2,
570 // (3) Lacks a company name,
571 // (4) Has a different full name variant, and
572 // (5) Has a language code.
573 AutofillProfile b = a;
574 b.set_guid(base::GenerateGUID());
575 b.set_origin("Chrome settings");
576 b.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("area 51"));
577 b.SetRawInfo(COMPANY_NAME, base::string16());
578 b.GetRawMultiInfo(NAME_FULL, &names);
579 names.push_back(ASCIIToUTF16("Marion M. Morrison"));
580 b.SetRawMultiInfo(NAME_FULL, names);
581 b.set_language_code("en");
583 a.OverwriteWithOrAddTo(b, "en-US");
584 EXPECT_EQ("Chrome settings", a.origin());
585 EXPECT_EQ(ASCIIToUTF16("area 51"), a.GetRawInfo(ADDRESS_HOME_LINE2));
586 EXPECT_EQ(ASCIIToUTF16("Fox"), a.GetRawInfo(COMPANY_NAME));
587 a.GetRawMultiInfo(NAME_FULL, &names);
588 ASSERT_EQ(3U, names.size());
589 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison"), names[0]);
590 EXPECT_EQ(ASCIIToUTF16("Marion Morrison"), names[1]);
591 EXPECT_EQ(ASCIIToUTF16("Marion M. Morrison"), names[2]);
592 EXPECT_EQ("en", a.language_code());
595 TEST(AutofillProfileTest, AssignmentOperator) {
596 AutofillProfile a(base::GenerateGUID(), "https://www.example.com/");
597 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison",
598 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
599 "Hollywood", "CA", "91601", "US",
602 // Result of assignment should be logically equal to the original profile.
603 AutofillProfile b(base::GenerateGUID(), "http://www.example.com/");
607 // Assignment to self should not change the profile value.
612 TEST(AutofillProfileTest, Copy) {
613 AutofillProfile a(base::GenerateGUID(), "https://www.example.com/");
614 test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison",
615 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
616 "Hollywood", "CA", "91601", "US",
619 // Clone should be logically equal to the original.
620 AutofillProfile b(a);
624 TEST(AutofillProfileTest, Compare) {
625 AutofillProfile a(base::GenerateGUID(), std::string());
626 AutofillProfile b(base::GenerateGUID(), std::string());
628 // Empty profiles are the same.
629 EXPECT_EQ(0, a.Compare(b));
631 // GUIDs don't count.
632 a.set_guid(base::GenerateGUID());
633 b.set_guid(base::GenerateGUID());
634 EXPECT_EQ(0, a.Compare(b));
636 // Origins don't count.
637 a.set_origin("apple");
638 b.set_origin("banana");
639 EXPECT_EQ(0, a.Compare(b));
641 // Different values produce non-zero results.
642 test::SetProfileInfo(&a, "Jimmy", NULL, NULL, NULL,
643 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
644 test::SetProfileInfo(&b, "Ringo", NULL, NULL, NULL,
645 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
646 EXPECT_GT(0, a.Compare(b));
647 EXPECT_LT(0, b.Compare(a));
649 // Phone numbers are compared by the full number, including the area code.
650 // This is a regression test for http://crbug.com/163024
651 test::SetProfileInfo(&a, NULL, NULL, NULL, NULL,
652 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "650.555.4321");
653 test::SetProfileInfo(&b, NULL, NULL, NULL, NULL,
654 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "408.555.4321");
655 EXPECT_GT(0, a.Compare(b));
656 EXPECT_LT(0, b.Compare(a));
659 TEST(AutofillProfileTest, MultiValueNames) {
660 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/");
661 const base::string16 kJohnDoe(ASCIIToUTF16("John Doe"));
662 const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe"));
663 std::vector<base::string16> set_values;
664 set_values.push_back(kJohnDoe);
665 set_values.push_back(kJohnPDoe);
666 p.SetRawMultiInfo(NAME_FULL, set_values);
668 // Expect regular |GetInfo| returns the first element.
669 EXPECT_EQ(kJohnDoe, p.GetRawInfo(NAME_FULL));
671 // Ensure that we get out what we put in.
672 std::vector<base::string16> get_values;
673 p.GetRawMultiInfo(NAME_FULL, &get_values);
674 ASSERT_EQ(2UL, get_values.size());
675 EXPECT_EQ(kJohnDoe, get_values[0]);
676 EXPECT_EQ(kJohnPDoe, get_values[1]);
678 // Update the values.
679 AutofillProfile p2 = p;
680 EXPECT_EQ(0, p.Compare(p2));
681 const base::string16 kNoOne(ASCIIToUTF16("No One"));
682 set_values[1] = kNoOne;
683 p.SetRawMultiInfo(NAME_FULL, set_values);
684 p.GetRawMultiInfo(NAME_FULL, &get_values);
685 ASSERT_EQ(2UL, get_values.size());
686 EXPECT_EQ(kJohnDoe, get_values[0]);
687 EXPECT_EQ(kNoOne, get_values[1]);
688 EXPECT_NE(0, p.Compare(p2));
692 p.SetRawMultiInfo(NAME_FULL, set_values);
693 p.GetRawMultiInfo(NAME_FULL, &get_values);
694 ASSERT_EQ(1UL, get_values.size());
695 EXPECT_EQ(base::string16(), get_values[0]);
697 // Expect regular |GetInfo| returns empty value.
698 EXPECT_EQ(base::string16(), p.GetRawInfo(NAME_FULL));
701 TEST(AutofillProfileTest, MultiValueEmails) {
702 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/");
703 const base::string16 kJohnDoe(ASCIIToUTF16("john@doe.com"));
704 const base::string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com"));
705 std::vector<base::string16> set_values;
706 set_values.push_back(kJohnDoe);
707 set_values.push_back(kJohnPDoe);
708 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
710 // Expect regular |GetInfo| returns the first element.
711 EXPECT_EQ(kJohnDoe, p.GetRawInfo(EMAIL_ADDRESS));
713 // Ensure that we get out what we put in.
714 std::vector<base::string16> get_values;
715 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values);
716 ASSERT_EQ(2UL, get_values.size());
717 EXPECT_EQ(kJohnDoe, get_values[0]);
718 EXPECT_EQ(kJohnPDoe, get_values[1]);
720 // Update the values.
721 AutofillProfile p2 = p;
722 EXPECT_EQ(0, p.Compare(p2));
723 const base::string16 kNoOne(ASCIIToUTF16("no@one.com"));
724 set_values[1] = kNoOne;
725 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
726 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values);
727 ASSERT_EQ(2UL, get_values.size());
728 EXPECT_EQ(kJohnDoe, get_values[0]);
729 EXPECT_EQ(kNoOne, get_values[1]);
730 EXPECT_NE(0, p.Compare(p2));
734 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
735 p.GetRawMultiInfo(EMAIL_ADDRESS, &get_values);
736 ASSERT_EQ(1UL, get_values.size());
737 EXPECT_EQ(base::string16(), get_values[0]);
739 // Expect regular |GetInfo| returns empty value.
740 EXPECT_EQ(base::string16(), p.GetRawInfo(EMAIL_ADDRESS));
743 TEST(AutofillProfileTest, MultiValuePhone) {
744 AutofillProfile p(base::GenerateGUID(), "https://www.example.com/");
745 const base::string16 kJohnDoe(ASCIIToUTF16("4151112222"));
746 const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333"));
747 std::vector<base::string16> set_values;
748 set_values.push_back(kJohnDoe);
749 set_values.push_back(kJohnPDoe);
750 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
752 // Expect regular |GetInfo| returns the first element.
753 EXPECT_EQ(kJohnDoe, p.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
755 // Ensure that we get out what we put in.
756 std::vector<base::string16> get_values;
757 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values);
758 ASSERT_EQ(2UL, get_values.size());
759 EXPECT_EQ(kJohnDoe, get_values[0]);
760 EXPECT_EQ(kJohnPDoe, get_values[1]);
762 // Update the values.
763 AutofillProfile p2 = p;
764 EXPECT_EQ(0, p.Compare(p2));
765 const base::string16 kNoOne(ASCIIToUTF16("4152110000"));
766 set_values[1] = kNoOne;
767 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
768 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values);
769 ASSERT_EQ(2UL, get_values.size());
770 EXPECT_EQ(kJohnDoe, get_values[0]);
771 EXPECT_EQ(kNoOne, get_values[1]);
772 EXPECT_NE(0, p.Compare(p2));
776 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
777 p.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values);
778 ASSERT_EQ(1UL, get_values.size());
779 EXPECT_EQ(base::string16(), get_values[0]);
781 // Expect regular |GetInfo| returns empty value.
782 EXPECT_EQ(base::string16(), p.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
785 TEST(AutofillProfileTest, IsPresentButInvalid) {
786 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
787 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE));
788 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP));
789 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER));
791 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
792 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE));
793 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP));
794 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER));
796 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C"));
797 EXPECT_TRUE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE));
799 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
800 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_STATE));
802 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90"));
803 EXPECT_TRUE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP));
805 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90210"));
806 EXPECT_FALSE(profile.IsPresentButInvalid(ADDRESS_HOME_ZIP));
808 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("310"));
809 EXPECT_TRUE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER));
811 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("(310) 310-6000"));
812 EXPECT_FALSE(profile.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER));
815 TEST(AutofillProfileTest, SetRawInfoPreservesLineBreaks) {
816 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
818 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS,
819 ASCIIToUTF16("123 Super St.\n"
821 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n"
823 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
826 TEST(AutofillProfileTest, SetInfoPreservesLineBreaks) {
827 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
829 profile.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS),
830 ASCIIToUTF16("123 Super St.\n"
833 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n"
835 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
838 TEST(AutofillProfileTest, SetRawInfoDoesntTrimWhitespace) {
839 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
841 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("\tuser@example.com "));
842 EXPECT_EQ(ASCIIToUTF16("\tuser@example.com "),
843 profile.GetRawInfo(EMAIL_ADDRESS));
846 TEST(AutofillProfileTest, SetInfoTrimsWhitespace) {
847 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
849 profile.SetInfo(AutofillType(EMAIL_ADDRESS),
850 ASCIIToUTF16("\tuser@example.com "),
852 EXPECT_EQ(ASCIIToUTF16("user@example.com"),
853 profile.GetRawInfo(EMAIL_ADDRESS));
856 } // namespace autofill