| 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 <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 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 const base::string16& prefix, | 436 const base::string16& prefix, |
| 437 const std::string& form_control_type)); | 437 const std::string& form_control_type)); |
| 438 MOCK_METHOD1(OnWillSubmitForm, void(const FormData& form)); | 438 MOCK_METHOD1(OnWillSubmitForm, void(const FormData& form)); |
| 439 | 439 |
| 440 private: | 440 private: |
| 441 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager); | 441 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager); |
| 442 }; | 442 }; |
| 443 | 443 |
| 444 class MockAutofillDriver : public TestAutofillDriver { | 444 class MockAutofillDriver : public TestAutofillDriver { |
| 445 public: | 445 public: |
| 446 MockAutofillDriver() {} | 446 MockAutofillDriver() : is_off_the_record_(false) {} |
| 447 | 447 |
| 448 // Mock methods to enable testability. | 448 // Mock methods to enable testability. |
| 449 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id, | 449 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id, |
| 450 RendererFormDataAction action, | 450 RendererFormDataAction action, |
| 451 const FormData& data)); | 451 const FormData& data)); |
| 452 | 452 |
| 453 void SetIsOffTheRecord(bool is_off_the_record) { |
| 454 is_off_the_record_ = is_off_the_record; |
| 455 } |
| 456 |
| 457 bool IsOffTheRecord() const override { return is_off_the_record_; } |
| 458 |
| 453 private: | 459 private: |
| 460 bool is_off_the_record_; |
| 454 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver); | 461 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver); |
| 455 }; | 462 }; |
| 456 | 463 |
| 457 class TestAutofillManager : public AutofillManager { | 464 class TestAutofillManager : public AutofillManager { |
| 458 public: | 465 public: |
| 459 TestAutofillManager(AutofillDriver* driver, | 466 TestAutofillManager(AutofillDriver* driver, |
| 460 AutofillClient* client, | 467 AutofillClient* client, |
| 461 TestPersonalDataManager* personal_data) | 468 TestPersonalDataManager* personal_data) |
| 462 : AutofillManager(driver, client, personal_data), | 469 : AutofillManager(driver, client, personal_data), |
| 463 personal_data_(personal_data), | 470 personal_data_(personal_data), |
| (...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 985 form2.fields.push_back(field); | 992 form2.fields.push_back(field); |
| 986 | 993 |
| 987 forms.clear(); | 994 forms.clear(); |
| 988 forms.push_back(form2); | 995 forms.push_back(form2); |
| 989 FormsSeen(forms); | 996 FormsSeen(forms); |
| 990 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", | 997 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", |
| 991 0 /* FORMS_LOADED */, 2); | 998 0 /* FORMS_LOADED */, 2); |
| 992 download_manager_->VerifyLastQueriedForms(forms); | 999 download_manager_->VerifyLastQueriedForms(forms); |
| 993 } | 1000 } |
| 994 | 1001 |
| 1002 // Test that no suggestions are returned when there are less than three fields |
| 1003 // and none of them have an autocomplete attribute. |
| 1004 TEST_F(AutofillManagerTest, GetProfileSuggestions_SmallFormNoAutocomplete) { |
| 1005 FormData form; |
| 1006 form.name = ASCIIToUTF16("MyForm"); |
| 1007 form.origin = GURL("https://myform.com/form.html"); |
| 1008 form.action = GURL("https://myform.com/submit.html"); |
| 1009 FormFieldData field; |
| 1010 test::CreateTestFormField("First Name", "firstname", "", "text", &field); |
| 1011 form.fields.push_back(field); |
| 1012 test::CreateTestFormField("Last Name", "lastname", "", "text", &field); |
| 1013 form.fields.push_back(field); |
| 1014 |
| 1015 std::vector<FormData> forms(1, form); |
| 1016 FormsSeen(forms); |
| 1017 |
| 1018 GetAutofillSuggestions(form, form.fields[0]); |
| 1019 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); |
| 1020 |
| 1021 GetAutofillSuggestions(form, form.fields[1]); |
| 1022 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); |
| 1023 } |
| 1024 |
| 1025 // Test that for form with two fields with one that has an autocomplete |
| 1026 // attribute, suggestions are only made for the one that has the attribute. |
| 1027 TEST_F(AutofillManagerTest, |
| 1028 GetProfileSuggestions_SmallFormWithOneAutocomplete) { |
| 1029 FormData form; |
| 1030 form.name = ASCIIToUTF16("MyForm"); |
| 1031 form.origin = GURL("https://myform.com/form.html"); |
| 1032 form.action = GURL("https://myform.com/submit.html"); |
| 1033 FormFieldData field; |
| 1034 test::CreateTestFormField("First Name", "firstname", "", "text", &field); |
| 1035 field.autocomplete_attribute = "given-name"; |
| 1036 form.fields.push_back(field); |
| 1037 test::CreateTestFormField("Last Name", "lastname", "", "text", &field); |
| 1038 field.autocomplete_attribute = ""; |
| 1039 form.fields.push_back(field); |
| 1040 |
| 1041 std::vector<FormData> forms(1, form); |
| 1042 FormsSeen(forms); |
| 1043 |
| 1044 // Check that suggestions are made for the field that has the autocomplete |
| 1045 // attribute. |
| 1046 GetAutofillSuggestions(form, form.fields[0]); |
| 1047 external_delegate_->CheckSuggestions(kDefaultPageID, |
| 1048 Suggestion("Elvis", "", "", 1), |
| 1049 Suggestion("Charles", "", "", 2)); |
| 1050 |
| 1051 // Check that there are no suggestions for the field without the autocomplete |
| 1052 // attribute. |
| 1053 GetAutofillSuggestions(form, form.fields[1]); |
| 1054 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); |
| 1055 } |
| 1056 |
| 1057 // Test that for a form with two fields with autocomplete attributes, |
| 1058 // suggestions are made for both fields. |
| 1059 TEST_F(AutofillManagerTest, |
| 1060 GetProfileSuggestions_SmallFormWithTwoAutocomplete) { |
| 1061 FormData form; |
| 1062 form.name = ASCIIToUTF16("MyForm"); |
| 1063 form.origin = GURL("https://myform.com/form.html"); |
| 1064 form.action = GURL("https://myform.com/submit.html"); |
| 1065 FormFieldData field; |
| 1066 test::CreateTestFormField("First Name", "firstname", "", "text", &field); |
| 1067 field.autocomplete_attribute = "given-name"; |
| 1068 form.fields.push_back(field); |
| 1069 test::CreateTestFormField("Last Name", "lastname", "", "text", &field); |
| 1070 field.autocomplete_attribute = "family-name"; |
| 1071 form.fields.push_back(field); |
| 1072 |
| 1073 std::vector<FormData> forms(1, form); |
| 1074 FormsSeen(forms); |
| 1075 |
| 1076 GetAutofillSuggestions(form, form.fields[0]); |
| 1077 external_delegate_->CheckSuggestions( |
| 1078 kDefaultPageID, Suggestion("Elvis", "Elvis Aaron Presley", "", 1), |
| 1079 Suggestion("Charles", "Charles Hardin Holley", "", 2)); |
| 1080 |
| 1081 GetAutofillSuggestions(form, form.fields[1]); |
| 1082 external_delegate_->CheckSuggestions( |
| 1083 kDefaultPageID, Suggestion("Presley", "Elvis Aaron Presley", "", 1), |
| 1084 Suggestion("Holley", "Charles Hardin Holley", "", 2)); |
| 1085 } |
| 1086 |
| 995 // Test that we return all address profile suggestions when all form fields are | 1087 // Test that we return all address profile suggestions when all form fields are |
| 996 // empty. | 1088 // empty. |
| 997 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { | 1089 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { |
| 998 // Set up our form data. | 1090 // Set up our form data. |
| 999 FormData form; | 1091 FormData form; |
| 1000 test::CreateTestAddressFormData(&form); | 1092 test::CreateTestAddressFormData(&form); |
| 1001 std::vector<FormData> forms(1, form); | 1093 std::vector<FormData> forms(1, form); |
| 1002 FormsSeen(forms); | 1094 FormsSeen(forms); |
| 1003 | 1095 |
| 1004 const FormFieldData& field = form.fields[0]; | 1096 const FormFieldData& field = form.fields[0]; |
| (...skipping 2688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3693 GetAutofillSuggestions(form, field); | 3785 GetAutofillSuggestions(form, field); |
| 3694 | 3786 |
| 3695 external_delegate_->CheckSuggestions( | 3787 external_delegate_->CheckSuggestions( |
| 3696 kDefaultPageID, | 3788 kDefaultPageID, |
| 3697 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", | 3789 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", |
| 3698 1), | 3790 1), |
| 3699 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", | 3791 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", |
| 3700 2)); | 3792 2)); |
| 3701 } | 3793 } |
| 3702 | 3794 |
| 3795 TEST_F(AutofillManagerTest, ShouldUploadForm) { |
| 3796 FormData form; |
| 3797 form.name = ASCIIToUTF16("TestForm"); |
| 3798 form.origin = GURL("http://example.com/form.html"); |
| 3799 form.action = GURL("http://example.com/submit.html"); |
| 3800 |
| 3801 FormFieldData field; |
| 3802 test::CreateTestFormField("Name", "name", "", "text", &field); |
| 3803 form.fields.push_back(field); |
| 3804 test::CreateTestFormField("Email", "email", "", "text", &field); |
| 3805 form.fields.push_back(field); |
| 3806 |
| 3807 FormStructure form_structure(form); |
| 3808 |
| 3809 // Has less than 3 fields. |
| 3810 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure)); |
| 3811 |
| 3812 // Has less than 3 fields but has autocomplete attribute. |
| 3813 form.fields[0].autocomplete_attribute = "given-name"; |
| 3814 FormStructure form_structure_2(form); |
| 3815 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_2)); |
| 3816 |
| 3817 // Has more than 3 fields, no autocomplete attribute. |
| 3818 form.fields[0].autocomplete_attribute = ""; |
| 3819 test::CreateTestFormField("Country", "country", "", "text", &field); |
| 3820 form.fields.push_back(field); |
| 3821 FormStructure form_structure_3(form); |
| 3822 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_3)); |
| 3823 |
| 3824 // Has more than 3 fields and at least one autocomplete attribute. |
| 3825 form.fields[0].autocomplete_attribute = "given-name"; |
| 3826 FormStructure form_structure_4(form); |
| 3827 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_4)); |
| 3828 |
| 3829 // Is off the record. |
| 3830 autofill_driver_->SetIsOffTheRecord(true); |
| 3831 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_4)); |
| 3832 |
| 3833 // Make sure it's reset for the next test case. |
| 3834 autofill_driver_->SetIsOffTheRecord(false); |
| 3835 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_4)); |
| 3836 |
| 3837 // Autofill disabled. |
| 3838 autofill_manager_->set_autofill_enabled(false); |
| 3839 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_3)); |
| 3840 } |
| 3841 |
| 3703 } // namespace autofill | 3842 } // namespace autofill |
| OLD | NEW |