| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "content/public/browser/interstitial_page.h" | 32 #include "content/public/browser/interstitial_page.h" |
| 33 #include "content/public/browser/interstitial_page_delegate.h" | 33 #include "content/public/browser/interstitial_page_delegate.h" |
| 34 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
| 35 #include "content/public/browser/render_view_host.h" | 35 #include "content/public/browser/render_view_host.h" |
| 36 #include "content/public/browser/render_widget_host_view.h" | 36 #include "content/public/browser/render_widget_host_view.h" |
| 37 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/browser/web_contents_view.h" | 38 #include "content/public/browser/web_contents_view.h" |
| 39 #include "content/public/test/browser_test_utils.h" | 39 #include "content/public/test/browser_test_utils.h" |
| 40 #include "net/test/test_server.h" | 40 #include "net/test/test_server.h" |
| 41 | 41 |
| 42 #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) | 42 #if defined(TOOLKIT_VIEWS) |
| 43 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 43 #include "ui/views/focus/focus_manager.h" | 44 #include "ui/views/focus/focus_manager.h" |
| 44 #include "ui/views/view.h" | 45 #include "ui/views/view.h" |
| 45 #endif | 46 #endif |
| 46 | 47 |
| 47 #if defined(TOOLKIT_VIEWS) | |
| 48 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 49 #endif | |
| 50 | |
| 51 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
| 52 #include <windows.h> | 49 #include <windows.h> |
| 53 #include <Psapi.h> | 50 #include <Psapi.h> |
| 54 #include "base/string_util.h" | 51 #include "base/string_util.h" |
| 55 #endif | 52 #endif |
| 56 | 53 |
| 57 using content::InterstitialPage; | 54 using content::InterstitialPage; |
| 58 using content::NavigationController; | 55 using content::NavigationController; |
| 59 using content::RenderViewHost; | 56 using content::RenderViewHost; |
| 60 using content::WebContents; | 57 using content::WebContents; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } else { | 144 } else { |
| 148 MessageLoop::current()->PostDelayedTask( | 145 MessageLoop::current()->PostDelayedTask( |
| 149 FROM_HERE, | 146 FROM_HERE, |
| 150 base::Bind(&CheckFocus, browser, id, timeout), | 147 base::Bind(&CheckFocus, browser, id, timeout), |
| 151 base::TimeDelta::FromMilliseconds(10)); | 148 base::TimeDelta::FromMilliseconds(10)); |
| 152 } | 149 } |
| 153 }; | 150 }; |
| 154 | 151 |
| 155 class BrowserFocusTest : public InProcessBrowserTest { | 152 class BrowserFocusTest : public InProcessBrowserTest { |
| 156 public: | 153 public: |
| 157 BrowserFocusTest() : | |
| 158 #if defined(USE_AURA) | |
| 159 location_bar_focus_view_id_(VIEW_ID_OMNIBOX) | |
| 160 #else | |
| 161 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR) | |
| 162 #endif | |
| 163 {} | |
| 164 | |
| 165 bool IsViewFocused(ViewID vid) { | 154 bool IsViewFocused(ViewID vid) { |
| 166 return ui_test_utils::IsViewFocused(browser(), vid); | 155 return ui_test_utils::IsViewFocused(browser(), vid); |
| 167 } | 156 } |
| 168 | 157 |
| 169 void ClickOnView(ViewID vid) { | 158 void ClickOnView(ViewID vid) { |
| 170 ui_test_utils::ClickOnView(browser(), vid); | 159 ui_test_utils::ClickOnView(browser(), vid); |
| 171 } | 160 } |
| 172 | 161 |
| 173 bool WaitForFocusChange(ViewID vid) { | 162 bool WaitForFocusChange(ViewID vid) { |
| 174 const base::Time timeout = base::Time::Now() + | 163 const base::Time timeout = base::Time::Now() + |
| 175 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs); | 164 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs); |
| 176 MessageLoop::current()->PostDelayedTask( | 165 MessageLoop::current()->PostDelayedTask( |
| 177 FROM_HERE, | 166 FROM_HERE, |
| 178 base::Bind(&CheckFocus, browser(), vid, timeout), | 167 base::Bind(&CheckFocus, browser(), vid, timeout), |
| 179 base::TimeDelta::FromMilliseconds(100)); | 168 base::TimeDelta::FromMilliseconds(100)); |
| 180 content::RunMessageLoop(); | 169 content::RunMessageLoop(); |
| 181 return IsViewFocused(vid); | 170 return IsViewFocused(vid); |
| 182 } | 171 } |
| 183 | |
| 184 ViewID location_bar_focus_view_id_; | |
| 185 }; | 172 }; |
| 186 | 173 |
| 187 class TestInterstitialPage : public content::InterstitialPageDelegate { | 174 class TestInterstitialPage : public content::InterstitialPageDelegate { |
| 188 public: | 175 public: |
| 189 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) { | 176 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) { |
| 190 base::FilePath file_path; | 177 base::FilePath file_path; |
| 191 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); | 178 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 192 EXPECT_TRUE(r); | 179 EXPECT_TRUE(r); |
| 193 file_path = file_path.AppendASCII("focus"); | 180 file_path = file_path.AppendASCII("focus"); |
| 194 file_path = file_path.AppendASCII(kTypicalPageName); | 181 file_path = file_path.AppendASCII(kTypicalPageName); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 #if defined(OS_POSIX) | 218 #if defined(OS_POSIX) |
| 232 // It seems we have to wait a little bit for the widgets to spin up before | 219 // It seems we have to wait a little bit for the widgets to spin up before |
| 233 // we can start clicking on them. | 220 // we can start clicking on them. |
| 234 MessageLoop::current()->PostDelayedTask( | 221 MessageLoop::current()->PostDelayedTask( |
| 235 FROM_HERE, | 222 FROM_HERE, |
| 236 MessageLoop::QuitClosure(), | 223 MessageLoop::QuitClosure(), |
| 237 base::TimeDelta::FromMilliseconds(kActionDelayMs)); | 224 base::TimeDelta::FromMilliseconds(kActionDelayMs)); |
| 238 content::RunMessageLoop(); | 225 content::RunMessageLoop(); |
| 239 #endif // defined(OS_POSIX) | 226 #endif // defined(OS_POSIX) |
| 240 | 227 |
| 241 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 228 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 242 | 229 |
| 243 ClickOnView(VIEW_ID_TAB_CONTAINER); | 230 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 244 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 231 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 245 | 232 |
| 246 ClickOnView(VIEW_ID_LOCATION_BAR); | 233 ClickOnView(VIEW_ID_OMNIBOX); |
| 247 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 234 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 248 } | 235 } |
| 249 | 236 |
| 250 // Flaky, http://crbug.com/69034. | 237 // Flaky, http://crbug.com/69034. |
| 251 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { | 238 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { |
| 252 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 239 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 253 ASSERT_TRUE(test_server()->Start()); | 240 ASSERT_TRUE(test_server()->Start()); |
| 254 | 241 |
| 255 // First we navigate to our test page. | 242 // First we navigate to our test page. |
| 256 GURL url = test_server()->GetURL(kSimplePage); | 243 GURL url = test_server()->GetURL(kSimplePage); |
| 257 ui_test_utils::NavigateToURL(browser(), url); | 244 ui_test_utils::NavigateToURL(browser(), url); |
| 258 | 245 |
| 259 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); | 246 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); |
| 260 | 247 |
| 261 // The focus should be on the Tab contents. | 248 // The focus should be on the Tab contents. |
| 262 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 249 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 263 // Now hide the window, show it again, the focus should not have changed. | 250 // Now hide the window, show it again, the focus should not have changed. |
| 264 ui_test_utils::HideNativeWindow(window); | 251 ui_test_utils::HideNativeWindow(window); |
| 265 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); | 252 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
| 266 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 253 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 267 | 254 |
| 268 chrome::FocusLocationBar(browser()); | 255 chrome::FocusLocationBar(browser()); |
| 269 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 256 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 270 // Hide the window, show it again, the focus should not have changed. | 257 // Hide the window, show it again, the focus should not have changed. |
| 271 ui_test_utils::HideNativeWindow(window); | 258 ui_test_utils::HideNativeWindow(window); |
| 272 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); | 259 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
| 273 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 260 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 274 | 261 |
| 275 // The rest of this test does not make sense on Linux because the behavior | 262 // The rest of this test does not make sense on Linux because the behavior |
| 276 // of Activate() is not well defined and can vary by window manager. | 263 // of Activate() is not well defined and can vary by window manager. |
| 277 #if defined(OS_WIN) | 264 #if defined(OS_WIN) |
| 278 // Open a new browser window. | 265 // Open a new browser window. |
| 279 Browser* browser2 = | 266 Browser* browser2 = |
| 280 new Browser(Browser::CreateParams(browser()->profile(), | 267 new Browser(Browser::CreateParams(browser()->profile(), |
| 281 browser()->host_desktop_type())); | 268 browser()->host_desktop_type())); |
| 282 ASSERT_TRUE(browser2); | 269 ASSERT_TRUE(browser2); |
| 283 chrome::AddBlankTabAt(browser2, -1, true); | 270 chrome::AddBlankTabAt(browser2, -1, true); |
| 284 browser2->window()->Show(); | 271 browser2->window()->Show(); |
| 285 ui_test_utils::NavigateToURL(browser2, url); | 272 ui_test_utils::NavigateToURL(browser2, url); |
| 286 | 273 |
| 287 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow(); | 274 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow(); |
| 288 BrowserView* browser_view2 = | 275 BrowserView* browser_view2 = |
| 289 BrowserView::GetBrowserViewForBrowser(browser2); | 276 BrowserView::GetBrowserViewForBrowser(browser2); |
| 290 ASSERT_TRUE(browser_view2); | 277 ASSERT_TRUE(browser_view2); |
| 291 const views::Widget* widget2 = | 278 const views::Widget* widget2 = |
| 292 views::Widget::GetWidgetForNativeWindow(window2); | 279 views::Widget::GetWidgetForNativeWindow(window2); |
| 293 ASSERT_TRUE(widget2); | 280 ASSERT_TRUE(widget2); |
| 294 const views::FocusManager* focus_manager2 = widget2->GetFocusManager(); | 281 const views::FocusManager* focus_manager2 = widget2->GetFocusManager(); |
| 295 ASSERT_TRUE(focus_manager2); | 282 ASSERT_TRUE(focus_manager2); |
| 296 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), | 283 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
| 297 focus_manager2->GetFocusedView()); | 284 focus_manager2->GetFocusedView()); |
| 298 | 285 |
| 299 // Switch to the 1st browser window, focus should still be on the location | 286 // Switch to the 1st browser window, focus should still be on the location |
| 300 // bar and the second browser should have nothing focused. | 287 // bar and the second browser should have nothing focused. |
| 301 browser()->window()->Activate(); | 288 browser()->window()->Activate(); |
| 302 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 289 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 303 EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); | 290 EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
| 304 | 291 |
| 305 // Switch back to the second browser, focus should still be on the page. | 292 // Switch back to the second browser, focus should still be on the page. |
| 306 browser2->window()->Activate(); | 293 browser2->window()->Activate(); |
| 307 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); | 294 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); |
| 308 ASSERT_TRUE(widget); | 295 ASSERT_TRUE(widget); |
| 309 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView()); | 296 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView()); |
| 310 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), | 297 EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
| 311 focus_manager2->GetFocusedView()); | 298 focus_manager2->GetFocusedView()); |
| 312 | 299 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 } else { | 336 } else { |
| 350 chrome::FocusLocationBar(browser()); | 337 chrome::FocusLocationBar(browser()); |
| 351 } | 338 } |
| 352 } | 339 } |
| 353 | 340 |
| 354 // Now come back to the tab and check the right view is focused. | 341 // Now come back to the tab and check the right view is focused. |
| 355 for (int j = 0; j < 5; j++) { | 342 for (int j = 0; j < 5; j++) { |
| 356 // Activate the tab. | 343 // Activate the tab. |
| 357 browser()->tab_strip_model()->ActivateTabAt(j, true); | 344 browser()->tab_strip_model()->ActivateTabAt(j, true); |
| 358 | 345 |
| 359 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : | 346 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX; |
| 360 location_bar_focus_view_id_; | |
| 361 ASSERT_TRUE(IsViewFocused(vid)); | 347 ASSERT_TRUE(IsViewFocused(vid)); |
| 362 } | 348 } |
| 363 | 349 |
| 364 browser()->tab_strip_model()->ActivateTabAt(0, true); | 350 browser()->tab_strip_model()->ActivateTabAt(0, true); |
| 365 // Try the above, but with ctrl+tab. Since tab normally changes focus, | 351 // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 366 // this has regressed in the past. Loop through several times to be sure. | 352 // this has regressed in the past. Loop through several times to be sure. |
| 367 for (int j = 0; j < 15; j++) { | 353 for (int j = 0; j < 15; j++) { |
| 368 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : | 354 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : |
| 369 location_bar_focus_view_id_; | 355 VIEW_ID_OMNIBOX; |
| 370 ASSERT_TRUE(IsViewFocused(vid)); | 356 ASSERT_TRUE(IsViewFocused(vid)); |
| 371 | 357 |
| 372 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 358 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 373 browser(), ui::VKEY_TAB, true, false, false, false)); | 359 browser(), ui::VKEY_TAB, true, false, false, false)); |
| 374 } | 360 } |
| 375 | 361 |
| 376 // As above, but with ctrl+shift+tab. | 362 // As above, but with ctrl+shift+tab. |
| 377 browser()->tab_strip_model()->ActivateTabAt(4, true); | 363 browser()->tab_strip_model()->ActivateTabAt(4, true); |
| 378 for (int j = 14; j >= 0; --j) { | 364 for (int j = 14; j >= 0; --j) { |
| 379 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : | 365 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : |
| 380 location_bar_focus_view_id_; | 366 VIEW_ID_OMNIBOX; |
| 381 ASSERT_TRUE(IsViewFocused(vid)); | 367 ASSERT_TRUE(IsViewFocused(vid)); |
| 382 | 368 |
| 383 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 369 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 384 browser(), ui::VKEY_TAB, true, true, false, false)); | 370 browser(), ui::VKEY_TAB, true, true, false, false)); |
| 385 } | 371 } |
| 386 } | 372 } |
| 387 } | 373 } |
| 388 | 374 |
| 389 // Tabs remember focus with find-in-page box. | 375 // Tabs remember focus with find-in-page box. |
| 390 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { | 376 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 406 | 392 |
| 407 // Create a 2nd tab. | 393 // Create a 2nd tab. |
| 408 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); | 394 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); |
| 409 | 395 |
| 410 // Focus should be on the recently opened tab page. | 396 // Focus should be on the recently opened tab page. |
| 411 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 397 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 412 | 398 |
| 413 // Select 1st tab, focus should still be on the location-bar. | 399 // Select 1st tab, focus should still be on the location-bar. |
| 414 // (bug http://crbug.com/23296) | 400 // (bug http://crbug.com/23296) |
| 415 browser()->tab_strip_model()->ActivateTabAt(0, true); | 401 browser()->tab_strip_model()->ActivateTabAt(0, true); |
| 416 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 402 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 417 | 403 |
| 418 // Now open the find box again, switch to another tab and come back, the focus | 404 // Now open the find box again, switch to another tab and come back, the focus |
| 419 // should return to the find box. | 405 // should return to the find box. |
| 420 chrome::Find(browser()); | 406 chrome::Find(browser()); |
| 421 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 407 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 422 browser()->tab_strip_model()->ActivateTabAt(1, true); | 408 browser()->tab_strip_model()->ActivateTabAt(1, true); |
| 423 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 409 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 424 browser()->tab_strip_model()->ActivateTabAt(0, true); | 410 browser()->tab_strip_model()->ActivateTabAt(0, true); |
| 425 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 411 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 426 } | 412 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 GURL url = test_server()->GetURL(kStealFocusPage); | 472 GURL url = test_server()->GetURL(kStealFocusPage); |
| 487 ui_test_utils::NavigateToURL(browser(), url); | 473 ui_test_utils::NavigateToURL(browser(), url); |
| 488 | 474 |
| 489 chrome::FocusLocationBar(browser()); | 475 chrome::FocusLocationBar(browser()); |
| 490 | 476 |
| 491 ASSERT_TRUE(content::ExecuteScript( | 477 ASSERT_TRUE(content::ExecuteScript( |
| 492 browser()->tab_strip_model()->GetActiveWebContents(), | 478 browser()->tab_strip_model()->GetActiveWebContents(), |
| 493 "stealFocus();")); | 479 "stealFocus();")); |
| 494 | 480 |
| 495 // Make sure the location bar is still focused. | 481 // Make sure the location bar is still focused. |
| 496 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 482 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 497 } | 483 } |
| 498 | 484 |
| 499 // Focus traversal on a regular page. | 485 // Focus traversal on a regular page. |
| 500 // Note that this test relies on a notification from the renderer that the | 486 // Note that this test relies on a notification from the renderer that the |
| 501 // focus has changed in the page. The notification in the renderer may change | 487 // focus has changed in the page. The notification in the renderer may change |
| 502 // at which point this test would fail (see comment in | 488 // at which point this test would fail (see comment in |
| 503 // RenderWidget::didFocus()). | 489 // RenderWidget::didFocus()). |
| 504 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { | 490 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
| 505 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 491 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 506 ASSERT_TRUE(test_server()->Start()); | 492 ASSERT_TRUE(test_server()->Start()); |
| 507 | 493 |
| 508 // First we navigate to our test page. | 494 // First we navigate to our test page. |
| 509 GURL url = test_server()->GetURL(kTypicalPage); | 495 GURL url = test_server()->GetURL(kTypicalPage); |
| 510 ui_test_utils::NavigateToURL(browser(), url); | 496 ui_test_utils::NavigateToURL(browser(), url); |
| 511 | 497 |
| 512 chrome::FocusLocationBar(browser()); | 498 chrome::FocusLocationBar(browser()); |
| 513 | 499 |
| 514 const char* kTextElementID = "textEdit"; | 500 const char* kTextElementID = "textEdit"; |
| 515 const char* kExpElementIDs[] = { | 501 const char* kExpElementIDs[] = { |
| 516 "", // Initially no element in the page should be focused | 502 "", // Initially no element in the page should be focused |
| 517 // (the location bar is focused). | 503 // (the location bar is focused). |
| 518 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink", | 504 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 519 "gmapLink" | 505 "gmapLink" |
| 520 }; | 506 }; |
| 521 | 507 |
| 522 // Test forward focus traversal. | 508 // Test forward focus traversal. |
| 523 for (int i = 0; i < 3; ++i) { | 509 for (int i = 0; i < 3; ++i) { |
| 524 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); | 510 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); |
| 525 // Location bar should be focused. | 511 // Location bar should be focused. |
| 526 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 512 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 527 | 513 |
| 528 // Move the caret to the end, otherwise the next Tab key may not move focus. | 514 // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 529 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 515 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 530 browser(), ui::VKEY_END, false, false, false, false)); | 516 browser(), ui::VKEY_END, false, false, false, false)); |
| 531 | 517 |
| 532 // Now let's press tab to move the focus. | 518 // Now let's press tab to move the focus. |
| 533 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { | 519 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
| 534 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j)); | 520 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j)); |
| 535 // Let's make sure the focus is on the expected element in the page. | 521 // Let's make sure the focus is on the expected element in the page. |
| 536 std::string actual; | 522 std::string actual; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 566 // At this point the renderer has sent us a message asking to advance the | 552 // At this point the renderer has sent us a message asking to advance the |
| 567 // focus (as the end of the focus loop was reached in the renderer). | 553 // focus (as the end of the focus loop was reached in the renderer). |
| 568 // We need to run the message loop to process it. | 554 // We need to run the message loop to process it. |
| 569 content::RunAllPendingInMessageLoop(); | 555 content::RunAllPendingInMessageLoop(); |
| 570 } | 556 } |
| 571 | 557 |
| 572 // Now let's try reverse focus traversal. | 558 // Now let's try reverse focus traversal. |
| 573 for (int i = 0; i < 3; ++i) { | 559 for (int i = 0; i < 3; ++i) { |
| 574 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); | 560 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); |
| 575 // Location bar should be focused. | 561 // Location bar should be focused. |
| 576 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 562 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 577 | 563 |
| 578 // Move the caret to the end, otherwise the next Tab key may not move focus. | 564 // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 579 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 565 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 580 browser(), ui::VKEY_END, false, false, false, false)); | 566 browser(), ui::VKEY_END, false, false, false, false)); |
| 581 | 567 |
| 582 // Now let's press shift-tab to move the focus in reverse. | 568 // Now let's press shift-tab to move the focus in reverse. |
| 583 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { | 569 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
| 584 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j)); | 570 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j)); |
| 585 const char* next_element = | 571 const char* next_element = |
| 586 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j]; | 572 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j]; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 const char* kExpElementIDs[] = { | 635 const char* kExpElementIDs[] = { |
| 650 "", // Initially no element in the page should be focused | 636 "", // Initially no element in the page should be focused |
| 651 // (the location bar is focused). | 637 // (the location bar is focused). |
| 652 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", | 638 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 653 "gmapLink" | 639 "gmapLink" |
| 654 }; | 640 }; |
| 655 | 641 |
| 656 // Test forward focus traversal. | 642 // Test forward focus traversal. |
| 657 for (int i = 0; i < 2; ++i) { | 643 for (int i = 0; i < 2; ++i) { |
| 658 // Location bar should be focused. | 644 // Location bar should be focused. |
| 659 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 645 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 660 | 646 |
| 661 // Move the caret to the end, otherwise the next Tab key may not move focus. | 647 // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 662 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 648 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 663 browser(), ui::VKEY_END, false, false, false, false)); | 649 browser(), ui::VKEY_END, false, false, false, false)); |
| 664 | 650 |
| 665 // Now let's press tab to move the focus. | 651 // Now let's press tab to move the focus. |
| 666 for (size_t j = 0; j < 7; ++j) { | 652 for (size_t j = 0; j < 7; ++j) { |
| 667 // Let's make sure the focus is on the expected element in the page. | 653 // Let's make sure the focus is on the expected element in the page. |
| 668 std::string actual; | 654 std::string actual; |
| 669 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 655 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 692 | 678 |
| 693 // At this point the renderer has sent us a message asking to advance the | 679 // At this point the renderer has sent us a message asking to advance the |
| 694 // focus (as the end of the focus loop was reached in the renderer). | 680 // focus (as the end of the focus loop was reached in the renderer). |
| 695 // We need to run the message loop to process it. | 681 // We need to run the message loop to process it. |
| 696 content::RunAllPendingInMessageLoop(); | 682 content::RunAllPendingInMessageLoop(); |
| 697 } | 683 } |
| 698 | 684 |
| 699 // Now let's try reverse focus traversal. | 685 // Now let's try reverse focus traversal. |
| 700 for (int i = 0; i < 2; ++i) { | 686 for (int i = 0; i < 2; ++i) { |
| 701 // Location bar should be focused. | 687 // Location bar should be focused. |
| 702 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 688 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 703 | 689 |
| 704 // Move the caret to the end, otherwise the next Tab key may not move focus. | 690 // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 705 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 691 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 706 browser(), ui::VKEY_END, false, false, false, false)); | 692 browser(), ui::VKEY_END, false, false, false, false)); |
| 707 | 693 |
| 708 // Now let's press shift-tab to move the focus in reverse. | 694 // Now let's press shift-tab to move the focus in reverse. |
| 709 for (size_t j = 0; j < 7; ++j) { | 695 for (size_t j = 0; j < 7; ++j) { |
| 710 int notification_type; | 696 int notification_type; |
| 711 content::NotificationSource notification_source = | 697 content::NotificationSource notification_source = |
| 712 content::NotificationService::AllSources(); | 698 content::NotificationService::AllSources(); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 browser(), ui::VKEY_F, false, false, false, true)); | 780 browser(), ui::VKEY_F, false, false, false, true)); |
| 795 #else | 781 #else |
| 796 // Press Ctrl+F, which will make the Find box open and request focus. | 782 // Press Ctrl+F, which will make the Find box open and request focus. |
| 797 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 783 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 798 browser(), ui::VKEY_F, true, false, false, false)); | 784 browser(), ui::VKEY_F, true, false, false, false)); |
| 799 #endif | 785 #endif |
| 800 | 786 |
| 801 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 787 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 802 | 788 |
| 803 chrome::FocusLocationBar(browser()); | 789 chrome::FocusLocationBar(browser()); |
| 804 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 790 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 805 | 791 |
| 806 // Now press Ctrl+F again and focus should move to the Find box. | 792 // Now press Ctrl+F again and focus should move to the Find box. |
| 807 #if defined(OS_MACOSX) | 793 #if defined(OS_MACOSX) |
| 808 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 794 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 809 browser(), ui::VKEY_F, false, false, false, true)); | 795 browser(), ui::VKEY_F, false, false, false, true)); |
| 810 #else | 796 #else |
| 811 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 797 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 812 browser(), ui::VKEY_F, true, false, false, false)); | 798 browser(), ui::VKEY_F, true, false, false, false)); |
| 813 #endif | 799 #endif |
| 814 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 800 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 838 // Open the history tab, focus should be on the tab contents. | 824 // Open the history tab, focus should be on the tab contents. |
| 839 chrome::ShowHistory(browser()); | 825 chrome::ShowHistory(browser()); |
| 840 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( | 826 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
| 841 browser()->tab_strip_model()->GetActiveWebContents())); | 827 browser()->tab_strip_model()->GetActiveWebContents())); |
| 842 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 828 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 843 | 829 |
| 844 // Open the new tab, focus should be on the location bar. | 830 // Open the new tab, focus should be on the location bar. |
| 845 chrome::NewTab(browser()); | 831 chrome::NewTab(browser()); |
| 846 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( | 832 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
| 847 browser()->tab_strip_model()->GetActiveWebContents())); | 833 browser()->tab_strip_model()->GetActiveWebContents())); |
| 848 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 834 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 849 | 835 |
| 850 // Open the download tab, focus should be on the tab contents. | 836 // Open the download tab, focus should be on the tab contents. |
| 851 chrome::ShowDownloads(browser()); | 837 chrome::ShowDownloads(browser()); |
| 852 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( | 838 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
| 853 browser()->tab_strip_model()->GetActiveWebContents())); | 839 browser()->tab_strip_model()->GetActiveWebContents())); |
| 854 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 840 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 855 | 841 |
| 856 // Open about:blank, focus should be on the location bar. | 842 // Open about:blank, focus should be on the location bar. |
| 857 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL), | 843 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL), |
| 858 content::PAGE_TRANSITION_LINK); | 844 content::PAGE_TRANSITION_LINK); |
| 859 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( | 845 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
| 860 browser()->tab_strip_model()->GetActiveWebContents())); | 846 browser()->tab_strip_model()->GetActiveWebContents())); |
| 861 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 847 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 862 } | 848 } |
| 863 | 849 |
| 864 // Tests that focus goes where expected when using reload. | 850 // Tests that focus goes where expected when using reload. |
| 865 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { | 851 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
| 866 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 852 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 867 ASSERT_TRUE(test_server()->Start()); | 853 ASSERT_TRUE(test_server()->Start()); |
| 868 | 854 |
| 869 // Open the new tab, reload. | 855 // Open the new tab, reload. |
| 870 { | 856 { |
| 871 content::WindowedNotificationObserver observer( | 857 content::WindowedNotificationObserver observer( |
| 872 content::NOTIFICATION_LOAD_STOP, | 858 content::NOTIFICATION_LOAD_STOP, |
| 873 content::NotificationService::AllSources()); | 859 content::NotificationService::AllSources()); |
| 874 chrome::NewTab(browser()); | 860 chrome::NewTab(browser()); |
| 875 observer.Wait(); | 861 observer.Wait(); |
| 876 } | 862 } |
| 877 content::RunAllPendingInMessageLoop(); | 863 content::RunAllPendingInMessageLoop(); |
| 878 | 864 |
| 879 { | 865 { |
| 880 content::WindowedNotificationObserver observer( | 866 content::WindowedNotificationObserver observer( |
| 881 content::NOTIFICATION_LOAD_STOP, | 867 content::NOTIFICATION_LOAD_STOP, |
| 882 content::Source<NavigationController>( | 868 content::Source<NavigationController>( |
| 883 &browser()->tab_strip_model()->GetActiveWebContents()-> | 869 &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 884 GetController())); | 870 GetController())); |
| 885 chrome::Reload(browser(), CURRENT_TAB); | 871 chrome::Reload(browser(), CURRENT_TAB); |
| 886 observer.Wait(); | 872 observer.Wait(); |
| 887 } | 873 } |
| 888 // Focus should stay on the location bar. | 874 // Focus should stay on the location bar. |
| 889 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 875 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 890 | 876 |
| 891 // Open a regular page, focus the location bar, reload. | 877 // Open a regular page, focus the location bar, reload. |
| 892 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); | 878 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
| 893 chrome::FocusLocationBar(browser()); | 879 chrome::FocusLocationBar(browser()); |
| 894 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 880 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 895 { | 881 { |
| 896 content::WindowedNotificationObserver observer( | 882 content::WindowedNotificationObserver observer( |
| 897 content::NOTIFICATION_LOAD_STOP, | 883 content::NOTIFICATION_LOAD_STOP, |
| 898 content::Source<NavigationController>( | 884 content::Source<NavigationController>( |
| 899 &browser()->tab_strip_model()->GetActiveWebContents()-> | 885 &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 900 GetController())); | 886 GetController())); |
| 901 chrome::Reload(browser(), CURRENT_TAB); | 887 chrome::Reload(browser(), CURRENT_TAB); |
| 902 observer.Wait(); | 888 observer.Wait(); |
| 903 } | 889 } |
| 904 | 890 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 | 942 |
| 957 // Make sure the second tab is selected. | 943 // Make sure the second tab is selected. |
| 958 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); | 944 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 959 | 945 |
| 960 // The tab contents should have the focus in the second tab. | 946 // The tab contents should have the focus in the second tab. |
| 961 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | 947 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
| 962 | 948 |
| 963 // Go back to the first tab. The focus should not be in the omnibox. | 949 // Go back to the first tab. The focus should not be in the omnibox. |
| 964 chrome::SelectPreviousTab(browser()); | 950 chrome::SelectPreviousTab(browser()); |
| 965 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); | 951 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 966 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), | 952 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
| 967 VIEW_ID_LOCATION_BAR)); | |
| 968 } | 953 } |
| 969 | 954 |
| 970 } // namespace | 955 } // namespace |
| OLD | NEW |