| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 9 #include "chrome/browser/automation/ui_controls.h" | 10 #include "chrome/browser/automation/ui_controls.h" |
| 10 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
| 11 #include "chrome/browser/browser_window.h" | 12 #include "chrome/browser/browser_window.h" |
| 12 #include "chrome/browser/renderer_host/render_view_host.h" | 13 #include "chrome/browser/renderer_host/render_view_host.h" |
| 13 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 14 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 14 #include "chrome/browser/tab_contents/interstitial_page.h" | 15 #include "chrome/browser/tab_contents/interstitial_page.h" |
| 15 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 #include "chrome/browser/tab_contents/tab_contents_view.h" | 17 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 unfocused_browser = browser2; | 395 unfocused_browser = browser2; |
| 395 #endif | 396 #endif |
| 396 | 397 |
| 397 GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); | 398 GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); |
| 398 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); | 399 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
| 399 | 400 |
| 400 // Activate the first browser. | 401 // Activate the first browser. |
| 401 focused_browser->window()->Activate(); | 402 focused_browser->window()->Activate(); |
| 402 | 403 |
| 403 // Wait for the focus to be stolen by the other browser. | 404 // Wait for the focus to be stolen by the other browser. |
| 404 PlatformThread::Sleep(2000); | 405 MessageLoop::current()->PostDelayedTask( |
| 406 FROM_HERE, new MessageLoop::QuitTask(), 2000); |
| 407 ui_test_utils::RunMessageLoop(); |
| 405 | 408 |
| 406 // Make sure the first browser is still active. | 409 // Make sure the first browser is still active. |
| 407 EXPECT_TRUE(focused_browser->window()->IsActive()); | 410 EXPECT_TRUE(focused_browser->window()->IsActive()); |
| 408 | 411 |
| 409 // Close the 2nd browser to avoid a DCHECK(). | 412 // Close the 2nd browser to avoid a DCHECK(). |
| 410 browser2->window()->Close(); | 413 browser2->window()->Close(); |
| 411 } | 414 } |
| 412 | 415 |
| 413 // Page cannot steal focus when focus is on location bar. | 416 // Page cannot steal focus when focus is on location bar. |
| 414 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { | 417 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 445 "", // Initially no element in the page should be focused | 448 "", // Initially no element in the page should be focused |
| 446 // (the location bar is focused). | 449 // (the location bar is focused). |
| 447 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", | 450 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 448 "gmapLink" | 451 "gmapLink" |
| 449 }; | 452 }; |
| 450 | 453 |
| 451 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 454 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 452 | 455 |
| 453 // Test forward focus traversal. | 456 // Test forward focus traversal. |
| 454 for (int i = 0; i < 3; ++i) { | 457 for (int i = 0; i < 3; ++i) { |
| 458 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
| 455 // Location bar should be focused. | 459 // Location bar should be focused. |
| 456 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 460 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 457 | 461 |
| 458 // Now let's press tab to move the focus. | 462 // Now let's press tab to move the focus. |
| 459 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { | 463 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
| 464 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j)); |
| 460 // Let's make sure the focus is on the expected element in the page. | 465 // Let's make sure the focus is on the expected element in the page. |
| 461 std::string actual; | 466 std::string actual; |
| 462 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 467 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 463 browser()->GetSelectedTabContents()->render_view_host(), | 468 browser()->GetSelectedTabContents()->render_view_host(), |
| 464 L"", | 469 L"", |
| 465 L"window.domAutomationController.send(getFocusedElement());", | 470 L"window.domAutomationController.send(getFocusedElement());", |
| 466 &actual)); | 471 &actual)); |
| 467 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 472 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 468 | 473 |
| 469 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 474 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 470 false, false, false)); | 475 false, false, false)); |
| 471 | 476 |
| 472 if (j < arraysize(kExpElementIDs) - 1) { | 477 if (j < arraysize(kExpElementIDs) - 1) { |
| 473 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> | 478 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> |
| 474 render_view_host()); | 479 render_view_host()); |
| 475 } else { | 480 } else { |
| 476 // On the last tab key press, the focus returns to the browser. | 481 // On the last tab key press, the focus returns to the browser. |
| 477 ui_test_utils::WaitForFocusInBrowser(browser()); | 482 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 478 } | 483 } |
| 479 } | 484 } |
| 480 | 485 |
| 481 // At this point the renderer has sent us a message asking to advance the | 486 // At this point the renderer has sent us a message asking to advance the |
| 482 // focus (as the end of the focus loop was reached in the renderer). | 487 // focus (as the end of the focus loop was reached in the renderer). |
| 483 // We need to run the message loop to process it. | 488 // We need to run the message loop to process it. |
| 484 MessageLoop::current()->RunAllPending(); | 489 ui_test_utils::RunAllPendingInMessageLoop(); |
| 485 } | 490 } |
| 486 | 491 |
| 487 // Now let's try reverse focus traversal. | 492 // Now let's try reverse focus traversal. |
| 488 for (int i = 0; i < 3; ++i) { | 493 for (int i = 0; i < 3; ++i) { |
| 494 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
| 489 // Location bar should be focused. | 495 // Location bar should be focused. |
| 490 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 496 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 491 | 497 |
| 492 // Now let's press shift-tab to move the focus in reverse. | 498 // Now let's press shift-tab to move the focus in reverse. |
| 493 for (size_t j = 0; j < 7; ++j) { | 499 for (size_t j = 0; j < 7; ++j) { |
| 500 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); |
| 494 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 501 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 495 false, true, false)); | 502 false, true, false)); |
| 496 | 503 |
| 497 if (j < arraysize(kExpElementIDs) - 1) { | 504 if (j < arraysize(kExpElementIDs) - 1) { |
| 498 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> | 505 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> |
| 499 render_view_host()); | 506 render_view_host()); |
| 500 } else { | 507 } else { |
| 501 // On the last tab key press, the focus returns to the browser. | 508 // On the last tab key press, the focus returns to the browser. |
| 502 ui_test_utils::WaitForFocusInBrowser(browser()); | 509 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 503 } | 510 } |
| 504 | 511 |
| 505 // Let's make sure the focus is on the expected element in the page. | 512 // Let's make sure the focus is on the expected element in the page. |
| 506 std::string actual; | 513 std::string actual; |
| 507 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 514 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 508 browser()->GetSelectedTabContents()->render_view_host(), | 515 browser()->GetSelectedTabContents()->render_view_host(), |
| 509 L"", | 516 L"", |
| 510 L"window.domAutomationController.send(getFocusedElement());", | 517 L"window.domAutomationController.send(getFocusedElement());", |
| 511 &actual)); | 518 &actual)); |
| 512 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 519 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 513 } | 520 } |
| 514 | 521 |
| 515 // At this point the renderer has sent us a message asking to advance the | 522 // At this point the renderer has sent us a message asking to advance the |
| 516 // focus (as the end of the focus loop was reached in the renderer). | 523 // focus (as the end of the focus loop was reached in the renderer). |
| 517 // We need to run the message loop to process it. | 524 // We need to run the message loop to process it. |
| 518 MessageLoop::current()->RunAllPending(); | 525 ui_test_utils::RunAllPendingInMessageLoop(); |
| 519 } | 526 } |
| 520 } | 527 } |
| 521 | 528 |
| 522 // Focus traversal while an interstitial is showing. | 529 // Focus traversal while an interstitial is showing. |
| 523 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { | 530 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
| 524 HTTPTestServer* server = StartHTTPServer(); | 531 HTTPTestServer* server = StartHTTPServer(); |
| 525 | 532 |
| 526 // First we navigate to our test page. | 533 // First we navigate to our test page. |
| 527 GURL url = server->TestServerPageW(kSimplePage); | 534 GURL url = server->TestServerPageW(kSimplePage); |
| 528 ui_test_utils::NavigateToURL(browser(), url); | 535 ui_test_utils::NavigateToURL(browser(), url); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 interstitial_page->WaitForFocusChange(); | 577 interstitial_page->WaitForFocusChange(); |
| 571 } else { | 578 } else { |
| 572 // On the last tab key press, the focus returns to the browser. | 579 // On the last tab key press, the focus returns to the browser. |
| 573 ui_test_utils::WaitForFocusInBrowser(browser()); | 580 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 574 } | 581 } |
| 575 } | 582 } |
| 576 | 583 |
| 577 // At this point the renderer has sent us a message asking to advance the | 584 // At this point the renderer has sent us a message asking to advance the |
| 578 // focus (as the end of the focus loop was reached in the renderer). | 585 // focus (as the end of the focus loop was reached in the renderer). |
| 579 // We need to run the message loop to process it. | 586 // We need to run the message loop to process it. |
| 580 MessageLoop::current()->RunAllPending(); | 587 ui_test_utils::RunAllPendingInMessageLoop(); |
| 581 } | 588 } |
| 582 | 589 |
| 583 // Now let's try reverse focus traversal. | 590 // Now let's try reverse focus traversal. |
| 584 for (int i = 0; i < 2; ++i) { | 591 for (int i = 0; i < 2; ++i) { |
| 585 // Location bar should be focused. | 592 // Location bar should be focused. |
| 586 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 593 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 587 | 594 |
| 588 // Now let's press shift-tab to move the focus in reverse. | 595 // Now let's press shift-tab to move the focus in reverse. |
| 589 for (size_t j = 0; j < 7; ++j) { | 596 for (size_t j = 0; j < 7; ++j) { |
| 590 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 597 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 591 false, true, false)); | 598 false, true, false)); |
| 592 | 599 |
| 593 if (j < arraysize(kExpElementIDs) - 1) { | 600 if (j < arraysize(kExpElementIDs) - 1) { |
| 594 interstitial_page->WaitForFocusChange(); | 601 interstitial_page->WaitForFocusChange(); |
| 595 } else { | 602 } else { |
| 596 // On the last tab key press, the focus returns to the browser. | 603 // On the last tab key press, the focus returns to the browser. |
| 597 ui_test_utils::WaitForFocusInBrowser(browser()); | 604 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 598 } | 605 } |
| 599 | 606 |
| 600 // Let's make sure the focus is on the expected element in the page. | 607 // Let's make sure the focus is on the expected element in the page. |
| 601 std::string actual = interstitial_page->GetFocusedElement(); | 608 std::string actual = interstitial_page->GetFocusedElement(); |
| 602 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 609 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 603 } | 610 } |
| 604 | 611 |
| 605 // At this point the renderer has sent us a message asking to advance the | 612 // At this point the renderer has sent us a message asking to advance the |
| 606 // focus (as the end of the focus loop was reached in the renderer). | 613 // focus (as the end of the focus loop was reached in the renderer). |
| 607 // We need to run the message loop to process it. | 614 // We need to run the message loop to process it. |
| 608 MessageLoop::current()->RunAllPending(); | 615 ui_test_utils::RunAllPendingInMessageLoop(); |
| 609 } | 616 } |
| 610 } | 617 } |
| 611 | 618 |
| 612 // Focus stays on page with interstitials. | 619 // Focus stays on page with interstitials. |
| 613 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { | 620 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
| 614 HTTPTestServer* server = StartHTTPServer(); | 621 HTTPTestServer* server = StartHTTPServer(); |
| 615 | 622 |
| 616 // First we navigate to our test page. | 623 // First we navigate to our test page. |
| 617 GURL url = server->TestServerPageW(kSimplePage); | 624 GURL url = server->TestServerPageW(kSimplePage); |
| 618 ui_test_utils::NavigateToURL(browser(), url); | 625 ui_test_utils::NavigateToURL(browser(), url); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 702 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 696 ui_test_utils::RunMessageLoop(); | 703 ui_test_utils::RunMessageLoop(); |
| 697 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 704 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 698 } | 705 } |
| 699 | 706 |
| 700 // Makes sure the focus is in the right location when opening the different | 707 // Makes sure the focus is in the right location when opening the different |
| 701 // types of tabs. | 708 // types of tabs. |
| 702 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { | 709 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
| 703 // Open the history tab, focus should be on the tab contents. | 710 // Open the history tab, focus should be on the tab contents. |
| 704 browser()->ShowHistoryTab(); | 711 browser()->ShowHistoryTab(); |
| 712 |
| 713 ui_test_utils::RunAllPendingInMessageLoop(); |
| 714 |
| 705 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 715 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 706 | 716 |
| 707 // Open the new tab, focus should be on the location bar. | 717 // Open the new tab, focus should be on the location bar. |
| 708 browser()->NewTab(); | 718 browser()->NewTab(); |
| 709 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 719 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 710 | 720 |
| 711 // Open the download tab, focus should be on the tab contents. | 721 // Open the download tab, focus should be on the tab contents. |
| 712 browser()->ShowDownloadsTab(); | 722 browser()->ShowDownloadsTab(); |
| 713 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 723 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 714 | 724 |
| 715 // Open about:blank, focus should be on the location bar. | 725 // Open about:blank, focus should be on the location bar. |
| 716 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, | 726 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
| 717 true, -1, false, NULL); | 727 true, -1, false, NULL); |
| 718 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 728 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 719 } | 729 } |
| 720 | 730 |
| 721 // Tests that focus goes where expected when using reload. | 731 // Tests that focus goes where expected when using reload. |
| 722 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { | 732 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
| 723 HTTPTestServer* server = StartHTTPServer(); | 733 HTTPTestServer* server = StartHTTPServer(); |
| 724 | 734 |
| 725 // Open the new tab, reload. | 735 // Open the new tab, reload. |
| 726 browser()->NewTab(); | 736 browser()->NewTab(); |
| 737 |
| 738 ui_test_utils::RunAllPendingInMessageLoop(); |
| 739 |
| 727 browser()->Reload(); | 740 browser()->Reload(); |
| 728 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 741 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 729 // Focus should stay on the location bar. | 742 // Focus should stay on the location bar. |
| 730 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 743 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 731 | 744 |
| 732 // Open a regular page, focus the location bar, reload. | 745 // Open a regular page, focus the location bar, reload. |
| 733 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); | 746 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 734 browser()->FocusLocationBar(); | 747 browser()->FocusLocationBar(); |
| 735 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 748 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 736 browser()->Reload(); | 749 browser()->Reload(); |
| 737 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 750 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 738 // Focus should now be on the tab contents. | 751 // Focus should now be on the tab contents. |
| 739 browser()->ShowDownloadsTab(); | 752 browser()->ShowDownloadsTab(); |
| 740 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 753 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 741 } | 754 } |
| 742 | 755 |
| 743 // Tests that focus goes where expected when using reload on a crashed tab. | 756 // Tests that focus goes where expected when using reload on a crashed tab. |
| 744 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { | 757 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { |
| 745 HTTPTestServer* server = StartHTTPServer(); | 758 HTTPTestServer* server = StartHTTPServer(); |
| 746 | 759 |
| 747 // Open a regular page, crash, reload. | 760 // Open a regular page, crash, reload. |
| 748 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); | 761 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 749 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); | 762 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
| 750 browser()->Reload(); | 763 browser()->Reload(); |
| 751 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 764 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 752 // Focus should now be on the tab contents. | 765 // Focus should now be on the tab contents. |
| 753 browser()->ShowDownloadsTab(); | 766 browser()->ShowDownloadsTab(); |
| 754 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 767 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 755 } | 768 } |
| OLD | NEW |