Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/autofill/phone_number_unittest.cc

Issue 6877130: These changes *are* for review :) (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.SetInfo(PHONE_HOME_WHOLE_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 99
97 TEST(PhoneNumberTest, Matcher) { 100 TEST(PhoneNumberTest, PhoneCombineHelper) {
98 // Set phone number so country_code == 12, city_code = 123, number = 1234567. 101 PhoneNumber::PhoneCombineHelper number1(AutofillType::PHONE_HOME);
99 string16 phone(ASCIIToUTF16("121231234567")); 102 EXPECT_FALSE(number1.SetInfo(ADDRESS_BILLING_CITY,
100 HomePhoneNumber phone_number; 103 ASCIIToUTF16("1")));
101 phone_number.set_whole_number(phone); 104 EXPECT_FALSE(number1.SetInfo(PHONE_FAX_COUNTRY_CODE,
105 ASCIIToUTF16("1")));
106 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_COUNTRY_CODE,
107 ASCIIToUTF16("1")));
108 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_CITY_CODE,
109 ASCIIToUTF16("650")));
110 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_NUMBER,
111 ASCIIToUTF16("2345678")));
112 string16 parsed_phone;
113 EXPECT_TRUE(number1.ParseNumber("US", &parsed_phone));
114 // International format as it has a country code.
115 EXPECT_EQ(ASCIIToUTF16("+1 650-234-5678"), parsed_phone);
102 116
103 EXPECT_FALSE(phone_number.IsCountryCode(ASCIIToUTF16(""))); 117 PhoneNumber::PhoneCombineHelper number2(AutofillType::PHONE_FAX);
104 EXPECT_FALSE(phone_number.IsCountryCode(ASCIIToUTF16("1"))); 118 EXPECT_FALSE(number2.SetInfo(PHONE_HOME_COUNTRY_CODE,
105 EXPECT_TRUE(phone_number.IsCountryCode(ASCIIToUTF16("12"))); 119 ASCIIToUTF16("1")));
106 EXPECT_FALSE(phone_number.IsCountryCode(ASCIIToUTF16("123"))); 120 EXPECT_TRUE(number2.SetInfo(PHONE_FAX_COUNTRY_CODE,
121 ASCIIToUTF16("1")));
122 EXPECT_TRUE(number2.SetInfo(PHONE_FAX_CITY_CODE,
123 ASCIIToUTF16("650")));
124 EXPECT_TRUE(number2.SetInfo(PHONE_FAX_NUMBER,
125 ASCIIToUTF16("2345679")));
126 EXPECT_TRUE(number2.ParseNumber("US", &parsed_phone));
127 // International format as it has a country code.
128 EXPECT_EQ(ASCIIToUTF16("+1 650-234-5679"), parsed_phone);
107 129
108 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16(""))); 130 PhoneNumber::PhoneCombineHelper number3(AutofillType::PHONE_HOME);
109 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16("1"))); 131 EXPECT_TRUE(number3.SetInfo(PHONE_HOME_CITY_CODE,
110 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16("12"))); 132 ASCIIToUTF16("650")));
111 EXPECT_TRUE(phone_number.IsCityCode(ASCIIToUTF16("123"))); 133 EXPECT_TRUE(number3.SetInfo(PHONE_HOME_NUMBER,
112 EXPECT_FALSE(phone_number.IsCityCode(ASCIIToUTF16("1234"))); 134 ASCIIToUTF16("2345680")));
135 EXPECT_TRUE(number3.ParseNumber("US", &parsed_phone));
136 // National format as it does not have a country code.
137 EXPECT_EQ(ASCIIToUTF16("(650) 234-5680"), parsed_phone);
113 138
114 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16(""))); 139 PhoneNumber::PhoneCombineHelper number4(AutofillType::PHONE_HOME);
115 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("1"))); 140 EXPECT_TRUE(number4.SetInfo(PHONE_HOME_CITY_CODE,
116 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("12"))); 141 ASCIIToUTF16("123"))); // Incorrect city code.
117 EXPECT_TRUE(phone_number.IsNumber(ASCIIToUTF16("123"))); 142 EXPECT_TRUE(number4.SetInfo(PHONE_HOME_NUMBER,
118 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("1234"))); 143 ASCIIToUTF16("2345680")));
119 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("12345"))); 144 EXPECT_FALSE(number4.ParseNumber("US", &parsed_phone));
120 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("123456"))); 145 EXPECT_EQ(string16(), parsed_phone);
121 EXPECT_TRUE(phone_number.IsNumber(ASCIIToUTF16("1234567"))); 146
122 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("234567"))); 147 PhoneNumber::PhoneCombineHelper number5(AutofillType::PHONE_HOME);
123 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("34567"))); 148 EXPECT_TRUE(number5.SetInfo(PHONE_HOME_CITY_AND_NUMBER,
124 EXPECT_TRUE(phone_number.IsNumber(ASCIIToUTF16("4567"))); 149 ASCIIToUTF16("6502345681")));
125 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("567"))); 150 EXPECT_TRUE(number5.ParseNumber("US", &parsed_phone));
126 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("67"))); 151 EXPECT_EQ(ASCIIToUTF16("(650) 234-5681"), parsed_phone);
127 EXPECT_FALSE(phone_number.IsNumber(ASCIIToUTF16("7"))); 152
153 PhoneNumber::PhoneCombineHelper number6(AutofillType::PHONE_HOME);
154 EXPECT_TRUE(number6.SetInfo(PHONE_HOME_CITY_CODE,
155 ASCIIToUTF16("650")));
156 EXPECT_TRUE(number6.SetInfo(PHONE_HOME_NUMBER,
157 ASCIIToUTF16("234")));
158 EXPECT_TRUE(number6.SetInfo(PHONE_HOME_NUMBER,
159 ASCIIToUTF16("5682")));
160 EXPECT_TRUE(number6.ParseNumber("US", &parsed_phone));
161 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone);
128 } 162 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/phone_number_i18n_unittest.cc ('k') | chrome/test/data/autofill/merge/input/multimerge.in » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698