| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/string16.h" | 5 #include "base/string16.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "components/autofill/browser/autofill_profile.h" | 7 #include "components/autofill/browser/autofill_profile.h" |
| 8 #include "components/autofill/browser/field_types.h" | 8 #include "components/autofill/browser/field_types.h" |
| 9 #include "components/autofill/browser/phone_number.h" | 9 #include "components/autofill/browser/phone_number.h" |
| 10 #include "components/autofill/browser/phone_number_i18n.h" | 10 #include "components/autofill/browser/phone_number_i18n.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 TEST(PhoneNumberTest, Matcher) { | 13 TEST(PhoneNumberTest, Matcher) { |
| 14 AutofillProfile profile; | 14 AutofillProfile profile; |
| 15 profile.SetCountryCode("US"); | 15 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
| 16 // Set phone number so country_code == 1, city_code = 650, number = 2345678. | 16 // Set phone number so country_code == 1, city_code = 650, number = 2345678. |
| 17 string16 phone(ASCIIToUTF16("1 [650] 234-5678")); | 17 string16 phone(ASCIIToUTF16("1 [650] 234-5678")); |
| 18 PhoneNumber phone_number(&profile); | 18 PhoneNumber phone_number(&profile); |
| 19 phone_number.SetInfo(PHONE_HOME_WHOLE_NUMBER, phone, "US"); | 19 phone_number.SetInfo(PHONE_HOME_WHOLE_NUMBER, phone, "US"); |
| 20 | 20 |
| 21 FieldTypeSet matching_types; | 21 FieldTypeSet matching_types; |
| 22 phone_number.GetMatchingTypes(string16(), "US", &matching_types); | 22 phone_number.GetMatchingTypes(string16(), "US", &matching_types); |
| 23 EXPECT_EQ(1U, matching_types.size()); | 23 EXPECT_EQ(1U, matching_types.size()); |
| 24 EXPECT_TRUE(matching_types.find(EMPTY_TYPE) != matching_types.end()); | 24 EXPECT_TRUE(matching_types.find(EMPTY_TYPE) != matching_types.end()); |
| 25 matching_types.clear(); | 25 matching_types.clear(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 phone_number.GetMatchingTypes(ASCIIToUTF16("(650)2345678"), "US", | 76 phone_number.GetMatchingTypes(ASCIIToUTF16("(650)2345678"), "US", |
| 77 &matching_types); | 77 &matching_types); |
| 78 EXPECT_EQ(1U, matching_types.size()); | 78 EXPECT_EQ(1U, matching_types.size()); |
| 79 EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_AND_NUMBER) != | 79 EXPECT_TRUE(matching_types.find(PHONE_HOME_CITY_AND_NUMBER) != |
| 80 matching_types.end()); | 80 matching_types.end()); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Verify that PhoneNumber::SetInfo() correctly formats the incoming number. | 83 // Verify that PhoneNumber::SetInfo() correctly formats the incoming number. |
| 84 TEST(PhoneNumberTest, SetInfo) { | 84 TEST(PhoneNumberTest, SetInfo) { |
| 85 AutofillProfile profile; | 85 AutofillProfile profile; |
| 86 profile.SetCountryCode("US"); | 86 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
| 87 | 87 |
| 88 PhoneNumber phone(&profile); | 88 PhoneNumber phone(&profile); |
| 89 EXPECT_EQ(string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 89 EXPECT_EQ(string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
| 90 | 90 |
| 91 // Set the formatted info directly. | 91 // Set the formatted info directly. |
| 92 EXPECT_TRUE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, | 92 EXPECT_TRUE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, |
| 93 ASCIIToUTF16("(650) 234-5678"), "US")); | 93 ASCIIToUTF16("(650) 234-5678"), "US")); |
| 94 EXPECT_EQ(ASCIIToUTF16("(650) 234-5678"), | 94 EXPECT_EQ(ASCIIToUTF16("(650) 234-5678"), |
| 95 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 95 phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
| 96 | 96 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 109 // Invalid numbers should not be stored. In the US, phone numbers cannot | 109 // Invalid numbers should not be stored. In the US, phone numbers cannot |
| 110 // start with the digit '1'. | 110 // start with the digit '1'. |
| 111 EXPECT_FALSE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, | 111 EXPECT_FALSE(phone.SetInfo(PHONE_HOME_WHOLE_NUMBER, |
| 112 ASCIIToUTF16("650111111"), "US")); | 112 ASCIIToUTF16("650111111"), "US")); |
| 113 EXPECT_EQ(string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 113 EXPECT_EQ(string16(), phone.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Test that cached phone numbers are correctly invalidated and updated. | 116 // Test that cached phone numbers are correctly invalidated and updated. |
| 117 TEST(PhoneNumberTest, UpdateCachedPhoneNumber) { | 117 TEST(PhoneNumberTest, UpdateCachedPhoneNumber) { |
| 118 AutofillProfile profile; | 118 AutofillProfile profile; |
| 119 profile.SetCountryCode("US"); | 119 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
| 120 | 120 |
| 121 PhoneNumber phone(&profile); | 121 PhoneNumber phone(&profile); |
| 122 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("6502345678")); | 122 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("6502345678")); |
| 123 EXPECT_EQ(ASCIIToUTF16("650"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 123 EXPECT_EQ(ASCIIToUTF16("650"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); |
| 124 | 124 |
| 125 // Update the area code. | 125 // Update the area code. |
| 126 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8322345678")); | 126 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("8322345678")); |
| 127 EXPECT_EQ(ASCIIToUTF16("832"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 127 EXPECT_EQ(ASCIIToUTF16("832"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); |
| 128 | 128 |
| 129 // Change the phone number to have a UK format, but try to parse with the | 129 // Change the phone number to have a UK format, but try to parse with the |
| 130 // wrong locale. | 130 // wrong locale. |
| 131 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); | 131 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); |
| 132 EXPECT_EQ(string16(), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 132 EXPECT_EQ(string16(), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); |
| 133 | 133 |
| 134 // Now try parsing using the correct locale. Note that the profile's country | 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". | 135 // code should override the app locale, which is still set to "US". |
| 136 profile.SetCountryCode("GB"); | 136 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB")); |
| 137 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); | 137 phone.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("07023456789")); |
| 138 EXPECT_EQ(ASCIIToUTF16("70"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); | 138 EXPECT_EQ(ASCIIToUTF16("70"), phone.GetInfo(PHONE_HOME_CITY_CODE, "US")); |
| 139 } | 139 } |
| 140 | 140 |
| 141 TEST(PhoneNumberTest, PhoneCombineHelper) { | 141 TEST(PhoneNumberTest, PhoneCombineHelper) { |
| 142 AutofillProfile profile; | 142 AutofillProfile profile; |
| 143 profile.SetCountryCode("US"); | 143 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); |
| 144 | 144 |
| 145 PhoneNumber::PhoneCombineHelper number1; | 145 PhoneNumber::PhoneCombineHelper number1; |
| 146 EXPECT_FALSE(number1.SetInfo(ADDRESS_BILLING_CITY, | 146 EXPECT_FALSE(number1.SetInfo(ADDRESS_BILLING_CITY, |
| 147 ASCIIToUTF16("1"))); | 147 ASCIIToUTF16("1"))); |
| 148 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_COUNTRY_CODE, | 148 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_COUNTRY_CODE, |
| 149 ASCIIToUTF16("1"))); | 149 ASCIIToUTF16("1"))); |
| 150 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_CITY_CODE, | 150 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_CITY_CODE, |
| 151 ASCIIToUTF16("650"))); | 151 ASCIIToUTF16("650"))); |
| 152 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_NUMBER, | 152 EXPECT_TRUE(number1.SetInfo(PHONE_HOME_NUMBER, |
| 153 ASCIIToUTF16("2345678"))); | 153 ASCIIToUTF16("2345678"))); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 PhoneNumber::PhoneCombineHelper number7; | 194 PhoneNumber::PhoneCombineHelper number7; |
| 195 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_CITY_CODE, | 195 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_CITY_CODE, |
| 196 ASCIIToUTF16("650"))); | 196 ASCIIToUTF16("650"))); |
| 197 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_NUMBER, | 197 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_NUMBER, |
| 198 ASCIIToUTF16("234"))); | 198 ASCIIToUTF16("234"))); |
| 199 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_NUMBER, | 199 EXPECT_TRUE(number7.SetInfo(PHONE_HOME_NUMBER, |
| 200 ASCIIToUTF16("5682"))); | 200 ASCIIToUTF16("5682"))); |
| 201 EXPECT_TRUE(number7.ParseNumber(AutofillProfile(), "en-US", &parsed_phone)); | 201 EXPECT_TRUE(number7.ParseNumber(AutofillProfile(), "en-US", &parsed_phone)); |
| 202 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone); | 202 EXPECT_EQ(ASCIIToUTF16("(650) 234-5682"), parsed_phone); |
| 203 } | 203 } |
| OLD | NEW |