blob: 9a5179f8fc624fceb521b00bca208b8eea8bb1a4 [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]a37d4b02012-06-25 21:56:1016#include "chrome/browser/ui/browser_commands.h"
[email protected]52877dbc62012-06-29 22:22:0317#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3318#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1219#include "chrome/browser/ui/chrome_pages.h"
[email protected]871dc682012-06-11 19:35:3320#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]b56e2e32012-05-11 21:18:0421#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1922#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2723#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3724#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5525#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3226#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5727#include "chrome/test/base/ui_test_utils.h"
[email protected]cadaec52012-02-08 21:53:1328#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1329#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5730#include "content/public/browser/notification_service.h"
[email protected]9c1662b2012-03-06 15:44:3331#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5832#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2033#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1034#include "content/public/browser/web_contents_view.h"
[email protected]3985ba82010-07-29 21:44:1235#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5736
37#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]477ae052011-11-18 23:53:5738#include "ui/views/focus/focus_manager.h"
[email protected]5025f862011-11-30 23:35:2039#include "ui/views/view.h"
[email protected]853300a82010-07-27 21:17:5740#endif
initial.commit09911bf2008-07-26 23:55:2941
[email protected]134c47b92009-08-19 03:33:4442#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5443#include "chrome/browser/ui/views/frame/browser_view.h"
44#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4445#endif
46
[email protected]a13283cc2012-04-05 00:21:2247#if defined(TOOLKIT_GTK)
[email protected]93270d002011-01-19 22:32:5948#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1749#endif
50
[email protected]a711df102010-12-14 09:47:0351#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0352#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5953#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4054#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0355#endif
56
[email protected]cadaec52012-02-08 21:53:1357using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5058using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1159using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2560using content::WebContents;
61
[email protected]3a3cf4d2011-12-09 00:39:3562#if defined(OS_MACOSX)
63// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4864#define MAYBE_FocusTraversal DISABLED_FocusTraversal
65#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4866#elif defined(OS_WIN)
67// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1868#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4869#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3570#else
71#define MAYBE_FocusTraversal FocusTraversal
72#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
73#endif
74
75#if defined(OS_LINUX) || defined(OS_MACOSX)
76// TODO(jcampan): http://crbug.com/23683 for linux.
77// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5778#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
79#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0780// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2681#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4182#endif
83
initial.commit09911bf2008-07-26 23:55:2984namespace {
85
[email protected]8bcdec92009-02-25 16:15:1886// The delay waited in some cases where we don't have a notifications for an
87// action we take.
initial.commit09911bf2008-07-26 23:55:2988const int kActionDelayMs = 500;
89
[email protected]a9c060ca2012-01-05 20:43:4190// Maxiumum time to wait until the focus is moved to expected view.
91const int kFocusChangeTimeoutMs = 500;
92
[email protected]f72a1cc2010-04-30 07:17:3093const char kSimplePage[] = "files/focus/page_with_focus.html";
94const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
95const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3196const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2997
[email protected]a711df102010-12-14 09:47:0398// Test to make sure Chrome is in the foreground as we start testing. This is
99// required for tests that synthesize input to the Chrome window.
100bool ChromeInForeground() {
101#if defined(OS_WIN)
102 HWND window = ::GetForegroundWindow();
103 std::wstring caption;
104 std::wstring filename;
105 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18106 if (len > 1)
107 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03108 bool chrome_window_in_foreground =
109 EndsWith(caption, L" - Google Chrome", true) ||
110 EndsWith(caption, L" - Chromium", true);
111 if (!chrome_window_in_foreground) {
112 DWORD process_id;
113 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
114
115 base::ProcessHandle process;
116 if (base::OpenProcessHandleWithAccess(process_id,
117 PROCESS_QUERY_LIMITED_INFORMATION,
118 &process)) {
[email protected]fdce4782011-11-29 20:06:18119 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
120 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03121 int error = GetLastError();
122 filename = std::wstring(L"Unable to read filename for process id '" +
123 base::IntToString16(process_id) +
124 L"' (error ") +
125 base::IntToString16(error) + L")";
126 }
127 base::CloseProcessHandle(process);
128 }
129 }
130 EXPECT_TRUE(chrome_window_in_foreground)
131 << "Chrome must be in the foreground when running interactive tests\n"
132 << "Process in foreground: " << filename.c_str() << "\n"
133 << "Window: " << window << "\n"
134 << "Caption: " << caption.c_str();
135 return chrome_window_in_foreground;
136#else
137 // Windows only at the moment.
138 return true;
139#endif
140}
141
[email protected]a9c060ca2012-01-05 20:43:41142// Wait the focus change in message loop.
143void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
144 if (ui_test_utils::IsViewFocused(browser, id) ||
145 base::Time::Now() > timeout) {
146 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
147 } else {
148 MessageLoop::current()->PostDelayedTask(
149 FROM_HERE,
150 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42151 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41152 }
153};
154
[email protected]8bcdec92009-02-25 16:15:18155class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29156 public:
[email protected]0b8fa8b2011-12-07 00:54:52157 BrowserFocusTest() :
158#if defined(USE_AURA)
159 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
160#else
161 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
162#endif
163 {
[email protected]8bcdec92009-02-25 16:15:18164 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29165 }
[email protected]b9821882009-08-17 22:25:17166
[email protected]21abcc742009-10-23 02:52:06167 bool IsViewFocused(ViewID vid) {
168 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17169 }
170
[email protected]fc2e0872009-08-21 22:14:41171 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06172 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41173 }
[email protected]0b8fa8b2011-12-07 00:54:52174
[email protected]a9c060ca2012-01-05 20:43:41175 bool WaitForFocusChange(ViewID vid) {
176 const base::Time timeout = base::Time::Now() +
177 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
178 MessageLoop::current()->PostDelayedTask(
179 FROM_HERE,
180 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42181 base::TimeDelta::FromMilliseconds(100));
[email protected]a9c060ca2012-01-05 20:43:41182 ui_test_utils::RunMessageLoop();
183 return IsViewFocused(vid);
184 }
185
[email protected]0b8fa8b2011-12-07 00:54:52186 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29187};
188
[email protected]85f0a572012-02-07 22:20:13189class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37190 public:
[email protected]85f0a572012-02-07 22:20:13191 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]b65de8b92009-09-14 19:36:31192 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37193 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
194 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31195 file_path = file_path.AppendASCII("focus");
196 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37197 r = file_util::ReadFileToString(file_path, &html_contents_);
198 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13199 interstitial_page_ = InterstitialPage::Create(
200 tab, new_navigation, url , this);
201 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37202 }
203
204 virtual std::string GetHTMLContents() {
205 return html_contents_;
206 }
207
[email protected]85f0a572012-02-07 22:20:13208 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13209 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37210 }
211
[email protected]85f0a572012-02-07 22:20:13212 void DontProceed() {
213 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21214 }
215
[email protected]9e0c83a2009-05-06 19:44:37216 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58217 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37218 }
219
220 private:
221 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13222 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37223};
[email protected]b9821882009-08-17 22:25:17224
[email protected]c39e233d2012-05-18 13:24:12225// Flaky on mac. http://crbug.com/67301.
226#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23227#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12228#else
[email protected]839e0802012-05-18 14:45:23229#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12230#endif
231IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30232 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42233#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41234 // It seems we have to wait a little bit for the widgets to spin up before
235 // we can start clicking on them.
[email protected]7e560102012-03-08 20:58:42236 MessageLoop::current()->PostDelayedTask(
237 FROM_HERE,
238 MessageLoop::QuitClosure(),
239 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]fc2e0872009-08-21 22:14:41240 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42241#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41242
[email protected]0b8fa8b2011-12-07 00:54:52243 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00244
[email protected]fc2e0872009-08-21 22:14:41245 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21246 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00247
[email protected]fc2e0872009-08-21 22:14:41248 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52249 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00250}
[email protected]186f13f2009-08-19 20:34:00251
[email protected]320948e02011-01-10 08:21:12252// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26253IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30254 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11255 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29256
257 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11258 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18259 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29260
[email protected]90556dd2012-06-07 20:26:18261 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00262
initial.commit09911bf2008-07-26 23:55:29263 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21264 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29265 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57266 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29267 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21268 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29269
[email protected]a37d4b02012-06-25 21:56:10270 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52271 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29272 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57273 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29274 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52275 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29276
[email protected]186f13f2009-08-19 20:34:00277 // The rest of this test does not make sense on Linux because the behavior
278 // of Activate() is not well defined and can vary by window manager.
279#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29280 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18281 Browser* browser2 = Browser::Create(browser()->profile());
282 ASSERT_TRUE(browser2);
[email protected]c0849252012-05-12 13:51:27283 browser2->tab_strip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18284 browser2->window()->Show();
285 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29286
[email protected]90556dd2012-06-07 20:26:18287 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow();
[email protected]4a507a62009-05-28 00:10:00288 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16289 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18290 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50291 const views::Widget* widget2 =
292 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10293 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50294 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18295 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54296 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38297 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29298
299 // Switch to the 1st browser window, focus should still be on the location
300 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18301 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52302 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18303 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29304
305 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18306 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10307 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
308 ASSERT_TRUE(widget);
309 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54310 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38311 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18312
313 // Close the 2nd browser to avoid a DCHECK().
314 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00315#endif
initial.commit09911bf2008-07-26 23:55:29316}
317
318// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39319// Disabled, http://crbug.com/62542.
320IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30321 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11322 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29323
324 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11325 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18326 ui_test_utils::NavigateToURL(browser(), url);
327
initial.commit09911bf2008-07-26 23:55:29328 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03329 for (int i = 0; i < 4; ++i) {
330 chrome::AddSelectedTabWithURL(browser(), url,
331 content::PAGE_TRANSITION_TYPED);
332 }
initial.commit09911bf2008-07-26 23:55:29333
334 // Alternate focus for the tab.
335 const bool kFocusPage[3][5] = {
336 { true, true, true, true, false },
337 { false, false, false, false, false },
338 { false, true, false, true, false }
339 };
340
341 for (int i = 1; i < 3; i++) {
342 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18343 // Activate the tab.
[email protected]52877dbc62012-06-29 22:22:03344 chrome::ActivateTabAt(browser(), j, true);
initial.commit09911bf2008-07-26 23:55:29345
346 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54347 if (kFocusPage[i][j]) {
[email protected]52877dbc62012-06-29 22:22:03348 chrome::GetWebContentsAt(browser(), j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54349 } else {
[email protected]a37d4b02012-06-25 21:56:10350 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54351 }
initial.commit09911bf2008-07-26 23:55:29352 }
353
354 // Now come back to the tab and check the right view is focused.
355 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18356 // Activate the tab.
[email protected]52877dbc62012-06-29 22:22:03357 chrome::ActivateTabAt(browser(), j, true);
initial.commit09911bf2008-07-26 23:55:29358
[email protected]f2159ba2012-04-17 19:13:21359 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52360 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06361 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29362 }
[email protected]cb7e2542009-12-14 22:02:35363
[email protected]52877dbc62012-06-29 22:22:03364 chrome::ActivateTabAt(browser(), 0, true);
[email protected]cb7e2542009-12-14 22:02:35365 // Try the above, but with ctrl+tab. Since tab normally changes focus,
366 // this has regressed in the past. Loop through several times to be sure.
367 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21368 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52369 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35370 ASSERT_TRUE(IsViewFocused(vid));
371
[email protected]1d000682010-08-23 16:21:28372 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09373 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35374 }
375
376 // As above, but with ctrl+shift+tab.
[email protected]52877dbc62012-06-29 22:22:03377 chrome::ActivateTabAt(browser(), 4, true);
[email protected]cb7e2542009-12-14 22:02:35378 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21379 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52380 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35381 ASSERT_TRUE(IsViewFocused(vid));
382
[email protected]1d000682010-08-23 16:21:28383 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09384 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35385 }
initial.commit09911bf2008-07-26 23:55:29386 }
387}
388
[email protected]ae40b572009-10-02 21:17:45389// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35390IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30391 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11392 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45393
394 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11395 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45396 ui_test_utils::NavigateToURL(browser(), url);
397
[email protected]a37d4b02012-06-25 21:56:10398 chrome::Find(browser());
[email protected]52877dbc62012-06-29 22:22:03399 ui_test_utils::FindInPage(chrome::GetActiveTabContents(browser()),
[email protected]ae40b572009-10-02 21:17:45400 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06401 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45402
403 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10404 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45405
406 // Create a 2nd tab.
[email protected]52877dbc62012-06-29 22:22:03407 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45408
409 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21410 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45411
412 // Select 1st tab, focus should still be on the location-bar.
413 // (bug http://crbug.com/23296)
[email protected]52877dbc62012-06-29 22:22:03414 chrome::ActivateTabAt(browser(), 0, true);
[email protected]0b8fa8b2011-12-07 00:54:52415 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45416
417 // Now open the find box again, switch to another tab and come back, the focus
418 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10419 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06420 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]52877dbc62012-06-29 22:22:03421 chrome::ActivateTabAt(browser(), 1, true);
[email protected]f2159ba2012-04-17 19:13:21422 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]52877dbc62012-06-29 22:22:03423 chrome::ActivateTabAt(browser(), 0, true);
[email protected]21abcc742009-10-23 02:52:06424 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45425}
426
initial.commit09911bf2008-07-26 23:55:29427// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07428// Flaky, http://crbug.com/62538.
429IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26430 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30431 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11432 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29433
initial.commit09911bf2008-07-26 23:55:29434 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18435 Browser* browser2 = Browser::Create(browser()->profile());
436 ASSERT_TRUE(browser2);
[email protected]c0849252012-05-12 13:51:27437 browser2->tab_strip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18438 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00439
[email protected]ed179ee2009-10-03 21:02:51440 Browser* focused_browser = NULL;
441 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16442#if defined(USE_X11)
443 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00444 // to figure out which browser does have focus.
445 if (browser2->window()->IsActive()) {
446 focused_browser = browser2;
447 unfocused_browser = browser();
448 } else if (browser()->window()->IsActive()) {
449 focused_browser = browser();
450 unfocused_browser = browser2;
451 } else {
[email protected]579c3d82010-10-06 03:53:51452 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00453 }
454#elif defined(OS_WIN)
455 focused_browser = browser();
456 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57457#elif defined(OS_MACOSX)
458 // On Mac, the newly created window always gets the focus.
459 focused_browser = browser2;
460 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00461#endif
462
[email protected]95409e12010-08-17 20:07:11463 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00464 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46465
[email protected]8bcdec92009-02-25 16:15:18466 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00467 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29468
[email protected]579c3d82010-10-06 03:53:51469 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]52877dbc62012-06-29 22:22:03470 chrome::GetActiveWebContents(unfocused_browser)->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51471 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29472
[email protected]8bcdec92009-02-25 16:15:18473 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00474 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29475}
476
477// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21478IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30479 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11480 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29481
482 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11483 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18484 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29485
[email protected]a37d4b02012-06-25 21:56:10486 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29487
[email protected]579c3d82010-10-06 03:53:51488 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]52877dbc62012-06-29 22:22:03489 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51490 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29491
492 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52493 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29494}
495
[email protected]9e0c83a2009-05-06 19:44:37496// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35497// Note that this test relies on a notification from the renderer that the
498// focus has changed in the page. The notification in the renderer may change
499// at which point this test would fail (see comment in
500// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57501IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30502 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11503 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29504
[email protected]8bcdec92009-02-25 16:15:18505 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11506 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18507 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29508
[email protected]a37d4b02012-06-25 21:56:10509 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29510
[email protected]546ae4e02010-12-08 14:57:19511 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18512 const char* kExpElementIDs[] = {
513 "", // Initially no element in the page should be focused
514 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19515 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18516 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29517 };
518
519 // Test forward focus traversal.
520 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40521 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29522 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52523 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29524
[email protected]911696b2011-01-28 02:36:49525 // Move the caret to the end, otherwise the next Tab key may not move focus.
526 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
527 browser(), ui::VKEY_END, false, false, false, false));
528
initial.commit09911bf2008-07-26 23:55:29529 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35530 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40531 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29532 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01533 std::string actual;
534 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]52877dbc62012-06-29 22:22:03535 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18536 L"",
[email protected]45671612009-04-29 22:24:01537 L"window.domAutomationController.send(getFocusedElement());",
538 &actual));
initial.commit09911bf2008-07-26 23:55:29539 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
540
[email protected]130efb02009-09-18 18:54:35541 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19542 // If the next element is the kTextElementID, we expect to be
543 // notified we have switched to an editable node.
544 bool is_editable_node =
545 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53546 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19547
548 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09549 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27550 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56551 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]52877dbc62012-06-29 22:22:03552 chrome::GetActiveWebContents(browser())->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19553 details));
[email protected]130efb02009-09-18 18:54:35554 } else {
555 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19556 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09557 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27558 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53559 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35560 }
initial.commit09911bf2008-07-26 23:55:29561 }
[email protected]8bcdec92009-02-25 16:15:18562
563 // At this point the renderer has sent us a message asking to advance the
564 // focus (as the end of the focus loop was reached in the renderer).
565 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42566 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29567 }
568
569 // Now let's try reverse focus traversal.
570 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40571 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29572 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52573 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29574
[email protected]911696b2011-01-28 02:36:49575 // Move the caret to the end, otherwise the next Tab key may not move focus.
576 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
577 browser(), ui::VKEY_END, false, false, false, false));
578
[email protected]8bcdec92009-02-25 16:15:18579 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30580 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40581 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19582 const char* next_element =
583 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35584
585 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19586 // If the next element is the kTextElementID, we expect to be
587 // notified we have switched to an editable node.
588 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53589 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19590
591 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09592 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27593 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56594 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]52877dbc62012-06-29 22:22:03595 chrome::GetActiveWebContents(browser())->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19596 details));
[email protected]130efb02009-09-18 18:54:35597 } else {
598 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19599 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09600 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27601 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53602 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35603 }
initial.commit09911bf2008-07-26 23:55:29604
605 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01606 std::string actual;
607 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]52877dbc62012-06-29 22:22:03608 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18609 L"",
[email protected]45671612009-04-29 22:24:01610 L"window.domAutomationController.send(getFocusedElement());",
611 &actual));
[email protected]546ae4e02010-12-08 14:57:19612 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29613 }
[email protected]8bcdec92009-02-25 16:15:18614
615 // At this point the renderer has sent us a message asking to advance the
616 // focus (as the end of the focus loop was reached in the renderer).
617 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42618 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29619 }
620}
621
[email protected]9e0c83a2009-05-06 19:44:37622// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41623IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30624 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11625 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37626
627 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11628 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37629 ui_test_utils::NavigateToURL(browser(), url);
630
[email protected]9e0c83a2009-05-06 19:44:37631 // Focus should be on the page.
[email protected]f2159ba2012-04-17 19:13:21632 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37633
634 // Let's show an interstitial.
635 TestInterstitialPage* interstitial_page =
[email protected]52877dbc62012-06-29 22:22:03636 new TestInterstitialPage(chrome::GetActiveWebContents(browser()),
[email protected]9e0c83a2009-05-06 19:44:37637 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37638 // Give some time for the interstitial to show.
639 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17640 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42641 base::TimeDelta::FromSeconds(1));
[email protected]9e0c83a2009-05-06 19:44:37642 ui_test_utils::RunMessageLoop();
643
[email protected]a37d4b02012-06-25 21:56:10644 chrome::FocusLocationBar(browser());
[email protected]9e0c83a2009-05-06 19:44:37645
646 const char* kExpElementIDs[] = {
647 "", // Initially no element in the page should be focused
648 // (the location bar is focused).
649 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
650 "gmapLink"
651 };
652
653 // Test forward focus traversal.
654 for (int i = 0; i < 2; ++i) {
655 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52656 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37657
[email protected]911696b2011-01-28 02:36:49658 // Move the caret to the end, otherwise the next Tab key may not move focus.
659 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
660 browser(), ui::VKEY_END, false, false, false, false));
661
[email protected]9e0c83a2009-05-06 19:44:37662 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35663 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37664 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30665 std::string actual;
666 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
667 interstitial_page->render_view_host(), L"",
668 L"window.domAutomationController.send(getFocusedElement());",
669 &actual));
[email protected]9e0c83a2009-05-06 19:44:37670 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
671
[email protected]432115822011-07-10 15:52:27672 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53673 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07674 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35675 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27676 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56677 notification_source = content::Source<RenderViewHost>(
678 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35679 } else {
680 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27681 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53682 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35683 }
[email protected]a6e602f2010-09-28 22:28:30684
685 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09686 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30687 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37688 }
689
690 // At this point the renderer has sent us a message asking to advance the
691 // focus (as the end of the focus loop was reached in the renderer).
692 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42693 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37694 }
695
696 // Now let's try reverse focus traversal.
697 for (int i = 0; i < 2; ++i) {
698 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52699 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37700
[email protected]911696b2011-01-28 02:36:49701 // Move the caret to the end, otherwise the next Tab key may not move focus.
702 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
703 browser(), ui::VKEY_END, false, false, false, false));
704
[email protected]9e0c83a2009-05-06 19:44:37705 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35706 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27707 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53708 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07709 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35710 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27711 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56712 notification_source = content::Source<RenderViewHost>(
713 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35714 } else {
715 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27716 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53717 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35718 }
[email protected]9e0c83a2009-05-06 19:44:37719
[email protected]a6e602f2010-09-28 22:28:30720 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09721 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30722 notification_type, notification_source));
723
[email protected]9e0c83a2009-05-06 19:44:37724 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30725 std::string actual;
726 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
727 interstitial_page->render_view_host(), L"",
728 L"window.domAutomationController.send(getFocusedElement());",
729 &actual));
[email protected]9e0c83a2009-05-06 19:44:37730 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
731 }
732
733 // At this point the renderer has sent us a message asking to advance the
734 // focus (as the end of the focus loop was reached in the renderer).
735 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42736 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37737 }
738}
739
740// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33741// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05742#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]4d2451652012-02-14 23:54:26743IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33744#else
[email protected]e4f4e0b2009-10-13 19:58:21745IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33746#endif
[email protected]a6e602f2010-09-28 22:28:30747 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11748 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37749
750 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11751 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37752 ui_test_utils::NavigateToURL(browser(), url);
753
[email protected]9e0c83a2009-05-06 19:44:37754 // Page should have focus.
[email protected]f2159ba2012-04-17 19:13:21755 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]52877dbc62012-06-29 22:22:03756 EXPECT_TRUE(chrome::GetActiveWebContents(browser())->GetRenderViewHost()->
[email protected]9f76c1e2012-03-05 15:15:58757 GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37758
[email protected]9d8a4642009-07-29 17:25:30759 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37760 TestInterstitialPage* interstitial_page =
[email protected]52877dbc62012-06-29 22:22:03761 new TestInterstitialPage(chrome::GetActiveWebContents(browser()),
[email protected]9e0c83a2009-05-06 19:44:37762 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37763 // Give some time for the interstitial to show.
764 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17765 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42766 base::TimeDelta::FromSeconds(1));
[email protected]9e0c83a2009-05-06 19:44:37767 ui_test_utils::RunMessageLoop();
768
769 // The interstitial should have focus now.
[email protected]f2159ba2012-04-17 19:13:21770 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37771 EXPECT_TRUE(interstitial_page->HasFocus());
772
773 // Hide the interstitial.
774 interstitial_page->DontProceed();
775
776 // Focus should be back on the original page.
[email protected]f2159ba2012-04-17 19:13:21777 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37778}
779
[email protected]9bd491ee2008-12-10 22:31:07780// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20781// Flaky on mac and valgrind. http://crbug.com/67301.
782#if defined(OS_MACOSX)
[email protected]4d2451652012-02-14 23:54:26783#define MAYBE_FindFocusTest DISABLED_FindFocusTest
[email protected]2004b762011-05-05 22:43:20784#else
785#define MAYBE_FindFocusTest FindFocusTest
786#endif
[email protected]c39e233d2012-05-18 13:24:12787IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30788 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11789 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55790
[email protected]9bd491ee2008-12-10 22:31:07791 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11792 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18793 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07794
[email protected]a711df102010-12-14 09:47:03795 EXPECT_TRUE(ChromeInForeground());
796
[email protected]853300a82010-07-27 21:17:57797#if defined(OS_MACOSX)
798 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28799 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09800 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57801#else
[email protected]9bd491ee2008-12-10 22:31:07802 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28803 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09804 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57805#endif
[email protected]8bcdec92009-02-25 16:15:18806
[email protected]a9c060ca2012-01-05 20:43:41807 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07808
[email protected]a37d4b02012-06-25 21:56:10809 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52810 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07811
812 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57813#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28814 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09815 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57816#else
[email protected]1d000682010-08-23 16:21:28817 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09818 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57819#endif
[email protected]21abcc742009-10-23 02:52:06820 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07821
822 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41823 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21824 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07825
826 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57827#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28828 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09829 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57830#else
[email protected]1d000682010-08-23 16:21:28831 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09832 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57833#endif
[email protected]8bcdec92009-02-25 16:15:18834
[email protected]a9c060ca2012-01-05 20:43:41835 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07836}
[email protected]401513c2009-03-12 00:21:28837
838// Makes sure the focus is in the right location when opening the different
839// types of tabs.
[email protected]c92928672010-11-09 18:31:07840// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26841IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30842 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07843
[email protected]401513c2009-03-12 00:21:28844 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12845 chrome::ShowHistory(browser());
[email protected]a3f343f2010-10-06 23:39:42846 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03847 chrome::GetActiveWebContents(browser())));
[email protected]f2159ba2012-04-17 19:13:21848 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28849
850 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10851 chrome::NewTab(browser());
[email protected]a3f343f2010-10-06 23:39:42852 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03853 chrome::GetActiveWebContents(browser())));
[email protected]0b8fa8b2011-12-07 00:54:52854 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28855
856 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12857 chrome::ShowDownloads(browser());
[email protected]a3f343f2010-10-06 23:39:42858 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03859 chrome::GetActiveWebContents(browser())));
[email protected]f2159ba2012-04-17 19:13:21860 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43861
862 // Open about:blank, focus should be on the location bar.
[email protected]52877dbc62012-06-29 22:22:03863 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL),
864 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42865 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03866 chrome::GetActiveWebContents(browser())));
[email protected]0b8fa8b2011-12-07 00:54:52867 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28868}
[email protected]9d8a4642009-07-29 17:25:30869
870// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21871IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30872 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11873 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30874
[email protected]9d8a4642009-07-29 17:25:30875 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06876 {
877 ui_test_utils::WindowedNotificationObserver observer(
878 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07879 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10880 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06881 observer.Wait();
882 }
[email protected]f07d7bf2010-04-06 08:02:42883 ui_test_utils::RunAllPendingInMessageLoop();
884
[email protected]6b4e8e42011-08-17 19:36:06885 {
886 ui_test_utils::WindowedNotificationObserver observer(
887 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50888 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03889 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10890 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06891 observer.Wait();
892 }
[email protected]9d8a4642009-07-29 17:25:30893 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52894 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30895
896 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11897 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10898 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52899 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06900 {
901 ui_test_utils::WindowedNotificationObserver observer(
902 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50903 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03904 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10905 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06906 observer.Wait();
907 }
[email protected]9ba21ede2010-07-30 01:11:07908
[email protected]9d8a4642009-07-29 17:25:30909 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12910 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21911 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30912}
913
914// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27915IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30916 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11917 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30918
[email protected]9d8a4642009-07-29 17:25:30919 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11920 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]52877dbc62012-06-29 22:22:03921 ui_test_utils::CrashTab(chrome::GetActiveWebContents(browser()));
[email protected]6b4e8e42011-08-17 19:36:06922 {
923 ui_test_utils::WindowedNotificationObserver observer(
924 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50925 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03926 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10927 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06928 observer.Wait();
929 }
[email protected]9ba21ede2010-07-30 01:11:07930
[email protected]9d8a4642009-07-29 17:25:30931 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12932 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21933 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30934}
[email protected]629e0342010-07-27 23:30:13935
936} // namespace