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/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/confirm_infobar_delegate.h"
21 #include "chrome/browser/infobars/infobar.h"
22 #include "chrome/browser/infobars/infobar_service.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/translate/translate_infobar_delegate.h"
25 #include "chrome/browser/translate/translate_manager.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/render_messages.h"
30 #include "chrome/test/base/in_process_browser_test.h"
31 #include "chrome/test/base/test_switches.h"
32 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/autofill/content/browser/autofill_driver_impl.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 "content/public/browser/navigation_controller.h"
42 #include "content/public/browser/notification_observer.h"
43 #include "content/public/browser/notification_registrar.h"
44 #include "content/public/browser/notification_service.h"
45 #include "content/public/browser/render_view_host.h"
46 #include "content/public/browser/render_widget_host.h"
47 #include "content/public/browser/web_contents.h"
48 #include "content/public/test/browser_test_utils.h"
49 #include "content/public/test/test_renderer_host.h"
50 #include "content/public/test/test_utils.h"
51 #include "net/url_request/test_url_fetcher_factory.h"
52 #include "testing/gmock/include/gmock/gmock.h"
53 #include "testing/gtest/include/gtest/gtest.h"
54 #include "ui/events/keycodes/keyboard_codes.h"
56 using base::ASCIIToUTF16
;
60 static const char* kDataURIPrefix
= "data:text/html;charset=utf-8,";
61 static const char* kTestFormString
=
62 "<form action=\"http://www.example.com/\" method=\"POST\">"
63 "<label for=\"firstname\">First name:</label>"
64 " <input type=\"text\" id=\"firstname\""
65 " onFocus=\"domAutomationController.send(true)\"><br>"
66 "<label for=\"lastname\">Last name:</label>"
67 " <input type=\"text\" id=\"lastname\"><br>"
68 "<label for=\"address1\">Address line 1:</label>"
69 " <input type=\"text\" id=\"address1\"><br>"
70 "<label for=\"address2\">Address line 2:</label>"
71 " <input type=\"text\" id=\"address2\"><br>"
72 "<label for=\"city\">City:</label>"
73 " <input type=\"text\" id=\"city\"><br>"
74 "<label for=\"state\">State:</label>"
75 " <select id=\"state\">"
76 " <option value=\"\" selected=\"yes\">--</option>"
77 " <option value=\"CA\">California</option>"
78 " <option value=\"TX\">Texas</option>"
80 "<label for=\"zip\">ZIP code:</label>"
81 " <input type=\"text\" id=\"zip\"><br>"
82 "<label for=\"country\">Country:</label>"
83 " <select id=\"country\">"
84 " <option value=\"\" selected=\"yes\">--</option>"
85 " <option value=\"CA\">Canada</option>"
86 " <option value=\"US\">United States</option>"
88 "<label for=\"phone\">Phone number:</label>"
89 " <input type=\"text\" id=\"phone\"><br>"
93 // AutofillManagerTestDelegateImpl --------------------------------------------
95 class AutofillManagerTestDelegateImpl
96 : public autofill::AutofillManagerTestDelegate
{
98 AutofillManagerTestDelegateImpl() {}
99 virtual ~AutofillManagerTestDelegateImpl() {}
101 // autofill::AutofillManagerTestDelegate:
102 virtual void DidPreviewFormData() OVERRIDE
{
103 loop_runner_
->Quit();
106 virtual void DidFillFormData() OVERRIDE
{
107 loop_runner_
->Quit();
110 virtual void DidShowSuggestions() OVERRIDE
{
111 loop_runner_
->Quit();
115 loop_runner_
= new content::MessageLoopRunner();
123 scoped_refptr
<content::MessageLoopRunner
> loop_runner_
;
125 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl
);
129 // WindowedPersonalDataManagerObserver ----------------------------------------
131 class WindowedPersonalDataManagerObserver
132 : public PersonalDataManagerObserver
,
133 public content::NotificationObserver
{
135 explicit WindowedPersonalDataManagerObserver(Browser
* browser
)
137 has_run_message_loop_(false),
139 infobar_service_(NULL
) {
140 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
142 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
143 content::NotificationService::AllSources());
146 virtual ~WindowedPersonalDataManagerObserver() {
147 if (infobar_service_
) {
148 while (infobar_service_
->infobar_count() > 0) {
149 infobar_service_
->RemoveInfoBar(infobar_service_
->infobar_at(0));
154 // PersonalDataManagerObserver:
155 virtual void OnPersonalDataChanged() OVERRIDE
{
156 if (has_run_message_loop_
) {
157 base::MessageLoopForUI::current()->Quit();
158 has_run_message_loop_
= false;
163 virtual void OnInsufficientFormData() OVERRIDE
{
164 OnPersonalDataChanged();
167 // content::NotificationObserver:
168 virtual void Observe(int type
,
169 const content::NotificationSource
& source
,
170 const content::NotificationDetails
& details
) OVERRIDE
{
171 infobar_service_
= InfoBarService::FromWebContents(
172 browser_
->tab_strip_model()->GetActiveWebContents());
173 infobar_service_
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()->
179 has_run_message_loop_
= true;
180 content::RunMessageLoop();
182 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
183 RemoveObserver(this);
188 bool has_run_message_loop_
;
190 content::NotificationRegistrar registrar_
;
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 virtual ~AutofillInteractiveTest() {}
206 // InProcessBrowserTest:
207 virtual void SetUpOnMainThread() OVERRIDE
{
208 // Don't want Keychain coming up on Mac.
209 test::DisableSystemServices(browser()->profile());
211 // Inject the test delegate into the AutofillManager.
212 content::WebContents
* web_contents
= GetWebContents();
213 AutofillDriverImpl
* autofill_driver
=
214 AutofillDriverImpl::FromWebContents(web_contents
);
215 AutofillManager
* autofill_manager
= autofill_driver
->autofill_manager();
216 autofill_manager
->SetTestDelegate(&test_delegate_
);
219 virtual void CleanUpOnMainThread() OVERRIDE
{
220 // Make sure to close any showing popups prior to tearing down the UI.
221 content::WebContents
* web_contents
= GetWebContents();
222 AutofillManager
* autofill_manager
=
223 AutofillDriverImpl::FromWebContents(web_contents
)->autofill_manager();
224 autofill_manager
->delegate()->HideAutofillPopup();
227 PersonalDataManager
* GetPersonalDataManager() {
228 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
231 content::WebContents
* GetWebContents() {
232 return browser()->tab_strip_model()->GetActiveWebContents();
235 content::RenderViewHost
* GetRenderViewHost() {
236 return GetWebContents()->GetRenderViewHost();
239 void CreateTestProfile() {
240 AutofillProfile profile
;
241 test::SetProfileInfo(
242 &profile
, "Milton", "C.", "Waddams",
243 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane",
244 "Basement", "Austin", "Texas", "78744", "US", "5125551234");
246 WindowedPersonalDataManagerObserver
observer(browser());
247 GetPersonalDataManager()->AddProfile(profile
);
249 // AddProfile is asynchronous. Wait for it to finish before continuing the
254 void SetProfiles(std::vector
<AutofillProfile
>* profiles
) {
255 WindowedPersonalDataManagerObserver
observer(browser());
256 GetPersonalDataManager()->SetProfiles(profiles
);
260 void SetProfile(const AutofillProfile
& profile
) {
261 std::vector
<AutofillProfile
> profiles
;
262 profiles
.push_back(profile
);
263 SetProfiles(&profiles
);
266 // Populates a webpage form using autofill data and keypress events.
267 // This function focuses the specified input field in the form, and then
268 // sends keypress events to the tab to cause the form to be populated.
269 void PopulateForm(const std::string
& field_id
) {
270 std::string
js("document.getElementById('" + field_id
+ "').focus();");
271 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js
));
273 SendKeyToPageAndWait(ui::VKEY_DOWN
);
274 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
275 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
278 void ExpectFieldValue(const std::string
& field_name
,
279 const std::string
& expected_value
) {
281 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
283 "window.domAutomationController.send("
284 " document.getElementById('" + field_name
+ "').value);",
286 EXPECT_EQ(expected_value
, value
);
289 void SimulateURLFetch(bool success
) {
290 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
291 ASSERT_TRUE(fetcher
);
292 net::URLRequestStatus status
;
293 status
.set_status(success
? net::URLRequestStatus::SUCCESS
:
294 net::URLRequestStatus::FAILED
);
296 std::string script
= " var google = {};"
297 "google.translate = (function() {"
299 " TranslateService: function() {"
301 " isAvailable : function() {"
304 " restore : function() {"
307 " getDetectedLanguage : function() {"
310 " translatePage : function(originalLang, targetLang,"
311 " onTranslateProgress) {"
312 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
313 std::string(kTestFormString
) +
315 " onTranslateProgress(100, true, false);"
321 "cr.googleTranslate.onTranslateElementLoad();";
323 fetcher
->set_url(fetcher
->GetOriginalURL());
324 fetcher
->set_status(status
);
325 fetcher
->set_response_code(success
? 200 : 500);
326 fetcher
->SetResponseString(script
);
327 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
330 void FocusFirstNameField() {
332 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
334 "if (document.readyState === 'complete')"
335 " document.getElementById('firstname').focus();"
337 " domAutomationController.send(false);",
342 void ExpectFilledTestForm() {
343 ExpectFieldValue("firstname", "Milton");
344 ExpectFieldValue("lastname", "Waddams");
345 ExpectFieldValue("address1", "4120 Freidrich Lane");
346 ExpectFieldValue("address2", "Basement");
347 ExpectFieldValue("city", "Austin");
348 ExpectFieldValue("state", "TX");
349 ExpectFieldValue("zip", "78744");
350 ExpectFieldValue("country", "US");
351 ExpectFieldValue("phone", "5125551234");
354 void SendKeyToPageAndWait(ui::KeyboardCode key
) {
355 test_delegate_
.Reset();
356 content::SimulateKeyPress(
357 GetWebContents(), key
, false, false, false, false);
358 test_delegate_
.Wait();
361 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent
& event
) {
365 void SendKeyToPopupAndWait(ui::KeyboardCode key
) {
366 // Route popup-targeted key presses via the render view host.
367 content::NativeWebKeyboardEvent event
;
368 event
.windowsKeyCode
= key
;
369 event
.type
= blink::WebKeyboardEvent::RawKeyDown
;
370 test_delegate_
.Reset();
371 // Install the key press event sink to ensure that any events that are not
372 // handled by the installed callbacks do not end up crashing the test.
373 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_
);
374 GetRenderViewHost()->ForwardKeyboardEvent(event
);
375 test_delegate_
.Wait();
376 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_
);
379 void TryBasicFormFill() {
380 FocusFirstNameField();
382 // Start filling the first name field with "M" and wait for the popup to be
384 SendKeyToPageAndWait(ui::VKEY_M
);
386 // Press the down arrow to select the suggestion and preview the autofilled
388 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
390 // The previewed values should not be accessible to JavaScript.
391 ExpectFieldValue("firstname", "M");
392 ExpectFieldValue("lastname", std::string());
393 ExpectFieldValue("address1", std::string());
394 ExpectFieldValue("address2", std::string());
395 ExpectFieldValue("city", std::string());
396 ExpectFieldValue("state", std::string());
397 ExpectFieldValue("zip", std::string());
398 ExpectFieldValue("country", std::string());
399 ExpectFieldValue("phone", std::string());
400 // TODO(isherman): It would be nice to test that the previewed values are
401 // displayed: http://crbug.com/57220
403 // Press Enter to accept the autofill suggestions.
404 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
406 // The form should be filled.
407 ExpectFilledTestForm();
411 AutofillManagerTestDelegateImpl test_delegate_
;
413 net::TestURLFetcherFactory url_fetcher_factory_
;
415 // KeyPressEventCallback that serves as a sink to ensure that every key press
416 // event the tests create and have the WebContents forward is handled by some
417 // key press event callback. It is necessary to have this sinkbecause if no
418 // key press event callback handles the event (at least on Mac), a DCHECK
419 // ends up going off that the |event| doesn't have an |os_event| associated
421 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_
;
423 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest
);
426 // Test that basic form fill is working.
427 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, BasicFormFill
) {
430 // Load the test page.
431 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
432 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
438 // Test that form filling can be initiated by pressing the down arrow.
439 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillViaDownArrow
) {
442 // Load the test page.
443 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
444 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
446 // Focus a fillable field.
447 FocusFirstNameField();
449 // Press the down arrow to initiate Autofill and wait for the popup to be
451 SendKeyToPageAndWait(ui::VKEY_DOWN
);
453 // Press the down arrow to select the suggestion and preview the autofilled
455 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
457 // Press Enter to accept the autofill suggestions.
458 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
460 // The form should be filled.
461 ExpectFilledTestForm();
464 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillSelectViaTab
) {
467 // Load the test page.
468 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
469 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
471 // Focus a fillable field.
472 FocusFirstNameField();
474 // Press the down arrow to initiate Autofill and wait for the popup to be
476 SendKeyToPageAndWait(ui::VKEY_DOWN
);
478 // Press the down arrow to select the suggestion and preview the autofilled
480 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
482 // Press tab to accept the autofill suggestions.
483 SendKeyToPopupAndWait(ui::VKEY_TAB
);
485 // The form should be filled.
486 ExpectFilledTestForm();
489 // Test that a JavaScript onchange event is fired after auto-filling a form.
490 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, OnChangeAfterAutofill
) {
493 const char* kOnChangeScript
=
495 "focused_fired = false;"
496 "unfocused_fired = false;"
497 "changed_select_fired = false;"
498 "unchanged_select_fired = false;"
499 "document.getElementById('firstname').onchange = function() {"
500 " focused_fired = true;"
502 "document.getElementById('lastname').onchange = function() {"
503 " unfocused_fired = true;"
505 "document.getElementById('state').onchange = function() {"
506 " changed_select_fired = true;"
508 "document.getElementById('country').onchange = function() {"
509 " unchanged_select_fired = true;"
511 "document.getElementById('country').value = 'US';"
514 // Load the test page.
515 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
516 GURL(std::string(kDataURIPrefix
) + kTestFormString
+ kOnChangeScript
)));
519 FocusFirstNameField();
521 // Start filling the first name field with "M" and wait for the popup to be
523 SendKeyToPageAndWait(ui::VKEY_M
);
525 // Press the down arrow to select the suggestion and preview the autofilled
527 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
529 // Press Enter to accept the autofill suggestions.
530 SendKeyToPopupAndWait(ui::VKEY_RETURN
);
532 // The form should be filled.
533 ExpectFilledTestForm();
535 // The change event should have already fired for unfocused fields, both of
536 // <input> and of <select> type. However, it should not yet have fired for the
538 bool focused_fired
= false;
539 bool unfocused_fired
= false;
540 bool changed_select_fired
= false;
541 bool unchanged_select_fired
= false;
542 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
544 "domAutomationController.send(focused_fired);",
546 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
548 "domAutomationController.send(unfocused_fired);",
550 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
552 "domAutomationController.send(changed_select_fired);",
553 &changed_select_fired
));
554 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
556 "domAutomationController.send(unchanged_select_fired);",
557 &unchanged_select_fired
));
558 EXPECT_FALSE(focused_fired
);
559 EXPECT_TRUE(unfocused_fired
);
560 EXPECT_TRUE(changed_select_fired
);
561 EXPECT_FALSE(unchanged_select_fired
);
563 // Unfocus the first name field. Its change event should fire.
564 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
566 "document.getElementById('firstname').blur();"
567 "domAutomationController.send(focused_fired);", &focused_fired
));
568 EXPECT_TRUE(focused_fired
);
571 // Test that we can autofill forms distinguished only by their |id| attribute.
572 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
573 AutofillFormsDistinguishedById
) {
576 // Load the test page.
577 const std::string kURL
=
578 std::string(kDataURIPrefix
) + kTestFormString
+
580 "var mainForm = document.forms[0];"
581 "mainForm.id = 'mainForm';"
582 "var newForm = document.createElement('form');"
583 "newForm.action = mainForm.action;"
584 "newForm.method = mainForm.method;"
585 "newForm.id = 'newForm';"
586 "mainForm.parentNode.insertBefore(newForm, mainForm);"
588 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL
)));
594 // Test that we properly autofill forms with repeated fields.
595 // In the wild, the repeated fields are typically either email fields
596 // (duplicated for "confirmation"); or variants that are hot-swapped via
597 // JavaScript, with only one actually visible at any given time.
598 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillFormWithRepeatedField
) {
601 // Load the test page.
602 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
603 GURL(std::string(kDataURIPrefix
) +
604 "<form action=\"http://www.example.com/\" method=\"POST\">"
605 "<label for=\"firstname\">First name:</label>"
606 " <input type=\"text\" id=\"firstname\""
607 " onFocus=\"domAutomationController.send(true)\"><br>"
608 "<label for=\"lastname\">Last name:</label>"
609 " <input type=\"text\" id=\"lastname\"><br>"
610 "<label for=\"address1\">Address line 1:</label>"
611 " <input type=\"text\" id=\"address1\"><br>"
612 "<label for=\"address2\">Address line 2:</label>"
613 " <input type=\"text\" id=\"address2\"><br>"
614 "<label for=\"city\">City:</label>"
615 " <input type=\"text\" id=\"city\"><br>"
616 "<label for=\"state\">State:</label>"
617 " <select id=\"state\">"
618 " <option value=\"\" selected=\"yes\">--</option>"
619 " <option value=\"CA\">California</option>"
620 " <option value=\"TX\">Texas</option>"
622 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
623 " <input type=\"text\" id=\"state_freeform\""
624 " style=\"display:none\"><br>"
625 "<label for=\"zip\">ZIP code:</label>"
626 " <input type=\"text\" id=\"zip\"><br>"
627 "<label for=\"country\">Country:</label>"
628 " <select id=\"country\">"
629 " <option value=\"\" selected=\"yes\">--</option>"
630 " <option value=\"CA\">Canada</option>"
631 " <option value=\"US\">United States</option>"
633 "<label for=\"phone\">Phone number:</label>"
634 " <input type=\"text\" id=\"phone\"><br>"
639 ExpectFieldValue("state_freeform", std::string());
642 // Test that we properly autofill forms with non-autofillable fields.
643 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
644 AutofillFormWithNonAutofillableField
) {
647 // Load the test page.
648 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
649 GURL(std::string(kDataURIPrefix
) +
650 "<form action=\"http://www.example.com/\" method=\"POST\">"
651 "<label for=\"firstname\">First name:</label>"
652 " <input type=\"text\" id=\"firstname\""
653 " onFocus=\"domAutomationController.send(true)\"><br>"
654 "<label for=\"middlename\">Middle name:</label>"
655 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
656 "<label for=\"lastname\">Last name:</label>"
657 " <input type=\"text\" id=\"lastname\"><br>"
658 "<label for=\"address1\">Address line 1:</label>"
659 " <input type=\"text\" id=\"address1\"><br>"
660 "<label for=\"address2\">Address line 2:</label>"
661 " <input type=\"text\" id=\"address2\"><br>"
662 "<label for=\"city\">City:</label>"
663 " <input type=\"text\" id=\"city\"><br>"
664 "<label for=\"state\">State:</label>"
665 " <select id=\"state\">"
666 " <option value=\"\" selected=\"yes\">--</option>"
667 " <option value=\"CA\">California</option>"
668 " <option value=\"TX\">Texas</option>"
670 "<label for=\"zip\">ZIP code:</label>"
671 " <input type=\"text\" id=\"zip\"><br>"
672 "<label for=\"country\">Country:</label>"
673 " <select id=\"country\">"
674 " <option value=\"\" selected=\"yes\">--</option>"
675 " <option value=\"CA\">Canada</option>"
676 " <option value=\"US\">United States</option>"
678 "<label for=\"phone\">Phone number:</label>"
679 " <input type=\"text\" id=\"phone\"><br>"
686 // Test that we can Autofill dynamically generated forms.
687 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, DynamicFormFill
) {
690 // Load the test page.
691 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
692 GURL(std::string(kDataURIPrefix
) +
693 "<form id=\"form\" action=\"http://www.example.com/\""
694 " method=\"POST\"></form>"
696 "function AddElement(name, label) {"
697 " var form = document.getElementById('form');"
699 " var label_text = document.createTextNode(label);"
700 " var label_element = document.createElement('label');"
701 " label_element.setAttribute('for', name);"
702 " label_element.appendChild(label_text);"
703 " form.appendChild(label_element);"
705 " if (name === 'state' || name === 'country') {"
706 " var select_element = document.createElement('select');"
707 " select_element.setAttribute('id', name);"
708 " select_element.setAttribute('name', name);"
710 " /* Add an empty selected option. */"
711 " var default_option = new Option('--', '', true);"
712 " select_element.appendChild(default_option);"
714 " /* Add the other options. */"
715 " if (name == 'state') {"
716 " var option1 = new Option('California', 'CA');"
717 " select_element.appendChild(option1);"
718 " var option2 = new Option('Texas', 'TX');"
719 " select_element.appendChild(option2);"
721 " var option1 = new Option('Canada', 'CA');"
722 " select_element.appendChild(option1);"
723 " var option2 = new Option('United States', 'US');"
724 " select_element.appendChild(option2);"
727 " form.appendChild(select_element);"
729 " var input_element = document.createElement('input');"
730 " input_element.setAttribute('id', name);"
731 " input_element.setAttribute('name', name);"
733 " /* Add the onFocus listener to the 'firstname' field. */"
734 " if (name === 'firstname') {"
735 " input_element.setAttribute("
736 " 'onFocus', 'domAutomationController.send(true)');"
739 " form.appendChild(input_element);"
742 " form.appendChild(document.createElement('br'));"
745 "function BuildForm() {"
747 " ['firstname', 'First name:'],"
748 " ['lastname', 'Last name:'],"
749 " ['address1', 'Address line 1:'],"
750 " ['address2', 'Address line 2:'],"
751 " ['city', 'City:'],"
752 " ['state', 'State:'],"
753 " ['zip', 'ZIP code:'],"
754 " ['country', 'Country:'],"
755 " ['phone', 'Phone number:'],"
758 " for (var i = 0; i < elements.length; i++) {"
759 " var name = elements[i][0];"
760 " var label = elements[i][1];"
761 " AddElement(name, label);"
766 // Dynamically construct the form.
767 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
773 // Test that form filling works after reloading the current page.
774 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillAfterReload
) {
777 // Load the test page.
778 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
779 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
782 content::WebContents
* web_contents
= GetWebContents();
783 web_contents
->GetController().Reload(false);
784 content::WaitForLoadStop(web_contents
);
790 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, AutofillAfterTranslate
) {
793 GURL
url(std::string(kDataURIPrefix
) +
794 "<form action=\"http://www.example.com/\" method=\"POST\">"
795 "<label for=\"fn\">なまえ</label>"
796 " <input type=\"text\" id=\"fn\""
797 " onFocus=\"domAutomationController.send(true)\""
799 "<label for=\"ln\">みょうじ</label>"
800 " <input type=\"text\" id=\"ln\"><br>"
801 "<label for=\"a1\">Address line 1:</label>"
802 " <input type=\"text\" id=\"a1\"><br>"
803 "<label for=\"a2\">Address line 2:</label>"
804 " <input type=\"text\" id=\"a2\"><br>"
805 "<label for=\"ci\">City:</label>"
806 " <input type=\"text\" id=\"ci\"><br>"
807 "<label for=\"st\">State:</label>"
808 " <select id=\"st\">"
809 " <option value=\"\" selected=\"yes\">--</option>"
810 " <option value=\"CA\">California</option>"
811 " <option value=\"TX\">Texas</option>"
813 "<label for=\"z\">ZIP code:</label>"
814 " <input type=\"text\" id=\"z\"><br>"
815 "<label for=\"co\">Country:</label>"
816 " <select id=\"co\">"
817 " <option value=\"\" selected=\"yes\">--</option>"
818 " <option value=\"CA\">Canada</option>"
819 " <option value=\"US\">United States</option>"
821 "<label for=\"ph\">Phone number:</label>"
822 " <input type=\"text\" id=\"ph\"><br>"
824 // Add additional Japanese characters to ensure the translate bar
826 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
827 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
829 content::WindowedNotificationObserver
infobar_observer(
830 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
831 content::NotificationService::AllSources());
832 ASSERT_NO_FATAL_FAILURE(
833 ui_test_utils::NavigateToURL(browser(), url
));
835 // Wait for the translation bar to appear and get it.
836 infobar_observer
.Wait();
837 TranslateInfoBarDelegate
* delegate
=
838 InfoBarService::FromWebContents(GetWebContents())->infobar_at(0)->
839 delegate()->AsTranslateInfoBarDelegate();
840 ASSERT_TRUE(delegate
);
841 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE
,
842 delegate
->infobar_type());
844 // Simulate translation button press.
845 delegate
->Translate();
847 content::WindowedNotificationObserver
translation_observer(
848 chrome::NOTIFICATION_PAGE_TRANSLATED
,
849 content::NotificationService::AllSources());
851 // Simulate the translate script being retrieved.
852 // Pass fake google.translate lib as the translate script.
853 SimulateURLFetch(true);
855 // Simulate the render notifying the translation has been done.
856 translation_observer
.Wait();
861 // Test phone fields parse correctly from a given profile.
862 // The high level key presses execute the following: Select the first text
863 // field, invoke the autofill popup list, select the first profile within the
864 // list, and commit to the profile to populate the form.
865 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, ComparePhoneNumbers
) {
866 ASSERT_TRUE(test_server()->Start());
868 AutofillProfile profile
;
869 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Bob"));
870 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
871 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("1234 H St."));
872 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("San Jose"));
873 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
874 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("95110"));
875 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("1-408-555-4567"));
878 GURL url
= test_server()->GetURL("files/autofill/form_phones.html");
879 ui_test_utils::NavigateToURL(browser(), url
);
880 PopulateForm("NAME_FIRST");
882 ExpectFieldValue("NAME_FIRST", "Bob");
883 ExpectFieldValue("NAME_LAST", "Smith");
884 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
885 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
886 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
887 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
888 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
889 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
890 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
891 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
892 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
893 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
894 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
895 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
896 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
897 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
898 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
901 // Test that Autofill does not fill in read-only fields.
902 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, NoAutofillForReadOnlyFields
) {
903 ASSERT_TRUE(test_server()->Start());
905 std::string
addr_line1("1234 H St.");
907 AutofillProfile profile
;
908 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Bob"));
909 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
910 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("bsmith@gmail.com"));
911 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16(addr_line1
));
912 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("San Jose"));
913 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
914 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("95110"));
915 profile
.SetRawInfo(COMPANY_NAME
, ASCIIToUTF16("Company X"));
916 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("408-871-4567"));
919 GURL url
= test_server()->GetURL("files/autofill/read_only_field_test.html");
920 ui_test_utils::NavigateToURL(browser(), url
);
921 PopulateForm("firstname");
923 ExpectFieldValue("email", std::string());
924 ExpectFieldValue("address", addr_line1
);
927 // Test form is fillable from a profile after form was reset.
929 // 1. Fill form using a saved profile.
930 // 2. Reset the form.
931 // 3. Fill form using a saved profile.
932 // Flakily times out: http://crbug.com/270341
933 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
, DISABLED_FormFillableOnReset
) {
934 ASSERT_TRUE(test_server()->Start());
938 GURL url
= test_server()->GetURL("files/autofill/autofill_test_form.html");
939 ui_test_utils::NavigateToURL(browser(), url
);
940 PopulateForm("NAME_FIRST");
942 ASSERT_TRUE(content::ExecuteScript(
943 GetWebContents(), "document.getElementById('testform').reset()"));
945 PopulateForm("NAME_FIRST");
947 ExpectFieldValue("NAME_FIRST", "Milton");
948 ExpectFieldValue("NAME_LAST", "Waddams");
949 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
950 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
951 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
952 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
953 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
954 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
955 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
958 // Test Autofill distinguishes a middle initial in a name.
959 // Flakily times out: http://crbug.com/270341
960 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
961 DISABLED_DistinguishMiddleInitialWithinName
) {
962 ASSERT_TRUE(test_server()->Start());
966 GURL url
= test_server()->GetURL(
967 "files/autofill/autofill_middleinit_form.html");
968 ui_test_utils::NavigateToURL(browser(), url
);
969 PopulateForm("NAME_FIRST");
971 ExpectFieldValue("NAME_MIDDLE", "C");
974 // Test forms with multiple email addresses are filled properly.
975 // Entire form should be filled with one user gesture.
976 // Flakily times out: http://crbug.com/270341
977 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
978 DISABLED_MultipleEmailFilledByOneUserGesture
) {
979 ASSERT_TRUE(test_server()->Start());
981 std::string
email("bsmith@gmail.com");
983 AutofillProfile profile
;
984 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Bob"));
985 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
986 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16(email
));
987 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("4088714567"));
990 GURL url
= test_server()->GetURL(
991 "files/autofill/autofill_confirmemail_form.html");
992 ui_test_utils::NavigateToURL(browser(), url
);
993 PopulateForm("NAME_FIRST");
995 ExpectFieldValue("EMAIL_CONFIRM", email
);
996 // TODO(isherman): verify entire form.
999 // http://crbug.com/281527
1000 #if defined(OS_MACOSX)
1001 #define MAYBE_FormFillLatencyAfterSubmit FormFillLatencyAfterSubmit
1003 #define MAYBE_FormFillLatencyAfterSubmit DISABLED_FormFillLatencyAfterSubmit
1005 // Test latency time on form submit with lots of stored Autofill profiles.
1006 // This test verifies when a profile is selected from the Autofill dictionary
1007 // that consists of thousands of profiles, the form does not hang after being
1009 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
1010 MAYBE_FormFillLatencyAfterSubmit
) {
1011 ASSERT_TRUE(test_server()->Start());
1013 std::vector
<std::string
> cities
;
1014 cities
.push_back("San Jose");
1015 cities
.push_back("San Francisco");
1016 cities
.push_back("Sacramento");
1017 cities
.push_back("Los Angeles");
1019 std::vector
<std::string
> streets
;
1020 streets
.push_back("St");
1021 streets
.push_back("Ave");
1022 streets
.push_back("Ln");
1023 streets
.push_back("Ct");
1025 const int kNumProfiles
= 1500;
1026 base::Time start_time
= base::Time::Now();
1027 std::vector
<AutofillProfile
> profiles
;
1028 for (int i
= 0; i
< kNumProfiles
; i
++) {
1029 AutofillProfile profile
;
1030 base::string16
name(base::IntToString16(i
));
1031 base::string16
email(name
+ ASCIIToUTF16("@example.com"));
1032 base::string16 street
= ASCIIToUTF16(
1033 base::IntToString(base::RandInt(0, 10000)) + " " +
1034 streets
[base::RandInt(0, streets
.size() - 1)]);
1035 base::string16 city
=
1036 ASCIIToUTF16(cities
[base::RandInt(0, cities
.size() - 1)]);
1037 base::string16
zip(base::IntToString16(base::RandInt(0, 10000)));
1038 profile
.SetRawInfo(NAME_FIRST
, name
);
1039 profile
.SetRawInfo(EMAIL_ADDRESS
, email
);
1040 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, street
);
1041 profile
.SetRawInfo(ADDRESS_HOME_CITY
, city
);
1042 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
1043 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, zip
);
1044 profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
1045 profiles
.push_back(profile
);
1047 SetProfiles(&profiles
);
1048 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1050 LOG(INFO
) << "Created " << kNumProfiles
<< " profiles in " <<
1051 (base::Time::Now() - start_time
).InSeconds() << " seconds.";
1053 GURL url
= test_server()->GetURL(
1054 "files/autofill/latency_after_submit_test.html");
1055 ui_test_utils::NavigateToURL(browser(), url
);
1056 PopulateForm("NAME_FIRST");
1058 content::WindowedNotificationObserver
load_stop_observer(
1059 content::NOTIFICATION_LOAD_STOP
,
1060 content::Source
<content::NavigationController
>(
1061 &GetWebContents()->GetController()));
1063 ASSERT_TRUE(content::ExecuteScript(
1064 GetRenderViewHost(),
1065 "document.getElementById('testform').submit();"));
1066 // This will ensure the test didn't hang.
1067 load_stop_observer
.Wait();
1070 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1071 // is interacting with the form. This is a regression test for
1072 // http://crbug.com/160476
1073 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest
,
1074 DisableAutocompleteWhileFilling
) {
1075 CreateTestProfile();
1077 // Load the test page.
1078 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1079 GURL(std::string(kDataURIPrefix
) + kTestFormString
)));
1081 // Invoke Autofill: Start filling the first name field with "M" and wait for
1082 // the popup to be shown.
1083 FocusFirstNameField();
1084 SendKeyToPageAndWait(ui::VKEY_M
);
1086 // Now that the popup with suggestions is showing, disable autocomplete for
1087 // the active field.
1088 ASSERT_TRUE(content::ExecuteScript(
1089 GetRenderViewHost(),
1090 "document.querySelector('input').autocomplete = 'off';"));
1092 // Press the down arrow to select the suggestion and attempt to preview the
1094 SendKeyToPopupAndWait(ui::VKEY_DOWN
);
1097 } // namespace autofill