blob: a32cadb7bb4047540c161a8b5245bec44f74b134 [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]1870d5cf2011-05-12 01:55:4011#include "base/stringprintf.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.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/ui_test_utils.h"
23#include "chrome/test/base/in_process_browser_test.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]ad50def52011-10-19 23:17:0729#include "content/public/browser/notification_service.h"
[email protected]216813952011-05-19 22:21:2630#include "content/common/view_messages.h"
[email protected]3985ba82010-07-29 21:44:1231#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5732
33#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0534#include "views/focus/focus_manager.h"
35#include "views/view.h"
[email protected]853300a82010-07-27 21:17:5736#endif
initial.commit09911bf2008-07-26 23:55:2937
[email protected]134c47b92009-08-19 03:33:4438#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5439#include "chrome/browser/ui/views/frame/browser_view.h"
40#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
41#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4442#endif
43
[email protected]753efc42010-03-09 19:52:1644#if defined(TOOLKIT_USES_GTK)
[email protected]93270d002011-01-19 22:32:5945#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1746#endif
47
[email protected]a711df102010-12-14 09:47:0348#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0349#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5950#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4051#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0352#endif
53
[email protected]fc2e0872009-08-21 22:14:4154#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5755#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1856#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3557// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5958#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5759#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1560// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4861#define MAYBE_FocusTraversal DISABLED_FocusTraversal
62#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]853300a82010-07-27 21:17:5763// TODO(suzhe): http://crbug.com/49737
64#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
65#elif defined(OS_WIN)
[email protected]26153732010-11-09 18:47:3966// Disabled, http://crbug.com/62543.
67#define MAYBE_FocusTraversal DISABLED_FocusTraversal
68// Disabled, http://crbug.com/62544.
69#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]c92928672010-11-09 18:31:0770// Flaky, http://crbug.com/62537.
71#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4172#endif
73
initial.commit09911bf2008-07-26 23:55:2974namespace {
75
[email protected]8bcdec92009-02-25 16:15:1876// The delay waited in some cases where we don't have a notifications for an
77// action we take.
initial.commit09911bf2008-07-26 23:55:2978const int kActionDelayMs = 500;
79
[email protected]f72a1cc2010-04-30 07:17:3080const char kSimplePage[] = "files/focus/page_with_focus.html";
81const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
82const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3183const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2984
[email protected]a711df102010-12-14 09:47:0385// Test to make sure Chrome is in the foreground as we start testing. This is
86// required for tests that synthesize input to the Chrome window.
87bool ChromeInForeground() {
88#if defined(OS_WIN)
89 HWND window = ::GetForegroundWindow();
90 std::wstring caption;
91 std::wstring filename;
92 int len = ::GetWindowTextLength(window) + 1;
93 ::GetWindowText(window, WriteInto(&caption, len), len);
94 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)) {
105 len = MAX_PATH;
106 if (!GetProcessImageFileName(process, WriteInto(&filename, len), len)) {
107 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]130efb02009-09-18 18:54:35175 protected:
[email protected]216813952011-05-19 22:21:26176 bool OnMessageReceived(const IPC::Message& message) {
177 bool handled = true;
178 IPC_BEGIN_MESSAGE_MAP(TestInterstitialPage, message)
179 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
180 IPC_MESSAGE_UNHANDLED(handled = false)
181 IPC_END_MESSAGE_MAP()
182 return handled;
183 }
184
185 void OnFocusedNodeChanged(bool is_editable_node) {
[email protected]ad50def52011-10-19 23:17:07186 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27187 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]6c2381d2011-10-19 02:52:53188 content::Source<TabContents>(tab()),
189 content::Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:35190 }
191
[email protected]9e0c83a2009-05-06 19:44:37192 private:
193 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37194};
[email protected]b9821882009-08-17 22:25:17195
[email protected]fab6ffb2011-09-22 18:35:34196IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30197 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42198#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41199 // It seems we have to wait a little bit for the widgets to spin up before
200 // we can start clicking on them.
201 MessageLoop::current()->PostDelayedTask(FROM_HERE,
202 new MessageLoop::QuitTask(),
203 kActionDelayMs);
204 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42205#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41206
[email protected]21abcc742009-10-23 02:52:06207 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00208
[email protected]fc2e0872009-08-21 22:14:41209 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06210 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00211
[email protected]fc2e0872009-08-21 22:14:41212 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06213 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00214}
[email protected]186f13f2009-08-19 20:34:00215
[email protected]320948e02011-01-10 08:21:12216// Flaky, http://crbug.com/69034.
217IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30218 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11219 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29220
221 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11222 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18223 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29224
[email protected]186f13f2009-08-19 20:34:00225 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
226
initial.commit09911bf2008-07-26 23:55:29227 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06228 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29229 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57230 ui_test_utils::HideNativeWindow(window);
231 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06232 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29233
[email protected]186f13f2009-08-19 20:34:00234 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06235 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29236 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57237 ui_test_utils::HideNativeWindow(window);
238 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06239 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29240
[email protected]186f13f2009-08-19 20:34:00241 // The rest of this test does not make sense on Linux because the behavior
242 // of Activate() is not well defined and can vary by window manager.
243#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29244 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18245 Browser* browser2 = Browser::Create(browser()->profile());
246 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43247 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18248 browser2->window()->Show();
249 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29250
[email protected]0c966682011-08-02 18:22:10251 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00252 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16253 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18254 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50255 const views::Widget* widget2 =
256 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10257 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50258 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18259 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54260 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38261 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29262
263 // Switch to the 1st browser window, focus should still be on the location
264 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18265 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06266 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18267 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29268
269 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18270 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10271 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
272 ASSERT_TRUE(widget);
273 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54274 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38275 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18276
277 // Close the 2nd browser to avoid a DCHECK().
278 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00279#endif
initial.commit09911bf2008-07-26 23:55:29280}
281
282// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39283// Disabled, http://crbug.com/62542.
284IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30285 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11286 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29287
288 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11289 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18290 ui_test_utils::NavigateToURL(browser(), url);
291
initial.commit09911bf2008-07-26 23:55:29292 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33293 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58294 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29295
296 // Alternate focus for the tab.
297 const bool kFocusPage[3][5] = {
298 { true, true, true, true, false },
299 { false, false, false, false, false },
300 { false, true, false, true, false }
301 };
302
303 for (int i = 1; i < 3; i++) {
304 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18305 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41306 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29307
308 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54309 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00310 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54311 } else {
[email protected]186f13f2009-08-19 20:34:00312 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54313 }
initial.commit09911bf2008-07-26 23:55:29314 }
315
316 // Now come back to the tab and check the right view is focused.
317 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18318 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41319 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29320
[email protected]186f13f2009-08-19 20:34:00321 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
322 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06323 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29324 }
[email protected]cb7e2542009-12-14 22:02:35325
[email protected]eaca0ad12011-04-18 15:53:41326 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35327 // Try the above, but with ctrl+tab. Since tab normally changes focus,
328 // this has regressed in the past. Loop through several times to be sure.
329 for (int j = 0; j < 15; j++) {
330 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
331 VIEW_ID_LOCATION_BAR;
332 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, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35336 }
337
338 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41339 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35340 for (int j = 14; j >= 0; --j) {
341 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
342 VIEW_ID_LOCATION_BAR;
343 ASSERT_TRUE(IsViewFocused(vid));
344
[email protected]1d000682010-08-23 16:21:28345 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09346 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35347 }
initial.commit09911bf2008-07-26 23:55:29348 }
349}
350
[email protected]ae40b572009-10-02 21:17:45351// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35352IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30353 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11354 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45355
356 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11357 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45358 ui_test_utils::NavigateToURL(browser(), url);
359
360 browser()->Find();
[email protected]9c318862011-02-01 22:27:24361 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45362 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06363 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45364
365 // Focus the location bar.
366 browser()->FocusLocationBar();
367
368 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58369 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45370
371 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06372 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45373
374 // Select 1st tab, focus should still be on the location-bar.
375 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41376 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06377 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45378
379 // Now open the find box again, switch to another tab and come back, the focus
380 // should return to the find box.
381 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06382 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41383 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06384 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41385 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06386 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45387}
388
initial.commit09911bf2008-07-26 23:55:29389// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07390// Flaky, http://crbug.com/62538.
391IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
392 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30393 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11394 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29395
initial.commit09911bf2008-07-26 23:55:29396 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18397 Browser* browser2 = Browser::Create(browser()->profile());
398 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43399 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18400 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00401
[email protected]ed179ee2009-10-03 21:02:51402 Browser* focused_browser = NULL;
403 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16404#if defined(USE_X11)
405 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00406 // to figure out which browser does have focus.
407 if (browser2->window()->IsActive()) {
408 focused_browser = browser2;
409 unfocused_browser = browser();
410 } else if (browser()->window()->IsActive()) {
411 focused_browser = browser();
412 unfocused_browser = browser2;
413 } else {
[email protected]579c3d82010-10-06 03:53:51414 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00415 }
416#elif defined(OS_WIN)
417 focused_browser = browser();
418 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57419#elif defined(OS_MACOSX)
420 // On Mac, the newly created window always gets the focus.
421 focused_browser = browser2;
422 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00423#endif
424
[email protected]95409e12010-08-17 20:07:11425 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00426 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46427
[email protected]8bcdec92009-02-25 16:15:18428 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00429 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29430
[email protected]579c3d82010-10-06 03:53:51431 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
432 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
433 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29434
[email protected]8bcdec92009-02-25 16:15:18435 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00436 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29437}
438
439// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21440IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30441 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11442 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29443
444 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11445 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18446 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29447
[email protected]186f13f2009-08-19 20:34:00448 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29449
[email protected]579c3d82010-10-06 03:53:51450 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
451 browser()->GetSelectedTabContents()->render_view_host(), L"",
452 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29453
454 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06455 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29456}
457
[email protected]9e0c83a2009-05-06 19:44:37458// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35459// Note that this test relies on a notification from the renderer that the
460// focus has changed in the page. The notification in the renderer may change
461// at which point this test would fail (see comment in
462// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57463IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30464 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11465 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29466
[email protected]8bcdec92009-02-25 16:15:18467 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11468 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18469 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29470
[email protected]186f13f2009-08-19 20:34:00471 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29472
[email protected]546ae4e02010-12-08 14:57:19473 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18474 const char* kExpElementIDs[] = {
475 "", // Initially no element in the page should be focused
476 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19477 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18478 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29479 };
480
481 // Test forward focus traversal.
482 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40483 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29484 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06485 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29486
[email protected]911696b2011-01-28 02:36:49487 // Move the caret to the end, otherwise the next Tab key may not move focus.
488 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
489 browser(), ui::VKEY_END, false, false, false, false));
490
initial.commit09911bf2008-07-26 23:55:29491 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35492 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40493 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29494 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01495 std::string actual;
496 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25497 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18498 L"",
[email protected]45671612009-04-29 22:24:01499 L"window.domAutomationController.send(getFocusedElement());",
500 &actual));
initial.commit09911bf2008-07-26 23:55:29501 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
502
[email protected]130efb02009-09-18 18:54:35503 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19504 // If the next element is the kTextElementID, we expect to be
505 // notified we have switched to an editable node.
506 bool is_editable_node =
507 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53508 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19509
510 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09511 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27512 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]6c2381d2011-10-19 02:52:53513 content::NotificationSource(content::Source<TabContents>(
[email protected]4e85c112011-01-26 22:27:21514 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19515 details));
[email protected]130efb02009-09-18 18:54:35516 } else {
517 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19518 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09519 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27520 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53521 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35522 }
initial.commit09911bf2008-07-26 23:55:29523 }
[email protected]8bcdec92009-02-25 16:15:18524
525 // At this point the renderer has sent us a message asking to advance the
526 // focus (as the end of the focus loop was reached in the renderer).
527 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42528 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29529 }
530
531 // Now let's try reverse focus traversal.
532 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40533 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29534 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06535 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29536
[email protected]911696b2011-01-28 02:36:49537 // Move the caret to the end, otherwise the next Tab key may not move focus.
538 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
539 browser(), ui::VKEY_END, false, false, false, false));
540
[email protected]8bcdec92009-02-25 16:15:18541 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30542 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40543 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19544 const char* next_element =
545 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35546
547 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19548 // If the next element is the kTextElementID, we expect to be
549 // notified we have switched to an editable node.
550 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53551 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19552
553 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09554 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27555 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]6c2381d2011-10-19 02:52:53556 content::NotificationSource(content::Source<TabContents>(
[email protected]4e85c112011-01-26 22:27:21557 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19558 details));
[email protected]130efb02009-09-18 18:54:35559 } else {
560 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19561 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09562 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27563 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53564 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35565 }
initial.commit09911bf2008-07-26 23:55:29566
567 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01568 std::string actual;
569 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25570 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18571 L"",
[email protected]45671612009-04-29 22:24:01572 L"window.domAutomationController.send(getFocusedElement());",
573 &actual));
[email protected]546ae4e02010-12-08 14:57:19574 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29575 }
[email protected]8bcdec92009-02-25 16:15:18576
577 // At this point the renderer has sent us a message asking to advance the
578 // focus (as the end of the focus loop was reached in the renderer).
579 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42580 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29581 }
582}
583
[email protected]9e0c83a2009-05-06 19:44:37584// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41585IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30586 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11587 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37588
589 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11590 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37591 ui_test_utils::NavigateToURL(browser(), url);
592
[email protected]9e0c83a2009-05-06 19:44:37593 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06594 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37595
596 // Let's show an interstitial.
597 TestInterstitialPage* interstitial_page =
598 new TestInterstitialPage(browser()->GetSelectedTabContents(),
599 true, GURL("http://interstitial.com"));
600 interstitial_page->Show();
601 // Give some time for the interstitial to show.
602 MessageLoop::current()->PostDelayedTask(FROM_HERE,
603 new MessageLoop::QuitTask(),
604 1000);
605 ui_test_utils::RunMessageLoop();
606
[email protected]fc2e0872009-08-21 22:14:41607 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37608
609 const char* kExpElementIDs[] = {
610 "", // Initially no element in the page should be focused
611 // (the location bar is focused).
612 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
613 "gmapLink"
614 };
615
616 // Test forward focus traversal.
617 for (int i = 0; i < 2; ++i) {
618 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06619 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37620
[email protected]911696b2011-01-28 02:36:49621 // Move the caret to the end, otherwise the next Tab key may not move focus.
622 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
623 browser(), ui::VKEY_END, false, false, false, false));
624
[email protected]9e0c83a2009-05-06 19:44:37625 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35626 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37627 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30628 std::string actual;
629 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
630 interstitial_page->render_view_host(), L"",
631 L"window.domAutomationController.send(getFocusedElement());",
632 &actual));
[email protected]9e0c83a2009-05-06 19:44:37633 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
634
[email protected]432115822011-07-10 15:52:27635 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53636 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07637 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35638 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27639 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]6c2381d2011-10-19 02:52:53640 notification_source = content::Source<TabContents>(
[email protected]4e85c112011-01-26 22:27:21641 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35642 } else {
643 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27644 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53645 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35646 }
[email protected]a6e602f2010-09-28 22:28:30647
648 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09649 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30650 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37651 }
652
653 // At this point the renderer has sent us a message asking to advance the
654 // focus (as the end of the focus loop was reached in the renderer).
655 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42656 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37657 }
658
659 // Now let's try reverse focus traversal.
660 for (int i = 0; i < 2; ++i) {
661 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06662 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37663
[email protected]911696b2011-01-28 02:36:49664 // Move the caret to the end, otherwise the next Tab key may not move focus.
665 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
666 browser(), ui::VKEY_END, false, false, false, false));
667
[email protected]9e0c83a2009-05-06 19:44:37668 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35669 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27670 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53671 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07672 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35673 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27674 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]6c2381d2011-10-19 02:52:53675 notification_source = content::Source<TabContents>(
[email protected]4e85c112011-01-26 22:27:21676 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35677 } else {
678 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27679 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53680 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35681 }
[email protected]9e0c83a2009-05-06 19:44:37682
[email protected]a6e602f2010-09-28 22:28:30683 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09684 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30685 notification_type, notification_source));
686
[email protected]9e0c83a2009-05-06 19:44:37687 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30688 std::string actual;
689 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
690 interstitial_page->render_view_host(), L"",
691 L"window.domAutomationController.send(getFocusedElement());",
692 &actual));
[email protected]9e0c83a2009-05-06 19:44:37693 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
694 }
695
696 // At this point the renderer has sent us a message asking to advance the
697 // focus (as the end of the focus loop was reached in the renderer).
698 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42699 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37700 }
701}
702
703// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33704// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05705#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]cb931932011-05-03 19:57:33706IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
707#else
[email protected]e4f4e0b2009-10-13 19:58:21708IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33709#endif
[email protected]a6e602f2010-09-28 22:28:30710 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11711 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37712
713 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11714 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37715 ui_test_utils::NavigateToURL(browser(), url);
716
[email protected]9e0c83a2009-05-06 19:44:37717 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06718 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37719 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
720 HasFocus());
721
[email protected]9d8a4642009-07-29 17:25:30722 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37723 TestInterstitialPage* interstitial_page =
724 new TestInterstitialPage(browser()->GetSelectedTabContents(),
725 true, GURL("http://interstitial.com"));
726 interstitial_page->Show();
727 // Give some time for the interstitial to show.
728 MessageLoop::current()->PostDelayedTask(FROM_HERE,
729 new MessageLoop::QuitTask(),
730 1000);
731 ui_test_utils::RunMessageLoop();
732
733 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06734 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37735 EXPECT_TRUE(interstitial_page->HasFocus());
736
737 // Hide the interstitial.
738 interstitial_page->DontProceed();
739
740 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06741 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37742}
743
[email protected]9bd491ee2008-12-10 22:31:07744// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20745// Flaky on mac and valgrind. http://crbug.com/67301.
746#if defined(OS_MACOSX)
747#define MAYBE_FindFocusTest FLAKY_FindFocusTest
748#else
749#define MAYBE_FindFocusTest FindFocusTest
750#endif
[email protected]a711df102010-12-14 09:47:03751IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30752 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11753 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55754
[email protected]9bd491ee2008-12-10 22:31:07755 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11756 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18757 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07758
[email protected]a711df102010-12-14 09:47:03759 EXPECT_TRUE(ChromeInForeground());
760
[email protected]853300a82010-07-27 21:17:57761#if defined(OS_MACOSX)
762 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28763 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09764 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57765#else
[email protected]9bd491ee2008-12-10 22:31:07766 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28767 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09768 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57769#endif
[email protected]8bcdec92009-02-25 16:15:18770
771 // Ideally, we wouldn't sleep here and instead would intercept the
772 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
773 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
774 // - creating a factory used to create the delegate
775 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41776 MessageLoop::current()->PostDelayedTask(
777 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18778 ui_test_utils::RunMessageLoop();
779
[email protected]21abcc742009-10-23 02:52:06780 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07781
[email protected]fc2e0872009-08-21 22:14:41782 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06783 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07784
785 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57786#if defined(OS_MACOSX)
[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, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57789#else
[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, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57792#endif
[email protected]21abcc742009-10-23 02:52:06793 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07794
795 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41796 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06797 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07798
799 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57800#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28801 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09802 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57803#else
[email protected]1d000682010-08-23 16:21:28804 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09805 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57806#endif
[email protected]8bcdec92009-02-25 16:15:18807
808 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41809 MessageLoop::current()->PostDelayedTask(
810 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18811 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06812 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07813}
[email protected]401513c2009-03-12 00:21:28814
815// Makes sure the focus is in the right location when opening the different
816// types of tabs.
[email protected]c92928672010-11-09 18:31:07817// Flaky, http://crbug.com/62539.
818IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30819 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07820
[email protected]401513c2009-03-12 00:21:28821 // Open the history tab, focus should be on the tab contents.
822 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42823 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54824 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42825 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28826
827 // Open the new tab, focus should be on the location bar.
828 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42829 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54830 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42831 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28832
833 // Open the download tab, focus should be on the tab contents.
834 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42835 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54836 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42837 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43838
839 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55840 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58841 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42842 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54843 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42844 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28845}
[email protected]9d8a4642009-07-29 17:25:30846
847// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21848IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30849 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11850 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30851
[email protected]9d8a4642009-07-29 17:25:30852 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06853 {
854 ui_test_utils::WindowedNotificationObserver observer(
855 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07856 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06857 browser()->NewTab();
858 observer.Wait();
859 }
[email protected]f07d7bf2010-04-06 08:02:42860 ui_test_utils::RunAllPendingInMessageLoop();
861
[email protected]6b4e8e42011-08-17 19:36:06862 {
863 ui_test_utils::WindowedNotificationObserver observer(
864 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53865 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06866 &browser()->GetSelectedTabContentsWrapper()->controller()));
867 browser()->Reload(CURRENT_TAB);
868 observer.Wait();
869 }
[email protected]9d8a4642009-07-29 17:25:30870 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06871 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30872
873 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11874 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41875 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06876 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]6b4e8e42011-08-17 19:36:06877 {
878 ui_test_utils::WindowedNotificationObserver observer(
879 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53880 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06881 &browser()->GetSelectedTabContentsWrapper()->controller()));
882 browser()->Reload(CURRENT_TAB);
883 observer.Wait();
884 }
[email protected]9ba21ede2010-07-30 01:11:07885
[email protected]9d8a4642009-07-29 17:25:30886 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41887 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06888 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30889}
890
891// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27892IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30893 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11894 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30895
[email protected]9d8a4642009-07-29 17:25:30896 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11897 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30898 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]6b4e8e42011-08-17 19:36:06899 {
900 ui_test_utils::WindowedNotificationObserver observer(
901 content::NOTIFICATION_LOAD_STOP,
[email protected]6c2381d2011-10-19 02:52:53902 content::Source<NavigationController>(
[email protected]6b4e8e42011-08-17 19:36:06903 &browser()->GetSelectedTabContentsWrapper()->controller()));
904 browser()->Reload(CURRENT_TAB);
905 observer.Wait();
906 }
[email protected]9ba21ede2010-07-30 01:11:07907
[email protected]9d8a4642009-07-29 17:25:30908 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41909 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06910 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30911}
[email protected]629e0342010-07-27 23:30:13912
913} // namespace