OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 7 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/infobars/confirm_infobar_delegate.h" | 9 #include "chrome/browser/infobars/confirm_infobar_delegate.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 " </select><br>" | 67 " </select><br>" |
68 "<label for=\"phone\">Phone number:</label>" | 68 "<label for=\"phone\">Phone number:</label>" |
69 " <input type=\"text\" id=\"phone\"><br>" | 69 " <input type=\"text\" id=\"phone\"><br>" |
70 "</form>"; | 70 "</form>"; |
71 | 71 |
72 class AutofillManagerTestDelegateImpl | 72 class AutofillManagerTestDelegateImpl |
73 : public autofill::AutofillManagerTestDelegate { | 73 : public autofill::AutofillManagerTestDelegate { |
74 public: | 74 public: |
75 AutofillManagerTestDelegateImpl() {} | 75 AutofillManagerTestDelegateImpl() {} |
76 | 76 |
| 77 // autofill::AutofillManagerTestDelegate: |
77 virtual void DidPreviewFormData() OVERRIDE { | 78 virtual void DidPreviewFormData() OVERRIDE { |
78 loop_runner_->Quit(); | 79 loop_runner_->Quit(); |
79 } | 80 } |
80 | 81 |
81 virtual void DidFillFormData() OVERRIDE { | 82 virtual void DidFillFormData() OVERRIDE { |
82 loop_runner_->Quit(); | 83 loop_runner_->Quit(); |
83 } | 84 } |
84 | 85 |
85 virtual void DidShowSuggestions() OVERRIDE { | 86 virtual void DidShowSuggestions() OVERRIDE { |
86 loop_runner_->Quit(); | 87 loop_runner_->Quit(); |
(...skipping 26 matching lines...) Expand all Loading... |
113 AddObserver(this); | 114 AddObserver(this); |
114 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | 115 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
115 content::NotificationService::AllSources()); | 116 content::NotificationService::AllSources()); |
116 } | 117 } |
117 | 118 |
118 virtual ~WindowedPersonalDataManagerObserver() { | 119 virtual ~WindowedPersonalDataManagerObserver() { |
119 if (infobar_service_ && infobar_service_->infobar_count() > 0) | 120 if (infobar_service_ && infobar_service_->infobar_count() > 0) |
120 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); | 121 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); |
121 } | 122 } |
122 | 123 |
123 void Wait() { | |
124 if (!alerted_) { | |
125 has_run_message_loop_ = true; | |
126 content::RunMessageLoop(); | |
127 } | |
128 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> | |
129 RemoveObserver(this); | |
130 } | |
131 | |
132 // PersonalDataManagerObserver: | 124 // PersonalDataManagerObserver: |
133 virtual void OnPersonalDataChanged() OVERRIDE { | 125 virtual void OnPersonalDataChanged() OVERRIDE { |
134 if (has_run_message_loop_) { | 126 if (has_run_message_loop_) { |
135 base::MessageLoopForUI::current()->Quit(); | 127 base::MessageLoopForUI::current()->Quit(); |
136 has_run_message_loop_ = false; | 128 has_run_message_loop_ = false; |
137 } | 129 } |
138 alerted_ = true; | 130 alerted_ = true; |
139 } | 131 } |
140 | 132 |
141 virtual void OnInsufficientFormData() OVERRIDE { | 133 virtual void OnInsufficientFormData() OVERRIDE { |
142 OnPersonalDataChanged(); | 134 OnPersonalDataChanged(); |
143 } | 135 } |
144 | 136 |
145 // content::NotificationObserver: | 137 // content::NotificationObserver: |
146 virtual void Observe(int type, | 138 virtual void Observe(int type, |
147 const content::NotificationSource& source, | 139 const content::NotificationSource& source, |
148 const content::NotificationDetails& details) OVERRIDE { | 140 const content::NotificationDetails& details) OVERRIDE { |
149 // Accept in the infobar. | 141 // Accept in the infobar. |
150 infobar_service_ = InfoBarService::FromWebContents( | 142 infobar_service_ = InfoBarService::FromWebContents( |
151 browser_->tab_strip_model()->GetActiveWebContents()); | 143 browser_->tab_strip_model()->GetActiveWebContents()); |
152 InfoBarDelegate* infobar = infobar_service_->infobar_at(0); | 144 InfoBarDelegate* infobar = infobar_service_->infobar_at(0); |
153 | 145 |
154 ConfirmInfoBarDelegate* confirm_infobar = | 146 ConfirmInfoBarDelegate* confirm_infobar = |
155 infobar->AsConfirmInfoBarDelegate(); | 147 infobar->AsConfirmInfoBarDelegate(); |
156 confirm_infobar->Accept(); | 148 confirm_infobar->Accept(); |
157 } | 149 } |
158 | 150 |
| 151 void Wait() { |
| 152 if (!alerted_) { |
| 153 has_run_message_loop_ = true; |
| 154 content::RunMessageLoop(); |
| 155 } |
| 156 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 157 RemoveObserver(this); |
| 158 } |
| 159 |
159 private: | 160 private: |
160 bool alerted_; | 161 bool alerted_; |
161 bool has_run_message_loop_; | 162 bool has_run_message_loop_; |
162 Browser* browser_; | 163 Browser* browser_; |
163 content::NotificationRegistrar registrar_; | 164 content::NotificationRegistrar registrar_; |
164 InfoBarService* infobar_service_; | 165 InfoBarService* infobar_service_; |
165 }; | 166 }; |
166 | 167 |
167 class TestAutofillExternalDelegate : public AutofillExternalDelegate { | 168 class TestAutofillExternalDelegate : public AutofillExternalDelegate { |
168 public: | 169 public: |
169 TestAutofillExternalDelegate(content::WebContents* web_contents, | 170 TestAutofillExternalDelegate(content::WebContents* web_contents, |
170 AutofillManager* autofill_manager, | 171 AutofillManager* autofill_manager, |
171 AutofillDriver* autofill_driver) | 172 AutofillDriver* autofill_driver) |
172 : AutofillExternalDelegate(web_contents, autofill_manager, | 173 : AutofillExternalDelegate(web_contents, autofill_manager, |
173 autofill_driver), | 174 autofill_driver), |
174 keyboard_listener_(NULL) { | 175 keyboard_listener_(NULL) { |
175 } | 176 } |
176 virtual ~TestAutofillExternalDelegate() {} | 177 virtual ~TestAutofillExternalDelegate() {} |
177 | 178 |
| 179 // AutofillExternalDelegate: |
178 virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE { | 180 virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE { |
179 AutofillExternalDelegate::OnPopupShown(listener); | 181 AutofillExternalDelegate::OnPopupShown(listener); |
180 keyboard_listener_ = listener; | 182 keyboard_listener_ = listener; |
181 } | 183 } |
182 | 184 |
183 virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE { | 185 virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE { |
184 keyboard_listener_ = NULL; | 186 keyboard_listener_ = NULL; |
185 AutofillExternalDelegate::OnPopupHidden(listener); | 187 AutofillExternalDelegate::OnPopupHidden(listener); |
186 } | 188 } |
187 | 189 |
188 content::KeyboardListener* keyboard_listener() { | 190 content::KeyboardListener* keyboard_listener() { |
189 return keyboard_listener_; | 191 return keyboard_listener_; |
190 } | 192 } |
191 | 193 |
192 private: | 194 private: |
193 // The popup that is currently registered as a keyboard listener, or NULL if | 195 // The popup that is currently registered as a keyboard listener, or NULL if |
194 // there is none. | 196 // there is none. |
195 content::KeyboardListener* keyboard_listener_; | 197 content::KeyboardListener* keyboard_listener_; |
196 | 198 |
197 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); | 199 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); |
198 }; | 200 }; |
199 | 201 |
200 class AutofillInteractiveTest : public InProcessBrowserTest { | 202 class AutofillInteractiveTest : public InProcessBrowserTest { |
201 protected: | 203 protected: |
202 AutofillInteractiveTest() {} | 204 AutofillInteractiveTest() {} |
203 | 205 |
| 206 // InProcessBrowserTest: |
204 virtual void SetUpOnMainThread() OVERRIDE { | 207 virtual void SetUpOnMainThread() OVERRIDE { |
205 // Don't want Keychain coming up on Mac. | 208 // Don't want Keychain coming up on Mac. |
206 test::DisableSystemServices(browser()->profile()); | 209 test::DisableSystemServices(browser()->profile()); |
207 | 210 |
208 // When testing the native UI, hook up a test external delegate, which | 211 // When testing the native UI, hook up a test external delegate, which |
209 // allows us to forward keyboard events to the popup directly. | 212 // allows us to forward keyboard events to the popup directly. |
210 content::WebContents* web_contents = | 213 content::WebContents* web_contents = |
211 browser()->tab_strip_model()->GetActiveWebContents(); | 214 browser()->tab_strip_model()->GetActiveWebContents(); |
212 AutofillDriverImpl* autofill_driver = | 215 AutofillDriverImpl* autofill_driver = |
213 AutofillDriverImpl::FromWebContents(web_contents); | 216 AutofillDriverImpl::FromWebContents(web_contents); |
214 AutofillManager* autofill_manager = autofill_driver->autofill_manager(); | 217 AutofillManager* autofill_manager = autofill_driver->autofill_manager(); |
215 scoped_ptr<AutofillExternalDelegate> external_delegate( | 218 scoped_ptr<AutofillExternalDelegate> external_delegate( |
216 new TestAutofillExternalDelegate(web_contents, autofill_manager, | 219 new TestAutofillExternalDelegate(web_contents, autofill_manager, |
217 autofill_driver)); | 220 autofill_driver)); |
218 autofill_driver->SetAutofillExternalDelegate(external_delegate.Pass()); | 221 autofill_driver->SetAutofillExternalDelegate(external_delegate.Pass()); |
219 autofill_manager->SetTestDelegate(&test_delegate_); | 222 autofill_manager->SetTestDelegate(&test_delegate_); |
220 } | 223 } |
221 | 224 |
222 virtual void CleanUpOnMainThread() OVERRIDE { | 225 virtual void CleanUpOnMainThread() OVERRIDE { |
223 // Make sure to close any showing popups prior to tearing down the UI. | 226 // Make sure to close any showing popups prior to tearing down the UI. |
224 content::WebContents* web_contents = | 227 content::WebContents* web_contents = |
225 browser()->tab_strip_model()->GetActiveWebContents(); | 228 browser()->tab_strip_model()->GetActiveWebContents(); |
226 AutofillManager* autofill_manager = | 229 AutofillManager* autofill_manager = |
227 AutofillDriverImpl::FromWebContents(web_contents)->autofill_manager(); | 230 AutofillDriverImpl::FromWebContents(web_contents)->autofill_manager(); |
228 autofill_manager->delegate()->HideAutofillPopup(); | 231 autofill_manager->delegate()->HideAutofillPopup(); |
229 } | 232 } |
230 | 233 |
231 PersonalDataManager* personal_data_manager() { | 234 PersonalDataManager* GetPersonalDataManager() { |
232 return PersonalDataManagerFactory::GetForProfile(browser()->profile()); | 235 return PersonalDataManagerFactory::GetForProfile(browser()->profile()); |
233 } | 236 } |
234 | 237 |
| 238 content::RenderViewHost* GetRenderViewHost() { |
| 239 return browser()->tab_strip_model()->GetActiveWebContents()-> |
| 240 GetRenderViewHost(); |
| 241 } |
| 242 |
| 243 TestAutofillExternalDelegate* GetExternalDelegate() { |
| 244 content::WebContents* web_contents = |
| 245 browser()->tab_strip_model()->GetActiveWebContents(); |
| 246 AutofillDriverImpl* autofill_driver = |
| 247 AutofillDriverImpl::FromWebContents(web_contents); |
| 248 return static_cast<TestAutofillExternalDelegate*>( |
| 249 autofill_driver->autofill_external_delegate()); |
| 250 } |
| 251 |
235 void CreateTestProfile() { | 252 void CreateTestProfile() { |
236 AutofillProfile profile; | 253 AutofillProfile profile; |
237 test::SetProfileInfo( | 254 test::SetProfileInfo( |
238 &profile, "Milton", "C.", "Waddams", | 255 &profile, "Milton", "C.", "Waddams", |
239 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane", | 256 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane", |
240 "Basement", "Austin", "Texas", "78744", "US", "5125551234"); | 257 "Basement", "Austin", "Texas", "78744", "US", "5125551234"); |
241 | 258 |
242 WindowedPersonalDataManagerObserver observer(browser()); | 259 WindowedPersonalDataManagerObserver observer(browser()); |
243 personal_data_manager()->AddProfile(profile); | 260 GetPersonalDataManager()->AddProfile(profile); |
244 | 261 |
245 // AddProfile is asynchronous. Wait for it to finish before continuing the | 262 // AddProfile is asynchronous. Wait for it to finish before continuing the |
246 // tests. | 263 // tests. |
247 observer.Wait(); | 264 observer.Wait(); |
248 } | 265 } |
249 | 266 |
250 void ExpectFieldValue(const std::string& field_name, | 267 void ExpectFieldValue(const std::string& field_name, |
251 const std::string& expected_value) { | 268 const std::string& expected_value) { |
252 std::string value; | 269 std::string value; |
253 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 270 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
254 browser()->tab_strip_model()->GetActiveWebContents(), | 271 browser()->tab_strip_model()->GetActiveWebContents(), |
255 "window.domAutomationController.send(" | 272 "window.domAutomationController.send(" |
256 " document.getElementById('" + field_name + "').value);", | 273 " document.getElementById('" + field_name + "').value);", |
257 &value)); | 274 &value)); |
258 EXPECT_EQ(expected_value, value); | 275 EXPECT_EQ(expected_value, value); |
259 } | 276 } |
260 | 277 |
261 RenderViewHost* render_view_host() { | |
262 return browser()->tab_strip_model()->GetActiveWebContents()-> | |
263 GetRenderViewHost(); | |
264 } | |
265 | |
266 void FocusFirstNameField() { | 278 void FocusFirstNameField() { |
267 LOG(WARNING) << "Clicking on the tab."; | 279 LOG(WARNING) << "Clicking on the tab."; |
268 content::SimulateMouseClick( | 280 content::SimulateMouseClick( |
269 browser()->tab_strip_model()->GetActiveWebContents(), | 281 browser()->tab_strip_model()->GetActiveWebContents(), |
270 0, | 282 0, |
271 WebKit::WebMouseEvent::ButtonLeft); | 283 WebKit::WebMouseEvent::ButtonLeft); |
272 | 284 |
273 LOG(WARNING) << "Focusing the first name field."; | 285 LOG(WARNING) << "Focusing the first name field."; |
274 bool result = false; | 286 bool result = false; |
275 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 287 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
276 render_view_host(), | 288 GetRenderViewHost(), |
277 "if (document.readyState === 'complete')" | 289 "if (document.readyState === 'complete')" |
278 " document.getElementById('firstname').focus();" | 290 " document.getElementById('firstname').focus();" |
279 "else" | 291 "else" |
280 " domAutomationController.send(false);", | 292 " domAutomationController.send(false);", |
281 &result)); | 293 &result)); |
282 ASSERT_TRUE(result); | 294 ASSERT_TRUE(result); |
283 } | 295 } |
284 | 296 |
285 void ExpectFilledTestForm() { | 297 void ExpectFilledTestForm() { |
286 ExpectFieldValue("firstname", "Milton"); | 298 ExpectFieldValue("firstname", "Milton"); |
(...skipping 11 matching lines...) Expand all Loading... |
298 test_delegate_.Reset(); | 310 test_delegate_.Reset(); |
299 content::SimulateKeyPress( | 311 content::SimulateKeyPress( |
300 browser()->tab_strip_model()->GetActiveWebContents(), | 312 browser()->tab_strip_model()->GetActiveWebContents(), |
301 key, false, false, false, false); | 313 key, false, false, false, false); |
302 test_delegate_.Wait(); | 314 test_delegate_.Wait(); |
303 } | 315 } |
304 | 316 |
305 void SendKeyToPopupAndWait(ui::KeyboardCode key) { | 317 void SendKeyToPopupAndWait(ui::KeyboardCode key) { |
306 // TODO(isherman): Remove this condition once the WebKit popup UI code is | 318 // TODO(isherman): Remove this condition once the WebKit popup UI code is |
307 // removed. | 319 // removed. |
308 if (!external_delegate()) { | 320 if (!GetExternalDelegate()) { |
309 // When testing the WebKit-based UI, route all keys to the page. | 321 // When testing the WebKit-based UI, route all keys to the page. |
310 SendKeyToPageAndWait(key); | 322 SendKeyToPageAndWait(key); |
311 return; | 323 return; |
312 } | 324 } |
313 | 325 |
314 // When testing the native UI, route popup-targeted key presses via the | 326 // When testing the native UI, route popup-targeted key presses via the |
315 // external delegate. | 327 // external delegate. |
316 content::NativeWebKeyboardEvent event; | 328 content::NativeWebKeyboardEvent event; |
317 event.windowsKeyCode = key; | 329 event.windowsKeyCode = key; |
318 test_delegate_.Reset(); | 330 test_delegate_.Reset(); |
319 external_delegate()->keyboard_listener()->HandleKeyPressEvent(event); | 331 GetExternalDelegate()->keyboard_listener()->HandleKeyPressEvent(event); |
320 test_delegate_.Wait(); | 332 test_delegate_.Wait(); |
321 } | 333 } |
322 | 334 |
323 TestAutofillExternalDelegate* external_delegate() { | |
324 content::WebContents* web_contents = | |
325 browser()->tab_strip_model()->GetActiveWebContents(); | |
326 AutofillDriverImpl* autofill_driver = | |
327 AutofillDriverImpl::FromWebContents(web_contents); | |
328 return static_cast<TestAutofillExternalDelegate*>( | |
329 autofill_driver->autofill_external_delegate()); | |
330 } | |
331 | |
332 AutofillManagerTestDelegateImpl test_delegate_; | 335 AutofillManagerTestDelegateImpl test_delegate_; |
333 }; | 336 }; |
334 | 337 |
335 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_AutofillSelectViaTab) { | 338 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_AutofillSelectViaTab) { |
336 CreateTestProfile(); | 339 CreateTestProfile(); |
337 | 340 |
338 // Load the test page. | 341 // Load the test page. |
339 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), | 342 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), |
340 GURL(std::string(kDataURIPrefix) + kTestFormString))); | 343 GURL(std::string(kDataURIPrefix) + kTestFormString))); |
341 | 344 |
342 // Focus a fillable field. | 345 // Focus a fillable field. |
343 FocusFirstNameField(); | 346 FocusFirstNameField(); |
344 | 347 |
345 // Press the down arrow to initiate Autofill and wait for the popup to be | 348 // Press the down arrow to initiate Autofill and wait for the popup to be |
346 // shown. | 349 // shown. |
347 SendKeyToPageAndWait(ui::VKEY_DOWN); | 350 SendKeyToPageAndWait(ui::VKEY_DOWN); |
348 | 351 |
349 // Press the down arrow to select the suggestion and preview the autofilled | 352 // Press the down arrow to select the suggestion and preview the autofilled |
350 // form. | 353 // form. |
351 SendKeyToPopupAndWait(ui::VKEY_DOWN); | 354 SendKeyToPopupAndWait(ui::VKEY_DOWN); |
352 | 355 |
353 // Press tab to accept the autofill suggestions. | 356 // Press tab to accept the autofill suggestions. |
354 SendKeyToPopupAndWait(ui::VKEY_TAB); | 357 SendKeyToPopupAndWait(ui::VKEY_TAB); |
355 | 358 |
356 // The form should be filled. | 359 // The form should be filled. |
357 ExpectFilledTestForm(); | 360 ExpectFilledTestForm(); |
358 } | 361 } |
359 | 362 |
360 } // namespace autofill | 363 } // namespace autofill |
OLD | NEW |