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