blob: d9771db10b832f60c5e6a00ee29992cfda8a5dca [file] [log] [blame]
[email protected]265ccd92010-04-29 17:57:171// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]3985ba82010-07-29 21:44:127#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:428#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:189#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1210#include "base/path_service.h"
[email protected]34877b32010-07-31 17:47:0511#include "base/string_util.h"
[email protected]be1ce6a72010-08-03 14:35:2212#include "base/utf_string_conversions.h"
[email protected]8bcdec92009-02-25 16:15:1813#include "chrome/browser/browser.h"
[email protected]134c47b92009-08-19 03:33:4414#include "chrome/browser/browser_window.h"
15#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3716#include "chrome/browser/renderer_host/render_widget_host_view.h"
17#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0018#include "chrome/browser/tab_contents/tab_contents.h"
19#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]c848d3d92010-09-16 21:57:4520#include "chrome/browser/tabs/tab_strip_model.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3722#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5523#include "chrome/common/url_constants.h"
[email protected]8bcdec92009-02-25 16:15:1824#include "chrome/test/in_process_browser_test.h"
25#include "chrome/test/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1226#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5727
28#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0529#include "views/focus/focus_manager.h"
30#include "views/view.h"
31#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5732#endif
initial.commit09911bf2008-07-26 23:55:2933
[email protected]134c47b92009-08-19 03:33:4434#if defined(TOOLKIT_VIEWS)
35#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1736#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4437#include "chrome/browser/views/tab_contents/tab_contents_container.h"
38#endif
39
[email protected]753efc42010-03-09 19:52:1640#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1741#include "chrome/browser/gtk/view_id_util.h"
42#endif
43
[email protected]fc2e0872009-08-21 22:14:4144#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5745#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1846#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3547// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5948#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5749#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1550// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4851#define MAYBE_FocusTraversal DISABLED_FocusTraversal
52#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]853300a82010-07-27 21:17:5753// TODO(suzhe): http://crbug.com/49737
54#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
55#elif defined(OS_WIN)
56#define MAYBE_FocusTraversal FocusTraversal
[email protected]e4f4e0b2009-10-13 19:58:2157#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3558#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4159#endif
60
initial.commit09911bf2008-07-26 23:55:2961namespace {
62
[email protected]8bcdec92009-02-25 16:15:1863// The delay waited in some cases where we don't have a notifications for an
64// action we take.
initial.commit09911bf2008-07-26 23:55:2965const int kActionDelayMs = 500;
66
[email protected]f72a1cc2010-04-30 07:17:3067const char kSimplePage[] = "files/focus/page_with_focus.html";
68const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
69const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3170const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2971
[email protected]8bcdec92009-02-25 16:15:1872class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2973 public:
74 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1875 set_show_window(true);
76 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2977 }
[email protected]b9821882009-08-17 22:25:1778
[email protected]21abcc742009-10-23 02:52:0679 bool IsViewFocused(ViewID vid) {
80 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1781 }
82
[email protected]fc2e0872009-08-21 22:14:4183 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0684 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4185 }
initial.commit09911bf2008-07-26 23:55:2986};
87
[email protected]9e0c83a2009-05-06 19:44:3788class TestInterstitialPage : public InterstitialPage {
89 public:
90 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:3091 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:3192 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:3793 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
94 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:3195 file_path = file_path.AppendASCII("focus");
96 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:3797 r = file_util::ReadFileToString(file_path, &html_contents_);
98 EXPECT_TRUE(r);
99 }
100
101 virtual std::string GetHTMLContents() {
102 return html_contents_;
103 }
104
[email protected]a6e602f2010-09-28 22:28:30105 // Exposing render_view_host() to be public; it is declared as protected in
106 // the superclass.
107 virtual RenderViewHost* render_view_host() {
108 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37109 }
110
111 bool HasFocus() {
112 return render_view_host()->view()->HasFocus();
113 }
114
[email protected]130efb02009-09-18 18:54:35115 protected:
116 virtual void FocusedNodeChanged() {
[email protected]a6e602f2010-09-28 22:28:30117 NotificationService::current()->Notify(
118 NotificationType::FOCUS_CHANGED_IN_PAGE,
119 Source<RenderViewHost>(render_view_host()),
120 NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:35121 }
122
[email protected]9e0c83a2009-05-06 19:44:37123 private:
124 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37125};
[email protected]b9821882009-08-17 22:25:17126
[email protected]e4f4e0b2009-10-13 19:58:21127IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30128 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]853300a82010-07-27 21:17:57129#if defined(USE_X11) || defined(OS_MACOSX)
[email protected]fc2e0872009-08-21 22:14:41130 // It seems we have to wait a little bit for the widgets to spin up before
131 // we can start clicking on them.
132 MessageLoop::current()->PostDelayedTask(FROM_HERE,
133 new MessageLoop::QuitTask(),
134 kActionDelayMs);
135 ui_test_utils::RunMessageLoop();
136#endif
137
[email protected]21abcc742009-10-23 02:52:06138 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00139
[email protected]fc2e0872009-08-21 22:14:41140 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06141 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00142
[email protected]fc2e0872009-08-21 22:14:41143 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06144 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00145}
[email protected]186f13f2009-08-19 20:34:00146
[email protected]e4f4e0b2009-10-13 19:58:21147IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30148 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11149 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29150
151 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11152 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18153 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29154
[email protected]186f13f2009-08-19 20:34:00155 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
156
initial.commit09911bf2008-07-26 23:55:29157 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06158 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29159 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57160 ui_test_utils::HideNativeWindow(window);
161 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06162 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29163
[email protected]186f13f2009-08-19 20:34:00164 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06165 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29166 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57167 ui_test_utils::HideNativeWindow(window);
168 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06169 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29170
[email protected]186f13f2009-08-19 20:34:00171 // The rest of this test does not make sense on Linux because the behavior
172 // of Activate() is not well defined and can vary by window manager.
173#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29174 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18175 Browser* browser2 = Browser::Create(browser()->profile());
176 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43177 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18178 browser2->window()->Show();
179 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29180
[email protected]8bcdec92009-02-25 16:15:18181 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00182 BrowserView* browser_view2 =
183 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18184 ASSERT_TRUE(browser_view2);
185 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00186 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18187 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54188 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38189 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29190
191 // Switch to the 1st browser window, focus should still be on the location
192 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18193 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06194 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18195 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29196
197 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18198 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00199 EXPECT_EQ(NULL,
200 views::FocusManager::GetFocusManagerForNativeView(
201 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54202 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38203 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18204
205 // Close the 2nd browser to avoid a DCHECK().
206 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00207#endif
initial.commit09911bf2008-07-26 23:55:29208}
209
210// Tabs remember focus.
[email protected]e4f4e0b2009-10-13 19:58:21211IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30212 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11213 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29214
215 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11216 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18217 ui_test_utils::NavigateToURL(browser(), url);
218
initial.commit09911bf2008-07-26 23:55:29219 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33220 for (int i = 0; i < 4; ++i)
221 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29222
223 // Alternate focus for the tab.
224 const bool kFocusPage[3][5] = {
225 { true, true, true, true, false },
226 { false, false, false, false, false },
227 { false, true, false, true, false }
228 };
229
230 for (int i = 1; i < 3; i++) {
231 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18232 // Activate the tab.
233 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29234
235 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54236 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00237 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54238 } else {
[email protected]186f13f2009-08-19 20:34:00239 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54240 }
initial.commit09911bf2008-07-26 23:55:29241 }
242
243 // Now come back to the tab and check the right view is focused.
244 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18245 // Activate the tab.
246 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29247
[email protected]186f13f2009-08-19 20:34:00248 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
249 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06250 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29251 }
[email protected]cb7e2542009-12-14 22:02:35252
[email protected]cb7e2542009-12-14 22:02:35253 browser()->SelectTabContentsAt(0, true);
254 // Try the above, but with ctrl+tab. Since tab normally changes focus,
255 // this has regressed in the past. Loop through several times to be sure.
256 for (int j = 0; j < 15; j++) {
257 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
258 VIEW_ID_LOCATION_BAR;
259 ASSERT_TRUE(IsViewFocused(vid));
260
[email protected]1d000682010-08-23 16:21:28261 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30262 browser(), app::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35263 }
264
265 // As above, but with ctrl+shift+tab.
266 browser()->SelectTabContentsAt(4, true);
267 for (int j = 14; j >= 0; --j) {
268 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
269 VIEW_ID_LOCATION_BAR;
270 ASSERT_TRUE(IsViewFocused(vid));
271
[email protected]1d000682010-08-23 16:21:28272 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30273 browser(), app::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35274 }
initial.commit09911bf2008-07-26 23:55:29275 }
276}
277
[email protected]ae40b572009-10-02 21:17:45278// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35279IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30280 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11281 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45282
283 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11284 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45285 ui_test_utils::NavigateToURL(browser(), url);
286
287 browser()->Find();
288 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
289 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06290 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45291
292 // Focus the location bar.
293 browser()->FocusLocationBar();
294
295 // Create a 2nd tab.
[email protected]616381f02010-11-02 15:15:33296 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
[email protected]ae40b572009-10-02 21:17:45297
298 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06299 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45300
301 // Select 1st tab, focus should still be on the location-bar.
302 // (bug http://crbug.com/23296)
303 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06304 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45305
306 // Now open the find box again, switch to another tab and come back, the focus
307 // should return to the find box.
308 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06309 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45310 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06311 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45312 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06313 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45314}
315
initial.commit09911bf2008-07-26 23:55:29316// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21317IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30318 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11319 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29320
initial.commit09911bf2008-07-26 23:55:29321 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18322 Browser* browser2 = Browser::Create(browser()->profile());
323 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43324 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18325 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00326
[email protected]ed179ee2009-10-03 21:02:51327 Browser* focused_browser = NULL;
328 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16329#if defined(USE_X11)
330 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00331 // to figure out which browser does have focus.
332 if (browser2->window()->IsActive()) {
333 focused_browser = browser2;
334 unfocused_browser = browser();
335 } else if (browser()->window()->IsActive()) {
336 focused_browser = browser();
337 unfocused_browser = browser2;
338 } else {
[email protected]579c3d82010-10-06 03:53:51339 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00340 }
341#elif defined(OS_WIN)
342 focused_browser = browser();
343 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57344#elif defined(OS_MACOSX)
345 // On Mac, the newly created window always gets the focus.
346 focused_browser = browser2;
347 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00348#endif
349
[email protected]95409e12010-08-17 20:07:11350 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00351 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46352
[email protected]8bcdec92009-02-25 16:15:18353 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00354 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29355
[email protected]579c3d82010-10-06 03:53:51356 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
357 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
358 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29359
[email protected]8bcdec92009-02-25 16:15:18360 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00361 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29362}
363
364// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21365IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30366 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11367 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29368
369 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11370 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18371 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29372
[email protected]186f13f2009-08-19 20:34:00373 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29374
[email protected]579c3d82010-10-06 03:53:51375 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
376 browser()->GetSelectedTabContents()->render_view_host(), L"",
377 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29378
379 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06380 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29381}
382
[email protected]9e0c83a2009-05-06 19:44:37383// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35384// Note that this test relies on a notification from the renderer that the
385// focus has changed in the page. The notification in the renderer may change
386// at which point this test would fail (see comment in
387// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57388IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30389 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11390 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29391
[email protected]8bcdec92009-02-25 16:15:18392 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11393 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18394 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29395
[email protected]186f13f2009-08-19 20:34:00396 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29397
[email protected]8bcdec92009-02-25 16:15:18398 const char* kExpElementIDs[] = {
399 "", // Initially no element in the page should be focused
400 // (the location bar is focused).
401 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
402 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29403 };
404
405 // Test forward focus traversal.
406 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42407 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29408 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06409 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29410
411 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35412 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42413 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29414 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01415 std::string actual;
416 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25417 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18418 L"",
[email protected]45671612009-04-29 22:24:01419 L"window.domAutomationController.send(getFocusedElement());",
420 &actual));
initial.commit09911bf2008-07-26 23:55:29421 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
422
[email protected]a6e602f2010-09-28 22:28:30423 NotificationType::Type notification_type;
424 NotificationSource notification_source =
425 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35426 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30427 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
428 notification_source = Source<RenderViewHost>(
429 browser()->GetSelectedTabContents()->render_view_host());
[email protected]130efb02009-09-18 18:54:35430 } else {
431 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30432 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
433 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35434 }
[email protected]a6e602f2010-09-28 22:28:30435
436 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
437 browser(), app::VKEY_TAB, false, false, false, false,
438 notification_type, notification_source));
initial.commit09911bf2008-07-26 23:55:29439 }
[email protected]8bcdec92009-02-25 16:15:18440
441 // At this point the renderer has sent us a message asking to advance the
442 // focus (as the end of the focus loop was reached in the renderer).
443 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42444 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29445 }
446
447 // Now let's try reverse focus traversal.
448 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42449 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29450 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06451 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29452
[email protected]8bcdec92009-02-25 16:15:18453 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30454 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42455 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35456
[email protected]a6e602f2010-09-28 22:28:30457 NotificationType::Type notification_type;
458 NotificationSource notification_source =
459 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35460 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30461 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
462 notification_source = Source<RenderViewHost>(
463 browser()->GetSelectedTabContents()->render_view_host());
[email protected]130efb02009-09-18 18:54:35464 } else {
465 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30466 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
467 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35468 }
initial.commit09911bf2008-07-26 23:55:29469
[email protected]a6e602f2010-09-28 22:28:30470 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
471 browser(), app::VKEY_TAB, false, true, false, false,
472 notification_type, notification_source));
473
initial.commit09911bf2008-07-26 23:55:29474 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01475 std::string actual;
476 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25477 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18478 L"",
[email protected]45671612009-04-29 22:24:01479 L"window.domAutomationController.send(getFocusedElement());",
480 &actual));
initial.commit09911bf2008-07-26 23:55:29481 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
482 }
[email protected]8bcdec92009-02-25 16:15:18483
484 // At this point the renderer has sent us a message asking to advance the
485 // focus (as the end of the focus loop was reached in the renderer).
486 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42487 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29488 }
489}
490
[email protected]9e0c83a2009-05-06 19:44:37491// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41492IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30493 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11494 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37495
496 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11497 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37498 ui_test_utils::NavigateToURL(browser(), url);
499
[email protected]9e0c83a2009-05-06 19:44:37500 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06501 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37502
503 // Let's show an interstitial.
504 TestInterstitialPage* interstitial_page =
505 new TestInterstitialPage(browser()->GetSelectedTabContents(),
506 true, GURL("http://interstitial.com"));
507 interstitial_page->Show();
508 // Give some time for the interstitial to show.
509 MessageLoop::current()->PostDelayedTask(FROM_HERE,
510 new MessageLoop::QuitTask(),
511 1000);
512 ui_test_utils::RunMessageLoop();
513
[email protected]fc2e0872009-08-21 22:14:41514 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37515
516 const char* kExpElementIDs[] = {
517 "", // Initially no element in the page should be focused
518 // (the location bar is focused).
519 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
520 "gmapLink"
521 };
522
523 // Test forward focus traversal.
524 for (int i = 0; i < 2; ++i) {
525 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06526 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37527
528 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35529 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37530 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30531 std::string actual;
532 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
533 interstitial_page->render_view_host(), L"",
534 L"window.domAutomationController.send(getFocusedElement());",
535 &actual));
[email protected]9e0c83a2009-05-06 19:44:37536 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
537
[email protected]a6e602f2010-09-28 22:28:30538 NotificationType::Type notification_type;
539 NotificationSource notification_source =
540 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35541 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30542 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
543 notification_source = Source<RenderViewHost>(
544 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35545 } else {
546 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30547 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
548 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35549 }
[email protected]a6e602f2010-09-28 22:28:30550
551 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
552 browser(), app::VKEY_TAB, false, false, false, false,
553 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37554 }
555
556 // At this point the renderer has sent us a message asking to advance the
557 // focus (as the end of the focus loop was reached in the renderer).
558 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42559 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37560 }
561
562 // Now let's try reverse focus traversal.
563 for (int i = 0; i < 2; ++i) {
564 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06565 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37566
567 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35568 for (size_t j = 0; j < 7; ++j) {
[email protected]a6e602f2010-09-28 22:28:30569 NotificationType::Type notification_type;
570 NotificationSource notification_source =
571 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35572 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30573 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
574 notification_source = Source<RenderViewHost>(
575 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35576 } else {
577 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30578 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
579 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35580 }
[email protected]9e0c83a2009-05-06 19:44:37581
[email protected]a6e602f2010-09-28 22:28:30582 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
583 browser(), app::VKEY_TAB, false, true, false, false,
584 notification_type, notification_source));
585
[email protected]9e0c83a2009-05-06 19:44:37586 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30587 std::string actual;
588 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
589 interstitial_page->render_view_host(), L"",
590 L"window.domAutomationController.send(getFocusedElement());",
591 &actual));
[email protected]9e0c83a2009-05-06 19:44:37592 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
593 }
594
595 // At this point the renderer has sent us a message asking to advance the
596 // focus (as the end of the focus loop was reached in the renderer).
597 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42598 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37599 }
600}
601
602// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21603IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30604 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11605 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37606
607 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11608 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37609 ui_test_utils::NavigateToURL(browser(), url);
610
[email protected]9e0c83a2009-05-06 19:44:37611 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06612 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37613 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
614 HasFocus());
615
[email protected]9d8a4642009-07-29 17:25:30616 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37617 TestInterstitialPage* interstitial_page =
618 new TestInterstitialPage(browser()->GetSelectedTabContents(),
619 true, GURL("http://interstitial.com"));
620 interstitial_page->Show();
621 // Give some time for the interstitial to show.
622 MessageLoop::current()->PostDelayedTask(FROM_HERE,
623 new MessageLoop::QuitTask(),
624 1000);
625 ui_test_utils::RunMessageLoop();
626
627 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06628 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37629 EXPECT_TRUE(interstitial_page->HasFocus());
630
631 // Hide the interstitial.
632 interstitial_page->DontProceed();
633
634 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06635 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37636}
637
[email protected]9bd491ee2008-12-10 22:31:07638// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21639IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30640 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11641 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55642
[email protected]9bd491ee2008-12-10 22:31:07643 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11644 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18645 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07646
[email protected]853300a82010-07-27 21:17:57647#if defined(OS_MACOSX)
648 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28649 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30650 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57651#else
[email protected]9bd491ee2008-12-10 22:31:07652 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28653 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30654 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57655#endif
[email protected]8bcdec92009-02-25 16:15:18656
657 // Ideally, we wouldn't sleep here and instead would intercept the
658 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
659 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
660 // - creating a factory used to create the delegate
661 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41662 MessageLoop::current()->PostDelayedTask(
663 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18664 ui_test_utils::RunMessageLoop();
665
[email protected]21abcc742009-10-23 02:52:06666 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07667
[email protected]fc2e0872009-08-21 22:14:41668 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06669 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07670
671 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57672#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28673 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30674 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57675#else
[email protected]1d000682010-08-23 16:21:28676 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30677 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57678#endif
[email protected]21abcc742009-10-23 02:52:06679 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07680
681 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41682 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06683 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07684
685 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57686#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28687 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30688 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57689#else
[email protected]1d000682010-08-23 16:21:28690 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30691 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57692#endif
[email protected]8bcdec92009-02-25 16:15:18693
694 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41695 MessageLoop::current()->PostDelayedTask(
696 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18697 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06698 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07699}
[email protected]401513c2009-03-12 00:21:28700
701// Makes sure the focus is in the right location when opening the different
702// types of tabs.
[email protected]a3f343f2010-10-06 23:39:42703IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30704 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07705
[email protected]401513c2009-03-12 00:21:28706 // Open the history tab, focus should be on the tab contents.
707 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42708 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
709 &browser()->GetSelectedTabContents()->controller()));
710 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28711
712 // Open the new tab, focus should be on the location bar.
713 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42714 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
715 &browser()->GetSelectedTabContents()->controller()));
716 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28717
718 // Open the download tab, focus should be on the tab contents.
719 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42720 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
721 &browser()->GetSelectedTabContents()->controller()));
722 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43723
724 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55725 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
726 PageTransition::LINK);
[email protected]a3f343f2010-10-06 23:39:42727 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
728 &browser()->GetSelectedTabContents()->controller()));
729 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28730}
[email protected]9d8a4642009-07-29 17:25:30731
732// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21733IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30734 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11735 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30736
[email protected]9d8a4642009-07-29 17:25:30737 // Open the new tab, reload.
738 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42739 ui_test_utils::RunAllPendingInMessageLoop();
740
[email protected]cb84d642010-06-10 00:56:28741 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40742 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30743 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06744 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30745
746 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11747 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41748 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06749 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28750 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40751 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07752
[email protected]9d8a4642009-07-29 17:25:30753 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41754 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06755 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30756}
757
[email protected]9d8a4642009-07-29 17:25:30758// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27759IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30760 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11761 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30762
[email protected]9d8a4642009-07-29 17:25:30763 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11764 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30765 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28766 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40767 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07768
[email protected]9d8a4642009-07-29 17:25:30769 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41770 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06771 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30772}
[email protected]629e0342010-07-27 23:30:13773
774} // namespace