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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
21 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "components/autofill/core/browser/autocomplete_history_manager.h" | 23 #include "components/autofill/core/browser/autocomplete_history_manager.h" |
| 24 #include "components/autofill/core/browser/autofill_download_manager.h" |
24 #include "components/autofill/core/browser/autofill_manager.h" | 25 #include "components/autofill/core/browser/autofill_manager.h" |
25 #include "components/autofill/core/browser/autofill_profile.h" | 26 #include "components/autofill/core/browser/autofill_profile.h" |
26 #include "components/autofill/core/browser/autofill_test_utils.h" | 27 #include "components/autofill/core/browser/autofill_test_utils.h" |
27 #include "components/autofill/core/browser/credit_card.h" | 28 #include "components/autofill/core/browser/credit_card.h" |
28 #include "components/autofill/core/browser/personal_data_manager.h" | 29 #include "components/autofill/core/browser/personal_data_manager.h" |
29 #include "components/autofill/core/browser/popup_item_ids.h" | 30 #include "components/autofill/core/browser/popup_item_ids.h" |
30 #include "components/autofill/core/browser/test_autofill_client.h" | 31 #include "components/autofill/core/browser/test_autofill_client.h" |
31 #include "components/autofill/core/browser/test_autofill_driver.h" | 32 #include "components/autofill/core/browser/test_autofill_driver.h" |
32 #include "components/autofill/core/browser/test_autofill_external_delegate.h" | 33 #include "components/autofill/core/browser/test_autofill_external_delegate.h" |
33 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 34 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 test::SetCreditCardInfo(credit_card, "", "", "", ""); | 202 test::SetCreditCardInfo(credit_card, "", "", "", ""); |
202 credit_card->set_guid("00000000-0000-0000-0000-000000000006"); | 203 credit_card->set_guid("00000000-0000-0000-0000-000000000006"); |
203 credit_cards->push_back(credit_card); | 204 credit_cards->push_back(credit_card); |
204 } | 205 } |
205 | 206 |
206 size_t num_times_save_imported_profile_called_; | 207 size_t num_times_save_imported_profile_called_; |
207 | 208 |
208 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); | 209 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); |
209 }; | 210 }; |
210 | 211 |
| 212 class TestAutofillDownloadManager : public AutofillDownloadManager { |
| 213 public: |
| 214 TestAutofillDownloadManager(AutofillDriver* driver, |
| 215 PrefService* pref_service, |
| 216 AutofillDownloadManager::Observer* observer) |
| 217 : AutofillDownloadManager(driver, pref_service, observer) {} |
| 218 |
| 219 bool StartQueryRequest(const std::vector<FormStructure*>& forms) override { |
| 220 last_queried_forms_ = forms; |
| 221 return true; |
| 222 } |
| 223 |
| 224 // Verify that the last queried forms equal |expected_forms|. |
| 225 void VerifyLastQueriedForms(const std::vector<FormData>& expected_forms) { |
| 226 ASSERT_EQ(expected_forms.size(), last_queried_forms_.size()); |
| 227 for (size_t i = 0; i < expected_forms.size(); ++i) { |
| 228 EXPECT_EQ(*last_queried_forms_[i], expected_forms[i]); |
| 229 } |
| 230 } |
| 231 |
| 232 private: |
| 233 std::vector<FormStructure*> last_queried_forms_; |
| 234 |
| 235 DISALLOW_COPY_AND_ASSIGN(TestAutofillDownloadManager); |
| 236 }; |
| 237 |
211 void ExpectFilledField(const char* expected_label, | 238 void ExpectFilledField(const char* expected_label, |
212 const char* expected_name, | 239 const char* expected_name, |
213 const char* expected_value, | 240 const char* expected_value, |
214 const char* expected_form_control_type, | 241 const char* expected_form_control_type, |
215 const FormFieldData& field) { | 242 const FormFieldData& field) { |
216 SCOPED_TRACE(expected_label); | 243 SCOPED_TRACE(expected_label); |
217 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label); | 244 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label); |
218 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name); | 245 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name); |
219 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value); | 246 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value); |
220 EXPECT_EQ(expected_form_control_type, field.form_control_type); | 247 EXPECT_EQ(expected_form_control_type, field.form_control_type); |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 void SetUp() override { | 624 void SetUp() override { |
598 autofill_client_.SetPrefs(test::PrefServiceForTesting()); | 625 autofill_client_.SetPrefs(test::PrefServiceForTesting()); |
599 personal_data_.set_database(autofill_client_.GetDatabase()); | 626 personal_data_.set_database(autofill_client_.GetDatabase()); |
600 personal_data_.SetPrefService(autofill_client_.GetPrefs()); | 627 personal_data_.SetPrefService(autofill_client_.GetPrefs()); |
601 autofill_driver_.reset(new MockAutofillDriver()); | 628 autofill_driver_.reset(new MockAutofillDriver()); |
602 request_context_ = new net::TestURLRequestContextGetter( | 629 request_context_ = new net::TestURLRequestContextGetter( |
603 base::ThreadTaskRunnerHandle::Get()); | 630 base::ThreadTaskRunnerHandle::Get()); |
604 autofill_driver_->SetURLRequestContext(request_context_.get()); | 631 autofill_driver_->SetURLRequestContext(request_context_.get()); |
605 autofill_manager_.reset(new TestAutofillManager( | 632 autofill_manager_.reset(new TestAutofillManager( |
606 autofill_driver_.get(), &autofill_client_, &personal_data_)); | 633 autofill_driver_.get(), &autofill_client_, &personal_data_)); |
607 | 634 download_manager_ = new TestAutofillDownloadManager( |
| 635 autofill_driver_.get(), autofill_client_.GetPrefs(), |
| 636 autofill_manager_.get()); |
| 637 // AutofillManager takes ownership of |download_manager_|. |
| 638 autofill_manager_->set_download_manager(download_manager_); |
608 external_delegate_.reset(new TestAutofillExternalDelegate( | 639 external_delegate_.reset(new TestAutofillExternalDelegate( |
609 autofill_manager_.get(), | 640 autofill_manager_.get(), |
610 autofill_driver_.get())); | 641 autofill_driver_.get())); |
611 autofill_manager_->SetExternalDelegate(external_delegate_.get()); | 642 autofill_manager_->SetExternalDelegate(external_delegate_.get()); |
612 } | 643 } |
613 | 644 |
614 void TearDown() override { | 645 void TearDown() override { |
615 // Order of destruction is important as AutofillManager relies on | 646 // Order of destruction is important as AutofillManager relies on |
616 // PersonalDataManager to be around when it gets destroyed. | 647 // PersonalDataManager to be around when it gets destroyed. |
617 autofill_manager_.reset(); | 648 autofill_manager_.reset(); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 #endif // defined(OS_IOS) | 805 #endif // defined(OS_IOS) |
775 } | 806 } |
776 | 807 |
777 protected: | 808 protected: |
778 base::MessageLoop message_loop_; | 809 base::MessageLoop message_loop_; |
779 MockAutofillClient autofill_client_; | 810 MockAutofillClient autofill_client_; |
780 scoped_ptr<MockAutofillDriver> autofill_driver_; | 811 scoped_ptr<MockAutofillDriver> autofill_driver_; |
781 scoped_ptr<TestAutofillManager> autofill_manager_; | 812 scoped_ptr<TestAutofillManager> autofill_manager_; |
782 scoped_ptr<TestAutofillExternalDelegate> external_delegate_; | 813 scoped_ptr<TestAutofillExternalDelegate> external_delegate_; |
783 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 814 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 815 TestAutofillDownloadManager* download_manager_; |
784 TestPersonalDataManager personal_data_; | 816 TestPersonalDataManager personal_data_; |
785 }; | 817 }; |
786 | 818 |
787 class TestFormStructure : public FormStructure { | 819 class TestFormStructure : public FormStructure { |
788 public: | 820 public: |
789 explicit TestFormStructure(const FormData& form) | 821 explicit TestFormStructure(const FormData& form) |
790 : FormStructure(form) {} | 822 : FormStructure(form) {} |
791 ~TestFormStructure() override {} | 823 ~TestFormStructure() override {} |
792 | 824 |
793 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types, | 825 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types, |
(...skipping 23 matching lines...) Expand all Loading... |
817 FormData form; | 849 FormData form; |
818 test::CreateTestAddressFormData(&form); | 850 test::CreateTestAddressFormData(&form); |
819 std::vector<FormData> forms(1, form); | 851 std::vector<FormData> forms(1, form); |
820 | 852 |
821 base::HistogramTester histogram_tester; | 853 base::HistogramTester histogram_tester; |
822 FormsSeen(forms); | 854 FormsSeen(forms); |
823 histogram_tester.ExpectUniqueSample( | 855 histogram_tester.ExpectUniqueSample( |
824 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); | 856 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); |
825 | 857 |
826 // No more forms, metric is not logged. | 858 // No more forms, metric is not logged. |
827 FormsSeen(std::vector<FormData>{}); | 859 forms.clear(); |
| 860 FormsSeen(forms); |
828 histogram_tester.ExpectUniqueSample( | 861 histogram_tester.ExpectUniqueSample( |
829 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); | 862 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); |
830 } | 863 } |
831 | 864 |
| 865 // Test that calling OnFormsSeen consecutively with a different set of forms |
| 866 // will query for each separately. |
| 867 TEST_F(AutofillManagerTest, OnFormsSeen_DifferentFormStructures) { |
| 868 // Set up our form data. |
| 869 FormData form; |
| 870 test::CreateTestAddressFormData(&form); |
| 871 std::vector<FormData> forms(1, form); |
| 872 |
| 873 base::HistogramTester histogram_tester; |
| 874 FormsSeen(forms); |
| 875 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", |
| 876 0 /* FORMS_LOADED */, 1); |
| 877 download_manager_->VerifyLastQueriedForms(forms); |
| 878 |
| 879 // Different form structure. |
| 880 FormData form2; |
| 881 form2.name = ASCIIToUTF16("MyForm"); |
| 882 form2.origin = GURL("https://myform.com/form.html"); |
| 883 form2.action = GURL("https://myform.com/submit.html"); |
| 884 FormFieldData field; |
| 885 test::CreateTestFormField("First Name", "firstname", "", "text", &field); |
| 886 form2.fields.push_back(field); |
| 887 test::CreateTestFormField("Last Name", "lastname", "", "text", &field); |
| 888 form2.fields.push_back(field); |
| 889 test::CreateTestFormField("Email", "email", "", "text", &field); |
| 890 form2.fields.push_back(field); |
| 891 |
| 892 forms.clear(); |
| 893 forms.push_back(form2); |
| 894 FormsSeen(forms); |
| 895 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", |
| 896 0 /* FORMS_LOADED */, 2); |
| 897 download_manager_->VerifyLastQueriedForms(forms); |
| 898 } |
| 899 |
832 // Test that we return all address profile suggestions when all form fields are | 900 // Test that we return all address profile suggestions when all form fields are |
833 // empty. | 901 // empty. |
834 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { | 902 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { |
835 // Set up our form data. | 903 // Set up our form data. |
836 FormData form; | 904 FormData form; |
837 test::CreateTestAddressFormData(&form); | 905 test::CreateTestAddressFormData(&form); |
838 std::vector<FormData> forms(1, form); | 906 std::vector<FormData> forms(1, form); |
839 FormsSeen(forms); | 907 FormsSeen(forms); |
840 | 908 |
841 const FormFieldData& field = form.fields[0]; | 909 const FormFieldData& field = form.fields[0]; |
(...skipping 2415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3257 | 3325 |
3258 external_delegate_->CheckSuggestions( | 3326 external_delegate_->CheckSuggestions( |
3259 kDefaultPageID, | 3327 kDefaultPageID, |
3260 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", | 3328 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", |
3261 1), | 3329 1), |
3262 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", | 3330 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", |
3263 2)); | 3331 2)); |
3264 } | 3332 } |
3265 | 3333 |
3266 } // namespace autofill | 3334 } // namespace autofill |
OLD | NEW |