| 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/logging.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| 10 #include "chrome/browser/automation/ui_controls.h" | 11 #include "chrome/browser/automation/ui_controls.h" |
| 11 #include "chrome/browser/browser.h" | 12 #include "chrome/browser/browser.h" |
| 12 #include "chrome/browser/browser_window.h" | 13 #include "chrome/browser/browser_window.h" |
| 13 #include "chrome/browser/renderer_host/render_view_host.h" | 14 #include "chrome/browser/renderer_host/render_view_host.h" |
| 14 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 15 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 15 #include "chrome/browser/tab_contents/interstitial_page.h" | 16 #include "chrome/browser/tab_contents/interstitial_page.h" |
| 16 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
| 17 #include "chrome/browser/tab_contents/tab_contents_view.h" | 18 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 18 #include "chrome/browser/view_ids.h" | 19 #include "chrome/browser/view_ids.h" |
| 19 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/test/in_process_browser_test.h" | 21 #include "chrome/test/in_process_browser_test.h" |
| 21 #include "chrome/test/ui_test_utils.h" | 22 #include "chrome/test/ui_test_utils.h" |
| 23 |
| 24 #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) |
| 22 #include "views/focus/focus_manager.h" | 25 #include "views/focus/focus_manager.h" |
| 23 #include "views/view.h" | 26 #include "views/view.h" |
| 24 #include "views/window/window.h" | 27 #include "views/window/window.h" |
| 28 #endif |
| 25 | 29 |
| 26 #if defined(TOOLKIT_VIEWS) | 30 #if defined(TOOLKIT_VIEWS) |
| 27 #include "chrome/browser/views/frame/browser_view.h" | 31 #include "chrome/browser/views/frame/browser_view.h" |
| 28 #include "chrome/browser/views/location_bar/location_bar_view.h" | 32 #include "chrome/browser/views/location_bar/location_bar_view.h" |
| 29 #include "chrome/browser/views/tab_contents/tab_contents_container.h" | 33 #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 30 #endif | 34 #endif |
| 31 | 35 |
| 32 #if defined(TOOLKIT_USES_GTK) | 36 #if defined(TOOLKIT_USES_GTK) |
| 33 #include "chrome/browser/gtk/view_id_util.h" | 37 #include "chrome/browser/gtk/view_id_util.h" |
| 34 #endif | 38 #endif |
| 35 | 39 |
| 36 #if defined(OS_LINUX) | 40 #if defined(OS_LINUX) |
| 41 #define MAYBE_FocusTraversal FocusTraversal |
| 37 // For some reason we hit an external DNS lookup in this test in Linux but not | 42 // For some reason we hit an external DNS lookup in this test in Linux but not |
| 38 // on Windows. TODO(estade): investigate. | 43 // on Windows. TODO(estade): investigate. |
| 39 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial | 44 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
| 40 // TODO(jcampan): http://crbug.com/23683 | 45 // TODO(jcampan): http://crbug.com/23683 |
| 41 #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage | 46 #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
| 42 #else | 47 #elif defined(OS_MACOSX) |
| 48 // TODO(suzhe): http://crbug.com/49738 (following two tests) |
| 49 #define MAYBE_FocusTraversal FAILS_FocusTraversal |
| 50 #define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial |
| 51 // TODO(suzhe): http://crbug.com/49737 |
| 52 #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
| 53 #elif defined(OS_WIN) |
| 54 #define MAYBE_FocusTraversal FocusTraversal |
| 43 #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial | 55 #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
| 44 #define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage | 56 #define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage |
| 45 #endif | 57 #endif |
| 46 | 58 |
| 47 namespace { | 59 namespace { |
| 48 | 60 |
| 49 // The delay waited in some cases where we don't have a notifications for an | 61 // The delay waited in some cases where we don't have a notifications for an |
| 50 // action we take. | 62 // action we take. |
| 51 const int kActionDelayMs = 500; | 63 const int kActionDelayMs = 500; |
| 52 | 64 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 63 } | 75 } |
| 64 | 76 |
| 65 bool IsViewFocused(ViewID vid) { | 77 bool IsViewFocused(ViewID vid) { |
| 66 return ui_test_utils::IsViewFocused(browser(), vid); | 78 return ui_test_utils::IsViewFocused(browser(), vid); |
| 67 } | 79 } |
| 68 | 80 |
| 69 void ClickOnView(ViewID vid) { | 81 void ClickOnView(ViewID vid) { |
| 70 ui_test_utils::ClickOnView(browser(), vid); | 82 ui_test_utils::ClickOnView(browser(), vid); |
| 71 } | 83 } |
| 72 | 84 |
| 73 static void HideNativeWindow(gfx::NativeWindow window) { | 85 void BringBrowserWindowToFront() { |
| 74 #if defined(OS_WIN) | 86 ui_test_utils::ShowAndFocusNativeWindow( |
| 75 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of | 87 browser()->window()->GetNativeHandle()); |
| 76 // using Windows API. | |
| 77 ::ShowWindow(window, SW_HIDE); | |
| 78 #elif defined(TOOLKIT_USES_GTK) | |
| 79 gtk_widget_hide(GTK_WIDGET(window)); | |
| 80 #else | |
| 81 NOTIMPLEMENTED(); | |
| 82 #endif | |
| 83 } | |
| 84 | |
| 85 static void ShowNativeWindow(gfx::NativeWindow window) { | |
| 86 #if defined(OS_WIN) | |
| 87 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of | |
| 88 // using Windows API. | |
| 89 ::ShowWindow(window, SW_SHOW); | |
| 90 #elif defined(TOOLKIT_USES_GTK) | |
| 91 gtk_widget_hide(GTK_WIDGET(window)); | |
| 92 #else | |
| 93 NOTIMPLEMENTED(); | |
| 94 #endif | |
| 95 } | 88 } |
| 96 }; | 89 }; |
| 97 | 90 |
| 98 class TestInterstitialPage : public InterstitialPage { | 91 class TestInterstitialPage : public InterstitialPage { |
| 99 public: | 92 public: |
| 100 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) | 93 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) |
| 101 : InterstitialPage(tab, new_navigation, url), | 94 : InterstitialPage(tab, new_navigation, url), |
| 102 waiting_for_dom_response_(false), | 95 waiting_for_dom_response_(false), |
| 103 waiting_for_focus_change_(false) { | 96 waiting_for_focus_change_(false) { |
| 104 FilePath file_path; | 97 FilePath file_path; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 std::string html_contents_; | 156 std::string html_contents_; |
| 164 | 157 |
| 165 bool waiting_for_dom_response_; | 158 bool waiting_for_dom_response_; |
| 166 bool waiting_for_focus_change_; | 159 bool waiting_for_focus_change_; |
| 167 std::string dom_response_; | 160 std::string dom_response_; |
| 168 }; | 161 }; |
| 169 | 162 |
| 170 } // namespace | 163 } // namespace |
| 171 | 164 |
| 172 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { | 165 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
| 173 #if defined(USE_X11) | 166 BringBrowserWindowToFront(); |
| 167 #if defined(USE_X11) || defined(OS_MACOSX) |
| 174 // It seems we have to wait a little bit for the widgets to spin up before | 168 // It seems we have to wait a little bit for the widgets to spin up before |
| 175 // we can start clicking on them. | 169 // we can start clicking on them. |
| 176 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 170 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 177 new MessageLoop::QuitTask(), | 171 new MessageLoop::QuitTask(), |
| 178 kActionDelayMs); | 172 kActionDelayMs); |
| 179 ui_test_utils::RunMessageLoop(); | 173 ui_test_utils::RunMessageLoop(); |
| 180 #endif | 174 #endif |
| 181 | 175 |
| 182 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 176 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 183 | 177 |
| 184 ClickOnView(VIEW_ID_TAB_CONTAINER); | 178 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 185 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 179 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 186 | 180 |
| 187 ClickOnView(VIEW_ID_LOCATION_BAR); | 181 ClickOnView(VIEW_ID_LOCATION_BAR); |
| 188 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 182 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 189 } | 183 } |
| 190 | 184 |
| 191 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { | 185 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
| 186 BringBrowserWindowToFront(); |
| 192 HTTPTestServer* server = StartHTTPServer(); | 187 HTTPTestServer* server = StartHTTPServer(); |
| 188 ASSERT_TRUE(server); |
| 193 | 189 |
| 194 // First we navigate to our test page. | 190 // First we navigate to our test page. |
| 195 GURL url = server->TestServerPage(kSimplePage); | 191 GURL url = server->TestServerPage(kSimplePage); |
| 196 ui_test_utils::NavigateToURL(browser(), url); | 192 ui_test_utils::NavigateToURL(browser(), url); |
| 197 | 193 |
| 198 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 194 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 199 | 195 |
| 200 // The focus should be on the Tab contents. | 196 // The focus should be on the Tab contents. |
| 201 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 197 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 202 // Now hide the window, show it again, the focus should not have changed. | 198 // Now hide the window, show it again, the focus should not have changed. |
| 203 HideNativeWindow(window); | 199 ui_test_utils::HideNativeWindow(window); |
| 204 ShowNativeWindow(window); | 200 ui_test_utils::ShowAndFocusNativeWindow(window); |
| 205 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 201 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 206 | 202 |
| 207 browser()->FocusLocationBar(); | 203 browser()->FocusLocationBar(); |
| 208 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 204 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 209 // Hide the window, show it again, the focus should not have changed. | 205 // Hide the window, show it again, the focus should not have changed. |
| 210 HideNativeWindow(window); | 206 ui_test_utils::HideNativeWindow(window); |
| 211 ShowNativeWindow(window); | 207 ui_test_utils::ShowAndFocusNativeWindow(window); |
| 212 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 208 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 213 | 209 |
| 214 // The rest of this test does not make sense on Linux because the behavior | 210 // The rest of this test does not make sense on Linux because the behavior |
| 215 // of Activate() is not well defined and can vary by window manager. | 211 // of Activate() is not well defined and can vary by window manager. |
| 216 #if defined(OS_WIN) | 212 #if defined(OS_WIN) |
| 217 // Open a new browser window. | 213 // Open a new browser window. |
| 218 Browser* browser2 = Browser::Create(browser()->profile()); | 214 Browser* browser2 = Browser::Create(browser()->profile()); |
| 219 ASSERT_TRUE(browser2); | 215 ASSERT_TRUE(browser2); |
| 220 browser2->tabstrip_model()->delegate()->AddBlankTab(true); | 216 browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
| 221 browser2->window()->Show(); | 217 browser2->window()->Show(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 245 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), | 241 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
| 246 focus_manager2->GetFocusedView()); | 242 focus_manager2->GetFocusedView()); |
| 247 | 243 |
| 248 // Close the 2nd browser to avoid a DCHECK(). | 244 // Close the 2nd browser to avoid a DCHECK(). |
| 249 browser_view2->Close(); | 245 browser_view2->Close(); |
| 250 #endif | 246 #endif |
| 251 } | 247 } |
| 252 | 248 |
| 253 // Tabs remember focus. | 249 // Tabs remember focus. |
| 254 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { | 250 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { |
| 251 BringBrowserWindowToFront(); |
| 255 HTTPTestServer* server = StartHTTPServer(); | 252 HTTPTestServer* server = StartHTTPServer(); |
| 253 ASSERT_TRUE(server); |
| 256 | 254 |
| 257 // First we navigate to our test page. | 255 // First we navigate to our test page. |
| 258 GURL url = server->TestServerPage(kSimplePage); | 256 GURL url = server->TestServerPage(kSimplePage); |
| 259 ui_test_utils::NavigateToURL(browser(), url); | 257 ui_test_utils::NavigateToURL(browser(), url); |
| 260 | 258 |
| 261 // Create several tabs. | 259 // Create several tabs. |
| 262 for (int i = 0; i < 4; ++i) { | 260 for (int i = 0; i < 4; ++i) { |
| 263 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1, | 261 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1, |
| 264 TabStripModel::ADD_SELECTED, NULL, std::string()); | 262 TabStripModel::ADD_SELECTED, NULL, std::string()); |
| 265 } | 263 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, | 317 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, |
| 320 true, false, false, | 318 true, false, false, |
| 321 new MessageLoop::QuitTask()); | 319 new MessageLoop::QuitTask()); |
| 322 ui_test_utils::RunMessageLoop(); | 320 ui_test_utils::RunMessageLoop(); |
| 323 } | 321 } |
| 324 } | 322 } |
| 325 } | 323 } |
| 326 | 324 |
| 327 // Tabs remember focus with find-in-page box. | 325 // Tabs remember focus with find-in-page box. |
| 328 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { | 326 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
| 327 BringBrowserWindowToFront(); |
| 329 HTTPTestServer* server = StartHTTPServer(); | 328 HTTPTestServer* server = StartHTTPServer(); |
| 329 ASSERT_TRUE(server); |
| 330 | 330 |
| 331 // First we navigate to our test page. | 331 // First we navigate to our test page. |
| 332 GURL url = server->TestServerPage(kSimplePage); | 332 GURL url = server->TestServerPage(kSimplePage); |
| 333 ui_test_utils::NavigateToURL(browser(), url); | 333 ui_test_utils::NavigateToURL(browser(), url); |
| 334 | 334 |
| 335 browser()->Find(); | 335 browser()->Find(); |
| 336 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), | 336 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
| 337 ASCIIToUTF16("a"), true, false, NULL); | 337 ASCIIToUTF16("a"), true, false, NULL); |
| 338 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 338 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 339 | 339 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 357 browser()->Find(); | 357 browser()->Find(); |
| 358 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 358 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 359 browser()->SelectTabContentsAt(1, true); | 359 browser()->SelectTabContentsAt(1, true); |
| 360 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 360 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 361 browser()->SelectTabContentsAt(0, true); | 361 browser()->SelectTabContentsAt(0, true); |
| 362 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 362 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 363 } | 363 } |
| 364 | 364 |
| 365 // Background window does not steal focus. | 365 // Background window does not steal focus. |
| 366 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { | 366 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { |
| 367 BringBrowserWindowToFront(); |
| 367 HTTPTestServer* server = StartHTTPServer(); | 368 HTTPTestServer* server = StartHTTPServer(); |
| 369 ASSERT_TRUE(server); |
| 368 | 370 |
| 369 // First we navigate to our test page. | 371 // First we navigate to our test page. |
| 370 GURL url = server->TestServerPage(kSimplePage); | 372 GURL url = server->TestServerPage(kSimplePage); |
| 371 ui_test_utils::NavigateToURL(browser(), url); | 373 ui_test_utils::NavigateToURL(browser(), url); |
| 372 | 374 |
| 373 // Open a new browser window. | 375 // Open a new browser window. |
| 374 Browser* browser2 = Browser::Create(browser()->profile()); | 376 Browser* browser2 = Browser::Create(browser()->profile()); |
| 375 ASSERT_TRUE(browser2); | 377 ASSERT_TRUE(browser2); |
| 376 browser2->tabstrip_model()->delegate()->AddBlankTab(true); | 378 browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
| 377 browser2->window()->Show(); | 379 browser2->window()->Show(); |
| 378 | 380 |
| 379 Browser* focused_browser = NULL; | 381 Browser* focused_browser = NULL; |
| 380 Browser* unfocused_browser = NULL; | 382 Browser* unfocused_browser = NULL; |
| 381 #if defined(USE_X11) | 383 #if defined(USE_X11) |
| 382 // On X11, calling Activate() is not guaranteed to move focus, so we have | 384 // On X11, calling Activate() is not guaranteed to move focus, so we have |
| 383 // to figure out which browser does have focus. | 385 // to figure out which browser does have focus. |
| 384 if (browser2->window()->IsActive()) { | 386 if (browser2->window()->IsActive()) { |
| 385 focused_browser = browser2; | 387 focused_browser = browser2; |
| 386 unfocused_browser = browser(); | 388 unfocused_browser = browser(); |
| 387 } else if (browser()->window()->IsActive()) { | 389 } else if (browser()->window()->IsActive()) { |
| 388 focused_browser = browser(); | 390 focused_browser = browser(); |
| 389 unfocused_browser = browser2; | 391 unfocused_browser = browser2; |
| 390 } else { | 392 } else { |
| 391 ASSERT_TRUE(false); | 393 ASSERT_TRUE(false); |
| 392 } | 394 } |
| 393 #elif defined(OS_WIN) | 395 #elif defined(OS_WIN) |
| 394 focused_browser = browser(); | 396 focused_browser = browser(); |
| 395 unfocused_browser = browser2; | 397 unfocused_browser = browser2; |
| 398 #elif defined(OS_MACOSX) |
| 399 // On Mac, the newly created window always gets the focus. |
| 400 focused_browser = browser2; |
| 401 unfocused_browser = browser(); |
| 396 #endif | 402 #endif |
| 397 | 403 |
| 398 GURL steal_focus_url = server->TestServerPage(kStealFocusPage); | 404 GURL steal_focus_url = server->TestServerPage(kStealFocusPage); |
| 399 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); | 405 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
| 400 | 406 |
| 401 // Activate the first browser. | 407 // Activate the first browser. |
| 402 focused_browser->window()->Activate(); | 408 focused_browser->window()->Activate(); |
| 403 | 409 |
| 404 // Wait for the focus to be stolen by the other browser. | 410 // Wait for the focus to be stolen by the other browser. |
| 405 MessageLoop::current()->PostDelayedTask( | 411 MessageLoop::current()->PostDelayedTask( |
| 406 FROM_HERE, new MessageLoop::QuitTask(), 2000); | 412 FROM_HERE, new MessageLoop::QuitTask(), 2000); |
| 407 ui_test_utils::RunMessageLoop(); | 413 ui_test_utils::RunMessageLoop(); |
| 408 | 414 |
| 409 // Make sure the first browser is still active. | 415 // Make sure the first browser is still active. |
| 410 EXPECT_TRUE(focused_browser->window()->IsActive()); | 416 EXPECT_TRUE(focused_browser->window()->IsActive()); |
| 411 | 417 |
| 412 // Close the 2nd browser to avoid a DCHECK(). | 418 // Close the 2nd browser to avoid a DCHECK(). |
| 413 browser2->window()->Close(); | 419 browser2->window()->Close(); |
| 414 } | 420 } |
| 415 | 421 |
| 416 // Page cannot steal focus when focus is on location bar. | 422 // Page cannot steal focus when focus is on location bar. |
| 417 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { | 423 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
| 424 BringBrowserWindowToFront(); |
| 418 HTTPTestServer* server = StartHTTPServer(); | 425 HTTPTestServer* server = StartHTTPServer(); |
| 426 ASSERT_TRUE(server); |
| 419 | 427 |
| 420 // Open the page that steals focus. | 428 // Open the page that steals focus. |
| 421 GURL url = server->TestServerPage(kStealFocusPage); | 429 GURL url = server->TestServerPage(kStealFocusPage); |
| 422 ui_test_utils::NavigateToURL(browser(), url); | 430 ui_test_utils::NavigateToURL(browser(), url); |
| 423 | 431 |
| 424 browser()->FocusLocationBar(); | 432 browser()->FocusLocationBar(); |
| 425 | 433 |
| 426 // Wait for the page to steal focus. | 434 // Wait for the page to steal focus. |
| 427 PlatformThread::Sleep(2000); | 435 PlatformThread::Sleep(2000); |
| 428 | 436 |
| 429 // Make sure the location bar is still focused. | 437 // Make sure the location bar is still focused. |
| 430 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 438 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 431 } | 439 } |
| 432 | 440 |
| 433 // Focus traversal on a regular page. | 441 // Focus traversal on a regular page. |
| 434 // Note that this test relies on a notification from the renderer that the | 442 // Note that this test relies on a notification from the renderer that the |
| 435 // focus has changed in the page. The notification in the renderer may change | 443 // focus has changed in the page. The notification in the renderer may change |
| 436 // at which point this test would fail (see comment in | 444 // at which point this test would fail (see comment in |
| 437 // RenderWidget::didFocus()). | 445 // RenderWidget::didFocus()). |
| 438 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { | 446 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
| 447 BringBrowserWindowToFront(); |
| 439 HTTPTestServer* server = StartHTTPServer(); | 448 HTTPTestServer* server = StartHTTPServer(); |
| 449 ASSERT_TRUE(server); |
| 440 | 450 |
| 441 // First we navigate to our test page. | 451 // First we navigate to our test page. |
| 442 GURL url = server->TestServerPage(kTypicalPage); | 452 GURL url = server->TestServerPage(kTypicalPage); |
| 443 ui_test_utils::NavigateToURL(browser(), url); | 453 ui_test_utils::NavigateToURL(browser(), url); |
| 444 | 454 |
| 445 browser()->FocusLocationBar(); | 455 browser()->FocusLocationBar(); |
| 446 | 456 |
| 447 const char* kExpElementIDs[] = { | 457 const char* kExpElementIDs[] = { |
| 448 "", // Initially no element in the page should be focused | 458 "", // Initially no element in the page should be focused |
| 449 // (the location bar is focused). | 459 // (the location bar is focused). |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 | 531 |
| 522 // At this point the renderer has sent us a message asking to advance the | 532 // At this point the renderer has sent us a message asking to advance the |
| 523 // focus (as the end of the focus loop was reached in the renderer). | 533 // focus (as the end of the focus loop was reached in the renderer). |
| 524 // We need to run the message loop to process it. | 534 // We need to run the message loop to process it. |
| 525 ui_test_utils::RunAllPendingInMessageLoop(); | 535 ui_test_utils::RunAllPendingInMessageLoop(); |
| 526 } | 536 } |
| 527 } | 537 } |
| 528 | 538 |
| 529 // Focus traversal while an interstitial is showing. | 539 // Focus traversal while an interstitial is showing. |
| 530 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { | 540 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
| 541 BringBrowserWindowToFront(); |
| 531 HTTPTestServer* server = StartHTTPServer(); | 542 HTTPTestServer* server = StartHTTPServer(); |
| 543 ASSERT_TRUE(server); |
| 532 | 544 |
| 533 // First we navigate to our test page. | 545 // First we navigate to our test page. |
| 534 GURL url = server->TestServerPage(kSimplePage); | 546 GURL url = server->TestServerPage(kSimplePage); |
| 535 ui_test_utils::NavigateToURL(browser(), url); | 547 ui_test_utils::NavigateToURL(browser(), url); |
| 536 | 548 |
| 537 // Focus should be on the page. | 549 // Focus should be on the page. |
| 538 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 550 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 539 | 551 |
| 540 // Let's show an interstitial. | 552 // Let's show an interstitial. |
| 541 TestInterstitialPage* interstitial_page = | 553 TestInterstitialPage* interstitial_page = |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 | 623 |
| 612 // At this point the renderer has sent us a message asking to advance the | 624 // At this point the renderer has sent us a message asking to advance the |
| 613 // focus (as the end of the focus loop was reached in the renderer). | 625 // focus (as the end of the focus loop was reached in the renderer). |
| 614 // We need to run the message loop to process it. | 626 // We need to run the message loop to process it. |
| 615 ui_test_utils::RunAllPendingInMessageLoop(); | 627 ui_test_utils::RunAllPendingInMessageLoop(); |
| 616 } | 628 } |
| 617 } | 629 } |
| 618 | 630 |
| 619 // Focus stays on page with interstitials. | 631 // Focus stays on page with interstitials. |
| 620 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { | 632 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
| 633 BringBrowserWindowToFront(); |
| 621 HTTPTestServer* server = StartHTTPServer(); | 634 HTTPTestServer* server = StartHTTPServer(); |
| 635 ASSERT_TRUE(server); |
| 622 | 636 |
| 623 // First we navigate to our test page. | 637 // First we navigate to our test page. |
| 624 GURL url = server->TestServerPage(kSimplePage); | 638 GURL url = server->TestServerPage(kSimplePage); |
| 625 ui_test_utils::NavigateToURL(browser(), url); | 639 ui_test_utils::NavigateToURL(browser(), url); |
| 626 | 640 |
| 627 // Page should have focus. | 641 // Page should have focus. |
| 628 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 642 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 629 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> | 643 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 630 HasFocus()); | 644 HasFocus()); |
| 631 | 645 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 646 | 660 |
| 647 // Hide the interstitial. | 661 // Hide the interstitial. |
| 648 interstitial_page->DontProceed(); | 662 interstitial_page->DontProceed(); |
| 649 | 663 |
| 650 // Focus should be back on the original page. | 664 // Focus should be back on the original page. |
| 651 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 665 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 652 } | 666 } |
| 653 | 667 |
| 654 // Make sure Find box can request focus, even when it is already open. | 668 // Make sure Find box can request focus, even when it is already open. |
| 655 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { | 669 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
| 670 BringBrowserWindowToFront(); |
| 656 HTTPTestServer* server = StartHTTPServer(); | 671 HTTPTestServer* server = StartHTTPServer(); |
| 672 ASSERT_TRUE(server); |
| 657 | 673 |
| 658 // Open some page (any page that doesn't steal focus). | 674 // Open some page (any page that doesn't steal focus). |
| 659 GURL url = server->TestServerPage(kTypicalPage); | 675 GURL url = server->TestServerPage(kTypicalPage); |
| 660 ui_test_utils::NavigateToURL(browser(), url); | 676 ui_test_utils::NavigateToURL(browser(), url); |
| 661 | 677 |
| 662 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); | 678 gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 663 | 679 |
| 680 #if defined(OS_MACOSX) |
| 681 // Press Cmd+F, which will make the Find box open and request focus. |
| 682 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, |
| 683 false, false, true, |
| 684 new MessageLoop::QuitTask()); |
| 685 #else |
| 664 // Press Ctrl+F, which will make the Find box open and request focus. | 686 // Press Ctrl+F, which will make the Find box open and request focus. |
| 665 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 687 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 666 false, false, false, | 688 false, false, false, |
| 667 new MessageLoop::QuitTask()); | 689 new MessageLoop::QuitTask()); |
| 690 #endif |
| 668 ui_test_utils::RunMessageLoop(); | 691 ui_test_utils::RunMessageLoop(); |
| 669 | 692 |
| 670 // Ideally, we wouldn't sleep here and instead would intercept the | 693 // Ideally, we wouldn't sleep here and instead would intercept the |
| 671 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we | 694 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 672 // could create a RenderViewHostDelegate wrapper and hook-it up by either: | 695 // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 673 // - creating a factory used to create the delegate | 696 // - creating a factory used to create the delegate |
| 674 // - making the test a private and overwriting the delegate member directly. | 697 // - making the test a private and overwriting the delegate member directly. |
| 675 MessageLoop::current()->PostDelayedTask( | 698 MessageLoop::current()->PostDelayedTask( |
| 676 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 699 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 677 ui_test_utils::RunMessageLoop(); | 700 ui_test_utils::RunMessageLoop(); |
| 678 | 701 |
| 679 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 702 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 680 | 703 |
| 681 browser()->FocusLocationBar(); | 704 browser()->FocusLocationBar(); |
| 682 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 705 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 683 | 706 |
| 684 // Now press Ctrl+F again and focus should move to the Find box. | 707 // Now press Ctrl+F again and focus should move to the Find box. |
| 708 #if defined(OS_MACOSX) |
| 709 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, |
| 710 false, false, true, |
| 711 new MessageLoop::QuitTask()); |
| 712 #else |
| 685 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, | 713 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 686 false, false, false, | 714 false, false, false, |
| 687 new MessageLoop::QuitTask()); | 715 new MessageLoop::QuitTask()); |
| 716 #endif |
| 688 ui_test_utils::RunMessageLoop(); | 717 ui_test_utils::RunMessageLoop(); |
| 689 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 718 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 690 | 719 |
| 691 // Set focus to the page. | 720 // Set focus to the page. |
| 692 ClickOnView(VIEW_ID_TAB_CONTAINER); | 721 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 693 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 722 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 694 | 723 |
| 695 // Now press Ctrl+F again and focus should move to the Find box. | 724 // Now press Ctrl+F again and focus should move to the Find box. |
| 696 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, false, | 725 #if defined(OS_MACOSX) |
| 697 false, false, | 726 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, |
| 727 false, false, true, |
| 698 new MessageLoop::QuitTask()); | 728 new MessageLoop::QuitTask()); |
| 729 #else |
| 730 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
| 731 false, false, false, |
| 732 new MessageLoop::QuitTask()); |
| 733 #endif |
| 699 ui_test_utils::RunMessageLoop(); | 734 ui_test_utils::RunMessageLoop(); |
| 700 | 735 |
| 701 // See remark above on why we wait. | 736 // See remark above on why we wait. |
| 702 MessageLoop::current()->PostDelayedTask( | 737 MessageLoop::current()->PostDelayedTask( |
| 703 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); | 738 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
| 704 ui_test_utils::RunMessageLoop(); | 739 ui_test_utils::RunMessageLoop(); |
| 705 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 740 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 706 } | 741 } |
| 707 | 742 |
| 708 // Makes sure the focus is in the right location when opening the different | 743 // Makes sure the focus is in the right location when opening the different |
| 709 // types of tabs. | 744 // types of tabs. |
| 710 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { | 745 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
| 746 BringBrowserWindowToFront(); |
| 711 // Open the history tab, focus should be on the tab contents. | 747 // Open the history tab, focus should be on the tab contents. |
| 712 browser()->ShowHistoryTab(); | 748 browser()->ShowHistoryTab(); |
| 713 | 749 |
| 714 ui_test_utils::RunAllPendingInMessageLoop(); | 750 ui_test_utils::RunAllPendingInMessageLoop(); |
| 715 | 751 |
| 716 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 752 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 717 | 753 |
| 718 // Open the new tab, focus should be on the location bar. | 754 // Open the new tab, focus should be on the location bar. |
| 719 browser()->NewTab(); | 755 browser()->NewTab(); |
| 720 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 756 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 721 | 757 |
| 722 // Open the download tab, focus should be on the tab contents. | 758 // Open the download tab, focus should be on the tab contents. |
| 723 browser()->ShowDownloadsTab(); | 759 browser()->ShowDownloadsTab(); |
| 724 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 760 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 725 | 761 |
| 726 // Open about:blank, focus should be on the location bar. | 762 // Open about:blank, focus should be on the location bar. |
| 727 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, | 763 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
| 728 -1, TabStripModel::ADD_SELECTED, NULL, | 764 -1, TabStripModel::ADD_SELECTED, NULL, |
| 729 std::string()); | 765 std::string()); |
| 730 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 766 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 731 } | 767 } |
| 732 | 768 |
| 733 // Tests that focus goes where expected when using reload. | 769 // Tests that focus goes where expected when using reload. |
| 734 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { | 770 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
| 771 BringBrowserWindowToFront(); |
| 735 HTTPTestServer* server = StartHTTPServer(); | 772 HTTPTestServer* server = StartHTTPServer(); |
| 773 ASSERT_TRUE(server); |
| 736 | 774 |
| 737 // Open the new tab, reload. | 775 // Open the new tab, reload. |
| 738 browser()->NewTab(); | 776 browser()->NewTab(); |
| 739 | 777 |
| 740 ui_test_utils::RunAllPendingInMessageLoop(); | 778 ui_test_utils::RunAllPendingInMessageLoop(); |
| 741 | 779 |
| 742 browser()->Reload(CURRENT_TAB); | 780 browser()->Reload(CURRENT_TAB); |
| 743 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 781 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 744 // Focus should stay on the location bar. | 782 // Focus should stay on the location bar. |
| 745 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 783 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 746 | 784 |
| 747 // Open a regular page, focus the location bar, reload. | 785 // Open a regular page, focus the location bar, reload. |
| 748 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage)); | 786 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage)); |
| 749 browser()->FocusLocationBar(); | 787 browser()->FocusLocationBar(); |
| 750 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); | 788 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
| 751 browser()->Reload(CURRENT_TAB); | 789 browser()->Reload(CURRENT_TAB); |
| 752 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 790 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 753 // Focus should now be on the tab contents. | 791 // Focus should now be on the tab contents. |
| 754 browser()->ShowDownloadsTab(); | 792 browser()->ShowDownloadsTab(); |
| 755 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 793 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 756 } | 794 } |
| 757 | 795 |
| 758 // Tests that focus goes where expected when using reload on a crashed tab. | 796 // Tests that focus goes where expected when using reload on a crashed tab. |
| 759 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { | 797 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { |
| 798 BringBrowserWindowToFront(); |
| 760 HTTPTestServer* server = StartHTTPServer(); | 799 HTTPTestServer* server = StartHTTPServer(); |
| 800 ASSERT_TRUE(server); |
| 761 | 801 |
| 762 // Open a regular page, crash, reload. | 802 // Open a regular page, crash, reload. |
| 763 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage)); | 803 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage)); |
| 764 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); | 804 ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
| 765 browser()->Reload(CURRENT_TAB); | 805 browser()->Reload(CURRENT_TAB); |
| 766 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 806 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
| 767 // Focus should now be on the tab contents. | 807 // Focus should now be on the tab contents. |
| 768 browser()->ShowDownloadsTab(); | 808 browser()->ShowDownloadsTab(); |
| 769 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 809 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 770 } | 810 } |
| OLD | NEW |