blob: 9d68a7b600822975c58acbff2afb86fc3d5fca98 [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]a9c060ca2012-01-05 20:43:417#include "base/bind.h"
[email protected]3985ba82010-07-29 21:44:128#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:429#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:1810#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1211#include "base/path_service.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]477ae052011-11-18 23:53:5713#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2214#include "base/utf_string_conversions.h"
[email protected]c848d3d92010-09-16 21:57:4515#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1016#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3317#include "chrome/browser/ui/browser_window.h"
[email protected]6b4e8e42011-08-17 19:36:0618#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]6a3ec2312010-12-02 19:30:1919#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2720#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3721#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5522#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3223#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5724#include "chrome/test/base/ui_test_utils.h"
[email protected]5de634712011-03-02 00:20:1925#include "content/browser/renderer_host/render_view_host.h"
[email protected]cadaec52012-02-08 21:53:1326#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1327#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5728#include "content/public/browser/notification_service.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]753efc42010-03-09 19:52:1645#if defined(TOOLKIT_USES_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]4b19ea52012-01-02 20:15:2557using content::WebContents;
58
[email protected]3a3cf4d2011-12-09 00:39:3559#if defined(OS_MACOSX)
60// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4861#define MAYBE_FocusTraversal DISABLED_FocusTraversal
62#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4863#elif defined(OS_WIN)
64// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1865#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4866#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3567#else
68#define MAYBE_FocusTraversal FocusTraversal
69#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
70#endif
71
72#if defined(OS_LINUX) || defined(OS_MACOSX)
73// TODO(jcampan): http://crbug.com/23683 for linux.
74// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]853300a82010-07-27 21:17:5775#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
76#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0777// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2678#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4179#endif
80
initial.commit09911bf2008-07-26 23:55:2981namespace {
82
[email protected]8bcdec92009-02-25 16:15:1883// The delay waited in some cases where we don't have a notifications for an
84// action we take.
initial.commit09911bf2008-07-26 23:55:2985const int kActionDelayMs = 500;
86
[email protected]a9c060ca2012-01-05 20:43:4187// Maxiumum time to wait until the focus is moved to expected view.
88const int kFocusChangeTimeoutMs = 500;
89
[email protected]f72a1cc2010-04-30 07:17:3090const char kSimplePage[] = "files/focus/page_with_focus.html";
91const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
92const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3193const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2994
[email protected]a711df102010-12-14 09:47:0395// Test to make sure Chrome is in the foreground as we start testing. This is
96// required for tests that synthesize input to the Chrome window.
97bool ChromeInForeground() {
98#if defined(OS_WIN)
99 HWND window = ::GetForegroundWindow();
100 std::wstring caption;
101 std::wstring filename;
102 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18103 if (len > 1)
104 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03105 bool chrome_window_in_foreground =
106 EndsWith(caption, L" - Google Chrome", true) ||
107 EndsWith(caption, L" - Chromium", true);
108 if (!chrome_window_in_foreground) {
109 DWORD process_id;
110 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
111
112 base::ProcessHandle process;
113 if (base::OpenProcessHandleWithAccess(process_id,
114 PROCESS_QUERY_LIMITED_INFORMATION,
115 &process)) {
[email protected]fdce4782011-11-29 20:06:18116 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
117 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03118 int error = GetLastError();
119 filename = std::wstring(L"Unable to read filename for process id '" +
120 base::IntToString16(process_id) +
121 L"' (error ") +
122 base::IntToString16(error) + L")";
123 }
124 base::CloseProcessHandle(process);
125 }
126 }
127 EXPECT_TRUE(chrome_window_in_foreground)
128 << "Chrome must be in the foreground when running interactive tests\n"
129 << "Process in foreground: " << filename.c_str() << "\n"
130 << "Window: " << window << "\n"
131 << "Caption: " << caption.c_str();
132 return chrome_window_in_foreground;
133#else
134 // Windows only at the moment.
135 return true;
136#endif
137}
138
[email protected]a9c060ca2012-01-05 20:43:41139// Wait the focus change in message loop.
140void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
141 if (ui_test_utils::IsViewFocused(browser, id) ||
142 base::Time::Now() > timeout) {
143 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
144 } else {
145 MessageLoop::current()->PostDelayedTask(
146 FROM_HERE,
147 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]bd493ca2012-03-04 07:32:33148 10);
[email protected]a9c060ca2012-01-05 20:43:41149 }
150};
151
[email protected]8bcdec92009-02-25 16:15:18152class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29153 public:
[email protected]0b8fa8b2011-12-07 00:54:52154 BrowserFocusTest() :
155#if defined(USE_AURA)
156 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
157#else
158 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
159#endif
160 {
[email protected]8bcdec92009-02-25 16:15:18161 set_show_window(true);
162 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29163 }
[email protected]b9821882009-08-17 22:25:17164
[email protected]21abcc742009-10-23 02:52:06165 bool IsViewFocused(ViewID vid) {
166 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17167 }
168
[email protected]fc2e0872009-08-21 22:14:41169 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06170 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41171 }
[email protected]0b8fa8b2011-12-07 00:54:52172
[email protected]a9c060ca2012-01-05 20:43:41173 bool WaitForFocusChange(ViewID vid) {
174 const base::Time timeout = base::Time::Now() +
175 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
176 MessageLoop::current()->PostDelayedTask(
177 FROM_HERE,
178 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]bd493ca2012-03-04 07:32:33179 100);
[email protected]a9c060ca2012-01-05 20:43:41180 ui_test_utils::RunMessageLoop();
181 return IsViewFocused(vid);
182 }
183
[email protected]0b8fa8b2011-12-07 00:54:52184 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29185};
186
[email protected]85f0a572012-02-07 22:20:13187class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37188 public:
[email protected]85f0a572012-02-07 22:20:13189 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]b65de8b92009-09-14 19:36:31190 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37191 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
192 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31193 file_path = file_path.AppendASCII("focus");
194 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37195 r = file_util::ReadFileToString(file_path, &html_contents_);
196 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13197 interstitial_page_ = InterstitialPage::Create(
198 tab, new_navigation, url , this);
199 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37200 }
201
202 virtual std::string GetHTMLContents() {
203 return html_contents_;
204 }
205
[email protected]85f0a572012-02-07 22:20:13206 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13207 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37208 }
209
[email protected]85f0a572012-02-07 22:20:13210 void DontProceed() {
211 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21212 }
213
[email protected]9e0c83a2009-05-06 19:44:37214 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58215 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37216 }
217
218 private:
219 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13220 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37221};
[email protected]b9821882009-08-17 22:25:17222
[email protected]fab6ffb2011-09-22 18:35:34223IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30224 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42225#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41226 // It seems we have to wait a little bit for the widgets to spin up before
227 // we can start clicking on them.
[email protected]bd493ca2012-03-04 07:32:33228 MessageLoop::current()->PostDelayedTask(FROM_HERE,
229 MessageLoop::QuitClosure(),
230 kActionDelayMs);
[email protected]fc2e0872009-08-21 22:14:41231 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42232#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41233
[email protected]0b8fa8b2011-12-07 00:54:52234 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00235
[email protected]fc2e0872009-08-21 22:14:41236 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06237 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00238
[email protected]fc2e0872009-08-21 22:14:41239 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52240 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00241}
[email protected]186f13f2009-08-19 20:34:00242
[email protected]320948e02011-01-10 08:21:12243// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26244IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30245 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11246 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29247
248 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11249 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18250 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29251
[email protected]186f13f2009-08-19 20:34:00252 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
253
initial.commit09911bf2008-07-26 23:55:29254 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06255 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29256 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57257 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29258 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]21abcc742009-10-23 02:52:06259 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29260
[email protected]186f13f2009-08-19 20:34:00261 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52262 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29263 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57264 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29265 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52266 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29267
[email protected]186f13f2009-08-19 20:34:00268 // The rest of this test does not make sense on Linux because the behavior
269 // of Activate() is not well defined and can vary by window manager.
270#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29271 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18272 Browser* browser2 = Browser::Create(browser()->profile());
273 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43274 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18275 browser2->window()->Show();
276 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29277
[email protected]0c966682011-08-02 18:22:10278 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00279 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16280 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18281 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50282 const views::Widget* widget2 =
283 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10284 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50285 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18286 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54287 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38288 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29289
290 // Switch to the 1st browser window, focus should still be on the location
291 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18292 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52293 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18294 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29295
296 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18297 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10298 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
299 ASSERT_TRUE(widget);
300 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54301 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38302 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18303
304 // Close the 2nd browser to avoid a DCHECK().
305 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00306#endif
initial.commit09911bf2008-07-26 23:55:29307}
308
309// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39310// Disabled, http://crbug.com/62542.
311IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30312 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11313 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29314
315 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11316 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18317 ui_test_utils::NavigateToURL(browser(), url);
318
initial.commit09911bf2008-07-26 23:55:29319 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33320 for (int i = 0; i < 4; ++i)
[email protected]2905f742011-10-13 03:51:58321 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
initial.commit09911bf2008-07-26 23:55:29322
323 // Alternate focus for the tab.
324 const bool kFocusPage[3][5] = {
325 { true, true, true, true, false },
326 { false, false, false, false, false },
327 { false, true, false, true, false }
328 };
329
330 for (int i = 1; i < 3; i++) {
331 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18332 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41333 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29334
335 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54336 if (kFocusPage[i][j]) {
[email protected]6acde6352012-01-04 16:52:20337 browser()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54338 } else {
[email protected]186f13f2009-08-19 20:34:00339 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54340 }
initial.commit09911bf2008-07-26 23:55:29341 }
342
343 // Now come back to the tab and check the right view is focused.
344 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18345 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41346 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29347
[email protected]186f13f2009-08-19 20:34:00348 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52349 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06350 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29351 }
[email protected]cb7e2542009-12-14 22:02:35352
[email protected]eaca0ad12011-04-18 15:53:41353 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35354 // Try the above, but with ctrl+tab. Since tab normally changes focus,
355 // this has regressed in the past. Loop through several times to be sure.
356 for (int j = 0; j < 15; j++) {
357 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[email protected]0b8fa8b2011-12-07 00:54:52358 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35359 ASSERT_TRUE(IsViewFocused(vid));
360
[email protected]1d000682010-08-23 16:21:28361 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09362 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35363 }
364
365 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41366 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35367 for (int j = 14; j >= 0; --j) {
368 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
[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, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35374 }
initial.commit09911bf2008-07-26 23:55:29375 }
376}
377
[email protected]ae40b572009-10-02 21:17:45378// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35379IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30380 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11381 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45382
383 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11384 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45385 ui_test_utils::NavigateToURL(browser(), url);
386
387 browser()->Find();
[email protected]9c318862011-02-01 22:27:24388 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45389 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06390 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45391
392 // Focus the location bar.
393 browser()->FocusLocationBar();
394
395 // Create a 2nd tab.
[email protected]2905f742011-10-13 03:51:58396 browser()->AddSelectedTabWithURL(url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45397
398 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06399 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45400
401 // Select 1st tab, focus should still be on the location-bar.
402 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41403 browser()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52404 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45405
406 // Now open the find box again, switch to another tab and come back, the focus
407 // should return to the find box.
408 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06409 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41410 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06411 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41412 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06413 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45414}
415
initial.commit09911bf2008-07-26 23:55:29416// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07417// Flaky, http://crbug.com/62538.
418IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26419 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30420 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11421 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29422
initial.commit09911bf2008-07-26 23:55:29423 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18424 Browser* browser2 = Browser::Create(browser()->profile());
425 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43426 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18427 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00428
[email protected]ed179ee2009-10-03 21:02:51429 Browser* focused_browser = NULL;
430 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16431#if defined(USE_X11)
432 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00433 // to figure out which browser does have focus.
434 if (browser2->window()->IsActive()) {
435 focused_browser = browser2;
436 unfocused_browser = browser();
437 } else if (browser()->window()->IsActive()) {
438 focused_browser = browser();
439 unfocused_browser = browser2;
440 } else {
[email protected]579c3d82010-10-06 03:53:51441 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00442 }
443#elif defined(OS_WIN)
444 focused_browser = browser();
445 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57446#elif defined(OS_MACOSX)
447 // On Mac, the newly created window always gets the focus.
448 focused_browser = browser2;
449 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00450#endif
451
[email protected]95409e12010-08-17 20:07:11452 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00453 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46454
[email protected]8bcdec92009-02-25 16:15:18455 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00456 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29457
[email protected]579c3d82010-10-06 03:53:51458 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20459 unfocused_browser->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51460 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29461
[email protected]8bcdec92009-02-25 16:15:18462 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00463 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29464}
465
466// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21467IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30468 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11469 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29470
471 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11472 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18473 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29474
[email protected]186f13f2009-08-19 20:34:00475 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29476
[email protected]579c3d82010-10-06 03:53:51477 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
[email protected]4ca15302012-01-03 05:53:20478 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51479 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29480
481 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52482 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29483}
484
[email protected]9e0c83a2009-05-06 19:44:37485// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35486// Note that this test relies on a notification from the renderer that the
487// focus has changed in the page. The notification in the renderer may change
488// at which point this test would fail (see comment in
489// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57490IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30491 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11492 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29493
[email protected]8bcdec92009-02-25 16:15:18494 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11495 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18496 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29497
[email protected]186f13f2009-08-19 20:34:00498 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29499
[email protected]546ae4e02010-12-08 14:57:19500 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18501 const char* kExpElementIDs[] = {
502 "", // Initially no element in the page should be focused
503 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19504 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18505 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29506 };
507
508 // Test forward focus traversal.
509 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40510 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29511 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52512 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29513
[email protected]911696b2011-01-28 02:36:49514 // Move the caret to the end, otherwise the next Tab key may not move focus.
515 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
516 browser(), ui::VKEY_END, false, false, false, false));
517
initial.commit09911bf2008-07-26 23:55:29518 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35519 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40520 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29521 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01522 std::string actual;
523 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20524 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18525 L"",
[email protected]45671612009-04-29 22:24:01526 L"window.domAutomationController.send(getFocusedElement());",
527 &actual));
initial.commit09911bf2008-07-26 23:55:29528 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
529
[email protected]130efb02009-09-18 18:54:35530 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19531 // If the next element is the kTextElementID, we expect to be
532 // notified we have switched to an editable node.
533 bool is_editable_node =
534 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53535 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19536
537 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09538 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27539 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56540 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20541 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19542 details));
[email protected]130efb02009-09-18 18:54:35543 } else {
544 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19545 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09546 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27547 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53548 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35549 }
initial.commit09911bf2008-07-26 23:55:29550 }
[email protected]8bcdec92009-02-25 16:15:18551
552 // At this point the renderer has sent us a message asking to advance the
553 // focus (as the end of the focus loop was reached in the renderer).
554 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42555 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29556 }
557
558 // Now let's try reverse focus traversal.
559 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40560 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29561 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52562 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29563
[email protected]911696b2011-01-28 02:36:49564 // Move the caret to the end, otherwise the next Tab key may not move focus.
565 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
566 browser(), ui::VKEY_END, false, false, false, false));
567
[email protected]8bcdec92009-02-25 16:15:18568 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30569 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40570 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19571 const char* next_element =
572 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35573
574 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19575 // If the next element is the kTextElementID, we expect to be
576 // notified we have switched to an editable node.
577 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53578 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19579
580 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09581 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27582 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56583 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]4ca15302012-01-03 05:53:20584 browser()->GetSelectedWebContents()->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19585 details));
[email protected]130efb02009-09-18 18:54:35586 } else {
587 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19588 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09589 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27590 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53591 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35592 }
initial.commit09911bf2008-07-26 23:55:29593
594 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01595 std::string actual;
596 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]4ca15302012-01-03 05:53:20597 browser()->GetSelectedWebContents()->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18598 L"",
[email protected]45671612009-04-29 22:24:01599 L"window.domAutomationController.send(getFocusedElement());",
600 &actual));
[email protected]546ae4e02010-12-08 14:57:19601 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29602 }
[email protected]8bcdec92009-02-25 16:15:18603
604 // At this point the renderer has sent us a message asking to advance the
605 // focus (as the end of the focus loop was reached in the renderer).
606 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42607 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29608 }
609}
610
[email protected]9e0c83a2009-05-06 19:44:37611// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41612IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30613 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11614 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37615
616 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11617 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37618 ui_test_utils::NavigateToURL(browser(), url);
619
[email protected]9e0c83a2009-05-06 19:44:37620 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06621 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37622
623 // Let's show an interstitial.
624 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20625 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37626 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37627 // Give some time for the interstitial to show.
628 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17629 MessageLoop::QuitClosure(),
[email protected]bd493ca2012-03-04 07:32:33630 1000);
[email protected]9e0c83a2009-05-06 19:44:37631 ui_test_utils::RunMessageLoop();
632
[email protected]fc2e0872009-08-21 22:14:41633 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37634
635 const char* kExpElementIDs[] = {
636 "", // Initially no element in the page should be focused
637 // (the location bar is focused).
638 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
639 "gmapLink"
640 };
641
642 // Test forward focus traversal.
643 for (int i = 0; i < 2; ++i) {
644 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52645 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37646
[email protected]911696b2011-01-28 02:36:49647 // Move the caret to the end, otherwise the next Tab key may not move focus.
648 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
649 browser(), ui::VKEY_END, false, false, false, false));
650
[email protected]9e0c83a2009-05-06 19:44:37651 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35652 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37653 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30654 std::string actual;
655 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
656 interstitial_page->render_view_host(), L"",
657 L"window.domAutomationController.send(getFocusedElement());",
658 &actual));
[email protected]9e0c83a2009-05-06 19:44:37659 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
660
[email protected]432115822011-07-10 15:52:27661 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53662 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07663 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35664 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27665 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56666 notification_source = content::Source<RenderViewHost>(
667 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35668 } else {
669 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27670 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53671 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35672 }
[email protected]a6e602f2010-09-28 22:28:30673
674 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09675 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30676 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37677 }
678
679 // At this point the renderer has sent us a message asking to advance the
680 // focus (as the end of the focus loop was reached in the renderer).
681 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42682 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37683 }
684
685 // Now let's try reverse focus traversal.
686 for (int i = 0; i < 2; ++i) {
687 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52688 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37689
[email protected]911696b2011-01-28 02:36:49690 // Move the caret to the end, otherwise the next Tab key may not move focus.
691 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
692 browser(), ui::VKEY_END, false, false, false, false));
693
[email protected]9e0c83a2009-05-06 19:44:37694 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35695 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27696 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53697 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07698 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35699 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27700 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56701 notification_source = content::Source<RenderViewHost>(
702 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35703 } else {
704 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27705 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53706 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35707 }
[email protected]9e0c83a2009-05-06 19:44:37708
[email protected]a6e602f2010-09-28 22:28:30709 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09710 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30711 notification_type, notification_source));
712
[email protected]9e0c83a2009-05-06 19:44:37713 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30714 std::string actual;
715 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
716 interstitial_page->render_view_host(), L"",
717 L"window.domAutomationController.send(getFocusedElement());",
718 &actual));
[email protected]9e0c83a2009-05-06 19:44:37719 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
720 }
721
722 // At this point the renderer has sent us a message asking to advance the
723 // focus (as the end of the focus loop was reached in the renderer).
724 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42725 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37726 }
727}
728
729// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33730// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05731#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]4d2451652012-02-14 23:54:26732IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33733#else
[email protected]e4f4e0b2009-10-13 19:58:21734IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33735#endif
[email protected]a6e602f2010-09-28 22:28:30736 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11737 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37738
739 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11740 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37741 ui_test_utils::NavigateToURL(browser(), url);
742
[email protected]9e0c83a2009-05-06 19:44:37743 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06744 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9f76c1e2012-03-05 15:15:58745 EXPECT_TRUE(browser()->GetSelectedWebContents()->GetRenderViewHost()->
746 GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37747
[email protected]9d8a4642009-07-29 17:25:30748 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37749 TestInterstitialPage* interstitial_page =
[email protected]4ca15302012-01-03 05:53:20750 new TestInterstitialPage(browser()->GetSelectedWebContents(),
[email protected]9e0c83a2009-05-06 19:44:37751 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37752 // Give some time for the interstitial to show.
753 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17754 MessageLoop::QuitClosure(),
[email protected]bd493ca2012-03-04 07:32:33755 1000);
[email protected]9e0c83a2009-05-06 19:44:37756 ui_test_utils::RunMessageLoop();
757
758 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06759 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37760 EXPECT_TRUE(interstitial_page->HasFocus());
761
762 // Hide the interstitial.
763 interstitial_page->DontProceed();
764
765 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06766 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37767}
768
[email protected]9bd491ee2008-12-10 22:31:07769// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20770// Flaky on mac and valgrind. http://crbug.com/67301.
771#if defined(OS_MACOSX)
[email protected]4d2451652012-02-14 23:54:26772#define MAYBE_FindFocusTest DISABLED_FindFocusTest
[email protected]2004b762011-05-05 22:43:20773#else
774#define MAYBE_FindFocusTest FindFocusTest
775#endif
[email protected]a711df102010-12-14 09:47:03776IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30777 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11778 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55779
[email protected]9bd491ee2008-12-10 22:31:07780 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11781 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18782 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07783
[email protected]a711df102010-12-14 09:47:03784 EXPECT_TRUE(ChromeInForeground());
785
[email protected]853300a82010-07-27 21:17:57786#if defined(OS_MACOSX)
787 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28788 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09789 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57790#else
[email protected]9bd491ee2008-12-10 22:31:07791 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28792 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09793 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57794#endif
[email protected]8bcdec92009-02-25 16:15:18795
[email protected]a9c060ca2012-01-05 20:43:41796 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07797
[email protected]fc2e0872009-08-21 22:14:41798 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52799 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07800
801 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57802#if defined(OS_MACOSX)
[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, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57805#else
[email protected]1d000682010-08-23 16:21:28806 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09807 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57808#endif
[email protected]21abcc742009-10-23 02:52:06809 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07810
811 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41812 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06813 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07814
815 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57816#if defined(OS_MACOSX)
[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, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57819#else
[email protected]1d000682010-08-23 16:21:28820 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09821 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57822#endif
[email protected]8bcdec92009-02-25 16:15:18823
[email protected]a9c060ca2012-01-05 20:43:41824 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07825}
[email protected]401513c2009-03-12 00:21:28826
827// Makes sure the focus is in the right location when opening the different
828// types of tabs.
[email protected]c92928672010-11-09 18:31:07829// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26830IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30831 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07832
[email protected]401513c2009-03-12 00:21:28833 // Open the history tab, focus should be on the tab contents.
834 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42835 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20836 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42837 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28838
839 // Open the new tab, focus should be on the location bar.
840 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42841 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20842 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52843 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28844
845 // Open the download tab, focus should be on the tab contents.
846 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42847 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20848 browser()->GetSelectedWebContents()));
[email protected]a3f343f2010-10-06 23:39:42849 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43850
851 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55852 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
[email protected]2905f742011-10-13 03:51:58853 content::PAGE_TRANSITION_LINK);
[email protected]a3f343f2010-10-06 23:39:42854 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]4ca15302012-01-03 05:53:20855 browser()->GetSelectedWebContents()));
[email protected]0b8fa8b2011-12-07 00:54:52856 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28857}
[email protected]9d8a4642009-07-29 17:25:30858
859// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21860IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30861 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11862 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30863
[email protected]9d8a4642009-07-29 17:25:30864 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06865 {
866 ui_test_utils::WindowedNotificationObserver observer(
867 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07868 content::NotificationService::AllSources());
[email protected]6b4e8e42011-08-17 19:36:06869 browser()->NewTab();
870 observer.Wait();
871 }
[email protected]f07d7bf2010-04-06 08:02:42872 ui_test_utils::RunAllPendingInMessageLoop();
873
[email protected]6b4e8e42011-08-17 19:36:06874 {
875 ui_test_utils::WindowedNotificationObserver observer(
876 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50877 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46878 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56879 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06880 browser()->Reload(CURRENT_TAB);
881 observer.Wait();
882 }
[email protected]9d8a4642009-07-29 17:25:30883 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52884 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30885
886 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11887 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41888 browser()->FocusLocationBar();
[email protected]0b8fa8b2011-12-07 00:54:52889 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06890 {
891 ui_test_utils::WindowedNotificationObserver observer(
892 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50893 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46894 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56895 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06896 browser()->Reload(CURRENT_TAB);
897 observer.Wait();
898 }
[email protected]9ba21ede2010-07-30 01:11:07899
[email protected]9d8a4642009-07-29 17:25:30900 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41901 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06902 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30903}
904
905// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27906IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30907 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11908 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30909
[email protected]9d8a4642009-07-29 17:25:30910 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11911 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]4ca15302012-01-03 05:53:20912 ui_test_utils::CrashTab(browser()->GetSelectedWebContents());
[email protected]6b4e8e42011-08-17 19:36:06913 {
914 ui_test_utils::WindowedNotificationObserver observer(
915 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50916 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:46917 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:56918 GetController()));
[email protected]6b4e8e42011-08-17 19:36:06919 browser()->Reload(CURRENT_TAB);
920 observer.Wait();
921 }
[email protected]9ba21ede2010-07-30 01:11:07922
[email protected]9d8a4642009-07-29 17:25:30923 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41924 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06925 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30926}
[email protected]629e0342010-07-27 23:30:13927
928} // namespace