blob: 8e255200a0247bf7756b395e823dc735537e733b [file] [log] [blame]
[email protected]a3e40032012-05-29 23:00:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c50a9482011-05-06 22:53:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]05871b32011-10-22 00:46:185#include "base/message_loop.h"
[email protected]7a8edaf2011-11-28 20:58:486#include "base/string16.h"
[email protected]c50a9482011-05-06 22:53:347#include "base/utf_string_conversions.h"
[email protected]e4b2fa32013-03-09 22:56:568#include "components/autofill/browser/phone_number_i18n.h"
[email protected]e97882f2012-06-04 02:23:179#include "content/public/test/test_browser_thread.h"
[email protected]c50a9482011-05-06 22:53:3410#include "testing/gtest/include/gtest/gtest.h"
[email protected]7df904d2013-01-15 22:33:2211#include "third_party/libphonenumber/src/phonenumber_api.h"
[email protected]c50a9482011-05-06 22:53:3412
[email protected]5b37cb722011-05-10 18:24:5613using autofill_i18n::NormalizePhoneNumber;
14using autofill_i18n::ParsePhoneNumber;
15using autofill_i18n::ConstructPhoneNumber;
[email protected]5b37cb722011-05-10 18:24:5616using autofill_i18n::PhoneNumbersMatch;
[email protected]631bb742011-11-02 11:29:3917using content::BrowserThread;
[email protected]5b37cb722011-05-10 18:24:5618
[email protected]7df904d2013-01-15 22:33:2219TEST(PhoneNumberI18NTest, NormalizePhoneNumber) {
[email protected]f4672472011-05-27 07:21:0120 // "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"));
[email protected]5b37cb722011-05-10 18:24:5625
[email protected]f4672472011-05-27 07:21:0126 // 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"));
[email protected]5b37cb722011-05-10 18:24:5630
[email protected]f4672472011-05-27 07:21:0131 string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5"));
32 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255"));
[email protected]5b37cb722011-05-10 18:24:5633
34 string16 phone4(UTF8ToUTF16("+1(650)2346789"));
[email protected]f4672472011-05-27 07:21:0135 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789"));
[email protected]5b37cb722011-05-10 18:24:5636
37 string16 phone5(UTF8ToUTF16("6502346789"));
[email protected]f4672472011-05-27 07:21:0138 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789"));
[email protected]5b37cb722011-05-10 18:24:5639}
40
[email protected]7df904d2013-01-15 22:33:2241TEST(PhoneNumberI18NTest, ParsePhoneNumber) {
[email protected]5b37cb722011-05-10 18:24:5642 string16 number;
43 string16 city_code;
44 string16 country_code;
[email protected]7df904d2013-01-15 22:33:2245 i18n::phonenumbers::PhoneNumber unused_i18n_number;
[email protected]5b37cb722011-05-10 18:24:5646
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,
[email protected]7df904d2013-01-15 22:33:2252 &number,
53 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:5654 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,
[email protected]7df904d2013-01-15 22:33:2263 &number,
64 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:5665 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,
[email protected]7df904d2013-01-15 22:33:2275 &number,
76 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:5677 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,
[email protected]7df904d2013-01-15 22:33:2286 &number,
87 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:5688 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,
[email protected]7df904d2013-01-15 22:33:2298 &number,
99 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56100 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,
[email protected]7df904d2013-01-15 22:33:22111 &number,
112 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56113 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,
[email protected]7df904d2013-01-15 22:33:22124 &number,
125 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56126 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,
[email protected]7df904d2013-01-15 22:33:22135 &number,
136 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56137 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,
[email protected]7df904d2013-01-15 22:33:22145 &number,
146 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56147 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,
[email protected]7df904d2013-01-15 22:33:22157 &number,
158 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56159 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,
[email protected]7df904d2013-01-15 22:33:22170 &number,
171 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56172 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,
[email protected]7df904d2013-01-15 22:33:22181 &number,
182 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56183 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,
[email protected]7df904d2013-01-15 22:33:22192 &number,
193 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56194 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,
[email protected]7df904d2013-01-15 22:33:22205 &number,
206 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56207 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,
[email protected]7df904d2013-01-15 22:33:22217 &number,
218 &unused_i18n_number));
[email protected]f4672472011-05-27 07:21:01219 EXPECT_EQ(ASCIIToUTF16("910112"), number);
220 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
[email protected]5b37cb722011-05-10 18:24:56221 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
222
223 EXPECT_TRUE(ParsePhoneNumber(phone12, "CZ",
224 &country_code,
225 &city_code,
[email protected]7df904d2013-01-15 22:33:22226 &number,
227 &unused_i18n_number));
[email protected]f4672472011-05-27 07:21:01228 EXPECT_EQ(ASCIIToUTF16("910112"), number);
229 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
[email protected]5b37cb722011-05-10 18:24:56230 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,
[email protected]7df904d2013-01-15 22:33:22236 &number,
237 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56238 EXPECT_TRUE(ParsePhoneNumber(phone13, "CZ",
239 &country_code,
240 &city_code,
[email protected]7df904d2013-01-15 22:33:22241 &number,
242 &unused_i18n_number));
[email protected]f4672472011-05-27 07:21:01243 EXPECT_EQ(ASCIIToUTF16("910112"), number);
244 EXPECT_EQ(ASCIIToUTF16("578"), city_code);
[email protected]5b37cb722011-05-10 18:24:56245 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,
[email protected]7df904d2013-01-15 22:33:22251 &number,
252 &unused_i18n_number));
[email protected]5b37cb722011-05-10 18:24:56253 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
254 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
255 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
[email protected]f4672472011-05-27 07:21:01256
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,
[email protected]7df904d2013-01-15 22:33:22263 &number,
264 &unused_i18n_number));
[email protected]f4672472011-05-27 07:21:01265 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
266 EXPECT_EQ(ASCIIToUTF16("800"), city_code);
267 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
[email protected]5b37cb722011-05-10 18:24:56268}
269
[email protected]7df904d2013-01-15 22:33:22270TEST(PhoneNumberI18NTest, ConstructPhoneNumber) {
[email protected]5b37cb722011-05-10 18:24:56271 string16 number;
272 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
273 ASCIIToUTF16("650"),
274 ASCIIToUTF16("2345678"),
275 "US",
[email protected]5b37cb722011-05-10 18:24:56276 &number));
277 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
[email protected]7a8edaf2011-11-28 20:58:48278 EXPECT_TRUE(ConstructPhoneNumber(string16(),
[email protected]5b37cb722011-05-10 18:24:56279 ASCIIToUTF16("650"),
280 ASCIIToUTF16("2345678"),
281 "US",
[email protected]7df904d2013-01-15 22:33:22282 &number));
283 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678"));
284 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
285 string16(),
286 ASCIIToUTF16("6502345678"),
287 "US",
[email protected]5b37cb722011-05-10 18:24:56288 &number));
289 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
[email protected]7a8edaf2011-11-28 20:58:48290 EXPECT_TRUE(ConstructPhoneNumber(string16(),
291 string16(),
[email protected]5b37cb722011-05-10 18:24:56292 ASCIIToUTF16("6502345678"),
293 "US",
[email protected]5b37cb722011-05-10 18:24:56294 &number));
[email protected]7df904d2013-01-15 22:33:22295 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678"));
[email protected]5b37cb722011-05-10 18:24:56296
[email protected]7a8edaf2011-11-28 20:58:48297 EXPECT_FALSE(ConstructPhoneNumber(string16(),
[email protected]5b37cb722011-05-10 18:24:56298 ASCIIToUTF16("650"),
299 ASCIIToUTF16("234567890"),
300 "US",
[email protected]5b37cb722011-05-10 18:24:56301 &number));
302 EXPECT_EQ(number, string16());
303 // Italian number
[email protected]7df904d2013-01-15 22:33:22304 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("39"),
305 ASCIIToUTF16("347"),
306 ASCIIToUTF16("2345678"),
307 "IT",
308 &number));
309 EXPECT_EQ(number, ASCIIToUTF16("+39 347 234 5678"));
[email protected]7a8edaf2011-11-28 20:58:48310 EXPECT_TRUE(ConstructPhoneNumber(string16(),
[email protected]11c2bdd12011-05-23 18:24:32311 ASCIIToUTF16("347"),
312 ASCIIToUTF16("2345678"),
313 "IT",
[email protected]5b37cb722011-05-10 18:24:56314 &number));
[email protected]7df904d2013-01-15 22:33:22315 EXPECT_EQ(number, ASCIIToUTF16("347 234 5678"));
[email protected]11c2bdd12011-05-23 18:24:32316 // German number.
317 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"),
318 ASCIIToUTF16("024"),
319 ASCIIToUTF16("2345678901"),
320 "DE",
[email protected]5b37cb722011-05-10 18:24:56321 &number));
[email protected]7df904d2013-01-15 22:33:22322 EXPECT_EQ(number, ASCIIToUTF16("+49 2423/45678901"));
323 EXPECT_TRUE(ConstructPhoneNumber(string16(),
[email protected]11c2bdd12011-05-23 18:24:32324 ASCIIToUTF16("024"),
325 ASCIIToUTF16("2345678901"),
326 "DE",
[email protected]11c2bdd12011-05-23 18:24:32327 &number));
[email protected]7df904d2013-01-15 22:33:22328 EXPECT_EQ(number, ASCIIToUTF16("02423/45678901"));
[email protected]5b37cb722011-05-10 18:24:56329}
330
[email protected]7df904d2013-01-15 22:33:22331TEST(PhoneNumberI18NTest, PhoneNumbersMatch) {
[email protected]c50a9482011-05-06 22:53:34332 // 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"));
[email protected]944a5cd82011-10-19 23:42:29359 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("1(415)888-99-99"),
360 ASCIIToUTF16("+14158889999"),
361 "US"));
[email protected]c50a9482011-05-06 22:53:34362
363 // Partial matches don't count.
364 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
365 ASCIIToUTF16("8889999"),
366 "US"));
[email protected]944a5cd82011-10-19 23:42:29367
368 // Different numbers don't match.
369 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
370 ASCIIToUTF16("1415888"),
371 "US"));
[email protected]c50a9482011-05-06 22:53:34372}