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
108  // Differently formatted numbers should be re-formatted.
109  EXPECT_TRUE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
110                            ASCIIToUTF16("800-432-8765"), "US"));
111  EXPECT_EQ(ASCIIToUTF16("(800) 432-8765"),
112            phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
113
114  // Invalid numbers should not be stored.  In the US, phone numbers cannot
115  // start with the digit '1'.
116  EXPECT_FALSE(phone.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER),
117                             ASCIIToUTF16("650111111"), "US"));
118  EXPECT_EQ(base::string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
119}
120
121// Test that cached phone numbers are correctly invalidated and updated.
122TEST(PhoneNumberTest, UpdateCachedPhoneNumber) {
123  AutofillProfile profile;
124  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
125
126  PhoneNumber phone(&profile);
127  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("6502345678"));
128  EXPECT_EQ(ASCIIToUTF16("650"),
129            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
130
131  // Update the area code.
132  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8322345678"));
133  EXPECT_EQ(ASCIIToUTF16("832"),
134            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
135
136  // Change the phone number to have a UK format, but try to parse with the
137  // wrong locale.
138  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789"));
139  EXPECT_EQ(base::string16(),
140            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
141
142  // Now try parsing using the correct locale.  Note that the profile's country
143  // code should override the app locale, which is still set to "US".
144  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
145  phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789"));
146  EXPECT_EQ(ASCIIToUTF16("70"),
147            phone.GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
148}
149
150TEST(PhoneNumberTest, PhoneCombineHelper) {
151  AutofillProfile profile;
152  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
153
154  PhoneNumber::PhoneCombineHelper number1;
155  EXPECT_FALSE(number1.SetInfo(AutofillType(ADDRESS_BILLING_CITY),
156                               ASCIIToUTF16("1")));
157  EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_COUNTRY_CODE),
158                              ASCIIToUTF16("1")));
159  EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
160                              ASCIIToUTF16("650")));
161  EXPECT_TRUE(number1.SetInfo(AutofillType(PHONE_HOME_NUMBER),
162                              ASCIIToUTF16("2345678")));
163  base::string16 parsed_phone;
164  EXPECT_TRUE(number1.ParseNumber(profile, "en-US", &parsed_phone));
165  // International format as it has a country code.
166  EXPECT_EQ(ASCIIToUTF16("+1 650-234-5678"), parsed_phone);
167
168  PhoneNumber::PhoneCombineHelper number3;
169  EXPECT_TRUE(number3.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
170                              ASCIIToUTF16("650")));
171  EXPECT_TRUE(number3.SetInfo(AutofillType(PHONE_HOME_NUMBER),
172                              ASCIIToUTF16("2345680")));
173  EXPECT_TRUE(number3.ParseNumber(profile, "en-US", &parsed_phone));
174  // National format as it does not have a country code.
175  EXPECT_EQ(ASCIIToUTF16("(650) 234-5680"), parsed_phone);
176
177  PhoneNumber::PhoneCombineHelper number4;
178  EXPECT_TRUE(number4.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
179                              ASCIIToUTF16("123")));  // Incorrect city code.
180  EXPECT_TRUE(number4.SetInfo(AutofillType(PHONE_HOME_NUMBER),
181                              ASCIIToUTF16("2345680")));
182  EXPECT_FALSE(number4.ParseNumber(profile, "en-US", &parsed_phone));
183  EXPECT_EQ(base::string16(), parsed_phone);
184
185  PhoneNumber::PhoneCombineHelper number5;
186  EXPECT_TRUE(number5.SetInfo(AutofillType(PHONE_HOME_CITY_AND_NUMBER),
187                              ASCIIToUTF16("6502345681")));
188  EXPECT_TRUE(number5.ParseNumber(profile, "en-US", &parsed_phone));
189  EXPECT_EQ(ASCIIToUTF16("(650) 234-5681"), parsed_phone);
190
191  PhoneNumber::PhoneCombineHelper number6;
192  EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
193                              ASCIIToUTF16("650")));
194  EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_NUMBER),
195                              ASCIIToUTF16("234")));
196  EXPECT_TRUE(number6.SetInfo(AutofillType(PHONE_HOME_NUMBER),
197                              ASCIIToUTF16("5682")));
198  EXPECT_TRUE(number6.ParseNumber(profile, "en-US", &parsed_phone));
199  EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone);
200
201  // Ensure parsing is possible when falling back to detecting the country code
202  // based on the app locale.
203  PhoneNumber::PhoneCombineHelper number7;
204  EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_CITY_CODE),
205                              ASCIIToUTF16("650")));
206  EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_NUMBER),
207                              ASCIIToUTF16("234")));
208  EXPECT_TRUE(number7.SetInfo(AutofillType(PHONE_HOME_NUMBER),
209                              ASCIIToUTF16("5682")));
210  EXPECT_TRUE(number7.ParseNumber(AutofillProfile(), "en-US", &parsed_phone));
211  EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone);
212}
213
214}  // namespace autofill
215