| 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/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 10 #include "chrome/browser/automation/ui_controls.h" | 10 #include "chrome/browser/automation/ui_controls.h" |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 297 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 298 browser()->SelectTabContentsAt(0, true); | 298 browser()->SelectTabContentsAt(0, true); |
| 299 // Try the above, but with ctrl+tab. Since tab normally changes focus, | 299 // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 300 // this has regressed in the past. Loop through several times to be sure. | 300 // this has regressed in the past. Loop through several times to be sure. |
| 301 for (int j = 0; j < 15; j++) { | 301 for (int j = 0; j < 15; j++) { |
| 302 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : | 302 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 303 VIEW_ID_LOCATION_BAR; | 303 VIEW_ID_LOCATION_BAR; |
| 304 ASSERT_TRUE(IsViewFocused(vid)); | 304 ASSERT_TRUE(IsViewFocused(vid)); |
| 305 | 305 |
| 306 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, | 306 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, |
| 307 false, false, | 307 false, false, false, |
| 308 new MessageLoop::QuitTask()); | 308 new MessageLoop::QuitTask()); |
| 309 ui_test_utils::RunMessageLoop(); | 309 ui_test_utils::RunMessageLoop(); |
| 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 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, | 319 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, |
| 320 true, false, | 320 true, false, false, |
| 321 new MessageLoop::QuitTask()); | 321 new MessageLoop::QuitTask()); |
| 322 ui_test_utils::RunMessageLoop(); | 322 ui_test_utils::RunMessageLoop(); |
| 323 } | 323 } |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 | 326 |
| 327 // Tabs remember focus with find-in-page box. | 327 // Tabs remember focus with find-in-page box. |
| 328 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { | 328 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
| 329 HTTPTestServer* server = StartHTTPServer(); | 329 HTTPTestServer* server = StartHTTPServer(); |
| 330 | 330 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 // Let's make sure the focus is on the expected element in the page. | 465 // Let's make sure the focus is on the expected element in the page. |
| 466 std::string actual; | 466 std::string actual; |
| 467 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 467 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 468 browser()->GetSelectedTabContents()->render_view_host(), | 468 browser()->GetSelectedTabContents()->render_view_host(), |
| 469 L"", | 469 L"", |
| 470 L"window.domAutomationController.send(getFocusedElement());", | 470 L"window.domAutomationController.send(getFocusedElement());", |
| 471 &actual)); | 471 &actual)); |
| 472 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 472 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 473 | 473 |
| 474 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 474 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 475 false, false, false)); | 475 false, false, false, false)); |
| 476 | 476 |
| 477 if (j < arraysize(kExpElementIDs) - 1) { | 477 if (j < arraysize(kExpElementIDs) - 1) { |
| 478 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> | 478 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> |
| 479 render_view_host()); | 479 render_view_host()); |
| 480 } else { | 480 } else { |
| 481 // On the last tab key press, the focus returns to the browser. | 481 // On the last tab key press, the focus returns to the browser. |
| 482 ui_test_utils::WaitForFocusInBrowser(browser()); | 482 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 483 } | 483 } |
| 484 } | 484 } |
| 485 | 485 |
| 486 // At this point the renderer has sent us a message asking to advance the | 486 // At this point the renderer has sent us a message asking to advance the |
| 487 // focus (as the end of the focus loop was reached in the renderer). | 487 // focus (as the end of the focus loop was reached in the renderer). |
| 488 // We need to run the message loop to process it. | 488 // We need to run the message loop to process it. |
| 489 ui_test_utils::RunAllPendingInMessageLoop(); | 489 ui_test_utils::RunAllPendingInMessageLoop(); |
| 490 } | 490 } |
| 491 | 491 |
| 492 // Now let's try reverse focus traversal. | 492 // Now let's try reverse focus traversal. |
| 493 for (int i = 0; i < 3; ++i) { | 493 for (int i = 0; i < 3; ++i) { |
| 494 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); | 494 SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
| 495 // Location bar should be focused. | 495 // Location bar should be focused. |
| 496 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 496 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 497 | 497 |
| 498 // Now let's press shift-tab to move the focus in reverse. | 498 // Now let's press shift-tab to move the focus in reverse. |
| 499 for (size_t j = 0; j < 7; ++j) { | 499 for (size_t j = 0; j < 7; ++j) { |
| 500 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); | 500 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); |
| 501 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 501 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 502 false, true, false)); | 502 false, true, false, false)); |
| 503 | 503 |
| 504 if (j < arraysize(kExpElementIDs) - 1) { | 504 if (j < arraysize(kExpElementIDs) - 1) { |
| 505 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> | 505 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> |
| 506 render_view_host()); | 506 render_view_host()); |
| 507 } else { | 507 } else { |
| 508 // On the last tab key press, the focus returns to the browser. | 508 // On the last tab key press, the focus returns to the browser. |
| 509 ui_test_utils::WaitForFocusInBrowser(browser()); | 509 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 510 } | 510 } |
| 511 | 511 |
| 512 // Let's make sure the focus is on the expected element in the page. | 512 // Let's make sure the focus is on the expected element in the page. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 // Location bar should be focused. | 564 // Location bar should be focused. |
| 565 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 565 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 566 | 566 |
| 567 // Now let's press tab to move the focus. | 567 // Now let's press tab to move the focus. |
| 568 for (size_t j = 0; j < 7; ++j) { | 568 for (size_t j = 0; j < 7; ++j) { |
| 569 // Let's make sure the focus is on the expected element in the page. | 569 // Let's make sure the focus is on the expected element in the page. |
| 570 std::string actual = interstitial_page->GetFocusedElement(); | 570 std::string actual = interstitial_page->GetFocusedElement(); |
| 571 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 571 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 572 | 572 |
| 573 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 573 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 574 false, false, false)); | 574 false, false, false, false)); |
| 575 | 575 |
| 576 if (j < arraysize(kExpElementIDs) - 1) { | 576 if (j < arraysize(kExpElementIDs) - 1) { |
| 577 interstitial_page->WaitForFocusChange(); | 577 interstitial_page->WaitForFocusChange(); |
| 578 } else { | 578 } else { |
| 579 // On the last tab key press, the focus returns to the browser. | 579 // On the last tab key press, the focus returns to the browser. |
| 580 ui_test_utils::WaitForFocusInBrowser(browser()); | 580 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 581 } | 581 } |
| 582 } | 582 } |
| 583 | 583 |
| 584 // At this point the renderer has sent us a message asking to advance the | 584 // At this point the renderer has sent us a message asking to advance the |
| 585 // focus (as the end of the focus loop was reached in the renderer). | 585 // focus (as the end of the focus loop was reached in the renderer). |
| 586 // We need to run the message loop to process it. | 586 // We need to run the message loop to process it. |
| 587 ui_test_utils::RunAllPendingInMessageLoop(); | 587 ui_test_utils::RunAllPendingInMessageLoop(); |
| 588 } | 588 } |
| 589 | 589 |
| 590 // Now let's try reverse focus traversal. | 590 // Now let's try reverse focus traversal. |
| 591 for (int i = 0; i < 2; ++i) { | 591 for (int i = 0; i < 2; ++i) { |
| 592 // Location bar should be focused. | 592 // Location bar should be focused. |
| 593 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 593 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 594 | 594 |
| 595 // Now let's press shift-tab to move the focus in reverse. | 595 // Now let's press shift-tab to move the focus in reverse. |
| 596 for (size_t j = 0; j < 7; ++j) { | 596 for (size_t j = 0; j < 7; ++j) { |
| 597 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, | 597 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 598 false, true, false)); | 598 false, true, false, false)); |
| 599 | 599 |
| 600 if (j < arraysize(kExpElementIDs) - 1) { | 600 if (j < arraysize(kExpElementIDs) - 1) { |
| 601 interstitial_page->WaitForFocusChange(); | 601 interstitial_page->WaitForFocusChange(); |
| 602 } else { | 602 } else { |
| 603 // On the last tab key press, the focus returns to the browser. | 603 // On the last tab key press, the focus returns to the browser. |
| 604 ui_test_utils::WaitForFocusInBrowser(browser()); | 604 ui_test_utils::WaitForFocusInBrowser(browser()); |
| 605 } | 605 } |
| 606 | 606 |
| 607 // Let's make sure the focus is on the expected element in the page. | 607 // Let's make sure the focus is on the expected element in the page. |
| 608 std::string actual = interstitial_page->GetFocusedElement(); | 608 std::string actual = interstitial_page->GetFocusedElement(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 HTTPTestServer* server = StartHTTPServer(); | 656 HTTPTestServer* server = StartHTTPServer(); |
| 657 | 657 |
| 658 // Open some page (any page that doesn't steal focus). | 658 // Open some page (any page that doesn't steal focus). |
| 659 GURL url = server->TestServerPageW(kTypicalPage); | 659 GURL url = server->TestServerPageW(kTypicalPage); |
| 660 ui_test_utils::NavigateToURL(browser(), url); | 660 ui_test_utils::NavigateToURL(browser(), url); |
| 661 | 661 |
| 662 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 662 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 663 | 663 |
| 664 // Press Ctrl+F, which will make the Find box open and request focus. | 664 // Press Ctrl+F, which will make the Find box open and request focus. |
| 665 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 665 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 666 false, false, | 666 false, false, false, |
| 667 new MessageLoop::QuitTask()); | 667 new MessageLoop::QuitTask()); |
| 668 ui_test_utils::RunMessageLoop(); | 668 ui_test_utils::RunMessageLoop(); |
| 669 | 669 |
| 670 // Ideally, we wouldn't sleep here and instead would intercept the | 670 // Ideally, we wouldn't sleep here and instead would intercept the |
| 671 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we | 671 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 672 // could create a RenderViewHostDelegate wrapper and hook-it up by either: | 672 // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 673 // - creating a factory used to create the delegate | 673 // - creating a factory used to create the delegate |
| 674 // - making the test a private and overwriting the delegate member directly. | 674 // - making the test a private and overwriting the delegate member directly. |
| 675 MessageLoop::current()->PostDelayedTask( | 675 MessageLoop::current()->PostDelayedTask( |
| 676 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 676 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 677 ui_test_utils::RunMessageLoop(); | 677 ui_test_utils::RunMessageLoop(); |
| 678 | 678 |
| 679 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 679 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 680 | 680 |
| 681 browser()->FocusLocationBar(); | 681 browser()->FocusLocationBar(); |
| 682 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 682 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 683 | 683 |
| 684 // Now press Ctrl+F again and focus should move to the Find box. | 684 // Now press Ctrl+F again and focus should move to the Find box. |
| 685 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 685 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 686 false, false, | 686 false, false, false, |
| 687 new MessageLoop::QuitTask()); | 687 new MessageLoop::QuitTask()); |
| 688 ui_test_utils::RunMessageLoop(); | 688 ui_test_utils::RunMessageLoop(); |
| 689 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 689 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 690 | 690 |
| 691 // Set focus to the page. | 691 // Set focus to the page. |
| 692 ClickOnView(VIEW_ID_TAB_CONTAINER); | 692 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 693 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 693 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 694 | 694 |
| 695 // Now press Ctrl+F again and focus should move to the Find box. | 695 // Now press Ctrl+F again and focus should move to the Find box. |
| 696 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, false, | 696 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, false, |
| 697 false, new MessageLoop::QuitTask()); | 697 false, false, |
| 698 new MessageLoop::QuitTask()); |
| 698 ui_test_utils::RunMessageLoop(); | 699 ui_test_utils::RunMessageLoop(); |
| 699 | 700 |
| 700 // See remark above on why we wait. | 701 // See remark above on why we wait. |
| 701 MessageLoop::current()->PostDelayedTask( | 702 MessageLoop::current()->PostDelayedTask( |
| 702 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 703 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 703 ui_test_utils::RunMessageLoop(); | 704 ui_test_utils::RunMessageLoop(); |
| 704 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 705 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 705 } | 706 } |
| 706 | 707 |
| 707 // Makes sure the focus is in the right location when opening the different | 708 // Makes sure the focus is in the right location when opening the different |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 | 760 |
| 760 // Open a regular page, crash, reload. | 761 // Open a regular page, crash, reload. |
| 761 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); | 762 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 762 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); | 763 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
| 763 browser()->Reload(); | 764 browser()->Reload(); |
| 764 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 765 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 765 // Focus should now be on the tab contents. | 766 // Focus should now be on the tab contents. |
| 766 browser()->ShowDownloadsTab(); | 767 browser()->ShowDownloadsTab(); |
| 767 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 768 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 768 } | 769 } |
| OLD | NEW |