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

Side by Side Diff: components/autofill/core/browser/autofill_manager_unittest.cc

Issue 1411363003: [Autofill] Always show available data when encountering autocomplete attributes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 5 years, 1 month 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
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 const base::string16& prefix, 407 const base::string16& prefix,
408 const std::string& form_control_type)); 408 const std::string& form_control_type));
409 MOCK_METHOD1(OnWillSubmitForm, void(const FormData& form)); 409 MOCK_METHOD1(OnWillSubmitForm, void(const FormData& form));
410 410
411 private: 411 private:
412 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager); 412 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager);
413 }; 413 };
414 414
415 class MockAutofillDriver : public TestAutofillDriver { 415 class MockAutofillDriver : public TestAutofillDriver {
416 public: 416 public:
417 MockAutofillDriver() {} 417 MockAutofillDriver() : is_off_the_record_(false) {}
418 418
419 // Mock methods to enable testability. 419 // Mock methods to enable testability.
420 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id, 420 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id,
421 RendererFormDataAction action, 421 RendererFormDataAction action,
422 const FormData& data)); 422 const FormData& data));
423 423
424 void SetIsOffTheRecord(bool is_off_the_record) {
425 is_off_the_record_ = is_off_the_record;
426 }
427
428 bool IsOffTheRecord() const override { return is_off_the_record_; }
429
424 private: 430 private:
431 bool is_off_the_record_;
425 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver); 432 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
426 }; 433 };
427 434
428 class TestAutofillManager : public AutofillManager { 435 class TestAutofillManager : public AutofillManager {
429 public: 436 public:
430 TestAutofillManager(AutofillDriver* driver, 437 TestAutofillManager(AutofillDriver* driver,
431 AutofillClient* client, 438 AutofillClient* client,
432 TestPersonalDataManager* personal_data) 439 TestPersonalDataManager* personal_data)
433 : AutofillManager(driver, client, personal_data), 440 : AutofillManager(driver, client, personal_data),
434 personal_data_(personal_data), 441 personal_data_(personal_data),
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 form2.fields.push_back(field); 919 form2.fields.push_back(field);
913 920
914 forms.clear(); 921 forms.clear();
915 forms.push_back(form2); 922 forms.push_back(form2);
916 FormsSeen(forms); 923 FormsSeen(forms);
917 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", 924 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
918 0 /* FORMS_LOADED */, 2); 925 0 /* FORMS_LOADED */, 2);
919 download_manager_->VerifyLastQueriedForms(forms); 926 download_manager_->VerifyLastQueriedForms(forms);
920 } 927 }
921 928
929 // Test that no suggestions are returned when there are less than three fields
930 // and none of them have an autocomplete attribute.
931 TEST_F(AutofillManagerTest, GetProfileSuggestions_SmallFormNoAutocomplete) {
932 FormData form;
933 form.name = ASCIIToUTF16("MyForm");
934 form.origin = GURL("https://myform.com/form.html");
935 form.action = GURL("https://myform.com/submit.html");
936 FormFieldData field;
937 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
938 form.fields.push_back(field);
939 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
940 form.fields.push_back(field);
941
942 std::vector<FormData> forms(1, form);
943 FormsSeen(forms);
944
945 GetAutofillSuggestions(form, form.fields[0]);
946 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
947
948 GetAutofillSuggestions(form, form.fields[1]);
949 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
950 }
951
952 // Test that for form with two fields with one that has an autocomplete
953 // attribute, suggestions are only made for the one that has the attribute.
954 TEST_F(AutofillManagerTest,
955 GetProfileSuggestions_SmallFormWithOneAutocomplete) {
956 FormData form;
957 form.name = ASCIIToUTF16("MyForm");
958 form.origin = GURL("https://myform.com/form.html");
959 form.action = GURL("https://myform.com/submit.html");
960 FormFieldData field;
961 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
962 field.autocomplete_attribute = "given-name";
963 form.fields.push_back(field);
964 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
965 field.autocomplete_attribute = "";
966 form.fields.push_back(field);
967
968 std::vector<FormData> forms(1, form);
969 FormsSeen(forms);
970
971 // Check that suggestions are made for the field that has the autocomplete
972 // attribute.
973 GetAutofillSuggestions(form, form.fields[0]);
974 external_delegate_->CheckSuggestions(kDefaultPageID,
975 Suggestion("Elvis", "", "", 1),
976 Suggestion("Charles", "", "", 2));
977
978 // Check that there are no suggestions for the field without the autocomplete
979 // attribute.
980 GetAutofillSuggestions(form, form.fields[1]);
981 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
982 }
983
984 // Test that for a form with two fields with autocomplete attributes,
985 // suggestions are made for both fields.
986 TEST_F(AutofillManagerTest,
987 GetProfileSuggestions_SmallFormWithTwoAutocomplete) {
988 FormData form;
989 form.name = ASCIIToUTF16("MyForm");
990 form.origin = GURL("https://myform.com/form.html");
991 form.action = GURL("https://myform.com/submit.html");
992 FormFieldData field;
993 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
994 field.autocomplete_attribute = "given-name";
995 form.fields.push_back(field);
996 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
997 field.autocomplete_attribute = "family-name";
998 form.fields.push_back(field);
999
1000 std::vector<FormData> forms(1, form);
1001 FormsSeen(forms);
1002
1003 GetAutofillSuggestions(form, form.fields[0]);
1004 external_delegate_->CheckSuggestions(
1005 kDefaultPageID, Suggestion("Elvis", "Elvis Aaron Presley", "", 1),
1006 Suggestion("Charles", "Charles Hardin Holley", "", 2));
1007
1008 GetAutofillSuggestions(form, form.fields[1]);
1009 external_delegate_->CheckSuggestions(
1010 kDefaultPageID, Suggestion("Presley", "Elvis Aaron Presley", "", 1),
1011 Suggestion("Holley", "Charles Hardin Holley", "", 2));
1012 }
1013
922 // Test that we return all address profile suggestions when all form fields are 1014 // Test that we return all address profile suggestions when all form fields are
923 // empty. 1015 // empty.
924 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { 1016 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
925 // Set up our form data. 1017 // Set up our form data.
926 FormData form; 1018 FormData form;
927 test::CreateTestAddressFormData(&form); 1019 test::CreateTestAddressFormData(&form);
928 std::vector<FormData> forms(1, form); 1020 std::vector<FormData> forms(1, form);
929 FormsSeen(forms); 1021 FormsSeen(forms);
930 1022
931 const FormFieldData& field = form.fields[0]; 1023 const FormFieldData& field = form.fields[0];
(...skipping 2479 matching lines...) Expand 10 before | Expand all | Expand 10 after
3411 GetAutofillSuggestions(form, field); 3503 GetAutofillSuggestions(form, field);
3412 3504
3413 external_delegate_->CheckSuggestions( 3505 external_delegate_->CheckSuggestions(
3414 kDefaultPageID, 3506 kDefaultPageID,
3415 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", 3507 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "",
3416 1), 3508 1),
3417 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", 3509 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "",
3418 2)); 3510 2));
3419 } 3511 }
3420 3512
3513 TEST_F(AutofillManagerTest, ShouldUploadForm) {
3514 FormData form;
3515 form.name = ASCIIToUTF16("TestForm");
3516 form.origin = GURL("http://example.com/form.html");
3517 form.action = GURL("http://example.com/submit.html");
3518
3519 FormFieldData field;
3520 test::CreateTestFormField("Name", "name", "", "text", &field);
3521 form.fields.push_back(field);
3522 test::CreateTestFormField("Email", "email", "", "text", &field);
3523 form.fields.push_back(field);
3524
3525 FormStructure form_structure(form);
3526
3527 // Has less than 3 fields.
3528 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure));
3529
3530 // Has less than 3 fields but has autocomplete attribute.
3531 form.fields[0].autocomplete_attribute = "given-name";
3532 FormStructure form_structure_2(form);
3533 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_2));
3534
3535 // Has more than 3 fields, no autocomplete attribute.
3536 form.fields[0].autocomplete_attribute = "";
3537 test::CreateTestFormField("Country", "country", "", "text", &field);
3538 form.fields.push_back(field);
3539 FormStructure form_structure_3(form);
3540 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_3));
3541
3542 // Has more than 3 fields and at least one autocomplete attribute.
3543 form.fields[0].autocomplete_attribute = "given-name";
3544 FormStructure form_structure_4(form);
3545 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_4));
3546
3547 // Is off the record.
3548 autofill_driver_->SetIsOffTheRecord(true);
3549 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_4));
3550
3551 // Make sure it's reset for the next test case.
3552 autofill_driver_->SetIsOffTheRecord(false);
3553 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_4));
3554
3555 // Autofill disabled.
3556 autofill_manager_->set_autofill_enabled(false);
3557 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_3));
3558 }
3559
3421 } // namespace autofill 3560 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698