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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); | 486 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); |
487 | 487 |
488 ASSERT_EQ(3U, form_structure->field_count()); | 488 ASSERT_EQ(3U, form_structure->field_count()); |
489 ASSERT_EQ(0U, form_structure->autofill_count()); | 489 ASSERT_EQ(0U, form_structure->autofill_count()); |
490 | 490 |
491 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); | 491 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); |
492 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); | 492 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); |
493 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | 493 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); |
494 } | 494 } |
495 | 495 |
| 496 // Even with an 'autocomplete' attribute set, ShouldBeCrowdsourced() should |
| 497 // return true if the structure contains a password field, since there are |
| 498 // no local heuristics to depend upon in this case. Fields will still not be |
| 499 // considered autofillable though. |
| 500 TEST(FormStructureTest, PasswordFormShouldBeCrowdsourced) { |
| 501 FormData form; |
| 502 |
| 503 // Start with a regular contact form. |
| 504 FormFieldData field; |
| 505 field.form_control_type = "text"; |
| 506 |
| 507 field.label = ASCIIToUTF16("First Name"); |
| 508 field.name = ASCIIToUTF16("firstname"); |
| 509 form.fields.push_back(field); |
| 510 |
| 511 field.label = ASCIIToUTF16("Last Name"); |
| 512 field.name = ASCIIToUTF16("lastname"); |
| 513 form.fields.push_back(field); |
| 514 |
| 515 field.label = ASCIIToUTF16("Email"); |
| 516 field.name = ASCIIToUTF16("email"); |
| 517 field.autocomplete_attribute = "username"; |
| 518 form.fields.push_back(field); |
| 519 |
| 520 field.label = ASCIIToUTF16("Password"); |
| 521 field.name = ASCIIToUTF16("Password"); |
| 522 field.form_control_type = "password"; |
| 523 form.fields.push_back(field); |
| 524 |
| 525 FormStructure form_structure(form); |
| 526 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); |
| 527 EXPECT_TRUE(form_structure.ShouldBeCrowdsourced()); |
| 528 } |
| 529 |
496 // Verify that we can correctly process sections listed in the |autocomplete| | 530 // Verify that we can correctly process sections listed in the |autocomplete| |
497 // attribute. | 531 // attribute. |
498 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) { | 532 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) { |
499 FormData form; | 533 FormData form; |
500 | 534 |
501 FormFieldData field; | 535 FormFieldData field; |
502 field.form_control_type = "text"; | 536 field.form_control_type = "text"; |
503 | 537 |
504 // Some fields will have no section specified. These fall into the default | 538 // Some fields will have no section specified. These fall into the default |
505 // section. | 539 // section. |
(...skipping 1843 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2349 | 2383 |
2350 // A freeform input (<input>) allows any value (overriding other <select>s). | 2384 // A freeform input (<input>) allows any value (overriding other <select>s). |
2351 FormFieldData freeform_field; | 2385 FormFieldData freeform_field; |
2352 freeform_field.autocomplete_attribute = "billing country"; | 2386 freeform_field.autocomplete_attribute = "billing country"; |
2353 form_data.fields.push_back(freeform_field); | 2387 form_data.fields.push_back(freeform_field); |
2354 FormStructure form_structure2(form_data); | 2388 FormStructure form_structure2(form_data); |
2355 form_structure2.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused); | 2389 form_structure2.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused); |
2356 EXPECT_EQ(0U, form_structure2.PossibleValues(ADDRESS_BILLING_COUNTRY).size()); | 2390 EXPECT_EQ(0U, form_structure2.PossibleValues(ADDRESS_BILLING_COUNTRY).size()); |
2357 } | 2391 } |
2358 | 2392 |
| 2393 TEST(FormStructureTest, ParseQueryResponse) { |
| 2394 FormData form; |
| 2395 FormFieldData field; |
| 2396 field.form_control_type = "text"; |
| 2397 |
| 2398 field.label = ASCIIToUTF16("fullname"); |
| 2399 field.name = ASCIIToUTF16("fullname"); |
| 2400 form.fields.push_back(field); |
| 2401 |
| 2402 field.label = ASCIIToUTF16("address"); |
| 2403 field.name = ASCIIToUTF16("address"); |
| 2404 form.fields.push_back(field); |
| 2405 |
| 2406 // Checkable fields should be ignored in parsing |
| 2407 FormFieldData checkable_field; |
| 2408 checkable_field.label = ASCIIToUTF16("radio_button"); |
| 2409 checkable_field.form_control_type = "radio"; |
| 2410 checkable_field.is_checkable = true; |
| 2411 form.fields.push_back(checkable_field); |
| 2412 |
| 2413 ScopedVector<FormStructure> forms; |
| 2414 forms.push_back(new FormStructure(form)); |
| 2415 |
| 2416 field.label = ASCIIToUTF16("email"); |
| 2417 field.name = ASCIIToUTF16("email"); |
| 2418 form.fields.push_back(field); |
| 2419 |
| 2420 field.label = ASCIIToUTF16("password"); |
| 2421 field.name = ASCIIToUTF16("password"); |
| 2422 field.form_control_type = "password"; |
| 2423 form.fields.push_back(field); |
| 2424 |
| 2425 forms.push_back(new FormStructure(form)); |
| 2426 |
| 2427 std::string response = |
| 2428 "<autofillqueryresponse>" |
| 2429 "<field autofilltype=\"7\" />" |
| 2430 "<field autofilltype=\"30\" />" |
| 2431 "<field autofilltype=\"9\" />" |
| 2432 "<field autofilltype=\"0\" />" |
| 2433 "</autofillqueryresponse>"; |
| 2434 |
| 2435 FormStructure::ParseQueryResponse(response, |
| 2436 forms.get(), |
| 2437 TestAutofillMetrics()); |
| 2438 |
| 2439 EXPECT_EQ(7, forms[0]->field(0)->server_type()); |
| 2440 EXPECT_EQ(30, forms[0]->field(1)->server_type()); |
| 2441 EXPECT_EQ(9, forms[1]->field(0)->server_type()); |
| 2442 EXPECT_EQ(0, forms[1]->field(1)->server_type()); |
| 2443 } |
| 2444 |
| 2445 // If user defined types are present, only parse password fields. |
| 2446 TEST(FormStructureTest, ParseQueryResponseAuthorDefinedTypes) { |
| 2447 FormData form; |
| 2448 FormFieldData field; |
| 2449 |
| 2450 field.label = ASCIIToUTF16("email"); |
| 2451 field.name = ASCIIToUTF16("email"); |
| 2452 field.form_control_type = "text"; |
| 2453 field.autocomplete_attribute = "email"; |
| 2454 form.fields.push_back(field); |
| 2455 |
| 2456 field.label = ASCIIToUTF16("password"); |
| 2457 field.name = ASCIIToUTF16("password"); |
| 2458 field.form_control_type = "password"; |
| 2459 field.autocomplete_attribute = "new-password"; |
| 2460 form.fields.push_back(field); |
| 2461 |
| 2462 ScopedVector<FormStructure> forms; |
| 2463 forms.push_back(new FormStructure(form)); |
| 2464 forms.front()->DetermineHeuristicTypes(TestAutofillMetrics()); |
| 2465 |
| 2466 std::string response = |
| 2467 "<autofillqueryresponse>" |
| 2468 "<field autofilltype=\"9\" />" |
| 2469 "<field autofilltype=\"76\" />" |
| 2470 "</autofillqueryresponse>"; |
| 2471 |
| 2472 FormStructure::ParseQueryResponse(response, |
| 2473 forms.get(), |
| 2474 TestAutofillMetrics()); |
| 2475 |
| 2476 EXPECT_EQ(NO_SERVER_DATA, forms[0]->field(0)->server_type()); |
| 2477 EXPECT_EQ(76, forms[0]->field(1)->server_type()); |
| 2478 } |
| 2479 |
2359 } // namespace autofill | 2480 } // namespace autofill |
OLD | NEW |