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/memory/scoped_ptr.h" 6 #include "base/memory/scoped_vector.h" 7 #include "base/utf_string_conversions.h" 8 #include "chrome/browser/autofill/autofill_field.h" 9 #include "chrome/browser/autofill/phone_field.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "webkit/glue/form_field.h" 12 13 namespace { 14 15 class PhoneFieldTest : public testing::Test { 16 public: 17 PhoneFieldTest() {} 18 19 protected: 20 ScopedVector<AutofillField> list_; 21 scoped_ptr<PhoneField> field_; 22 FieldTypeMap field_type_map_; 23 std::vector<AutofillField*>::const_iterator iter_; 24 25 private: 26 DISALLOW_COPY_AND_ASSIGN(PhoneFieldTest); 27 }; 28 29 TEST_F(PhoneFieldTest, Empty) { 30 list_.push_back(NULL); 31 iter_ = list_.begin(); 32 field_.reset(PhoneField::Parse(&iter_, false)); 33 ASSERT_EQ(static_cast<PhoneField*>(NULL), field_.get()); 34 } 35 36 TEST_F(PhoneFieldTest, NonParse) { 37 list_.push_back(new AutofillField); 38 list_.push_back(NULL); 39 iter_ = list_.begin(); 40 field_.reset(PhoneField::Parse(&iter_, false)); 41 ASSERT_EQ(static_cast<PhoneField*>(NULL), field_.get()); 42 } 43 44 TEST_F(PhoneFieldTest, ParseOneLinePhone) { 45 list_.push_back( 46 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone"), 47 ASCIIToUTF16("phone"), 48 string16(), 49 ASCIIToUTF16("text"), 50 0, 51 false), 52 ASCIIToUTF16("phone1"))); 53 list_.push_back(NULL); 54 iter_ = list_.begin(); 55 field_.reset(PhoneField::Parse(&iter_, false)); 56 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 57 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 58 ASSERT_TRUE( 59 field_type_map_.find(ASCIIToUTF16("phone1")) != field_type_map_.end()); 60 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, field_type_map_[ASCIIToUTF16("phone1")]); 61 } 62 63 TEST_F(PhoneFieldTest, ParseOneLinePhoneEcml) { 64 list_.push_back( 65 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone"), 66 ASCIIToUTF16(kEcmlShipToPhone), 67 string16(), 68 ASCIIToUTF16("text"), 69 0, 70 false), 71 ASCIIToUTF16("phone1"))); 72 list_.push_back(NULL); 73 iter_ = list_.begin(); 74 field_.reset(PhoneField::Parse(&iter_, true)); 75 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 76 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 77 ASSERT_TRUE( 78 field_type_map_.find(ASCIIToUTF16("phone1")) != field_type_map_.end()); 79 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, field_type_map_[ASCIIToUTF16("phone1")]); 80 } 81 82 TEST_F(PhoneFieldTest, ParseTwoLinePhone) { 83 list_.push_back( 84 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Area Code"), 85 ASCIIToUTF16("area code"), 86 string16(), 87 ASCIIToUTF16("text"), 88 0, 89 false), 90 ASCIIToUTF16("areacode1"))); 91 list_.push_back( 92 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone"), 93 ASCIIToUTF16("phone"), 94 string16(), 95 ASCIIToUTF16("text"), 96 0, 97 false), 98 ASCIIToUTF16("phone1"))); 99 list_.push_back(NULL); 100 iter_ = list_.begin(); 101 field_.reset(PhoneField::Parse(&iter_, false)); 102 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 103 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 104 ASSERT_TRUE( 105 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 106 EXPECT_EQ(PHONE_HOME_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 107 ASSERT_TRUE( 108 field_type_map_.find(ASCIIToUTF16("phone1")) != field_type_map_.end()); 109 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("phone1")]); 110 } 111 112 TEST_F(PhoneFieldTest, ParseTwoLinePhoneEcmlShipTo) { 113 list_.push_back( 114 new AutofillField( 115 webkit_glue::FormField(ASCIIToUTF16("Area Code"), 116 ASCIIToUTF16(kEcmlShipToPostalCode), 117 string16(), 118 ASCIIToUTF16("text"), 119 0, 120 false), 121 ASCIIToUTF16("areacode1"))); 122 list_.push_back( 123 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone"), 124 ASCIIToUTF16(kEcmlShipToPhone), 125 string16(), 126 ASCIIToUTF16("text"), 127 0, 128 false), 129 ASCIIToUTF16("phone1"))); 130 list_.push_back(NULL); 131 iter_ = list_.begin(); 132 field_.reset(PhoneField::Parse(&iter_, false)); 133 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 134 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 135 ASSERT_TRUE( 136 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 137 EXPECT_EQ(PHONE_HOME_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 138 ASSERT_TRUE( 139 field_type_map_.find(ASCIIToUTF16("phone1")) != field_type_map_.end()); 140 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("phone1")]); 141 } 142 143 TEST_F(PhoneFieldTest, ParseTwoLinePhoneEcmlBillTo) { 144 list_.push_back( 145 new AutofillField( 146 webkit_glue::FormField(ASCIIToUTF16("Area Code"), 147 ASCIIToUTF16(kEcmlBillToPostalCode), 148 string16(), 149 ASCIIToUTF16("text"), 150 0, 151 false), 152 ASCIIToUTF16("areacode1"))); 153 list_.push_back( 154 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone"), 155 ASCIIToUTF16(kEcmlBillToPhone), 156 string16(), 157 ASCIIToUTF16("text"), 158 0, 159 false), 160 ASCIIToUTF16("phone1"))); 161 list_.push_back(NULL); 162 iter_ = list_.begin(); 163 field_.reset(PhoneField::Parse(&iter_, false)); 164 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 165 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 166 ASSERT_TRUE( 167 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 168 EXPECT_EQ(PHONE_HOME_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 169 ASSERT_TRUE( 170 field_type_map_.find(ASCIIToUTF16("phone1")) != field_type_map_.end()); 171 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("phone1")]); 172 } 173 174 TEST_F(PhoneFieldTest, ThreePartPhoneNumber) { 175 // Phone in format <field> - <field> - <field> could be either 176 // <area code> - <prefix> - <suffix>, or 177 // <country code> - <area code> - <phone>. The only distinguishing feature is 178 // size: <prefix> is no bigger than 3 characters, and <suffix> is no bigger 179 // than 4. 180 list_.push_back( 181 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone:"), 182 ASCIIToUTF16("dayphone1"), 183 string16(), 184 ASCIIToUTF16("text"), 185 0, 186 false), 187 ASCIIToUTF16("areacode1"))); 188 list_.push_back( 189 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("-"), 190 ASCIIToUTF16("dayphone2"), 191 string16(), 192 ASCIIToUTF16("text"), 193 3, 194 false), 195 ASCIIToUTF16("prefix1"))); 196 list_.push_back( 197 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("-"), 198 ASCIIToUTF16("dayphone3"), 199 string16(), 200 ASCIIToUTF16("text"), 201 4, 202 false), 203 ASCIIToUTF16("suffix1"))); 204 list_.push_back( 205 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("ext.:"), 206 ASCIIToUTF16("dayphone4"), 207 string16(), 208 ASCIIToUTF16("text"), 209 0, 210 false), 211 ASCIIToUTF16("ext1"))); 212 list_.push_back(NULL); 213 iter_ = list_.begin(); 214 field_.reset(PhoneField::Parse(&iter_, false)); 215 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 216 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 217 ASSERT_TRUE( 218 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 219 EXPECT_EQ(PHONE_HOME_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 220 ASSERT_TRUE( 221 field_type_map_.find(ASCIIToUTF16("prefix1")) != field_type_map_.end()); 222 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("prefix1")]); 223 ASSERT_TRUE( 224 field_type_map_.find(ASCIIToUTF16("suffix1")) != field_type_map_.end()); 225 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("suffix1")]); 226 } 227 228 // This scenario of explicitly labeled "prefix" and "suffix" phone numbers 229 // encountered in http://crbug.com/40694 with page 230 // https://www.wrapables.com/jsp/Signup.jsp. 231 TEST_F(PhoneFieldTest, ThreePartPhoneNumberPrefixSuffix) { 232 list_.push_back( 233 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Phone:"), 234 ASCIIToUTF16("area"), 235 string16(), 236 ASCIIToUTF16("text"), 237 0, 238 false), 239 ASCIIToUTF16("areacode1"))); 240 list_.push_back( 241 new AutofillField(webkit_glue::FormField(string16(), 242 ASCIIToUTF16("prefix"), 243 string16(), 244 ASCIIToUTF16("text"), 245 0, 246 false), 247 ASCIIToUTF16("prefix1"))); 248 list_.push_back( 249 new AutofillField(webkit_glue::FormField(string16(), 250 ASCIIToUTF16("suffix"), 251 string16(), 252 ASCIIToUTF16("text"), 253 0, 254 false), 255 ASCIIToUTF16("suffix1"))); 256 list_.push_back(NULL); 257 iter_ = list_.begin(); 258 field_.reset(PhoneField::Parse(&iter_, false)); 259 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 260 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 261 ASSERT_TRUE( 262 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 263 EXPECT_EQ(PHONE_HOME_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 264 ASSERT_TRUE( 265 field_type_map_.find(ASCIIToUTF16("prefix1")) != field_type_map_.end()); 266 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("prefix1")]); 267 ASSERT_TRUE( 268 field_type_map_.find(ASCIIToUTF16("suffix1")) != field_type_map_.end()); 269 EXPECT_EQ(PHONE_HOME_NUMBER, field_type_map_[ASCIIToUTF16("suffix1")]); 270 } 271 272 TEST_F(PhoneFieldTest, ParseOneLineFax) { 273 list_.push_back( 274 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Fax"), 275 ASCIIToUTF16("fax"), 276 string16(), 277 ASCIIToUTF16("text"), 278 0, 279 false), 280 ASCIIToUTF16("fax1"))); 281 list_.push_back(NULL); 282 iter_ = list_.begin(); 283 field_.reset(PhoneField::Parse(&iter_, false)); 284 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 285 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 286 ASSERT_TRUE( 287 field_type_map_.find(ASCIIToUTF16("fax1")) != field_type_map_.end()); 288 EXPECT_EQ(PHONE_FAX_WHOLE_NUMBER, field_type_map_[ASCIIToUTF16("fax1")]); 289 } 290 291 TEST_F(PhoneFieldTest, ParseTwoLineFax) { 292 list_.push_back( 293 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Area Code"), 294 ASCIIToUTF16("area code"), 295 string16(), 296 ASCIIToUTF16("text"), 297 0, 298 false), 299 ASCIIToUTF16("areacode1"))); 300 list_.push_back( 301 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Fax"), 302 ASCIIToUTF16("fax"), 303 string16(), 304 ASCIIToUTF16("text"), 305 0, 306 false), 307 ASCIIToUTF16("fax1"))); 308 list_.push_back(NULL); 309 iter_ = list_.begin(); 310 field_.reset(PhoneField::Parse(&iter_, false)); 311 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 312 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 313 ASSERT_TRUE( 314 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 315 // It should be FAX, based on the other phone in the group. 316 EXPECT_EQ(PHONE_FAX_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 317 ASSERT_TRUE( 318 field_type_map_.find(ASCIIToUTF16("fax1")) != field_type_map_.end()); 319 EXPECT_EQ(PHONE_FAX_NUMBER, field_type_map_[ASCIIToUTF16("fax1")]); 320 } 321 322 TEST_F(PhoneFieldTest, ThreePartFaxNumberPrefixSuffix) { 323 list_.push_back( 324 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Fax:"), 325 ASCIIToUTF16("area"), 326 string16(), 327 ASCIIToUTF16("text"), 328 0, 329 false), 330 ASCIIToUTF16("areacode1"))); 331 list_.push_back( 332 new AutofillField(webkit_glue::FormField(string16(), 333 ASCIIToUTF16("prefix"), 334 string16(), 335 ASCIIToUTF16("text"), 336 0, 337 false), 338 ASCIIToUTF16("prefix1"))); 339 list_.push_back( 340 new AutofillField(webkit_glue::FormField(string16(), 341 ASCIIToUTF16("suffix"), 342 string16(), 343 ASCIIToUTF16("text"), 344 0, 345 false), 346 ASCIIToUTF16("suffix1"))); 347 list_.push_back(NULL); 348 iter_ = list_.begin(); 349 field_.reset(PhoneField::Parse(&iter_, false)); 350 ASSERT_NE(static_cast<PhoneField*>(NULL), field_.get()); 351 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 352 ASSERT_TRUE( 353 field_type_map_.find(ASCIIToUTF16("areacode1")) != field_type_map_.end()); 354 EXPECT_EQ(PHONE_FAX_CITY_CODE, field_type_map_[ASCIIToUTF16("areacode1")]); 355 ASSERT_TRUE( 356 field_type_map_.find(ASCIIToUTF16("prefix1")) != field_type_map_.end()); 357 EXPECT_EQ(PHONE_FAX_NUMBER, field_type_map_[ASCIIToUTF16("prefix1")]); 358 ASSERT_TRUE( 359 field_type_map_.find(ASCIIToUTF16("suffix1")) != field_type_map_.end()); 360 EXPECT_EQ(PHONE_FAX_NUMBER, field_type_map_[ASCIIToUTF16("suffix1")]); 361 } 362 363 } // namespace 364