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
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/autofill/personal_data_manager_factory.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/infobars/infobar_service.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/translate/chrome_translate_client.h"
23 #include "chrome/browser/translate/translate_service.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/render_messages.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "chrome/test/base/test_switches.h"
30 #include "chrome/test/base/ui_test_utils.h"
31 #include "components/autofill/content/browser/content_autofill_driver.h"
32 #include "components/autofill/core/browser/autofill_manager.h"
33 #include "components/autofill/core/browser/autofill_manager_test_delegate.h"
34 #include "components/autofill/core/browser/autofill_profile.h"
35 #include "components/autofill/core/browser/autofill_test_utils.h"
36 #include "components/autofill/core/browser/personal_data_manager.h"
37 #include "components/autofill/core/browser/personal_data_manager_observer.h"
38 #include "components/autofill/core/browser/validation.h"
39 #include "components/infobars/core/confirm_infobar_delegate.h"
40 #include "components/infobars/core/infobar.h"
41 #include "components/infobars/core/infobar_manager.h"
42 #include "components/translate/core/browser/translate_infobar_delegate.h"
43 #include "content/public/browser/navigation_controller.h"
44 #include "content/public/browser/notification_observer.h"
45 #include "content/public/browser/notification_registrar.h"
46 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/render_view_host.h"
48 #include "content/public/browser/render_widget_host.h"
49 #include "content/public/browser/web_contents.h"
50 #include "content/public/test/browser_test_utils.h"
51 #include "content/public/test/test_renderer_host.h"
52 #include "content/public/test/test_utils.h"
53 #include "net/url_request/test_url_fetcher_factory.h"
54 #include "testing/gmock/include/gmock/gmock.h"
55 #include "testing/gtest/include/gtest/gtest.h"
56 #include "ui/events/keycodes/keyboard_codes.h"
57
58 using base::ASCIIToUTF16;
59
60 namespace autofill {
61
62 static const char kDataURIPrefix[] = "data:text/html;charset=utf-8,";
63 static const char kTestFormString[] =
64 "<form action=\"http://www.example.com/\" method=\"POST\">"
65 "<label for=\"firstname\">First name:</label>"
66 " <input type=\"text\" id=\"firstname\""
67 " onfocus=\"domAutomationController.send(true)\"><br>"
68 "<label for=\"lastname\">Last name:</label>"
69 " <input type=\"text\" id=\"lastname\"><br>"
70 "<label for=\"address1\">Address line 1:</label>"
71 " <input type=\"text\" id=\"address1\"><br>"
72 "<label for=\"address2\">Address line 2:</label>"
73 " <input type=\"text\" id=\"address2\"><br>"
74 "<label for=\"city\">City:</label>"
75 " <input type=\"text\" id=\"city\"><br>"
76 "<label for=\"state\">State:</label>"
77 " <select id=\"state\">"
78 " <option value=\"\" selected=\"yes\">--</option>"
79 " <option value=\"CA\">California</option>"
80 " <option value=\"TX\">Texas</option>"
81 " </select><br>"
82 "<label for=\"zip\">ZIP code:</label>"
83 " <input type=\"text\" id=\"zip\"><br>"
84 "<label for=\"country\">Country:</label>"
85 " <select id=\"country\">"
86 " <option value=\"\" selected=\"yes\">--</option>"
87 " <option value=\"CA\">Canada</option>"
88 " <option value=\"US\">United States</option>"
89 " </select><br>"
90 "<label for=\"phone\">Phone number:</label>"
91 " <input type=\"text\" id=\"phone\"><br>"
92 "</form>";
93
94
95 // AutofillManagerTestDelegateImpl --------------------------------------------
96
97 class AutofillManagerTestDelegateImpl
98 : public autofill::AutofillManagerTestDelegate {
99 public:
AutofillManagerTestDelegateImpl()100 AutofillManagerTestDelegateImpl() {}
~AutofillManagerTestDelegateImpl()101 virtual ~AutofillManagerTestDelegateImpl() {}
102
103 // autofill::AutofillManagerTestDelegate:
DidPreviewFormData()104 virtual void DidPreviewFormData() OVERRIDE {
105 loop_runner_->Quit();
106 }
107
DidFillFormData()108 virtual void DidFillFormData() OVERRIDE {
109 loop_runner_->Quit();
110 }
111
DidShowSuggestions()112 virtual void DidShowSuggestions() OVERRIDE {
113 loop_runner_->Quit();
114 }
115
Reset()116 void Reset() {
117 loop_runner_ = new content::MessageLoopRunner();
118 }
119
Wait()120 void Wait() {
121 loop_runner_->Run();
122 }
123
124 private:
125 scoped_refptr<content::MessageLoopRunner> loop_runner_;
126
127 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl);
128 };
129
130
131 // WindowedPersonalDataManagerObserver ----------------------------------------
132
133 class WindowedPersonalDataManagerObserver
134 : public PersonalDataManagerObserver,
135 public infobars::InfoBarManager::Observer {
136 public:
WindowedPersonalDataManagerObserver(Browser * browser)137 explicit WindowedPersonalDataManagerObserver(Browser* browser)
138 : alerted_(false),
139 has_run_message_loop_(false),
140 browser_(browser),
141 infobar_service_(InfoBarService::FromWebContents(
142 browser_->tab_strip_model()->GetActiveWebContents())) {
143 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
144 AddObserver(this);
145 infobar_service_->AddObserver(this);
146 }
147
~WindowedPersonalDataManagerObserver()148 virtual ~WindowedPersonalDataManagerObserver() {
149 while (infobar_service_->infobar_count() > 0) {
150 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0));
151 }
152 infobar_service_->RemoveObserver(this);
153 }
154
155 // PersonalDataManagerObserver:
OnPersonalDataChanged()156 virtual void OnPersonalDataChanged() OVERRIDE {
157 if (has_run_message_loop_) {
158 base::MessageLoopForUI::current()->Quit();
159 has_run_message_loop_ = false;
160 }
161 alerted_ = true;
162 }
163
OnInsufficientFormData()164 virtual void OnInsufficientFormData() OVERRIDE {
165 OnPersonalDataChanged();
166 }
167
168
Wait()169 void Wait() {
170 if (!alerted_) {
171 has_run_message_loop_ = true;
172 content::RunMessageLoop();
173 }
174 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
175 RemoveObserver(this);
176 }
177
178 private:
179 // infobars::InfoBarManager::Observer:
OnInfoBarAdded(infobars::InfoBar * infobar)180 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) OVERRIDE {
181 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()->
182 Accept();
183 }
184
185 bool alerted_;
186 bool has_run_message_loop_;
187 Browser* browser_;
188 InfoBarService* infobar_service_;
189
190 DISALLOW_COPY_AND_ASSIGN(WindowedPersonalDataManagerObserver);
191 };
192
193 // AutofillInteractiveTest ----------------------------------------------------
194
195 class AutofillInteractiveTest : public InProcessBrowserTest {
196 protected:
AutofillInteractiveTest()197 AutofillInteractiveTest() :
198 key_press_event_sink_(
199 base::Bind(&AutofillInteractiveTest::HandleKeyPressEvent,
200 base::Unretained(this))) {}
~AutofillInteractiveTest()201 virtual ~AutofillInteractiveTest() {}
202
203 // InProcessBrowserTest:
SetUpOnMainThread()204 virtual void SetUpOnMainThread() OVERRIDE {
205 // Don't want Keychain coming up on Mac.
206 test::DisableSystemServices(browser()->profile()->GetPrefs());
207
208 // Inject the test delegate into the AutofillManager.
209 content::WebContents* web_contents = GetWebContents();
210 ContentAutofillDriver* autofill_driver =
211 ContentAutofillDriver::FromWebContents(web_contents);
212 AutofillManager* autofill_manager = autofill_driver->autofill_manager();
213 autofill_manager->SetTestDelegate(&test_delegate_);
214 }
215
CleanUpOnMainThread()216 virtual void CleanUpOnMainThread() OVERRIDE {
217 // Make sure to close any showing popups prior to tearing down the UI.
218 content::WebContents* web_contents = GetWebContents();
219 AutofillManager* autofill_manager = ContentAutofillDriver::FromWebContents(
220 web_contents)->autofill_manager();
221 autofill_manager->client()->HideAutofillPopup();
222 }
223
GetPersonalDataManager()224 PersonalDataManager* GetPersonalDataManager() {
225 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
226 }
227
GetWebContents()228 content::WebContents* GetWebContents() {
229 return browser()->tab_strip_model()->GetActiveWebContents();
230 }
231
GetRenderViewHost()232 content::RenderViewHost* GetRenderViewHost() {
233 return GetWebContents()->GetRenderViewHost();
234 }
235
CreateTestProfile()236 void CreateTestProfile() {
237 AutofillProfile profile;
238 test::SetProfileInfo(
239 &profile, "Milton", "C.", "Waddams",
240 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane",
241 "Basement", "Austin", "Texas", "78744", "US", "5125551234");
242
243 WindowedPersonalDataManagerObserver observer(browser());
244 GetPersonalDataManager()->AddProfile(profile);
245
246 // AddProfile is asynchronous. Wait for it to finish before continuing the
247 // tests.
248 observer.Wait();
249 }
250
SetProfiles(std::vector<AutofillProfile> * profiles)251 void SetProfiles(std::vector<AutofillProfile>* profiles) {
252 WindowedPersonalDataManagerObserver observer(browser());
253 GetPersonalDataManager()->SetProfiles(profiles);
254 observer.Wait();
255 }
256
SetProfile(const AutofillProfile & profile)257 void SetProfile(const AutofillProfile& profile) {
258 std::vector<AutofillProfile> profiles;
259 profiles.push_back(profile);
260 SetProfiles(&profiles);
261 }
262
263 // Populates a webpage form using autofill data and keypress events.
264 // This function focuses the specified input field in the form, and then
265 // sends keypress events to the tab to cause the form to be populated.
PopulateForm(const std::string & field_id)266 void PopulateForm(const std::string& field_id) {
267 std::string js("document.getElementById('" + field_id + "').focus();");
268 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js));
269
270 SendKeyToPageAndWait(ui::VKEY_DOWN);
271 SendKeyToPopupAndWait(ui::VKEY_DOWN);
272 SendKeyToPopupAndWait(ui::VKEY_RETURN);
273 }
274
ExpectFieldValue(const std::string & field_name,const std::string & expected_value)275 void ExpectFieldValue(const std::string& field_name,
276 const std::string& expected_value) {
277 std::string value;
278 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
279 GetWebContents(),
280 "window.domAutomationController.send("
281 " document.getElementById('" + field_name + "').value);",
282 &value));
283 EXPECT_EQ(expected_value, value);
284 }
285
SimulateURLFetch(bool success)286 void SimulateURLFetch(bool success) {
287 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
288 ASSERT_TRUE(fetcher);
289 net::URLRequestStatus status;
290 status.set_status(success ? net::URLRequestStatus::SUCCESS :
291 net::URLRequestStatus::FAILED);
292
293 std::string script = " var google = {};"
294 "google.translate = (function() {"
295 " return {"
296 " TranslateService: function() {"
297 " return {"
298 " isAvailable : function() {"
299 " return true;"
300 " },"
301 " restore : function() {"
302 " return;"
303 " },"
304 " getDetectedLanguage : function() {"
305 " return \"ja\";"
306 " },"
307 " translatePage : function(originalLang, targetLang,"
308 " onTranslateProgress) {"
309 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
310 std::string(kTestFormString) +
311 " ';"
312 " onTranslateProgress(100, true, false);"
313 " }"
314 " };"
315 " }"
316 " };"
317 "})();"
318 "cr.googleTranslate.onTranslateElementLoad();";
319
320 fetcher->set_url(fetcher->GetOriginalURL());
321 fetcher->set_status(status);
322 fetcher->set_response_code(success ? 200 : 500);
323 fetcher->SetResponseString(script);
324 fetcher->delegate()->OnURLFetchComplete(fetcher);
325 }
326
FocusFirstNameField()327 void FocusFirstNameField() {
328 bool result = false;
329 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
330 GetRenderViewHost(),
331 "if (document.readyState === 'complete')"
332 " document.getElementById('firstname').focus();"
333 "else"
334 " domAutomationController.send(false);",
335 &result));
336 ASSERT_TRUE(result);
337 }
338
ExpectFilledTestForm()339 void ExpectFilledTestForm() {
340 ExpectFieldValue("firstname", "Milton");
341 ExpectFieldValue("lastname", "Waddams");
342 ExpectFieldValue("address1", "4120 Freidrich Lane");
343 ExpectFieldValue("address2", "Basement");
344 ExpectFieldValue("city", "Austin");
345 ExpectFieldValue("state", "TX");
346 ExpectFieldValue("zip", "78744");
347 ExpectFieldValue("country", "US");
348 ExpectFieldValue("phone", "5125551234");
349 }
350
SendKeyToPageAndWait(ui::KeyboardCode key)351 void SendKeyToPageAndWait(ui::KeyboardCode key) {
352 test_delegate_.Reset();
353 content::SimulateKeyPress(
354 GetWebContents(), key, false, false, false, false);
355 test_delegate_.Wait();
356 }
357
HandleKeyPressEvent(const content::NativeWebKeyboardEvent & event)358 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) {
359 return true;
360 }
361
SendKeyToPopupAndWait(ui::KeyboardCode key)362 void SendKeyToPopupAndWait(ui::KeyboardCode key) {
363 // Route popup-targeted key presses via the render view host.
364 content::NativeWebKeyboardEvent event;
365 event.windowsKeyCode = key;
366 event.type = blink::WebKeyboardEvent::RawKeyDown;
367 test_delegate_.Reset();
368 // Install the key press event sink to ensure that any events that are not
369 // handled by the installed callbacks do not end up crashing the test.
370 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
371 GetRenderViewHost()->ForwardKeyboardEvent(event);
372 test_delegate_.Wait();
373 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
374 }
375
TryBasicFormFill()376 void TryBasicFormFill() {
377 FocusFirstNameField();
378
379 // Start filling the first name field with "M" and wait for the popup to be
380 // shown.
381 SendKeyToPageAndWait(ui::VKEY_M);
382
383 // Press the down arrow to select the suggestion and preview the autofilled
384 // form.
385 SendKeyToPopupAndWait(ui::VKEY_DOWN);
386
387 // The previewed values should not be accessible to JavaScript.
388 ExpectFieldValue("firstname", "M");
389 ExpectFieldValue("lastname", std::string());
390 ExpectFieldValue("address1", std::string());
391 ExpectFieldValue("address2", std::string());
392 ExpectFieldValue("city", std::string());
393 ExpectFieldValue("state", std::string());
394 ExpectFieldValue("zip", std::string());
395 ExpectFieldValue("country", std::string());
396 ExpectFieldValue("phone", std::string());
397 // TODO(isherman): It would be nice to test that the previewed values are
398 // displayed: http://crbug.com/57220
399
400 // Press Enter to accept the autofill suggestions.
401 SendKeyToPopupAndWait(ui::VKEY_RETURN);
402
403 // The form should be filled.
404 ExpectFilledTestForm();
405 }
406
407 private:
408 AutofillManagerTestDelegateImpl test_delegate_;
409
410 net::TestURLFetcherFactory url_fetcher_factory_;
411
412 // KeyPressEventCallback that serves as a sink to ensure that every key press
413 // event the tests create and have the WebContents forward is handled by some
414 // key press event callback. It is necessary to have this sinkbecause if no
415 // key press event callback handles the event (at least on Mac), a DCHECK
416 // ends up going off that the |event| doesn't have an |os_event| associated
417 // with it.
418 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_;
419
420 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest);
421 };
422
423 // Test that basic form fill is working.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,BasicFormFill)424 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) {
425 CreateTestProfile();
426
427 // Load the test page.
428 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
429 GURL(std::string(kDataURIPrefix) + kTestFormString)));
430
431 // Invoke Autofill.
432 TryBasicFormFill();
433 }
434
435 // Test that form filling can be initiated by pressing the down arrow.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillViaDownArrow)436 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) {
437 CreateTestProfile();
438
439 // Load the test page.
440 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
441 GURL(std::string(kDataURIPrefix) + kTestFormString)));
442
443 // Focus a fillable field.
444 FocusFirstNameField();
445
446 // Press the down arrow to initiate Autofill and wait for the popup to be
447 // shown.
448 SendKeyToPageAndWait(ui::VKEY_DOWN);
449
450 // Press the down arrow to select the suggestion and preview the autofilled
451 // form.
452 SendKeyToPopupAndWait(ui::VKEY_DOWN);
453
454 // Press Enter to accept the autofill suggestions.
455 SendKeyToPopupAndWait(ui::VKEY_RETURN);
456
457 // The form should be filled.
458 ExpectFilledTestForm();
459 }
460
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillSelectViaTab)461 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillSelectViaTab) {
462 CreateTestProfile();
463
464 // Load the test page.
465 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
466 GURL(std::string(kDataURIPrefix) + kTestFormString)));
467
468 // Focus a fillable field.
469 FocusFirstNameField();
470
471 // Press the down arrow to initiate Autofill and wait for the popup to be
472 // shown.
473 SendKeyToPageAndWait(ui::VKEY_DOWN);
474
475 // Press the down arrow to select the suggestion and preview the autofilled
476 // form.
477 SendKeyToPopupAndWait(ui::VKEY_DOWN);
478
479 // Press tab to accept the autofill suggestions.
480 SendKeyToPopupAndWait(ui::VKEY_TAB);
481
482 // The form should be filled.
483 ExpectFilledTestForm();
484 }
485
486 // Test that a field is still autofillable after the previously autofilled
487 // value is deleted.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,OnDeleteValueAfterAutofill)488 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) {
489 CreateTestProfile();
490
491 // Load the test page.
492 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
493 GURL(std::string(kDataURIPrefix) + kTestFormString)));
494
495 // Invoke and accept the Autofill popup and verify the form was filled.
496 FocusFirstNameField();
497 SendKeyToPageAndWait(ui::VKEY_M);
498 SendKeyToPopupAndWait(ui::VKEY_DOWN);
499 SendKeyToPopupAndWait(ui::VKEY_RETURN);
500 ExpectFilledTestForm();
501
502 // Delete the value of a filled field.
503 ASSERT_TRUE(content::ExecuteScript(
504 GetRenderViewHost(),
505 "document.getElementById('firstname').value = '';"));
506 ExpectFieldValue("firstname", "");
507
508 // Invoke and accept the Autofill popup and verify the field was filled.
509 SendKeyToPageAndWait(ui::VKEY_M);
510 SendKeyToPopupAndWait(ui::VKEY_DOWN);
511 SendKeyToPopupAndWait(ui::VKEY_RETURN);
512 ExpectFieldValue("firstname", "Milton");
513 }
514
515 // Test that a JavaScript oninput event is fired after auto-filling a form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,OnInputAfterAutofill)516 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) {
517 CreateTestProfile();
518
519 const char kOnInputScript[] =
520 "<script>"
521 "focused_fired = false;"
522 "unfocused_fired = false;"
523 "changed_select_fired = false;"
524 "unchanged_select_fired = false;"
525 "document.getElementById('firstname').oninput = function() {"
526 " focused_fired = true;"
527 "};"
528 "document.getElementById('lastname').oninput = function() {"
529 " unfocused_fired = true;"
530 "};"
531 "document.getElementById('state').oninput = function() {"
532 " changed_select_fired = true;"
533 "};"
534 "document.getElementById('country').oninput = function() {"
535 " unchanged_select_fired = true;"
536 "};"
537 "document.getElementById('country').value = 'US';"
538 "</script>";
539
540 // Load the test page.
541 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
542 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnInputScript)));
543
544 // Invoke Autofill.
545 FocusFirstNameField();
546
547 // Start filling the first name field with "M" and wait for the popup to be
548 // shown.
549 SendKeyToPageAndWait(ui::VKEY_M);
550
551 // Press the down arrow to select the suggestion and preview the autofilled
552 // form.
553 SendKeyToPopupAndWait(ui::VKEY_DOWN);
554
555 // Press Enter to accept the autofill suggestions.
556 SendKeyToPopupAndWait(ui::VKEY_RETURN);
557
558 // The form should be filled.
559 ExpectFilledTestForm();
560
561 bool focused_fired = false;
562 bool unfocused_fired = false;
563 bool changed_select_fired = false;
564 bool unchanged_select_fired = false;
565 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
566 GetRenderViewHost(),
567 "domAutomationController.send(focused_fired);",
568 &focused_fired));
569 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
570 GetRenderViewHost(),
571 "domAutomationController.send(unfocused_fired);",
572 &unfocused_fired));
573 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
574 GetRenderViewHost(),
575 "domAutomationController.send(changed_select_fired);",
576 &changed_select_fired));
577 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
578 GetRenderViewHost(),
579 "domAutomationController.send(unchanged_select_fired);",
580 &unchanged_select_fired));
581 EXPECT_TRUE(focused_fired);
582 EXPECT_TRUE(unfocused_fired);
583 EXPECT_TRUE(changed_select_fired);
584 EXPECT_FALSE(unchanged_select_fired);
585 }
586
587 // Test that a JavaScript onchange event is fired after auto-filling a form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,OnChangeAfterAutofill)588 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) {
589 CreateTestProfile();
590
591 const char kOnChangeScript[] =
592 "<script>"
593 "focused_fired = false;"
594 "unfocused_fired = false;"
595 "changed_select_fired = false;"
596 "unchanged_select_fired = false;"
597 "document.getElementById('firstname').onchange = function() {"
598 " focused_fired = true;"
599 "};"
600 "document.getElementById('lastname').onchange = function() {"
601 " unfocused_fired = true;"
602 "};"
603 "document.getElementById('state').onchange = function() {"
604 " changed_select_fired = true;"
605 "};"
606 "document.getElementById('country').onchange = function() {"
607 " unchanged_select_fired = true;"
608 "};"
609 "document.getElementById('country').value = 'US';"
610 "</script>";
611
612 // Load the test page.
613 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
614 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnChangeScript)));
615
616 // Invoke Autofill.
617 FocusFirstNameField();
618
619 // Start filling the first name field with "M" and wait for the popup to be
620 // shown.
621 SendKeyToPageAndWait(ui::VKEY_M);
622
623 // Press the down arrow to select the suggestion and preview the autofilled
624 // form.
625 SendKeyToPopupAndWait(ui::VKEY_DOWN);
626
627 // Press Enter to accept the autofill suggestions.
628 SendKeyToPopupAndWait(ui::VKEY_RETURN);
629
630 // The form should be filled.
631 ExpectFilledTestForm();
632
633 bool focused_fired = false;
634 bool unfocused_fired = false;
635 bool changed_select_fired = false;
636 bool unchanged_select_fired = false;
637 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
638 GetRenderViewHost(),
639 "domAutomationController.send(focused_fired);",
640 &focused_fired));
641 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
642 GetRenderViewHost(),
643 "domAutomationController.send(unfocused_fired);",
644 &unfocused_fired));
645 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
646 GetRenderViewHost(),
647 "domAutomationController.send(changed_select_fired);",
648 &changed_select_fired));
649 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
650 GetRenderViewHost(),
651 "domAutomationController.send(unchanged_select_fired);",
652 &unchanged_select_fired));
653 EXPECT_TRUE(focused_fired);
654 EXPECT_TRUE(unfocused_fired);
655 EXPECT_TRUE(changed_select_fired);
656 EXPECT_FALSE(unchanged_select_fired);
657 }
658
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,InputFiresBeforeChange)659 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) {
660 CreateTestProfile();
661
662 const char kInputFiresBeforeChangeScript[] =
663 "<script>"
664 "inputElementEvents = [];"
665 "function recordInputElementEvent(e) {"
666 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';"
667 " inputElementEvents.push(e.type);"
668 "}"
669 "selectElementEvents = [];"
670 "function recordSelectElementEvent(e) {"
671 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';"
672 " selectElementEvents.push(e.type);"
673 "}"
674 "document.getElementById('lastname').oninput = recordInputElementEvent;"
675 "document.getElementById('lastname').onchange = recordInputElementEvent;"
676 "document.getElementById('country').oninput = recordSelectElementEvent;"
677 "document.getElementById('country').onchange = recordSelectElementEvent;"
678 "</script>";
679
680 // Load the test page.
681 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
682 GURL(std::string(kDataURIPrefix) + kTestFormString +
683 kInputFiresBeforeChangeScript)));
684
685 // Invoke and accept the Autofill popup and verify the form was filled.
686 FocusFirstNameField();
687 SendKeyToPageAndWait(ui::VKEY_M);
688 SendKeyToPopupAndWait(ui::VKEY_DOWN);
689 SendKeyToPopupAndWait(ui::VKEY_RETURN);
690 ExpectFilledTestForm();
691
692 int num_input_element_events = -1;
693 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
694 GetRenderViewHost(),
695 "domAutomationController.send(inputElementEvents.length);",
696 &num_input_element_events));
697 EXPECT_EQ(2, num_input_element_events);
698
699 std::vector<std::string> input_element_events;
700 input_element_events.resize(2);
701
702 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
703 GetRenderViewHost(),
704 "domAutomationController.send(inputElementEvents[0]);",
705 &input_element_events[0]));
706 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
707 GetRenderViewHost(),
708 "domAutomationController.send(inputElementEvents[1]);",
709 &input_element_events[1]));
710
711 EXPECT_EQ("input", input_element_events[0]);
712 EXPECT_EQ("change", input_element_events[1]);
713
714 int num_select_element_events = -1;
715 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
716 GetRenderViewHost(),
717 "domAutomationController.send(selectElementEvents.length);",
718 &num_select_element_events));
719 EXPECT_EQ(2, num_select_element_events);
720
721 std::vector<std::string> select_element_events;
722 select_element_events.resize(2);
723
724 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
725 GetRenderViewHost(),
726 "domAutomationController.send(selectElementEvents[0]);",
727 &select_element_events[0]));
728 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
729 GetRenderViewHost(),
730 "domAutomationController.send(selectElementEvents[1]);",
731 &select_element_events[1]));
732
733 EXPECT_EQ("input", select_element_events[0]);
734 EXPECT_EQ("change", select_element_events[1]);
735 }
736
737 // Test that we can autofill forms distinguished only by their |id| attribute.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillFormsDistinguishedById)738 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
739 AutofillFormsDistinguishedById) {
740 CreateTestProfile();
741
742 // Load the test page.
743 const std::string kURL =
744 std::string(kDataURIPrefix) + kTestFormString +
745 "<script>"
746 "var mainForm = document.forms[0];"
747 "mainForm.id = 'mainForm';"
748 "var newForm = document.createElement('form');"
749 "newForm.action = mainForm.action;"
750 "newForm.method = mainForm.method;"
751 "newForm.id = 'newForm';"
752 "mainForm.parentNode.insertBefore(newForm, mainForm);"
753 "</script>";
754 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL)));
755
756 // Invoke Autofill.
757 TryBasicFormFill();
758 }
759
760 // Test that we properly autofill forms with repeated fields.
761 // In the wild, the repeated fields are typically either email fields
762 // (duplicated for "confirmation"); or variants that are hot-swapped via
763 // JavaScript, with only one actually visible at any given time.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillFormWithRepeatedField)764 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) {
765 CreateTestProfile();
766
767 // Load the test page.
768 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
769 GURL(std::string(kDataURIPrefix) +
770 "<form action=\"http://www.example.com/\" method=\"POST\">"
771 "<label for=\"firstname\">First name:</label>"
772 " <input type=\"text\" id=\"firstname\""
773 " onfocus=\"domAutomationController.send(true)\"><br>"
774 "<label for=\"lastname\">Last name:</label>"
775 " <input type=\"text\" id=\"lastname\"><br>"
776 "<label for=\"address1\">Address line 1:</label>"
777 " <input type=\"text\" id=\"address1\"><br>"
778 "<label for=\"address2\">Address line 2:</label>"
779 " <input type=\"text\" id=\"address2\"><br>"
780 "<label for=\"city\">City:</label>"
781 " <input type=\"text\" id=\"city\"><br>"
782 "<label for=\"state\">State:</label>"
783 " <select id=\"state\">"
784 " <option value=\"\" selected=\"yes\">--</option>"
785 " <option value=\"CA\">California</option>"
786 " <option value=\"TX\">Texas</option>"
787 " </select><br>"
788 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
789 " <input type=\"text\" id=\"state_freeform\""
790 " style=\"display:none\"><br>"
791 "<label for=\"zip\">ZIP code:</label>"
792 " <input type=\"text\" id=\"zip\"><br>"
793 "<label for=\"country\">Country:</label>"
794 " <select id=\"country\">"
795 " <option value=\"\" selected=\"yes\">--</option>"
796 " <option value=\"CA\">Canada</option>"
797 " <option value=\"US\">United States</option>"
798 " </select><br>"
799 "<label for=\"phone\">Phone number:</label>"
800 " <input type=\"text\" id=\"phone\"><br>"
801 "</form>")));
802
803 // Invoke Autofill.
804 TryBasicFormFill();
805 ExpectFieldValue("state_freeform", std::string());
806 }
807
808 // Test that we properly autofill forms with non-autofillable fields.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillFormWithNonAutofillableField)809 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
810 AutofillFormWithNonAutofillableField) {
811 CreateTestProfile();
812
813 // Load the test page.
814 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
815 GURL(std::string(kDataURIPrefix) +
816 "<form action=\"http://www.example.com/\" method=\"POST\">"
817 "<label for=\"firstname\">First name:</label>"
818 " <input type=\"text\" id=\"firstname\""
819 " onfocus=\"domAutomationController.send(true)\"><br>"
820 "<label for=\"middlename\">Middle name:</label>"
821 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
822 "<label for=\"lastname\">Last name:</label>"
823 " <input type=\"text\" id=\"lastname\"><br>"
824 "<label for=\"address1\">Address line 1:</label>"
825 " <input type=\"text\" id=\"address1\"><br>"
826 "<label for=\"address2\">Address line 2:</label>"
827 " <input type=\"text\" id=\"address2\"><br>"
828 "<label for=\"city\">City:</label>"
829 " <input type=\"text\" id=\"city\"><br>"
830 "<label for=\"state\">State:</label>"
831 " <select id=\"state\">"
832 " <option value=\"\" selected=\"yes\">--</option>"
833 " <option value=\"CA\">California</option>"
834 " <option value=\"TX\">Texas</option>"
835 " </select><br>"
836 "<label for=\"zip\">ZIP code:</label>"
837 " <input type=\"text\" id=\"zip\"><br>"
838 "<label for=\"country\">Country:</label>"
839 " <select id=\"country\">"
840 " <option value=\"\" selected=\"yes\">--</option>"
841 " <option value=\"CA\">Canada</option>"
842 " <option value=\"US\">United States</option>"
843 " </select><br>"
844 "<label for=\"phone\">Phone number:</label>"
845 " <input type=\"text\" id=\"phone\"><br>"
846 "</form>")));
847
848 // Invoke Autofill.
849 TryBasicFormFill();
850 }
851
852 // Test that we can Autofill dynamically generated forms.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,DynamicFormFill)853 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) {
854 CreateTestProfile();
855
856 // Load the test page.
857 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
858 GURL(std::string(kDataURIPrefix) +
859 "<form id=\"form\" action=\"http://www.example.com/\""
860 " method=\"POST\"></form>"
861 "<script>"
862 "function AddElement(name, label) {"
863 " var form = document.getElementById('form');"
864 ""
865 " var label_text = document.createTextNode(label);"
866 " var label_element = document.createElement('label');"
867 " label_element.setAttribute('for', name);"
868 " label_element.appendChild(label_text);"
869 " form.appendChild(label_element);"
870 ""
871 " if (name === 'state' || name === 'country') {"
872 " var select_element = document.createElement('select');"
873 " select_element.setAttribute('id', name);"
874 " select_element.setAttribute('name', name);"
875 ""
876 " /* Add an empty selected option. */"
877 " var default_option = new Option('--', '', true);"
878 " select_element.appendChild(default_option);"
879 ""
880 " /* Add the other options. */"
881 " if (name == 'state') {"
882 " var option1 = new Option('California', 'CA');"
883 " select_element.appendChild(option1);"
884 " var option2 = new Option('Texas', 'TX');"
885 " select_element.appendChild(option2);"
886 " } else {"
887 " var option1 = new Option('Canada', 'CA');"
888 " select_element.appendChild(option1);"
889 " var option2 = new Option('United States', 'US');"
890 " select_element.appendChild(option2);"
891 " }"
892 ""
893 " form.appendChild(select_element);"
894 " } else {"
895 " var input_element = document.createElement('input');"
896 " input_element.setAttribute('id', name);"
897 " input_element.setAttribute('name', name);"
898 ""
899 " /* Add the onfocus listener to the 'firstname' field. */"
900 " if (name === 'firstname') {"
901 " input_element.onfocus = function() {"
902 " domAutomationController.send(true);"
903 " };"
904 " }"
905 ""
906 " form.appendChild(input_element);"
907 " }"
908 ""
909 " form.appendChild(document.createElement('br'));"
910 "};"
911 ""
912 "function BuildForm() {"
913 " var elements = ["
914 " ['firstname', 'First name:'],"
915 " ['lastname', 'Last name:'],"
916 " ['address1', 'Address line 1:'],"
917 " ['address2', 'Address line 2:'],"
918 " ['city', 'City:'],"
919 " ['state', 'State:'],"
920 " ['zip', 'ZIP code:'],"
921 " ['country', 'Country:'],"
922 " ['phone', 'Phone number:'],"
923 " ];"
924 ""
925 " for (var i = 0; i < elements.length; i++) {"
926 " var name = elements[i][0];"
927 " var label = elements[i][1];"
928 " AddElement(name, label);"
929 " }"
930 "};"
931 "</script>")));
932
933 // Dynamically construct the form.
934 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
935
936 // Invoke Autofill.
937 TryBasicFormFill();
938 }
939
940 // Test that form filling works after reloading the current page.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillAfterReload)941 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) {
942 CreateTestProfile();
943
944 // Load the test page.
945 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
946 GURL(std::string(kDataURIPrefix) + kTestFormString)));
947
948 // Reload the page.
949 content::WebContents* web_contents = GetWebContents();
950 web_contents->GetController().Reload(false);
951 content::WaitForLoadStop(web_contents);
952
953 // Invoke Autofill.
954 TryBasicFormFill();
955 }
956
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,AutofillAfterTranslate)957 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterTranslate) {
958 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
959 if (TranslateService::IsTranslateBubbleEnabled())
960 return;
961
962 CreateTestProfile();
963
964 GURL url(std::string(kDataURIPrefix) +
965 "<form action=\"http://www.example.com/\" method=\"POST\">"
966 "<label for=\"fn\">なまえ</label>"
967 " <input type=\"text\" id=\"fn\""
968 " onfocus=\"domAutomationController.send(true)\""
969 "><br>"
970 "<label for=\"ln\">みょうじ</label>"
971 " <input type=\"text\" id=\"ln\"><br>"
972 "<label for=\"a1\">Address line 1:</label>"
973 " <input type=\"text\" id=\"a1\"><br>"
974 "<label for=\"a2\">Address line 2:</label>"
975 " <input type=\"text\" id=\"a2\"><br>"
976 "<label for=\"ci\">City:</label>"
977 " <input type=\"text\" id=\"ci\"><br>"
978 "<label for=\"st\">State:</label>"
979 " <select id=\"st\">"
980 " <option value=\"\" selected=\"yes\">--</option>"
981 " <option value=\"CA\">California</option>"
982 " <option value=\"TX\">Texas</option>"
983 " </select><br>"
984 "<label for=\"z\">ZIP code:</label>"
985 " <input type=\"text\" id=\"z\"><br>"
986 "<label for=\"co\">Country:</label>"
987 " <select id=\"co\">"
988 " <option value=\"\" selected=\"yes\">--</option>"
989 " <option value=\"CA\">Canada</option>"
990 " <option value=\"US\">United States</option>"
991 " </select><br>"
992 "<label for=\"ph\">Phone number:</label>"
993 " <input type=\"text\" id=\"ph\"><br>"
994 "</form>"
995 // Add additional Japanese characters to ensure the translate bar
996 // will appear.
997 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
998 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
999
1000 content::WindowedNotificationObserver infobar_observer(
1001 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
1002 content::NotificationService::AllSources());
1003 ASSERT_NO_FATAL_FAILURE(
1004 ui_test_utils::NavigateToURL(browser(), url));
1005
1006 // Wait for the translation bar to appear and get it.
1007 infobar_observer.Wait();
1008 InfoBarService* infobar_service =
1009 InfoBarService::FromWebContents(GetWebContents());
1010 TranslateInfoBarDelegate* delegate =
1011 infobar_service->infobar_at(0)->delegate()->AsTranslateInfoBarDelegate();
1012 ASSERT_TRUE(delegate);
1013 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1014 delegate->translate_step());
1015
1016 // Simulate translation button press.
1017 delegate->Translate();
1018
1019 content::WindowedNotificationObserver translation_observer(
1020 chrome::NOTIFICATION_PAGE_TRANSLATED,
1021 content::NotificationService::AllSources());
1022
1023 // Simulate the translate script being retrieved.
1024 // Pass fake google.translate lib as the translate script.
1025 SimulateURLFetch(true);
1026
1027 // Simulate the render notifying the translation has been done.
1028 translation_observer.Wait();
1029
1030 TryBasicFormFill();
1031 }
1032
1033 // Test phone fields parse correctly from a given profile.
1034 // The high level key presses execute the following: Select the first text
1035 // field, invoke the autofill popup list, select the first profile within the
1036 // list, and commit to the profile to populate the form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,ComparePhoneNumbers)1037 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, ComparePhoneNumbers) {
1038 ASSERT_TRUE(test_server()->Start());
1039
1040 AutofillProfile profile;
1041 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1042 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1043 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 H St."));
1044 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1045 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1046 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1047 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1-408-555-4567"));
1048 SetProfile(profile);
1049
1050 GURL url = test_server()->GetURL("files/autofill/form_phones.html");
1051 ui_test_utils::NavigateToURL(browser(), url);
1052 PopulateForm("NAME_FIRST");
1053
1054 ExpectFieldValue("NAME_FIRST", "Bob");
1055 ExpectFieldValue("NAME_LAST", "Smith");
1056 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
1057 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
1058 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
1059 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
1060 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
1061 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
1062 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
1063 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
1064 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
1065 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
1066 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
1067 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
1068 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
1069 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
1070 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
1071 }
1072
1073 // Test that Autofill does not fill in read-only fields.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,NoAutofillForReadOnlyFields)1074 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, NoAutofillForReadOnlyFields) {
1075 ASSERT_TRUE(test_server()->Start());
1076
1077 std::string addr_line1("1234 H St.");
1078
1079 AutofillProfile profile;
1080 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1081 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1082 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("bsmith@gmail.com"));
1083 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
1084 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1085 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1086 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1087 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Company X"));
1088 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("408-871-4567"));
1089 SetProfile(profile);
1090
1091 GURL url = test_server()->GetURL("files/autofill/read_only_field_test.html");
1092 ui_test_utils::NavigateToURL(browser(), url);
1093 PopulateForm("firstname");
1094
1095 ExpectFieldValue("email", std::string());
1096 ExpectFieldValue("address", addr_line1);
1097 }
1098
1099 // Test form is fillable from a profile after form was reset.
1100 // Steps:
1101 // 1. Fill form using a saved profile.
1102 // 2. Reset the form.
1103 // 3. Fill form using a saved profile.
1104 // Flakily times out: http://crbug.com/270341
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,DISABLED_FormFillableOnReset)1105 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_FormFillableOnReset) {
1106 ASSERT_TRUE(test_server()->Start());
1107
1108 CreateTestProfile();
1109
1110 GURL url = test_server()->GetURL("files/autofill/autofill_test_form.html");
1111 ui_test_utils::NavigateToURL(browser(), url);
1112 PopulateForm("NAME_FIRST");
1113
1114 ASSERT_TRUE(content::ExecuteScript(
1115 GetWebContents(), "document.getElementById('testform').reset()"));
1116
1117 PopulateForm("NAME_FIRST");
1118
1119 ExpectFieldValue("NAME_FIRST", "Milton");
1120 ExpectFieldValue("NAME_LAST", "Waddams");
1121 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
1122 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
1123 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
1124 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
1125 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
1126 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
1127 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
1128 }
1129
1130 // Test Autofill distinguishes a middle initial in a name.
1131 // Flakily times out: http://crbug.com/270341
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,DISABLED_DistinguishMiddleInitialWithinName)1132 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1133 DISABLED_DistinguishMiddleInitialWithinName) {
1134 ASSERT_TRUE(test_server()->Start());
1135
1136 CreateTestProfile();
1137
1138 GURL url = test_server()->GetURL(
1139 "files/autofill/autofill_middleinit_form.html");
1140 ui_test_utils::NavigateToURL(browser(), url);
1141 PopulateForm("NAME_FIRST");
1142
1143 ExpectFieldValue("NAME_MIDDLE", "C");
1144 }
1145
1146 // Test forms with multiple email addresses are filled properly.
1147 // Entire form should be filled with one user gesture.
1148 // Flakily times out: http://crbug.com/270341
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,DISABLED_MultipleEmailFilledByOneUserGesture)1149 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1150 DISABLED_MultipleEmailFilledByOneUserGesture) {
1151 ASSERT_TRUE(test_server()->Start());
1152
1153 std::string email("bsmith@gmail.com");
1154
1155 AutofillProfile profile;
1156 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1157 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1158 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email));
1159 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("4088714567"));
1160 SetProfile(profile);
1161
1162 GURL url = test_server()->GetURL(
1163 "files/autofill/autofill_confirmemail_form.html");
1164 ui_test_utils::NavigateToURL(browser(), url);
1165 PopulateForm("NAME_FIRST");
1166
1167 ExpectFieldValue("EMAIL_CONFIRM", email);
1168 // TODO(isherman): verify entire form.
1169 }
1170
1171 // http://crbug.com/281527
1172 #if defined(OS_MACOSX)
1173 #define MAYBE_FormFillLatencyAfterSubmit FormFillLatencyAfterSubmit
1174 #else
1175 #define MAYBE_FormFillLatencyAfterSubmit DISABLED_FormFillLatencyAfterSubmit
1176 #endif
1177 // Test latency time on form submit with lots of stored Autofill profiles.
1178 // This test verifies when a profile is selected from the Autofill dictionary
1179 // that consists of thousands of profiles, the form does not hang after being
1180 // submitted.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,MAYBE_FormFillLatencyAfterSubmit)1181 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1182 MAYBE_FormFillLatencyAfterSubmit) {
1183 ASSERT_TRUE(test_server()->Start());
1184
1185 std::vector<std::string> cities;
1186 cities.push_back("San Jose");
1187 cities.push_back("San Francisco");
1188 cities.push_back("Sacramento");
1189 cities.push_back("Los Angeles");
1190
1191 std::vector<std::string> streets;
1192 streets.push_back("St");
1193 streets.push_back("Ave");
1194 streets.push_back("Ln");
1195 streets.push_back("Ct");
1196
1197 const int kNumProfiles = 1500;
1198 base::Time start_time = base::Time::Now();
1199 std::vector<AutofillProfile> profiles;
1200 for (int i = 0; i < kNumProfiles; i++) {
1201 AutofillProfile profile;
1202 base::string16 name(base::IntToString16(i));
1203 base::string16 email(name + ASCIIToUTF16("@example.com"));
1204 base::string16 street = ASCIIToUTF16(
1205 base::IntToString(base::RandInt(0, 10000)) + " " +
1206 streets[base::RandInt(0, streets.size() - 1)]);
1207 base::string16 city =
1208 ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]);
1209 base::string16 zip(base::IntToString16(base::RandInt(0, 10000)));
1210 profile.SetRawInfo(NAME_FIRST, name);
1211 profile.SetRawInfo(EMAIL_ADDRESS, email);
1212 profile.SetRawInfo(ADDRESS_HOME_LINE1, street);
1213 profile.SetRawInfo(ADDRESS_HOME_CITY, city);
1214 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1215 profile.SetRawInfo(ADDRESS_HOME_ZIP, zip);
1216 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1217 profiles.push_back(profile);
1218 }
1219 SetProfiles(&profiles);
1220 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1221 // can be removd.
1222 LOG(INFO) << "Created " << kNumProfiles << " profiles in " <<
1223 (base::Time::Now() - start_time).InSeconds() << " seconds.";
1224
1225 GURL url = test_server()->GetURL(
1226 "files/autofill/latency_after_submit_test.html");
1227 ui_test_utils::NavigateToURL(browser(), url);
1228 PopulateForm("NAME_FIRST");
1229
1230 content::WindowedNotificationObserver load_stop_observer(
1231 content::NOTIFICATION_LOAD_STOP,
1232 content::Source<content::NavigationController>(
1233 &GetWebContents()->GetController()));
1234
1235 ASSERT_TRUE(content::ExecuteScript(
1236 GetRenderViewHost(),
1237 "document.getElementById('testform').submit();"));
1238 // This will ensure the test didn't hang.
1239 load_stop_observer.Wait();
1240 }
1241
1242 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1243 // is interacting with the form. This is a regression test for
1244 // http://crbug.com/160476
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,DisableAutocompleteWhileFilling)1245 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1246 DisableAutocompleteWhileFilling) {
1247 CreateTestProfile();
1248
1249 // Load the test page.
1250 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1251 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1252
1253 // Invoke Autofill: Start filling the first name field with "M" and wait for
1254 // the popup to be shown.
1255 FocusFirstNameField();
1256 SendKeyToPageAndWait(ui::VKEY_M);
1257
1258 // Now that the popup with suggestions is showing, disable autocomplete for
1259 // the active field.
1260 ASSERT_TRUE(content::ExecuteScript(
1261 GetRenderViewHost(),
1262 "document.querySelector('input').autocomplete = 'off';"));
1263
1264 // Press the down arrow to select the suggestion and attempt to preview the
1265 // autofilled form.
1266 SendKeyToPopupAndWait(ui::VKEY_DOWN);
1267 }
1268
1269 } // namespace autofill
1270