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.
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/files/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/interactive_test_utils.h"
30 #include "chrome/test/base/test_switches.h"
31 #include "chrome/test/base/ui_test_utils.h"
32 #include "components/autofill/content/browser/content_autofill_driver.h"
33 #include "components/autofill/core/browser/autofill_manager.h"
34 #include "components/autofill/core/browser/autofill_manager_test_delegate.h"
35 #include "components/autofill/core/browser/autofill_profile.h"
36 #include "components/autofill/core/browser/autofill_test_utils.h"
37 #include "components/autofill/core/browser/personal_data_manager.h"
38 #include "components/autofill/core/browser/personal_data_manager_observer.h"
39 #include "components/autofill/core/browser/validation.h"
40 #include "components/infobars/core/confirm_infobar_delegate.h"
41 #include "components/infobars/core/infobar.h"
42 #include "components/infobars/core/infobar_manager.h"
43 #include "components/translate/core/browser/translate_infobar_delegate.h"
44 #include "content/public/browser/navigation_controller.h"
45 #include "content/public/browser/notification_observer.h"
46 #include "content/public/browser/notification_registrar.h"
47 #include "content/public/browser/notification_service.h"
48 #include "content/public/browser/render_view_host.h"
49 #include "content/public/browser/render_widget_host.h"
50 #include "content/public/browser/web_contents.h"
51 #include "content/public/test/browser_test_utils.h"
52 #include "content/public/test/test_renderer_host.h"
53 #include "content/public/test/test_utils.h"
54 #include "net/url_request/test_url_fetcher_factory.h"
55 #include "testing/gmock/include/gmock/gmock.h"
56 #include "testing/gtest/include/gtest/gtest.h"
57 #include "ui/events/keycodes/keyboard_codes.h"
59 using base::ASCIIToUTF16;
63 static const char kDataURIPrefix[] = "data:text/html;charset=utf-8,";
64 static const char kTestFormString[] =
65 "<form action=\"http://www.example.com/\" method=\"POST\">"
66 "<label for=\"firstname\">First name:</label>"
67 " <input type=\"text\" id=\"firstname\""
68 " onfocus=\"domAutomationController.send(true)\"><br>"
69 "<label for=\"lastname\">Last name:</label>"
70 " <input type=\"text\" id=\"lastname\"><br>"
71 "<label for=\"address1\">Address line 1:</label>"
72 " <input type=\"text\" id=\"address1\"><br>"
73 "<label for=\"address2\">Address line 2:</label>"
74 " <input type=\"text\" id=\"address2\"><br>"
75 "<label for=\"city\">City:</label>"
76 " <input type=\"text\" id=\"city\"><br>"
77 "<label for=\"state\">State:</label>"
78 " <select id=\"state\">"
79 " <option value=\"\" selected=\"yes\">--</option>"
80 " <option value=\"CA\">California</option>"
81 " <option value=\"TX\">Texas</option>"
83 "<label for=\"zip\">ZIP code:</label>"
84 " <input type=\"text\" id=\"zip\"><br>"
85 "<label for=\"country\">Country:</label>"
86 " <select id=\"country\">"
87 " <option value=\"\" selected=\"yes\">--</option>"
88 " <option value=\"CA\">Canada</option>"
89 " <option value=\"US\">United States</option>"
91 "<label for=\"phone\">Phone number:</label>"
92 " <input type=\"text\" id=\"phone\"><br>"
96 // AutofillManagerTestDelegateImpl --------------------------------------------
98 class AutofillManagerTestDelegateImpl
99 : public autofill::AutofillManagerTestDelegate {
101 AutofillManagerTestDelegateImpl() {}
102 virtual ~AutofillManagerTestDelegateImpl() {}
104 // autofill::AutofillManagerTestDelegate:
105 virtual void DidPreviewFormData() OVERRIDE {
106 ASSERT_TRUE(loop_runner_->loop_running());
107 loop_runner_->Quit();
110 virtual void DidFillFormData() OVERRIDE {
111 ASSERT_TRUE(loop_runner_->loop_running());
112 loop_runner_->Quit();
115 virtual void DidShowSuggestions() OVERRIDE {
116 ASSERT_TRUE(loop_runner_->loop_running());
117 loop_runner_->Quit();
121 loop_runner_ = new content::MessageLoopRunner();
129 scoped_refptr<content::MessageLoopRunner> loop_runner_;
131 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl);
135 // WindowedPersonalDataManagerObserver ----------------------------------------
137 class WindowedPersonalDataManagerObserver
138 : public PersonalDataManagerObserver,
139 public infobars::InfoBarManager::Observer {
141 explicit WindowedPersonalDataManagerObserver(Browser* browser)
143 has_run_message_loop_(false),
145 infobar_service_(InfoBarService::FromWebContents(
146 browser_->tab_strip_model()->GetActiveWebContents())) {
147 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
149 infobar_service_->AddObserver(this);
152 virtual ~WindowedPersonalDataManagerObserver() {
153 while (infobar_service_->infobar_count() > 0) {
154 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0));
156 infobar_service_->RemoveObserver(this);
159 // PersonalDataManagerObserver:
160 virtual void OnPersonalDataChanged() OVERRIDE {
161 if (has_run_message_loop_) {
162 base::MessageLoopForUI::current()->Quit();
163 has_run_message_loop_ = false;
168 virtual void OnInsufficientFormData() OVERRIDE {
169 OnPersonalDataChanged();
175 has_run_message_loop_ = true;
176 content::RunMessageLoop();
178 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
179 RemoveObserver(this);
183 // infobars::InfoBarManager::Observer:
184 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) OVERRIDE {
185 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()->
190 bool has_run_message_loop_;
192 InfoBarService* infobar_service_;
194 DISALLOW_COPY_AND_ASSIGN(WindowedPersonalDataManagerObserver);
197 // AutofillInteractiveTest ----------------------------------------------------
199 class AutofillInteractiveTest : public InProcessBrowserTest {
201 AutofillInteractiveTest() :
202 key_press_event_sink_(
203 base::Bind(&AutofillInteractiveTest::HandleKeyPressEvent,
204 base::Unretained(this))) {}
205 virtual ~AutofillInteractiveTest() {}
207 // InProcessBrowserTest:
208 virtual void SetUpOnMainThread() OVERRIDE {
209 // Don't want Keychain coming up on Mac.
210 test::DisableSystemServices(browser()->profile()->GetPrefs());
212 // Inject the test delegate into the AutofillManager.
213 content::WebContents* web_contents = GetWebContents();
214 ContentAutofillDriver* autofill_driver =
215 ContentAutofillDriver::FromWebContents(web_contents);
216 AutofillManager* autofill_manager = autofill_driver->autofill_manager();
217 autofill_manager->SetTestDelegate(&test_delegate_);
219 // If the mouse happened to be over where the suggestions are shown, then
220 // the preview will show up and will fail the tests. We need to give it a
221 // point that's within the browser frame, or else the method hangs.
222 gfx::Point reset_mouse(GetWebContents()->GetContainerBounds().origin());
223 reset_mouse = gfx::Point(reset_mouse.x() + 5, reset_mouse.y() + 5);
224 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(reset_mouse));
227 virtual void TearDownOnMainThread() OVERRIDE {
228 // Make sure to close any showing popups prior to tearing down the UI.
229 content::WebContents* web_contents = GetWebContents();
230 AutofillManager* autofill_manager = ContentAutofillDriver::FromWebContents(
231 web_contents)->autofill_manager();
232 autofill_manager->client()->HideAutofillPopup();
235 PersonalDataManager* GetPersonalDataManager() {
236 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
239 content::WebContents* GetWebContents() {
240 return browser()->tab_strip_model()->GetActiveWebContents();
243 content::RenderViewHost* GetRenderViewHost() {
244 return GetWebContents()->GetRenderViewHost();
247 void CreateTestProfile() {
248 AutofillProfile profile;
249 test::SetProfileInfo(
250 &profile, "Milton", "C.", "Waddams",
251 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane",
252 "Basement", "Austin", "Texas", "78744", "US", "5125551234");
254 WindowedPersonalDataManagerObserver observer(browser());
255 GetPersonalDataManager()->AddProfile(profile);
257 // AddProfile is asynchronous. Wait for it to finish before continuing the
262 void SetProfiles(std::vector<AutofillProfile>* profiles) {
263 WindowedPersonalDataManagerObserver observer(browser());
264 GetPersonalDataManager()->SetProfiles(profiles);
268 void SetProfile(const AutofillProfile& profile) {
269 std::vector<AutofillProfile> profiles;
270 profiles.push_back(profile);
271 SetProfiles(&profiles);
274 // Populates a webpage form using autofill data and keypress events.
275 // This function focuses the specified input field in the form, and then
276 // sends keypress events to the tab to cause the form to be populated.
277 void PopulateForm(const std::string& field_id) {
278 std::string js("document.getElementById('" + field_id + "').focus();");
279 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js));
281 SendKeyToPageAndWait(ui::VKEY_DOWN);
282 SendKeyToPopupAndWait(ui::VKEY_DOWN);
283 SendKeyToPopupAndWait(ui::VKEY_RETURN);
286 void ExpectFieldValue(const std::string& field_name,
287 const std::string& expected_value) {
289 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
291 "window.domAutomationController.send("
292 " document.getElementById('" + field_name + "').value);",
294 EXPECT_EQ(expected_value, value);
297 void GetFieldBackgroundColor(const std::string& field_name,
298 std::string* color) {
299 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
301 "window.domAutomationController.send("
302 " document.defaultView.getComputedStyle(document.getElementById('" +
303 field_name + "')).backgroundColor);",
307 void SimulateURLFetch(bool success) {
308 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
309 ASSERT_TRUE(fetcher);
310 net::URLRequestStatus status;
311 status.set_status(success ? net::URLRequestStatus::SUCCESS :
312 net::URLRequestStatus::FAILED);
314 std::string script = " var google = {};"
315 "google.translate = (function() {"
317 " TranslateService: function() {"
319 " isAvailable : function() {"
322 " restore : function() {"
325 " getDetectedLanguage : function() {"
328 " translatePage : function(originalLang, targetLang,"
329 " onTranslateProgress) {"
330 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
331 std::string(kTestFormString) +
333 " onTranslateProgress(100, true, false);"
339 "cr.googleTranslate.onTranslateElementLoad();";
341 fetcher->set_url(fetcher->GetOriginalURL());
342 fetcher->set_status(status);
343 fetcher->set_response_code(success ? 200 : 500);
344 fetcher->SetResponseString(script);
345 fetcher->delegate()->OnURLFetchComplete(fetcher);
348 void FocusFirstNameField() {
350 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
352 "if (document.readyState === 'complete')"
353 " document.getElementById('firstname').focus();"
355 " domAutomationController.send(false);",
360 // Simulates a click on the middle of the DOM element with the given |id|.
361 void ClickElementWithId(const std::string& id) {
363 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
365 "var bounds = document.getElementById('" +
367 "').getBoundingClientRect();"
368 "domAutomationController.send("
369 " Math.floor(bounds.left + bounds.width / 2));",
372 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
374 "var bounds = document.getElementById('" +
376 "').getBoundingClientRect();"
377 "domAutomationController.send("
378 " Math.floor(bounds.top + bounds.height / 2));",
380 content::SimulateMouseClickAt(GetWebContents(),
382 blink::WebMouseEvent::ButtonLeft,
386 void ClickFirstNameField() {
387 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("firstname"));
390 // Make a pointless round trip to the renderer, giving the popup a chance to
391 // show if it's going to. If it does show, an assert in
392 // AutofillManagerTestDelegateImpl will trigger.
393 void MakeSurePopupDoesntAppear() {
395 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
396 GetRenderViewHost(), "domAutomationController.send(42)", &unused));
399 void ExpectFilledTestForm() {
400 ExpectFieldValue("firstname", "Milton");
401 ExpectFieldValue("lastname", "Waddams");
402 ExpectFieldValue("address1", "4120 Freidrich Lane");
403 ExpectFieldValue("address2", "Basement");
404 ExpectFieldValue("city", "Austin");
405 ExpectFieldValue("state", "TX");
406 ExpectFieldValue("zip", "78744");
407 ExpectFieldValue("country", "US");
408 ExpectFieldValue("phone", "5125551234");
411 void SendKeyToPageAndWait(ui::KeyboardCode key) {
412 test_delegate_.Reset();
413 content::SimulateKeyPress(
414 GetWebContents(), key, false, false, false, false);
415 test_delegate_.Wait();
418 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) {
422 void SendKeyToPopupAndWait(ui::KeyboardCode key) {
423 // Route popup-targeted key presses via the render view host.
424 content::NativeWebKeyboardEvent event;
425 event.windowsKeyCode = key;
426 event.type = blink::WebKeyboardEvent::RawKeyDown;
427 test_delegate_.Reset();
428 // Install the key press event sink to ensure that any events that are not
429 // handled by the installed callbacks do not end up crashing the test.
430 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
431 GetRenderViewHost()->ForwardKeyboardEvent(event);
432 test_delegate_.Wait();
433 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
436 // Datalist does not support autofill preview. There is no need to start
437 // message loop for Datalist.
438 void SendKeyToDataListPopup(ui::KeyboardCode key) {
439 // Route popup-targeted key presses via the render view host.
440 content::NativeWebKeyboardEvent event;
441 event.windowsKeyCode = key;
442 event.type = blink::WebKeyboardEvent::RawKeyDown;
443 // Install the key press event sink to ensure that any events that are not
444 // handled by the installed callbacks do not end up crashing the test.
445 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
446 GetRenderViewHost()->ForwardKeyboardEvent(event);
447 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
450 void TryBasicFormFill() {
451 FocusFirstNameField();
453 // Start filling the first name field with "M" and wait for the popup to be
455 SendKeyToPageAndWait(ui::VKEY_M);
457 // Press the down arrow to select the suggestion and preview the autofilled
459 SendKeyToPopupAndWait(ui::VKEY_DOWN);
461 // The previewed values should not be accessible to JavaScript.
462 ExpectFieldValue("firstname", "M");
463 ExpectFieldValue("lastname", std::string());
464 ExpectFieldValue("address1", std::string());
465 ExpectFieldValue("address2", std::string());
466 ExpectFieldValue("city", std::string());
467 ExpectFieldValue("state", std::string());
468 ExpectFieldValue("zip", std::string());
469 ExpectFieldValue("country", std::string());
470 ExpectFieldValue("phone", std::string());
471 // TODO(isherman): It would be nice to test that the previewed values are
472 // displayed: http://crbug.com/57220
474 // Press Enter to accept the autofill suggestions.
475 SendKeyToPopupAndWait(ui::VKEY_RETURN);
477 // The form should be filled.
478 ExpectFilledTestForm();
481 AutofillManagerTestDelegateImpl* test_delegate() { return &test_delegate_; }
484 AutofillManagerTestDelegateImpl test_delegate_;
486 net::TestURLFetcherFactory url_fetcher_factory_;
488 // KeyPressEventCallback that serves as a sink to ensure that every key press
489 // event the tests create and have the WebContents forward is handled by some
490 // key press event callback. It is necessary to have this sinkbecause if no
491 // key press event callback handles the event (at least on Mac), a DCHECK
492 // ends up going off that the |event| doesn't have an |os_event| associated
494 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_;
496 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest);
499 // Test that basic form fill is working.
500 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) {
503 // Load the test page.
504 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
505 GURL(std::string(kDataURIPrefix) + kTestFormString)));
511 // Test that form filling can be initiated by pressing the down arrow.
512 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) {
515 // Load the test page.
516 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
517 GURL(std::string(kDataURIPrefix) + kTestFormString)));
519 // Focus a fillable field.
520 FocusFirstNameField();
522 // Press the down arrow to initiate Autofill and wait for the popup to be
524 SendKeyToPageAndWait(ui::VKEY_DOWN);
526 // Press the down arrow to select the suggestion and preview the autofilled
528 SendKeyToPopupAndWait(ui::VKEY_DOWN);
530 // Press Enter to accept the autofill suggestions.
531 SendKeyToPopupAndWait(ui::VKEY_RETURN);
533 // The form should be filled.
534 ExpectFilledTestForm();
537 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillSelectViaTab) {
540 // Load the test page.
541 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
542 GURL(std::string(kDataURIPrefix) + kTestFormString)));
544 // Focus a fillable field.
545 FocusFirstNameField();
547 // Press the down arrow to initiate Autofill and wait for the popup to be
549 SendKeyToPageAndWait(ui::VKEY_DOWN);
551 // Press the down arrow to select the suggestion and preview the autofilled
553 SendKeyToPopupAndWait(ui::VKEY_DOWN);
555 // Press tab to accept the autofill suggestions.
556 SendKeyToPopupAndWait(ui::VKEY_TAB);
558 // The form should be filled.
559 ExpectFilledTestForm();
562 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaClick) {
565 // Load the test page.
566 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
567 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
568 // Focus a fillable field.
569 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
572 test_delegate()->Reset();
573 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
574 test_delegate()->Wait();
576 // Press the down arrow to select the suggestion and preview the autofilled
578 SendKeyToPopupAndWait(ui::VKEY_DOWN);
580 // Press Enter to accept the autofill suggestions.
581 SendKeyToPopupAndWait(ui::VKEY_RETURN);
583 // The form should be filled.
584 ExpectFilledTestForm();
587 // Makes sure that the first click does *not* activate the popup.
588 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForFirstClick) {
591 // Load the test page.
592 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
593 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
595 // Click the first name field while it's out of focus, then twiddle our thumbs
596 // a bit. If a popup were to show, it would hit the asserts in
597 // AutofillManagerTestDelegateImpl while we're wasting time.
598 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
599 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
601 // The second click should activate the popup since the first click focused
603 test_delegate()->Reset();
604 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
605 test_delegate()->Wait();
608 // Makes sure that clicking outside the focused field doesn't activate
610 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForOutsideClick) {
613 // Load the test page.
614 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
616 GURL(std::string(kDataURIPrefix) + kTestFormString +
617 "<button disabled id='disabled-button'>Cant click this</button>")));
619 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
621 // Clicking a disabled button will generate a mouse event but focus doesn't
622 // change. This tests that autofill can handle a mouse event outside a focused
623 // input *without* showing the popup.
624 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("disabled-button"));
625 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
627 test_delegate()->Reset();
628 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
629 test_delegate()->Wait();
632 // Test that a field is still autofillable after the previously autofilled
634 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) {
637 // Load the test page.
638 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
639 GURL(std::string(kDataURIPrefix) + kTestFormString)));
641 // Invoke and accept the Autofill popup and verify the form was filled.
642 FocusFirstNameField();
643 SendKeyToPageAndWait(ui::VKEY_M);
644 SendKeyToPopupAndWait(ui::VKEY_DOWN);
645 SendKeyToPopupAndWait(ui::VKEY_RETURN);
646 ExpectFilledTestForm();
648 // Delete the value of a filled field.
649 ASSERT_TRUE(content::ExecuteScript(
651 "document.getElementById('firstname').value = '';"));
652 ExpectFieldValue("firstname", "");
654 // Invoke and accept the Autofill popup and verify the field was filled.
655 SendKeyToPageAndWait(ui::VKEY_M);
656 SendKeyToPopupAndWait(ui::VKEY_DOWN);
657 SendKeyToPopupAndWait(ui::VKEY_RETURN);
658 ExpectFieldValue("firstname", "Milton");
661 // Test that an input field is not rendered with the yellow autofilled
662 // background color when choosing an option from the datalist suggestion list.
663 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnSelectOptionFromDatalist) {
664 // Load the test page.
665 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
667 GURL(std::string(kDataURIPrefix) +
668 "<form action=\"http://www.example.com/\" method=\"POST\">"
669 " <input list=\"dl\" type=\"search\" id=\"firstname\""
670 " onfocus=\"domAutomationController.send(true)\"><br>"
671 " <datalist id=\"dl\">"
672 " <option value=\"Adam\"></option>"
673 " <option value=\"Bob\"></option>"
674 " <option value=\"Carl\"></option>"
677 std::string orginalcolor;
678 GetFieldBackgroundColor("firstname", &orginalcolor);
680 FocusFirstNameField();
681 SendKeyToPageAndWait(ui::VKEY_DOWN);
682 SendKeyToDataListPopup(ui::VKEY_DOWN);
683 SendKeyToDataListPopup(ui::VKEY_RETURN);
684 ExpectFieldValue("firstname", "Adam");
686 GetFieldBackgroundColor("firstname", &color);
687 EXPECT_EQ(color, orginalcolor);
690 // Test that a JavaScript oninput event is fired after auto-filling a form.
691 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) {
694 const char kOnInputScript[] =
696 "focused_fired = false;"
697 "unfocused_fired = false;"
698 "changed_select_fired = false;"
699 "unchanged_select_fired = false;"
700 "document.getElementById('firstname').oninput = function() {"
701 " focused_fired = true;"
703 "document.getElementById('lastname').oninput = function() {"
704 " unfocused_fired = true;"
706 "document.getElementById('state').oninput = function() {"
707 " changed_select_fired = true;"
709 "document.getElementById('country').oninput = function() {"
710 " unchanged_select_fired = true;"
712 "document.getElementById('country').value = 'US';"
715 // Load the test page.
716 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
717 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnInputScript)));
720 FocusFirstNameField();
722 // Start filling the first name field with "M" and wait for the popup to be
724 SendKeyToPageAndWait(ui::VKEY_M);
726 // Press the down arrow to select the suggestion and preview the autofilled
728 SendKeyToPopupAndWait(ui::VKEY_DOWN);
730 // Press Enter to accept the autofill suggestions.
731 SendKeyToPopupAndWait(ui::VKEY_RETURN);
733 // The form should be filled.
734 ExpectFilledTestForm();
736 bool focused_fired = false;
737 bool unfocused_fired = false;
738 bool changed_select_fired = false;
739 bool unchanged_select_fired = false;
740 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
742 "domAutomationController.send(focused_fired);",
744 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
746 "domAutomationController.send(unfocused_fired);",
748 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
750 "domAutomationController.send(changed_select_fired);",
751 &changed_select_fired));
752 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
754 "domAutomationController.send(unchanged_select_fired);",
755 &unchanged_select_fired));
756 EXPECT_TRUE(focused_fired);
757 EXPECT_TRUE(unfocused_fired);
758 EXPECT_TRUE(changed_select_fired);
759 EXPECT_FALSE(unchanged_select_fired);
762 // Test that a JavaScript onchange event is fired after auto-filling a form.
763 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) {
766 const char kOnChangeScript[] =
768 "focused_fired = false;"
769 "unfocused_fired = false;"
770 "changed_select_fired = false;"
771 "unchanged_select_fired = false;"
772 "document.getElementById('firstname').onchange = function() {"
773 " focused_fired = true;"
775 "document.getElementById('lastname').onchange = function() {"
776 " unfocused_fired = true;"
778 "document.getElementById('state').onchange = function() {"
779 " changed_select_fired = true;"
781 "document.getElementById('country').onchange = function() {"
782 " unchanged_select_fired = true;"
784 "document.getElementById('country').value = 'US';"
787 // Load the test page.
788 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
789 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnChangeScript)));
792 FocusFirstNameField();
794 // Start filling the first name field with "M" and wait for the popup to be
796 SendKeyToPageAndWait(ui::VKEY_M);
798 // Press the down arrow to select the suggestion and preview the autofilled
800 SendKeyToPopupAndWait(ui::VKEY_DOWN);
802 // Press Enter to accept the autofill suggestions.
803 SendKeyToPopupAndWait(ui::VKEY_RETURN);
805 // The form should be filled.
806 ExpectFilledTestForm();
808 bool focused_fired = false;
809 bool unfocused_fired = false;
810 bool changed_select_fired = false;
811 bool unchanged_select_fired = false;
812 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
814 "domAutomationController.send(focused_fired);",
816 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
818 "domAutomationController.send(unfocused_fired);",
820 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
822 "domAutomationController.send(changed_select_fired);",
823 &changed_select_fired));
824 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
826 "domAutomationController.send(unchanged_select_fired);",
827 &unchanged_select_fired));
828 EXPECT_TRUE(focused_fired);
829 EXPECT_TRUE(unfocused_fired);
830 EXPECT_TRUE(changed_select_fired);
831 EXPECT_FALSE(unchanged_select_fired);
834 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) {
837 const char kInputFiresBeforeChangeScript[] =
839 "inputElementEvents = [];"
840 "function recordInputElementEvent(e) {"
841 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';"
842 " inputElementEvents.push(e.type);"
844 "selectElementEvents = [];"
845 "function recordSelectElementEvent(e) {"
846 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';"
847 " selectElementEvents.push(e.type);"
849 "document.getElementById('lastname').oninput = recordInputElementEvent;"
850 "document.getElementById('lastname').onchange = recordInputElementEvent;"
851 "document.getElementById('country').oninput = recordSelectElementEvent;"
852 "document.getElementById('country').onchange = recordSelectElementEvent;"
855 // Load the test page.
856 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
857 GURL(std::string(kDataURIPrefix) + kTestFormString +
858 kInputFiresBeforeChangeScript)));
860 // Invoke and accept the Autofill popup and verify the form was filled.
861 FocusFirstNameField();
862 SendKeyToPageAndWait(ui::VKEY_M);
863 SendKeyToPopupAndWait(ui::VKEY_DOWN);
864 SendKeyToPopupAndWait(ui::VKEY_RETURN);
865 ExpectFilledTestForm();
867 int num_input_element_events = -1;
868 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
870 "domAutomationController.send(inputElementEvents.length);",
871 &num_input_element_events));
872 EXPECT_EQ(2, num_input_element_events);
874 std::vector<std::string> input_element_events;
875 input_element_events.resize(2);
877 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
879 "domAutomationController.send(inputElementEvents[0]);",
880 &input_element_events[0]));
881 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
883 "domAutomationController.send(inputElementEvents[1]);",
884 &input_element_events[1]));
886 EXPECT_EQ("input", input_element_events[0]);
887 EXPECT_EQ("change", input_element_events[1]);
889 int num_select_element_events = -1;
890 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
892 "domAutomationController.send(selectElementEvents.length);",
893 &num_select_element_events));
894 EXPECT_EQ(2, num_select_element_events);
896 std::vector<std::string> select_element_events;
897 select_element_events.resize(2);
899 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
901 "domAutomationController.send(selectElementEvents[0]);",
902 &select_element_events[0]));
903 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
905 "domAutomationController.send(selectElementEvents[1]);",
906 &select_element_events[1]));
908 EXPECT_EQ("input", select_element_events[0]);
909 EXPECT_EQ("change", select_element_events[1]);
912 // Test that we can autofill forms distinguished only by their |id| attribute.
913 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
914 AutofillFormsDistinguishedById) {
917 // Load the test page.
918 const std::string kURL =
919 std::string(kDataURIPrefix) + kTestFormString +
921 "var mainForm = document.forms[0];"
922 "mainForm.id = 'mainForm';"
923 "var newForm = document.createElement('form');"
924 "newForm.action = mainForm.action;"
925 "newForm.method = mainForm.method;"
926 "newForm.id = 'newForm';"
927 "mainForm.parentNode.insertBefore(newForm, mainForm);"
929 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL)));
935 // Test that we properly autofill forms with repeated fields.
936 // In the wild, the repeated fields are typically either email fields
937 // (duplicated for "confirmation"); or variants that are hot-swapped via
938 // JavaScript, with only one actually visible at any given time.
939 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) {
942 // Load the test page.
943 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
944 GURL(std::string(kDataURIPrefix) +
945 "<form action=\"http://www.example.com/\" method=\"POST\">"
946 "<label for=\"firstname\">First name:</label>"
947 " <input type=\"text\" id=\"firstname\""
948 " onfocus=\"domAutomationController.send(true)\"><br>"
949 "<label for=\"lastname\">Last name:</label>"
950 " <input type=\"text\" id=\"lastname\"><br>"
951 "<label for=\"address1\">Address line 1:</label>"
952 " <input type=\"text\" id=\"address1\"><br>"
953 "<label for=\"address2\">Address line 2:</label>"
954 " <input type=\"text\" id=\"address2\"><br>"
955 "<label for=\"city\">City:</label>"
956 " <input type=\"text\" id=\"city\"><br>"
957 "<label for=\"state\">State:</label>"
958 " <select id=\"state\">"
959 " <option value=\"\" selected=\"yes\">--</option>"
960 " <option value=\"CA\">California</option>"
961 " <option value=\"TX\">Texas</option>"
963 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
964 " <input type=\"text\" id=\"state_freeform\""
965 " style=\"display:none\"><br>"
966 "<label for=\"zip\">ZIP code:</label>"
967 " <input type=\"text\" id=\"zip\"><br>"
968 "<label for=\"country\">Country:</label>"
969 " <select id=\"country\">"
970 " <option value=\"\" selected=\"yes\">--</option>"
971 " <option value=\"CA\">Canada</option>"
972 " <option value=\"US\">United States</option>"
974 "<label for=\"phone\">Phone number:</label>"
975 " <input type=\"text\" id=\"phone\"><br>"
980 ExpectFieldValue("state_freeform", std::string());
983 // Test that we properly autofill forms with non-autofillable fields.
984 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
985 AutofillFormWithNonAutofillableField) {
988 // Load the test page.
989 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
990 GURL(std::string(kDataURIPrefix) +
991 "<form action=\"http://www.example.com/\" method=\"POST\">"
992 "<label for=\"firstname\">First name:</label>"
993 " <input type=\"text\" id=\"firstname\""
994 " onfocus=\"domAutomationController.send(true)\"><br>"
995 "<label for=\"middlename\">Middle name:</label>"
996 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
997 "<label for=\"lastname\">Last name:</label>"
998 " <input type=\"text\" id=\"lastname\"><br>"
999 "<label for=\"address1\">Address line 1:</label>"
1000 " <input type=\"text\" id=\"address1\"><br>"
1001 "<label for=\"address2\">Address line 2:</label>"
1002 " <input type=\"text\" id=\"address2\"><br>"
1003 "<label for=\"city\">City:</label>"
1004 " <input type=\"text\" id=\"city\"><br>"
1005 "<label for=\"state\">State:</label>"
1006 " <select id=\"state\">"
1007 " <option value=\"\" selected=\"yes\">--</option>"
1008 " <option value=\"CA\">California</option>"
1009 " <option value=\"TX\">Texas</option>"
1011 "<label for=\"zip\">ZIP code:</label>"
1012 " <input type=\"text\" id=\"zip\"><br>"
1013 "<label for=\"country\">Country:</label>"
1014 " <select id=\"country\">"
1015 " <option value=\"\" selected=\"yes\">--</option>"
1016 " <option value=\"CA\">Canada</option>"
1017 " <option value=\"US\">United States</option>"
1019 "<label for=\"phone\">Phone number:</label>"
1020 " <input type=\"text\" id=\"phone\"><br>"
1027 // Test that we can Autofill dynamically generated forms.
1028 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) {
1029 CreateTestProfile();
1031 // Load the test page.
1032 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1033 GURL(std::string(kDataURIPrefix) +
1034 "<form id=\"form\" action=\"http://www.example.com/\""
1035 " method=\"POST\"></form>"
1037 "function AddElement(name, label) {"
1038 " var form = document.getElementById('form');"
1040 " var label_text = document.createTextNode(label);"
1041 " var label_element = document.createElement('label');"
1042 " label_element.setAttribute('for', name);"
1043 " label_element.appendChild(label_text);"
1044 " form.appendChild(label_element);"
1046 " if (name === 'state' || name === 'country') {"
1047 " var select_element = document.createElement('select');"
1048 " select_element.setAttribute('id', name);"
1049 " select_element.setAttribute('name', name);"
1051 " /* Add an empty selected option. */"
1052 " var default_option = new Option('--', '', true);"
1053 " select_element.appendChild(default_option);"
1055 " /* Add the other options. */"
1056 " if (name == 'state') {"
1057 " var option1 = new Option('California', 'CA');"
1058 " select_element.appendChild(option1);"
1059 " var option2 = new Option('Texas', 'TX');"
1060 " select_element.appendChild(option2);"
1062 " var option1 = new Option('Canada', 'CA');"
1063 " select_element.appendChild(option1);"
1064 " var option2 = new Option('United States', 'US');"
1065 " select_element.appendChild(option2);"
1068 " form.appendChild(select_element);"
1070 " var input_element = document.createElement('input');"
1071 " input_element.setAttribute('id', name);"
1072 " input_element.setAttribute('name', name);"
1074 " /* Add the onfocus listener to the 'firstname' field. */"
1075 " if (name === 'firstname') {"
1076 " input_element.onfocus = function() {"
1077 " domAutomationController.send(true);"
1081 " form.appendChild(input_element);"
1084 " form.appendChild(document.createElement('br'));"
1087 "function BuildForm() {"
1089 " ['firstname', 'First name:'],"
1090 " ['lastname', 'Last name:'],"
1091 " ['address1', 'Address line 1:'],"
1092 " ['address2', 'Address line 2:'],"
1093 " ['city', 'City:'],"
1094 " ['state', 'State:'],"
1095 " ['zip', 'ZIP code:'],"
1096 " ['country', 'Country:'],"
1097 " ['phone', 'Phone number:'],"
1100 " for (var i = 0; i < elements.length; i++) {"
1101 " var name = elements[i][0];"
1102 " var label = elements[i][1];"
1103 " AddElement(name, label);"
1108 // Dynamically construct the form.
1109 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
1115 // Test that form filling works after reloading the current page.
1116 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) {
1117 CreateTestProfile();
1119 // Load the test page.
1120 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1121 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1124 content::WebContents* web_contents = GetWebContents();
1125 web_contents->GetController().Reload(false);
1126 content::WaitForLoadStop(web_contents);
1132 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterTranslate) {
1133 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1134 if (TranslateService::IsTranslateBubbleEnabled())
1137 CreateTestProfile();
1139 GURL url(std::string(kDataURIPrefix) +
1140 "<form action=\"http://www.example.com/\" method=\"POST\">"
1141 "<label for=\"fn\">なまえ</label>"
1142 " <input type=\"text\" id=\"fn\""
1143 " onfocus=\"domAutomationController.send(true)\""
1145 "<label for=\"ln\">みょうじ</label>"
1146 " <input type=\"text\" id=\"ln\"><br>"
1147 "<label for=\"a1\">Address line 1:</label>"
1148 " <input type=\"text\" id=\"a1\"><br>"
1149 "<label for=\"a2\">Address line 2:</label>"
1150 " <input type=\"text\" id=\"a2\"><br>"
1151 "<label for=\"ci\">City:</label>"
1152 " <input type=\"text\" id=\"ci\"><br>"
1153 "<label for=\"st\">State:</label>"
1154 " <select id=\"st\">"
1155 " <option value=\"\" selected=\"yes\">--</option>"
1156 " <option value=\"CA\">California</option>"
1157 " <option value=\"TX\">Texas</option>"
1159 "<label for=\"z\">ZIP code:</label>"
1160 " <input type=\"text\" id=\"z\"><br>"
1161 "<label for=\"co\">Country:</label>"
1162 " <select id=\"co\">"
1163 " <option value=\"\" selected=\"yes\">--</option>"
1164 " <option value=\"CA\">Canada</option>"
1165 " <option value=\"US\">United States</option>"
1167 "<label for=\"ph\">Phone number:</label>"
1168 " <input type=\"text\" id=\"ph\"><br>"
1170 // Add additional Japanese characters to ensure the translate bar
1172 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
1173 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
1175 content::WindowedNotificationObserver infobar_observer(
1176 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
1177 content::NotificationService::AllSources());
1178 ASSERT_NO_FATAL_FAILURE(
1179 ui_test_utils::NavigateToURL(browser(), url));
1181 // Wait for the translation bar to appear and get it.
1182 infobar_observer.Wait();
1183 InfoBarService* infobar_service =
1184 InfoBarService::FromWebContents(GetWebContents());
1185 translate::TranslateInfoBarDelegate* delegate =
1186 infobar_service->infobar_at(0)->delegate()->AsTranslateInfoBarDelegate();
1187 ASSERT_TRUE(delegate);
1188 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1189 delegate->translate_step());
1191 // Simulate translation button press.
1192 delegate->Translate();
1194 content::WindowedNotificationObserver translation_observer(
1195 chrome::NOTIFICATION_PAGE_TRANSLATED,
1196 content::NotificationService::AllSources());
1198 // Simulate the translate script being retrieved.
1199 // Pass fake google.translate lib as the translate script.
1200 SimulateURLFetch(true);
1202 // Simulate the render notifying the translation has been done.
1203 translation_observer.Wait();
1208 // Test phone fields parse correctly from a given profile.
1209 // The high level key presses execute the following: Select the first text
1210 // field, invoke the autofill popup list, select the first profile within the
1211 // list, and commit to the profile to populate the form.
1212 // Flakily times out on windows. http://crbug.com/390564
1214 #define MAYBE_ComparePhoneNumbers DISABLED_ComparePhoneNumbers
1216 #define MAYBE_ComparePhoneNumbers ComparePhoneNumbers
1218 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_ComparePhoneNumbers) {
1219 ASSERT_TRUE(test_server()->Start());
1221 AutofillProfile profile;
1222 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1223 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1224 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 H St."));
1225 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1226 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1227 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1228 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1-408-555-4567"));
1229 SetProfile(profile);
1231 GURL url = test_server()->GetURL("files/autofill/form_phones.html");
1232 ui_test_utils::NavigateToURL(browser(), url);
1233 PopulateForm("NAME_FIRST");
1235 ExpectFieldValue("NAME_FIRST", "Bob");
1236 ExpectFieldValue("NAME_LAST", "Smith");
1237 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
1238 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
1239 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
1240 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
1241 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
1242 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
1243 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
1244 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
1245 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
1246 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
1247 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
1248 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
1249 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
1250 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
1251 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
1254 // Test that Autofill does not fill in read-only fields.
1255 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, NoAutofillForReadOnlyFields) {
1256 ASSERT_TRUE(test_server()->Start());
1258 std::string addr_line1("1234 H St.");
1260 AutofillProfile profile;
1261 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1262 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1263 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("bsmith@gmail.com"));
1264 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
1265 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1266 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1267 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1268 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Company X"));
1269 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("408-871-4567"));
1270 SetProfile(profile);
1272 GURL url = test_server()->GetURL("files/autofill/read_only_field_test.html");
1273 ui_test_utils::NavigateToURL(browser(), url);
1274 PopulateForm("firstname");
1276 ExpectFieldValue("email", std::string());
1277 ExpectFieldValue("address", addr_line1);
1280 // Test form is fillable from a profile after form was reset.
1282 // 1. Fill form using a saved profile.
1283 // 2. Reset the form.
1284 // 3. Fill form using a saved profile.
1285 // Flakily times out: http://crbug.com/270341
1286 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_FormFillableOnReset) {
1287 ASSERT_TRUE(test_server()->Start());
1289 CreateTestProfile();
1291 GURL url = test_server()->GetURL("files/autofill/autofill_test_form.html");
1292 ui_test_utils::NavigateToURL(browser(), url);
1293 PopulateForm("NAME_FIRST");
1295 ASSERT_TRUE(content::ExecuteScript(
1296 GetWebContents(), "document.getElementById('testform').reset()"));
1298 PopulateForm("NAME_FIRST");
1300 ExpectFieldValue("NAME_FIRST", "Milton");
1301 ExpectFieldValue("NAME_LAST", "Waddams");
1302 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
1303 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
1304 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
1305 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
1306 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
1307 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
1308 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
1311 // Test Autofill distinguishes a middle initial in a name.
1312 // Flakily times out: http://crbug.com/270341
1313 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1314 DISABLED_DistinguishMiddleInitialWithinName) {
1315 ASSERT_TRUE(test_server()->Start());
1317 CreateTestProfile();
1319 GURL url = test_server()->GetURL(
1320 "files/autofill/autofill_middleinit_form.html");
1321 ui_test_utils::NavigateToURL(browser(), url);
1322 PopulateForm("NAME_FIRST");
1324 ExpectFieldValue("NAME_MIDDLE", "C");
1327 // Test forms with multiple email addresses are filled properly.
1328 // Entire form should be filled with one user gesture.
1329 // Flakily times out: http://crbug.com/270341
1330 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1331 DISABLED_MultipleEmailFilledByOneUserGesture) {
1332 ASSERT_TRUE(test_server()->Start());
1334 std::string email("bsmith@gmail.com");
1336 AutofillProfile profile;
1337 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1338 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1339 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email));
1340 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("4088714567"));
1341 SetProfile(profile);
1343 GURL url = test_server()->GetURL(
1344 "files/autofill/autofill_confirmemail_form.html");
1345 ui_test_utils::NavigateToURL(browser(), url);
1346 PopulateForm("NAME_FIRST");
1348 ExpectFieldValue("EMAIL_CONFIRM", email);
1349 // TODO(isherman): verify entire form.
1352 // http://crbug.com/281527
1353 #if defined(OS_MACOSX)
1354 #define MAYBE_FormFillLatencyAfterSubmit FormFillLatencyAfterSubmit
1356 #define MAYBE_FormFillLatencyAfterSubmit DISABLED_FormFillLatencyAfterSubmit
1358 // Test latency time on form submit with lots of stored Autofill profiles.
1359 // This test verifies when a profile is selected from the Autofill dictionary
1360 // that consists of thousands of profiles, the form does not hang after being
1362 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1363 MAYBE_FormFillLatencyAfterSubmit) {
1364 ASSERT_TRUE(test_server()->Start());
1366 std::vector<std::string> cities;
1367 cities.push_back("San Jose");
1368 cities.push_back("San Francisco");
1369 cities.push_back("Sacramento");
1370 cities.push_back("Los Angeles");
1372 std::vector<std::string> streets;
1373 streets.push_back("St");
1374 streets.push_back("Ave");
1375 streets.push_back("Ln");
1376 streets.push_back("Ct");
1378 const int kNumProfiles = 1500;
1379 base::Time start_time = base::Time::Now();
1380 std::vector<AutofillProfile> profiles;
1381 for (int i = 0; i < kNumProfiles; i++) {
1382 AutofillProfile profile;
1383 base::string16 name(base::IntToString16(i));
1384 base::string16 email(name + ASCIIToUTF16("@example.com"));
1385 base::string16 street = ASCIIToUTF16(
1386 base::IntToString(base::RandInt(0, 10000)) + " " +
1387 streets[base::RandInt(0, streets.size() - 1)]);
1388 base::string16 city =
1389 ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]);
1390 base::string16 zip(base::IntToString16(base::RandInt(0, 10000)));
1391 profile.SetRawInfo(NAME_FIRST, name);
1392 profile.SetRawInfo(EMAIL_ADDRESS, email);
1393 profile.SetRawInfo(ADDRESS_HOME_LINE1, street);
1394 profile.SetRawInfo(ADDRESS_HOME_CITY, city);
1395 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1396 profile.SetRawInfo(ADDRESS_HOME_ZIP, zip);
1397 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1398 profiles.push_back(profile);
1400 SetProfiles(&profiles);
1401 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1403 LOG(INFO) << "Created " << kNumProfiles << " profiles in " <<
1404 (base::Time::Now() - start_time).InSeconds() << " seconds.";
1406 GURL url = test_server()->GetURL(
1407 "files/autofill/latency_after_submit_test.html");
1408 ui_test_utils::NavigateToURL(browser(), url);
1409 PopulateForm("NAME_FIRST");
1411 content::WindowedNotificationObserver load_stop_observer(
1412 content::NOTIFICATION_LOAD_STOP,
1413 content::Source<content::NavigationController>(
1414 &GetWebContents()->GetController()));
1416 ASSERT_TRUE(content::ExecuteScript(
1417 GetRenderViewHost(),
1418 "document.getElementById('testform').submit();"));
1419 // This will ensure the test didn't hang.
1420 load_stop_observer.Wait();
1423 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1424 // is interacting with the form. This is a regression test for
1425 // http://crbug.com/160476
1426 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1427 DisableAutocompleteWhileFilling) {
1428 CreateTestProfile();
1430 // Load the test page.
1431 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1432 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1434 // Invoke Autofill: Start filling the first name field with "M" and wait for
1435 // the popup to be shown.
1436 FocusFirstNameField();
1437 SendKeyToPageAndWait(ui::VKEY_M);
1439 // Now that the popup with suggestions is showing, disable autocomplete for
1440 // the active field.
1441 ASSERT_TRUE(content::ExecuteScript(
1442 GetRenderViewHost(),
1443 "document.querySelector('input').autocomplete = 'off';"));
1445 // Press the down arrow to select the suggestion and attempt to preview the
1447 SendKeyToPopupAndWait(ui::VKEY_DOWN);
1450 } // namespace autofill