blob: 9108dfd9bb9b1c7edbfd40d2844260a3bb4258b1 [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 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]a711df102010-12-14 09:47:0311#include "base/string_number_conversions.h"
[email protected]477ae052011-11-18 23:53:5712#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2213#include "base/utf_string_conversions.h"
[email protected]c848d3d92010-09-16 21:57:4514#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3316#include "chrome/browser/ui/browser_window.h"
[email protected]6b4e8e42011-08-17 19:36:0617#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]6a3ec2312010-12-02 19:30:1918#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2719#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3720#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5521#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3222#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5723#include "chrome/test/base/ui_test_utils.h"
[email protected]5de634712011-03-02 00:20:1924#include "content/browser/renderer_host/render_view_host.h"
25#include "content/browser/renderer_host/render_widget_host_view.h"
26#include "content/browser/tab_contents/interstitial_page.h"
27#include "content/browser/tab_contents/tab_contents.h"
28#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]477ae052011-11-18 23:53:5729#include "content/public/browser/notification_service.h"
[email protected]3985ba82010-07-29 21:44:1230#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5731
32#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]477ae052011-11-18 23:53:5733#include "ui/views/focus/focus_manager.h"
[email protected]5025f862011-11-30 23:35:2034#include "ui/views/view.h"
[email protected]853300a82010-07-27 21:17:5735#endif
initial.commit09911bf2008-07-26 23:55:2936
[email protected]134c47b92009-08-19 03:33:4437#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5438#include "chrome/browser/ui/views/frame/browser_view.h"
39#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
40#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4441#endif
42
[email protected]753efc42010-03-09 19:52:1643#if defined(TOOLKIT_USES_GTK)
[email protected]93270d002011-01-19 22:32:5944#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1745#endif
46
[email protected]a711df102010-12-14 09:47:0347#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0348#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5949#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4050#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0351#endif
52
[email protected]fc2e0872009-08-21 22:14:4153#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5754#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1855#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3556// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5957#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5758#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1559// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4860#define MAYBE_FocusTraversal DISABLED_FocusTraversal
61#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]853300a82010-07-27 21:17:5762// TODO(suzhe): http://crbug.com/49737
63#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
64#elif defined(OS_WIN)
[email protected]26153732010-11-09 18:47:3965// Disabled, http://crbug.com/62543.
[email protected]d53092652011-12-06 06:12:5666#define MAYBE_FocusTraversal FocusTraversal
[email protected]26153732010-11-09 18:47:3967// Disabled, http://crbug.com/62544.
[email protected]d53092652011-12-06 06:12:5668#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]c92928672010-11-09 18:31:0769// Flaky, http://crbug.com/62537.
70#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4171#endif
72
initial.commit09911bf2008-07-26 23:55:2973namespace {
74
[email protected]8bcdec92009-02-25 16:15:1875// The delay waited in some cases where we don't have a notifications for an
76// action we take.
initial.commit09911bf2008-07-26 23:55:2977const int kActionDelayMs = 500;
78
[email protected]f72a1cc2010-04-30 07:17:3079const char kSimplePage[] = "files/focus/page_with_focus.html";
80const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
81const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3182const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2983
[email protected]a711df102010-12-14 09:47:0384// Test to make sure Chrome is in the foreground as we start testing. This is
85// required for tests that synthesize input to the Chrome window.
86bool ChromeInForeground() {
87#if defined(OS_WIN)
88 HWND window = ::GetForegroundWindow();
89 std::wstring caption;
90 std::wstring filename;
91 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:1892 if (len > 1)
93 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:0394 bool chrome_window_in_foreground =
95 EndsWith(caption, L" - Google Chrome", true) ||
96 EndsWith(caption, L" - Chromium", true);
97 if (!chrome_window_in_foreground) {
98 DWORD process_id;
99 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
100
101 base::ProcessHandle process;
102 if (base::OpenProcessHandleWithAccess(process_id,
103 PROCESS_QUERY_LIMITED_INFORMATION,
104 &process)) {
[email protected]fdce4782011-11-29 20:06:18105 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
106 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03107 int error = GetLastError();
108 filename = std::wstring(L"Unable to read filename for process id '" +
109 base::IntToString16(process_id) +
110 L"' (error ") +
111 base::IntToString16(error) + L")";
112 }
113 base::CloseProcessHandle(process);
114 }
115 }
116 EXPECT_TRUE(chrome_window_in_foreground)
117 << "Chrome must be in the foreground when running interactive tests\n"
118 << "Process in foreground: " << filename.c_str() << "\n"
119 << "Window: " << window << "\n"
120 << "Caption: " << caption.c_str();
121 return chrome_window_in_foreground;
122#else
123 // Windows only at the moment.
124 return true;
125#endif
126}
127
[email protected]8bcdec92009-02-25 16:15:18128class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29129 public:
130 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:18131 set_show_window(true);
132 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29133 }
[email protected]b9821882009-08-17 22:25:17134
[email protected]21abcc742009-10-23 02:52:06135 bool IsViewFocused(ViewID vid) {
136 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17137 }
138
[email protected]fc2e0872009-08-21 22:14:41139 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06140 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41141 }
initial.commit09911bf2008-07-26 23:55:29142};
143
[email protected]9e0c83a2009-05-06 19:44:37144class TestInterstitialPage : public InterstitialPage {
145 public:
146 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30147 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31148 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37149 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
150 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31151 file_path = file_path.AppendASCII("focus");
152 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37153 r = file_util::ReadFileToString(file_path, &html_contents_);
154 EXPECT_TRUE(r);
155 }
156
157 virtual std::string GetHTMLContents() {
158 return html_contents_;
159 }
160
[email protected]4e85c112011-01-26 22:27:21161 // Exposing render_view_host() and tab() to be public; they are declared as
162 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30163 virtual RenderViewHost* render_view_host() {
164 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37165 }
166
[email protected]4e85c112011-01-26 22:27:21167 virtual TabContents* tab() {
168 return InterstitialPage::tab();
169 }
170
[email protected]9e0c83a2009-05-06 19:44:37171 bool HasFocus() {
172 return render_view_host()->view()->HasFocus();
173 }
174
[email protected]9e0c83a2009-05-06 19:44:37175 private:
176 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37177};
[email protected]b9821882009-08-17 22:25:17178
[email protected]fab6ffb2011-09-22 18:35:34179IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30180 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42181#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41182 // It seems we have to wait a little bit for the widgets to spin up before
183 // we can start clicking on them.
184 MessageLoop::current()->PostDelayedTask(FROM_HERE,
185 new MessageLoop::QuitTask(),
186 kActionDelayMs);
187 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42188#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41189
[email protected]21abcc742009-10-23 02:52:06190 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00191
[email protected]fc2e0872009-08-21 22:14:41192 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06193 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00194
[email protected]fc2e0872009-08-21 22:14:41195 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06196 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00197}
[email protected]186f13f2009-08-19 20:34:00198
[email protected]320948e02011-01-10 08:21:12199// Flaky, http://crbug.com/69034.
200IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30201 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11202 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29203
204 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11205 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18206 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29207
[email protected]186f13f2009-08-19 20:34:00208 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
209
initial.commit09911bf2008-07-26 23:55:29210 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06211 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29212 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57213 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29214 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06215 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29216
[email protected]186f13f2009-08-19 20:34:00217 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06218 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29219 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57220 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29221 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06222 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29223
[email protected]186f13f2009-08-19 20:34:00224 // The rest of this test does not make sense on Linux because the behavior
225 // of Activate() is not well defined and can vary by window manager.
226#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29227 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18228 Browser* browser2 = Browser::Create(browser()->profile());
229 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43230 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18231 browser2->window()->Show();
232 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29233
[email protected]0c966682011-08-02 18:22:10234 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00235 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16236 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18237 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50238 const views::Widget* widget2 =
239 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10240 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50241 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18242 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54243 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38244 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29245
246 // Switch to the 1st browser window, focus should still be on the location
247 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18248 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06249 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18250 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29251
252 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18253 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10254 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
255 ASSERT_TRUE(widget);
256 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54257 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38258 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18259
260 // Close the 2nd browser to avoid a DCHECK().
261 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00262#endif
initial.commit09911bf2008-07-26 23:55:29263}
264
265// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39266// Disabled, http://crbug.com/62542.
267IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30268 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11269 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29270
271 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11272 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18273 ui_test_utils::NavigateToURL(browser(), url);
274
initial.commit09911bf2008-07-26 23:55:29275 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33276 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58277 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29278
279 // Alternate focus for the tab.
280 const bool kFocusPage[3][5] = {
281 { true, true, true, true, false },
282 { false, false, false, false, false },
283 { false, true, false, true, false }
284 };
285
286 for (int i = 1; i < 3; i++) {
287 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18288 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41289 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29290
291 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54292 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00293 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54294 } else {
[email protected]186f13f2009-08-19 20:34:00295 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54296 }
initial.commit09911bf2008-07-26 23:55:29297 }
298
299 // Now come back to the tab and check the right view is focused.
300 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18301 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41302 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29303
[email protected]186f13f2009-08-19 20:34:00304 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
305 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06306 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29307 }
[email protected]cb7e2542009-12-14 22:02:35308
[email protected]eaca0ad12011-04-18 15:53:41309 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35310 // Try the above, but with ctrl+tab. Since tab normally changes focus,
311 // this has regressed in the past. Loop through several times to be sure.
312 for (int j = 0; j < 15; j++) {
313 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
314 VIEW_ID_LOCATION_BAR;
315 ASSERT_TRUE(IsViewFocused(vid));
316
[email protected]1d000682010-08-23 16:21:28317 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09318 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35319 }
320
321 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41322 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35323 for (int j = 14; j >= 0; --j) {
324 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
325 VIEW_ID_LOCATION_BAR;
326 ASSERT_TRUE(IsViewFocused(vid));
327
[email protected]1d000682010-08-23 16:21:28328 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09329 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35330 }
initial.commit09911bf2008-07-26 23:55:29331 }
332}
333
[email protected]ae40b572009-10-02 21:17:45334// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35335IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30336 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11337 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45338
339 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11340 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45341 ui_test_utils::NavigateToURL(browser(), url);
342
343 browser()->Find();
[email protected]9c318862011-02-01 22:27:24344 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45345 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06346 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45347
348 // Focus the location bar.
349 browser()->FocusLocationBar();
350
351 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58352 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45353
354 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06355 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45356
357 // Select 1st tab, focus should still be on the location-bar.
358 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41359 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06360 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45361
362 // Now open the find box again, switch to another tab and come back, the focus
363 // should return to the find box.
364 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06365 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41366 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06367 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41368 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06369 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45370}
371
initial.commit09911bf2008-07-26 23:55:29372// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07373// Flaky, http://crbug.com/62538.
374IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
375 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30376 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11377 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29378
initial.commit09911bf2008-07-26 23:55:29379 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18380 Browser* browser2 = Browser::Create(browser()->profile());
381 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43382 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18383 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00384
[email protected]ed179ee2009-10-03 21:02:51385 Browser* focused_browser = NULL;
386 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16387#if defined(USE_X11)
388 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00389 // to figure out which browser does have focus.
390 if (browser2->window()->IsActive()) {
391 focused_browser = browser2;
392 unfocused_browser = browser();
393 } else if (browser()->window()->IsActive()) {
394 focused_browser = browser();
395 unfocused_browser = browser2;
396 } else {
[email protected]579c3d82010-10-06 03:53:51397 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00398 }
399#elif defined(OS_WIN)
400 focused_browser = browser();
401 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57402#elif defined(OS_MACOSX)
403 // On Mac, the newly created window always gets the focus.
404 focused_browser = browser2;
405 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00406#endif
407
[email protected]95409e12010-08-17 20:07:11408 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00409 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46410
[email protected]8bcdec92009-02-25 16:15:18411 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00412 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29413
[email protected]579c3d82010-10-06 03:53:51414 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
415 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
416 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29417
[email protected]8bcdec92009-02-25 16:15:18418 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00419 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29420}
421
422// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21423IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30424 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11425 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29426
427 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11428 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18429 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29430
[email protected]186f13f2009-08-19 20:34:00431 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29432
[email protected]579c3d82010-10-06 03:53:51433 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
434 browser()->GetSelectedTabContents()->render_view_host(), L"",
435 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29436
437 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06438 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29439}
440
[email protected]9e0c83a2009-05-06 19:44:37441// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35442// Note that this test relies on a notification from the renderer that the
443// focus has changed in the page. The notification in the renderer may change
444// at which point this test would fail (see comment in
445// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57446IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30447 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11448 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29449
[email protected]8bcdec92009-02-25 16:15:18450 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11451 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18452 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29453
[email protected]186f13f2009-08-19 20:34:00454 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29455
[email protected]546ae4e02010-12-08 14:57:19456 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18457 const char* kExpElementIDs[] = {
458 "", // Initially no element in the page should be focused
459 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19460 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18461 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29462 };
463
464 // Test forward focus traversal.
465 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40466 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29467 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06468 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29469
[email protected]911696b2011-01-28 02:36:49470 // Move the caret to the end, otherwise the next Tab key may not move focus.
471 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
472 browser(), ui::VKEY_END, false, false, false, false));
473
initial.commit09911bf2008-07-26 23:55:29474 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35475 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40476 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29477 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01478 std::string actual;
479 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25480 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18481 L"",
[email protected]45671612009-04-29 22:24:01482 L"window.domAutomationController.send(getFocusedElement());",
483 &actual));
initial.commit09911bf2008-07-26 23:55:29484 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
485
[email protected]130efb02009-09-18 18:54:35486 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19487 // If the next element is the kTextElementID, we expect to be
488 // notified we have switched to an editable node.
489 bool is_editable_node =
490 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53491 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19492
493 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09494 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27495 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56496 content::NotificationSource(content::Source<RenderViewHost>(
497 browser()->GetSelectedTabContents()->render_view_host())),
[email protected]546ae4e02010-12-08 14:57:19498 details));
[email protected]130efb02009-09-18 18:54:35499 } else {
500 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19501 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09502 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27503 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53504 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35505 }
initial.commit09911bf2008-07-26 23:55:29506 }
[email protected]8bcdec92009-02-25 16:15:18507
508 // At this point the renderer has sent us a message asking to advance the
509 // focus (as the end of the focus loop was reached in the renderer).
510 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42511 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29512 }
513
514 // Now let's try reverse focus traversal.
515 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40516 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29517 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06518 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29519
[email protected]911696b2011-01-28 02:36:49520 // Move the caret to the end, otherwise the next Tab key may not move focus.
521 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
522 browser(), ui::VKEY_END, false, false, false, false));
523
[email protected]8bcdec92009-02-25 16:15:18524 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30525 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40526 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19527 const char* next_element =
528 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35529
530 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19531 // If the next element is the kTextElementID, we expect to be
532 // notified we have switched to an editable node.
533 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53534 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19535
536 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09537 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27538 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56539 content::NotificationSource(content::Source<RenderViewHost>(
540 browser()->GetSelectedTabContents()->render_view_host())),
[email protected]546ae4e02010-12-08 14:57:19541 details));
[email protected]130efb02009-09-18 18:54:35542 } else {
543 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19544 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09545 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27546 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53547 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35548 }
initial.commit09911bf2008-07-26 23:55:29549
550 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01551 std::string actual;
552 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25553 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18554 L"",
[email protected]45671612009-04-29 22:24:01555 L"window.domAutomationController.send(getFocusedElement());",
556 &actual));
[email protected]546ae4e02010-12-08 14:57:19557 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29558 }
[email protected]8bcdec92009-02-25 16:15:18559
560 // At this point the renderer has sent us a message asking to advance the
561 // focus (as the end of the focus loop was reached in the renderer).
562 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42563 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29564 }
565}
566
[email protected]9e0c83a2009-05-06 19:44:37567// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41568IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30569 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11570 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37571
572 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11573 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37574 ui_test_utils::NavigateToURL(browser(), url);
575
[email protected]9e0c83a2009-05-06 19:44:37576 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06577 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37578
579 // Let's show an interstitial.
580 TestInterstitialPage* interstitial_page =
581 new TestInterstitialPage(browser()->GetSelectedTabContents(),
582 true, GURL("http://interstitial.com"));
583 interstitial_page->Show();
584 // Give some time for the interstitial to show.
585 MessageLoop::current()->PostDelayedTask(FROM_HERE,
586 new MessageLoop::QuitTask(),
587 1000);
588 ui_test_utils::RunMessageLoop();
589
[email protected]fc2e0872009-08-21 22:14:41590 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37591
592 const char* kExpElementIDs[] = {
593 "", // Initially no element in the page should be focused
594 // (the location bar is focused).
595 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
596 "gmapLink"
597 };
598
599 // Test forward focus traversal.
600 for (int i = 0; i < 2; ++i) {
601 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06602 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37603
[email protected]911696b2011-01-28 02:36:49604 // Move the caret to the end, otherwise the next Tab key may not move focus.
605 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
606 browser(), ui::VKEY_END, false, false, false, false));
607
[email protected]9e0c83a2009-05-06 19:44:37608 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35609 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37610 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30611 std::string actual;
612 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
613 interstitial_page->render_view_host(), L"",
614 L"window.domAutomationController.send(getFocusedElement());",
615 &actual));
[email protected]9e0c83a2009-05-06 19:44:37616 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
617
[email protected]432115822011-07-10 15:52:27618 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53619 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07620 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35621 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27622 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56623 notification_source = content::Source<RenderViewHost>(
624 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35625 } else {
626 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27627 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53628 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35629 }
[email protected]a6e602f2010-09-28 22:28:30630
631 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09632 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30633 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37634 }
635
636 // At this point the renderer has sent us a message asking to advance the
637 // focus (as the end of the focus loop was reached in the renderer).
638 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42639 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37640 }
641
642 // Now let's try reverse focus traversal.
643 for (int i = 0; i < 2; ++i) {
644 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06645 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37646
[email protected]911696b2011-01-28 02:36:49647 // Move the caret to the end, otherwise the next Tab key may not move focus.
648 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
649 browser(), ui::VKEY_END, false, false, false, false));
650
[email protected]9e0c83a2009-05-06 19:44:37651 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35652 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27653 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53654 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07655 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35656 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27657 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56658 notification_source = content::Source<RenderViewHost>(
659 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35660 } else {
661 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27662 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53663 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35664 }
[email protected]9e0c83a2009-05-06 19:44:37665
[email protected]a6e602f2010-09-28 22:28:30666 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09667 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30668 notification_type, notification_source));
669
[email protected]9e0c83a2009-05-06 19:44:37670 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30671 std::string actual;
672 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
673 interstitial_page->render_view_host(), L"",
674 L"window.domAutomationController.send(getFocusedElement());",
675 &actual));
[email protected]9e0c83a2009-05-06 19:44:37676 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
677 }
678
679 // At this point the renderer has sent us a message asking to advance the
680 // focus (as the end of the focus loop was reached in the renderer).
681 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42682 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37683 }
684}
685
686// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33687// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05688#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]cb931932011-05-03 19:57:33689IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
690#else
[email protected]e4f4e0b2009-10-13 19:58:21691IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33692#endif
[email protected]a6e602f2010-09-28 22:28:30693 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11694 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37695
696 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11697 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37698 ui_test_utils::NavigateToURL(browser(), url);
699
[email protected]9e0c83a2009-05-06 19:44:37700 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06701 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37702 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
703 HasFocus());
704
[email protected]9d8a4642009-07-29 17:25:30705 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37706 TestInterstitialPage* interstitial_page =
707 new TestInterstitialPage(browser()->GetSelectedTabContents(),
708 true, GURL("http://interstitial.com"));
709 interstitial_page->Show();
710 // Give some time for the interstitial to show.
711 MessageLoop::current()->PostDelayedTask(FROM_HERE,
712 new MessageLoop::QuitTask(),
713 1000);
714 ui_test_utils::RunMessageLoop();
715
716 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06717 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37718 EXPECT_TRUE(interstitial_page->HasFocus());
719
720 // Hide the interstitial.
721 interstitial_page->DontProceed();
722
723 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06724 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37725}
726
[email protected]9bd491ee2008-12-10 22:31:07727// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20728// Flaky on mac and valgrind. http://crbug.com/67301.
729#if defined(OS_MACOSX)
730#define MAYBE_FindFocusTest FLAKY_FindFocusTest
731#else
732#define MAYBE_FindFocusTest FindFocusTest
733#endif
[email protected]a711df102010-12-14 09:47:03734IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30735 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11736 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55737
[email protected]9bd491ee2008-12-10 22:31:07738 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11739 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18740 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07741
[email protected]a711df102010-12-14 09:47:03742 EXPECT_TRUE(ChromeInForeground());
743
[email protected]853300a82010-07-27 21:17:57744#if defined(OS_MACOSX)
745 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28746 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09747 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57748#else
[email protected]9bd491ee2008-12-10 22:31:07749 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28750 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09751 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57752#endif
[email protected]8bcdec92009-02-25 16:15:18753
754 // Ideally, we wouldn't sleep here and instead would intercept the
755 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
756 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
757 // - creating a factory used to create the delegate
758 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41759 MessageLoop::current()->PostDelayedTask(
760 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18761 ui_test_utils::RunMessageLoop();
762
[email protected]21abcc742009-10-23 02:52:06763 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07764
[email protected]fc2e0872009-08-21 22:14:41765 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06766 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07767
768 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57769#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28770 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09771 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57772#else
[email protected]1d000682010-08-23 16:21:28773 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09774 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57775#endif
[email protected]21abcc742009-10-23 02:52:06776 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07777
778 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41779 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06780 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07781
782 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57783#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28784 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09785 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57786#else
[email protected]1d000682010-08-23 16:21:28787 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09788 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57789#endif
[email protected]8bcdec92009-02-25 16:15:18790
791 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41792 MessageLoop::current()->PostDelayedTask(
793 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18794 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06795 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07796}
[email protected]401513c2009-03-12 00:21:28797
798// Makes sure the focus is in the right location when opening the different
799// types of tabs.
[email protected]c92928672010-11-09 18:31:07800// Flaky, http://crbug.com/62539.
801IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30802 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07803
[email protected]401513c2009-03-12 00:21:28804 // Open the history tab, focus should be on the tab contents.
805 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42806 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54807 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42808 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28809
810 // Open the new tab, focus should be on the location bar.
811 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42812 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54813 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42814 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28815
816 // Open the download tab, focus should be on the tab contents.
817 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42818 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54819 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42820 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43821
822 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55823 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58824 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42825 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54826 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42827 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28828}
[email protected]9d8a4642009-07-29 17:25:30829
830// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21831IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30832 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11833 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30834
[email protected]9d8a4642009-07-29 17:25:30835 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06836 {
837 ui_test_utils::WindowedNotificationObserver observer(
838 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07839 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06840 browser()->NewTab();
841 observer.Wait();
842 }
[email protected]f07d7bf2010-04-06 08:02:42843 ui_test_utils::RunAllPendingInMessageLoop();
844
[email protected]6b4e8e42011-08-17 19:36:06845 {
846 ui_test_utils::WindowedNotificationObserver observer(
847 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53848 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06849 &browser()->GetSelectedTabContentsWrapper()->controller()));
850 browser()->Reload(CURRENT_TAB);
851 observer.Wait();
852 }
[email protected]9d8a4642009-07-29 17:25:30853 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06854 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30855
856 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11857 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41858 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06859 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]6b4e8e42011-08-17 19:36:06860 {
861 ui_test_utils::WindowedNotificationObserver observer(
862 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53863 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06864 &browser()->GetSelectedTabContentsWrapper()->controller()));
865 browser()->Reload(CURRENT_TAB);
866 observer.Wait();
867 }
[email protected]9ba21ede2010-07-30 01:11:07868
[email protected]9d8a4642009-07-29 17:25:30869 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41870 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06871 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30872}
873
874// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27875IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30876 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11877 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30878
[email protected]9d8a4642009-07-29 17:25:30879 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11880 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30881 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]6b4e8e42011-08-17 19:36:06882 {
883 ui_test_utils::WindowedNotificationObserver observer(
884 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53885 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06886 &browser()->GetSelectedTabContentsWrapper()->controller()));
887 browser()->Reload(CURRENT_TAB);
888 observer.Wait();
889 }
[email protected]9ba21ede2010-07-30 01:11:07890
[email protected]9d8a4642009-07-29 17:25:30891 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41892 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06893 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30894}
[email protected]629e0342010-07-27 23:30:13895
896} // namespace