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/name_field.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "webkit/glue/form_field.h" 12 13 namespace { 14 15 class NameFieldTest : public testing::Test { 16 public: 17 NameFieldTest() {} 18 19 protected: 20 ScopedVector<AutofillField> list_; 21 scoped_ptr<NameField> field_; 22 FieldTypeMap field_type_map_; 23 std::vector<AutofillField*>::const_iterator iter_; 24 25 private: 26 DISALLOW_COPY_AND_ASSIGN(NameFieldTest); 27 }; 28 29 TEST_F(NameFieldTest, FirstMiddleLast) { 30 list_.push_back( 31 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("First Name"), 32 ASCIIToUTF16("First"), 33 string16(), 34 ASCIIToUTF16("text"), 35 0, 36 false), 37 ASCIIToUTF16("name1"))); 38 list_.push_back( 39 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Middle Name"), 40 ASCIIToUTF16("Middle"), 41 string16(), 42 ASCIIToUTF16("text"), 43 0, 44 false), 45 ASCIIToUTF16("name2"))); 46 list_.push_back( 47 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Last Name"), 48 ASCIIToUTF16("Last"), 49 string16(), 50 ASCIIToUTF16("text"), 51 0, 52 false), 53 ASCIIToUTF16("name3"))); 54 list_.push_back(NULL); 55 iter_ = list_.begin(); 56 field_.reset(NameField::Parse(&iter_, false)); 57 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 58 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 59 ASSERT_TRUE( 60 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 61 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 62 ASSERT_TRUE( 63 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 64 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]); 65 ASSERT_TRUE( 66 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 67 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 68 } 69 70 TEST_F(NameFieldTest, FirstMiddleLast2) { 71 list_.push_back( 72 new AutofillField(webkit_glue::FormField(string16(), 73 ASCIIToUTF16("firstName"), 74 string16(), 75 ASCIIToUTF16("text"), 76 0, 77 false), 78 ASCIIToUTF16("name1"))); 79 list_.push_back( 80 new AutofillField(webkit_glue::FormField(string16(), 81 ASCIIToUTF16("middleName"), 82 string16(), 83 ASCIIToUTF16("text"), 84 0, 85 false), 86 ASCIIToUTF16("name2"))); 87 list_.push_back( 88 new AutofillField(webkit_glue::FormField(string16(), 89 ASCIIToUTF16("lastName"), 90 string16(), 91 ASCIIToUTF16("text"), 92 0, 93 false), 94 ASCIIToUTF16("name3"))); 95 list_.push_back(NULL); 96 iter_ = list_.begin(); 97 field_.reset(NameField::Parse(&iter_, false)); 98 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 99 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 100 ASSERT_TRUE( 101 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 102 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 103 ASSERT_TRUE( 104 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 105 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]); 106 ASSERT_TRUE( 107 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 108 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 109 } 110 111 TEST_F(NameFieldTest, FirstLast) { 112 list_.push_back( 113 new AutofillField(webkit_glue::FormField(string16(), 114 ASCIIToUTF16("first_name"), 115 string16(), 116 ASCIIToUTF16("text"), 117 0, 118 false), 119 ASCIIToUTF16("name1"))); 120 list_.push_back( 121 new AutofillField(webkit_glue::FormField(string16(), 122 ASCIIToUTF16("last_name"), 123 string16(), 124 ASCIIToUTF16("text"), 125 0, 126 false), 127 ASCIIToUTF16("name2"))); 128 list_.push_back(NULL); 129 iter_ = list_.begin(); 130 field_.reset(NameField::Parse(&iter_, false)); 131 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 132 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 133 ASSERT_TRUE( 134 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 135 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 136 ASSERT_TRUE( 137 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 138 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name2")]); 139 } 140 141 TEST_F(NameFieldTest, FirstLast2) { 142 list_.push_back( 143 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name"), 144 ASCIIToUTF16("first_name"), 145 string16(), 146 ASCIIToUTF16("text"), 147 0, 148 false), 149 ASCIIToUTF16("name1"))); 150 list_.push_back( 151 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name"), 152 ASCIIToUTF16("last_name"), 153 string16(), 154 ASCIIToUTF16("text"), 155 0, 156 false), 157 ASCIIToUTF16("name2"))); 158 list_.push_back(NULL); 159 iter_ = list_.begin(); 160 field_.reset(NameField::Parse(&iter_, false)); 161 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 162 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 163 ASSERT_TRUE( 164 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 165 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 166 ASSERT_TRUE( 167 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 168 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name2")]); 169 } 170 171 TEST_F(NameFieldTest, FirstLastMiddleWithSpaces) { 172 list_.push_back( 173 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("First Name"), 174 ASCIIToUTF16("first name"), 175 string16(), 176 ASCIIToUTF16("text"), 177 0, 178 false), 179 ASCIIToUTF16("name1"))); 180 list_.push_back( 181 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Middle Name"), 182 ASCIIToUTF16("middle name"), 183 string16(), 184 ASCIIToUTF16("text"), 185 0, 186 false), 187 ASCIIToUTF16("name2"))); 188 list_.push_back( 189 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Last Name"), 190 ASCIIToUTF16("last name"), 191 string16(), 192 ASCIIToUTF16("text"), 193 0, 194 false), 195 ASCIIToUTF16("name3"))); 196 list_.push_back(NULL); 197 iter_ = list_.begin(); 198 field_.reset(NameField::Parse(&iter_, false)); 199 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 200 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 201 ASSERT_TRUE( 202 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 203 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 204 ASSERT_TRUE( 205 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 206 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]); 207 ASSERT_TRUE( 208 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 209 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 210 } 211 212 TEST_F(NameFieldTest, FirstLastEmpty) { 213 list_.push_back( 214 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name"), 215 ASCIIToUTF16("first_name"), 216 string16(), 217 ASCIIToUTF16("text"), 218 0, 219 false), 220 ASCIIToUTF16("name1"))); 221 list_.push_back( 222 new AutofillField(webkit_glue::FormField(string16(), 223 ASCIIToUTF16("last_name"), 224 string16(), 225 ASCIIToUTF16("text"), 226 0, 227 false), 228 ASCIIToUTF16("name2"))); 229 list_.push_back(NULL); 230 iter_ = list_.begin(); 231 field_.reset(NameField::Parse(&iter_, false)); 232 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 233 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 234 ASSERT_TRUE( 235 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 236 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 237 ASSERT_TRUE( 238 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 239 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name2")]); 240 } 241 242 TEST_F(NameFieldTest, FirstMiddleLastEmpty) { 243 list_.push_back( 244 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name"), 245 ASCIIToUTF16("first_name"), 246 string16(), 247 ASCIIToUTF16("text"), 248 0, 249 false), 250 ASCIIToUTF16("name1"))); 251 list_.push_back( 252 new AutofillField(webkit_glue::FormField(string16(), 253 ASCIIToUTF16("middle_name"), 254 string16(), 255 ASCIIToUTF16("text"), 256 0, 257 false), 258 ASCIIToUTF16("name2"))); 259 list_.push_back( 260 new AutofillField(webkit_glue::FormField(string16(), 261 ASCIIToUTF16("last_name"), 262 string16(), 263 ASCIIToUTF16("text"), 264 0, 265 false), 266 ASCIIToUTF16("name3"))); 267 list_.push_back(NULL); 268 iter_ = list_.begin(); 269 field_.reset(NameField::Parse(&iter_, false)); 270 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 271 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 272 ASSERT_TRUE( 273 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 274 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 275 ASSERT_TRUE( 276 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 277 EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]); 278 ASSERT_TRUE( 279 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 280 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 281 } 282 283 TEST_F(NameFieldTest, MiddleInitial) { 284 list_.push_back( 285 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("First Name"), 286 ASCIIToUTF16("first_name"), 287 string16(), 288 ASCIIToUTF16("text"), 289 0, 290 false), 291 ASCIIToUTF16("name1"))); 292 list_.push_back( 293 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("MI"), 294 ASCIIToUTF16("middle_name"), 295 string16(), 296 ASCIIToUTF16("text"), 297 0, 298 false), 299 ASCIIToUTF16("name2"))); 300 list_.push_back( 301 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Last Name"), 302 ASCIIToUTF16("last_name"), 303 string16(), 304 ASCIIToUTF16("text"), 305 0, 306 false), 307 ASCIIToUTF16("name3"))); 308 list_.push_back(NULL); 309 iter_ = list_.begin(); 310 field_.reset(NameField::Parse(&iter_, false)); 311 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 312 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 313 ASSERT_TRUE( 314 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 315 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 316 ASSERT_TRUE( 317 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 318 EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]); 319 ASSERT_TRUE( 320 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 321 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 322 } 323 324 TEST_F(NameFieldTest, MiddleInitialNoLastName) { 325 list_.push_back( 326 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("First Name"), 327 ASCIIToUTF16("first_name"), 328 string16(), 329 ASCIIToUTF16("text"), 330 0, 331 false), 332 ASCIIToUTF16("name1"))); 333 list_.push_back( 334 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("MI"), 335 ASCIIToUTF16("middle_name"), 336 string16(), 337 ASCIIToUTF16("text"), 338 0, 339 false), 340 ASCIIToUTF16("name2"))); 341 list_.push_back(NULL); 342 iter_ = list_.begin(); 343 field_.reset(NameField::Parse(&iter_, false)); 344 ASSERT_EQ(static_cast<NameField*>(NULL), field_.get()); 345 } 346 347 // This case is from the dell.com checkout page. The middle initial "mi" string 348 // came at the end following other descriptive text. http://crbug.com/45123. 349 TEST_F(NameFieldTest, MiddleInitialAtEnd) { 350 list_.push_back( 351 new AutofillField(webkit_glue::FormField(string16(), 352 ASCIIToUTF16("XXXnameXXXfirst"), 353 string16(), 354 ASCIIToUTF16("text"), 355 0, 356 false), 357 ASCIIToUTF16("name1"))); 358 list_.push_back( 359 new AutofillField(webkit_glue::FormField(string16(), 360 ASCIIToUTF16("XXXnameXXXmi"), 361 string16(), 362 ASCIIToUTF16("text"), 363 0, 364 false), 365 ASCIIToUTF16("name2"))); 366 list_.push_back( 367 new AutofillField(webkit_glue::FormField(string16(), 368 ASCIIToUTF16("XXXnameXXXlast"), 369 string16(), 370 ASCIIToUTF16("text"), 371 0, 372 false), 373 ASCIIToUTF16("name3"))); 374 list_.push_back(NULL); 375 iter_ = list_.begin(); 376 field_.reset(NameField::Parse(&iter_, false)); 377 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 378 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 379 ASSERT_TRUE( 380 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 381 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 382 ASSERT_TRUE( 383 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 384 EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]); 385 ASSERT_TRUE( 386 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 387 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 388 } 389 390 TEST_F(NameFieldTest, ECMLNoName) { 391 list_.push_back(new AutofillField( 392 webkit_glue::FormField(ASCIIToUTF16("Company"), 393 ASCIIToUTF16("ecom_shipto_postal_company"), 394 string16(), 395 ASCIIToUTF16("text"), 396 0, 397 false), 398 ASCIIToUTF16("field1"))); 399 list_.push_back(NULL); 400 iter_ = list_.begin(); 401 field_.reset(NameField::Parse(&iter_, true)); 402 ASSERT_EQ(static_cast<NameField*>(NULL), field_.get()); 403 } 404 405 TEST_F(NameFieldTest, ECMLMiddleInitialNoLastName) { 406 list_.push_back(new AutofillField( 407 webkit_glue::FormField(ASCIIToUTF16("First Name"), 408 ASCIIToUTF16("ecom_shipto_postal_name_first"), 409 string16(), 410 ASCIIToUTF16("text"), 411 0, 412 false), 413 ASCIIToUTF16("name1"))); 414 list_.push_back(new AutofillField( 415 webkit_glue::FormField(ASCIIToUTF16("Middle"), 416 ASCIIToUTF16("ecom_shipto_postal_name_middle"), 417 string16(), 418 ASCIIToUTF16("text"), 419 0, 420 false), 421 ASCIIToUTF16("name2"))); 422 list_.push_back(NULL); 423 iter_ = list_.begin(); 424 field_.reset(NameField::Parse(&iter_, true)); 425 ASSERT_EQ(static_cast<NameField*>(NULL), field_.get()); 426 } 427 428 TEST_F(NameFieldTest, ECMLFirstMiddleLast) { 429 list_.push_back(new AutofillField( 430 webkit_glue::FormField(string16(), 431 ASCIIToUTF16("ecom_shipto_postal_name_first"), 432 string16(), 433 ASCIIToUTF16("text"), 434 0, 435 false), 436 ASCIIToUTF16("name1"))); 437 list_.push_back(new AutofillField( 438 webkit_glue::FormField(string16(), 439 ASCIIToUTF16("ecom_shipto_postal_name_middle"), 440 string16(), 441 ASCIIToUTF16("text"), 442 0, 443 false), 444 ASCIIToUTF16("name2"))); 445 list_.push_back(new AutofillField( 446 webkit_glue::FormField(string16(), 447 ASCIIToUTF16("ecom_shipto_postal_name_last"), 448 string16(), 449 ASCIIToUTF16("text"), 450 0, 451 false), 452 ASCIIToUTF16("name3"))); 453 list_.push_back(NULL); 454 iter_ = list_.begin(); 455 field_.reset(NameField::Parse(&iter_, true)); 456 ASSERT_NE(static_cast<NameField*>(NULL), field_.get()); 457 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_)); 458 ASSERT_TRUE( 459 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end()); 460 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]); 461 ASSERT_TRUE( 462 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end()); 463 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]); 464 ASSERT_TRUE( 465 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end()); 466 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]); 467 } 468 469 } // namespace 470