blob: d75c320c1ec8beda60c8bbc0a6d58867882cd0de [file] [log] [blame]
[email protected]c50a9482011-05-06 22:53:341// 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
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"
8#include "chrome/browser/autofill/phone_number_i18n.h"
[email protected]c38831a12011-10-28 12:44:499#include "content/test/test_browser_thread.h"
[email protected]c50a9482011-05-06 22:53:3410#include "testing/gtest/include/gtest/gtest.h"
11
[email protected]5b37cb722011-05-10 18:24:5612using autofill_i18n::NormalizePhoneNumber;
13using autofill_i18n::ParsePhoneNumber;
14using autofill_i18n::ConstructPhoneNumber;
[email protected]5b37cb722011-05-10 18:24:5615using autofill_i18n::PhoneNumbersMatch;
[email protected]631bb742011-11-02 11:29:3916using content::BrowserThread;
[email protected]5b37cb722011-05-10 18:24:5617
[email protected]05871b32011-10-22 00:46:1818class PhoneNumberI18NTest : public testing::Test {
19 public:
20 // In order to access the application locale -- which the tested functions do
21 // internally -- this test must run on the UI thread.
22 PhoneNumberI18NTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
23
24 private:
25 MessageLoopForUI message_loop_;
[email protected]c38831a12011-10-28 12:44:4926 content::TestBrowserThread ui_thread_;
[email protected]05871b32011-10-22 00:46:1827
28 DISALLOW_COPY_AND_ASSIGN(PhoneNumberI18NTest);
29};
[email protected]a5f9f372011-08-03 23:02:2130
31TEST_F(PhoneNumberI18NTest, NormalizePhoneNumber) {
[email protected]f4672472011-05-27 07:21:0132 // "Large" digits.
33 string16 phone1(UTF8ToUTF16("\xEF\xBC\x91\xEF\xBC\x96\xEF\xBC\x95\xEF\xBC\x90"
34 "\xEF\xBC\x97\xEF\xBC\x94\xEF\xBC\x99\xEF\xBC\x98"
35 "\xEF\xBC\x93\xEF\xBC\x92\xEF\xBC\x93"));
36 EXPECT_EQ(NormalizePhoneNumber(phone1, "US"), ASCIIToUTF16("16507498323"));
[email protected]5b37cb722011-05-10 18:24:5637
[email protected]f4672472011-05-27 07:21:0138 // Devanagari script digits.
39 string16 phone2(UTF8ToUTF16("\xD9\xA1\xD9\xA6\xD9\xA5\xD9\xA0\xD9\xA8\xD9\xA3"
40 "\xD9\xA2\xD9\xA3\xD9\xA7\xD9\xA4\xD9\xA9"));
41 EXPECT_EQ(NormalizePhoneNumber(phone2, "US"), ASCIIToUTF16("16508323749"));
[email protected]5b37cb722011-05-10 18:24:5642
[email protected]f4672472011-05-27 07:21:0143 string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5"));
44 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255"));
[email protected]5b37cb722011-05-10 18:24:5645
46 string16 phone4(UTF8ToUTF16("+1(650)2346789"));
[email protected]f4672472011-05-27 07:21:0147 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789"));
[email protected]5b37cb722011-05-10 18:24:5648
49 string16 phone5(UTF8ToUTF16("6502346789"));
[email protected]f4672472011-05-27 07:21:0150 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789"));
[email protected]5b37cb722011-05-10 18:24:5651}
52
[email protected]a5f9f372011-08-03 23:02:2153TEST_F(PhoneNumberI18NTest, ParsePhoneNumber) {
[email protected]5b37cb722011-05-10 18:24:5654 string16 number;
55 string16 city_code;
56 string16 country_code;
57
58 // Test for empty string. Should give back empty strings.
59 string16 phone0;
60 EXPECT_FALSE(ParsePhoneNumber(phone0, "US",
61 &country_code,
62 &city_code,
63 &number));
64 EXPECT_EQ(string16(), number);
65 EXPECT_EQ(string16(), city_code);
66 EXPECT_EQ(string16(), country_code);
67
68 // Test for string with less than 7 digits. Should give back empty strings.
69 string16 phone1(ASCIIToUTF16("1234"));
70 EXPECT_FALSE(ParsePhoneNumber(phone1, "US",
71 &country_code,
72 &city_code,
73 &number));
74 EXPECT_EQ(string16(), number);
75 EXPECT_EQ(string16(), city_code);
76 EXPECT_EQ(string16(), country_code);
77
78 // Test for string with exactly 7 digits.
79 // Not a valid number - starts with 1
80 string16 phone2(ASCIIToUTF16("1234567"));
81 EXPECT_FALSE(ParsePhoneNumber(phone2, "US",
82 &country_code,
83 &city_code,
84 &number));
85 EXPECT_EQ(string16(), number);
86 EXPECT_EQ(string16(), city_code);
87 EXPECT_EQ(string16(), country_code);
88
89 // Not a valid number - does not have area code.
90 string16 phone3(ASCIIToUTF16("2234567"));
91 EXPECT_FALSE(ParsePhoneNumber(phone3, "US",
92 &country_code,
93 &city_code,
94 &number));
95 EXPECT_EQ(string16(), number);
96 EXPECT_EQ(string16(), city_code);
97 EXPECT_EQ(string16(), country_code);
98
99 // Test for string with greater than 7 digits but less than 10 digits.
100 // Should fail parsing in US.
101 string16 phone4(ASCIIToUTF16("123456789"));
102 EXPECT_FALSE(ParsePhoneNumber(phone4, "US",
103 &country_code,
104 &city_code,
105 &number));
106 EXPECT_EQ(string16(), number);
107 EXPECT_EQ(string16(), city_code);
108 EXPECT_EQ(string16(), country_code);
109
110 // Test for string with greater than 7 digits but less than 10 digits and
111 // separators.
112 // Should fail parsing in US.
113 string16 phone_separator4(ASCIIToUTF16("12.345-6789"));
114 EXPECT_FALSE(ParsePhoneNumber(phone_separator4, "US",
115 &country_code,
116 &city_code,
117 &number));
118 EXPECT_EQ(string16(), number);
119 EXPECT_EQ(string16(), city_code);
120 EXPECT_EQ(string16(), country_code);
121
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 string16 phone5(ASCIIToUTF16("1234567890"));
126 EXPECT_FALSE(ParsePhoneNumber(phone5, "US",
127 &country_code,
128 &city_code,
129 &number));
130 EXPECT_EQ(string16(), number);
131 EXPECT_EQ(string16(), city_code);
132 EXPECT_EQ(string16(), country_code);
133
134 string16 phone6(ASCIIToUTF16("6501567890"));
135 // This one going to fail because of the incorrect number (starts with 1).
136 EXPECT_FALSE(ParsePhoneNumber(phone6, "US",
137 &country_code,
138 &city_code,
139 &number));
140 EXPECT_EQ(string16(), number);
141 EXPECT_EQ(string16(), city_code);
142 EXPECT_EQ(string16(), country_code);
143
144 string16 phone7(ASCIIToUTF16("6504567890"));
145 EXPECT_TRUE(ParsePhoneNumber(phone7, "US",
146 &country_code,
147 &city_code,
148 &number));
149 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
150 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
151 EXPECT_EQ(string16(), country_code);
152
153 // Test for string with exactly 10 digits and separators.
154 // Should give back phone number and city code.
155 string16 phone_separator7(ASCIIToUTF16("(650) 456-7890"));
156 EXPECT_TRUE(ParsePhoneNumber(phone_separator7, "US",
157 &country_code,
158 &city_code,
159 &number));
160 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
161 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
162 EXPECT_EQ(string16(), country_code);
163
164 // Tests for string with over 10 digits.
165 // 01 is incorrect prefix in the USA, and if we interpret 011 as prefix, the
166 // rest is too short for international number - the parsing should fail.
167 string16 phone8(ASCIIToUTF16("0116504567890"));
168 EXPECT_FALSE(ParsePhoneNumber(phone8, "US",
169 &country_code,
170 &city_code,
171 &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 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
183 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
184 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
185
186 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed.
187 string16 phone10(ASCIIToUTF16("01178124567890"));
188 EXPECT_TRUE(ParsePhoneNumber(phone10, "US",
189 &country_code,
190 &city_code,
191 &number));
192 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
193 EXPECT_EQ(ASCIIToUTF16("812"), city_code);
194 EXPECT_EQ(ASCIIToUTF16("7"), country_code);
195
196 // Test for string with over 10 digits with separator characters.
197 // Should give back phone number, city code, and country code. "011" is
198 // US "dial out" code, which is discarded.
199 string16 phone11(ASCIIToUTF16("(0111) 650-456.7890"));
200 EXPECT_TRUE(ParsePhoneNumber(phone11, "US",
201 &country_code,
202 &city_code,
203 &number));
204 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
205 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
206 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
207
208 // Now try phone from Chech republic - it has 00 dial out code, 420 country
209 // code and variable length area codes.
210 string16 phone12(ASCIIToUTF16("+420 27-89.10.112"));
211 EXPECT_TRUE(ParsePhoneNumber(phone12, "US",
212 &country_code,
213 &city_code,
214 &number));
[email protected]f4672472011-05-27 07:21:01215 EXPECT_EQ(ASCIIToUTF16("910112"), number);
216 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
[email protected]5b37cb722011-05-10 18:24:56217 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
218
219 EXPECT_TRUE(ParsePhoneNumber(phone12, "CZ",
220 &country_code,
221 &city_code,
222 &number));
[email protected]f4672472011-05-27 07:21:01223 EXPECT_EQ(ASCIIToUTF16("910112"), number);
224 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
[email protected]5b37cb722011-05-10 18:24:56225 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
226
227 string16 phone13(ASCIIToUTF16("420 57-89.10.112"));
228 EXPECT_FALSE(ParsePhoneNumber(phone13, "US",
229 &country_code,
230 &city_code,
231 &number));
232 EXPECT_TRUE(ParsePhoneNumber(phone13, "CZ",
233 &country_code,
234 &city_code,
235 &number));
[email protected]f4672472011-05-27 07:21:01236 EXPECT_EQ(ASCIIToUTF16("910112"), number);
237 EXPECT_EQ(ASCIIToUTF16("578"), city_code);
[email protected]5b37cb722011-05-10 18:24:56238 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
239
240 string16 phone14(ASCIIToUTF16("1-650-FLOWERS"));
241 EXPECT_TRUE(ParsePhoneNumber(phone14, "US",
242 &country_code,
243 &city_code,
244 &number));
245 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
246 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
247 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
[email protected]f4672472011-05-27 07:21:01248
249 // 800 is not an area code, but the destination code. In our library these
250 // codes should be treated the same as area codes.
251 string16 phone15(ASCIIToUTF16("1-800-FLOWERS"));
252 EXPECT_TRUE(ParsePhoneNumber(phone15, "US",
253 &country_code,
254 &city_code,
255 &number));
256 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
257 EXPECT_EQ(ASCIIToUTF16("800"), city_code);
258 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
[email protected]5b37cb722011-05-10 18:24:56259}
260
[email protected]a5f9f372011-08-03 23:02:21261TEST_F(PhoneNumberI18NTest, ConstructPhoneNumber) {
[email protected]5b37cb722011-05-10 18:24:56262 string16 number;
263 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
264 ASCIIToUTF16("650"),
265 ASCIIToUTF16("2345678"),
266 "US",
267 autofill_i18n::E164,
268 &number));
269 EXPECT_EQ(number, ASCIIToUTF16("+16502345678"));
270 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
271 ASCIIToUTF16("650"),
272 ASCIIToUTF16("2345678"),
273 "US",
274 autofill_i18n::INTERNATIONAL,
275 &number));
276 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
277 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
278 ASCIIToUTF16("650"),
279 ASCIIToUTF16("2345678"),
280 "US",
281 autofill_i18n::NATIONAL,
282 &number));
283 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678"));
284 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
285 ASCIIToUTF16("650"),
286 ASCIIToUTF16("2345678"),
287 "US",
288 autofill_i18n::RFC3966,
289 &number));
290 EXPECT_EQ(number, ASCIIToUTF16("+1-650-234-5678"));
[email protected]7a8edaf2011-11-28 20:58:48291 EXPECT_TRUE(ConstructPhoneNumber(string16(),
[email protected]5b37cb722011-05-10 18:24:56292 ASCIIToUTF16("650"),
293 ASCIIToUTF16("2345678"),
294 "US",
295 autofill_i18n::INTERNATIONAL,
296 &number));
297 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
[email protected]7a8edaf2011-11-28 20:58:48298 EXPECT_TRUE(ConstructPhoneNumber(string16(),
299 string16(),
[email protected]5b37cb722011-05-10 18:24:56300 ASCIIToUTF16("6502345678"),
301 "US",
302 autofill_i18n::INTERNATIONAL,
303 &number));
304 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
305
[email protected]7a8edaf2011-11-28 20:58:48306 EXPECT_FALSE(ConstructPhoneNumber(string16(),
[email protected]5b37cb722011-05-10 18:24:56307 ASCIIToUTF16("650"),
308 ASCIIToUTF16("234567890"),
309 "US",
310 autofill_i18n::INTERNATIONAL,
311 &number));
312 EXPECT_EQ(number, string16());
313 // Italian number
[email protected]7a8edaf2011-11-28 20:58:48314 EXPECT_TRUE(ConstructPhoneNumber(string16(),
[email protected]11c2bdd12011-05-23 18:24:32315 ASCIIToUTF16("347"),
316 ASCIIToUTF16("2345678"),
317 "IT",
[email protected]5b37cb722011-05-10 18:24:56318 autofill_i18n::INTERNATIONAL,
319 &number));
[email protected]11c2bdd12011-05-23 18:24:32320 EXPECT_EQ(number, ASCIIToUTF16("+39 347 234 5678"));
321 // German number.
322 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"),
323 ASCIIToUTF16("024"),
324 ASCIIToUTF16("2345678901"),
325 "DE",
[email protected]5b37cb722011-05-10 18:24:56326 autofill_i18n::NATIONAL,
327 &number));
[email protected]11c2bdd12011-05-23 18:24:32328 EXPECT_EQ(number, ASCIIToUTF16("02423/45678901"));
329
330 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"),
331 ASCIIToUTF16("024"),
332 ASCIIToUTF16("2345678901"),
333 "DE",
334 autofill_i18n::INTERNATIONAL,
335 &number));
336 EXPECT_EQ(number, ASCIIToUTF16("+49 2423/45678901"));
[email protected]5b37cb722011-05-10 18:24:56337}
338
[email protected]a5f9f372011-08-03 23:02:21339TEST_F(PhoneNumberI18NTest, PhoneNumbersMatch) {
[email protected]c50a9482011-05-06 22:53:34340 // Same numbers, defined country code.
341 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
342 ASCIIToUTF16("4158889999"),
343 "US"));
344 // Same numbers, undefined country code.
345 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
346 ASCIIToUTF16("4158889999"),
347 ""));
348
349 // Numbers differ by country code only.
350 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
351 ASCIIToUTF16("4158889999"),
352 "US"));
353
354 // Same numbers, different formats.
355 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
356 ASCIIToUTF16("415-888-9999"),
357 "US"));
358 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
359 ASCIIToUTF16("(415)888-9999"),
360 "US"));
361 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
362 ASCIIToUTF16("415 888 9999"),
363 "US"));
364 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
365 ASCIIToUTF16("415 TUV WXYZ"),
366 "US"));
[email protected]944a5cd82011-10-19 23:42:29367 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("1(415)888-99-99"),
368 ASCIIToUTF16("+14158889999"),
369 "US"));
[email protected]c50a9482011-05-06 22:53:34370
371 // Partial matches don't count.
372 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
373 ASCIIToUTF16("8889999"),
374 "US"));
[email protected]944a5cd82011-10-19 23:42:29375
376 // Different numbers don't match.
377 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
378 ASCIIToUTF16("1415888"),
379 "US"));
[email protected]c50a9482011-05-06 22:53:34380}