| 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/ref_counted.h" | 6 #include "base/ref_counted.h" |
| 7 #include "chrome/browser/automation/ui_controls.h" | 7 #include "chrome/browser/automation/ui_controls.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 9 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 10 #include "chrome/browser/tab_contents/interstitial_page.h" | 10 #include "chrome/browser/tab_contents/interstitial_page.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 // First we navigate to our test page. | 102 // First we navigate to our test page. |
| 103 GURL url = server->TestServerPageW(kSimplePage); | 103 GURL url = server->TestServerPageW(kSimplePage); |
| 104 ui_test_utils::NavigateToURL(browser(), url); | 104 ui_test_utils::NavigateToURL(browser(), url); |
| 105 | 105 |
| 106 // The focus should be on the Tab contents. | 106 // The focus should be on the Tab contents. |
| 107 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 107 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 108 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 108 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 109 ASSERT_TRUE(browser_view); | 109 ASSERT_TRUE(browser_view); |
| 110 views::FocusManager* focus_manager = | 110 views::FocusManager* focus_manager = |
| 111 views::FocusManager::GetFocusManager(hwnd); | 111 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 112 ASSERT_TRUE(focus_manager); | 112 ASSERT_TRUE(focus_manager); |
| 113 | 113 |
| 114 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 114 EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
| 115 focus_manager->GetFocusedView()); | 115 focus_manager->GetFocusedView()); |
| 116 | 116 |
| 117 // Now hide the window, show it again, the focus should not have changed. | 117 // Now hide the window, show it again, the focus should not have changed. |
| 118 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of | 118 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
| 119 // using Windows API. | 119 // using Windows API. |
| 120 ::ShowWindow(hwnd, SW_HIDE); | 120 ::ShowWindow(hwnd, SW_HIDE); |
| 121 ::ShowWindow(hwnd, SW_SHOW); | 121 ::ShowWindow(hwnd, SW_SHOW); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 142 ASSERT_TRUE(browser2); | 142 ASSERT_TRUE(browser2); |
| 143 browser2->tabstrip_model()->delegate()->AddBlankTab(true); | 143 browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
| 144 browser2->window()->Show(); | 144 browser2->window()->Show(); |
| 145 ui_test_utils::NavigateToURL(browser2, url); | 145 ui_test_utils::NavigateToURL(browser2, url); |
| 146 | 146 |
| 147 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); | 147 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
| 148 BrowserView* browser_view2 = | 148 BrowserView* browser_view2 = |
| 149 BrowserView::GetBrowserViewForNativeWindow(hwnd2); | 149 BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
| 150 ASSERT_TRUE(browser_view2); | 150 ASSERT_TRUE(browser_view2); |
| 151 views::FocusManager* focus_manager2 = | 151 views::FocusManager* focus_manager2 = |
| 152 views::FocusManager::GetFocusManager(hwnd2); | 152 views::FocusManager::GetFocusManagerForNativeView(hwnd2); |
| 153 ASSERT_TRUE(focus_manager2); | 153 ASSERT_TRUE(focus_manager2); |
| 154 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), | 154 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
| 155 focus_manager2->GetFocusedView()); | 155 focus_manager2->GetFocusedView()); |
| 156 | 156 |
| 157 // Switch to the 1st browser window, focus should still be on the location | 157 // Switch to the 1st browser window, focus should still be on the location |
| 158 // bar and the second browser should have nothing focused. | 158 // bar and the second browser should have nothing focused. |
| 159 browser()->window()->Activate(); | 159 browser()->window()->Activate(); |
| 160 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 160 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
| 161 EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); | 161 EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
| 162 | 162 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 176 | 176 |
| 177 // First we navigate to our test page. | 177 // First we navigate to our test page. |
| 178 GURL url = server->TestServerPageW(kSimplePage); | 178 GURL url = server->TestServerPageW(kSimplePage); |
| 179 ui_test_utils::NavigateToURL(browser(), url); | 179 ui_test_utils::NavigateToURL(browser(), url); |
| 180 | 180 |
| 181 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 181 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 182 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 182 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 183 ASSERT_TRUE(browser_view); | 183 ASSERT_TRUE(browser_view); |
| 184 | 184 |
| 185 views::FocusManager* focus_manager = | 185 views::FocusManager* focus_manager = |
| 186 views::FocusManager::GetFocusManager(hwnd); | 186 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 187 ASSERT_TRUE(focus_manager); | 187 ASSERT_TRUE(focus_manager); |
| 188 | 188 |
| 189 // Create several tabs. | 189 // Create several tabs. |
| 190 for (int i = 0; i < 4; ++i) { | 190 for (int i = 0; i < 4; ++i) { |
| 191 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, false
, | 191 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, |
| 192 NULL); | 192 false, NULL); |
| 193 } | 193 } |
| 194 | 194 |
| 195 // Alternate focus for the tab. | 195 // Alternate focus for the tab. |
| 196 const bool kFocusPage[3][5] = { | 196 const bool kFocusPage[3][5] = { |
| 197 { true, true, true, true, false }, | 197 { true, true, true, true, false }, |
| 198 { false, false, false, false, false }, | 198 { false, false, false, false, false }, |
| 199 { false, true, false, true, false } | 199 { false, true, false, true, false } |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 for (int i = 1; i < 3; i++) { | 202 for (int i = 1; i < 3; i++) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { | 276 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
| 277 HTTPTestServer* server = StartHTTPServer(); | 277 HTTPTestServer* server = StartHTTPServer(); |
| 278 | 278 |
| 279 // Open the page that steals focus. | 279 // Open the page that steals focus. |
| 280 GURL url = server->TestServerPageW(kStealFocusPage); | 280 GURL url = server->TestServerPageW(kStealFocusPage); |
| 281 ui_test_utils::NavigateToURL(browser(), url); | 281 ui_test_utils::NavigateToURL(browser(), url); |
| 282 | 282 |
| 283 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 283 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 284 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 284 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 285 views::FocusManager* focus_manager = | 285 views::FocusManager* focus_manager = |
| 286 views::FocusManager::GetFocusManager(hwnd); | 286 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 287 | 287 |
| 288 // Click on the location bar. | 288 // Click on the location bar. |
| 289 LocationBarView* location_bar = browser_view->GetLocationBarView(); | 289 LocationBarView* location_bar = browser_view->GetLocationBarView(); |
| 290 ui_controls::MoveMouseToCenterAndPress(location_bar, | 290 ui_controls::MoveMouseToCenterAndPress(location_bar, |
| 291 ui_controls::LEFT, | 291 ui_controls::LEFT, |
| 292 ui_controls::DOWN | ui_controls::UP, | 292 ui_controls::DOWN | ui_controls::UP, |
| 293 new MessageLoop::QuitTask()); | 293 new MessageLoop::QuitTask()); |
| 294 ui_test_utils::RunMessageLoop(); | 294 ui_test_utils::RunMessageLoop(); |
| 295 | 295 |
| 296 // Wait for the page to steal focus. | 296 // Wait for the page to steal focus. |
| 297 ::Sleep(2000); | 297 ::Sleep(2000); |
| 298 | 298 |
| 299 // Make sure the location bar is still focused. | 299 // Make sure the location bar is still focused. |
| 300 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 300 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
| 301 } | 301 } |
| 302 | 302 |
| 303 // Focus traversal on a regular page. | 303 // Focus traversal on a regular page. |
| 304 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { | 304 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { |
| 305 HTTPTestServer* server = StartHTTPServer(); | 305 HTTPTestServer* server = StartHTTPServer(); |
| 306 | 306 |
| 307 // First we navigate to our test page. | 307 // First we navigate to our test page. |
| 308 GURL url = server->TestServerPageW(kTypicalPage); | 308 GURL url = server->TestServerPageW(kTypicalPage); |
| 309 ui_test_utils::NavigateToURL(browser(), url); | 309 ui_test_utils::NavigateToURL(browser(), url); |
| 310 | 310 |
| 311 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 311 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 312 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 312 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 313 views::FocusManager* focus_manager = | 313 views::FocusManager* focus_manager = |
| 314 views::FocusManager::GetFocusManager(hwnd); | 314 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 315 | 315 |
| 316 // Click on the location bar. | 316 // Click on the location bar. |
| 317 LocationBarView* location_bar = browser_view->GetLocationBarView(); | 317 LocationBarView* location_bar = browser_view->GetLocationBarView(); |
| 318 ui_controls::MoveMouseToCenterAndPress(location_bar, | 318 ui_controls::MoveMouseToCenterAndPress(location_bar, |
| 319 ui_controls::LEFT, | 319 ui_controls::LEFT, |
| 320 ui_controls::DOWN | ui_controls::UP, | 320 ui_controls::DOWN | ui_controls::UP, |
| 321 new MessageLoop::QuitTask()); | 321 new MessageLoop::QuitTask()); |
| 322 ui_test_utils::RunMessageLoop(); | 322 ui_test_utils::RunMessageLoop(); |
| 323 | 323 |
| 324 const char* kExpElementIDs[] = { | 324 const char* kExpElementIDs[] = { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) { | 394 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) { |
| 395 HTTPTestServer* server = StartHTTPServer(); | 395 HTTPTestServer* server = StartHTTPServer(); |
| 396 | 396 |
| 397 // First we navigate to our test page. | 397 // First we navigate to our test page. |
| 398 GURL url = server->TestServerPageW(kSimplePage); | 398 GURL url = server->TestServerPageW(kSimplePage); |
| 399 ui_test_utils::NavigateToURL(browser(), url); | 399 ui_test_utils::NavigateToURL(browser(), url); |
| 400 | 400 |
| 401 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 401 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 402 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 402 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 403 views::FocusManager* focus_manager = | 403 views::FocusManager* focus_manager = |
| 404 views::FocusManager::GetFocusManager(hwnd); | 404 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 405 | 405 |
| 406 // Focus should be on the page. | 406 // Focus should be on the page. |
| 407 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 407 EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
| 408 focus_manager->GetFocusedView()); | 408 focus_manager->GetFocusedView()); |
| 409 | 409 |
| 410 // Let's show an interstitial. | 410 // Let's show an interstitial. |
| 411 TestInterstitialPage* interstitial_page = | 411 TestInterstitialPage* interstitial_page = |
| 412 new TestInterstitialPage(browser()->GetSelectedTabContents(), | 412 new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 413 true, GURL("http://interstitial.com")); | 413 true, GURL("http://interstitial.com")); |
| 414 interstitial_page->Show(); | 414 interstitial_page->Show(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { | 489 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
| 490 HTTPTestServer* server = StartHTTPServer(); | 490 HTTPTestServer* server = StartHTTPServer(); |
| 491 | 491 |
| 492 // First we navigate to our test page. | 492 // First we navigate to our test page. |
| 493 GURL url = server->TestServerPageW(kSimplePage); | 493 GURL url = server->TestServerPageW(kSimplePage); |
| 494 ui_test_utils::NavigateToURL(browser(), url); | 494 ui_test_utils::NavigateToURL(browser(), url); |
| 495 | 495 |
| 496 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 496 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 497 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 497 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 498 views::FocusManager* focus_manager = | 498 views::FocusManager* focus_manager = |
| 499 views::FocusManager::GetFocusManager(hwnd); | 499 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 500 | 500 |
| 501 // Page should have focus. | 501 // Page should have focus. |
| 502 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 502 EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
| 503 focus_manager->GetFocusedView()); | 503 focus_manager->GetFocusedView()); |
| 504 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> | 504 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 505 HasFocus()); | 505 HasFocus()); |
| 506 | 506 |
| 507 // Let's show an interstitial.erstitial | 507 // Let's show an interstitial.erstitial |
| 508 TestInterstitialPage* interstitial_page = | 508 TestInterstitialPage* interstitial_page = |
| 509 new TestInterstitialPage(browser()->GetSelectedTabContents(), | 509 new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 534 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { | 534 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
| 535 HTTPTestServer* server = StartHTTPServer(); | 535 HTTPTestServer* server = StartHTTPServer(); |
| 536 | 536 |
| 537 // Open some page (any page that doesn't steal focus). | 537 // Open some page (any page that doesn't steal focus). |
| 538 GURL url = server->TestServerPageW(kTypicalPage); | 538 GURL url = server->TestServerPageW(kTypicalPage); |
| 539 ui_test_utils::NavigateToURL(browser(), url); | 539 ui_test_utils::NavigateToURL(browser(), url); |
| 540 | 540 |
| 541 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 541 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 542 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 542 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 543 views::FocusManager* focus_manager = | 543 views::FocusManager* focus_manager = |
| 544 views::FocusManager::GetFocusManager(hwnd); | 544 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 545 LocationBarView* location_bar = browser_view->GetLocationBarView(); | 545 LocationBarView* location_bar = browser_view->GetLocationBarView(); |
| 546 | 546 |
| 547 // Press Ctrl+F, which will make the Find box open and request focus. | 547 // Press Ctrl+F, which will make the Find box open and request focus. |
| 548 static const int VK_F = 0x46; | 548 static const int VK_F = 0x46; |
| 549 ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false, | 549 ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false, |
| 550 new MessageLoop::QuitTask()); | 550 new MessageLoop::QuitTask()); |
| 551 ui_test_utils::RunMessageLoop(); | 551 ui_test_utils::RunMessageLoop(); |
| 552 | 552 |
| 553 // Ideally, we wouldn't sleep here and instead would intercept the | 553 // Ideally, we wouldn't sleep here and instead would intercept the |
| 554 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we | 554 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); | 606 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); |
| 607 } | 607 } |
| 608 | 608 |
| 609 // Makes sure the focus is in the right location when opening the different | 609 // Makes sure the focus is in the right location when opening the different |
| 610 // types of tabs. | 610 // types of tabs. |
| 611 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { | 611 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
| 612 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 612 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 613 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 613 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| 614 ASSERT_TRUE(browser_view); | 614 ASSERT_TRUE(browser_view); |
| 615 views::FocusManager* focus_manager = | 615 views::FocusManager* focus_manager = |
| 616 views::FocusManager::GetFocusManager(hwnd); | 616 views::FocusManager::GetFocusManagerForNativeView(hwnd); |
| 617 ASSERT_TRUE(focus_manager); | 617 ASSERT_TRUE(focus_manager); |
| 618 | 618 |
| 619 // Open the history tab, focus should be on the tab contents. | 619 // Open the history tab, focus should be on the tab contents. |
| 620 browser()->ShowHistoryTab(); | 620 browser()->ShowHistoryTab(); |
| 621 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 621 EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
| 622 focus_manager->GetFocusedView()); | 622 focus_manager->GetFocusedView()); |
| 623 | 623 |
| 624 // Open the new tab, focus should be on the location bar. | 624 // Open the new tab, focus should be on the location bar. |
| 625 browser()->NewTab(); | 625 browser()->NewTab(); |
| 626 EXPECT_EQ(browser_view->GetLocationBarView(), | 626 EXPECT_EQ(browser_view->GetLocationBarView(), |
| 627 focus_manager->GetFocusedView()); | 627 focus_manager->GetFocusedView()); |
| 628 | 628 |
| 629 // Open the download tab, focus should be on the tab contents. | 629 // Open the download tab, focus should be on the tab contents. |
| 630 browser()->ShowDownloadsTab(); | 630 browser()->ShowDownloadsTab(); |
| 631 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 631 EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
| 632 focus_manager->GetFocusedView()); | 632 focus_manager->GetFocusedView()); |
| 633 } | 633 } |
| OLD | NEW |