blob: f6c30abad4a77e5f5e23265b354be44826c7fd6c [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 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]a9c060ca2012-01-05 20:43:417#include "base/bind.h"
[email protected]3985ba82010-07-29 21:44:128#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:429#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:1810#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1211#include "base/path_service.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]477ae052011-11-18 23:53:5713#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2214#include "base/utf_string_conversions.h"
[email protected]c848d3d92010-09-16 21:57:4515#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1016#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3317#include "chrome/browser/ui/browser_window.h"
[email protected]6b4e8e42011-08-17 19:36:0618#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]6a3ec2312010-12-02 19:30:1919#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2720#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3721#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5522#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3223#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5724#include "chrome/test/base/ui_test_utils.h"
[email protected]5de634712011-03-02 00:20:1925#include "content/browser/renderer_host/render_view_host.h"
26#include "content/browser/renderer_host/render_widget_host_view.h"
27#include "content/browser/tab_contents/interstitial_page.h"
[email protected]477ae052011-11-18 23:53:5728#include "content/public/browser/notification_service.h"
[email protected]6acde6352012-01-04 16:52:2029#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1030#include "content/public/browser/web_contents_view.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]477ae052011-11-18 23:53:5734#include "ui/views/focus/focus_manager.h"
[email protected]5025f862011-11-30 23:35:2035#include "ui/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]c5eed492012-01-04 17:07:5054using content::NavigationController;
[email protected]4b19ea52012-01-02 20:15:2555using content::WebContents;
56
[email protected]3a3cf4d2011-12-09 00:39:3557#if defined(OS_MACOSX)
58// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4859#define MAYBE_FocusTraversal DISABLED_FocusTraversal
60#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4861#elif defined(OS_WIN)
62// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1863#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4864#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3565#else
66#define MAYBE_FocusTraversal FocusTraversal
67#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
68#endif
69
70#if defined(OS_LINUX) || defined(OS_MACOSX)
71// TODO(jcampan): http://crbug.com/23683 for linux.
72// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5773#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
74#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0775// Flaky, http://crbug.com/62537.
76#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4177#endif
78
initial.commit09911bf2008-07-26 23:55:2979namespace {
80
[email protected]8bcdec92009-02-25 16:15:1881// The delay waited in some cases where we don't have a notifications for an
82// action we take.
initial.commit09911bf2008-07-26 23:55:2983const int kActionDelayMs = 500;
84
[email protected]a9c060ca2012-01-05 20:43:4185// Maxiumum time to wait until the focus is moved to expected view.
86const int kFocusChangeTimeoutMs = 500;
87
[email protected]f72a1cc2010-04-30 07:17:3088const char kSimplePage[] = "files/focus/page_with_focus.html";
89const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
90const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3191const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2992
[email protected]a711df102010-12-14 09:47:0393// Test to make sure Chrome is in the foreground as we start testing. This is
94// required for tests that synthesize input to the Chrome window.
95bool ChromeInForeground() {
96#if defined(OS_WIN)
97 HWND window = ::GetForegroundWindow();
98 std::wstring caption;
99 std::wstring filename;
100 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18101 if (len > 1)
102 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03103 bool chrome_window_in_foreground =
104 EndsWith(caption, L" - Google Chrome", true) ||
105 EndsWith(caption, L" - Chromium", true);
106 if (!chrome_window_in_foreground) {
107 DWORD process_id;
108 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
109
110 base::ProcessHandle process;
111 if (base::OpenProcessHandleWithAccess(process_id,
112 PROCESS_QUERY_LIMITED_INFORMATION,
113 &process)) {
[email protected]fdce4782011-11-29 20:06:18114 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
115 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03116 int error = GetLastError();
117 filename = std::wstring(L"Unable to read filename for process id '" +
118 base::IntToString16(process_id) +
119 L"' (error ") +
120 base::IntToString16(error) + L")";
121 }
122 base::CloseProcessHandle(process);
123 }
124 }
125 EXPECT_TRUE(chrome_window_in_foreground)
126 << "Chrome must be in the foreground when running interactive tests\n"
127 << "Process in foreground: " << filename.c_str() << "\n"
128 << "Window: " << window << "\n"
129 << "Caption: " << caption.c_str();
130 return chrome_window_in_foreground;
131#else
132 // Windows only at the moment.
133 return true;
134#endif
135}
136
[email protected]a9c060ca2012-01-05 20:43:41137// Wait the focus change in message loop.
138void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
139 if (ui_test_utils::IsViewFocused(browser, id) ||
140 base::Time::Now() > timeout) {
141 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
142 } else {
143 MessageLoop::current()->PostDelayedTask(
144 FROM_HERE,
145 base::Bind(&CheckFocus, browser, id, timeout),
146 10);
147 }
148};
149
[email protected]8bcdec92009-02-25 16:15:18150class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29151 public:
[email protected]0b8fa8b2011-12-07 00:54:52152 BrowserFocusTest() :
153#if defined(USE_AURA)
154 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
155#else
156 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
157#endif
158 {
[email protected]8bcdec92009-02-25 16:15:18159 set_show_window(true);
160 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29161 }
[email protected]b9821882009-08-17 22:25:17162
[email protected]21abcc742009-10-23 02:52:06163 bool IsViewFocused(ViewID vid) {
164 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17165 }
166
[email protected]fc2e0872009-08-21 22:14:41167 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06168 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41169 }
[email protected]0b8fa8b2011-12-07 00:54:52170
[email protected]a9c060ca2012-01-05 20:43:41171 bool WaitForFocusChange(ViewID vid) {
172 const base::Time timeout = base::Time::Now() +
173 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
174 MessageLoop::current()->PostDelayedTask(
175 FROM_HERE,
176 base::Bind(&CheckFocus, browser(), vid, timeout),
177 100);
178 ui_test_utils::RunMessageLoop();
179 return IsViewFocused(vid);
180 }
181
[email protected]0b8fa8b2011-12-07 00:54:52182 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29183};
184
[email protected]9e0c83a2009-05-06 19:44:37185class TestInterstitialPage : public InterstitialPage {
186 public:
[email protected]4ca15302012-01-03 05:53:20187 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30188 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31189 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37190 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
191 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31192 file_path = file_path.AppendASCII("focus");
193 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37194 r = file_util::ReadFileToString(file_path, &html_contents_);
195 EXPECT_TRUE(r);
196 }
197
198 virtual std::string GetHTMLContents() {
199 return html_contents_;
200 }
201
[email protected]4e85c112011-01-26 22:27:21202 // Exposing render_view_host() and tab() to be public; they are declared as
203 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30204 virtual RenderViewHost* render_view_host() {
205 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37206 }
207
[email protected]4b19ea52012-01-02 20:15:25208 virtual WebContents* tab() {
[email protected]4e85c112011-01-26 22:27:21209 return InterstitialPage::tab();
210 }
211
[email protected]9e0c83a2009-05-06 19:44:37212 bool HasFocus() {
213 return render_view_host()->view()->HasFocus();
214 }
215
216 private:
217 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37218};
[email protected]b9821882009-08-17 22:25:17219
[email protected]fab6ffb2011-09-22 18:35:34220IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30221 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42222#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41223 // It seems we have to wait a little bit for the widgets to spin up before
224 // we can start clicking on them.
225 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17226 MessageLoop::QuitClosure(),
[email protected]fc2e0872009-08-21 22:14:41227 kActionDelayMs);
228 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42229#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41230
[email protected]0b8fa8b2011-12-07 00:54:52231 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00232
[email protected]fc2e0872009-08-21 22:14:41233 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06234 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00235
[email protected]fc2e0872009-08-21 22:14:41236 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52237 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00238}
[email protected]186f13f2009-08-19 20:34:00239
[email protected]320948e02011-01-10 08:21:12240// Flaky, http://crbug.com/69034.
241IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30242 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11243 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29244
245 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11246 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18247 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29248
[email protected]186f13f2009-08-19 20:34:00249 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
250
initial.commit09911bf2008-07-26 23:55:29251 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06252 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29253 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57254 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29255 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06256 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29257
[email protected]186f13f2009-08-19 20:34:00258 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52259 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29260 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57261 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29262 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52263 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29264
[email protected]186f13f2009-08-19 20:34:00265 // The rest of this test does not make sense on Linux because the behavior
266 // of Activate() is not well defined and can vary by window manager.
267#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29268 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18269 Browser* browser2 = Browser::Create(browser()->profile());
270 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43271 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18272 browser2->window()->Show();
273 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29274
[email protected]0c966682011-08-02 18:22:10275 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00276 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16277 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18278 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50279 const views::Widget* widget2 =
280 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10281 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50282 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18283 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54284 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38285 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29286
287 // Switch to the 1st browser window, focus should still be on the location
288 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18289 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52290 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18291 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29292
293 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18294 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10295 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
296 ASSERT_TRUE(widget);
297 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54298 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38299 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18300
301 // Close the 2nd browser to avoid a DCHECK().
302 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00303#endif
initial.commit09911bf2008-07-26 23:55:29304}
305
306// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39307// Disabled, http://crbug.com/62542.
308IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30309 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11310 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29311
312 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11313 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18314 ui_test_utils::NavigateToURL(browser(), url);
315
initial.commit09911bf2008-07-26 23:55:29316 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33317 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58318 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29319
320 // Alternate focus for the tab.
321 const bool kFocusPage[3][5] = {
322 { true, true, true, true, false },
323 { false, false, false, false, false },
324 { false, true, false, true, false }
325 };
326
327 for (int i = 1; i < 3; i++) {
328 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18329 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41330 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29331
332 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54333 if (kFocusPage[i][j]) {
[email protected]6acde6352012-01-04 16:52:20334 browser()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54335 } else {
[email protected]186f13f2009-08-19 20:34:00336 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54337 }
initial.commit09911bf2008-07-26 23:55:29338 }
339
340 // Now come back to the tab and check the right view is focused.
341 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18342 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41343 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29344
[email protected]186f13f2009-08-19 20:34:00345 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52346 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06347 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29348 }
[email protected]cb7e2542009-12-14 22:02:35349
[email protected]eaca0ad12011-04-18 15:53:41350 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35351 // Try the above, but with ctrl+tab. Since tab normally changes focus,
352 // this has regressed in the past. Loop through several times to be sure.
353 for (int j = 0; j < 15; j++) {
354 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52355 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35356 ASSERT_TRUE(IsViewFocused(vid));
357
[email protected]1d000682010-08-23 16:21:28358 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09359 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35360 }
361
362 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41363 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35364 for (int j = 14; j >= 0; --j) {
365 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52366 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35367 ASSERT_TRUE(IsViewFocused(vid));
368
[email protected]1d000682010-08-23 16:21:28369 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09370 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35371 }
initial.commit09911bf2008-07-26 23:55:29372 }
373}
374
[email protected]ae40b572009-10-02 21:17:45375// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35376IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30377 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11378 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45379
380 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11381 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45382 ui_test_utils::NavigateToURL(browser(), url);
383
384 browser()->Find();
[email protected]9c318862011-02-01 22:27:24385 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45386 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06387 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45388
389 // Focus the location bar.
390 browser()->FocusLocationBar();
391
392 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58393 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45394
395 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06396 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45397
398 // Select 1st tab, focus should still be on the location-bar.
399 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41400 browser()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52401 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45402
403 // Now open the find box again, switch to another tab and come back, the focus
404 // should return to the find box.
405 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06406 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41407 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06408 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41409 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06410 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45411}
412
initial.commit09911bf2008-07-26 23:55:29413// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07414// Flaky, http://crbug.com/62538.
415IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
416 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30417 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11418 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29419
initial.commit09911bf2008-07-26 23:55:29420 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18421 Browser* browser2 = Browser::Create(browser()->profile());
422 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43423 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18424 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00425
[email protected]ed179ee2009-10-03 21:02:51426 Browser* focused_browser = NULL;
427 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16428#if defined(USE_X11)
429 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00430 // to figure out which browser does have focus.
431 if (browser2->window()->IsActive()) {
432 focused_browser = browser2;
433 unfocused_browser = browser();
434 } else if (browser()->window()->IsActive()) {
435 focused_browser = browser();
436 unfocused_browser = browser2;
437 } else {
[email protected]579c3d82010-10-06 03:53:51438 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00439 }
440#elif defined(OS_WIN)
441 focused_browser = browser();
442 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57443#elif defined(OS_MACOSX)
444 // On Mac, the newly created window always gets the focus.
445 focused_browser = browser2;
446 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00447#endif
448
[email protected]95409e12010-08-17 20:07:11449 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00450 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46451
[email protected]8bcdec92009-02-25 16:15:18452 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00453 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29454
[email protected]579c3d82010-10-06 03:53:51455 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20456 unfocused_browser->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51457 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29458
[email protected]8bcdec92009-02-25 16:15:18459 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00460 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29461}
462
463// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21464IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30465 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11466 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29467
468 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11469 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18470 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29471
[email protected]186f13f2009-08-19 20:34:00472 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29473
[email protected]579c3d82010-10-06 03:53:51474 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20475 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51476 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29477
478 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52479 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29480}
481
[email protected]9e0c83a2009-05-06 19:44:37482// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35483// Note that this test relies on a notification from the renderer that the
484// focus has changed in the page. The notification in the renderer may change
485// at which point this test would fail (see comment in
486// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57487IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30488 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11489 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29490
[email protected]8bcdec92009-02-25 16:15:18491 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11492 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18493 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29494
[email protected]186f13f2009-08-19 20:34:00495 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29496
[email protected]546ae4e02010-12-08 14:57:19497 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18498 const char* kExpElementIDs[] = {
499 "", // Initially no element in the page should be focused
500 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19501 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18502 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29503 };
504
505 // Test forward focus traversal.
506 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40507 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29508 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52509 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29510
[email protected]911696b2011-01-28 02:36:49511 // Move the caret to the end, otherwise the next Tab key may not move focus.
512 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
513 browser(), ui::VKEY_END, false, false, false, false));
514
initial.commit09911bf2008-07-26 23:55:29515 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35516 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40517 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29518 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01519 std::string actual;
520 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20521 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18522 L"",
[email protected]45671612009-04-29 22:24:01523 L"window.domAutomationController.send(getFocusedElement());",
524 &actual));
initial.commit09911bf2008-07-26 23:55:29525 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
526
[email protected]130efb02009-09-18 18:54:35527 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19528 // If the next element is the kTextElementID, we expect to be
529 // notified we have switched to an editable node.
530 bool is_editable_node =
531 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53532 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19533
534 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09535 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27536 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56537 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20538 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19539 details));
[email protected]130efb02009-09-18 18:54:35540 } else {
541 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19542 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09543 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27544 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53545 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35546 }
initial.commit09911bf2008-07-26 23:55:29547 }
[email protected]8bcdec92009-02-25 16:15:18548
549 // At this point the renderer has sent us a message asking to advance the
550 // focus (as the end of the focus loop was reached in the renderer).
551 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42552 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29553 }
554
555 // Now let's try reverse focus traversal.
556 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40557 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29558 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52559 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29560
[email protected]911696b2011-01-28 02:36:49561 // Move the caret to the end, otherwise the next Tab key may not move focus.
562 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
563 browser(), ui::VKEY_END, false, false, false, false));
564
[email protected]8bcdec92009-02-25 16:15:18565 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30566 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40567 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19568 const char* next_element =
569 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35570
571 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19572 // If the next element is the kTextElementID, we expect to be
573 // notified we have switched to an editable node.
574 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53575 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19576
577 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09578 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27579 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56580 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20581 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19582 details));
[email protected]130efb02009-09-18 18:54:35583 } else {
584 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19585 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09586 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27587 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53588 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35589 }
initial.commit09911bf2008-07-26 23:55:29590
591 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01592 std::string actual;
593 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20594 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18595 L"",
[email protected]45671612009-04-29 22:24:01596 L"window.domAutomationController.send(getFocusedElement());",
597 &actual));
[email protected]546ae4e02010-12-08 14:57:19598 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29599 }
[email protected]8bcdec92009-02-25 16:15:18600
601 // At this point the renderer has sent us a message asking to advance the
602 // focus (as the end of the focus loop was reached in the renderer).
603 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42604 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29605 }
606}
607
[email protected]9e0c83a2009-05-06 19:44:37608// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41609IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30610 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11611 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37612
613 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11614 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37615 ui_test_utils::NavigateToURL(browser(), url);
616
[email protected]9e0c83a2009-05-06 19:44:37617 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06618 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37619
620 // Let's show an interstitial.
621 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20622 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37623 true, GURL("http://interstitial.com"));
624 interstitial_page->Show();
625 // Give some time for the interstitial to show.
626 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17627 MessageLoop::QuitClosure(),
[email protected]9e0c83a2009-05-06 19:44:37628 1000);
629 ui_test_utils::RunMessageLoop();
630
[email protected]fc2e0872009-08-21 22:14:41631 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37632
633 const char* kExpElementIDs[] = {
634 "", // Initially no element in the page should be focused
635 // (the location bar is focused).
636 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
637 "gmapLink"
638 };
639
640 // Test forward focus traversal.
641 for (int i = 0; i < 2; ++i) {
642 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52643 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37644
[email protected]911696b2011-01-28 02:36:49645 // Move the caret to the end, otherwise the next Tab key may not move focus.
646 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
647 browser(), ui::VKEY_END, false, false, false, false));
648
[email protected]9e0c83a2009-05-06 19:44:37649 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35650 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37651 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30652 std::string actual;
653 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
654 interstitial_page->render_view_host(), L"",
655 L"window.domAutomationController.send(getFocusedElement());",
656 &actual));
[email protected]9e0c83a2009-05-06 19:44:37657 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
658
[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]a6e602f2010-09-28 22:28:30671
672 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09673 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30674 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37675 }
676
677 // At this point the renderer has sent us a message asking to advance the
678 // focus (as the end of the focus loop was reached in the renderer).
679 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42680 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37681 }
682
683 // Now let's try reverse focus traversal.
684 for (int i = 0; i < 2; ++i) {
685 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52686 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37687
[email protected]911696b2011-01-28 02:36:49688 // Move the caret to the end, otherwise the next Tab key may not move focus.
689 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
690 browser(), ui::VKEY_END, false, false, false, false));
691
[email protected]9e0c83a2009-05-06 19:44:37692 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35693 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27694 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53695 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07696 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35697 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27698 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56699 notification_source = content::Source<RenderViewHost>(
700 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35701 } else {
702 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27703 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53704 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35705 }
[email protected]9e0c83a2009-05-06 19:44:37706
[email protected]a6e602f2010-09-28 22:28:30707 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09708 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30709 notification_type, notification_source));
710
[email protected]9e0c83a2009-05-06 19:44:37711 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30712 std::string actual;
713 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
714 interstitial_page->render_view_host(), L"",
715 L"window.domAutomationController.send(getFocusedElement());",
716 &actual));
[email protected]9e0c83a2009-05-06 19:44:37717 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
718 }
719
720 // At this point the renderer has sent us a message asking to advance the
721 // focus (as the end of the focus loop was reached in the renderer).
722 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42723 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37724 }
725}
726
727// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33728// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05729#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]cb931932011-05-03 19:57:33730IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
731#else
[email protected]e4f4e0b2009-10-13 19:58:21732IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33733#endif
[email protected]a6e602f2010-09-28 22:28:30734 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11735 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37736
737 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11738 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37739 ui_test_utils::NavigateToURL(browser(), url);
740
[email protected]9e0c83a2009-05-06 19:44:37741 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06742 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]4ca15302012-01-03 05:53:20743 EXPECT_TRUE(browser()->GetSelectedWebContents()->GetRenderViewHost()->view()->
[email protected]9e0c83a2009-05-06 19:44:37744 HasFocus());
745
[email protected]9d8a4642009-07-29 17:25:30746 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37747 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20748 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37749 true, GURL("http://interstitial.com"));
750 interstitial_page->Show();
751 // Give some time for the interstitial to show.
752 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17753 MessageLoop::QuitClosure(),
[email protected]9e0c83a2009-05-06 19:44:37754 1000);
755 ui_test_utils::RunMessageLoop();
756
757 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06758 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37759 EXPECT_TRUE(interstitial_page->HasFocus());
760
761 // Hide the interstitial.
762 interstitial_page->DontProceed();
763
764 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06765 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37766}
767
[email protected]9bd491ee2008-12-10 22:31:07768// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20769// Flaky on mac and valgrind. http://crbug.com/67301.
770#if defined(OS_MACOSX)
771#define MAYBE_FindFocusTest FLAKY_FindFocusTest
772#else
773#define MAYBE_FindFocusTest FindFocusTest
774#endif
[email protected]a711df102010-12-14 09:47:03775IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30776 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11777 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55778
[email protected]9bd491ee2008-12-10 22:31:07779 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11780 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18781 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07782
[email protected]a711df102010-12-14 09:47:03783 EXPECT_TRUE(ChromeInForeground());
784
[email protected]853300a82010-07-27 21:17:57785#if defined(OS_MACOSX)
786 // Press Cmd+F, which will make the Find box open and request focus.
[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]9bd491ee2008-12-10 22:31:07790 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28791 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09792 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57793#endif
[email protected]8bcdec92009-02-25 16:15:18794
[email protected]a9c060ca2012-01-05 20:43:41795 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07796
[email protected]fc2e0872009-08-21 22:14:41797 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52798 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07799
800 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57801#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28802 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09803 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57804#else
[email protected]1d000682010-08-23 16:21:28805 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09806 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57807#endif
[email protected]21abcc742009-10-23 02:52:06808 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07809
810 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41811 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06812 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07813
814 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57815#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28816 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09817 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57818#else
[email protected]1d000682010-08-23 16:21:28819 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09820 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57821#endif
[email protected]8bcdec92009-02-25 16:15:18822
[email protected]a9c060ca2012-01-05 20:43:41823 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07824}
[email protected]401513c2009-03-12 00:21:28825
826// Makes sure the focus is in the right location when opening the different
827// types of tabs.
[email protected]c92928672010-11-09 18:31:07828// Flaky, http://crbug.com/62539.
829IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30830 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07831
[email protected]401513c2009-03-12 00:21:28832 // Open the history tab, focus should be on the tab contents.
833 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42834 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20835 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42836 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28837
838 // Open the new tab, focus should be on the location bar.
839 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42840 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20841 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52842 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28843
844 // Open the download tab, focus should be on the tab contents.
845 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42846 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20847 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42848 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43849
850 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55851 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58852 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42853 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20854 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52855 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28856}
[email protected]9d8a4642009-07-29 17:25:30857
858// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21859IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30860 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11861 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30862
[email protected]9d8a4642009-07-29 17:25:30863 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06864 {
865 ui_test_utils::WindowedNotificationObserver observer(
866 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07867 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06868 browser()->NewTab();
869 observer.Wait();
870 }
[email protected]f07d7bf2010-04-06 08:02:42871 ui_test_utils::RunAllPendingInMessageLoop();
872
[email protected]6b4e8e42011-08-17 19:36:06873 {
874 ui_test_utils::WindowedNotificationObserver observer(
875 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50876 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46877 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56878 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06879 browser()->Reload(CURRENT_TAB);
880 observer.Wait();
881 }
[email protected]9d8a4642009-07-29 17:25:30882 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52883 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30884
885 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11886 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41887 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52888 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06889 {
890 ui_test_utils::WindowedNotificationObserver observer(
891 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50892 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46893 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56894 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06895 browser()->Reload(CURRENT_TAB);
896 observer.Wait();
897 }
[email protected]9ba21ede2010-07-30 01:11:07898
[email protected]9d8a4642009-07-29 17:25:30899 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41900 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06901 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30902}
903
904// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27905IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30906 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11907 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30908
[email protected]9d8a4642009-07-29 17:25:30909 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11910 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]4ca15302012-01-03 05:53:20911 ui_test_utils::CrashTab(browser()->GetSelectedWebContents());
[email protected]6b4e8e42011-08-17 19:36:06912 {
913 ui_test_utils::WindowedNotificationObserver observer(
914 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50915 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46916 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56917 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06918 browser()->Reload(CURRENT_TAB);
919 observer.Wait();
920 }
[email protected]9ba21ede2010-07-30 01:11:07921
[email protected]9d8a4642009-07-29 17:25:30922 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41923 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06924 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30925}
[email protected]629e0342010-07-27 23:30:13926
927} // namespace