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