OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/autofill/core/browser/form_structure.h" | 5 #include "components/autofill/core/browser/form_structure.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 ASSERT_EQ(3U, form_structure->autofill_count()); | 438 ASSERT_EQ(3U, form_structure->autofill_count()); |
439 | 439 |
440 EXPECT_EQ(HTML_TYPE_GIVEN_NAME, form_structure->field(0)->html_type()); | 440 EXPECT_EQ(HTML_TYPE_GIVEN_NAME, form_structure->field(0)->html_type()); |
441 EXPECT_EQ(HTML_TYPE_FAMILY_NAME, form_structure->field(1)->html_type()); | 441 EXPECT_EQ(HTML_TYPE_FAMILY_NAME, form_structure->field(1)->html_type()); |
442 EXPECT_EQ(HTML_TYPE_EMAIL, form_structure->field(2)->html_type()); | 442 EXPECT_EQ(HTML_TYPE_EMAIL, form_structure->field(2)->html_type()); |
443 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); | 443 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); |
444 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); | 444 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); |
445 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | 445 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); |
446 } | 446 } |
447 | 447 |
| 448 // All fields share a common prefix which could confuse the heuristics. Test |
| 449 // that the common prefix is stripped out before running heuristics. |
| 450 TEST_F(FormStructureTest, StripCommonNamePrefix) { |
| 451 FormData form; |
| 452 FormFieldData field; |
| 453 field.form_control_type = "text"; |
| 454 |
| 455 field.label = ASCIIToUTF16("First Name"); |
| 456 field.name = ASCIIToUTF16("ctl01$ctl00$ShippingAddressCreditPhone$firstname"); |
| 457 form.fields.push_back(field); |
| 458 |
| 459 field.label = ASCIIToUTF16("Last Name"); |
| 460 field.name = ASCIIToUTF16("ctl01$ctl00$ShippingAddressCreditPhone$lastname"); |
| 461 form.fields.push_back(field); |
| 462 |
| 463 field.label = ASCIIToUTF16("Email"); |
| 464 field.name = ASCIIToUTF16("ctl01$ctl00$ShippingAddressCreditPhone$email"); |
| 465 form.fields.push_back(field); |
| 466 |
| 467 field.label = ASCIIToUTF16("Phone"); |
| 468 field.name = ASCIIToUTF16("ctl01$ctl00$ShippingAddressCreditPhone$phone"); |
| 469 form.fields.push_back(field); |
| 470 |
| 471 field.label = base::string16(); |
| 472 field.name = ASCIIToUTF16("ctl01$ctl00$ShippingAddressCreditPhone$submit"); |
| 473 field.form_control_type = "submit"; |
| 474 form.fields.push_back(field); |
| 475 |
| 476 scoped_ptr<FormStructure> form_structure(new FormStructure(form)); |
| 477 form_structure->DetermineHeuristicTypes(); |
| 478 EXPECT_TRUE(form_structure->IsAutofillable()); |
| 479 |
| 480 // Expect the correct number of fields. |
| 481 ASSERT_EQ(5U, form_structure->field_count()); |
| 482 ASSERT_EQ(4U, form_structure->autofill_count()); |
| 483 |
| 484 // First name. |
| 485 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); |
| 486 // Last name. |
| 487 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); |
| 488 // Email. |
| 489 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); |
| 490 // Phone. |
| 491 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, |
| 492 form_structure->field(3)->heuristic_type()); |
| 493 // Submit. |
| 494 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(4)->heuristic_type()); |
| 495 } |
| 496 |
| 497 // All fields share a common prefix which is small enough that it is not |
| 498 // stripped from the name before running the heuristics. |
| 499 TEST_F(FormStructureTest, StripCommonNamePrefix_SmallPrefix) { |
| 500 FormData form; |
| 501 FormFieldData field; |
| 502 field.form_control_type = "text"; |
| 503 |
| 504 field.label = ASCIIToUTF16("Address 1"); |
| 505 field.name = ASCIIToUTF16("address1"); |
| 506 form.fields.push_back(field); |
| 507 |
| 508 field.label = ASCIIToUTF16("Address 2"); |
| 509 field.name = ASCIIToUTF16("address2"); |
| 510 form.fields.push_back(field); |
| 511 |
| 512 field.label = ASCIIToUTF16("Address 3"); |
| 513 field.name = ASCIIToUTF16("address3"); |
| 514 form.fields.push_back(field); |
| 515 |
| 516 scoped_ptr<FormStructure> form_structure(new FormStructure(form)); |
| 517 form_structure->DetermineHeuristicTypes(); |
| 518 EXPECT_TRUE(form_structure->IsAutofillable()); |
| 519 |
| 520 // Expect the correct number of fields. |
| 521 ASSERT_EQ(3U, form_structure->field_count()); |
| 522 ASSERT_EQ(3U, form_structure->autofill_count()); |
| 523 |
| 524 // Address 1. |
| 525 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); |
| 526 // Address 2. |
| 527 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); |
| 528 // Address 3 |
| 529 EXPECT_EQ(ADDRESS_HOME_LINE3, form_structure->field(2)->heuristic_type()); |
| 530 } |
| 531 |
448 // Verify that we can correctly process the 'autocomplete' attribute for phone | 532 // Verify that we can correctly process the 'autocomplete' attribute for phone |
449 // number types (especially phone prefixes and suffixes). | 533 // number types (especially phone prefixes and suffixes). |
450 TEST_F(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) { | 534 TEST_F(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) { |
451 scoped_ptr<FormStructure> form_structure; | 535 scoped_ptr<FormStructure> form_structure; |
452 FormData form; | 536 FormData form; |
453 | 537 |
454 FormFieldData field; | 538 FormFieldData field; |
455 field.form_control_type = "text"; | 539 field.form_control_type = "text"; |
456 | 540 |
457 field.label = base::string16(); | 541 field.label = base::string16(); |
(...skipping 1773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2231 encoded_upload.SerializeToString(&encoded_upload_string); | 2315 encoded_upload.SerializeToString(&encoded_upload_string); |
2232 EXPECT_EQ(expected_upload_string, encoded_upload_string); | 2316 EXPECT_EQ(expected_upload_string, encoded_upload_string); |
2233 } | 2317 } |
2234 | 2318 |
2235 // Test that the form name is sent in the upload request. | 2319 // Test that the form name is sent in the upload request. |
2236 TEST_F(FormStructureTest, EncodeUploadRequest_WithFormName) { | 2320 TEST_F(FormStructureTest, EncodeUploadRequest_WithFormName) { |
2237 scoped_ptr<FormStructure> form_structure; | 2321 scoped_ptr<FormStructure> form_structure; |
2238 std::vector<ServerFieldTypeSet> possible_field_types; | 2322 std::vector<ServerFieldTypeSet> possible_field_types; |
2239 FormData form; | 2323 FormData form; |
2240 // Setting the form name which we expect to see in the upload. | 2324 // Setting the form name which we expect to see in the upload. |
2241 form.name = base::ASCIIToUTF16("myform"); | 2325 form.name = ASCIIToUTF16("myform"); |
2242 form_structure.reset(new FormStructure(form)); | 2326 form_structure.reset(new FormStructure(form)); |
2243 form_structure->DetermineHeuristicTypes(); | 2327 form_structure->DetermineHeuristicTypes(); |
2244 | 2328 |
2245 FormFieldData field; | 2329 FormFieldData field; |
2246 field.form_control_type = "text"; | 2330 field.form_control_type = "text"; |
2247 | 2331 |
2248 form.fields.push_back(field); | 2332 form.fields.push_back(field); |
2249 possible_field_types.push_back(ServerFieldTypeSet()); | 2333 possible_field_types.push_back(ServerFieldTypeSet()); |
2250 possible_field_types.back().insert(NAME_FIRST); | 2334 possible_field_types.back().insert(NAME_FIRST); |
2251 | 2335 |
(...skipping 1060 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3312 | 3396 |
3313 std::string response_string; | 3397 std::string response_string; |
3314 ASSERT_TRUE(response.SerializeToString(&response_string)); | 3398 ASSERT_TRUE(response.SerializeToString(&response_string)); |
3315 FormStructure::ParseQueryResponse(response_string, forms.get(), nullptr); | 3399 FormStructure::ParseQueryResponse(response_string, forms.get(), nullptr); |
3316 | 3400 |
3317 ASSERT_GE(forms[0]->field_count(), 2U); | 3401 ASSERT_GE(forms[0]->field_count(), 2U); |
3318 EXPECT_EQ(NO_SERVER_DATA, forms[0]->field(0)->server_type()); | 3402 EXPECT_EQ(NO_SERVER_DATA, forms[0]->field(0)->server_type()); |
3319 EXPECT_EQ(76, forms[0]->field(1)->server_type()); | 3403 EXPECT_EQ(76, forms[0]->field(1)->server_type()); |
3320 } | 3404 } |
3321 | 3405 |
| 3406 TEST_F(FormStructureTest, FindLongestCommonPrefix) { |
| 3407 // Normal case. |
| 3408 std::vector<base::string16> strings; |
| 3409 strings.push_back(ASCIIToUTF16("123456789")); |
| 3410 strings.push_back(ASCIIToUTF16("12345678")); |
| 3411 strings.push_back(ASCIIToUTF16("123456")); |
| 3412 strings.push_back(ASCIIToUTF16("1234567")); |
| 3413 base::StringPiece16 prefix = FormStructure::FindLongestCommonPrefix(strings); |
| 3414 EXPECT_EQ(ASCIIToUTF16("123456"), prefix.as_string()); |
| 3415 |
| 3416 // Handles no common prefix. |
| 3417 strings.clear(); |
| 3418 strings.push_back(ASCIIToUTF16("123")); |
| 3419 strings.push_back(ASCIIToUTF16("456")); |
| 3420 strings.push_back(ASCIIToUTF16("789")); |
| 3421 prefix = FormStructure::FindLongestCommonPrefix(strings); |
| 3422 EXPECT_EQ(ASCIIToUTF16(""), prefix.as_string()); |
| 3423 |
| 3424 // Empty strings in the mix. |
| 3425 strings.clear(); |
| 3426 strings.push_back(ASCIIToUTF16("123456789")); |
| 3427 strings.push_back(ASCIIToUTF16("")); |
| 3428 strings.push_back(ASCIIToUTF16("12345678")); |
| 3429 prefix = FormStructure::FindLongestCommonPrefix(strings); |
| 3430 EXPECT_EQ(ASCIIToUTF16(""), prefix.as_string()); |
| 3431 |
| 3432 // Only one string. |
| 3433 strings.clear(); |
| 3434 strings.push_back(ASCIIToUTF16("123456789")); |
| 3435 prefix = FormStructure::FindLongestCommonPrefix(strings); |
| 3436 EXPECT_EQ(ASCIIToUTF16("123456789"), prefix.as_string()); |
| 3437 |
| 3438 // Empty vector. |
| 3439 strings.clear(); |
| 3440 prefix = FormStructure::FindLongestCommonPrefix(strings); |
| 3441 EXPECT_EQ(ASCIIToUTF16(""), prefix.as_string()); |
| 3442 } |
| 3443 |
3322 } // namespace autofill | 3444 } // namespace autofill |
OLD | NEW |