| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/autofill/form_structure.h" | 8 #include "chrome/browser/autofill/form_structure.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 // Address. | 332 // Address. |
| 333 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(5)->heuristic_type()); | 333 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(5)->heuristic_type()); |
| 334 // City. | 334 // City. |
| 335 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(6)->heuristic_type()); | 335 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(6)->heuristic_type()); |
| 336 // Zip. | 336 // Zip. |
| 337 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(7)->heuristic_type()); | 337 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(7)->heuristic_type()); |
| 338 // Submit. | 338 // Submit. |
| 339 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(8)->heuristic_type()); | 339 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(8)->heuristic_type()); |
| 340 } | 340 } |
| 341 | 341 |
| 342 // Verify that we can correctly process the 'autocompletetype' attribute. | 342 // Verify that we can correctly process the |autocompletetype| attribute. |
| 343 TEST(FormStructureTest, HeuristicsAutocompletetype) { | 343 TEST(FormStructureTest, HeuristicsAutocompletetype) { |
| 344 scoped_ptr<FormStructure> form_structure; | 344 scoped_ptr<FormStructure> form_structure; |
| 345 FormData form; | 345 FormData form; |
| 346 form.method = ASCIIToUTF16("post"); | 346 form.method = ASCIIToUTF16("post"); |
| 347 | 347 |
| 348 FormField field; | 348 FormField field; |
| 349 field.form_control_type = ASCIIToUTF16("text"); | 349 field.form_control_type = ASCIIToUTF16("text"); |
| 350 | 350 |
| 351 field.label = string16(); | 351 field.label = string16(); |
| 352 field.name = ASCIIToUTF16("field1"); | 352 field.name = ASCIIToUTF16("field1"); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 369 | 369 |
| 370 // Expect the correct number of fields. | 370 // Expect the correct number of fields. |
| 371 ASSERT_EQ(3U, form_structure->field_count()); | 371 ASSERT_EQ(3U, form_structure->field_count()); |
| 372 ASSERT_EQ(3U, form_structure->autofill_count()); | 372 ASSERT_EQ(3U, form_structure->autofill_count()); |
| 373 | 373 |
| 374 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | 374 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); |
| 375 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | 375 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); |
| 376 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); | 376 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); |
| 377 } | 377 } |
| 378 | 378 |
| 379 // If at least one field includes the 'autocompletetype' attribute, we should | 379 // If at least one field includes the |autocompletetype| attribute, we should |
| 380 // not try to apply any other heuristics. | 380 // not try to apply any other heuristics. |
| 381 TEST(FormStructureTest, AutocompletetypeOverridesOtherHeuristics) { | 381 TEST(FormStructureTest, AutocompletetypeOverridesOtherHeuristics) { |
| 382 scoped_ptr<FormStructure> form_structure; | 382 scoped_ptr<FormStructure> form_structure; |
| 383 FormData form; | 383 FormData form; |
| 384 form.method = ASCIIToUTF16("post"); | 384 form.method = ASCIIToUTF16("post"); |
| 385 | 385 |
| 386 // Start with a regular contact form. | 386 // Start with a regular contact form. |
| 387 FormField field; | 387 FormField field; |
| 388 field.form_control_type = ASCIIToUTF16("text"); | 388 field.form_control_type = ASCIIToUTF16("text"); |
| 389 | 389 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 419 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); | 419 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); |
| 420 | 420 |
| 421 ASSERT_EQ(3U, form_structure->field_count()); | 421 ASSERT_EQ(3U, form_structure->field_count()); |
| 422 ASSERT_EQ(0U, form_structure->autofill_count()); | 422 ASSERT_EQ(0U, form_structure->autofill_count()); |
| 423 | 423 |
| 424 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); | 424 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); |
| 425 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); | 425 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); |
| 426 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | 426 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); |
| 427 } | 427 } |
| 428 | 428 |
| 429 // Verify that we can correctly process sections listed in the |autocomplete| |
| 430 // attribute. |
| 431 TEST(FormStructureTest, HeuristicsAutocompletetypeWithSections) { |
| 432 scoped_ptr<FormStructure> form_structure; |
| 433 FormData form; |
| 434 form.method = ASCIIToUTF16("post"); |
| 435 |
| 436 FormField field; |
| 437 field.form_control_type = ASCIIToUTF16("text"); |
| 438 |
| 439 // We expect "shipping" and "billing" to be the most common sections. |
| 440 field.label = string16(); |
| 441 field.name = ASCIIToUTF16("field1"); |
| 442 field.autocomplete_type = |
| 443 ASCIIToUTF16("section-shipping given-name"); |
| 444 form.fields.push_back(field); |
| 445 |
| 446 // Some field will have no section specified. These fall into the default |
| 447 // section, with an empty name. |
| 448 field.label = string16(); |
| 449 field.name = ASCIIToUTF16("field1"); |
| 450 field.autocomplete_type = |
| 451 ASCIIToUTF16("surname"); |
| 452 form.fields.push_back(field); |
| 453 |
| 454 // We allow arbitrary section names. |
| 455 field.label = string16(); |
| 456 field.name = ASCIIToUTF16("field2"); |
| 457 field.autocomplete_type = ASCIIToUTF16("section-foo address-line1"); |
| 458 form.fields.push_back(field); |
| 459 |
| 460 // Specifying "section-" is equivalent to not specifying a section. |
| 461 field.label = string16(); |
| 462 field.name = ASCIIToUTF16("field3"); |
| 463 field.autocomplete_type = |
| 464 ASCIIToUTF16("section- address-line2"); |
| 465 form.fields.push_back(field); |
| 466 |
| 467 // We don't do anything clever to try to coalesce sections; it's up to site |
| 468 // authors to avoid typos. |
| 469 field.label = string16(); |
| 470 field.name = ASCIIToUTF16("field3"); |
| 471 field.autocomplete_type = |
| 472 ASCIIToUTF16("section--shipping locality"); |
| 473 form.fields.push_back(field); |
| 474 |
| 475 form_structure.reset(new FormStructure(form)); |
| 476 form_structure->DetermineHeuristicTypes(); |
| 477 EXPECT_TRUE(form_structure->IsAutofillable(true)); |
| 478 |
| 479 // Expect the correct number of fields. |
| 480 ASSERT_EQ(5U, form_structure->field_count()); |
| 481 ASSERT_EQ(5U, form_structure->autofill_count()); |
| 482 |
| 483 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); |
| 484 EXPECT_EQ(ASCIIToUTF16("shipping"), form_structure->field(0)->section()); |
| 485 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); |
| 486 EXPECT_EQ(string16(), form_structure->field(1)->section()); |
| 487 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(2)->heuristic_type()); |
| 488 EXPECT_EQ(ASCIIToUTF16("foo"), form_structure->field(2)->section()); |
| 489 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(3)->heuristic_type()); |
| 490 EXPECT_EQ(string16(), form_structure->field(3)->section()); |
| 491 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type()); |
| 492 EXPECT_EQ(ASCIIToUTF16("-shipping"), form_structure->field(4)->section()); |
| 493 } |
| 494 |
| 429 // Verify that we can correctly process fallback types listed in the | 495 // Verify that we can correctly process fallback types listed in the |
| 430 // 'autocompletetype' attribute. | 496 // |autocompletetype| attribute. |
| 431 TEST(FormStructureTest, HeuristicsAutocompletetypeWithFallbacks) { | 497 TEST(FormStructureTest, HeuristicsAutocompletetypeWithFallbacks) { |
| 432 scoped_ptr<FormStructure> form_structure; | 498 scoped_ptr<FormStructure> form_structure; |
| 433 FormData form; | 499 FormData form; |
| 434 form.method = ASCIIToUTF16("post"); | 500 form.method = ASCIIToUTF16("post"); |
| 435 | 501 |
| 436 FormField field; | 502 FormField field; |
| 437 field.form_control_type = ASCIIToUTF16("text"); | 503 field.form_control_type = ASCIIToUTF16("text"); |
| 438 | 504 |
| 439 // Skip over any sections and "x"-prefixed types. | 505 // Skip over any sections and "x"-prefixed types. |
| 440 field.label = string16(); | 506 field.label = string16(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 458 | 524 |
| 459 form_structure.reset(new FormStructure(form)); | 525 form_structure.reset(new FormStructure(form)); |
| 460 form_structure->DetermineHeuristicTypes(); | 526 form_structure->DetermineHeuristicTypes(); |
| 461 EXPECT_TRUE(form_structure->IsAutofillable(true)); | 527 EXPECT_TRUE(form_structure->IsAutofillable(true)); |
| 462 | 528 |
| 463 // Expect the correct number of fields. | 529 // Expect the correct number of fields. |
| 464 ASSERT_EQ(3U, form_structure->field_count()); | 530 ASSERT_EQ(3U, form_structure->field_count()); |
| 465 ASSERT_EQ(3U, form_structure->autofill_count()); | 531 ASSERT_EQ(3U, form_structure->autofill_count()); |
| 466 | 532 |
| 467 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | 533 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); |
| 534 EXPECT_EQ(ASCIIToUTF16("full-name"), form_structure->field(0)->section()); |
| 468 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | 535 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); |
| 536 EXPECT_EQ(ASCIIToUTF16("full-name"), form_structure->field(1)->section()); |
| 469 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, | 537 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, |
| 470 form_structure->field(2)->heuristic_type()); | 538 form_structure->field(2)->heuristic_type()); |
| 539 EXPECT_EQ(ASCIIToUTF16("shipping"), form_structure->field(2)->section()); |
| 471 } | 540 } |
| 472 | 541 |
| 473 TEST(FormStructureTest, HeuristicsSample8) { | 542 TEST(FormStructureTest, HeuristicsSample8) { |
| 474 scoped_ptr<FormStructure> form_structure; | 543 scoped_ptr<FormStructure> form_structure; |
| 475 FormData form; | 544 FormData form; |
| 476 form.method = ASCIIToUTF16("post"); | 545 form.method = ASCIIToUTF16("post"); |
| 477 | 546 |
| 478 FormField field; | 547 FormField field; |
| 479 field.form_control_type = ASCIIToUTF16("text"); | 548 field.form_control_type = ASCIIToUTF16("text"); |
| 480 | 549 |
| (...skipping 1427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1908 form_structure->FormSignature()); | 1977 form_structure->FormSignature()); |
| 1909 | 1978 |
| 1910 form.name = ASCIIToUTF16("login_form"); | 1979 form.name = ASCIIToUTF16("login_form"); |
| 1911 form_structure.reset(new FormStructure(form)); | 1980 form_structure.reset(new FormStructure(form)); |
| 1912 EXPECT_EQ(FormStructureTest::Hash64Bit( | 1981 EXPECT_EQ(FormStructureTest::Hash64Bit( |
| 1913 std::string("https://login.facebook.com&login_form&email&first")), | 1982 std::string("https://login.facebook.com&login_form&email&first")), |
| 1914 form_structure->FormSignature()); | 1983 form_structure->FormSignature()); |
| 1915 } | 1984 } |
| 1916 | 1985 |
| 1917 } // namespace | 1986 } // namespace |
| OLD | NEW |