blob: c5d6c55164af29c185006a8d680465b3c9fc29ad [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]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3316#include "chrome/browser/ui/browser_window.h"
[email protected]871dc682012-06-11 19:35:3317#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]b56e2e32012-05-11 21:18:0418#include "chrome/browser/ui/tabs/tab_strip_model.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]cadaec52012-02-08 21:53:1325#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1326#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5727#include "content/public/browser/notification_service.h"
[email protected]9c1662b2012-03-06 15:44:3328#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5829#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2030#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1031#include "content/public/browser/web_contents_view.h"
[email protected]3985ba82010-07-29 21:44:1232#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5733
34#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]477ae052011-11-18 23:53:5735#include "ui/views/focus/focus_manager.h"
[email protected]5025f862011-11-30 23:35:2036#include "ui/views/view.h"
[email protected]853300a82010-07-27 21:17:5737#endif
initial.commit09911bf2008-07-26 23:55:2938
[email protected]134c47b92009-08-19 03:33:4439#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5440#include "chrome/browser/ui/views/frame/browser_view.h"
41#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4442#endif
43
[email protected]a13283cc2012-04-05 00:21:2244#if defined(TOOLKIT_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]cadaec52012-02-08 21:53:1354using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5055using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1156using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2557using content::WebContents;
58
[email protected]3a3cf4d2011-12-09 00:39:3559#if defined(OS_MACOSX)
60// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4861#define MAYBE_FocusTraversal DISABLED_FocusTraversal
62#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4863#elif defined(OS_WIN)
64// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1865#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4866#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3567#else
68#define MAYBE_FocusTraversal FocusTraversal
69#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
70#endif
71
72#if defined(OS_LINUX) || defined(OS_MACOSX)
73// TODO(jcampan): http://crbug.com/23683 for linux.
74// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5775#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
76#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0777// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2678#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4179#endif
80
initial.commit09911bf2008-07-26 23:55:2981namespace {
82
[email protected]8bcdec92009-02-25 16:15:1883// The delay waited in some cases where we don't have a notifications for an
84// action we take.
initial.commit09911bf2008-07-26 23:55:2985const int kActionDelayMs = 500;
86
[email protected]a9c060ca2012-01-05 20:43:4187// Maxiumum time to wait until the focus is moved to expected view.
88const int kFocusChangeTimeoutMs = 500;
89
[email protected]f72a1cc2010-04-30 07:17:3090const char kSimplePage[] = "files/focus/page_with_focus.html";
91const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
92const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3193const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2994
[email protected]a711df102010-12-14 09:47:0395// Test to make sure Chrome is in the foreground as we start testing. This is
96// required for tests that synthesize input to the Chrome window.
97bool ChromeInForeground() {
98#if defined(OS_WIN)
99 HWND window = ::GetForegroundWindow();
100 std::wstring caption;
101 std::wstring filename;
102 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18103 if (len > 1)
104 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03105 bool chrome_window_in_foreground =
106 EndsWith(caption, L" - Google Chrome", true) ||
107 EndsWith(caption, L" - Chromium", true);
108 if (!chrome_window_in_foreground) {
109 DWORD process_id;
110 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
111
112 base::ProcessHandle process;
113 if (base::OpenProcessHandleWithAccess(process_id,
114 PROCESS_QUERY_LIMITED_INFORMATION,
115 &process)) {
[email protected]fdce4782011-11-29 20:06:18116 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
117 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03118 int error = GetLastError();
119 filename = std::wstring(L"Unable to read filename for process id '" +
120 base::IntToString16(process_id) +
121 L"' (error ") +
122 base::IntToString16(error) + L")";
123 }
124 base::CloseProcessHandle(process);
125 }
126 }
127 EXPECT_TRUE(chrome_window_in_foreground)
128 << "Chrome must be in the foreground when running interactive tests\n"
129 << "Process in foreground: " << filename.c_str() << "\n"
130 << "Window: " << window << "\n"
131 << "Caption: " << caption.c_str();
132 return chrome_window_in_foreground;
133#else
134 // Windows only at the moment.
135 return true;
136#endif
137}
138
[email protected]a9c060ca2012-01-05 20:43:41139// Wait the focus change in message loop.
140void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
141 if (ui_test_utils::IsViewFocused(browser, id) ||
142 base::Time::Now() > timeout) {
143 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
144 } else {
145 MessageLoop::current()->PostDelayedTask(
146 FROM_HERE,
147 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42148 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41149 }
150};
151
[email protected]8bcdec92009-02-25 16:15:18152class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29153 public:
[email protected]0b8fa8b2011-12-07 00:54:52154 BrowserFocusTest() :
155#if defined(USE_AURA)
156 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
157#else
158 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
159#endif
160 {
[email protected]8bcdec92009-02-25 16:15:18161 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29162 }
[email protected]b9821882009-08-17 22:25:17163
[email protected]21abcc742009-10-23 02:52:06164 bool IsViewFocused(ViewID vid) {
165 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17166 }
167
[email protected]fc2e0872009-08-21 22:14:41168 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06169 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41170 }
[email protected]0b8fa8b2011-12-07 00:54:52171
[email protected]a9c060ca2012-01-05 20:43:41172 bool WaitForFocusChange(ViewID vid) {
173 const base::Time timeout = base::Time::Now() +
174 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
175 MessageLoop::current()->PostDelayedTask(
176 FROM_HERE,
177 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42178 base::TimeDelta::FromMilliseconds(100));
[email protected]a9c060ca2012-01-05 20:43:41179 ui_test_utils::RunMessageLoop();
180 return IsViewFocused(vid);
181 }
182
[email protected]0b8fa8b2011-12-07 00:54:52183 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29184};
185
[email protected]85f0a572012-02-07 22:20:13186class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37187 public:
[email protected]85f0a572012-02-07 22:20:13188 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& 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);
[email protected]85f0a572012-02-07 22:20:13196 interstitial_page_ = InterstitialPage::Create(
197 tab, new_navigation, url , this);
198 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37199 }
200
201 virtual std::string GetHTMLContents() {
202 return html_contents_;
203 }
204
[email protected]85f0a572012-02-07 22:20:13205 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13206 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37207 }
208
[email protected]85f0a572012-02-07 22:20:13209 void DontProceed() {
210 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21211 }
212
[email protected]9e0c83a2009-05-06 19:44:37213 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58214 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37215 }
216
217 private:
218 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13219 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37220};
[email protected]b9821882009-08-17 22:25:17221
[email protected]c39e233d2012-05-18 13:24:12222// Flaky on mac. http://crbug.com/67301.
223#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23224#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12225#else
[email protected]839e0802012-05-18 14:45:23226#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12227#endif
228IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30229 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42230#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41231 // It seems we have to wait a little bit for the widgets to spin up before
232 // we can start clicking on them.
[email protected]7e560102012-03-08 20:58:42233 MessageLoop::current()->PostDelayedTask(
234 FROM_HERE,
235 MessageLoop::QuitClosure(),
236 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]fc2e0872009-08-21 22:14:41237 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42238#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41239
[email protected]0b8fa8b2011-12-07 00:54:52240 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00241
[email protected]fc2e0872009-08-21 22:14:41242 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21243 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00244
[email protected]fc2e0872009-08-21 22:14:41245 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52246 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00247}
[email protected]186f13f2009-08-19 20:34:00248
[email protected]320948e02011-01-10 08:21:12249// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26250IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30251 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11252 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29253
254 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11255 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18256 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29257
[email protected]90556dd2012-06-07 20:26:18258 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00259
initial.commit09911bf2008-07-26 23:55:29260 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21261 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29262 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57263 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29264 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21265 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29266
[email protected]186f13f2009-08-19 20:34:00267 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52268 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29269 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57270 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29271 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52272 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29273
[email protected]186f13f2009-08-19 20:34:00274 // The rest of this test does not make sense on Linux because the behavior
275 // of Activate() is not well defined and can vary by window manager.
276#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29277 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18278 Browser* browser2 = Browser::Create(browser()->profile());
279 ASSERT_TRUE(browser2);
[email protected]c0849252012-05-12 13:51:27280 browser2->tab_strip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18281 browser2->window()->Show();
282 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29283
[email protected]90556dd2012-06-07 20:26:18284 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow();
[email protected]4a507a62009-05-28 00:10:00285 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16286 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18287 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50288 const views::Widget* widget2 =
289 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10290 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50291 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18292 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54293 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38294 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29295
296 // Switch to the 1st browser window, focus should still be on the location
297 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18298 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52299 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18300 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29301
302 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18303 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10304 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
305 ASSERT_TRUE(widget);
306 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54307 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38308 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18309
310 // Close the 2nd browser to avoid a DCHECK().
311 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00312#endif
initial.commit09911bf2008-07-26 23:55:29313}
314
315// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39316// Disabled, http://crbug.com/62542.
317IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30318 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11319 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29320
321 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11322 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18323 ui_test_utils::NavigateToURL(browser(), url);
324
initial.commit09911bf2008-07-26 23:55:29325 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33326 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58327 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29328
329 // Alternate focus for the tab.
330 const bool kFocusPage[3][5] = {
331 { true, true, true, true, false },
332 { false, false, false, false, false },
333 { false, true, false, true, false }
334 };
335
336 for (int i = 1; i < 3; i++) {
337 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18338 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41339 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29340
341 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54342 if (kFocusPage[i][j]) {
[email protected]6acde6352012-01-04 16:52:20343 browser()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54344 } else {
[email protected]186f13f2009-08-19 20:34:00345 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54346 }
initial.commit09911bf2008-07-26 23:55:29347 }
348
349 // Now come back to the tab and check the right view is focused.
350 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18351 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41352 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29353
[email protected]f2159ba2012-04-17 19:13:21354 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52355 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06356 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29357 }
[email protected]cb7e2542009-12-14 22:02:35358
[email protected]eaca0ad12011-04-18 15:53:41359 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35360 // Try the above, but with ctrl+tab. Since tab normally changes focus,
361 // this has regressed in the past. Loop through several times to be sure.
362 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21363 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52364 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35365 ASSERT_TRUE(IsViewFocused(vid));
366
[email protected]1d000682010-08-23 16:21:28367 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09368 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35369 }
370
371 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41372 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35373 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21374 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52375 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35376 ASSERT_TRUE(IsViewFocused(vid));
377
[email protected]1d000682010-08-23 16:21:28378 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09379 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35380 }
initial.commit09911bf2008-07-26 23:55:29381 }
382}
383
[email protected]ae40b572009-10-02 21:17:45384// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35385IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30386 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11387 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45388
389 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11390 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45391 ui_test_utils::NavigateToURL(browser(), url);
392
393 browser()->Find();
[email protected]871dc682012-06-11 19:35:33394 ui_test_utils::FindInPage(browser()->GetActiveTabContents(),
[email protected]ae40b572009-10-02 21:17:45395 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06396 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45397
398 // Focus the location bar.
399 browser()->FocusLocationBar();
400
401 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58402 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45403
404 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21405 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45406
407 // Select 1st tab, focus should still be on the location-bar.
408 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41409 browser()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52410 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45411
412 // Now open the find box again, switch to another tab and come back, the focus
413 // should return to the find box.
414 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06415 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41416 browser()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21417 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]eaca0ad12011-04-18 15:53:41418 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06419 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45420}
421
initial.commit09911bf2008-07-26 23:55:29422// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07423// Flaky, http://crbug.com/62538.
424IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26425 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30426 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11427 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29428
initial.commit09911bf2008-07-26 23:55:29429 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18430 Browser* browser2 = Browser::Create(browser()->profile());
431 ASSERT_TRUE(browser2);
[email protected]c0849252012-05-12 13:51:27432 browser2->tab_strip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18433 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00434
[email protected]ed179ee2009-10-03 21:02:51435 Browser* focused_browser = NULL;
436 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16437#if defined(USE_X11)
438 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00439 // to figure out which browser does have focus.
440 if (browser2->window()->IsActive()) {
441 focused_browser = browser2;
442 unfocused_browser = browser();
443 } else if (browser()->window()->IsActive()) {
444 focused_browser = browser();
445 unfocused_browser = browser2;
446 } else {
[email protected]579c3d82010-10-06 03:53:51447 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00448 }
449#elif defined(OS_WIN)
450 focused_browser = browser();
451 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57452#elif defined(OS_MACOSX)
453 // On Mac, the newly created window always gets the focus.
454 focused_browser = browser2;
455 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00456#endif
457
[email protected]95409e12010-08-17 20:07:11458 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00459 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46460
[email protected]8bcdec92009-02-25 16:15:18461 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00462 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29463
[email protected]579c3d82010-10-06 03:53:51464 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]871dc682012-06-11 19:35:33465 unfocused_browser->GetActiveWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51466 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29467
[email protected]8bcdec92009-02-25 16:15:18468 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00469 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29470}
471
472// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21473IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30474 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11475 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29476
477 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11478 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18479 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29480
[email protected]186f13f2009-08-19 20:34:00481 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29482
[email protected]579c3d82010-10-06 03:53:51483 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]871dc682012-06-11 19:35:33484 browser()->GetActiveWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51485 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29486
487 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52488 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29489}
490
[email protected]9e0c83a2009-05-06 19:44:37491// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35492// Note that this test relies on a notification from the renderer that the
493// focus has changed in the page. The notification in the renderer may change
494// at which point this test would fail (see comment in
495// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57496IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30497 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11498 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29499
[email protected]8bcdec92009-02-25 16:15:18500 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11501 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18502 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29503
[email protected]186f13f2009-08-19 20:34:00504 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29505
[email protected]546ae4e02010-12-08 14:57:19506 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18507 const char* kExpElementIDs[] = {
508 "", // Initially no element in the page should be focused
509 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19510 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18511 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29512 };
513
514 // Test forward focus traversal.
515 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40516 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29517 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52518 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29519
[email protected]911696b2011-01-28 02:36:49520 // Move the caret to the end, otherwise the next Tab key may not move focus.
521 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
522 browser(), ui::VKEY_END, false, false, false, false));
523
initial.commit09911bf2008-07-26 23:55:29524 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35525 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40526 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29527 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01528 std::string actual;
529 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]871dc682012-06-11 19:35:33530 browser()->GetActiveWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18531 L"",
[email protected]45671612009-04-29 22:24:01532 L"window.domAutomationController.send(getFocusedElement());",
533 &actual));
initial.commit09911bf2008-07-26 23:55:29534 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
535
[email protected]130efb02009-09-18 18:54:35536 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19537 // If the next element is the kTextElementID, we expect to be
538 // notified we have switched to an editable node.
539 bool is_editable_node =
540 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53541 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19542
543 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09544 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27545 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56546 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]871dc682012-06-11 19:35:33547 browser()->GetActiveWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19548 details));
[email protected]130efb02009-09-18 18:54:35549 } else {
550 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19551 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09552 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27553 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53554 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35555 }
initial.commit09911bf2008-07-26 23:55:29556 }
[email protected]8bcdec92009-02-25 16:15:18557
558 // At this point the renderer has sent us a message asking to advance the
559 // focus (as the end of the focus loop was reached in the renderer).
560 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42561 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29562 }
563
564 // Now let's try reverse focus traversal.
565 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40566 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29567 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52568 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29569
[email protected]911696b2011-01-28 02:36:49570 // Move the caret to the end, otherwise the next Tab key may not move focus.
571 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
572 browser(), ui::VKEY_END, false, false, false, false));
573
[email protected]8bcdec92009-02-25 16:15:18574 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30575 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40576 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19577 const char* next_element =
578 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35579
580 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19581 // If the next element is the kTextElementID, we expect to be
582 // notified we have switched to an editable node.
583 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53584 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19585
586 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09587 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27588 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56589 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]871dc682012-06-11 19:35:33590 browser()->GetActiveWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19591 details));
[email protected]130efb02009-09-18 18:54:35592 } else {
593 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19594 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09595 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27596 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53597 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35598 }
initial.commit09911bf2008-07-26 23:55:29599
600 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01601 std::string actual;
602 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]871dc682012-06-11 19:35:33603 browser()->GetActiveWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18604 L"",
[email protected]45671612009-04-29 22:24:01605 L"window.domAutomationController.send(getFocusedElement());",
606 &actual));
[email protected]546ae4e02010-12-08 14:57:19607 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29608 }
[email protected]8bcdec92009-02-25 16:15:18609
610 // At this point the renderer has sent us a message asking to advance the
611 // focus (as the end of the focus loop was reached in the renderer).
612 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42613 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29614 }
615}
616
[email protected]9e0c83a2009-05-06 19:44:37617// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41618IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30619 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11620 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37621
622 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11623 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37624 ui_test_utils::NavigateToURL(browser(), url);
625
[email protected]9e0c83a2009-05-06 19:44:37626 // Focus should be on the page.
[email protected]f2159ba2012-04-17 19:13:21627 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37628
629 // Let's show an interstitial.
630 TestInterstitialPage* interstitial_page =
[email protected]871dc682012-06-11 19:35:33631 new TestInterstitialPage(browser()->GetActiveWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37632 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37633 // Give some time for the interstitial to show.
634 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17635 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42636 base::TimeDelta::FromSeconds(1));
[email protected]9e0c83a2009-05-06 19:44:37637 ui_test_utils::RunMessageLoop();
638
[email protected]fc2e0872009-08-21 22:14:41639 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37640
641 const char* kExpElementIDs[] = {
642 "", // Initially no element in the page should be focused
643 // (the location bar is focused).
644 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
645 "gmapLink"
646 };
647
648 // Test forward focus traversal.
649 for (int i = 0; i < 2; ++i) {
650 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52651 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37652
[email protected]911696b2011-01-28 02:36:49653 // Move the caret to the end, otherwise the next Tab key may not move focus.
654 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
655 browser(), ui::VKEY_END, false, false, false, false));
656
[email protected]9e0c83a2009-05-06 19:44:37657 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35658 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37659 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30660 std::string actual;
661 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
662 interstitial_page->render_view_host(), L"",
663 L"window.domAutomationController.send(getFocusedElement());",
664 &actual));
[email protected]9e0c83a2009-05-06 19:44:37665 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
666
[email protected]432115822011-07-10 15:52:27667 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53668 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07669 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35670 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27671 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56672 notification_source = content::Source<RenderViewHost>(
673 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35674 } else {
675 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27676 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53677 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35678 }
[email protected]a6e602f2010-09-28 22:28:30679
680 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09681 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30682 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37683 }
684
685 // At this point the renderer has sent us a message asking to advance the
686 // focus (as the end of the focus loop was reached in the renderer).
687 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42688 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37689 }
690
691 // Now let's try reverse focus traversal.
692 for (int i = 0; i < 2; ++i) {
693 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52694 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37695
[email protected]911696b2011-01-28 02:36:49696 // Move the caret to the end, otherwise the next Tab key may not move focus.
697 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
698 browser(), ui::VKEY_END, false, false, false, false));
699
[email protected]9e0c83a2009-05-06 19:44:37700 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35701 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27702 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53703 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07704 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35705 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27706 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56707 notification_source = content::Source<RenderViewHost>(
708 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35709 } else {
710 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27711 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53712 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35713 }
[email protected]9e0c83a2009-05-06 19:44:37714
[email protected]a6e602f2010-09-28 22:28:30715 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09716 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30717 notification_type, notification_source));
718
[email protected]9e0c83a2009-05-06 19:44:37719 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30720 std::string actual;
721 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
722 interstitial_page->render_view_host(), L"",
723 L"window.domAutomationController.send(getFocusedElement());",
724 &actual));
[email protected]9e0c83a2009-05-06 19:44:37725 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
726 }
727
728 // At this point the renderer has sent us a message asking to advance the
729 // focus (as the end of the focus loop was reached in the renderer).
730 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42731 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37732 }
733}
734
735// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33736// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05737#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]4d2451652012-02-14 23:54:26738IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33739#else
[email protected]e4f4e0b2009-10-13 19:58:21740IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33741#endif
[email protected]a6e602f2010-09-28 22:28:30742 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11743 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37744
745 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11746 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37747 ui_test_utils::NavigateToURL(browser(), url);
748
[email protected]9e0c83a2009-05-06 19:44:37749 // Page should have focus.
[email protected]f2159ba2012-04-17 19:13:21750 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]871dc682012-06-11 19:35:33751 EXPECT_TRUE(browser()->GetActiveWebContents()->GetRenderViewHost()->
[email protected]9f76c1e2012-03-05 15:15:58752 GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37753
[email protected]9d8a4642009-07-29 17:25:30754 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37755 TestInterstitialPage* interstitial_page =
[email protected]871dc682012-06-11 19:35:33756 new TestInterstitialPage(browser()->GetActiveWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37757 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37758 // Give some time for the interstitial to show.
759 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17760 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42761 base::TimeDelta::FromSeconds(1));
[email protected]9e0c83a2009-05-06 19:44:37762 ui_test_utils::RunMessageLoop();
763
764 // The interstitial should have focus now.
[email protected]f2159ba2012-04-17 19:13:21765 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37766 EXPECT_TRUE(interstitial_page->HasFocus());
767
768 // Hide the interstitial.
769 interstitial_page->DontProceed();
770
771 // Focus should be back on the original page.
[email protected]f2159ba2012-04-17 19:13:21772 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37773}
774
[email protected]9bd491ee2008-12-10 22:31:07775// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20776// Flaky on mac and valgrind. http://crbug.com/67301.
777#if defined(OS_MACOSX)
[email protected]4d2451652012-02-14 23:54:26778#define MAYBE_FindFocusTest DISABLED_FindFocusTest
[email protected]2004b762011-05-05 22:43:20779#else
780#define MAYBE_FindFocusTest FindFocusTest
781#endif
[email protected]c39e233d2012-05-18 13:24:12782IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30783 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11784 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55785
[email protected]9bd491ee2008-12-10 22:31:07786 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11787 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18788 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07789
[email protected]a711df102010-12-14 09:47:03790 EXPECT_TRUE(ChromeInForeground());
791
[email protected]853300a82010-07-27 21:17:57792#if defined(OS_MACOSX)
793 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28794 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09795 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57796#else
[email protected]9bd491ee2008-12-10 22:31:07797 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28798 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09799 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57800#endif
[email protected]8bcdec92009-02-25 16:15:18801
[email protected]a9c060ca2012-01-05 20:43:41802 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07803
[email protected]fc2e0872009-08-21 22:14:41804 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52805 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07806
807 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57808#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28809 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09810 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57811#else
[email protected]1d000682010-08-23 16:21:28812 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09813 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57814#endif
[email protected]21abcc742009-10-23 02:52:06815 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07816
817 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41818 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21819 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07820
821 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57822#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28823 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09824 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57825#else
[email protected]1d000682010-08-23 16:21:28826 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09827 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57828#endif
[email protected]8bcdec92009-02-25 16:15:18829
[email protected]a9c060ca2012-01-05 20:43:41830 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07831}
[email protected]401513c2009-03-12 00:21:28832
833// Makes sure the focus is in the right location when opening the different
834// types of tabs.
[email protected]c92928672010-11-09 18:31:07835// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26836IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30837 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07838
[email protected]401513c2009-03-12 00:21:28839 // Open the history tab, focus should be on the tab contents.
840 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42841 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]871dc682012-06-11 19:35:33842 browser()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21843 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28844
845 // Open the new tab, focus should be on the location bar.
846 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42847 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]871dc682012-06-11 19:35:33848 browser()->GetActiveWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52849 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28850
851 // Open the download tab, focus should be on the tab contents.
852 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42853 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]871dc682012-06-11 19:35:33854 browser()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21855 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43856
857 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55858 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58859 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42860 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]871dc682012-06-11 19:35:33861 browser()->GetActiveWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52862 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28863}
[email protected]9d8a4642009-07-29 17:25:30864
865// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21866IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30867 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11868 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30869
[email protected]9d8a4642009-07-29 17:25:30870 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06871 {
872 ui_test_utils::WindowedNotificationObserver observer(
873 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07874 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06875 browser()->NewTab();
876 observer.Wait();
877 }
[email protected]f07d7bf2010-04-06 08:02:42878 ui_test_utils::RunAllPendingInMessageLoop();
879
[email protected]6b4e8e42011-08-17 19:36:06880 {
881 ui_test_utils::WindowedNotificationObserver observer(
882 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50883 content::Source<NavigationController>(
[email protected]871dc682012-06-11 19:35:33884 &browser()->GetActiveWebContents()->GetController()));
[email protected]6b4e8e42011-08-17 19:36:06885 browser()->Reload(CURRENT_TAB);
886 observer.Wait();
887 }
[email protected]9d8a4642009-07-29 17:25:30888 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52889 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30890
891 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11892 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41893 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52894 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06895 {
896 ui_test_utils::WindowedNotificationObserver observer(
897 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50898 content::Source<NavigationController>(
[email protected]871dc682012-06-11 19:35:33899 &browser()->GetActiveWebContents()->GetController()));
[email protected]6b4e8e42011-08-17 19:36:06900 browser()->Reload(CURRENT_TAB);
901 observer.Wait();
902 }
[email protected]9ba21ede2010-07-30 01:11:07903
[email protected]9d8a4642009-07-29 17:25:30904 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41905 browser()->ShowDownloadsTab();
[email protected]f2159ba2012-04-17 19:13:21906 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30907}
908
909// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27910IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30911 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11912 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30913
[email protected]9d8a4642009-07-29 17:25:30914 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11915 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]871dc682012-06-11 19:35:33916 ui_test_utils::CrashTab(browser()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06917 {
918 ui_test_utils::WindowedNotificationObserver observer(
919 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50920 content::Source<NavigationController>(
[email protected]871dc682012-06-11 19:35:33921 &browser()->GetActiveWebContents()->GetController()));
[email protected]6b4e8e42011-08-17 19:36:06922 browser()->Reload(CURRENT_TAB);
923 observer.Wait();
924 }
[email protected]9ba21ede2010-07-30 01:11:07925
[email protected]9d8a4642009-07-29 17:25:30926 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41927 browser()->ShowDownloadsTab();
[email protected]f2159ba2012-04-17 19:13:21928 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30929}
[email protected]629e0342010-07-27 23:30:13930
931} // namespace