| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 using testing::_; | 31 using testing::_; |
| 32 | 32 |
| 33 namespace autofill { | 33 namespace autofill { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 class MockWebDataService : public AutofillWebDataService { | 37 class MockWebDataService : public AutofillWebDataService { |
| 38 public: | 38 public: |
| 39 MockWebDataService() | 39 MockWebDataService() |
| 40 : AutofillWebDataService() { | 40 : AutofillWebDataService() { |
| 41 current_mock_web_data_service_ = this; | |
| 42 } | 41 } |
| 43 | 42 |
| 44 MOCK_METHOD1(AddFormFields, void(const std::vector<FormFieldData>&)); | 43 MOCK_METHOD1(AddFormFields, void(const std::vector<FormFieldData>&)); |
| 45 | 44 |
| 46 static scoped_refptr<MockWebDataService> GetCurrent() { | |
| 47 if (!current_mock_web_data_service_) { | |
| 48 return new MockWebDataService(); | |
| 49 } | |
| 50 return current_mock_web_data_service_; | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 virtual ~MockWebDataService() {} | 45 virtual ~MockWebDataService() {} |
| 55 | |
| 56 private: | |
| 57 // Keep track of the most recently created instance, so that it can be | |
| 58 // associated with the current profile when Build() is called. | |
| 59 static MockWebDataService* current_mock_web_data_service_; | |
| 60 }; | |
| 61 | |
| 62 MockWebDataService* MockWebDataService::current_mock_web_data_service_ = NULL; | |
| 63 | |
| 64 class MockWebDataServiceWrapperCurrent : public MockWebDataServiceWrapperBase { | |
| 65 public: | |
| 66 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | |
| 67 return new MockWebDataServiceWrapperCurrent(); | |
| 68 } | |
| 69 | |
| 70 MockWebDataServiceWrapperCurrent() {} | |
| 71 | |
| 72 virtual scoped_refptr<AutofillWebDataService> GetAutofillWebData() OVERRIDE { | |
| 73 return MockWebDataService::GetCurrent(); | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 DISALLOW_COPY_AND_ASSIGN(MockWebDataServiceWrapperCurrent); | |
| 78 }; | 46 }; |
| 79 | 47 |
| 80 class MockAutofillManagerDelegate | 48 class MockAutofillManagerDelegate |
| 81 : public autofill::TestAutofillManagerDelegate { | 49 : public autofill::TestAutofillManagerDelegate { |
| 82 public: | 50 public: |
| 83 MockAutofillManagerDelegate() {} | 51 MockAutofillManagerDelegate() {} |
| 84 virtual ~MockAutofillManagerDelegate() {} | 52 virtual ~MockAutofillManagerDelegate() {} |
| 85 virtual PrefService* GetPrefs() OVERRIDE { return &prefs_; } | 53 virtual PrefService* GetPrefs() OVERRIDE { return &prefs_; } |
| 86 | 54 |
| 87 private: | 55 private: |
| 88 TestingPrefServiceSimple prefs_; | 56 TestingPrefServiceSimple prefs_; |
| 89 | 57 |
| 90 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); | 58 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); |
| 91 }; | 59 }; |
| 92 | 60 |
| 61 BrowserContextKeyedService* BuildMockWebDataServiceWrapper( |
| 62 content::BrowserContext* profile) { |
| 63 return new MockWebDataServiceWrapper( |
| 64 NULL, |
| 65 new MockWebDataService(), |
| 66 NULL); |
| 67 } |
| 68 |
| 93 } // namespace | 69 } // namespace |
| 94 | 70 |
| 95 class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness { | 71 class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness { |
| 96 protected: | 72 protected: |
| 97 virtual void SetUp() OVERRIDE { | 73 virtual void SetUp() OVERRIDE { |
| 98 ChromeRenderViewHostTestHarness::SetUp(); | 74 ChromeRenderViewHostTestHarness::SetUp(); |
| 99 web_data_service_ = new MockWebDataService(); | 75 MockWebDataServiceWrapper* wrapper = |
| 100 WebDataServiceFactory::GetInstance()->SetTestingFactory( | 76 static_cast<MockWebDataServiceWrapper*>( |
| 101 profile(), MockWebDataServiceWrapperCurrent::Build); | 77 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 78 profile(), BuildMockWebDataServiceWrapper)); |
| 79 web_data_service_ = |
| 80 static_cast<MockWebDataService*>(wrapper->GetAutofillWebData()); |
| 102 autocomplete_manager_.reset(new AutocompleteHistoryManager(web_contents())); | 81 autocomplete_manager_.reset(new AutocompleteHistoryManager(web_contents())); |
| 103 } | 82 } |
| 104 | 83 |
| 105 virtual void TearDown() OVERRIDE { | 84 virtual void TearDown() OVERRIDE { |
| 106 autocomplete_manager_.reset(); | 85 autocomplete_manager_.reset(); |
| 86 web_data_service_->ShutdownOnUIThread(); |
| 107 web_data_service_ = NULL; | 87 web_data_service_ = NULL; |
| 108 ChromeRenderViewHostTestHarness::TearDown(); | 88 ChromeRenderViewHostTestHarness::TearDown(); |
| 109 } | 89 } |
| 110 | 90 |
| 111 scoped_refptr<MockWebDataService> web_data_service_; | 91 MockWebDataService* web_data_service_; |
| 112 scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_; | 92 scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_; |
| 113 MockAutofillManagerDelegate manager_delegate; | 93 MockAutofillManagerDelegate manager_delegate; |
| 114 }; | 94 }; |
| 115 | 95 |
| 116 // Tests that credit card numbers are not sent to the WebDatabase to be saved. | 96 // Tests that credit card numbers are not sent to the WebDatabase to be saved. |
| 117 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) { | 97 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) { |
| 118 FormData form; | 98 FormData form; |
| 119 form.name = ASCIIToUTF16("MyForm"); | 99 form.name = ASCIIToUTF16("MyForm"); |
| 120 form.method = ASCIIToUTF16("POST"); | 100 form.method = ASCIIToUTF16("POST"); |
| 121 form.origin = GURL("http://myform.com/form.html"); | 101 form.origin = GURL("http://myform.com/form.html"); |
| 122 form.action = GURL("http://myform.com/submit.html"); | 102 form.action = GURL("http://myform.com/submit.html"); |
| 123 form.user_submitted = true; | 103 form.user_submitted = true; |
| 124 | 104 |
| 125 // Valid Visa credit card number pulled from the paypal help site. | 105 // Valid Visa credit card number pulled from the paypal help site. |
| 126 FormFieldData valid_cc; | 106 FormFieldData valid_cc; |
| 127 valid_cc.label = ASCIIToUTF16("Credit Card"); | 107 valid_cc.label = ASCIIToUTF16("Credit Card"); |
| 128 valid_cc.name = ASCIIToUTF16("ccnum"); | 108 valid_cc.name = ASCIIToUTF16("ccnum"); |
| 129 valid_cc.value = ASCIIToUTF16("4012888888881881"); | 109 valid_cc.value = ASCIIToUTF16("4012888888881881"); |
| 130 valid_cc.form_control_type = "text"; | 110 valid_cc.form_control_type = "text"; |
| 131 form.fields.push_back(valid_cc); | 111 form.fields.push_back(valid_cc); |
| 132 | 112 |
| 133 EXPECT_CALL(*web_data_service_.get(), AddFormFields(_)).Times(0); | 113 EXPECT_CALL(*web_data_service_, AddFormFields(_)).Times(0); |
| 134 autocomplete_manager_->OnFormSubmitted(form); | 114 autocomplete_manager_->OnFormSubmitted(form); |
| 135 } | 115 } |
| 136 | 116 |
| 137 // Contrary test to AutocompleteHistoryManagerTest.CreditCardNumberValue. The | 117 // Contrary test to AutocompleteHistoryManagerTest.CreditCardNumberValue. The |
| 138 // value being submitted is not a valid credit card number, so it will be sent | 118 // value being submitted is not a valid credit card number, so it will be sent |
| 139 // to the WebDatabase to be saved. | 119 // to the WebDatabase to be saved. |
| 140 TEST_F(AutocompleteHistoryManagerTest, NonCreditCardNumberValue) { | 120 TEST_F(AutocompleteHistoryManagerTest, NonCreditCardNumberValue) { |
| 141 FormData form; | 121 FormData form; |
| 142 form.name = ASCIIToUTF16("MyForm"); | 122 form.name = ASCIIToUTF16("MyForm"); |
| 143 form.method = ASCIIToUTF16("POST"); | 123 form.method = ASCIIToUTF16("POST"); |
| 144 form.origin = GURL("http://myform.com/form.html"); | 124 form.origin = GURL("http://myform.com/form.html"); |
| 145 form.action = GURL("http://myform.com/submit.html"); | 125 form.action = GURL("http://myform.com/submit.html"); |
| 146 form.user_submitted = true; | 126 form.user_submitted = true; |
| 147 | 127 |
| 148 // Invalid credit card number. | 128 // Invalid credit card number. |
| 149 FormFieldData invalid_cc; | 129 FormFieldData invalid_cc; |
| 150 invalid_cc.label = ASCIIToUTF16("Credit Card"); | 130 invalid_cc.label = ASCIIToUTF16("Credit Card"); |
| 151 invalid_cc.name = ASCIIToUTF16("ccnum"); | 131 invalid_cc.name = ASCIIToUTF16("ccnum"); |
| 152 invalid_cc.value = ASCIIToUTF16("4580123456789012"); | 132 invalid_cc.value = ASCIIToUTF16("4580123456789012"); |
| 153 invalid_cc.form_control_type = "text"; | 133 invalid_cc.form_control_type = "text"; |
| 154 form.fields.push_back(invalid_cc); | 134 form.fields.push_back(invalid_cc); |
| 155 | 135 |
| 156 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); | 136 EXPECT_CALL(*(web_data_service_), AddFormFields(_)).Times(1); |
| 157 autocomplete_manager_->OnFormSubmitted(form); | 137 autocomplete_manager_->OnFormSubmitted(form); |
| 158 } | 138 } |
| 159 | 139 |
| 160 // Tests that SSNs are not sent to the WebDatabase to be saved. | 140 // Tests that SSNs are not sent to the WebDatabase to be saved. |
| 161 TEST_F(AutocompleteHistoryManagerTest, SSNValue) { | 141 TEST_F(AutocompleteHistoryManagerTest, SSNValue) { |
| 162 FormData form; | 142 FormData form; |
| 163 form.name = ASCIIToUTF16("MyForm"); | 143 form.name = ASCIIToUTF16("MyForm"); |
| 164 form.method = ASCIIToUTF16("POST"); | 144 form.method = ASCIIToUTF16("POST"); |
| 165 form.origin = GURL("http://myform.com/form.html"); | 145 form.origin = GURL("http://myform.com/form.html"); |
| 166 form.action = GURL("http://myform.com/submit.html"); | 146 form.action = GURL("http://myform.com/submit.html"); |
| 167 form.user_submitted = true; | 147 form.user_submitted = true; |
| 168 | 148 |
| 169 FormFieldData ssn; | 149 FormFieldData ssn; |
| 170 ssn.label = ASCIIToUTF16("Social Security Number"); | 150 ssn.label = ASCIIToUTF16("Social Security Number"); |
| 171 ssn.name = ASCIIToUTF16("ssn"); | 151 ssn.name = ASCIIToUTF16("ssn"); |
| 172 ssn.value = ASCIIToUTF16("078-05-1120"); | 152 ssn.value = ASCIIToUTF16("078-05-1120"); |
| 173 ssn.form_control_type = "text"; | 153 ssn.form_control_type = "text"; |
| 174 form.fields.push_back(ssn); | 154 form.fields.push_back(ssn); |
| 175 | 155 |
| 176 EXPECT_CALL(*web_data_service_.get(), AddFormFields(_)).Times(0); | 156 EXPECT_CALL(*web_data_service_, AddFormFields(_)).Times(0); |
| 177 autocomplete_manager_->OnFormSubmitted(form); | 157 autocomplete_manager_->OnFormSubmitted(form); |
| 178 } | 158 } |
| 179 | 159 |
| 180 // Verify that autocomplete text is saved for search fields. | 160 // Verify that autocomplete text is saved for search fields. |
| 181 TEST_F(AutocompleteHistoryManagerTest, SearchField) { | 161 TEST_F(AutocompleteHistoryManagerTest, SearchField) { |
| 182 FormData form; | 162 FormData form; |
| 183 form.name = ASCIIToUTF16("MyForm"); | 163 form.name = ASCIIToUTF16("MyForm"); |
| 184 form.method = ASCIIToUTF16("POST"); | 164 form.method = ASCIIToUTF16("POST"); |
| 185 form.origin = GURL("http://myform.com/form.html"); | 165 form.origin = GURL("http://myform.com/form.html"); |
| 186 form.action = GURL("http://myform.com/submit.html"); | 166 form.action = GURL("http://myform.com/submit.html"); |
| 187 form.user_submitted = true; | 167 form.user_submitted = true; |
| 188 | 168 |
| 189 // Search field. | 169 // Search field. |
| 190 FormFieldData search_field; | 170 FormFieldData search_field; |
| 191 search_field.label = ASCIIToUTF16("Search"); | 171 search_field.label = ASCIIToUTF16("Search"); |
| 192 search_field.name = ASCIIToUTF16("search"); | 172 search_field.name = ASCIIToUTF16("search"); |
| 193 search_field.value = ASCIIToUTF16("my favorite query"); | 173 search_field.value = ASCIIToUTF16("my favorite query"); |
| 194 search_field.form_control_type = "search"; | 174 search_field.form_control_type = "search"; |
| 195 form.fields.push_back(search_field); | 175 form.fields.push_back(search_field); |
| 196 | 176 |
| 197 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); | 177 EXPECT_CALL(*(web_data_service_), AddFormFields(_)).Times(1); |
| 198 autocomplete_manager_->OnFormSubmitted(form); | 178 autocomplete_manager_->OnFormSubmitted(form); |
| 199 } | 179 } |
| 200 | 180 |
| 201 namespace { | 181 namespace { |
| 202 | 182 |
| 203 class MockAutofillExternalDelegate : public AutofillExternalDelegate { | 183 class MockAutofillExternalDelegate : public AutofillExternalDelegate { |
| 204 public: | 184 public: |
| 205 explicit MockAutofillExternalDelegate(content::WebContents* web_contents) | 185 explicit MockAutofillExternalDelegate(content::WebContents* web_contents) |
| 206 : AutofillExternalDelegate( | 186 : AutofillExternalDelegate( |
| 207 web_contents, AutofillManager::FromWebContents(web_contents)) {} | 187 web_contents, AutofillManager::FromWebContents(web_contents)) {} |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 231 |
| 252 MockAutofillExternalDelegate external_delegate(web_contents()); | 232 MockAutofillExternalDelegate external_delegate(web_contents()); |
| 253 autocomplete_history_manager.SetExternalDelegate(&external_delegate); | 233 autocomplete_history_manager.SetExternalDelegate(&external_delegate); |
| 254 | 234 |
| 255 // Should trigger a call to OnSuggestionsReturned, verified by the mock. | 235 // Should trigger a call to OnSuggestionsReturned, verified by the mock. |
| 256 EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _)); | 236 EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _)); |
| 257 autocomplete_history_manager.SendSuggestions(NULL); | 237 autocomplete_history_manager.SendSuggestions(NULL); |
| 258 } | 238 } |
| 259 | 239 |
| 260 } // namespace autofill | 240 } // namespace autofill |
| OLD | NEW |