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 3 // found in the LICENSE file. 4 5 #include "components/autofill/core/browser/form_structure.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/strings/string_util.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "components/autofill/core/browser/autofill_metrics.h" 11 #include "components/autofill/core/common/form_data.h" 12 #include "components/autofill/core/common/form_field_data.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "url/gurl.h" 15 16 using base::ASCIIToUTF16; 17 18 namespace autofill { 19 namespace { 20 21 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, 22 // which are handy for briefer test code. The AutofillMetrics class is 23 // stateless, so this is safe. 24 class TestAutofillMetrics : public AutofillMetrics { 25 public: 26 TestAutofillMetrics() {} 27 virtual ~TestAutofillMetrics() {} 28 }; 29 30 } // anonymous namespace 31 32 33 namespace content { 34 35 std::ostream& operator<<(std::ostream& os, const FormData& form) { 36 os << base::UTF16ToUTF8(form.name) 37 << " " 38 << form.origin.spec() 39 << " " 40 << form.action.spec() 41 << " "; 42 43 for (std::vector<FormFieldData>::const_iterator iter = 44 form.fields.begin(); 45 iter != form.fields.end(); ++iter) { 46 os << *iter 47 << " "; 48 } 49 50 return os; 51 } 52 53 } // namespace content 54 55 class FormStructureTest { 56 public: 57 static std::string Hash64Bit(const std::string& str) { 58 return FormStructure::Hash64Bit(str); 59 } 60 }; 61 62 TEST(FormStructureTest, FieldCount) { 63 scoped_ptr<FormStructure> form_structure; 64 FormData form; 65 66 FormFieldData field; 67 field.label = ASCIIToUTF16("username"); 68 field.name = ASCIIToUTF16("username"); 69 field.form_control_type = "text"; 70 form.fields.push_back(field); 71 72 field.label = ASCIIToUTF16("password"); 73 field.name = ASCIIToUTF16("password"); 74 field.form_control_type = "password"; 75 form.fields.push_back(field); 76 77 field.label = base::string16(); 78 field.name = ASCIIToUTF16("Submit"); 79 field.form_control_type = "submit"; 80 form.fields.push_back(field); 81 82 field.label = ASCIIToUTF16("address1"); 83 field.name = ASCIIToUTF16("address1"); 84 field.form_control_type = "text"; 85 field.should_autocomplete = false; 86 form.fields.push_back(field); 87 88 // The render process sends all fields to browser including fields with 89 // autocomplete=off 90 form_structure.reset(new FormStructure(form)); 91 EXPECT_EQ(4U, form_structure->field_count()); 92 } 93 94 TEST(FormStructureTest, AutofillCount) { 95 scoped_ptr<FormStructure> form_structure; 96 FormData form; 97 98 FormFieldData field; 99 field.label = ASCIIToUTF16("username"); 100 field.name = ASCIIToUTF16("username"); 101 field.form_control_type = "text"; 102 form.fields.push_back(field); 103 104 field.label = ASCIIToUTF16("password"); 105 field.name = ASCIIToUTF16("password"); 106 field.form_control_type = "password"; 107 form.fields.push_back(field); 108 109 field.label = ASCIIToUTF16("state"); 110 field.name = ASCIIToUTF16("state"); 111 field.form_control_type = "select-one"; 112 form.fields.push_back(field); 113 114 field.label = base::string16(); 115 field.name = ASCIIToUTF16("Submit"); 116 field.form_control_type = "submit"; 117 form.fields.push_back(field); 118 119 // Only text and select fields that are heuristically matched are counted. 120 form_structure.reset(new FormStructure(form)); 121 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 122 EXPECT_EQ(1U, form_structure->autofill_count()); 123 124 // Add a field with should_autocomplete=false. This should not be considered a 125 // fillable field. 126 field.label = ASCIIToUTF16("address1"); 127 field.name = ASCIIToUTF16("address1"); 128 field.form_control_type = "text"; 129 field.should_autocomplete = false; 130 form.fields.push_back(field); 131 132 form_structure.reset(new FormStructure(form)); 133 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 134 EXPECT_EQ(1U, form_structure->autofill_count()); 135 } 136 137 TEST(FormStructureTest, SourceURL) { 138 FormData form; 139 form.origin = GURL("http://www.foo.com/"); 140 FormStructure form_structure(form); 141 142 EXPECT_EQ(form.origin, form_structure.source_url()); 143 } 144 145 TEST(FormStructureTest, IsAutofillable) { 146 scoped_ptr<FormStructure> form_structure; 147 FormData form; 148 149 // We need at least three text fields to be auto-fillable. 150 FormFieldData field; 151 152 field.label = ASCIIToUTF16("username"); 153 field.name = ASCIIToUTF16("username"); 154 field.form_control_type = "text"; 155 form.fields.push_back(field); 156 157 field.label = ASCIIToUTF16("password"); 158 field.name = ASCIIToUTF16("password"); 159 field.form_control_type = "password"; 160 form.fields.push_back(field); 161 162 field.label = base::string16(); 163 field.name = ASCIIToUTF16("Submit"); 164 field.form_control_type = "submit"; 165 form.fields.push_back(field); 166 167 form_structure.reset(new FormStructure(form)); 168 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 169 EXPECT_FALSE(form_structure->IsAutofillable()); 170 171 // We now have three text fields, but only two auto-fillable fields. 172 field.label = ASCIIToUTF16("First Name"); 173 field.name = ASCIIToUTF16("firstname"); 174 field.form_control_type = "text"; 175 form.fields.push_back(field); 176 177 field.label = ASCIIToUTF16("Last Name"); 178 field.name = ASCIIToUTF16("lastname"); 179 field.form_control_type = "text"; 180 form.fields.push_back(field); 181 182 form_structure.reset(new FormStructure(form)); 183 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 184 EXPECT_FALSE(form_structure->IsAutofillable()); 185 186 // We now have three auto-fillable fields. 187 field.label = ASCIIToUTF16("Email"); 188 field.name = ASCIIToUTF16("email"); 189 field.form_control_type = "email"; 190 form.fields.push_back(field); 191 192 form_structure.reset(new FormStructure(form)); 193 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 194 EXPECT_TRUE(form_structure->IsAutofillable()); 195 196 // The target cannot include http(s)://*/search... 197 form.action = GURL("http://google.com/search?q=hello"); 198 form_structure.reset(new FormStructure(form)); 199 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 200 EXPECT_FALSE(form_structure->IsAutofillable()); 201 202 // But search can be in the URL. 203 form.action = GURL("http://search.com/?q=hello"); 204 form_structure.reset(new FormStructure(form)); 205 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 206 EXPECT_TRUE(form_structure->IsAutofillable()); 207 } 208 209 TEST(FormStructureTest, ShouldBeParsed) { 210 scoped_ptr<FormStructure> form_structure; 211 FormData form; 212 213 // We need at least three text fields to be parseable. 214 FormFieldData field; 215 field.label = ASCIIToUTF16("username"); 216 field.name = ASCIIToUTF16("username"); 217 field.form_control_type = "text"; 218 form.fields.push_back(field); 219 220 FormFieldData checkable_field; 221 checkable_field.is_checkable = true; 222 checkable_field.name = ASCIIToUTF16("radiobtn"); 223 checkable_field.form_control_type = "radio"; 224 form.fields.push_back(checkable_field); 225 226 checkable_field.name = ASCIIToUTF16("checkbox"); 227 checkable_field.form_control_type = "checkbox"; 228 form.fields.push_back(checkable_field); 229 230 // We have only one text field, should not be parsed. 231 form_structure.reset(new FormStructure(form)); 232 EXPECT_FALSE(form_structure->ShouldBeParsed()); 233 234 // We now have three text fields, though only two are auto-fillable. 235 field.label = ASCIIToUTF16("First Name"); 236 field.name = ASCIIToUTF16("firstname"); 237 field.form_control_type = "text"; 238 form.fields.push_back(field); 239 240 field.label = ASCIIToUTF16("Last Name"); 241 field.name = ASCIIToUTF16("lastname"); 242 field.form_control_type = "text"; 243 form.fields.push_back(field); 244 245 form_structure.reset(new FormStructure(form)); 246 EXPECT_TRUE(form_structure->ShouldBeParsed()); 247 248 form_structure.reset(new FormStructure(form)); 249 EXPECT_FALSE(form_structure->IsAutofillable()); 250 EXPECT_TRUE(form_structure->ShouldBeParsed()); 251 252 // The target cannot include http(s)://*/search... 253 form.action = GURL("http://google.com/search?q=hello"); 254 form_structure.reset(new FormStructure(form)); 255 EXPECT_FALSE(form_structure->ShouldBeParsed()); 256 257 // But search can be in the URL. 258 form.action = GURL("http://search.com/?q=hello"); 259 form_structure.reset(new FormStructure(form)); 260 EXPECT_TRUE(form_structure->ShouldBeParsed()); 261 262 // The form need only have three fields, but at least one must be a text 263 // field. 264 form.fields.clear(); 265 266 field.label = ASCIIToUTF16("Email"); 267 field.name = ASCIIToUTF16("email"); 268 field.form_control_type = "email"; 269 form.fields.push_back(field); 270 271 field.label = ASCIIToUTF16("State"); 272 field.name = ASCIIToUTF16("state"); 273 field.form_control_type = "select-one"; 274 form.fields.push_back(field); 275 276 field.label = ASCIIToUTF16("Country"); 277 field.name = ASCIIToUTF16("country"); 278 field.form_control_type = "select-one"; 279 form.fields.push_back(field); 280 281 form_structure.reset(new FormStructure(form)); 282 EXPECT_TRUE(form_structure->ShouldBeParsed()); 283 284 form.fields[0].form_control_type = "select-one"; 285 // Now, no text fields. 286 form_structure.reset(new FormStructure(form)); 287 EXPECT_FALSE(form_structure->ShouldBeParsed()); 288 } 289 290 TEST(FormStructureTest, HeuristicsContactInfo) { 291 scoped_ptr<FormStructure> form_structure; 292 FormData form; 293 294 FormFieldData field; 295 field.form_control_type = "text"; 296 297 field.label = ASCIIToUTF16("First Name"); 298 field.name = ASCIIToUTF16("firstname"); 299 form.fields.push_back(field); 300 301 field.label = ASCIIToUTF16("Last Name"); 302 field.name = ASCIIToUTF16("lastname"); 303 form.fields.push_back(field); 304 305 field.label = ASCIIToUTF16("Email"); 306 field.name = ASCIIToUTF16("email"); 307 form.fields.push_back(field); 308 309 field.label = ASCIIToUTF16("Phone"); 310 field.name = ASCIIToUTF16("phone"); 311 form.fields.push_back(field); 312 313 field.label = ASCIIToUTF16("Address"); 314 field.name = ASCIIToUTF16("address"); 315 form.fields.push_back(field); 316 317 field.label = ASCIIToUTF16("City"); 318 field.name = ASCIIToUTF16("city"); 319 form.fields.push_back(field); 320 321 field.label = ASCIIToUTF16("Zip code"); 322 field.name = ASCIIToUTF16("zipcode"); 323 form.fields.push_back(field); 324 325 field.label = base::string16(); 326 field.name = ASCIIToUTF16("Submit"); 327 field.form_control_type = "submit"; 328 form.fields.push_back(field); 329 330 form_structure.reset(new FormStructure(form)); 331 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 332 EXPECT_TRUE(form_structure->IsAutofillable()); 333 334 // Expect the correct number of fields. 335 ASSERT_EQ(8U, form_structure->field_count()); 336 ASSERT_EQ(7U, form_structure->autofill_count()); 337 338 // First name. 339 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 340 // Last name. 341 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 342 // Email. 343 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 344 // Phone. 345 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 346 form_structure->field(3)->heuristic_type()); 347 // Address. 348 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type()); 349 // City. 350 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type()); 351 // Zip. 352 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); 353 // Submit. 354 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type()); 355 } 356 357 // Verify that we can correctly process the |autocomplete| attribute. 358 TEST(FormStructureTest, HeuristicsAutocompleteAttribute) { 359 scoped_ptr<FormStructure> form_structure; 360 FormData form; 361 362 FormFieldData field; 363 field.form_control_type = "text"; 364 365 field.label = base::string16(); 366 field.name = ASCIIToUTF16("field1"); 367 field.autocomplete_attribute = "given-name"; 368 form.fields.push_back(field); 369 370 field.label = base::string16(); 371 field.name = ASCIIToUTF16("field2"); 372 field.autocomplete_attribute = "family-name"; 373 form.fields.push_back(field); 374 375 field.label = base::string16(); 376 field.name = ASCIIToUTF16("field3"); 377 field.autocomplete_attribute = "email"; 378 form.fields.push_back(field); 379 380 form_structure.reset(new FormStructure(form)); 381 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 382 EXPECT_TRUE(form_structure->IsAutofillable()); 383 384 // Expect the correct number of fields. 385 ASSERT_EQ(3U, form_structure->field_count()); 386 ASSERT_EQ(3U, form_structure->autofill_count()); 387 388 EXPECT_EQ(HTML_TYPE_GIVEN_NAME, form_structure->field(0)->html_type()); 389 EXPECT_EQ(HTML_TYPE_FAMILY_NAME, form_structure->field(1)->html_type()); 390 EXPECT_EQ(HTML_TYPE_EMAIL, form_structure->field(2)->html_type()); 391 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); 392 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); 393 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 394 } 395 396 // Verify that we can correctly process the 'autocomplete' attribute for phone 397 // number types (especially phone prefixes and suffixes). 398 TEST(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) { 399 scoped_ptr<FormStructure> form_structure; 400 FormData form; 401 402 FormFieldData field; 403 field.form_control_type = "text"; 404 405 field.label = base::string16(); 406 field.name = ASCIIToUTF16("field1"); 407 field.autocomplete_attribute = "tel-local"; 408 form.fields.push_back(field); 409 410 field.label = base::string16(); 411 field.name = ASCIIToUTF16("field2"); 412 field.autocomplete_attribute = "tel-local-prefix"; 413 form.fields.push_back(field); 414 415 field.label = base::string16(); 416 field.name = ASCIIToUTF16("field3"); 417 field.autocomplete_attribute = "tel-local-suffix"; 418 form.fields.push_back(field); 419 420 form_structure.reset(new FormStructure(form)); 421 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 422 EXPECT_TRUE(form_structure->IsAutofillable()); 423 424 // Expect the correct number of fields. 425 ASSERT_EQ(3U, form_structure->field_count()); 426 EXPECT_EQ(3U, form_structure->autofill_count()); 427 428 EXPECT_EQ(HTML_TYPE_TEL_LOCAL, form_structure->field(0)->html_type()); 429 EXPECT_EQ(AutofillField::IGNORED, form_structure->field(0)->phone_part()); 430 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_PREFIX, form_structure->field(1)->html_type()); 431 EXPECT_EQ(AutofillField::PHONE_PREFIX, 432 form_structure->field(1)->phone_part()); 433 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_SUFFIX, form_structure->field(2)->html_type()); 434 EXPECT_EQ(AutofillField::PHONE_SUFFIX, 435 form_structure->field(2)->phone_part()); 436 } 437 438 // If at least one field includes type hints in the 'autocomplete' attribute, we 439 // should not try to apply any other heuristics. 440 TEST(FormStructureTest, AutocompleteAttributeOverridesOtherHeuristics) { 441 scoped_ptr<FormStructure> form_structure; 442 FormData form; 443 444 // Start with a regular contact form. 445 FormFieldData field; 446 field.form_control_type = "text"; 447 448 field.label = ASCIIToUTF16("First Name"); 449 field.name = ASCIIToUTF16("firstname"); 450 form.fields.push_back(field); 451 452 field.label = ASCIIToUTF16("Last Name"); 453 field.name = ASCIIToUTF16("lastname"); 454 form.fields.push_back(field); 455 456 field.label = ASCIIToUTF16("Email"); 457 field.name = ASCIIToUTF16("email"); 458 form.fields.push_back(field); 459 460 form_structure.reset(new FormStructure(form)); 461 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 462 EXPECT_TRUE(form_structure->IsAutofillable()); 463 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced()); 464 465 ASSERT_EQ(3U, form_structure->field_count()); 466 ASSERT_EQ(3U, form_structure->autofill_count()); 467 468 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 469 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 470 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 471 472 // Now update the first form field to include an 'autocomplete' attribute. 473 form.fields.front().autocomplete_attribute = "x-other"; 474 form_structure.reset(new FormStructure(form)); 475 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 476 EXPECT_FALSE(form_structure->IsAutofillable()); 477 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); 478 479 ASSERT_EQ(3U, form_structure->field_count()); 480 ASSERT_EQ(0U, form_structure->autofill_count()); 481 482 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); 483 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); 484 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 485 } 486 487 // Verify that we can correctly process sections listed in the |autocomplete| 488 // attribute. 489 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) { 490 FormData form; 491 492 FormFieldData field; 493 field.form_control_type = "text"; 494 495 // Some fields will have no section specified. These fall into the default 496 // section. 497 field.autocomplete_attribute = "email"; 498 form.fields.push_back(field); 499 500 // We allow arbitrary section names. 501 field.autocomplete_attribute = "section-foo email"; 502 form.fields.push_back(field); 503 504 // "shipping" and "billing" are special section tokens that don't require the 505 // "section-" prefix. 506 field.autocomplete_attribute = "shipping email"; 507 form.fields.push_back(field); 508 field.autocomplete_attribute = "billing email"; 509 form.fields.push_back(field); 510 511 // "shipping" and "billing" can be combined with other section names. 512 field.autocomplete_attribute = "section-foo shipping email"; 513 form.fields.push_back(field); 514 field.autocomplete_attribute = "section-foo billing email"; 515 form.fields.push_back(field); 516 517 // We don't do anything clever to try to coalesce sections; it's up to site 518 // authors to avoid typos. 519 field.autocomplete_attribute = "section--foo email"; 520 form.fields.push_back(field); 521 522 // "shipping email" and "section--shipping" email should be parsed as 523 // different sections. This is only an interesting test due to how we 524 // implement implicit section names from attributes like "shipping email"; see 525 // the implementation for more details. 526 field.autocomplete_attribute = "section--shipping email"; 527 form.fields.push_back(field); 528 529 // Credit card fields are implicitly in a separate section from other fields. 530 field.autocomplete_attribute = "section-foo cc-number"; 531 form.fields.push_back(field); 532 533 FormStructure form_structure(form); 534 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 535 EXPECT_TRUE(form_structure.IsAutofillable()); 536 537 // Expect the correct number of fields. 538 ASSERT_EQ(9U, form_structure.field_count()); 539 EXPECT_EQ(9U, form_structure.autofill_count()); 540 541 // All of the fields in this form should be parsed as belonging to different 542 // sections. 543 std::set<std::string> section_names; 544 for (size_t i = 0; i < 9; ++i) { 545 section_names.insert(form_structure.field(i)->section()); 546 } 547 EXPECT_EQ(9U, section_names.size()); 548 } 549 550 // Verify that we can correctly process a degenerate section listed in the 551 // |autocomplete| attribute. 552 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsDegenerate) { 553 FormData form; 554 555 FormFieldData field; 556 field.form_control_type = "text"; 557 558 // Some fields will have no section specified. These fall into the default 559 // section. 560 field.autocomplete_attribute = "email"; 561 form.fields.push_back(field); 562 563 // Specifying "section-" is equivalent to not specifying a section. 564 field.autocomplete_attribute = "section- email"; 565 form.fields.push_back(field); 566 567 // Invalid tokens should prevent us from setting a section name. 568 field.autocomplete_attribute = "garbage section-foo email"; 569 form.fields.push_back(field); 570 field.autocomplete_attribute = "garbage section-bar email"; 571 form.fields.push_back(field); 572 field.autocomplete_attribute = "garbage shipping email"; 573 form.fields.push_back(field); 574 field.autocomplete_attribute = "garbage billing email"; 575 form.fields.push_back(field); 576 577 FormStructure form_structure(form); 578 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 579 580 // Expect the correct number of fields. 581 ASSERT_EQ(6U, form_structure.field_count()); 582 EXPECT_EQ(2U, form_structure.autofill_count()); 583 584 // All of the fields in this form should be parsed as belonging to the same 585 // section. 586 std::set<std::string> section_names; 587 for (size_t i = 0; i < 6; ++i) { 588 section_names.insert(form_structure.field(i)->section()); 589 } 590 EXPECT_EQ(1U, section_names.size()); 591 } 592 593 // Verify that we can correctly process repeated sections listed in the 594 // |autocomplete| attribute. 595 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsRepeated) { 596 FormData form; 597 598 FormFieldData field; 599 field.form_control_type = "text"; 600 601 field.autocomplete_attribute = "section-foo email"; 602 form.fields.push_back(field); 603 field.autocomplete_attribute = "section-foo address-line1"; 604 form.fields.push_back(field); 605 606 FormStructure form_structure(form); 607 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 608 609 // Expect the correct number of fields. 610 ASSERT_EQ(2U, form_structure.field_count()); 611 EXPECT_EQ(2U, form_structure.autofill_count()); 612 613 // All of the fields in this form should be parsed as belonging to the same 614 // section. 615 std::set<std::string> section_names; 616 for (size_t i = 0; i < 2; ++i) { 617 section_names.insert(form_structure.field(i)->section()); 618 } 619 EXPECT_EQ(1U, section_names.size()); 620 } 621 622 // Verify that we do not override the author-specified sections from a form with 623 // local heuristics. 624 TEST(FormStructureTest, HeuristicsDontOverrideAutocompleteAttributeSections) { 625 FormData form; 626 627 FormFieldData field; 628 field.form_control_type = "text"; 629 630 field.name = ASCIIToUTF16("one"); 631 field.autocomplete_attribute = "address-line1"; 632 form.fields.push_back(field); 633 field.name = base::string16(); 634 field.autocomplete_attribute = "section-foo email"; 635 form.fields.push_back(field); 636 field.name = base::string16(); 637 field.autocomplete_attribute = "name"; 638 form.fields.push_back(field); 639 field.name = ASCIIToUTF16("two"); 640 field.autocomplete_attribute = "address-line1"; 641 form.fields.push_back(field); 642 643 FormStructure form_structure(form); 644 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); 645 646 // Expect the correct number of fields. 647 ASSERT_EQ(4U, form_structure.field_count()); 648 EXPECT_EQ(4U, form_structure.autofill_count()); 649 650 // Normally, the two separate address fields would cause us to detect two 651 // separate sections; but because there is an author-specified section in this 652 // form, we do not apply these usual heuristics. 653 EXPECT_EQ(ASCIIToUTF16("one"), form_structure.field(0)->name); 654 EXPECT_EQ(ASCIIToUTF16("two"), form_structure.field(3)->name); 655 EXPECT_EQ(form_structure.field(0)->section(), 656 form_structure.field(3)->section()); 657 } 658 659 TEST(FormStructureTest, HeuristicsSample8) { 660 scoped_ptr<FormStructure> form_structure; 661 FormData form; 662 663 FormFieldData field; 664 field.form_control_type = "text"; 665 666 field.label = ASCIIToUTF16("Your First Name:"); 667 field.name = ASCIIToUTF16("bill.first"); 668 form.fields.push_back(field); 669 670 field.label = ASCIIToUTF16("Your Last Name:"); 671 field.name = ASCIIToUTF16("bill.last"); 672 form.fields.push_back(field); 673 674 field.label = ASCIIToUTF16("Street Address Line 1:"); 675 field.name = ASCIIToUTF16("bill.street1"); 676 form.fields.push_back(field); 677 678 field.label = ASCIIToUTF16("Street Address Line 2:"); 679 field.name = ASCIIToUTF16("bill.street2"); 680 form.fields.push_back(field); 681 682 field.label = ASCIIToUTF16("City"); 683 field.name = ASCIIToUTF16("bill.city"); 684 form.fields.push_back(field); 685 686 field.label = ASCIIToUTF16("State (U.S.):"); 687 field.name = ASCIIToUTF16("bill.state"); 688 form.fields.push_back(field); 689 690 field.label = ASCIIToUTF16("Zip/Postal Code:"); 691 field.name = ASCIIToUTF16("BillTo.PostalCode"); 692 form.fields.push_back(field); 693 694 field.label = ASCIIToUTF16("Country:"); 695 field.name = ASCIIToUTF16("bill.country"); 696 form.fields.push_back(field); 697 698 field.label = ASCIIToUTF16("Phone Number:"); 699 field.name = ASCIIToUTF16("BillTo.Phone"); 700 form.fields.push_back(field); 701 702 field.label = base::string16(); 703 field.name = ASCIIToUTF16("Submit"); 704 field.form_control_type = "submit"; 705 form.fields.push_back(field); 706 707 form_structure.reset(new FormStructure(form)); 708 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 709 EXPECT_TRUE(form_structure->IsAutofillable()); 710 ASSERT_EQ(10U, form_structure->field_count()); 711 ASSERT_EQ(9U, form_structure->autofill_count()); 712 713 // First name. 714 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 715 // Last name. 716 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 717 // Address. 718 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(2)->heuristic_type()); 719 // Address. 720 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(3)->heuristic_type()); 721 // City. 722 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type()); 723 // State. 724 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(5)->heuristic_type()); 725 // Zip. 726 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); 727 // Country. 728 EXPECT_EQ(ADDRESS_HOME_COUNTRY, form_structure->field(7)->heuristic_type()); 729 // Phone. 730 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 731 form_structure->field(8)->heuristic_type()); 732 // Submit. 733 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(9)->heuristic_type()); 734 } 735 736 TEST(FormStructureTest, HeuristicsSample6) { 737 scoped_ptr<FormStructure> form_structure; 738 FormData form; 739 740 FormFieldData field; 741 field.form_control_type = "text"; 742 743 field.label = ASCIIToUTF16("E-mail address"); 744 field.name = ASCIIToUTF16("email"); 745 form.fields.push_back(field); 746 747 field.label = ASCIIToUTF16("Full name"); 748 field.name = ASCIIToUTF16("name"); 749 form.fields.push_back(field); 750 751 field.label = ASCIIToUTF16("Company"); 752 field.name = ASCIIToUTF16("company"); 753 form.fields.push_back(field); 754 755 field.label = ASCIIToUTF16("Address"); 756 field.name = ASCIIToUTF16("address"); 757 form.fields.push_back(field); 758 759 field.label = ASCIIToUTF16("City"); 760 field.name = ASCIIToUTF16("city"); 761 form.fields.push_back(field); 762 763 field.label = ASCIIToUTF16("Zip Code"); 764 field.name = ASCIIToUTF16("Home.PostalCode"); 765 form.fields.push_back(field); 766 767 field.label = base::string16(); 768 field.name = ASCIIToUTF16("Submit"); 769 field.value = ASCIIToUTF16("continue"); 770 field.form_control_type = "submit"; 771 form.fields.push_back(field); 772 773 form_structure.reset(new FormStructure(form)); 774 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 775 EXPECT_TRUE(form_structure->IsAutofillable()); 776 ASSERT_EQ(7U, form_structure->field_count()); 777 ASSERT_EQ(6U, form_structure->autofill_count()); 778 779 // Email. 780 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(0)->heuristic_type()); 781 // Full name. 782 EXPECT_EQ(NAME_FULL, form_structure->field(1)->heuristic_type()); 783 // Company 784 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type()); 785 // Address. 786 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type()); 787 // City. 788 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type()); 789 // Zip. 790 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(5)->heuristic_type()); 791 // Submit. 792 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type()); 793 } 794 795 // Tests a sequence of FormFields where only labels are supplied to heuristics 796 // for matching. This works because FormFieldData labels are matched in the 797 // case that input element ids (or |name| fields) are missing. 798 TEST(FormStructureTest, HeuristicsLabelsOnly) { 799 scoped_ptr<FormStructure> form_structure; 800 FormData form; 801 802 FormFieldData field; 803 field.form_control_type = "text"; 804 805 field.label = ASCIIToUTF16("First Name"); 806 field.name = base::string16(); 807 form.fields.push_back(field); 808 809 field.label = ASCIIToUTF16("Last Name"); 810 field.name = base::string16(); 811 form.fields.push_back(field); 812 813 field.label = ASCIIToUTF16("Email"); 814 field.name = base::string16(); 815 form.fields.push_back(field); 816 817 field.label = ASCIIToUTF16("Phone"); 818 field.name = base::string16(); 819 form.fields.push_back(field); 820 821 field.label = ASCIIToUTF16("Address"); 822 field.name = base::string16(); 823 form.fields.push_back(field); 824 825 field.label = ASCIIToUTF16("Address"); 826 field.name = base::string16(); 827 form.fields.push_back(field); 828 829 field.label = ASCIIToUTF16("Zip code"); 830 field.name = base::string16(); 831 form.fields.push_back(field); 832 833 field.label = base::string16(); 834 field.name = ASCIIToUTF16("Submit"); 835 field.form_control_type = "submit"; 836 form.fields.push_back(field); 837 838 form_structure.reset(new FormStructure(form)); 839 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 840 EXPECT_TRUE(form_structure->IsAutofillable()); 841 ASSERT_EQ(8U, form_structure->field_count()); 842 ASSERT_EQ(7U, form_structure->autofill_count()); 843 844 // First name. 845 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 846 // Last name. 847 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 848 // Email. 849 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); 850 // Phone. 851 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 852 form_structure->field(3)->heuristic_type()); 853 // Address. 854 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type()); 855 // Address Line 2. 856 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(5)->heuristic_type()); 857 // Zip. 858 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); 859 // Submit. 860 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type()); 861 } 862 863 TEST(FormStructureTest, HeuristicsCreditCardInfo) { 864 scoped_ptr<FormStructure> form_structure; 865 FormData form; 866 867 FormFieldData field; 868 field.form_control_type = "text"; 869 870 field.label = ASCIIToUTF16("Name on Card"); 871 field.name = ASCIIToUTF16("name_on_card"); 872 form.fields.push_back(field); 873 874 field.label = ASCIIToUTF16("Card Number"); 875 field.name = ASCIIToUTF16("card_number"); 876 form.fields.push_back(field); 877 878 field.label = ASCIIToUTF16("Exp Month"); 879 field.name = ASCIIToUTF16("ccmonth"); 880 form.fields.push_back(field); 881 882 field.label = ASCIIToUTF16("Exp Year"); 883 field.name = ASCIIToUTF16("ccyear"); 884 form.fields.push_back(field); 885 886 field.label = ASCIIToUTF16("Verification"); 887 field.name = ASCIIToUTF16("verification"); 888 form.fields.push_back(field); 889 890 field.label = base::string16(); 891 field.name = ASCIIToUTF16("Submit"); 892 field.form_control_type = "submit"; 893 form.fields.push_back(field); 894 895 form_structure.reset(new FormStructure(form)); 896 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 897 EXPECT_TRUE(form_structure->IsAutofillable()); 898 ASSERT_EQ(6U, form_structure->field_count()); 899 ASSERT_EQ(5U, form_structure->autofill_count()); 900 901 // Credit card name. 902 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); 903 // Credit card number. 904 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(1)->heuristic_type()); 905 // Credit card expiration month. 906 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(2)->heuristic_type()); 907 // Credit card expiration year. 908 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 909 form_structure->field(3)->heuristic_type()); 910 // CVV. 911 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, 912 form_structure->field(4)->heuristic_type()); 913 // Submit. 914 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(5)->heuristic_type()); 915 } 916 917 TEST(FormStructureTest, HeuristicsCreditCardInfoWithUnknownCardField) { 918 scoped_ptr<FormStructure> form_structure; 919 FormData form; 920 921 FormFieldData field; 922 field.form_control_type = "text"; 923 924 field.label = ASCIIToUTF16("Name on Card"); 925 field.name = ASCIIToUTF16("name_on_card"); 926 form.fields.push_back(field); 927 928 // This is not a field we know how to process. But we should skip over it 929 // and process the other fields in the card block. 930 field.label = ASCIIToUTF16("Card image"); 931 field.name = ASCIIToUTF16("card_image"); 932 form.fields.push_back(field); 933 934 field.label = ASCIIToUTF16("Card Number"); 935 field.name = ASCIIToUTF16("card_number"); 936 form.fields.push_back(field); 937 938 field.label = ASCIIToUTF16("Exp Month"); 939 field.name = ASCIIToUTF16("ccmonth"); 940 form.fields.push_back(field); 941 942 field.label = ASCIIToUTF16("Exp Year"); 943 field.name = ASCIIToUTF16("ccyear"); 944 form.fields.push_back(field); 945 946 field.label = ASCIIToUTF16("Verification"); 947 field.name = ASCIIToUTF16("verification"); 948 form.fields.push_back(field); 949 950 field.label = base::string16(); 951 field.name = ASCIIToUTF16("Submit"); 952 field.form_control_type = "submit"; 953 form.fields.push_back(field); 954 955 form_structure.reset(new FormStructure(form)); 956 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 957 EXPECT_TRUE(form_structure->IsAutofillable()); 958 ASSERT_EQ(7U, form_structure->field_count()); 959 ASSERT_EQ(5U, form_structure->autofill_count()); 960 961 // Credit card name. 962 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); 963 // Credit card type. This is an unknown type but related to the credit card. 964 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); 965 // Credit card number. 966 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type()); 967 // Credit card expiration month. 968 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); 969 // Credit card expiration year. 970 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 971 form_structure->field(4)->heuristic_type()); 972 // CVV. 973 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, 974 form_structure->field(5)->heuristic_type()); 975 // Submit. 976 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type()); 977 } 978 979 TEST(FormStructureTest, ThreeAddressLines) { 980 scoped_ptr<FormStructure> form_structure; 981 FormData form; 982 983 FormFieldData field; 984 field.form_control_type = "text"; 985 986 field.label = ASCIIToUTF16("Address Line1"); 987 field.name = ASCIIToUTF16("Address"); 988 form.fields.push_back(field); 989 990 field.label = ASCIIToUTF16("Address Line2"); 991 field.name = ASCIIToUTF16("Address"); 992 form.fields.push_back(field); 993 994 field.label = ASCIIToUTF16("Address Line3"); 995 field.name = ASCIIToUTF16("Address"); 996 form.fields.push_back(field); 997 998 field.label = ASCIIToUTF16("City"); 999 field.name = ASCIIToUTF16("city"); 1000 form.fields.push_back(field); 1001 1002 form_structure.reset(new FormStructure(form)); 1003 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1004 EXPECT_TRUE(form_structure->IsAutofillable()); 1005 ASSERT_EQ(4U, form_structure->field_count()); 1006 ASSERT_EQ(3U, form_structure->autofill_count()); 1007 1008 // Address Line 1. 1009 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1010 // Address Line 2. 1011 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1012 // Address Line 3. 1013 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1014 // City. 1015 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type()); 1016 } 1017 1018 // Numbered address lines after line two are ignored. 1019 TEST(FormStructureTest, SurplusAddressLinesIgnored) { 1020 scoped_ptr<FormStructure> form_structure; 1021 FormData form; 1022 1023 FormFieldData field; 1024 field.form_control_type = "text"; 1025 1026 field.label = ASCIIToUTF16("Address Line1"); 1027 field.name = ASCIIToUTF16("shipping.address.addressLine1"); 1028 form.fields.push_back(field); 1029 1030 field.label = ASCIIToUTF16("Address Line2"); 1031 field.name = ASCIIToUTF16("shipping.address.addressLine2"); 1032 form.fields.push_back(field); 1033 1034 field.label = ASCIIToUTF16("Address Line3"); 1035 field.name = ASCIIToUTF16("billing.address.addressLine3"); 1036 form.fields.push_back(field); 1037 1038 field.label = ASCIIToUTF16("Address Line4"); 1039 field.name = ASCIIToUTF16("billing.address.addressLine4"); 1040 form.fields.push_back(field); 1041 1042 form_structure.reset(new FormStructure(form)); 1043 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1044 ASSERT_EQ(4U, form_structure->field_count()); 1045 ASSERT_EQ(2U, form_structure->autofill_count()); 1046 1047 // Address Line 1. 1048 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1049 // Address Line 2. 1050 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1051 // Address Line 3 (ignored). 1052 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1053 // Address Line 4 (ignored). 1054 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type()); 1055 } 1056 1057 // This example comes from expedia.com where they use a "Suite" label to 1058 // indicate a suite or apartment number. We interpret this as address line 2. 1059 // And the following "Street address second line" we interpret as address line 1060 // 3 and discard. 1061 // See http://crbug.com/48197 for details. 1062 TEST(FormStructureTest, ThreeAddressLinesExpedia) { 1063 scoped_ptr<FormStructure> form_structure; 1064 FormData form; 1065 1066 FormFieldData field; 1067 field.form_control_type = "text"; 1068 1069 field.label = ASCIIToUTF16("Street:"); 1070 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads1"); 1071 form.fields.push_back(field); 1072 1073 field.label = ASCIIToUTF16("Suite or Apt:"); 1074 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adap"); 1075 form.fields.push_back(field); 1076 1077 field.label = ASCIIToUTF16("Street address second line"); 1078 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads2"); 1079 form.fields.push_back(field); 1080 1081 field.label = ASCIIToUTF16("City:"); 1082 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adct"); 1083 form.fields.push_back(field); 1084 1085 form_structure.reset(new FormStructure(form)); 1086 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1087 EXPECT_TRUE(form_structure->IsAutofillable()); 1088 ASSERT_EQ(4U, form_structure->field_count()); 1089 EXPECT_EQ(3U, form_structure->autofill_count()); 1090 1091 // Address Line 1. 1092 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1093 // Suite / Apt. 1094 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1095 // Address Line 3. 1096 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1097 // City. 1098 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type()); 1099 } 1100 1101 // This example comes from ebay.com where the word "suite" appears in the label 1102 // and the name "address2" clearly indicates that this is the address line 2. 1103 // See http://crbug.com/48197 for details. 1104 TEST(FormStructureTest, TwoAddressLinesEbay) { 1105 scoped_ptr<FormStructure> form_structure; 1106 FormData form; 1107 1108 FormFieldData field; 1109 field.form_control_type = "text"; 1110 1111 field.label = ASCIIToUTF16("Address Line1"); 1112 field.name = ASCIIToUTF16("address1"); 1113 form.fields.push_back(field); 1114 1115 field.label = ASCIIToUTF16("Floor number, suite number, etc"); 1116 field.name = ASCIIToUTF16("address2"); 1117 form.fields.push_back(field); 1118 1119 field.label = ASCIIToUTF16("City:"); 1120 field.name = ASCIIToUTF16("city"); 1121 form.fields.push_back(field); 1122 1123 form_structure.reset(new FormStructure(form)); 1124 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1125 EXPECT_TRUE(form_structure->IsAutofillable()); 1126 ASSERT_EQ(3U, form_structure->field_count()); 1127 ASSERT_EQ(3U, form_structure->autofill_count()); 1128 1129 // Address Line 1. 1130 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1131 // Address Line 2. 1132 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1133 // City. 1134 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(2)->heuristic_type()); 1135 } 1136 1137 TEST(FormStructureTest, HeuristicsStateWithProvince) { 1138 scoped_ptr<FormStructure> form_structure; 1139 FormData form; 1140 1141 FormFieldData field; 1142 field.form_control_type = "text"; 1143 1144 field.label = ASCIIToUTF16("Address Line1"); 1145 field.name = ASCIIToUTF16("Address"); 1146 form.fields.push_back(field); 1147 1148 field.label = ASCIIToUTF16("Address Line2"); 1149 field.name = ASCIIToUTF16("Address"); 1150 form.fields.push_back(field); 1151 1152 field.label = ASCIIToUTF16("State/Province/Region"); 1153 field.name = ASCIIToUTF16("State"); 1154 form.fields.push_back(field); 1155 1156 form_structure.reset(new FormStructure(form)); 1157 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1158 EXPECT_TRUE(form_structure->IsAutofillable()); 1159 ASSERT_EQ(3U, form_structure->field_count()); 1160 ASSERT_EQ(3U, form_structure->autofill_count()); 1161 1162 // Address Line 1. 1163 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); 1164 // Address Line 2. 1165 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); 1166 // State. 1167 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(2)->heuristic_type()); 1168 } 1169 1170 // This example comes from lego.com's checkout page. 1171 TEST(FormStructureTest, HeuristicsWithBilling) { 1172 scoped_ptr<FormStructure> form_structure; 1173 FormData form; 1174 1175 FormFieldData field; 1176 field.form_control_type = "text"; 1177 1178 field.label = ASCIIToUTF16("First Name*:"); 1179 field.name = ASCIIToUTF16("editBillingAddress$firstNameBox"); 1180 form.fields.push_back(field); 1181 1182 field.label = ASCIIToUTF16("Last Name*:"); 1183 field.name = ASCIIToUTF16("editBillingAddress$lastNameBox"); 1184 form.fields.push_back(field); 1185 1186 field.label = ASCIIToUTF16("Company Name:"); 1187 field.name = ASCIIToUTF16("editBillingAddress$companyBox"); 1188 form.fields.push_back(field); 1189 1190 field.label = ASCIIToUTF16("Address*:"); 1191 field.name = ASCIIToUTF16("editBillingAddress$addressLine1Box"); 1192 form.fields.push_back(field); 1193 1194 field.label = ASCIIToUTF16("Apt/Suite :"); 1195 field.name = ASCIIToUTF16("editBillingAddress$addressLine2Box"); 1196 form.fields.push_back(field); 1197 1198 field.label = ASCIIToUTF16("City*:"); 1199 field.name = ASCIIToUTF16("editBillingAddress$cityBox"); 1200 form.fields.push_back(field); 1201 1202 field.label = ASCIIToUTF16("State/Province*:"); 1203 field.name = ASCIIToUTF16("editBillingAddress$stateDropDown"); 1204 form.fields.push_back(field); 1205 1206 field.label = ASCIIToUTF16("Country*:"); 1207 field.name = ASCIIToUTF16("editBillingAddress$countryDropDown"); 1208 form.fields.push_back(field); 1209 1210 field.label = ASCIIToUTF16("Postal Code*:"); 1211 field.name = ASCIIToUTF16("editBillingAddress$zipCodeBox"); 1212 form.fields.push_back(field); 1213 1214 field.label = ASCIIToUTF16("Phone*:"); 1215 field.name = ASCIIToUTF16("editBillingAddress$phoneBox"); 1216 form.fields.push_back(field); 1217 1218 field.label = ASCIIToUTF16("Email Address*:"); 1219 field.name = ASCIIToUTF16("email$emailBox"); 1220 form.fields.push_back(field); 1221 1222 form_structure.reset(new FormStructure(form)); 1223 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1224 EXPECT_TRUE(form_structure->IsAutofillable()); 1225 ASSERT_EQ(11U, form_structure->field_count()); 1226 ASSERT_EQ(11U, form_structure->autofill_count()); 1227 1228 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 1229 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 1230 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type()); 1231 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type()); 1232 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(4)->heuristic_type()); 1233 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type()); 1234 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(6)->heuristic_type()); 1235 EXPECT_EQ(ADDRESS_HOME_COUNTRY, form_structure->field(7)->heuristic_type()); 1236 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(8)->heuristic_type()); 1237 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 1238 form_structure->field(9)->heuristic_type()); 1239 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(10)->heuristic_type()); 1240 } 1241 1242 TEST(FormStructureTest, ThreePartPhoneNumber) { 1243 scoped_ptr<FormStructure> form_structure; 1244 FormData form; 1245 1246 FormFieldData field; 1247 field.form_control_type = "text"; 1248 1249 field.label = ASCIIToUTF16("Phone:"); 1250 field.name = ASCIIToUTF16("dayphone1"); 1251 field.max_length = 0; 1252 form.fields.push_back(field); 1253 1254 field.label = ASCIIToUTF16("-"); 1255 field.name = ASCIIToUTF16("dayphone2"); 1256 field.max_length = 3; // Size of prefix is 3. 1257 form.fields.push_back(field); 1258 1259 field.label = ASCIIToUTF16("-"); 1260 field.name = ASCIIToUTF16("dayphone3"); 1261 field.max_length = 4; // Size of suffix is 4. If unlimited size is 1262 // passed, phone will be parsed as 1263 // <country code> - <area code> - <phone>. 1264 form.fields.push_back(field); 1265 1266 field.label = ASCIIToUTF16("ext.:"); 1267 field.name = ASCIIToUTF16("dayphone4"); 1268 field.max_length = 0; 1269 form.fields.push_back(field); 1270 1271 form_structure.reset(new FormStructure(form)); 1272 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1273 EXPECT_TRUE(form_structure->IsAutofillable()); 1274 ASSERT_EQ(4U, form_structure->field_count()); 1275 ASSERT_EQ(3U, form_structure->autofill_count()); 1276 1277 // Area code. 1278 EXPECT_EQ(PHONE_HOME_CITY_CODE, form_structure->field(0)->heuristic_type()); 1279 // Phone number suffix. 1280 EXPECT_EQ(PHONE_HOME_NUMBER, 1281 form_structure->field(1)->heuristic_type()); 1282 // Phone number suffix. 1283 EXPECT_EQ(PHONE_HOME_NUMBER, 1284 form_structure->field(2)->heuristic_type()); 1285 // Unknown. 1286 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type()); 1287 } 1288 1289 TEST(FormStructureTest, HeuristicsInfernoCC) { 1290 scoped_ptr<FormStructure> form_structure; 1291 FormData form; 1292 1293 FormFieldData field; 1294 field.form_control_type = "text"; 1295 1296 field.label = ASCIIToUTF16("Name on Card"); 1297 field.name = ASCIIToUTF16("name_on_card"); 1298 form.fields.push_back(field); 1299 1300 field.label = ASCIIToUTF16("Address"); 1301 field.name = ASCIIToUTF16("billing_address"); 1302 form.fields.push_back(field); 1303 1304 field.label = ASCIIToUTF16("Card Number"); 1305 field.name = ASCIIToUTF16("card_number"); 1306 form.fields.push_back(field); 1307 1308 field.label = ASCIIToUTF16("Expiration Date"); 1309 field.name = ASCIIToUTF16("expiration_month"); 1310 form.fields.push_back(field); 1311 1312 field.label = ASCIIToUTF16("Expiration Year"); 1313 field.name = ASCIIToUTF16("expiration_year"); 1314 form.fields.push_back(field); 1315 1316 form_structure.reset(new FormStructure(form)); 1317 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1318 EXPECT_TRUE(form_structure->IsAutofillable()); 1319 1320 // Expect the correct number of fields. 1321 ASSERT_EQ(5U, form_structure->field_count()); 1322 EXPECT_EQ(5U, form_structure->autofill_count()); 1323 1324 // Name on Card. 1325 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); 1326 // Address. 1327 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(1)->heuristic_type()); 1328 // Card Number. 1329 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type()); 1330 // Expiration Date. 1331 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); 1332 // Expiration Year. 1333 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 1334 form_structure->field(4)->heuristic_type()); 1335 } 1336 1337 TEST(FormStructureTest, CVCCodeClash) { 1338 scoped_ptr<FormStructure> form_structure; 1339 FormData form; 1340 1341 FormFieldData field; 1342 field.form_control_type = "text"; 1343 1344 field.label = ASCIIToUTF16("Card number"); 1345 field.name = ASCIIToUTF16("ccnumber"); 1346 form.fields.push_back(field); 1347 1348 field.label = ASCIIToUTF16("First name"); 1349 field.name = ASCIIToUTF16("first_name"); 1350 form.fields.push_back(field); 1351 1352 field.label = ASCIIToUTF16("Last name"); 1353 field.name = ASCIIToUTF16("last_name"); 1354 form.fields.push_back(field); 1355 1356 field.label = ASCIIToUTF16("Expiration date"); 1357 field.name = ASCIIToUTF16("ccexpiresmonth"); 1358 form.fields.push_back(field); 1359 1360 field.label = base::string16(); 1361 field.name = ASCIIToUTF16("ccexpiresyear"); 1362 form.fields.push_back(field); 1363 1364 field.label = ASCIIToUTF16("cvc number"); 1365 field.name = ASCIIToUTF16("csc"); 1366 form.fields.push_back(field); 1367 1368 form_structure.reset(new FormStructure(form)); 1369 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1370 EXPECT_TRUE(form_structure->IsAutofillable()); 1371 1372 // Expect the correct number of fields. 1373 ASSERT_EQ(6U, form_structure->field_count()); 1374 ASSERT_EQ(5U, form_structure->autofill_count()); 1375 1376 // Card Number. 1377 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(0)->heuristic_type()); 1378 // First name, taken as name on card. 1379 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(1)->heuristic_type()); 1380 // Last name is not merged. 1381 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); 1382 // Expiration Date. 1383 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); 1384 // Expiration Year. 1385 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, 1386 form_structure->field(4)->heuristic_type()); 1387 // CVC code. 1388 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, 1389 form_structure->field(5)->heuristic_type()); 1390 } 1391 1392 TEST(FormStructureTest, EncodeQueryRequest) { 1393 FormData form; 1394 1395 FormFieldData field; 1396 field.form_control_type = "text"; 1397 1398 field.label = ASCIIToUTF16("Name on Card"); 1399 field.name = ASCIIToUTF16("name_on_card"); 1400 form.fields.push_back(field); 1401 1402 field.label = ASCIIToUTF16("Address"); 1403 field.name = ASCIIToUTF16("billing_address"); 1404 form.fields.push_back(field); 1405 1406 field.label = ASCIIToUTF16("Card Number"); 1407 field.name = ASCIIToUTF16("card_number"); 1408 form.fields.push_back(field); 1409 1410 field.label = ASCIIToUTF16("Expiration Date"); 1411 field.name = ASCIIToUTF16("expiration_month"); 1412 form.fields.push_back(field); 1413 1414 field.label = ASCIIToUTF16("Expiration Year"); 1415 field.name = ASCIIToUTF16("expiration_year"); 1416 form.fields.push_back(field); 1417 1418 // Add checkable field. 1419 FormFieldData checkable_field; 1420 checkable_field.is_checkable = true; 1421 checkable_field.label = ASCIIToUTF16("Checkable1"); 1422 checkable_field.name = ASCIIToUTF16("Checkable1"); 1423 form.fields.push_back(checkable_field); 1424 1425 ScopedVector<FormStructure> forms; 1426 forms.push_back(new FormStructure(form)); 1427 std::vector<std::string> encoded_signatures; 1428 std::string encoded_xml; 1429 const char kSignature1[] = "11337937696949187602"; 1430 const char kResponse1[] = 1431 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1432 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">" 1433 "<form signature=\"11337937696949187602\">" 1434 "<field signature=\"412125936\"/>" 1435 "<field signature=\"1917667676\"/>" 1436 "<field signature=\"2226358947\"/>" 1437 "<field signature=\"747221617\"/>" 1438 "<field signature=\"4108155786\"/>" 1439 "</form>" 1440 "</autofillquery>"; 1441 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1442 &encoded_signatures, 1443 &encoded_xml)); 1444 ASSERT_EQ(1U, encoded_signatures.size()); 1445 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1446 EXPECT_EQ(kResponse1, encoded_xml); 1447 1448 // Add the same form, only one will be encoded, so EncodeQueryRequest() should 1449 // return the same data. 1450 forms.push_back(new FormStructure(form)); 1451 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1452 &encoded_signatures, 1453 &encoded_xml)); 1454 ASSERT_EQ(1U, encoded_signatures.size()); 1455 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1456 EXPECT_EQ(kResponse1, encoded_xml); 1457 // Add 5 address fields - this should be still a valid form. 1458 for (size_t i = 0; i < 5; ++i) { 1459 field.label = ASCIIToUTF16("Address"); 1460 field.name = ASCIIToUTF16("address"); 1461 form.fields.push_back(field); 1462 } 1463 1464 forms.push_back(new FormStructure(form)); 1465 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1466 &encoded_signatures, 1467 &encoded_xml)); 1468 ASSERT_EQ(2U, encoded_signatures.size()); 1469 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1470 const char kSignature2[] = "8308881815906226214"; 1471 EXPECT_EQ(kSignature2, encoded_signatures[1]); 1472 const char kResponse2[] = 1473 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1474 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">" 1475 "<form signature=\"11337937696949187602\">" 1476 "<field signature=\"412125936\"/>" 1477 "<field signature=\"1917667676\"/>" 1478 "<field signature=\"2226358947\"/>" 1479 "<field signature=\"747221617\"/>" 1480 "<field signature=\"4108155786\"/>" 1481 "</form>" 1482 "<form signature=\"8308881815906226214\">" 1483 "<field signature=\"412125936\"/>" 1484 "<field signature=\"1917667676\"/>" 1485 "<field signature=\"2226358947\"/>" 1486 "<field signature=\"747221617\"/>" 1487 "<field signature=\"4108155786\"/>" 1488 "<field signature=\"509334676\"/>" 1489 "<field signature=\"509334676\"/>" 1490 "<field signature=\"509334676\"/>" 1491 "<field signature=\"509334676\"/>" 1492 "<field signature=\"509334676\"/>" 1493 "</form>" 1494 "</autofillquery>"; 1495 EXPECT_EQ(kResponse2, encoded_xml); 1496 1497 FormData malformed_form(form); 1498 // Add 50 address fields - the form is not valid anymore, but previous ones 1499 // are. The result should be the same as in previous test. 1500 for (size_t i = 0; i < 50; ++i) { 1501 field.label = ASCIIToUTF16("Address"); 1502 field.name = ASCIIToUTF16("address"); 1503 malformed_form.fields.push_back(field); 1504 } 1505 1506 forms.push_back(new FormStructure(malformed_form)); 1507 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 1508 &encoded_signatures, 1509 &encoded_xml)); 1510 ASSERT_EQ(2U, encoded_signatures.size()); 1511 EXPECT_EQ(kSignature1, encoded_signatures[0]); 1512 EXPECT_EQ(kSignature2, encoded_signatures[1]); 1513 EXPECT_EQ(kResponse2, encoded_xml); 1514 1515 // Check that we fail if there are only bad form(s). 1516 ScopedVector<FormStructure> bad_forms; 1517 bad_forms.push_back(new FormStructure(malformed_form)); 1518 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(), 1519 &encoded_signatures, 1520 &encoded_xml)); 1521 EXPECT_EQ(0U, encoded_signatures.size()); 1522 EXPECT_EQ("", encoded_xml); 1523 } 1524 1525 TEST(FormStructureTest, EncodeUploadRequest) { 1526 scoped_ptr<FormStructure> form_structure; 1527 std::vector<ServerFieldTypeSet> possible_field_types; 1528 FormData form; 1529 form_structure.reset(new FormStructure(form)); 1530 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1531 1532 FormFieldData field; 1533 field.form_control_type = "text"; 1534 1535 field.label = ASCIIToUTF16("First Name"); 1536 field.name = ASCIIToUTF16("firstname"); 1537 form.fields.push_back(field); 1538 possible_field_types.push_back(ServerFieldTypeSet()); 1539 possible_field_types.back().insert(NAME_FIRST); 1540 1541 field.label = ASCIIToUTF16("Last Name"); 1542 field.name = ASCIIToUTF16("lastname"); 1543 form.fields.push_back(field); 1544 possible_field_types.push_back(ServerFieldTypeSet()); 1545 possible_field_types.back().insert(NAME_LAST); 1546 1547 field.label = ASCIIToUTF16("Email"); 1548 field.name = ASCIIToUTF16("email"); 1549 field.form_control_type = "email"; 1550 form.fields.push_back(field); 1551 possible_field_types.push_back(ServerFieldTypeSet()); 1552 possible_field_types.back().insert(EMAIL_ADDRESS); 1553 1554 field.label = ASCIIToUTF16("Phone"); 1555 field.name = ASCIIToUTF16("phone"); 1556 field.form_control_type = "number"; 1557 form.fields.push_back(field); 1558 possible_field_types.push_back(ServerFieldTypeSet()); 1559 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); 1560 1561 field.label = ASCIIToUTF16("Country"); 1562 field.name = ASCIIToUTF16("country"); 1563 field.form_control_type = "select-one"; 1564 form.fields.push_back(field); 1565 possible_field_types.push_back(ServerFieldTypeSet()); 1566 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1567 1568 // Add checkable field. 1569 FormFieldData checkable_field; 1570 checkable_field.is_checkable = true; 1571 checkable_field.label = ASCIIToUTF16("Checkable1"); 1572 checkable_field.name = ASCIIToUTF16("Checkable1"); 1573 form.fields.push_back(checkable_field); 1574 possible_field_types.push_back(ServerFieldTypeSet()); 1575 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1576 1577 form_structure.reset(new FormStructure(form)); 1578 1579 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1580 for (size_t i = 0; i < form_structure->field_count(); ++i) 1581 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1582 1583 ServerFieldTypeSet available_field_types; 1584 available_field_types.insert(NAME_FIRST); 1585 available_field_types.insert(NAME_LAST); 1586 available_field_types.insert(ADDRESS_HOME_LINE1); 1587 available_field_types.insert(ADDRESS_HOME_LINE2); 1588 available_field_types.insert(ADDRESS_HOME_COUNTRY); 1589 available_field_types.insert(ADDRESS_BILLING_LINE1); 1590 available_field_types.insert(ADDRESS_BILLING_LINE2); 1591 available_field_types.insert(EMAIL_ADDRESS); 1592 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 1593 1594 std::string encoded_xml; 1595 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 1596 &encoded_xml)); 1597 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1598 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " 1599 "formsignature=\"8736493185895608956\" autofillused=\"false\" " 1600 "datapresent=\"144200030e\">" 1601 "<field signature=\"3763331450\" autofilltype=\"3\"/>" 1602 "<field signature=\"3494530716\" autofilltype=\"5\"/>" 1603 "<field signature=\"1029417091\" autofilltype=\"9\"/>" 1604 "<field signature=\"466116101\" autofilltype=\"14\"/>" 1605 "<field signature=\"2799270304\" autofilltype=\"36\"/>" 1606 "</autofillupload>", 1607 encoded_xml); 1608 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true, 1609 &encoded_xml)); 1610 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1611 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " 1612 "formsignature=\"8736493185895608956\" autofillused=\"true\" " 1613 "datapresent=\"144200030e\">" 1614 "<field signature=\"3763331450\" autofilltype=\"3\"/>" 1615 "<field signature=\"3494530716\" autofilltype=\"5\"/>" 1616 "<field signature=\"1029417091\" autofilltype=\"9\"/>" 1617 "<field signature=\"466116101\" autofilltype=\"14\"/>" 1618 "<field signature=\"2799270304\" autofilltype=\"36\"/>" 1619 "</autofillupload>", 1620 encoded_xml); 1621 1622 // Add 2 address fields - this should be still a valid form. 1623 for (size_t i = 0; i < 2; ++i) { 1624 field.label = ASCIIToUTF16("Address"); 1625 field.name = ASCIIToUTF16("address"); 1626 field.form_control_type = "text"; 1627 form.fields.push_back(field); 1628 possible_field_types.push_back(ServerFieldTypeSet()); 1629 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 1630 possible_field_types.back().insert(ADDRESS_HOME_LINE2); 1631 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); 1632 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); 1633 } 1634 1635 form_structure.reset(new FormStructure(form)); 1636 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1637 for (size_t i = 0; i < form_structure->field_count(); ++i) 1638 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1639 1640 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 1641 &encoded_xml)); 1642 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1643 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " 1644 "formsignature=\"7816485729218079147\" autofillused=\"false\" " 1645 "datapresent=\"144200030e\">" 1646 "<field signature=\"3763331450\" autofilltype=\"3\"/>" 1647 "<field signature=\"3494530716\" autofilltype=\"5\"/>" 1648 "<field signature=\"1029417091\" autofilltype=\"9\"/>" 1649 "<field signature=\"466116101\" autofilltype=\"14\"/>" 1650 "<field signature=\"2799270304\" autofilltype=\"36\"/>" 1651 "<field signature=\"509334676\" autofilltype=\"30\"/>" 1652 "<field signature=\"509334676\" autofilltype=\"31\"/>" 1653 "<field signature=\"509334676\" autofilltype=\"37\"/>" 1654 "<field signature=\"509334676\" autofilltype=\"38\"/>" 1655 "<field signature=\"509334676\" autofilltype=\"30\"/>" 1656 "<field signature=\"509334676\" autofilltype=\"31\"/>" 1657 "<field signature=\"509334676\" autofilltype=\"37\"/>" 1658 "<field signature=\"509334676\" autofilltype=\"38\"/>" 1659 "</autofillupload>", 1660 encoded_xml); 1661 1662 // Add 50 address fields - now the form is invalid, as it has too many fields. 1663 for (size_t i = 0; i < 50; ++i) { 1664 field.label = ASCIIToUTF16("Address"); 1665 field.name = ASCIIToUTF16("address"); 1666 field.form_control_type = "text"; 1667 form.fields.push_back(field); 1668 possible_field_types.push_back(ServerFieldTypeSet()); 1669 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 1670 possible_field_types.back().insert(ADDRESS_HOME_LINE2); 1671 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); 1672 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); 1673 } 1674 form_structure.reset(new FormStructure(form)); 1675 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1676 for (size_t i = 0; i < form_structure->field_count(); ++i) 1677 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1678 EXPECT_FALSE(form_structure->EncodeUploadRequest(available_field_types, false, 1679 &encoded_xml)); 1680 } 1681 1682 TEST(FormStructureTest, EncodeFieldAssignments) { 1683 scoped_ptr<FormStructure> form_structure; 1684 std::vector<ServerFieldTypeSet> possible_field_types; 1685 FormData form; 1686 form_structure.reset(new FormStructure(form)); 1687 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); 1688 1689 FormFieldData field; 1690 field.form_control_type = "text"; 1691 1692 field.label = ASCIIToUTF16("First Name"); 1693 field.name = ASCIIToUTF16("firstname"); 1694 form.fields.push_back(field); 1695 possible_field_types.push_back(ServerFieldTypeSet()); 1696 possible_field_types.back().insert(NAME_FIRST); 1697 1698 field.label = ASCIIToUTF16("Last Name"); 1699 field.name = ASCIIToUTF16("lastname"); 1700 form.fields.push_back(field); 1701 possible_field_types.push_back(ServerFieldTypeSet()); 1702 possible_field_types.back().insert(NAME_LAST); 1703 1704 field.label = ASCIIToUTF16("Email"); 1705 field.name = ASCIIToUTF16("email"); 1706 field.form_control_type = "email"; 1707 form.fields.push_back(field); 1708 possible_field_types.push_back(ServerFieldTypeSet()); 1709 possible_field_types.back().insert(EMAIL_ADDRESS); 1710 1711 field.label = ASCIIToUTF16("Phone"); 1712 field.name = ASCIIToUTF16("phone"); 1713 field.form_control_type = "number"; 1714 form.fields.push_back(field); 1715 possible_field_types.push_back(ServerFieldTypeSet()); 1716 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); 1717 1718 field.label = ASCIIToUTF16("Country"); 1719 field.name = ASCIIToUTF16("country"); 1720 field.form_control_type = "select-one"; 1721 form.fields.push_back(field); 1722 possible_field_types.push_back(ServerFieldTypeSet()); 1723 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1724 1725 // Add checkable field. 1726 FormFieldData checkable_field; 1727 checkable_field.is_checkable = true; 1728 checkable_field.label = ASCIIToUTF16("Checkable1"); 1729 checkable_field.name = ASCIIToUTF16("Checkable1"); 1730 form.fields.push_back(checkable_field); 1731 possible_field_types.push_back(ServerFieldTypeSet()); 1732 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); 1733 1734 form_structure.reset(new FormStructure(form)); 1735 1736 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1737 for (size_t i = 0; i < form_structure->field_count(); ++i) 1738 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1739 1740 ServerFieldTypeSet available_field_types; 1741 available_field_types.insert(NAME_FIRST); 1742 available_field_types.insert(NAME_LAST); 1743 available_field_types.insert(ADDRESS_HOME_LINE1); 1744 available_field_types.insert(ADDRESS_HOME_LINE2); 1745 available_field_types.insert(ADDRESS_HOME_COUNTRY); 1746 available_field_types.insert(ADDRESS_BILLING_LINE1); 1747 available_field_types.insert(ADDRESS_BILLING_LINE2); 1748 available_field_types.insert(EMAIL_ADDRESS); 1749 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 1750 1751 std::string encoded_xml; 1752 EXPECT_TRUE(form_structure->EncodeFieldAssignments( 1753 available_field_types, &encoded_xml)); 1754 EXPECT_EQ( 1755 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 1756 "<fieldassignments formsignature=\"8736493185895608956\">" 1757 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>" 1758 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>" 1759 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>" 1760 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>" 1761 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>" 1762 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>" 1763 "</fieldassignments>", 1764 encoded_xml); 1765 1766 // Add 2 address fields - this should be still a valid form. 1767 for (size_t i = 0; i < 2; ++i) { 1768 field.label = ASCIIToUTF16("Address"); 1769 field.name = ASCIIToUTF16("address"); 1770 field.form_control_type = "text"; 1771 form.fields.push_back(field); 1772 possible_field_types.push_back(ServerFieldTypeSet()); 1773 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 1774 possible_field_types.back().insert(ADDRESS_HOME_LINE2); 1775 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); 1776 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); 1777 } 1778 1779 form_structure.reset(new FormStructure(form)); 1780 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); 1781 for (size_t i = 0; i < form_structure->field_count(); ++i) 1782 form_structure->field(i)->set_possible_types(possible_field_types[i]); 1783 1784 EXPECT_TRUE(form_structure->EncodeFieldAssignments( 1785 available_field_types, &encoded_xml)); 1786 EXPECT_EQ( 1787 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 1788 "<fieldassignments formsignature=\"7816485729218079147\">" 1789 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>" 1790 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>" 1791 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>" 1792 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>" 1793 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>" 1794 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>" 1795 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>" 1796 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>" 1797 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>" 1798 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>" 1799 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>" 1800 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>" 1801 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>" 1802 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>" 1803 "</fieldassignments>", 1804 encoded_xml); 1805 } 1806 1807 // Check that we compute the "datapresent" string correctly for the given 1808 // |available_types|. 1809 TEST(FormStructureTest, CheckDataPresence) { 1810 FormData form; 1811 1812 FormFieldData field; 1813 field.form_control_type = "text"; 1814 1815 field.label = ASCIIToUTF16("First Name"); 1816 field.name = ASCIIToUTF16("first"); 1817 form.fields.push_back(field); 1818 1819 field.label = ASCIIToUTF16("Last Name"); 1820 field.name = ASCIIToUTF16("last"); 1821 form.fields.push_back(field); 1822 1823 field.label = ASCIIToUTF16("Email"); 1824 field.name = ASCIIToUTF16("email"); 1825 form.fields.push_back(field); 1826 1827 FormStructure form_structure(form); 1828 1829 ServerFieldTypeSet unknown_type; 1830 unknown_type.insert(UNKNOWN_TYPE); 1831 for (size_t i = 0; i < form_structure.field_count(); ++i) 1832 form_structure.field(i)->set_possible_types(unknown_type); 1833 1834 // No available types. 1835 // datapresent should be "" == trimmmed(0x0000000000000000) == 1836 // 0b0000000000000000000000000000000000000000000000000000000000000000 1837 ServerFieldTypeSet available_field_types; 1838 1839 std::string encoded_xml; 1840 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 1841 &encoded_xml)); 1842 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1843 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 1844 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 1845 " datapresent=\"\">" 1846 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 1847 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 1848 "<field signature=\"420638584\" autofilltype=\"1\"/>" 1849 "</autofillupload>", 1850 encoded_xml); 1851 1852 // Only a few types available. 1853 // datapresent should be "1540000240" == trimmmed(0x1540000240000000) == 1854 // 0b0001010101000000000000000000001001000000000000000000000000000000 1855 // The set bits are: 1856 // 3 == NAME_FIRST 1857 // 5 == NAME_LAST 1858 // 7 == NAME_FULL 1859 // 9 == EMAIL_ADDRESS 1860 // 30 == ADDRESS_HOME_LINE1 1861 // 33 == ADDRESS_HOME_CITY 1862 available_field_types.clear(); 1863 available_field_types.insert(NAME_FIRST); 1864 available_field_types.insert(NAME_LAST); 1865 available_field_types.insert(NAME_FULL); 1866 available_field_types.insert(EMAIL_ADDRESS); 1867 available_field_types.insert(ADDRESS_HOME_LINE1); 1868 available_field_types.insert(ADDRESS_HOME_CITY); 1869 1870 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 1871 &encoded_xml)); 1872 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1873 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 1874 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 1875 " datapresent=\"1540000240\">" 1876 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 1877 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 1878 "<field signature=\"420638584\" autofilltype=\"1\"/>" 1879 "</autofillupload>", 1880 encoded_xml); 1881 1882 // All supported non-credit card types available. 1883 // datapresent should be "1f7e000378000008" == trimmmed(0x1f7e000378000008) == 1884 // 0b0001111101111110000000000000001101111000000000000000000000001000 1885 // The set bits are: 1886 // 3 == NAME_FIRST 1887 // 4 == NAME_MIDDLE 1888 // 5 == NAME_LAST 1889 // 6 == NAME_MIDDLE_INITIAL 1890 // 7 == NAME_FULL 1891 // 9 == EMAIL_ADDRESS 1892 // 10 == PHONE_HOME_NUMBER, 1893 // 11 == PHONE_HOME_CITY_CODE, 1894 // 12 == PHONE_HOME_COUNTRY_CODE, 1895 // 13 == PHONE_HOME_CITY_AND_NUMBER, 1896 // 14 == PHONE_HOME_WHOLE_NUMBER, 1897 // 30 == ADDRESS_HOME_LINE1 1898 // 31 == ADDRESS_HOME_LINE2 1899 // 33 == ADDRESS_HOME_CITY 1900 // 34 == ADDRESS_HOME_STATE 1901 // 35 == ADDRESS_HOME_ZIP 1902 // 36 == ADDRESS_HOME_COUNTRY 1903 // 60 == COMPANY_NAME 1904 available_field_types.clear(); 1905 available_field_types.insert(NAME_FIRST); 1906 available_field_types.insert(NAME_MIDDLE); 1907 available_field_types.insert(NAME_LAST); 1908 available_field_types.insert(NAME_MIDDLE_INITIAL); 1909 available_field_types.insert(NAME_FULL); 1910 available_field_types.insert(EMAIL_ADDRESS); 1911 available_field_types.insert(PHONE_HOME_NUMBER); 1912 available_field_types.insert(PHONE_HOME_CITY_CODE); 1913 available_field_types.insert(PHONE_HOME_COUNTRY_CODE); 1914 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER); 1915 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 1916 available_field_types.insert(ADDRESS_HOME_LINE1); 1917 available_field_types.insert(ADDRESS_HOME_LINE2); 1918 available_field_types.insert(ADDRESS_HOME_CITY); 1919 available_field_types.insert(ADDRESS_HOME_STATE); 1920 available_field_types.insert(ADDRESS_HOME_ZIP); 1921 available_field_types.insert(ADDRESS_HOME_COUNTRY); 1922 available_field_types.insert(COMPANY_NAME); 1923 1924 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 1925 &encoded_xml)); 1926 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1927 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 1928 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 1929 " datapresent=\"1f7e000378000008\">" 1930 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 1931 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 1932 "<field signature=\"420638584\" autofilltype=\"1\"/>" 1933 "</autofillupload>", 1934 encoded_xml); 1935 1936 // All supported credit card types available. 1937 // datapresent should be "0000000000001fc0" == trimmmed(0x0000000000001fc0) == 1938 // 0b0000000000000000000000000000000000000000000000000001111111000000 1939 // The set bits are: 1940 // 51 == CREDIT_CARD_NAME 1941 // 52 == CREDIT_CARD_NUMBER 1942 // 53 == CREDIT_CARD_EXP_MONTH 1943 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR 1944 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR 1945 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR 1946 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR 1947 available_field_types.clear(); 1948 available_field_types.insert(CREDIT_CARD_NAME); 1949 available_field_types.insert(CREDIT_CARD_NUMBER); 1950 available_field_types.insert(CREDIT_CARD_EXP_MONTH); 1951 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); 1952 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); 1953 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); 1954 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); 1955 1956 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 1957 &encoded_xml)); 1958 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 1959 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 1960 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 1961 " datapresent=\"0000000000001fc0\">" 1962 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 1963 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 1964 "<field signature=\"420638584\" autofilltype=\"1\"/>" 1965 "</autofillupload>", 1966 encoded_xml); 1967 1968 // All supported types available. 1969 // datapresent should be "1f7e000378001fc8" == trimmmed(0x1f7e000378001fc8) == 1970 // 0b0001111101111110000000000000001101111000000000000001111111001000 1971 // The set bits are: 1972 // 3 == NAME_FIRST 1973 // 4 == NAME_MIDDLE 1974 // 5 == NAME_LAST 1975 // 6 == NAME_MIDDLE_INITIAL 1976 // 7 == NAME_FULL 1977 // 9 == EMAIL_ADDRESS 1978 // 10 == PHONE_HOME_NUMBER, 1979 // 11 == PHONE_HOME_CITY_CODE, 1980 // 12 == PHONE_HOME_COUNTRY_CODE, 1981 // 13 == PHONE_HOME_CITY_AND_NUMBER, 1982 // 14 == PHONE_HOME_WHOLE_NUMBER, 1983 // 30 == ADDRESS_HOME_LINE1 1984 // 31 == ADDRESS_HOME_LINE2 1985 // 33 == ADDRESS_HOME_CITY 1986 // 34 == ADDRESS_HOME_STATE 1987 // 35 == ADDRESS_HOME_ZIP 1988 // 36 == ADDRESS_HOME_COUNTRY 1989 // 51 == CREDIT_CARD_NAME 1990 // 52 == CREDIT_CARD_NUMBER 1991 // 53 == CREDIT_CARD_EXP_MONTH 1992 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR 1993 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR 1994 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR 1995 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR 1996 // 60 == COMPANY_NAME 1997 available_field_types.clear(); 1998 available_field_types.insert(NAME_FIRST); 1999 available_field_types.insert(NAME_MIDDLE); 2000 available_field_types.insert(NAME_LAST); 2001 available_field_types.insert(NAME_MIDDLE_INITIAL); 2002 available_field_types.insert(NAME_FULL); 2003 available_field_types.insert(EMAIL_ADDRESS); 2004 available_field_types.insert(PHONE_HOME_NUMBER); 2005 available_field_types.insert(PHONE_HOME_CITY_CODE); 2006 available_field_types.insert(PHONE_HOME_COUNTRY_CODE); 2007 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER); 2008 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 2009 available_field_types.insert(ADDRESS_HOME_LINE1); 2010 available_field_types.insert(ADDRESS_HOME_LINE2); 2011 available_field_types.insert(ADDRESS_HOME_CITY); 2012 available_field_types.insert(ADDRESS_HOME_STATE); 2013 available_field_types.insert(ADDRESS_HOME_ZIP); 2014 available_field_types.insert(ADDRESS_HOME_COUNTRY); 2015 available_field_types.insert(CREDIT_CARD_NAME); 2016 available_field_types.insert(CREDIT_CARD_NUMBER); 2017 available_field_types.insert(CREDIT_CARD_EXP_MONTH); 2018 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); 2019 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); 2020 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); 2021 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); 2022 available_field_types.insert(COMPANY_NAME); 2023 2024 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, 2025 &encoded_xml)); 2026 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2027 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2028 " formsignature=\"6402244543831589061\" autofillused=\"false\"" 2029 " datapresent=\"1f7e000378001fc8\">" 2030 "<field signature=\"1089846351\" autofilltype=\"1\"/>" 2031 "<field signature=\"2404144663\" autofilltype=\"1\"/>" 2032 "<field signature=\"420638584\" autofilltype=\"1\"/>" 2033 "</autofillupload>", 2034 encoded_xml); 2035 } 2036 2037 TEST(FormStructureTest, CheckMultipleTypes) { 2038 // Throughout this test, datapresent should be 2039 // 0x1440000360000008 == 2040 // 0b0001010001000000000000000000001101100000000000000000000000001000 2041 // The set bits are: 2042 // 3 == NAME_FIRST 2043 // 5 == NAME_LAST 2044 // 9 == EMAIL_ADDRESS 2045 // 30 == ADDRESS_HOME_LINE1 2046 // 31 == ADDRESS_HOME_LINE2 2047 // 33 == ADDRESS_HOME_CITY 2048 // 34 == ADDRESS_HOME_STATE 2049 // 60 == COMPANY_NAME 2050 ServerFieldTypeSet available_field_types; 2051 available_field_types.insert(NAME_FIRST); 2052 available_field_types.insert(NAME_LAST); 2053 available_field_types.insert(EMAIL_ADDRESS); 2054 available_field_types.insert(ADDRESS_HOME_LINE1); 2055 available_field_types.insert(ADDRESS_HOME_LINE2); 2056 available_field_types.insert(ADDRESS_HOME_CITY); 2057 available_field_types.insert(ADDRESS_HOME_STATE); 2058 available_field_types.insert(COMPANY_NAME); 2059 2060 // Check that multiple types for the field are processed correctly. 2061 scoped_ptr<FormStructure> form_structure; 2062 std::vector<ServerFieldTypeSet> possible_field_types; 2063 FormData form; 2064 2065 FormFieldData field; 2066 field.form_control_type = "text"; 2067 2068 field.label = ASCIIToUTF16("email"); 2069 field.name = ASCIIToUTF16("email"); 2070 form.fields.push_back(field); 2071 possible_field_types.push_back(ServerFieldTypeSet()); 2072 possible_field_types.back().insert(EMAIL_ADDRESS); 2073 2074 field.label = ASCIIToUTF16("First Name"); 2075 field.name = ASCIIToUTF16("first"); 2076 form.fields.push_back(field); 2077 possible_field_types.push_back(ServerFieldTypeSet()); 2078 possible_field_types.back().insert(NAME_FIRST); 2079 2080 field.label = ASCIIToUTF16("Last Name"); 2081 field.name = ASCIIToUTF16("last"); 2082 form.fields.push_back(field); 2083 possible_field_types.push_back(ServerFieldTypeSet()); 2084 possible_field_types.back().insert(NAME_LAST); 2085 2086 field.label = ASCIIToUTF16("Address"); 2087 field.name = ASCIIToUTF16("address"); 2088 form.fields.push_back(field); 2089 possible_field_types.push_back(ServerFieldTypeSet()); 2090 possible_field_types.back().insert(ADDRESS_HOME_LINE1); 2091 2092 form_structure.reset(new FormStructure(form)); 2093 2094 for (size_t i = 0; i < form_structure->field_count(); ++i) 2095 form_structure->field(i)->set_possible_types(possible_field_types[i]); 2096 std::string encoded_xml; 2097 2098 // Now we matched both fields singularly. 2099 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2100 &encoded_xml)); 2101 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2102 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2103 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2104 " datapresent=\"1440000360000008\">" 2105 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2106 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2107 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2108 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2109 "</autofillupload>", 2110 encoded_xml); 2111 // Match third field as both first and last. 2112 possible_field_types[2].insert(NAME_FIRST); 2113 form_structure->field(2)->set_possible_types(possible_field_types[2]); 2114 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2115 &encoded_xml)); 2116 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2117 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2118 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2119 " datapresent=\"1440000360000008\">" 2120 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2121 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2122 "<field signature=\"2404144663\" autofilltype=\"3\"/>" 2123 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2124 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2125 "</autofillupload>", 2126 encoded_xml); 2127 possible_field_types[3].insert(ADDRESS_HOME_LINE2); 2128 form_structure->field(form_structure->field_count() - 1)->set_possible_types( 2129 possible_field_types[form_structure->field_count() - 1]); 2130 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2131 &encoded_xml)); 2132 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2133 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2134 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2135 " datapresent=\"1440000360000008\">" 2136 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2137 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2138 "<field signature=\"2404144663\" autofilltype=\"3\"/>" 2139 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2140 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2141 "<field signature=\"509334676\" autofilltype=\"31\"/>" 2142 "</autofillupload>", 2143 encoded_xml); 2144 possible_field_types[3].clear(); 2145 possible_field_types[3].insert(ADDRESS_HOME_LINE1); 2146 possible_field_types[3].insert(COMPANY_NAME); 2147 form_structure->field(form_structure->field_count() - 1)->set_possible_types( 2148 possible_field_types[form_structure->field_count() - 1]); 2149 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, 2150 &encoded_xml)); 2151 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" 2152 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" 2153 " formsignature=\"18062476096658145866\" autofillused=\"false\"" 2154 " datapresent=\"1440000360000008\">" 2155 "<field signature=\"420638584\" autofilltype=\"9\"/>" 2156 "<field signature=\"1089846351\" autofilltype=\"3\"/>" 2157 "<field signature=\"2404144663\" autofilltype=\"3\"/>" 2158 "<field signature=\"2404144663\" autofilltype=\"5\"/>" 2159 "<field signature=\"509334676\" autofilltype=\"30\"/>" 2160 "<field signature=\"509334676\" autofilltype=\"60\"/>" 2161 "</autofillupload>", 2162 encoded_xml); 2163 } 2164 2165 TEST(FormStructureTest, CheckFormSignature) { 2166 // Check that form signature is created correctly. 2167 scoped_ptr<FormStructure> form_structure; 2168 FormData form; 2169 2170 FormFieldData field; 2171 field.form_control_type = "text"; 2172 2173 field.label = ASCIIToUTF16("email"); 2174 field.name = ASCIIToUTF16("email"); 2175 form.fields.push_back(field); 2176 2177 field.label = ASCIIToUTF16("First Name"); 2178 field.name = ASCIIToUTF16("first"); 2179 form.fields.push_back(field); 2180 2181 // Checkable fields shouldn't affect the signature. 2182 field.label = ASCIIToUTF16("Select"); 2183 field.name = ASCIIToUTF16("Select"); 2184 field.form_control_type = "checkbox"; 2185 field.is_checkable = true; 2186 form.fields.push_back(field); 2187 2188 form_structure.reset(new FormStructure(form)); 2189 2190 EXPECT_EQ(FormStructureTest::Hash64Bit( 2191 std::string("://&&email&first")), 2192 form_structure->FormSignature()); 2193 2194 form.origin = GURL(std::string("http://www.facebook.com")); 2195 form_structure.reset(new FormStructure(form)); 2196 EXPECT_EQ(FormStructureTest::Hash64Bit( 2197 std::string("http://www.facebook.com&&email&first")), 2198 form_structure->FormSignature()); 2199 2200 form.action = GURL(std::string("https://login.facebook.com/path")); 2201 form_structure.reset(new FormStructure(form)); 2202 EXPECT_EQ(FormStructureTest::Hash64Bit( 2203 std::string("https://login.facebook.com&&email&first")), 2204 form_structure->FormSignature()); 2205 2206 form.name = ASCIIToUTF16("login_form"); 2207 form_structure.reset(new FormStructure(form)); 2208 EXPECT_EQ(FormStructureTest::Hash64Bit( 2209 std::string("https://login.facebook.com&login_form&email&first")), 2210 form_structure->FormSignature()); 2211 2212 field.is_checkable = false; 2213 field.label = ASCIIToUTF16("Random Field label"); 2214 field.name = ASCIIToUTF16("random1234"); 2215 field.form_control_type = "text"; 2216 form.fields.push_back(field); 2217 field.label = ASCIIToUTF16("Random Field label2"); 2218 field.name = ASCIIToUTF16("random12345"); 2219 form.fields.push_back(field); 2220 field.label = ASCIIToUTF16("Random Field label3"); 2221 field.name = ASCIIToUTF16("1random12345678"); 2222 form.fields.push_back(field); 2223 field.label = ASCIIToUTF16("Random Field label3"); 2224 field.name = ASCIIToUTF16("12345random"); 2225 form.fields.push_back(field); 2226 form_structure.reset(new FormStructure(form)); 2227 EXPECT_EQ(FormStructureTest::Hash64Bit( 2228 std::string("https://login.facebook.com&login_form&email&first&" 2229 "random1234&random&1random&random")), 2230 form_structure->FormSignature()); 2231 2232 } 2233 2234 TEST(FormStructureTest, ToFormData) { 2235 FormData form; 2236 form.name = ASCIIToUTF16("the-name"); 2237 form.origin = GURL("http://cool.com"); 2238 form.action = form.origin.Resolve("/login"); 2239 2240 FormFieldData field; 2241 field.label = ASCIIToUTF16("username"); 2242 field.name = ASCIIToUTF16("username"); 2243 field.form_control_type = "text"; 2244 form.fields.push_back(field); 2245 2246 field.label = ASCIIToUTF16("password"); 2247 field.name = ASCIIToUTF16("password"); 2248 field.form_control_type = "password"; 2249 form.fields.push_back(field); 2250 2251 field.label = base::string16(); 2252 field.name = ASCIIToUTF16("Submit"); 2253 field.form_control_type = "submit"; 2254 form.fields.push_back(field); 2255 2256 EXPECT_EQ(form, FormStructure(form).ToFormData()); 2257 2258 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always 2259 // false. This forces a future author that changes this to update this test. 2260 form.user_submitted = true; 2261 EXPECT_NE(form, FormStructure(form).ToFormData()); 2262 } 2263 2264 TEST(FormStructureTest, SkipFieldTest) { 2265 FormData form; 2266 form.name = ASCIIToUTF16("the-name"); 2267 form.origin = GURL("http://cool.com"); 2268 form.action = form.origin.Resolve("/login"); 2269 2270 FormFieldData field; 2271 field.label = ASCIIToUTF16("username"); 2272 field.name = ASCIIToUTF16("username"); 2273 field.form_control_type = "text"; 2274 form.fields.push_back(field); 2275 2276 field.label = ASCIIToUTF16("select"); 2277 field.name = ASCIIToUTF16("select"); 2278 field.form_control_type = "checkbox"; 2279 field.is_checkable = true; 2280 form.fields.push_back(field); 2281 2282 field.label = base::string16(); 2283 field.name = ASCIIToUTF16("email"); 2284 field.form_control_type = "text"; 2285 field.is_checkable = false; 2286 form.fields.push_back(field); 2287 2288 ScopedVector<FormStructure> forms; 2289 forms.push_back(new FormStructure(form)); 2290 std::vector<std::string> encoded_signatures; 2291 std::string encoded_xml; 2292 2293 const char kSignature[] = "18006745212084723782"; 2294 const char kResponse[] = 2295 "<\?xml version=\"1.0\" encoding=\"UTF-8\"?>" 2296 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">" 2297 "<form signature=\"18006745212084723782\">" 2298 "<field signature=\"239111655\"/>" 2299 "<field signature=\"420638584\"/>" 2300 "</form>" 2301 "</autofillquery>"; 2302 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), 2303 &encoded_signatures, 2304 &encoded_xml)); 2305 ASSERT_EQ(1U, encoded_signatures.size()); 2306 EXPECT_EQ(kSignature, encoded_signatures[0]); 2307 EXPECT_EQ(kResponse, encoded_xml); 2308 } 2309 2310 TEST(FormStructureTest, PossibleValues) { 2311 FormData form_data; 2312 FormFieldData field; 2313 field.autocomplete_attribute = "billing country"; 2314 field.option_contents.push_back(ASCIIToUTF16("Down Under")); 2315 field.option_values.push_back(ASCIIToUTF16("AU")); 2316 field.option_contents.push_back(ASCIIToUTF16("Fr")); 2317 field.option_values.push_back(ASCIIToUTF16("")); 2318 field.option_contents.push_back(ASCIIToUTF16("Germany")); 2319 field.option_values.push_back(ASCIIToUTF16("GRMNY")); 2320 form_data.fields.push_back(field); 2321 FormStructure form_structure(form_data); 2322 2323 bool unused; 2324 form_structure.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused); 2325 2326 // All values in <option> value= or contents are returned, set to upper case. 2327 std::set<base::string16> possible_values = 2328 form_structure.PossibleValues(ADDRESS_BILLING_COUNTRY); 2329 EXPECT_EQ(5U, possible_values.size()); 2330 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("AU"))); 2331 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("FR"))); 2332 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("DOWN UNDER"))); 2333 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("GERMANY"))); 2334 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("GRMNY"))); 2335 EXPECT_EQ(0U, possible_values.count(ASCIIToUTF16("Fr"))); 2336 EXPECT_EQ(0U, possible_values.count(ASCIIToUTF16("DE"))); 2337 2338 // No field for the given type; empty value set. 2339 EXPECT_EQ(0U, form_structure.PossibleValues(ADDRESS_HOME_COUNTRY).size()); 2340 2341 // A freeform input (<input>) allows any value (overriding other <select>s). 2342 FormFieldData freeform_field; 2343 freeform_field.autocomplete_attribute = "billing country"; 2344 form_data.fields.push_back(freeform_field); 2345 FormStructure form_structure2(form_data); 2346 form_structure2.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused); 2347 EXPECT_EQ(0U, form_structure2.PossibleValues(ADDRESS_BILLING_COUNTRY).size()); 2348 } 2349 2350 } // namespace autofill 2351