1 // 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 5 #include "base/utf_string_conversions.h" 6 #include "chrome/browser/autofill/home_phone_number.h" 7 #include "chrome/browser/autofill/phone_number.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 // Tests the phone number parser. 11 TEST(PhoneNumberTest, Parser) { 12 string16 number; 13 string16 city_code; 14 string16 country_code; 15 16 // Test for empty string. Should give back empty strings. 17 string16 phone0; 18 PhoneNumber::ParsePhoneNumber(phone0, &number, &city_code, &country_code); 19 EXPECT_EQ(string16(), number); 20 EXPECT_EQ(string16(), city_code); 21 EXPECT_EQ(string16(), country_code); 22 23 // Test for string with less than 7 digits. Should give back empty strings. 24 string16 phone1(ASCIIToUTF16("1234")); 25 PhoneNumber::ParsePhoneNumber(phone1, &number, &city_code, &country_code); 26 EXPECT_EQ(string16(), number); 27 EXPECT_EQ(string16(), city_code); 28 EXPECT_EQ(string16(), country_code); 29 30 // Test for string with exactly 7 digits. Should give back only phone number. 31 string16 phone2(ASCIIToUTF16("1234567")); 32 PhoneNumber::ParsePhoneNumber(phone2, &number, &city_code, &country_code); 33 EXPECT_EQ(ASCIIToUTF16("1234567"), number); 34 EXPECT_EQ(string16(), city_code); 35 EXPECT_EQ(string16(), country_code); 36 37 // Test for string with exactly 7 digits and separators. Should give back 38 // only phone number. 39 string16 phone_separator2(ASCIIToUTF16("123-4567")); 40 PhoneNumber::ParsePhoneNumber(phone_separator2, 41 &number, &city_code, &country_code); 42 EXPECT_EQ(ASCIIToUTF16("1234567"), number); 43 EXPECT_EQ(string16(), city_code); 44 EXPECT_EQ(string16(), country_code); 45 46 // Test for string with greater than 7 digits but less than 10 digits. 47 // Should give back only phone number. 48 string16 phone3(ASCIIToUTF16("123456789")); 49 PhoneNumber::ParsePhoneNumber(phone3, &number, &city_code, &country_code); 50 EXPECT_EQ(ASCIIToUTF16("3456789"), number); 51 EXPECT_EQ(string16(), city_code); 52 EXPECT_EQ(string16(), country_code); 53 54 // Test for string with greater than 7 digits but less than 10 digits and 55 // separators. 56 // Should give back only phone number. 57 string16 phone_separator3(ASCIIToUTF16("12.345-6789")); 58 PhoneNumber::ParsePhoneNumber(phone3, &number, &city_code, &country_code); 59 EXPECT_EQ(ASCIIToUTF16("3456789"), number); 60 EXPECT_EQ(string16(), city_code); 61 EXPECT_EQ(string16(), country_code); 62 63 // Test for string with exactly 10 digits. 64 // Should give back phone number and city code. 65 string16 phone4(ASCIIToUTF16("1234567890")); 66 PhoneNumber::ParsePhoneNumber(phone4, &number, &city_code, &country_code); 67 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 68 EXPECT_EQ(ASCIIToUTF16("123"), city_code); 69 EXPECT_EQ(string16(), country_code); 70 71 // Test for string with exactly 10 digits and separators. 72 // Should give back phone number and city code. 73 string16 phone_separator4(ASCIIToUTF16("(123) 456-7890")); 74 PhoneNumber::ParsePhoneNumber(phone_separator4, 75 &number, &city_code, &country_code); 76 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 77 EXPECT_EQ(ASCIIToUTF16("123"), city_code); 78 EXPECT_EQ(string16(), country_code); 79 80 // Test for string with over 10 digits. 81 // Should give back phone number, city code, and country code. 82 string16 phone5(ASCIIToUTF16("011234567890")); 83 PhoneNumber::ParsePhoneNumber(phone5, &number, &city_code, &country_code); 84 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 85 EXPECT_EQ(ASCIIToUTF16("123"), city_code); 86 EXPECT_EQ(ASCIIToUTF16("01"), country_code); 87 88 // Test for string with over 10 digits with separator characters. 89 // Should give back phone number, city code, and country code. 90 string16 phone6(ASCIIToUTF16("(01) 123-456.7890")); 91 PhoneNumber::ParsePhoneNumber(phone6, &number, &city_code, &country_code); 92 EXPECT_EQ(ASCIIToUTF16("4567890"), number); 93 EXPECT_EQ(ASCIIToUTF16("123"), city_code); 94 EXPECT_EQ(ASCIIToUTF16("01"), country_code); 95 } 96 97 TEST(PhoneNumberTest, Matcher) { 98 // Set phone number so country_code == 12, city_code = 123, number = 1234567. 99 string16 phone(ASCIIToUTF16("121231234567")); 100 HomePhoneNumber phone_number; 101 phone_number.set_whole_number(phone); 102 103 EXPECT_FALSE(phone_number.IsCountryCode(ASCIIToUTF16(""))); 104 EXPECT_FALSE(phone_number.IsCountryCode(ASCIIToUTF16("1"))); 105 EXPECT_TRUE(phone_number.IsCountryCode(ASCIIToUTF16("12"))); 106 EXPECT_FALSE(phone_number.IsCountryCode(ASCIIToUTF16("123"))); 107 108 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16(""))); 109 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16("1"))); 110 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16("12"))); 111 EXPECT_TRUE(phone_number.IsCityCode(ASCIIToUTF16("123"))); 112 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16("1234"))); 113 114 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16(""))); 115 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("1"))); 116 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("12"))); 117 EXPECT_TRUE(phone_number.IsNumber(ASCIIToUTF16("123"))); 118 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("1234"))); 119 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("12345"))); 120 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("123456"))); 121 EXPECT_TRUE(phone_number.IsNumber(ASCIIToUTF16("1234567"))); 122 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("234567"))); 123 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("34567"))); 124 EXPECT_TRUE(phone_number.IsNumber(ASCIIToUTF16("4567"))); 125 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("567"))); 126 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("67"))); 127 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("7"))); 128 } 129