blob: 8f58d4d82ca4bee490bbb189ecb347c726239f3d [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]a9c060ca2012-01-05 20:43:417#include "base/bind.h"
[email protected]3985ba82010-07-29 21:44:128#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:429#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:1810#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1211#include "base/path_service.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]477ae052011-11-18 23:53:5713#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2214#include "base/utf_string_conversions.h"
[email protected]c848d3d92010-09-16 21:57:4515#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1016#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3317#include "chrome/browser/ui/browser_window.h"
[email protected]6b4e8e42011-08-17 19:36:0618#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]6a3ec2312010-12-02 19:30:1919#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2720#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3721#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5522#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3223#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5724#include "chrome/test/base/ui_test_utils.h"
[email protected]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"
42#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4443#endif
44
[email protected]a13283cc2012-04-05 00:21:2245#if defined(TOOLKIT_GTK)
[email protected]93270d002011-01-19 22:32:5946#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1747#endif
48
[email protected]a711df102010-12-14 09:47:0349#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0350#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5951#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4052#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0353#endif
54
[email protected]cadaec52012-02-08 21:53:1355using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5056using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1157using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2558using content::WebContents;
59
[email protected]3a3cf4d2011-12-09 00:39:3560#if defined(OS_MACOSX)
61// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4862#define MAYBE_FocusTraversal DISABLED_FocusTraversal
63#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4864#elif defined(OS_WIN)
65// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1866#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4867#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3568#else
69#define MAYBE_FocusTraversal FocusTraversal
70#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
71#endif
72
73#if defined(OS_LINUX) || defined(OS_MACOSX)
74// TODO(jcampan): http://crbug.com/23683 for linux.
75// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5776#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
77#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0778// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2679#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4180#endif
81
initial.commit09911bf2008-07-26 23:55:2982namespace {
83
[email protected]8bcdec92009-02-25 16:15:1884// The delay waited in some cases where we don't have a notifications for an
85// action we take.
initial.commit09911bf2008-07-26 23:55:2986const int kActionDelayMs = 500;
87
[email protected]a9c060ca2012-01-05 20:43:4188// Maxiumum time to wait until the focus is moved to expected view.
89const int kFocusChangeTimeoutMs = 500;
90
[email protected]f72a1cc2010-04-30 07:17:3091const char kSimplePage[] = "files/focus/page_with_focus.html";
92const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
93const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3194const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2995
[email protected]a711df102010-12-14 09:47:0396// Test to make sure Chrome is in the foreground as we start testing. This is
97// required for tests that synthesize input to the Chrome window.
98bool ChromeInForeground() {
99#if defined(OS_WIN)
100 HWND window = ::GetForegroundWindow();
101 std::wstring caption;
102 std::wstring filename;
103 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18104 if (len > 1)
105 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03106 bool chrome_window_in_foreground =
107 EndsWith(caption, L" - Google Chrome", true) ||
108 EndsWith(caption, L" - Chromium", true);
109 if (!chrome_window_in_foreground) {
110 DWORD process_id;
111 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
112
113 base::ProcessHandle process;
114 if (base::OpenProcessHandleWithAccess(process_id,
115 PROCESS_QUERY_LIMITED_INFORMATION,
116 &process)) {
[email protected]fdce4782011-11-29 20:06:18117 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
118 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03119 int error = GetLastError();
120 filename = std::wstring(L"Unable to read filename for process id '" +
121 base::IntToString16(process_id) +
122 L"' (error ") +
123 base::IntToString16(error) + L")";
124 }
125 base::CloseProcessHandle(process);
126 }
127 }
128 EXPECT_TRUE(chrome_window_in_foreground)
129 << "Chrome must be in the foreground when running interactive tests\n"
130 << "Process in foreground: " << filename.c_str() << "\n"
131 << "Window: " << window << "\n"
132 << "Caption: " << caption.c_str();
133 return chrome_window_in_foreground;
134#else
135 // Windows only at the moment.
136 return true;
137#endif
138}
139
[email protected]a9c060ca2012-01-05 20:43:41140// Wait the focus change in message loop.
141void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
142 if (ui_test_utils::IsViewFocused(browser, id) ||
143 base::Time::Now() > timeout) {
144 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
145 } else {
146 MessageLoop::current()->PostDelayedTask(
147 FROM_HERE,
148 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42149 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41150 }
151};
152
[email protected]8bcdec92009-02-25 16:15:18153class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29154 public:
[email protected]0b8fa8b2011-12-07 00:54:52155 BrowserFocusTest() :
156#if defined(USE_AURA)
157 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
158#else
159 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
160#endif
161 {
[email protected]8bcdec92009-02-25 16:15:18162 set_show_window(true);
163 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29164 }
[email protected]b9821882009-08-17 22:25:17165
[email protected]21abcc742009-10-23 02:52:06166 bool IsViewFocused(ViewID vid) {
167 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17168 }
169
[email protected]fc2e0872009-08-21 22:14:41170 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06171 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41172 }
[email protected]0b8fa8b2011-12-07 00:54:52173
[email protected]a9c060ca2012-01-05 20:43:41174 bool WaitForFocusChange(ViewID vid) {
175 const base::Time timeout = base::Time::Now() +
176 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
177 MessageLoop::current()->PostDelayedTask(
178 FROM_HERE,
179 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42180 base::TimeDelta::FromMilliseconds(100));
[email protected]a9c060ca2012-01-05 20:43:41181 ui_test_utils::RunMessageLoop();
182 return IsViewFocused(vid);
183 }
184
[email protected]0b8fa8b2011-12-07 00:54:52185 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29186};
187
[email protected]85f0a572012-02-07 22:20:13188class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37189 public:
[email protected]85f0a572012-02-07 22:20:13190 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]b65de8b92009-09-14 19:36:31191 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37192 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
193 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31194 file_path = file_path.AppendASCII("focus");
195 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37196 r = file_util::ReadFileToString(file_path, &html_contents_);
197 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13198 interstitial_page_ = InterstitialPage::Create(
199 tab, new_navigation, url , this);
200 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37201 }
202
203 virtual std::string GetHTMLContents() {
204 return html_contents_;
205 }
206
[email protected]85f0a572012-02-07 22:20:13207 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13208 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37209 }
210
[email protected]85f0a572012-02-07 22:20:13211 void DontProceed() {
212 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21213 }
214
[email protected]9e0c83a2009-05-06 19:44:37215 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58216 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37217 }
218
219 private:
220 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13221 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37222};
[email protected]b9821882009-08-17 22:25:17223
[email protected]fab6ffb2011-09-22 18:35:34224IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30225 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42226#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41227 // It seems we have to wait a little bit for the widgets to spin up before
228 // we can start clicking on them.
[email protected]7e560102012-03-08 20:58:42229 MessageLoop::current()->PostDelayedTask(
230 FROM_HERE,
231 MessageLoop::QuitClosure(),
232 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]fc2e0872009-08-21 22:14:41233 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42234#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41235
[email protected]0b8fa8b2011-12-07 00:54:52236 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00237
[email protected]fc2e0872009-08-21 22:14:41238 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06239 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00240
[email protected]fc2e0872009-08-21 22:14:41241 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52242 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00243}
[email protected]186f13f2009-08-19 20:34:00244
[email protected]320948e02011-01-10 08:21:12245// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26246IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30247 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11248 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29249
250 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11251 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18252 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29253
[email protected]186f13f2009-08-19 20:34:00254 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
255
initial.commit09911bf2008-07-26 23:55:29256 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06257 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29258 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57259 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29260 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06261 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29262
[email protected]186f13f2009-08-19 20:34:00263 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52264 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29265 // 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]0b8fa8b2011-12-07 00:54:52268 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29269
[email protected]186f13f2009-08-19 20:34:00270 // The rest of this test does not make sense on Linux because the behavior
271 // of Activate() is not well defined and can vary by window manager.
272#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29273 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18274 Browser* browser2 = Browser::Create(browser()->profile());
275 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43276 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18277 browser2->window()->Show();
278 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29279
[email protected]0c966682011-08-02 18:22:10280 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00281 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16282 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18283 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50284 const views::Widget* widget2 =
285 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10286 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50287 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18288 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54289 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38290 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29291
292 // Switch to the 1st browser window, focus should still be on the location
293 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18294 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52295 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18296 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29297
298 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18299 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10300 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
301 ASSERT_TRUE(widget);
302 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54303 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38304 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18305
306 // Close the 2nd browser to avoid a DCHECK().
307 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00308#endif
initial.commit09911bf2008-07-26 23:55:29309}
310
311// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39312// Disabled, http://crbug.com/62542.
313IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30314 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11315 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29316
317 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11318 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18319 ui_test_utils::NavigateToURL(browser(), url);
320
initial.commit09911bf2008-07-26 23:55:29321 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33322 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58323 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29324
325 // Alternate focus for the tab.
326 const bool kFocusPage[3][5] = {
327 { true, true, true, true, false },
328 { false, false, false, false, false },
329 { false, true, false, true, false }
330 };
331
332 for (int i = 1; i < 3; i++) {
333 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18334 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41335 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29336
337 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54338 if (kFocusPage[i][j]) {
[email protected]6acde6352012-01-04 16:52:20339 browser()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54340 } else {
[email protected]186f13f2009-08-19 20:34:00341 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54342 }
initial.commit09911bf2008-07-26 23:55:29343 }
344
345 // Now come back to the tab and check the right view is focused.
346 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18347 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41348 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29349
[email protected]186f13f2009-08-19 20:34:00350 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52351 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06352 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29353 }
[email protected]cb7e2542009-12-14 22:02:35354
[email protected]eaca0ad12011-04-18 15:53:41355 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35356 // Try the above, but with ctrl+tab. Since tab normally changes focus,
357 // this has regressed in the past. Loop through several times to be sure.
358 for (int j = 0; j < 15; j++) {
359 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52360 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35361 ASSERT_TRUE(IsViewFocused(vid));
362
[email protected]1d000682010-08-23 16:21:28363 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09364 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35365 }
366
367 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41368 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35369 for (int j = 14; j >= 0; --j) {
370 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52371 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35372 ASSERT_TRUE(IsViewFocused(vid));
373
[email protected]1d000682010-08-23 16:21:28374 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09375 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35376 }
initial.commit09911bf2008-07-26 23:55:29377 }
378}
379
[email protected]ae40b572009-10-02 21:17:45380// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35381IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30382 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11383 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45384
385 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11386 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45387 ui_test_utils::NavigateToURL(browser(), url);
388
389 browser()->Find();
[email protected]9c318862011-02-01 22:27:24390 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45391 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06392 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45393
394 // Focus the location bar.
395 browser()->FocusLocationBar();
396
397 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58398 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45399
400 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06401 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45402
403 // Select 1st tab, focus should still be on the location-bar.
404 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41405 browser()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52406 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45407
408 // Now open the find box again, switch to another tab and come back, the focus
409 // should return to the find box.
410 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06411 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41412 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06413 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41414 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06415 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45416}
417
initial.commit09911bf2008-07-26 23:55:29418// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07419// Flaky, http://crbug.com/62538.
420IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26421 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30422 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11423 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29424
initial.commit09911bf2008-07-26 23:55:29425 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18426 Browser* browser2 = Browser::Create(browser()->profile());
427 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43428 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18429 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00430
[email protected]ed179ee2009-10-03 21:02:51431 Browser* focused_browser = NULL;
432 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16433#if defined(USE_X11)
434 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00435 // to figure out which browser does have focus.
436 if (browser2->window()->IsActive()) {
437 focused_browser = browser2;
438 unfocused_browser = browser();
439 } else if (browser()->window()->IsActive()) {
440 focused_browser = browser();
441 unfocused_browser = browser2;
442 } else {
[email protected]579c3d82010-10-06 03:53:51443 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00444 }
445#elif defined(OS_WIN)
446 focused_browser = browser();
447 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57448#elif defined(OS_MACOSX)
449 // On Mac, the newly created window always gets the focus.
450 focused_browser = browser2;
451 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00452#endif
453
[email protected]95409e12010-08-17 20:07:11454 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00455 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46456
[email protected]8bcdec92009-02-25 16:15:18457 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00458 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29459
[email protected]579c3d82010-10-06 03:53:51460 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20461 unfocused_browser->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51462 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29463
[email protected]8bcdec92009-02-25 16:15:18464 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00465 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29466}
467
468// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21469IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30470 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11471 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29472
473 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11474 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18475 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29476
[email protected]186f13f2009-08-19 20:34:00477 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29478
[email protected]579c3d82010-10-06 03:53:51479 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20480 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51481 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29482
483 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52484 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29485}
486
[email protected]9e0c83a2009-05-06 19:44:37487// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35488// Note that this test relies on a notification from the renderer that the
489// focus has changed in the page. The notification in the renderer may change
490// at which point this test would fail (see comment in
491// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57492IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30493 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11494 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29495
[email protected]8bcdec92009-02-25 16:15:18496 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11497 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18498 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29499
[email protected]186f13f2009-08-19 20:34:00500 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29501
[email protected]546ae4e02010-12-08 14:57:19502 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18503 const char* kExpElementIDs[] = {
504 "", // Initially no element in the page should be focused
505 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19506 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18507 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29508 };
509
510 // Test forward focus traversal.
511 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40512 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29513 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52514 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29515
[email protected]911696b2011-01-28 02:36:49516 // Move the caret to the end, otherwise the next Tab key may not move focus.
517 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
518 browser(), ui::VKEY_END, false, false, false, false));
519
initial.commit09911bf2008-07-26 23:55:29520 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35521 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40522 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29523 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01524 std::string actual;
525 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20526 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18527 L"",
[email protected]45671612009-04-29 22:24:01528 L"window.domAutomationController.send(getFocusedElement());",
529 &actual));
initial.commit09911bf2008-07-26 23:55:29530 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
531
[email protected]130efb02009-09-18 18:54:35532 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19533 // If the next element is the kTextElementID, we expect to be
534 // notified we have switched to an editable node.
535 bool is_editable_node =
536 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53537 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19538
539 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09540 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27541 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56542 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20543 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19544 details));
[email protected]130efb02009-09-18 18:54:35545 } else {
546 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19547 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09548 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27549 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53550 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35551 }
initial.commit09911bf2008-07-26 23:55:29552 }
[email protected]8bcdec92009-02-25 16:15:18553
554 // At this point the renderer has sent us a message asking to advance the
555 // focus (as the end of the focus loop was reached in the renderer).
556 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42557 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29558 }
559
560 // Now let's try reverse focus traversal.
561 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40562 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29563 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52564 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29565
[email protected]911696b2011-01-28 02:36:49566 // Move the caret to the end, otherwise the next Tab key may not move focus.
567 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
568 browser(), ui::VKEY_END, false, false, false, false));
569
[email protected]8bcdec92009-02-25 16:15:18570 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30571 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40572 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19573 const char* next_element =
574 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35575
576 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19577 // If the next element is the kTextElementID, we expect to be
578 // notified we have switched to an editable node.
579 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53580 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19581
582 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09583 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27584 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56585 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20586 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19587 details));
[email protected]130efb02009-09-18 18:54:35588 } else {
589 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19590 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09591 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27592 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53593 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35594 }
initial.commit09911bf2008-07-26 23:55:29595
596 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01597 std::string actual;
598 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20599 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18600 L"",
[email protected]45671612009-04-29 22:24:01601 L"window.domAutomationController.send(getFocusedElement());",
602 &actual));
[email protected]546ae4e02010-12-08 14:57:19603 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29604 }
[email protected]8bcdec92009-02-25 16:15:18605
606 // At this point the renderer has sent us a message asking to advance the
607 // focus (as the end of the focus loop was reached in the renderer).
608 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42609 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29610 }
611}
612
[email protected]9e0c83a2009-05-06 19:44:37613// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41614IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30615 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11616 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37617
618 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11619 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37620 ui_test_utils::NavigateToURL(browser(), url);
621
[email protected]9e0c83a2009-05-06 19:44:37622 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06623 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37624
625 // Let's show an interstitial.
626 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20627 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37628 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37629 // Give some time for the interstitial to show.
630 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17631 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42632 base::TimeDelta::FromSeconds(1));
[email protected]9e0c83a2009-05-06 19:44:37633 ui_test_utils::RunMessageLoop();
634
[email protected]fc2e0872009-08-21 22:14:41635 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37636
637 const char* kExpElementIDs[] = {
638 "", // Initially no element in the page should be focused
639 // (the location bar is focused).
640 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
641 "gmapLink"
642 };
643
644 // Test forward focus traversal.
645 for (int i = 0; i < 2; ++i) {
646 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52647 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37648
[email protected]911696b2011-01-28 02:36:49649 // Move the caret to the end, otherwise the next Tab key may not move focus.
650 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
651 browser(), ui::VKEY_END, false, false, false, false));
652
[email protected]9e0c83a2009-05-06 19:44:37653 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35654 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37655 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30656 std::string actual;
657 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
658 interstitial_page->render_view_host(), L"",
659 L"window.domAutomationController.send(getFocusedElement());",
660 &actual));
[email protected]9e0c83a2009-05-06 19:44:37661 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
662
[email protected]432115822011-07-10 15:52:27663 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53664 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07665 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35666 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27667 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56668 notification_source = content::Source<RenderViewHost>(
669 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35670 } else {
671 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27672 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53673 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35674 }
[email protected]a6e602f2010-09-28 22:28:30675
676 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09677 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30678 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37679 }
680
681 // At this point the renderer has sent us a message asking to advance the
682 // focus (as the end of the focus loop was reached in the renderer).
683 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42684 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37685 }
686
687 // Now let's try reverse focus traversal.
688 for (int i = 0; i < 2; ++i) {
689 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52690 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37691
[email protected]911696b2011-01-28 02:36:49692 // Move the caret to the end, otherwise the next Tab key may not move focus.
693 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
694 browser(), ui::VKEY_END, false, false, false, false));
695
[email protected]9e0c83a2009-05-06 19:44:37696 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35697 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27698 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53699 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07700 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35701 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27702 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56703 notification_source = content::Source<RenderViewHost>(
704 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35705 } else {
706 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27707 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53708 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35709 }
[email protected]9e0c83a2009-05-06 19:44:37710
[email protected]a6e602f2010-09-28 22:28:30711 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09712 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30713 notification_type, notification_source));
714
[email protected]9e0c83a2009-05-06 19:44:37715 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30716 std::string actual;
717 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
718 interstitial_page->render_view_host(), L"",
719 L"window.domAutomationController.send(getFocusedElement());",
720 &actual));
[email protected]9e0c83a2009-05-06 19:44:37721 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
722 }
723
724 // At this point the renderer has sent us a message asking to advance the
725 // focus (as the end of the focus loop was reached in the renderer).
726 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42727 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37728 }
729}
730
731// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33732// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05733#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]4d2451652012-02-14 23:54:26734IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33735#else
[email protected]e4f4e0b2009-10-13 19:58:21736IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33737#endif
[email protected]a6e602f2010-09-28 22:28:30738 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11739 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37740
741 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11742 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37743 ui_test_utils::NavigateToURL(browser(), url);
744
[email protected]9e0c83a2009-05-06 19:44:37745 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06746 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9f76c1e2012-03-05 15:15:58747 EXPECT_TRUE(browser()->GetSelectedWebContents()->GetRenderViewHost()->
748 GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37749
[email protected]9d8a4642009-07-29 17:25:30750 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37751 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20752 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37753 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37754 // Give some time for the interstitial to show.
755 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17756 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42757 base::TimeDelta::FromSeconds(1));
[email protected]9e0c83a2009-05-06 19:44:37758 ui_test_utils::RunMessageLoop();
759
760 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06761 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37762 EXPECT_TRUE(interstitial_page->HasFocus());
763
764 // Hide the interstitial.
765 interstitial_page->DontProceed();
766
767 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06768 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37769}
770
[email protected]9bd491ee2008-12-10 22:31:07771// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20772// Flaky on mac and valgrind. http://crbug.com/67301.
773#if defined(OS_MACOSX)
[email protected]4d2451652012-02-14 23:54:26774#define MAYBE_FindFocusTest DISABLED_FindFocusTest
[email protected]2004b762011-05-05 22:43:20775#else
776#define MAYBE_FindFocusTest FindFocusTest
777#endif
[email protected]a711df102010-12-14 09:47:03778IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30779 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11780 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55781
[email protected]9bd491ee2008-12-10 22:31:07782 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11783 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18784 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07785
[email protected]a711df102010-12-14 09:47:03786 EXPECT_TRUE(ChromeInForeground());
787
[email protected]853300a82010-07-27 21:17:57788#if defined(OS_MACOSX)
789 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28790 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09791 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57792#else
[email protected]9bd491ee2008-12-10 22:31:07793 // Press Ctrl+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, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57796#endif
[email protected]8bcdec92009-02-25 16:15:18797
[email protected]a9c060ca2012-01-05 20:43:41798 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07799
[email protected]fc2e0872009-08-21 22:14:41800 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52801 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07802
803 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57804#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28805 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09806 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57807#else
[email protected]1d000682010-08-23 16:21:28808 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09809 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57810#endif
[email protected]21abcc742009-10-23 02:52:06811 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07812
813 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41814 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06815 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07816
817 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57818#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28819 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09820 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57821#else
[email protected]1d000682010-08-23 16:21:28822 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09823 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57824#endif
[email protected]8bcdec92009-02-25 16:15:18825
[email protected]a9c060ca2012-01-05 20:43:41826 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07827}
[email protected]401513c2009-03-12 00:21:28828
829// Makes sure the focus is in the right location when opening the different
830// types of tabs.
[email protected]c92928672010-11-09 18:31:07831// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26832IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30833 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07834
[email protected]401513c2009-03-12 00:21:28835 // Open the history tab, focus should be on the tab contents.
836 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42837 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20838 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42839 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28840
841 // Open the new tab, focus should be on the location bar.
842 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42843 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20844 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52845 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28846
847 // Open the download tab, focus should be on the tab contents.
848 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42849 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20850 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42851 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43852
853 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55854 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58855 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42856 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20857 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52858 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28859}
[email protected]9d8a4642009-07-29 17:25:30860
861// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21862IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30863 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11864 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30865
[email protected]9d8a4642009-07-29 17:25:30866 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06867 {
868 ui_test_utils::WindowedNotificationObserver observer(
869 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07870 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06871 browser()->NewTab();
872 observer.Wait();
873 }
[email protected]f07d7bf2010-04-06 08:02:42874 ui_test_utils::RunAllPendingInMessageLoop();
875
[email protected]6b4e8e42011-08-17 19:36:06876 {
877 ui_test_utils::WindowedNotificationObserver observer(
878 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50879 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46880 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56881 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06882 browser()->Reload(CURRENT_TAB);
883 observer.Wait();
884 }
[email protected]9d8a4642009-07-29 17:25:30885 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52886 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30887
888 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11889 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41890 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52891 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06892 {
893 ui_test_utils::WindowedNotificationObserver observer(
894 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50895 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46896 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56897 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06898 browser()->Reload(CURRENT_TAB);
899 observer.Wait();
900 }
[email protected]9ba21ede2010-07-30 01:11:07901
[email protected]9d8a4642009-07-29 17:25:30902 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41903 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06904 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30905}
906
907// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27908IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30909 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11910 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30911
[email protected]9d8a4642009-07-29 17:25:30912 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11913 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]4ca15302012-01-03 05:53:20914 ui_test_utils::CrashTab(browser()->GetSelectedWebContents());
[email protected]6b4e8e42011-08-17 19:36:06915 {
916 ui_test_utils::WindowedNotificationObserver observer(
917 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50918 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46919 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56920 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06921 browser()->Reload(CURRENT_TAB);
922 observer.Wait();
923 }
[email protected]9ba21ede2010-07-30 01:11:07924
[email protected]9d8a4642009-07-29 17:25:30925 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41926 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06927 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30928}
[email protected]629e0342010-07-27 23:30:13929
930} // namespace