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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 "<label for=\"country\">Country:</label>" | 62 "<label for=\"country\">Country:</label>" |
63 " <select id=\"country\">" | 63 " <select id=\"country\">" |
64 " <option value=\"\" selected=\"yes\">--</option>" | 64 " <option value=\"\" selected=\"yes\">--</option>" |
65 " <option value=\"CA\">Canada</option>" | 65 " <option value=\"CA\">Canada</option>" |
66 " <option value=\"US\">United States</option>" | 66 " <option value=\"US\">United States</option>" |
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 |
| 73 // AutofillManagerTestDelegateImpl -------------------------------------------- |
| 74 |
72 class AutofillManagerTestDelegateImpl | 75 class AutofillManagerTestDelegateImpl |
73 : public autofill::AutofillManagerTestDelegate { | 76 : public autofill::AutofillManagerTestDelegate { |
74 public: | 77 public: |
75 AutofillManagerTestDelegateImpl() {} | 78 AutofillManagerTestDelegateImpl(); |
| 79 virtual ~AutofillManagerTestDelegateImpl(); |
76 | 80 |
77 virtual void DidPreviewFormData() OVERRIDE { | 81 // autofill::AutofillManagerTestDelegate: |
78 loop_runner_->Quit(); | 82 virtual void DidPreviewFormData() OVERRIDE; |
79 } | 83 virtual void DidFillFormData() OVERRIDE; |
| 84 virtual void DidShowSuggestions() OVERRIDE; |
80 | 85 |
81 virtual void DidFillFormData() OVERRIDE { | 86 void Reset(); |
82 loop_runner_->Quit(); | 87 void Wait(); |
83 } | |
84 | |
85 virtual void DidShowSuggestions() OVERRIDE { | |
86 loop_runner_->Quit(); | |
87 } | |
88 | |
89 void Reset() { | |
90 loop_runner_ = new content::MessageLoopRunner(); | |
91 } | |
92 | |
93 void Wait() { | |
94 loop_runner_->Run(); | |
95 } | |
96 | 88 |
97 private: | 89 private: |
98 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 90 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
99 | 91 |
100 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl); | 92 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl); |
101 }; | 93 }; |
102 | 94 |
| 95 AutofillManagerTestDelegateImpl::AutofillManagerTestDelegateImpl() { |
| 96 } |
| 97 |
| 98 AutofillManagerTestDelegateImpl::~AutofillManagerTestDelegateImpl() { |
| 99 } |
| 100 |
| 101 void AutofillManagerTestDelegateImpl::DidPreviewFormData() { |
| 102 loop_runner_->Quit(); |
| 103 } |
| 104 |
| 105 void AutofillManagerTestDelegateImpl::DidFillFormData() { |
| 106 loop_runner_->Quit(); |
| 107 } |
| 108 |
| 109 void AutofillManagerTestDelegateImpl::DidShowSuggestions() { |
| 110 loop_runner_->Quit(); |
| 111 } |
| 112 |
| 113 void AutofillManagerTestDelegateImpl::Reset() { |
| 114 loop_runner_ = new content::MessageLoopRunner(); |
| 115 } |
| 116 |
| 117 void AutofillManagerTestDelegateImpl::Wait() { |
| 118 loop_runner_->Run(); |
| 119 } |
| 120 |
| 121 |
| 122 // WindowedPersonalDataManagerObserver ---------------------------------------- |
| 123 |
103 class WindowedPersonalDataManagerObserver | 124 class WindowedPersonalDataManagerObserver |
104 : public PersonalDataManagerObserver, | 125 : public PersonalDataManagerObserver, |
105 public content::NotificationObserver { | 126 public content::NotificationObserver { |
106 public: | 127 public: |
107 explicit WindowedPersonalDataManagerObserver(Browser* browser) | 128 explicit WindowedPersonalDataManagerObserver(Browser* browser); |
108 : alerted_(false), | 129 virtual ~WindowedPersonalDataManagerObserver(); |
109 has_run_message_loop_(false), | |
110 browser_(browser), | |
111 infobar_service_(NULL) { | |
112 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> | |
113 AddObserver(this); | |
114 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
115 content::NotificationService::AllSources()); | |
116 } | |
117 | |
118 virtual ~WindowedPersonalDataManagerObserver() { | |
119 if (infobar_service_ && infobar_service_->infobar_count() > 0) | |
120 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); | |
121 } | |
122 | |
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 | 130 |
132 // PersonalDataManagerObserver: | 131 // PersonalDataManagerObserver: |
133 virtual void OnPersonalDataChanged() OVERRIDE { | 132 virtual void OnPersonalDataChanged() OVERRIDE; |
134 if (has_run_message_loop_) { | 133 virtual void OnInsufficientFormData() OVERRIDE; |
135 base::MessageLoopForUI::current()->Quit(); | |
136 has_run_message_loop_ = false; | |
137 } | |
138 alerted_ = true; | |
139 } | |
140 | |
141 virtual void OnInsufficientFormData() OVERRIDE { | |
142 OnPersonalDataChanged(); | |
143 } | |
144 | 134 |
145 // content::NotificationObserver: | 135 // content::NotificationObserver: |
146 virtual void Observe(int type, | 136 virtual void Observe(int type, |
147 const content::NotificationSource& source, | 137 const content::NotificationSource& source, |
148 const content::NotificationDetails& details) OVERRIDE { | 138 const content::NotificationDetails& details) OVERRIDE; |
149 // Accept in the infobar. | |
150 infobar_service_ = InfoBarService::FromWebContents( | |
151 browser_->tab_strip_model()->GetActiveWebContents()); | |
152 InfoBarDelegate* infobar = infobar_service_->infobar_at(0); | |
153 | 139 |
154 ConfirmInfoBarDelegate* confirm_infobar = | 140 void Wait(); |
155 infobar->AsConfirmInfoBarDelegate(); | |
156 confirm_infobar->Accept(); | |
157 } | |
158 | 141 |
159 private: | 142 private: |
160 bool alerted_; | 143 bool alerted_; |
161 bool has_run_message_loop_; | 144 bool has_run_message_loop_; |
162 Browser* browser_; | 145 Browser* browser_; |
163 content::NotificationRegistrar registrar_; | 146 content::NotificationRegistrar registrar_; |
164 InfoBarService* infobar_service_; | 147 InfoBarService* infobar_service_; |
165 }; | 148 }; |
166 | 149 |
| 150 WindowedPersonalDataManagerObserver::WindowedPersonalDataManagerObserver( |
| 151 Browser* browser) |
| 152 : alerted_(false), |
| 153 has_run_message_loop_(false), |
| 154 browser_(browser), |
| 155 infobar_service_(NULL) { |
| 156 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 157 AddObserver(this); |
| 158 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 159 content::NotificationService::AllSources()); |
| 160 } |
| 161 |
| 162 WindowedPersonalDataManagerObserver::~WindowedPersonalDataManagerObserver() { |
| 163 if (infobar_service_ && infobar_service_->infobar_count() > 0) |
| 164 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); |
| 165 } |
| 166 |
| 167 void WindowedPersonalDataManagerObserver::OnPersonalDataChanged() { |
| 168 if (has_run_message_loop_) { |
| 169 base::MessageLoopForUI::current()->Quit(); |
| 170 has_run_message_loop_ = false; |
| 171 } |
| 172 alerted_ = true; |
| 173 } |
| 174 |
| 175 void WindowedPersonalDataManagerObserver::OnInsufficientFormData() { |
| 176 OnPersonalDataChanged(); |
| 177 } |
| 178 |
| 179 void WindowedPersonalDataManagerObserver::Observe( |
| 180 int type, |
| 181 const content::NotificationSource& source, |
| 182 const content::NotificationDetails& details) { |
| 183 // Accept in the infobar. |
| 184 infobar_service_ = InfoBarService::FromWebContents( |
| 185 browser_->tab_strip_model()->GetActiveWebContents()); |
| 186 InfoBarDelegate* infobar = infobar_service_->infobar_at(0); |
| 187 |
| 188 ConfirmInfoBarDelegate* confirm_infobar = |
| 189 infobar->AsConfirmInfoBarDelegate(); |
| 190 confirm_infobar->Accept(); |
| 191 } |
| 192 |
| 193 void WindowedPersonalDataManagerObserver::Wait() { |
| 194 if (!alerted_) { |
| 195 has_run_message_loop_ = true; |
| 196 content::RunMessageLoop(); |
| 197 } |
| 198 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 199 RemoveObserver(this); |
| 200 } |
| 201 |
| 202 |
| 203 // TestAutofillExternalDelegate ----------------------------------------------- |
| 204 |
167 class TestAutofillExternalDelegate : public AutofillExternalDelegate { | 205 class TestAutofillExternalDelegate : public AutofillExternalDelegate { |
168 public: | 206 public: |
169 TestAutofillExternalDelegate(content::WebContents* web_contents, | 207 TestAutofillExternalDelegate(content::WebContents* web_contents, |
170 AutofillManager* autofill_manager, | 208 AutofillManager* autofill_manager, |
171 AutofillDriver* autofill_driver) | 209 AutofillDriver* autofill_driver); |
172 : AutofillExternalDelegate(web_contents, autofill_manager, | 210 virtual ~TestAutofillExternalDelegate(); |
173 autofill_driver), | |
174 keyboard_listener_(NULL) { | |
175 } | |
176 virtual ~TestAutofillExternalDelegate() {} | |
177 | 211 |
178 virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE { | 212 // AutofillExternalDelegate: |
179 AutofillExternalDelegate::OnPopupShown(listener); | 213 virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE; |
180 keyboard_listener_ = listener; | 214 virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE; |
181 } | |
182 | |
183 virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE { | |
184 keyboard_listener_ = NULL; | |
185 AutofillExternalDelegate::OnPopupHidden(listener); | |
186 } | |
187 | 215 |
188 content::KeyboardListener* keyboard_listener() { | 216 content::KeyboardListener* keyboard_listener() { |
189 return keyboard_listener_; | 217 return keyboard_listener_; |
190 } | 218 } |
191 | 219 |
192 private: | 220 private: |
193 // The popup that is currently registered as a keyboard listener, or NULL if | 221 // The popup that is currently registered as a keyboard listener, or NULL if |
194 // there is none. | 222 // there is none. |
195 content::KeyboardListener* keyboard_listener_; | 223 content::KeyboardListener* keyboard_listener_; |
196 | 224 |
197 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); | 225 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); |
198 }; | 226 }; |
199 | 227 |
| 228 TestAutofillExternalDelegate::TestAutofillExternalDelegate( |
| 229 content::WebContents* web_contents, |
| 230 AutofillManager* autofill_manager, |
| 231 AutofillDriver* autofill_driver) |
| 232 : AutofillExternalDelegate(web_contents, autofill_manager, autofill_driver), |
| 233 keyboard_listener_(NULL) { |
| 234 } |
| 235 |
| 236 TestAutofillExternalDelegate::~TestAutofillExternalDelegate() { |
| 237 } |
| 238 |
| 239 void TestAutofillExternalDelegate::OnPopupShown( |
| 240 content::KeyboardListener* listener) { |
| 241 AutofillExternalDelegate::OnPopupShown(listener); |
| 242 keyboard_listener_ = listener; |
| 243 } |
| 244 |
| 245 void TestAutofillExternalDelegate::OnPopupHidden( |
| 246 content::KeyboardListener* listener) { |
| 247 keyboard_listener_ = NULL; |
| 248 AutofillExternalDelegate::OnPopupHidden(listener); |
| 249 } |
| 250 |
| 251 |
| 252 // AutofillInteractiveTest ---------------------------------------------------- |
| 253 |
200 class AutofillInteractiveTest : public InProcessBrowserTest { | 254 class AutofillInteractiveTest : public InProcessBrowserTest { |
201 protected: | 255 protected: |
202 AutofillInteractiveTest() {} | 256 AutofillInteractiveTest(); |
203 | 257 |
204 virtual void SetUpOnMainThread() OVERRIDE { | 258 // InProcessBrowserTest: |
205 // Don't want Keychain coming up on Mac. | 259 virtual void SetUpOnMainThread() OVERRIDE; |
206 test::DisableSystemServices(browser()->profile()); | 260 virtual void CleanUpOnMainThread() OVERRIDE; |
207 | 261 |
208 // When testing the native UI, hook up a test external delegate, which | 262 PersonalDataManager* GetPersonalDataManager(); |
209 // allows us to forward keyboard events to the popup directly. | 263 content::RenderViewHost* GetRenderViewHost(); |
210 content::WebContents* web_contents = | 264 TestAutofillExternalDelegate* GetExternalDelegate(); |
211 browser()->tab_strip_model()->GetActiveWebContents(); | |
212 AutofillDriverImpl* autofill_driver = | |
213 AutofillDriverImpl::FromWebContents(web_contents); | |
214 AutofillManager* autofill_manager = autofill_driver->autofill_manager(); | |
215 scoped_ptr<AutofillExternalDelegate> external_delegate( | |
216 new TestAutofillExternalDelegate(web_contents, autofill_manager, | |
217 autofill_driver)); | |
218 autofill_driver->SetAutofillExternalDelegate(external_delegate.Pass()); | |
219 autofill_manager->SetTestDelegate(&test_delegate_); | |
220 } | |
221 | 265 |
222 virtual void CleanUpOnMainThread() OVERRIDE { | 266 void CreateTestProfile(); |
223 // Make sure to close any showing popups prior to tearing down the UI. | |
224 content::WebContents* web_contents = | |
225 browser()->tab_strip_model()->GetActiveWebContents(); | |
226 AutofillManager* autofill_manager = | |
227 AutofillDriverImpl::FromWebContents(web_contents)->autofill_manager(); | |
228 autofill_manager->delegate()->HideAutofillPopup(); | |
229 } | |
230 | |
231 PersonalDataManager* personal_data_manager() { | |
232 return PersonalDataManagerFactory::GetForProfile(browser()->profile()); | |
233 } | |
234 | |
235 void CreateTestProfile() { | |
236 AutofillProfile profile; | |
237 test::SetProfileInfo( | |
238 &profile, "Milton", "C.", "Waddams", | |
239 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane", | |
240 "Basement", "Austin", "Texas", "78744", "US", "5125551234"); | |
241 | |
242 WindowedPersonalDataManagerObserver observer(browser()); | |
243 personal_data_manager()->AddProfile(profile); | |
244 | |
245 // AddProfile is asynchronous. Wait for it to finish before continuing the | |
246 // tests. | |
247 observer.Wait(); | |
248 } | |
249 | |
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 void FocusFirstNameField(); |
253 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 270 void ExpectFilledTestForm(); |
254 browser()->tab_strip_model()->GetActiveWebContents(), | 271 void SendKeyToPageAndWait(ui::KeyboardCode key); |
255 "window.domAutomationController.send(" | 272 void SendKeyToPopupAndWait(ui::KeyboardCode key); |
256 " document.getElementById('" + field_name + "').value);", | |
257 &value)); | |
258 EXPECT_EQ(expected_value, value); | |
259 } | |
260 | |
261 RenderViewHost* render_view_host() { | |
262 return browser()->tab_strip_model()->GetActiveWebContents()-> | |
263 GetRenderViewHost(); | |
264 } | |
265 | |
266 void FocusFirstNameField() { | |
267 LOG(WARNING) << "Clicking on the tab."; | |
268 content::SimulateMouseClick( | |
269 browser()->tab_strip_model()->GetActiveWebContents(), | |
270 0, | |
271 WebKit::WebMouseEvent::ButtonLeft); | |
272 | |
273 LOG(WARNING) << "Focusing the first name field."; | |
274 bool result = false; | |
275 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
276 render_view_host(), | |
277 "if (document.readyState === 'complete')" | |
278 " document.getElementById('firstname').focus();" | |
279 "else" | |
280 " domAutomationController.send(false);", | |
281 &result)); | |
282 ASSERT_TRUE(result); | |
283 } | |
284 | |
285 void ExpectFilledTestForm() { | |
286 ExpectFieldValue("firstname", "Milton"); | |
287 ExpectFieldValue("lastname", "Waddams"); | |
288 ExpectFieldValue("address1", "4120 Freidrich Lane"); | |
289 ExpectFieldValue("address2", "Basement"); | |
290 ExpectFieldValue("city", "Austin"); | |
291 ExpectFieldValue("state", "TX"); | |
292 ExpectFieldValue("zip", "78744"); | |
293 ExpectFieldValue("country", "US"); | |
294 ExpectFieldValue("phone", "5125551234"); | |
295 } | |
296 | |
297 void SendKeyToPageAndWait(ui::KeyboardCode key) { | |
298 test_delegate_.Reset(); | |
299 content::SimulateKeyPress( | |
300 browser()->tab_strip_model()->GetActiveWebContents(), | |
301 key, false, false, false, false); | |
302 test_delegate_.Wait(); | |
303 } | |
304 | |
305 void SendKeyToPopupAndWait(ui::KeyboardCode key) { | |
306 // TODO(isherman): Remove this condition once the WebKit popup UI code is | |
307 // removed. | |
308 if (!external_delegate()) { | |
309 // When testing the WebKit-based UI, route all keys to the page. | |
310 SendKeyToPageAndWait(key); | |
311 return; | |
312 } | |
313 | |
314 // When testing the native UI, route popup-targeted key presses via the | |
315 // external delegate. | |
316 content::NativeWebKeyboardEvent event; | |
317 event.windowsKeyCode = key; | |
318 test_delegate_.Reset(); | |
319 external_delegate()->keyboard_listener()->HandleKeyPressEvent(event); | |
320 test_delegate_.Wait(); | |
321 } | |
322 | |
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 | 273 |
332 AutofillManagerTestDelegateImpl test_delegate_; | 274 AutofillManagerTestDelegateImpl test_delegate_; |
333 }; | 275 }; |
334 | 276 |
| 277 AutofillInteractiveTest::AutofillInteractiveTest() { |
| 278 } |
| 279 |
| 280 void AutofillInteractiveTest::SetUpOnMainThread() { |
| 281 // Don't want Keychain coming up on Mac. |
| 282 test::DisableSystemServices(browser()->profile()); |
| 283 |
| 284 // When testing the native UI, hook up a test external delegate, which allows |
| 285 // us to forward keyboard events to the popup directly. |
| 286 content::WebContents* web_contents = |
| 287 browser()->tab_strip_model()->GetActiveWebContents(); |
| 288 AutofillDriverImpl* autofill_driver = |
| 289 AutofillDriverImpl::FromWebContents(web_contents); |
| 290 AutofillManager* autofill_manager = autofill_driver->autofill_manager(); |
| 291 scoped_ptr<AutofillExternalDelegate> external_delegate( |
| 292 new TestAutofillExternalDelegate(web_contents, autofill_manager, |
| 293 autofill_driver)); |
| 294 autofill_driver->SetAutofillExternalDelegate(external_delegate.Pass()); |
| 295 autofill_manager->SetTestDelegate(&test_delegate_); |
| 296 } |
| 297 |
| 298 void AutofillInteractiveTest::CleanUpOnMainThread() { |
| 299 // Make sure to close any showing popups prior to tearing down the UI. |
| 300 content::WebContents* web_contents = |
| 301 browser()->tab_strip_model()->GetActiveWebContents(); |
| 302 AutofillManager* autofill_manager = |
| 303 AutofillDriverImpl::FromWebContents(web_contents)->autofill_manager(); |
| 304 autofill_manager->delegate()->HideAutofillPopup(); |
| 305 } |
| 306 |
| 307 PersonalDataManager* AutofillInteractiveTest::GetPersonalDataManager() { |
| 308 return PersonalDataManagerFactory::GetForProfile(browser()->profile()); |
| 309 } |
| 310 |
| 311 RenderViewHost* AutofillInteractiveTest::GetRenderViewHost() { |
| 312 return browser()->tab_strip_model()->GetActiveWebContents()-> |
| 313 GetRenderViewHost(); |
| 314 } |
| 315 |
| 316 TestAutofillExternalDelegate* AutofillInteractiveTest::GetExternalDelegate() { |
| 317 content::WebContents* web_contents = |
| 318 browser()->tab_strip_model()->GetActiveWebContents(); |
| 319 AutofillDriverImpl* autofill_driver = |
| 320 AutofillDriverImpl::FromWebContents(web_contents); |
| 321 return static_cast<TestAutofillExternalDelegate*>( |
| 322 autofill_driver->autofill_external_delegate()); |
| 323 } |
| 324 |
| 325 void AutofillInteractiveTest::CreateTestProfile() { |
| 326 AutofillProfile profile; |
| 327 test::SetProfileInfo( |
| 328 &profile, "Milton", "C.", "Waddams", |
| 329 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane", |
| 330 "Basement", "Austin", "Texas", "78744", "US", "5125551234"); |
| 331 |
| 332 WindowedPersonalDataManagerObserver observer(browser()); |
| 333 GetPersonalDataManager()->AddProfile(profile); |
| 334 |
| 335 // AddProfile is asynchronous. Wait for it to finish before continuing the |
| 336 // tests. |
| 337 observer.Wait(); |
| 338 } |
| 339 |
| 340 void AutofillInteractiveTest::ExpectFieldValue( |
| 341 const std::string& field_name, |
| 342 const std::string& expected_value) { |
| 343 std::string value; |
| 344 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 345 browser()->tab_strip_model()->GetActiveWebContents(), |
| 346 "window.domAutomationController.send(" |
| 347 " document.getElementById('" + field_name + "').value);", |
| 348 &value)); |
| 349 EXPECT_EQ(expected_value, value); |
| 350 } |
| 351 |
| 352 void AutofillInteractiveTest::FocusFirstNameField() { |
| 353 LOG(WARNING) << "Clicking on the tab."; |
| 354 content::SimulateMouseClick( |
| 355 browser()->tab_strip_model()->GetActiveWebContents(), |
| 356 0, |
| 357 WebKit::WebMouseEvent::ButtonLeft); |
| 358 |
| 359 LOG(WARNING) << "Focusing the first name field."; |
| 360 bool result = false; |
| 361 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 362 GetRenderViewHost(), |
| 363 "if (document.readyState === 'complete')" |
| 364 " document.getElementById('firstname').focus();" |
| 365 "else" |
| 366 " domAutomationController.send(false);", |
| 367 &result)); |
| 368 ASSERT_TRUE(result); |
| 369 } |
| 370 |
| 371 void AutofillInteractiveTest::ExpectFilledTestForm() { |
| 372 ExpectFieldValue("firstname", "Milton"); |
| 373 ExpectFieldValue("lastname", "Waddams"); |
| 374 ExpectFieldValue("address1", "4120 Freidrich Lane"); |
| 375 ExpectFieldValue("address2", "Basement"); |
| 376 ExpectFieldValue("city", "Austin"); |
| 377 ExpectFieldValue("state", "TX"); |
| 378 ExpectFieldValue("zip", "78744"); |
| 379 ExpectFieldValue("country", "US"); |
| 380 ExpectFieldValue("phone", "5125551234"); |
| 381 } |
| 382 |
| 383 void AutofillInteractiveTest::SendKeyToPageAndWait(ui::KeyboardCode key) { |
| 384 test_delegate_.Reset(); |
| 385 content::SimulateKeyPress( |
| 386 browser()->tab_strip_model()->GetActiveWebContents(), |
| 387 key, false, false, false, false); |
| 388 test_delegate_.Wait(); |
| 389 } |
| 390 |
| 391 void AutofillInteractiveTest::SendKeyToPopupAndWait(ui::KeyboardCode key) { |
| 392 // TODO(isherman): Remove this condition once the WebKit popup UI code is |
| 393 // removed. |
| 394 if (!GetExternalDelegate()) { |
| 395 // When testing the WebKit-based UI, route all keys to the page. |
| 396 SendKeyToPageAndWait(key); |
| 397 return; |
| 398 } |
| 399 |
| 400 // When testing the native UI, route popup-targeted key presses via the |
| 401 // external delegate. |
| 402 content::NativeWebKeyboardEvent event; |
| 403 event.windowsKeyCode = key; |
| 404 test_delegate_.Reset(); |
| 405 GetExternalDelegate()->keyboard_listener()->HandleKeyPressEvent(event); |
| 406 test_delegate_.Wait(); |
| 407 } |
| 408 |
| 409 |
| 410 // Actual tests --------------------------------------------------------------- |
| 411 |
335 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_AutofillSelectViaTab) { | 412 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_AutofillSelectViaTab) { |
336 CreateTestProfile(); | 413 CreateTestProfile(); |
337 | 414 |
338 // Load the test page. | 415 // Load the test page. |
339 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), | 416 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), |
340 GURL(std::string(kDataURIPrefix) + kTestFormString))); | 417 GURL(std::string(kDataURIPrefix) + kTestFormString))); |
341 | 418 |
342 // Focus a fillable field. | 419 // Focus a fillable field. |
343 FocusFirstNameField(); | 420 FocusFirstNameField(); |
344 | 421 |
345 // Press the down arrow to initiate Autofill and wait for the popup to be | 422 // Press the down arrow to initiate Autofill and wait for the popup to be |
346 // shown. | 423 // shown. |
347 SendKeyToPageAndWait(ui::VKEY_DOWN); | 424 SendKeyToPageAndWait(ui::VKEY_DOWN); |
348 | 425 |
349 // Press the down arrow to select the suggestion and preview the autofilled | 426 // Press the down arrow to select the suggestion and preview the autofilled |
350 // form. | 427 // form. |
351 SendKeyToPopupAndWait(ui::VKEY_DOWN); | 428 SendKeyToPopupAndWait(ui::VKEY_DOWN); |
352 | 429 |
353 // Press tab to accept the autofill suggestions. | 430 // Press tab to accept the autofill suggestions. |
354 SendKeyToPopupAndWait(ui::VKEY_TAB); | 431 SendKeyToPopupAndWait(ui::VKEY_TAB); |
355 | 432 |
356 // The form should be filled. | 433 // The form should be filled. |
357 ExpectFilledTestForm(); | 434 ExpectFilledTestForm(); |
358 } | 435 } |
359 | 436 |
360 } // namespace autofill | 437 } // namespace autofill |
OLD | NEW |