phone_number_i18n_unittest.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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. 4 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" 12 13using content::BrowserThread; 14 15namespace autofill { 16 17using i18n::NormalizePhoneNumber; 18using i18n::ParsePhoneNumber; 19using i18n::ConstructPhoneNumber; 20using i18n::PhoneNumbersMatch; 21 22TEST(PhoneNumberI18NTest, NormalizePhoneNumber) { 23 // "Large" digits. 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")); 29 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")); 35 36 base::string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5")); 37 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255")); 38 39 base::string16 phone4(UTF8ToUTF16("+1(650)2346789")); 40 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789")); 41 42 base::string16 phone5(UTF8ToUTF16("6502346789")); 43 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789")); 44} 45 46TEST(PhoneNumberI18NTest, ParsePhoneNumber) { 47 base::string16 number; 48 base::string16 city_code; 49 base::string16 country_code; 50 ::i18n::phonenumbers::PhoneNumber unused_i18n_number; 51 52 // Test for empty string. Should give back empty strings. 53 base::string16 phone0; 54 EXPECT_FALSE(ParsePhoneNumber(phone0, "US", 55 &country_code, 56 &city_code, 57 &number, 58 &unused_i18n_number)); 59 EXPECT_EQ(base::string16(), number); 60 EXPECT_EQ(base::string16(), city_code); 61 EXPECT_EQ(base::string16(), country_code); 62 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", 66 &country_code, 67 &city_code, 68 &number, 69 &unused_i18n_number)); 70 EXPECT_EQ(base::string16(), number); 71 EXPECT_EQ(base::string16(), city_code); 72 EXPECT_EQ(base::string16(), country_code); 73 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", 78 &country_code, 79 &city_code, 80 &number, 81 &unused_i18n_number)); 82 EXPECT_EQ(base::string16(), number); 83 EXPECT_EQ(base::string16(), city_code); 84 EXPECT_EQ(base::string16(), country_code); 85 86 // Not a valid number - does not have area code. 87 base::string16 phone3(ASCIIToUTF16("2234567")); 88 EXPECT_FALSE(ParsePhoneNumber(phone3, "US", 89 &country_code, 90 &city_code, 91 &number, 92 &unused_i18n_number)); 93 EXPECT_EQ(base::string16(), number); 94 EXPECT_EQ(base::string16(), city_code); 95 EXPECT_EQ(base::string16(), country_code); 96 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", 101 &country_code, 102 &city_code, 103 &number, 104 &unused_i18n_number)); 105 EXPECT_EQ(base::string16(), number); 106 EXPECT_EQ(base::string16(), city_code); 107 EXPECT_EQ(base::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 base::string16 phone_separator4(ASCIIToUTF16("12.345-6789")); 113 EXPECT_FALSE(ParsePhoneNumber(phone_separator4, "US", 114 &country_code, 115 &city_code, 116 &number, 117 &unused_i18n_number)); 118 EXPECT_EQ(base::string16(), number); 119 EXPECT_EQ(base::string16(), city_code); 120 EXPECT_EQ(base::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 base::string16 phone5(ASCIIToUTF16("1234567890")); 126 EXPECT_FALSE(ParsePhoneNumber(phone5, "US", 127 &country_code, 128 &city_code, 129 &number, 130 &unused_i18n_number)); 131 EXPECT_EQ(base::string16(), number); 132 EXPECT_EQ(base::string16(), city_code); 133 EXPECT_EQ(base::string16(), country_code); 134 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", 138 &country_code, 139 &city_code, 140 &number, 141 &unused_i18n_number)); 142 EXPECT_EQ(base::string16(), number); 143 EXPECT_EQ(base::string16(), city_code); 144 EXPECT_EQ(base::string16(), country_code); 145 146 base::string16 phone7(ASCIIToUTF16("6504567890")); 147 EXPECT_TRUE(ParsePhoneNumber(phone7, "US", 148 &country_code, 149 &city_code, 150 &number, 151 &unused_i18n_number)); 152 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 153 EXPECT_EQ(ASCIIToUTF16("650"), city_code); 154 EXPECT_EQ(base::string16(), country_code); 155 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", 160 &country_code, 161 &city_code, 162 &number, 163 &unused_i18n_number)); 164 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 165 EXPECT_EQ(ASCIIToUTF16("650"), city_code); 166 EXPECT_EQ(base::string16(), country_code); 167 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", 173 &country_code, 174 &city_code, 175 &number, 176 &unused_i18n_number)); 177 EXPECT_EQ(base::string16(), number); 178 EXPECT_EQ(base::string16(), city_code); 179 EXPECT_EQ(base::string16(), country_code); 180 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", 184 &country_code, 185 &city_code, 186 &number, 187 &unused_i18n_number)); 188 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 189 EXPECT_EQ(ASCIIToUTF16("650"), city_code); 190 EXPECT_EQ(ASCIIToUTF16("1"), country_code); 191 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", 195 &country_code, 196 &city_code, 197 &number, 198 &unused_i18n_number)); 199 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 200 EXPECT_EQ(ASCIIToUTF16("812"), city_code); 201 EXPECT_EQ(ASCIIToUTF16("7"), country_code); 202 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", 208 &country_code, 209 &city_code, 210 &number, 211 &unused_i18n_number)); 212 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 213 EXPECT_EQ(ASCIIToUTF16("650"), city_code); 214 EXPECT_EQ(ASCIIToUTF16("1"), country_code); 215 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", 220 &country_code, 221 &city_code, 222 &number, 223 &unused_i18n_number)); 224 EXPECT_EQ(ASCIIToUTF16("910112"), number); 225 EXPECT_EQ(ASCIIToUTF16("278"), city_code); 226 EXPECT_EQ(ASCIIToUTF16("420"), country_code); 227 228 EXPECT_TRUE(ParsePhoneNumber(phone12, "CZ", 229 &country_code, 230 &city_code, 231 &number, 232 &unused_i18n_number)); 233 EXPECT_EQ(ASCIIToUTF16("910112"), number); 234 EXPECT_EQ(ASCIIToUTF16("278"), city_code); 235 EXPECT_EQ(ASCIIToUTF16("420"), country_code); 236 237 base::string16 phone13(ASCIIToUTF16("420 57-89.10.112")); 238 EXPECT_FALSE(ParsePhoneNumber(phone13, "US", 239 &country_code, 240 &city_code, 241 &number, 242 &unused_i18n_number)); 243 EXPECT_TRUE(ParsePhoneNumber(phone13, "CZ", 244 &country_code, 245 &city_code, 246 &number, 247 &unused_i18n_number)); 248 EXPECT_EQ(ASCIIToUTF16("910112"), number); 249 EXPECT_EQ(ASCIIToUTF16("578"), city_code); 250 EXPECT_EQ(ASCIIToUTF16("420"), country_code); 251 252 base::string16 phone14(ASCIIToUTF16("1-650-FLOWERS")); 253 EXPECT_TRUE(ParsePhoneNumber(phone14, "US", 254 &country_code, 255 &city_code, 256 &number, 257 &unused_i18n_number)); 258 EXPECT_EQ(ASCIIToUTF16("3569377"), number); 259 EXPECT_EQ(ASCIIToUTF16("650"), city_code); 260 EXPECT_EQ(ASCIIToUTF16("1"), country_code); 261 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", 266 &country_code, 267 &city_code, 268 &number, 269 &unused_i18n_number)); 270 EXPECT_EQ(ASCIIToUTF16("3569377"), number); 271 EXPECT_EQ(ASCIIToUTF16("800"), city_code); 272 EXPECT_EQ(ASCIIToUTF16("1"), country_code); 273} 274 275TEST(PhoneNumberI18NTest, ConstructPhoneNumber) { 276 base::string16 number; 277 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"), 278 ASCIIToUTF16("650"), 279 ASCIIToUTF16("2345678"), 280 "US", 281 &number)); 282 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678")); 283 EXPECT_TRUE(ConstructPhoneNumber(base::string16(), 284 ASCIIToUTF16("650"), 285 ASCIIToUTF16("2345678"), 286 "US", 287 &number)); 288 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678")); 289 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"), 290 base::string16(), 291 ASCIIToUTF16("6502345678"), 292 "US", 293 &number)); 294 EXPECT_EQ(number, ASCIIToUTF16("+1 650-234-5678")); 295 EXPECT_TRUE(ConstructPhoneNumber(base::string16(), 296 base::string16(), 297 ASCIIToUTF16("6502345678"), 298 "US", 299 &number)); 300 EXPECT_EQ(number, ASCIIToUTF16("(650) 234-5678")); 301 302 EXPECT_FALSE(ConstructPhoneNumber(base::string16(), 303 ASCIIToUTF16("650"), 304 ASCIIToUTF16("234567890"), 305 "US", 306 &number)); 307 EXPECT_EQ(number, base::string16()); 308 // Italian number 309 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("39"), 310 ASCIIToUTF16("347"), 311 ASCIIToUTF16("2345678"), 312 "IT", 313 &number)); 314 EXPECT_EQ(number, ASCIIToUTF16("+39 347 234 5678")); 315 EXPECT_TRUE(ConstructPhoneNumber(base::string16(), 316 ASCIIToUTF16("347"), 317 ASCIIToUTF16("2345678"), 318 "IT", 319 &number)); 320 EXPECT_EQ(number, ASCIIToUTF16("347 234 5678")); 321 // German number. 322 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("49"), 323 ASCIIToUTF16("024"), 324 ASCIIToUTF16("2345678901"), 325 "DE", 326 &number)); 327 EXPECT_EQ(number, ASCIIToUTF16("+49 2423 45678901")); 328 EXPECT_TRUE(ConstructPhoneNumber(base::string16(), 329 ASCIIToUTF16("024"), 330 ASCIIToUTF16("2345678901"), 331 "DE", 332 &number)); 333 EXPECT_EQ(number, ASCIIToUTF16("02423 45678901")); 334} 335 336TEST(PhoneNumberI18NTest, PhoneNumbersMatch) { 337 // Same numbers, defined country code. 338 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), 339 ASCIIToUTF16("4158889999"), 340 "US", 341 "en-US")); 342 // Same numbers, undefined country code. 343 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), 344 ASCIIToUTF16("4158889999"), 345 std::string(), 346 "en-US")); 347 348 // Numbers differ by country code only. 349 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), 350 ASCIIToUTF16("4158889999"), 351 "US", 352 "en-US")); 353 354 // Same numbers, different formats. 355 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), 356 ASCIIToUTF16("415-888-9999"), 357 "US", 358 "en-US")); 359 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), 360 ASCIIToUTF16("(415)888-9999"), 361 "US", 362 "en-US")); 363 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), 364 ASCIIToUTF16("415 888 9999"), 365 "US", 366 "en-US")); 367 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("4158889999"), 368 ASCIIToUTF16("415 TUV WXYZ"), 369 "US", 370 "en-US")); 371 EXPECT_TRUE(PhoneNumbersMatch(ASCIIToUTF16("1(415)888-99-99"), 372 ASCIIToUTF16("+14158889999"), 373 "US", 374 "en-US")); 375 376 // Partial matches don't count. 377 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), 378 ASCIIToUTF16("8889999"), 379 "US", 380 "en-US")); 381 382 // Different numbers don't match. 383 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), 384 ASCIIToUTF16("1415888"), 385 "US", 386 "en-US")); 387} 388 389} // namespace autofill 390