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"); | |
dhollowa
2011/08/11 14:52:14
nit: indent (and below).
Ilya Sherman
2011/08/12 01:30:50
Done. Thanks for the catch =)
| |
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("field2"); | |
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("field3"); | |
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("field4"); | |
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("field5"); | |
471 field.autocomplete_type = | |
472 ASCIIToUTF16("section--shipping locality"); | |
473 form.fields.push_back(field); | |
474 | |
475 // Credit card fields are implicitly in a separate section from other fields. | |
476 field.label = string16(); | |
477 field.name = ASCIIToUTF16("field6"); | |
478 field.autocomplete_type = | |
479 ASCIIToUTF16("section-shipping cc-number"); | |
480 form.fields.push_back(field); | |
481 | |
482 form_structure.reset(new FormStructure(form)); | |
483 form_structure->DetermineHeuristicTypes(); | |
484 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
485 | |
486 // Expect the correct number of fields. | |
487 ASSERT_EQ(6U, form_structure->field_count()); | |
488 ASSERT_EQ(6U, form_structure->autofill_count()); | |
489 | |
490 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
491 EXPECT_EQ(ASCIIToUTF16("shipping-default"), | |
492 form_structure->field(0)->section()); | |
493 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
494 EXPECT_EQ(ASCIIToUTF16("-default"), form_structure->field(1)->section()); | |
495 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(2)->heuristic_type()); | |
496 EXPECT_EQ(ASCIIToUTF16("foo-default"), form_structure->field(2)->section()); | |
497 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(3)->heuristic_type()); | |
498 EXPECT_EQ(ASCIIToUTF16("-default"), form_structure->field(3)->section()); | |
499 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type()); | |
500 EXPECT_EQ(ASCIIToUTF16("-shipping-default"), | |
501 form_structure->field(4)->section()); | |
502 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(5)->heuristic_type()); | |
503 EXPECT_EQ(ASCIIToUTF16("shipping-cc"), form_structure->field(5)->section()); | |
504 } | |
505 | |
429 // Verify that we can correctly process fallback types listed in the | 506 // Verify that we can correctly process fallback types listed in the |
430 // 'autocompletetype' attribute. | 507 // |autocompletetype| attribute. |
431 TEST(FormStructureTest, HeuristicsAutocompletetypeWithFallbacks) { | 508 TEST(FormStructureTest, HeuristicsAutocompletetypeWithFallbacks) { |
432 scoped_ptr<FormStructure> form_structure; | 509 scoped_ptr<FormStructure> form_structure; |
433 FormData form; | 510 FormData form; |
434 form.method = ASCIIToUTF16("post"); | 511 form.method = ASCIIToUTF16("post"); |
435 | 512 |
436 FormField field; | 513 FormField field; |
437 field.form_control_type = ASCIIToUTF16("text"); | 514 field.form_control_type = ASCIIToUTF16("text"); |
438 | 515 |
439 // Skip over any sections and "x"-prefixed types. | 516 // Skip over any sections and "x"-prefixed types. |
440 field.label = string16(); | 517 field.label = string16(); |
(...skipping 17 matching lines...) Expand all Loading... | |
458 | 535 |
459 form_structure.reset(new FormStructure(form)); | 536 form_structure.reset(new FormStructure(form)); |
460 form_structure->DetermineHeuristicTypes(); | 537 form_structure->DetermineHeuristicTypes(); |
461 EXPECT_TRUE(form_structure->IsAutofillable(true)); | 538 EXPECT_TRUE(form_structure->IsAutofillable(true)); |
462 | 539 |
463 // Expect the correct number of fields. | 540 // Expect the correct number of fields. |
464 ASSERT_EQ(3U, form_structure->field_count()); | 541 ASSERT_EQ(3U, form_structure->field_count()); |
465 ASSERT_EQ(3U, form_structure->autofill_count()); | 542 ASSERT_EQ(3U, form_structure->autofill_count()); |
466 | 543 |
467 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | 544 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); |
545 EXPECT_EQ(ASCIIToUTF16("full-name-default"), | |
546 form_structure->field(0)->section()); | |
468 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | 547 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); |
548 EXPECT_EQ(ASCIIToUTF16("full-name-default"), | |
549 form_structure->field(1)->section()); | |
469 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, | 550 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, |
470 form_structure->field(2)->heuristic_type()); | 551 form_structure->field(2)->heuristic_type()); |
552 EXPECT_EQ(ASCIIToUTF16("shipping-default"), | |
553 form_structure->field(2)->section()); | |
471 } | 554 } |
472 | 555 |
473 TEST(FormStructureTest, HeuristicsSample8) { | 556 TEST(FormStructureTest, HeuristicsSample8) { |
474 scoped_ptr<FormStructure> form_structure; | 557 scoped_ptr<FormStructure> form_structure; |
475 FormData form; | 558 FormData form; |
476 form.method = ASCIIToUTF16("post"); | 559 form.method = ASCIIToUTF16("post"); |
477 | 560 |
478 FormField field; | 561 FormField field; |
479 field.form_control_type = ASCIIToUTF16("text"); | 562 field.form_control_type = ASCIIToUTF16("text"); |
480 | 563 |
(...skipping 1427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1908 form_structure->FormSignature()); | 1991 form_structure->FormSignature()); |
1909 | 1992 |
1910 form.name = ASCIIToUTF16("login_form"); | 1993 form.name = ASCIIToUTF16("login_form"); |
1911 form_structure.reset(new FormStructure(form)); | 1994 form_structure.reset(new FormStructure(form)); |
1912 EXPECT_EQ(FormStructureTest::Hash64Bit( | 1995 EXPECT_EQ(FormStructureTest::Hash64Bit( |
1913 std::string("https://login.facebook.com&login_form&email&first")), | 1996 std::string("https://login.facebook.com&login_form&email&first")), |
1914 form_structure->FormSignature()); | 1997 form_structure->FormSignature()); |
1915 } | 1998 } |
1916 | 1999 |
1917 } // namespace | 2000 } // namespace |
OLD | NEW |