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/content/browser/content_autofill_driver_factory.h"
34 #include "components/autofill/core/browser/autofill_manager.h"
35 #include "components/autofill/core/browser/autofill_manager_test_delegate.h"
36 #include "components/autofill/core/browser/autofill_profile.h"
37 #include "components/autofill/core/browser/autofill_test_utils.h"
38 #include "components/autofill/core/browser/personal_data_manager.h"
39 #include "components/autofill/core/browser/personal_data_manager_observer.h"
40 #include "components/autofill/core/browser/validation.h"
41 #include "components/infobars/core/confirm_infobar_delegate.h"
42 #include "components/infobars/core/infobar.h"
43 #include "components/infobars/core/infobar_manager.h"
44 #include "components/translate/core/browser/translate_infobar_delegate.h"
45 #include "content/public/browser/navigation_controller.h"
46 #include "content/public/browser/notification_observer.h"
47 #include "content/public/browser/notification_registrar.h"
48 #include "content/public/browser/notification_service.h"
49 #include "content/public/browser/render_view_host.h"
50 #include "content/public/browser/render_widget_host.h"
51 #include "content/public/browser/web_contents.h"
52 #include "content/public/test/browser_test_utils.h"
53 #include "content/public/test/test_renderer_host.h"
54 #include "content/public/test/test_utils.h"
55 #include "net/url_request/test_url_fetcher_factory.h"
56 #include "testing/gmock/include/gmock/gmock.h"
57 #include "testing/gtest/include/gtest/gtest.h"
58 #include "ui/events/keycodes/keyboard_codes.h"
60 using base::ASCIIToUTF16
;
64 static const char kDataURIPrefix
[] = "data:text/html;charset=utf-8,";
65 static const char kTestFormString
[] =
66 "<form action=\"http://www.example.com/\" method=\"POST\">"
67 "<label for=\"firstname\">First name:</label>"
68 " <input type=\"text\" id=\"firstname\""
69 " onfocus=\"domAutomationController.send(true)\"><br>"
70 "<label for=\"lastname\">Last name:</label>"
71 " <input type=\"text\" id=\"lastname\"><br>"
72 "<label for=\"address1\">Address line 1:</label>"
73 " <input type=\"text\" id=\"address1\"><br>"
74 "<label for=\"address2\">Address line 2:</label>"
75 " <input type=\"text\" id=\"address2\"><br>"
76 "<label for=\"city\">City:</label>"
77 " <input type=\"text\" id=\"city\"><br>"
78 "<label for=\"state\">State:</label>"
79 " <select id=\"state\">"
80 " <option value=\"\" selected=\"yes\">--</option>"
81 " <option value=\"CA\">California</option>"
82 " <option value=\"TX\">Texas</option>"
84 "<label for=\"zip\">ZIP code:</label>"
85 " <input type=\"text\" id=\"zip\"><br>"
86 "<label for=\"country\">Country:</label>"
87 " <select id=\"country\">"
88 " <option value=\"\" selected=\"yes\">--</option>"
89 " <option value=\"CA\">Canada</option>"
90 " <option value=\"US\">United States</option>"
92 "<label for=\"phone\">Phone number:</label>"
93 " <input type=\"text\" id=\"phone\"><br>"
97 // AutofillManagerTestDelegateImpl --------------------------------------------
99 class AutofillManagerTestDelegateImpl
100 : public autofill::AutofillManagerTestDelegate
{
102 AutofillManagerTestDelegateImpl() {}
103 ~AutofillManagerTestDelegateImpl() override
{}
105 // autofill::AutofillManagerTestDelegate:
106 void DidPreviewFormData() override
{
107 ASSERT_TRUE(loop_runner_
->loop_running());
108 loop_runner_
->Quit();
111 void DidFillFormData() override
{
112 ASSERT_TRUE(loop_runner_
->loop_running());
113 loop_runner_
->Quit();
116 void DidShowSuggestions() override
{
117 ASSERT_TRUE(loop_runner_
->loop_running());
118 loop_runner_
->Quit();
122 loop_runner_
= new content::MessageLoopRunner();
130 scoped_refptr
<content::MessageLoopRunner
> loop_runner_
;
132 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl
);
136 // WindowedPersonalDataManagerObserver ----------------------------------------
138 class WindowedPersonalDataManagerObserver
139 : public PersonalDataManagerObserver
,
140 public infobars::InfoBarManager::Observer
{
142 explicit WindowedPersonalDataManagerObserver(Browser
* browser
)
144 has_run_message_loop_(false),
146 infobar_service_(InfoBarService::FromWebContents(
147 browser_
->tab_strip_model()->GetActiveWebContents())) {
148 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
150 infobar_service_
->AddObserver(this);
153 ~WindowedPersonalDataManagerObserver() override
{
154 while (infobar_service_
->infobar_count() > 0) {
155 infobar_service_
->RemoveInfoBar(infobar_service_
->infobar_at(0));
157 infobar_service_
->RemoveObserver(this);
160 // PersonalDataManagerObserver:
161 void OnPersonalDataChanged() override
{
162 if (has_run_message_loop_
) {
163 base::MessageLoopForUI::current()->Quit();
164 has_run_message_loop_
= false;
169 void OnInsufficientFormData() override
{ OnPersonalDataChanged(); }
174 has_run_message_loop_
= true;
175 content::RunMessageLoop();
177 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
178 RemoveObserver(this);
182 // infobars::InfoBarManager::Observer:
183 void OnInfoBarAdded(infobars::InfoBar
* infobar
) override
{
184 infobar_service_
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()->
189 bool has_run_message_loop_
;
191 InfoBarService
* infobar_service_
;
193 DISALLOW_COPY_AND_ASSIGN(WindowedPersonalDataManagerObserver
);
196 // AutofillInteractiveTest ----------------------------------------------------
198 class AutofillInteractiveTest
: public InProcessBrowserTest
{
200 AutofillInteractiveTest() :
201 key_press_event_sink_(
202 base::Bind(&AutofillInteractiveTest::HandleKeyPressEvent
,
203 base::Unretained(this))) {}
204 ~AutofillInteractiveTest() override
{}
206 // InProcessBrowserTest:
207 void SetUpOnMainThread() override
{
208 // Don't want Keychain coming up on Mac.
209 test::DisableSystemServices(browser()->profile()->GetPrefs());
211 // Inject the test delegate into the AutofillManager.
212 content::WebContents
* web_contents
= GetWebContents();
213 ContentAutofillDriver
* autofill_driver
=
214 ContentAutofillDriverFactory::FromWebContents(web_contents
)
215 ->DriverForFrame(web_contents
->GetMainFrame());
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 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
=
231 ContentAutofillDriverFactory::FromWebContents(web_contents
)
232 ->DriverForFrame(web_contents
->GetMainFrame())
233 ->autofill_manager();
234 autofill_manager
->client()->HideAutofillPopup();
237 PersonalDataManager
* GetPersonalDataManager() {
238 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
241 content::WebContents
* GetWebContents() {
242 return browser()->tab_strip_model()->GetActiveWebContents();
245 content::RenderViewHost
* GetRenderViewHost() {
246 return GetWebContents()->GetRenderViewHost();
249 void CreateTestProfile() {
250 AutofillProfile profile
;
251 test::SetProfileInfo(
252 &profile
, "Milton", "C.", "Waddams",
253 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane",
254 "Basement", "Austin", "Texas", "78744", "US", "5125551234");
256 WindowedPersonalDataManagerObserver
observer(browser());
257 GetPersonalDataManager()->AddProfile(profile
);
259 // AddProfile is asynchronous. Wait for it to finish before continuing the
264 void SetProfiles(std::vector
<AutofillProfile
>* profiles
) {
265 WindowedPersonalDataManagerObserver
observer(browser());
266 GetPersonalDataManager()->SetProfiles(profiles
);
270 void SetProfile(const AutofillProfile
& profile
) {
271 std::vector
<AutofillProfile
> profiles
;
272 profiles
.push_back(profile
);
273 SetProfiles(&profiles
);
276 // Populates a webpage form using autofill data and keypress events.
277 // This function focuses the specified input field in the form, and then
278 // sends keypress events to the tab to cause the form to be populated.
279 void PopulateForm(const std::string
& field_id
) {
280 std::string
js("document.getElementById('" + field_id
+ "').focus();");
281 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js
));
283 SendKeyToPageAndWait(ui::VKEY_DOWN
);
284 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
285 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
288 void ExpectFieldValue(const std::string
& field_name
,
289 const std::string
& expected_value
) {
291 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
293 "window.domAutomationController.send("
294 " document.getElementById('" + field_name
+ "').value);",
296 EXPECT_EQ(expected_value
, value
);
299 void GetFieldBackgroundColor(const std::string
& field_name
,
300 std::string
* color
) {
301 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
303 "window.domAutomationController.send("
304 " document.defaultView.getComputedStyle(document.getElementById('" +
305 field_name
+ "')).backgroundColor);",
309 void SimulateURLFetch(bool success
) {
310 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
311 ASSERT_TRUE(fetcher
);
312 net::URLRequestStatus status
;
313 status
.set_status(success
? net::URLRequestStatus::SUCCESS
:
314 net::URLRequestStatus::FAILED
);
316 std::string script
= " var google = {};"
317 "google.translate = (function() {"
319 " TranslateService: function() {"
321 " isAvailable : function() {"
324 " restore : function() {"
327 " getDetectedLanguage : function() {"
330 " translatePage : function(originalLang, targetLang,"
331 " onTranslateProgress) {"
332 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
333 std::string(kTestFormString
) +
335 " onTranslateProgress(100, true, false);"
341 "cr.googleTranslate.onTranslateElementLoad();";
343 fetcher
->set_url(fetcher
->GetOriginalURL());
344 fetcher
->set_status(status
);
345 fetcher
->set_response_code(success
? 200 : 500);
346 fetcher
->SetResponseString(script
);
347 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
350 void FocusFirstNameField() {
352 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
354 "if (document.readyState === 'complete')"
355 " document.getElementById('firstname').focus();"
357 " domAutomationController.send(false);",
362 // Simulates a click on the middle of the DOM element with the given |id|.
363 void ClickElementWithId(const std::string
& id
) {
365 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
367 "var bounds = document.getElementById('" +
369 "').getBoundingClientRect();"
370 "domAutomationController.send("
371 " Math.floor(bounds.left + bounds.width / 2));",
374 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
376 "var bounds = document.getElementById('" +
378 "').getBoundingClientRect();"
379 "domAutomationController.send("
380 " Math.floor(bounds.top + bounds.height / 2));",
382 content::SimulateMouseClickAt(GetWebContents(),
384 blink::WebMouseEvent::ButtonLeft
,
388 void ClickFirstNameField() {
389 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("firstname"));
392 // Make a pointless round trip to the renderer, giving the popup a chance to
393 // show if it's going to. If it does show, an assert in
394 // AutofillManagerTestDelegateImpl will trigger.
395 void MakeSurePopupDoesntAppear() {
397 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
398 GetRenderViewHost(), "domAutomationController.send(42)", &unused
));
401 void ExpectFilledTestForm() {
402 ExpectFieldValue("firstname", "Milton");
403 ExpectFieldValue("lastname", "Waddams");
404 ExpectFieldValue("address1", "4120 Freidrich Lane");
405 ExpectFieldValue("address2", "Basement");
406 ExpectFieldValue("city", "Austin");
407 ExpectFieldValue("state", "TX");
408 ExpectFieldValue("zip", "78744");
409 ExpectFieldValue("country", "US");
410 ExpectFieldValue("phone", "5125551234");
413 void SendKeyToPageAndWait(ui::KeyboardCode key
) {
414 test_delegate_
.Reset();
415 content::SimulateKeyPress(
416 GetWebContents(), key
, false, false, false, false);
417 test_delegate_
.Wait();
420 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent
& event
) {
424 void SendKeyToPopupAndWait(ui::KeyboardCode key
) {
425 // Route popup-targeted key presses via the render view host.
426 content::NativeWebKeyboardEvent event
;
427 event
.windowsKeyCode
= key
;
428 event
.type
= blink::WebKeyboardEvent::RawKeyDown
;
429 test_delegate_
.Reset();
430 // Install the key press event sink to ensure that any events that are not
431 // handled by the installed callbacks do not end up crashing the test.
432 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_
);
433 GetRenderViewHost()->ForwardKeyboardEvent(event
);
434 test_delegate_
.Wait();
435 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_
);
438 // Datalist does not support autofill preview. There is no need to start
439 // message loop for Datalist.
440 void SendKeyToDataListPopup(ui::KeyboardCode key
) {
441 // Route popup-targeted key presses via the render view host.
442 content::NativeWebKeyboardEvent event
;
443 event
.windowsKeyCode
= key
;
444 event
.type
= blink::WebKeyboardEvent::RawKeyDown
;
445 // Install the key press event sink to ensure that any events that are not
446 // handled by the installed callbacks do not end up crashing the test.
447 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_
);
448 GetRenderViewHost()->ForwardKeyboardEvent(event
);
449 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_
);
452 void TryBasicFormFill() {
453 FocusFirstNameField();
455 // Start filling the first name field with "M" and wait for the popup to be
457 SendKeyToPageAndWait(ui::VKEY_M
);
459 // Press the down arrow to select the suggestion and preview the autofilled
461 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
463 // The previewed values should not be accessible to JavaScript.
464 ExpectFieldValue("firstname", "M");
465 ExpectFieldValue("lastname", std::string());
466 ExpectFieldValue("address1", std::string());
467 ExpectFieldValue("address2", std::string());
468 ExpectFieldValue("city", std::string());
469 ExpectFieldValue("state", std::string());
470 ExpectFieldValue("zip", std::string());
471 ExpectFieldValue("country", std::string());
472 ExpectFieldValue("phone", std::string());
473 // TODO(isherman): It would be nice to test that the previewed values are
474 // displayed: http://crbug.com/57220
476 // Press Enter to accept the autofill suggestions.
477 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
479 // The form should be filled.
480 ExpectFilledTestForm();
483 AutofillManagerTestDelegateImpl
* test_delegate() { return &test_delegate_
; }
486 AutofillManagerTestDelegateImpl test_delegate_
;
488 net::TestURLFetcherFactory url_fetcher_factory_
;
490 // KeyPressEventCallback that serves as a sink to ensure that every key press
491 // event the tests create and have the WebContents forward is handled by some
492 // key press event callback. It is necessary to have this sinkbecause if no
493 // key press event callback handles the event (at least on Mac), a DCHECK
494 // ends up going off that the |event| doesn't have an |os_event| associated
496 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_
;
498 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest
);
501 // Test that basic form fill is working.
502 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, BasicFormFill
) {
505 // Load the test page.
506 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
507 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
513 // Test that form filling can be initiated by pressing the down arrow.
514 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillViaDownArrow
) {
517 // Load the test page.
518 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
519 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
521 // Focus a fillable field.
522 FocusFirstNameField();
524 // Press the down arrow to initiate Autofill and wait for the popup to be
526 SendKeyToPageAndWait(ui::VKEY_DOWN
);
528 // Press the down arrow to select the suggestion and preview the autofilled
530 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
532 // Press Enter to accept the autofill suggestions.
533 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
535 // The form should be filled.
536 ExpectFilledTestForm();
539 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillSelectViaTab
) {
542 // Load the test page.
543 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
544 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
546 // Focus a fillable field.
547 FocusFirstNameField();
549 // Press the down arrow to initiate Autofill and wait for the popup to be
551 SendKeyToPageAndWait(ui::VKEY_DOWN
);
553 // Press the down arrow to select the suggestion and preview the autofilled
555 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
557 // Press tab to accept the autofill suggestions.
558 SendKeyToPopupAndWait(ui::VKEY_TAB
);
560 // The form should be filled.
561 ExpectFilledTestForm();
564 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillViaClick
) {
567 // Load the test page.
568 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
569 browser(), GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
570 // Focus a fillable field.
571 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
574 test_delegate()->Reset();
575 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
576 test_delegate()->Wait();
578 // Press the down arrow to select the suggestion and preview the autofilled
580 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
582 // Press Enter to accept the autofill suggestions.
583 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
585 // The form should be filled.
586 ExpectFilledTestForm();
589 // Makes sure that the first click does *not* activate the popup.
590 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, DontAutofillForFirstClick
) {
593 // Load the test page.
594 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
595 browser(), GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
597 // Click the first name field while it's out of focus, then twiddle our thumbs
598 // a bit. If a popup were to show, it would hit the asserts in
599 // AutofillManagerTestDelegateImpl while we're wasting time.
600 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
601 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
603 // The second click should activate the popup since the first click focused
605 test_delegate()->Reset();
606 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
607 test_delegate()->Wait();
610 // Makes sure that clicking outside the focused field doesn't activate
612 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, DontAutofillForOutsideClick
) {
615 // Load the test page.
616 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
618 GURL(std::string(kDataURIPrefix
) + kTestFormString
+
619 "<button disabled id='disabled-button'>Cant click this</button>")));
621 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
623 // Clicking a disabled button will generate a mouse event but focus doesn't
624 // change. This tests that autofill can handle a mouse event outside a focused
625 // input *without* showing the popup.
626 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("disabled-button"));
627 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
629 test_delegate()->Reset();
630 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
631 test_delegate()->Wait();
634 // Test that a field is still autofillable after the previously autofilled
636 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, OnDeleteValueAfterAutofill
) {
639 // Load the test page.
640 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
641 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
643 // Invoke and accept the Autofill popup and verify the form was filled.
644 FocusFirstNameField();
645 SendKeyToPageAndWait(ui::VKEY_M
);
646 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
647 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
648 ExpectFilledTestForm();
650 // Delete the value of a filled field.
651 ASSERT_TRUE(content::ExecuteScript(
653 "document.getElementById('firstname').value = '';"));
654 ExpectFieldValue("firstname", "");
656 // Invoke and accept the Autofill popup and verify the field was filled.
657 SendKeyToPageAndWait(ui::VKEY_M
);
658 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
659 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
660 ExpectFieldValue("firstname", "Milton");
663 // Test that an input field is not rendered with the yellow autofilled
664 // background color when choosing an option from the datalist suggestion list.
665 #if defined(OS_MACOSX)
666 // Flakily triggers and assert on Mac.
667 // http://crbug.com/419868
668 #define MAYBE_OnSelectOptionFromDatalist DISABLED_OnSelectOptionFromDatalist
670 #define MAYBE_OnSelectOptionFromDatalist OnSelectOptionFromDatalist
672 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
673 MAYBE_OnSelectOptionFromDatalist
) {
674 // Load the test page.
675 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
677 GURL(std::string(kDataURIPrefix
) +
678 "<form action=\"http://www.example.com/\" method=\"POST\">"
679 " <input list=\"dl\" type=\"search\" id=\"firstname\""
680 " onfocus=\"domAutomationController.send(true)\"><br>"
681 " <datalist id=\"dl\">"
682 " <option value=\"Adam\"></option>"
683 " <option value=\"Bob\"></option>"
684 " <option value=\"Carl\"></option>"
687 std::string orginalcolor
;
688 GetFieldBackgroundColor("firstname", &orginalcolor
);
690 FocusFirstNameField();
691 SendKeyToPageAndWait(ui::VKEY_DOWN
);
692 SendKeyToDataListPopup(ui::VKEY_DOWN
);
693 SendKeyToDataListPopup(ui::VKEY_RETURN
);
694 ExpectFieldValue("firstname", "Adam");
696 GetFieldBackgroundColor("firstname", &color
);
697 EXPECT_EQ(color
, orginalcolor
);
700 // Test that a JavaScript oninput event is fired after auto-filling a form.
701 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, OnInputAfterAutofill
) {
704 const char kOnInputScript
[] =
706 "focused_fired = false;"
707 "unfocused_fired = false;"
708 "changed_select_fired = false;"
709 "unchanged_select_fired = false;"
710 "document.getElementById('firstname').oninput = function() {"
711 " focused_fired = true;"
713 "document.getElementById('lastname').oninput = function() {"
714 " unfocused_fired = true;"
716 "document.getElementById('state').oninput = function() {"
717 " changed_select_fired = true;"
719 "document.getElementById('country').oninput = function() {"
720 " unchanged_select_fired = true;"
722 "document.getElementById('country').value = 'US';"
725 // Load the test page.
726 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
727 GURL(std::string(kDataURIPrefix
) + kTestFormString
+ kOnInputScript
)));
730 FocusFirstNameField();
732 // Start filling the first name field with "M" and wait for the popup to be
734 SendKeyToPageAndWait(ui::VKEY_M
);
736 // Press the down arrow to select the suggestion and preview the autofilled
738 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
740 // Press Enter to accept the autofill suggestions.
741 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
743 // The form should be filled.
744 ExpectFilledTestForm();
746 bool focused_fired
= false;
747 bool unfocused_fired
= false;
748 bool changed_select_fired
= false;
749 bool unchanged_select_fired
= false;
750 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
752 "domAutomationController.send(focused_fired);",
754 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
756 "domAutomationController.send(unfocused_fired);",
758 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
760 "domAutomationController.send(changed_select_fired);",
761 &changed_select_fired
));
762 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
764 "domAutomationController.send(unchanged_select_fired);",
765 &unchanged_select_fired
));
766 EXPECT_TRUE(focused_fired
);
767 EXPECT_TRUE(unfocused_fired
);
768 EXPECT_TRUE(changed_select_fired
);
769 EXPECT_FALSE(unchanged_select_fired
);
772 // Test that a JavaScript onchange event is fired after auto-filling a form.
773 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, OnChangeAfterAutofill
) {
776 const char kOnChangeScript
[] =
778 "focused_fired = false;"
779 "unfocused_fired = false;"
780 "changed_select_fired = false;"
781 "unchanged_select_fired = false;"
782 "document.getElementById('firstname').onchange = function() {"
783 " focused_fired = true;"
785 "document.getElementById('lastname').onchange = function() {"
786 " unfocused_fired = true;"
788 "document.getElementById('state').onchange = function() {"
789 " changed_select_fired = true;"
791 "document.getElementById('country').onchange = function() {"
792 " unchanged_select_fired = true;"
794 "document.getElementById('country').value = 'US';"
797 // Load the test page.
798 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
799 GURL(std::string(kDataURIPrefix
) + kTestFormString
+ kOnChangeScript
)));
802 FocusFirstNameField();
804 // Start filling the first name field with "M" and wait for the popup to be
806 SendKeyToPageAndWait(ui::VKEY_M
);
808 // Press the down arrow to select the suggestion and preview the autofilled
810 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
812 // Press Enter to accept the autofill suggestions.
813 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
815 // The form should be filled.
816 ExpectFilledTestForm();
818 bool focused_fired
= false;
819 bool unfocused_fired
= false;
820 bool changed_select_fired
= false;
821 bool unchanged_select_fired
= false;
822 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
824 "domAutomationController.send(focused_fired);",
826 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
828 "domAutomationController.send(unfocused_fired);",
830 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
832 "domAutomationController.send(changed_select_fired);",
833 &changed_select_fired
));
834 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
836 "domAutomationController.send(unchanged_select_fired);",
837 &unchanged_select_fired
));
838 EXPECT_TRUE(focused_fired
);
839 EXPECT_TRUE(unfocused_fired
);
840 EXPECT_TRUE(changed_select_fired
);
841 EXPECT_FALSE(unchanged_select_fired
);
844 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, InputFiresBeforeChange
) {
847 const char kInputFiresBeforeChangeScript
[] =
849 "inputElementEvents = [];"
850 "function recordInputElementEvent(e) {"
851 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';"
852 " inputElementEvents.push(e.type);"
854 "selectElementEvents = [];"
855 "function recordSelectElementEvent(e) {"
856 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';"
857 " selectElementEvents.push(e.type);"
859 "document.getElementById('lastname').oninput = recordInputElementEvent;"
860 "document.getElementById('lastname').onchange = recordInputElementEvent;"
861 "document.getElementById('country').oninput = recordSelectElementEvent;"
862 "document.getElementById('country').onchange = recordSelectElementEvent;"
865 // Load the test page.
866 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
867 GURL(std::string(kDataURIPrefix
) + kTestFormString
+
868 kInputFiresBeforeChangeScript
)));
870 // Invoke and accept the Autofill popup and verify the form was filled.
871 FocusFirstNameField();
872 SendKeyToPageAndWait(ui::VKEY_M
);
873 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
874 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
875 ExpectFilledTestForm();
877 int num_input_element_events
= -1;
878 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
880 "domAutomationController.send(inputElementEvents.length);",
881 &num_input_element_events
));
882 EXPECT_EQ(2, num_input_element_events
);
884 std::vector
<std::string
> input_element_events
;
885 input_element_events
.resize(2);
887 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
889 "domAutomationController.send(inputElementEvents[0]);",
890 &input_element_events
[0]));
891 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
893 "domAutomationController.send(inputElementEvents[1]);",
894 &input_element_events
[1]));
896 EXPECT_EQ("input", input_element_events
[0]);
897 EXPECT_EQ("change", input_element_events
[1]);
899 int num_select_element_events
= -1;
900 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
902 "domAutomationController.send(selectElementEvents.length);",
903 &num_select_element_events
));
904 EXPECT_EQ(2, num_select_element_events
);
906 std::vector
<std::string
> select_element_events
;
907 select_element_events
.resize(2);
909 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
911 "domAutomationController.send(selectElementEvents[0]);",
912 &select_element_events
[0]));
913 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
915 "domAutomationController.send(selectElementEvents[1]);",
916 &select_element_events
[1]));
918 EXPECT_EQ("input", select_element_events
[0]);
919 EXPECT_EQ("change", select_element_events
[1]);
922 // Test that we can autofill forms distinguished only by their |id| attribute.
923 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
924 AutofillFormsDistinguishedById
) {
927 // Load the test page.
928 const std::string kURL
=
929 std::string(kDataURIPrefix
) + kTestFormString
+
931 "var mainForm = document.forms[0];"
932 "mainForm.id = 'mainForm';"
933 "var newForm = document.createElement('form');"
934 "newForm.action = mainForm.action;"
935 "newForm.method = mainForm.method;"
936 "newForm.id = 'newForm';"
937 "mainForm.parentNode.insertBefore(newForm, mainForm);"
939 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL
)));
945 // Test that we properly autofill forms with repeated fields.
946 // In the wild, the repeated fields are typically either email fields
947 // (duplicated for "confirmation"); or variants that are hot-swapped via
948 // JavaScript, with only one actually visible at any given time.
949 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillFormWithRepeatedField
) {
952 // Load the test page.
953 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
954 GURL(std::string(kDataURIPrefix
) +
955 "<form action=\"http://www.example.com/\" method=\"POST\">"
956 "<label for=\"firstname\">First name:</label>"
957 " <input type=\"text\" id=\"firstname\""
958 " onfocus=\"domAutomationController.send(true)\"><br>"
959 "<label for=\"lastname\">Last name:</label>"
960 " <input type=\"text\" id=\"lastname\"><br>"
961 "<label for=\"address1\">Address line 1:</label>"
962 " <input type=\"text\" id=\"address1\"><br>"
963 "<label for=\"address2\">Address line 2:</label>"
964 " <input type=\"text\" id=\"address2\"><br>"
965 "<label for=\"city\">City:</label>"
966 " <input type=\"text\" id=\"city\"><br>"
967 "<label for=\"state\">State:</label>"
968 " <select id=\"state\">"
969 " <option value=\"\" selected=\"yes\">--</option>"
970 " <option value=\"CA\">California</option>"
971 " <option value=\"TX\">Texas</option>"
973 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
974 " <input type=\"text\" id=\"state_freeform\""
975 " style=\"display:none\"><br>"
976 "<label for=\"zip\">ZIP code:</label>"
977 " <input type=\"text\" id=\"zip\"><br>"
978 "<label for=\"country\">Country:</label>"
979 " <select id=\"country\">"
980 " <option value=\"\" selected=\"yes\">--</option>"
981 " <option value=\"CA\">Canada</option>"
982 " <option value=\"US\">United States</option>"
984 "<label for=\"phone\">Phone number:</label>"
985 " <input type=\"text\" id=\"phone\"><br>"
990 ExpectFieldValue("state_freeform", std::string());
993 // Test that we properly autofill forms with non-autofillable fields.
994 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
995 AutofillFormWithNonAutofillableField
) {
998 // Load the test page.
999 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1000 GURL(std::string(kDataURIPrefix
) +
1001 "<form action=\"http://www.example.com/\" method=\"POST\">"
1002 "<label for=\"firstname\">First name:</label>"
1003 " <input type=\"text\" id=\"firstname\""
1004 " onfocus=\"domAutomationController.send(true)\"><br>"
1005 "<label for=\"middlename\">Middle name:</label>"
1006 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
1007 "<label for=\"lastname\">Last name:</label>"
1008 " <input type=\"text\" id=\"lastname\"><br>"
1009 "<label for=\"address1\">Address line 1:</label>"
1010 " <input type=\"text\" id=\"address1\"><br>"
1011 "<label for=\"address2\">Address line 2:</label>"
1012 " <input type=\"text\" id=\"address2\"><br>"
1013 "<label for=\"city\">City:</label>"
1014 " <input type=\"text\" id=\"city\"><br>"
1015 "<label for=\"state\">State:</label>"
1016 " <select id=\"state\">"
1017 " <option value=\"\" selected=\"yes\">--</option>"
1018 " <option value=\"CA\">California</option>"
1019 " <option value=\"TX\">Texas</option>"
1021 "<label for=\"zip\">ZIP code:</label>"
1022 " <input type=\"text\" id=\"zip\"><br>"
1023 "<label for=\"country\">Country:</label>"
1024 " <select id=\"country\">"
1025 " <option value=\"\" selected=\"yes\">--</option>"
1026 " <option value=\"CA\">Canada</option>"
1027 " <option value=\"US\">United States</option>"
1029 "<label for=\"phone\">Phone number:</label>"
1030 " <input type=\"text\" id=\"phone\"><br>"
1037 // Test that we can Autofill dynamically generated forms.
1038 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, DynamicFormFill
) {
1039 CreateTestProfile();
1041 // Load the test page.
1042 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1043 GURL(std::string(kDataURIPrefix
) +
1044 "<form id=\"form\" action=\"http://www.example.com/\""
1045 " method=\"POST\"></form>"
1047 "function AddElement(name, label) {"
1048 " var form = document.getElementById('form');"
1050 " var label_text = document.createTextNode(label);"
1051 " var label_element = document.createElement('label');"
1052 " label_element.setAttribute('for', name);"
1053 " label_element.appendChild(label_text);"
1054 " form.appendChild(label_element);"
1056 " if (name === 'state' || name === 'country') {"
1057 " var select_element = document.createElement('select');"
1058 " select_element.setAttribute('id', name);"
1059 " select_element.setAttribute('name', name);"
1061 " /* Add an empty selected option. */"
1062 " var default_option = new Option('--', '', true);"
1063 " select_element.appendChild(default_option);"
1065 " /* Add the other options. */"
1066 " if (name == 'state') {"
1067 " var option1 = new Option('California', 'CA');"
1068 " select_element.appendChild(option1);"
1069 " var option2 = new Option('Texas', 'TX');"
1070 " select_element.appendChild(option2);"
1072 " var option1 = new Option('Canada', 'CA');"
1073 " select_element.appendChild(option1);"
1074 " var option2 = new Option('United States', 'US');"
1075 " select_element.appendChild(option2);"
1078 " form.appendChild(select_element);"
1080 " var input_element = document.createElement('input');"
1081 " input_element.setAttribute('id', name);"
1082 " input_element.setAttribute('name', name);"
1084 " /* Add the onfocus listener to the 'firstname' field. */"
1085 " if (name === 'firstname') {"
1086 " input_element.onfocus = function() {"
1087 " domAutomationController.send(true);"
1091 " form.appendChild(input_element);"
1094 " form.appendChild(document.createElement('br'));"
1097 "function BuildForm() {"
1099 " ['firstname', 'First name:'],"
1100 " ['lastname', 'Last name:'],"
1101 " ['address1', 'Address line 1:'],"
1102 " ['address2', 'Address line 2:'],"
1103 " ['city', 'City:'],"
1104 " ['state', 'State:'],"
1105 " ['zip', 'ZIP code:'],"
1106 " ['country', 'Country:'],"
1107 " ['phone', 'Phone number:'],"
1110 " for (var i = 0; i < elements.length; i++) {"
1111 " var name = elements[i][0];"
1112 " var label = elements[i][1];"
1113 " AddElement(name, label);"
1118 // Dynamically construct the form.
1119 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
1125 // Test that form filling works after reloading the current page.
1126 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillAfterReload
) {
1127 CreateTestProfile();
1129 // Load the test page.
1130 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1131 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
1134 content::WebContents
* web_contents
= GetWebContents();
1135 web_contents
->GetController().Reload(false);
1136 content::WaitForLoadStop(web_contents
);
1142 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillAfterTranslate
) {
1143 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1144 if (TranslateService::IsTranslateBubbleEnabled())
1147 CreateTestProfile();
1149 GURL
url(std::string(kDataURIPrefix
) +
1150 "<form action=\"http://www.example.com/\" method=\"POST\">"
1151 "<label for=\"fn\">なまえ</label>"
1152 " <input type=\"text\" id=\"fn\""
1153 " onfocus=\"domAutomationController.send(true)\""
1155 "<label for=\"ln\">みょうじ</label>"
1156 " <input type=\"text\" id=\"ln\"><br>"
1157 "<label for=\"a1\">Address line 1:</label>"
1158 " <input type=\"text\" id=\"a1\"><br>"
1159 "<label for=\"a2\">Address line 2:</label>"
1160 " <input type=\"text\" id=\"a2\"><br>"
1161 "<label for=\"ci\">City:</label>"
1162 " <input type=\"text\" id=\"ci\"><br>"
1163 "<label for=\"st\">State:</label>"
1164 " <select id=\"st\">"
1165 " <option value=\"\" selected=\"yes\">--</option>"
1166 " <option value=\"CA\">California</option>"
1167 " <option value=\"TX\">Texas</option>"
1169 "<label for=\"z\">ZIP code:</label>"
1170 " <input type=\"text\" id=\"z\"><br>"
1171 "<label for=\"co\">Country:</label>"
1172 " <select id=\"co\">"
1173 " <option value=\"\" selected=\"yes\">--</option>"
1174 " <option value=\"CA\">Canada</option>"
1175 " <option value=\"US\">United States</option>"
1177 "<label for=\"ph\">Phone number:</label>"
1178 " <input type=\"text\" id=\"ph\"><br>"
1180 // Add additional Japanese characters to ensure the translate bar
1182 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
1183 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
1185 content::WindowedNotificationObserver
infobar_observer(
1186 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
1187 content::NotificationService::AllSources());
1188 ASSERT_NO_FATAL_FAILURE(
1189 ui_test_utils::NavigateToURL(browser(), url
));
1191 // Wait for the translation bar to appear and get it.
1192 infobar_observer
.Wait();
1193 InfoBarService
* infobar_service
=
1194 InfoBarService::FromWebContents(GetWebContents());
1195 translate::TranslateInfoBarDelegate
* delegate
=
1196 infobar_service
->infobar_at(0)->delegate()->AsTranslateInfoBarDelegate();
1197 ASSERT_TRUE(delegate
);
1198 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE
,
1199 delegate
->translate_step());
1201 // Simulate translation button press.
1202 delegate
->Translate();
1204 content::WindowedNotificationObserver
translation_observer(
1205 chrome::NOTIFICATION_PAGE_TRANSLATED
,
1206 content::NotificationService::AllSources());
1208 // Simulate the translate script being retrieved.
1209 // Pass fake google.translate lib as the translate script.
1210 SimulateURLFetch(true);
1212 // Simulate the render notifying the translation has been done.
1213 translation_observer
.Wait();
1218 // Test phone fields parse correctly from a given profile.
1219 // The high level key presses execute the following: Select the first text
1220 // field, invoke the autofill popup list, select the first profile within the
1221 // list, and commit to the profile to populate the form.
1222 // Flakily times out on windows. http://crbug.com/390564
1224 #define MAYBE_ComparePhoneNumbers DISABLED_ComparePhoneNumbers
1226 #define MAYBE_ComparePhoneNumbers ComparePhoneNumbers
1228 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, MAYBE_ComparePhoneNumbers
) {
1229 ASSERT_TRUE(test_server()->Start());
1231 AutofillProfile profile
;
1232 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Bob"));
1233 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
1234 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("1234 H St."));
1235 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("San Jose"));
1236 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
1237 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("95110"));
1238 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("1-408-555-4567"));
1239 SetProfile(profile
);
1241 GURL url
= test_server()->GetURL("files/autofill/form_phones.html");
1242 ui_test_utils::NavigateToURL(browser(), url
);
1243 PopulateForm("NAME_FIRST");
1245 ExpectFieldValue("NAME_FIRST", "Bob");
1246 ExpectFieldValue("NAME_LAST", "Smith");
1247 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
1248 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
1249 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
1250 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
1251 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
1252 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
1253 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
1254 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
1255 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
1256 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
1257 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
1258 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
1259 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
1260 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
1261 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
1264 // Test that Autofill does not fill in read-only fields.
1265 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, NoAutofillForReadOnlyFields
) {
1266 ASSERT_TRUE(test_server()->Start());
1268 std::string
addr_line1("1234 H St.");
1270 AutofillProfile profile
;
1271 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Bob"));
1272 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
1273 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("bsmith@gmail.com"));
1274 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16(addr_line1
));
1275 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("San Jose"));
1276 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
1277 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("95110"));
1278 profile
.SetRawInfo(COMPANY_NAME
, ASCIIToUTF16("Company X"));
1279 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("408-871-4567"));
1280 SetProfile(profile
);
1282 GURL url
= test_server()->GetURL("files/autofill/read_only_field_test.html");
1283 ui_test_utils::NavigateToURL(browser(), url
);
1284 PopulateForm("firstname");
1286 ExpectFieldValue("email", std::string());
1287 ExpectFieldValue("address", addr_line1
);
1290 // Test form is fillable from a profile after form was reset.
1292 // 1. Fill form using a saved profile.
1293 // 2. Reset the form.
1294 // 3. Fill form using a saved profile.
1295 // Flakily times out: http://crbug.com/270341
1296 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, DISABLED_FormFillableOnReset
) {
1297 ASSERT_TRUE(test_server()->Start());
1299 CreateTestProfile();
1301 GURL url
= test_server()->GetURL("files/autofill/autofill_test_form.html");
1302 ui_test_utils::NavigateToURL(browser(), url
);
1303 PopulateForm("NAME_FIRST");
1305 ASSERT_TRUE(content::ExecuteScript(
1306 GetWebContents(), "document.getElementById('testform').reset()"));
1308 PopulateForm("NAME_FIRST");
1310 ExpectFieldValue("NAME_FIRST", "Milton");
1311 ExpectFieldValue("NAME_LAST", "Waddams");
1312 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
1313 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
1314 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
1315 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
1316 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
1317 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
1318 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
1321 // Test Autofill distinguishes a middle initial in a name.
1322 // Flakily times out: http://crbug.com/270341
1323 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
1324 DISABLED_DistinguishMiddleInitialWithinName
) {
1325 ASSERT_TRUE(test_server()->Start());
1327 CreateTestProfile();
1329 GURL url
= test_server()->GetURL(
1330 "files/autofill/autofill_middleinit_form.html");
1331 ui_test_utils::NavigateToURL(browser(), url
);
1332 PopulateForm("NAME_FIRST");
1334 ExpectFieldValue("NAME_MIDDLE", "C");
1337 // Test forms with multiple email addresses are filled properly.
1338 // Entire form should be filled with one user gesture.
1339 // Flakily times out: http://crbug.com/270341
1340 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
1341 DISABLED_MultipleEmailFilledByOneUserGesture
) {
1342 ASSERT_TRUE(test_server()->Start());
1344 std::string
email("bsmith@gmail.com");
1346 AutofillProfile profile
;
1347 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Bob"));
1348 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
1349 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16(email
));
1350 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("4088714567"));
1351 SetProfile(profile
);
1353 GURL url
= test_server()->GetURL(
1354 "files/autofill/autofill_confirmemail_form.html");
1355 ui_test_utils::NavigateToURL(browser(), url
);
1356 PopulateForm("NAME_FIRST");
1358 ExpectFieldValue("EMAIL_CONFIRM", email
);
1359 // TODO(isherman): verify entire form.
1362 // http://crbug.com/281527
1363 #if defined(OS_MACOSX)
1364 #define MAYBE_FormFillLatencyAfterSubmit FormFillLatencyAfterSubmit
1366 #define MAYBE_FormFillLatencyAfterSubmit DISABLED_FormFillLatencyAfterSubmit
1368 // Test latency time on form submit with lots of stored Autofill profiles.
1369 // This test verifies when a profile is selected from the Autofill dictionary
1370 // that consists of thousands of profiles, the form does not hang after being
1372 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
1373 MAYBE_FormFillLatencyAfterSubmit
) {
1374 ASSERT_TRUE(test_server()->Start());
1376 std::vector
<std::string
> cities
;
1377 cities
.push_back("San Jose");
1378 cities
.push_back("San Francisco");
1379 cities
.push_back("Sacramento");
1380 cities
.push_back("Los Angeles");
1382 std::vector
<std::string
> streets
;
1383 streets
.push_back("St");
1384 streets
.push_back("Ave");
1385 streets
.push_back("Ln");
1386 streets
.push_back("Ct");
1388 const int kNumProfiles
= 1500;
1389 base::Time start_time
= base::Time::Now();
1390 std::vector
<AutofillProfile
> profiles
;
1391 for (int i
= 0; i
< kNumProfiles
; i
++) {
1392 AutofillProfile profile
;
1393 base::string16
name(base::IntToString16(i
));
1394 base::string16
email(name
+ ASCIIToUTF16("@example.com"));
1395 base::string16 street
= ASCIIToUTF16(
1396 base::IntToString(base::RandInt(0, 10000)) + " " +
1397 streets
[base::RandInt(0, streets
.size() - 1)]);
1398 base::string16 city
=
1399 ASCIIToUTF16(cities
[base::RandInt(0, cities
.size() - 1)]);
1400 base::string16
zip(base::IntToString16(base::RandInt(0, 10000)));
1401 profile
.SetRawInfo(NAME_FIRST
, name
);
1402 profile
.SetRawInfo(EMAIL_ADDRESS
, email
);
1403 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, street
);
1404 profile
.SetRawInfo(ADDRESS_HOME_CITY
, city
);
1405 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
1406 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, zip
);
1407 profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
1408 profiles
.push_back(profile
);
1410 SetProfiles(&profiles
);
1411 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1413 LOG(INFO
) << "Created " << kNumProfiles
<< " profiles in " <<
1414 (base::Time::Now() - start_time
).InSeconds() << " seconds.";
1416 GURL url
= test_server()->GetURL(
1417 "files/autofill/latency_after_submit_test.html");
1418 ui_test_utils::NavigateToURL(browser(), url
);
1419 PopulateForm("NAME_FIRST");
1421 content::WindowedNotificationObserver
load_stop_observer(
1422 content::NOTIFICATION_LOAD_STOP
,
1423 content::Source
<content::NavigationController
>(
1424 &GetWebContents()->GetController()));
1426 ASSERT_TRUE(content::ExecuteScript(
1427 GetRenderViewHost(),
1428 "document.getElementById('testform').submit();"));
1429 // This will ensure the test didn't hang.
1430 load_stop_observer
.Wait();
1433 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1434 // is interacting with the form. This is a regression test for
1435 // http://crbug.com/160476
1436 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
1437 DisableAutocompleteWhileFilling
) {
1438 CreateTestProfile();
1440 // Load the test page.
1441 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1442 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
1444 // Invoke Autofill: Start filling the first name field with "M" and wait for
1445 // the popup to be shown.
1446 FocusFirstNameField();
1447 SendKeyToPageAndWait(ui::VKEY_M
);
1449 // Now that the popup with suggestions is showing, disable autocomplete for
1450 // the active field.
1451 ASSERT_TRUE(content::ExecuteScript(
1452 GetRenderViewHost(),
1453 "document.querySelector('input').autocomplete = 'off';"));
1455 // Press the down arrow to select the suggestion and attempt to preview the
1457 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
1460 } // namespace autofill