OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/message_loop.h" | |
6 #include "base/string16.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/autofill/phone_number_i18n.h" | |
9 #include "content/public/test/test_browser_thread.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "third_party/libphonenumber/src/phonenumber_api.h" | |
12 | |
13 using autofill_i18n::NormalizePhoneNumber; | |
14 using autofill_i18n::ParsePhoneNumber; | |
15 using autofill_i18n::ConstructPhoneNumber; | |
16 using autofill_i18n::PhoneNumbersMatch; | |
17 using content::BrowserThread; | |
18 | |
19 TEST(PhoneNumberI18NTest, NormalizePhoneNumber) { | |
20 // "Large" digits. | |
21 string16 phone1(UTF8ToUTF16("\xEF\xBC\x91\xEF\xBC\x96\xEF\xBC\x95\xEF\xBC\x90" | |
22 "\xEF\xBC\x97\xEF\xBC\x94\xEF\xBC\x99\xEF\xBC\x98" | |
23 "\xEF\xBC\x93\xEF\xBC\x92\xEF\xBC\x93")); | |
24 EXPECT_EQ(NormalizePhoneNumber(phone1, "US"), ASCIIToUTF16("16507498323")); | |
25 | |
26 // Devanagari script digits. | |
27 string16 phone2(UTF8ToUTF16("\xD9\xA1\xD9\xA6\xD9\xA5\xD9\xA0\xD9\xA8\xD9\xA3" | |
28 "\xD9\xA2\xD9\xA3\xD9\xA7\xD9\xA4\xD9\xA9")); | |
29 EXPECT_EQ(NormalizePhoneNumber(phone2, "US"), ASCIIToUTF16("16508323749")); | |
30 | |
31 string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5")); | |
32 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255")); | |
33 | |
34 string16 phone4(UTF8ToUTF16("+1(650)2346789")); | |
35 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789")); | |
36 | |
37 string16 phone5(UTF8ToUTF16("6502346789")); | |
38 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789")); | |
39 } | |
40 | |
41 TEST(PhoneNumberI18NTest, ParsePhoneNumber) { | |
42 string16 number; | |
43 string16 city_code; | |
44 string16 country_code; | |
45 i18n::phonenumbers::PhoneNumber unused_i18n_number; | |
46 | |
47 // Test for empty string. Should give back empty strings. | |
48 string16 phone0; | |
49 EXPECT_FALSE(ParsePhoneNumber(phone0, "US", | |
50 &country_code, | |
51 &city_code, | |
52 &number, | |
53 &unused_i18n_number)); | |
54 EXPECT_EQ(string16(), number); | |
55 EXPECT_EQ(string16(), city_code); | |
56 EXPECT_EQ(string16(), country_code); | |
57 | |
58 // Test for string with less than 7 digits. Should give back empty strings. | |
59 string16 phone1(ASCIIToUTF16("1234")); | |
60 EXPECT_FALSE(ParsePhoneNumber(phone1, "US", | |
61 &country_code, | |
62 &city_code, | |
63 &number, | |
64 &unused_i18n_number)); | |
65 EXPECT_EQ(string16(), number); | |
66 EXPECT_EQ(string16(), city_code); | |
67 EXPECT_EQ(string16(), country_code); | |
68 | |
69 // Test for string with exactly 7 digits. | |
70 // Not a valid number - starts with 1 | |
71 string16 phone2(ASCIIToUTF16("1234567")); | |
72 EXPECT_FALSE(ParsePhoneNumber(phone2, "US", | |
73 &country_code, | |
74 &city_code, | |
75 &number, | |
76 &unused_i18n_number)); | |
77 EXPECT_EQ(string16(), number); | |
78 EXPECT_EQ(string16(), city_code); | |
79 EXPECT_EQ(string16(), country_code); | |
80 | |
81 // Not a valid number - does not have area code. | |
82 string16 phone3(ASCIIToUTF16("2234567")); | |
83 EXPECT_FALSE(ParsePhoneNumber(phone3, "US", | |
84 &country_code, | |
85 &city_code, | |
86 &number, | |
87 &unused_i18n_number)); | |
88 EXPECT_EQ(string16(), number); | |
89 EXPECT_EQ(string16(), city_code); | |
90 EXPECT_EQ(string16(), country_code); | |
91 | |
92 // Test for string with greater than 7 digits but less than 10 digits. | |
93 // Should fail parsing in US. | |
94 string16 phone4(ASCIIToUTF16("123456789")); | |
95 EXPECT_FALSE(ParsePhoneNumber(phone4, "US", | |
96 &country_code, | |
97 &city_code, | |
98 &number, | |
99 &unused_i18n_number)); | |
100 EXPECT_EQ(string16(), number); | |
101 EXPECT_EQ(string16(), city_code); | |
102 EXPECT_EQ(string16(), country_code); | |
103 | |
104 // Test for string with greater than 7 digits but less than 10 digits and | |
105 // separators. | |
106 // Should fail parsing in US. | |
107 string16 phone_separator4(ASCIIToUTF16("12.345-6789")); | |
108 EXPECT_FALSE(ParsePhoneNumber(phone_separator4, "US", | |
109 &country_code, | |
110 &city_code, | |
111 &number, | |
112 &unused_i18n_number)); | |
113 EXPECT_EQ(string16(), number); | |
114 EXPECT_EQ(string16(), city_code); | |
115 EXPECT_EQ(string16(), country_code); | |
116 | |
117 // Test for string with exactly 10 digits. | |
118 // Should give back phone number and city code. | |
119 // This one going to fail because of the incorrect area code. | |
120 string16 phone5(ASCIIToUTF16("1234567890")); | |
121 EXPECT_FALSE(ParsePhoneNumber(phone5, "US", | |
122 &country_code, | |
123 &city_code, | |
124 &number, | |
125 &unused_i18n_number)); | |
126 EXPECT_EQ(string16(), number); | |
127 EXPECT_EQ(string16(), city_code); | |
128 EXPECT_EQ(string16(), country_code); | |
129 | |
130 string16 phone6(ASCIIToUTF16("6501567890")); | |
131 // This one going to fail because of the incorrect number (starts with 1). | |
132 EXPECT_FALSE(ParsePhoneNumber(phone6, "US", | |
133 &country_code, | |
134 &city_code, | |
135 &number, | |
136 &unused_i18n_number)); | |
137 EXPECT_EQ(string16(), number); | |
138 EXPECT_EQ(string16(), city_code); | |
139 EXPECT_EQ(string16(), country_code); | |
140 | |
141 string16 phone7(ASCIIToUTF16("6504567890")); | |
142 EXPECT_TRUE(ParsePhoneNumber(phone7, "US", | |
143 &country_code, | |
144 &city_code, | |
145 &number, | |
146 &unused_i18n_number)); | |
147 EXPECT_EQ(ASCIIToUTF16("4567890"), number); | |
148 EXPECT_EQ(ASCIIToUTF16("650"), city_code); | |
149 EXPECT_EQ(string16(), country_code); | |
150 | |
151 // Test for string with exactly 10 digits and separators. | |
152 // Should give back phone number and city code. | |
153 string16 phone_separator7(ASCIIToUTF16("(650) 456-7890")); | |
154 EXPECT_TRUE(ParsePhoneNumber(phone_separator7, "US", | |
155 &country_code, | |
156 &city_code, | |
157 &number, | |
158 &unused_i18n_number)); | |
159 EXPECT_EQ(ASCIIToUTF16("4567890"), number); | |
160 EXPECT_EQ(ASCIIToUTF16("650"), city_code); | |
161 EXPECT_EQ(string16(), country_code); | |
162 | |
163 // Tests for string with over 10 digits. | |
164 // 01 is incorrect prefix in the USA, and if we interpret 011 as prefix, the | |
165 // rest is too short for international number - the parsing should fail. | |
166 string16 phone8(ASCIIToUTF16("0116504567890")); | |
167 EXPECT_FALSE(ParsePhoneNumber(phone8, "US", | |
168 &country_code, | |
169 &city_code, | |
170 &number, | |
171 &unused_i18n_number)); | |
172 EXPECT_EQ(string16(), number); | |
173 EXPECT_EQ(string16(), city_code); | |
174 EXPECT_EQ(string16(), country_code); | |
175 | |
176 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed. | |
177 string16 phone9(ASCIIToUTF16("01116504567890")); | |
178 EXPECT_TRUE(ParsePhoneNumber(phone9, "US", | |
179 &country_code, | |
180 &city_code, | |
181 &number, | |
182 &unused_i18n_number)); | |
183 EXPECT_EQ(ASCIIToUTF16("4567890"), number); | |
184 EXPECT_EQ(ASCIIToUTF16("650"), city_code); | |
185 EXPECT_EQ(ASCIIToUTF16("1"), country_code); | |
186 | |
187 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed. | |
188 string16 phone10(ASCIIToUTF16("01178124567890")); | |
189 EXPECT_TRUE(ParsePhoneNumber(phone10, "US", | |
190 &country_code, | |
191 &city_code, | |
192 &number, | |
193 &unused_i18n_number)); | |
194 EXPECT_EQ(ASCIIToUTF16("4567890"), number); | |
195 EXPECT_EQ(ASCIIToUTF16("812"), city_code); | |
196 EXPECT_EQ(ASCIIToUTF16("7"), country_code); | |
197 | |
198 // Test for string with over 10 digits with separator characters. | |
199 // Should give back phone number, city code, and country code. "011" is | |
200 // US "dial out" code, which is discarded. | |
201 string16 phone11(ASCIIToUTF16("(0111) 650-456.7890")); | |
202 EXPECT_TRUE(ParsePhoneNumber(phone11, "US", | |
203 &country_code, | |
204 &city_code, | |
205 &number, | |
206 &unused_i18n_number)); | |
207 EXPECT_EQ(ASCIIToUTF16("4567890"), number); | |
208 EXPECT_EQ(ASCIIToUTF16("650"), city_code); | |
209 EXPECT_EQ(ASCIIToUTF16("1"), country_code); | |
210 | |
211 // Now try phone from Chech republic - it has 00 dial out code, 420 country | |
212 // code and variable length area codes. | |
213 string16 phone12(ASCIIToUTF16("+420 27-89.10.112")); | |
214 EXPECT_TRUE(ParsePhoneNumber(phone12, "US", | |
215 &country_code, | |
216 &city_code, | |
217 &number, | |
218 &unused_i18n_number)); | |
219 EXPECT_EQ(ASCIIToUTF16("910112"), number); | |
220 EXPECT_EQ(ASCIIToUTF16("278"), city_code); | |
221 EXPECT_EQ(ASCIIToUTF16("420"), country_code); | |
222 | |
223 EXPECT_TRUE(ParsePhoneNumber(phone12, "CZ", | |
224 &country_code, | |
225 &city_code, | |
226 &number, | |
227 &unused_i18n_number)); | |
228 EXPECT_EQ(ASCIIToUTF16("910112"), number); | |
229 EXPECT_EQ(ASCIIToUTF16("278"), city_code); | |
230 EXPECT_EQ(ASCIIToUTF16("420"), country_code); | |
231 | |
232 string16 phone13(ASCIIToUTF16("420 57-89.10.112")); | |
233 EXPECT_FALSE(ParsePhoneNumber(phone13, "US", | |
234 &country_code, | |
235 &city_code, | |
236 &number, | |
237 &unused_i18n_number)); | |
238 EXPECT_TRUE(ParsePhoneNumber(phone13, "CZ", | |
239 &country_code, | |
240 &city_code, | |
241 &number, | |
242 &unused_i18n_number)); | |
243 EXPECT_EQ(ASCIIToUTF16("910112"), number); | |
244 EXPECT_EQ(ASCIIToUTF16("578"), city_code); | |
245 EXPECT_EQ(ASCIIToUTF16("420"), country_code); | |
246 | |
247 string16 phone14(ASCIIToUTF16("1-650-FLOWERS")); | |
248 EXPECT_TRUE(ParsePhoneNumber(phone14, "US", | |
249 &country_code, | |
250 &city_code, | |
251 &number, | |
252 &unused_i18n_number)); | |
253 EXPECT_EQ(ASCIIToUTF16("3569377"), number); | |
254 EXPECT_EQ(ASCIIToUTF16("650"), city_code); | |
255 EXPECT_EQ(ASCIIToUTF16("1"), country_code); | |
256 | |
257 // 800 is not an area code, but the destination code. In our library these | |
258 // codes should be treated the same as area codes. | |
259 string16 phone15(ASCIIToUTF16("1-800-FLOWERS")); | |
260 EXPECT_TRUE(ParsePhoneNumber(phone15, "US", | |
261 &country_code, | |
262 &city_code, | |
263 &number, | |
264 &unused_i18n_number)); | |
265 EXPECT_EQ(ASCIIToUTF16("3569377"), number); | |
266 EXPECT_EQ(ASCIIToUTF16("800"), city_code); | |
267 EXPECT_EQ(ASCIIToUTF16("1"), country_code); | |
268 } | |
269 | |
270 TEST(PhoneNumberI18NTest, ConstructPhoneNumber) { | |
271 string16 number; | |
272 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"), | |
273 ASCIIToUTF16("650"), | |
274 ASCIIToUTF16("2345678"), | |
275 "US", | |
276 &number)); | |
277 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678")); | |
278 EXPECT_TRUE(ConstructPhoneNumber(string16(), | |
279 ASCIIToUTF16("650"), | |
280 ASCIIToUTF16("2345678"), | |
281 "US", | |
282 &number)); | |
283 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678")); | |
284 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"), | |
285 string16(), | |
286 ASCIIToUTF16("6502345678"), | |
287 "US", | |
288 &number)); | |
289 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678")); | |
290 EXPECT_TRUE(ConstructPhoneNumber(string16(), | |
291 string16(), | |
292 ASCIIToUTF16("6502345678"), | |
293 "US", | |
294 &number)); | |
295 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678")); | |
296 | |
297 EXPECT_FALSE(ConstructPhoneNumber(string16(), | |
298 ASCIIToUTF16("650"), | |
299 ASCIIToUTF16("234567890"), | |
300 "US", | |
301 &number)); | |
302 EXPECT_EQ(number, string16()); | |
303 // Italian number | |
304 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("39"), | |
305 ASCIIToUTF16("347"), | |
306 ASCIIToUTF16("2345678"), | |
307 "IT", | |
308 &number)); | |
309 EXPECT_EQ(number, ASCIIToUTF16("+39 347 234 5678")); | |
310 EXPECT_TRUE(ConstructPhoneNumber(string16(), | |
311 ASCIIToUTF16("347"), | |
312 ASCIIToUTF16("2345678"), | |
313 "IT", | |
314 &number)); | |
315 EXPECT_EQ(number, ASCIIToUTF16("347 234 5678")); | |
316 // German number. | |
317 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"), | |
318 ASCIIToUTF16("024"), | |
319 ASCIIToUTF16("2345678901"), | |
320 "DE", | |
321 &number)); | |
322 EXPECT_EQ(number, ASCIIToUTF16("+49 2423/45678901")); | |
323 EXPECT_TRUE(ConstructPhoneNumber(string16(), | |
324 ASCIIToUTF16("024"), | |
325 ASCIIToUTF16("2345678901"), | |
326 "DE", | |
327 &number)); | |
328 EXPECT_EQ(number, ASCIIToUTF16("02423/45678901")); | |
329 } | |
330 | |
331 TEST(PhoneNumberI18NTest, PhoneNumbersMatch) { | |
332 // Same numbers, defined country code. | |
333 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), | |
334 ASCIIToUTF16("4158889999"), | |
335 "US")); | |
336 // Same numbers, undefined country code. | |
337 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), | |
338 ASCIIToUTF16("4158889999"), | |
339 "")); | |
340 | |
341 // Numbers differ by country code only. | |
342 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), | |
343 ASCIIToUTF16("4158889999"), | |
344 "US")); | |
345 | |
346 // Same numbers, different formats. | |
347 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), | |
348 ASCIIToUTF16("415-888-9999"), | |
349 "US")); | |
350 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), | |
351 ASCIIToUTF16("(415)888-9999"), | |
352 "US")); | |
353 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), | |
354 ASCIIToUTF16("415 888 9999"), | |
355 "US")); | |
356 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), | |
357 ASCIIToUTF16("415 TUV WXYZ"), | |
358 "US")); | |
359 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("1(415)888-99-99"), | |
360 ASCIIToUTF16("+14158889999"), | |
361 "US")); | |
362 | |
363 // Partial matches don't count. | |
364 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), | |
365 ASCIIToUTF16("8889999"), | |
366 "US")); | |
367 | |
368 // Different numbers don't match. | |
369 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), | |
370 ASCIIToUTF16("1415888"), | |
371 "US")); | |
372 } | |
OLD | NEW |