Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: chrome/browser/browser_focus_uitest.cc

Issue 3354005: Re-lands 58186: (Closed)
Patch Set: Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 308 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
309 window, base::VKEY_TAB, true, false, false, false)); 309 window, app::VKEY_TAB, true, false, false, false));
310 } 310 }
311 311
312 // As above, but with ctrl+shift+tab. 312 // As above, but with ctrl+shift+tab.
313 browser()->SelectTabContentsAt(4, true); 313 browser()->SelectTabContentsAt(4, true);
314 for (int j = 14; j >= 0; --j) { 314 for (int j = 14; j >= 0; --j) {
315 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 :
316 VIEW_ID_LOCATION_BAR; 316 VIEW_ID_LOCATION_BAR;
317 ASSERT_TRUE(IsViewFocused(vid)); 317 ASSERT_TRUE(IsViewFocused(vid));
318 318
319 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 319 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
320 window, base::VKEY_TAB, true, true, false, false)); 320 window, app::VKEY_TAB, true, true, false, false));
321 } 321 }
322 } 322 }
323 } 323 }
324 324
325 // Tabs remember focus with find-in-page box. 325 // Tabs remember focus with find-in-page box.
326 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { 326 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
327 BringBrowserWindowToFront(); 327 BringBrowserWindowToFront();
328 ASSERT_TRUE(test_server()->Start()); 328 ASSERT_TRUE(test_server()->Start());
329 329
330 // First we navigate to our test page. 330 // First we navigate to our test page.
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j)); 473 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
474 // Let's make sure the focus is on the expected element in the page. 474 // Let's make sure the focus is on the expected element in the page.
475 std::string actual; 475 std::string actual;
476 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( 476 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
477 browser()->GetSelectedTabContents()->render_view_host(), 477 browser()->GetSelectedTabContents()->render_view_host(),
478 L"", 478 L"",
479 L"window.domAutomationController.send(getFocusedElement());", 479 L"window.domAutomationController.send(getFocusedElement());",
480 &actual)); 480 &actual));
481 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); 481 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
482 482
483 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, 483 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
484 false, false, false, false)); 484 false, false, false, false));
485 485
486 if (j < arraysize(kExpElementIDs) - 1) { 486 if (j < arraysize(kExpElementIDs) - 1) {
487 ASSERT_NO_FATAL_FAILURE( 487 ASSERT_NO_FATAL_FAILURE(
488 ui_test_utils::WaitForFocusChange( 488 ui_test_utils::WaitForFocusChange(
489 browser()->GetSelectedTabContents()->render_view_host())); 489 browser()->GetSelectedTabContents()->render_view_host()));
490 } else { 490 } else {
491 // 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.
492 ASSERT_NO_FATAL_FAILURE( 492 ASSERT_NO_FATAL_FAILURE(
493 ui_test_utils::WaitForFocusInBrowser(browser())); 493 ui_test_utils::WaitForFocusInBrowser(browser()));
494 } 494 }
495 } 495 }
496 496
497 // 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
498 // 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).
499 // We need to run the message loop to process it. 499 // We need to run the message loop to process it.
500 ui_test_utils::RunAllPendingInMessageLoop(); 500 ui_test_utils::RunAllPendingInMessageLoop();
501 } 501 }
502 502
503 // Now let's try reverse focus traversal. 503 // Now let's try reverse focus traversal.
504 for (int i = 0; i < 3; ++i) { 504 for (int i = 0; i < 3; ++i) {
505 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); 505 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
506 // Location bar should be focused. 506 // Location bar should be focused.
507 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); 507 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
508 508
509 // 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.
510 for (size_t j = 0; j < 7; ++j) { 510 for (size_t j = 0; j < 7; ++j) {
511 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); 511 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
512 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, 512 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
513 false, true, false, false)); 513 false, true, false, false));
514 514
515 if (j < arraysize(kExpElementIDs) - 1) { 515 if (j < arraysize(kExpElementIDs) - 1) {
516 ASSERT_NO_FATAL_FAILURE( 516 ASSERT_NO_FATAL_FAILURE(
517 ui_test_utils::WaitForFocusChange( 517 ui_test_utils::WaitForFocusChange(
518 browser()->GetSelectedTabContents()->render_view_host())); 518 browser()->GetSelectedTabContents()->render_view_host()));
519 } else { 519 } else {
520 // 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.
521 ASSERT_NO_FATAL_FAILURE( 521 ASSERT_NO_FATAL_FAILURE(
522 ui_test_utils::WaitForFocusInBrowser(browser())); 522 ui_test_utils::WaitForFocusInBrowser(browser()));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 for (int i = 0; i < 2; ++i) { 577 for (int i = 0; i < 2; ++i) {
578 // Location bar should be focused. 578 // Location bar should be focused.
579 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); 579 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
580 580
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, app::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 ASSERT_NO_FATAL_FAILURE(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 ASSERT_NO_FATAL_FAILURE( 594 ASSERT_NO_FATAL_FAILURE(
595 ui_test_utils::WaitForFocusInBrowser(browser())); 595 ui_test_utils::WaitForFocusInBrowser(browser()));
596 } 596 }
597 } 597 }
598 598
599 // 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
600 // 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).
601 // We need to run the message loop to process it. 601 // We need to run the message loop to process it.
602 ui_test_utils::RunAllPendingInMessageLoop(); 602 ui_test_utils::RunAllPendingInMessageLoop();
603 } 603 }
604 604
605 // Now let's try reverse focus traversal. 605 // Now let's try reverse focus traversal.
606 for (int i = 0; i < 2; ++i) { 606 for (int i = 0; i < 2; ++i) {
607 // Location bar should be focused. 607 // Location bar should be focused.
608 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); 608 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
609 609
610 // 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.
611 for (size_t j = 0; j < 7; ++j) { 611 for (size_t j = 0; j < 7; ++j) {
612 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, 612 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
613 false, true, false, false)); 613 false, true, false, false));
614 614
615 if (j < arraysize(kExpElementIDs) - 1) { 615 if (j < arraysize(kExpElementIDs) - 1) {
616 ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); 616 ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange());
617 } else { 617 } else {
618 // 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.
619 ASSERT_NO_FATAL_FAILURE( 619 ASSERT_NO_FATAL_FAILURE(
620 ui_test_utils::WaitForFocusInBrowser(browser())); 620 ui_test_utils::WaitForFocusInBrowser(browser()));
621 } 621 }
622 622
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 675
676 // Open some page (any page that doesn't steal focus). 676 // Open some page (any page that doesn't steal focus).
677 GURL url = test_server()->GetURL(kTypicalPage); 677 GURL url = test_server()->GetURL(kTypicalPage);
678 ui_test_utils::NavigateToURL(browser(), url); 678 ui_test_utils::NavigateToURL(browser(), url);
679 679
680 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); 680 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
681 681
682 #if defined(OS_MACOSX) 682 #if defined(OS_MACOSX)
683 // 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.
684 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 684 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
685 window, base::VKEY_F, false, false, false, true)); 685 window, app::VKEY_F, false, false, false, true));
686 #else 686 #else
687 // 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.
688 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 688 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
689 window, base::VKEY_F, true, false, false, false)); 689 window, app::VKEY_F, true, false, false, false));
690 #endif 690 #endif
691 691
692 // Ideally, we wouldn't sleep here and instead would intercept the 692 // Ideally, we wouldn't sleep here and instead would intercept the
693 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we 693 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
694 // could create a RenderViewHostDelegate wrapper and hook-it up by either: 694 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
695 // - creating a factory used to create the delegate 695 // - creating a factory used to create the delegate
696 // - making the test a private and overwriting the delegate member directly. 696 // - making the test a private and overwriting the delegate member directly.
697 MessageLoop::current()->PostDelayedTask( 697 MessageLoop::current()->PostDelayedTask(
698 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); 698 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
699 ui_test_utils::RunMessageLoop(); 699 ui_test_utils::RunMessageLoop();
700 700
701 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 701 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
702 702
703 browser()->FocusLocationBar(); 703 browser()->FocusLocationBar();
704 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); 704 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
705 705
706 // 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.
707 #if defined(OS_MACOSX) 707 #if defined(OS_MACOSX)
708 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 708 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
709 window, base::VKEY_F, false, false, false, true)); 709 window, app::VKEY_F, false, false, false, true));
710 #else 710 #else
711 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 711 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
712 window, base::VKEY_F, true, false, false, false)); 712 window, app::VKEY_F, true, false, false, false));
713 #endif 713 #endif
714 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 714 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
715 715
716 // Set focus to the page. 716 // Set focus to the page.
717 ClickOnView(VIEW_ID_TAB_CONTAINER); 717 ClickOnView(VIEW_ID_TAB_CONTAINER);
718 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); 718 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
719 719
720 // 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.
721 #if defined(OS_MACOSX) 721 #if defined(OS_MACOSX)
722 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 722 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
723 window, base::VKEY_F, false, false, false, true)); 723 window, app::VKEY_F, false, false, false, true));
724 #else 724 #else
725 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 725 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
726 window, base::VKEY_F, true, false, false, false)); 726 window, app::VKEY_F, true, false, false, false));
727 #endif 727 #endif
728 728
729 // See remark above on why we wait. 729 // See remark above on why we wait.
730 MessageLoop::current()->PostDelayedTask( 730 MessageLoop::current()->PostDelayedTask(
731 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); 731 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
732 ui_test_utils::RunMessageLoop(); 732 ui_test_utils::RunMessageLoop();
733 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 733 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
734 } 734 }
735 735
736 // 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
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); 803 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
804 browser()->Reload(CURRENT_TAB); 804 browser()->Reload(CURRENT_TAB);
805 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); 805 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
806 806
807 // Focus should now be on the tab contents. 807 // Focus should now be on the tab contents.
808 browser()->ShowDownloadsTab(); 808 browser()->ShowDownloadsTab();
809 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); 809 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
810 } 810 }
811 811
812 } // namespace 812 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/automation/ui_controls_win.cc ('k') | chrome/browser/browser_keyevents_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698