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/message_loop/message_loop.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/browser/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"
13 using content::BrowserThread;
17 using i18n::NormalizePhoneNumber;
18 using i18n::ParsePhoneNumber;
19 using i18n::ConstructPhoneNumber;
20 using i18n::PhoneNumbersMatch;
22 TEST(PhoneNumberI18NTest, NormalizePhoneNumber) {
24 base::string16 phone1(UTF8ToUTF16(
25 "\xEF\xBC\x91\xEF\xBC\x96\xEF\xBC\x95\xEF\xBC\x90"
26 "\xEF\xBC\x97\xEF\xBC\x94\xEF\xBC\x99\xEF\xBC\x98"
27 "\xEF\xBC\x93\xEF\xBC\x92\xEF\xBC\x93"));
28 EXPECT_EQ(NormalizePhoneNumber(phone1, "US"), ASCIIToUTF16("16507498323"));
30 // Devanagari script digits.
31 base::string16 phone2(UTF8ToUTF16(
32 "\xD9\xA1\xD9\xA6\xD9\xA5\xD9\xA0\xD9\xA8\xD9\xA3"
33 "\xD9\xA2\xD9\xA3\xD9\xA7\xD9\xA4\xD9\xA9"));
34 EXPECT_EQ(NormalizePhoneNumber(phone2, "US"), ASCIIToUTF16("16508323749"));
36 base::string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5"));
37 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255"));
39 base::string16 phone4(UTF8ToUTF16("+1(650)2346789"));
40 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789"));
42 base::string16 phone5(UTF8ToUTF16("6502346789"));
43 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789"));
46 TEST(PhoneNumberI18NTest, ParsePhoneNumber) {
47 base::string16 number;
48 base::string16 city_code;
49 base::string16 country_code;
50 ::i18n::phonenumbers::PhoneNumber unused_i18n_number;
52 // Test for empty string. Should give back empty strings.
53 base::string16 phone0;
54 EXPECT_FALSE(ParsePhoneNumber(phone0, "US",
58 &unused_i18n_number));
59 EXPECT_EQ(base::string16(), number);
60 EXPECT_EQ(base::string16(), city_code);
61 EXPECT_EQ(base::string16(), country_code);
63 // Test for string with less than 7 digits. Should give back empty strings.
64 base::string16 phone1(ASCIIToUTF16("1234"));
65 EXPECT_FALSE(ParsePhoneNumber(phone1, "US",
69 &unused_i18n_number));
70 EXPECT_EQ(base::string16(), number);
71 EXPECT_EQ(base::string16(), city_code);
72 EXPECT_EQ(base::string16(), country_code);
74 // Test for string with exactly 7 digits.
75 // Not a valid number - starts with 1
76 base::string16 phone2(ASCIIToUTF16("1234567"));
77 EXPECT_FALSE(ParsePhoneNumber(phone2, "US",
81 &unused_i18n_number));
82 EXPECT_EQ(base::string16(), number);
83 EXPECT_EQ(base::string16(), city_code);
84 EXPECT_EQ(base::string16(), country_code);
86 // Not a valid number - does not have area code.
87 base::string16 phone3(ASCIIToUTF16("2234567"));
88 EXPECT_FALSE(ParsePhoneNumber(phone3, "US",
92 &unused_i18n_number));
93 EXPECT_EQ(base::string16(), number);
94 EXPECT_EQ(base::string16(), city_code);
95 EXPECT_EQ(base::string16(), country_code);
97 // Test for string with greater than 7 digits but less than 10 digits.
98 // Should fail parsing in US.
99 base::string16 phone4(ASCIIToUTF16("123456789"));
100 EXPECT_FALSE(ParsePhoneNumber(phone4, "US",
104 &unused_i18n_number));
105 EXPECT_EQ(base::string16(), number);
106 EXPECT_EQ(base::string16(), city_code);
107 EXPECT_EQ(base::string16(), country_code);
109 // Test for string with greater than 7 digits but less than 10 digits and
111 // Should fail parsing in US.
112 base::string16 phone_separator4(ASCIIToUTF16("12.345-6789"));
113 EXPECT_FALSE(ParsePhoneNumber(phone_separator4, "US",
117 &unused_i18n_number));
118 EXPECT_EQ(base::string16(), number);
119 EXPECT_EQ(base::string16(), city_code);
120 EXPECT_EQ(base::string16(), country_code);
122 // Test for string with exactly 10 digits.
123 // Should give back phone number and city code.
124 // This one going to fail because of the incorrect area code.
125 base::string16 phone5(ASCIIToUTF16("1234567890"));
126 EXPECT_FALSE(ParsePhoneNumber(phone5, "US",
130 &unused_i18n_number));
131 EXPECT_EQ(base::string16(), number);
132 EXPECT_EQ(base::string16(), city_code);
133 EXPECT_EQ(base::string16(), country_code);
135 base::string16 phone6(ASCIIToUTF16("6501567890"));
136 // This one going to fail because of the incorrect number (starts with 1).
137 EXPECT_FALSE(ParsePhoneNumber(phone6, "US",
141 &unused_i18n_number));
142 EXPECT_EQ(base::string16(), number);
143 EXPECT_EQ(base::string16(), city_code);
144 EXPECT_EQ(base::string16(), country_code);
146 base::string16 phone7(ASCIIToUTF16("6504567890"));
147 EXPECT_TRUE(ParsePhoneNumber(phone7, "US",
151 &unused_i18n_number));
152 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
153 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
154 EXPECT_EQ(base::string16(), country_code);
156 // Test for string with exactly 10 digits and separators.
157 // Should give back phone number and city code.
158 base::string16 phone_separator7(ASCIIToUTF16("(650) 456-7890"));
159 EXPECT_TRUE(ParsePhoneNumber(phone_separator7, "US",
163 &unused_i18n_number));
164 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
165 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
166 EXPECT_EQ(base::string16(), country_code);
168 // Tests for string with over 10 digits.
169 // 01 is incorrect prefix in the USA, and if we interpret 011 as prefix, the
170 // rest is too short for international number - the parsing should fail.
171 base::string16 phone8(ASCIIToUTF16("0116504567890"));
172 EXPECT_FALSE(ParsePhoneNumber(phone8, "US",
176 &unused_i18n_number));
177 EXPECT_EQ(base::string16(), number);
178 EXPECT_EQ(base::string16(), city_code);
179 EXPECT_EQ(base::string16(), country_code);
181 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed.
182 base::string16 phone9(ASCIIToUTF16("01116504567890"));
183 EXPECT_TRUE(ParsePhoneNumber(phone9, "US",
187 &unused_i18n_number));
188 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
189 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
190 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
192 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed.
193 base::string16 phone10(ASCIIToUTF16("01178124567890"));
194 EXPECT_TRUE(ParsePhoneNumber(phone10, "US",
198 &unused_i18n_number));
199 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
200 EXPECT_EQ(ASCIIToUTF16("812"), city_code);
201 EXPECT_EQ(ASCIIToUTF16("7"), country_code);
203 // Test for string with over 10 digits with separator characters.
204 // Should give back phone number, city code, and country code. "011" is
205 // US "dial out" code, which is discarded.
206 base::string16 phone11(ASCIIToUTF16("(0111) 650-456.7890"));
207 EXPECT_TRUE(ParsePhoneNumber(phone11, "US",
211 &unused_i18n_number));
212 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
213 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
214 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
216 // Now try phone from Chech republic - it has 00 dial out code, 420 country
217 // code and variable length area codes.
218 base::string16 phone12(ASCIIToUTF16("+420 27-89.10.112"));
219 EXPECT_TRUE(ParsePhoneNumber(phone12, "US",
223 &unused_i18n_number));
224 EXPECT_EQ(ASCIIToUTF16("910112"), number);
225 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
226 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
228 EXPECT_TRUE(ParsePhoneNumber(phone12, "CZ",
232 &unused_i18n_number));
233 EXPECT_EQ(ASCIIToUTF16("910112"), number);
234 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
235 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
237 base::string16 phone13(ASCIIToUTF16("420 57-89.10.112"));
238 EXPECT_FALSE(ParsePhoneNumber(phone13, "US",
242 &unused_i18n_number));
243 EXPECT_TRUE(ParsePhoneNumber(phone13, "CZ",
247 &unused_i18n_number));
248 EXPECT_EQ(ASCIIToUTF16("910112"), number);
249 EXPECT_EQ(ASCIIToUTF16("578"), city_code);
250 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
252 base::string16 phone14(ASCIIToUTF16("1-650-FLOWERS"));
253 EXPECT_TRUE(ParsePhoneNumber(phone14, "US",
257 &unused_i18n_number));
258 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
259 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
260 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
262 // 800 is not an area code, but the destination code. In our library these
263 // codes should be treated the same as area codes.
264 base::string16 phone15(ASCIIToUTF16("1-800-FLOWERS"));
265 EXPECT_TRUE(ParsePhoneNumber(phone15, "US",
269 &unused_i18n_number));
270 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
271 EXPECT_EQ(ASCIIToUTF16("800"), city_code);
272 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
275 TEST(PhoneNumberI18NTest, ConstructPhoneNumber) {
276 base::string16 number;
277 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
279 ASCIIToUTF16("2345678"),
282 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
283 EXPECT_TRUE(ConstructPhoneNumber(base::string16(),
285 ASCIIToUTF16("2345678"),
288 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678"));
289 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
291 ASCIIToUTF16("6502345678"),
294 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
295 EXPECT_TRUE(ConstructPhoneNumber(base::string16(),
297 ASCIIToUTF16("6502345678"),
300 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678"));
302 EXPECT_FALSE(ConstructPhoneNumber(base::string16(),
304 ASCIIToUTF16("234567890"),
307 EXPECT_EQ(number, base::string16());
309 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("39"),
311 ASCIIToUTF16("2345678"),
314 EXPECT_EQ(number, ASCIIToUTF16("+39 347 234 5678"));
315 EXPECT_TRUE(ConstructPhoneNumber(base::string16(),
317 ASCIIToUTF16("2345678"),
320 EXPECT_EQ(number, ASCIIToUTF16("347 234 5678"));
322 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"),
324 ASCIIToUTF16("2345678901"),
327 EXPECT_EQ(number, ASCIIToUTF16("+49 2423 45678901"));
328 EXPECT_TRUE(ConstructPhoneNumber(base::string16(),
330 ASCIIToUTF16("2345678901"),
333 EXPECT_EQ(number, ASCIIToUTF16("02423 45678901"));
336 TEST(PhoneNumberI18NTest, PhoneNumbersMatch) {
337 // Same numbers, defined country code.
338 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
339 ASCIIToUTF16("4158889999"),
342 // Same numbers, undefined country code.
343 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
344 ASCIIToUTF16("4158889999"),
348 // Numbers differ by country code only.
349 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
350 ASCIIToUTF16("4158889999"),
354 // Same numbers, different formats.
355 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
356 ASCIIToUTF16("415-888-9999"),
359 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
360 ASCIIToUTF16("(415)888-9999"),
363 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
364 ASCIIToUTF16("415 888 9999"),
367 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
368 ASCIIToUTF16("415 TUV WXYZ"),
371 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("1(415)888-99-99"),
372 ASCIIToUTF16("+14158889999"),
376 // Partial matches don't count.
377 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
378 ASCIIToUTF16("8889999"),
382 // Different numbers don't match.
383 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
384 ASCIIToUTF16("1415888"),
389 } // namespace autofill