OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 unfocused_browser = browser(); | 458 unfocused_browser = browser(); |
459 #endif | 459 #endif |
460 | 460 |
461 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage); | 461 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage); |
462 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); | 462 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
463 | 463 |
464 // Activate the first browser. | 464 // Activate the first browser. |
465 focused_browser->window()->Activate(); | 465 focused_browser->window()->Activate(); |
466 | 466 |
467 ASSERT_TRUE(content::ExecuteJavaScript( | 467 ASSERT_TRUE(content::ExecuteJavaScript( |
468 chrome::GetActiveWebContents(unfocused_browser)->GetRenderViewHost(), L"", | 468 chrome::GetActiveWebContents(unfocused_browser)->GetRenderViewHost(), |
469 L"stealFocus();")); | 469 "", |
| 470 "stealFocus();")); |
470 | 471 |
471 // Make sure the first browser is still active. | 472 // Make sure the first browser is still active. |
472 EXPECT_TRUE(focused_browser->window()->IsActive()); | 473 EXPECT_TRUE(focused_browser->window()->IsActive()); |
473 } | 474 } |
474 | 475 |
475 // Page cannot steal focus when focus is on location bar. | 476 // Page cannot steal focus when focus is on location bar. |
476 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { | 477 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
477 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 478 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
478 ASSERT_TRUE(test_server()->Start()); | 479 ASSERT_TRUE(test_server()->Start()); |
479 | 480 |
480 // Open the page that steals focus. | 481 // Open the page that steals focus. |
481 GURL url = test_server()->GetURL(kStealFocusPage); | 482 GURL url = test_server()->GetURL(kStealFocusPage); |
482 ui_test_utils::NavigateToURL(browser(), url); | 483 ui_test_utils::NavigateToURL(browser(), url); |
483 | 484 |
484 chrome::FocusLocationBar(browser()); | 485 chrome::FocusLocationBar(browser()); |
485 | 486 |
486 ASSERT_TRUE(content::ExecuteJavaScript( | 487 ASSERT_TRUE(content::ExecuteJavaScript( |
487 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", | 488 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
488 L"stealFocus();")); | 489 "", |
| 490 "stealFocus();")); |
489 | 491 |
490 // Make sure the location bar is still focused. | 492 // Make sure the location bar is still focused. |
491 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 493 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); |
492 } | 494 } |
493 | 495 |
494 // Focus traversal on a regular page. | 496 // Focus traversal on a regular page. |
495 // Note that this test relies on a notification from the renderer that the | 497 // Note that this test relies on a notification from the renderer that the |
496 // focus has changed in the page. The notification in the renderer may change | 498 // focus has changed in the page. The notification in the renderer may change |
497 // at which point this test would fail (see comment in | 499 // at which point this test would fail (see comment in |
498 // RenderWidget::didFocus()). | 500 // RenderWidget::didFocus()). |
(...skipping 25 matching lines...) Expand all Loading... |
524 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 526 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
525 browser(), ui::VKEY_END, false, false, false, false)); | 527 browser(), ui::VKEY_END, false, false, false, false)); |
526 | 528 |
527 // Now let's press tab to move the focus. | 529 // Now let's press tab to move the focus. |
528 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { | 530 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
529 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j)); | 531 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j)); |
530 // Let's make sure the focus is on the expected element in the page. | 532 // Let's make sure the focus is on the expected element in the page. |
531 std::string actual; | 533 std::string actual; |
532 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 534 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
533 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 535 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
534 L"", | 536 "", |
535 L"window.domAutomationController.send(getFocusedElement());", | 537 "window.domAutomationController.send(getFocusedElement());", |
536 &actual)); | 538 &actual)); |
537 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 539 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
538 | 540 |
539 if (j < arraysize(kExpElementIDs) - 1) { | 541 if (j < arraysize(kExpElementIDs) - 1) { |
540 // If the next element is the kTextElementID, we expect to be | 542 // If the next element is the kTextElementID, we expect to be |
541 // notified we have switched to an editable node. | 543 // notified we have switched to an editable node. |
542 bool is_editable_node = | 544 bool is_editable_node = |
543 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); | 545 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); |
544 content::Details<bool> details(&is_editable_node); | 546 content::Details<bool> details(&is_editable_node); |
545 | 547 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 599 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
598 browser(), ui::VKEY_TAB, false, true, false, false, | 600 browser(), ui::VKEY_TAB, false, true, false, false, |
599 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, | 601 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, |
600 content::NotificationSource(content::Source<Browser>(browser())))); | 602 content::NotificationSource(content::Source<Browser>(browser())))); |
601 } | 603 } |
602 | 604 |
603 // Let's make sure the focus is on the expected element in the page. | 605 // Let's make sure the focus is on the expected element in the page. |
604 std::string actual; | 606 std::string actual; |
605 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 607 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
606 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 608 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
607 L"", | 609 "", |
608 L"window.domAutomationController.send(getFocusedElement());", | 610 "window.domAutomationController.send(getFocusedElement());", |
609 &actual)); | 611 &actual)); |
610 ASSERT_STREQ(next_element, actual.c_str()); | 612 ASSERT_STREQ(next_element, actual.c_str()); |
611 } | 613 } |
612 | 614 |
613 // At this point the renderer has sent us a message asking to advance the | 615 // At this point the renderer has sent us a message asking to advance the |
614 // focus (as the end of the focus loop was reached in the renderer). | 616 // focus (as the end of the focus loop was reached in the renderer). |
615 // We need to run the message loop to process it. | 617 // We need to run the message loop to process it. |
616 content::RunAllPendingInMessageLoop(); | 618 content::RunAllPendingInMessageLoop(); |
617 } | 619 } |
618 } | 620 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 | 657 |
656 // Move the caret to the end, otherwise the next Tab key may not move focus. | 658 // Move the caret to the end, otherwise the next Tab key may not move focus. |
657 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 659 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
658 browser(), ui::VKEY_END, false, false, false, false)); | 660 browser(), ui::VKEY_END, false, false, false, false)); |
659 | 661 |
660 // Now let's press tab to move the focus. | 662 // Now let's press tab to move the focus. |
661 for (size_t j = 0; j < 7; ++j) { | 663 for (size_t j = 0; j < 7; ++j) { |
662 // Let's make sure the focus is on the expected element in the page. | 664 // Let's make sure the focus is on the expected element in the page. |
663 std::string actual; | 665 std::string actual; |
664 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 666 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
665 interstitial_page->render_view_host(), L"", | 667 interstitial_page->render_view_host(), |
666 L"window.domAutomationController.send(getFocusedElement());", | 668 "", |
| 669 "window.domAutomationController.send(getFocusedElement());", |
667 &actual)); | 670 &actual)); |
668 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 671 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
669 | 672 |
670 int notification_type; | 673 int notification_type; |
671 content::NotificationSource notification_source = | 674 content::NotificationSource notification_source = |
672 content::NotificationService::AllSources(); | 675 content::NotificationService::AllSources(); |
673 if (j < arraysize(kExpElementIDs) - 1) { | 676 if (j < arraysize(kExpElementIDs) - 1) { |
674 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; | 677 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; |
675 notification_source = content::Source<RenderViewHost>( | 678 notification_source = content::Source<RenderViewHost>( |
676 interstitial_page->render_view_host()); | 679 interstitial_page->render_view_host()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 notification_source = content::Source<Browser>(browser()); | 718 notification_source = content::Source<Browser>(browser()); |
716 } | 719 } |
717 | 720 |
718 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 721 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
719 browser(), ui::VKEY_TAB, false, true, false, false, | 722 browser(), ui::VKEY_TAB, false, true, false, false, |
720 notification_type, notification_source)); | 723 notification_type, notification_source)); |
721 | 724 |
722 // Let's make sure the focus is on the expected element in the page. | 725 // Let's make sure the focus is on the expected element in the page. |
723 std::string actual; | 726 std::string actual; |
724 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 727 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
725 interstitial_page->render_view_host(), L"", | 728 interstitial_page->render_view_host(), |
726 L"window.domAutomationController.send(getFocusedElement());", | 729 "", |
| 730 "window.domAutomationController.send(getFocusedElement());", |
727 &actual)); | 731 &actual)); |
728 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 732 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
729 } | 733 } |
730 | 734 |
731 // At this point the renderer has sent us a message asking to advance the | 735 // At this point the renderer has sent us a message asking to advance the |
732 // focus (as the end of the focus loop was reached in the renderer). | 736 // focus (as the end of the focus loop was reached in the renderer). |
733 // We need to run the message loop to process it. | 737 // We need to run the message loop to process it. |
734 content::RunAllPendingInMessageLoop(); | 738 content::RunAllPendingInMessageLoop(); |
735 } | 739 } |
736 } | 740 } |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | 961 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
958 | 962 |
959 // Go back to the first tab. The focus should not be in the omnibox. | 963 // Go back to the first tab. The focus should not be in the omnibox. |
960 chrome::SelectPreviousTab(browser()); | 964 chrome::SelectPreviousTab(browser()); |
961 EXPECT_EQ(0, browser()->active_index()); | 965 EXPECT_EQ(0, browser()->active_index()); |
962 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), | 966 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), |
963 VIEW_ID_LOCATION_BAR)); | 967 VIEW_ID_LOCATION_BAR)); |
964 } | 968 } |
965 | 969 |
966 } // namespace | 970 } // namespace |
OLD | NEW |