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

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: 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 28 matching lines...) Expand all
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 FormsSeen(std::vector<FormData>{});
828 histogram_tester.ExpectUniqueSample( 860 histogram_tester.ExpectUniqueSample(
829 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1); 861 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1);
830 } 862 }
831 863
864 // Test that calling OnFormsSeen consecutively with a different set of forms
865 // will query for each separately.
866 TEST_F(AutofillManagerTest, OnFormsSeen_DifferentFormStructures) {
867 // Set up our form data.
868 FormData form;
869 test::CreateTestAddressFormData(&form);
870
871 base::HistogramTester histogram_tester;
872 FormsSeen({form});
873 histogram_tester.ExpectUniqueSample(
874 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 1);
875 download_manager_->VerifyLastQueriedForms({form});
876
877 // Different form structure.
878 FormData form2;
879 form2.name = ASCIIToUTF16("MyForm");
880 form2.origin = GURL("https://myform.com/form.html");
881 form2.action = GURL("https://myform.com/submit.html");
882 FormFieldData field;
883 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
884 form2.fields.push_back(field);
885 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
886 form2.fields.push_back(field);
887 test::CreateTestFormField("Email", "email", "", "text", &field);
888 form2.fields.push_back(field);
889 FormsSeen({form2});
890
891 histogram_tester.ExpectUniqueSample(
892 "Autofill.UserHappiness", 0 /* FORMS_LOADED */, 2);
893 download_manager_->VerifyLastQueriedForms({form2});
894 }
895
832 // Test that we return all address profile suggestions when all form fields are 896 // Test that we return all address profile suggestions when all form fields are
833 // empty. 897 // empty.
834 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { 898 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
835 // Set up our form data. 899 // Set up our form data.
836 FormData form; 900 FormData form;
837 test::CreateTestAddressFormData(&form); 901 test::CreateTestAddressFormData(&form);
838 std::vector<FormData> forms(1, form); 902 std::vector<FormData> forms(1, form);
839 FormsSeen(forms); 903 FormsSeen(forms);
840 904
841 const FormFieldData& field = form.fields[0]; 905 const FormFieldData& field = form.fields[0];
(...skipping 2415 matching lines...) Expand 10 before | Expand all | Expand 10 after
3257 3321
3258 external_delegate_->CheckSuggestions( 3322 external_delegate_->CheckSuggestions(
3259 kDefaultPageID, 3323 kDefaultPageID,
3260 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "", 3324 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "",
3261 1), 3325 1),
3262 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "", 3326 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "",
3263 2)); 3327 2));
3264 } 3328 }
3265 3329
3266 } // namespace autofill 3330 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698