Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chrome/browser/autofill/form_structure_unittest.cc

Issue 7613015: Add preliminary Autofill support for 'autocompletetype' sections. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Line unwrapping Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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 = ASCIIToUTF16("section-shipping given-name");
443 form.fields.push_back(field);
444
445 // Some field will have no section specified. These fall into the default
446 // section, with an empty name.
447 field.label = string16();
448 field.name = ASCIIToUTF16("field2");
449 field.autocomplete_type = ASCIIToUTF16("surname");
450 form.fields.push_back(field);
451
452 // We allow arbitrary section names.
453 field.label = string16();
454 field.name = ASCIIToUTF16("field3");
455 field.autocomplete_type = ASCIIToUTF16("section-foo address-line1");
456 form.fields.push_back(field);
457
458 // Specifying "section-" is equivalent to not specifying a section.
459 field.label = string16();
460 field.name = ASCIIToUTF16("field4");
461 field.autocomplete_type = ASCIIToUTF16("section- address-line2");
462 form.fields.push_back(field);
463
464 // We don't do anything clever to try to coalesce sections; it's up to site
465 // authors to avoid typos.
466 field.label = string16();
467 field.name = ASCIIToUTF16("field5");
468 field.autocomplete_type = ASCIIToUTF16("section--shipping locality");
469 form.fields.push_back(field);
470
471 // Credit card fields are implicitly in a separate section from other fields.
472 field.label = string16();
473 field.name = ASCIIToUTF16("field6");
474 field.autocomplete_type = ASCIIToUTF16("section-shipping cc-number");
475 form.fields.push_back(field);
476
477 form_structure.reset(new FormStructure(form));
478 form_structure->DetermineHeuristicTypes();
479 EXPECT_TRUE(form_structure->IsAutofillable(true));
480
481 // Expect the correct number of fields.
482 ASSERT_EQ(6U, form_structure->field_count());
483 ASSERT_EQ(6U, form_structure->autofill_count());
484
485 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
486 EXPECT_EQ(ASCIIToUTF16("shipping-default"),
487 form_structure->field(0)->section());
488 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
489 EXPECT_EQ(ASCIIToUTF16("-default"), form_structure->field(1)->section());
490 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(2)->heuristic_type());
491 EXPECT_EQ(ASCIIToUTF16("foo-default"), form_structure->field(2)->section());
492 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(3)->heuristic_type());
493 EXPECT_EQ(ASCIIToUTF16("-default"), form_structure->field(3)->section());
494 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type());
495 EXPECT_EQ(ASCIIToUTF16("-shipping-default"),
496 form_structure->field(4)->section());
497 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(5)->heuristic_type());
498 EXPECT_EQ(ASCIIToUTF16("shipping-cc"), form_structure->field(5)->section());
499 }
500
429 // Verify that we can correctly process fallback types listed in the 501 // Verify that we can correctly process fallback types listed in the
430 // 'autocompletetype' attribute. 502 // |autocompletetype| attribute.
431 TEST(FormStructureTest, HeuristicsAutocompletetypeWithFallbacks) { 503 TEST(FormStructureTest, HeuristicsAutocompletetypeWithFallbacks) {
432 scoped_ptr<FormStructure> form_structure; 504 scoped_ptr<FormStructure> form_structure;
433 FormData form; 505 FormData form;
434 form.method = ASCIIToUTF16("post"); 506 form.method = ASCIIToUTF16("post");
435 507
436 FormField field; 508 FormField field;
437 field.form_control_type = ASCIIToUTF16("text"); 509 field.form_control_type = ASCIIToUTF16("text");
438 510
439 // Skip over any sections and "x"-prefixed types. 511 // Skip over any sections and "x"-prefixed types.
440 field.label = string16(); 512 field.label = string16();
(...skipping 17 matching lines...) Expand all
458 530
459 form_structure.reset(new FormStructure(form)); 531 form_structure.reset(new FormStructure(form));
460 form_structure->DetermineHeuristicTypes(); 532 form_structure->DetermineHeuristicTypes();
461 EXPECT_TRUE(form_structure->IsAutofillable(true)); 533 EXPECT_TRUE(form_structure->IsAutofillable(true));
462 534
463 // Expect the correct number of fields. 535 // Expect the correct number of fields.
464 ASSERT_EQ(3U, form_structure->field_count()); 536 ASSERT_EQ(3U, form_structure->field_count());
465 ASSERT_EQ(3U, form_structure->autofill_count()); 537 ASSERT_EQ(3U, form_structure->autofill_count());
466 538
467 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); 539 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
540 EXPECT_EQ(ASCIIToUTF16("full-name-default"),
541 form_structure->field(0)->section());
468 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); 542 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
543 EXPECT_EQ(ASCIIToUTF16("full-name-default"),
544 form_structure->field(1)->section());
469 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 545 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
470 form_structure->field(2)->heuristic_type()); 546 form_structure->field(2)->heuristic_type());
547 EXPECT_EQ(ASCIIToUTF16("shipping-default"),
548 form_structure->field(2)->section());
471 } 549 }
472 550
473 TEST(FormStructureTest, HeuristicsSample8) { 551 TEST(FormStructureTest, HeuristicsSample8) {
474 scoped_ptr<FormStructure> form_structure; 552 scoped_ptr<FormStructure> form_structure;
475 FormData form; 553 FormData form;
476 form.method = ASCIIToUTF16("post"); 554 form.method = ASCIIToUTF16("post");
477 555
478 FormField field; 556 FormField field;
479 field.form_control_type = ASCIIToUTF16("text"); 557 field.form_control_type = ASCIIToUTF16("text");
480 558
(...skipping 1427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1908 form_structure->FormSignature()); 1986 form_structure->FormSignature());
1909 1987
1910 form.name = ASCIIToUTF16("login_form"); 1988 form.name = ASCIIToUTF16("login_form");
1911 form_structure.reset(new FormStructure(form)); 1989 form_structure.reset(new FormStructure(form));
1912 EXPECT_EQ(FormStructureTest::Hash64Bit( 1990 EXPECT_EQ(FormStructureTest::Hash64Bit(
1913 std::string("https://login.facebook.com&login_form&email&first")), 1991 std::string("https://login.facebook.com&login_form&email&first")),
1914 form_structure->FormSignature()); 1992 form_structure->FormSignature());
1915 } 1993 }
1916 1994
1917 } // namespace 1995 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698