1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/format_macros.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/autofill/core/browser/autofill_profile.h"
15 #include "components/autofill/core/browser/autofill_test_utils.h"
16 #include "components/autofill/core/browser/autofill_type.h"
17 #include "components/autofill/core/browser/field_types.h"
18 #include "components/autofill/core/common/form_field_data.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::ASCIIToUTF16
;
22 using base::UTF8ToUTF16
;
28 base::string16
GetLabel(AutofillProfile
* profile
) {
29 std::vector
<AutofillProfile
*> profiles
;
30 profiles
.push_back(profile
);
31 std::vector
<base::string16
> labels
;
32 AutofillProfile::CreateDifferentiatingLabels(profiles
, "en-US", &labels
);
36 // Holds the autofill profile |first|, |middle| and |last| names.
38 NameParts(const std::string
& first
,
39 const std::string
& middle
,
40 const std::string
& last
)
41 : first(first
), middle(middle
), last(last
) {}
48 // Test case to be executed to validate OverwriteOrAppendNames.
50 TestCase(const NameParts
& starting_name
,
51 const NameParts
& additional_name
,
52 const NameParts
& expected_result
)
53 : starting_names(std::vector
<NameParts
>(1, starting_name
)),
54 additional_names(std::vector
<NameParts
>(1, additional_name
)),
55 expected_result(std::vector
<NameParts
>(1, expected_result
)) {}
57 TestCase(const std::vector
<NameParts
>& starting_names
,
58 const std::vector
<NameParts
>& additional_names
,
59 const std::vector
<NameParts
>& expected_result
)
60 : starting_names(starting_names
),
61 additional_names(additional_names
),
62 expected_result(expected_result
) {}
64 std::vector
<NameParts
> starting_names
;
65 std::vector
<NameParts
> additional_names
;
66 std::vector
<NameParts
> expected_result
;
69 // Populates |first_names|, |middle_names| and |last_names| from the list of
70 // NameParts from |starting_names|, |additional_names| or |expected_result|
71 // from the testcase to create and verify the autofill profile.
72 void GetNamePartsList(const std::vector
<NameParts
>& names
,
73 std::vector
<base::string16
>* first_names
,
74 std::vector
<base::string16
>* middle_names
,
75 std::vector
<base::string16
>* last_names
) {
76 for (size_t i
= 0; i
< names
.size(); ++i
) {
77 first_names
->push_back(ASCIIToUTF16(names
[i
].first
));
78 middle_names
->push_back(ASCIIToUTF16(names
[i
].middle
));
79 last_names
->push_back(ASCIIToUTF16(names
[i
].last
));
85 // Tests different possibilities for summary string generation.
86 // Based on existence of first name, last name, and address line 1.
87 TEST(AutofillProfileTest
, PreviewSummaryString
) {
89 AutofillProfile
profile0(base::GenerateGUID(), "https://www.example.com/");
90 // Empty profile - nothing to update.
91 base::string16 summary0
= GetLabel(&profile0
);
92 EXPECT_EQ(base::string16(), summary0
);
94 // Case 0a/empty name and address, so the first two fields of the rest of the
95 // data is used: "Hollywood, CA"
96 AutofillProfile
profile00(base::GenerateGUID(), "https://www.example.com/");
97 test::SetProfileInfo(&profile00
, "", "", "",
98 "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", "91601", "US",
100 base::string16 summary00
= GetLabel(&profile00
);
101 EXPECT_EQ(ASCIIToUTF16("Hollywood, CA"), summary00
);
103 // Case 1: "<address>" without line 2.
104 AutofillProfile
profile1(base::GenerateGUID(), "https://www.example.com/");
105 test::SetProfileInfo(&profile1
, "", "", "",
106 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "", "Hollywood", "CA",
107 "91601", "US", "16505678910");
108 base::string16 summary1
= GetLabel(&profile1
);
109 EXPECT_EQ(ASCIIToUTF16("123 Zoo St., Hollywood"), summary1
);
111 // Case 1a: "<address>" with line 2.
112 AutofillProfile
profile1a(base::GenerateGUID(), "https://www.example.com/");
113 test::SetProfileInfo(&profile1a
, "", "", "",
114 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
115 "91601", "US", "16505678910");
116 base::string16 summary1a
= GetLabel(&profile1a
);
117 EXPECT_EQ(ASCIIToUTF16("123 Zoo St., unit 5"), summary1a
);
119 // Case 2: "<lastname>"
120 AutofillProfile
profile2(base::GenerateGUID(), "https://www.example.com/");
121 test::SetProfileInfo(&profile2
, "", "Mitchell",
122 "Morrison", "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA",
123 "91601", "US", "16505678910");
124 base::string16 summary2
= GetLabel(&profile2
);
125 // Summary includes full name, to the maximal extent available.
126 EXPECT_EQ(ASCIIToUTF16("Mitchell Morrison, Hollywood"), summary2
);
128 // Case 3: "<lastname>, <address>"
129 AutofillProfile
profile3(base::GenerateGUID(), "https://www.example.com/");
130 test::SetProfileInfo(&profile3
, "", "Mitchell",
131 "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "",
132 "Hollywood", "CA", "91601", "US", "16505678910");
133 base::string16 summary3
= GetLabel(&profile3
);
134 EXPECT_EQ(ASCIIToUTF16("Mitchell Morrison, 123 Zoo St."), summary3
);
136 // Case 4: "<firstname>"
137 AutofillProfile
profile4(base::GenerateGUID(), "https://www.example.com/");
138 test::SetProfileInfo(&profile4
, "Marion", "Mitchell", "",
139 "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA", "91601", "US",
141 base::string16 summary4
= GetLabel(&profile4
);
142 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, Hollywood"), summary4
);
144 // Case 5: "<firstname>, <address>"
145 AutofillProfile
profile5(base::GenerateGUID(), "https://www.example.com/");
146 test::SetProfileInfo(&profile5
, "Marion", "Mitchell", "",
147 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
148 "91601", "US", "16505678910");
149 base::string16 summary5
= GetLabel(&profile5
);
150 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell, 123 Zoo St."), summary5
);
152 // Case 6: "<firstname> <lastname>"
153 AutofillProfile
profile6(base::GenerateGUID(), "https://www.example.com/");
154 test::SetProfileInfo(&profile6
, "Marion", "Mitchell",
155 "Morrison", "johnwayne@me.xyz", "Fox", "", "", "Hollywood", "CA",
156 "91601", "US", "16505678910");
157 base::string16 summary6
= GetLabel(&profile6
);
158 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, Hollywood"),
161 // Case 7: "<firstname> <lastname>, <address>"
162 AutofillProfile
profile7(base::GenerateGUID(), "https://www.example.com/");
163 test::SetProfileInfo(&profile7
, "Marion", "Mitchell",
164 "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
165 "Hollywood", "CA", "91601", "US", "16505678910");
166 base::string16 summary7
= GetLabel(&profile7
);
167 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison, 123 Zoo St."), summary7
);
169 // Case 7a: "<firstname> <lastname>, <address>" - same as #7, except for
171 AutofillProfile
profile7a(base::GenerateGUID(), "https://www.example.com/");
172 test::SetProfileInfo(&profile7a
, "Marion", "Mitchell",
173 "Morrison", "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
174 "Hollywood", "CA", "91601", "US", "16505678910");
175 std::vector
<AutofillProfile
*> profiles
;
176 profiles
.push_back(&profile7
);
177 profiles
.push_back(&profile7a
);
178 std::vector
<base::string16
> labels
;
179 AutofillProfile::CreateDifferentiatingLabels(profiles
, "en-US", &labels
);
180 ASSERT_EQ(profiles
.size(), labels
.size());
181 summary7
= labels
[0];
182 base::string16 summary7a
= labels
[1];
183 EXPECT_EQ(ASCIIToUTF16(
184 "Marion Mitchell Morrison, 123 Zoo St., johnwayne@me.xyz"), summary7
);
185 EXPECT_EQ(ASCIIToUTF16(
186 "Marion Mitchell Morrison, 123 Zoo St., marion@me.xyz"), summary7a
);
189 TEST(AutofillProfileTest
, AdjustInferredLabels
) {
190 ScopedVector
<AutofillProfile
> profiles
;
192 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
193 test::SetProfileInfo(
207 new AutofillProfile(base::GenerateGUID(), "http://www.example.com/"));
208 test::SetProfileInfo(
213 "janedoe@tertium.com",
221 std::vector
<base::string16
> labels
;
222 AutofillProfile::CreateDifferentiatingLabels(
223 profiles
.get(), "en-US", &labels
);
224 ASSERT_EQ(2U, labels
.size());
225 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels
[0]);
226 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels
[1]);
229 new AutofillProfile(base::GenerateGUID(), "Chrome settings"));
230 test::SetProfileInfo(
235 "johndoe@tertium.com",
244 AutofillProfile::CreateDifferentiatingLabels(
245 profiles
.get(), "en-US", &labels
);
247 // Profile 0 and 2 inferred label now includes an e-mail.
248 ASSERT_EQ(3U, labels
.size());
249 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@hades.com"),
251 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels
[1]);
252 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., johndoe@tertium.com"),
258 new AutofillProfile(base::GenerateGUID(), std::string()));
259 test::SetProfileInfo(
268 "Elysium", "CO", // State is different
274 AutofillProfile::CreateDifferentiatingLabels(
275 profiles
.get(), "en-US", &labels
);
277 // Profile 0 and 2 inferred label now includes a state.
278 ASSERT_EQ(3U, labels
.size());
279 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels
[0]);
280 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels
[1]);
281 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO"), labels
[2]);
284 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
285 test::SetProfileInfo(
294 "Elysium", "CO", // State is different for some.
297 "16504444444"); // Phone is different for some.
300 AutofillProfile::CreateDifferentiatingLabels(
301 profiles
.get(), "en-US", &labels
);
302 ASSERT_EQ(4U, labels
.size());
303 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels
[0]);
304 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels
[1]);
305 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, 16502111111"),
307 // This one differs from other ones by unique phone, so no need for extra
309 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, 16504444444"),
313 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
314 test::SetProfileInfo(
319 "johndoe@styx.com", // E-Mail is different for some.
323 "Elysium", "CO", // State is different for some.
326 "16504444444"); // Phone is different for some.
329 AutofillProfile::CreateDifferentiatingLabels(
330 profiles
.get(), "en-US", &labels
);
331 ASSERT_EQ(5U, labels
.size());
332 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CA"), labels
[0]);
333 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels
[1]);
334 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@hades.com,"
335 " 16502111111"), labels
[2]);
336 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@hades.com,"
337 " 16504444444"), labels
[3]);
338 // This one differs from other ones by unique e-mail, so no need for extra
340 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., CO, johndoe@styx.com"),
344 TEST(AutofillProfileTest
, CreateInferredLabelsI18n_CH
) {
345 ScopedVector
<AutofillProfile
> profiles
;
347 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
348 test::SetProfileInfo(profiles
.back(),
352 "hrgiger@beispiel.com",
354 "Brandschenkestrasse 110",
360 profiles
.back()->set_language_code("de_CH");
361 static const char* kExpectedLabels
[] = {
364 "H. R. Giger, Brandschenkestrasse 110",
365 "H. R. Giger, Brandschenkestrasse 110, Zurich",
366 "H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich",
367 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich",
368 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich, "
370 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich, "
371 "Switzerland, hrgiger@beispiel.com",
372 "Beispiel Inc, H. R. Giger, Brandschenkestrasse 110, CH-8002 Zurich, "
373 "Switzerland, hrgiger@beispiel.com, +41446681800",
376 std::vector
<base::string16
> labels
;
377 for (size_t i
= 0; i
< arraysize(kExpectedLabels
); ++i
) {
378 AutofillProfile::CreateInferredLabels(
379 profiles
.get(), NULL
, UNKNOWN_TYPE
, i
, "en-US", &labels
);
380 ASSERT_FALSE(labels
.empty());
381 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels
[i
]), labels
.back());
386 TEST(AutofillProfileTest
, CreateInferredLabelsI18n_FR
) {
387 ScopedVector
<AutofillProfile
> profiles
;
389 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
390 test::SetProfileInfo(profiles
.back(),
394 "antoine@exemple.com",
401 "+33 (0) 1 42 68 53 00");
402 profiles
.back()->set_language_code("fr_FR");
403 profiles
.back()->SetInfo(
404 AutofillType(ADDRESS_HOME_SORTING_CODE
), UTF8ToUTF16("CEDEX"), "en-US");
405 static const char* kExpectedLabels
[] = {
407 "Antoine de Saint-Exupéry",
408 "Antoine de Saint-Exupéry, 8 Rue de Londres",
409 "Antoine de Saint-Exupéry, 8 Rue de Londres, Paris",
410 "Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris",
411 "Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris CEDEX",
412 "Exemple Inc, Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris "
414 "Exemple Inc, Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris "
416 "Exemple Inc, Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris "
417 "CEDEX, France, antoine@exemple.com",
418 "Exemple Inc, Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris "
419 "CEDEX, France, antoine@exemple.com, +33142685300",
420 "Exemple Inc, Antoine de Saint-Exupéry, 8 Rue de Londres, 75009 Paris "
421 "CEDEX, France, antoine@exemple.com, +33142685300",
424 std::vector
<base::string16
> labels
;
425 for (size_t i
= 0; i
< arraysize(kExpectedLabels
); ++i
) {
426 AutofillProfile::CreateInferredLabels(
427 profiles
.get(), NULL
, UNKNOWN_TYPE
, i
, "en-US", &labels
);
428 ASSERT_FALSE(labels
.empty());
429 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels
[i
]), labels
.back());
433 TEST(AutofillProfileTest
, CreateInferredLabelsI18n_KR
) {
434 ScopedVector
<AutofillProfile
> profiles
;
436 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
437 test::SetProfileInfo(profiles
.back(),
443 "Gangnam Finance Center",
445 "Gangnam-Gu", "Seoul",
449 profiles
.back()->set_language_code("ko_Latn");
450 profiles
.back()->SetInfo(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY
),
451 UTF8ToUTF16("Yeoksam-Dong"),
453 static const char* kExpectedLabels
[] = {
456 "Park Jae-sang, Gangnam Finance Center",
457 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro",
458 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong",
459 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong, "
461 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong, "
463 "Park Jae-sang, Gangnam Finance Center, 152 Teheran-ro, Yeoksam-Dong, "
464 "Gangnam-Gu, Seoul, 135-984",
465 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, "
466 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984",
467 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, "
468 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984, South Korea",
469 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, "
470 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984, South Korea, "
472 "Park Jae-sang, Yeleul Inc, Gangnam Finance Center, 152 Teheran-ro, "
473 "Yeoksam-Dong, Gangnam-Gu, Seoul, 135-984, South Korea, "
474 "park@yeleul.com, +8225319000",
477 std::vector
<base::string16
> labels
;
478 for (size_t i
= 0; i
< arraysize(kExpectedLabels
); ++i
) {
479 AutofillProfile::CreateInferredLabels(
480 profiles
.get(), NULL
, UNKNOWN_TYPE
, i
, "en-US", &labels
);
481 ASSERT_FALSE(labels
.empty());
482 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels
[i
]), labels
.back());
486 TEST(AutofillProfileTest
, CreateInferredLabelsI18n_JP_Latn
) {
487 ScopedVector
<AutofillProfile
> profiles
;
489 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
490 test::SetProfileInfo(profiles
.back(),
496 "Roppongi Hills Mori Tower",
498 "Minato-ku", "Tokyo",
502 profiles
.back()->set_language_code("ja_Latn");
503 static const char* kExpectedLabels
[] = {
506 "Miku Hatsune, Roppongi Hills Mori Tower",
507 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi",
508 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi, Minato-ku",
509 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi, Minato-ku, "
511 "Miku Hatsune, Roppongi Hills Mori Tower, 6-10-1 Roppongi, Minato-ku, "
513 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, "
514 "Minato-ku, Tokyo, 106-6126",
515 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, "
516 "Minato-ku, Tokyo, 106-6126, Japan",
517 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, "
518 "Minato-ku, Tokyo, 106-6126, Japan, miku@rei.com",
519 "Miku Hatsune, Rei Inc, Roppongi Hills Mori Tower, 6-10-1 Roppongi, "
520 "Minato-ku, Tokyo, 106-6126, Japan, miku@rei.com, +81363849000",
523 std::vector
<base::string16
> labels
;
524 for (size_t i
= 0; i
< arraysize(kExpectedLabels
); ++i
) {
525 AutofillProfile::CreateInferredLabels(
526 profiles
.get(), NULL
, UNKNOWN_TYPE
, i
, "en-US", &labels
);
527 ASSERT_FALSE(labels
.empty());
528 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels
[i
]), labels
.back());
532 TEST(AutofillProfileTest
, CreateInferredLabelsI18n_JP_ja
) {
533 ScopedVector
<AutofillProfile
> profiles
;
535 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
536 test::SetProfileInfo(profiles
.back(),
548 profiles
.back()->set_language_code("ja_JP");
549 static const char* kExpectedLabels
[] = {
553 "六本木ヒルズ森タワー六本木 6-10-1ミク 初音",
554 "港区六本木ヒルズ森タワー六本木 6-10-1ミク 初音",
555 "東京都港区六本木ヒルズ森タワー六本木 6-10-1ミク 初音",
556 "〒106-6126東京都港区六本木ヒルズ森タワー六本木 6-10-1ミク 初音",
557 "〒106-6126東京都港区六本木ヒルズ森タワー六本木 6-10-1例ミク 初音",
558 "〒106-6126東京都港区六本木ヒルズ森タワー六本木 6-10-1例ミク 初音, Japan",
559 "〒106-6126東京都港区六本木ヒルズ森タワー六本木 6-10-1例ミク 初音, Japan, "
561 "〒106-6126東京都港区六本木ヒルズ森タワー六本木 6-10-1例ミク 初音, Japan, "
562 "miku@rei.com, 0363849000",
565 std::vector
<base::string16
> labels
;
566 for (size_t i
= 0; i
< arraysize(kExpectedLabels
); ++i
) {
567 AutofillProfile::CreateInferredLabels(
568 profiles
.get(), NULL
, UNKNOWN_TYPE
, i
, "en-US", &labels
);
569 ASSERT_FALSE(labels
.empty());
570 EXPECT_EQ(UTF8ToUTF16(kExpectedLabels
[i
]), labels
.back());
574 TEST(AutofillProfileTest
, CreateInferredLabels
) {
575 ScopedVector
<AutofillProfile
> profiles
;
577 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
578 test::SetProfileInfo(profiles
[0],
591 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
592 test::SetProfileInfo(profiles
[1],
596 "janedoe@tertium.com",
604 std::vector
<base::string16
> labels
;
605 // Two fields at least - no filter.
606 AutofillProfile::CreateInferredLabels(profiles
.get(), NULL
, UNKNOWN_TYPE
, 2,
608 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St."), labels
[0]);
609 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore."), labels
[1]);
611 // Three fields at least - no filter.
612 AutofillProfile::CreateInferredLabels(profiles
.get(), NULL
, UNKNOWN_TYPE
, 3,
614 EXPECT_EQ(ASCIIToUTF16("John Doe, 666 Erebus St., Elysium"),
616 EXPECT_EQ(ASCIIToUTF16("Jane Doe, 123 Letha Shore., Dis"),
619 std::vector
<ServerFieldType
> suggested_fields
;
620 suggested_fields
.push_back(ADDRESS_HOME_CITY
);
621 suggested_fields
.push_back(ADDRESS_HOME_STATE
);
622 suggested_fields
.push_back(ADDRESS_HOME_ZIP
);
624 // Two fields at least, from suggested fields - no filter.
625 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
626 UNKNOWN_TYPE
, 2, "en-US", &labels
);
627 EXPECT_EQ(ASCIIToUTF16("Elysium, CA"), labels
[0]);
628 EXPECT_EQ(ASCIIToUTF16("Dis, CA"), labels
[1]);
630 // Three fields at least, from suggested fields - no filter.
631 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
632 UNKNOWN_TYPE
, 3, "en-US", &labels
);
633 EXPECT_EQ(ASCIIToUTF16("Elysium, CA 91111"), labels
[0]);
634 EXPECT_EQ(ASCIIToUTF16("Dis, CA 91222"), labels
[1]);
636 // Three fields at least, from suggested fields - but filter reduces available
638 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
639 ADDRESS_HOME_STATE
, 3, "en-US",
641 EXPECT_EQ(ASCIIToUTF16("Elysium 91111"), labels
[0]);
642 EXPECT_EQ(ASCIIToUTF16("Dis 91222"), labels
[1]);
644 suggested_fields
.clear();
645 // In our implementation we always display NAME_FULL for all NAME* fields...
646 suggested_fields
.push_back(NAME_MIDDLE
);
647 // One field at least, from suggested fields - no filter.
648 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
649 UNKNOWN_TYPE
, 1, "en-US", &labels
);
650 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels
[0]);
651 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels
[1]);
653 // One field at least, from suggested fields - filter the same as suggested
655 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
656 NAME_MIDDLE
, 1, "en-US", &labels
);
657 EXPECT_EQ(base::string16(), labels
[0]);
658 EXPECT_EQ(base::string16(), labels
[1]);
660 suggested_fields
.clear();
661 // In our implementation we always display NAME_FULL for NAME_MIDDLE_INITIAL
662 suggested_fields
.push_back(NAME_MIDDLE_INITIAL
);
663 // One field at least, from suggested fields - no filter.
664 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
665 UNKNOWN_TYPE
, 1, "en-US", &labels
);
666 EXPECT_EQ(ASCIIToUTF16("John Doe"), labels
[0]);
667 EXPECT_EQ(ASCIIToUTF16("Jane Doe"), labels
[1]);
669 // One field at least, from suggested fields - filter same as the first non-
670 // unknown suggested field.
671 suggested_fields
.clear();
672 suggested_fields
.push_back(UNKNOWN_TYPE
);
673 suggested_fields
.push_back(NAME_FULL
);
674 suggested_fields
.push_back(ADDRESS_HOME_LINE1
);
675 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
676 NAME_FULL
, 1, "en-US", &labels
);
677 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels
[0]);
678 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels
[1]);
680 // No suggested fields, but non-unknown excluded field.
681 AutofillProfile::CreateInferredLabels(profiles
.get(), NULL
,
682 NAME_FULL
, 1, "en-US", &labels
);
683 EXPECT_EQ(base::string16(ASCIIToUTF16("666 Erebus St.")), labels
[0]);
684 EXPECT_EQ(base::string16(ASCIIToUTF16("123 Letha Shore.")), labels
[1]);
687 // Test that we fall back to using the full name if there are no other
688 // distinguishing fields, but only if it makes sense given the suggested fields.
689 TEST(AutofillProfileTest
, CreateInferredLabelsFallsBackToFullName
) {
690 ScopedVector
<AutofillProfile
> profiles
;
692 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
693 test::SetProfileInfo(profiles
[0],
694 "John", "", "Doe", "doe@example.com", "",
695 "88 Nowhere Ave.", "", "", "", "", "", "");
697 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
698 test::SetProfileInfo(profiles
[1],
699 "Johnny", "K", "Doe", "doe@example.com", "",
700 "88 Nowhere Ave.", "", "", "", "", "", "");
702 // If the only name field in the suggested fields is the excluded field, we
703 // should not fall back to the full name as a distinguishing field.
704 std::vector
<ServerFieldType
> suggested_fields
;
705 suggested_fields
.push_back(NAME_LAST
);
706 suggested_fields
.push_back(ADDRESS_HOME_LINE1
);
707 suggested_fields
.push_back(EMAIL_ADDRESS
);
708 std::vector
<base::string16
> labels
;
709 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
710 NAME_LAST
, 1, "en-US", &labels
);
711 ASSERT_EQ(2U, labels
.size());
712 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels
[0]);
713 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave."), labels
[1]);
715 // Otherwise, we should.
716 suggested_fields
.push_back(NAME_FIRST
);
717 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
718 NAME_LAST
, 1, "en-US", &labels
);
719 ASSERT_EQ(2U, labels
.size());
720 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., John Doe"), labels
[0]);
721 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Johnny K Doe"), labels
[1]);
724 // Test that we do not show duplicate fields in the labels.
725 TEST(AutofillProfileTest
, CreateInferredLabelsNoDuplicatedFields
) {
726 ScopedVector
<AutofillProfile
> profiles
;
728 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
729 test::SetProfileInfo(profiles
[0],
730 "John", "", "Doe", "doe@example.com", "",
731 "88 Nowhere Ave.", "", "", "", "", "", "");
733 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
734 test::SetProfileInfo(profiles
[1],
735 "John", "", "Doe", "dojo@example.com", "",
736 "88 Nowhere Ave.", "", "", "", "", "", "");
738 // If the only name field in the suggested fields is the excluded field, we
739 // should not fall back to the full name as a distinguishing field.
740 std::vector
<ServerFieldType
> suggested_fields
;
741 suggested_fields
.push_back(ADDRESS_HOME_LINE1
);
742 suggested_fields
.push_back(ADDRESS_BILLING_LINE1
);
743 suggested_fields
.push_back(EMAIL_ADDRESS
);
744 std::vector
<base::string16
> labels
;
745 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
746 UNKNOWN_TYPE
, 2, "en-US", &labels
);
747 ASSERT_EQ(2U, labels
.size());
748 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., doe@example.com"), labels
[0]);
749 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., dojo@example.com"), labels
[1]);
752 // Make sure that empty fields are not treated as distinguishing fields.
753 TEST(AutofillProfileTest
, CreateInferredLabelsSkipsEmptyFields
) {
754 ScopedVector
<AutofillProfile
> profiles
;
756 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
757 test::SetProfileInfo(profiles
[0],
758 "John", "", "Doe", "doe@example.com",
759 "Gogole", "", "", "", "", "", "", "");
761 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
762 test::SetProfileInfo(profiles
[1],
763 "John", "", "Doe", "doe@example.com",
764 "Ggoole", "", "", "", "", "", "", "");
766 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
767 test::SetProfileInfo(profiles
[2],
768 "John", "", "Doe", "john.doe@example.com",
769 "Goolge", "", "", "", "", "", "", "");
771 std::vector
<base::string16
> labels
;
772 AutofillProfile::CreateInferredLabels(profiles
.get(), NULL
, UNKNOWN_TYPE
, 3,
774 ASSERT_EQ(3U, labels
.size());
775 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels
[0]);
776 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Ggoole"), labels
[1]);
777 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com, Goolge"), labels
[2]);
779 // A field must have a non-empty value for each profile to be considered a
780 // distinguishing field.
781 profiles
[1]->SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("88 Nowhere Ave."));
782 AutofillProfile::CreateInferredLabels(profiles
.get(), NULL
, UNKNOWN_TYPE
, 1,
784 ASSERT_EQ(3U, labels
.size());
785 EXPECT_EQ(ASCIIToUTF16("John Doe, doe@example.com, Gogole"), labels
[0]);
786 EXPECT_EQ(ASCIIToUTF16("John Doe, 88 Nowhere Ave., doe@example.com, Ggoole"),
787 labels
[1]) << labels
[1];
788 EXPECT_EQ(ASCIIToUTF16("John Doe, john.doe@example.com"), labels
[2]);
791 // Test that labels that would otherwise have multiline values are flattened.
792 TEST(AutofillProfileTest
, CreateInferredLabelsFlattensMultiLineValues
) {
793 ScopedVector
<AutofillProfile
> profiles
;
795 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
796 test::SetProfileInfo(profiles
[0],
797 "John", "", "Doe", "doe@example.com", "",
798 "88 Nowhere Ave.", "Apt. 42", "", "", "", "", "");
800 // If the only name field in the suggested fields is the excluded field, we
801 // should not fall back to the full name as a distinguishing field.
802 std::vector
<ServerFieldType
> suggested_fields
;
803 suggested_fields
.push_back(NAME_FULL
);
804 suggested_fields
.push_back(ADDRESS_HOME_STREET_ADDRESS
);
805 std::vector
<base::string16
> labels
;
806 AutofillProfile::CreateInferredLabels(profiles
.get(), &suggested_fields
,
807 NAME_FULL
, 1, "en-US", &labels
);
808 ASSERT_EQ(1U, labels
.size());
809 EXPECT_EQ(ASCIIToUTF16("88 Nowhere Ave., Apt. 42"), labels
[0]);
812 TEST(AutofillProfileTest
, IsSubsetOf
) {
813 scoped_ptr
<AutofillProfile
> a
, b
;
815 // |a| is a subset of |b|.
817 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
819 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
820 test::SetProfileInfo(a
.get(), "Thomas", NULL
, "Jefferson",
821 "declaration_guy@gmail.com", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
823 test::SetProfileInfo(b
.get(), "Thomas", NULL
, "Jefferson",
824 "declaration_guy@gmail.com", "United States Government", "Monticello",
825 NULL
, "Charlottesville", "Virginia", "22902", NULL
, NULL
);
826 EXPECT_TRUE(a
->IsSubsetOf(*b
, "en-US"));
828 // |b| is not a subset of |a|.
829 EXPECT_FALSE(b
->IsSubsetOf(*a
, "en-US"));
831 // |a| is a subset of |a|.
832 EXPECT_TRUE(a
->IsSubsetOf(*a
, "en-US"));
834 // One field in |b| is different.
836 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
838 new AutofillProfile(base::GenerateGUID(), "https://www.example.com/"));
839 test::SetProfileInfo(a
.get(), "Thomas", NULL
, "Jefferson",
840 "declaration_guy@gmail.com", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
842 test::SetProfileInfo(a
.get(), "Thomas", NULL
, "Adams",
843 "declaration_guy@gmail.com", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
845 EXPECT_FALSE(a
->IsSubsetOf(*b
, "en-US"));
848 TEST(AutofillProfileTest
, OverwriteWithOrAddTo
) {
849 AutofillProfile
a(base::GenerateGUID(), "https://www.example.com");
850 test::SetProfileInfo(&a
, "Marion", "Mitchell", "Morrison",
851 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
852 "Hollywood", "CA", "91601", "US",
854 std::vector
<base::string16
> first_names
;
855 a
.GetRawMultiInfo(NAME_FIRST
, &first_names
);
856 first_names
.push_back(ASCIIToUTF16("Marion"));
857 a
.SetRawMultiInfo(NAME_FIRST
, first_names
);
859 std::vector
<base::string16
> last_names
;
860 a
.GetRawMultiInfo(NAME_LAST
, &last_names
);
861 last_names
[last_names
.size() - 1] = ASCIIToUTF16("Morrison");
862 a
.SetRawMultiInfo(NAME_LAST
, last_names
);
864 // Create an identical profile except that the new profile:
865 // (1) Has a different origin,
866 // (2) Has a different address line 2,
867 // (3) Lacks a company name,
868 // (4) Has a different full name variant, and
869 // (5) Has a language code.
870 AutofillProfile b
= a
;
871 b
.set_guid(base::GenerateGUID());
872 b
.set_origin("Chrome settings");
873 b
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("area 51"));
874 b
.SetRawInfo(COMPANY_NAME
, base::string16());
876 std::vector
<base::string16
> names
;
877 b
.GetMultiInfo(AutofillType(NAME_FULL
), "en-US", &names
);
878 names
.push_back(ASCIIToUTF16("Marion M. Morrison"));
879 b
.SetRawMultiInfo(NAME_FULL
, names
);
880 b
.set_language_code("en");
882 a
.OverwriteWithOrAddTo(b
, "en-US");
883 EXPECT_EQ("Chrome settings", a
.origin());
884 EXPECT_EQ(ASCIIToUTF16("area 51"), a
.GetRawInfo(ADDRESS_HOME_LINE2
));
885 EXPECT_EQ(ASCIIToUTF16("Fox"), a
.GetRawInfo(COMPANY_NAME
));
886 a
.GetMultiInfo(AutofillType(NAME_FULL
), "en-US", &names
);
887 ASSERT_EQ(3U, names
.size());
888 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison"), names
[0]);
889 EXPECT_EQ(ASCIIToUTF16("Marion Morrison"), names
[1]);
890 EXPECT_EQ(ASCIIToUTF16("Marion M. Morrison"), names
[2]);
891 EXPECT_EQ("en", a
.language_code());
894 TEST(AutofillProfileTest
, AssignmentOperator
) {
895 AutofillProfile
a(base::GenerateGUID(), "https://www.example.com/");
896 test::SetProfileInfo(&a
, "Marion", "Mitchell", "Morrison",
897 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
898 "Hollywood", "CA", "91601", "US",
901 // Result of assignment should be logically equal to the original profile.
902 AutofillProfile
b(base::GenerateGUID(), "http://www.example.com/");
906 // Assignment to self should not change the profile value.
911 TEST(AutofillProfileTest
, SetMultiInfo
) {
912 std::vector
<base::string16
> full_names
;
913 full_names
.push_back(ASCIIToUTF16("John Davis"));
914 full_names
.push_back(ASCIIToUTF16("Elouise Davis"));
916 p
.SetMultiInfo(AutofillType(NAME_FULL
), full_names
, "en-US");
918 std::vector
<base::string16
> first_names
;
919 p
.GetMultiInfo(AutofillType(NAME_FIRST
), "en-US", &first_names
);
920 ASSERT_EQ(2U, first_names
.size());
921 EXPECT_EQ(ASCIIToUTF16("John"), first_names
[0]);
922 EXPECT_EQ(ASCIIToUTF16("Elouise"), first_names
[1]);
924 std::vector
<base::string16
> last_names
;
925 p
.GetMultiInfo(AutofillType(NAME_LAST
), "en-US", &last_names
);
926 ASSERT_EQ(2U, last_names
.size());
927 EXPECT_EQ(ASCIIToUTF16("Davis"), last_names
[0]);
928 EXPECT_EQ(ASCIIToUTF16("Davis"), last_names
[1]);
931 TEST(AutofillProfileTest
, Copy
) {
932 AutofillProfile
a(base::GenerateGUID(), "https://www.example.com/");
933 test::SetProfileInfo(&a
, "Marion", "Mitchell", "Morrison",
934 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
935 "Hollywood", "CA", "91601", "US",
938 // Clone should be logically equal to the original.
939 AutofillProfile
b(a
);
943 TEST(AutofillProfileTest
, Compare
) {
944 AutofillProfile
a(base::GenerateGUID(), std::string());
945 AutofillProfile
b(base::GenerateGUID(), std::string());
947 // Empty profiles are the same.
948 EXPECT_EQ(0, a
.Compare(b
));
950 // GUIDs don't count.
951 a
.set_guid(base::GenerateGUID());
952 b
.set_guid(base::GenerateGUID());
953 EXPECT_EQ(0, a
.Compare(b
));
955 // Origins don't count.
956 a
.set_origin("apple");
957 b
.set_origin("banana");
958 EXPECT_EQ(0, a
.Compare(b
));
960 // Different values produce non-zero results.
961 test::SetProfileInfo(&a
, "Jimmy", NULL
, NULL
, NULL
,
962 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
963 test::SetProfileInfo(&b
, "Ringo", NULL
, NULL
, NULL
,
964 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
965 EXPECT_GT(0, a
.Compare(b
));
966 EXPECT_LT(0, b
.Compare(a
));
968 // Phone numbers are compared by the full number, including the area code.
969 // This is a regression test for http://crbug.com/163024
970 test::SetProfileInfo(&a
, NULL
, NULL
, NULL
, NULL
,
971 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "650.555.4321");
972 test::SetProfileInfo(&b
, NULL
, NULL
, NULL
, NULL
,
973 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "408.555.4321");
974 EXPECT_GT(0, a
.Compare(b
));
975 EXPECT_LT(0, b
.Compare(a
));
977 // Addresses are compared in full. Regression test for http://crbug.com/375545
978 test::SetProfileInfo(&a
, "John", NULL
, NULL
, NULL
,
979 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
980 a
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
,
981 ASCIIToUTF16("line one\nline two"));
982 test::SetProfileInfo(&b
, "John", NULL
, NULL
, NULL
,
983 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
984 b
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
,
985 ASCIIToUTF16("line one\nline two\nline three"));
986 EXPECT_GT(0, a
.Compare(b
));
987 EXPECT_LT(0, b
.Compare(a
));
990 TEST(AutofillProfileTest
, MultiValueNames
) {
991 AutofillProfile
p(base::GenerateGUID(), "https://www.example.com/");
992 const base::string16
kJohnDoe(ASCIIToUTF16("John Doe"));
993 const base::string16
kJohnPDoe(ASCIIToUTF16("John P. Doe"));
994 std::vector
<base::string16
> set_values
;
995 set_values
.push_back(kJohnDoe
);
996 set_values
.push_back(kJohnPDoe
);
997 p
.SetRawMultiInfo(NAME_FULL
, set_values
);
999 // Expect regular |GetInfo| returns the first element.
1000 EXPECT_EQ(kJohnDoe
, p
.GetRawInfo(NAME_FULL
));
1002 // Ensure that we get out what we put in.
1003 std::vector
<base::string16
> get_values
;
1004 p
.GetRawMultiInfo(NAME_FULL
, &get_values
);
1005 ASSERT_EQ(2UL, get_values
.size());
1006 EXPECT_EQ(kJohnDoe
, get_values
[0]);
1007 EXPECT_EQ(kJohnPDoe
, get_values
[1]);
1009 // Update the values.
1010 AutofillProfile p2
= p
;
1011 EXPECT_EQ(0, p
.Compare(p2
));
1012 const base::string16
kNoOne(ASCIIToUTF16("No One"));
1013 set_values
[1] = kNoOne
;
1014 p
.SetRawMultiInfo(NAME_FULL
, set_values
);
1015 p
.GetRawMultiInfo(NAME_FULL
, &get_values
);
1016 ASSERT_EQ(2UL, get_values
.size());
1017 EXPECT_EQ(kJohnDoe
, get_values
[0]);
1018 EXPECT_EQ(kNoOne
, get_values
[1]);
1019 EXPECT_NE(0, p
.Compare(p2
));
1023 p
.SetRawMultiInfo(NAME_FULL
, set_values
);
1024 p
.GetRawMultiInfo(NAME_FULL
, &get_values
);
1025 ASSERT_EQ(1UL, get_values
.size());
1026 EXPECT_EQ(base::string16(), get_values
[0]);
1028 // Expect regular |GetInfo| returns empty value.
1029 EXPECT_EQ(base::string16(), p
.GetRawInfo(NAME_FULL
));
1032 TEST(AutofillProfileTest
, MultiValueEmails
) {
1033 AutofillProfile
p(base::GenerateGUID(), "https://www.example.com/");
1034 const base::string16
kJohnDoe(ASCIIToUTF16("john@doe.com"));
1035 const base::string16
kJohnPDoe(ASCIIToUTF16("john_p@doe.com"));
1036 std::vector
<base::string16
> set_values
;
1037 set_values
.push_back(kJohnDoe
);
1038 set_values
.push_back(kJohnPDoe
);
1039 p
.SetRawMultiInfo(EMAIL_ADDRESS
, set_values
);
1041 // Expect regular |GetInfo| returns the first element.
1042 EXPECT_EQ(kJohnDoe
, p
.GetRawInfo(EMAIL_ADDRESS
));
1044 // Ensure that we get out what we put in.
1045 std::vector
<base::string16
> get_values
;
1046 p
.GetRawMultiInfo(EMAIL_ADDRESS
, &get_values
);
1047 ASSERT_EQ(2UL, get_values
.size());
1048 EXPECT_EQ(kJohnDoe
, get_values
[0]);
1049 EXPECT_EQ(kJohnPDoe
, get_values
[1]);
1051 // Update the values.
1052 AutofillProfile p2
= p
;
1053 EXPECT_EQ(0, p
.Compare(p2
));
1054 const base::string16
kNoOne(ASCIIToUTF16("no@one.com"));
1055 set_values
[1] = kNoOne
;
1056 p
.SetRawMultiInfo(EMAIL_ADDRESS
, set_values
);
1057 p
.GetRawMultiInfo(EMAIL_ADDRESS
, &get_values
);
1058 ASSERT_EQ(2UL, get_values
.size());
1059 EXPECT_EQ(kJohnDoe
, get_values
[0]);
1060 EXPECT_EQ(kNoOne
, get_values
[1]);
1061 EXPECT_NE(0, p
.Compare(p2
));
1065 p
.SetRawMultiInfo(EMAIL_ADDRESS
, set_values
);
1066 p
.GetRawMultiInfo(EMAIL_ADDRESS
, &get_values
);
1067 ASSERT_EQ(1UL, get_values
.size());
1068 EXPECT_EQ(base::string16(), get_values
[0]);
1070 // Expect regular |GetInfo| returns empty value.
1071 EXPECT_EQ(base::string16(), p
.GetRawInfo(EMAIL_ADDRESS
));
1074 TEST(AutofillProfileTest
, MultiValuePhone
) {
1075 AutofillProfile
p(base::GenerateGUID(), "https://www.example.com/");
1076 const base::string16
kJohnDoe(ASCIIToUTF16("4151112222"));
1077 const base::string16
kJohnPDoe(ASCIIToUTF16("4151113333"));
1078 std::vector
<base::string16
> set_values
;
1079 set_values
.push_back(kJohnDoe
);
1080 set_values
.push_back(kJohnPDoe
);
1081 p
.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER
, set_values
);
1083 // Expect regular |GetInfo| returns the first element.
1084 EXPECT_EQ(kJohnDoe
, p
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
));
1086 // Ensure that we get out what we put in.
1087 std::vector
<base::string16
> get_values
;
1088 p
.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER
, &get_values
);
1089 ASSERT_EQ(2UL, get_values
.size());
1090 EXPECT_EQ(kJohnDoe
, get_values
[0]);
1091 EXPECT_EQ(kJohnPDoe
, get_values
[1]);
1093 // Update the values.
1094 AutofillProfile p2
= p
;
1095 EXPECT_EQ(0, p
.Compare(p2
));
1096 const base::string16
kNoOne(ASCIIToUTF16("4152110000"));
1097 set_values
[1] = kNoOne
;
1098 p
.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER
, set_values
);
1099 p
.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER
, &get_values
);
1100 ASSERT_EQ(2UL, get_values
.size());
1101 EXPECT_EQ(kJohnDoe
, get_values
[0]);
1102 EXPECT_EQ(kNoOne
, get_values
[1]);
1103 EXPECT_NE(0, p
.Compare(p2
));
1107 p
.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER
, set_values
);
1108 p
.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER
, &get_values
);
1109 ASSERT_EQ(1UL, get_values
.size());
1110 EXPECT_EQ(base::string16(), get_values
[0]);
1112 // Expect regular |GetInfo| returns empty value.
1113 EXPECT_EQ(base::string16(), p
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
));
1116 TEST(AutofillProfileTest
, IsPresentButInvalid
) {
1117 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
1118 EXPECT_FALSE(profile
.IsPresentButInvalid(ADDRESS_HOME_STATE
));
1119 EXPECT_FALSE(profile
.IsPresentButInvalid(ADDRESS_HOME_ZIP
));
1120 EXPECT_FALSE(profile
.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER
));
1122 profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
1123 EXPECT_FALSE(profile
.IsPresentButInvalid(ADDRESS_HOME_STATE
));
1124 EXPECT_FALSE(profile
.IsPresentButInvalid(ADDRESS_HOME_ZIP
));
1125 EXPECT_FALSE(profile
.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER
));
1127 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("C"));
1128 EXPECT_TRUE(profile
.IsPresentButInvalid(ADDRESS_HOME_STATE
));
1130 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
1131 EXPECT_FALSE(profile
.IsPresentButInvalid(ADDRESS_HOME_STATE
));
1133 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("90"));
1134 EXPECT_TRUE(profile
.IsPresentButInvalid(ADDRESS_HOME_ZIP
));
1136 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("90210"));
1137 EXPECT_FALSE(profile
.IsPresentButInvalid(ADDRESS_HOME_ZIP
));
1139 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("310"));
1140 EXPECT_TRUE(profile
.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER
));
1142 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("(310) 310-6000"));
1143 EXPECT_FALSE(profile
.IsPresentButInvalid(PHONE_HOME_WHOLE_NUMBER
));
1146 TEST(AutofillProfileTest
, SetRawInfoPreservesLineBreaks
) {
1147 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
1149 profile
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
,
1150 ASCIIToUTF16("123 Super St.\n"
1152 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n"
1154 profile
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
1157 TEST(AutofillProfileTest
, SetInfoPreservesLineBreaks
) {
1158 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
1160 profile
.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS
),
1161 ASCIIToUTF16("123 Super St.\n"
1164 EXPECT_EQ(ASCIIToUTF16("123 Super St.\n"
1166 profile
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
1169 TEST(AutofillProfileTest
, SetRawInfoDoesntTrimWhitespace
) {
1170 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
1172 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("\tuser@example.com "));
1173 EXPECT_EQ(ASCIIToUTF16("\tuser@example.com "),
1174 profile
.GetRawInfo(EMAIL_ADDRESS
));
1177 TEST(AutofillProfileTest
, SetInfoTrimsWhitespace
) {
1178 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
1180 profile
.SetInfo(AutofillType(EMAIL_ADDRESS
),
1181 ASCIIToUTF16("\tuser@example.com "),
1183 EXPECT_EQ(ASCIIToUTF16("user@example.com"),
1184 profile
.GetRawInfo(EMAIL_ADDRESS
));
1187 TEST(AutofillProfileTest
, FullAddress
) {
1188 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
1189 test::SetProfileInfo(&profile
, "Marion", "Mitchell", "Morrison",
1190 "marion@me.xyz", "Fox", "123 Zoo St.", "unit 5",
1191 "Hollywood", "CA", "91601", "US",
1194 AutofillType
full_address(HTML_TYPE_FULL_ADDRESS
, HTML_MODE_NONE
);
1195 base::string16
formatted_address(ASCIIToUTF16(
1196 "Marion Mitchell Morrison\n"
1200 "Hollywood, CA 91601"));
1201 EXPECT_EQ(formatted_address
, profile
.GetInfo(full_address
, "en-US"));
1202 // This should fail and leave the profile unchanged.
1203 EXPECT_FALSE(profile
.SetInfo(full_address
, ASCIIToUTF16("foobar"), "en-US"));
1204 EXPECT_EQ(formatted_address
, profile
.GetInfo(full_address
, "en-US"));
1206 // Some things can be missing...
1207 profile
.SetInfo(AutofillType(ADDRESS_HOME_LINE2
),
1210 profile
.SetInfo(AutofillType(EMAIL_ADDRESS
),
1213 EXPECT_EQ(ASCIIToUTF16("Marion Mitchell Morrison\n"
1216 "Hollywood, CA 91601"),
1217 profile
.GetInfo(full_address
, "en-US"));
1219 // ...but nothing comes out if a required field is missing.
1220 profile
.SetInfo(AutofillType(ADDRESS_HOME_STATE
), base::string16(), "en-US");
1221 EXPECT_TRUE(profile
.GetInfo(full_address
, "en-US").empty());
1223 // Restore the state but remove country. This should also fail.
1224 profile
.SetInfo(AutofillType(ADDRESS_HOME_STATE
),
1227 EXPECT_FALSE(profile
.GetInfo(full_address
, "en-US").empty());
1228 profile
.SetInfo(AutofillType(ADDRESS_HOME_COUNTRY
),
1231 EXPECT_TRUE(profile
.GetInfo(full_address
, "en-US").empty());
1234 TEST(AutofillProfileTest
, CopyAndUpdateNameList
) {
1235 std::vector
<base::string16
> user_inputs
;
1236 user_inputs
.push_back(ASCIIToUTF16("James Cameron Smith Harding"));
1237 user_inputs
.push_back(ASCIIToUTF16("Jane Smith Harding"));
1238 user_inputs
.push_back(ASCIIToUTF16("Alison Smith Harding"));
1240 // With no base profile, names should just be copied.
1241 AutofillProfile new_profile
;
1242 new_profile
.CopyAndUpdateNameList(user_inputs
, NULL
, "en-US");
1243 std::vector
<base::string16
> stored_full_names
;
1244 new_profile
.GetMultiInfo(AutofillType(NAME_FULL
), "en-US",
1245 &stored_full_names
);
1246 EXPECT_EQ(user_inputs
, stored_full_names
);
1248 // With a base profile, names should be copied but keep their original parsing
1250 AutofillProfile original_profile
;
1251 std::vector
<base::string16
> original_first_names
, original_middle_names
,
1252 original_last_names
;
1253 original_first_names
.push_back(ASCIIToUTF16("James"));
1254 original_middle_names
.push_back(ASCIIToUTF16("Cameron"));
1255 original_last_names
.push_back(ASCIIToUTF16("Smith Harding"));
1257 original_first_names
.push_back(ASCIIToUTF16("Jane Smith"));
1258 original_middle_names
.push_back(base::string16());
1259 original_last_names
.push_back(ASCIIToUTF16("Harding"));
1261 original_profile
.SetRawMultiInfo(NAME_FIRST
, original_first_names
);
1262 original_profile
.SetRawMultiInfo(NAME_MIDDLE
, original_middle_names
);
1263 original_profile
.SetRawMultiInfo(NAME_LAST
, original_last_names
);
1265 new_profile
.CopyAndUpdateNameList(user_inputs
, &original_profile
, "en-US");
1266 new_profile
.GetMultiInfo(AutofillType(NAME_FULL
), "en-US",
1267 &stored_full_names
);
1268 EXPECT_EQ(user_inputs
, stored_full_names
);
1270 // The parsed names shouldn't have changed, except for the addition of the new
1272 std::vector
<base::string16
> expected_first_names
= original_first_names
;
1273 expected_first_names
.push_back(ASCIIToUTF16("Alison"));
1274 std::vector
<base::string16
> stored_first_names
;
1275 new_profile
.GetMultiInfo(AutofillType(NAME_FIRST
), "en-US",
1276 &stored_first_names
);
1277 EXPECT_EQ(expected_first_names
, stored_first_names
);
1279 std::vector
<base::string16
> expected_middle_names
= original_middle_names
;
1280 expected_middle_names
.push_back(ASCIIToUTF16("Smith"));
1281 std::vector
<base::string16
> stored_middle_names
;
1282 new_profile
.GetMultiInfo(AutofillType(NAME_MIDDLE
), "en-US",
1283 &stored_middle_names
);
1284 EXPECT_EQ(expected_middle_names
, stored_middle_names
);
1286 std::vector
<base::string16
> expected_last_names
= original_last_names
;
1287 expected_last_names
.push_back(ASCIIToUTF16("Harding"));
1288 std::vector
<base::string16
> stored_last_names
;
1289 new_profile
.GetMultiInfo(AutofillType(NAME_LAST
), "en-US",
1290 &stored_last_names
);
1291 EXPECT_EQ(expected_last_names
, stored_last_names
);
1294 TEST(AutofillProfileTest
, OverwriteOrAppendNames
) {
1295 std::vector
<TestCase
> test_cases
;
1298 test_cases
.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
1299 NameParts("Marion", "Mitchell", "Morrison"),
1300 NameParts("Marion", "Mitchell", "Morrison")));
1301 test_cases
.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
1302 NameParts("MARION", "MITCHELL", "MORRISON"),
1303 NameParts("Marion", "Mitchell", "Morrison")));
1305 // A parse that has a two-word last name should take precedence over a
1306 // parse that assumes the two names are a middle and a last name.
1307 test_cases
.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
1308 NameParts("Marion", "", "Mitchell Morrison"),
1309 NameParts("Marion", "", "Mitchell Morrison")));
1310 test_cases
.push_back(TestCase(NameParts("Marion", "", "Mitchell Morrison"),
1311 NameParts("Marion", "Mitchell", "Morrison"),
1312 NameParts("Marion", "", "Mitchell Morrison")));
1314 // A parse that has a two-word first name should take precedence over a
1315 // parse that assumes the two names are a first and a middle name.
1316 test_cases
.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
1317 NameParts("Marion Mitchell", "", "Morrison"),
1318 NameParts("Marion Mitchell", "", "Morrison")));
1319 test_cases
.push_back(TestCase(NameParts("Marion Mitchell", "", "Morrison"),
1320 NameParts("Marion", "Mitchell", "Morrison"),
1321 NameParts("Marion Mitchell", "", "Morrison")));
1323 // Two names that are identical in full, but not in parts: the parse that
1324 // does *not* match the heuristic parse should be preferred.
1325 test_cases
.push_back(
1326 TestCase(NameParts("Arthur", "Ignatius Conan", "Doyle"),
1328 NameParts("Arthur Ignatius", "Conan", "Doyle"),
1329 NameParts("Arthur", "Ignatius Conan", "Doyle")));
1330 test_cases
.push_back(
1332 TestCase(NameParts("Arthur Ignatius", "Conan", "Doyle"),
1333 NameParts("Arthur", "Ignatius Conan", "Doyle"),
1334 NameParts("Arthur", "Ignatius Conan", "Doyle")));
1336 // A parse that has a many-word first name and/or last name should take
1337 // precedence over a heuristically parsed name.
1338 test_cases
.push_back(
1340 TestCase(NameParts("Roberto Carlos da", "Silva", "Rocha"),
1341 NameParts("Roberto Carlos da Silva", "", "Rocha"),
1342 NameParts("Roberto Carlos da Silva", "", "Rocha")));
1344 // Cases where merging 2 profiles with same full names but
1345 // different canonical forms appends instead of overwrites,
1346 // provided they dont form heuristically parsed names.
1348 NameParts
name1("Marion Mitchell", "", "Morrison");
1349 NameParts
name2("Marion", "", "Mitchell Morrison");
1350 std::vector
<NameParts
> starting_names(1, name1
);
1351 std::vector
<NameParts
> additional_names(1, name2
);
1352 std::vector
<NameParts
> expected_result
;
1353 expected_result
.push_back(name1
);
1354 expected_result
.push_back(name2
);
1355 test_cases
.push_back(
1356 TestCase(starting_names
, additional_names
, expected_result
));
1359 // Cases where the names do not have the same full name strings,
1360 // i.e. the list of merged names is longer than either of the incoming
1363 NameParts
name1("Antonio", "Augusto Ribeiro", "Reis Jr.");
1364 NameParts
name2("Juninho", "", "Pernambucano");
1365 NameParts
name3("Marion", "Mitchell", "Morrison");
1366 NameParts
name4("Marion", "M.", "Morrison");
1367 std::vector
<NameParts
> starting_names
;
1368 std::vector
<NameParts
> additional_names
;
1369 std::vector
<NameParts
> expected_result
;
1370 starting_names
.push_back(name1
);
1371 starting_names
.push_back(name2
);
1372 additional_names
.push_back(name3
);
1373 additional_names
.push_back(name4
);
1374 expected_result
.push_back(name1
);
1375 expected_result
.push_back(name2
);
1376 expected_result
.push_back(name3
);
1377 expected_result
.push_back(name4
);
1378 test_cases
.push_back(
1379 TestCase(starting_names
, additional_names
, expected_result
));
1382 for (std::vector
<TestCase
>::iterator it
= test_cases
.begin();
1383 it
!= test_cases
.end();
1385 TestCase current_case
= *it
;
1386 SCOPED_TRACE(current_case
.starting_names
[0].first
+ " + " +
1387 current_case
.additional_names
[0].first
+ " = " +
1388 current_case
.expected_result
[0].first
);
1390 std::vector
<base::string16
> first_names
, middle_names
, last_names
;
1392 current_case
.starting_names
, &first_names
, &middle_names
, &last_names
);
1394 // Construct the starting_profile.
1395 AutofillProfile
starting_profile(base::GenerateGUID(),
1396 "https://www.example.com/");
1398 starting_profile
.SetRawMultiInfo(NAME_FIRST
, first_names
);
1399 starting_profile
.SetRawMultiInfo(NAME_MIDDLE
, middle_names
);
1400 starting_profile
.SetRawMultiInfo(NAME_LAST
, last_names
);
1402 first_names
.clear();
1403 middle_names
.clear();
1406 current_case
.additional_names
, &first_names
, &middle_names
,
1409 // Construct the additional_profile.
1410 AutofillProfile
additional_profile(base::GenerateGUID(),
1411 "https://www.example.com/");
1412 additional_profile
.SetRawMultiInfo(NAME_FIRST
, first_names
);
1413 additional_profile
.SetRawMultiInfo(NAME_MIDDLE
, middle_names
);
1414 additional_profile
.SetRawMultiInfo(NAME_LAST
, last_names
);
1416 // Merge the names from the |additional_profile| into the |starting_profile|
1417 starting_profile
.OverwriteWithOrAddTo(additional_profile
, "en-US");
1419 // Verify the test expectations.
1420 first_names
.clear();
1421 middle_names
.clear();
1424 current_case
.expected_result
, &first_names
, &middle_names
, &last_names
);
1426 std::vector
<base::string16
> merged_first_names
, merged_middle_names
,
1428 starting_profile
.GetRawMultiInfo(NAME_FIRST
, &merged_first_names
);
1429 starting_profile
.GetRawMultiInfo(NAME_MIDDLE
, &merged_middle_names
);
1430 starting_profile
.GetRawMultiInfo(NAME_LAST
, &merged_last_names
);
1431 ASSERT_EQ(current_case
.expected_result
.size(), merged_first_names
.size());
1432 ASSERT_EQ(current_case
.expected_result
.size(), merged_middle_names
.size());
1433 ASSERT_EQ(current_case
.expected_result
.size(), merged_last_names
.size());
1435 for (size_t i
= 0; i
< current_case
.expected_result
.size(); ++i
) {
1436 EXPECT_EQ(first_names
[i
], merged_first_names
[i
]);
1437 EXPECT_EQ(middle_names
[i
], merged_middle_names
[i
]);
1438 EXPECT_EQ(last_names
[i
], merged_last_names
[i
]);
1443 TEST(AutofillProfileTest
, CanonicalizeProfileString
) {
1445 EXPECT_EQ(base::string16(),
1446 AutofillProfile::CanonicalizeProfileString(base::string16()));
1448 // Simple punctuation removed.
1449 EXPECT_EQ(ASCIIToUTF16("1600 amphitheatre pkwy"),
1450 AutofillProfile::CanonicalizeProfileString(ASCIIToUTF16(
1451 "1600 Amphitheatre, Pkwy.")));
1453 // Unicode punctuation (hyphen and space), multiple spaces collapsed.
1454 EXPECT_EQ(ASCIIToUTF16("mid island plaza"),
1455 AutofillProfile::CanonicalizeProfileString(base::WideToUTF16(
1456 L
"Mid\x2013Island\x2003 Plaza")));
1459 } // namespace autofill