Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_interactive_uitest.cc
blob7a679e9303e3108e9b93aa6e8e810da30c562ee6
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/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/autofill_uitest_util.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_window.h"
25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/interactive_test_utils.h"
28 #include "chrome/test/base/test_switches.h"
29 #include "chrome/test/base/ui_test_utils.h"
30 #include "components/autofill/content/browser/content_autofill_driver.h"
31 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
32 #include "components/autofill/core/browser/autofill_manager.h"
33 #include "components/autofill/core/browser/autofill_manager_test_delegate.h"
34 #include "components/autofill/core/browser/autofill_profile.h"
35 #include "components/autofill/core/browser/autofill_test_utils.h"
36 #include "components/autofill/core/browser/validation.h"
37 #include "components/infobars/core/infobar.h"
38 #include "components/translate/core/browser/translate_infobar_delegate.h"
39 #include "components/translate/core/browser/translate_manager.h"
40 #include "content/public/browser/navigation_controller.h"
41 #include "content/public/browser/notification_observer.h"
42 #include "content/public/browser/notification_registrar.h"
43 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/render_view_host.h"
45 #include "content/public/browser/render_widget_host.h"
46 #include "content/public/browser/web_contents.h"
47 #include "content/public/test/browser_test_utils.h"
48 #include "content/public/test/test_renderer_host.h"
49 #include "net/base/net_errors.h"
50 #include "net/url_request/test_url_fetcher_factory.h"
51 #include "net/url_request/url_request_status.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;
58 namespace autofill {
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>"
79 " </select><br>"
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>"
87 " </select><br>"
88 "<label for=\"phone\">Phone number:</label>"
89 " <input type=\"text\" id=\"phone\"><br>"
90 "</form>";
93 // AutofillManagerTestDelegateImpl --------------------------------------------
95 class AutofillManagerTestDelegateImpl
96 : public autofill::AutofillManagerTestDelegate {
97 public:
98 AutofillManagerTestDelegateImpl() {}
99 ~AutofillManagerTestDelegateImpl() override {}
101 // autofill::AutofillManagerTestDelegate:
102 void DidPreviewFormData() override {
103 ASSERT_TRUE(loop_runner_->loop_running());
104 loop_runner_->Quit();
107 void DidFillFormData() override {
108 ASSERT_TRUE(loop_runner_->loop_running());
109 loop_runner_->Quit();
112 void DidShowSuggestions() override {
113 ASSERT_TRUE(loop_runner_->loop_running());
114 loop_runner_->Quit();
117 void Reset() {
118 loop_runner_ = new content::MessageLoopRunner();
121 void Wait() {
122 loop_runner_->Run();
125 private:
126 scoped_refptr<content::MessageLoopRunner> loop_runner_;
128 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl);
131 // AutofillInteractiveTest ----------------------------------------------------
133 class AutofillInteractiveTest : public InProcessBrowserTest {
134 protected:
135 AutofillInteractiveTest() :
136 key_press_event_sink_(
137 base::Bind(&AutofillInteractiveTest::HandleKeyPressEvent,
138 base::Unretained(this))) {}
139 ~AutofillInteractiveTest() override {}
141 // InProcessBrowserTest:
142 void SetUpOnMainThread() override {
143 // Don't want Keychain coming up on Mac.
144 test::DisableSystemServices(browser()->profile()->GetPrefs());
146 // Inject the test delegate into the AutofillManager.
147 content::WebContents* web_contents = GetWebContents();
148 ContentAutofillDriver* autofill_driver =
149 ContentAutofillDriverFactory::FromWebContents(web_contents)
150 ->DriverForFrame(web_contents->GetMainFrame());
151 AutofillManager* autofill_manager = autofill_driver->autofill_manager();
152 autofill_manager->SetTestDelegate(&test_delegate_);
154 // If the mouse happened to be over where the suggestions are shown, then
155 // the preview will show up and will fail the tests. We need to give it a
156 // point that's within the browser frame, or else the method hangs.
157 gfx::Point reset_mouse(GetWebContents()->GetContainerBounds().origin());
158 reset_mouse = gfx::Point(reset_mouse.x() + 5, reset_mouse.y() + 5);
159 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(reset_mouse));
162 void TearDownOnMainThread() override {
163 // Make sure to close any showing popups prior to tearing down the UI.
164 content::WebContents* web_contents = GetWebContents();
165 AutofillManager* autofill_manager =
166 ContentAutofillDriverFactory::FromWebContents(web_contents)
167 ->DriverForFrame(web_contents->GetMainFrame())
168 ->autofill_manager();
169 autofill_manager->client()->HideAutofillPopup();
172 content::WebContents* GetWebContents() {
173 return browser()->tab_strip_model()->GetActiveWebContents();
176 content::RenderViewHost* GetRenderViewHost() {
177 return GetWebContents()->GetRenderViewHost();
180 void CreateTestProfile() {
181 AutofillProfile profile;
182 test::SetProfileInfo(
183 &profile, "Milton", "C.", "Waddams",
184 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane",
185 "Basement", "Austin", "Texas", "78744", "US", "5125551234");
187 AddTestProfile(browser(), profile);
190 // Populates a webpage form using autofill data and keypress events.
191 // This function focuses the specified input field in the form, and then
192 // sends keypress events to the tab to cause the form to be populated.
193 void PopulateForm(const std::string& field_id) {
194 std::string js("document.getElementById('" + field_id + "').focus();");
195 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js));
197 SendKeyToPageAndWait(ui::VKEY_DOWN);
198 SendKeyToPopupAndWait(ui::VKEY_DOWN);
199 SendKeyToPopupAndWait(ui::VKEY_RETURN);
202 void ExpectFieldValue(const std::string& field_name,
203 const std::string& expected_value) {
204 std::string value;
205 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
206 GetWebContents(),
207 "window.domAutomationController.send("
208 " document.getElementById('" + field_name + "').value);",
209 &value));
210 EXPECT_EQ(expected_value, value);
213 void GetFieldBackgroundColor(const std::string& field_name,
214 std::string* color) {
215 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
216 GetWebContents(),
217 "window.domAutomationController.send("
218 " document.defaultView.getComputedStyle(document.getElementById('" +
219 field_name + "')).backgroundColor);",
220 color));
223 void SimulateURLFetch(bool success) {
224 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
225 ASSERT_TRUE(fetcher);
226 net::Error error = success ? net::OK : net::ERR_FAILED;
228 std::string script = " var google = {};"
229 "google.translate = (function() {"
230 " return {"
231 " TranslateService: function() {"
232 " return {"
233 " isAvailable : function() {"
234 " return true;"
235 " },"
236 " restore : function() {"
237 " return;"
238 " },"
239 " getDetectedLanguage : function() {"
240 " return \"ja\";"
241 " },"
242 " translatePage : function(originalLang, targetLang,"
243 " onTranslateProgress) {"
244 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
245 std::string(kTestFormString) +
246 " ';"
247 " onTranslateProgress(100, true, false);"
248 " }"
249 " };"
250 " }"
251 " };"
252 "})();"
253 "cr.googleTranslate.onTranslateElementLoad();";
255 fetcher->set_url(fetcher->GetOriginalURL());
256 fetcher->set_status(net::URLRequestStatus::FromError(error));
257 fetcher->set_response_code(success ? 200 : 500);
258 fetcher->SetResponseString(script);
259 fetcher->delegate()->OnURLFetchComplete(fetcher);
262 void FocusFirstNameField() {
263 bool result = false;
264 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
265 GetRenderViewHost(),
266 "if (document.readyState === 'complete')"
267 " document.getElementById('firstname').focus();"
268 "else"
269 " domAutomationController.send(false);",
270 &result));
271 ASSERT_TRUE(result);
274 // Simulates a click on the middle of the DOM element with the given |id|.
275 void ClickElementWithId(const std::string& id) {
276 int x;
277 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
278 GetRenderViewHost(),
279 "var bounds = document.getElementById('" +
280 id +
281 "').getBoundingClientRect();"
282 "domAutomationController.send("
283 " Math.floor(bounds.left + bounds.width / 2));",
284 &x));
285 int y;
286 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
287 GetRenderViewHost(),
288 "var bounds = document.getElementById('" +
289 id +
290 "').getBoundingClientRect();"
291 "domAutomationController.send("
292 " Math.floor(bounds.top + bounds.height / 2));",
293 &y));
294 content::SimulateMouseClickAt(GetWebContents(),
296 blink::WebMouseEvent::ButtonLeft,
297 gfx::Point(x, y));
300 void ClickFirstNameField() {
301 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("firstname"));
304 // Make a pointless round trip to the renderer, giving the popup a chance to
305 // show if it's going to. If it does show, an assert in
306 // AutofillManagerTestDelegateImpl will trigger.
307 void MakeSurePopupDoesntAppear() {
308 int unused;
309 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
310 GetRenderViewHost(), "domAutomationController.send(42)", &unused));
313 void ExpectFilledTestForm() {
314 ExpectFieldValue("firstname", "Milton");
315 ExpectFieldValue("lastname", "Waddams");
316 ExpectFieldValue("address1", "4120 Freidrich Lane");
317 ExpectFieldValue("address2", "Basement");
318 ExpectFieldValue("city", "Austin");
319 ExpectFieldValue("state", "TX");
320 ExpectFieldValue("zip", "78744");
321 ExpectFieldValue("country", "US");
322 ExpectFieldValue("phone", "5125551234");
325 void SendKeyToPageAndWait(ui::KeyboardCode key) {
326 test_delegate_.Reset();
327 content::SimulateKeyPress(
328 GetWebContents(), key, false, false, false, false);
329 test_delegate_.Wait();
332 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) {
333 return true;
336 void SendKeyToPopupAndWait(ui::KeyboardCode key) {
337 // Route popup-targeted key presses via the render view host.
338 content::NativeWebKeyboardEvent event;
339 event.windowsKeyCode = key;
340 event.type = blink::WebKeyboardEvent::RawKeyDown;
341 test_delegate_.Reset();
342 // Install the key press event sink to ensure that any events that are not
343 // handled by the installed callbacks do not end up crashing the test.
344 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
345 GetRenderViewHost()->ForwardKeyboardEvent(event);
346 test_delegate_.Wait();
347 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
350 // Datalist does not support autofill preview. There is no need to start
351 // message loop for Datalist.
352 void SendKeyToDataListPopup(ui::KeyboardCode key) {
353 // Route popup-targeted key presses via the render view host.
354 content::NativeWebKeyboardEvent event;
355 event.windowsKeyCode = key;
356 event.type = blink::WebKeyboardEvent::RawKeyDown;
357 // Install the key press event sink to ensure that any events that are not
358 // handled by the installed callbacks do not end up crashing the test.
359 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
360 GetRenderViewHost()->ForwardKeyboardEvent(event);
361 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
364 void TryBasicFormFill() {
365 FocusFirstNameField();
367 // Start filling the first name field with "M" and wait for the popup to be
368 // shown.
369 SendKeyToPageAndWait(ui::VKEY_M);
371 // Press the down arrow to select the suggestion and preview the autofilled
372 // form.
373 SendKeyToPopupAndWait(ui::VKEY_DOWN);
375 // The previewed values should not be accessible to JavaScript.
376 ExpectFieldValue("firstname", "M");
377 ExpectFieldValue("lastname", std::string());
378 ExpectFieldValue("address1", std::string());
379 ExpectFieldValue("address2", std::string());
380 ExpectFieldValue("city", std::string());
381 ExpectFieldValue("state", std::string());
382 ExpectFieldValue("zip", std::string());
383 ExpectFieldValue("country", std::string());
384 ExpectFieldValue("phone", std::string());
385 // TODO(isherman): It would be nice to test that the previewed values are
386 // displayed: http://crbug.com/57220
388 // Press Enter to accept the autofill suggestions.
389 SendKeyToPopupAndWait(ui::VKEY_RETURN);
391 // The form should be filled.
392 ExpectFilledTestForm();
395 AutofillManagerTestDelegateImpl* test_delegate() { return &test_delegate_; }
397 private:
398 AutofillManagerTestDelegateImpl test_delegate_;
400 net::TestURLFetcherFactory url_fetcher_factory_;
402 // KeyPressEventCallback that serves as a sink to ensure that every key press
403 // event the tests create and have the WebContents forward is handled by some
404 // key press event callback. It is necessary to have this sinkbecause if no
405 // key press event callback handles the event (at least on Mac), a DCHECK
406 // ends up going off that the |event| doesn't have an |os_event| associated
407 // with it.
408 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_;
410 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest);
413 // Test that basic form fill is working.
414 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) {
415 CreateTestProfile();
417 // Load the test page.
418 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
419 GURL(std::string(kDataURIPrefix) + kTestFormString)));
421 // Invoke Autofill.
422 TryBasicFormFill();
425 // Test that form filling can be initiated by pressing the down arrow.
426 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) {
427 CreateTestProfile();
429 // Load the test page.
430 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
431 GURL(std::string(kDataURIPrefix) + kTestFormString)));
433 // Focus a fillable field.
434 FocusFirstNameField();
436 // Press the down arrow to initiate Autofill and wait for the popup to be
437 // shown.
438 SendKeyToPageAndWait(ui::VKEY_DOWN);
440 // Press the down arrow to select the suggestion and preview the autofilled
441 // form.
442 SendKeyToPopupAndWait(ui::VKEY_DOWN);
444 // Press Enter to accept the autofill suggestions.
445 SendKeyToPopupAndWait(ui::VKEY_RETURN);
447 // The form should be filled.
448 ExpectFilledTestForm();
451 // Flaky on the official cros-trunk. crbug.com/516052
452 #if defined(OFFICIAL_BUILD)
453 #define MAYBE_AutofillSelectViaTab DISABLED_AutofillSelectViaTab
454 #else
455 #define MAYBE_AutofillSelectViaTab AutofillSelectViaTab
456 #endif // defined(OFFICIAL_BUILD)
457 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_AutofillSelectViaTab) {
458 CreateTestProfile();
460 // Load the test page.
461 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
462 GURL(std::string(kDataURIPrefix) + kTestFormString)));
464 // Focus a fillable field.
465 FocusFirstNameField();
467 // Press the down arrow to initiate Autofill and wait for the popup to be
468 // shown.
469 SendKeyToPageAndWait(ui::VKEY_DOWN);
471 // Press the down arrow to select the suggestion and preview the autofilled
472 // form.
473 SendKeyToPopupAndWait(ui::VKEY_DOWN);
475 // Press tab to accept the autofill suggestions.
476 SendKeyToPopupAndWait(ui::VKEY_TAB);
478 // The form should be filled.
479 ExpectFilledTestForm();
482 // Flaky on the official cros-trunk. crbug.com/516052
483 #if defined(OFFICIAL_BUILD)
484 #define MAYBE_AutofillViaClick DISABLED_AutofillViaClick
485 #else
486 #define MAYBE_AutofillViaClick AutofillViaClick
487 #endif // defined(OFFICIAL_BUILD)
488 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_AutofillViaClick) {
489 CreateTestProfile();
491 // Load the test page.
492 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
493 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
494 // Focus a fillable field.
495 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
497 // Now click it.
498 test_delegate()->Reset();
499 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
500 test_delegate()->Wait();
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 // Makes sure that the first click does *not* activate the popup.
514 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForFirstClick) {
515 CreateTestProfile();
517 // Load the test page.
518 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
519 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
521 // Click the first name field while it's out of focus, then twiddle our thumbs
522 // a bit. If a popup were to show, it would hit the asserts in
523 // AutofillManagerTestDelegateImpl while we're wasting time.
524 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
525 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
527 // The second click should activate the popup since the first click focused
528 // the field.
529 test_delegate()->Reset();
530 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
531 test_delegate()->Wait();
534 // Makes sure that clicking outside the focused field doesn't activate
535 // the popup.
536 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForOutsideClick) {
537 CreateTestProfile();
539 // Load the test page.
540 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
541 browser(),
542 GURL(std::string(kDataURIPrefix) + kTestFormString +
543 "<button disabled id='disabled-button'>Cant click this</button>")));
545 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
547 // Clicking a disabled button will generate a mouse event but focus doesn't
548 // change. This tests that autofill can handle a mouse event outside a focused
549 // input *without* showing the popup.
550 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("disabled-button"));
551 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
553 test_delegate()->Reset();
554 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
555 test_delegate()->Wait();
558 // Test that a field is still autofillable after the previously autofilled
559 // value is deleted.
560 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) {
561 CreateTestProfile();
563 // Load the test page.
564 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
565 GURL(std::string(kDataURIPrefix) + kTestFormString)));
567 // Invoke and accept the Autofill popup and verify the form was filled.
568 FocusFirstNameField();
569 SendKeyToPageAndWait(ui::VKEY_M);
570 SendKeyToPopupAndWait(ui::VKEY_DOWN);
571 SendKeyToPopupAndWait(ui::VKEY_RETURN);
572 ExpectFilledTestForm();
574 // Delete the value of a filled field.
575 ASSERT_TRUE(content::ExecuteScript(
576 GetRenderViewHost(),
577 "document.getElementById('firstname').value = '';"));
578 ExpectFieldValue("firstname", "");
580 // Invoke and accept the Autofill popup and verify the field was filled.
581 SendKeyToPageAndWait(ui::VKEY_M);
582 SendKeyToPopupAndWait(ui::VKEY_DOWN);
583 SendKeyToPopupAndWait(ui::VKEY_RETURN);
584 ExpectFieldValue("firstname", "Milton");
587 // Test that an input field is not rendered with the yellow autofilled
588 // background color when choosing an option from the datalist suggestion list.
589 #if defined(OS_MACOSX)
590 // Flakily triggers and assert on Mac.
591 // http://crbug.com/419868
592 #define MAYBE_OnSelectOptionFromDatalist DISABLED_OnSelectOptionFromDatalist
593 #else
594 #define MAYBE_OnSelectOptionFromDatalist OnSelectOptionFromDatalist
595 #endif
596 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
597 MAYBE_OnSelectOptionFromDatalist) {
598 // Load the test page.
599 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
600 browser(),
601 GURL(std::string(kDataURIPrefix) +
602 "<form action=\"http://www.example.com/\" method=\"POST\">"
603 " <input list=\"dl\" type=\"search\" id=\"firstname\""
604 " onfocus=\"domAutomationController.send(true)\"><br>"
605 " <datalist id=\"dl\">"
606 " <option value=\"Adam\"></option>"
607 " <option value=\"Bob\"></option>"
608 " <option value=\"Carl\"></option>"
609 " </datalist>"
610 "</form>")));
611 std::string orginalcolor;
612 GetFieldBackgroundColor("firstname", &orginalcolor);
614 FocusFirstNameField();
615 SendKeyToPageAndWait(ui::VKEY_DOWN);
616 SendKeyToDataListPopup(ui::VKEY_DOWN);
617 SendKeyToDataListPopup(ui::VKEY_RETURN);
618 ExpectFieldValue("firstname", "Adam");
619 std::string color;
620 GetFieldBackgroundColor("firstname", &color);
621 EXPECT_EQ(color, orginalcolor);
624 // Test that a JavaScript oninput event is fired after auto-filling a form.
625 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) {
626 CreateTestProfile();
628 const char kOnInputScript[] =
629 "<script>"
630 "focused_fired = false;"
631 "unfocused_fired = false;"
632 "changed_select_fired = false;"
633 "unchanged_select_fired = false;"
634 "document.getElementById('firstname').oninput = function() {"
635 " focused_fired = true;"
636 "};"
637 "document.getElementById('lastname').oninput = function() {"
638 " unfocused_fired = true;"
639 "};"
640 "document.getElementById('state').oninput = function() {"
641 " changed_select_fired = true;"
642 "};"
643 "document.getElementById('country').oninput = function() {"
644 " unchanged_select_fired = true;"
645 "};"
646 "document.getElementById('country').value = 'US';"
647 "</script>";
649 // Load the test page.
650 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
651 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnInputScript)));
653 // Invoke Autofill.
654 FocusFirstNameField();
656 // Start filling the first name field with "M" and wait for the popup to be
657 // shown.
658 SendKeyToPageAndWait(ui::VKEY_M);
660 // Press the down arrow to select the suggestion and preview the autofilled
661 // form.
662 SendKeyToPopupAndWait(ui::VKEY_DOWN);
664 // Press Enter to accept the autofill suggestions.
665 SendKeyToPopupAndWait(ui::VKEY_RETURN);
667 // The form should be filled.
668 ExpectFilledTestForm();
670 bool focused_fired = false;
671 bool unfocused_fired = false;
672 bool changed_select_fired = false;
673 bool unchanged_select_fired = false;
674 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
675 GetRenderViewHost(),
676 "domAutomationController.send(focused_fired);",
677 &focused_fired));
678 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
679 GetRenderViewHost(),
680 "domAutomationController.send(unfocused_fired);",
681 &unfocused_fired));
682 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
683 GetRenderViewHost(),
684 "domAutomationController.send(changed_select_fired);",
685 &changed_select_fired));
686 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
687 GetRenderViewHost(),
688 "domAutomationController.send(unchanged_select_fired);",
689 &unchanged_select_fired));
690 EXPECT_TRUE(focused_fired);
691 EXPECT_TRUE(unfocused_fired);
692 EXPECT_TRUE(changed_select_fired);
693 EXPECT_FALSE(unchanged_select_fired);
696 // Test that a JavaScript onchange event is fired after auto-filling a form.
697 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) {
698 CreateTestProfile();
700 const char kOnChangeScript[] =
701 "<script>"
702 "focused_fired = false;"
703 "unfocused_fired = false;"
704 "changed_select_fired = false;"
705 "unchanged_select_fired = false;"
706 "document.getElementById('firstname').onchange = function() {"
707 " focused_fired = true;"
708 "};"
709 "document.getElementById('lastname').onchange = function() {"
710 " unfocused_fired = true;"
711 "};"
712 "document.getElementById('state').onchange = function() {"
713 " changed_select_fired = true;"
714 "};"
715 "document.getElementById('country').onchange = function() {"
716 " unchanged_select_fired = true;"
717 "};"
718 "document.getElementById('country').value = 'US';"
719 "</script>";
721 // Load the test page.
722 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
723 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnChangeScript)));
725 // Invoke Autofill.
726 FocusFirstNameField();
728 // Start filling the first name field with "M" and wait for the popup to be
729 // shown.
730 SendKeyToPageAndWait(ui::VKEY_M);
732 // Press the down arrow to select the suggestion and preview the autofilled
733 // form.
734 SendKeyToPopupAndWait(ui::VKEY_DOWN);
736 // Press Enter to accept the autofill suggestions.
737 SendKeyToPopupAndWait(ui::VKEY_RETURN);
739 // The form should be filled.
740 ExpectFilledTestForm();
742 bool focused_fired = false;
743 bool unfocused_fired = false;
744 bool changed_select_fired = false;
745 bool unchanged_select_fired = false;
746 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
747 GetRenderViewHost(),
748 "domAutomationController.send(focused_fired);",
749 &focused_fired));
750 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
751 GetRenderViewHost(),
752 "domAutomationController.send(unfocused_fired);",
753 &unfocused_fired));
754 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
755 GetRenderViewHost(),
756 "domAutomationController.send(changed_select_fired);",
757 &changed_select_fired));
758 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
759 GetRenderViewHost(),
760 "domAutomationController.send(unchanged_select_fired);",
761 &unchanged_select_fired));
762 EXPECT_TRUE(focused_fired);
763 EXPECT_TRUE(unfocused_fired);
764 EXPECT_TRUE(changed_select_fired);
765 EXPECT_FALSE(unchanged_select_fired);
768 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) {
769 CreateTestProfile();
771 const char kInputFiresBeforeChangeScript[] =
772 "<script>"
773 "inputElementEvents = [];"
774 "function recordInputElementEvent(e) {"
775 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';"
776 " inputElementEvents.push(e.type);"
778 "selectElementEvents = [];"
779 "function recordSelectElementEvent(e) {"
780 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';"
781 " selectElementEvents.push(e.type);"
783 "document.getElementById('lastname').oninput = recordInputElementEvent;"
784 "document.getElementById('lastname').onchange = recordInputElementEvent;"
785 "document.getElementById('country').oninput = recordSelectElementEvent;"
786 "document.getElementById('country').onchange = recordSelectElementEvent;"
787 "</script>";
789 // Load the test page.
790 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
791 GURL(std::string(kDataURIPrefix) + kTestFormString +
792 kInputFiresBeforeChangeScript)));
794 // Invoke and accept the Autofill popup and verify the form was filled.
795 FocusFirstNameField();
796 SendKeyToPageAndWait(ui::VKEY_M);
797 SendKeyToPopupAndWait(ui::VKEY_DOWN);
798 SendKeyToPopupAndWait(ui::VKEY_RETURN);
799 ExpectFilledTestForm();
801 int num_input_element_events = -1;
802 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
803 GetRenderViewHost(),
804 "domAutomationController.send(inputElementEvents.length);",
805 &num_input_element_events));
806 EXPECT_EQ(2, num_input_element_events);
808 std::vector<std::string> input_element_events;
809 input_element_events.resize(2);
811 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
812 GetRenderViewHost(),
813 "domAutomationController.send(inputElementEvents[0]);",
814 &input_element_events[0]));
815 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
816 GetRenderViewHost(),
817 "domAutomationController.send(inputElementEvents[1]);",
818 &input_element_events[1]));
820 EXPECT_EQ("input", input_element_events[0]);
821 EXPECT_EQ("change", input_element_events[1]);
823 int num_select_element_events = -1;
824 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
825 GetRenderViewHost(),
826 "domAutomationController.send(selectElementEvents.length);",
827 &num_select_element_events));
828 EXPECT_EQ(2, num_select_element_events);
830 std::vector<std::string> select_element_events;
831 select_element_events.resize(2);
833 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
834 GetRenderViewHost(),
835 "domAutomationController.send(selectElementEvents[0]);",
836 &select_element_events[0]));
837 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
838 GetRenderViewHost(),
839 "domAutomationController.send(selectElementEvents[1]);",
840 &select_element_events[1]));
842 EXPECT_EQ("input", select_element_events[0]);
843 EXPECT_EQ("change", select_element_events[1]);
846 // Test that we can autofill forms distinguished only by their |id| attribute.
847 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
848 AutofillFormsDistinguishedById) {
849 CreateTestProfile();
851 // Load the test page.
852 const std::string kURL =
853 std::string(kDataURIPrefix) + kTestFormString +
854 "<script>"
855 "var mainForm = document.forms[0];"
856 "mainForm.id = 'mainForm';"
857 "var newForm = document.createElement('form');"
858 "newForm.action = mainForm.action;"
859 "newForm.method = mainForm.method;"
860 "newForm.id = 'newForm';"
861 "mainForm.parentNode.insertBefore(newForm, mainForm);"
862 "</script>";
863 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL)));
865 // Invoke Autofill.
866 TryBasicFormFill();
869 // Test that we properly autofill forms with repeated fields.
870 // In the wild, the repeated fields are typically either email fields
871 // (duplicated for "confirmation"); or variants that are hot-swapped via
872 // JavaScript, with only one actually visible at any given time.
873 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) {
874 CreateTestProfile();
876 // Load the test page.
877 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
878 GURL(std::string(kDataURIPrefix) +
879 "<form action=\"http://www.example.com/\" method=\"POST\">"
880 "<label for=\"firstname\">First name:</label>"
881 " <input type=\"text\" id=\"firstname\""
882 " onfocus=\"domAutomationController.send(true)\"><br>"
883 "<label for=\"lastname\">Last name:</label>"
884 " <input type=\"text\" id=\"lastname\"><br>"
885 "<label for=\"address1\">Address line 1:</label>"
886 " <input type=\"text\" id=\"address1\"><br>"
887 "<label for=\"address2\">Address line 2:</label>"
888 " <input type=\"text\" id=\"address2\"><br>"
889 "<label for=\"city\">City:</label>"
890 " <input type=\"text\" id=\"city\"><br>"
891 "<label for=\"state\">State:</label>"
892 " <select id=\"state\">"
893 " <option value=\"\" selected=\"yes\">--</option>"
894 " <option value=\"CA\">California</option>"
895 " <option value=\"TX\">Texas</option>"
896 " </select><br>"
897 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
898 " <input type=\"text\" id=\"state_freeform\""
899 " style=\"display:none\"><br>"
900 "<label for=\"zip\">ZIP code:</label>"
901 " <input type=\"text\" id=\"zip\"><br>"
902 "<label for=\"country\">Country:</label>"
903 " <select id=\"country\">"
904 " <option value=\"\" selected=\"yes\">--</option>"
905 " <option value=\"CA\">Canada</option>"
906 " <option value=\"US\">United States</option>"
907 " </select><br>"
908 "<label for=\"phone\">Phone number:</label>"
909 " <input type=\"text\" id=\"phone\"><br>"
910 "</form>")));
912 // Invoke Autofill.
913 TryBasicFormFill();
914 ExpectFieldValue("state_freeform", std::string());
917 // Test that we properly autofill forms with non-autofillable fields.
918 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
919 AutofillFormWithNonAutofillableField) {
920 CreateTestProfile();
922 // Load the test page.
923 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
924 GURL(std::string(kDataURIPrefix) +
925 "<form action=\"http://www.example.com/\" method=\"POST\">"
926 "<label for=\"firstname\">First name:</label>"
927 " <input type=\"text\" id=\"firstname\""
928 " onfocus=\"domAutomationController.send(true)\"><br>"
929 "<label for=\"middlename\">Middle name:</label>"
930 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
931 "<label for=\"lastname\">Last name:</label>"
932 " <input type=\"text\" id=\"lastname\"><br>"
933 "<label for=\"address1\">Address line 1:</label>"
934 " <input type=\"text\" id=\"address1\"><br>"
935 "<label for=\"address2\">Address line 2:</label>"
936 " <input type=\"text\" id=\"address2\"><br>"
937 "<label for=\"city\">City:</label>"
938 " <input type=\"text\" id=\"city\"><br>"
939 "<label for=\"state\">State:</label>"
940 " <select id=\"state\">"
941 " <option value=\"\" selected=\"yes\">--</option>"
942 " <option value=\"CA\">California</option>"
943 " <option value=\"TX\">Texas</option>"
944 " </select><br>"
945 "<label for=\"zip\">ZIP code:</label>"
946 " <input type=\"text\" id=\"zip\"><br>"
947 "<label for=\"country\">Country:</label>"
948 " <select id=\"country\">"
949 " <option value=\"\" selected=\"yes\">--</option>"
950 " <option value=\"CA\">Canada</option>"
951 " <option value=\"US\">United States</option>"
952 " </select><br>"
953 "<label for=\"phone\">Phone number:</label>"
954 " <input type=\"text\" id=\"phone\"><br>"
955 "</form>")));
957 // Invoke Autofill.
958 TryBasicFormFill();
961 // Test that we can Autofill dynamically generated forms.
962 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) {
963 CreateTestProfile();
965 // Load the test page.
966 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
967 GURL(std::string(kDataURIPrefix) +
968 "<form id=\"form\" action=\"http://www.example.com/\""
969 " method=\"POST\"></form>"
970 "<script>"
971 "function AddElement(name, label) {"
972 " var form = document.getElementById('form');"
974 " var label_text = document.createTextNode(label);"
975 " var label_element = document.createElement('label');"
976 " label_element.setAttribute('for', name);"
977 " label_element.appendChild(label_text);"
978 " form.appendChild(label_element);"
980 " if (name === 'state' || name === 'country') {"
981 " var select_element = document.createElement('select');"
982 " select_element.setAttribute('id', name);"
983 " select_element.setAttribute('name', name);"
985 " /* Add an empty selected option. */"
986 " var default_option = new Option('--', '', true);"
987 " select_element.appendChild(default_option);"
989 " /* Add the other options. */"
990 " if (name == 'state') {"
991 " var option1 = new Option('California', 'CA');"
992 " select_element.appendChild(option1);"
993 " var option2 = new Option('Texas', 'TX');"
994 " select_element.appendChild(option2);"
995 " } else {"
996 " var option1 = new Option('Canada', 'CA');"
997 " select_element.appendChild(option1);"
998 " var option2 = new Option('United States', 'US');"
999 " select_element.appendChild(option2);"
1000 " }"
1002 " form.appendChild(select_element);"
1003 " } else {"
1004 " var input_element = document.createElement('input');"
1005 " input_element.setAttribute('id', name);"
1006 " input_element.setAttribute('name', name);"
1008 " /* Add the onfocus listener to the 'firstname' field. */"
1009 " if (name === 'firstname') {"
1010 " input_element.onfocus = function() {"
1011 " domAutomationController.send(true);"
1012 " };"
1013 " }"
1015 " form.appendChild(input_element);"
1016 " }"
1018 " form.appendChild(document.createElement('br'));"
1019 "};"
1021 "function BuildForm() {"
1022 " var elements = ["
1023 " ['firstname', 'First name:'],"
1024 " ['lastname', 'Last name:'],"
1025 " ['address1', 'Address line 1:'],"
1026 " ['address2', 'Address line 2:'],"
1027 " ['city', 'City:'],"
1028 " ['state', 'State:'],"
1029 " ['zip', 'ZIP code:'],"
1030 " ['country', 'Country:'],"
1031 " ['phone', 'Phone number:'],"
1032 " ];"
1034 " for (var i = 0; i < elements.length; i++) {"
1035 " var name = elements[i][0];"
1036 " var label = elements[i][1];"
1037 " AddElement(name, label);"
1038 " }"
1039 "};"
1040 "</script>")));
1042 // Dynamically construct the form.
1043 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
1045 // Invoke Autofill.
1046 TryBasicFormFill();
1049 // Test that form filling works after reloading the current page.
1050 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) {
1051 CreateTestProfile();
1053 // Load the test page.
1054 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1055 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1057 // Reload the page.
1058 content::WebContents* web_contents = GetWebContents();
1059 web_contents->GetController().Reload(false);
1060 content::WaitForLoadStop(web_contents);
1062 // Invoke Autofill.
1063 TryBasicFormFill();
1066 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterTranslate) {
1067 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
1068 if (TranslateService::IsTranslateBubbleEnabled())
1069 return;
1071 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
1073 CreateTestProfile();
1075 GURL url(std::string(kDataURIPrefix) +
1076 "<form action=\"http://www.example.com/\" method=\"POST\">"
1077 "<label for=\"fn\">なまえ</label>"
1078 " <input type=\"text\" id=\"fn\""
1079 " onfocus=\"domAutomationController.send(true)\""
1080 "><br>"
1081 "<label for=\"ln\">みょうじ</label>"
1082 " <input type=\"text\" id=\"ln\"><br>"
1083 "<label for=\"a1\">Address line 1:</label>"
1084 " <input type=\"text\" id=\"a1\"><br>"
1085 "<label for=\"a2\">Address line 2:</label>"
1086 " <input type=\"text\" id=\"a2\"><br>"
1087 "<label for=\"ci\">City:</label>"
1088 " <input type=\"text\" id=\"ci\"><br>"
1089 "<label for=\"st\">State:</label>"
1090 " <select id=\"st\">"
1091 " <option value=\"\" selected=\"yes\">--</option>"
1092 " <option value=\"CA\">California</option>"
1093 " <option value=\"TX\">Texas</option>"
1094 " </select><br>"
1095 "<label for=\"z\">ZIP code:</label>"
1096 " <input type=\"text\" id=\"z\"><br>"
1097 "<label for=\"co\">Country:</label>"
1098 " <select id=\"co\">"
1099 " <option value=\"\" selected=\"yes\">--</option>"
1100 " <option value=\"CA\">Canada</option>"
1101 " <option value=\"US\">United States</option>"
1102 " </select><br>"
1103 "<label for=\"ph\">Phone number:</label>"
1104 " <input type=\"text\" id=\"ph\"><br>"
1105 "</form>"
1106 // Add additional Japanese characters to ensure the translate bar
1107 // will appear.
1108 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
1109 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
1111 content::WindowedNotificationObserver infobar_observer(
1112 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
1113 content::NotificationService::AllSources());
1114 ASSERT_NO_FATAL_FAILURE(
1115 ui_test_utils::NavigateToURL(browser(), url));
1117 // Wait for the translation bar to appear and get it.
1118 infobar_observer.Wait();
1119 InfoBarService* infobar_service =
1120 InfoBarService::FromWebContents(GetWebContents());
1121 translate::TranslateInfoBarDelegate* delegate =
1122 infobar_service->infobar_at(0)->delegate()->AsTranslateInfoBarDelegate();
1123 ASSERT_TRUE(delegate);
1124 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
1125 delegate->translate_step());
1127 // Simulate translation button press.
1128 delegate->Translate();
1130 content::WindowedNotificationObserver translation_observer(
1131 chrome::NOTIFICATION_PAGE_TRANSLATED,
1132 content::NotificationService::AllSources());
1134 // Simulate the translate script being retrieved.
1135 // Pass fake google.translate lib as the translate script.
1136 SimulateURLFetch(true);
1138 // Simulate the render notifying the translation has been done.
1139 translation_observer.Wait();
1141 TryBasicFormFill();
1144 // Test phone fields parse correctly from a given profile.
1145 // The high level key presses execute the following: Select the first text
1146 // field, invoke the autofill popup list, select the first profile within the
1147 // list, and commit to the profile to populate the form.
1148 // Flakily times out on windows. http://crbug.com/390564
1149 // Flaky on the official cros-trunk crbug.com/516052
1150 #if defined(OS_WIN) || defined(OFFICIAL_BUILD)
1151 #define MAYBE_ComparePhoneNumbers DISABLED_ComparePhoneNumbers
1152 #else
1153 #define MAYBE_ComparePhoneNumbers ComparePhoneNumbers
1154 #endif // defined(OS_WIN) || defined(OFFICIAL_BUILD)
1155 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_ComparePhoneNumbers) {
1156 ASSERT_TRUE(test_server()->Start());
1158 AutofillProfile profile;
1159 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1160 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1161 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 H St."));
1162 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1163 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1164 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1165 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1-408-555-4567"));
1166 SetTestProfile(browser(), profile);
1168 GURL url = test_server()->GetURL("files/autofill/form_phones.html");
1169 ui_test_utils::NavigateToURL(browser(), url);
1170 PopulateForm("NAME_FIRST");
1172 ExpectFieldValue("NAME_FIRST", "Bob");
1173 ExpectFieldValue("NAME_LAST", "Smith");
1174 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
1175 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
1176 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
1177 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
1178 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
1179 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
1180 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
1181 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
1182 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
1183 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
1184 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
1185 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
1186 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
1187 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
1188 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
1191 // Test that Autofill does not fill in read-only fields.
1192 // Flaky on the official cros-trunk. crbug.com/516052
1193 #if defined(OFFICIAL_BUILD)
1194 #define MAYBE_NoAutofillForReadOnlyFields DISABLED_NoAutofillForReadOnlyFields
1195 #else
1196 #define MAYBE_NoAutofillForReadOnlyFields NoAutofillForReadOnlyFields
1197 #endif // defined(OFFICIAL_BUILD)
1198 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1199 MAYBE_NoAutofillForReadOnlyFields) {
1200 ASSERT_TRUE(test_server()->Start());
1202 std::string addr_line1("1234 H St.");
1204 AutofillProfile profile;
1205 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1206 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1207 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("bsmith@gmail.com"));
1208 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
1209 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1210 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1211 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1212 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Company X"));
1213 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("408-871-4567"));
1214 SetTestProfile(browser(), profile);
1216 GURL url = test_server()->GetURL("files/autofill/read_only_field_test.html");
1217 ui_test_utils::NavigateToURL(browser(), url);
1218 PopulateForm("firstname");
1220 ExpectFieldValue("email", std::string());
1221 ExpectFieldValue("address", addr_line1);
1224 // Test form is fillable from a profile after form was reset.
1225 // Steps:
1226 // 1. Fill form using a saved profile.
1227 // 2. Reset the form.
1228 // 3. Fill form using a saved profile.
1229 // Flakily times out: http://crbug.com/270341
1230 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_FormFillableOnReset) {
1231 ASSERT_TRUE(test_server()->Start());
1233 CreateTestProfile();
1235 GURL url = test_server()->GetURL("files/autofill/autofill_test_form.html");
1236 ui_test_utils::NavigateToURL(browser(), url);
1237 PopulateForm("NAME_FIRST");
1239 ASSERT_TRUE(content::ExecuteScript(
1240 GetWebContents(), "document.getElementById('testform').reset()"));
1242 PopulateForm("NAME_FIRST");
1244 ExpectFieldValue("NAME_FIRST", "Milton");
1245 ExpectFieldValue("NAME_LAST", "Waddams");
1246 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
1247 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
1248 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
1249 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
1250 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
1251 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
1252 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
1255 // Test Autofill distinguishes a middle initial in a name.
1256 // Flakily times out: http://crbug.com/270341
1257 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1258 DISABLED_DistinguishMiddleInitialWithinName) {
1259 ASSERT_TRUE(test_server()->Start());
1261 CreateTestProfile();
1263 GURL url = test_server()->GetURL(
1264 "files/autofill/autofill_middleinit_form.html");
1265 ui_test_utils::NavigateToURL(browser(), url);
1266 PopulateForm("NAME_FIRST");
1268 ExpectFieldValue("NAME_MIDDLE", "C");
1271 // Test forms with multiple email addresses are filled properly.
1272 // Entire form should be filled with one user gesture.
1273 // Flakily times out: http://crbug.com/270341
1274 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1275 DISABLED_MultipleEmailFilledByOneUserGesture) {
1276 ASSERT_TRUE(test_server()->Start());
1278 std::string email("bsmith@gmail.com");
1280 AutofillProfile profile;
1281 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1282 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1283 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email));
1284 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("4088714567"));
1285 SetTestProfile(browser(), profile);
1287 GURL url = test_server()->GetURL(
1288 "files/autofill/autofill_confirmemail_form.html");
1289 ui_test_utils::NavigateToURL(browser(), url);
1290 PopulateForm("NAME_FIRST");
1292 ExpectFieldValue("EMAIL_CONFIRM", email);
1293 // TODO(isherman): verify entire form.
1296 // Test latency time on form submit with lots of stored Autofill profiles.
1297 // This test verifies when a profile is selected from the Autofill dictionary
1298 // that consists of thousands of profiles, the form does not hang after being
1299 // submitted.
1300 // Flakily times out: http://crbug.com/281527
1301 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1302 DISABLED_FormFillLatencyAfterSubmit) {
1303 ASSERT_TRUE(test_server()->Start());
1305 std::vector<std::string> cities;
1306 cities.push_back("San Jose");
1307 cities.push_back("San Francisco");
1308 cities.push_back("Sacramento");
1309 cities.push_back("Los Angeles");
1311 std::vector<std::string> streets;
1312 streets.push_back("St");
1313 streets.push_back("Ave");
1314 streets.push_back("Ln");
1315 streets.push_back("Ct");
1317 const int kNumProfiles = 1500;
1318 base::Time start_time = base::Time::Now();
1319 std::vector<AutofillProfile> profiles;
1320 for (int i = 0; i < kNumProfiles; i++) {
1321 AutofillProfile profile;
1322 base::string16 name(base::IntToString16(i));
1323 base::string16 email(name + ASCIIToUTF16("@example.com"));
1324 base::string16 street = ASCIIToUTF16(
1325 base::IntToString(base::RandInt(0, 10000)) + " " +
1326 streets[base::RandInt(0, streets.size() - 1)]);
1327 base::string16 city =
1328 ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]);
1329 base::string16 zip(base::IntToString16(base::RandInt(0, 10000)));
1330 profile.SetRawInfo(NAME_FIRST, name);
1331 profile.SetRawInfo(EMAIL_ADDRESS, email);
1332 profile.SetRawInfo(ADDRESS_HOME_LINE1, street);
1333 profile.SetRawInfo(ADDRESS_HOME_CITY, city);
1334 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1335 profile.SetRawInfo(ADDRESS_HOME_ZIP, zip);
1336 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1337 profiles.push_back(profile);
1339 SetTestProfiles(browser(), &profiles);
1340 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1341 // can be removd.
1342 LOG(INFO) << "Created " << kNumProfiles << " profiles in " <<
1343 (base::Time::Now() - start_time).InSeconds() << " seconds.";
1345 GURL url = test_server()->GetURL(
1346 "files/autofill/latency_after_submit_test.html");
1347 ui_test_utils::NavigateToURL(browser(), url);
1348 PopulateForm("NAME_FIRST");
1350 content::WindowedNotificationObserver load_stop_observer(
1351 content::NOTIFICATION_LOAD_STOP,
1352 content::Source<content::NavigationController>(
1353 &GetWebContents()->GetController()));
1355 ASSERT_TRUE(content::ExecuteScript(
1356 GetRenderViewHost(),
1357 "document.getElementById('testform').submit();"));
1358 // This will ensure the test didn't hang.
1359 load_stop_observer.Wait();
1362 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1363 // is interacting with the form. This is a regression test for
1364 // http://crbug.com/160476
1365 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1366 DisableAutocompleteWhileFilling) {
1367 CreateTestProfile();
1369 // Load the test page.
1370 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1371 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1373 // Invoke Autofill: Start filling the first name field with "M" and wait for
1374 // the popup to be shown.
1375 FocusFirstNameField();
1376 SendKeyToPageAndWait(ui::VKEY_M);
1378 // Now that the popup with suggestions is showing, disable autocomplete for
1379 // the active field.
1380 ASSERT_TRUE(content::ExecuteScript(
1381 GetRenderViewHost(),
1382 "document.querySelector('input').autocomplete = 'off';"));
1384 // Press the down arrow to select the suggestion and attempt to preview the
1385 // autofilled form.
1386 SendKeyToPopupAndWait(ui::VKEY_DOWN);
1389 } // namespace autofill