blob: 6368b0572ef9798971ad0a71a813301dbf1d8a3a [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]3a3cf4d2011-12-09 00:39:3553#if defined(OS_MACOSX)
54// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4855#define MAYBE_FocusTraversal DISABLED_FocusTraversal
56#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3557#else
58#define MAYBE_FocusTraversal FocusTraversal
59#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
60#endif
61
62#if defined(OS_LINUX) || defined(OS_MACOSX)
63// TODO(jcampan): http://crbug.com/23683 for linux.
64// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5765#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
66#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0767// Flaky, http://crbug.com/62537.
68#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4169#endif
70
initial.commit09911bf2008-07-26 23:55:2971namespace {
72
[email protected]8bcdec92009-02-25 16:15:1873// The delay waited in some cases where we don't have a notifications for an
74// action we take.
initial.commit09911bf2008-07-26 23:55:2975const int kActionDelayMs = 500;
76
[email protected]f72a1cc2010-04-30 07:17:3077const char kSimplePage[] = "files/focus/page_with_focus.html";
78const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
79const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3180const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2981
[email protected]a711df102010-12-14 09:47:0382// Test to make sure Chrome is in the foreground as we start testing. This is
83// required for tests that synthesize input to the Chrome window.
84bool ChromeInForeground() {
85#if defined(OS_WIN)
86 HWND window = ::GetForegroundWindow();
87 std::wstring caption;
88 std::wstring filename;
89 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:1890 if (len > 1)
91 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:0392 bool chrome_window_in_foreground =
93 EndsWith(caption, L" - Google Chrome", true) ||
94 EndsWith(caption, L" - Chromium", true);
95 if (!chrome_window_in_foreground) {
96 DWORD process_id;
97 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
98
99 base::ProcessHandle process;
100 if (base::OpenProcessHandleWithAccess(process_id,
101 PROCESS_QUERY_LIMITED_INFORMATION,
102 &process)) {
[email protected]fdce4782011-11-29 20:06:18103 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
104 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03105 int error = GetLastError();
106 filename = std::wstring(L"Unable to read filename for process id '" +
107 base::IntToString16(process_id) +
108 L"' (error ") +
109 base::IntToString16(error) + L")";
110 }
111 base::CloseProcessHandle(process);
112 }
113 }
114 EXPECT_TRUE(chrome_window_in_foreground)
115 << "Chrome must be in the foreground when running interactive tests\n"
116 << "Process in foreground: " << filename.c_str() << "\n"
117 << "Window: " << window << "\n"
118 << "Caption: " << caption.c_str();
119 return chrome_window_in_foreground;
120#else
121 // Windows only at the moment.
122 return true;
123#endif
124}
125
[email protected]8bcdec92009-02-25 16:15:18126class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29127 public:
[email protected]0b8fa8b2011-12-07 00:54:52128 BrowserFocusTest() :
129#if defined(USE_AURA)
130 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
131#else
132 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
133#endif
134 {
[email protected]8bcdec92009-02-25 16:15:18135 set_show_window(true);
136 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29137 }
[email protected]b9821882009-08-17 22:25:17138
[email protected]21abcc742009-10-23 02:52:06139 bool IsViewFocused(ViewID vid) {
140 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17141 }
142
[email protected]fc2e0872009-08-21 22:14:41143 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06144 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41145 }
[email protected]0b8fa8b2011-12-07 00:54:52146
147 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29148};
149
[email protected]9e0c83a2009-05-06 19:44:37150class TestInterstitialPage : public InterstitialPage {
151 public:
152 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30153 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31154 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37155 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
156 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31157 file_path = file_path.AppendASCII("focus");
158 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37159 r = file_util::ReadFileToString(file_path, &html_contents_);
160 EXPECT_TRUE(r);
161 }
162
163 virtual std::string GetHTMLContents() {
164 return html_contents_;
165 }
166
[email protected]4e85c112011-01-26 22:27:21167 // Exposing render_view_host() and tab() to be public; they are declared as
168 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30169 virtual RenderViewHost* render_view_host() {
170 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37171 }
172
[email protected]4e85c112011-01-26 22:27:21173 virtual TabContents* tab() {
174 return InterstitialPage::tab();
175 }
176
[email protected]9e0c83a2009-05-06 19:44:37177 bool HasFocus() {
178 return render_view_host()->view()->HasFocus();
179 }
180
181 private:
182 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37183};
[email protected]b9821882009-08-17 22:25:17184
[email protected]fab6ffb2011-09-22 18:35:34185IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30186 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42187#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41188 // It seems we have to wait a little bit for the widgets to spin up before
189 // we can start clicking on them.
190 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17191 MessageLoop::QuitClosure(),
[email protected]fc2e0872009-08-21 22:14:41192 kActionDelayMs);
193 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42194#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41195
[email protected]0b8fa8b2011-12-07 00:54:52196 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00197
[email protected]fc2e0872009-08-21 22:14:41198 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06199 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00200
[email protected]fc2e0872009-08-21 22:14:41201 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52202 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00203}
[email protected]186f13f2009-08-19 20:34:00204
[email protected]320948e02011-01-10 08:21:12205// Flaky, http://crbug.com/69034.
206IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30207 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11208 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29209
210 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11211 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18212 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29213
[email protected]186f13f2009-08-19 20:34:00214 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
215
initial.commit09911bf2008-07-26 23:55:29216 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06217 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29218 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57219 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29220 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06221 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29222
[email protected]186f13f2009-08-19 20:34:00223 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52224 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29225 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57226 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29227 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52228 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29229
[email protected]186f13f2009-08-19 20:34:00230 // The rest of this test does not make sense on Linux because the behavior
231 // of Activate() is not well defined and can vary by window manager.
232#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29233 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18234 Browser* browser2 = Browser::Create(browser()->profile());
235 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43236 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18237 browser2->window()->Show();
238 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29239
[email protected]0c966682011-08-02 18:22:10240 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00241 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16242 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18243 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50244 const views::Widget* widget2 =
245 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10246 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50247 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18248 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54249 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38250 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29251
252 // Switch to the 1st browser window, focus should still be on the location
253 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18254 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52255 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18256 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29257
258 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18259 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10260 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
261 ASSERT_TRUE(widget);
262 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54263 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38264 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18265
266 // Close the 2nd browser to avoid a DCHECK().
267 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00268#endif
initial.commit09911bf2008-07-26 23:55:29269}
270
271// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39272// Disabled, http://crbug.com/62542.
273IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30274 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11275 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29276
277 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11278 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18279 ui_test_utils::NavigateToURL(browser(), url);
280
initial.commit09911bf2008-07-26 23:55:29281 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33282 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58283 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29284
285 // Alternate focus for the tab.
286 const bool kFocusPage[3][5] = {
287 { true, true, true, true, false },
288 { false, false, false, false, false },
289 { false, true, false, true, false }
290 };
291
292 for (int i = 1; i < 3; i++) {
293 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18294 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41295 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29296
297 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54298 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00299 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54300 } else {
[email protected]186f13f2009-08-19 20:34:00301 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54302 }
initial.commit09911bf2008-07-26 23:55:29303 }
304
305 // Now come back to the tab and check the right view is focused.
306 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18307 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41308 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29309
[email protected]186f13f2009-08-19 20:34:00310 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52311 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06312 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29313 }
[email protected]cb7e2542009-12-14 22:02:35314
[email protected]eaca0ad12011-04-18 15:53:41315 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35316 // Try the above, but with ctrl+tab. Since tab normally changes focus,
317 // this has regressed in the past. Loop through several times to be sure.
318 for (int j = 0; j < 15; j++) {
319 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52320 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35321 ASSERT_TRUE(IsViewFocused(vid));
322
[email protected]1d000682010-08-23 16:21:28323 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09324 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35325 }
326
327 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41328 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35329 for (int j = 14; j >= 0; --j) {
330 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52331 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35332 ASSERT_TRUE(IsViewFocused(vid));
333
[email protected]1d000682010-08-23 16:21:28334 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09335 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35336 }
initial.commit09911bf2008-07-26 23:55:29337 }
338}
339
[email protected]ae40b572009-10-02 21:17:45340// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35341IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30342 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11343 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45344
345 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11346 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45347 ui_test_utils::NavigateToURL(browser(), url);
348
349 browser()->Find();
[email protected]9c318862011-02-01 22:27:24350 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45351 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06352 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45353
354 // Focus the location bar.
355 browser()->FocusLocationBar();
356
357 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58358 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45359
360 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06361 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45362
363 // Select 1st tab, focus should still be on the location-bar.
364 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41365 browser()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52366 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45367
368 // Now open the find box again, switch to another tab and come back, the focus
369 // should return to the find box.
370 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06371 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41372 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06373 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41374 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06375 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45376}
377
initial.commit09911bf2008-07-26 23:55:29378// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07379// Flaky, http://crbug.com/62538.
380IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
381 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30382 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11383 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29384
initial.commit09911bf2008-07-26 23:55:29385 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18386 Browser* browser2 = Browser::Create(browser()->profile());
387 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43388 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18389 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00390
[email protected]ed179ee2009-10-03 21:02:51391 Browser* focused_browser = NULL;
392 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16393#if defined(USE_X11)
394 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00395 // to figure out which browser does have focus.
396 if (browser2->window()->IsActive()) {
397 focused_browser = browser2;
398 unfocused_browser = browser();
399 } else if (browser()->window()->IsActive()) {
400 focused_browser = browser();
401 unfocused_browser = browser2;
402 } else {
[email protected]579c3d82010-10-06 03:53:51403 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00404 }
405#elif defined(OS_WIN)
406 focused_browser = browser();
407 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57408#elif defined(OS_MACOSX)
409 // On Mac, the newly created window always gets the focus.
410 focused_browser = browser2;
411 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00412#endif
413
[email protected]95409e12010-08-17 20:07:11414 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00415 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46416
[email protected]8bcdec92009-02-25 16:15:18417 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00418 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29419
[email protected]579c3d82010-10-06 03:53:51420 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
421 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
422 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29423
[email protected]8bcdec92009-02-25 16:15:18424 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00425 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29426}
427
428// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21429IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30430 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11431 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29432
433 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11434 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18435 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29436
[email protected]186f13f2009-08-19 20:34:00437 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29438
[email protected]579c3d82010-10-06 03:53:51439 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
440 browser()->GetSelectedTabContents()->render_view_host(), L"",
441 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29442
443 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52444 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29445}
446
[email protected]9e0c83a2009-05-06 19:44:37447// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35448// Note that this test relies on a notification from the renderer that the
449// focus has changed in the page. The notification in the renderer may change
450// at which point this test would fail (see comment in
451// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57452IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30453 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11454 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29455
[email protected]8bcdec92009-02-25 16:15:18456 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11457 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18458 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29459
[email protected]186f13f2009-08-19 20:34:00460 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29461
[email protected]546ae4e02010-12-08 14:57:19462 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18463 const char* kExpElementIDs[] = {
464 "", // Initially no element in the page should be focused
465 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19466 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18467 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29468 };
469
470 // Test forward focus traversal.
471 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40472 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29473 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52474 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29475
[email protected]911696b2011-01-28 02:36:49476 // Move the caret to the end, otherwise the next Tab key may not move focus.
477 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
478 browser(), ui::VKEY_END, false, false, false, false));
479
initial.commit09911bf2008-07-26 23:55:29480 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35481 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40482 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29483 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01484 std::string actual;
485 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25486 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18487 L"",
[email protected]45671612009-04-29 22:24:01488 L"window.domAutomationController.send(getFocusedElement());",
489 &actual));
initial.commit09911bf2008-07-26 23:55:29490 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
491
[email protected]130efb02009-09-18 18:54:35492 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19493 // If the next element is the kTextElementID, we expect to be
494 // notified we have switched to an editable node.
495 bool is_editable_node =
496 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53497 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19498
499 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09500 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27501 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56502 content::NotificationSource(content::Source<RenderViewHost>(
503 browser()->GetSelectedTabContents()->render_view_host())),
[email protected]546ae4e02010-12-08 14:57:19504 details));
[email protected]130efb02009-09-18 18:54:35505 } else {
506 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19507 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09508 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27509 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53510 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35511 }
initial.commit09911bf2008-07-26 23:55:29512 }
[email protected]8bcdec92009-02-25 16:15:18513
514 // At this point the renderer has sent us a message asking to advance the
515 // focus (as the end of the focus loop was reached in the renderer).
516 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42517 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29518 }
519
520 // Now let's try reverse focus traversal.
521 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40522 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29523 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52524 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29525
[email protected]911696b2011-01-28 02:36:49526 // Move the caret to the end, otherwise the next Tab key may not move focus.
527 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
528 browser(), ui::VKEY_END, false, false, false, false));
529
[email protected]8bcdec92009-02-25 16:15:18530 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30531 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40532 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19533 const char* next_element =
534 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35535
536 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19537 // If the next element is the kTextElementID, we expect to be
538 // notified we have switched to an editable node.
539 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53540 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19541
542 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09543 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27544 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56545 content::NotificationSource(content::Source<RenderViewHost>(
546 browser()->GetSelectedTabContents()->render_view_host())),
[email protected]546ae4e02010-12-08 14:57:19547 details));
[email protected]130efb02009-09-18 18:54:35548 } else {
549 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19550 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09551 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27552 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53553 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35554 }
initial.commit09911bf2008-07-26 23:55:29555
556 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01557 std::string actual;
558 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25559 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18560 L"",
[email protected]45671612009-04-29 22:24:01561 L"window.domAutomationController.send(getFocusedElement());",
562 &actual));
[email protected]546ae4e02010-12-08 14:57:19563 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29564 }
[email protected]8bcdec92009-02-25 16:15:18565
566 // At this point the renderer has sent us a message asking to advance the
567 // focus (as the end of the focus loop was reached in the renderer).
568 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42569 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29570 }
571}
572
[email protected]9e0c83a2009-05-06 19:44:37573// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41574IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30575 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11576 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37577
578 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11579 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37580 ui_test_utils::NavigateToURL(browser(), url);
581
[email protected]9e0c83a2009-05-06 19:44:37582 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06583 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37584
585 // Let's show an interstitial.
586 TestInterstitialPage* interstitial_page =
587 new TestInterstitialPage(browser()->GetSelectedTabContents(),
588 true, GURL("http://interstitial.com"));
589 interstitial_page->Show();
590 // Give some time for the interstitial to show.
591 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17592 MessageLoop::QuitClosure(),
[email protected]9e0c83a2009-05-06 19:44:37593 1000);
594 ui_test_utils::RunMessageLoop();
595
[email protected]fc2e0872009-08-21 22:14:41596 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37597
598 const char* kExpElementIDs[] = {
599 "", // Initially no element in the page should be focused
600 // (the location bar is focused).
601 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
602 "gmapLink"
603 };
604
605 // Test forward focus traversal.
606 for (int i = 0; i < 2; ++i) {
607 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52608 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37609
[email protected]911696b2011-01-28 02:36:49610 // Move the caret to the end, otherwise the next Tab key may not move focus.
611 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
612 browser(), ui::VKEY_END, false, false, false, false));
613
[email protected]9e0c83a2009-05-06 19:44:37614 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35615 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37616 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30617 std::string actual;
618 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
619 interstitial_page->render_view_host(), L"",
620 L"window.domAutomationController.send(getFocusedElement());",
621 &actual));
[email protected]9e0c83a2009-05-06 19:44:37622 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
623
[email protected]432115822011-07-10 15:52:27624 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53625 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07626 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35627 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27628 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56629 notification_source = content::Source<RenderViewHost>(
630 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35631 } else {
632 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27633 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53634 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35635 }
[email protected]a6e602f2010-09-28 22:28:30636
637 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09638 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30639 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37640 }
641
642 // At this point the renderer has sent us a message asking to advance the
643 // focus (as the end of the focus loop was reached in the renderer).
644 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42645 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37646 }
647
648 // Now let's try reverse focus traversal.
649 for (int i = 0; i < 2; ++i) {
650 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52651 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37652
[email protected]911696b2011-01-28 02:36:49653 // Move the caret to the end, otherwise the next Tab key may not move focus.
654 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
655 browser(), ui::VKEY_END, false, false, false, false));
656
[email protected]9e0c83a2009-05-06 19:44:37657 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35658 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27659 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53660 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07661 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35662 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27663 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56664 notification_source = content::Source<RenderViewHost>(
665 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35666 } else {
667 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27668 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53669 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35670 }
[email protected]9e0c83a2009-05-06 19:44:37671
[email protected]a6e602f2010-09-28 22:28:30672 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09673 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30674 notification_type, notification_source));
675
[email protected]9e0c83a2009-05-06 19:44:37676 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30677 std::string actual;
678 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
679 interstitial_page->render_view_host(), L"",
680 L"window.domAutomationController.send(getFocusedElement());",
681 &actual));
[email protected]9e0c83a2009-05-06 19:44:37682 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
683 }
684
685 // At this point the renderer has sent us a message asking to advance the
686 // focus (as the end of the focus loop was reached in the renderer).
687 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42688 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37689 }
690}
691
692// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33693// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05694#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]cb931932011-05-03 19:57:33695IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
696#else
[email protected]e4f4e0b2009-10-13 19:58:21697IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33698#endif
[email protected]a6e602f2010-09-28 22:28:30699 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11700 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37701
702 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11703 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37704 ui_test_utils::NavigateToURL(browser(), url);
705
[email protected]9e0c83a2009-05-06 19:44:37706 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06707 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37708 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
709 HasFocus());
710
[email protected]9d8a4642009-07-29 17:25:30711 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37712 TestInterstitialPage* interstitial_page =
713 new TestInterstitialPage(browser()->GetSelectedTabContents(),
714 true, GURL("http://interstitial.com"));
715 interstitial_page->Show();
716 // Give some time for the interstitial to show.
717 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17718 MessageLoop::QuitClosure(),
[email protected]9e0c83a2009-05-06 19:44:37719 1000);
720 ui_test_utils::RunMessageLoop();
721
722 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06723 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37724 EXPECT_TRUE(interstitial_page->HasFocus());
725
726 // Hide the interstitial.
727 interstitial_page->DontProceed();
728
729 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06730 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37731}
732
[email protected]9bd491ee2008-12-10 22:31:07733// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20734// Flaky on mac and valgrind. http://crbug.com/67301.
735#if defined(OS_MACOSX)
736#define MAYBE_FindFocusTest FLAKY_FindFocusTest
737#else
738#define MAYBE_FindFocusTest FindFocusTest
739#endif
[email protected]a711df102010-12-14 09:47:03740IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30741 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11742 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55743
[email protected]9bd491ee2008-12-10 22:31:07744 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11745 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18746 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07747
[email protected]a711df102010-12-14 09:47:03748 EXPECT_TRUE(ChromeInForeground());
749
[email protected]853300a82010-07-27 21:17:57750#if defined(OS_MACOSX)
751 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28752 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09753 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57754#else
[email protected]9bd491ee2008-12-10 22:31:07755 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28756 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09757 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57758#endif
[email protected]8bcdec92009-02-25 16:15:18759
760 // Ideally, we wouldn't sleep here and instead would intercept the
761 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
762 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
763 // - creating a factory used to create the delegate
764 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41765 MessageLoop::current()->PostDelayedTask(
[email protected]a778709f2011-12-10 00:28:17766 FROM_HERE, MessageLoop::QuitClosure(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18767 ui_test_utils::RunMessageLoop();
768
[email protected]21abcc742009-10-23 02:52:06769 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07770
[email protected]fc2e0872009-08-21 22:14:41771 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52772 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07773
774 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57775#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28776 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09777 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57778#else
[email protected]1d000682010-08-23 16:21:28779 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09780 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57781#endif
[email protected]21abcc742009-10-23 02:52:06782 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07783
784 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41785 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06786 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07787
788 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57789#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28790 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09791 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57792#else
[email protected]1d000682010-08-23 16:21:28793 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09794 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57795#endif
[email protected]8bcdec92009-02-25 16:15:18796
797 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41798 MessageLoop::current()->PostDelayedTask(
[email protected]a778709f2011-12-10 00:28:17799 FROM_HERE, MessageLoop::QuitClosure(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18800 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06801 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07802}
[email protected]401513c2009-03-12 00:21:28803
804// Makes sure the focus is in the right location when opening the different
805// types of tabs.
[email protected]c92928672010-11-09 18:31:07806// Flaky, http://crbug.com/62539.
807IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30808 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07809
[email protected]401513c2009-03-12 00:21:28810 // Open the history tab, focus should be on the tab contents.
811 browser()->ShowHistoryTab();
[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_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28815
816 // Open the new tab, focus should be on the location bar.
817 browser()->NewTab();
[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]0b8fa8b2011-12-07 00:54:52820 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28821
822 // Open the download tab, focus should be on the tab contents.
823 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42824 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54825 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42826 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43827
828 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55829 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58830 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42831 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54832 browser()->GetSelectedTabContents()));
[email protected]0b8fa8b2011-12-07 00:54:52833 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28834}
[email protected]9d8a4642009-07-29 17:25:30835
836// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21837IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30838 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11839 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30840
[email protected]9d8a4642009-07-29 17:25:30841 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06842 {
843 ui_test_utils::WindowedNotificationObserver observer(
844 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07845 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06846 browser()->NewTab();
847 observer.Wait();
848 }
[email protected]f07d7bf2010-04-06 08:02:42849 ui_test_utils::RunAllPendingInMessageLoop();
850
[email protected]6b4e8e42011-08-17 19:36:06851 {
852 ui_test_utils::WindowedNotificationObserver observer(
853 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53854 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06855 &browser()->GetSelectedTabContentsWrapper()->controller()));
856 browser()->Reload(CURRENT_TAB);
857 observer.Wait();
858 }
[email protected]9d8a4642009-07-29 17:25:30859 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52860 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30861
862 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11863 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41864 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52865 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06866 {
867 ui_test_utils::WindowedNotificationObserver observer(
868 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53869 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06870 &browser()->GetSelectedTabContentsWrapper()->controller()));
871 browser()->Reload(CURRENT_TAB);
872 observer.Wait();
873 }
[email protected]9ba21ede2010-07-30 01:11:07874
[email protected]9d8a4642009-07-29 17:25:30875 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41876 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06877 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30878}
879
880// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27881IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30882 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11883 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30884
[email protected]9d8a4642009-07-29 17:25:30885 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11886 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30887 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]6b4e8e42011-08-17 19:36:06888 {
889 ui_test_utils::WindowedNotificationObserver observer(
890 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53891 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06892 &browser()->GetSelectedTabContentsWrapper()->controller()));
893 browser()->Reload(CURRENT_TAB);
894 observer.Wait();
895 }
[email protected]9ba21ede2010-07-30 01:11:07896
[email protected]9d8a4642009-07-29 17:25:30897 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41898 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06899 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30900}
[email protected]629e0342010-07-27 23:30:13901
902} // namespace