1// Copyright (c) 2011 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/memory/scoped_ptr.h"
7#include "base/memory/scoped_vector.h"
8#include "base/stl_util-inl.h"
9#include "base/string16.h"
10#include "base/utf_string_conversions.h"
11#include "chrome/browser/autofill/autofill_common_test.h"
12#include "chrome/browser/autofill/autofill_profile.h"
13#include "chrome/common/guid.h"
14#include "grit/generated_resources.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace {
18
19bool UpdateProfileLabel(AutofillProfile *profile) {
20  std::vector<AutofillProfile*> profiles;
21  profiles.push_back(profile);
22  return AutofillProfile::AdjustInferredLabels(&profiles);
23}
24
25}  // namespace
26
27// Tests different possibilities for summary string generation.
28// Based on existence of first name, last name, and address line 1.
29TEST(AutofillProfileTest, PreviewSummaryString) {
30  // Case 0/null: ""
31  AutofillProfile profile0;
32  // Empty profile - nothing to update.
33  EXPECT_FALSE(UpdateProfileLabel(&profile0));
34  string16 summary0 = profile0.Label();
35  EXPECT_EQ(string16(), summary0);
36
37  // Case 0a/empty name and address, so the first two fields of the rest of the
38  // data is used: "Hollywood, CA"
39  AutofillProfile profile00;
40  autofill_test::SetProfileInfo(&profile00, "", "Mitchell", "",
41      "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA", "91601", "US",
42      "12345678910", "01987654321");
43  EXPECT_TRUE(UpdateProfileLabel(&profile00));
44  string16 summary00 = profile00.Label();
45  EXPECT_EQ(ASCIIToUTF16("Hollywood, CA"), summary00);
46
47  // Case 1: "<address>"
48  AutofillProfile profile1;
49  autofill_test::SetProfileInfo(&profile1, "", "Mitchell", "",
50      "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
51      "91601", "US", "12345678910", "01987654321");
52  EXPECT_TRUE(UpdateProfileLabel(&profile1));
53  string16 summary1 = profile1.Label();
54  EXPECT_EQ(ASCIIToUTF16("123 Zoo St., Hollywood"), summary1);
55
56  // Case 2: "<lastname>"
57  AutofillProfile profile2;
58  autofill_test::SetProfileInfo(&profile2, "", "Mitchell",
59      "Morrison", "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA",
60      "91601", "US", "12345678910", "01987654321");
61  EXPECT_TRUE(UpdateProfileLabel(&profile2));
62  string16 summary2 = profile2.Label();
63  // Summary does include full name which is empty if the first name is empty.
64  EXPECT_EQ(ASCIIToUTF16("Hollywood, CA"), summary2);
65
66  // Case 3: "<lastname>, <address>"
67  AutofillProfile profile3;
68  autofill_test::SetProfileInfo(&profile3, "", "Mitchell",
69      "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
70      "Hollywood", "CA", "91601", "US", "12345678910", "01987654321");
71  EXPECT_TRUE(UpdateProfileLabel(&profile3));
72  string16 summary3 = profile3.Label();
73  EXPECT_EQ(ASCIIToUTF16("123 Zoo St., Hollywood"), summary3);
74
75  // Case 4: "<firstname>"
76  AutofillProfile profile4;
77  autofill_test::SetProfileInfo(&profile4, "Marion", "Mitchell", "",
78      "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA", "91601", "US",
79      "12345678910", "01987654321");
80  EXPECT_TRUE(UpdateProfileLabel(&profile4));
81  string16 summary4 = profile4.Label();
82  EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, Hollywood"), summary4);
83
84  // Case 5: "<firstname>, <address>"
85  AutofillProfile profile5;
86  autofill_test::SetProfileInfo(&profile5, "Marion", "Mitchell", "",
87      "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
88      "91601", "US", "12345678910", "01987654321");
89  EXPECT_TRUE(UpdateProfileLabel(&profile5));
90  string16 summary5 = profile5.Label();
91  EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, 123 Zoo St."), summary5);
92
93  // Case 6: "<firstname> <lastname>"
94  AutofillProfile profile6;
95  autofill_test::SetProfileInfo(&profile6, "Marion", "Mitchell",
96      "Morrison", "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA",
97      "91601", "US", "12345678910", "01987654321");
98  EXPECT_TRUE(UpdateProfileLabel(&profile6));
99  string16 summary6 = profile6.Label();
100  EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, Hollywood"),
101            summary6);
102
103  // Case 7: "<firstname> <lastname>, <address>"
104  AutofillProfile profile7;
105  autofill_test::SetProfileInfo(&profile7, "Marion", "Mitchell",
106      "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
107      "Hollywood", "CA", "91601", "US", "12345678910", "01987654321");
108  EXPECT_TRUE(UpdateProfileLabel(&profile7));
109  string16 summary7 = profile7.Label();
110  EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, 123 Zoo St."),
111            summary7);
112
113  // Case 7a: "<firstname> <lastname>, <address>" - same as #7, except for
114  // e-mail.
115  AutofillProfile profile7a;
116  autofill_test::SetProfileInfo(&profile7a, "Marion", "Mitchell",
117    "Morrison", "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
118    "Hollywood", "CA", "91601", "US", "12345678910", "01987654321");
119  std::vector<AutofillProfile*> profiles;
120  profiles.push_back(&profile7);
121  profiles.push_back(&profile7a);
122  EXPECT_TRUE(AutofillProfile::AdjustInferredLabels(&profiles));
123  summary7 = profile7.Label();
124  string16 summary7a = profile7a.Label();
125  EXPECT_EQ(ASCIIToUTF16(
126      "Marion Mitchell Morrison, 123 Zoo St., johnwayne@me.xyz"), summary7);
127  EXPECT_EQ(ASCIIToUTF16(
128      "Marion Mitchell Morrison, 123 Zoo St., marion@me.xyz"), summary7a);
129}
130
131TEST(AutofillProfileTest, AdjustInferredLabels) {
132  std::vector<AutofillProfile*> profiles;
133  profiles.push_back(new AutofillProfile);
134  autofill_test::SetProfileInfo(
135      profiles[0],
136      "John",
137      "",
138      "Doe",
139      "johndoe@hades.com",
140      "Underworld",
141      "666 Erebus St.",
142      "",
143      "Elysium", "CA",
144      "91111",
145      "US",
146      "11111111111",
147      "22222222222");
148  profiles.push_back(new AutofillProfile);
149  autofill_test::SetProfileInfo(
150      profiles[1],
151      "Jane",
152      "",
153      "Doe",
154      "janedoe@tertium.com",
155      "Pluto Inc.",
156      "123 Letha Shore.",
157      "",
158      "Dis", "CA",
159      "91222",
160      "US",
161      "12345678910",
162      "01987654321");
163  // As labels are empty they are adjusted the first time.
164  EXPECT_TRUE(AutofillProfile::AdjustInferredLabels(&profiles));
165  // No need to adjust them anymore.
166  EXPECT_FALSE(AutofillProfile::AdjustInferredLabels(&profiles));
167  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."),
168            profiles[0]->Label());
169  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."),
170            profiles[1]->Label());
171
172  profiles.push_back(new AutofillProfile);
173  autofill_test::SetProfileInfo(
174      profiles[2],
175      "John",
176      "",
177      "Doe",
178      "johndoe@tertium.com",
179      "Underworld",
180      "666 Erebus St.",
181      "",
182      "Elysium", "CA",
183      "91111",
184      "US",
185      "11111111111",
186      "22222222222");
187  EXPECT_TRUE(AutofillProfile::AdjustInferredLabels(&profiles));
188
189  // Profile 0 and 2 inferred label now includes an e-mail.
190  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com"),
191            profiles[0]->Label());
192  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."),
193            profiles[1]->Label());
194  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@tertium.com"),
195            profiles[2]->Label());
196
197  delete profiles[2];
198  profiles.pop_back();
199
200  profiles.push_back(new AutofillProfile);
201  autofill_test::SetProfileInfo(
202      profiles[2],
203      "John",
204      "",
205      "Doe",
206      "johndoe@hades.com",
207      "Underworld",
208      "666 Erebus St.",
209      "",
210      "Elysium", "CA",
211      "91111",
212      "US",
213      "11111111111",
214      "33333333333");  // Fax is different
215
216  EXPECT_TRUE(AutofillProfile::AdjustInferredLabels(&profiles));
217
218  // Profile 0 and 2 inferred label now includes a fax number.
219  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., fax:#22222222222"),
220            profiles[0]->Label());
221  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."),
222            profiles[1]->Label());
223  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., fax:#33333333333"),
224            profiles[2]->Label());
225
226  profiles.push_back(new AutofillProfile);
227  autofill_test::SetProfileInfo(
228      profiles[3],
229      "John",
230      "",
231      "Doe",
232      "johndoe@hades.com",
233      "Underworld",
234      "666 Erebus St.",
235      "",
236      "Elysium", "CA",
237      "91111",
238      "US",
239      "44444444444",  // Phone is different for some.
240      "33333333333");  // Fax is different for some.
241
242  EXPECT_TRUE(AutofillProfile::AdjustInferredLabels(&profiles));
243
244  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., 11111111111,"
245                         " fax:#22222222222"),
246            profiles[0]->Label());
247  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."),
248            profiles[1]->Label());
249  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., 11111111111,"
250                         " fax:#33333333333"),
251            profiles[2]->Label());
252  // This one differs from other ones by unique phone, so no need for extra
253  // information.
254  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., 44444444444"),
255            profiles[3]->Label());
256
257  profiles.push_back(new AutofillProfile);
258  autofill_test::SetProfileInfo(
259      profiles[4],
260      "John",
261      "",
262      "Doe",
263      "johndoe@styx.com",  // E-Mail is different for some.
264      "Underworld",
265      "666 Erebus St.",
266      "",
267      "Elysium", "CA",
268      "91111",
269      "US",
270      "44444444444",  // Phone is different for some.
271      "33333333333");  // Fax is different for some.
272
273  EXPECT_TRUE(AutofillProfile::AdjustInferredLabels(&profiles));
274
275  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com,"
276                         " 11111111111, fax:#22222222222"),
277            profiles[0]->Label());
278  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."),
279            profiles[1]->Label());
280  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com,"
281                         " 11111111111, fax:#33333333333"),
282            profiles[2]->Label());
283  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com,"
284                         " 44444444444, fax:#33333333333"),
285            profiles[3]->Label());
286  // This one differs from other ones by unique e-mail, so no need for extra
287  // information.
288  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@styx.com"),
289            profiles[4]->Label());
290
291  EXPECT_FALSE(AutofillProfile::AdjustInferredLabels(&profiles));
292
293  // Clean up.
294  STLDeleteContainerPointers(profiles.begin(), profiles.end());
295}
296
297TEST(AutofillProfileTest, CreateInferredLabels) {
298  std::vector<AutofillProfile*> profiles;
299  profiles.push_back(new AutofillProfile);
300  autofill_test::SetProfileInfo(profiles[0],
301                                "John",
302                                "",
303                                "Doe",
304                                "johndoe@hades.com",
305                                "Underworld",
306                                "666 Erebus St.",
307                                "",
308                                "Elysium", "CA",
309                                "91111",
310                                "US",
311                                "11111111111",
312                                "22222222222");
313  profiles.push_back(new AutofillProfile);
314  autofill_test::SetProfileInfo(profiles[1],
315                                "Jane",
316                                "",
317                                "Doe",
318                                "janedoe@tertium.com",
319                                "Pluto Inc.",
320                                "123 Letha Shore.",
321                                "",
322                                "Dis", "CA",
323                                "91222",
324                                "US",
325                                "12345678910",
326                                "01987654321");
327  std::vector<string16> labels;
328  // Two fields at least - no filter.
329  AutofillProfile::CreateInferredLabels(&profiles, NULL, UNKNOWN_TYPE, 2,
330                                        &labels);
331  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels[0]);
332  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels[1]);
333
334  // Three fields at least - no filter.
335  AutofillProfile::CreateInferredLabels(&profiles, NULL, UNKNOWN_TYPE, 3,
336                                        &labels);
337  EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., Elysium"),
338            labels[0]);
339  EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore., Dis"),
340            labels[1]);
341
342  std::vector<AutofillFieldType> suggested_fields;
343  suggested_fields.push_back(ADDRESS_HOME_CITY);
344  suggested_fields.push_back(ADDRESS_HOME_STATE);
345  suggested_fields.push_back(ADDRESS_HOME_ZIP);
346
347  // Two fields at least, from suggested fields - no filter.
348  AutofillProfile::CreateInferredLabels(&profiles, &suggested_fields,
349                                        UNKNOWN_TYPE, 2, &labels);
350  EXPECT_EQ(ASCIIToUTF16("Elysium, CA"), labels[0]);
351  EXPECT_EQ(ASCIIToUTF16("Dis, CA"), labels[1]);
352
353  // Three fields at least, from suggested fields - no filter.
354  AutofillProfile::CreateInferredLabels(&profiles, &suggested_fields,
355                                        UNKNOWN_TYPE, 3, &labels);
356  EXPECT_EQ(ASCIIToUTF16("Elysium, CA, 91111"), labels[0]);
357  EXPECT_EQ(ASCIIToUTF16("Dis, CA, 91222"), labels[1]);
358
359  // Three fields at least, from suggested fields - but filter reduces available
360  // fields to two.
361  AutofillProfile::CreateInferredLabels(&profiles, &suggested_fields,
362                                        ADDRESS_HOME_STATE, 3, &labels);
363  EXPECT_EQ(ASCIIToUTF16("Elysium, 91111"), labels[0]);
364  EXPECT_EQ(ASCIIToUTF16("Dis, 91222"), labels[1]);
365
366  suggested_fields.clear();
367  // In our implementation we always display NAME_FULL for all NAME* fields...
368  suggested_fields.push_back(NAME_MIDDLE);
369  // One field at least, from suggested fields - no filter.
370  AutofillProfile::CreateInferredLabels(&profiles, &suggested_fields,
371                                        UNKNOWN_TYPE, 1, &labels);
372  EXPECT_EQ(ASCIIToUTF16("John Doe"), labels[0]);
373  EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels[1]);
374
375  // One field at least, from suggested fields - filter the same as suggested
376  // field.
377  AutofillProfile::CreateInferredLabels(&profiles, &suggested_fields,
378                                        NAME_MIDDLE, 1, &labels);
379  EXPECT_EQ(string16(), labels[0]);
380  EXPECT_EQ(string16(), labels[1]);
381
382  // One field at least, from suggested fields - filter same as the first non-
383  // unknown suggested field.
384  suggested_fields.clear();
385  suggested_fields.push_back(UNKNOWN_TYPE);
386  suggested_fields.push_back(NAME_FULL);
387  suggested_fields.push_back(ADDRESS_HOME_LINE1);
388  AutofillProfile::CreateInferredLabels(&profiles, &suggested_fields, NAME_FULL,
389                                        1, &labels);
390  EXPECT_EQ(string16(ASCIIToUTF16("666 Erebus St.")), labels[0]);
391  EXPECT_EQ(string16(ASCIIToUTF16("123 Letha Shore.")), labels[1]);
392
393  // Clean up.
394  STLDeleteContainerPointers(profiles.begin(), profiles.end());
395}
396
397// Test that we fall back to using the full name if there are no other
398// distinguishing fields, but only if it makes sense given the suggested fields.
399TEST(AutofillProfileTest, CreateInferredLabelsFallsBackToFullName) {
400  ScopedVector<AutofillProfile> profiles;
401  profiles.push_back(new AutofillProfile);
402  autofill_test::SetProfileInfo(profiles[0],
403                                "John", "", "Doe", "doe@example.com", "",
404                                "88 Nowhere Ave.", "", "", "", "", "", "", "");
405  profiles.push_back(new AutofillProfile);
406  autofill_test::SetProfileInfo(profiles[1],
407                                "Johnny", "K", "Doe", "doe@example.com", "",
408                                "88 Nowhere Ave.", "", "", "", "", "", "", "");
409
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<AutofillFieldType> 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<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]);
422
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]);
430}
431
432// Test that we do not show duplicate fields in the labels.
433TEST(AutofillProfileTest, CreateInferredLabelsNoDuplicatedFields) {
434  ScopedVector<AutofillProfile> profiles;
435  profiles.push_back(new AutofillProfile);
436  autofill_test::SetProfileInfo(profiles[0],
437                                "John", "", "Doe", "doe@example.com", "",
438                                "88 Nowhere Ave.", "", "", "", "", "", "", "");
439  profiles.push_back(new AutofillProfile);
440  autofill_test::SetProfileInfo(profiles[1],
441                                "John", "", "Doe", "dojo@example.com", "",
442                                "88 Nowhere Ave.", "", "", "", "", "", "", "");
443
444  // If the only name field in the suggested fields is the excluded field, we
445  // should not fall back to the full name as a distinguishing field.
446  std::vector<AutofillFieldType> suggested_fields;
447  suggested_fields.push_back(ADDRESS_HOME_LINE1);
448  suggested_fields.push_back(ADDRESS_BILLING_LINE1);
449  suggested_fields.push_back(EMAIL_ADDRESS);
450  std::vector<string16> labels;
451  AutofillProfile::CreateInferredLabels(&profiles.get(), &suggested_fields,
452                                        UNKNOWN_TYPE, 2, &labels);
453  ASSERT_EQ(2U, labels.size());
454  EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., doe@example.com"), labels[0]);
455  EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., dojo@example.com"), labels[1]);
456}
457
458// Make sure that empty fields are not treated as distinguishing fields.
459TEST(AutofillProfileTest, CreateInferredLabelsSkipsEmptyFields) {
460  ScopedVector<AutofillProfile> profiles;
461  profiles.push_back(new AutofillProfile);
462  autofill_test::SetProfileInfo(profiles[0],
463                                "John", "", "Doe", "doe@example.com",
464                                "Gogole", "", "", "", "", "", "", "", "");
465  profiles.push_back(new AutofillProfile);
466  autofill_test::SetProfileInfo(profiles[1],
467                                "John", "", "Doe", "doe@example.com",
468                                "Ggoole", "", "", "", "", "", "", "", "");
469  profiles.push_back(new AutofillProfile);
470  autofill_test::SetProfileInfo(profiles[2],
471                                "John", "", "Doe", "john.doe@example.com",
472                                "Goolge", "", "", "", "", "", "", "", "");
473
474  std::vector<string16> labels;
475  AutofillProfile::CreateInferredLabels(&profiles.get(), NULL, UNKNOWN_TYPE, 3,
476                                        &labels);
477  ASSERT_EQ(3U, labels.size());
478  EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels[0]);
479  EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Ggoole"), labels[1]);
480  EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com, Goolge"), labels[2]);
481
482  // A field must have a non-empty value for each profile to be considered a
483  // distinguishing field.
484  profiles[1]->SetInfo(ADDRESS_HOME_LINE1,
485                       ASCIIToUTF16("88 Nowhere Ave."));
486  AutofillProfile::CreateInferredLabels(&profiles.get(), NULL, UNKNOWN_TYPE, 1,
487                                        &labels);
488  ASSERT_EQ(3U, labels.size());
489  EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels[0]);
490  EXPECT_EQ(ASCIIToUTF16("John Doe, 88 Nowhere Ave., doe@example.com, Ggoole"),
491            labels[1]) << labels[1];
492  EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com"), labels[2]);
493}
494
495TEST(AutofillProfileTest, IsSubsetOf) {
496  scoped_ptr<AutofillProfile> a, b;
497
498  // |a| is a subset of |b|.
499  a.reset(new AutofillProfile);
500  b.reset(new AutofillProfile);
501  autofill_test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
502      "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
503      NULL, NULL);
504  autofill_test::SetProfileInfo(b.get(), "Thomas", NULL, "Jefferson",
505      "declaration_guy@gmail.com", "United States Government", "Monticello",
506      NULL, "Charlottesville", "Virginia", "22902", NULL, NULL, NULL);
507  EXPECT_TRUE(a->IsSubsetOf(*b));
508
509  // |b| is not a subset of |a|.
510  EXPECT_FALSE(b->IsSubsetOf(*a));
511
512  // |a| is a subset of |a|.
513  EXPECT_TRUE(a->IsSubsetOf(*a));
514
515  // One field in |b| is different.
516  a.reset(new AutofillProfile);
517  b.reset(new AutofillProfile);
518  autofill_test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
519      "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
520      NULL, NULL);
521  autofill_test::SetProfileInfo(a.get(), "Thomas", NULL, "Adams",
522      "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
523      NULL, NULL);
524  EXPECT_FALSE(a->IsSubsetOf(*b));
525}
526
527TEST(AutofillProfileTest, IntersectionOfTypesHasEqualValues) {
528  scoped_ptr<AutofillProfile> a, b;
529
530  // Intersection of types contains the fields NAME_FIRST, NAME_LAST,
531  // EMAIL_ADDRESS.  The values of these field types are equal between the two
532  // profiles.
533  a.reset(new AutofillProfile);
534  b.reset(new AutofillProfile);
535  autofill_test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
536      "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
537      "12134759123", "19384284720");
538  autofill_test::SetProfileInfo(b.get(), "Thomas", NULL, "Jefferson",
539      "declaration_guy@gmail.com", "United States Government", "Monticello",
540      NULL, "Charlottesville", "Virginia", "22902", NULL, NULL, NULL);
541  EXPECT_TRUE(a->IntersectionOfTypesHasEqualValues(*b));
542
543  // Intersection of types contains the fields NAME_FIRST, NAME_LAST,
544  // EMAIL_ADDRESS. The value of EMAIL_ADDRESS differs between the two profiles.
545  a.reset(new AutofillProfile);
546  b.reset(new AutofillProfile);
547  autofill_test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
548      "poser@yahoo.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
549      "12134759123", "19384284720");
550  autofill_test::SetProfileInfo(b.get(), "Thomas", NULL, "Jefferson",\
551      "declaration_guy@gmail.com", "United States Government", "Monticello",
552      NULL, "Charlottesville", "Virginia", "22902", NULL, NULL, NULL);
553  EXPECT_FALSE(a->IntersectionOfTypesHasEqualValues(*b));
554
555  // Intersection of types is empty.
556  a.reset(new AutofillProfile);
557  b.reset(new AutofillProfile);
558  autofill_test::SetProfileInfo(a.get(), "Thomas", NULL, "Jefferson",
559      "poser@yahoo.com", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
560      "12134759123", "19384284720");
561  autofill_test::SetProfileInfo(b.get(), NULL, NULL, NULL, NULL,
562      "United States Government", "Monticello", NULL, "Charlottesville",
563      "Virginia", "22902", NULL, NULL, NULL);
564  EXPECT_FALSE(a->IntersectionOfTypesHasEqualValues(*b));
565}
566
567TEST(AutofillProfileTest, MergeWith) {
568  scoped_ptr<AutofillProfile> a, b;
569
570  // Merge |b| into |a|.
571  a.reset(new AutofillProfile);
572  b.reset(new AutofillProfile);
573  autofill_test::SetProfileInfo(a.get(), "Jimmy", NULL, NULL, NULL,
574      NULL, NULL, NULL, NULL, NULL, NULL, NULL, "12134759123", "19384284720");
575  autofill_test::SetProfileInfo(b.get(), "James", NULL, "Madison",
576      "constitutionalist@gmail.com", "United States Government", "Monticello",
577      NULL, "Charlottesville", "Virginia", "22902", NULL, NULL, NULL);
578  AutofillProfile expected_b(*b);
579  a->MergeWith(*b);
580
581  AutofillProfile expected_a;
582  autofill_test::SetProfileInfo(&expected_a, "Jimmy", NULL, "Madison",
583      "constitutionalist@gmail.com", "United States Government", "Monticello",
584      NULL, "Charlottesville", "Virginia", "22902", NULL, "12134759123",
585      "19384284720");
586  EXPECT_EQ(0, expected_a.Compare(*a));
587  EXPECT_EQ(0, expected_b.Compare(*b));
588}
589
590TEST(AutofillProfileTest, AssignmentOperator){
591  AutofillProfile a, b;
592
593  // Result of assignment should be logically equal to the original profile.
594  autofill_test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison",
595                                "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
596                                "Hollywood", "CA", "91601", "US", "12345678910",
597                                "01987654321");
598  b = a;
599  EXPECT_TRUE(a == b);
600
601  // Assignment to self should not change the profile value.
602  a = a;
603  EXPECT_TRUE(a == b);
604}
605
606TEST(AutofillProfileTest, Copy) {
607  AutofillProfile a;
608
609  // Clone should be logically equal to the original.
610  autofill_test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison",
611                                "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
612                                "Hollywood", "CA", "91601", "US", "12345678910",
613                                "01987654321");
614  AutofillProfile b(a);
615  EXPECT_TRUE(a == b);
616}
617
618TEST(AutofillProfileTest, Compare) {
619  AutofillProfile a, b;
620
621  // Empty profiles are the same.
622  EXPECT_EQ(0, a.Compare(b));
623
624  // GUIDs don't count.
625  a.set_guid(guid::GenerateGUID());
626  b.set_guid(guid::GenerateGUID());
627  EXPECT_EQ(0, a.Compare(b));
628
629  // Different values produce non-zero results.
630  autofill_test::SetProfileInfo(&a, "Jimmy", NULL, NULL, NULL,
631      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
632  autofill_test::SetProfileInfo(&b, "Ringo", NULL, NULL, NULL,
633      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
634  EXPECT_GT(0, a.Compare(b));
635  EXPECT_LT(0, b.Compare(a));
636}
637
638TEST(AutofillProfileTest, CountryCode) {
639  AutofillProfile profile;
640  EXPECT_EQ(std::string(), profile.CountryCode());
641
642  profile.SetCountryCode("US");
643  EXPECT_EQ("US", profile.CountryCode());
644}
645
646TEST(AutofillProfileTest, MultiValueNames) {
647  AutofillProfile p;
648  const string16 kJohnDoe(ASCIIToUTF16("John Doe"));
649  const string16 kJohnPDoe(ASCIIToUTF16("John P. Doe"));
650  std::vector<string16> set_values;
651  set_values.push_back(kJohnDoe);
652  set_values.push_back(kJohnPDoe);
653  p.SetMultiInfo(NAME_FULL, set_values);
654
655  // Expect regular |GetInfo| returns the first element.
656  EXPECT_EQ(kJohnDoe, p.GetInfo(NAME_FULL));
657
658  // Ensure that we get out what we put in.
659  std::vector<string16> get_values;
660  p.GetMultiInfo(NAME_FULL, &get_values);
661  ASSERT_EQ(2UL, get_values.size());
662  EXPECT_EQ(kJohnDoe, get_values[0]);
663  EXPECT_EQ(kJohnPDoe, get_values[1]);
664
665  // Update the values.
666  AutofillProfile p2 = p;
667  EXPECT_EQ(0, p.Compare(p2));
668  EXPECT_EQ(0, p.CompareMulti(p2));
669  const string16 kNoOne(ASCIIToUTF16("No One"));
670  set_values[1] = kNoOne;
671  p.SetMultiInfo(NAME_FULL, set_values);
672  p.GetMultiInfo(NAME_FULL, &get_values);
673  ASSERT_EQ(2UL, get_values.size());
674  EXPECT_EQ(kJohnDoe, get_values[0]);
675  EXPECT_EQ(kNoOne, get_values[1]);
676  EXPECT_EQ(0, p.Compare(p2));
677  EXPECT_NE(0, p.CompareMulti(p2));
678
679  // Delete values.
680  set_values.clear();
681  p.SetMultiInfo(NAME_FULL, set_values);
682  p.GetMultiInfo(NAME_FULL, &get_values);
683  ASSERT_EQ(1UL, get_values.size());
684  EXPECT_EQ(string16(), get_values[0]);
685
686  // Expect regular |GetInfo| returns empty value.
687  EXPECT_EQ(string16(), p.GetInfo(NAME_FULL));
688}
689
690TEST(AutofillProfileTest, MultiValueEmails) {
691  AutofillProfile p;
692  const string16 kJohnDoe(ASCIIToUTF16("john@doe.com"));
693  const string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com"));
694  std::vector<string16> set_values;
695  set_values.push_back(kJohnDoe);
696  set_values.push_back(kJohnPDoe);
697  p.SetMultiInfo(EMAIL_ADDRESS, set_values);
698
699  // Expect regular |GetInfo| returns the first element.
700  EXPECT_EQ(kJohnDoe, p.GetInfo(EMAIL_ADDRESS));
701
702  // Ensure that we get out what we put in.
703  std::vector<string16> get_values;
704  p.GetMultiInfo(EMAIL_ADDRESS, &get_values);
705  ASSERT_EQ(2UL, get_values.size());
706  EXPECT_EQ(kJohnDoe, get_values[0]);
707  EXPECT_EQ(kJohnPDoe, get_values[1]);
708
709  // Update the values.
710  AutofillProfile p2 = p;
711  EXPECT_EQ(0, p.Compare(p2));
712  EXPECT_EQ(0, p.CompareMulti(p2));
713  const string16 kNoOne(ASCIIToUTF16("no@one.com"));
714  set_values[1] = kNoOne;
715  p.SetMultiInfo(EMAIL_ADDRESS, set_values);
716  p.GetMultiInfo(EMAIL_ADDRESS, &get_values);
717  ASSERT_EQ(2UL, get_values.size());
718  EXPECT_EQ(kJohnDoe, get_values[0]);
719  EXPECT_EQ(kNoOne, get_values[1]);
720  EXPECT_EQ(0, p.Compare(p2));
721  EXPECT_NE(0, p.CompareMulti(p2));
722
723  // Delete values.
724  set_values.clear();
725  p.SetMultiInfo(EMAIL_ADDRESS, set_values);
726  p.GetMultiInfo(EMAIL_ADDRESS, &get_values);
727  ASSERT_EQ(1UL, get_values.size());
728  EXPECT_EQ(string16(), get_values[0]);
729
730  // Expect regular |GetInfo| returns empty value.
731  EXPECT_EQ(string16(), p.GetInfo(EMAIL_ADDRESS));
732}
733
734TEST(AutofillProfileTest, MultiValuePhone) {
735  AutofillProfile p;
736  const string16 kJohnDoe(ASCIIToUTF16("4151112222"));
737  const string16 kJohnPDoe(ASCIIToUTF16("4151113333"));
738  std::vector<string16> set_values;
739  set_values.push_back(kJohnDoe);
740  set_values.push_back(kJohnPDoe);
741  p.SetMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
742
743  // Expect regular |GetInfo| returns the first element.
744  EXPECT_EQ(kJohnDoe, p.GetInfo(PHONE_HOME_WHOLE_NUMBER));
745
746  // Ensure that we get out what we put in.
747  std::vector<string16> get_values;
748  p.GetMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values);
749  ASSERT_EQ(2UL, get_values.size());
750  EXPECT_EQ(kJohnDoe, get_values[0]);
751  EXPECT_EQ(kJohnPDoe, get_values[1]);
752
753  // Update the values.
754  AutofillProfile p2 = p;
755  EXPECT_EQ(0, p.Compare(p2));
756  EXPECT_EQ(0, p.CompareMulti(p2));
757  const string16 kNoOne(ASCIIToUTF16("4151110000"));
758  set_values[1] = kNoOne;
759  p.SetMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
760  p.GetMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values);
761  ASSERT_EQ(2UL, get_values.size());
762  EXPECT_EQ(kJohnDoe, get_values[0]);
763  EXPECT_EQ(kNoOne, get_values[1]);
764  EXPECT_EQ(0, p.Compare(p2));
765  EXPECT_NE(0, p.CompareMulti(p2));
766
767  // Delete values.
768  set_values.clear();
769  p.SetMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
770  p.GetMultiInfo(PHONE_HOME_WHOLE_NUMBER, &get_values);
771  ASSERT_EQ(1UL, get_values.size());
772  EXPECT_EQ(string16(), get_values[0]);
773
774  // Expect regular |GetInfo| returns empty value.
775  EXPECT_EQ(string16(), p.GetInfo(PHONE_HOME_WHOLE_NUMBER));
776}
777
778TEST(AutofillProfileTest, MultiValueFax) {
779  AutofillProfile p;
780  const string16 kJohnDoe(ASCIIToUTF16("4151112222"));
781  const string16 kJohnPDoe(ASCIIToUTF16("4151113333"));
782  std::vector<string16> set_values;
783  set_values.push_back(kJohnDoe);
784  set_values.push_back(kJohnPDoe);
785  p.SetMultiInfo(PHONE_FAX_WHOLE_NUMBER, set_values);
786
787  // Expect regular |GetInfo| returns the first element.
788  EXPECT_EQ(kJohnDoe, p.GetInfo(PHONE_FAX_WHOLE_NUMBER));
789
790  // Ensure that we get out what we put in.
791  std::vector<string16> get_values;
792  p.GetMultiInfo(PHONE_FAX_WHOLE_NUMBER, &get_values);
793  ASSERT_EQ(2UL, get_values.size());
794  EXPECT_EQ(kJohnDoe, get_values[0]);
795  EXPECT_EQ(kJohnPDoe, get_values[1]);
796
797  // Update the values.
798  AutofillProfile p2 = p;
799  EXPECT_EQ(0, p.Compare(p2));
800  EXPECT_EQ(0, p.CompareMulti(p2));
801  const string16 kNoOne(ASCIIToUTF16("4151110000"));
802  set_values[1] = kNoOne;
803  p.SetMultiInfo(PHONE_FAX_WHOLE_NUMBER, set_values);
804  p.GetMultiInfo(PHONE_FAX_WHOLE_NUMBER, &get_values);
805  ASSERT_EQ(2UL, get_values.size());
806  EXPECT_EQ(kJohnDoe, get_values[0]);
807  EXPECT_EQ(kNoOne, get_values[1]);
808  EXPECT_EQ(0, p.Compare(p2));
809  EXPECT_NE(0, p.CompareMulti(p2));
810
811  // Delete values.
812  set_values.clear();
813  p.SetMultiInfo(PHONE_FAX_WHOLE_NUMBER, set_values);
814  p.GetMultiInfo(PHONE_FAX_WHOLE_NUMBER, &get_values);
815  ASSERT_EQ(1UL, get_values.size());
816  EXPECT_EQ(string16(), get_values[0]);
817
818  // Expect regular |GetInfo| returns empty value.
819  EXPECT_EQ(string16(), p.GetInfo(PHONE_FAX_WHOLE_NUMBER));
820}
821