| 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 "components/autofill/core/browser/phone_field.h" | 5 #include "components/autofill/core/browser/phone_field.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "components/autofill/core/browser/autofill_field.h" | 16 #include "components/autofill/core/browser/autofill_field.h" |
| 17 #include "components/autofill/core/browser/autofill_scanner.h" | 17 #include "components/autofill/core/browser/autofill_scanner.h" |
| 18 #include "components/autofill/core/common/form_field_data.h" | 18 #include "components/autofill/core/common/form_field_data.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
| 22 | 22 |
| 23 namespace autofill { | 23 namespace autofill { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 field_candidates_map_.clear(); | 49 field_candidates_map_.clear(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void CheckField(const std::string& name, | 52 void CheckField(const std::string& name, |
| 53 ServerFieldType expected_type) const { | 53 ServerFieldType expected_type) const { |
| 54 auto it = field_candidates_map_.find(ASCIIToUTF16(name)); | 54 auto it = field_candidates_map_.find(ASCIIToUTF16(name)); |
| 55 ASSERT_TRUE(it != field_candidates_map_.end()) << name; | 55 ASSERT_TRUE(it != field_candidates_map_.end()) << name; |
| 56 EXPECT_EQ(expected_type, it->second.BestHeuristicType()) << name; | 56 EXPECT_EQ(expected_type, it->second.BestHeuristicType()) << name; |
| 57 } | 57 } |
| 58 | 58 |
| 59 ScopedVector<AutofillField> list_; | 59 std::vector<std::unique_ptr<AutofillField>> list_; |
| 60 std::unique_ptr<PhoneField> field_; | 60 std::unique_ptr<PhoneField> field_; |
| 61 FieldCandidatesMap field_candidates_map_; | 61 FieldCandidatesMap field_candidates_map_; |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(PhoneFieldTest); | 64 DISALLOW_COPY_AND_ASSIGN(PhoneFieldTest); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 TEST_F(PhoneFieldTest, Empty) { | 67 TEST_F(PhoneFieldTest, Empty) { |
| 68 AutofillScanner scanner(list_.get()); | 68 AutofillScanner scanner(list_); |
| 69 field_ = Parse(&scanner); | 69 field_ = Parse(&scanner); |
| 70 ASSERT_EQ(nullptr, field_.get()); | 70 ASSERT_EQ(nullptr, field_.get()); |
| 71 } | 71 } |
| 72 | 72 |
| 73 TEST_F(PhoneFieldTest, NonParse) { | 73 TEST_F(PhoneFieldTest, NonParse) { |
| 74 list_.push_back(new AutofillField); | 74 list_.push_back(base::MakeUnique<AutofillField>()); |
| 75 AutofillScanner scanner(list_.get()); | 75 AutofillScanner scanner(list_); |
| 76 field_ = Parse(&scanner); | 76 field_ = Parse(&scanner); |
| 77 ASSERT_EQ(nullptr, field_.get()); | 77 ASSERT_EQ(nullptr, field_.get()); |
| 78 } | 78 } |
| 79 | 79 |
| 80 TEST_F(PhoneFieldTest, ParseOneLinePhone) { | 80 TEST_F(PhoneFieldTest, ParseOneLinePhone) { |
| 81 FormFieldData field; | 81 FormFieldData field; |
| 82 | 82 |
| 83 for (const char* field_type : kFieldTypes) { | 83 for (const char* field_type : kFieldTypes) { |
| 84 Clear(); | 84 Clear(); |
| 85 | 85 |
| 86 field.form_control_type = field_type; | 86 field.form_control_type = field_type; |
| 87 field.label = ASCIIToUTF16("Phone"); | 87 field.label = ASCIIToUTF16("Phone"); |
| 88 field.name = ASCIIToUTF16("phone"); | 88 field.name = ASCIIToUTF16("phone"); |
| 89 list_.push_back(new AutofillField(field, ASCIIToUTF16("phone1"))); | 89 list_.push_back( |
| 90 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("phone1"))); |
| 90 | 91 |
| 91 AutofillScanner scanner(list_.get()); | 92 AutofillScanner scanner(list_); |
| 92 field_ = Parse(&scanner); | 93 field_ = Parse(&scanner); |
| 93 ASSERT_NE(nullptr, field_.get()); | 94 ASSERT_NE(nullptr, field_.get()); |
| 94 field_->AddClassifications(&field_candidates_map_); | 95 field_->AddClassifications(&field_candidates_map_); |
| 95 CheckField("phone1", PHONE_HOME_WHOLE_NUMBER); | 96 CheckField("phone1", PHONE_HOME_WHOLE_NUMBER); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 TEST_F(PhoneFieldTest, ParseTwoLinePhone) { | 100 TEST_F(PhoneFieldTest, ParseTwoLinePhone) { |
| 100 FormFieldData field; | 101 FormFieldData field; |
| 101 | 102 |
| 102 for (const char* field_type : kFieldTypes) { | 103 for (const char* field_type : kFieldTypes) { |
| 103 Clear(); | 104 Clear(); |
| 104 | 105 |
| 105 field.form_control_type = field_type; | 106 field.form_control_type = field_type; |
| 106 field.label = ASCIIToUTF16("Area Code"); | 107 field.label = ASCIIToUTF16("Area Code"); |
| 107 field.name = ASCIIToUTF16("area code"); | 108 field.name = ASCIIToUTF16("area code"); |
| 108 list_.push_back(new AutofillField(field, ASCIIToUTF16("areacode1"))); | 109 list_.push_back( |
| 110 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("areacode1"))); |
| 109 | 111 |
| 110 field.label = ASCIIToUTF16("Phone"); | 112 field.label = ASCIIToUTF16("Phone"); |
| 111 field.name = ASCIIToUTF16("phone"); | 113 field.name = ASCIIToUTF16("phone"); |
| 112 list_.push_back(new AutofillField(field, ASCIIToUTF16("phone2"))); | 114 list_.push_back( |
| 115 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("phone2"))); |
| 113 | 116 |
| 114 AutofillScanner scanner(list_.get()); | 117 AutofillScanner scanner(list_); |
| 115 field_ = Parse(&scanner); | 118 field_ = Parse(&scanner); |
| 116 ASSERT_NE(nullptr, field_.get()); | 119 ASSERT_NE(nullptr, field_.get()); |
| 117 field_->AddClassifications(&field_candidates_map_); | 120 field_->AddClassifications(&field_candidates_map_); |
| 118 CheckField("areacode1", PHONE_HOME_CITY_CODE); | 121 CheckField("areacode1", PHONE_HOME_CITY_CODE); |
| 119 CheckField("phone2", PHONE_HOME_NUMBER); | 122 CheckField("phone2", PHONE_HOME_NUMBER); |
| 120 } | 123 } |
| 121 } | 124 } |
| 122 | 125 |
| 123 TEST_F(PhoneFieldTest, ThreePartPhoneNumber) { | 126 TEST_F(PhoneFieldTest, ThreePartPhoneNumber) { |
| 124 // Phone in format <field> - <field> - <field> could be either | 127 // Phone in format <field> - <field> - <field> could be either |
| 125 // <area code> - <prefix> - <suffix>, or | 128 // <area code> - <prefix> - <suffix>, or |
| 126 // <country code> - <area code> - <phone>. The only distinguishing feature is | 129 // <country code> - <area code> - <phone>. The only distinguishing feature is |
| 127 // size: <prefix> is no bigger than 3 characters, and <suffix> is no bigger | 130 // size: <prefix> is no bigger than 3 characters, and <suffix> is no bigger |
| 128 // than 4. | 131 // than 4. |
| 129 FormFieldData field; | 132 FormFieldData field; |
| 130 | 133 |
| 131 for (const char* field_type : kFieldTypes) { | 134 for (const char* field_type : kFieldTypes) { |
| 132 Clear(); | 135 Clear(); |
| 133 | 136 |
| 134 field.form_control_type = field_type; | 137 field.form_control_type = field_type; |
| 135 field.label = ASCIIToUTF16("Phone:"); | 138 field.label = ASCIIToUTF16("Phone:"); |
| 136 field.name = ASCIIToUTF16("dayphone1"); | 139 field.name = ASCIIToUTF16("dayphone1"); |
| 137 field.max_length = 0; | 140 field.max_length = 0; |
| 138 list_.push_back(new AutofillField(field, ASCIIToUTF16("areacode1"))); | 141 list_.push_back( |
| 142 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("areacode1"))); |
| 139 | 143 |
| 140 field.label = ASCIIToUTF16("-"); | 144 field.label = ASCIIToUTF16("-"); |
| 141 field.name = ASCIIToUTF16("dayphone2"); | 145 field.name = ASCIIToUTF16("dayphone2"); |
| 142 field.max_length = 3; | 146 field.max_length = 3; |
| 143 list_.push_back(new AutofillField(field, ASCIIToUTF16("prefix2"))); | 147 list_.push_back( |
| 148 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("prefix2"))); |
| 144 | 149 |
| 145 field.label = ASCIIToUTF16("-"); | 150 field.label = ASCIIToUTF16("-"); |
| 146 field.name = ASCIIToUTF16("dayphone3"); | 151 field.name = ASCIIToUTF16("dayphone3"); |
| 147 field.max_length = 4; | 152 field.max_length = 4; |
| 148 list_.push_back(new AutofillField(field, ASCIIToUTF16("suffix3"))); | 153 list_.push_back( |
| 154 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("suffix3"))); |
| 149 | 155 |
| 150 field.label = ASCIIToUTF16("ext.:"); | 156 field.label = ASCIIToUTF16("ext.:"); |
| 151 field.name = ASCIIToUTF16("dayphone4"); | 157 field.name = ASCIIToUTF16("dayphone4"); |
| 152 field.max_length = 0; | 158 field.max_length = 0; |
| 153 list_.push_back(new AutofillField(field, ASCIIToUTF16("ext4"))); | 159 list_.push_back( |
| 160 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("ext4"))); |
| 154 | 161 |
| 155 AutofillScanner scanner(list_.get()); | 162 AutofillScanner scanner(list_); |
| 156 field_ = Parse(&scanner); | 163 field_ = Parse(&scanner); |
| 157 ASSERT_NE(nullptr, field_.get()); | 164 ASSERT_NE(nullptr, field_.get()); |
| 158 field_->AddClassifications(&field_candidates_map_); | 165 field_->AddClassifications(&field_candidates_map_); |
| 159 CheckField("areacode1", PHONE_HOME_CITY_CODE); | 166 CheckField("areacode1", PHONE_HOME_CITY_CODE); |
| 160 CheckField("prefix2", PHONE_HOME_NUMBER); | 167 CheckField("prefix2", PHONE_HOME_NUMBER); |
| 161 CheckField("suffix3", PHONE_HOME_NUMBER); | 168 CheckField("suffix3", PHONE_HOME_NUMBER); |
| 162 EXPECT_TRUE(base::ContainsKey(field_candidates_map_, ASCIIToUTF16("ext4"))); | 169 EXPECT_TRUE(base::ContainsKey(field_candidates_map_, ASCIIToUTF16("ext4"))); |
| 163 } | 170 } |
| 164 } | 171 } |
| 165 | 172 |
| 166 // This scenario of explicitly labeled "prefix" and "suffix" phone numbers | 173 // This scenario of explicitly labeled "prefix" and "suffix" phone numbers |
| 167 // encountered in http://crbug.com/40694 with page | 174 // encountered in http://crbug.com/40694 with page |
| 168 // https://www.wrapables.com/jsp/Signup.jsp. | 175 // https://www.wrapables.com/jsp/Signup.jsp. |
| 169 TEST_F(PhoneFieldTest, ThreePartPhoneNumberPrefixSuffix) { | 176 TEST_F(PhoneFieldTest, ThreePartPhoneNumberPrefixSuffix) { |
| 170 FormFieldData field; | 177 FormFieldData field; |
| 171 | 178 |
| 172 for (const char* field_type : kFieldTypes) { | 179 for (const char* field_type : kFieldTypes) { |
| 173 Clear(); | 180 Clear(); |
| 174 | 181 |
| 175 field.form_control_type = field_type; | 182 field.form_control_type = field_type; |
| 176 field.label = ASCIIToUTF16("Phone:"); | 183 field.label = ASCIIToUTF16("Phone:"); |
| 177 field.name = ASCIIToUTF16("area"); | 184 field.name = ASCIIToUTF16("area"); |
| 178 list_.push_back(new AutofillField(field, ASCIIToUTF16("areacode1"))); | 185 list_.push_back( |
| 186 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("areacode1"))); |
| 179 | 187 |
| 180 field.label = base::string16(); | 188 field.label = base::string16(); |
| 181 field.name = ASCIIToUTF16("prefix"); | 189 field.name = ASCIIToUTF16("prefix"); |
| 182 list_.push_back(new AutofillField(field, ASCIIToUTF16("prefix2"))); | 190 list_.push_back( |
| 191 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("prefix2"))); |
| 183 | 192 |
| 184 field.label = base::string16(); | 193 field.label = base::string16(); |
| 185 field.name = ASCIIToUTF16("suffix"); | 194 field.name = ASCIIToUTF16("suffix"); |
| 186 list_.push_back(new AutofillField(field, ASCIIToUTF16("suffix3"))); | 195 list_.push_back( |
| 196 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("suffix3"))); |
| 187 | 197 |
| 188 AutofillScanner scanner(list_.get()); | 198 AutofillScanner scanner(list_); |
| 189 field_ = Parse(&scanner); | 199 field_ = Parse(&scanner); |
| 190 ASSERT_NE(nullptr, field_.get()); | 200 ASSERT_NE(nullptr, field_.get()); |
| 191 field_->AddClassifications(&field_candidates_map_); | 201 field_->AddClassifications(&field_candidates_map_); |
| 192 CheckField("areacode1", PHONE_HOME_CITY_CODE); | 202 CheckField("areacode1", PHONE_HOME_CITY_CODE); |
| 193 CheckField("prefix2", PHONE_HOME_NUMBER); | 203 CheckField("prefix2", PHONE_HOME_NUMBER); |
| 194 CheckField("suffix3", PHONE_HOME_NUMBER); | 204 CheckField("suffix3", PHONE_HOME_NUMBER); |
| 195 } | 205 } |
| 196 } | 206 } |
| 197 | 207 |
| 198 TEST_F(PhoneFieldTest, ThreePartPhoneNumberPrefixSuffix2) { | 208 TEST_F(PhoneFieldTest, ThreePartPhoneNumberPrefixSuffix2) { |
| 199 FormFieldData field; | 209 FormFieldData field; |
| 200 | 210 |
| 201 for (const char* field_type : kFieldTypes) { | 211 for (const char* field_type : kFieldTypes) { |
| 202 Clear(); | 212 Clear(); |
| 203 | 213 |
| 204 field.form_control_type = field_type; | 214 field.form_control_type = field_type; |
| 205 field.label = ASCIIToUTF16("("); | 215 field.label = ASCIIToUTF16("("); |
| 206 field.name = ASCIIToUTF16("phone1"); | 216 field.name = ASCIIToUTF16("phone1"); |
| 207 field.max_length = 3; | 217 field.max_length = 3; |
| 208 list_.push_back(new AutofillField(field, ASCIIToUTF16("phone1"))); | 218 list_.push_back( |
| 219 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("phone1"))); |
| 209 | 220 |
| 210 field.label = ASCIIToUTF16(")"); | 221 field.label = ASCIIToUTF16(")"); |
| 211 field.name = ASCIIToUTF16("phone2"); | 222 field.name = ASCIIToUTF16("phone2"); |
| 212 field.max_length = 3; | 223 field.max_length = 3; |
| 213 list_.push_back(new AutofillField(field, ASCIIToUTF16("phone2"))); | 224 list_.push_back( |
| 225 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("phone2"))); |
| 214 | 226 |
| 215 field.label = base::string16(); | 227 field.label = base::string16(); |
| 216 field.name = ASCIIToUTF16("phone3"); | 228 field.name = ASCIIToUTF16("phone3"); |
| 217 field.max_length = 4; | 229 field.max_length = 4; |
| 218 list_.push_back(new AutofillField(field, ASCIIToUTF16("phone3"))); | 230 list_.push_back( |
| 231 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("phone3"))); |
| 219 | 232 |
| 220 AutofillScanner scanner(list_.get()); | 233 AutofillScanner scanner(list_); |
| 221 field_ = Parse(&scanner); | 234 field_ = Parse(&scanner); |
| 222 ASSERT_NE(nullptr, field_.get()); | 235 ASSERT_NE(nullptr, field_.get()); |
| 223 field_->AddClassifications(&field_candidates_map_); | 236 field_->AddClassifications(&field_candidates_map_); |
| 224 CheckField("phone1", PHONE_HOME_CITY_CODE); | 237 CheckField("phone1", PHONE_HOME_CITY_CODE); |
| 225 CheckField("phone2", PHONE_HOME_NUMBER); | 238 CheckField("phone2", PHONE_HOME_NUMBER); |
| 226 CheckField("phone3", PHONE_HOME_NUMBER); | 239 CheckField("phone3", PHONE_HOME_NUMBER); |
| 227 } | 240 } |
| 228 } | 241 } |
| 229 | 242 |
| 230 TEST_F(PhoneFieldTest, CountryAndCityAndPhoneNumber) { | 243 TEST_F(PhoneFieldTest, CountryAndCityAndPhoneNumber) { |
| 231 // Phone in format <country code>:3 - <city and number>:10 | 244 // Phone in format <country code>:3 - <city and number>:10 |
| 232 // The |maxlength| is considered, otherwise it's too broad. | 245 // The |maxlength| is considered, otherwise it's too broad. |
| 233 FormFieldData field; | 246 FormFieldData field; |
| 234 | 247 |
| 235 for (const char* field_type : kFieldTypes) { | 248 for (const char* field_type : kFieldTypes) { |
| 236 Clear(); | 249 Clear(); |
| 237 | 250 |
| 238 field.form_control_type = field_type; | 251 field.form_control_type = field_type; |
| 239 field.label = ASCIIToUTF16("Phone Number"); | 252 field.label = ASCIIToUTF16("Phone Number"); |
| 240 field.name = ASCIIToUTF16("CountryCode"); | 253 field.name = ASCIIToUTF16("CountryCode"); |
| 241 field.max_length = 3; | 254 field.max_length = 3; |
| 242 list_.push_back(new AutofillField(field, ASCIIToUTF16("country"))); | 255 list_.push_back( |
| 256 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("country"))); |
| 243 | 257 |
| 244 field.label = ASCIIToUTF16("Phone Number"); | 258 field.label = ASCIIToUTF16("Phone Number"); |
| 245 field.name = ASCIIToUTF16("PhoneNumber"); | 259 field.name = ASCIIToUTF16("PhoneNumber"); |
| 246 field.max_length = 10; | 260 field.max_length = 10; |
| 247 list_.push_back(new AutofillField(field, ASCIIToUTF16("phone"))); | 261 list_.push_back( |
| 262 base::MakeUnique<AutofillField>(field, ASCIIToUTF16("phone"))); |
| 248 | 263 |
| 249 AutofillScanner scanner(list_.get()); | 264 AutofillScanner scanner(list_); |
| 250 field_ = Parse(&scanner); | 265 field_ = Parse(&scanner); |
| 251 ASSERT_NE(nullptr, field_.get()); | 266 ASSERT_NE(nullptr, field_.get()); |
| 252 field_->AddClassifications(&field_candidates_map_); | 267 field_->AddClassifications(&field_candidates_map_); |
| 253 CheckField("country", PHONE_HOME_COUNTRY_CODE); | 268 CheckField("country", PHONE_HOME_COUNTRY_CODE); |
| 254 CheckField("phone", PHONE_HOME_CITY_AND_NUMBER); | 269 CheckField("phone", PHONE_HOME_CITY_AND_NUMBER); |
| 255 } | 270 } |
| 256 } | 271 } |
| 257 | 272 |
| 258 } // namespace autofill | 273 } // namespace autofill |
| OLD | NEW |