blob: 7c87318ae985bc7122aa4733095a38016ba0dec3 [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]5de634712011-03-02 00:20:1928#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]477ae052011-11-18 23:53:5729#include "content/public/browser/notification_service.h"
[email protected]6acde6352012-01-04 16:52:2030#include "content/public/browser/web_contents.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
63#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3564#else
65#define MAYBE_FocusTraversal FocusTraversal
66#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
67#endif
68
69#if defined(OS_LINUX) || defined(OS_MACOSX)
70// TODO(jcampan): http://crbug.com/23683 for linux.
71// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5772#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
73#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0774// Flaky, http://crbug.com/62537.
75#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4176#endif
77
initial.commit09911bf2008-07-26 23:55:2978namespace {
79
[email protected]8bcdec92009-02-25 16:15:1880// The delay waited in some cases where we don't have a notifications for an
81// action we take.
initial.commit09911bf2008-07-26 23:55:2982const int kActionDelayMs = 500;
83
[email protected]a9c060ca2012-01-05 20:43:4184// Maxiumum time to wait until the focus is moved to expected view.
85const int kFocusChangeTimeoutMs = 500;
86
[email protected]f72a1cc2010-04-30 07:17:3087const char kSimplePage[] = "files/focus/page_with_focus.html";
88const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
89const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3190const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2991
[email protected]a711df102010-12-14 09:47:0392// Test to make sure Chrome is in the foreground as we start testing. This is
93// required for tests that synthesize input to the Chrome window.
94bool ChromeInForeground() {
95#if defined(OS_WIN)
96 HWND window = ::GetForegroundWindow();
97 std::wstring caption;
98 std::wstring filename;
99 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18100 if (len > 1)
101 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03102 bool chrome_window_in_foreground =
103 EndsWith(caption, L" - Google Chrome", true) ||
104 EndsWith(caption, L" - Chromium", true);
105 if (!chrome_window_in_foreground) {
106 DWORD process_id;
107 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
108
109 base::ProcessHandle process;
110 if (base::OpenProcessHandleWithAccess(process_id,
111 PROCESS_QUERY_LIMITED_INFORMATION,
112 &process)) {
[email protected]fdce4782011-11-29 20:06:18113 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
114 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03115 int error = GetLastError();
116 filename = std::wstring(L"Unable to read filename for process id '" +
117 base::IntToString16(process_id) +
118 L"' (error ") +
119 base::IntToString16(error) + L")";
120 }
121 base::CloseProcessHandle(process);
122 }
123 }
124 EXPECT_TRUE(chrome_window_in_foreground)
125 << "Chrome must be in the foreground when running interactive tests\n"
126 << "Process in foreground: " << filename.c_str() << "\n"
127 << "Window: " << window << "\n"
128 << "Caption: " << caption.c_str();
129 return chrome_window_in_foreground;
130#else
131 // Windows only at the moment.
132 return true;
133#endif
134}
135
[email protected]a9c060ca2012-01-05 20:43:41136// Wait the focus change in message loop.
137void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
138 if (ui_test_utils::IsViewFocused(browser, id) ||
139 base::Time::Now() > timeout) {
140 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
141 } else {
142 MessageLoop::current()->PostDelayedTask(
143 FROM_HERE,
144 base::Bind(&CheckFocus, browser, id, timeout),
145 10);
146 }
147};
148
[email protected]8bcdec92009-02-25 16:15:18149class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29150 public:
[email protected]0b8fa8b2011-12-07 00:54:52151 BrowserFocusTest() :
152#if defined(USE_AURA)
153 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
154#else
155 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
156#endif
157 {
[email protected]8bcdec92009-02-25 16:15:18158 set_show_window(true);
159 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29160 }
[email protected]b9821882009-08-17 22:25:17161
[email protected]21abcc742009-10-23 02:52:06162 bool IsViewFocused(ViewID vid) {
163 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17164 }
165
[email protected]fc2e0872009-08-21 22:14:41166 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06167 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41168 }
[email protected]0b8fa8b2011-12-07 00:54:52169
[email protected]a9c060ca2012-01-05 20:43:41170 bool WaitForFocusChange(ViewID vid) {
171 const base::Time timeout = base::Time::Now() +
172 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
173 MessageLoop::current()->PostDelayedTask(
174 FROM_HERE,
175 base::Bind(&CheckFocus, browser(), vid, timeout),
176 100);
177 ui_test_utils::RunMessageLoop();
178 return IsViewFocused(vid);
179 }
180
[email protected]0b8fa8b2011-12-07 00:54:52181 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29182};
183
[email protected]9e0c83a2009-05-06 19:44:37184class TestInterstitialPage : public InterstitialPage {
185 public:
[email protected]4ca15302012-01-03 05:53:20186 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30187 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31188 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37189 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
190 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31191 file_path = file_path.AppendASCII("focus");
192 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37193 r = file_util::ReadFileToString(file_path, &html_contents_);
194 EXPECT_TRUE(r);
195 }
196
197 virtual std::string GetHTMLContents() {
198 return html_contents_;
199 }
200
[email protected]4e85c112011-01-26 22:27:21201 // Exposing render_view_host() and tab() to be public; they are declared as
202 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30203 virtual RenderViewHost* render_view_host() {
204 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37205 }
206
[email protected]4b19ea52012-01-02 20:15:25207 virtual WebContents* tab() {
[email protected]4e85c112011-01-26 22:27:21208 return InterstitialPage::tab();
209 }
210
[email protected]9e0c83a2009-05-06 19:44:37211 bool HasFocus() {
212 return render_view_host()->view()->HasFocus();
213 }
214
215 private:
216 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37217};
[email protected]b9821882009-08-17 22:25:17218
[email protected]fab6ffb2011-09-22 18:35:34219IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30220 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42221#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41222 // It seems we have to wait a little bit for the widgets to spin up before
223 // we can start clicking on them.
224 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17225 MessageLoop::QuitClosure(),
[email protected]fc2e0872009-08-21 22:14:41226 kActionDelayMs);
227 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42228#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41229
[email protected]0b8fa8b2011-12-07 00:54:52230 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00231
[email protected]fc2e0872009-08-21 22:14:41232 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06233 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00234
[email protected]fc2e0872009-08-21 22:14:41235 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52236 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00237}
[email protected]186f13f2009-08-19 20:34:00238
[email protected]320948e02011-01-10 08:21:12239// Flaky, http://crbug.com/69034.
240IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30241 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11242 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29243
244 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11245 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18246 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29247
[email protected]186f13f2009-08-19 20:34:00248 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
249
initial.commit09911bf2008-07-26 23:55:29250 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06251 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29252 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57253 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29254 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06255 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29256
[email protected]186f13f2009-08-19 20:34:00257 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52258 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29259 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57260 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29261 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52262 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29263
[email protected]186f13f2009-08-19 20:34:00264 // The rest of this test does not make sense on Linux because the behavior
265 // of Activate() is not well defined and can vary by window manager.
266#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29267 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18268 Browser* browser2 = Browser::Create(browser()->profile());
269 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43270 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18271 browser2->window()->Show();
272 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29273
[email protected]0c966682011-08-02 18:22:10274 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00275 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16276 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18277 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50278 const views::Widget* widget2 =
279 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10280 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50281 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18282 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54283 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38284 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29285
286 // Switch to the 1st browser window, focus should still be on the location
287 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18288 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52289 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18290 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29291
292 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18293 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10294 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
295 ASSERT_TRUE(widget);
296 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54297 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38298 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18299
300 // Close the 2nd browser to avoid a DCHECK().
301 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00302#endif
initial.commit09911bf2008-07-26 23:55:29303}
304
305// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39306// Disabled, http://crbug.com/62542.
307IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30308 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11309 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29310
311 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11312 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18313 ui_test_utils::NavigateToURL(browser(), url);
314
initial.commit09911bf2008-07-26 23:55:29315 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33316 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58317 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29318
319 // Alternate focus for the tab.
320 const bool kFocusPage[3][5] = {
321 { true, true, true, true, false },
322 { false, false, false, false, false },
323 { false, true, false, true, false }
324 };
325
326 for (int i = 1; i < 3; i++) {
327 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18328 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41329 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29330
331 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54332 if (kFocusPage[i][j]) {
[email protected]6acde6352012-01-04 16:52:20333 browser()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54334 } else {
[email protected]186f13f2009-08-19 20:34:00335 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54336 }
initial.commit09911bf2008-07-26 23:55:29337 }
338
339 // Now come back to the tab and check the right view is focused.
340 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18341 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41342 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29343
[email protected]186f13f2009-08-19 20:34:00344 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52345 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06346 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29347 }
[email protected]cb7e2542009-12-14 22:02:35348
[email protected]eaca0ad12011-04-18 15:53:41349 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35350 // Try the above, but with ctrl+tab. Since tab normally changes focus,
351 // this has regressed in the past. Loop through several times to be sure.
352 for (int j = 0; j < 15; j++) {
353 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52354 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35355 ASSERT_TRUE(IsViewFocused(vid));
356
[email protected]1d000682010-08-23 16:21:28357 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09358 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35359 }
360
361 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41362 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35363 for (int j = 14; j >= 0; --j) {
364 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52365 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35366 ASSERT_TRUE(IsViewFocused(vid));
367
[email protected]1d000682010-08-23 16:21:28368 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09369 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35370 }
initial.commit09911bf2008-07-26 23:55:29371 }
372}
373
[email protected]ae40b572009-10-02 21:17:45374// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35375IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30376 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11377 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45378
379 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11380 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45381 ui_test_utils::NavigateToURL(browser(), url);
382
383 browser()->Find();
[email protected]9c318862011-02-01 22:27:24384 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45385 ASCIIToUTF16("a"), true, false, NULL);
[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 // Focus the location bar.
389 browser()->FocusLocationBar();
390
391 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58392 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45393
394 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06395 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45396
397 // Select 1st tab, focus should still be on the location-bar.
398 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41399 browser()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52400 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45401
402 // Now open the find box again, switch to another tab and come back, the focus
403 // should return to the find box.
404 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06405 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41406 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06407 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41408 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06409 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45410}
411
initial.commit09911bf2008-07-26 23:55:29412// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07413// Flaky, http://crbug.com/62538.
414IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
415 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30416 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11417 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29418
initial.commit09911bf2008-07-26 23:55:29419 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18420 Browser* browser2 = Browser::Create(browser()->profile());
421 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43422 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18423 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00424
[email protected]ed179ee2009-10-03 21:02:51425 Browser* focused_browser = NULL;
426 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16427#if defined(USE_X11)
428 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00429 // to figure out which browser does have focus.
430 if (browser2->window()->IsActive()) {
431 focused_browser = browser2;
432 unfocused_browser = browser();
433 } else if (browser()->window()->IsActive()) {
434 focused_browser = browser();
435 unfocused_browser = browser2;
436 } else {
[email protected]579c3d82010-10-06 03:53:51437 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00438 }
439#elif defined(OS_WIN)
440 focused_browser = browser();
441 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57442#elif defined(OS_MACOSX)
443 // On Mac, the newly created window always gets the focus.
444 focused_browser = browser2;
445 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00446#endif
447
[email protected]95409e12010-08-17 20:07:11448 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00449 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46450
[email protected]8bcdec92009-02-25 16:15:18451 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00452 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29453
[email protected]579c3d82010-10-06 03:53:51454 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20455 unfocused_browser->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51456 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29457
[email protected]8bcdec92009-02-25 16:15:18458 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00459 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29460}
461
462// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21463IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[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
467 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11468 GURL url = test_server()->GetURL(kStealFocusPage);
[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]579c3d82010-10-06 03:53:51473 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20474 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51475 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29476
477 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52478 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29479}
480
[email protected]9e0c83a2009-05-06 19:44:37481// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35482// Note that this test relies on a notification from the renderer that the
483// focus has changed in the page. The notification in the renderer may change
484// at which point this test would fail (see comment in
485// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57486IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30487 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11488 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29489
[email protected]8bcdec92009-02-25 16:15:18490 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11491 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18492 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29493
[email protected]186f13f2009-08-19 20:34:00494 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29495
[email protected]546ae4e02010-12-08 14:57:19496 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18497 const char* kExpElementIDs[] = {
498 "", // Initially no element in the page should be focused
499 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19500 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18501 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29502 };
503
504 // Test forward focus traversal.
505 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40506 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29507 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52508 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29509
[email protected]911696b2011-01-28 02:36:49510 // Move the caret to the end, otherwise the next Tab key may not move focus.
511 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
512 browser(), ui::VKEY_END, false, false, false, false));
513
initial.commit09911bf2008-07-26 23:55:29514 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35515 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40516 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29517 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01518 std::string actual;
519 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20520 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18521 L"",
[email protected]45671612009-04-29 22:24:01522 L"window.domAutomationController.send(getFocusedElement());",
523 &actual));
initial.commit09911bf2008-07-26 23:55:29524 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
525
[email protected]130efb02009-09-18 18:54:35526 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19527 // If the next element is the kTextElementID, we expect to be
528 // notified we have switched to an editable node.
529 bool is_editable_node =
530 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53531 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19532
533 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09534 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27535 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56536 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20537 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19538 details));
[email protected]130efb02009-09-18 18:54:35539 } else {
540 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19541 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09542 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27543 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53544 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35545 }
initial.commit09911bf2008-07-26 23:55:29546 }
[email protected]8bcdec92009-02-25 16:15:18547
548 // At this point the renderer has sent us a message asking to advance the
549 // focus (as the end of the focus loop was reached in the renderer).
550 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42551 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29552 }
553
554 // Now let's try reverse focus traversal.
555 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40556 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29557 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52558 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29559
[email protected]911696b2011-01-28 02:36:49560 // Move the caret to the end, otherwise the next Tab key may not move focus.
561 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
562 browser(), ui::VKEY_END, false, false, false, false));
563
[email protected]8bcdec92009-02-25 16:15:18564 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30565 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40566 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19567 const char* next_element =
568 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35569
570 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19571 // If the next element is the kTextElementID, we expect to be
572 // notified we have switched to an editable node.
573 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53574 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19575
576 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09577 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27578 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56579 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20580 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19581 details));
[email protected]130efb02009-09-18 18:54:35582 } else {
583 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19584 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09585 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27586 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53587 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35588 }
initial.commit09911bf2008-07-26 23:55:29589
590 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01591 std::string actual;
592 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20593 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18594 L"",
[email protected]45671612009-04-29 22:24:01595 L"window.domAutomationController.send(getFocusedElement());",
596 &actual));
[email protected]546ae4e02010-12-08 14:57:19597 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29598 }
[email protected]8bcdec92009-02-25 16:15:18599
600 // At this point the renderer has sent us a message asking to advance the
601 // focus (as the end of the focus loop was reached in the renderer).
602 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42603 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29604 }
605}
606
[email protected]9e0c83a2009-05-06 19:44:37607// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41608IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30609 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11610 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37611
612 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11613 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37614 ui_test_utils::NavigateToURL(browser(), url);
615
[email protected]9e0c83a2009-05-06 19:44:37616 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06617 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37618
619 // Let's show an interstitial.
620 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20621 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37622 true, GURL("http://interstitial.com"));
623 interstitial_page->Show();
624 // Give some time for the interstitial to show.
625 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17626 MessageLoop::QuitClosure(),
[email protected]9e0c83a2009-05-06 19:44:37627 1000);
628 ui_test_utils::RunMessageLoop();
629
[email protected]fc2e0872009-08-21 22:14:41630 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37631
632 const char* kExpElementIDs[] = {
633 "", // Initially no element in the page should be focused
634 // (the location bar is focused).
635 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
636 "gmapLink"
637 };
638
639 // Test forward focus traversal.
640 for (int i = 0; i < 2; ++i) {
641 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52642 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37643
[email protected]911696b2011-01-28 02:36:49644 // Move the caret to the end, otherwise the next Tab key may not move focus.
645 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
646 browser(), ui::VKEY_END, false, false, false, false));
647
[email protected]9e0c83a2009-05-06 19:44:37648 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35649 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37650 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30651 std::string actual;
652 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
653 interstitial_page->render_view_host(), L"",
654 L"window.domAutomationController.send(getFocusedElement());",
655 &actual));
[email protected]9e0c83a2009-05-06 19:44:37656 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
657
[email protected]432115822011-07-10 15:52:27658 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53659 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07660 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35661 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27662 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56663 notification_source = content::Source<RenderViewHost>(
664 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35665 } else {
666 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27667 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53668 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35669 }
[email protected]a6e602f2010-09-28 22:28:30670
671 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09672 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30673 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37674 }
675
676 // At this point the renderer has sent us a message asking to advance the
677 // focus (as the end of the focus loop was reached in the renderer).
678 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42679 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37680 }
681
682 // Now let's try reverse focus traversal.
683 for (int i = 0; i < 2; ++i) {
684 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52685 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37686
[email protected]911696b2011-01-28 02:36:49687 // Move the caret to the end, otherwise the next Tab key may not move focus.
688 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
689 browser(), ui::VKEY_END, false, false, false, false));
690
[email protected]9e0c83a2009-05-06 19:44:37691 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35692 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27693 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53694 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07695 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35696 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27697 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56698 notification_source = content::Source<RenderViewHost>(
699 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35700 } else {
701 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27702 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53703 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35704 }
[email protected]9e0c83a2009-05-06 19:44:37705
[email protected]a6e602f2010-09-28 22:28:30706 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09707 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30708 notification_type, notification_source));
709
[email protected]9e0c83a2009-05-06 19:44:37710 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30711 std::string actual;
712 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
713 interstitial_page->render_view_host(), L"",
714 L"window.domAutomationController.send(getFocusedElement());",
715 &actual));
[email protected]9e0c83a2009-05-06 19:44:37716 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
717 }
718
719 // At this point the renderer has sent us a message asking to advance the
720 // focus (as the end of the focus loop was reached in the renderer).
721 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42722 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37723 }
724}
725
726// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33727// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05728#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]cb931932011-05-03 19:57:33729IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
730#else
[email protected]e4f4e0b2009-10-13 19:58:21731IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33732#endif
[email protected]a6e602f2010-09-28 22:28:30733 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11734 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37735
736 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11737 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37738 ui_test_utils::NavigateToURL(browser(), url);
739
[email protected]9e0c83a2009-05-06 19:44:37740 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06741 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]4ca15302012-01-03 05:53:20742 EXPECT_TRUE(browser()->GetSelectedWebContents()->GetRenderViewHost()->view()->
[email protected]9e0c83a2009-05-06 19:44:37743 HasFocus());
744
[email protected]9d8a4642009-07-29 17:25:30745 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37746 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20747 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37748 true, GURL("http://interstitial.com"));
749 interstitial_page->Show();
750 // Give some time for the interstitial to show.
751 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17752 MessageLoop::QuitClosure(),
[email protected]9e0c83a2009-05-06 19:44:37753 1000);
754 ui_test_utils::RunMessageLoop();
755
756 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06757 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37758 EXPECT_TRUE(interstitial_page->HasFocus());
759
760 // Hide the interstitial.
761 interstitial_page->DontProceed();
762
763 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06764 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37765}
766
[email protected]9bd491ee2008-12-10 22:31:07767// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20768// Flaky on mac and valgrind. http://crbug.com/67301.
769#if defined(OS_MACOSX)
770#define MAYBE_FindFocusTest FLAKY_FindFocusTest
771#else
772#define MAYBE_FindFocusTest FindFocusTest
773#endif
[email protected]a711df102010-12-14 09:47:03774IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30775 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11776 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55777
[email protected]9bd491ee2008-12-10 22:31:07778 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11779 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18780 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07781
[email protected]a711df102010-12-14 09:47:03782 EXPECT_TRUE(ChromeInForeground());
783
[email protected]853300a82010-07-27 21:17:57784#if defined(OS_MACOSX)
785 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28786 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09787 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57788#else
[email protected]9bd491ee2008-12-10 22:31:07789 // Press Ctrl+F, which will make the Find box open and request focus.
[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]8bcdec92009-02-25 16:15:18793
[email protected]a9c060ca2012-01-05 20:43:41794 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07795
[email protected]fc2e0872009-08-21 22:14:41796 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52797 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[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]21abcc742009-10-23 02:52:06807 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07808
809 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41810 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06811 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07812
813 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57814#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28815 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09816 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57817#else
[email protected]1d000682010-08-23 16:21:28818 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09819 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57820#endif
[email protected]8bcdec92009-02-25 16:15:18821
[email protected]a9c060ca2012-01-05 20:43:41822 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07823}
[email protected]401513c2009-03-12 00:21:28824
825// Makes sure the focus is in the right location when opening the different
826// types of tabs.
[email protected]c92928672010-11-09 18:31:07827// Flaky, http://crbug.com/62539.
828IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30829 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07830
[email protected]401513c2009-03-12 00:21:28831 // Open the history tab, focus should be on the tab contents.
832 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42833 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20834 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42835 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28836
837 // Open the new tab, focus should be on the location bar.
838 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42839 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20840 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52841 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28842
843 // Open the download tab, focus should be on the tab contents.
844 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42845 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20846 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42847 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43848
849 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55850 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58851 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42852 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20853 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52854 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28855}
[email protected]9d8a4642009-07-29 17:25:30856
857// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21858IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30859 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11860 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30861
[email protected]9d8a4642009-07-29 17:25:30862 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06863 {
864 ui_test_utils::WindowedNotificationObserver observer(
865 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07866 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06867 browser()->NewTab();
868 observer.Wait();
869 }
[email protected]f07d7bf2010-04-06 08:02:42870 ui_test_utils::RunAllPendingInMessageLoop();
871
[email protected]6b4e8e42011-08-17 19:36:06872 {
873 ui_test_utils::WindowedNotificationObserver observer(
874 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50875 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46876 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56877 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06878 browser()->Reload(CURRENT_TAB);
879 observer.Wait();
880 }
[email protected]9d8a4642009-07-29 17:25:30881 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52882 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30883
884 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11885 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41886 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52887 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06888 {
889 ui_test_utils::WindowedNotificationObserver observer(
890 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50891 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46892 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56893 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06894 browser()->Reload(CURRENT_TAB);
895 observer.Wait();
896 }
[email protected]9ba21ede2010-07-30 01:11:07897
[email protected]9d8a4642009-07-29 17:25:30898 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41899 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06900 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30901}
902
903// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27904IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30905 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11906 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30907
[email protected]9d8a4642009-07-29 17:25:30908 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11909 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]4ca15302012-01-03 05:53:20910 ui_test_utils::CrashTab(browser()->GetSelectedWebContents());
[email protected]6b4e8e42011-08-17 19:36:06911 {
912 ui_test_utils::WindowedNotificationObserver observer(
913 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50914 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46915 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56916 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06917 browser()->Reload(CURRENT_TAB);
918 observer.Wait();
919 }
[email protected]9ba21ede2010-07-30 01:11:07920
[email protected]9d8a4642009-07-29 17:25:30921 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41922 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06923 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30924}
[email protected]629e0342010-07-27 23:30:13925
926} // namespace