1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/frame/browser_view.h"
7 #include "build/build_config.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/view_ids.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/interactive_test_utils.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "ui/views/focus/focus_manager.h"
18 #include "ui/views/view.h"
21 const char kSimplePage
[] = "/focus/page_with_focus.html";
23 class BrowserViewFocusTest
: public InProcessBrowserTest
{
25 bool IsViewFocused(ViewID vid
) {
26 return ui_test_utils::IsViewFocused(browser(), vid
);
30 // Flaky, http://crbug.com/69034.
31 IN_PROC_BROWSER_TEST_F(BrowserViewFocusTest
, DISABLED_BrowsersRememberFocus
) {
32 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
33 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
35 // First we navigate to our test page.
36 GURL url
= embedded_test_server()->GetURL(kSimplePage
);
37 ui_test_utils::NavigateToURL(browser(), url
);
39 gfx::NativeWindow window
= browser()->window()->GetNativeWindow();
41 // The focus should be on the Tab contents.
42 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
43 // Now hide the window, show it again, the focus should not have changed.
44 ui_test_utils::HideNativeWindow(window
);
45 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window
));
46 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
48 chrome::FocusLocationBar(browser());
49 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
50 // Hide the window, show it again, the focus should not have changed.
51 ui_test_utils::HideNativeWindow(window
);
52 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window
));
53 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
55 // The rest of this test does not make sense on Linux because the behavior
56 // of Activate() is not well defined and can vary by window manager.
58 // Open a new browser window.
60 new Browser(Browser::CreateParams(browser()->profile(),
61 browser()->host_desktop_type()));
62 ASSERT_TRUE(browser2
);
63 chrome::AddTabAt(browser2
, GURL(), -1, true);
64 browser2
->window()->Show();
65 ui_test_utils::NavigateToURL(browser2
, url
);
67 gfx::NativeWindow window2
= browser2
->window()->GetNativeWindow();
68 BrowserView
* browser_view2
= BrowserView::GetBrowserViewForBrowser(browser2
);
69 ASSERT_TRUE(browser_view2
);
70 const views::Widget
* widget2
=
71 views::Widget::GetWidgetForNativeWindow(window2
);
73 const views::FocusManager
* focus_manager2
= widget2
->GetFocusManager();
74 ASSERT_TRUE(focus_manager2
);
75 EXPECT_EQ(browser_view2
->GetTabContentsContainerView(),
76 focus_manager2
->GetFocusedView());
78 // Switch to the 1st browser window, focus should still be on the location
79 // bar and the second browser should have nothing focused.
80 browser()->window()->Activate();
81 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
82 EXPECT_EQ(nullptr, focus_manager2
->GetFocusedView());
84 // Switch back to the second browser, focus should still be on the page.
85 browser2
->window()->Activate();
86 views::Widget
* widget
= views::Widget::GetWidgetForNativeWindow(window
);
88 EXPECT_EQ(nullptr, widget
->GetFocusManager()->GetFocusedView());
89 EXPECT_EQ(browser_view2
->GetTabContentsContainerView(),
90 focus_manager2
->GetFocusedView());
92 // Close the 2nd browser to avoid a DCHECK().
93 browser_view2
->Close();