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