OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 298 |
299 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 299 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
300 browser()->SelectTabContentsAt(0, true); | 300 browser()->SelectTabContentsAt(0, true); |
301 // Try the above, but with ctrl+tab. Since tab normally changes focus, | 301 // Try the above, but with ctrl+tab. Since tab normally changes focus, |
302 // this has regressed in the past. Loop through several times to be sure. | 302 // this has regressed in the past. Loop through several times to be sure. |
303 for (int j = 0; j < 15; j++) { | 303 for (int j = 0; j < 15; j++) { |
304 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : | 304 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
305 VIEW_ID_LOCATION_BAR; | 305 VIEW_ID_LOCATION_BAR; |
306 ASSERT_TRUE(IsViewFocused(vid)); | 306 ASSERT_TRUE(IsViewFocused(vid)); |
307 | 307 |
308 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, | 308 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
309 false, false, false, | 309 window, base::VKEY_TAB, true, false, false, false)); |
310 new MessageLoop::QuitTask()); | |
311 ui_test_utils::RunMessageLoop(); | |
312 } | 310 } |
313 | 311 |
314 // As above, but with ctrl+shift+tab. | 312 // As above, but with ctrl+shift+tab. |
315 browser()->SelectTabContentsAt(4, true); | 313 browser()->SelectTabContentsAt(4, true); |
316 for (int j = 14; j >= 0; --j) { | 314 for (int j = 14; j >= 0; --j) { |
317 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : | 315 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
318 VIEW_ID_LOCATION_BAR; | 316 VIEW_ID_LOCATION_BAR; |
319 ASSERT_TRUE(IsViewFocused(vid)); | 317 ASSERT_TRUE(IsViewFocused(vid)); |
320 | 318 |
321 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, | 319 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
322 true, false, false, | 320 window, base::VKEY_TAB, true, true, false, false)); |
323 new MessageLoop::QuitTask()); | |
324 ui_test_utils::RunMessageLoop(); | |
325 } | 321 } |
326 } | 322 } |
327 } | 323 } |
328 | 324 |
329 // Tabs remember focus with find-in-page box. | 325 // Tabs remember focus with find-in-page box. |
330 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { | 326 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
331 BringBrowserWindowToFront(); | 327 BringBrowserWindowToFront(); |
332 ASSERT_TRUE(test_server()->Start()); | 328 ASSERT_TRUE(test_server()->Start()); |
333 | 329 |
334 // First we navigate to our test page. | 330 // First we navigate to our test page. |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 browser()->GetSelectedTabContents()->render_view_host(), | 477 browser()->GetSelectedTabContents()->render_view_host(), |
482 L"", | 478 L"", |
483 L"window.domAutomationController.send(getFocusedElement());", | 479 L"window.domAutomationController.send(getFocusedElement());", |
484 &actual)); | 480 &actual)); |
485 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 481 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
486 | 482 |
487 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 483 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
488 false, false, false, false)); | 484 false, false, false, false)); |
489 | 485 |
490 if (j < arraysize(kExpElementIDs) - 1) { | 486 if (j < arraysize(kExpElementIDs) - 1) { |
491 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> | 487 ASSERT_NO_FATAL_FAILURE( |
492 render_view_host()); | 488 ui_test_utils::WaitForFocusChange( |
| 489 browser()->GetSelectedTabContents()->render_view_host())); |
493 } else { | 490 } else { |
494 // On the last tab key press, the focus returns to the browser. | 491 // On the last tab key press, the focus returns to the browser. |
495 ui_test_utils::WaitForFocusInBrowser(browser()); | 492 ASSERT_NO_FATAL_FAILURE( |
| 493 ui_test_utils::WaitForFocusInBrowser(browser())); |
496 } | 494 } |
497 } | 495 } |
498 | 496 |
499 // At this point the renderer has sent us a message asking to advance the | 497 // At this point the renderer has sent us a message asking to advance the |
500 // focus (as the end of the focus loop was reached in the renderer). | 498 // focus (as the end of the focus loop was reached in the renderer). |
501 // We need to run the message loop to process it. | 499 // We need to run the message loop to process it. |
502 ui_test_utils::RunAllPendingInMessageLoop(); | 500 ui_test_utils::RunAllPendingInMessageLoop(); |
503 } | 501 } |
504 | 502 |
505 // Now let's try reverse focus traversal. | 503 // Now let's try reverse focus traversal. |
506 for (int i = 0; i < 3; ++i) { | 504 for (int i = 0; i < 3; ++i) { |
507 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); | 505 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
508 // Location bar should be focused. | 506 // Location bar should be focused. |
509 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 507 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
510 | 508 |
511 // Now let's press shift-tab to move the focus in reverse. | 509 // Now let's press shift-tab to move the focus in reverse. |
512 for (size_t j = 0; j < 7; ++j) { | 510 for (size_t j = 0; j < 7; ++j) { |
513 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); | 511 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); |
514 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 512 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
515 false, true, false, false)); | 513 false, true, false, false)); |
516 | 514 |
517 if (j < arraysize(kExpElementIDs) - 1) { | 515 if (j < arraysize(kExpElementIDs) - 1) { |
518 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> | 516 ASSERT_NO_FATAL_FAILURE( |
519 render_view_host()); | 517 ui_test_utils::WaitForFocusChange( |
| 518 browser()->GetSelectedTabContents()->render_view_host())); |
520 } else { | 519 } else { |
521 // On the last tab key press, the focus returns to the browser. | 520 // On the last tab key press, the focus returns to the browser. |
522 ui_test_utils::WaitForFocusInBrowser(browser()); | 521 ASSERT_NO_FATAL_FAILURE( |
| 522 ui_test_utils::WaitForFocusInBrowser(browser())); |
523 } | 523 } |
524 | 524 |
525 // Let's make sure the focus is on the expected element in the page. | 525 // Let's make sure the focus is on the expected element in the page. |
526 std::string actual; | 526 std::string actual; |
527 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 527 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
528 browser()->GetSelectedTabContents()->render_view_host(), | 528 browser()->GetSelectedTabContents()->render_view_host(), |
529 L"", | 529 L"", |
530 L"window.domAutomationController.send(getFocusedElement());", | 530 L"window.domAutomationController.send(getFocusedElement());", |
531 &actual)); | 531 &actual)); |
532 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 532 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 // Now let's press tab to move the focus. | 581 // Now let's press tab to move the focus. |
582 for (size_t j = 0; j < 7; ++j) { | 582 for (size_t j = 0; j < 7; ++j) { |
583 // Let's make sure the focus is on the expected element in the page. | 583 // Let's make sure the focus is on the expected element in the page. |
584 std::string actual = interstitial_page->GetFocusedElement(); | 584 std::string actual = interstitial_page->GetFocusedElement(); |
585 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 585 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
586 | 586 |
587 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 587 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
588 false, false, false, false)); | 588 false, false, false, false)); |
589 | 589 |
590 if (j < arraysize(kExpElementIDs) - 1) { | 590 if (j < arraysize(kExpElementIDs) - 1) { |
591 interstitial_page->WaitForFocusChange(); | 591 ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); |
592 } else { | 592 } else { |
593 // On the last tab key press, the focus returns to the browser. | 593 // On the last tab key press, the focus returns to the browser. |
594 ui_test_utils::WaitForFocusInBrowser(browser()); | 594 ASSERT_NO_FATAL_FAILURE( |
| 595 ui_test_utils::WaitForFocusInBrowser(browser())); |
595 } | 596 } |
596 } | 597 } |
597 | 598 |
598 // At this point the renderer has sent us a message asking to advance the | 599 // At this point the renderer has sent us a message asking to advance the |
599 // focus (as the end of the focus loop was reached in the renderer). | 600 // focus (as the end of the focus loop was reached in the renderer). |
600 // We need to run the message loop to process it. | 601 // We need to run the message loop to process it. |
601 ui_test_utils::RunAllPendingInMessageLoop(); | 602 ui_test_utils::RunAllPendingInMessageLoop(); |
602 } | 603 } |
603 | 604 |
604 // Now let's try reverse focus traversal. | 605 // Now let's try reverse focus traversal. |
605 for (int i = 0; i < 2; ++i) { | 606 for (int i = 0; i < 2; ++i) { |
606 // Location bar should be focused. | 607 // Location bar should be focused. |
607 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 608 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
608 | 609 |
609 // Now let's press shift-tab to move the focus in reverse. | 610 // Now let's press shift-tab to move the focus in reverse. |
610 for (size_t j = 0; j < 7; ++j) { | 611 for (size_t j = 0; j < 7; ++j) { |
611 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 612 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
612 false, true, false, false)); | 613 false, true, false, false)); |
613 | 614 |
614 if (j < arraysize(kExpElementIDs) - 1) { | 615 if (j < arraysize(kExpElementIDs) - 1) { |
615 interstitial_page->WaitForFocusChange(); | 616 ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); |
616 } else { | 617 } else { |
617 // On the last tab key press, the focus returns to the browser. | 618 // On the last tab key press, the focus returns to the browser. |
618 ui_test_utils::WaitForFocusInBrowser(browser()); | 619 ASSERT_NO_FATAL_FAILURE( |
| 620 ui_test_utils::WaitForFocusInBrowser(browser())); |
619 } | 621 } |
620 | 622 |
621 // Let's make sure the focus is on the expected element in the page. | 623 // Let's make sure the focus is on the expected element in the page. |
622 std::string actual = interstitial_page->GetFocusedElement(); | 624 std::string actual = interstitial_page->GetFocusedElement(); |
623 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 625 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
624 } | 626 } |
625 | 627 |
626 // At this point the renderer has sent us a message asking to advance the | 628 // At this point the renderer has sent us a message asking to advance the |
627 // focus (as the end of the focus loop was reached in the renderer). | 629 // focus (as the end of the focus loop was reached in the renderer). |
628 // We need to run the message loop to process it. | 630 // We need to run the message loop to process it. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 ASSERT_TRUE(test_server()->Start()); | 674 ASSERT_TRUE(test_server()->Start()); |
673 | 675 |
674 // Open some page (any page that doesn't steal focus). | 676 // Open some page (any page that doesn't steal focus). |
675 GURL url = test_server()->GetURL(kTypicalPage); | 677 GURL url = test_server()->GetURL(kTypicalPage); |
676 ui_test_utils::NavigateToURL(browser(), url); | 678 ui_test_utils::NavigateToURL(browser(), url); |
677 | 679 |
678 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 680 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
679 | 681 |
680 #if defined(OS_MACOSX) | 682 #if defined(OS_MACOSX) |
681 // Press Cmd+F, which will make the Find box open and request focus. | 683 // Press Cmd+F, which will make the Find box open and request focus. |
682 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, | 684 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
683 false, false, true, | 685 window, base::VKEY_F, false, false, false, true)); |
684 new MessageLoop::QuitTask()); | |
685 #else | 686 #else |
686 // Press Ctrl+F, which will make the Find box open and request focus. | 687 // Press Ctrl+F, which will make the Find box open and request focus. |
687 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 688 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
688 false, false, false, | 689 window, base::VKEY_F, true, false, false, false)); |
689 new MessageLoop::QuitTask()); | |
690 #endif | 690 #endif |
691 ui_test_utils::RunMessageLoop(); | |
692 | 691 |
693 // Ideally, we wouldn't sleep here and instead would intercept the | 692 // Ideally, we wouldn't sleep here and instead would intercept the |
694 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we | 693 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
695 // could create a RenderViewHostDelegate wrapper and hook-it up by either: | 694 // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
696 // - creating a factory used to create the delegate | 695 // - creating a factory used to create the delegate |
697 // - making the test a private and overwriting the delegate member directly. | 696 // - making the test a private and overwriting the delegate member directly. |
698 MessageLoop::current()->PostDelayedTask( | 697 MessageLoop::current()->PostDelayedTask( |
699 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 698 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
700 ui_test_utils::RunMessageLoop(); | 699 ui_test_utils::RunMessageLoop(); |
701 | 700 |
702 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 701 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
703 | 702 |
704 browser()->FocusLocationBar(); | 703 browser()->FocusLocationBar(); |
705 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 704 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
706 | 705 |
707 // Now press Ctrl+F again and focus should move to the Find box. | 706 // Now press Ctrl+F again and focus should move to the Find box. |
708 #if defined(OS_MACOSX) | 707 #if defined(OS_MACOSX) |
709 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, | 708 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
710 false, false, true, | 709 window, base::VKEY_F, false, false, false, true)); |
711 new MessageLoop::QuitTask()); | |
712 #else | 710 #else |
713 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 711 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
714 false, false, false, | 712 window, base::VKEY_F, true, false, false, false)); |
715 new MessageLoop::QuitTask()); | |
716 #endif | 713 #endif |
717 ui_test_utils::RunMessageLoop(); | |
718 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 714 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
719 | 715 |
720 // Set focus to the page. | 716 // Set focus to the page. |
721 ClickOnView(VIEW_ID_TAB_CONTAINER); | 717 ClickOnView(VIEW_ID_TAB_CONTAINER); |
722 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 718 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
723 | 719 |
724 // Now press Ctrl+F again and focus should move to the Find box. | 720 // Now press Ctrl+F again and focus should move to the Find box. |
725 #if defined(OS_MACOSX) | 721 #if defined(OS_MACOSX) |
726 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, | 722 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
727 false, false, true, | 723 window, base::VKEY_F, false, false, false, true)); |
728 new MessageLoop::QuitTask()); | |
729 #else | 724 #else |
730 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 725 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
731 false, false, false, | 726 window, base::VKEY_F, true, false, false, false)); |
732 new MessageLoop::QuitTask()); | |
733 #endif | 727 #endif |
734 ui_test_utils::RunMessageLoop(); | |
735 | 728 |
736 // See remark above on why we wait. | 729 // See remark above on why we wait. |
737 MessageLoop::current()->PostDelayedTask( | 730 MessageLoop::current()->PostDelayedTask( |
738 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 731 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
739 ui_test_utils::RunMessageLoop(); | 732 ui_test_utils::RunMessageLoop(); |
740 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 733 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
741 } | 734 } |
742 | 735 |
743 // Makes sure the focus is in the right location when opening the different | 736 // Makes sure the focus is in the right location when opening the different |
744 // types of tabs. | 737 // types of tabs. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); | 804 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
812 browser()->Reload(CURRENT_TAB); | 805 browser()->Reload(CURRENT_TAB); |
813 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 806 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
814 | 807 |
815 // Focus should now be on the tab contents. | 808 // Focus should now be on the tab contents. |
816 browser()->ShowDownloadsTab(); | 809 browser()->ShowDownloadsTab(); |
817 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 810 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
818 } | 811 } |
819 | 812 |
820 } // namespace | 813 } // namespace |
OLD | NEW |