OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <vector> | |
6 | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/prefs/testing_pref_service.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/strings/string16.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "base/synchronization/waitable_event.h" | |
13 #include "chrome/browser/webdata/web_data_service_factory.h" | |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
15 #include "chrome/test/base/testing_browser_process.h" | |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "components/autofill/browser/autocomplete_history_manager.h" | |
18 #include "components/autofill/browser/autofill_external_delegate.h" | |
19 #include "components/autofill/browser/autofill_manager.h" | |
20 #include "components/autofill/browser/test_autofill_driver.h" | |
21 #include "components/autofill/browser/test_autofill_manager_delegate.h" | |
22 #include "components/autofill/browser/webdata/autofill_webdata_service.h" | |
23 #include "components/autofill/core/common/form_data.h" | |
24 #include "components/webdata/common/web_data_service_test_util.h" | |
25 #include "content/public/test/test_browser_thread.h" | |
26 #include "content/public/test/test_utils.h" | |
27 #include "testing/gmock/include/gmock/gmock.h" | |
28 #include "testing/gtest/include/gtest/gtest.h" | |
29 #include "ui/gfx/rect.h" | |
30 | |
31 using content::WebContents; | |
32 using testing::_; | |
33 | |
34 namespace autofill { | |
35 | |
36 namespace { | |
37 | |
38 class MockWebDataService : public AutofillWebDataService { | |
39 public: | |
40 MockWebDataService() | |
41 : AutofillWebDataService() { | |
42 current_mock_web_data_service_ = this; | |
43 } | |
44 | |
45 MOCK_METHOD1(AddFormFields, void(const std::vector<FormFieldData>&)); | |
46 | |
47 static scoped_refptr<MockWebDataService> GetCurrent() { | |
48 if (!current_mock_web_data_service_) { | |
49 return new MockWebDataService(); | |
50 } | |
51 return current_mock_web_data_service_; | |
52 } | |
53 | |
54 protected: | |
55 virtual ~MockWebDataService() {} | |
56 | |
57 private: | |
58 // Keep track of the most recently created instance, so that it can be | |
59 // associated with the current profile when Build() is called. | |
60 static MockWebDataService* current_mock_web_data_service_; | |
61 }; | |
62 | |
63 MockWebDataService* MockWebDataService::current_mock_web_data_service_ = NULL; | |
64 | |
65 class MockWebDataServiceWrapperCurrent : public MockWebDataServiceWrapperBase { | |
66 public: | |
67 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | |
68 return new MockWebDataServiceWrapperCurrent(); | |
69 } | |
70 | |
71 MockWebDataServiceWrapperCurrent() {} | |
72 | |
73 virtual scoped_refptr<AutofillWebDataService> GetAutofillWebData() OVERRIDE { | |
74 return MockWebDataService::GetCurrent(); | |
75 } | |
76 | |
77 private: | |
78 DISALLOW_COPY_AND_ASSIGN(MockWebDataServiceWrapperCurrent); | |
79 }; | |
80 | |
81 class MockAutofillManagerDelegate | |
82 : public autofill::TestAutofillManagerDelegate { | |
83 public: | |
84 MockAutofillManagerDelegate() {} | |
85 virtual ~MockAutofillManagerDelegate() {} | |
86 virtual PrefService* GetPrefs() OVERRIDE { return &prefs_; } | |
87 | |
88 private: | |
89 TestingPrefServiceSimple prefs_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); | |
92 }; | |
93 | |
94 } // namespace | |
95 | |
96 class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness { | |
97 protected: | |
98 virtual void SetUp() OVERRIDE { | |
99 ChromeRenderViewHostTestHarness::SetUp(); | |
100 web_data_service_ = new MockWebDataService(); | |
101 WebDataServiceFactory::GetInstance()->SetTestingFactory( | |
102 profile(), MockWebDataServiceWrapperCurrent::Build); | |
103 autofill_driver_.reset(new TestAutofillDriver(web_contents())); | |
104 autocomplete_manager_.reset( | |
105 new AutocompleteHistoryManager(autofill_driver_.get())); | |
106 } | |
107 | |
108 virtual void TearDown() OVERRIDE { | |
109 autocomplete_manager_.reset(); | |
110 web_data_service_ = NULL; | |
111 ChromeRenderViewHostTestHarness::TearDown(); | |
112 } | |
113 | |
114 scoped_refptr<MockWebDataService> web_data_service_; | |
115 scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_; | |
116 scoped_ptr<AutofillDriver> autofill_driver_; | |
117 MockAutofillManagerDelegate manager_delegate; | |
118 }; | |
119 | |
120 // Tests that credit card numbers are not sent to the WebDatabase to be saved. | |
121 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) { | |
122 FormData form; | |
123 form.name = ASCIIToUTF16("MyForm"); | |
124 form.method = ASCIIToUTF16("POST"); | |
125 form.origin = GURL("http://myform.com/form.html"); | |
126 form.action = GURL("http://myform.com/submit.html"); | |
127 form.user_submitted = true; | |
128 | |
129 // Valid Visa credit card number pulled from the paypal help site. | |
130 FormFieldData valid_cc; | |
131 valid_cc.label = ASCIIToUTF16("Credit Card"); | |
132 valid_cc.name = ASCIIToUTF16("ccnum"); | |
133 valid_cc.value = ASCIIToUTF16("4012888888881881"); | |
134 valid_cc.form_control_type = "text"; | |
135 form.fields.push_back(valid_cc); | |
136 | |
137 EXPECT_CALL(*web_data_service_.get(), AddFormFields(_)).Times(0); | |
138 autocomplete_manager_->OnFormSubmitted(form); | |
139 } | |
140 | |
141 // Contrary test to AutocompleteHistoryManagerTest.CreditCardNumberValue. The | |
142 // value being submitted is not a valid credit card number, so it will be sent | |
143 // to the WebDatabase to be saved. | |
144 TEST_F(AutocompleteHistoryManagerTest, NonCreditCardNumberValue) { | |
145 FormData form; | |
146 form.name = ASCIIToUTF16("MyForm"); | |
147 form.method = ASCIIToUTF16("POST"); | |
148 form.origin = GURL("http://myform.com/form.html"); | |
149 form.action = GURL("http://myform.com/submit.html"); | |
150 form.user_submitted = true; | |
151 | |
152 // Invalid credit card number. | |
153 FormFieldData invalid_cc; | |
154 invalid_cc.label = ASCIIToUTF16("Credit Card"); | |
155 invalid_cc.name = ASCIIToUTF16("ccnum"); | |
156 invalid_cc.value = ASCIIToUTF16("4580123456789012"); | |
157 invalid_cc.form_control_type = "text"; | |
158 form.fields.push_back(invalid_cc); | |
159 | |
160 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); | |
161 autocomplete_manager_->OnFormSubmitted(form); | |
162 } | |
163 | |
164 // Tests that SSNs are not sent to the WebDatabase to be saved. | |
165 TEST_F(AutocompleteHistoryManagerTest, SSNValue) { | |
166 FormData form; | |
167 form.name = ASCIIToUTF16("MyForm"); | |
168 form.method = ASCIIToUTF16("POST"); | |
169 form.origin = GURL("http://myform.com/form.html"); | |
170 form.action = GURL("http://myform.com/submit.html"); | |
171 form.user_submitted = true; | |
172 | |
173 FormFieldData ssn; | |
174 ssn.label = ASCIIToUTF16("Social Security Number"); | |
175 ssn.name = ASCIIToUTF16("ssn"); | |
176 ssn.value = ASCIIToUTF16("078-05-1120"); | |
177 ssn.form_control_type = "text"; | |
178 form.fields.push_back(ssn); | |
179 | |
180 EXPECT_CALL(*web_data_service_.get(), AddFormFields(_)).Times(0); | |
181 autocomplete_manager_->OnFormSubmitted(form); | |
182 } | |
183 | |
184 // Verify that autocomplete text is saved for search fields. | |
185 TEST_F(AutocompleteHistoryManagerTest, SearchField) { | |
186 FormData form; | |
187 form.name = ASCIIToUTF16("MyForm"); | |
188 form.method = ASCIIToUTF16("POST"); | |
189 form.origin = GURL("http://myform.com/form.html"); | |
190 form.action = GURL("http://myform.com/submit.html"); | |
191 form.user_submitted = true; | |
192 | |
193 // Search field. | |
194 FormFieldData search_field; | |
195 search_field.label = ASCIIToUTF16("Search"); | |
196 search_field.name = ASCIIToUTF16("search"); | |
197 search_field.value = ASCIIToUTF16("my favorite query"); | |
198 search_field.form_control_type = "search"; | |
199 form.fields.push_back(search_field); | |
200 | |
201 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); | |
202 autocomplete_manager_->OnFormSubmitted(form); | |
203 } | |
204 | |
205 namespace { | |
206 | |
207 class MockAutofillExternalDelegate : public AutofillExternalDelegate { | |
208 public: | |
209 explicit MockAutofillExternalDelegate(content::WebContents* web_contents, | |
210 AutofillManager* autofill_manager) | |
211 : AutofillExternalDelegate(web_contents, autofill_manager) {} | |
212 virtual ~MockAutofillExternalDelegate() {} | |
213 | |
214 MOCK_METHOD5(OnSuggestionsReturned, | |
215 void(int query_id, | |
216 const std::vector<base::string16>& autofill_values, | |
217 const std::vector<base::string16>& autofill_labels, | |
218 const std::vector<base::string16>& autofill_icons, | |
219 const std::vector<int>& autofill_unique_ids)); | |
220 | |
221 private: | |
222 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate); | |
223 }; | |
224 | |
225 class AutocompleteHistoryManagerNoIPC : public AutocompleteHistoryManager { | |
226 public: | |
227 explicit AutocompleteHistoryManagerNoIPC(AutofillDriver* driver) | |
228 : AutocompleteHistoryManager(driver) { | |
229 // Ensure that IPC is not sent during the test. | |
230 set_send_ipc(false); | |
231 } | |
232 | |
233 using AutocompleteHistoryManager::SendSuggestions; | |
234 }; | |
235 | |
236 } // namespace | |
237 | |
238 // Make sure our external delegate is called at the right time. | |
239 TEST_F(AutocompleteHistoryManagerTest, ExternalDelegate) { | |
240 AutocompleteHistoryManagerNoIPC autocomplete_history_manager( | |
241 autofill_driver_.get()); | |
242 | |
243 scoped_ptr<AutofillManager> autofill_manager(new AutofillManager( | |
244 autofill_driver_.get(), | |
245 &manager_delegate, | |
246 "en-US", | |
247 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER)); | |
248 | |
249 MockAutofillExternalDelegate external_delegate(web_contents(), | |
250 autofill_manager.get()); | |
251 autocomplete_history_manager.SetExternalDelegate(&external_delegate); | |
252 | |
253 // Should trigger a call to OnSuggestionsReturned, verified by the mock. | |
254 EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _)); | |
255 autocomplete_history_manager.SendSuggestions(NULL); | |
256 } | |
257 | |
258 } // namespace autofill | |
OLD | NEW |