blob: 4f6dfa359b8d61855d73bbb8d4b663f825a5d5c6 [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)
50// TODO(suzhe): http://crbug.com/49738 (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]22735af62009-04-07 21:09:58220 for (int i = 0; i < 4; ++i) {
[email protected]cecc93a2010-10-05 15:58:55221 Browser::AddTabWithURLParams params(url, PageTransition::TYPED);
222 browser()->AddTabWithURL(&params);
223 EXPECT_EQ(browser(), params.target);
[email protected]22735af62009-04-07 21:09:58224 }
initial.commit09911bf2008-07-26 23:55:29225
226 // Alternate focus for the tab.
227 const bool kFocusPage[3][5] = {
228 { true, true, true, true, false },
229 { false, false, false, false, false },
230 { false, true, false, true, false }
231 };
232
233 for (int i = 1; i < 3; i++) {
234 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18235 // Activate the tab.
236 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29237
238 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54239 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00240 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54241 } else {
[email protected]186f13f2009-08-19 20:34:00242 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54243 }
initial.commit09911bf2008-07-26 23:55:29244 }
245
246 // Now come back to the tab and check the right view is focused.
247 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18248 // Activate the tab.
249 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29250
[email protected]186f13f2009-08-19 20:34:00251 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
252 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06253 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29254 }
[email protected]cb7e2542009-12-14 22:02:35255
[email protected]cb7e2542009-12-14 22:02:35256 browser()->SelectTabContentsAt(0, true);
257 // Try the above, but with ctrl+tab. Since tab normally changes focus,
258 // this has regressed in the past. Loop through several times to be sure.
259 for (int j = 0; j < 15; j++) {
260 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
261 VIEW_ID_LOCATION_BAR;
262 ASSERT_TRUE(IsViewFocused(vid));
263
[email protected]1d000682010-08-23 16:21:28264 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30265 browser(), app::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35266 }
267
268 // As above, but with ctrl+shift+tab.
269 browser()->SelectTabContentsAt(4, true);
270 for (int j = 14; j >= 0; --j) {
271 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
272 VIEW_ID_LOCATION_BAR;
273 ASSERT_TRUE(IsViewFocused(vid));
274
[email protected]1d000682010-08-23 16:21:28275 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30276 browser(), app::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35277 }
initial.commit09911bf2008-07-26 23:55:29278 }
279}
280
[email protected]ae40b572009-10-02 21:17:45281// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35282IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30283 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11284 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45285
286 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11287 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45288 ui_test_utils::NavigateToURL(browser(), url);
289
290 browser()->Find();
291 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
292 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06293 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45294
295 // Focus the location bar.
296 browser()->FocusLocationBar();
297
298 // Create a 2nd tab.
[email protected]cecc93a2010-10-05 15:58:55299 Browser::AddTabWithURLParams params(url, PageTransition::TYPED);
300 browser()->AddTabWithURL(&params);
301 EXPECT_EQ(browser(), params.target);
[email protected]ae40b572009-10-02 21:17:45302
303 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06304 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45305
306 // Select 1st tab, focus should still be on the location-bar.
307 // (bug http://crbug.com/23296)
308 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06309 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45310
311 // Now open the find box again, switch to another tab and come back, the focus
312 // should return to the find box.
313 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06314 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45315 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06316 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45317 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06318 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45319}
320
initial.commit09911bf2008-07-26 23:55:29321// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21322IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30323 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11324 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29325
initial.commit09911bf2008-07-26 23:55:29326 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18327 Browser* browser2 = Browser::Create(browser()->profile());
328 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43329 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18330 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00331
[email protected]ed179ee2009-10-03 21:02:51332 Browser* focused_browser = NULL;
333 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16334#if defined(USE_X11)
335 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00336 // to figure out which browser does have focus.
337 if (browser2->window()->IsActive()) {
338 focused_browser = browser2;
339 unfocused_browser = browser();
340 } else if (browser()->window()->IsActive()) {
341 focused_browser = browser();
342 unfocused_browser = browser2;
343 } else {
[email protected]579c3d82010-10-06 03:53:51344 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00345 }
346#elif defined(OS_WIN)
347 focused_browser = browser();
348 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57349#elif defined(OS_MACOSX)
350 // On Mac, the newly created window always gets the focus.
351 focused_browser = browser2;
352 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00353#endif
354
[email protected]95409e12010-08-17 20:07:11355 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00356 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46357
[email protected]8bcdec92009-02-25 16:15:18358 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00359 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29360
[email protected]579c3d82010-10-06 03:53:51361 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
362 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
363 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29364
[email protected]8bcdec92009-02-25 16:15:18365 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00366 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29367}
368
369// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21370IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30371 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11372 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29373
374 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11375 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18376 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29377
[email protected]186f13f2009-08-19 20:34:00378 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29379
[email protected]579c3d82010-10-06 03:53:51380 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
381 browser()->GetSelectedTabContents()->render_view_host(), L"",
382 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29383
384 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06385 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29386}
387
[email protected]9e0c83a2009-05-06 19:44:37388// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35389// Note that this test relies on a notification from the renderer that the
390// focus has changed in the page. The notification in the renderer may change
391// at which point this test would fail (see comment in
392// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57393IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30394 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11395 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29396
[email protected]8bcdec92009-02-25 16:15:18397 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11398 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18399 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29400
[email protected]186f13f2009-08-19 20:34:00401 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29402
[email protected]8bcdec92009-02-25 16:15:18403 const char* kExpElementIDs[] = {
404 "", // Initially no element in the page should be focused
405 // (the location bar is focused).
406 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
407 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29408 };
409
410 // Test forward focus traversal.
411 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42412 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29413 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06414 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29415
416 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35417 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42418 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29419 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01420 std::string actual;
421 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25422 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18423 L"",
[email protected]45671612009-04-29 22:24:01424 L"window.domAutomationController.send(getFocusedElement());",
425 &actual));
initial.commit09911bf2008-07-26 23:55:29426 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
427
[email protected]a6e602f2010-09-28 22:28:30428 NotificationType::Type notification_type;
429 NotificationSource notification_source =
430 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35431 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30432 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
433 notification_source = Source<RenderViewHost>(
434 browser()->GetSelectedTabContents()->render_view_host());
[email protected]130efb02009-09-18 18:54:35435 } else {
436 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30437 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
438 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35439 }
[email protected]a6e602f2010-09-28 22:28:30440
441 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
442 browser(), app::VKEY_TAB, false, false, false, false,
443 notification_type, notification_source));
initial.commit09911bf2008-07-26 23:55:29444 }
[email protected]8bcdec92009-02-25 16:15:18445
446 // At this point the renderer has sent us a message asking to advance the
447 // focus (as the end of the focus loop was reached in the renderer).
448 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42449 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29450 }
451
452 // Now let's try reverse focus traversal.
453 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42454 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29455 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06456 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29457
[email protected]8bcdec92009-02-25 16:15:18458 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30459 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42460 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35461
[email protected]a6e602f2010-09-28 22:28:30462 NotificationType::Type notification_type;
463 NotificationSource notification_source =
464 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35465 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30466 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
467 notification_source = Source<RenderViewHost>(
468 browser()->GetSelectedTabContents()->render_view_host());
[email protected]130efb02009-09-18 18:54:35469 } else {
470 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30471 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
472 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35473 }
initial.commit09911bf2008-07-26 23:55:29474
[email protected]a6e602f2010-09-28 22:28:30475 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
476 browser(), app::VKEY_TAB, false, true, false, false,
477 notification_type, notification_source));
478
initial.commit09911bf2008-07-26 23:55:29479 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01480 std::string actual;
481 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25482 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18483 L"",
[email protected]45671612009-04-29 22:24:01484 L"window.domAutomationController.send(getFocusedElement());",
485 &actual));
initial.commit09911bf2008-07-26 23:55:29486 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
487 }
[email protected]8bcdec92009-02-25 16:15:18488
489 // At this point the renderer has sent us a message asking to advance the
490 // focus (as the end of the focus loop was reached in the renderer).
491 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42492 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29493 }
494}
495
[email protected]9e0c83a2009-05-06 19:44:37496// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41497IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30498 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11499 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37500
501 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11502 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37503 ui_test_utils::NavigateToURL(browser(), url);
504
[email protected]9e0c83a2009-05-06 19:44:37505 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06506 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37507
508 // Let's show an interstitial.
509 TestInterstitialPage* interstitial_page =
510 new TestInterstitialPage(browser()->GetSelectedTabContents(),
511 true, GURL("http://interstitial.com"));
512 interstitial_page->Show();
513 // Give some time for the interstitial to show.
514 MessageLoop::current()->PostDelayedTask(FROM_HERE,
515 new MessageLoop::QuitTask(),
516 1000);
517 ui_test_utils::RunMessageLoop();
518
[email protected]fc2e0872009-08-21 22:14:41519 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37520
521 const char* kExpElementIDs[] = {
522 "", // Initially no element in the page should be focused
523 // (the location bar is focused).
524 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
525 "gmapLink"
526 };
527
528 // Test forward focus traversal.
529 for (int i = 0; i < 2; ++i) {
530 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06531 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37532
533 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35534 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37535 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30536 std::string actual;
537 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
538 interstitial_page->render_view_host(), L"",
539 L"window.domAutomationController.send(getFocusedElement());",
540 &actual));
[email protected]9e0c83a2009-05-06 19:44:37541 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
542
[email protected]a6e602f2010-09-28 22:28:30543 NotificationType::Type notification_type;
544 NotificationSource notification_source =
545 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35546 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30547 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
548 notification_source = Source<RenderViewHost>(
549 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35550 } else {
551 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30552 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
553 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35554 }
[email protected]a6e602f2010-09-28 22:28:30555
556 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
557 browser(), app::VKEY_TAB, false, false, false, false,
558 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37559 }
560
561 // At this point the renderer has sent us a message asking to advance the
562 // focus (as the end of the focus loop was reached in the renderer).
563 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42564 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37565 }
566
567 // Now let's try reverse focus traversal.
568 for (int i = 0; i < 2; ++i) {
569 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06570 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37571
572 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35573 for (size_t j = 0; j < 7; ++j) {
[email protected]a6e602f2010-09-28 22:28:30574 NotificationType::Type notification_type;
575 NotificationSource notification_source =
576 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35577 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30578 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
579 notification_source = Source<RenderViewHost>(
580 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35581 } else {
582 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30583 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
584 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35585 }
[email protected]9e0c83a2009-05-06 19:44:37586
[email protected]a6e602f2010-09-28 22:28:30587 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
588 browser(), app::VKEY_TAB, false, true, false, false,
589 notification_type, notification_source));
590
[email protected]9e0c83a2009-05-06 19:44:37591 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30592 std::string actual;
593 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
594 interstitial_page->render_view_host(), L"",
595 L"window.domAutomationController.send(getFocusedElement());",
596 &actual));
[email protected]9e0c83a2009-05-06 19:44:37597 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
598 }
599
600 // At this point the renderer has sent us a message asking to advance the
601 // focus (as the end of the focus loop was reached in the renderer).
602 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42603 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37604 }
605}
606
607// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21608IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30609 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11610 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37611
612 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11613 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37614 ui_test_utils::NavigateToURL(browser(), url);
615
[email protected]9e0c83a2009-05-06 19:44:37616 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06617 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37618 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
619 HasFocus());
620
[email protected]9d8a4642009-07-29 17:25:30621 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37622 TestInterstitialPage* interstitial_page =
623 new TestInterstitialPage(browser()->GetSelectedTabContents(),
624 true, GURL("http://interstitial.com"));
625 interstitial_page->Show();
626 // Give some time for the interstitial to show.
627 MessageLoop::current()->PostDelayedTask(FROM_HERE,
628 new MessageLoop::QuitTask(),
629 1000);
630 ui_test_utils::RunMessageLoop();
631
632 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06633 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37634 EXPECT_TRUE(interstitial_page->HasFocus());
635
636 // Hide the interstitial.
637 interstitial_page->DontProceed();
638
639 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06640 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37641}
642
[email protected]9bd491ee2008-12-10 22:31:07643// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21644IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30645 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11646 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55647
[email protected]9bd491ee2008-12-10 22:31:07648 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11649 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18650 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07651
[email protected]853300a82010-07-27 21:17:57652#if defined(OS_MACOSX)
653 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28654 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30655 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57656#else
[email protected]9bd491ee2008-12-10 22:31:07657 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28658 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30659 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57660#endif
[email protected]8bcdec92009-02-25 16:15:18661
662 // Ideally, we wouldn't sleep here and instead would intercept the
663 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
664 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
665 // - creating a factory used to create the delegate
666 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41667 MessageLoop::current()->PostDelayedTask(
668 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18669 ui_test_utils::RunMessageLoop();
670
[email protected]21abcc742009-10-23 02:52:06671 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07672
[email protected]fc2e0872009-08-21 22:14:41673 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06674 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07675
676 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57677#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28678 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30679 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57680#else
[email protected]1d000682010-08-23 16:21:28681 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30682 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57683#endif
[email protected]21abcc742009-10-23 02:52:06684 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07685
686 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41687 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06688 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07689
690 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57691#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28692 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30693 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57694#else
[email protected]1d000682010-08-23 16:21:28695 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30696 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57697#endif
[email protected]8bcdec92009-02-25 16:15:18698
699 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41700 MessageLoop::current()->PostDelayedTask(
701 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18702 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06703 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07704}
[email protected]401513c2009-03-12 00:21:28705
706// Makes sure the focus is in the right location when opening the different
707// types of tabs.
[email protected]a3f343f2010-10-06 23:39:42708IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30709 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07710
[email protected]401513c2009-03-12 00:21:28711 // Open the history tab, focus should be on the tab contents.
712 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42713 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
714 &browser()->GetSelectedTabContents()->controller()));
715 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28716
717 // Open the new tab, focus should be on the location bar.
718 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42719 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
720 &browser()->GetSelectedTabContents()->controller()));
721 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28722
723 // Open the download tab, focus should be on the tab contents.
724 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42725 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
726 &browser()->GetSelectedTabContents()->controller()));
727 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43728
729 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55730 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
731 PageTransition::LINK);
[email protected]a3f343f2010-10-06 23:39:42732 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
733 &browser()->GetSelectedTabContents()->controller()));
734 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28735}
[email protected]9d8a4642009-07-29 17:25:30736
737// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21738IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30739 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11740 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30741
[email protected]9d8a4642009-07-29 17:25:30742 // Open the new tab, reload.
743 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42744 ui_test_utils::RunAllPendingInMessageLoop();
745
[email protected]cb84d642010-06-10 00:56:28746 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40747 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30748 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06749 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30750
751 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11752 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41753 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06754 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28755 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40756 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07757
[email protected]9d8a4642009-07-29 17:25:30758 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41759 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06760 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30761}
762
[email protected]0e7c82ed2010-08-31 20:39:19763#if (defined(OS_CHROMEOS) || defined(OS_LINUX)) && !defined(NDEBUG)
764// Hangy, http://crbug.com/50025.
[email protected]c38a1902010-07-23 20:55:15765#define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab
[email protected]517d2fe72010-07-23 15:40:41766#else
767#define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab
768#endif
769
[email protected]9d8a4642009-07-29 17:25:30770// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]517d2fe72010-07-23 15:40:41771IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30772 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11773 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30774
[email protected]9d8a4642009-07-29 17:25:30775 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11776 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30777 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28778 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40779 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07780
[email protected]9d8a4642009-07-29 17:25:30781 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41782 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06783 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30784}
[email protected]629e0342010-07-27 23:30:13785
786} // namespace