OLD | NEW |
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/strings/string16.h" | 5 #include "base/strings/string16.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "components/autofill/core/browser/autofill_profile.h" | 7 #include "components/autofill/core/browser/autofill_profile.h" |
| 8 #include "components/autofill/core/browser/autofill_type.h" |
8 #include "components/autofill/core/browser/field_types.h" | 9 #include "components/autofill/core/browser/field_types.h" |
9 #include "components/autofill/core/browser/phone_number.h" | 10 #include "components/autofill/core/browser/phone_number.h" |
10 #include "components/autofill/core/browser/phone_number_i18n.h" | 11 #include "components/autofill/core/browser/phone_number_i18n.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace autofill { | 14 namespace autofill { |
14 | 15 |
15 TEST(PhoneNumberTest, Matcher) { | 16 TEST(PhoneNumberTest, Matcher) { |
16 AutofillProfile profile; | 17 AutofillProfile profile; |
17 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); | 18 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
18 // Set phone number so country_code == 1, city_code = 650, number = 2345678. | 19 // Set phone number so country_code == 1, city_code = 650, number = 2345678. |
19 base::string16 phone(ASCIIToUTF16("1 [650] 234-5678")); | 20 base::string16 phone(ASCIIToUTF16("1 [650] 234-5678")); |
20 PhoneNumber phone_number(&profile); | 21 PhoneNumber phone_number(&profile); |
21 phone_number.SetInfo(PHONE_HOME_WHOLE_NUMBER, phone, "US"); | 22 phone_number.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone, "US"); |
22 | 23 |
23 FieldTypeSet matching_types; | 24 ServerFieldTypeSet matching_types; |
24 phone_number.GetMatchingTypes(base::string16(), "US", &matching_types); | 25 phone_number.GetMatchingTypes(base::string16(), "US", &matching_types); |
25 EXPECT_EQ(1U, matching_types.size()); | 26 EXPECT_EQ(1U, matching_types.size()); |
26 EXPECT_TRUE(matching_types.find(EMPTY_TYPE) != matching_types.end()); | 27 EXPECT_TRUE(matching_types.find(EMPTY_TYPE) != matching_types.end()); |
27 matching_types.clear(); | 28 matching_types.clear(); |
28 phone_number.GetMatchingTypes(ASCIIToUTF16("1"), "US", &matching_types); | 29 phone_number.GetMatchingTypes(ASCIIToUTF16("1"), "US", &matching_types); |
29 EXPECT_EQ(1U, matching_types.size()); | 30 EXPECT_EQ(1U, matching_types.size()); |
30 EXPECT_TRUE(matching_types.find(PHONE_HOME_COUNTRY_CODE) != | 31 EXPECT_TRUE(matching_types.find(PHONE_HOME_COUNTRY_CODE) != |
31 matching_types.end()); | 32 matching_types.end()); |
32 matching_types.clear(); | 33 matching_types.clear(); |
33 phone_number.GetMatchingTypes(ASCIIToUTF16("16"), "US", &matching_types); | 34 phone_number.GetMatchingTypes(ASCIIToUTF16("16"), "US", &matching_types); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 85 |
85 // Verify that PhoneNumber::SetInfo() correctly formats the incoming number. | 86 // Verify that PhoneNumber::SetInfo() correctly formats the incoming number. |
86 TEST(PhoneNumberTest, SetInfo) { | 87 TEST(PhoneNumberTest, SetInfo) { |
87 AutofillProfile profile; | 88 AutofillProfile profile; |
88 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); | 89 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
89 | 90 |
90 PhoneNumber phone(&profile); | 91 PhoneNumber phone(&profile); |
91 EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 92 EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
92 | 93 |
93 // Set the formatted info directly. | 94 // Set the formatted info directly. |
94 EXPECT_TRUE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, | 95 EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), |
95 ASCIIToUTF16("(650) 234-5678"), "US")); | 96 ASCIIToUTF16("(650) 234-5678"), "US")); |
96 EXPECT_EQ(ASCIIToUTF16("(650) 234-5678"), | 97 EXPECT_EQ(ASCIIToUTF16("(650) 234-5678"), |
97 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 98 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
98 | 99 |
99 // Unformatted numbers should be formatted. | 100 // Unformatted numbers should be formatted. |
100 EXPECT_TRUE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, | 101 EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), |
101 ASCIIToUTF16("8887776666"), "US")); | 102 ASCIIToUTF16("8887776666"), "US")); |
102 EXPECT_EQ(ASCIIToUTF16("(888) 777-6666"), | 103 EXPECT_EQ(ASCIIToUTF16("(888) 777-6666"), |
103 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 104 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
104 | 105 |
105 // Differently formatted numbers should be re-formatted. | 106 // Differently formatted numbers should be re-formatted. |
106 EXPECT_TRUE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, | 107 EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), |
107 ASCIIToUTF16("800-432-8765"), "US")); | 108 ASCIIToUTF16("800-432-8765"), "US")); |
108 EXPECT_EQ(ASCIIToUTF16("(800) 432-8765"), | 109 EXPECT_EQ(ASCIIToUTF16("(800) 432-8765"), |
109 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 110 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
110 | 111 |
111 // Invalid numbers should not be stored. In the US, phone numbers cannot | 112 // Invalid numbers should not be stored. In the US, phone numbers cannot |
112 // start with the digit '1'. | 113 // start with the digit '1'. |
113 EXPECT_FALSE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, | 114 EXPECT_FALSE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), |
114 ASCIIToUTF16("650111111"), "US")); | 115 ASCIIToUTF16("650111111"), "US")); |
115 EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 116 EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
116 } | 117 } |
117 | 118 |
118 // Test that cached phone numbers are correctly invalidated and updated. | 119 // Test that cached phone numbers are correctly invalidated and updated. |
119 TEST(PhoneNumberTest, UpdateCachedPhoneNumber) { | 120 TEST(PhoneNumberTest, UpdateCachedPhoneNumber) { |
120 AutofillProfile profile; | 121 AutofillProfile profile; |
121 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); | 122 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
122 | 123 |
123 PhoneNumber phone(&profile); | 124 PhoneNumber phone(&profile); |
124 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("6502345678")); | 125 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("6502345678")); |
125 EXPECT_EQ(ASCIIToUTF16("650"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 126 EXPECT_EQ(ASCIIToUTF16("650"), |
| 127 phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US")); |
126 | 128 |
127 // Update the area code. | 129 // Update the area code. |
128 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8322345678")); | 130 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8322345678")); |
129 EXPECT_EQ(ASCIIToUTF16("832"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 131 EXPECT_EQ(ASCIIToUTF16("832"), |
| 132 phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US")); |
130 | 133 |
131 // Change the phone number to have a UK format, but try to parse with the | 134 // Change the phone number to have a UK format, but try to parse with the |
132 // wrong locale. | 135 // wrong locale. |
133 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); | 136 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); |
134 EXPECT_EQ(base::string16(), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 137 EXPECT_EQ(base::string16(), |
| 138 phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US")); |
135 | 139 |
136 // Now try parsing using the correct locale. Note that the profile's country | 140 // Now try parsing using the correct locale. Note that the profile's country |
137 // code should override the app locale, which is still set to "US". | 141 // code should override the app locale, which is still set to "US". |
138 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB")); | 142 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB")); |
139 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); | 143 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); |
140 EXPECT_EQ(ASCIIToUTF16("70"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 144 EXPECT_EQ(ASCIIToUTF16("70"), |
| 145 phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US")); |
141 } | 146 } |
142 | 147 |
143 TEST(PhoneNumberTest, PhoneCombineHelper) { | 148 TEST(PhoneNumberTest, PhoneCombineHelper) { |
144 AutofillProfile profile; | 149 AutofillProfile profile; |
145 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); | 150 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
146 | 151 |
147 PhoneNumber::PhoneCombineHelper number1; | 152 PhoneNumber::PhoneCombineHelper number1; |
148 EXPECT_FALSE(number1.SetInfo(ADDRESS_BILLING_CITY, | 153 EXPECT_FALSE(number1.SetInfo(AutofillType(ADDRESS_BILLING_CITY), |
149 ASCIIToUTF16("1"))); | 154 ASCIIToUTF16("1"))); |
150 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_COUNTRY_CODE, | 155 EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_COUNTRY_CODE), |
151 ASCIIToUTF16("1"))); | 156 ASCIIToUTF16("1"))); |
152 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_CITY_CODE, | 157 EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_CITY_CODE), |
153 ASCIIToUTF16("650"))); | 158 ASCIIToUTF16("650"))); |
154 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_NUMBER, | 159 EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
155 ASCIIToUTF16("2345678"))); | 160 ASCIIToUTF16("2345678"))); |
156 base::string16 parsed_phone; | 161 base::string16 parsed_phone; |
157 EXPECT_TRUE(number1.ParseNumber(profile, "en-US", &parsed_phone)); | 162 EXPECT_TRUE(number1.ParseNumber(profile, "en-US", &parsed_phone)); |
158 // International format as it has a country code. | 163 // International format as it has a country code. |
159 EXPECT_EQ(ASCIIToUTF16("+1 650-234-5678"), parsed_phone); | 164 EXPECT_EQ(ASCIIToUTF16("+1 650-234-5678"), parsed_phone); |
160 | 165 |
161 PhoneNumber::PhoneCombineHelper number3; | 166 PhoneNumber::PhoneCombineHelper number3; |
162 EXPECT_TRUE(number3.SetInfo(PHONE_HOME_CITY_CODE, | 167 EXPECT_TRUE(number3.SetInfo(AutofillType(PHONE_HOME_CITY_CODE), |
163 ASCIIToUTF16("650"))); | 168 ASCIIToUTF16("650"))); |
164 EXPECT_TRUE(number3.SetInfo(PHONE_HOME_NUMBER, | 169 EXPECT_TRUE(number3.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
165 ASCIIToUTF16("2345680"))); | 170 ASCIIToUTF16("2345680"))); |
166 EXPECT_TRUE(number3.ParseNumber(profile, "en-US", &parsed_phone)); | 171 EXPECT_TRUE(number3.ParseNumber(profile, "en-US", &parsed_phone)); |
167 // National format as it does not have a country code. | 172 // National format as it does not have a country code. |
168 EXPECT_EQ(ASCIIToUTF16("(650) 234-5680"), parsed_phone); | 173 EXPECT_EQ(ASCIIToUTF16("(650) 234-5680"), parsed_phone); |
169 | 174 |
170 PhoneNumber::PhoneCombineHelper number4; | 175 PhoneNumber::PhoneCombineHelper number4; |
171 EXPECT_TRUE(number4.SetInfo(PHONE_HOME_CITY_CODE, | 176 EXPECT_TRUE(number4.SetInfo(AutofillType(PHONE_HOME_CITY_CODE), |
172 ASCIIToUTF16("123"))); // Incorrect city code. | 177 ASCIIToUTF16("123"))); // Incorrect city code. |
173 EXPECT_TRUE(number4.SetInfo(PHONE_HOME_NUMBER, | 178 EXPECT_TRUE(number4.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
174 ASCIIToUTF16("2345680"))); | 179 ASCIIToUTF16("2345680"))); |
175 EXPECT_FALSE(number4.ParseNumber(profile, "en-US", &parsed_phone)); | 180 EXPECT_FALSE(number4.ParseNumber(profile, "en-US", &parsed_phone)); |
176 EXPECT_EQ(base::string16(), parsed_phone); | 181 EXPECT_EQ(base::string16(), parsed_phone); |
177 | 182 |
178 PhoneNumber::PhoneCombineHelper number5; | 183 PhoneNumber::PhoneCombineHelper number5; |
179 EXPECT_TRUE(number5.SetInfo(PHONE_HOME_CITY_AND_NUMBER, | 184 EXPECT_TRUE(number5.SetInfo(AutofillType(PHONE_HOME_CITY_AND_NUMBER), |
180 ASCIIToUTF16("6502345681"))); | 185 ASCIIToUTF16("6502345681"))); |
181 EXPECT_TRUE(number5.ParseNumber(profile, "en-US", &parsed_phone)); | 186 EXPECT_TRUE(number5.ParseNumber(profile, "en-US", &parsed_phone)); |
182 EXPECT_EQ(ASCIIToUTF16("(650) 234-5681"), parsed_phone); | 187 EXPECT_EQ(ASCIIToUTF16("(650) 234-5681"), parsed_phone); |
183 | 188 |
184 PhoneNumber::PhoneCombineHelper number6; | 189 PhoneNumber::PhoneCombineHelper number6; |
185 EXPECT_TRUE(number6.SetInfo(PHONE_HOME_CITY_CODE, | 190 EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_CITY_CODE), |
186 ASCIIToUTF16("650"))); | 191 ASCIIToUTF16("650"))); |
187 EXPECT_TRUE(number6.SetInfo(PHONE_HOME_NUMBER, | 192 EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
188 ASCIIToUTF16("234"))); | 193 ASCIIToUTF16("234"))); |
189 EXPECT_TRUE(number6.SetInfo(PHONE_HOME_NUMBER, | 194 EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
190 ASCIIToUTF16("5682"))); | 195 ASCIIToUTF16("5682"))); |
191 EXPECT_TRUE(number6.ParseNumber(profile, "en-US", &parsed_phone)); | 196 EXPECT_TRUE(number6.ParseNumber(profile, "en-US", &parsed_phone)); |
192 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone); | 197 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone); |
193 | 198 |
194 // Ensure parsing is possible when falling back to detecting the country code | 199 // Ensure parsing is possible when falling back to detecting the country code |
195 // based on the app locale. | 200 // based on the app locale. |
196 PhoneNumber::PhoneCombineHelper number7; | 201 PhoneNumber::PhoneCombineHelper number7; |
197 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_CITY_CODE, | 202 EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_CITY_CODE), |
198 ASCIIToUTF16("650"))); | 203 ASCIIToUTF16("650"))); |
199 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_NUMBER, | 204 EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
200 ASCIIToUTF16("234"))); | 205 ASCIIToUTF16("234"))); |
201 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_NUMBER, | 206 EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_NUMBER), |
202 ASCIIToUTF16("5682"))); | 207 ASCIIToUTF16("5682"))); |
203 EXPECT_TRUE(number7.ParseNumber(AutofillProfile(), "en-US", &parsed_phone)); | 208 EXPECT_TRUE(number7.ParseNumber(AutofillProfile(), "en-US", &parsed_phone)); |
204 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone); | 209 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone); |
205 } | 210 } |
206 | 211 |
207 } // namespace autofill | 212 } // namespace autofill |
OLD | NEW |