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

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

Issue 1377923002: [Autofill] Only query for newly parsed forms (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean Created 5 years, 2 months 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"
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
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
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
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 15 matching lines...) Expand all
809 DISALLOW_COPY_AND_ASSIGN(TestFormStructure); 841 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
810 }; 842 };
811 843
812 // Test that calling OnFormsSeen with an empty set of forms (such as when 844 // Test that calling OnFormsSeen with an empty set of forms (such as when
813 // reloading a page or when the renderer processes a set of forms but detects 845 // reloading a page or when the renderer processes a set of forms but detects
814 // no changes) does not load the forms again. 846 // no changes) does not load the forms again.
815 TEST_F(AutofillManagerTest, OnFormsSeen_Empty) { 847 TEST_F(AutofillManagerTest, OnFormsSeen_Empty) {
816 // Set up our form data. 848 // Set up our form data.
817 FormData form; 849 FormData form;
818 test::CreateTestAddressFormData(&form); 850 test::CreateTestAddressFormData(&form);
819 std::vector<FormData> forms(1, form);
820 851
821 base::HistogramTester histogram_tester; 852 base::HistogramTester histogram_tester;
822 FormsSeen(forms); 853 FormsSeen({form});
823 histogram_tester.ExpectUniqueSample( 854 histogram_tester.ExpectUniqueSample(
824 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); 855 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1);
825 856
826 // No more forms, metric is not logged. 857 // No more forms, metric is not logged.
827 FormsSeen(std::vector<FormData>{}); 858 FormsSeen(std::vector<FormData>{});
828 histogram_tester.ExpectUniqueSample( 859 histogram_tester.ExpectUniqueSample(
829 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); 860 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1);
830 } 861 }
831 862
863 // Test that calling OnFormsSeen consecutively with a different set of forms
864 // will query for each separately.
865 TEST_F(AutofillManagerTest, OnFormsSeen_DifferentFormStructures) {
866 // Set up our form data.
867 FormData form;
868 test::CreateTestAddressFormData(&form);
869
870 base::HistogramTester histogram_tester;
871 FormsSeen({form});
872 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
873 0 /* FORMS_LOADED */, 1);
874 download_manager_->VerifyLastQueriedForms({form});
875
876 // Different form structure.
877 FormData form2;
878 form2.name = ASCIIToUTF16("MyForm");
879 form2.origin = GURL("https://myform.com/form.html");
880 form2.action = GURL("https://myform.com/submit.html");
881 FormFieldData field;
882 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
883 form2.fields.push_back(field);
884 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
885 form2.fields.push_back(field);
886 test::CreateTestFormField("Email", "email", "", "text", &field);
887 form2.fields.push_back(field);
888 FormsSeen({form2});
889
890 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
891 0 /* FORMS_LOADED */, 2);
892 download_manager_->VerifyLastQueriedForms({form2});
893 }
894
832 // Test that we return all address profile suggestions when all form fields are 895 // Test that we return all address profile suggestions when all form fields are
833 // empty. 896 // empty.
834 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { 897 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
835 // Set up our form data. 898 // Set up our form data.
836 FormData form; 899 FormData form;
837 test::CreateTestAddressFormData(&form); 900 test::CreateTestAddressFormData(&form);
838 std::vector<FormData> forms(1, form); 901 std::vector<FormData> forms(1, form);
839 FormsSeen(forms); 902 FormsSeen(forms);
840 903
841 const FormFieldData& field = form.fields[0]; 904 const FormFieldData& field = form.fields[0];
(...skipping 2415 matching lines...) Expand 10 before | Expand all | Expand 10 after
3257 3320
3258 external_delegate_->CheckSuggestions( 3321 external_delegate_->CheckSuggestions(
3259 kDefaultPageID, 3322 kDefaultPageID,
3260 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", 3323 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "",
3261 1), 3324 1),
3262 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", 3325 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "",
3263 2)); 3326 2));
3264 } 3327 }
3265 3328
3266 } // namespace autofill 3329 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698