| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "components/payments/core/payment_request_data_util.h" | 5 #include "components/payments/core/payment_request_data_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "components/autofill/core/browser/autofill_profile.h" | 13 #include "components/autofill/core/browser/autofill_profile.h" |
| 13 #include "components/autofill/core/browser/autofill_test_utils.h" | 14 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 14 #include "components/autofill/core/browser/credit_card.h" | 15 #include "components/autofill/core/browser/credit_card.h" |
| 15 #include "components/payments/core/basic_card_response.h" | 16 #include "components/payments/core/basic_card_response.h" |
| 16 #include "components/payments/core/payment_address.h" | 17 #include "components/payments/core/payment_address.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace payments { | 20 namespace payments { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Tests that the phone numbers are correctly formatted to display to the user. | 86 // Tests that the phone numbers are correctly formatted to display to the user. |
| 86 TEST(PaymentRequestDataUtilTest, FormatPhoneForDisplay) { | 87 TEST(PaymentRequestDataUtilTest, FormatPhoneForDisplay) { |
| 87 EXPECT_EQ("+1 515-123-1234", | 88 EXPECT_EQ("+1 515-123-1234", |
| 88 payments::data_util::FormatPhoneForDisplay("5151231234", "US")); | 89 payments::data_util::FormatPhoneForDisplay("5151231234", "US")); |
| 89 EXPECT_EQ("+33 1 42 68 53 00", | 90 EXPECT_EQ("+33 1 42 68 53 00", |
| 90 payments::data_util::FormatPhoneForDisplay("142685300", "FR")); | 91 payments::data_util::FormatPhoneForDisplay("142685300", "FR")); |
| 91 } | 92 } |
| 92 | 93 |
| 94 // Test for the GetFormattedPhoneNumberForDisplay method. |
| 95 struct PhoneNumberFormatCase { |
| 96 PhoneNumberFormatCase(const char* phone, |
| 97 const char* country, |
| 98 const char* expected_format, |
| 99 const char* locale = "") |
| 100 : phone(phone), |
| 101 country(country), |
| 102 expected_format(expected_format), |
| 103 locale(locale) {} |
| 104 |
| 105 const char* phone; |
| 106 const char* country; |
| 107 const char* expected_format; |
| 108 const char* locale; |
| 109 }; |
| 110 |
| 111 class GetFormattedPhoneNumberForDisplayTest |
| 112 : public testing::TestWithParam<PhoneNumberFormatCase> {}; |
| 113 |
| 114 TEST_P(GetFormattedPhoneNumberForDisplayTest, |
| 115 GetFormattedPhoneNumberForDisplay) { |
| 116 autofill::AutofillProfile profile; |
| 117 profile.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, |
| 118 base::UTF8ToUTF16(GetParam().phone)); |
| 119 profile.SetRawInfo(autofill::ADDRESS_HOME_COUNTRY, |
| 120 base::UTF8ToUTF16(GetParam().country)); |
| 121 EXPECT_EQ(GetParam().expected_format, |
| 122 base::UTF16ToUTF8( |
| 123 GetFormattedPhoneNumberForDisplay(profile, GetParam().locale))); |
| 124 } |
| 125 |
| 126 INSTANTIATE_TEST_CASE_P( |
| 127 GetFormattedPhoneNumberForDisplay, |
| 128 GetFormattedPhoneNumberForDisplayTest, |
| 129 testing::Values( |
| 130 ////////////////////////// |
| 131 // US phone in US. |
| 132 ////////////////////////// |
| 133 // Formatted phone numbers. |
| 134 PhoneNumberFormatCase("+1 415-555-5555", "US", "+1 415-555-5555"), |
| 135 PhoneNumberFormatCase("1 415-555-5555", "US", "+1 415-555-5555"), |
| 136 PhoneNumberFormatCase("415-555-5555", "US", "+1 415-555-5555"), |
| 137 // Raw phone numbers. |
| 138 PhoneNumberFormatCase("+14155555555", "US", "+1 415-555-5555"), |
| 139 PhoneNumberFormatCase("14155555555", "US", "+1 415-555-5555"), |
| 140 PhoneNumberFormatCase("4155555555", "US", "+1 415-555-5555"), |
| 141 |
| 142 ////////////////////////// |
| 143 // US phone in CA. |
| 144 ////////////////////////// |
| 145 // Formatted phone numbers. |
| 146 PhoneNumberFormatCase("+1 415-555-5555", "CA", "+1 415-555-5555"), |
| 147 PhoneNumberFormatCase("1 415-555-5555", "CA", "+1 415-555-5555"), |
| 148 PhoneNumberFormatCase("415-555-5555", "CA", "+1 415-555-5555"), |
| 149 // Raw phone numbers. |
| 150 PhoneNumberFormatCase("+14155555555", "CA", "+1 415-555-5555"), |
| 151 PhoneNumberFormatCase("14155555555", "CA", "+1 415-555-5555"), |
| 152 PhoneNumberFormatCase("4155555555", "CA", "+1 415-555-5555"), |
| 153 |
| 154 ////////////////////////// |
| 155 // US phone in AU. |
| 156 ////////////////////////// |
| 157 // A US phone with the country code is correctly formatted as an US |
| 158 // number. |
| 159 PhoneNumberFormatCase("+1 415-555-5555", "AU", "+1 415-555-5555"), |
| 160 PhoneNumberFormatCase("1 415-555-5555", "AU", "+1 415-555-5555"), |
| 161 // Without a country code, the phone is formatted for the profile's |
| 162 // country, even if it's invalid. |
| 163 PhoneNumberFormatCase("415-555-5555", "AU", "+61 4155555555"), |
| 164 |
| 165 ////////////////////////// |
| 166 // US phone in MX. |
| 167 ////////////////////////// |
| 168 // A US phone with the country code is correctly formatted as an US |
| 169 // number. |
| 170 PhoneNumberFormatCase("+1 415-555-5555", "MX", "+1 415-555-5555"), |
| 171 // "+52 1 415 555 5555" is a valid number for Mexico, |
| 172 PhoneNumberFormatCase("1 415-555-5555", "MX", "+52 1 415 555 5555"), |
| 173 // Without a country code, the phone is formatted for the profile's |
| 174 // country. |
| 175 PhoneNumberFormatCase("415-555-5555", "MX", "+52 415 555 5555"), |
| 176 |
| 177 ////////////////////////// |
| 178 // AU phone in AU. |
| 179 ////////////////////////// |
| 180 // Formatted phone numbers. |
| 181 PhoneNumberFormatCase("+61 2 9374 4000", "AU", "+61 2 9374 4000"), |
| 182 PhoneNumberFormatCase("61 2 9374 4000", "AU", "+61 2 9374 4000"), |
| 183 PhoneNumberFormatCase("02 9374 4000", "AU", "+61 2 9374 4000"), |
| 184 PhoneNumberFormatCase("2 9374 4000", "AU", "+61 2 9374 4000"), |
| 185 // Raw phone numbers. |
| 186 PhoneNumberFormatCase("+61293744000", "AU", "+61 2 9374 4000"), |
| 187 PhoneNumberFormatCase("61293744000", "AU", "+61 2 9374 4000"), |
| 188 PhoneNumberFormatCase("0293744000", "AU", "+61 2 9374 4000"), |
| 189 PhoneNumberFormatCase("293744000", "AU", "+61 2 9374 4000"), |
| 190 |
| 191 ////////////////////////// |
| 192 // AU phone in US. |
| 193 ////////////////////////// |
| 194 // An AU phone with the country code is correctly formatted as an AU |
| 195 // number. |
| 196 PhoneNumberFormatCase("+61 2 9374 4000", "US", "+61 2 9374 4000"), |
| 197 PhoneNumberFormatCase("61 2 9374 4000", "US", "+61 2 9374 4000"), |
| 198 // Without a country code, the phone is formatted for the profile's |
| 199 // country. |
| 200 // This local AU number fits the length of a US number, so it's |
| 201 // formatted for US. |
| 202 PhoneNumberFormatCase("02 9374 4000", "US", "+1 029-374-4000"), |
| 203 // This local AU number is formatted as an US number, even if it's |
| 204 // invlaid. |
| 205 PhoneNumberFormatCase("2 9374 4000", "US", "+1 293744000"), |
| 206 |
| 207 ////////////////////////// |
| 208 // MX phone in MX. |
| 209 ////////////////////////// |
| 210 // Formatted phone numbers. |
| 211 PhoneNumberFormatCase("+52 55 5342 8400", "MX", "+52 55 5342 8400"), |
| 212 PhoneNumberFormatCase("52 55 5342 8400", "MX", "+52 55 5342 8400"), |
| 213 PhoneNumberFormatCase("55 5342 8400", "MX", "+52 55 5342 8400"), |
| 214 // Raw phone numbers. |
| 215 PhoneNumberFormatCase("+525553428400", "MX", "+52 55 5342 8400"), |
| 216 PhoneNumberFormatCase("525553428400", "MX", "+52 55 5342 8400"), |
| 217 PhoneNumberFormatCase("5553428400", "MX", "+52 55 5342 8400"), |
| 218 |
| 219 ////////////////////////// |
| 220 // MX phone in US. |
| 221 ////////////////////////// |
| 222 // A MX phone with the country code is correctly formatted as a MX |
| 223 // number. |
| 224 PhoneNumberFormatCase("+52 55 5342 8400", "US", "+52 55 5342 8400"), |
| 225 PhoneNumberFormatCase("52 55 5342 8400", "US", "+52 55 5342 8400"), |
| 226 // This local MX number fits the length of a US number, so it's |
| 227 // formatted for US. |
| 228 PhoneNumberFormatCase("55 5342 8400", "US", "+1 555-342-8400"))); |
| 229 |
| 230 INSTANTIATE_TEST_CASE_P( |
| 231 GetFormattedPhoneNumberForDisplay_EdgeCases, |
| 232 GetFormattedPhoneNumberForDisplayTest, |
| 233 testing::Values( |
| 234 ////////////////////////// |
| 235 // No country. |
| 236 ////////////////////////// |
| 237 // Fallback to locale if no country is set. |
| 238 PhoneNumberFormatCase("52 55 5342 8400", |
| 239 "", |
| 240 "+52 55 5342 8400", |
| 241 "es_MX"), |
| 242 PhoneNumberFormatCase("55 5342 8400", "", "+52 55 5342 8400", "es_MX"), |
| 243 PhoneNumberFormatCase("55 5342 8400", "", "+1 555-342-8400", "en_US"), |
| 244 PhoneNumberFormatCase("61 2 9374 4000", "", "+61 2 9374 4000", "en_AU"), |
| 245 PhoneNumberFormatCase("02 9374 4000", "", "+61 2 9374 4000", "en_AU"), |
| 246 |
| 247 ////////////////////////// |
| 248 // No country or locale. |
| 249 ////////////////////////// |
| 250 // Format according to the country code. |
| 251 PhoneNumberFormatCase("61 2 9374 4000", "", "+61 2 9374 4000"), |
| 252 PhoneNumberFormatCase("52 55 5342 8400", "", "+52 55 5342 8400"), |
| 253 PhoneNumberFormatCase("1 415 555 5555", "", "+1 415-555-5555"), |
| 254 // If no country code is found, formats for US. |
| 255 PhoneNumberFormatCase("02 9374 4000", "", "+1 029-374-4000"), |
| 256 PhoneNumberFormatCase("2 9374 4000", "", "+1 293744000"), |
| 257 PhoneNumberFormatCase("55 5342 8400", "", "+1 555-342-8400"), |
| 258 PhoneNumberFormatCase("52 55 5342 8400", "", "+52 55 5342 8400"), |
| 259 PhoneNumberFormatCase("415-555-5555", "", "+1 415-555-5555"))); |
| 93 } // namespace data_util | 260 } // namespace data_util |
| 94 } // namespace payments | 261 } // namespace payments |
| OLD | NEW |