phone_number_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/strings/string16.h"
6#include "base/strings/utf_string_conversions.h"
7#include "components/autofill/core/browser/autofill_profile.h"
8#include "components/autofill/core/browser/autofill_type.h"
9#include "components/autofill/core/browser/field_types.h"
10#include "components/autofill/core/browser/phone_number.h"
11#include "components/autofill/core/browser/phone_number_i18n.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14using base::ASCIIToUTF16;
15
16namespace autofill {
17
18TEST(PhoneNumberTest, Matcher) {
19  AutofillProfile profile;
20  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
21  // Set phone number so country_code == 1, city_code = 650, number = 2345678.
22  base::string16 phone(ASCIIToUTF16("1 [650] 234-5678"));
23  PhoneNumber phone_number(&profile);
24  phone_number.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone, "US");
25
26  ServerFieldTypeSet matching_types;
27  phone_number.GetMatchingTypes(base::string16(), "US", &matching_types);
28  EXPECT_EQ(1U, matching_types.size());
29  EXPECT_TRUE(matching_types.find(EMPTY_TYPE) != matching_types.end());
30  matching_types.clear();
31  phone_number.GetMatchingTypes(ASCIIToUTF16("1"), "US", &matching_types);
32  EXPECT_EQ(1U, matching_types.size());
33  EXPECT_TRUE(matching_types.find(PHONE_HOME_COUNTRY_CODE) !=
34              matching_types.end());
35  matching_types.clear();
36  phone_number.GetMatchingTypes(ASCIIToUTF16("16"), "US", &matching_types);
37  EXPECT_EQ(0U, matching_types.size());
38  phone_number.GetMatchingTypes(ASCIIToUTF16("165"), "US", &matching_types);
39  EXPECT_EQ(0U, matching_types.size());
40  phone_number.GetMatchingTypes(ASCIIToUTF16("1650"), "US", &matching_types);
41  EXPECT_EQ(0U, matching_types.size());
42  phone_number.GetMatchingTypes(ASCIIToUTF16("16502"), "US", &matching_types);
43  EXPECT_EQ(0U, matching_types.size());
44  phone_number.GetMatchingTypes(ASCIIToUTF16("165023"), "US", &matching_types);
45  EXPECT_EQ(0U, matching_types.size());
46  phone_number.GetMatchingTypes(ASCIIToUTF16("1650234"), "US", &matching_types);
47  EXPECT_EQ(0U, matching_types.size());
48  matching_types.clear();
49  phone_number.GetMatchingTypes(ASCIIToUTF16("16502345678"), "US",
50                                &matching_types);
51  EXPECT_EQ(1U, matching_types.size());
52  EXPECT_TRUE(matching_types.find(PHONE_HOME_WHOLE_NUMBER) !=
53              matching_types.end());
54  matching_types.clear();
55  phone_number.GetMatchingTypes(ASCIIToUTF16("650"), "US", &matching_types);
56  EXPECT_EQ(1U, matching_types.size());
57  EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_CODE) !=
58              matching_types.end());
59  matching_types.clear();
60  phone_number.GetMatchingTypes(ASCIIToUTF16("2345678"), "US", &matching_types);
61  EXPECT_EQ(1U, matching_types.size());
62  EXPECT_TRUE(matching_types.find(PHONE_HOME_NUMBER) != matching_types.end());
63  matching_types.clear();
64  phone_number.GetMatchingTypes(ASCIIToUTF16("234"), "US", &matching_types);
65  EXPECT_EQ(1U, matching_types.size());
66  EXPECT_TRUE(matching_types.find(PHONE_HOME_NUMBER) != matching_types.end());
67  matching_types.clear();
68  phone_number.GetMatchingTypes(ASCIIToUTF16("5678"), "US", &matching_types);
69  EXPECT_EQ(1U, matching_types.size());
70  EXPECT_TRUE(matching_types.find(PHONE_HOME_NUMBER) != matching_types.end());
71  matching_types.clear();
72  phone_number.GetMatchingTypes(ASCIIToUTF16("2345"), "US", &matching_types);
73  EXPECT_EQ(0U, matching_types.size());
74  matching_types.clear();
75  phone_number.GetMatchingTypes(ASCIIToUTF16("6502345678"), "US",
76                                &matching_types);
77  EXPECT_EQ(1U, matching_types.size());
78  EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_AND_NUMBER) !=
79              matching_types.end());
80  matching_types.clear();
81  phone_number.GetMatchingTypes(ASCIIToUTF16("(650)2345678"), "US",
82                                &matching_types);
83  EXPECT_EQ(1U, matching_types.size());
84  EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_AND_NUMBER) !=
85              matching_types.end());
86}
87
88// Verify that PhoneNumber::SetInfo() correctly formats the incoming number.
89TEST(PhoneNumberTest, SetInfo) {
90  AutofillProfile profile;
91  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
92
93  PhoneNumber phone(&profile);
94  EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
95
96  // Set the formatted info directly.
97  EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
98                            ASCIIToUTF16("(650) 234-5678"), "US"));
99  EXPECT_EQ(ASCIIToUTF16("(650) 234-5678"),
100            phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
101
102  // Unformatted numbers should be formatted.
103  EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
104                            ASCIIToUTF16("8887776666"), "US"));
105  EXPECT_EQ(ASCIIToUTF16("(888) 777-6666"),
106            phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
107  EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
108                            ASCIIToUTF16("+18887776666"),
109                            "US"));
110  EXPECT_EQ(ASCIIToUTF16("1 888-777-6666"),
111            phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
112
113  // Differently formatted numbers should be left alone.
114  EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
115                            ASCIIToUTF16("800-432-8765"), "US"));
116  EXPECT_EQ(ASCIIToUTF16("800-432-8765"),
117            phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
118
119  // SetRawInfo should not try to format.
120  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8004328765"));
121  EXPECT_EQ(ASCIIToUTF16("8004328765"),
122            phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
123
124  // Invalid numbers should not be stored.  In the US, phone numbers cannot
125  // start with the digit '1'.
126  EXPECT_FALSE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
127                             ASCIIToUTF16("650111111"), "US"));
128  EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
129}
130
131// Test that cached phone numbers are correctly invalidated and updated.
132TEST(PhoneNumberTest, UpdateCachedPhoneNumber) {
133  AutofillProfile profile;
134  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
135
136  PhoneNumber phone(&profile);
137  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("6502345678"));
138  EXPECT_EQ(ASCIIToUTF16("650"),
139            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
140
141  // Update the area code.
142  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8322345678"));
143  EXPECT_EQ(ASCIIToUTF16("832"),
144            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
145
146  // Change the phone number to have a UK format, but try to parse with the
147  // wrong locale.
148  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789"));
149  EXPECT_EQ(base::string16(),
150            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
151
152  // Now try parsing using the correct locale.  Note that the profile's country
153  // code should override the app locale, which is still set to "US".
154  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
155  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789"));
156  EXPECT_EQ(ASCIIToUTF16("70"),
157            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
158}
159
160TEST(PhoneNumberTest, PhoneCombineHelper) {
161  AutofillProfile profile;
162  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
163
164  PhoneNumber::PhoneCombineHelper number1;
165  EXPECT_FALSE(number1.SetInfo(AutofillType(ADDRESS_BILLING_CITY),
166                               ASCIIToUTF16("1")));
167  EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_COUNTRY_CODE),
168                              ASCIIToUTF16("1")));
169  EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
170                              ASCIIToUTF16("650")));
171  EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_NUMBER),
172                              ASCIIToUTF16("2345678")));
173  base::string16 parsed_phone;
174  EXPECT_TRUE(number1.ParseNumber(profile, "en-US", &parsed_phone));
175  // International format as it has a country code.
176  EXPECT_EQ(ASCIIToUTF16("1 650-234-5678"), parsed_phone);
177
178  PhoneNumber::PhoneCombineHelper number3;
179  EXPECT_TRUE(number3.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
180                              ASCIIToUTF16("650")));
181  EXPECT_TRUE(number3.SetInfo(AutofillType(PHONE_HOME_NUMBER),
182                              ASCIIToUTF16("2345680")));
183  EXPECT_TRUE(number3.ParseNumber(profile, "en-US", &parsed_phone));
184  // National format as it does not have a country code.
185  EXPECT_EQ(ASCIIToUTF16("(650) 234-5680"), parsed_phone);
186
187  PhoneNumber::PhoneCombineHelper number4;
188  EXPECT_TRUE(number4.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
189                              ASCIIToUTF16("123")));  // Incorrect city code.
190  EXPECT_TRUE(number4.SetInfo(AutofillType(PHONE_HOME_NUMBER),
191                              ASCIIToUTF16("2345680")));
192  EXPECT_FALSE(number4.ParseNumber(profile, "en-US", &parsed_phone));
193  EXPECT_EQ(base::string16(), parsed_phone);
194
195  PhoneNumber::PhoneCombineHelper number5;
196  EXPECT_TRUE(number5.SetInfo(AutofillType(PHONE_HOME_CITY_AND_NUMBER),
197                              ASCIIToUTF16("6502345681")));
198  EXPECT_TRUE(number5.ParseNumber(profile, "en-US", &parsed_phone));
199  EXPECT_EQ(ASCIIToUTF16("(650) 234-5681"), parsed_phone);
200
201  PhoneNumber::PhoneCombineHelper number6;
202  EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
203                              ASCIIToUTF16("650")));
204  EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_NUMBER),
205                              ASCIIToUTF16("234")));
206  EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_NUMBER),
207                              ASCIIToUTF16("5682")));
208  EXPECT_TRUE(number6.ParseNumber(profile, "en-US", &parsed_phone));
209  EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone);
210
211  // Ensure parsing is possible when falling back to detecting the country code
212  // based on the app locale.
213  PhoneNumber::PhoneCombineHelper number7;
214  EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
215                              ASCIIToUTF16("650")));
216  EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_NUMBER),
217                              ASCIIToUTF16("234")));
218  EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_NUMBER),
219                              ASCIIToUTF16("5682")));
220  EXPECT_TRUE(number7.ParseNumber(AutofillProfile(), "en-US", &parsed_phone));
221  EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone);
222}
223
224}  // namespace autofill
225