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