Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_interactive_uitest.cc
blobd27625d1500dc6460e296b5f1026cc79c4dfed6b
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/browser/ui/translate/translate_bubble_test_utils.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "chrome/test/base/interactive_test_utils.h"
30 #include "chrome/test/base/test_switches.h"
31 #include "chrome/test/base/ui_test_utils.h"
32 #include "components/autofill/content/browser/content_autofill_driver.h"
33 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
34 #include "components/autofill/core/browser/autofill_manager.h"
35 #include "components/autofill/core/browser/autofill_manager_test_delegate.h"
36 #include "components/autofill/core/browser/autofill_profile.h"
37 #include "components/autofill/core/browser/autofill_test_utils.h"
38 #include "components/autofill/core/browser/validation.h"
39 #include "components/infobars/core/infobar.h"
40 #include "components/translate/core/browser/translate_infobar_delegate.h"
41 #include "components/translate/core/browser/translate_manager.h"
42 #include "content/public/browser/navigation_controller.h"
43 #include "content/public/browser/notification_observer.h"
44 #include "content/public/browser/notification_registrar.h"
45 #include "content/public/browser/notification_service.h"
46 #include "content/public/browser/render_view_host.h"
47 #include "content/public/browser/render_widget_host.h"
48 #include "content/public/browser/web_contents.h"
49 #include "content/public/test/browser_test_utils.h"
50 #include "content/public/test/test_renderer_host.h"
51 #include "content/public/test/test_utils.h"
52 #include "net/base/net_errors.h"
53 #include "net/url_request/test_url_fetcher_factory.h"
54 #include "net/url_request/url_request_status.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 ~AutofillManagerTestDelegateImpl() override {}
104 // autofill::AutofillManagerTestDelegate:
105 void DidPreviewFormData() override {
106 ASSERT_TRUE(loop_runner_->loop_running());
107 loop_runner_->Quit();
110 void DidFillFormData() override {
111 ASSERT_TRUE(loop_runner_->loop_running());
112 loop_runner_->Quit();
115 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);
134 // AutofillInteractiveTest ----------------------------------------------------
136 class AutofillInteractiveTest : public InProcessBrowserTest {
137 protected:
138 AutofillInteractiveTest() :
139 key_press_event_sink_(
140 base::Bind(&AutofillInteractiveTest::HandleKeyPressEvent,
141 base::Unretained(this))) {}
142 ~AutofillInteractiveTest() override {}
144 // InProcessBrowserTest:
145 void SetUpOnMainThread() override {
146 // Don't want Keychain coming up on Mac.
147 test::DisableSystemServices(browser()->profile()->GetPrefs());
149 // Inject the test delegate into the AutofillManager.
150 content::WebContents* web_contents = GetWebContents();
151 ContentAutofillDriver* autofill_driver =
152 ContentAutofillDriverFactory::FromWebContents(web_contents)
153 ->DriverForFrame(web_contents->GetMainFrame());
154 AutofillManager* autofill_manager = autofill_driver->autofill_manager();
155 autofill_manager->SetTestDelegate(&test_delegate_);
157 // If the mouse happened to be over where the suggestions are shown, then
158 // the preview will show up and will fail the tests. We need to give it a
159 // point that's within the browser frame, or else the method hangs.
160 gfx::Point reset_mouse(GetWebContents()->GetContainerBounds().origin());
161 reset_mouse = gfx::Point(reset_mouse.x() + 5, reset_mouse.y() + 5);
162 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(reset_mouse));
165 void TearDownOnMainThread() override {
166 // Make sure to close any showing popups prior to tearing down the UI.
167 content::WebContents* web_contents = GetWebContents();
168 AutofillManager* autofill_manager =
169 ContentAutofillDriverFactory::FromWebContents(web_contents)
170 ->DriverForFrame(web_contents->GetMainFrame())
171 ->autofill_manager();
172 autofill_manager->client()->HideAutofillPopup();
175 content::WebContents* GetWebContents() {
176 return browser()->tab_strip_model()->GetActiveWebContents();
179 content::RenderViewHost* GetRenderViewHost() {
180 return GetWebContents()->GetRenderViewHost();
183 void CreateTestProfile() {
184 AutofillProfile profile;
185 test::SetProfileInfo(
186 &profile, "Milton", "C.", "Waddams",
187 "red.swingline@initech.com", "Initech", "4120 Freidrich Lane",
188 "Basement", "Austin", "Texas", "78744", "US", "5125551234");
190 AddTestProfile(browser(), profile);
193 // Populates a webpage form using autofill data and keypress events.
194 // This function focuses the specified input field in the form, and then
195 // sends keypress events to the tab to cause the form to be populated.
196 void PopulateForm(const std::string& field_id) {
197 std::string js("document.getElementById('" + field_id + "').focus();");
198 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js));
200 SendKeyToPageAndWait(ui::VKEY_DOWN);
201 SendKeyToPopupAndWait(ui::VKEY_DOWN);
202 SendKeyToPopupAndWait(ui::VKEY_RETURN);
205 void ExpectFieldValue(const std::string& field_name,
206 const std::string& expected_value) {
207 std::string value;
208 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
209 GetWebContents(),
210 "window.domAutomationController.send("
211 " document.getElementById('" + field_name + "').value);",
212 &value));
213 EXPECT_EQ(expected_value, value) << "for field " << field_name;
216 void GetFieldBackgroundColor(const std::string& field_name,
217 std::string* color) {
218 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
219 GetWebContents(),
220 "window.domAutomationController.send("
221 " document.defaultView.getComputedStyle(document.getElementById('" +
222 field_name + "')).backgroundColor);",
223 color));
226 void SimulateURLFetch(bool success) {
227 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
228 ASSERT_TRUE(fetcher);
229 net::Error error = success ? net::OK : net::ERR_FAILED;
231 std::string script = " var google = {};"
232 "google.translate = (function() {"
233 " return {"
234 " TranslateService: function() {"
235 " return {"
236 " isAvailable : function() {"
237 " return true;"
238 " },"
239 " restore : function() {"
240 " return;"
241 " },"
242 " getDetectedLanguage : function() {"
243 " return \"ja\";"
244 " },"
245 " translatePage : function(originalLang, targetLang,"
246 " onTranslateProgress) {"
247 " document.getElementsByTagName(\"body\")[0].innerHTML = '" +
248 std::string(kTestFormString) +
249 " ';"
250 " onTranslateProgress(100, true, false);"
251 " }"
252 " };"
253 " }"
254 " };"
255 "})();"
256 "cr.googleTranslate.onTranslateElementLoad();";
258 fetcher->set_url(fetcher->GetOriginalURL());
259 fetcher->set_status(net::URLRequestStatus::FromError(error));
260 fetcher->set_response_code(success ? 200 : 500);
261 fetcher->SetResponseString(script);
262 fetcher->delegate()->OnURLFetchComplete(fetcher);
265 void FocusFirstNameField() {
266 bool result = false;
267 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
268 GetRenderViewHost(),
269 "if (document.readyState === 'complete')"
270 " document.getElementById('firstname').focus();"
271 "else"
272 " domAutomationController.send(false);",
273 &result));
274 ASSERT_TRUE(result);
277 // Simulates a click on the middle of the DOM element with the given |id|.
278 void ClickElementWithId(const std::string& id) {
279 int x;
280 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
281 GetRenderViewHost(),
282 "var bounds = document.getElementById('" +
283 id +
284 "').getBoundingClientRect();"
285 "domAutomationController.send("
286 " Math.floor(bounds.left + bounds.width / 2));",
287 &x));
288 int y;
289 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
290 GetRenderViewHost(),
291 "var bounds = document.getElementById('" +
292 id +
293 "').getBoundingClientRect();"
294 "domAutomationController.send("
295 " Math.floor(bounds.top + bounds.height / 2));",
296 &y));
297 content::SimulateMouseClickAt(GetWebContents(),
299 blink::WebMouseEvent::ButtonLeft,
300 gfx::Point(x, y));
303 void ClickFirstNameField() {
304 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("firstname"));
307 // Make a pointless round trip to the renderer, giving the popup a chance to
308 // show if it's going to. If it does show, an assert in
309 // AutofillManagerTestDelegateImpl will trigger.
310 void MakeSurePopupDoesntAppear() {
311 int unused;
312 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
313 GetRenderViewHost(), "domAutomationController.send(42)", &unused));
316 void ExpectFilledTestForm() {
317 ExpectFieldValue("firstname", "Milton");
318 ExpectFieldValue("lastname", "Waddams");
319 ExpectFieldValue("address1", "4120 Freidrich Lane");
320 ExpectFieldValue("address2", "Basement");
321 ExpectFieldValue("city", "Austin");
322 ExpectFieldValue("state", "TX");
323 ExpectFieldValue("zip", "78744");
324 ExpectFieldValue("country", "US");
325 ExpectFieldValue("phone", "5125551234");
328 void SendKeyToPageAndWait(ui::KeyboardCode key) {
329 test_delegate_.Reset();
330 content::SimulateKeyPress(
331 GetWebContents(), key, false, false, false, false);
332 test_delegate_.Wait();
335 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) {
336 return true;
339 void SendKeyToPopupAndWait(ui::KeyboardCode key) {
340 // Route popup-targeted key presses via the render view host.
341 content::NativeWebKeyboardEvent event;
342 event.windowsKeyCode = key;
343 event.type = blink::WebKeyboardEvent::RawKeyDown;
344 test_delegate_.Reset();
345 // Install the key press event sink to ensure that any events that are not
346 // handled by the installed callbacks do not end up crashing the test.
347 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
348 GetRenderViewHost()->ForwardKeyboardEvent(event);
349 test_delegate_.Wait();
350 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
353 // Datalist does not support autofill preview. There is no need to start
354 // message loop for Datalist.
355 void SendKeyToDataListPopup(ui::KeyboardCode key) {
356 // Route popup-targeted key presses via the render view host.
357 content::NativeWebKeyboardEvent event;
358 event.windowsKeyCode = key;
359 event.type = blink::WebKeyboardEvent::RawKeyDown;
360 // Install the key press event sink to ensure that any events that are not
361 // handled by the installed callbacks do not end up crashing the test.
362 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_);
363 GetRenderViewHost()->ForwardKeyboardEvent(event);
364 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_);
367 void TryBasicFormFill() {
368 FocusFirstNameField();
370 // Start filling the first name field with "M" and wait for the popup to be
371 // shown.
372 SendKeyToPageAndWait(ui::VKEY_M);
374 // Press the down arrow to select the suggestion and preview the autofilled
375 // form.
376 SendKeyToPopupAndWait(ui::VKEY_DOWN);
378 // The previewed values should not be accessible to JavaScript.
379 ExpectFieldValue("firstname", "M");
380 ExpectFieldValue("lastname", std::string());
381 ExpectFieldValue("address1", std::string());
382 ExpectFieldValue("address2", std::string());
383 ExpectFieldValue("city", std::string());
384 ExpectFieldValue("state", std::string());
385 ExpectFieldValue("zip", std::string());
386 ExpectFieldValue("country", std::string());
387 ExpectFieldValue("phone", std::string());
388 // TODO(isherman): It would be nice to test that the previewed values are
389 // displayed: http://crbug.com/57220
391 // Press Enter to accept the autofill suggestions.
392 SendKeyToPopupAndWait(ui::VKEY_RETURN);
394 // The form should be filled.
395 ExpectFilledTestForm();
398 AutofillManagerTestDelegateImpl* test_delegate() { return &test_delegate_; }
400 private:
401 AutofillManagerTestDelegateImpl test_delegate_;
403 net::TestURLFetcherFactory url_fetcher_factory_;
405 // KeyPressEventCallback that serves as a sink to ensure that every key press
406 // event the tests create and have the WebContents forward is handled by some
407 // key press event callback. It is necessary to have this sinkbecause if no
408 // key press event callback handles the event (at least on Mac), a DCHECK
409 // ends up going off that the |event| doesn't have an |os_event| associated
410 // with it.
411 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_;
413 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest);
416 // Test that basic form fill is working.
417 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) {
418 CreateTestProfile();
420 // Load the test page.
421 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
422 GURL(std::string(kDataURIPrefix) + kTestFormString)));
424 // Invoke Autofill.
425 TryBasicFormFill();
428 // Test that form filling can be initiated by pressing the down arrow.
429 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) {
430 CreateTestProfile();
432 // Load the test page.
433 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
434 GURL(std::string(kDataURIPrefix) + kTestFormString)));
436 // Focus a fillable field.
437 FocusFirstNameField();
439 // Press the down arrow to initiate Autofill and wait for the popup to be
440 // shown.
441 SendKeyToPageAndWait(ui::VKEY_DOWN);
443 // Press the down arrow to select the suggestion and preview the autofilled
444 // form.
445 SendKeyToPopupAndWait(ui::VKEY_DOWN);
447 // Press Enter to accept the autofill suggestions.
448 SendKeyToPopupAndWait(ui::VKEY_RETURN);
450 // The form should be filled.
451 ExpectFilledTestForm();
454 // Flaky on the official cros-trunk. crbug.com/516052
455 #if defined(OFFICIAL_BUILD)
456 #define MAYBE_AutofillSelectViaTab DISABLED_AutofillSelectViaTab
457 #else
458 #define MAYBE_AutofillSelectViaTab AutofillSelectViaTab
459 #endif // defined(OFFICIAL_BUILD)
460 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_AutofillSelectViaTab) {
461 CreateTestProfile();
463 // Load the test page.
464 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
465 GURL(std::string(kDataURIPrefix) + kTestFormString)));
467 // Focus a fillable field.
468 FocusFirstNameField();
470 // Press the down arrow to initiate Autofill and wait for the popup to be
471 // shown.
472 SendKeyToPageAndWait(ui::VKEY_DOWN);
474 // Press the down arrow to select the suggestion and preview the autofilled
475 // form.
476 SendKeyToPopupAndWait(ui::VKEY_DOWN);
478 // Press tab to accept the autofill suggestions.
479 SendKeyToPopupAndWait(ui::VKEY_TAB);
481 // The form should be filled.
482 ExpectFilledTestForm();
485 // Flaky on the official cros-trunk. crbug.com/516052
486 #if defined(OFFICIAL_BUILD)
487 #define MAYBE_AutofillViaClick DISABLED_AutofillViaClick
488 #else
489 #define MAYBE_AutofillViaClick AutofillViaClick
490 #endif // defined(OFFICIAL_BUILD)
491 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_AutofillViaClick) {
492 CreateTestProfile();
494 // Load the test page.
495 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
496 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
497 // Focus a fillable field.
498 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
500 // Now click it.
501 test_delegate()->Reset();
502 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
503 test_delegate()->Wait();
505 // Press the down arrow to select the suggestion and preview the autofilled
506 // form.
507 SendKeyToPopupAndWait(ui::VKEY_DOWN);
509 // Press Enter to accept the autofill suggestions.
510 SendKeyToPopupAndWait(ui::VKEY_RETURN);
512 // The form should be filled.
513 ExpectFilledTestForm();
516 // Makes sure that the first click does *not* activate the popup.
517 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForFirstClick) {
518 CreateTestProfile();
520 // Load the test page.
521 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
522 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString)));
524 // Click the first name field while it's out of focus, then twiddle our thumbs
525 // a bit. If a popup were to show, it would hit the asserts in
526 // AutofillManagerTestDelegateImpl while we're wasting time.
527 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
528 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
530 // The second click should activate the popup since the first click focused
531 // the field.
532 test_delegate()->Reset();
533 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
534 test_delegate()->Wait();
537 // Makes sure that clicking outside the focused field doesn't activate
538 // the popup.
539 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForOutsideClick) {
540 CreateTestProfile();
542 // Load the test page.
543 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
544 browser(),
545 GURL(std::string(kDataURIPrefix) + kTestFormString +
546 "<button disabled id='disabled-button'>Cant click this</button>")));
548 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField());
550 // Clicking a disabled button will generate a mouse event but focus doesn't
551 // change. This tests that autofill can handle a mouse event outside a focused
552 // input *without* showing the popup.
553 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("disabled-button"));
554 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear());
556 test_delegate()->Reset();
557 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField());
558 test_delegate()->Wait();
561 // Test that a field is still autofillable after the previously autofilled
562 // value is deleted.
563 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) {
564 CreateTestProfile();
566 // Load the test page.
567 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
568 GURL(std::string(kDataURIPrefix) + kTestFormString)));
570 // Invoke and accept the Autofill popup and verify the form was filled.
571 FocusFirstNameField();
572 SendKeyToPageAndWait(ui::VKEY_M);
573 SendKeyToPopupAndWait(ui::VKEY_DOWN);
574 SendKeyToPopupAndWait(ui::VKEY_RETURN);
575 ExpectFilledTestForm();
577 // Delete the value of a filled field.
578 ASSERT_TRUE(content::ExecuteScript(
579 GetRenderViewHost(),
580 "document.getElementById('firstname').value = '';"));
581 ExpectFieldValue("firstname", "");
583 // Invoke and accept the Autofill popup and verify the field was filled.
584 SendKeyToPageAndWait(ui::VKEY_M);
585 SendKeyToPopupAndWait(ui::VKEY_DOWN);
586 SendKeyToPopupAndWait(ui::VKEY_RETURN);
587 ExpectFieldValue("firstname", "Milton");
590 // Test that an input field is not rendered with the yellow autofilled
591 // background color when choosing an option from the datalist suggestion list.
592 #if defined(OS_MACOSX)
593 // Flakily triggers and assert on Mac.
594 // http://crbug.com/419868
595 #define MAYBE_OnSelectOptionFromDatalist DISABLED_OnSelectOptionFromDatalist
596 #else
597 #define MAYBE_OnSelectOptionFromDatalist OnSelectOptionFromDatalist
598 #endif
599 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
600 MAYBE_OnSelectOptionFromDatalist) {
601 // Load the test page.
602 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
603 browser(),
604 GURL(std::string(kDataURIPrefix) +
605 "<form action=\"http://www.example.com/\" method=\"POST\">"
606 " <input list=\"dl\" type=\"search\" id=\"firstname\""
607 " onfocus=\"domAutomationController.send(true)\"><br>"
608 " <datalist id=\"dl\">"
609 " <option value=\"Adam\"></option>"
610 " <option value=\"Bob\"></option>"
611 " <option value=\"Carl\"></option>"
612 " </datalist>"
613 "</form>")));
614 std::string orginalcolor;
615 GetFieldBackgroundColor("firstname", &orginalcolor);
617 FocusFirstNameField();
618 SendKeyToPageAndWait(ui::VKEY_DOWN);
619 SendKeyToDataListPopup(ui::VKEY_DOWN);
620 SendKeyToDataListPopup(ui::VKEY_RETURN);
621 ExpectFieldValue("firstname", "Adam");
622 std::string color;
623 GetFieldBackgroundColor("firstname", &color);
624 EXPECT_EQ(color, orginalcolor);
627 // Test that a JavaScript oninput event is fired after auto-filling a form.
628 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) {
629 CreateTestProfile();
631 const char kOnInputScript[] =
632 "<script>"
633 "focused_fired = false;"
634 "unfocused_fired = false;"
635 "changed_select_fired = false;"
636 "unchanged_select_fired = false;"
637 "document.getElementById('firstname').oninput = function() {"
638 " focused_fired = true;"
639 "};"
640 "document.getElementById('lastname').oninput = function() {"
641 " unfocused_fired = true;"
642 "};"
643 "document.getElementById('state').oninput = function() {"
644 " changed_select_fired = true;"
645 "};"
646 "document.getElementById('country').oninput = function() {"
647 " unchanged_select_fired = true;"
648 "};"
649 "document.getElementById('country').value = 'US';"
650 "</script>";
652 // Load the test page.
653 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
654 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnInputScript)));
656 // Invoke Autofill.
657 FocusFirstNameField();
659 // Start filling the first name field with "M" and wait for the popup to be
660 // shown.
661 SendKeyToPageAndWait(ui::VKEY_M);
663 // Press the down arrow to select the suggestion and preview the autofilled
664 // form.
665 SendKeyToPopupAndWait(ui::VKEY_DOWN);
667 // Press Enter to accept the autofill suggestions.
668 SendKeyToPopupAndWait(ui::VKEY_RETURN);
670 // The form should be filled.
671 ExpectFilledTestForm();
673 bool focused_fired = false;
674 bool unfocused_fired = false;
675 bool changed_select_fired = false;
676 bool unchanged_select_fired = false;
677 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
678 GetRenderViewHost(),
679 "domAutomationController.send(focused_fired);",
680 &focused_fired));
681 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
682 GetRenderViewHost(),
683 "domAutomationController.send(unfocused_fired);",
684 &unfocused_fired));
685 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
686 GetRenderViewHost(),
687 "domAutomationController.send(changed_select_fired);",
688 &changed_select_fired));
689 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
690 GetRenderViewHost(),
691 "domAutomationController.send(unchanged_select_fired);",
692 &unchanged_select_fired));
693 EXPECT_TRUE(focused_fired);
694 EXPECT_TRUE(unfocused_fired);
695 EXPECT_TRUE(changed_select_fired);
696 EXPECT_FALSE(unchanged_select_fired);
699 // Test that a JavaScript onchange event is fired after auto-filling a form.
700 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) {
701 CreateTestProfile();
703 const char kOnChangeScript[] =
704 "<script>"
705 "focused_fired = false;"
706 "unfocused_fired = false;"
707 "changed_select_fired = false;"
708 "unchanged_select_fired = false;"
709 "document.getElementById('firstname').onchange = function() {"
710 " focused_fired = true;"
711 "};"
712 "document.getElementById('lastname').onchange = function() {"
713 " unfocused_fired = true;"
714 "};"
715 "document.getElementById('state').onchange = function() {"
716 " changed_select_fired = true;"
717 "};"
718 "document.getElementById('country').onchange = function() {"
719 " unchanged_select_fired = true;"
720 "};"
721 "document.getElementById('country').value = 'US';"
722 "</script>";
724 // Load the test page.
725 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
726 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnChangeScript)));
728 // Invoke Autofill.
729 FocusFirstNameField();
731 // Start filling the first name field with "M" and wait for the popup to be
732 // shown.
733 SendKeyToPageAndWait(ui::VKEY_M);
735 // Press the down arrow to select the suggestion and preview the autofilled
736 // form.
737 SendKeyToPopupAndWait(ui::VKEY_DOWN);
739 // Press Enter to accept the autofill suggestions.
740 SendKeyToPopupAndWait(ui::VKEY_RETURN);
742 // The form should be filled.
743 ExpectFilledTestForm();
745 bool focused_fired = false;
746 bool unfocused_fired = false;
747 bool changed_select_fired = false;
748 bool unchanged_select_fired = false;
749 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
750 GetRenderViewHost(),
751 "domAutomationController.send(focused_fired);",
752 &focused_fired));
753 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
754 GetRenderViewHost(),
755 "domAutomationController.send(unfocused_fired);",
756 &unfocused_fired));
757 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
758 GetRenderViewHost(),
759 "domAutomationController.send(changed_select_fired);",
760 &changed_select_fired));
761 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
762 GetRenderViewHost(),
763 "domAutomationController.send(unchanged_select_fired);",
764 &unchanged_select_fired));
765 EXPECT_TRUE(focused_fired);
766 EXPECT_TRUE(unfocused_fired);
767 EXPECT_TRUE(changed_select_fired);
768 EXPECT_FALSE(unchanged_select_fired);
771 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) {
772 CreateTestProfile();
774 const char kInputFiresBeforeChangeScript[] =
775 "<script>"
776 "inputElementEvents = [];"
777 "function recordInputElementEvent(e) {"
778 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';"
779 " inputElementEvents.push(e.type);"
781 "selectElementEvents = [];"
782 "function recordSelectElementEvent(e) {"
783 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';"
784 " selectElementEvents.push(e.type);"
786 "document.getElementById('lastname').oninput = recordInputElementEvent;"
787 "document.getElementById('lastname').onchange = recordInputElementEvent;"
788 "document.getElementById('country').oninput = recordSelectElementEvent;"
789 "document.getElementById('country').onchange = recordSelectElementEvent;"
790 "</script>";
792 // Load the test page.
793 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
794 GURL(std::string(kDataURIPrefix) + kTestFormString +
795 kInputFiresBeforeChangeScript)));
797 // Invoke and accept the Autofill popup and verify the form was filled.
798 FocusFirstNameField();
799 SendKeyToPageAndWait(ui::VKEY_M);
800 SendKeyToPopupAndWait(ui::VKEY_DOWN);
801 SendKeyToPopupAndWait(ui::VKEY_RETURN);
802 ExpectFilledTestForm();
804 int num_input_element_events = -1;
805 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
806 GetRenderViewHost(),
807 "domAutomationController.send(inputElementEvents.length);",
808 &num_input_element_events));
809 EXPECT_EQ(2, num_input_element_events);
811 std::vector<std::string> input_element_events;
812 input_element_events.resize(2);
814 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
815 GetRenderViewHost(),
816 "domAutomationController.send(inputElementEvents[0]);",
817 &input_element_events[0]));
818 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
819 GetRenderViewHost(),
820 "domAutomationController.send(inputElementEvents[1]);",
821 &input_element_events[1]));
823 EXPECT_EQ("input", input_element_events[0]);
824 EXPECT_EQ("change", input_element_events[1]);
826 int num_select_element_events = -1;
827 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
828 GetRenderViewHost(),
829 "domAutomationController.send(selectElementEvents.length);",
830 &num_select_element_events));
831 EXPECT_EQ(2, num_select_element_events);
833 std::vector<std::string> select_element_events;
834 select_element_events.resize(2);
836 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
837 GetRenderViewHost(),
838 "domAutomationController.send(selectElementEvents[0]);",
839 &select_element_events[0]));
840 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
841 GetRenderViewHost(),
842 "domAutomationController.send(selectElementEvents[1]);",
843 &select_element_events[1]));
845 EXPECT_EQ("input", select_element_events[0]);
846 EXPECT_EQ("change", select_element_events[1]);
849 // Test that we can autofill forms distinguished only by their |id| attribute.
850 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
851 AutofillFormsDistinguishedById) {
852 CreateTestProfile();
854 // Load the test page.
855 const std::string kURL =
856 std::string(kDataURIPrefix) + kTestFormString +
857 "<script>"
858 "var mainForm = document.forms[0];"
859 "mainForm.id = 'mainForm';"
860 "var newForm = document.createElement('form');"
861 "newForm.action = mainForm.action;"
862 "newForm.method = mainForm.method;"
863 "newForm.id = 'newForm';"
864 "mainForm.parentNode.insertBefore(newForm, mainForm);"
865 "</script>";
866 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL)));
868 // Invoke Autofill.
869 TryBasicFormFill();
872 // Test that we properly autofill forms with repeated fields.
873 // In the wild, the repeated fields are typically either email fields
874 // (duplicated for "confirmation"); or variants that are hot-swapped via
875 // JavaScript, with only one actually visible at any given time.
876 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) {
877 CreateTestProfile();
879 // Load the test page.
880 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
881 GURL(std::string(kDataURIPrefix) +
882 "<form action=\"http://www.example.com/\" method=\"POST\">"
883 "<label for=\"firstname\">First name:</label>"
884 " <input type=\"text\" id=\"firstname\""
885 " onfocus=\"domAutomationController.send(true)\"><br>"
886 "<label for=\"lastname\">Last name:</label>"
887 " <input type=\"text\" id=\"lastname\"><br>"
888 "<label for=\"address1\">Address line 1:</label>"
889 " <input type=\"text\" id=\"address1\"><br>"
890 "<label for=\"address2\">Address line 2:</label>"
891 " <input type=\"text\" id=\"address2\"><br>"
892 "<label for=\"city\">City:</label>"
893 " <input type=\"text\" id=\"city\"><br>"
894 "<label for=\"state\">State:</label>"
895 " <select id=\"state\">"
896 " <option value=\"\" selected=\"yes\">--</option>"
897 " <option value=\"CA\">California</option>"
898 " <option value=\"TX\">Texas</option>"
899 " </select><br>"
900 "<label for=\"state_freeform\" style=\"display:none\">State:</label>"
901 " <input type=\"text\" id=\"state_freeform\""
902 " style=\"display:none\"><br>"
903 "<label for=\"zip\">ZIP code:</label>"
904 " <input type=\"text\" id=\"zip\"><br>"
905 "<label for=\"country\">Country:</label>"
906 " <select id=\"country\">"
907 " <option value=\"\" selected=\"yes\">--</option>"
908 " <option value=\"CA\">Canada</option>"
909 " <option value=\"US\">United States</option>"
910 " </select><br>"
911 "<label for=\"phone\">Phone number:</label>"
912 " <input type=\"text\" id=\"phone\"><br>"
913 "</form>")));
915 // Invoke Autofill.
916 TryBasicFormFill();
917 ExpectFieldValue("state_freeform", std::string());
920 // Test that we properly autofill forms with non-autofillable fields.
921 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
922 AutofillFormWithNonAutofillableField) {
923 CreateTestProfile();
925 // Load the test page.
926 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
927 GURL(std::string(kDataURIPrefix) +
928 "<form action=\"http://www.example.com/\" method=\"POST\">"
929 "<label for=\"firstname\">First name:</label>"
930 " <input type=\"text\" id=\"firstname\""
931 " onfocus=\"domAutomationController.send(true)\"><br>"
932 "<label for=\"middlename\">Middle name:</label>"
933 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>"
934 "<label for=\"lastname\">Last name:</label>"
935 " <input type=\"text\" id=\"lastname\"><br>"
936 "<label for=\"address1\">Address line 1:</label>"
937 " <input type=\"text\" id=\"address1\"><br>"
938 "<label for=\"address2\">Address line 2:</label>"
939 " <input type=\"text\" id=\"address2\"><br>"
940 "<label for=\"city\">City:</label>"
941 " <input type=\"text\" id=\"city\"><br>"
942 "<label for=\"state\">State:</label>"
943 " <select id=\"state\">"
944 " <option value=\"\" selected=\"yes\">--</option>"
945 " <option value=\"CA\">California</option>"
946 " <option value=\"TX\">Texas</option>"
947 " </select><br>"
948 "<label for=\"zip\">ZIP code:</label>"
949 " <input type=\"text\" id=\"zip\"><br>"
950 "<label for=\"country\">Country:</label>"
951 " <select id=\"country\">"
952 " <option value=\"\" selected=\"yes\">--</option>"
953 " <option value=\"CA\">Canada</option>"
954 " <option value=\"US\">United States</option>"
955 " </select><br>"
956 "<label for=\"phone\">Phone number:</label>"
957 " <input type=\"text\" id=\"phone\"><br>"
958 "</form>")));
960 // Invoke Autofill.
961 TryBasicFormFill();
964 // Test that we can Autofill dynamically generated forms.
965 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) {
966 CreateTestProfile();
968 // Load the test page.
969 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
970 GURL(std::string(kDataURIPrefix) +
971 "<form id=\"form\" action=\"http://www.example.com/\""
972 " method=\"POST\"></form>"
973 "<script>"
974 "function AddElement(name, label) {"
975 " var form = document.getElementById('form');"
977 " var label_text = document.createTextNode(label);"
978 " var label_element = document.createElement('label');"
979 " label_element.setAttribute('for', name);"
980 " label_element.appendChild(label_text);"
981 " form.appendChild(label_element);"
983 " if (name === 'state' || name === 'country') {"
984 " var select_element = document.createElement('select');"
985 " select_element.setAttribute('id', name);"
986 " select_element.setAttribute('name', name);"
988 " /* Add an empty selected option. */"
989 " var default_option = new Option('--', '', true);"
990 " select_element.appendChild(default_option);"
992 " /* Add the other options. */"
993 " if (name == 'state') {"
994 " var option1 = new Option('California', 'CA');"
995 " select_element.appendChild(option1);"
996 " var option2 = new Option('Texas', 'TX');"
997 " select_element.appendChild(option2);"
998 " } else {"
999 " var option1 = new Option('Canada', 'CA');"
1000 " select_element.appendChild(option1);"
1001 " var option2 = new Option('United States', 'US');"
1002 " select_element.appendChild(option2);"
1003 " }"
1005 " form.appendChild(select_element);"
1006 " } else {"
1007 " var input_element = document.createElement('input');"
1008 " input_element.setAttribute('id', name);"
1009 " input_element.setAttribute('name', name);"
1011 " /* Add the onfocus listener to the 'firstname' field. */"
1012 " if (name === 'firstname') {"
1013 " input_element.onfocus = function() {"
1014 " domAutomationController.send(true);"
1015 " };"
1016 " }"
1018 " form.appendChild(input_element);"
1019 " }"
1021 " form.appendChild(document.createElement('br'));"
1022 "};"
1024 "function BuildForm() {"
1025 " var elements = ["
1026 " ['firstname', 'First name:'],"
1027 " ['lastname', 'Last name:'],"
1028 " ['address1', 'Address line 1:'],"
1029 " ['address2', 'Address line 2:'],"
1030 " ['city', 'City:'],"
1031 " ['state', 'State:'],"
1032 " ['zip', 'ZIP code:'],"
1033 " ['country', 'Country:'],"
1034 " ['phone', 'Phone number:'],"
1035 " ];"
1037 " for (var i = 0; i < elements.length; i++) {"
1038 " var name = elements[i][0];"
1039 " var label = elements[i][1];"
1040 " AddElement(name, label);"
1041 " }"
1042 "};"
1043 "</script>")));
1045 // Dynamically construct the form.
1046 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();"));
1048 // Invoke Autofill.
1049 TryBasicFormFill();
1052 // Test that form filling works after reloading the current page.
1053 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) {
1054 CreateTestProfile();
1056 // Load the test page.
1057 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1058 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1060 // Reload the page.
1061 content::WebContents* web_contents = GetWebContents();
1062 web_contents->GetController().Reload(false);
1063 content::WaitForLoadStop(web_contents);
1065 // Invoke Autofill.
1066 TryBasicFormFill();
1069 // Test fails on Linux ASAN, see http://crbug.com/532737
1070 #if defined(ADDRESS_SANITIZER)
1071 #define MAYBE_AutofillAfterTranslate DISABLED_AutofillAfterTranslate
1072 #else
1073 #define MAYBE_AutofillAfterTranslate AutofillAfterTranslate
1074 #endif // ADDRESS_SANITIZER
1075 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_AutofillAfterTranslate) {
1076 // TODO(groby): Remove once the bubble is enabled by default everywhere.
1077 // http://crbug.com/507442
1078 #if defined(OS_MACOSX)
1079 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1080 ::switches::kEnableTranslateNewUX);
1081 #endif
1082 ASSERT_TRUE(TranslateService::IsTranslateBubbleEnabled());
1084 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
1086 CreateTestProfile();
1088 GURL url(std::string(kDataURIPrefix) +
1089 "<form action=\"http://www.example.com/\" method=\"POST\">"
1090 "<label for=\"fn\">なまえ</label>"
1091 " <input type=\"text\" id=\"fn\""
1092 " onfocus=\"domAutomationController.send(true)\""
1093 "><br>"
1094 "<label for=\"ln\">みょうじ</label>"
1095 " <input type=\"text\" id=\"ln\"><br>"
1096 "<label for=\"a1\">Address line 1:</label>"
1097 " <input type=\"text\" id=\"a1\"><br>"
1098 "<label for=\"a2\">Address line 2:</label>"
1099 " <input type=\"text\" id=\"a2\"><br>"
1100 "<label for=\"ci\">City:</label>"
1101 " <input type=\"text\" id=\"ci\"><br>"
1102 "<label for=\"st\">State:</label>"
1103 " <select id=\"st\">"
1104 " <option value=\"\" selected=\"yes\">--</option>"
1105 " <option value=\"CA\">California</option>"
1106 " <option value=\"TX\">Texas</option>"
1107 " </select><br>"
1108 "<label for=\"z\">ZIP code:</label>"
1109 " <input type=\"text\" id=\"z\"><br>"
1110 "<label for=\"co\">Country:</label>"
1111 " <select id=\"co\">"
1112 " <option value=\"\" selected=\"yes\">--</option>"
1113 " <option value=\"CA\">Canada</option>"
1114 " <option value=\"US\">United States</option>"
1115 " </select><br>"
1116 "<label for=\"ph\">Phone number:</label>"
1117 " <input type=\"text\" id=\"ph\"><br>"
1118 "</form>"
1119 // Add additional Japanese characters to ensure the translate bar
1120 // will appear.
1121 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは"
1122 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利");
1124 // Set up an observer to be able to wait for the bubble to be shown.
1125 content::Source<content::WebContents> source(GetWebContents());
1126 content::WindowedNotificationObserver language_detected_signal(
1127 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, source);
1129 ASSERT_NO_FATAL_FAILURE(
1130 ui_test_utils::NavigateToURL(browser(), url));
1132 // Wait for the translate bubble to appear.
1133 language_detected_signal.Wait();
1135 // Verify current translate step.
1136 const TranslateBubbleModel* model =
1137 translate::test_utils::GetCurrentModel(browser());
1138 ASSERT_NE(nullptr, model);
1139 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1140 model->GetViewState());
1142 translate::test_utils::PressTranslate(browser());
1144 // Wait for translation.
1145 content::WindowedNotificationObserver translation_observer(
1146 chrome::NOTIFICATION_PAGE_TRANSLATED,
1147 content::NotificationService::AllSources());
1149 // Simulate the translate script being retrieved.
1150 // Pass fake google.translate lib as the translate script.
1151 SimulateURLFetch(true);
1153 // Simulate the render notifying the translation has been done.
1154 translation_observer.Wait();
1156 TryBasicFormFill();
1159 // Test phone fields parse correctly from a given profile.
1160 // The high level key presses execute the following: Select the first text
1161 // field, invoke the autofill popup list, select the first profile within the
1162 // list, and commit to the profile to populate the form.
1163 // Flakily times out on windows. http://crbug.com/390564
1164 // Flaky on the official cros-trunk crbug.com/516052
1165 #if defined(OS_WIN) || defined(OFFICIAL_BUILD)
1166 #define MAYBE_ComparePhoneNumbers DISABLED_ComparePhoneNumbers
1167 #else
1168 #define MAYBE_ComparePhoneNumbers ComparePhoneNumbers
1169 #endif // defined(OS_WIN) || defined(OFFICIAL_BUILD)
1170 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_ComparePhoneNumbers) {
1171 ASSERT_TRUE(test_server()->Start());
1173 AutofillProfile profile;
1174 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1175 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1176 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 H St."));
1177 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1178 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1179 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1180 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1-408-555-4567"));
1181 SetTestProfile(browser(), profile);
1183 GURL url = test_server()->GetURL("files/autofill/form_phones.html");
1184 ui_test_utils::NavigateToURL(browser(), url);
1185 PopulateForm("NAME_FIRST");
1187 ExpectFieldValue("NAME_FIRST", "Bob");
1188 ExpectFieldValue("NAME_LAST", "Smith");
1189 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St.");
1190 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose");
1191 ExpectFieldValue("ADDRESS_HOME_STATE", "CA");
1192 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110");
1193 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567");
1194 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408");
1195 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408");
1196 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567");
1197 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555");
1198 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555");
1199 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567");
1200 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567");
1201 ExpectFieldValue("PHONE_HOME_EXT-1", std::string());
1202 ExpectFieldValue("PHONE_HOME_EXT-2", std::string());
1203 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1");
1206 // Test that Autofill does not fill in read-only fields.
1207 // Flaky on the official cros-trunk. crbug.com/516052
1208 #if defined(OFFICIAL_BUILD)
1209 #define MAYBE_NoAutofillForReadOnlyFields DISABLED_NoAutofillForReadOnlyFields
1210 #else
1211 #define MAYBE_NoAutofillForReadOnlyFields NoAutofillForReadOnlyFields
1212 #endif // defined(OFFICIAL_BUILD)
1213 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1214 MAYBE_NoAutofillForReadOnlyFields) {
1215 ASSERT_TRUE(test_server()->Start());
1217 std::string addr_line1("1234 H St.");
1219 AutofillProfile profile;
1220 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1221 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1222 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("bsmith@gmail.com"));
1223 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
1224 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose"));
1225 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1226 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110"));
1227 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Company X"));
1228 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("408-871-4567"));
1229 SetTestProfile(browser(), profile);
1231 GURL url = test_server()->GetURL("files/autofill/read_only_field_test.html");
1232 ui_test_utils::NavigateToURL(browser(), url);
1233 PopulateForm("firstname");
1235 ExpectFieldValue("email", std::string());
1236 ExpectFieldValue("address", addr_line1);
1239 // Test form is fillable from a profile after form was reset.
1240 // Steps:
1241 // 1. Fill form using a saved profile.
1242 // 2. Reset the form.
1243 // 3. Fill form using a saved profile.
1244 // Flakily times out: http://crbug.com/270341
1245 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_FormFillableOnReset) {
1246 ASSERT_TRUE(test_server()->Start());
1248 CreateTestProfile();
1250 GURL url = test_server()->GetURL("files/autofill/autofill_test_form.html");
1251 ui_test_utils::NavigateToURL(browser(), url);
1252 PopulateForm("NAME_FIRST");
1254 ASSERT_TRUE(content::ExecuteScript(
1255 GetWebContents(), "document.getElementById('testform').reset()"));
1257 PopulateForm("NAME_FIRST");
1259 ExpectFieldValue("NAME_FIRST", "Milton");
1260 ExpectFieldValue("NAME_LAST", "Waddams");
1261 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline@initech.com");
1262 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane");
1263 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin");
1264 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas");
1265 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744");
1266 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States");
1267 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234");
1270 // Test Autofill distinguishes a middle initial in a name.
1271 // Flakily times out: http://crbug.com/270341
1272 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1273 DISABLED_DistinguishMiddleInitialWithinName) {
1274 ASSERT_TRUE(test_server()->Start());
1276 CreateTestProfile();
1278 GURL url = test_server()->GetURL(
1279 "files/autofill/autofill_middleinit_form.html");
1280 ui_test_utils::NavigateToURL(browser(), url);
1281 PopulateForm("NAME_FIRST");
1283 ExpectFieldValue("NAME_MIDDLE", "C");
1286 // Test forms with multiple email addresses are filled properly.
1287 // Entire form should be filled with one user gesture.
1288 // Flakily times out: http://crbug.com/270341
1289 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1290 DISABLED_MultipleEmailFilledByOneUserGesture) {
1291 ASSERT_TRUE(test_server()->Start());
1293 std::string email("bsmith@gmail.com");
1295 AutofillProfile profile;
1296 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob"));
1297 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1298 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email));
1299 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("4088714567"));
1300 SetTestProfile(browser(), profile);
1302 GURL url = test_server()->GetURL(
1303 "files/autofill/autofill_confirmemail_form.html");
1304 ui_test_utils::NavigateToURL(browser(), url);
1305 PopulateForm("NAME_FIRST");
1307 ExpectFieldValue("EMAIL_CONFIRM", email);
1308 // TODO(isherman): verify entire form.
1311 // Test latency time on form submit with lots of stored Autofill profiles.
1312 // This test verifies when a profile is selected from the Autofill dictionary
1313 // that consists of thousands of profiles, the form does not hang after being
1314 // submitted.
1315 // Flakily times out: http://crbug.com/281527
1316 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1317 DISABLED_FormFillLatencyAfterSubmit) {
1318 ASSERT_TRUE(test_server()->Start());
1320 std::vector<std::string> cities;
1321 cities.push_back("San Jose");
1322 cities.push_back("San Francisco");
1323 cities.push_back("Sacramento");
1324 cities.push_back("Los Angeles");
1326 std::vector<std::string> streets;
1327 streets.push_back("St");
1328 streets.push_back("Ave");
1329 streets.push_back("Ln");
1330 streets.push_back("Ct");
1332 const int kNumProfiles = 1500;
1333 base::Time start_time = base::Time::Now();
1334 std::vector<AutofillProfile> profiles;
1335 for (int i = 0; i < kNumProfiles; i++) {
1336 AutofillProfile profile;
1337 base::string16 name(base::IntToString16(i));
1338 base::string16 email(name + ASCIIToUTF16("@example.com"));
1339 base::string16 street = ASCIIToUTF16(
1340 base::IntToString(base::RandInt(0, 10000)) + " " +
1341 streets[base::RandInt(0, streets.size() - 1)]);
1342 base::string16 city =
1343 ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]);
1344 base::string16 zip(base::IntToString16(base::RandInt(0, 10000)));
1345 profile.SetRawInfo(NAME_FIRST, name);
1346 profile.SetRawInfo(EMAIL_ADDRESS, email);
1347 profile.SetRawInfo(ADDRESS_HOME_LINE1, street);
1348 profile.SetRawInfo(ADDRESS_HOME_CITY, city);
1349 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1350 profile.SetRawInfo(ADDRESS_HOME_ZIP, zip);
1351 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1352 profiles.push_back(profile);
1354 SetTestProfiles(browser(), &profiles);
1355 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this
1356 // can be removd.
1357 LOG(INFO) << "Created " << kNumProfiles << " profiles in " <<
1358 (base::Time::Now() - start_time).InSeconds() << " seconds.";
1360 GURL url = test_server()->GetURL(
1361 "files/autofill/latency_after_submit_test.html");
1362 ui_test_utils::NavigateToURL(browser(), url);
1363 PopulateForm("NAME_FIRST");
1365 content::WindowedNotificationObserver load_stop_observer(
1366 content::NOTIFICATION_LOAD_STOP,
1367 content::Source<content::NavigationController>(
1368 &GetWebContents()->GetController()));
1370 ASSERT_TRUE(content::ExecuteScript(
1371 GetRenderViewHost(),
1372 "document.getElementById('testform').submit();"));
1373 // This will ensure the test didn't hang.
1374 load_stop_observer.Wait();
1377 // Test that Chrome doesn't crash when autocomplete is disabled while the user
1378 // is interacting with the form. This is a regression test for
1379 // http://crbug.com/160476
1380 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
1381 DisableAutocompleteWhileFilling) {
1382 CreateTestProfile();
1384 // Load the test page.
1385 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
1386 GURL(std::string(kDataURIPrefix) + kTestFormString)));
1388 // Invoke Autofill: Start filling the first name field with "M" and wait for
1389 // the popup to be shown.
1390 FocusFirstNameField();
1391 SendKeyToPageAndWait(ui::VKEY_M);
1393 // Now that the popup with suggestions is showing, disable autocomplete for
1394 // the active field.
1395 ASSERT_TRUE(content::ExecuteScript(
1396 GetRenderViewHost(),
1397 "document.querySelector('input').autocomplete = 'off';"));
1399 // Press the down arrow to select the suggestion and attempt to preview the
1400 // autofilled form.
1401 SendKeyToPopupAndWait(ui::VKEY_DOWN);
1404 } // namespace autofill