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