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. 4 5 #include <string> 6 7 #include "base/basictypes.h" 8 #include "base/command_line.h" 9 #include "base/files/file_util.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/rand_util.h" 13 #include "base/strings/string16.h" 14 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_split.h" 16 #include "base/strings/utf_string_conversions.h" 17 #include "base/time/time.h" 18 #include "chrome/browser/autofill/personal_data_manager_factory.h" 19 #include "chrome/browser/chrome_notification_types.h" 20 #include "chrome/browser/infobars/infobar_service.h" 21 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/translate/chrome_translate_client.h" 23 #include "chrome/browser/translate/translate_service.h" 24 #include "chrome/browser/ui/browser.h" 25 #include "chrome/browser/ui/browser_window.h" 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" 27 #include "chrome/common/render_messages.h" 28 #include "chrome/test/base/in_process_browser_test.h" 29 #include "chrome/test/base/interactive_test_utils.h" 30 #include "chrome/test/base/test_switches.h" 31 #include "chrome/test/base/ui_test_utils.h" 32 #include "components/autofill/content/browser/content_autofill_driver.h" 33 #include "components/autofill/core/browser/autofill_manager.h" 34 #include "components/autofill/core/browser/autofill_manager_test_delegate.h" 35 #include "components/autofill/core/browser/autofill_profile.h" 36 #include "components/autofill/core/browser/autofill_test_utils.h" 37 #include "components/autofill/core/browser/personal_data_manager.h" 38 #include "components/autofill/core/browser/personal_data_manager_observer.h" 39 #include "components/autofill/core/browser/validation.h" 40 #include "components/infobars/core/confirm_infobar_delegate.h" 41 #include "components/infobars/core/infobar.h" 42 #include "components/infobars/core/infobar_manager.h" 43 #include "components/translate/core/browser/translate_infobar_delegate.h" 44 #include "content/public/browser/navigation_controller.h" 45 #include "content/public/browser/notification_observer.h" 46 #include "content/public/browser/notification_registrar.h" 47 #include "content/public/browser/notification_service.h" 48 #include "content/public/browser/render_view_host.h" 49 #include "content/public/browser/render_widget_host.h" 50 #include "content/public/browser/web_contents.h" 51 #include "content/public/test/browser_test_utils.h" 52 #include "content/public/test/test_renderer_host.h" 53 #include "content/public/test/test_utils.h" 54 #include "net/url_request/test_url_fetcher_factory.h" 55 #include "testing/gmock/include/gmock/gmock.h" 56 #include "testing/gtest/include/gtest/gtest.h" 57 #include "ui/events/keycodes/keyboard_codes.h" 58 59 using base::ASCIIToUTF16; 60 61 namespace autofill { 62 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>"; 94 95 96 // AutofillManagerTestDelegateImpl -------------------------------------------- 97 98 class AutofillManagerTestDelegateImpl 99 : public autofill::AutofillManagerTestDelegate { 100 public: 101 AutofillManagerTestDelegateImpl() {} 102 virtual ~AutofillManagerTestDelegateImpl() {} 103 104 // autofill::AutofillManagerTestDelegate: 105 virtual void DidPreviewFormData() OVERRIDE { 106 ASSERT_TRUE(loop_runner_->loop_running()); 107 loop_runner_->Quit(); 108 } 109 110 virtual void DidFillFormData() OVERRIDE { 111 ASSERT_TRUE(loop_runner_->loop_running()); 112 loop_runner_->Quit(); 113 } 114 115 virtual void DidShowSuggestions() OVERRIDE { 116 ASSERT_TRUE(loop_runner_->loop_running()); 117 loop_runner_->Quit(); 118 } 119 120 void Reset() { 121 loop_runner_ = new content::MessageLoopRunner(); 122 } 123 124 void Wait() { 125 loop_runner_->Run(); 126 } 127 128 private: 129 scoped_refptr<content::MessageLoopRunner> loop_runner_; 130 131 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl); 132 }; 133 134 135 // WindowedPersonalDataManagerObserver ---------------------------------------- 136 137 class WindowedPersonalDataManagerObserver 138 : public PersonalDataManagerObserver, 139 public infobars::InfoBarManager::Observer { 140 public: 141 explicit WindowedPersonalDataManagerObserver(Browser* browser) 142 : alerted_(false), 143 has_run_message_loop_(false), 144 browser_(browser), 145 infobar_service_(InfoBarService::FromWebContents( 146 browser_->tab_strip_model()->GetActiveWebContents())) { 147 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> 148 AddObserver(this); 149 infobar_service_->AddObserver(this); 150 } 151 152 virtual ~WindowedPersonalDataManagerObserver() { 153 while (infobar_service_->infobar_count() > 0) { 154 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); 155 } 156 infobar_service_->RemoveObserver(this); 157 } 158 159 // PersonalDataManagerObserver: 160 virtual void OnPersonalDataChanged() OVERRIDE { 161 if (has_run_message_loop_) { 162 base::MessageLoopForUI::current()->Quit(); 163 has_run_message_loop_ = false; 164 } 165 alerted_ = true; 166 } 167 168 virtual void OnInsufficientFormData() OVERRIDE { 169 OnPersonalDataChanged(); 170 } 171 172 173 void Wait() { 174 if (!alerted_) { 175 has_run_message_loop_ = true; 176 content::RunMessageLoop(); 177 } 178 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> 179 RemoveObserver(this); 180 } 181 182 private: 183 // infobars::InfoBarManager::Observer: 184 virtual void OnInfoBarAdded(infobars::InfoBar* infobar) OVERRIDE { 185 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()-> 186 Accept(); 187 } 188 189 bool alerted_; 190 bool has_run_message_loop_; 191 Browser* browser_; 192 InfoBarService* infobar_service_; 193 194 DISALLOW_COPY_AND_ASSIGN(WindowedPersonalDataManagerObserver); 195 }; 196 197 // AutofillInteractiveTest ---------------------------------------------------- 198 199 class AutofillInteractiveTest : public InProcessBrowserTest { 200 protected: 201 AutofillInteractiveTest() : 202 key_press_event_sink_( 203 base::Bind(&AutofillInteractiveTest::HandleKeyPressEvent, 204 base::Unretained(this))) {} 205 virtual ~AutofillInteractiveTest() {} 206 207 // InProcessBrowserTest: 208 virtual void SetUpOnMainThread() OVERRIDE { 209 // Don't want Keychain coming up on Mac. 210 test::DisableSystemServices(browser()->profile()->GetPrefs()); 211 212 // Inject the test delegate into the AutofillManager. 213 content::WebContents* web_contents = GetWebContents(); 214 ContentAutofillDriver* autofill_driver = 215 ContentAutofillDriver::FromWebContents(web_contents); 216 AutofillManager* autofill_manager = autofill_driver->autofill_manager(); 217 autofill_manager->SetTestDelegate(&test_delegate_); 218 219 // If the mouse happened to be over where the suggestions are shown, then 220 // the preview will show up and will fail the tests. We need to give it a 221 // point that's within the browser frame, or else the method hangs. 222 gfx::Point reset_mouse(GetWebContents()->GetContainerBounds().origin()); 223 reset_mouse = gfx::Point(reset_mouse.x() + 5, reset_mouse.y() + 5); 224 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(reset_mouse)); 225 } 226 227 virtual void TearDownOnMainThread() OVERRIDE { 228 // Make sure to close any showing popups prior to tearing down the UI. 229 content::WebContents* web_contents = GetWebContents(); 230 AutofillManager* autofill_manager = ContentAutofillDriver::FromWebContents( 231 web_contents)->autofill_manager(); 232 autofill_manager->client()->HideAutofillPopup(); 233 } 234 235 PersonalDataManager* GetPersonalDataManager() { 236 return PersonalDataManagerFactory::GetForProfile(browser()->profile()); 237 } 238 239 content::WebContents* GetWebContents() { 240 return browser()->tab_strip_model()->GetActiveWebContents(); 241 } 242 243 content::RenderViewHost* GetRenderViewHost() { 244 return GetWebContents()->GetRenderViewHost(); 245 } 246 247 void CreateTestProfile() { 248 AutofillProfile profile; 249 test::SetProfileInfo( 250 &profile, "Milton", "C.", "Waddams", 251 "red.swingline (at) initech.com", "Initech", "4120 Freidrich Lane", 252 "Basement", "Austin", "Texas", "78744", "US", "5125551234"); 253 254 WindowedPersonalDataManagerObserver observer(browser()); 255 GetPersonalDataManager()->AddProfile(profile); 256 257 // AddProfile is asynchronous. Wait for it to finish before continuing the 258 // tests. 259 observer.Wait(); 260 } 261 262 void SetProfiles(std::vector<AutofillProfile>* profiles) { 263 WindowedPersonalDataManagerObserver observer(browser()); 264 GetPersonalDataManager()->SetProfiles(profiles); 265 observer.Wait(); 266 } 267 268 void SetProfile(const AutofillProfile& profile) { 269 std::vector<AutofillProfile> profiles; 270 profiles.push_back(profile); 271 SetProfiles(&profiles); 272 } 273 274 // Populates a webpage form using autofill data and keypress events. 275 // This function focuses the specified input field in the form, and then 276 // sends keypress events to the tab to cause the form to be populated. 277 void PopulateForm(const std::string& field_id) { 278 std::string js("document.getElementById('" + field_id + "').focus();"); 279 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), js)); 280 281 SendKeyToPageAndWait(ui::VKEY_DOWN); 282 SendKeyToPopupAndWait(ui::VKEY_DOWN); 283 SendKeyToPopupAndWait(ui::VKEY_RETURN); 284 } 285 286 void ExpectFieldValue(const std::string& field_name, 287 const std::string& expected_value) { 288 std::string value; 289 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 290 GetWebContents(), 291 "window.domAutomationController.send(" 292 " document.getElementById('" + field_name + "').value);", 293 &value)); 294 EXPECT_EQ(expected_value, value); 295 } 296 297 void GetFieldBackgroundColor(const std::string& field_name, 298 std::string* color) { 299 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 300 GetWebContents(), 301 "window.domAutomationController.send(" 302 " document.defaultView.getComputedStyle(document.getElementById('" + 303 field_name + "')).backgroundColor);", 304 color)); 305 } 306 307 void SimulateURLFetch(bool success) { 308 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 309 ASSERT_TRUE(fetcher); 310 net::URLRequestStatus status; 311 status.set_status(success ? net::URLRequestStatus::SUCCESS : 312 net::URLRequestStatus::FAILED); 313 314 std::string script = " var google = {};" 315 "google.translate = (function() {" 316 " return {" 317 " TranslateService: function() {" 318 " return {" 319 " isAvailable : function() {" 320 " return true;" 321 " }," 322 " restore : function() {" 323 " return;" 324 " }," 325 " getDetectedLanguage : function() {" 326 " return \"ja\";" 327 " }," 328 " translatePage : function(originalLang, targetLang," 329 " onTranslateProgress) {" 330 " document.getElementsByTagName(\"body\")[0].innerHTML = '" + 331 std::string(kTestFormString) + 332 " ';" 333 " onTranslateProgress(100, true, false);" 334 " }" 335 " };" 336 " }" 337 " };" 338 "})();" 339 "cr.googleTranslate.onTranslateElementLoad();"; 340 341 fetcher->set_url(fetcher->GetOriginalURL()); 342 fetcher->set_status(status); 343 fetcher->set_response_code(success ? 200 : 500); 344 fetcher->SetResponseString(script); 345 fetcher->delegate()->OnURLFetchComplete(fetcher); 346 } 347 348 void FocusFirstNameField() { 349 bool result = false; 350 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 351 GetRenderViewHost(), 352 "if (document.readyState === 'complete')" 353 " document.getElementById('firstname').focus();" 354 "else" 355 " domAutomationController.send(false);", 356 &result)); 357 ASSERT_TRUE(result); 358 } 359 360 // Simulates a click on the middle of the DOM element with the given |id|. 361 void ClickElementWithId(const std::string& id) { 362 int x; 363 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 364 GetRenderViewHost(), 365 "var bounds = document.getElementById('" + 366 id + 367 "').getBoundingClientRect();" 368 "domAutomationController.send(" 369 " Math.floor(bounds.left + bounds.width / 2));", 370 &x)); 371 int y; 372 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 373 GetRenderViewHost(), 374 "var bounds = document.getElementById('" + 375 id + 376 "').getBoundingClientRect();" 377 "domAutomationController.send(" 378 " Math.floor(bounds.top + bounds.height / 2));", 379 &y)); 380 content::SimulateMouseClickAt(GetWebContents(), 381 0, 382 blink::WebMouseEvent::ButtonLeft, 383 gfx::Point(x, y)); 384 } 385 386 void ClickFirstNameField() { 387 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("firstname")); 388 } 389 390 // Make a pointless round trip to the renderer, giving the popup a chance to 391 // show if it's going to. If it does show, an assert in 392 // AutofillManagerTestDelegateImpl will trigger. 393 void MakeSurePopupDoesntAppear() { 394 int unused; 395 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 396 GetRenderViewHost(), "domAutomationController.send(42)", &unused)); 397 } 398 399 void ExpectFilledTestForm() { 400 ExpectFieldValue("firstname", "Milton"); 401 ExpectFieldValue("lastname", "Waddams"); 402 ExpectFieldValue("address1", "4120 Freidrich Lane"); 403 ExpectFieldValue("address2", "Basement"); 404 ExpectFieldValue("city", "Austin"); 405 ExpectFieldValue("state", "TX"); 406 ExpectFieldValue("zip", "78744"); 407 ExpectFieldValue("country", "US"); 408 ExpectFieldValue("phone", "5125551234"); 409 } 410 411 void SendKeyToPageAndWait(ui::KeyboardCode key) { 412 test_delegate_.Reset(); 413 content::SimulateKeyPress( 414 GetWebContents(), key, false, false, false, false); 415 test_delegate_.Wait(); 416 } 417 418 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) { 419 return true; 420 } 421 422 void SendKeyToPopupAndWait(ui::KeyboardCode key) { 423 // Route popup-targeted key presses via the render view host. 424 content::NativeWebKeyboardEvent event; 425 event.windowsKeyCode = key; 426 event.type = blink::WebKeyboardEvent::RawKeyDown; 427 test_delegate_.Reset(); 428 // Install the key press event sink to ensure that any events that are not 429 // handled by the installed callbacks do not end up crashing the test. 430 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_); 431 GetRenderViewHost()->ForwardKeyboardEvent(event); 432 test_delegate_.Wait(); 433 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_); 434 } 435 436 // Datalist does not support autofill preview. There is no need to start 437 // message loop for Datalist. 438 void SendKeyToDataListPopup(ui::KeyboardCode key) { 439 // Route popup-targeted key presses via the render view host. 440 content::NativeWebKeyboardEvent event; 441 event.windowsKeyCode = key; 442 event.type = blink::WebKeyboardEvent::RawKeyDown; 443 // Install the key press event sink to ensure that any events that are not 444 // handled by the installed callbacks do not end up crashing the test. 445 GetRenderViewHost()->AddKeyPressEventCallback(key_press_event_sink_); 446 GetRenderViewHost()->ForwardKeyboardEvent(event); 447 GetRenderViewHost()->RemoveKeyPressEventCallback(key_press_event_sink_); 448 } 449 450 void TryBasicFormFill() { 451 FocusFirstNameField(); 452 453 // Start filling the first name field with "M" and wait for the popup to be 454 // shown. 455 SendKeyToPageAndWait(ui::VKEY_M); 456 457 // Press the down arrow to select the suggestion and preview the autofilled 458 // form. 459 SendKeyToPopupAndWait(ui::VKEY_DOWN); 460 461 // The previewed values should not be accessible to JavaScript. 462 ExpectFieldValue("firstname", "M"); 463 ExpectFieldValue("lastname", std::string()); 464 ExpectFieldValue("address1", std::string()); 465 ExpectFieldValue("address2", std::string()); 466 ExpectFieldValue("city", std::string()); 467 ExpectFieldValue("state", std::string()); 468 ExpectFieldValue("zip", std::string()); 469 ExpectFieldValue("country", std::string()); 470 ExpectFieldValue("phone", std::string()); 471 // TODO(isherman): It would be nice to test that the previewed values are 472 // displayed: http://crbug.com/57220 473 474 // Press Enter to accept the autofill suggestions. 475 SendKeyToPopupAndWait(ui::VKEY_RETURN); 476 477 // The form should be filled. 478 ExpectFilledTestForm(); 479 } 480 481 AutofillManagerTestDelegateImpl* test_delegate() { return &test_delegate_; } 482 483 private: 484 AutofillManagerTestDelegateImpl test_delegate_; 485 486 net::TestURLFetcherFactory url_fetcher_factory_; 487 488 // KeyPressEventCallback that serves as a sink to ensure that every key press 489 // event the tests create and have the WebContents forward is handled by some 490 // key press event callback. It is necessary to have this sinkbecause if no 491 // key press event callback handles the event (at least on Mac), a DCHECK 492 // ends up going off that the |event| doesn't have an |os_event| associated 493 // with it. 494 content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_; 495 496 DISALLOW_COPY_AND_ASSIGN(AutofillInteractiveTest); 497 }; 498 499 // Test that basic form fill is working. 500 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) { 501 CreateTestProfile(); 502 503 // Load the test page. 504 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 505 GURL(std::string(kDataURIPrefix) + kTestFormString))); 506 507 // Invoke Autofill. 508 TryBasicFormFill(); 509 } 510 511 // Test that form filling can be initiated by pressing the down arrow. 512 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) { 513 CreateTestProfile(); 514 515 // Load the test page. 516 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 517 GURL(std::string(kDataURIPrefix) + kTestFormString))); 518 519 // Focus a fillable field. 520 FocusFirstNameField(); 521 522 // Press the down arrow to initiate Autofill and wait for the popup to be 523 // shown. 524 SendKeyToPageAndWait(ui::VKEY_DOWN); 525 526 // Press the down arrow to select the suggestion and preview the autofilled 527 // form. 528 SendKeyToPopupAndWait(ui::VKEY_DOWN); 529 530 // Press Enter to accept the autofill suggestions. 531 SendKeyToPopupAndWait(ui::VKEY_RETURN); 532 533 // The form should be filled. 534 ExpectFilledTestForm(); 535 } 536 537 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillSelectViaTab) { 538 CreateTestProfile(); 539 540 // Load the test page. 541 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 542 GURL(std::string(kDataURIPrefix) + kTestFormString))); 543 544 // Focus a fillable field. 545 FocusFirstNameField(); 546 547 // Press the down arrow to initiate Autofill and wait for the popup to be 548 // shown. 549 SendKeyToPageAndWait(ui::VKEY_DOWN); 550 551 // Press the down arrow to select the suggestion and preview the autofilled 552 // form. 553 SendKeyToPopupAndWait(ui::VKEY_DOWN); 554 555 // Press tab to accept the autofill suggestions. 556 SendKeyToPopupAndWait(ui::VKEY_TAB); 557 558 // The form should be filled. 559 ExpectFilledTestForm(); 560 } 561 562 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaClick) { 563 CreateTestProfile(); 564 565 // Load the test page. 566 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( 567 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString))); 568 // Focus a fillable field. 569 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField()); 570 571 // Now click it. 572 test_delegate()->Reset(); 573 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField()); 574 test_delegate()->Wait(); 575 576 // Press the down arrow to select the suggestion and preview the autofilled 577 // form. 578 SendKeyToPopupAndWait(ui::VKEY_DOWN); 579 580 // Press Enter to accept the autofill suggestions. 581 SendKeyToPopupAndWait(ui::VKEY_RETURN); 582 583 // The form should be filled. 584 ExpectFilledTestForm(); 585 } 586 587 // Makes sure that the first click does *not* activate the popup. 588 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForFirstClick) { 589 CreateTestProfile(); 590 591 // Load the test page. 592 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( 593 browser(), GURL(std::string(kDataURIPrefix) + kTestFormString))); 594 595 // Click the first name field while it's out of focus, then twiddle our thumbs 596 // a bit. If a popup were to show, it would hit the asserts in 597 // AutofillManagerTestDelegateImpl while we're wasting time. 598 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField()); 599 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear()); 600 601 // The second click should activate the popup since the first click focused 602 // the field. 603 test_delegate()->Reset(); 604 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField()); 605 test_delegate()->Wait(); 606 } 607 608 // Makes sure that clicking outside the focused field doesn't activate 609 // the popup. 610 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForOutsideClick) { 611 CreateTestProfile(); 612 613 // Load the test page. 614 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( 615 browser(), 616 GURL(std::string(kDataURIPrefix) + kTestFormString + 617 "<button disabled id='disabled-button'>Cant click this</button>"))); 618 619 ASSERT_NO_FATAL_FAILURE(FocusFirstNameField()); 620 621 // Clicking a disabled button will generate a mouse event but focus doesn't 622 // change. This tests that autofill can handle a mouse event outside a focused 623 // input *without* showing the popup. 624 ASSERT_NO_FATAL_FAILURE(ClickElementWithId("disabled-button")); 625 ASSERT_NO_FATAL_FAILURE(MakeSurePopupDoesntAppear()); 626 627 test_delegate()->Reset(); 628 ASSERT_NO_FATAL_FAILURE(ClickFirstNameField()); 629 test_delegate()->Wait(); 630 } 631 632 // Test that a field is still autofillable after the previously autofilled 633 // value is deleted. 634 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) { 635 CreateTestProfile(); 636 637 // Load the test page. 638 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 639 GURL(std::string(kDataURIPrefix) + kTestFormString))); 640 641 // Invoke and accept the Autofill popup and verify the form was filled. 642 FocusFirstNameField(); 643 SendKeyToPageAndWait(ui::VKEY_M); 644 SendKeyToPopupAndWait(ui::VKEY_DOWN); 645 SendKeyToPopupAndWait(ui::VKEY_RETURN); 646 ExpectFilledTestForm(); 647 648 // Delete the value of a filled field. 649 ASSERT_TRUE(content::ExecuteScript( 650 GetRenderViewHost(), 651 "document.getElementById('firstname').value = '';")); 652 ExpectFieldValue("firstname", ""); 653 654 // Invoke and accept the Autofill popup and verify the field was filled. 655 SendKeyToPageAndWait(ui::VKEY_M); 656 SendKeyToPopupAndWait(ui::VKEY_DOWN); 657 SendKeyToPopupAndWait(ui::VKEY_RETURN); 658 ExpectFieldValue("firstname", "Milton"); 659 } 660 661 // Test that an input field is not rendered with the yellow autofilled 662 // background color when choosing an option from the datalist suggestion list. 663 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnSelectOptionFromDatalist) { 664 // Load the test page. 665 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( 666 browser(), 667 GURL(std::string(kDataURIPrefix) + 668 "<form action=\"http://www.example.com/\" method=\"POST\">" 669 " <input list=\"dl\" type=\"search\" id=\"firstname\"" 670 " onfocus=\"domAutomationController.send(true)\"><br>" 671 " <datalist id=\"dl\">" 672 " <option value=\"Adam\"></option>" 673 " <option value=\"Bob\"></option>" 674 " <option value=\"Carl\"></option>" 675 " </datalist>" 676 "</form>"))); 677 std::string orginalcolor; 678 GetFieldBackgroundColor("firstname", &orginalcolor); 679 680 FocusFirstNameField(); 681 SendKeyToPageAndWait(ui::VKEY_DOWN); 682 SendKeyToDataListPopup(ui::VKEY_DOWN); 683 SendKeyToDataListPopup(ui::VKEY_RETURN); 684 ExpectFieldValue("firstname", "Adam"); 685 std::string color; 686 GetFieldBackgroundColor("firstname", &color); 687 EXPECT_EQ(color, orginalcolor); 688 } 689 690 // Test that a JavaScript oninput event is fired after auto-filling a form. 691 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) { 692 CreateTestProfile(); 693 694 const char kOnInputScript[] = 695 "<script>" 696 "focused_fired = false;" 697 "unfocused_fired = false;" 698 "changed_select_fired = false;" 699 "unchanged_select_fired = false;" 700 "document.getElementById('firstname').oninput = function() {" 701 " focused_fired = true;" 702 "};" 703 "document.getElementById('lastname').oninput = function() {" 704 " unfocused_fired = true;" 705 "};" 706 "document.getElementById('state').oninput = function() {" 707 " changed_select_fired = true;" 708 "};" 709 "document.getElementById('country').oninput = function() {" 710 " unchanged_select_fired = true;" 711 "};" 712 "document.getElementById('country').value = 'US';" 713 "</script>"; 714 715 // Load the test page. 716 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 717 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnInputScript))); 718 719 // Invoke Autofill. 720 FocusFirstNameField(); 721 722 // Start filling the first name field with "M" and wait for the popup to be 723 // shown. 724 SendKeyToPageAndWait(ui::VKEY_M); 725 726 // Press the down arrow to select the suggestion and preview the autofilled 727 // form. 728 SendKeyToPopupAndWait(ui::VKEY_DOWN); 729 730 // Press Enter to accept the autofill suggestions. 731 SendKeyToPopupAndWait(ui::VKEY_RETURN); 732 733 // The form should be filled. 734 ExpectFilledTestForm(); 735 736 bool focused_fired = false; 737 bool unfocused_fired = false; 738 bool changed_select_fired = false; 739 bool unchanged_select_fired = false; 740 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 741 GetRenderViewHost(), 742 "domAutomationController.send(focused_fired);", 743 &focused_fired)); 744 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 745 GetRenderViewHost(), 746 "domAutomationController.send(unfocused_fired);", 747 &unfocused_fired)); 748 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 749 GetRenderViewHost(), 750 "domAutomationController.send(changed_select_fired);", 751 &changed_select_fired)); 752 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 753 GetRenderViewHost(), 754 "domAutomationController.send(unchanged_select_fired);", 755 &unchanged_select_fired)); 756 EXPECT_TRUE(focused_fired); 757 EXPECT_TRUE(unfocused_fired); 758 EXPECT_TRUE(changed_select_fired); 759 EXPECT_FALSE(unchanged_select_fired); 760 } 761 762 // Test that a JavaScript onchange event is fired after auto-filling a form. 763 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) { 764 CreateTestProfile(); 765 766 const char kOnChangeScript[] = 767 "<script>" 768 "focused_fired = false;" 769 "unfocused_fired = false;" 770 "changed_select_fired = false;" 771 "unchanged_select_fired = false;" 772 "document.getElementById('firstname').onchange = function() {" 773 " focused_fired = true;" 774 "};" 775 "document.getElementById('lastname').onchange = function() {" 776 " unfocused_fired = true;" 777 "};" 778 "document.getElementById('state').onchange = function() {" 779 " changed_select_fired = true;" 780 "};" 781 "document.getElementById('country').onchange = function() {" 782 " unchanged_select_fired = true;" 783 "};" 784 "document.getElementById('country').value = 'US';" 785 "</script>"; 786 787 // Load the test page. 788 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 789 GURL(std::string(kDataURIPrefix) + kTestFormString + kOnChangeScript))); 790 791 // Invoke Autofill. 792 FocusFirstNameField(); 793 794 // Start filling the first name field with "M" and wait for the popup to be 795 // shown. 796 SendKeyToPageAndWait(ui::VKEY_M); 797 798 // Press the down arrow to select the suggestion and preview the autofilled 799 // form. 800 SendKeyToPopupAndWait(ui::VKEY_DOWN); 801 802 // Press Enter to accept the autofill suggestions. 803 SendKeyToPopupAndWait(ui::VKEY_RETURN); 804 805 // The form should be filled. 806 ExpectFilledTestForm(); 807 808 bool focused_fired = false; 809 bool unfocused_fired = false; 810 bool changed_select_fired = false; 811 bool unchanged_select_fired = false; 812 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 813 GetRenderViewHost(), 814 "domAutomationController.send(focused_fired);", 815 &focused_fired)); 816 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 817 GetRenderViewHost(), 818 "domAutomationController.send(unfocused_fired);", 819 &unfocused_fired)); 820 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 821 GetRenderViewHost(), 822 "domAutomationController.send(changed_select_fired);", 823 &changed_select_fired)); 824 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 825 GetRenderViewHost(), 826 "domAutomationController.send(unchanged_select_fired);", 827 &unchanged_select_fired)); 828 EXPECT_TRUE(focused_fired); 829 EXPECT_TRUE(unfocused_fired); 830 EXPECT_TRUE(changed_select_fired); 831 EXPECT_FALSE(unchanged_select_fired); 832 } 833 834 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) { 835 CreateTestProfile(); 836 837 const char kInputFiresBeforeChangeScript[] = 838 "<script>" 839 "inputElementEvents = [];" 840 "function recordInputElementEvent(e) {" 841 " if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';" 842 " inputElementEvents.push(e.type);" 843 "}" 844 "selectElementEvents = [];" 845 "function recordSelectElementEvent(e) {" 846 " if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';" 847 " selectElementEvents.push(e.type);" 848 "}" 849 "document.getElementById('lastname').oninput = recordInputElementEvent;" 850 "document.getElementById('lastname').onchange = recordInputElementEvent;" 851 "document.getElementById('country').oninput = recordSelectElementEvent;" 852 "document.getElementById('country').onchange = recordSelectElementEvent;" 853 "</script>"; 854 855 // Load the test page. 856 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 857 GURL(std::string(kDataURIPrefix) + kTestFormString + 858 kInputFiresBeforeChangeScript))); 859 860 // Invoke and accept the Autofill popup and verify the form was filled. 861 FocusFirstNameField(); 862 SendKeyToPageAndWait(ui::VKEY_M); 863 SendKeyToPopupAndWait(ui::VKEY_DOWN); 864 SendKeyToPopupAndWait(ui::VKEY_RETURN); 865 ExpectFilledTestForm(); 866 867 int num_input_element_events = -1; 868 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 869 GetRenderViewHost(), 870 "domAutomationController.send(inputElementEvents.length);", 871 &num_input_element_events)); 872 EXPECT_EQ(2, num_input_element_events); 873 874 std::vector<std::string> input_element_events; 875 input_element_events.resize(2); 876 877 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 878 GetRenderViewHost(), 879 "domAutomationController.send(inputElementEvents[0]);", 880 &input_element_events[0])); 881 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 882 GetRenderViewHost(), 883 "domAutomationController.send(inputElementEvents[1]);", 884 &input_element_events[1])); 885 886 EXPECT_EQ("input", input_element_events[0]); 887 EXPECT_EQ("change", input_element_events[1]); 888 889 int num_select_element_events = -1; 890 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 891 GetRenderViewHost(), 892 "domAutomationController.send(selectElementEvents.length);", 893 &num_select_element_events)); 894 EXPECT_EQ(2, num_select_element_events); 895 896 std::vector<std::string> select_element_events; 897 select_element_events.resize(2); 898 899 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 900 GetRenderViewHost(), 901 "domAutomationController.send(selectElementEvents[0]);", 902 &select_element_events[0])); 903 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 904 GetRenderViewHost(), 905 "domAutomationController.send(selectElementEvents[1]);", 906 &select_element_events[1])); 907 908 EXPECT_EQ("input", select_element_events[0]); 909 EXPECT_EQ("change", select_element_events[1]); 910 } 911 912 // Test that we can autofill forms distinguished only by their |id| attribute. 913 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, 914 AutofillFormsDistinguishedById) { 915 CreateTestProfile(); 916 917 // Load the test page. 918 const std::string kURL = 919 std::string(kDataURIPrefix) + kTestFormString + 920 "<script>" 921 "var mainForm = document.forms[0];" 922 "mainForm.id = 'mainForm';" 923 "var newForm = document.createElement('form');" 924 "newForm.action = mainForm.action;" 925 "newForm.method = mainForm.method;" 926 "newForm.id = 'newForm';" 927 "mainForm.parentNode.insertBefore(newForm, mainForm);" 928 "</script>"; 929 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), GURL(kURL))); 930 931 // Invoke Autofill. 932 TryBasicFormFill(); 933 } 934 935 // Test that we properly autofill forms with repeated fields. 936 // In the wild, the repeated fields are typically either email fields 937 // (duplicated for "confirmation"); or variants that are hot-swapped via 938 // JavaScript, with only one actually visible at any given time. 939 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) { 940 CreateTestProfile(); 941 942 // Load the test page. 943 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 944 GURL(std::string(kDataURIPrefix) + 945 "<form action=\"http://www.example.com/\" method=\"POST\">" 946 "<label for=\"firstname\">First name:</label>" 947 " <input type=\"text\" id=\"firstname\"" 948 " onfocus=\"domAutomationController.send(true)\"><br>" 949 "<label for=\"lastname\">Last name:</label>" 950 " <input type=\"text\" id=\"lastname\"><br>" 951 "<label for=\"address1\">Address line 1:</label>" 952 " <input type=\"text\" id=\"address1\"><br>" 953 "<label for=\"address2\">Address line 2:</label>" 954 " <input type=\"text\" id=\"address2\"><br>" 955 "<label for=\"city\">City:</label>" 956 " <input type=\"text\" id=\"city\"><br>" 957 "<label for=\"state\">State:</label>" 958 " <select id=\"state\">" 959 " <option value=\"\" selected=\"yes\">--</option>" 960 " <option value=\"CA\">California</option>" 961 " <option value=\"TX\">Texas</option>" 962 " </select><br>" 963 "<label for=\"state_freeform\" style=\"display:none\">State:</label>" 964 " <input type=\"text\" id=\"state_freeform\"" 965 " style=\"display:none\"><br>" 966 "<label for=\"zip\">ZIP code:</label>" 967 " <input type=\"text\" id=\"zip\"><br>" 968 "<label for=\"country\">Country:</label>" 969 " <select id=\"country\">" 970 " <option value=\"\" selected=\"yes\">--</option>" 971 " <option value=\"CA\">Canada</option>" 972 " <option value=\"US\">United States</option>" 973 " </select><br>" 974 "<label for=\"phone\">Phone number:</label>" 975 " <input type=\"text\" id=\"phone\"><br>" 976 "</form>"))); 977 978 // Invoke Autofill. 979 TryBasicFormFill(); 980 ExpectFieldValue("state_freeform", std::string()); 981 } 982 983 // Test that we properly autofill forms with non-autofillable fields. 984 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, 985 AutofillFormWithNonAutofillableField) { 986 CreateTestProfile(); 987 988 // Load the test page. 989 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 990 GURL(std::string(kDataURIPrefix) + 991 "<form action=\"http://www.example.com/\" method=\"POST\">" 992 "<label for=\"firstname\">First name:</label>" 993 " <input type=\"text\" id=\"firstname\"" 994 " onfocus=\"domAutomationController.send(true)\"><br>" 995 "<label for=\"middlename\">Middle name:</label>" 996 " <input type=\"text\" id=\"middlename\" autocomplete=\"off\" /><br>" 997 "<label for=\"lastname\">Last name:</label>" 998 " <input type=\"text\" id=\"lastname\"><br>" 999 "<label for=\"address1\">Address line 1:</label>" 1000 " <input type=\"text\" id=\"address1\"><br>" 1001 "<label for=\"address2\">Address line 2:</label>" 1002 " <input type=\"text\" id=\"address2\"><br>" 1003 "<label for=\"city\">City:</label>" 1004 " <input type=\"text\" id=\"city\"><br>" 1005 "<label for=\"state\">State:</label>" 1006 " <select id=\"state\">" 1007 " <option value=\"\" selected=\"yes\">--</option>" 1008 " <option value=\"CA\">California</option>" 1009 " <option value=\"TX\">Texas</option>" 1010 " </select><br>" 1011 "<label for=\"zip\">ZIP code:</label>" 1012 " <input type=\"text\" id=\"zip\"><br>" 1013 "<label for=\"country\">Country:</label>" 1014 " <select id=\"country\">" 1015 " <option value=\"\" selected=\"yes\">--</option>" 1016 " <option value=\"CA\">Canada</option>" 1017 " <option value=\"US\">United States</option>" 1018 " </select><br>" 1019 "<label for=\"phone\">Phone number:</label>" 1020 " <input type=\"text\" id=\"phone\"><br>" 1021 "</form>"))); 1022 1023 // Invoke Autofill. 1024 TryBasicFormFill(); 1025 } 1026 1027 // Test that we can Autofill dynamically generated forms. 1028 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) { 1029 CreateTestProfile(); 1030 1031 // Load the test page. 1032 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 1033 GURL(std::string(kDataURIPrefix) + 1034 "<form id=\"form\" action=\"http://www.example.com/\"" 1035 " method=\"POST\"></form>" 1036 "<script>" 1037 "function AddElement(name, label) {" 1038 " var form = document.getElementById('form');" 1039 "" 1040 " var label_text = document.createTextNode(label);" 1041 " var label_element = document.createElement('label');" 1042 " label_element.setAttribute('for', name);" 1043 " label_element.appendChild(label_text);" 1044 " form.appendChild(label_element);" 1045 "" 1046 " if (name === 'state' || name === 'country') {" 1047 " var select_element = document.createElement('select');" 1048 " select_element.setAttribute('id', name);" 1049 " select_element.setAttribute('name', name);" 1050 "" 1051 " /* Add an empty selected option. */" 1052 " var default_option = new Option('--', '', true);" 1053 " select_element.appendChild(default_option);" 1054 "" 1055 " /* Add the other options. */" 1056 " if (name == 'state') {" 1057 " var option1 = new Option('California', 'CA');" 1058 " select_element.appendChild(option1);" 1059 " var option2 = new Option('Texas', 'TX');" 1060 " select_element.appendChild(option2);" 1061 " } else {" 1062 " var option1 = new Option('Canada', 'CA');" 1063 " select_element.appendChild(option1);" 1064 " var option2 = new Option('United States', 'US');" 1065 " select_element.appendChild(option2);" 1066 " }" 1067 "" 1068 " form.appendChild(select_element);" 1069 " } else {" 1070 " var input_element = document.createElement('input');" 1071 " input_element.setAttribute('id', name);" 1072 " input_element.setAttribute('name', name);" 1073 "" 1074 " /* Add the onfocus listener to the 'firstname' field. */" 1075 " if (name === 'firstname') {" 1076 " input_element.onfocus = function() {" 1077 " domAutomationController.send(true);" 1078 " };" 1079 " }" 1080 "" 1081 " form.appendChild(input_element);" 1082 " }" 1083 "" 1084 " form.appendChild(document.createElement('br'));" 1085 "};" 1086 "" 1087 "function BuildForm() {" 1088 " var elements = [" 1089 " ['firstname', 'First name:']," 1090 " ['lastname', 'Last name:']," 1091 " ['address1', 'Address line 1:']," 1092 " ['address2', 'Address line 2:']," 1093 " ['city', 'City:']," 1094 " ['state', 'State:']," 1095 " ['zip', 'ZIP code:']," 1096 " ['country', 'Country:']," 1097 " ['phone', 'Phone number:']," 1098 " ];" 1099 "" 1100 " for (var i = 0; i < elements.length; i++) {" 1101 " var name = elements[i][0];" 1102 " var label = elements[i][1];" 1103 " AddElement(name, label);" 1104 " }" 1105 "};" 1106 "</script>"))); 1107 1108 // Dynamically construct the form. 1109 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(), "BuildForm();")); 1110 1111 // Invoke Autofill. 1112 TryBasicFormFill(); 1113 } 1114 1115 // Test that form filling works after reloading the current page. 1116 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) { 1117 CreateTestProfile(); 1118 1119 // Load the test page. 1120 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 1121 GURL(std::string(kDataURIPrefix) + kTestFormString))); 1122 1123 // Reload the page. 1124 content::WebContents* web_contents = GetWebContents(); 1125 web_contents->GetController().Reload(false); 1126 content::WaitForLoadStop(web_contents); 1127 1128 // Invoke Autofill. 1129 TryBasicFormFill(); 1130 } 1131 1132 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterTranslate) { 1133 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235 1134 if (TranslateService::IsTranslateBubbleEnabled()) 1135 return; 1136 1137 CreateTestProfile(); 1138 1139 GURL url(std::string(kDataURIPrefix) + 1140 "<form action=\"http://www.example.com/\" method=\"POST\">" 1141 "<label for=\"fn\"></label>" 1142 " <input type=\"text\" id=\"fn\"" 1143 " onfocus=\"domAutomationController.send(true)\"" 1144 "><br>" 1145 "<label for=\"ln\"></label>" 1146 " <input type=\"text\" id=\"ln\"><br>" 1147 "<label for=\"a1\">Address line 1:</label>" 1148 " <input type=\"text\" id=\"a1\"><br>" 1149 "<label for=\"a2\">Address line 2:</label>" 1150 " <input type=\"text\" id=\"a2\"><br>" 1151 "<label for=\"ci\">City:</label>" 1152 " <input type=\"text\" id=\"ci\"><br>" 1153 "<label for=\"st\">State:</label>" 1154 " <select id=\"st\">" 1155 " <option value=\"\" selected=\"yes\">--</option>" 1156 " <option value=\"CA\">California</option>" 1157 " <option value=\"TX\">Texas</option>" 1158 " </select><br>" 1159 "<label for=\"z\">ZIP code:</label>" 1160 " <input type=\"text\" id=\"z\"><br>" 1161 "<label for=\"co\">Country:</label>" 1162 " <select id=\"co\">" 1163 " <option value=\"\" selected=\"yes\">--</option>" 1164 " <option value=\"CA\">Canada</option>" 1165 " <option value=\"US\">United States</option>" 1166 " </select><br>" 1167 "<label for=\"ph\">Phone number:</label>" 1168 " <input type=\"text\" id=\"ph\"><br>" 1169 "</form>" 1170 // Add additional Japanese characters to ensure the translate bar 1171 // will appear. 1172 "" 1173 ""); 1174 1175 content::WindowedNotificationObserver infobar_observer( 1176 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, 1177 content::NotificationService::AllSources()); 1178 ASSERT_NO_FATAL_FAILURE( 1179 ui_test_utils::NavigateToURL(browser(), url)); 1180 1181 // Wait for the translation bar to appear and get it. 1182 infobar_observer.Wait(); 1183 InfoBarService* infobar_service = 1184 InfoBarService::FromWebContents(GetWebContents()); 1185 translate::TranslateInfoBarDelegate* delegate = 1186 infobar_service->infobar_at(0)->delegate()->AsTranslateInfoBarDelegate(); 1187 ASSERT_TRUE(delegate); 1188 EXPECT_EQ(translate::TRANSLATE_STEP_BEFORE_TRANSLATE, 1189 delegate->translate_step()); 1190 1191 // Simulate translation button press. 1192 delegate->Translate(); 1193 1194 content::WindowedNotificationObserver translation_observer( 1195 chrome::NOTIFICATION_PAGE_TRANSLATED, 1196 content::NotificationService::AllSources()); 1197 1198 // Simulate the translate script being retrieved. 1199 // Pass fake google.translate lib as the translate script. 1200 SimulateURLFetch(true); 1201 1202 // Simulate the render notifying the translation has been done. 1203 translation_observer.Wait(); 1204 1205 TryBasicFormFill(); 1206 } 1207 1208 // Test phone fields parse correctly from a given profile. 1209 // The high level key presses execute the following: Select the first text 1210 // field, invoke the autofill popup list, select the first profile within the 1211 // list, and commit to the profile to populate the form. 1212 // Flakily times out on windows. http://crbug.com/390564 1213 #if defined(OS_WIN) 1214 #define MAYBE_ComparePhoneNumbers DISABLED_ComparePhoneNumbers 1215 #else 1216 #define MAYBE_ComparePhoneNumbers ComparePhoneNumbers 1217 #endif 1218 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, MAYBE_ComparePhoneNumbers) { 1219 ASSERT_TRUE(test_server()->Start()); 1220 1221 AutofillProfile profile; 1222 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob")); 1223 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1224 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 H St.")); 1225 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose")); 1226 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1227 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110")); 1228 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1-408-555-4567")); 1229 SetProfile(profile); 1230 1231 GURL url = test_server()->GetURL("files/autofill/form_phones.html"); 1232 ui_test_utils::NavigateToURL(browser(), url); 1233 PopulateForm("NAME_FIRST"); 1234 1235 ExpectFieldValue("NAME_FIRST", "Bob"); 1236 ExpectFieldValue("NAME_LAST", "Smith"); 1237 ExpectFieldValue("ADDRESS_HOME_LINE1", "1234 H St."); 1238 ExpectFieldValue("ADDRESS_HOME_CITY", "San Jose"); 1239 ExpectFieldValue("ADDRESS_HOME_STATE", "CA"); 1240 ExpectFieldValue("ADDRESS_HOME_ZIP", "95110"); 1241 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "14085554567"); 1242 ExpectFieldValue("PHONE_HOME_CITY_CODE-1", "408"); 1243 ExpectFieldValue("PHONE_HOME_CITY_CODE-2", "408"); 1244 ExpectFieldValue("PHONE_HOME_NUMBER", "5554567"); 1245 ExpectFieldValue("PHONE_HOME_NUMBER_3-1", "555"); 1246 ExpectFieldValue("PHONE_HOME_NUMBER_3-2", "555"); 1247 ExpectFieldValue("PHONE_HOME_NUMBER_4-1", "4567"); 1248 ExpectFieldValue("PHONE_HOME_NUMBER_4-2", "4567"); 1249 ExpectFieldValue("PHONE_HOME_EXT-1", std::string()); 1250 ExpectFieldValue("PHONE_HOME_EXT-2", std::string()); 1251 ExpectFieldValue("PHONE_HOME_COUNTRY_CODE-1", "1"); 1252 } 1253 1254 // Test that Autofill does not fill in read-only fields. 1255 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, NoAutofillForReadOnlyFields) { 1256 ASSERT_TRUE(test_server()->Start()); 1257 1258 std::string addr_line1("1234 H St."); 1259 1260 AutofillProfile profile; 1261 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob")); 1262 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1263 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("bsmith (at) gmail.com")); 1264 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1)); 1265 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("San Jose")); 1266 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1267 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95110")); 1268 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Company X")); 1269 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("408-871-4567")); 1270 SetProfile(profile); 1271 1272 GURL url = test_server()->GetURL("files/autofill/read_only_field_test.html"); 1273 ui_test_utils::NavigateToURL(browser(), url); 1274 PopulateForm("firstname"); 1275 1276 ExpectFieldValue("email", std::string()); 1277 ExpectFieldValue("address", addr_line1); 1278 } 1279 1280 // Test form is fillable from a profile after form was reset. 1281 // Steps: 1282 // 1. Fill form using a saved profile. 1283 // 2. Reset the form. 1284 // 3. Fill form using a saved profile. 1285 // Flakily times out: http://crbug.com/270341 1286 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DISABLED_FormFillableOnReset) { 1287 ASSERT_TRUE(test_server()->Start()); 1288 1289 CreateTestProfile(); 1290 1291 GURL url = test_server()->GetURL("files/autofill/autofill_test_form.html"); 1292 ui_test_utils::NavigateToURL(browser(), url); 1293 PopulateForm("NAME_FIRST"); 1294 1295 ASSERT_TRUE(content::ExecuteScript( 1296 GetWebContents(), "document.getElementById('testform').reset()")); 1297 1298 PopulateForm("NAME_FIRST"); 1299 1300 ExpectFieldValue("NAME_FIRST", "Milton"); 1301 ExpectFieldValue("NAME_LAST", "Waddams"); 1302 ExpectFieldValue("EMAIL_ADDRESS", "red.swingline (at) initech.com"); 1303 ExpectFieldValue("ADDRESS_HOME_LINE1", "4120 Freidrich Lane"); 1304 ExpectFieldValue("ADDRESS_HOME_CITY", "Austin"); 1305 ExpectFieldValue("ADDRESS_HOME_STATE", "Texas"); 1306 ExpectFieldValue("ADDRESS_HOME_ZIP", "78744"); 1307 ExpectFieldValue("ADDRESS_HOME_COUNTRY", "United States"); 1308 ExpectFieldValue("PHONE_HOME_WHOLE_NUMBER", "5125551234"); 1309 } 1310 1311 // Test Autofill distinguishes a middle initial in a name. 1312 // Flakily times out: http://crbug.com/270341 1313 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, 1314 DISABLED_DistinguishMiddleInitialWithinName) { 1315 ASSERT_TRUE(test_server()->Start()); 1316 1317 CreateTestProfile(); 1318 1319 GURL url = test_server()->GetURL( 1320 "files/autofill/autofill_middleinit_form.html"); 1321 ui_test_utils::NavigateToURL(browser(), url); 1322 PopulateForm("NAME_FIRST"); 1323 1324 ExpectFieldValue("NAME_MIDDLE", "C"); 1325 } 1326 1327 // Test forms with multiple email addresses are filled properly. 1328 // Entire form should be filled with one user gesture. 1329 // Flakily times out: http://crbug.com/270341 1330 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, 1331 DISABLED_MultipleEmailFilledByOneUserGesture) { 1332 ASSERT_TRUE(test_server()->Start()); 1333 1334 std::string email("bsmith (at) gmail.com"); 1335 1336 AutofillProfile profile; 1337 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bob")); 1338 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1339 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email)); 1340 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("4088714567")); 1341 SetProfile(profile); 1342 1343 GURL url = test_server()->GetURL( 1344 "files/autofill/autofill_confirmemail_form.html"); 1345 ui_test_utils::NavigateToURL(browser(), url); 1346 PopulateForm("NAME_FIRST"); 1347 1348 ExpectFieldValue("EMAIL_CONFIRM", email); 1349 // TODO(isherman): verify entire form. 1350 } 1351 1352 // http://crbug.com/281527 1353 #if defined(OS_MACOSX) 1354 #define MAYBE_FormFillLatencyAfterSubmit FormFillLatencyAfterSubmit 1355 #else 1356 #define MAYBE_FormFillLatencyAfterSubmit DISABLED_FormFillLatencyAfterSubmit 1357 #endif 1358 // Test latency time on form submit with lots of stored Autofill profiles. 1359 // This test verifies when a profile is selected from the Autofill dictionary 1360 // that consists of thousands of profiles, the form does not hang after being 1361 // submitted. 1362 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, 1363 MAYBE_FormFillLatencyAfterSubmit) { 1364 ASSERT_TRUE(test_server()->Start()); 1365 1366 std::vector<std::string> cities; 1367 cities.push_back("San Jose"); 1368 cities.push_back("San Francisco"); 1369 cities.push_back("Sacramento"); 1370 cities.push_back("Los Angeles"); 1371 1372 std::vector<std::string> streets; 1373 streets.push_back("St"); 1374 streets.push_back("Ave"); 1375 streets.push_back("Ln"); 1376 streets.push_back("Ct"); 1377 1378 const int kNumProfiles = 1500; 1379 base::Time start_time = base::Time::Now(); 1380 std::vector<AutofillProfile> profiles; 1381 for (int i = 0; i < kNumProfiles; i++) { 1382 AutofillProfile profile; 1383 base::string16 name(base::IntToString16(i)); 1384 base::string16 email(name + ASCIIToUTF16("@example.com")); 1385 base::string16 street = ASCIIToUTF16( 1386 base::IntToString(base::RandInt(0, 10000)) + " " + 1387 streets[base::RandInt(0, streets.size() - 1)]); 1388 base::string16 city = 1389 ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]); 1390 base::string16 zip(base::IntToString16(base::RandInt(0, 10000))); 1391 profile.SetRawInfo(NAME_FIRST, name); 1392 profile.SetRawInfo(EMAIL_ADDRESS, email); 1393 profile.SetRawInfo(ADDRESS_HOME_LINE1, street); 1394 profile.SetRawInfo(ADDRESS_HOME_CITY, city); 1395 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1396 profile.SetRawInfo(ADDRESS_HOME_ZIP, zip); 1397 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 1398 profiles.push_back(profile); 1399 } 1400 SetProfiles(&profiles); 1401 // TODO(isherman): once we're sure this test doesn't timeout on any bots, this 1402 // can be removd. 1403 LOG(INFO) << "Created " << kNumProfiles << " profiles in " << 1404 (base::Time::Now() - start_time).InSeconds() << " seconds."; 1405 1406 GURL url = test_server()->GetURL( 1407 "files/autofill/latency_after_submit_test.html"); 1408 ui_test_utils::NavigateToURL(browser(), url); 1409 PopulateForm("NAME_FIRST"); 1410 1411 content::WindowedNotificationObserver load_stop_observer( 1412 content::NOTIFICATION_LOAD_STOP, 1413 content::Source<content::NavigationController>( 1414 &GetWebContents()->GetController())); 1415 1416 ASSERT_TRUE(content::ExecuteScript( 1417 GetRenderViewHost(), 1418 "document.getElementById('testform').submit();")); 1419 // This will ensure the test didn't hang. 1420 load_stop_observer.Wait(); 1421 } 1422 1423 // Test that Chrome doesn't crash when autocomplete is disabled while the user 1424 // is interacting with the form. This is a regression test for 1425 // http://crbug.com/160476 1426 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, 1427 DisableAutocompleteWhileFilling) { 1428 CreateTestProfile(); 1429 1430 // Load the test page. 1431 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), 1432 GURL(std::string(kDataURIPrefix) + kTestFormString))); 1433 1434 // Invoke Autofill: Start filling the first name field with "M" and wait for 1435 // the popup to be shown. 1436 FocusFirstNameField(); 1437 SendKeyToPageAndWait(ui::VKEY_M); 1438 1439 // Now that the popup with suggestions is showing, disable autocomplete for 1440 // the active field. 1441 ASSERT_TRUE(content::ExecuteScript( 1442 GetRenderViewHost(), 1443 "document.querySelector('input').autocomplete = 'off';")); 1444 1445 // Press the down arrow to select the suggestion and attempt to preview the 1446 // autofilled form. 1447 SendKeyToPopupAndWait(ui::VKEY_DOWN); 1448 } 1449 1450 } // namespace autofill 1451