summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_profile_unittest.cc
blob: 2a0a6e89c7ace51e1cb9e077eea708b962785f05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "app/l10n_util.h"
#include "base/basictypes.h"
#include "base/scoped_ptr.h"
#include "base/stl_util-inl.h"
#include "base/string16.h"
#include "chrome/browser/autofill/autofill_common_unittest.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Tests different possibilities for summary string generation.
// Based on existence of first name, last name, and address line 1.
TEST(AutoFillProfileTest, PreviewSummaryString) {
  // Case 0/null: ""
  AutoFillProfile profile0(string16(), 0);
  string16 summary0 = profile0.PreviewSummary();
  EXPECT_EQ(string16(), summary0);

  // Case 0/empty: ""
  AutoFillProfile profile00(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile00, "Billing", "", "Mitchell", "",
      "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA", "91601", "US",
      "12345678910", "01987654321");
  string16 summary00 = profile00.PreviewSummary();
  EXPECT_EQ(string16(), summary00);

  // Case 1: "<address>"
  AutoFillProfile profile1(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile1, "Billing", "", "Mitchell", "",
      "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
      "91601", "US", "12345678910", "01987654321");
  string16 summary1 = profile1.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("123 Zoo St.")), summary1);

  // Case 2: "<lastname>"
  AutoFillProfile profile2(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile2, "Billing", "", "Mitchell",
      "Morrison", "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA",
      "91601", "US", "12345678910", "01987654321");
  string16 summary2 = profile2.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("Morrison")), summary2);

  // Case 3: "<lastname>, <address>"
  AutoFillProfile profile3(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile3, "Billing", "", "Mitchell",
      "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
      "Hollywood", "CA", "91601", "US", "12345678910", "01987654321");
  string16 summary3 = profile3.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("Morrison, 123 Zoo St.")), summary3);

  // Case 4: "<firstname>"
  AutoFillProfile profile4(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile4, "Billing", "Marion", "Mitchell",
      "", "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA", "91601",
      "US", "12345678910", "01987654321");
  string16 summary4 = profile4.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("Marion")), summary4);

  // Case 5: "<firstname>, <address>"
  AutoFillProfile profile5(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile5, "Billing", "Marion", "Mitchell",
      "", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
      "91601", "US", "12345678910", "01987654321");
  string16 summary5 = profile5.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("Marion, 123 Zoo St.")), summary5);

  // Case 6: "<firstname> <lastname>"
  AutoFillProfile profile6(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile6, "Billing", "Marion", "Mitchell",
      "Morrison", "johnwayne@me.xyz", "Fox", "", "unit 5", "Hollywood", "CA",
      "91601", "US", "12345678910", "01987654321");
  string16 summary6 = profile6.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("Marion Morrison")), summary6);

  // Case 7: "<firstname> <lastname>, <address>"
  AutoFillProfile profile7(string16(), 0);
  autofill_unittest::SetProfileInfo(&profile7, "Billing", "Marion", "Mitchell",
      "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
      "Hollywood", "CA", "91601", "US", "12345678910", "01987654321");
  string16 summary7 = profile7.PreviewSummary();
  EXPECT_EQ(string16(ASCIIToUTF16("Marion Morrison, 123 Zoo St.")), summary7);
}

TEST(AutoFillProfileTest, AdjustInferredLabels) {
  std::vector<AutoFillProfile*> profiles;
  profiles.push_back(new AutoFillProfile(string16(), 0));
  autofill_unittest::SetProfileInfo(
      profiles[0],
      "",
      "John",
      "",
      "Doe",
      "johndoe@hades.com",
      "Underworld",
      "666 Erebus St.",
      "",
      "Elysium", "CA",
      "91111",
      "US",
      "11111111111",
      "22222222222");
  profiles.push_back(new AutoFillProfile(string16(), 0));
  autofill_unittest::SetProfileInfo(
      profiles[1],
      "",
      "Jane",
      "",
      "Doe",
      "janedoe@tertium.com",
      "Pluto Inc.",
      "123 Letha Shore.",
      "",
      "Dis", "CA",
      "91222",
      "US",
      "12345678910",
      "01987654321");
  // As labels are empty they are adjusted the first time.
  EXPECT_TRUE(AutoFillProfile::AdjustInferredLabels(&profiles));
  // No need to adjust them anymore.
  EXPECT_FALSE(AutoFillProfile::AdjustInferredLabels(&profiles));
  EXPECT_EQ(string16(ASCIIToUTF16("John Doe, 666 Erebus St.")),
            profiles[0]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16("Jane Doe, 123 Letha Shore.")),
            profiles[1]->Label());

  profiles.push_back(new AutoFillProfile(string16(), 0));
  autofill_unittest::SetProfileInfo(
      profiles[2],
      "",
      "John",
      "",
      "Doe",
      "johndoe@tertium.com",
      "Underworld",
      "666 Erebus St.",
      "",
      "Elysium", "CA",
      "91111",
      "US",
      "11111111111",
      "22222222222");
  EXPECT_TRUE(AutoFillProfile::AdjustInferredLabels(&profiles));

  // Profile 0 and 2 inferred label now includes an e-mail.
  EXPECT_EQ(string16(
            ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com")),
            profiles[0]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16("Jane Doe, 123 Letha Shore.")),
            profiles[1]->Label());
  EXPECT_EQ(string16(
            ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@tertium.com")),
            profiles[2]->Label());

  delete profiles[2];
  profiles.pop_back();

  profiles.push_back(new AutoFillProfile(string16(), 0));
  autofill_unittest::SetProfileInfo(
      profiles[2],
      "",
      "John",
      "",
      "Doe",
      "johndoe@hades.com",
      "Underworld",
      "666 Erebus St.",
      "",
      "Elysium", "CA",
      "91111",
      "US",
      "11111111111",
      "33333333333");  // Fax is different

  EXPECT_TRUE(AutoFillProfile::AdjustInferredLabels(&profiles));

  // Profile 0 and 2 inferred label now includes a fax number.
  EXPECT_EQ(string16(ASCIIToUTF16(
            "John Doe, 666 Erebus St., fax:#22222222222")),
            profiles[0]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16("Jane Doe, 123 Letha Shore.")),
            profiles[1]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16(
            "John Doe, 666 Erebus St., fax:#33333333333")),
            profiles[2]->Label());

  profiles.push_back(new AutoFillProfile(string16(), 0));
  autofill_unittest::SetProfileInfo(
      profiles[3],
      "",
      "John",
      "",
      "Doe",
      "johndoe@hades.com",
      "Underworld",
      "666 Erebus St.",
      "",
      "Elysium", "CA",
      "91111",
      "US",
      "44444444444",  // Phone is different for some.
      "33333333333");  // Fax is different for some.

  EXPECT_TRUE(AutoFillProfile::AdjustInferredLabels(&profiles));

  EXPECT_EQ(string16(ASCIIToUTF16("John Doe, 666 Erebus St., 11111111111,"
                                  " fax:#22222222222")),
            profiles[0]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16("Jane Doe, 123 Letha Shore.")),
            profiles[1]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16("John Doe, 666 Erebus St., 11111111111,"
                                  " fax:#33333333333")),
            profiles[2]->Label());
  // This one differs from other ones by unique phone, so no need for extra
  // information.
  EXPECT_EQ(string16(ASCIIToUTF16("John Doe, 666 Erebus St., 44444444444")),
            profiles[3]->Label());

  profiles.push_back(new AutoFillProfile(string16(), 0));
  autofill_unittest::SetProfileInfo(
      profiles[4],
      "",
      "John",
      "",
      "Doe",
      "johndoe@styx.com",  // E-Mail is different for some.
      "Underworld",
      "666 Erebus St.",
      "",
      "Elysium", "CA",
      "91111",
      "US",
      "44444444444",  // Phone is different for some.
      "33333333333");  // Fax is different for some.

  EXPECT_TRUE(AutoFillProfile::AdjustInferredLabels(&profiles));

  EXPECT_EQ(string16(ASCIIToUTF16(
      "John Doe, 666 Erebus St., johndoe@hades.com,"
      " 11111111111, fax:#22222222222")),
      profiles[0]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16("Jane Doe, 123 Letha Shore.")),
            profiles[1]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16(
      "John Doe, 666 Erebus St., johndoe@hades.com,"
      " 11111111111, fax:#33333333333")),
      profiles[2]->Label());
  EXPECT_EQ(string16(ASCIIToUTF16(
      "John Doe, 666 Erebus St., johndoe@hades.com,"
      " 44444444444, fax:#33333333333")),
      profiles[3]->Label());
  // This one differs from other ones by unique e-mail, so no need for extra
  // information.
  EXPECT_EQ(string16(ASCIIToUTF16(
      "John Doe, 666 Erebus St., johndoe@styx.com")),
      profiles[4]->Label());

  EXPECT_FALSE(AutoFillProfile::AdjustInferredLabels(&profiles));

  // Clean up.
  STLDeleteContainerPointers(profiles.begin(), profiles.end());
}

TEST(AutoFillProfileTest, IsSubsetOf) {
  scoped_ptr<AutoFillProfile> a, b;

  // |a| is a subset of |b|.
  a.reset(new AutoFillProfile);
  b.reset(new AutoFillProfile);
  autofill_unittest::SetProfileInfo(a.get(), "label1", "Thomas", NULL,
      "Jefferson", "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL);
  autofill_unittest::SetProfileInfo(b.get(), "label2", "Thomas", NULL,
      "Jefferson", "declaration_guy@gmail.com", "United States Government",
      "Monticello", NULL, "Charlottesville", "Virginia", "22902", NULL, NULL,
      NULL);
  EXPECT_TRUE(a->IsSubsetOf(*b));

  // |b| is not a subset of |a|.
  EXPECT_FALSE(b->IsSubsetOf(*a));

  // |a| is a subset of |a|.
  EXPECT_TRUE(a->IsSubsetOf(*a));

  // One field in |b| is different.
  a.reset(new AutoFillProfile);
  b.reset(new AutoFillProfile);
  autofill_unittest::SetProfileInfo(a.get(), "label1", "Thomas", NULL,
      "Jefferson", "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL);
  autofill_unittest::SetProfileInfo(a.get(), "label2", "Thomas", NULL,
      "Adams", "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL);
  EXPECT_FALSE(a->IsSubsetOf(*b));
}

TEST(AutoFillProfileTest, IntersectionOfTypesHasEqualValues) {
  scoped_ptr<AutoFillProfile> a, b;

  // Intersection of types contains the fields NAME_FIRST, NAME_LAST,
  // EMAIL_ADDRESS.  The values of these field types are equal between the two
  // profiles.
  a.reset(new AutoFillProfile);
  b.reset(new AutoFillProfile);
  autofill_unittest::SetProfileInfo(a.get(), "label1", "Thomas", NULL,
      "Jefferson", "declaration_guy@gmail.com", NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, "12134759123", "19384284720");
  autofill_unittest::SetProfileInfo(b.get(), "label2", "Thomas", NULL,
      "Jefferson", "declaration_guy@gmail.com", "United States Government",
      "Monticello", NULL, "Charlottesville", "Virginia", "22902", NULL, NULL,
      NULL);
  EXPECT_TRUE(a->IntersectionOfTypesHasEqualValues(*b));

  // Intersection of types contains the fields NAME_FIRST, NAME_LAST,
  // EMAIL_ADDRESS. The value of EMAIL_ADDRESS differs between the two profiles.
  a.reset(new AutoFillProfile);
  b.reset(new AutoFillProfile);
  autofill_unittest::SetProfileInfo(a.get(), "label1", "Thomas", NULL,
      "Jefferson", "poser@yahoo.com", NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, "12134759123", "19384284720");
  autofill_unittest::SetProfileInfo(b.get(), "label2", "Thomas", NULL,
      "Jefferson", "declaration_guy@gmail.com", "United States Government",
      "Monticello", NULL, "Charlottesville", "Virginia", "22902", NULL, NULL,
      NULL);
  EXPECT_FALSE(a->IntersectionOfTypesHasEqualValues(*b));

  // Intersection of types is empty.
  a.reset(new AutoFillProfile);
  b.reset(new AutoFillProfile);
  autofill_unittest::SetProfileInfo(a.get(), "label1", "Thomas", NULL,
      "Jefferson", "poser@yahoo.com", NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, "12134759123", "19384284720");
  autofill_unittest::SetProfileInfo(b.get(), "label2", NULL, NULL, NULL, NULL,
      "United States Government", "Monticello", NULL, "Charlottesville",
      "Virginia", "22902", NULL, NULL, NULL);
  EXPECT_FALSE(a->IntersectionOfTypesHasEqualValues(*b));
}

TEST(AutoFillProfileTest, MergeWith) {
  scoped_ptr<AutoFillProfile> a, b;

  // Merge |b| into |a|.
  a.reset(new AutoFillProfile);
  b.reset(new AutoFillProfile);
  autofill_unittest::SetProfileInfo(a.get(), "label1", "Jimmy", NULL,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, "12134759123", "19384284720");
  autofill_unittest::SetProfileInfo(b.get(), "label2", "James", NULL,
      "Madison", "constitutionalist@gmail.com", "United States Government",
      "Monticello", NULL, "Charlottesville", "Virginia", "22902", NULL, NULL,
      NULL);
  AutoFillProfile expected_b(*b);
  a->MergeWith(*b);

  AutoFillProfile expected_a;
  autofill_unittest::SetProfileInfo(&expected_a, "label1", "Jimmy", NULL,
      "Madison", "constitutionalist@gmail.com", "United States Government",
      "Monticello", NULL, "Charlottesville", "Virginia", "22902", NULL,
      "12134759123", "19384284720");
  EXPECT_EQ(expected_a, *a);
  EXPECT_EQ(expected_b, *b);
}

}  // namespace