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