| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/keyboard_codes.h" | 5 #include "base/keyboard_codes.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/ref_counted.h" | 7 #include "base/ref_counted.h" |
| 8 #include "chrome/browser/automation/ui_controls.h" | 8 #include "chrome/browser/automation/ui_controls.h" |
| 9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/browser_window.h" | 10 #include "chrome/browser/browser_window.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #if defined(TOOLKIT_VIEWS) | 24 #if defined(TOOLKIT_VIEWS) |
| 25 #include "chrome/browser/views/frame/browser_view.h" | 25 #include "chrome/browser/views/frame/browser_view.h" |
| 26 #include "chrome/browser/views/location_bar_view.h" | 26 #include "chrome/browser/views/location_bar_view.h" |
| 27 #include "chrome/browser/views/tab_contents/tab_contents_container.h" | 27 #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 #if defined(OS_LINUX) | 30 #if defined(OS_LINUX) |
| 31 #include "chrome/browser/gtk/view_id_util.h" | 31 #include "chrome/browser/gtk/view_id_util.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 #if defined(OS_LINUX) |
| 35 // For some reason we hit an external DNS lookup in this test in Linux but not |
| 36 // on Windows. TODO(estade): investigate. |
| 37 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
| 38 #else |
| 39 #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
| 40 #endif |
| 41 |
| 34 namespace { | 42 namespace { |
| 35 | 43 |
| 36 // The delay waited in some cases where we don't have a notifications for an | 44 // The delay waited in some cases where we don't have a notifications for an |
| 37 // action we take. | 45 // action we take. |
| 38 const int kActionDelayMs = 500; | 46 const int kActionDelayMs = 500; |
| 39 | 47 |
| 40 const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; | 48 const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; |
| 41 const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; | 49 const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; |
| 42 const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; | 50 const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; |
| 43 const wchar_t kTypicalPageName[] = L"typical_page.html"; | 51 const wchar_t kTypicalPageName[] = L"typical_page.html"; |
| 44 | 52 |
| 45 class BrowserFocusTest : public InProcessBrowserTest { | 53 class BrowserFocusTest : public InProcessBrowserTest { |
| 46 public: | 54 public: |
| 47 BrowserFocusTest() { | 55 BrowserFocusTest() { |
| 48 set_show_window(true); | 56 set_show_window(true); |
| 49 EnableDOMAutomation(); | 57 EnableDOMAutomation(); |
| 50 } | 58 } |
| 51 | 59 |
| 52 void CheckViewHasFocus(ViewID vid) { | 60 void CheckViewHasFocus(ViewID vid) { |
| 53 BrowserWindow* browser_window = browser()->window(); | 61 BrowserWindow* browser_window = browser()->window(); |
| 54 ASSERT_TRUE(browser_window); | 62 ASSERT_TRUE(browser_window); |
| 55 gfx::NativeWindow window = browser_window->GetNativeHandle(); | 63 gfx::NativeWindow window = browser_window->GetNativeHandle(); |
| 56 ASSERT_TRUE(window); | 64 ASSERT_TRUE(window); |
| 57 #if defined(OS_WIN) | 65 #if defined(OS_WIN) |
| 58 views::FocusManager* focus_manager = | 66 views::FocusManager* focus_manager = |
| 59 views::FocusManager::GetFocusManagerForNativeView(window); | 67 views::FocusManager::GetFocusManagerForNativeView(window); |
| 60 ASSERT_TRUE(focus_manager); | 68 ASSERT_TRUE(focus_manager); |
| 61 EXPECT_EQ(reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid), | 69 EXPECT_EQ(vid, focus_manager->GetFocusedView()->GetID()) << |
| 62 focus_manager->GetFocusedView()) << "For view id " << vid; | 70 "For view id " << vid; |
| 63 #elif defined(OS_LINUX) | 71 #elif defined(OS_LINUX) |
| 64 GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); | 72 GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); |
| 65 ASSERT_TRUE(widget); | 73 ASSERT_TRUE(widget); |
| 66 EXPECT_TRUE(WidgetInFocusChain(GTK_WIDGET(window), widget)); | 74 EXPECT_TRUE(WidgetInFocusChain(GTK_WIDGET(window), widget)) << |
| 75 "For view id " << vid; |
| 67 #else | 76 #else |
| 68 NOTIMPLEMENTED(); | 77 NOTIMPLEMENTED(); |
| 69 #endif | 78 #endif |
| 70 } | 79 } |
| 71 | 80 |
| 81 void ClickOnView(ViewID vid) { |
| 82 BrowserWindow* browser_window = browser()->window(); |
| 83 ASSERT_TRUE(browser_window); |
| 84 #if defined(OS_WIN) |
| 85 views::View* view = |
| 86 reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid); |
| 87 #elif defined(OS_LINUX) |
| 88 gfx::NativeWindow window = browser_window->GetNativeHandle(); |
| 89 ASSERT_TRUE(window); |
| 90 GtkWidget* view = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); |
| 91 #endif |
| 92 ASSERT_TRUE(view); |
| 93 ui_controls::MoveMouseToCenterAndPress( |
| 94 view, |
| 95 ui_controls::LEFT, |
| 96 ui_controls::DOWN | ui_controls::UP, |
| 97 new MessageLoop::QuitTask()); |
| 98 ui_test_utils::RunMessageLoop(); |
| 99 } |
| 100 |
| 72 static void HideNativeWindow(gfx::NativeWindow window) { | 101 static void HideNativeWindow(gfx::NativeWindow window) { |
| 73 #if defined(OS_WIN) | 102 #if defined(OS_WIN) |
| 74 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of | 103 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
| 75 // using Windows API. | 104 // using Windows API. |
| 76 ::ShowWindow(window, SW_HIDE); | 105 ::ShowWindow(window, SW_HIDE); |
| 77 #elif defined(OS_LINUX) | 106 #elif defined(OS_LINUX) |
| 78 gtk_widget_hide(GTK_WIDGET(window)); | 107 gtk_widget_hide(GTK_WIDGET(window)); |
| 79 #else | 108 #else |
| 80 NOTIMPLEMENTED(); | 109 NOTIMPLEMENTED(); |
| 81 #endif | 110 #endif |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 private: | 195 private: |
| 167 std::string html_contents_; | 196 std::string html_contents_; |
| 168 | 197 |
| 169 bool waiting_for_dom_response_; | 198 bool waiting_for_dom_response_; |
| 170 std::string dom_response_; | 199 std::string dom_response_; |
| 171 | 200 |
| 172 }; | 201 }; |
| 173 | 202 |
| 174 } // namespace | 203 } // namespace |
| 175 | 204 |
| 176 // TODO(estade): port. | |
| 177 #if defined(OS_WIN) | |
| 178 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { | 205 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
| 179 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, | 206 #if defined(OS_LINUX) |
| 180 true, -1, false, NULL); | 207 // It seems we have to wait a little bit for the widgets to spin up before |
| 208 // we can start clicking on them. |
| 209 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 210 new MessageLoop::QuitTask(), |
| 211 kActionDelayMs); |
| 212 ui_test_utils::RunMessageLoop(); |
| 213 #endif |
| 214 |
| 181 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); | 215 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 182 | 216 |
| 183 BrowserView* browser_view = static_cast<BrowserView*>(browser()->window()); | 217 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 184 ui_controls::MoveMouseToCenterAndPress( | |
| 185 browser_view->GetTabContentsContainerView(), | |
| 186 ui_controls::LEFT, | |
| 187 ui_controls::DOWN | ui_controls::UP, | |
| 188 new MessageLoop::QuitTask()); | |
| 189 ui_test_utils::RunMessageLoop(); | |
| 190 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); | 218 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 191 | 219 |
| 192 ui_controls::MoveMouseToCenterAndPress( | 220 ClickOnView(VIEW_ID_LOCATION_BAR); |
| 193 browser_view->GetLocationBarView(), | |
| 194 ui_controls::LEFT, | |
| 195 ui_controls::DOWN | ui_controls::UP, | |
| 196 new MessageLoop::QuitTask()); | |
| 197 ui_test_utils::RunMessageLoop(); | |
| 198 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); | 221 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 199 } | 222 } |
| 200 #endif | |
| 201 | 223 |
| 202 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { | 224 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
| 203 HTTPTestServer* server = StartHTTPServer(); | 225 HTTPTestServer* server = StartHTTPServer(); |
| 204 | 226 |
| 205 // First we navigate to our test page. | 227 // First we navigate to our test page. |
| 206 GURL url = server->TestServerPageW(kSimplePage); | 228 GURL url = server->TestServerPageW(kSimplePage); |
| 207 ui_test_utils::NavigateToURL(browser(), url); | 229 ui_test_utils::NavigateToURL(browser(), url); |
| 208 | 230 |
| 209 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 231 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 210 | 232 |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 } | 471 } |
| 450 | 472 |
| 451 // At this point the renderer has sent us a message asking to advance the | 473 // At this point the renderer has sent us a message asking to advance the |
| 452 // focus (as the end of the focus loop was reached in the renderer). | 474 // focus (as the end of the focus loop was reached in the renderer). |
| 453 // We need to run the message loop to process it. | 475 // We need to run the message loop to process it. |
| 454 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 476 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 455 ui_test_utils::RunMessageLoop(); | 477 ui_test_utils::RunMessageLoop(); |
| 456 } | 478 } |
| 457 } | 479 } |
| 458 | 480 |
| 459 #if defined(OS_WIN) | |
| 460 // Focus traversal while an interstitial is showing. | 481 // Focus traversal while an interstitial is showing. |
| 461 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) { | 482 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
| 462 HTTPTestServer* server = StartHTTPServer(); | 483 HTTPTestServer* server = StartHTTPServer(); |
| 463 | 484 |
| 464 // First we navigate to our test page. | 485 // First we navigate to our test page. |
| 465 GURL url = server->TestServerPageW(kSimplePage); | 486 GURL url = server->TestServerPageW(kSimplePage); |
| 466 ui_test_utils::NavigateToURL(browser(), url); | 487 ui_test_utils::NavigateToURL(browser(), url); |
| 467 | 488 |
| 468 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | |
| 469 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 470 views::FocusManager* focus_manager = | |
| 471 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 472 | |
| 473 // Focus should be on the page. | 489 // Focus should be on the page. |
| 474 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 490 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 475 focus_manager->GetFocusedView()); | |
| 476 | 491 |
| 477 // Let's show an interstitial. | 492 // Let's show an interstitial. |
| 478 TestInterstitialPage* interstitial_page = | 493 TestInterstitialPage* interstitial_page = |
| 479 new TestInterstitialPage(browser()->GetSelectedTabContents(), | 494 new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 480 true, GURL("http://interstitial.com")); | 495 true, GURL("http://interstitial.com")); |
| 481 interstitial_page->Show(); | 496 interstitial_page->Show(); |
| 482 // Give some time for the interstitial to show. | 497 // Give some time for the interstitial to show. |
| 483 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 498 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 484 new MessageLoop::QuitTask(), | 499 new MessageLoop::QuitTask(), |
| 485 1000); | 500 1000); |
| 486 ui_test_utils::RunMessageLoop(); | 501 ui_test_utils::RunMessageLoop(); |
| 487 | 502 |
| 488 // Click on the location bar. | 503 browser()->FocusLocationBar(); |
| 489 LocationBarView* location_bar = browser_view->GetLocationBarView(); | |
| 490 ui_controls::MoveMouseToCenterAndPress(location_bar, | |
| 491 ui_controls::LEFT, | |
| 492 ui_controls::DOWN | ui_controls::UP, | |
| 493 new MessageLoop::QuitTask()); | |
| 494 ui_test_utils::RunMessageLoop(); | |
| 495 | 504 |
| 496 const char* kExpElementIDs[] = { | 505 const char* kExpElementIDs[] = { |
| 497 "", // Initially no element in the page should be focused | 506 "", // Initially no element in the page should be focused |
| 498 // (the location bar is focused). | 507 // (the location bar is focused). |
| 499 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", | 508 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 500 "gmapLink" | 509 "gmapLink" |
| 501 }; | 510 }; |
| 502 | 511 |
| 512 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 513 |
| 503 // Test forward focus traversal. | 514 // Test forward focus traversal. |
| 504 for (int i = 0; i < 2; ++i) { | 515 for (int i = 0; i < 2; ++i) { |
| 505 // Location bar should be focused. | 516 // Location bar should be focused. |
| 506 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 517 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 507 | 518 |
| 508 // Now let's press tab to move the focus. | 519 // Now let's press tab to move the focus. |
| 509 for (int j = 0; j < 7; ++j) { | 520 for (int j = 0; j < 7; ++j) { |
| 510 // Let's make sure the focus is on the expected element in the page. | 521 // Let's make sure the focus is on the expected element in the page. |
| 511 std::string actual = interstitial_page->GetFocusedElement(); | 522 std::string actual = interstitial_page->GetFocusedElement(); |
| 512 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 523 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 513 | 524 |
| 514 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_TAB, false, | 525 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, false, |
| 515 false, false, | 526 false, false, |
| 516 new MessageLoop::QuitTask()); | 527 new MessageLoop::QuitTask()); |
| 517 ui_test_utils::RunMessageLoop(); | 528 ui_test_utils::RunMessageLoop(); |
| 518 // Ideally, we wouldn't sleep here and instead would use the event | 529 // Ideally, we wouldn't sleep here and instead would use the event |
| 519 // processed ack notification from the renderer. I am reluctant to create | 530 // processed ack notification from the renderer. I am reluctant to create |
| 520 // a new notification/callback for that purpose just for this test. | 531 // a new notification/callback for that purpose just for this test. |
| 521 ::Sleep(kActionDelayMs); | 532 PlatformThread::Sleep(kActionDelayMs); |
| 522 } | 533 } |
| 523 | 534 |
| 524 // At this point the renderer has sent us a message asking to advance the | 535 // At this point the renderer has sent us a message asking to advance the |
| 525 // focus (as the end of the focus loop was reached in the renderer). | 536 // focus (as the end of the focus loop was reached in the renderer). |
| 526 // We need to run the message loop to process it. | 537 // We need to run the message loop to process it. |
| 527 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 538 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 528 ui_test_utils::RunMessageLoop(); | 539 ui_test_utils::RunMessageLoop(); |
| 529 } | 540 } |
| 530 | 541 |
| 531 // Now let's try reverse focus traversal. | 542 // Now let's try reverse focus traversal. |
| 532 for (int i = 0; i < 2; ++i) { | 543 for (int i = 0; i < 2; ++i) { |
| 533 // Location bar should be focused. | 544 // Location bar should be focused. |
| 534 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 545 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 535 | 546 |
| 536 // Now let's press shift-tab to move the focus in reverse. | 547 // Now let's press shift-tab to move the focus in reverse. |
| 537 for (int j = 0; j < 7; ++j) { | 548 for (int j = 0; j < 7; ++j) { |
| 538 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_TAB, false, | 549 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, false, |
| 539 true, false, | 550 true, false, |
| 540 new MessageLoop::QuitTask()); | 551 new MessageLoop::QuitTask()); |
| 541 ui_test_utils::RunMessageLoop(); | 552 ui_test_utils::RunMessageLoop(); |
| 542 ::Sleep(kActionDelayMs); | 553 PlatformThread::Sleep(kActionDelayMs); |
| 543 | 554 |
| 544 // Let's make sure the focus is on the expected element in the page. | 555 // Let's make sure the focus is on the expected element in the page. |
| 545 std::string actual = interstitial_page->GetFocusedElement(); | 556 std::string actual = interstitial_page->GetFocusedElement(); |
| 546 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 557 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 547 } | 558 } |
| 548 | 559 |
| 549 // At this point the renderer has sent us a message asking to advance the | 560 // At this point the renderer has sent us a message asking to advance the |
| 550 // focus (as the end of the focus loop was reached in the renderer). | 561 // focus (as the end of the focus loop was reached in the renderer). |
| 551 // We need to run the message loop to process it. | 562 // We need to run the message loop to process it. |
| 552 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 563 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 553 ui_test_utils::RunMessageLoop(); | 564 ui_test_utils::RunMessageLoop(); |
| 554 } | 565 } |
| 555 } | 566 } |
| 556 | 567 |
| 557 // Focus stays on page with interstitials. | 568 // Focus stays on page with interstitials. |
| 558 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { | 569 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
| 559 HTTPTestServer* server = StartHTTPServer(); | 570 HTTPTestServer* server = StartHTTPServer(); |
| 560 | 571 |
| 561 // First we navigate to our test page. | 572 // First we navigate to our test page. |
| 562 GURL url = server->TestServerPageW(kSimplePage); | 573 GURL url = server->TestServerPageW(kSimplePage); |
| 563 ui_test_utils::NavigateToURL(browser(), url); | 574 ui_test_utils::NavigateToURL(browser(), url); |
| 564 | 575 |
| 565 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | |
| 566 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 567 views::FocusManager* focus_manager = | |
| 568 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 569 | |
| 570 // Page should have focus. | 576 // Page should have focus. |
| 571 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 577 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 572 focus_manager->GetFocusedView()); | |
| 573 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> | 578 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 574 HasFocus()); | 579 HasFocus()); |
| 575 | 580 |
| 576 // Let's show an interstitial. | 581 // Let's show an interstitial. |
| 577 TestInterstitialPage* interstitial_page = | 582 TestInterstitialPage* interstitial_page = |
| 578 new TestInterstitialPage(browser()->GetSelectedTabContents(), | 583 new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 579 true, GURL("http://interstitial.com")); | 584 true, GURL("http://interstitial.com")); |
| 580 interstitial_page->Show(); | 585 interstitial_page->Show(); |
| 581 // Give some time for the interstitial to show. | 586 // Give some time for the interstitial to show. |
| 582 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 587 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 583 new MessageLoop::QuitTask(), | 588 new MessageLoop::QuitTask(), |
| 584 1000); | 589 1000); |
| 585 ui_test_utils::RunMessageLoop(); | 590 ui_test_utils::RunMessageLoop(); |
| 586 | 591 |
| 587 // The interstitial should have focus now. | 592 // The interstitial should have focus now. |
| 588 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 593 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 589 focus_manager->GetFocusedView()); | |
| 590 EXPECT_TRUE(interstitial_page->HasFocus()); | 594 EXPECT_TRUE(interstitial_page->HasFocus()); |
| 591 | 595 |
| 592 // Hide the interstitial. | 596 // Hide the interstitial. |
| 593 interstitial_page->DontProceed(); | 597 interstitial_page->DontProceed(); |
| 594 | 598 |
| 595 // Focus should be back on the original page. | 599 // Focus should be back on the original page. |
| 596 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 600 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 597 focus_manager->GetFocusedView()); | |
| 598 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> | |
| 599 HasFocus()); | |
| 600 } | 601 } |
| 601 | 602 |
| 602 // Make sure Find box can request focus, even when it is already open. | 603 // Make sure Find box can request focus, even when it is already open. |
| 603 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { | 604 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
| 604 HTTPTestServer* server = StartHTTPServer(); | 605 HTTPTestServer* server = StartHTTPServer(); |
| 605 | 606 |
| 606 // Open some page (any page that doesn't steal focus). | 607 // Open some page (any page that doesn't steal focus). |
| 607 GURL url = server->TestServerPageW(kTypicalPage); | 608 GURL url = server->TestServerPageW(kTypicalPage); |
| 608 ui_test_utils::NavigateToURL(browser(), url); | 609 ui_test_utils::NavigateToURL(browser(), url); |
| 609 | 610 |
| 610 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 611 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 611 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 612 views::FocusManager* focus_manager = | |
| 613 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 614 LocationBarView* location_bar = browser_view->GetLocationBarView(); | |
| 615 | 612 |
| 616 // Press Ctrl+F, which will make the Find box open and request focus. | 613 // Press Ctrl+F, which will make the Find box open and request focus. |
| 617 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_F, true, | 614 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 618 false, false, | 615 false, false, |
| 619 new MessageLoop::QuitTask()); | 616 new MessageLoop::QuitTask()); |
| 620 ui_test_utils::RunMessageLoop(); | 617 ui_test_utils::RunMessageLoop(); |
| 621 | 618 |
| 622 // Ideally, we wouldn't sleep here and instead would intercept the | 619 // Ideally, we wouldn't sleep here and instead would intercept the |
| 623 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we | 620 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 624 // could create a RenderViewHostDelegate wrapper and hook-it up by either: | 621 // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 625 // - creating a factory used to create the delegate | 622 // - creating a factory used to create the delegate |
| 626 // - making the test a private and overwriting the delegate member directly. | 623 // - making the test a private and overwriting the delegate member directly. |
| 627 ::Sleep(kActionDelayMs); | 624 MessageLoop::current()->PostDelayedTask( |
| 628 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 625 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 629 ui_test_utils::RunMessageLoop(); | 626 ui_test_utils::RunMessageLoop(); |
| 630 | 627 |
| 631 views::View* focused_view = focus_manager->GetFocusedView(); | 628 CheckViewHasFocus(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
| 632 ASSERT_TRUE(focused_view != NULL); | |
| 633 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); | |
| 634 | 629 |
| 635 // Click on the location bar. | 630 browser()->FocusLocationBar(); |
| 636 ui_controls::MoveMouseToCenterAndPress(location_bar, | 631 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 637 ui_controls::LEFT, | |
| 638 ui_controls::DOWN | ui_controls::UP, | |
| 639 new MessageLoop::QuitTask()); | |
| 640 ui_test_utils::RunMessageLoop(); | |
| 641 | |
| 642 // Make sure the location bar is focused. | |
| 643 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | |
| 644 | 632 |
| 645 // Now press Ctrl+F again and focus should move to the Find box. | 633 // Now press Ctrl+F again and focus should move to the Find box. |
| 646 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_F, true, | 634 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 647 false, false, | 635 false, false, |
| 648 new MessageLoop::QuitTask()); | 636 new MessageLoop::QuitTask()); |
| 649 ui_test_utils::RunMessageLoop(); | 637 ui_test_utils::RunMessageLoop(); |
| 650 focused_view = focus_manager->GetFocusedView(); | 638 CheckViewHasFocus(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
| 651 ASSERT_TRUE(focused_view != NULL); | |
| 652 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); | |
| 653 | 639 |
| 654 // Set focus to the page. | 640 // Set focus to the page. |
| 655 ui_controls::MoveMouseToCenterAndPress( | 641 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 656 browser_view->GetTabContentsContainerView(), | 642 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 657 ui_controls::LEFT, | |
| 658 ui_controls::DOWN | ui_controls::UP, | |
| 659 new MessageLoop::QuitTask()); | |
| 660 ui_test_utils::RunMessageLoop(); | |
| 661 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | |
| 662 focus_manager->GetFocusedView()); | |
| 663 | 643 |
| 664 // Now press Ctrl+F again and focus should move to the Find box. | 644 // Now press Ctrl+F again and focus should move to the Find box. |
| 665 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_F, true, false, | 645 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, false, |
| 666 false, new MessageLoop::QuitTask()); | 646 false, new MessageLoop::QuitTask()); |
| 667 ui_test_utils::RunMessageLoop(); | 647 ui_test_utils::RunMessageLoop(); |
| 668 | 648 |
| 669 // See remark above on why we wait. | 649 // See remark above on why we wait. |
| 670 ::Sleep(kActionDelayMs); | 650 MessageLoop::current()->PostDelayedTask( |
| 671 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 651 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 672 ui_test_utils::RunMessageLoop(); | 652 ui_test_utils::RunMessageLoop(); |
| 673 | 653 CheckViewHasFocus(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD); |
| 674 focused_view = focus_manager->GetFocusedView(); | |
| 675 ASSERT_TRUE(focused_view != NULL); | |
| 676 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); | |
| 677 } | 654 } |
| 678 #endif // defined(OS_WIN) | |
| 679 | 655 |
| 680 // Makes sure the focus is in the right location when opening the different | 656 // Makes sure the focus is in the right location when opening the different |
| 681 // types of tabs. | 657 // types of tabs. |
| 682 // TODO(estade): undisable this. | |
| 683 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { | 658 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
| 684 // Open the history tab, focus should be on the tab contents. | 659 // Open the history tab, focus should be on the tab contents. |
| 685 browser()->ShowHistoryTab(); | 660 browser()->ShowHistoryTab(); |
| 686 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); | 661 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 687 | 662 |
| 688 // Open the new tab, focus should be on the location bar. | 663 // Open the new tab, focus should be on the location bar. |
| 689 browser()->NewTab(); | 664 browser()->NewTab(); |
| 690 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); | 665 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 691 | 666 |
| 692 // Open the download tab, focus should be on the tab contents. | 667 // Open the download tab, focus should be on the tab contents. |
| 693 browser()->ShowDownloadsTab(); | 668 browser()->ShowDownloadsTab(); |
| 694 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); | 669 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 695 | 670 |
| 696 // Open about:blank, focus should be on the location bar. | 671 // Open about:blank, focus should be on the location bar. |
| 697 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, | 672 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
| 698 true, -1, false, NULL); | 673 true, -1, false, NULL); |
| 699 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); | 674 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 700 } | 675 } |
| 701 | 676 |
| 702 #if defined(OS_WIN) | |
| 703 // Tests that focus goes where expected when using reload. | 677 // Tests that focus goes where expected when using reload. |
| 704 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { | 678 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
| 705 HTTPTestServer* server = StartHTTPServer(); | 679 HTTPTestServer* server = StartHTTPServer(); |
| 706 | 680 |
| 707 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | |
| 708 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 709 ASSERT_TRUE(browser_view); | |
| 710 views::FocusManager* focus_manager = | |
| 711 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 712 ASSERT_TRUE(focus_manager); | |
| 713 | |
| 714 // Open the new tab, reload. | 681 // Open the new tab, reload. |
| 715 browser()->NewTab(); | 682 browser()->NewTab(); |
| 716 browser()->Reload(); | 683 browser()->Reload(); |
| 717 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 684 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 718 // Focus should stay on the location bar. | 685 // Focus should stay on the location bar. |
| 719 EXPECT_EQ(browser_view->GetLocationBarView(), | 686 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 720 focus_manager->GetFocusedView()); | |
| 721 | 687 |
| 722 // Open a regular page, focus the location bar, reload. | 688 // Open a regular page, focus the location bar, reload. |
| 723 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); | 689 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 724 browser_view->GetLocationBarView()->FocusLocation(); | 690 browser()->FocusLocationBar(); |
| 725 EXPECT_EQ(browser_view->GetLocationBarView(), | 691 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 726 focus_manager->GetFocusedView()); | |
| 727 browser()->Reload(); | 692 browser()->Reload(); |
| 728 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 693 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 729 // Focus should now be on the tab contents. | 694 // Focus should now be on the tab contents. |
| 730 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 695 browser()->ShowDownloadsTab(); |
| 731 focus_manager->GetFocusedView()); | 696 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 732 } | 697 } |
| 733 | 698 |
| 734 // Tests that focus goes where expected when using reload on a crashed tab. | 699 // Tests that focus goes where expected when using reload on a crashed tab. |
| 735 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { | 700 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { |
| 736 HTTPTestServer* server = StartHTTPServer(); | 701 HTTPTestServer* server = StartHTTPServer(); |
| 737 | 702 |
| 738 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | |
| 739 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 740 ASSERT_TRUE(browser_view); | |
| 741 views::FocusManager* focus_manager = | |
| 742 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 743 ASSERT_TRUE(focus_manager); | |
| 744 | |
| 745 // Open a regular page, crash, reload. | 703 // Open a regular page, crash, reload. |
| 746 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); | 704 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 747 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); | 705 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
| 748 browser()->Reload(); | 706 browser()->Reload(); |
| 749 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 707 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 750 // Focus should now be on the tab contents. | 708 // Focus should now be on the tab contents. |
| 751 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 709 browser()->ShowDownloadsTab(); |
| 752 focus_manager->GetFocusedView()); | 710 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 753 } | 711 } |
| 754 #endif // defined(OS_WIN) | |
| OLD | NEW |