| 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/renderer_host/render_widget_host_view.h" | 10 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 11 #include "chrome/browser/tab_contents/interstitial_page.h" | 11 #include "chrome/browser/tab_contents/interstitial_page.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 12 #include "chrome/browser/view_ids.h" | 14 #include "chrome/browser/view_ids.h" |
| 13 #include "chrome/browser/views/frame/browser_view.h" | 15 #include "chrome/browser/views/frame/browser_view.h" |
| 14 #include "chrome/browser/views/location_bar_view.h" | 16 #include "chrome/browser/views/location_bar_view.h" |
| 15 #include "chrome/browser/views/tab_contents/tab_contents_container.h" | 17 #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 16 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/test/in_process_browser_test.h" | 19 #include "chrome/test/in_process_browser_test.h" |
| 18 #include "chrome/test/ui_test_utils.h" | 20 #include "chrome/test/ui_test_utils.h" |
| 19 #include "views/focus/focus_manager.h" | 21 #include "views/focus/focus_manager.h" |
| 20 #include "views/view.h" | 22 #include "views/view.h" |
| 21 #include "views/window/window.h" | 23 #include "views/window/window.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 44 | 46 |
| 45 void CheckViewHasFocus(ViewID vid) { | 47 void CheckViewHasFocus(ViewID vid) { |
| 46 BrowserWindow* browser_window = browser()->window(); | 48 BrowserWindow* browser_window = browser()->window(); |
| 47 ASSERT_TRUE(browser_window); | 49 ASSERT_TRUE(browser_window); |
| 48 gfx::NativeWindow window = browser_window->GetNativeHandle(); | 50 gfx::NativeWindow window = browser_window->GetNativeHandle(); |
| 49 ASSERT_TRUE(window); | 51 ASSERT_TRUE(window); |
| 50 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 51 views::FocusManager* focus_manager = | 53 views::FocusManager* focus_manager = |
| 52 views::FocusManager::GetFocusManagerForNativeView(window); | 54 views::FocusManager::GetFocusManagerForNativeView(window); |
| 53 ASSERT_TRUE(focus_manager); | 55 ASSERT_TRUE(focus_manager); |
| 54 EXPECT_EQ(reinterpret_cast<views::View*>(browser_window)->GetViewByID(vid), | 56 EXPECT_EQ(reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid), |
| 55 focus_manager->GetFocusedView()); | 57 focus_manager->GetFocusedView()) << "For view id " << vid; |
| 56 #elif defined(OS_LINUX) | 58 #elif defined(OS_LINUX) |
| 57 GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); | 59 GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); |
| 58 ASSERT_TRUE(widget); | 60 ASSERT_TRUE(widget); |
| 59 EXPECT_TRUE(WidgetInFocusChain(GTK_WIDGET(window), widget)); | 61 EXPECT_TRUE(WidgetInFocusChain(GTK_WIDGET(window), widget)); |
| 60 #else | 62 #else |
| 61 NOTIMPLEMENTED(); | 63 NOTIMPLEMENTED(); |
| 62 #endif | 64 #endif |
| 63 } | 65 } |
| 64 | 66 |
| 67 static void HideNativeWindow(gfx::NativeWindow window) { |
| 68 #if defined(OS_WIN) |
| 69 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
| 70 // using Windows API. |
| 71 ::ShowWindow(window, SW_HIDE); |
| 72 #elif defined(OS_LINUX) |
| 73 gtk_widget_hide(GTK_WIDGET(window)); |
| 74 #else |
| 75 NOTIMPLEMENTED(); |
| 76 #endif |
| 77 } |
| 78 |
| 79 static void ShowNativeWindow(gfx::NativeWindow window) { |
| 80 #if defined(OS_WIN) |
| 81 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
| 82 // using Windows API. |
| 83 ::ShowWindow(window, SW_SHOW); |
| 84 #elif defined(OS_LINUX) |
| 85 gtk_widget_hide(GTK_WIDGET(window)); |
| 86 #else |
| 87 NOTIMPLEMENTED(); |
| 88 #endif |
| 89 } |
| 90 |
| 65 private: | 91 private: |
| 66 #if defined(OS_LINUX) | 92 #if defined(OS_LINUX) |
| 67 // Check if the focused widget for |root| is |target| or a child of |target|. | 93 // Check if the focused widget for |root| is |target| or a child of |target|. |
| 68 static bool WidgetInFocusChain(GtkWidget* root, GtkWidget* target) { | 94 static bool WidgetInFocusChain(GtkWidget* root, GtkWidget* target) { |
| 69 GtkWidget* iter = root; | 95 GtkWidget* iter = root; |
| 70 | 96 |
| 71 while (iter) { | 97 while (iter) { |
| 72 if (iter == target) | 98 if (iter == target) |
| 73 return true; | 99 return true; |
| 74 | 100 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 private: | 161 private: |
| 136 std::string html_contents_; | 162 std::string html_contents_; |
| 137 | 163 |
| 138 bool waiting_for_dom_response_; | 164 bool waiting_for_dom_response_; |
| 139 std::string dom_response_; | 165 std::string dom_response_; |
| 140 | 166 |
| 141 }; | 167 }; |
| 142 | 168 |
| 143 } // namespace | 169 } // namespace |
| 144 | 170 |
| 171 // TODO(estade): port. |
| 145 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
| 173 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
| 174 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
| 175 true, -1, false, NULL); |
| 176 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 177 |
| 178 BrowserView* browser_view = static_cast<BrowserView*>(browser()->window()); |
| 179 ui_controls::MoveMouseToCenterAndPress( |
| 180 browser_view->GetTabContentsContainerView(), |
| 181 ui_controls::LEFT, |
| 182 ui_controls::DOWN | ui_controls::UP, |
| 183 new MessageLoop::QuitTask()); |
| 184 ui_test_utils::RunMessageLoop(); |
| 185 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 186 |
| 187 ui_controls::MoveMouseToCenterAndPress( |
| 188 browser_view->GetLocationBarView(), |
| 189 ui_controls::LEFT, |
| 190 ui_controls::DOWN | ui_controls::UP, |
| 191 new MessageLoop::QuitTask()); |
| 192 ui_test_utils::RunMessageLoop(); |
| 193 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 194 } |
| 195 #endif |
| 196 |
| 146 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { | 197 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
| 147 HTTPTestServer* server = StartHTTPServer(); | 198 HTTPTestServer* server = StartHTTPServer(); |
| 148 | 199 |
| 149 // First we navigate to our test page. | 200 // First we navigate to our test page. |
| 150 GURL url = server->TestServerPageW(kSimplePage); | 201 GURL url = server->TestServerPageW(kSimplePage); |
| 151 ui_test_utils::NavigateToURL(browser(), url); | 202 ui_test_utils::NavigateToURL(browser(), url); |
| 152 | 203 |
| 204 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 205 |
| 153 // The focus should be on the Tab contents. | 206 // The focus should be on the Tab contents. |
| 154 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 207 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 155 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 208 // Now hide the window, show it again, the focus should not have changed. |
| 156 ASSERT_TRUE(browser_view); | 209 HideNativeWindow(window); |
| 157 views::FocusManager* focus_manager = | 210 ShowNativeWindow(window); |
| 158 views::FocusManager::GetFocusManagerForNativeView(hwnd); | 211 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 159 ASSERT_TRUE(focus_manager); | |
| 160 | 212 |
| 161 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 213 browser()->FocusLocationBar(); |
| 162 focus_manager->GetFocusedView()); | 214 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 215 // Hide the window, show it again, the focus should not have changed. |
| 216 HideNativeWindow(window); |
| 217 ShowNativeWindow(window); |
| 218 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 163 | 219 |
| 164 // Now hide the window, show it again, the focus should not have changed. | 220 // The rest of this test does not make sense on Linux because the behavior |
| 165 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of | 221 // of Activate() is not well defined and can vary by window manager. |
| 166 // using Windows API. | 222 #if defined(OS_WIN) |
| 167 ::ShowWindow(hwnd, SW_HIDE); | |
| 168 ::ShowWindow(hwnd, SW_SHOW); | |
| 169 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | |
| 170 focus_manager->GetFocusedView()); | |
| 171 | |
| 172 // Click on the location bar. | |
| 173 LocationBarView* location_bar = browser_view->GetLocationBarView(); | |
| 174 ui_controls::MoveMouseToCenterAndPress(location_bar, | |
| 175 ui_controls::LEFT, | |
| 176 ui_controls::DOWN | ui_controls::UP, | |
| 177 new MessageLoop::QuitTask()); | |
| 178 ui_test_utils::RunMessageLoop(); | |
| 179 // Location bar should have focus. | |
| 180 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | |
| 181 | |
| 182 // Hide the window, show it again, the focus should not have changed. | |
| 183 ::ShowWindow(hwnd, SW_HIDE); | |
| 184 ::ShowWindow(hwnd, SW_SHOW); | |
| 185 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | |
| 186 | |
| 187 // Open a new browser window. | 223 // Open a new browser window. |
| 188 Browser* browser2 = Browser::Create(browser()->profile()); | 224 Browser* browser2 = Browser::Create(browser()->profile()); |
| 189 ASSERT_TRUE(browser2); | 225 ASSERT_TRUE(browser2); |
| 190 browser2->tabstrip_model()->delegate()->AddBlankTab(true); | 226 browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
| 191 browser2->window()->Show(); | 227 browser2->window()->Show(); |
| 192 ui_test_utils::NavigateToURL(browser2, url); | 228 ui_test_utils::NavigateToURL(browser2, url); |
| 193 | 229 |
| 194 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); | 230 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
| 195 BrowserView* browser_view2 = | 231 BrowserView* browser_view2 = |
| 196 BrowserView::GetBrowserViewForNativeWindow(hwnd2); | 232 BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
| 197 ASSERT_TRUE(browser_view2); | 233 ASSERT_TRUE(browser_view2); |
| 198 views::FocusManager* focus_manager2 = | 234 views::FocusManager* focus_manager2 = |
| 199 views::FocusManager::GetFocusManagerForNativeView(hwnd2); | 235 views::FocusManager::GetFocusManagerForNativeView(hwnd2); |
| 200 ASSERT_TRUE(focus_manager2); | 236 ASSERT_TRUE(focus_manager2); |
| 201 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), | 237 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
| 202 focus_manager2->GetFocusedView()); | 238 focus_manager2->GetFocusedView()); |
| 203 | 239 |
| 204 // Switch to the 1st browser window, focus should still be on the location | 240 // Switch to the 1st browser window, focus should still be on the location |
| 205 // bar and the second browser should have nothing focused. | 241 // bar and the second browser should have nothing focused. |
| 206 browser()->window()->Activate(); | 242 browser()->window()->Activate(); |
| 207 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 243 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 208 EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); | 244 EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
| 209 | 245 |
| 210 // Switch back to the second browser, focus should still be on the page. | 246 // Switch back to the second browser, focus should still be on the page. |
| 211 browser2->window()->Activate(); | 247 browser2->window()->Activate(); |
| 212 EXPECT_EQ(NULL, focus_manager->GetFocusedView()); | 248 EXPECT_EQ(NULL, |
| 249 views::FocusManager::GetFocusManagerForNativeView( |
| 250 browser()->window()->GetNativeHandle())->GetFocusedView()); |
| 213 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), | 251 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
| 214 focus_manager2->GetFocusedView()); | 252 focus_manager2->GetFocusedView()); |
| 215 | 253 |
| 216 // Close the 2nd browser to avoid a DCHECK(). | 254 // Close the 2nd browser to avoid a DCHECK(). |
| 217 browser_view2->Close(); | 255 browser_view2->Close(); |
| 256 #endif |
| 218 } | 257 } |
| 219 | 258 |
| 220 // Tabs remember focus. | 259 // Tabs remember focus. |
| 221 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { | 260 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { |
| 222 HTTPTestServer* server = StartHTTPServer(); | 261 HTTPTestServer* server = StartHTTPServer(); |
| 223 | 262 |
| 224 // First we navigate to our test page. | 263 // First we navigate to our test page. |
| 225 GURL url = server->TestServerPageW(kSimplePage); | 264 GURL url = server->TestServerPageW(kSimplePage); |
| 226 ui_test_utils::NavigateToURL(browser(), url); | 265 ui_test_utils::NavigateToURL(browser(), url); |
| 227 | 266 |
| 228 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | |
| 229 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 230 ASSERT_TRUE(browser_view); | |
| 231 | |
| 232 views::FocusManager* focus_manager = | |
| 233 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 234 ASSERT_TRUE(focus_manager); | |
| 235 | |
| 236 // Create several tabs. | 267 // Create several tabs. |
| 237 for (int i = 0; i < 4; ++i) { | 268 for (int i = 0; i < 4; ++i) { |
| 238 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, | 269 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, |
| 239 false, NULL); | 270 false, NULL); |
| 240 } | 271 } |
| 241 | 272 |
| 242 // Alternate focus for the tab. | 273 // Alternate focus for the tab. |
| 243 const bool kFocusPage[3][5] = { | 274 const bool kFocusPage[3][5] = { |
| 244 { true, true, true, true, false }, | 275 { true, true, true, true, false }, |
| 245 { false, false, false, false, false }, | 276 { false, false, false, false, false }, |
| 246 { false, true, false, true, false } | 277 { false, true, false, true, false } |
| 247 }; | 278 }; |
| 248 | 279 |
| 249 for (int i = 1; i < 3; i++) { | 280 for (int i = 1; i < 3; i++) { |
| 250 for (int j = 0; j < 5; j++) { | 281 for (int j = 0; j < 5; j++) { |
| 251 // Activate the tab. | 282 // Activate the tab. |
| 252 browser()->SelectTabContentsAt(j, true); | 283 browser()->SelectTabContentsAt(j, true); |
| 253 | 284 |
| 254 // Activate the location bar or the page. | 285 // Activate the location bar or the page. |
| 255 views::View* view_to_focus; | |
| 256 if (kFocusPage[i][j]) { | 286 if (kFocusPage[i][j]) { |
| 257 view_to_focus = browser_view->GetTabContentsContainerView(); | 287 browser()->GetTabContentsAt(j)->view()->Focus(); |
| 258 } else { | 288 } else { |
| 259 view_to_focus = browser_view->GetLocationBarView(); | 289 browser()->FocusLocationBar(); |
| 260 } | 290 } |
| 261 | |
| 262 ui_controls::MoveMouseToCenterAndPress(view_to_focus, | |
| 263 ui_controls::LEFT, | |
| 264 ui_controls::DOWN | | |
| 265 ui_controls::UP, | |
| 266 new MessageLoop::QuitTask()); | |
| 267 ui_test_utils::RunMessageLoop(); | |
| 268 } | 291 } |
| 269 | 292 |
| 270 // Now come back to the tab and check the right view is focused. | 293 // Now come back to the tab and check the right view is focused. |
| 271 for (int j = 0; j < 5; j++) { | 294 for (int j = 0; j < 5; j++) { |
| 272 // Activate the tab. | 295 // Activate the tab. |
| 273 browser()->SelectTabContentsAt(j, true); | 296 browser()->SelectTabContentsAt(j, true); |
| 274 | 297 |
| 275 // Activate the location bar or the page. | 298 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 276 views::View* view; | 299 VIEW_ID_LOCATION_BAR; |
| 277 if (kFocusPage[i][j]) { | 300 CheckViewHasFocus(vid); |
| 278 view = browser_view->GetTabContentsContainerView(); | |
| 279 } else { | |
| 280 view = browser_view->GetLocationBarView(); | |
| 281 } | |
| 282 EXPECT_EQ(view, focus_manager->GetFocusedView()); | |
| 283 } | 301 } |
| 284 } | 302 } |
| 285 } | 303 } |
| 286 | 304 |
| 287 // Background window does not steal focus. | 305 // Background window does not steal focus. |
| 288 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { | 306 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { |
| 289 HTTPTestServer* server = StartHTTPServer(); | 307 HTTPTestServer* server = StartHTTPServer(); |
| 290 | 308 |
| 291 // First we navigate to our test page. | 309 // First we navigate to our test page. |
| 292 GURL url = server->TestServerPageW(kSimplePage); | 310 GURL url = server->TestServerPageW(kSimplePage); |
| 293 ui_test_utils::NavigateToURL(browser(), url); | 311 ui_test_utils::NavigateToURL(browser(), url); |
| 294 | 312 |
| 295 // Open a new browser window. | 313 // Open a new browser window. |
| 296 Browser* browser2 = Browser::Create(browser()->profile()); | 314 Browser* browser2 = Browser::Create(browser()->profile()); |
| 297 ASSERT_TRUE(browser2); | 315 ASSERT_TRUE(browser2); |
| 298 browser2->tabstrip_model()->delegate()->AddBlankTab(true); | 316 browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
| 299 browser2->window()->Show(); | 317 browser2->window()->Show(); |
| 318 |
| 319 Browser* focused_browser; |
| 320 Browser* unfocused_browser; |
| 321 #if defined(OS_LINUX) |
| 322 // On Linux, calling Activate() is not guaranteed to move focus, so we have |
| 323 // to figure out which browser does have focus. |
| 324 if (browser2->window()->IsActive()) { |
| 325 focused_browser = browser2; |
| 326 unfocused_browser = browser(); |
| 327 } else if (browser()->window()->IsActive()) { |
| 328 focused_browser = browser(); |
| 329 unfocused_browser = browser2; |
| 330 } else { |
| 331 ASSERT_TRUE(false); |
| 332 } |
| 333 #elif defined(OS_WIN) |
| 334 focused_browser = browser(); |
| 335 unfocused_browser = browser2; |
| 336 #endif |
| 337 |
| 300 GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); | 338 GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); |
| 301 ui_test_utils::NavigateToURL(browser2, steal_focus_url); | 339 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
| 302 | 340 |
| 303 // Activate the first browser. | 341 // Activate the first browser. |
| 304 browser()->window()->Activate(); | 342 focused_browser->window()->Activate(); |
| 305 | 343 |
| 306 // Wait for the focus to be stolen by the other browser. | 344 // Wait for the focus to be stolen by the other browser. |
| 307 ::Sleep(2000); | 345 PlatformThread::Sleep(2000); |
| 308 | 346 |
| 309 // Make sure the first browser is still active. | 347 // Make sure the first browser is still active. |
| 310 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 348 EXPECT_TRUE(focused_browser->window()->IsActive()); |
| 311 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 312 ASSERT_TRUE(browser_view); | |
| 313 EXPECT_TRUE(browser_view->frame()->GetWindow()->IsActive()); | |
| 314 | 349 |
| 315 // Close the 2nd browser to avoid a DCHECK(). | 350 // Close the 2nd browser to avoid a DCHECK(). |
| 316 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); | 351 browser2->window()->Close(); |
| 317 BrowserView* browser_view2 = | |
| 318 BrowserView::GetBrowserViewForNativeWindow(hwnd2); | |
| 319 browser_view2->Close(); | |
| 320 } | 352 } |
| 321 | 353 |
| 322 // Page cannot steal focus when focus is on location bar. | 354 // Page cannot steal focus when focus is on location bar. |
| 323 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { | 355 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
| 324 HTTPTestServer* server = StartHTTPServer(); | 356 HTTPTestServer* server = StartHTTPServer(); |
| 325 | 357 |
| 326 // Open the page that steals focus. | 358 // Open the page that steals focus. |
| 327 GURL url = server->TestServerPageW(kStealFocusPage); | 359 GURL url = server->TestServerPageW(kStealFocusPage); |
| 328 ui_test_utils::NavigateToURL(browser(), url); | 360 ui_test_utils::NavigateToURL(browser(), url); |
| 329 | 361 |
| 330 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 362 browser()->FocusLocationBar(); |
| 331 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 332 views::FocusManager* focus_manager = | |
| 333 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 334 | |
| 335 // Click on the location bar. | |
| 336 LocationBarView* location_bar = browser_view->GetLocationBarView(); | |
| 337 ui_controls::MoveMouseToCenterAndPress(location_bar, | |
| 338 ui_controls::LEFT, | |
| 339 ui_controls::DOWN | ui_controls::UP, | |
| 340 new MessageLoop::QuitTask()); | |
| 341 ui_test_utils::RunMessageLoop(); | |
| 342 | 363 |
| 343 // Wait for the page to steal focus. | 364 // Wait for the page to steal focus. |
| 344 ::Sleep(2000); | 365 PlatformThread::Sleep(2000); |
| 345 | 366 |
| 346 // Make sure the location bar is still focused. | 367 // Make sure the location bar is still focused. |
| 347 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 368 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 348 } | 369 } |
| 349 | 370 |
| 350 // Focus traversal on a regular page. | 371 // Focus traversal on a regular page. |
| 351 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { | 372 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { |
| 352 HTTPTestServer* server = StartHTTPServer(); | 373 HTTPTestServer* server = StartHTTPServer(); |
| 353 | 374 |
| 354 // First we navigate to our test page. | 375 // First we navigate to our test page. |
| 355 GURL url = server->TestServerPageW(kTypicalPage); | 376 GURL url = server->TestServerPageW(kTypicalPage); |
| 356 ui_test_utils::NavigateToURL(browser(), url); | 377 ui_test_utils::NavigateToURL(browser(), url); |
| 357 | 378 |
| 358 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 379 browser()->FocusLocationBar(); |
| 359 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | |
| 360 views::FocusManager* focus_manager = | |
| 361 views::FocusManager::GetFocusManagerForNativeView(hwnd); | |
| 362 | |
| 363 // Click on the location bar. | |
| 364 LocationBarView* location_bar = browser_view->GetLocationBarView(); | |
| 365 ui_controls::MoveMouseToCenterAndPress(location_bar, | |
| 366 ui_controls::LEFT, | |
| 367 ui_controls::DOWN | ui_controls::UP, | |
| 368 new MessageLoop::QuitTask()); | |
| 369 ui_test_utils::RunMessageLoop(); | |
| 370 | 380 |
| 371 const char* kExpElementIDs[] = { | 381 const char* kExpElementIDs[] = { |
| 372 "", // Initially no element in the page should be focused | 382 "", // Initially no element in the page should be focused |
| 373 // (the location bar is focused). | 383 // (the location bar is focused). |
| 374 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", | 384 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 375 "gmapLink" | 385 "gmapLink" |
| 376 }; | 386 }; |
| 377 | 387 |
| 388 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 389 |
| 378 // Test forward focus traversal. | 390 // Test forward focus traversal. |
| 379 for (int i = 0; i < 3; ++i) { | 391 for (int i = 0; i < 3; ++i) { |
| 380 // Location bar should be focused. | 392 // Location bar should be focused. |
| 381 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 393 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 382 | 394 |
| 383 // Now let's press tab to move the focus. | 395 // Now let's press tab to move the focus. |
| 384 for (int j = 0; j < 7; ++j) { | 396 for (int j = 0; j < 7; ++j) { |
| 385 // Let's make sure the focus is on the expected element in the page. | 397 // Let's make sure the focus is on the expected element in the page. |
| 386 std::string actual; | 398 std::string actual; |
| 387 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 399 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 388 browser()->GetSelectedTabContents()->render_view_host(), | 400 browser()->GetSelectedTabContents()->render_view_host(), |
| 389 L"", | 401 L"", |
| 390 L"window.domAutomationController.send(getFocusedElement());", | 402 L"window.domAutomationController.send(getFocusedElement());", |
| 391 &actual)); | 403 &actual)); |
| 392 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 404 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 393 | 405 |
| 394 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_TAB, false, | 406 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, false, |
| 395 false, false, | 407 false, false, |
| 396 new MessageLoop::QuitTask()); | 408 new MessageLoop::QuitTask()); |
| 397 ui_test_utils::RunMessageLoop(); | 409 ui_test_utils::RunMessageLoop(); |
| 398 // Ideally, we wouldn't sleep here and instead would use the event | 410 // Ideally, we wouldn't sleep here and instead would use the event |
| 399 // processed ack notification from the renderer. I am reluctant to create | 411 // processed ack notification from the renderer. I am reluctant to create |
| 400 // a new notification/callback for that purpose just for this test. | 412 // a new notification/callback for that purpose just for this test. |
| 401 ::Sleep(kActionDelayMs); | 413 PlatformThread::Sleep(kActionDelayMs); |
| 402 } | 414 } |
| 403 | 415 |
| 404 // At this point the renderer has sent us a message asking to advance the | 416 // At this point the renderer has sent us a message asking to advance the |
| 405 // focus (as the end of the focus loop was reached in the renderer). | 417 // focus (as the end of the focus loop was reached in the renderer). |
| 406 // We need to run the message loop to process it. | 418 // We need to run the message loop to process it. |
| 407 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 419 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 408 ui_test_utils::RunMessageLoop(); | 420 ui_test_utils::RunMessageLoop(); |
| 409 } | 421 } |
| 410 | 422 |
| 411 // Now let's try reverse focus traversal. | 423 // Now let's try reverse focus traversal. |
| 412 for (int i = 0; i < 3; ++i) { | 424 for (int i = 0; i < 3; ++i) { |
| 413 // Location bar should be focused. | 425 // Location bar should be focused. |
| 414 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 426 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 415 | 427 |
| 416 // Now let's press shift-tab to move the focus in reverse. | 428 // Now let's press shift-tab to move the focus in reverse. |
| 417 for (int j = 0; j < 7; ++j) { | 429 for (int j = 0; j < 7; ++j) { |
| 418 ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_TAB, false, | 430 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, false, |
| 419 true, false, | 431 true, false, |
| 420 new MessageLoop::QuitTask()); | 432 new MessageLoop::QuitTask()); |
| 421 ui_test_utils::RunMessageLoop(); | 433 ui_test_utils::RunMessageLoop(); |
| 422 ::Sleep(kActionDelayMs); | 434 PlatformThread::Sleep(kActionDelayMs); |
| 423 | 435 |
| 424 // Let's make sure the focus is on the expected element in the page. | 436 // Let's make sure the focus is on the expected element in the page. |
| 425 std::string actual; | 437 std::string actual; |
| 426 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 438 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 427 browser()->GetSelectedTabContents()->render_view_host(), | 439 browser()->GetSelectedTabContents()->render_view_host(), |
| 428 L"", | 440 L"", |
| 429 L"window.domAutomationController.send(getFocusedElement());", | 441 L"window.domAutomationController.send(getFocusedElement());", |
| 430 &actual)); | 442 &actual)); |
| 431 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 443 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 432 } | 444 } |
| 433 | 445 |
| 434 // At this point the renderer has sent us a message asking to advance the | 446 // At this point the renderer has sent us a message asking to advance the |
| 435 // focus (as the end of the focus loop was reached in the renderer). | 447 // focus (as the end of the focus loop was reached in the renderer). |
| 436 // We need to run the message loop to process it. | 448 // We need to run the message loop to process it. |
| 437 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 449 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 438 ui_test_utils::RunMessageLoop(); | 450 ui_test_utils::RunMessageLoop(); |
| 439 } | 451 } |
| 440 } | 452 } |
| 441 | 453 |
| 454 #if defined(OS_WIN) |
| 442 // Focus traversal while an interstitial is showing. | 455 // Focus traversal while an interstitial is showing. |
| 443 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) { | 456 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) { |
| 444 HTTPTestServer* server = StartHTTPServer(); | 457 HTTPTestServer* server = StartHTTPServer(); |
| 445 | 458 |
| 446 // First we navigate to our test page. | 459 // First we navigate to our test page. |
| 447 GURL url = server->TestServerPageW(kSimplePage); | 460 GURL url = server->TestServerPageW(kSimplePage); |
| 448 ui_test_utils::NavigateToURL(browser(), url); | 461 ui_test_utils::NavigateToURL(browser(), url); |
| 449 | 462 |
| 450 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); | 463 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
| 451 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); | 464 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 | 668 |
| 656 focused_view = focus_manager->GetFocusedView(); | 669 focused_view = focus_manager->GetFocusedView(); |
| 657 ASSERT_TRUE(focused_view != NULL); | 670 ASSERT_TRUE(focused_view != NULL); |
| 658 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); | 671 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID()); |
| 659 } | 672 } |
| 660 #endif // defined(OS_WIN) | 673 #endif // defined(OS_WIN) |
| 661 | 674 |
| 662 // Makes sure the focus is in the right location when opening the different | 675 // Makes sure the focus is in the right location when opening the different |
| 663 // types of tabs. | 676 // types of tabs. |
| 664 // TODO(estade): undisable this. | 677 // TODO(estade): undisable this. |
| 665 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) { | 678 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
| 666 // Open the history tab, focus should be on the tab contents. | 679 // Open the history tab, focus should be on the tab contents. |
| 667 browser()->ShowHistoryTab(); | 680 browser()->ShowHistoryTab(); |
| 668 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER); | 681 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 669 | 682 |
| 670 // Open the new tab, focus should be on the location bar. | 683 // Open the new tab, focus should be on the location bar. |
| 671 browser()->NewTab(); | 684 browser()->NewTab(); |
| 672 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); | 685 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 673 | 686 |
| 674 // Open the download tab, focus should be on the tab contents. | 687 // Open the download tab, focus should be on the tab contents. |
| 675 browser()->ShowDownloadsTab(); | 688 browser()->ShowDownloadsTab(); |
| 676 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER); | 689 CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
| 677 | 690 |
| 678 // Open about:blank, focus should be on the location bar. | 691 // Open about:blank, focus should be on the location bar. |
| 679 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, | 692 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
| 680 true, -1, false, NULL); | 693 true, -1, false, NULL); |
| 681 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); | 694 CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
| 682 } | 695 } |
| 683 | 696 |
| 684 #if defined(OS_WIN) | 697 #if defined(OS_WIN) |
| 685 // Tests that focus goes where expected when using reload. | 698 // Tests that focus goes where expected when using reload. |
| 686 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { | 699 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 // Open a regular page, crash, reload. | 740 // Open a regular page, crash, reload. |
| 728 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); | 741 ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 729 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); | 742 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
| 730 browser()->Reload(); | 743 browser()->Reload(); |
| 731 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 744 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 732 // Focus should now be on the tab contents. | 745 // Focus should now be on the tab contents. |
| 733 EXPECT_EQ(browser_view->GetTabContentsContainerView(), | 746 EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
| 734 focus_manager->GetFocusedView()); | 747 focus_manager->GetFocusedView()); |
| 735 } | 748 } |
| 736 #endif // defined(OS_WIN) | 749 #endif // defined(OS_WIN) |
| OLD | NEW |