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

Side by Side Diff: components/autofill/browser/autocomplete_history_manager_unittest.cc

Issue 16286020: Abstract WebContentsObserver from Autofill shared code (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Response to review of PS #6 Created 7 years, 6 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 (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"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "chrome/browser/webdata/web_data_service_factory.h" 13 #include "chrome/browser/webdata/web_data_service_factory.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_browser_process.h" 15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "components/autofill/browser/autocomplete_history_manager.h" 17 #include "components/autofill/browser/autocomplete_history_manager.h"
18 #include "components/autofill/browser/autofill_external_delegate.h" 18 #include "components/autofill/browser/autofill_external_delegate.h"
19 #include "components/autofill/browser/autofill_manager.h" 19 #include "components/autofill/browser/autofill_manager.h"
20 #include "components/autofill/browser/test_autofill_driver.h"
20 #include "components/autofill/browser/test_autofill_manager_delegate.h" 21 #include "components/autofill/browser/test_autofill_manager_delegate.h"
21 #include "components/autofill/browser/webdata/autofill_webdata_service.h" 22 #include "components/autofill/browser/webdata/autofill_webdata_service.h"
22 #include "components/autofill/common/form_data.h" 23 #include "components/autofill/common/form_data.h"
23 #include "components/webdata/common/web_data_service_test_util.h" 24 #include "components/webdata/common/web_data_service_test_util.h"
24 #include "content/public/test/test_browser_thread.h" 25 #include "content/public/test/test_browser_thread.h"
25 #include "content/public/test/test_utils.h" 26 #include "content/public/test/test_utils.h"
26 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/gfx/rect.h" 29 #include "ui/gfx/rect.h"
29 30
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 93
93 } // namespace 94 } // namespace
94 95
95 class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness { 96 class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness {
96 protected: 97 protected:
97 virtual void SetUp() OVERRIDE { 98 virtual void SetUp() OVERRIDE {
98 ChromeRenderViewHostTestHarness::SetUp(); 99 ChromeRenderViewHostTestHarness::SetUp();
99 web_data_service_ = new MockWebDataService(); 100 web_data_service_ = new MockWebDataService();
100 WebDataServiceFactory::GetInstance()->SetTestingFactory( 101 WebDataServiceFactory::GetInstance()->SetTestingFactory(
101 profile(), MockWebDataServiceWrapperCurrent::Build); 102 profile(), MockWebDataServiceWrapperCurrent::Build);
102 autocomplete_manager_.reset(new AutocompleteHistoryManager(web_contents())); 103 autofill_driver_.reset(new TestAutofillDriver(web_contents()));
104 autocomplete_manager_.reset(
105 new AutocompleteHistoryManager(autofill_driver_.get()));
103 } 106 }
104 107
105 virtual void TearDown() OVERRIDE { 108 virtual void TearDown() OVERRIDE {
106 autocomplete_manager_.reset(); 109 autocomplete_manager_.reset();
107 web_data_service_ = NULL; 110 web_data_service_ = NULL;
108 ChromeRenderViewHostTestHarness::TearDown(); 111 ChromeRenderViewHostTestHarness::TearDown();
109 } 112 }
110 113
111 scoped_refptr<MockWebDataService> web_data_service_; 114 scoped_refptr<MockWebDataService> web_data_service_;
112 scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_; 115 scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_;
116 scoped_ptr<AutofillDriver> autofill_driver_;
113 MockAutofillManagerDelegate manager_delegate; 117 MockAutofillManagerDelegate manager_delegate;
114 }; 118 };
115 119
116 // Tests that credit card numbers are not sent to the WebDatabase to be saved. 120 // Tests that credit card numbers are not sent to the WebDatabase to be saved.
117 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) { 121 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) {
118 FormData form; 122 FormData form;
119 form.name = ASCIIToUTF16("MyForm"); 123 form.name = ASCIIToUTF16("MyForm");
120 form.method = ASCIIToUTF16("POST"); 124 form.method = ASCIIToUTF16("POST");
121 form.origin = GURL("http://myform.com/form.html"); 125 form.origin = GURL("http://myform.com/form.html");
122 form.action = GURL("http://myform.com/submit.html"); 126 form.action = GURL("http://myform.com/submit.html");
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 form.fields.push_back(search_field); 199 form.fields.push_back(search_field);
196 200
197 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); 201 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1);
198 autocomplete_manager_->OnFormSubmitted(form); 202 autocomplete_manager_->OnFormSubmitted(form);
199 } 203 }
200 204
201 namespace { 205 namespace {
202 206
203 class MockAutofillExternalDelegate : public AutofillExternalDelegate { 207 class MockAutofillExternalDelegate : public AutofillExternalDelegate {
204 public: 208 public:
205 explicit MockAutofillExternalDelegate(content::WebContents* web_contents) 209 explicit MockAutofillExternalDelegate(content::WebContents* web_contents,
206 : AutofillExternalDelegate( 210 AutofillManager* autofill_manager)
207 web_contents, AutofillManager::FromWebContents(web_contents)) {} 211 : AutofillExternalDelegate(web_contents, autofill_manager) {}
208 virtual ~MockAutofillExternalDelegate() {} 212 virtual ~MockAutofillExternalDelegate() {}
209 213
210 MOCK_METHOD5(OnSuggestionsReturned, 214 MOCK_METHOD5(OnSuggestionsReturned,
211 void(int query_id, 215 void(int query_id,
212 const std::vector<base::string16>& autofill_values, 216 const std::vector<base::string16>& autofill_values,
213 const std::vector<base::string16>& autofill_labels, 217 const std::vector<base::string16>& autofill_labels,
214 const std::vector<base::string16>& autofill_icons, 218 const std::vector<base::string16>& autofill_icons,
215 const std::vector<int>& autofill_unique_ids)); 219 const std::vector<int>& autofill_unique_ids));
216 220
217 private: 221 private:
218 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate); 222 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate);
219 }; 223 };
220 224
221 class AutocompleteHistoryManagerStubSend : public AutocompleteHistoryManager { 225 class AutocompleteHistoryManagerNoIPC : public AutocompleteHistoryManager {
222 public: 226 public:
223 explicit AutocompleteHistoryManagerStubSend(WebContents* web_contents) 227 explicit AutocompleteHistoryManagerNoIPC(AutofillDriver* driver)
224 : AutocompleteHistoryManager(web_contents) {} 228 : AutocompleteHistoryManager(driver) {
229 // Ensure that IPC is not sent during the test.
230 set_send_ipc(false);
231 }
225 232
226 // Increase visibility for testing. 233 // Increase visibility for testing.
227 void SendSuggestions(const std::vector<base::string16>* suggestions) { 234 void SendSuggestions(const std::vector<base::string16>* suggestions) {
228 AutocompleteHistoryManager::SendSuggestions(suggestions); 235 AutocompleteHistoryManager::SendSuggestions(suggestions);
229 } 236 }
230
231 // Intentionally swallow the message.
232 virtual bool Send(IPC::Message* message) OVERRIDE {
233 delete message;
234 return true;
235 }
236 }; 237 };
237 238
238 } // namespace 239 } // namespace
239 240
240 // Make sure our external delegate is called at the right time. 241 // Make sure our external delegate is called at the right time.
241 TEST_F(AutocompleteHistoryManagerTest, ExternalDelegate) { 242 TEST_F(AutocompleteHistoryManagerTest, ExternalDelegate) {
242 // Local version with a stubbed out Send() 243 AutocompleteHistoryManagerNoIPC autocomplete_history_manager(
243 AutocompleteHistoryManagerStubSend autocomplete_history_manager( 244 autofill_driver_.get());
244 web_contents());
245 245
246 AutofillManager::CreateForWebContentsAndDelegate( 246 scoped_ptr<AutofillManager> autofill_manager(new AutofillManager(
247 web_contents(), 247 autofill_driver_.get(),
248 &manager_delegate, 248 &manager_delegate,
249 "en-US", 249 "en-US",
250 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER); 250 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER));
251 251
252 MockAutofillExternalDelegate external_delegate(web_contents()); 252 MockAutofillExternalDelegate external_delegate(web_contents(),
253 autofill_manager.get());
253 autocomplete_history_manager.SetExternalDelegate(&external_delegate); 254 autocomplete_history_manager.SetExternalDelegate(&external_delegate);
254 255
255 // Should trigger a call to OnSuggestionsReturned, verified by the mock. 256 // Should trigger a call to OnSuggestionsReturned, verified by the mock.
256 EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _)); 257 EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _));
257 autocomplete_history_manager.SendSuggestions(NULL); 258 autocomplete_history_manager.SendSuggestions(NULL);
258 } 259 }
259 260
260 } // namespace autofill 261 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698