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