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

Side by Side Diff: components/autofill/browser/phone_number_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698