Index: chrome/browser/browser_focus_uitest.cc |
=================================================================== |
--- chrome/browser/browser_focus_uitest.cc (revision 23643) |
+++ chrome/browser/browser_focus_uitest.cc (working copy) |
@@ -9,6 +9,8 @@ |
#include "chrome/browser/browser.h" |
#include "chrome/browser/renderer_host/render_widget_host_view.h" |
#include "chrome/browser/tab_contents/interstitial_page.h" |
+#include "chrome/browser/tab_contents/tab_contents.h" |
+#include "chrome/browser/tab_contents/tab_contents_view.h" |
#include "chrome/browser/view_ids.h" |
#include "chrome/browser/views/frame/browser_view.h" |
#include "chrome/browser/views/location_bar_view.h" |
@@ -51,8 +53,8 @@ |
views::FocusManager* focus_manager = |
views::FocusManager::GetFocusManagerForNativeView(window); |
ASSERT_TRUE(focus_manager); |
- EXPECT_EQ(reinterpret_cast<views::View*>(browser_window)->GetViewByID(vid), |
- focus_manager->GetFocusedView()); |
+ EXPECT_EQ(reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid), |
+ focus_manager->GetFocusedView()) << "For view id " << vid; |
#elif defined(OS_LINUX) |
GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); |
ASSERT_TRUE(widget); |
@@ -62,6 +64,30 @@ |
#endif |
} |
+ static void HideNativeWindow(gfx::NativeWindow window) { |
+#if defined(OS_WIN) |
+ // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
+ // using Windows API. |
+ ::ShowWindow(window, SW_HIDE); |
+#elif defined(OS_LINUX) |
+ gtk_widget_hide(GTK_WIDGET(window)); |
+#else |
+ NOTIMPLEMENTED(); |
+#endif |
+ } |
+ |
+ static void ShowNativeWindow(gfx::NativeWindow window) { |
+#if defined(OS_WIN) |
+ // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
+ // using Windows API. |
+ ::ShowWindow(window, SW_SHOW); |
+#elif defined(OS_LINUX) |
+ gtk_widget_hide(GTK_WIDGET(window)); |
+#else |
+ NOTIMPLEMENTED(); |
+#endif |
+ } |
+ |
private: |
#if defined(OS_LINUX) |
// Check if the focused widget for |root| is |target| or a child of |target|. |
@@ -142,7 +168,32 @@ |
} // namespace |
+// TODO(estade): port. |
#if defined(OS_WIN) |
+IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
+ browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
+ true, -1, false, NULL); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
+ |
+ BrowserView* browser_view = static_cast<BrowserView*>(browser()->window()); |
+ ui_controls::MoveMouseToCenterAndPress( |
+ browser_view->GetTabContentsContainerView(), |
+ ui_controls::LEFT, |
+ ui_controls::DOWN | ui_controls::UP, |
+ new MessageLoop::QuitTask()); |
+ ui_test_utils::RunMessageLoop(); |
+ CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
+ |
+ ui_controls::MoveMouseToCenterAndPress( |
+ browser_view->GetLocationBarView(), |
+ ui_controls::LEFT, |
+ ui_controls::DOWN | ui_controls::UP, |
+ new MessageLoop::QuitTask()); |
+ ui_test_utils::RunMessageLoop(); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
+} |
+#endif |
+ |
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
HTTPTestServer* server = StartHTTPServer(); |
@@ -150,40 +201,25 @@ |
GURL url = server->TestServerPageW(kSimplePage); |
ui_test_utils::NavigateToURL(browser(), url); |
+ gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
+ |
// The focus should be on the Tab contents. |
- HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
- BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
- ASSERT_TRUE(browser_view); |
- views::FocusManager* focus_manager = |
- views::FocusManager::GetFocusManagerForNativeView(hwnd); |
- ASSERT_TRUE(focus_manager); |
- |
- EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
- focus_manager->GetFocusedView()); |
- |
+ CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
// Now hide the window, show it again, the focus should not have changed. |
- // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
- // using Windows API. |
- ::ShowWindow(hwnd, SW_HIDE); |
- ::ShowWindow(hwnd, SW_SHOW); |
- EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
- focus_manager->GetFocusedView()); |
+ HideNativeWindow(window); |
+ ShowNativeWindow(window); |
+ CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
- // Click on the location bar. |
- LocationBarView* location_bar = browser_view->GetLocationBarView(); |
- ui_controls::MoveMouseToCenterAndPress(location_bar, |
- ui_controls::LEFT, |
- ui_controls::DOWN | ui_controls::UP, |
- new MessageLoop::QuitTask()); |
- ui_test_utils::RunMessageLoop(); |
- // Location bar should have focus. |
- EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
- |
+ browser()->FocusLocationBar(); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
// Hide the window, show it again, the focus should not have changed. |
- ::ShowWindow(hwnd, SW_HIDE); |
- ::ShowWindow(hwnd, SW_SHOW); |
- EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
+ HideNativeWindow(window); |
+ ShowNativeWindow(window); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
+ // The rest of this test does not make sense on Linux because the behavior |
+ // of Activate() is not well defined and can vary by window manager. |
+#if defined(OS_WIN) |
// Open a new browser window. |
Browser* browser2 = Browser::Create(browser()->profile()); |
ASSERT_TRUE(browser2); |
@@ -204,17 +240,20 @@ |
// Switch to the 1st browser window, focus should still be on the location |
// bar and the second browser should have nothing focused. |
browser()->window()->Activate(); |
- EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
// Switch back to the second browser, focus should still be on the page. |
browser2->window()->Activate(); |
- EXPECT_EQ(NULL, focus_manager->GetFocusedView()); |
+ EXPECT_EQ(NULL, |
+ views::FocusManager::GetFocusManagerForNativeView( |
+ browser()->window()->GetNativeHandle())->GetFocusedView()); |
EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
focus_manager2->GetFocusedView()); |
// Close the 2nd browser to avoid a DCHECK(). |
browser_view2->Close(); |
+#endif |
} |
// Tabs remember focus. |
@@ -225,14 +264,6 @@ |
GURL url = server->TestServerPageW(kSimplePage); |
ui_test_utils::NavigateToURL(browser(), url); |
- HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
- BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
- ASSERT_TRUE(browser_view); |
- |
- views::FocusManager* focus_manager = |
- views::FocusManager::GetFocusManagerForNativeView(hwnd); |
- ASSERT_TRUE(focus_manager); |
- |
// Create several tabs. |
for (int i = 0; i < 4; ++i) { |
browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, |
@@ -252,19 +283,11 @@ |
browser()->SelectTabContentsAt(j, true); |
// Activate the location bar or the page. |
- views::View* view_to_focus; |
if (kFocusPage[i][j]) { |
- view_to_focus = browser_view->GetTabContentsContainerView(); |
+ browser()->GetTabContentsAt(j)->view()->Focus(); |
} else { |
- view_to_focus = browser_view->GetLocationBarView(); |
+ browser()->FocusLocationBar(); |
} |
- |
- ui_controls::MoveMouseToCenterAndPress(view_to_focus, |
- ui_controls::LEFT, |
- ui_controls::DOWN | |
- ui_controls::UP, |
- new MessageLoop::QuitTask()); |
- ui_test_utils::RunMessageLoop(); |
} |
// Now come back to the tab and check the right view is focused. |
@@ -272,14 +295,9 @@ |
// Activate the tab. |
browser()->SelectTabContentsAt(j, true); |
- // Activate the location bar or the page. |
- views::View* view; |
- if (kFocusPage[i][j]) { |
- view = browser_view->GetTabContentsContainerView(); |
- } else { |
- view = browser_view->GetLocationBarView(); |
- } |
- EXPECT_EQ(view, focus_manager->GetFocusedView()); |
+ ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
+ VIEW_ID_LOCATION_BAR; |
+ CheckViewHasFocus(vid); |
} |
} |
} |
@@ -297,26 +315,40 @@ |
ASSERT_TRUE(browser2); |
browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
browser2->window()->Show(); |
+ |
+ Browser* focused_browser; |
+ Browser* unfocused_browser; |
+#if defined(OS_LINUX) |
+ // On Linux, calling Activate() is not guaranteed to move focus, so we have |
+ // to figure out which browser does have focus. |
+ if (browser2->window()->IsActive()) { |
+ focused_browser = browser2; |
+ unfocused_browser = browser(); |
+ } else if (browser()->window()->IsActive()) { |
+ focused_browser = browser(); |
+ unfocused_browser = browser2; |
+ } else { |
+ ASSERT_TRUE(false); |
+ } |
+#elif defined(OS_WIN) |
+ focused_browser = browser(); |
+ unfocused_browser = browser2; |
+#endif |
+ |
GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); |
- ui_test_utils::NavigateToURL(browser2, steal_focus_url); |
+ ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
// Activate the first browser. |
- browser()->window()->Activate(); |
+ focused_browser->window()->Activate(); |
// Wait for the focus to be stolen by the other browser. |
- ::Sleep(2000); |
+ PlatformThread::Sleep(2000); |
// Make sure the first browser is still active. |
- HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
- BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
- ASSERT_TRUE(browser_view); |
- EXPECT_TRUE(browser_view->frame()->GetWindow()->IsActive()); |
+ EXPECT_TRUE(focused_browser->window()->IsActive()); |
// Close the 2nd browser to avoid a DCHECK(). |
- HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
- BrowserView* browser_view2 = |
- BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
- browser_view2->Close(); |
+ browser2->window()->Close(); |
} |
// Page cannot steal focus when focus is on location bar. |
@@ -327,24 +359,13 @@ |
GURL url = server->TestServerPageW(kStealFocusPage); |
ui_test_utils::NavigateToURL(browser(), url); |
- HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
- BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
- views::FocusManager* focus_manager = |
- views::FocusManager::GetFocusManagerForNativeView(hwnd); |
+ browser()->FocusLocationBar(); |
- // Click on the location bar. |
- LocationBarView* location_bar = browser_view->GetLocationBarView(); |
- ui_controls::MoveMouseToCenterAndPress(location_bar, |
- ui_controls::LEFT, |
- ui_controls::DOWN | ui_controls::UP, |
- new MessageLoop::QuitTask()); |
- ui_test_utils::RunMessageLoop(); |
- |
// Wait for the page to steal focus. |
- ::Sleep(2000); |
+ PlatformThread::Sleep(2000); |
// Make sure the location bar is still focused. |
- EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
} |
// Focus traversal on a regular page. |
@@ -355,19 +376,8 @@ |
GURL url = server->TestServerPageW(kTypicalPage); |
ui_test_utils::NavigateToURL(browser(), url); |
- HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
- BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
- views::FocusManager* focus_manager = |
- views::FocusManager::GetFocusManagerForNativeView(hwnd); |
+ browser()->FocusLocationBar(); |
- // Click on the location bar. |
- LocationBarView* location_bar = browser_view->GetLocationBarView(); |
- ui_controls::MoveMouseToCenterAndPress(location_bar, |
- ui_controls::LEFT, |
- ui_controls::DOWN | ui_controls::UP, |
- new MessageLoop::QuitTask()); |
- ui_test_utils::RunMessageLoop(); |
- |
const char* kExpElementIDs[] = { |
"", // Initially no element in the page should be focused |
// (the location bar is focused). |
@@ -375,10 +385,12 @@ |
"gmapLink" |
}; |
+ gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
+ |
// Test forward focus traversal. |
for (int i = 0; i < 3; ++i) { |
// Location bar should be focused. |
- EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
// Now let's press tab to move the focus. |
for (int j = 0; j < 7; ++j) { |
@@ -391,14 +403,14 @@ |
&actual)); |
ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
- ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_TAB, false, |
+ ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, false, |
false, false, |
new MessageLoop::QuitTask()); |
ui_test_utils::RunMessageLoop(); |
// Ideally, we wouldn't sleep here and instead would use the event |
// processed ack notification from the renderer. I am reluctant to create |
// a new notification/callback for that purpose just for this test. |
- ::Sleep(kActionDelayMs); |
+ PlatformThread::Sleep(kActionDelayMs); |
} |
// At this point the renderer has sent us a message asking to advance the |
@@ -411,15 +423,15 @@ |
// Now let's try reverse focus traversal. |
for (int i = 0; i < 3; ++i) { |
// Location bar should be focused. |
- EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
+ CheckViewHasFocus(VIEW_ID_LOCATION_BAR); |
// Now let's press shift-tab to move the focus in reverse. |
for (int j = 0; j < 7; ++j) { |
- ui_controls::SendKeyPressNotifyWhenDone(NULL, base::VKEY_TAB, false, |
+ ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, false, |
true, false, |
new MessageLoop::QuitTask()); |
ui_test_utils::RunMessageLoop(); |
- ::Sleep(kActionDelayMs); |
+ PlatformThread::Sleep(kActionDelayMs); |
// Let's make sure the focus is on the expected element in the page. |
std::string actual; |
@@ -439,6 +451,7 @@ |
} |
} |
+#if defined(OS_WIN) |
// Focus traversal while an interstitial is showing. |
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) { |
HTTPTestServer* server = StartHTTPServer(); |
@@ -662,10 +675,10 @@ |
// Makes sure the focus is in the right location when opening the different |
// types of tabs. |
// TODO(estade): undisable this. |
-IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) { |
+IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
// Open the history tab, focus should be on the tab contents. |
browser()->ShowHistoryTab(); |
- CheckViewHasFocus(VIEW_ID_TAB_CONTAINER); |
+ CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
// Open the new tab, focus should be on the location bar. |
browser()->NewTab(); |
@@ -673,7 +686,7 @@ |
// Open the download tab, focus should be on the tab contents. |
browser()->ShowDownloadsTab(); |
- CheckViewHasFocus(VIEW_ID_TAB_CONTAINER); |
+ CheckViewHasFocus(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW); |
// Open about:blank, focus should be on the location bar. |
browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |