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(), | 468 chrome::GetActiveWebContents(unfocused_browser)->GetRenderViewHost(), L"", |
469 "", | 469 L"stealFocus();")); |
470 "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::ExecuteJavaScript( |
488 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 487 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", |
489 "", | 488 L"stealFocus();")); |
490 "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 |
500 // RenderWidget::didFocus()). | 498 // RenderWidget::didFocus()). |
(...skipping 25 matching lines...) Expand all Loading... |
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::ExecuteJavaScriptAndExtractString( |
535 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 533 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
536 "", | 534 L"", |
537 "window.domAutomationController.send(getFocusedElement());", | 535 L"window.domAutomationController.send(getFocusedElement());", |
538 &actual)); | 536 &actual)); |
539 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 537 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
540 | 538 |
541 if (j < arraysize(kExpElementIDs) - 1) { | 539 if (j < arraysize(kExpElementIDs) - 1) { |
542 // If the next element is the kTextElementID, we expect to be | 540 // If the next element is the kTextElementID, we expect to be |
543 // notified we have switched to an editable node. | 541 // notified we have switched to an editable node. |
544 bool is_editable_node = | 542 bool is_editable_node = |
545 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); | 543 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); |
546 content::Details<bool> details(&is_editable_node); | 544 content::Details<bool> details(&is_editable_node); |
547 | 545 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 597 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
600 browser(), ui::VKEY_TAB, false, true, false, false, | 598 browser(), ui::VKEY_TAB, false, true, false, false, |
601 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, | 599 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, |
602 content::NotificationSource(content::Source<Browser>(browser())))); | 600 content::NotificationSource(content::Source<Browser>(browser())))); |
603 } | 601 } |
604 | 602 |
605 // Let's make sure the focus is on the expected element in the page. | 603 // Let's make sure the focus is on the expected element in the page. |
606 std::string actual; | 604 std::string actual; |
607 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 605 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
608 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), | 606 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), |
609 "", | 607 L"", |
610 "window.domAutomationController.send(getFocusedElement());", | 608 L"window.domAutomationController.send(getFocusedElement());", |
611 &actual)); | 609 &actual)); |
612 ASSERT_STREQ(next_element, actual.c_str()); | 610 ASSERT_STREQ(next_element, actual.c_str()); |
613 } | 611 } |
614 | 612 |
615 // At this point the renderer has sent us a message asking to advance the | 613 // 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). | 614 // focus (as the end of the focus loop was reached in the renderer). |
617 // We need to run the message loop to process it. | 615 // We need to run the message loop to process it. |
618 content::RunAllPendingInMessageLoop(); | 616 content::RunAllPendingInMessageLoop(); |
619 } | 617 } |
620 } | 618 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 | 655 |
658 // Move the caret to the end, otherwise the next Tab key may not move focus. | 656 // Move the caret to the end, otherwise the next Tab key may not move focus. |
659 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 657 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
660 browser(), ui::VKEY_END, false, false, false, false)); | 658 browser(), ui::VKEY_END, false, false, false, false)); |
661 | 659 |
662 // Now let's press tab to move the focus. | 660 // Now let's press tab to move the focus. |
663 for (size_t j = 0; j < 7; ++j) { | 661 for (size_t j = 0; j < 7; ++j) { |
664 // Let's make sure the focus is on the expected element in the page. | 662 // Let's make sure the focus is on the expected element in the page. |
665 std::string actual; | 663 std::string actual; |
666 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 664 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
667 interstitial_page->render_view_host(), | 665 interstitial_page->render_view_host(), L"", |
668 "", | 666 L"window.domAutomationController.send(getFocusedElement());", |
669 "window.domAutomationController.send(getFocusedElement());", | |
670 &actual)); | 667 &actual)); |
671 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 668 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
672 | 669 |
673 int notification_type; | 670 int notification_type; |
674 content::NotificationSource notification_source = | 671 content::NotificationSource notification_source = |
675 content::NotificationService::AllSources(); | 672 content::NotificationService::AllSources(); |
676 if (j < arraysize(kExpElementIDs) - 1) { | 673 if (j < arraysize(kExpElementIDs) - 1) { |
677 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; | 674 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; |
678 notification_source = content::Source<RenderViewHost>( | 675 notification_source = content::Source<RenderViewHost>( |
679 interstitial_page->render_view_host()); | 676 interstitial_page->render_view_host()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 notification_source = content::Source<Browser>(browser()); | 715 notification_source = content::Source<Browser>(browser()); |
719 } | 716 } |
720 | 717 |
721 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 718 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
722 browser(), ui::VKEY_TAB, false, true, false, false, | 719 browser(), ui::VKEY_TAB, false, true, false, false, |
723 notification_type, notification_source)); | 720 notification_type, notification_source)); |
724 | 721 |
725 // Let's make sure the focus is on the expected element in the page. | 722 // Let's make sure the focus is on the expected element in the page. |
726 std::string actual; | 723 std::string actual; |
727 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 724 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
728 interstitial_page->render_view_host(), | 725 interstitial_page->render_view_host(), L"", |
729 "", | 726 L"window.domAutomationController.send(getFocusedElement());", |
730 "window.domAutomationController.send(getFocusedElement());", | |
731 &actual)); | 727 &actual)); |
732 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 728 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
733 } | 729 } |
734 | 730 |
735 // At this point the renderer has sent us a message asking to advance the | 731 // 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). | 732 // focus (as the end of the focus loop was reached in the renderer). |
737 // We need to run the message loop to process it. | 733 // We need to run the message loop to process it. |
738 content::RunAllPendingInMessageLoop(); | 734 content::RunAllPendingInMessageLoop(); |
739 } | 735 } |
740 } | 736 } |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
961 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | 957 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
962 | 958 |
963 // Go back to the first tab. The focus should not be in the omnibox. | 959 // Go back to the first tab. The focus should not be in the omnibox. |
964 chrome::SelectPreviousTab(browser()); | 960 chrome::SelectPreviousTab(browser()); |
965 EXPECT_EQ(0, browser()->active_index()); | 961 EXPECT_EQ(0, browser()->active_index()); |
966 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), | 962 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), |
967 VIEW_ID_LOCATION_BAR)); | 963 VIEW_ID_LOCATION_BAR)); |
968 } | 964 } |
969 | 965 |
970 } // namespace | 966 } // namespace |
OLD | NEW |