OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/autofill/home_phone_number.h" | 6 #include "chrome/browser/autofill/field_types.h" |
7 #include "chrome/browser/autofill/phone_number.h" | 7 #include "chrome/browser/autofill/phone_number.h" |
| 8 #include "chrome/browser/autofill/phone_number_i18n.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 // Tests the phone number parser. | 11 TEST(PhoneNumberTest, Matcher) { |
11 TEST(PhoneNumberTest, Parser) { | 12 // Set phone number so country_code == 1, city_code = 650, number = 2345678. |
12 string16 number; | 13 string16 phone(ASCIIToUTF16("1 [650] 234-5678")); |
13 string16 city_code; | 14 PhoneNumber phone_number(AutofillType::PHONE_HOME); |
14 string16 country_code; | 15 phone_number.set_number(phone); |
| 16 phone_number.set_locale(std::string("US")); |
| 17 phone_number.NormalizePhone(); |
15 | 18 |
16 // Test for empty string. Should give back empty strings. | 19 FieldTypeSet matching_types; |
17 string16 phone0; | 20 phone_number.GetMatchingTypes(ASCIIToUTF16(""), &matching_types); |
18 PhoneNumber::ParsePhoneNumber(phone0, &number, &city_code, &country_code); | 21 EXPECT_EQ(0U, matching_types.size()); |
19 EXPECT_EQ(string16(), number); | 22 matching_types.clear(); |
20 EXPECT_EQ(string16(), city_code); | 23 phone_number.GetMatchingTypes(ASCIIToUTF16("1"), &matching_types); |
21 EXPECT_EQ(string16(), country_code); | 24 EXPECT_EQ(1U, matching_types.size()); |
| 25 EXPECT_TRUE(matching_types.find(PHONE_HOME_COUNTRY_CODE) != |
| 26 matching_types.end()); |
| 27 matching_types.clear(); |
| 28 phone_number.GetMatchingTypes(ASCIIToUTF16("16"), &matching_types); |
| 29 EXPECT_EQ(0U, matching_types.size()); |
| 30 phone_number.GetMatchingTypes(ASCIIToUTF16("165"), &matching_types); |
| 31 EXPECT_EQ(0U, matching_types.size()); |
| 32 phone_number.GetMatchingTypes(ASCIIToUTF16("1650"), &matching_types); |
| 33 EXPECT_EQ(0U, matching_types.size()); |
| 34 phone_number.GetMatchingTypes(ASCIIToUTF16("16502"), &matching_types); |
| 35 EXPECT_EQ(0U, matching_types.size()); |
| 36 phone_number.GetMatchingTypes(ASCIIToUTF16("165023"), &matching_types); |
| 37 EXPECT_EQ(0U, matching_types.size()); |
| 38 phone_number.GetMatchingTypes(ASCIIToUTF16("1650234"), &matching_types); |
| 39 EXPECT_EQ(0U, matching_types.size()); |
| 40 matching_types.clear(); |
| 41 phone_number.GetMatchingTypes(ASCIIToUTF16("16502345678"), &matching_types); |
| 42 EXPECT_EQ(1U, matching_types.size()); |
| 43 EXPECT_TRUE(matching_types.find(PHONE_HOME_WHOLE_NUMBER) != |
| 44 matching_types.end()); |
| 45 matching_types.clear(); |
| 46 phone_number.GetMatchingTypes(ASCIIToUTF16("650"), &matching_types); |
| 47 EXPECT_EQ(1U, matching_types.size()); |
| 48 EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_CODE) != |
| 49 matching_types.end()); |
| 50 matching_types.clear(); |
| 51 phone_number.GetMatchingTypes(ASCIIToUTF16("2345678"), &matching_types); |
| 52 EXPECT_EQ(1U, matching_types.size()); |
| 53 EXPECT_TRUE(matching_types.find(PHONE_HOME_NUMBER) != matching_types.end()); |
| 54 matching_types.clear(); |
| 55 phone_number.GetMatchingTypes(ASCIIToUTF16("234"), &matching_types); |
| 56 EXPECT_EQ(1U, matching_types.size()); |
| 57 EXPECT_TRUE(matching_types.find(PHONE_HOME_NUMBER) != matching_types.end()); |
| 58 matching_types.clear(); |
| 59 phone_number.GetMatchingTypes(ASCIIToUTF16("5678"), &matching_types); |
| 60 EXPECT_EQ(1U, matching_types.size()); |
| 61 EXPECT_TRUE(matching_types.find(PHONE_HOME_NUMBER) != matching_types.end()); |
| 62 matching_types.clear(); |
| 63 phone_number.GetMatchingTypes(ASCIIToUTF16("2345"), &matching_types); |
| 64 EXPECT_EQ(0U, matching_types.size()); |
| 65 matching_types.clear(); |
| 66 phone_number.GetMatchingTypes(ASCIIToUTF16("6502345678"), &matching_types); |
| 67 EXPECT_EQ(2U, matching_types.size()); |
| 68 EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_AND_NUMBER) != |
| 69 matching_types.end()); |
| 70 EXPECT_TRUE(matching_types.find(PHONE_HOME_WHOLE_NUMBER) != |
| 71 matching_types.end()); |
| 72 matching_types.clear(); |
| 73 phone_number.GetMatchingTypes(ASCIIToUTF16("(650)2345678"), &matching_types); |
| 74 EXPECT_EQ(2U, matching_types.size()); |
| 75 EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_AND_NUMBER) != |
| 76 matching_types.end()); |
| 77 EXPECT_TRUE(matching_types.find(PHONE_HOME_WHOLE_NUMBER) != |
| 78 matching_types.end()); |
22 | 79 |
23 // Test for string with less than 7 digits. Should give back empty strings. | 80 string16 fax(ASCIIToUTF16("+1(650)650-5678")); |
24 string16 phone1(ASCIIToUTF16("1234")); | 81 PhoneNumber fax_number; |
25 PhoneNumber::ParsePhoneNumber(phone1, &number, &city_code, &country_code); | 82 fax_number.set_locale(std::string("US")); |
26 EXPECT_EQ(string16(), number); | 83 fax_number.SetInfo(PHONE_FAX_WHOLE_NUMBER, fax); |
27 EXPECT_EQ(string16(), city_code); | |
28 EXPECT_EQ(string16(), country_code); | |
29 | 84 |
30 // Test for string with exactly 7 digits. Should give back only phone number. | 85 matching_types.clear(); |
31 string16 phone2(ASCIIToUTF16("1234567")); | 86 fax_number.GetMatchingTypes(ASCIIToUTF16("16506505678"), &matching_types); |
32 PhoneNumber::ParsePhoneNumber(phone2, &number, &city_code, &country_code); | 87 EXPECT_EQ(1U, matching_types.size()); |
33 EXPECT_EQ(ASCIIToUTF16("1234567"), number); | 88 EXPECT_TRUE(matching_types.find(PHONE_FAX_WHOLE_NUMBER) != |
34 EXPECT_EQ(string16(), city_code); | 89 matching_types.end()); |
35 EXPECT_EQ(string16(), country_code); | |
36 | 90 |
37 // Test for string with exactly 7 digits and separators. Should give back | 91 matching_types.clear(); |
38 // only phone number. | 92 fax_number.GetMatchingTypes(ASCIIToUTF16("650"), &matching_types); |
39 string16 phone_separator2(ASCIIToUTF16("123-4567")); | 93 EXPECT_EQ(2U, matching_types.size()); |
40 PhoneNumber::ParsePhoneNumber(phone_separator2, | 94 EXPECT_TRUE(matching_types.find(PHONE_FAX_CITY_CODE) != |
41 &number, &city_code, &country_code); | 95 matching_types.end()); |
42 EXPECT_EQ(ASCIIToUTF16("1234567"), number); | 96 EXPECT_TRUE(matching_types.find(PHONE_FAX_NUMBER) != |
43 EXPECT_EQ(string16(), city_code); | 97 matching_types.end()); |
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 } | 98 } |
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 } | |
OLD | NEW |