| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 base::string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5")); | 39 base::string16 phone3(UTF8ToUTF16("16503334\xef\xbc\x92\x35\xd9\xa5")); |
| 40 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255")); | 40 EXPECT_EQ(NormalizePhoneNumber(phone3, "US"), ASCIIToUTF16("16503334255")); |
| 41 | 41 |
| 42 base::string16 phone4(UTF8ToUTF16("+1(650)2346789")); | 42 base::string16 phone4(UTF8ToUTF16("+1(650)2346789")); |
| 43 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789")); | 43 EXPECT_EQ(NormalizePhoneNumber(phone4, "US"), ASCIIToUTF16("16502346789")); |
| 44 | 44 |
| 45 base::string16 phone5(UTF8ToUTF16("6502346789")); | 45 base::string16 phone5(UTF8ToUTF16("6502346789")); |
| 46 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789")); | 46 EXPECT_EQ(NormalizePhoneNumber(phone5, "US"), ASCIIToUTF16("6502346789")); |
| 47 } | 47 } |
| 48 | 48 |
| 49 TEST(PhoneNumberI18NTest, ParsePhoneNumber) { | 49 struct ParseNumberTestCase { |
| 50 const struct test_case { | 50 // Expected parsing result. |
| 51 // Expected parsing result. | 51 bool valid; |
| 52 bool valid; | 52 // Inputs. |
| 53 // Inputs. | 53 std::string input; |
| 54 std::string input; | 54 std::string assumed_region; |
| 55 std::string assumed_region; | 55 // Further expectations. |
| 56 // Further expectations. | 56 std::string number; |
| 57 std::string number; | 57 std::string city_code; |
| 58 std::string city_code; | 58 std::string country_code; |
| 59 std::string country_code; | 59 std::string deduced_region; |
| 60 std::string deduced_region; | 60 }; |
| 61 } test_cases[] = { | 61 |
| 62 class ParseNumberTest : public testing::TestWithParam<ParseNumberTestCase> {}; |
| 63 |
| 64 TEST_P(ParseNumberTest, ParsePhoneNumber) { |
| 65 auto test_case = GetParam(); |
| 66 SCOPED_TRACE("Testing phone number " + test_case.input); |
| 67 |
| 68 base::string16 country_code, city_code, number; |
| 69 std::string deduced_region; |
| 70 ::i18n::phonenumbers::PhoneNumber unused_i18n_number; |
| 71 EXPECT_EQ( |
| 72 test_case.valid, |
| 73 ParsePhoneNumber(ASCIIToUTF16(test_case.input), test_case.assumed_region, |
| 74 &country_code, &city_code, &number, &deduced_region, |
| 75 &unused_i18n_number)); |
| 76 EXPECT_EQ(ASCIIToUTF16(test_case.number), number); |
| 77 EXPECT_EQ(ASCIIToUTF16(test_case.city_code), city_code); |
| 78 EXPECT_EQ(ASCIIToUTF16(test_case.country_code), country_code); |
| 79 EXPECT_EQ(test_case.deduced_region, deduced_region); |
| 80 } |
| 81 |
| 82 INSTANTIATE_TEST_CASE_P( |
| 83 PhoneNumberI18NTest, |
| 84 ParseNumberTest, |
| 85 testing::Values( |
| 62 // Test for empty string. Should give back empty strings. | 86 // Test for empty string. Should give back empty strings. |
| 63 {false, "", "US"}, | 87 ParseNumberTestCase{false, "", "US"}, |
| 64 // Test for string with less than 7 digits. Should give back empty | 88 // Test for string with less than 7 digits. Should give back empty |
| 65 // strings. | 89 // strings. |
| 66 {false, "1234", "US"}, | 90 ParseNumberTestCase{false, "1234", "US"}, |
| 67 // Test for string with exactly 7 digits. | 91 // Test for string with exactly 7 digits. |
| 68 // Not a valid number - starts with 1 | 92 // Not a valid number - starts with 1 |
| 69 {false, "17134567", "US"}, | 93 ParseNumberTestCase{false, "17134567", "US"}, |
| 70 // Not a valid number - does not have area code. | 94 // Not a valid number - does not have area code. |
| 71 {false, "7134567", "US"}, | 95 ParseNumberTestCase{false, "7134567", "US"}, |
| 72 // Valid Canadian toll-free number. | 96 // Valid Canadian toll-free number. |
| 73 {true, "3101234", "US", "3101234", "", "", "CA"}, | 97 ParseNumberTestCase{true, "3101234", "US", "3101234", "", "", "CA"}, |
| 74 // Test for string with greater than 7 digits but less than 10 digits. | 98 // Test for string with greater than 7 digits but less than 10 digits. |
| 75 // Should fail parsing in US. | 99 // Should fail parsing in US. |
| 76 {false, "123456789", "US"}, | 100 ParseNumberTestCase{false, "123456789", "US"}, |
| 77 // Test for string with greater than 7 digits but less than 10 digits | 101 // Test for string with greater than 7 digits but less than 10 digits |
| 78 // and | 102 // and |
| 79 // separators. | 103 // separators. |
| 80 // Should fail parsing in US. | 104 // Should fail parsing in US. |
| 81 {false, "12.345-6789", "US"}, | 105 ParseNumberTestCase{false, "12.345-6789", "US"}, |
| 82 // Test for string with exactly 10 digits. | 106 // Test for string with exactly 10 digits. |
| 83 // Should give back phone number and city code. | 107 // Should give back phone number and city code. |
| 84 // This one going to fail because of the incorrect area code. | 108 // This one going to fail because of the incorrect area code. |
| 85 {false, "1234567890", "US"}, | 109 ParseNumberTestCase{false, "1234567890", "US"}, |
| 86 // This one going to fail because of the incorrect number (starts with | 110 // This one going to fail because of the incorrect number (starts with |
| 87 // 1). | 111 // 1). |
| 88 {false, "6501567890", "US"}, | 112 ParseNumberTestCase{false, "6501567890", "US"}, |
| 89 {true, "6504567890", "US", "4567890", "650", "", "US"}, | 113 ParseNumberTestCase{true, "6504567890", "US", "4567890", "650", "", |
| 114 "US"}, |
| 90 // Test for string with exactly 10 digits and separators. | 115 // Test for string with exactly 10 digits and separators. |
| 91 // Should give back phone number and city code. | 116 // Should give back phone number and city code. |
| 92 {true, "(650) 456-7890", "US", "4567890", "650", "", "US"}, | 117 ParseNumberTestCase{true, "(650) 456-7890", "US", "4567890", "650", "", |
| 118 "US"}, |
| 93 // Tests for string with over 10 digits. | 119 // Tests for string with over 10 digits. |
| 94 // 01 is incorrect prefix in the USA, and if we interpret 011 as prefix, | 120 // 01 is incorrect prefix in the USA, and if we interpret 011 as prefix, |
| 95 // the | 121 // the |
| 96 // rest is too short for international number - the parsing should fail. | 122 // rest is too short for international number - the parsing should fail. |
| 97 {false, "0116504567890", "US"}, | 123 ParseNumberTestCase{false, "0116504567890", "US"}, |
| 98 // 011 is a correct "dial out" prefix in the USA - the parsing should | 124 // 011 is a correct "dial out" prefix in the USA - the parsing should |
| 99 // succeed. | 125 // succeed. |
| 100 {true, "01116504567890", "US", "4567890", "650", "1", "US"}, | 126 ParseNumberTestCase{true, "01116504567890", "US", "4567890", "650", "1", |
| 127 "US"}, |
| 101 // 011 is a correct "dial out" prefix in the USA but the rest of the | 128 // 011 is a correct "dial out" prefix in the USA but the rest of the |
| 102 // number | 129 // number |
| 103 // can't parse as a US number. | 130 // can't parse as a US number. |
| 104 {true, "01178124567890", "US", "4567890", "812", "7", "RU"}, | 131 ParseNumberTestCase{true, "01178124567890", "US", "4567890", "812", "7", |
| 132 "RU"}, |
| 105 // Test for string with over 10 digits with separator characters. | 133 // Test for string with over 10 digits with separator characters. |
| 106 // Should give back phone number, city code, and country code. "011" is | 134 // Should give back phone number, city code, and country code. "011" is |
| 107 // US "dial out" code, which is discarded. | 135 // US "dial out" code, which is discarded. |
| 108 {true, "(0111) 650-456.7890", "US", "4567890", "650", "1", "US"}, | 136 ParseNumberTestCase{true, "(0111) 650-456.7890", "US", "4567890", "650", |
| 137 "1", "US"}, |
| 109 // Now try phone from Czech republic - it has 00 dial out code, 420 | 138 // Now try phone from Czech republic - it has 00 dial out code, 420 |
| 110 // country | 139 // country |
| 111 // code and variable length area codes. | 140 // code and variable length area codes. |
| 112 {true, "+420 27-89.10.112", "US", "910112", "278", "420", "CZ"}, | 141 ParseNumberTestCase{true, "+420 27-89.10.112", "US", "910112", "278", |
| 113 {false, "27-89.10.112", "US"}, | 142 "420", "CZ"}, |
| 114 {true, "27-89.10.112", "CZ", "910112", "278", "", "CZ"}, | 143 ParseNumberTestCase{false, "27-89.10.112", "US"}, |
| 115 {false, "420 57-89.10.112", "US"}, | 144 ParseNumberTestCase{true, "27-89.10.112", "CZ", "910112", "278", "", |
| 116 {true, "420 57-89.10.112", "CZ", "910112", "578", "420", "CZ"}, | 145 "CZ"}, |
| 146 ParseNumberTestCase{false, "420 57-89.10.112", "US"}, |
| 147 ParseNumberTestCase{true, "420 57-89.10.112", "CZ", "910112", "578", |
| 148 "420", "CZ"}, |
| 117 // Parses vanity numbers. | 149 // Parses vanity numbers. |
| 118 {true, "1-650-FLOWERS", "US", "3569377", "650", "1", "US"}, | 150 ParseNumberTestCase{true, "1-650-FLOWERS", "US", "3569377", "650", "1", |
| 151 "US"}, |
| 119 // 800 is not an area code, but the destination code. In our library | 152 // 800 is not an area code, but the destination code. In our library |
| 120 // these | 153 // these |
| 121 // codes should be treated the same as area codes. | 154 // codes should be treated the same as area codes. |
| 122 {true, "1-800-FLOWERS", "US", "3569377", "800", "1", "US"}, | 155 ParseNumberTestCase{true, "1-800-FLOWERS", "US", "3569377", "800", "1", |
| 156 "US"}, |
| 123 // Don't add a country code where there was none. | 157 // Don't add a country code where there was none. |
| 124 {true, "(08) 450 777 7777", "DE", "7777777", "8450", "", "DE"}, | 158 ParseNumberTestCase{true, "(08) 450 777 7777", "DE", "7777777", "8450", |
| 125 }; | 159 "", "DE"})); |
| 126 | |
| 127 for (const auto& test_case : test_cases) { | |
| 128 SCOPED_TRACE("Testing phone number " + test_case.input); | |
| 129 | |
| 130 base::string16 country_code, city_code, number; | |
| 131 std::string deduced_region; | |
| 132 ::i18n::phonenumbers::PhoneNumber unused_i18n_number; | |
| 133 EXPECT_EQ( | |
| 134 test_case.valid, | |
| 135 ParsePhoneNumber(ASCIIToUTF16(test_case.input), | |
| 136 test_case.assumed_region, &country_code, &city_code, | |
| 137 &number, &deduced_region, &unused_i18n_number)); | |
| 138 EXPECT_EQ(ASCIIToUTF16(test_case.number), number); | |
| 139 EXPECT_EQ(ASCIIToUTF16(test_case.city_code), city_code); | |
| 140 EXPECT_EQ(ASCIIToUTF16(test_case.country_code), country_code); | |
| 141 EXPECT_EQ(test_case.deduced_region, deduced_region); | |
| 142 } | |
| 143 } | |
| 144 | 160 |
| 145 TEST(PhoneNumberI18NTest, ConstructPhoneNumber) { | 161 TEST(PhoneNumberI18NTest, ConstructPhoneNumber) { |
| 146 base::string16 number; | 162 base::string16 number; |
| 147 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"), | 163 EXPECT_TRUE(ConstructPhoneNumber(ASCIIToUTF16("1"), |
| 148 ASCIIToUTF16("650"), | 164 ASCIIToUTF16("650"), |
| 149 ASCIIToUTF16("2345678"), | 165 ASCIIToUTF16("2345678"), |
| 150 "US", | 166 "US", |
| 151 &number)); | 167 &number)); |
| 152 EXPECT_EQ(ASCIIToUTF16("1 650-234-5678"), number); | 168 EXPECT_EQ(ASCIIToUTF16("1 650-234-5678"), number); |
| 153 EXPECT_TRUE(ConstructPhoneNumber(base::string16(), | 169 EXPECT_TRUE(ConstructPhoneNumber(base::string16(), |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 "en-US")); | 266 "en-US")); |
| 251 | 267 |
| 252 // Different numbers don't match. | 268 // Different numbers don't match. |
| 253 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), | 269 EXPECT_FALSE(PhoneNumbersMatch(ASCIIToUTF16("14158889999"), |
| 254 ASCIIToUTF16("1415888"), | 270 ASCIIToUTF16("1415888"), |
| 255 "US", | 271 "US", |
| 256 "en-US")); | 272 "en-US")); |
| 257 } | 273 } |
| 258 | 274 |
| 259 } // namespace autofill | 275 } // namespace autofill |
| OLD | NEW |