blob: 762b9089171360456b884b519456feb5171c8605 [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]c50a9482011-05-06 22:53:346#include "base/utf_string_conversions.h"
7#include "chrome/browser/autofill/phone_number_i18n.h"
[email protected]c38831a12011-10-28 12:44:498#include "content/test/test_browser_thread.h"
[email protected]c50a9482011-05-06 22:53:349#include "testing/gtest/include/gtest/gtest.h"
10
[email protected]5b37cb722011-05-10 18:24:5611using autofill_i18n::NormalizePhoneNumber;
12using autofill_i18n::ParsePhoneNumber;
13using autofill_i18n::ConstructPhoneNumber;
[email protected]5b37cb722011-05-10 18:24:5614using autofill_i18n::PhoneNumbersMatch;
[email protected]631bb742011-11-02 11:29:3915using content::BrowserThread;
[email protected]5b37cb722011-05-10 18:24:5616
[email protected]05871b32011-10-22 00:46:1817class PhoneNumberI18NTest : public testing::Test {
18 public:
19 // In order to access the application locale -- which the tested functions do
20 // internally -- this test must run on the UI thread.
21 PhoneNumberI18NTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
22
23 private:
24 MessageLoopForUI message_loop_;
[email protected]c38831a12011-10-28 12:44:4925 content::TestBrowserThread ui_thread_;
[email protected]05871b32011-10-22 00:46:1826
27 DISALLOW_COPY_AND_ASSIGN(PhoneNumberI18NTest);
28};
[email protected]a5f9f372011-08-03 23:02:2129
30TEST_F(PhoneNumberI18NTest, NormalizePhoneNumber) {
[email protected]f4672472011-05-27 07:21:0131 // "Large" digits.
32 string16 phone1(UTF8ToUTF16("\xEF\xBC\x91\xEF\xBC\x96\xEF\xBC\x95\xEF\xBC\x90"
33 "\xEF\xBC\x97\xEF\xBC\x94\xEF\xBC\x99\xEF\xBC\x98"
34 "\xEF\xBC\x93\xEF\xBC\x92\xEF\xBC\x93"));
35 EXPECT_EQ(NormalizePhoneNumber(phone1, "US"), ASCIIToUTF16("16507498323"));
[email protected]5b37cb722011-05-10 18:24:5636
[email protected]f4672472011-05-27 07:21:0137 // Devanagari script digits.
38 string16 phone2(UTF8ToUTF16("\xD9\xA1\xD9\xA6\xD9\xA5\xD9\xA0\xD9\xA8\xD9\xA3"
39 "\xD9\xA2\xD9\xA3\xD9\xA7\xD9\xA4\xD9\xA9"));
40 EXPECT_EQ(NormalizePhoneNumber(phone2, "US"), ASCIIToUTF16("16508323749"));
[email protected]5b37cb722011-05-10 18:24:5641
[email protected]f4672472011-05-27 07:21:0142 string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5"));
43 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255"));
[email protected]5b37cb722011-05-10 18:24:5644
45 string16 phone4(UTF8ToUTF16("+1(650)2346789"));
[email protected]f4672472011-05-27 07:21:0146 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789"));
[email protected]5b37cb722011-05-10 18:24:5647
48 string16 phone5(UTF8ToUTF16("6502346789"));
[email protected]f4672472011-05-27 07:21:0149 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789"));
[email protected]5b37cb722011-05-10 18:24:5650}
51
[email protected]a5f9f372011-08-03 23:02:2152TEST_F(PhoneNumberI18NTest, ParsePhoneNumber) {
[email protected]5b37cb722011-05-10 18:24:5653 string16 number;
54 string16 city_code;
55 string16 country_code;
56
57 // Test for empty string. Should give back empty strings.
58 string16 phone0;
59 EXPECT_FALSE(ParsePhoneNumber(phone0, "US",
60 &country_code,
61 &city_code,
62 &number));
63 EXPECT_EQ(string16(), number);
64 EXPECT_EQ(string16(), city_code);
65 EXPECT_EQ(string16(), country_code);
66
67 // Test for string with less than 7 digits. Should give back empty strings.
68 string16 phone1(ASCIIToUTF16("1234"));
69 EXPECT_FALSE(ParsePhoneNumber(phone1, "US",
70 &country_code,
71 &city_code,
72 &number));
73 EXPECT_EQ(string16(), number);
74 EXPECT_EQ(string16(), city_code);
75 EXPECT_EQ(string16(), country_code);
76
77 // Test for string with exactly 7 digits.
78 // Not a valid number - starts with 1
79 string16 phone2(ASCIIToUTF16("1234567"));
80 EXPECT_FALSE(ParsePhoneNumber(phone2, "US",
81 &country_code,
82 &city_code,
83 &number));
84 EXPECT_EQ(string16(), number);
85 EXPECT_EQ(string16(), city_code);
86 EXPECT_EQ(string16(), country_code);
87
88 // Not a valid number - does not have area code.
89 string16 phone3(ASCIIToUTF16("2234567"));
90 EXPECT_FALSE(ParsePhoneNumber(phone3, "US",
91 &country_code,
92 &city_code,
93 &number));
94 EXPECT_EQ(string16(), number);
95 EXPECT_EQ(string16(), city_code);
96 EXPECT_EQ(string16(), country_code);
97
98 // Test for string with greater than 7 digits but less than 10 digits.
99 // Should fail parsing in US.
100 string16 phone4(ASCIIToUTF16("123456789"));
101 EXPECT_FALSE(ParsePhoneNumber(phone4, "US",
102 &country_code,
103 &city_code,
104 &number));
105 EXPECT_EQ(string16(), number);
106 EXPECT_EQ(string16(), city_code);
107 EXPECT_EQ(string16(), country_code);
108
109 // Test for string with greater than 7 digits but less than 10 digits and
110 // separators.
111 // Should fail parsing in US.
112 string16 phone_separator4(ASCIIToUTF16("12.345-6789"));
113 EXPECT_FALSE(ParsePhoneNumber(phone_separator4, "US",
114 &country_code,
115 &city_code,
116 &number));
117 EXPECT_EQ(string16(), number);
118 EXPECT_EQ(string16(), city_code);
119 EXPECT_EQ(string16(), country_code);
120
121 // Test for string with exactly 10 digits.
122 // Should give back phone number and city code.
123 // This one going to fail because of the incorrect area code.
124 string16 phone5(ASCIIToUTF16("1234567890"));
125 EXPECT_FALSE(ParsePhoneNumber(phone5, "US",
126 &country_code,
127 &city_code,
128 &number));
129 EXPECT_EQ(string16(), number);
130 EXPECT_EQ(string16(), city_code);
131 EXPECT_EQ(string16(), country_code);
132
133 string16 phone6(ASCIIToUTF16("6501567890"));
134 // This one going to fail because of the incorrect number (starts with 1).
135 EXPECT_FALSE(ParsePhoneNumber(phone6, "US",
136 &country_code,
137 &city_code,
138 &number));
139 EXPECT_EQ(string16(), number);
140 EXPECT_EQ(string16(), city_code);
141 EXPECT_EQ(string16(), country_code);
142
143 string16 phone7(ASCIIToUTF16("6504567890"));
144 EXPECT_TRUE(ParsePhoneNumber(phone7, "US",
145 &country_code,
146 &city_code,
147 &number));
148 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
149 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
150 EXPECT_EQ(string16(), country_code);
151
152 // Test for string with exactly 10 digits and separators.
153 // Should give back phone number and city code.
154 string16 phone_separator7(ASCIIToUTF16("(650) 456-7890"));
155 EXPECT_TRUE(ParsePhoneNumber(phone_separator7, "US",
156 &country_code,
157 &city_code,
158 &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 EXPECT_EQ(string16(), number);
172 EXPECT_EQ(string16(), city_code);
173 EXPECT_EQ(string16(), country_code);
174
175 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed.
176 string16 phone9(ASCIIToUTF16("01116504567890"));
177 EXPECT_TRUE(ParsePhoneNumber(phone9, "US",
178 &country_code,
179 &city_code,
180 &number));
181 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
182 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
183 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
184
185 // 011 is a correct "dial out" prefix in the USA - the parsing should succeed.
186 string16 phone10(ASCIIToUTF16("01178124567890"));
187 EXPECT_TRUE(ParsePhoneNumber(phone10, "US",
188 &country_code,
189 &city_code,
190 &number));
191 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
192 EXPECT_EQ(ASCIIToUTF16("812"), city_code);
193 EXPECT_EQ(ASCIIToUTF16("7"), country_code);
194
195 // Test for string with over 10 digits with separator characters.
196 // Should give back phone number, city code, and country code. "011" is
197 // US "dial out" code, which is discarded.
198 string16 phone11(ASCIIToUTF16("(0111) 650-456.7890"));
199 EXPECT_TRUE(ParsePhoneNumber(phone11, "US",
200 &country_code,
201 &city_code,
202 &number));
203 EXPECT_EQ(ASCIIToUTF16("4567890"), number);
204 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
205 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
206
207 // Now try phone from Chech republic - it has 00 dial out code, 420 country
208 // code and variable length area codes.
209 string16 phone12(ASCIIToUTF16("+420 27-89.10.112"));
210 EXPECT_TRUE(ParsePhoneNumber(phone12, "US",
211 &country_code,
212 &city_code,
213 &number));
[email protected]f4672472011-05-27 07:21:01214 EXPECT_EQ(ASCIIToUTF16("910112"), number);
215 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
[email protected]5b37cb722011-05-10 18:24:56216 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
217
218 EXPECT_TRUE(ParsePhoneNumber(phone12, "CZ",
219 &country_code,
220 &city_code,
221 &number));
[email protected]f4672472011-05-27 07:21:01222 EXPECT_EQ(ASCIIToUTF16("910112"), number);
223 EXPECT_EQ(ASCIIToUTF16("278"), city_code);
[email protected]5b37cb722011-05-10 18:24:56224 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
225
226 string16 phone13(ASCIIToUTF16("420 57-89.10.112"));
227 EXPECT_FALSE(ParsePhoneNumber(phone13, "US",
228 &country_code,
229 &city_code,
230 &number));
231 EXPECT_TRUE(ParsePhoneNumber(phone13, "CZ",
232 &country_code,
233 &city_code,
234 &number));
[email protected]f4672472011-05-27 07:21:01235 EXPECT_EQ(ASCIIToUTF16("910112"), number);
236 EXPECT_EQ(ASCIIToUTF16("578"), city_code);
[email protected]5b37cb722011-05-10 18:24:56237 EXPECT_EQ(ASCIIToUTF16("420"), country_code);
238
239 string16 phone14(ASCIIToUTF16("1-650-FLOWERS"));
240 EXPECT_TRUE(ParsePhoneNumber(phone14, "US",
241 &country_code,
242 &city_code,
243 &number));
244 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
245 EXPECT_EQ(ASCIIToUTF16("650"), city_code);
246 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
[email protected]f4672472011-05-27 07:21:01247
248 // 800 is not an area code, but the destination code. In our library these
249 // codes should be treated the same as area codes.
250 string16 phone15(ASCIIToUTF16("1-800-FLOWERS"));
251 EXPECT_TRUE(ParsePhoneNumber(phone15, "US",
252 &country_code,
253 &city_code,
254 &number));
255 EXPECT_EQ(ASCIIToUTF16("3569377"), number);
256 EXPECT_EQ(ASCIIToUTF16("800"), city_code);
257 EXPECT_EQ(ASCIIToUTF16("1"), country_code);
[email protected]5b37cb722011-05-10 18:24:56258}
259
[email protected]a5f9f372011-08-03 23:02:21260TEST_F(PhoneNumberI18NTest, ConstructPhoneNumber) {
[email protected]5b37cb722011-05-10 18:24:56261 string16 number;
262 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
263 ASCIIToUTF16("650"),
264 ASCIIToUTF16("2345678"),
265 "US",
266 autofill_i18n::E164,
267 &number));
268 EXPECT_EQ(number, ASCIIToUTF16("+16502345678"));
269 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
270 ASCIIToUTF16("650"),
271 ASCIIToUTF16("2345678"),
272 "US",
273 autofill_i18n::INTERNATIONAL,
274 &number));
275 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
276 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
277 ASCIIToUTF16("650"),
278 ASCIIToUTF16("2345678"),
279 "US",
280 autofill_i18n::NATIONAL,
281 &number));
282 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678"));
283 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"),
284 ASCIIToUTF16("650"),
285 ASCIIToUTF16("2345678"),
286 "US",
287 autofill_i18n::RFC3966,
288 &number));
289 EXPECT_EQ(number, ASCIIToUTF16("+1-650-234-5678"));
290 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16(""),
291 ASCIIToUTF16("650"),
292 ASCIIToUTF16("2345678"),
293 "US",
294 autofill_i18n::INTERNATIONAL,
295 &number));
296 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
297 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16(""),
298 ASCIIToUTF16(""),
299 ASCIIToUTF16("6502345678"),
300 "US",
301 autofill_i18n::INTERNATIONAL,
302 &number));
303 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678"));
304
305 EXPECT_FALSE(ConstructPhoneNumber(ASCIIToUTF16(""),
306 ASCIIToUTF16("650"),
307 ASCIIToUTF16("234567890"),
308 "US",
309 autofill_i18n::INTERNATIONAL,
310 &number));
311 EXPECT_EQ(number, string16());
312 // Italian number
[email protected]11c2bdd12011-05-23 18:24:32313 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16(""),
314 ASCIIToUTF16("347"),
315 ASCIIToUTF16("2345678"),
316 "IT",
[email protected]5b37cb722011-05-10 18:24:56317 autofill_i18n::INTERNATIONAL,
318 &number));
[email protected]11c2bdd12011-05-23 18:24:32319 EXPECT_EQ(number, ASCIIToUTF16("+39 347 234 5678"));
320 // German number.
321 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"),
322 ASCIIToUTF16("024"),
323 ASCIIToUTF16("2345678901"),
324 "DE",
[email protected]5b37cb722011-05-10 18:24:56325 autofill_i18n::NATIONAL,
326 &number));
[email protected]11c2bdd12011-05-23 18:24:32327 EXPECT_EQ(number, ASCIIToUTF16("02423/45678901"));
328
329 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"),
330 ASCIIToUTF16("024"),
331 ASCIIToUTF16("2345678901"),
332 "DE",
333 autofill_i18n::INTERNATIONAL,
334 &number));
335 EXPECT_EQ(number, ASCIIToUTF16("+49 2423/45678901"));
[email protected]5b37cb722011-05-10 18:24:56336}
337
[email protected]a5f9f372011-08-03 23:02:21338TEST_F(PhoneNumberI18NTest, PhoneNumbersMatch) {
[email protected]c50a9482011-05-06 22:53:34339 // Same numbers, defined country code.
340 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
341 ASCIIToUTF16("4158889999"),
342 "US"));
343 // Same numbers, undefined country code.
344 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
345 ASCIIToUTF16("4158889999"),
346 ""));
347
348 // Numbers differ by country code only.
349 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
350 ASCIIToUTF16("4158889999"),
351 "US"));
352
353 // Same numbers, different formats.
354 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
355 ASCIIToUTF16("415-888-9999"),
356 "US"));
357 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
358 ASCIIToUTF16("(415)888-9999"),
359 "US"));
360 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
361 ASCIIToUTF16("415 888 9999"),
362 "US"));
363 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"),
364 ASCIIToUTF16("415 TUV WXYZ"),
365 "US"));
[email protected]944a5cd82011-10-19 23:42:29366 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("1(415)888-99-99"),
367 ASCIIToUTF16("+14158889999"),
368 "US"));
[email protected]c50a9482011-05-06 22:53:34369
370 // Partial matches don't count.
371 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
372 ASCIIToUTF16("8889999"),
373 "US"));
[email protected]944a5cd82011-10-19 23:42:29374
375 // Different numbers don't match.
376 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"),
377 ASCIIToUTF16("1415888"),
378 "US"));
[email protected]c50a9482011-05-06 22:53:34379}