Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_interactive_uitest.cc
bloba641fa6bb9ca09f5de82504045c2b5e2d20c840d
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.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/autofill/personal_data_manager_factory.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/infobars/infobar_service.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/translate/chrome_translate_client.h"
23 #include "chrome/browser/translate/translate_service.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/render_messages.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "chrome/test/base/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;
61 namespace autofill {
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>"
82 " </select><br>"
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>"
90 " </select><br>"
91 "<label for=\"phone\">Phone number:</label>"
92 " <input type=\"text\" id=\"phone\"><br>"
93 "</form>";
96 // AutofillManagerTestDelegateImpl --------------------------------------------
98 class AutofillManagerTestDelegateImpl
99 : public autofill::AutofillManagerTestDelegate {
100 public:
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();
120 void Reset() {
121 loop_runner_ = new content::MessageLoopRunner();
124 void Wait() {
125 loop_runner_->Run();
128 private:
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 {
140 public:
141 explicit WindowedPersonalDataManagerObserver(Browser* browser)
142 : alerted_(false),
143 has_run_message_loop_(false),
144 browser_(browser),
145 infobar_service_(InfoBarService::FromWebContents(
146 browser_->tab_strip_model()->GetActiveWebContents())) {
147 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
148 AddObserver(this);
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;
165 alerted_ = true;
168 virtual void OnInsufficientFormData() OVERRIDE {
169 OnPersonalDataChanged();
173 void Wait() {
174 if (!alerted_) {
175 has_run_message_loop_ = true;
176 content::RunMessageLoop();
178 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
179 RemoveObserver(this);
182 private:
183 // infobars::InfoBarManager::Observer:
184 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) OVERRIDE {
185 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()->
186 Accept();
189 bool alerted_;
190 bool has_run_message_loop_;
191 Browser* browser_;
192 InfoBarService* infobar_service_;
194 DISALLOW_COPY_AND_ASSIGN(WindowedPersonalDataManagerObserver);
197 // AutofillInteractiveTest ----------------------------------------------------
199 class AutofillInteractiveTest : public InProcessBrowserTest {
200 protected:
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
258 // tests.
259 observer.Wait();
262 void SetProfiles(std::vector<AutofillProfile>* profiles) {
263 WindowedPersonalDataManagerObserver observer(browser());
264 GetPersonalDataManager()->SetProfiles(profiles);
265 observer.Wait();
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) {
288 std::string value;
289 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
290 GetWebContents(),
291 "window.domAutomationController.send("
292 " document.getElementById('" + field_name + "').value);",
293 &value));
294 EXPECT_EQ(expected_value, value);
297 void SimulateURLFetch(bool success) {
298 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
299 ASSERT_TRUE(fetcher);
300 net::URLRequestStatus status;
301 status.set_status(success ? net::URLRequestStatus::SUCCESS :
302 net::URLRequestStatus::FAILED);
304 std::string script = " var google = {};"
305 "google.translate = (function() {"
306 " return {"
307 " TranslateService: function() {"
308 " return {"
309 " isAvailable : function() {"
310 " return true;"
311 " },"
312 " restore : function() {"
313 " return;"
314 " },"
315 " getDetectedLanguage : function() {"
316 " return \"ja\";"
317 " },"
318 " translatePage : function(originalLang, targetLang,"
319 " onTranslateProgress) {"
320 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
321 std::string(kTestFormString) +
322 " ';"
323 " onTranslateProgress(100, true, false);"
324 " }"
325 " };"
326 " }"
327 " };"
328 "})();"
329 "cr.googleTranslate.onTranslateElementLoad();";
331 fetcher->set_url(fetcher->GetOriginalURL());
332 fetcher->set_status(status);
333 fetcher->set_response_code(success ? 200 : 500);
334 fetcher->SetResponseString(script);
335 fetcher->delegate()->OnURLFetchComplete(fetcher);
338 void FocusFirstNameField() {
339 bool result = false;
340 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
341 GetRenderViewHost(),
342 "if (document.readyState === 'complete')"
343 " document.getElementById('firstname').focus();"
344 "else"
345 " domAutomationController.send(false);",
346 &result));
347 ASSERT_TRUE(result);
350 // Simulates a click on the middle of the DOM element with the given |id|.
351 void ClickElementWithId(const std::string& id) {
352 int x;
353 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
354 GetRenderViewHost(),
355 "var bounds = document.getElementById('" +
356 id +
357 "').getBoundingClientRect();"
358 "domAutomationController.send("
359 " Math.floor(bounds.left + bounds.width / 2));",
360 &x));
361 int y;
362 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
363 GetRenderViewHost(),
364 "var bounds = document.getElementById('" +
365 id +
366 "').getBoundingClientRect();"
367 "domAutomationController.send("
368 " Math.floor(bounds.top + bounds.height / 2));",
369 &y));
370 content::SimulateMouseClickAt(GetWebContents(),
372 blink::WebMouseEvent::ButtonLeft,
373 gfx::Point(x, y));
376 void ClickFirstNameField() {
377 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("firstname"));
380 // Make a pointless round trip to the renderer, giving the popup a chance to
381 // show if it's going to. If it does show, an assert in
382 // AutofillManagerTestDelegateImpl will trigger.
383 void MakeSurePopupDoesntAppear() {
384 int unused;
385 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
386 GetRenderViewHost(), "domAutomationController.send(42)", &unused));
389 void ExpectFilledTestForm() {
390 ExpectFieldValue("firstname", "Milton");
391 ExpectFieldValue("lastname", "Waddams");
392 ExpectFieldValue("address1", "4120 Freidrich Lane");
393 ExpectFieldValue("address2", "Basement");
394 ExpectFieldValue("city", "Austin");
395 ExpectFieldValue("state", "TX");
396 ExpectFieldValue("zip", "78744");
397 ExpectFieldValue("country", "US");
398 ExpectFieldValue("phone", "5125551234");
401 void SendKeyToPageAndWait(ui::KeyboardCode key) {
402 test_delegate_.Reset();
403 content::SimulateKeyPress(
404 GetWebContents(), key, false, false, false, false);
405 test_delegate_.Wait();
408 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) {
409 return true;
412 void SendKeyToPopupAndWait(ui::KeyboardCode key) {
413 // Route popup-targeted key presses via the render view host.
414 content::NativeWebKeyboardEvent event;
415 event.windowsKeyCode = key;
416 event.type = blink::WebKeyboardEvent::RawKeyDown;
417 test_delegate_.Reset();
418 // Install the key press event sink to ensure that any events that are not
419 // handled by the installed callbacks do not end up crashing the test.
420 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
421 GetRenderViewHost()->ForwardKeyboardEvent(event);
422 test_delegate_.Wait();
423 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
426 void TryBasicFormFill() {
427 FocusFirstNameField();
429 // Start filling the first name field with "M" and wait for the popup to be
430 // shown.
431 SendKeyToPageAndWait(ui::VKEY_M);
433 // Press the down arrow to select the suggestion and preview the autofilled
434 // form.
435 SendKeyToPopupAndWait(ui::VKEY_DOWN);
437 // The previewed values should not be accessible to JavaScript.
438 ExpectFieldValue("firstname", "M");
439 ExpectFieldValue("lastname", std::string());
440 ExpectFieldValue("address1", std::string());
441 ExpectFieldValue("address2", std::string());
442 ExpectFieldValue("city", std::string());
443 ExpectFieldValue("state", std::string());
444 ExpectFieldValue("zip", std::string());
445 ExpectFieldValue("country", std::string());
446 ExpectFieldValue("phone", std::string());
447 // TODO(isherman): It would be nice to test that the previewed values are
448 // displayed: http://crbug.com/57220
450 // Press Enter to accept the autofill suggestions.
451 SendKeyToPopupAndWait(ui::VKEY_RETURN);
453 // The form should be filled.
454 ExpectFilledTestForm();
457 AutofillManagerTestDelegateImpl* test_delegate() { return &test_delegate_; }
459 private:
460 AutofillManagerTestDelegateImpl test_delegate_;
462 net::TestURLFetcherFactory url_fetcher_factory_;
464 // KeyPressEventCallback that serves as a sink to ensure that every key press
465 // event the tests create and have the WebContents forward is handled by some
466 // key press event callback. It is necessary to have this sinkbecause if no
467 // key press event callback handles the event (at least on Mac), a DCHECK
468 // ends up going off that the |event| doesn't have an |os_event| associated
469 // with it.
470 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_;
472 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest);
475 // Test that basic form fill is working.
476 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) {
477 CreateTestProfile();
479 // Load the test page.
480 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
481 GURL(std::string(kDataURIPrefix) + kTestFormString)));
483 // Invoke Autofill.
484 TryBasicFormFill();
487 // Test that form filling can be initiated by pressing the down arrow.
488 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) {
489 CreateTestProfile();
491 // Load the test page.
492 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
493 GURL(std::string(kDataURIPrefix) + kTestFormString)));
495 // Focus a fillable field.
496 FocusFirstNameField();
498 // Press the down arrow to initiate Autofill and wait for the popup to be
499 // shown.
500 SendKeyToPageAndWait(ui::VKEY_DOWN);
502 // Press the down arrow to select the suggestion and preview the autofilled
503 // form.
504 SendKeyToPopupAndWait(ui::VKEY_DOWN);
506 // Press Enter to accept the autofill suggestions.
507 SendKeyToPopupAndWait(ui::VKEY_RETURN);
509 // The form should be filled.
510 ExpectFilledTestForm();
513 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillSelectViaTab) {
514 CreateTestProfile();
516 // Load the test page.
517 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
518 GURL(std::string(kDataURIPrefix) + kTestFormString)));
520 // Focus a fillable field.
521 FocusFirstNameField();
523 // Press the down arrow to initiate Autofill and wait for the popup to be
524 // shown.
525 SendKeyToPageAndWait(ui::VKEY_DOWN);
527 // Press the down arrow to select the suggestion and preview the autofilled
528 // form.
529 SendKeyToPopupAndWait(ui::VKEY_DOWN);
531 // Press tab to accept the autofill suggestions.
532 SendKeyToPopupAndWait(ui::VKEY_TAB);
534 // The form should be filled.
535 ExpectFilledTestForm();
538 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaClick) {
539 CreateTestProfile();
541 // Load the test page.
542 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
543 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
544 // Focus a fillable field.
545 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
547 // Now click it.
548 test_delegate()->Reset();
549 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
550 test_delegate()->Wait();
552 // Press the down arrow to select the suggestion and preview the autofilled
553 // form.
554 SendKeyToPopupAndWait(ui::VKEY_DOWN);
556 // Press Enter to accept the autofill suggestions.
557 SendKeyToPopupAndWait(ui::VKEY_RETURN);
559 // The form should be filled.
560 ExpectFilledTestForm();
563 // Makes sure that the first click does *not* activate the popup.
564 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForFirstClick) {
565 CreateTestProfile();
567 // Load the test page.
568 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
569 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
571 // Click the first name field while it's out of focus, then twiddle our thumbs
572 // a bit. If a popup were to show, it would hit the asserts in
573 // AutofillManagerTestDelegateImpl while we're wasting time.
574 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
575 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
577 // The second click should activate the popup since the first click focused
578 // the field.
579 test_delegate()->Reset();
580 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
581 test_delegate()->Wait();
584 // Makes sure that clicking outside the focused field doesn't activate
585 // the popup.
586 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForOutsideClick) {
587 CreateTestProfile();
589 // Load the test page.
590 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
591 browser(),
592 GURL(std::string(kDataURIPrefix) + kTestFormString +
593 "<button disabled id='disabled-button'>Cant click this</button>")));
595 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
597 // Clicking a disabled button will generate a mouse event but focus doesn't
598 // change. This tests that autofill can handle a mouse event outside a focused
599 // input *without* showing the popup.
600 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("disabled-button"));
601 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
603 test_delegate()->Reset();
604 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
605 test_delegate()->Wait();
608 // Test that a field is still autofillable after the previously autofilled
609 // value is deleted.
610 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) {
611 CreateTestProfile();
613 // Load the test page.
614 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
615 GURL(std::string(kDataURIPrefix) + kTestFormString)));
617 // Invoke and accept the Autofill popup and verify the form was filled.
618 FocusFirstNameField();
619 SendKeyToPageAndWait(ui::VKEY_M);
620 SendKeyToPopupAndWait(ui::VKEY_DOWN);
621 SendKeyToPopupAndWait(ui::VKEY_RETURN);
622 ExpectFilledTestForm();
624 // Delete the value of a filled field.
625 ASSERT_TRUE(content::ExecuteScript(
626 GetRenderViewHost(),
627 "document.getElementById('firstname').value = '';"));
628 ExpectFieldValue("firstname", "");
630 // Invoke and accept the Autofill popup and verify the field was filled.
631 SendKeyToPageAndWait(ui::VKEY_M);
632 SendKeyToPopupAndWait(ui::VKEY_DOWN);
633 SendKeyToPopupAndWait(ui::VKEY_RETURN);
634 ExpectFieldValue("firstname", "Milton");
637 // Test that a JavaScript oninput event is fired after auto-filling a form.
638 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) {
639 CreateTestProfile();
641 const char kOnInputScript[] =
642 "<script>"
643 "focused_fired = false;"
644 "unfocused_fired = false;"
645 "changed_select_fired = false;"
646 "unchanged_select_fired = false;"
647 "document.getElementById('firstname').oninput = function() {"
648 " focused_fired = true;"
649 "};"
650 "document.getElementById('lastname').oninput = function() {"
651 " unfocused_fired = true;"
652 "};"
653 "document.getElementById('state').oninput = function() {"
654 " changed_select_fired = true;"
655 "};"
656 "document.getElementById('country').oninput = function() {"
657 " unchanged_select_fired = true;"
658 "};"
659 "document.getElementById('country').value = 'US';"
660 "</script>";
662 // Load the test page.
663 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
664 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnInputScript)));
666 // Invoke Autofill.
667 FocusFirstNameField();
669 // Start filling the first name field with "M" and wait for the popup to be
670 // shown.
671 SendKeyToPageAndWait(ui::VKEY_M);
673 // Press the down arrow to select the suggestion and preview the autofilled
674 // form.
675 SendKeyToPopupAndWait(ui::VKEY_DOWN);
677 // Press Enter to accept the autofill suggestions.
678 SendKeyToPopupAndWait(ui::VKEY_RETURN);
680 // The form should be filled.
681 ExpectFilledTestForm();
683 bool focused_fired = false;
684 bool unfocused_fired = false;
685 bool changed_select_fired = false;
686 bool unchanged_select_fired = false;
687 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
688 GetRenderViewHost(),
689 "domAutomationController.send(focused_fired);",
690 &focused_fired));
691 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
692 GetRenderViewHost(),
693 "domAutomationController.send(unfocused_fired);",
694 &unfocused_fired));
695 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
696 GetRenderViewHost(),
697 "domAutomationController.send(changed_select_fired);",
698 &changed_select_fired));
699 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
700 GetRenderViewHost(),
701 "domAutomationController.send(unchanged_select_fired);",
702 &unchanged_select_fired));
703 EXPECT_TRUE(focused_fired);
704 EXPECT_TRUE(unfocused_fired);
705 EXPECT_TRUE(changed_select_fired);
706 EXPECT_FALSE(unchanged_select_fired);
709 // Test that a JavaScript onchange event is fired after auto-filling a form.
710 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) {
711 CreateTestProfile();
713 const char kOnChangeScript[] =
714 "<script>"
715 "focused_fired = false;"
716 "unfocused_fired = false;"
717 "changed_select_fired = false;"
718 "unchanged_select_fired = false;"
719 "document.getElementById('firstname').onchange = function() {"
720 " focused_fired = true;"
721 "};"
722 "document.getElementById('lastname').onchange = function() {"
723 " unfocused_fired = true;"
724 "};"
725 "document.getElementById('state').onchange = function() {"
726 " changed_select_fired = true;"
727 "};"
728 "document.getElementById('country').onchange = function() {"
729 " unchanged_select_fired = true;"
730 "};"
731 "document.getElementById('country').value = 'US';"
732 "</script>";
734 // Load the test page.
735 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
736 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnChangeScript)));
738 // Invoke Autofill.
739 FocusFirstNameField();
741 // Start filling the first name field with "M" and wait for the popup to be
742 // shown.
743 SendKeyToPageAndWait(ui::VKEY_M);
745 // Press the down arrow to select the suggestion and preview the autofilled
746 // form.
747 SendKeyToPopupAndWait(ui::VKEY_DOWN);
749 // Press Enter to accept the autofill suggestions.
750 SendKeyToPopupAndWait(ui::VKEY_RETURN);
752 // The form should be filled.
753 ExpectFilledTestForm();
755 bool focused_fired = false;
756 bool unfocused_fired = false;
757 bool changed_select_fired = false;
758 bool unchanged_select_fired = false;
759 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
760 GetRenderViewHost(),
761 "domAutomationController.send(focused_fired);",
762 &focused_fired));
763 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
764 GetRenderViewHost(),
765 "domAutomationController.send(unfocused_fired);",
766 &unfocused_fired));
767 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
768 GetRenderViewHost(),
769 "domAutomationController.send(changed_select_fired);",
770 &changed_select_fired));
771 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
772 GetRenderViewHost(),
773 "domAutomationController.send(unchanged_select_fired);",
774 &unchanged_select_fired));
775 EXPECT_TRUE(focused_fired);
776 EXPECT_TRUE(unfocused_fired);
777 EXPECT_TRUE(changed_select_fired);
778 EXPECT_FALSE(unchanged_select_fired);
781 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) {
782 CreateTestProfile();
784 const char kInputFiresBeforeChangeScript[] =
785 "<script>"
786 "inputElementEvents = [];"
787 "function recordInputElementEvent(e) {"
788 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';"
789 " inputElementEvents.push(e.type);"
791 "selectElementEvents = [];"
792 "function recordSelectElementEvent(e) {"
793 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';"
794 " selectElementEvents.push(e.type);"
796 "document.getElementById('lastname').oninput = recordInputElementEvent;"
797 "document.getElementById('lastname').onchange = recordInputElementEvent;"
798 "document.getElementById('country').oninput = recordSelectElementEvent;"
799 "document.getElementById('country').onchange = recordSelectElementEvent;"
800 "</script>";
802 // Load the test page.
803 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
804 GURL(std::string(kDataURIPrefix) + kTestFormString +
805 kInputFiresBeforeChangeScript)));
807 // Invoke and accept the Autofill popup and verify the form was filled.
808 FocusFirstNameField();
809 SendKeyToPageAndWait(ui::VKEY_M);
810 SendKeyToPopupAndWait(ui::VKEY_DOWN);
811 SendKeyToPopupAndWait(ui::VKEY_RETURN);
812 ExpectFilledTestForm();
814 int num_input_element_events = -1;
815 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
816 GetRenderViewHost(),
817 "domAutomationController.send(inputElementEvents.length);",
818 &num_input_element_events));
819 EXPECT_EQ(2, num_input_element_events);
821 std::vector<std::string> input_element_events;
822 input_element_events.resize(2);
824 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
825 GetRenderViewHost(),
826 "domAutomationController.send(inputElementEvents[0]);",
827 &input_element_events[0]));
828 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
829 GetRenderViewHost(),
830 "domAutomationController.send(inputElementEvents[1]);",
831 &input_element_events[1]));
833 EXPECT_EQ("input", input_element_events[0]);
834 EXPECT_EQ("change", input_element_events[1]);
836 int num_select_element_events = -1;
837 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
838 GetRenderViewHost(),
839 "domAutomationController.send(selectElementEvents.length);",
840 &num_select_element_events));
841 EXPECT_EQ(2, num_select_element_events);
843 std::vector<std::string> select_element_events;
844 select_element_events.resize(2);
846 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
847 GetRenderViewHost(),
848 "domAutomationController.send(selectElementEvents[0]);",
849 &select_element_events[0]));
850 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
851 GetRenderViewHost(),
852 "domAutomationController.send(selectElementEvents[1]);",
853 &select_element_events[1]));
855 EXPECT_EQ("input", select_element_events[0]);
856 EXPECT_EQ("change", select_element_events[1]);
859 // Test that we can autofill forms distinguished only by their |id| attribute.
860 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
861 AutofillFormsDistinguishedById) {
862 CreateTestProfile();
864 // Load the test page.
865 const std::string kURL =
866 std::string(kDataURIPrefix) + kTestFormString +
867 "<script>"
868 "var mainForm = document.forms[0];"
869 "mainForm.id = 'mainForm';"
870 "var newForm = document.createElement('form');"
871 "newForm.action = mainForm.action;"
872 "newForm.method = mainForm.method;"
873 "newForm.id = 'newForm';"
874 "mainForm.parentNode.insertBefore(newForm, mainForm);"
875 "</script>";
876 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL)));
878 // Invoke Autofill.
879 TryBasicFormFill();
882 // Test that we properly autofill forms with repeated fields.
883 // In the wild, the repeated fields are typically either email fields
884 // (duplicated for "confirmation"); or variants that are hot-swapped via
885 // JavaScript, with only one actually visible at any given time.
886 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) {
887 CreateTestProfile();
889 // Load the test page.
890 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
891 GURL(std::string(kDataURIPrefix) +
892 "<form action=\"http://www.example.com/\" method=\"POST\">"
893 "<label for=\"firstname\">First name:</label>"
894 " <input type=\"text\" id=\"firstname\""
895 " onfocus=\"domAutomationController.send(true)\"><br>"
896 "<label for=\"lastname\">Last name:</label>"
897 " <input type=\"text\" id=\"lastname\"><br>"
898 "<label for=\"address1\">Address line 1:</label>"
899 " <input type=\"text\" id=\"address1\"><br>"
900 "<label for=\"address2\">Address line 2:</label>"
901 " <input type=\"text\" id=\"address2\"><br>"
902 "<label for=\"city\">City:</label>"
903 " <input type=\"text\" id=\"city\"><br>"
904 "<label for=\"state\">State:</label>"
905 " <select id=\"state\">"
906 " <option value=\"\" selected=\"yes\">--</option>"
907 " <option value=\"CA\">California</option>"
908 " <option value=\"TX\">Texas</option>"
909 " </select><br>"
910 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
911 " <input type=\"text\" id=\"state_freeform\""
912 " style=\"display:none\"><br>"
913 "<label for=\"zip\">ZIP code:</label>"
914 " <input type=\"text\" id=\"zip\"><br>"
915 "<label for=\"country\">Country:</label>"
916 " <select id=\"country\">"
917 " <option value=\"\" selected=\"yes\">--</option>"
918 " <option value=\"CA\">Canada</option>"
919 " <option value=\"US\">United States</option>"
920 " </select><br>"
921 "<label for=\"phone\">Phone number:</label>"
922 " <input type=\"text\" id=\"phone\"><br>"
923 "</form>")));
925 // Invoke Autofill.
926 TryBasicFormFill();
927 ExpectFieldValue("state_freeform", std::string());
930 // Test that we properly autofill forms with non-autofillable fields.
931 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
932 AutofillFormWithNonAutofillableField) {
933 CreateTestProfile();
935 // Load the test page.
936 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
937 GURL(std::string(kDataURIPrefix) +
938 "<form action=\"http://www.example.com/\" method=\"POST\">"
939 "<label for=\"firstname\">First name:</label>"
940 " <input type=\"text\" id=\"firstname\""
941 " onfocus=\"domAutomationController.send(true)\"><br>"
942 "<label for=\"middlename\">Middle name:</label>"
943 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
944 "<label for=\"lastname\">Last name:</label>"
945 " <input type=\"text\" id=\"lastname\"><br>"
946 "<label for=\"address1\">Address line 1:</label>"
947 " <input type=\"text\" id=\"address1\"><br>"
948 "<label for=\"address2\">Address line 2:</label>"
949 " <input type=\"text\" id=\"address2\"><br>"
950 "<label for=\"city\">City:</label>"
951 " <input type=\"text\" id=\"city\"><br>"
952 "<label for=\"state\">State:</label>"
953 " <select id=\"state\">"
954 " <option value=\"\" selected=\"yes\">--</option>"
955 " <option value=\"CA\">California</option>"
956 " <option value=\"TX\">Texas</option>"
957 " </select><br>"
958 "<label for=\"zip\">ZIP code:</label>"
959 " <input type=\"text\" id=\"zip\"><br>"
960 "<label for=\"country\">Country:</label>"
961 " <select id=\"country\">"
962 " <option value=\"\" selected=\"yes\">--</option>"
963 " <option value=\"CA\">Canada</option>"
964 " <option value=\"US\">United States</option>"
965 " </select><br>"
966 "<label for=\"phone\">Phone number:</label>"
967 " <input type=\"text\" id=\"phone\"><br>"
968 "</form>")));
970 // Invoke Autofill.
971 TryBasicFormFill();
974 // Test that we can Autofill dynamically generated forms.
975 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) {
976 CreateTestProfile();
978 // Load the test page.
979 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
980 GURL(std::string(kDataURIPrefix) +
981 "<form id=\"form\" action=\"http://www.example.com/\""
982 " method=\"POST\"></form>"
983 "<script>"
984 "function AddElement(name, label) {"
985 " var form = document.getElementById('form');"
987 " var label_text = document.createTextNode(label);"
988 " var label_element = document.createElement('label');"
989 " label_element.setAttribute('for', name);"
990 " label_element.appendChild(label_text);"
991 " form.appendChild(label_element);"
993 " if (name === 'state' || name === 'country') {"
994 " var select_element = document.createElement('select');"
995 " select_element.setAttribute('id', name);"
996 " select_element.setAttribute('name', name);"
998 " /* Add an empty selected option. */"
999 " var default_option = new Option('--', '', true);"
1000 " select_element.appendChild(default_option);"
1002 " /* Add the other options. */"
1003 " if (name == 'state') {"
1004 " var option1 = new Option('California', 'CA');"
1005 " select_element.appendChild(option1);"
1006 " var option2 = new Option('Texas', 'TX');"
1007 " select_element.appendChild(option2);"
1008 " } else {"
1009 " var option1 = new Option('Canada', 'CA');"
1010 " select_element.appendChild(option1);"
1011 " var option2 = new Option('United States', 'US');"
1012 " select_element.appendChild(option2);"
1013 " }"
1015 " form.appendChild(select_element);"
1016 " } else {"
1017 " var input_element = document.createElement('input');"
1018 " input_element.setAttribute('id', name);"
1019 " input_element.setAttribute('name', name);"
1021 " /* Add the onfocus listener to the 'firstname' field. */"
1022 " if (name === 'firstname') {"
1023 " input_element.onfocus = function() {"
1024 " domAutomationController.send(true);"
1025 " };"
1026 " }"
1028 " form.appendChild(input_element);"
1029 " }"
1031 " form.appendChild(document.createElement('br'));"
1032 "};"
1034 "function BuildForm() {"
1035 " var elements = ["
1036 " ['firstname', 'First name:'],"
1037 " ['lastname', 'Last name:'],"
1038 " ['address1', 'Address line 1:'],"
1039 " ['address2', 'Address line 2:'],"
1040 " ['city', 'City:'],"
1041 " ['state', 'State:'],"
1042 " ['zip', 'ZIP code:'],"
1043 " ['country', 'Country:'],"
1044 " ['phone', 'Phone number:'],"
1045 " ];"
1047 " for (var i = 0; i < elements.length; i++) {"
1048 " var name = elements[i][0];"
1049 " var label = elements[i][1];"
1050 " AddElement(name, label);"
1051 " }"
1052 "};"
1053 "</script>")));
1055 // Dynamically construct the form.
1056 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
1058 // Invoke Autofill.
1059 TryBasicFormFill();
1062 // Test that form filling works after reloading the current page.
1063 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) {
1064 CreateTestProfile();
1066 // Load the test page.
1067 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1068 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1070 // Reload the page.
1071 content::WebContents* web_contents = GetWebContents();
1072 web_contents->GetController().Reload(false);
1073 content::WaitForLoadStop(web_contents);
1075 // Invoke Autofill.
1076 TryBasicFormFill();
1079 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterTranslate) {
1080 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1081 if (TranslateService::IsTranslateBubbleEnabled())
1082 return;
1084 CreateTestProfile();
1086 GURL url(std::string(kDataURIPrefix) +
1087 "<form action=\"http://www.example.com/\" method=\"POST\">"
1088 "<label for=\"fn\">なまえ</label>"
1089 " <input type=\"text\" id=\"fn\""
1090 " onfocus=\"domAutomationController.send(true)\""
1091 "><br>"
1092 "<label for=\"ln\">みょうじ</label>"
1093 " <input type=\"text\" id=\"ln\"><br>"
1094 "<label for=\"a1\">Address line 1:</label>"
1095 " <input type=\"text\" id=\"a1\"><br>"
1096 "<label for=\"a2\">Address line 2:</label>"
1097 " <input type=\"text\" id=\"a2\"><br>"
1098 "<label for=\"ci\">City:</label>"
1099 " <input type=\"text\" id=\"ci\"><br>"
1100 "<label for=\"st\">State:</label>"
1101 " <select id=\"st\">"
1102 " <option value=\"\" selected=\"yes\">--</option>"
1103 " <option value=\"CA\">California</option>"
1104 " <option value=\"TX\">Texas</option>"
1105 " </select><br>"
1106 "<label for=\"z\">ZIP code:</label>"
1107 " <input type=\"text\" id=\"z\"><br>"
1108 "<label for=\"co\">Country:</label>"
1109 " <select id=\"co\">"
1110 " <option value=\"\" selected=\"yes\">--</option>"
1111 " <option value=\"CA\">Canada</option>"
1112 " <option value=\"US\">United States</option>"
1113 " </select><br>"
1114 "<label for=\"ph\">Phone number:</label>"
1115 " <input type=\"text\" id=\"ph\"><br>"
1116 "</form>"
1117 // Add additional Japanese characters to ensure the translate bar
1118 // will appear.
1119 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
1120 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
1122 content::WindowedNotificationObserver infobar_observer(
1123 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
1124 content::NotificationService::AllSources());
1125 ASSERT_NO_FATAL_FAILURE(
1126 ui_test_utils::NavigateToURL(browser(), url));
1128 // Wait for the translation bar to appear and get it.
1129 infobar_observer.Wait();
1130 InfoBarService* infobar_service =
1131 InfoBarService::FromWebContents(GetWebContents());
1132 translate::TranslateInfoBarDelegate* delegate =
1133 infobar_service->infobar_at(0)->delegate()->AsTranslateInfoBarDelegate();
1134 ASSERT_TRUE(delegate);
1135 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1136 delegate->translate_step());
1138 // Simulate translation button press.
1139 delegate->Translate();
1141 content::WindowedNotificationObserver translation_observer(
1142 chrome::NOTIFICATION_PAGE_TRANSLATED,
1143 content::NotificationService::AllSources());
1145 // Simulate the translate script being retrieved.
1146 // Pass fake google.translate lib as the translate script.
1147 SimulateURLFetch(true);
1149 // Simulate the render notifying the translation has been done.
1150 translation_observer.Wait();
1152 TryBasicFormFill();
1155 // Test phone fields parse correctly from a given profile.
1156 // The high level key presses execute the following: Select the first text
1157 // field, invoke the autofill popup list, select the first profile within the
1158 // list, and commit to the profile to populate the form.
1159 // Flakily times out on windows. http://crbug.com/390564
1160 #if defined(OS_WIN)
1161 #define MAYBE_ComparePhoneNumbers DISABLED_ComparePhoneNumbers
1162 #else
1163 #define MAYBE_ComparePhoneNumbers ComparePhoneNumbers
1164 #endif
1165 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_ComparePhoneNumbers) {
1166 ASSERT_TRUE(test_server()->Start());
1168 AutofillProfile profile;
1169 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1170 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1171 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 H St."));
1172 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1173 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1174 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1175 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1-408-555-4567"));
1176 SetProfile(profile);
1178 GURL url = test_server()->GetURL("files/autofill/form_phones.html");
1179 ui_test_utils::NavigateToURL(browser(), url);
1180 PopulateForm("NAME_FIRST");
1182 ExpectFieldValue("NAME_FIRST", "Bob");
1183 ExpectFieldValue("NAME_LAST", "Smith");
1184 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
1185 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
1186 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
1187 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
1188 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
1189 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
1190 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
1191 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
1192 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
1193 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
1194 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
1195 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
1196 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
1197 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
1198 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
1201 // Test that Autofill does not fill in read-only fields.
1202 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, NoAutofillForReadOnlyFields) {
1203 ASSERT_TRUE(test_server()->Start());
1205 std::string addr_line1("1234 H St.");
1207 AutofillProfile profile;
1208 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1209 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1210 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("bsmith@gmail.com"));
1211 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
1212 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1213 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1214 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1215 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Company X"));
1216 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("408-871-4567"));
1217 SetProfile(profile);
1219 GURL url = test_server()->GetURL("files/autofill/read_only_field_test.html");
1220 ui_test_utils::NavigateToURL(browser(), url);
1221 PopulateForm("firstname");
1223 ExpectFieldValue("email", std::string());
1224 ExpectFieldValue("address", addr_line1);
1227 // Test form is fillable from a profile after form was reset.
1228 // Steps:
1229 // 1. Fill form using a saved profile.
1230 // 2. Reset the form.
1231 // 3. Fill form using a saved profile.
1232 // Flakily times out: http://crbug.com/270341
1233 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_FormFillableOnReset) {
1234 ASSERT_TRUE(test_server()->Start());
1236 CreateTestProfile();
1238 GURL url = test_server()->GetURL("files/autofill/autofill_test_form.html");
1239 ui_test_utils::NavigateToURL(browser(), url);
1240 PopulateForm("NAME_FIRST");
1242 ASSERT_TRUE(content::ExecuteScript(
1243 GetWebContents(), "document.getElementById('testform').reset()"));
1245 PopulateForm("NAME_FIRST");
1247 ExpectFieldValue("NAME_FIRST", "Milton");
1248 ExpectFieldValue("NAME_LAST", "Waddams");
1249 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
1250 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
1251 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
1252 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
1253 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
1254 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
1255 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
1258 // Test Autofill distinguishes a middle initial in a name.
1259 // Flakily times out: http://crbug.com/270341
1260 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1261 DISABLED_DistinguishMiddleInitialWithinName) {
1262 ASSERT_TRUE(test_server()->Start());
1264 CreateTestProfile();
1266 GURL url = test_server()->GetURL(
1267 "files/autofill/autofill_middleinit_form.html");
1268 ui_test_utils::NavigateToURL(browser(), url);
1269 PopulateForm("NAME_FIRST");
1271 ExpectFieldValue("NAME_MIDDLE", "C");
1274 // Test forms with multiple email addresses are filled properly.
1275 // Entire form should be filled with one user gesture.
1276 // Flakily times out: http://crbug.com/270341
1277 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1278 DISABLED_MultipleEmailFilledByOneUserGesture) {
1279 ASSERT_TRUE(test_server()->Start());
1281 std::string email("bsmith@gmail.com");
1283 AutofillProfile profile;
1284 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1285 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1286 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email));
1287 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("4088714567"));
1288 SetProfile(profile);
1290 GURL url = test_server()->GetURL(
1291 "files/autofill/autofill_confirmemail_form.html");
1292 ui_test_utils::NavigateToURL(browser(), url);
1293 PopulateForm("NAME_FIRST");
1295 ExpectFieldValue("EMAIL_CONFIRM", email);
1296 // TODO(isherman): verify entire form.
1299 // http://crbug.com/281527
1300 #if defined(OS_MACOSX)
1301 #define MAYBE_FormFillLatencyAfterSubmit FormFillLatencyAfterSubmit
1302 #else
1303 #define MAYBE_FormFillLatencyAfterSubmit DISABLED_FormFillLatencyAfterSubmit
1304 #endif
1305 // Test latency time on form submit with lots of stored Autofill profiles.
1306 // This test verifies when a profile is selected from the Autofill dictionary
1307 // that consists of thousands of profiles, the form does not hang after being
1308 // submitted.
1309 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1310 MAYBE_FormFillLatencyAfterSubmit) {
1311 ASSERT_TRUE(test_server()->Start());
1313 std::vector<std::string> cities;
1314 cities.push_back("San Jose");
1315 cities.push_back("San Francisco");
1316 cities.push_back("Sacramento");
1317 cities.push_back("Los Angeles");
1319 std::vector<std::string> streets;
1320 streets.push_back("St");
1321 streets.push_back("Ave");
1322 streets.push_back("Ln");
1323 streets.push_back("Ct");
1325 const int kNumProfiles = 1500;
1326 base::Time start_time = base::Time::Now();
1327 std::vector<AutofillProfile> profiles;
1328 for (int i = 0; i < kNumProfiles; i++) {
1329 AutofillProfile profile;
1330 base::string16 name(base::IntToString16(i));
1331 base::string16 email(name + ASCIIToUTF16("@example.com"));
1332 base::string16 street = ASCIIToUTF16(
1333 base::IntToString(base::RandInt(0, 10000)) + " " +
1334 streets[base::RandInt(0, streets.size() - 1)]);
1335 base::string16 city =
1336 ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]);
1337 base::string16 zip(base::IntToString16(base::RandInt(0, 10000)));
1338 profile.SetRawInfo(NAME_FIRST, name);
1339 profile.SetRawInfo(EMAIL_ADDRESS, email);
1340 profile.SetRawInfo(ADDRESS_HOME_LINE1, street);
1341 profile.SetRawInfo(ADDRESS_HOME_CITY, city);
1342 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1343 profile.SetRawInfo(ADDRESS_HOME_ZIP, zip);
1344 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1345 profiles.push_back(profile);
1347 SetProfiles(&profiles);
1348 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1349 // can be removd.
1350 LOG(INFO) << "Created " << kNumProfiles << " profiles in " <<
1351 (base::Time::Now() - start_time).InSeconds() << " seconds.";
1353 GURL url = test_server()->GetURL(
1354 "files/autofill/latency_after_submit_test.html");
1355 ui_test_utils::NavigateToURL(browser(), url);
1356 PopulateForm("NAME_FIRST");
1358 content::WindowedNotificationObserver load_stop_observer(
1359 content::NOTIFICATION_LOAD_STOP,
1360 content::Source<content::NavigationController>(
1361 &GetWebContents()->GetController()));
1363 ASSERT_TRUE(content::ExecuteScript(
1364 GetRenderViewHost(),
1365 "document.getElementById('testform').submit();"));
1366 // This will ensure the test didn't hang.
1367 load_stop_observer.Wait();
1370 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1371 // is interacting with the form. This is a regression test for
1372 // http://crbug.com/160476
1373 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1374 DisableAutocompleteWhileFilling) {
1375 CreateTestProfile();
1377 // Load the test page.
1378 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1379 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1381 // Invoke Autofill: Start filling the first name field with "M" and wait for
1382 // the popup to be shown.
1383 FocusFirstNameField();
1384 SendKeyToPageAndWait(ui::VKEY_M);
1386 // Now that the popup with suggestions is showing, disable autocomplete for
1387 // the active field.
1388 ASSERT_TRUE(content::ExecuteScript(
1389 GetRenderViewHost(),
1390 "document.querySelector('input').autocomplete = 'off';"));
1392 // Press the down arrow to select the suggestion and attempt to preview the
1393 // autofilled form.
1394 SendKeyToPopupAndWait(ui::VKEY_DOWN);
1397 } // namespace autofill