Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: components/autofill/core/browser/autofill_profile_unittest.cc

Issue 261993006: Modified to allow to preserve two-word string in first-name and last-name in autofill profile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Incorporated review comments and modified unit tests. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/format_macros.h"
6 #include "base/guid.h" 7 #include "base/guid.h"
7 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
9 #include "base/stl_util.h" 10 #include "base/stl_util.h"
10 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/browser/autofill_profile.h" 14 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h" 15 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/browser/autofill_type.h" 16 #include "components/autofill/core/browser/autofill_type.h"
17 #include "components/autofill/core/browser/field_types.h"
15 #include "components/autofill/core/common/form_field_data.h" 18 #include "components/autofill/core/common/form_field_data.h"
16 #include "grit/components_strings.h" 19 #include "grit/components_strings.h"
17 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
18 21
19 using base::ASCIIToUTF16; 22 using base::ASCIIToUTF16;
20 23
21 namespace autofill { 24 namespace autofill {
22 25
23 namespace { 26 namespace {
24 27
25 base::string16 GetLabel(AutofillProfile* profile) { 28 base::string16 GetLabel(AutofillProfile* profile) {
26 std::vector<AutofillProfile*> profiles; 29 std::vector<AutofillProfile*> profiles;
27 profiles.push_back(profile); 30 profiles.push_back(profile);
28 std::vector<base::string16> labels; 31 std::vector<base::string16> labels;
29 AutofillProfile::CreateDifferentiatingLabels(profiles, &labels); 32 AutofillProfile::CreateDifferentiatingLabels(profiles, &labels);
30 return labels[0]; 33 return labels[0];
31 } 34 }
32 35
36 // Holds the autofill profile |first|, |middle| and |last| names.
37 struct NameParts {
38 NameParts(const std::string& first,
39 const std::string& middle,
40 const std::string& last)
41 : first(first), middle(middle), last(last) {}
42
43 std::string first;
44 std::string middle;
45 std::string last;
46 };
47
48 // Test case to be executed to validate OverwriteOrAppendNames.
49 struct TestCase {
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)) {}
56
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) {}
63
64 std::vector<NameParts> starting_names;
65 std::vector<NameParts> additional_names;
66 std::vector<NameParts> expected_result;
67 };
68
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));
80 }
81 }
82
33 } // namespace 83 } // namespace
34 84
35 // Tests different possibilities for summary string generation. 85 // Tests different possibilities for summary string generation.
36 // Based on existence of first name, last name, and address line 1. 86 // Based on existence of first name, last name, and address line 1.
37 TEST(AutofillProfileTest, PreviewSummaryString) { 87 TEST(AutofillProfileTest, PreviewSummaryString) {
38 // Case 0/null: "" 88 // Case 0/null: ""
39 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com/"); 89 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com/");
40 // Empty profile - nothing to update. 90 // Empty profile - nothing to update.
41 base::string16 summary0 = GetLabel(&profile0); 91 base::string16 summary0 = GetLabel(&profile0);
42 EXPECT_EQ(base::string16(), summary0); 92 EXPECT_EQ(base::string16(), summary0);
(...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), 941 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE),
892 ASCIIToUTF16("CA"), 942 ASCIIToUTF16("CA"),
893 "en-US"); 943 "en-US");
894 EXPECT_FALSE(profile.GetInfo(full_address, "en-US").empty()); 944 EXPECT_FALSE(profile.GetInfo(full_address, "en-US").empty());
895 profile.SetInfo(AutofillType(ADDRESS_HOME_COUNTRY), 945 profile.SetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
896 base::string16(), 946 base::string16(),
897 "en-US"); 947 "en-US");
898 EXPECT_TRUE(profile.GetInfo(full_address, "en-US").empty()); 948 EXPECT_TRUE(profile.GetInfo(full_address, "en-US").empty());
899 } 949 }
900 950
951 TEST(AutofillProfileTest, OverwriteOrAppendNames) {
952 std::vector<TestCase> test_cases;
953
954 // Identical name.
955 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
956 NameParts("Marion", "Mitchell", "Morrison"),
957 NameParts("Marion", "Mitchell", "Morrison")));
958 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
959 NameParts("MARION", "MITCHELL", "MORRISON"),
960 NameParts("Marion", "Mitchell", "Morrison")));
961
962 // A parse that has a two-word last name should take precedence over a
963 // parse that assumes the two names are a middle and a last name.
964 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
965 NameParts("Marion", "", "Mitchell Morrison"),
966 NameParts("Marion", "", "Mitchell Morrison")));
967 test_cases.push_back(TestCase(NameParts("Marion", "", "Mitchell Morrison"),
968 NameParts("Marion", "Mitchell", "Morrison"),
969 NameParts("Marion", "", "Mitchell Morrison")));
970
971 // A parse that has a two-word first name should take precedence over a
972 // parse that assumes the two names are a first and a middle name.
973 test_cases.push_back(TestCase(NameParts("Marion", "Mitchell", "Morrison"),
974 NameParts("Marion Mitchell", "", "Morrison"),
975 NameParts("Marion Mitchell", "", "Morrison")));
976 test_cases.push_back(TestCase(NameParts("Marion Mitchell", "", "Morrison"),
977 NameParts("Marion", "Mitchell", "Morrison"),
978 NameParts("Marion Mitchell", "", "Morrison")));
979
980 // A parse that has a two-word first name and two-word last name should
981 // take precedence over a parse that assumes the two middle names and
982 // one last name.
983 test_cases.push_back(
984 TestCase(NameParts("Arthur", "Ignatius Conan", "Doyle"),
985 NameParts("Arthur Ignatius", "", "Conan Doyle"),
986 NameParts("Arthur Ignatius", "", "Conan Doyle")));
987 test_cases.push_back(
988 TestCase(NameParts("Arthur Ignatius", "", "Conan Doyle"),
989 NameParts("Arthur", "Ignatius Conan", "Doyle"),
990 NameParts("Arthur Ignatius", "", "Conan Doyle")));
991
992 // A parse that has a many-word first name and/or last name should take
993 // precedence over a heuristically parsed name into {first, middle1
994 // middle2.. middlen, name}.
995 test_cases.push_back(
996 TestCase(NameParts("Arthur Ignatius Conan", "", "Doyle"),
997 NameParts("Arthur", "Ignatius Conan", "Doyle"),
998 NameParts("Arthur Ignatius Conan", "", "Doyle")));
999 test_cases.push_back(
1000 TestCase(NameParts("Roberto", "Carlos da Silva", "Rocha"),
1001 NameParts("Roberto Carlos da Silva", "", "Rocha"),
1002 NameParts("Roberto Carlos da Silva", "", "Rocha")));
1003 test_cases.push_back(
1004 TestCase(NameParts("Antonio", "Augusto", "Ribeiro Reis Jr."),
1005 NameParts("Antonio", "Augusto Ribeiro Reis", "Jr."),
1006 NameParts("Antonio", "Augusto", "Ribeiro Reis Jr.")));
1007
1008 // Cases where merging 2 profiles with same full names but
1009 // different canonical forms appends instead of overwrites,
1010 // provided they dont form heuristically parsed names.
1011 {
1012 NameParts name1("Marion Mitchell", "", "Morrison");
1013 NameParts name2("Marion", "", "Mitchell Morrison");
1014 std::vector<NameParts> starting_names(1, name1);
1015 std::vector<NameParts> additional_names(1, name2);
1016 std::vector<NameParts> expected_result;
1017 expected_result.push_back(name1);
1018 expected_result.push_back(name2);
1019 test_cases.push_back(
1020 TestCase(starting_names, additional_names, expected_result));
1021 }
1022
1023 // Cases where the names do not have the same full name strings,
1024 // i.e. the list of merged names is longer than either of the incoming
1025 // lists.
1026 {
1027 NameParts name1("Antonio", "Augusto Ribeiro", "Reis Jr.");
1028 NameParts name2("Juninho", "", "Pernambucano");
1029 NameParts name3("Marion", "Mitchell", "Morrison");
1030 NameParts name4("Marion", "M.", "Morrison");
1031 std::vector<NameParts> starting_names;
1032 std::vector<NameParts> additional_names;
1033 std::vector<NameParts> expected_result;
1034 starting_names.push_back(name1);
1035 starting_names.push_back(name2);
1036 additional_names.push_back(name3);
1037 additional_names.push_back(name4);
1038 expected_result.push_back(name1);
1039 expected_result.push_back(name2);
1040 expected_result.push_back(name3);
1041 expected_result.push_back(name4);
1042 test_cases.push_back(
1043 TestCase(starting_names, additional_names, expected_result));
1044 }
1045
1046 for (std::vector<TestCase>::iterator it = test_cases.begin();
1047 it != test_cases.end();
1048 ++it) {
1049 TestCase current_case = *it;
1050
1051 std::vector<base::string16> first_names, middle_names, last_names;
1052 GetNamePartsList(
1053 current_case.starting_names, first_names, middle_names, last_names);
1054
1055 // Construct the starting_profile.
1056 AutofillProfile starting_profile(base::GenerateGUID(),
1057 "https://www.example.com/");
1058
1059 starting_profile.SetRawMultiInfo(NAME_FIRST, first_names);
1060 starting_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names);
1061 starting_profile.SetRawMultiInfo(NAME_LAST, last_names);
1062
1063 first_names.clear();
1064 middle_names.clear();
1065 last_names.clear();
1066 GetNamePartsList(
1067 current_case.additional_names, first_names, middle_names, last_names);
1068
1069 // Construct the additional_profile.
1070 AutofillProfile additional_profile(base::GenerateGUID(),
1071 "https://www.example.com/");
1072 additional_profile.SetRawMultiInfo(NAME_FIRST, first_names);
1073 additional_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names);
1074 additional_profile.SetRawMultiInfo(NAME_LAST, last_names);
1075
1076 // Merge the names from the |additional_profile| into the |starting_profile|
1077 starting_profile.OverwriteWithOrAddTo(additional_profile, "en-US");
1078
1079 // Verify the test expectations.
1080 first_names.clear();
1081 middle_names.clear();
1082 last_names.clear();
1083 GetNamePartsList(
1084 current_case.expected_result, first_names, middle_names, last_names);
1085
1086 std::vector<base::string16> merged_first_names, merged_middle_names,
1087 merged_last_names;
1088 starting_profile.GetRawMultiInfo(NAME_FIRST, &merged_first_names);
1089 starting_profile.GetRawMultiInfo(NAME_MIDDLE, &merged_middle_names);
1090 starting_profile.GetRawMultiInfo(NAME_LAST, &merged_last_names);
1091 ASSERT_EQ(current_case.expected_result.size(), merged_first_names.size());
1092 ASSERT_EQ(current_case.expected_result.size(), merged_middle_names.size());
1093 ASSERT_EQ(current_case.expected_result.size(), merged_last_names.size());
1094
1095 for (size_t i = 0; i < current_case.expected_result.size(); ++i) {
1096 EXPECT_EQ(first_names[i], merged_first_names[i]);
1097 EXPECT_EQ(middle_names[i], merged_middle_names[i]);
1098 EXPECT_EQ(last_names[i], merged_last_names[i]);
1099 }
1100 }
1101 }
1102
901 } // namespace autofill 1103 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/autofill_profile.cc ('k') | components/autofill/core/browser/contact_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698