blob: 303f5e8d82de01f5af693038a5163d78135f409c [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 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]3985ba82010-07-29 21:44:127#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:428#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:189#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1210#include "base/path_service.h"
[email protected]1870d5cf2011-05-12 01:55:4011#include "base/stringprintf.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]be1ce6a72010-08-03 14:35:2213#include "base/utf_string_conversions.h"
[email protected]c848d3d92010-09-16 21:57:4514#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3316#include "chrome/browser/ui/browser_window.h"
[email protected]6a3ec2312010-12-02 19:30:1917#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2718#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3719#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5520#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3221#include "chrome/test/base/ui_test_utils.h"
22#include "chrome/test/base/in_process_browser_test.h"
[email protected]5de634712011-03-02 00:20:1923#include "content/browser/renderer_host/render_view_host.h"
24#include "content/browser/renderer_host/render_widget_host_view.h"
25#include "content/browser/tab_contents/interstitial_page.h"
26#include "content/browser/tab_contents/tab_contents.h"
27#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]216813952011-05-19 22:21:2628#include "content/common/view_messages.h"
[email protected]3985ba82010-07-29 21:44:1229#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5730
31#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0532#include "views/focus/focus_manager.h"
33#include "views/view.h"
[email protected]853300a82010-07-27 21:17:5734#endif
initial.commit09911bf2008-07-26 23:55:2935
[email protected]134c47b92009-08-19 03:33:4436#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5437#include "chrome/browser/ui/views/frame/browser_view.h"
38#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
39#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4440#endif
41
[email protected]753efc42010-03-09 19:52:1642#if defined(TOOLKIT_USES_GTK)
[email protected]93270d002011-01-19 22:32:5943#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1744#endif
45
[email protected]a711df102010-12-14 09:47:0346#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0347#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5948#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4049#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0350#endif
51
[email protected]fc2e0872009-08-21 22:14:4152#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5753#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1854#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3555// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5956#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5757#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1558// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4859#define MAYBE_FocusTraversal DISABLED_FocusTraversal
60#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]853300a82010-07-27 21:17:5761// TODO(suzhe): http://crbug.com/49737
62#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
63#elif defined(OS_WIN)
[email protected]26153732010-11-09 18:47:3964// Disabled, http://crbug.com/62543.
65#define MAYBE_FocusTraversal DISABLED_FocusTraversal
66// Disabled, http://crbug.com/62544.
67#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]c92928672010-11-09 18:31:0768// Flaky, http://crbug.com/62537.
69#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4170#endif
71
initial.commit09911bf2008-07-26 23:55:2972namespace {
73
[email protected]8bcdec92009-02-25 16:15:1874// The delay waited in some cases where we don't have a notifications for an
75// action we take.
initial.commit09911bf2008-07-26 23:55:2976const int kActionDelayMs = 500;
77
[email protected]f72a1cc2010-04-30 07:17:3078const char kSimplePage[] = "files/focus/page_with_focus.html";
79const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
80const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3181const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2982
[email protected]a711df102010-12-14 09:47:0383// Test to make sure Chrome is in the foreground as we start testing. This is
84// required for tests that synthesize input to the Chrome window.
85bool ChromeInForeground() {
86#if defined(OS_WIN)
87 HWND window = ::GetForegroundWindow();
88 std::wstring caption;
89 std::wstring filename;
90 int len = ::GetWindowTextLength(window) + 1;
91 ::GetWindowText(window, WriteInto(&caption, len), len);
92 bool chrome_window_in_foreground =
93 EndsWith(caption, L" - Google Chrome", true) ||
94 EndsWith(caption, L" - Chromium", true);
95 if (!chrome_window_in_foreground) {
96 DWORD process_id;
97 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
98
99 base::ProcessHandle process;
100 if (base::OpenProcessHandleWithAccess(process_id,
101 PROCESS_QUERY_LIMITED_INFORMATION,
102 &process)) {
103 len = MAX_PATH;
104 if (!GetProcessImageFileName(process, WriteInto(&filename, len), len)) {
105 int error = GetLastError();
106 filename = std::wstring(L"Unable to read filename for process id '" +
107 base::IntToString16(process_id) +
108 L"' (error ") +
109 base::IntToString16(error) + L")";
110 }
111 base::CloseProcessHandle(process);
112 }
113 }
114 EXPECT_TRUE(chrome_window_in_foreground)
115 << "Chrome must be in the foreground when running interactive tests\n"
116 << "Process in foreground: " << filename.c_str() << "\n"
117 << "Window: " << window << "\n"
118 << "Caption: " << caption.c_str();
119 return chrome_window_in_foreground;
120#else
121 // Windows only at the moment.
122 return true;
123#endif
124}
125
[email protected]8bcdec92009-02-25 16:15:18126class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29127 public:
128 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:18129 set_show_window(true);
130 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29131 }
[email protected]b9821882009-08-17 22:25:17132
[email protected]21abcc742009-10-23 02:52:06133 bool IsViewFocused(ViewID vid) {
134 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17135 }
136
[email protected]fc2e0872009-08-21 22:14:41137 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06138 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41139 }
initial.commit09911bf2008-07-26 23:55:29140};
141
[email protected]9e0c83a2009-05-06 19:44:37142class TestInterstitialPage : public InterstitialPage {
143 public:
144 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30145 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31146 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37147 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
148 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31149 file_path = file_path.AppendASCII("focus");
150 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37151 r = file_util::ReadFileToString(file_path, &html_contents_);
152 EXPECT_TRUE(r);
153 }
154
155 virtual std::string GetHTMLContents() {
156 return html_contents_;
157 }
158
[email protected]4e85c112011-01-26 22:27:21159 // Exposing render_view_host() and tab() to be public; they are declared as
160 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30161 virtual RenderViewHost* render_view_host() {
162 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37163 }
164
[email protected]4e85c112011-01-26 22:27:21165 virtual TabContents* tab() {
166 return InterstitialPage::tab();
167 }
168
[email protected]9e0c83a2009-05-06 19:44:37169 bool HasFocus() {
170 return render_view_host()->view()->HasFocus();
171 }
172
[email protected]130efb02009-09-18 18:54:35173 protected:
[email protected]216813952011-05-19 22:21:26174 bool OnMessageReceived(const IPC::Message& message) {
175 bool handled = true;
176 IPC_BEGIN_MESSAGE_MAP(TestInterstitialPage, message)
177 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
178 IPC_MESSAGE_UNHANDLED(handled = false)
179 IPC_END_MESSAGE_MAP()
180 return handled;
181 }
182
183 void OnFocusedNodeChanged(bool is_editable_node) {
[email protected]a6e602f2010-09-28 22:28:30184 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27185 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21186 Source<TabContents>(tab()),
[email protected]546ae4e02010-12-08 14:57:19187 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:35188 }
189
[email protected]9e0c83a2009-05-06 19:44:37190 private:
191 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37192};
[email protected]b9821882009-08-17 22:25:17193
[email protected]e4f4e0b2009-10-13 19:58:21194IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30195 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42196#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41197 // It seems we have to wait a little bit for the widgets to spin up before
198 // we can start clicking on them.
199 MessageLoop::current()->PostDelayedTask(FROM_HERE,
200 new MessageLoop::QuitTask(),
201 kActionDelayMs);
202 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42203#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41204
[email protected]21abcc742009-10-23 02:52:06205 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00206
[email protected]fc2e0872009-08-21 22:14:41207 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06208 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00209
[email protected]fc2e0872009-08-21 22:14:41210 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06211 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00212}
[email protected]186f13f2009-08-19 20:34:00213
[email protected]320948e02011-01-10 08:21:12214// Flaky, http://crbug.com/69034.
215IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30216 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11217 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29218
219 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11220 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18221 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29222
[email protected]186f13f2009-08-19 20:34:00223 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
224
initial.commit09911bf2008-07-26 23:55:29225 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06226 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29227 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57228 ui_test_utils::HideNativeWindow(window);
229 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06230 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29231
[email protected]186f13f2009-08-19 20:34:00232 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06233 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29234 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57235 ui_test_utils::HideNativeWindow(window);
236 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06237 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29238
[email protected]186f13f2009-08-19 20:34:00239 // The rest of this test does not make sense on Linux because the behavior
240 // of Activate() is not well defined and can vary by window manager.
241#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29242 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18243 Browser* browser2 = Browser::Create(browser()->profile());
244 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43245 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18246 browser2->window()->Show();
247 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29248
[email protected]8bcdec92009-02-25 16:15:18249 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00250 BrowserView* browser_view2 =
251 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18252 ASSERT_TRUE(browser_view2);
253 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00254 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18255 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54256 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38257 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29258
259 // Switch to the 1st browser window, focus should still be on the location
260 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18261 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06262 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18263 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29264
265 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18266 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00267 EXPECT_EQ(NULL,
268 views::FocusManager::GetFocusManagerForNativeView(
269 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54270 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38271 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18272
273 // Close the 2nd browser to avoid a DCHECK().
274 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00275#endif
initial.commit09911bf2008-07-26 23:55:29276}
277
278// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39279// Disabled, http://crbug.com/62542.
280IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30281 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11282 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29283
284 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11285 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18286 ui_test_utils::NavigateToURL(browser(), url);
287
initial.commit09911bf2008-07-26 23:55:29288 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33289 for (int i = 0; i < 4; ++i)
290 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29291
292 // Alternate focus for the tab.
293 const bool kFocusPage[3][5] = {
294 { true, true, true, true, false },
295 { false, false, false, false, false },
296 { false, true, false, true, false }
297 };
298
299 for (int i = 1; i < 3; i++) {
300 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18301 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41302 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29303
304 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54305 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00306 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54307 } else {
[email protected]186f13f2009-08-19 20:34:00308 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54309 }
initial.commit09911bf2008-07-26 23:55:29310 }
311
312 // Now come back to the tab and check the right view is focused.
313 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18314 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41315 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29316
[email protected]186f13f2009-08-19 20:34:00317 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
318 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06319 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29320 }
[email protected]cb7e2542009-12-14 22:02:35321
[email protected]eaca0ad12011-04-18 15:53:41322 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35323 // Try the above, but with ctrl+tab. Since tab normally changes focus,
324 // this has regressed in the past. Loop through several times to be sure.
325 for (int j = 0; j < 15; j++) {
326 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
327 VIEW_ID_LOCATION_BAR;
328 ASSERT_TRUE(IsViewFocused(vid));
329
[email protected]1d000682010-08-23 16:21:28330 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09331 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35332 }
333
334 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41335 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35336 for (int j = 14; j >= 0; --j) {
337 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
338 VIEW_ID_LOCATION_BAR;
339 ASSERT_TRUE(IsViewFocused(vid));
340
[email protected]1d000682010-08-23 16:21:28341 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09342 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35343 }
initial.commit09911bf2008-07-26 23:55:29344 }
345}
346
[email protected]ae40b572009-10-02 21:17:45347// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35348IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30349 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11350 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45351
352 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11353 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45354 ui_test_utils::NavigateToURL(browser(), url);
355
356 browser()->Find();
[email protected]9c318862011-02-01 22:27:24357 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45358 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06359 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45360
361 // Focus the location bar.
362 browser()->FocusLocationBar();
363
364 // Create a 2nd tab.
[email protected]616381f02010-11-02 15:15:33365 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
[email protected]ae40b572009-10-02 21:17:45366
367 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06368 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45369
370 // Select 1st tab, focus should still be on the location-bar.
371 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41372 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06373 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45374
375 // Now open the find box again, switch to another tab and come back, the focus
376 // should return to the find box.
377 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06378 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41379 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06380 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41381 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06382 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45383}
384
initial.commit09911bf2008-07-26 23:55:29385// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07386// Flaky, http://crbug.com/62538.
387IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
388 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30389 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11390 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29391
initial.commit09911bf2008-07-26 23:55:29392 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18393 Browser* browser2 = Browser::Create(browser()->profile());
394 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43395 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18396 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00397
[email protected]ed179ee2009-10-03 21:02:51398 Browser* focused_browser = NULL;
399 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16400#if defined(USE_X11)
401 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00402 // to figure out which browser does have focus.
403 if (browser2->window()->IsActive()) {
404 focused_browser = browser2;
405 unfocused_browser = browser();
406 } else if (browser()->window()->IsActive()) {
407 focused_browser = browser();
408 unfocused_browser = browser2;
409 } else {
[email protected]579c3d82010-10-06 03:53:51410 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00411 }
412#elif defined(OS_WIN)
413 focused_browser = browser();
414 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57415#elif defined(OS_MACOSX)
416 // On Mac, the newly created window always gets the focus.
417 focused_browser = browser2;
418 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00419#endif
420
[email protected]95409e12010-08-17 20:07:11421 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00422 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46423
[email protected]8bcdec92009-02-25 16:15:18424 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00425 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29426
[email protected]579c3d82010-10-06 03:53:51427 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
428 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
429 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29430
[email protected]8bcdec92009-02-25 16:15:18431 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00432 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29433}
434
435// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21436IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30437 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11438 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29439
440 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11441 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18442 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29443
[email protected]186f13f2009-08-19 20:34:00444 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29445
[email protected]579c3d82010-10-06 03:53:51446 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
447 browser()->GetSelectedTabContents()->render_view_host(), L"",
448 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29449
450 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06451 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29452}
453
[email protected]9e0c83a2009-05-06 19:44:37454// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35455// Note that this test relies on a notification from the renderer that the
456// focus has changed in the page. The notification in the renderer may change
457// at which point this test would fail (see comment in
458// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57459IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30460 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11461 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29462
[email protected]8bcdec92009-02-25 16:15:18463 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11464 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18465 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29466
[email protected]186f13f2009-08-19 20:34:00467 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29468
[email protected]546ae4e02010-12-08 14:57:19469 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18470 const char* kExpElementIDs[] = {
471 "", // Initially no element in the page should be focused
472 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19473 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18474 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29475 };
476
477 // Test forward focus traversal.
478 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40479 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29480 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06481 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29482
[email protected]911696b2011-01-28 02:36:49483 // Move the caret to the end, otherwise the next Tab key may not move focus.
484 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
485 browser(), ui::VKEY_END, false, false, false, false));
486
initial.commit09911bf2008-07-26 23:55:29487 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35488 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40489 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29490 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01491 std::string actual;
492 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25493 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18494 L"",
[email protected]45671612009-04-29 22:24:01495 L"window.domAutomationController.send(getFocusedElement());",
496 &actual));
initial.commit09911bf2008-07-26 23:55:29497 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
498
[email protected]130efb02009-09-18 18:54:35499 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19500 // If the next element is the kTextElementID, we expect to be
501 // notified we have switched to an editable node.
502 bool is_editable_node =
503 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
504 Details<bool> details(&is_editable_node);
505
506 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09507 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27508 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21509 NotificationSource(Source<TabContents>(
510 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19511 details));
[email protected]130efb02009-09-18 18:54:35512 } else {
513 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19514 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09515 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27516 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]546ae4e02010-12-08 14:57:19517 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35518 }
initial.commit09911bf2008-07-26 23:55:29519 }
[email protected]8bcdec92009-02-25 16:15:18520
521 // At this point the renderer has sent us a message asking to advance the
522 // focus (as the end of the focus loop was reached in the renderer).
523 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42524 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29525 }
526
527 // Now let's try reverse focus traversal.
528 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40529 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29530 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06531 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29532
[email protected]911696b2011-01-28 02:36:49533 // Move the caret to the end, otherwise the next Tab key may not move focus.
534 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
535 browser(), ui::VKEY_END, false, false, false, false));
536
[email protected]8bcdec92009-02-25 16:15:18537 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30538 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40539 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19540 const char* next_element =
541 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35542
[email protected]130efb02009-09-18 18:54:35543 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19544 // If the next element is the kTextElementID, we expect to be
545 // notified we have switched to an editable node.
546 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
547 Details<bool> details(&is_editable_node);
548
549 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09550 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27551 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21552 NotificationSource(Source<TabContents>(
553 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19554 details));
[email protected]130efb02009-09-18 18:54:35555 } else {
556 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19557 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09558 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27559 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]546ae4e02010-12-08 14:57:19560 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35561 }
initial.commit09911bf2008-07-26 23:55:29562
initial.commit09911bf2008-07-26 23:55:29563 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01564 std::string actual;
565 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25566 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18567 L"",
[email protected]45671612009-04-29 22:24:01568 L"window.domAutomationController.send(getFocusedElement());",
569 &actual));
[email protected]546ae4e02010-12-08 14:57:19570 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29571 }
[email protected]8bcdec92009-02-25 16:15:18572
573 // At this point the renderer has sent us a message asking to advance the
574 // focus (as the end of the focus loop was reached in the renderer).
575 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42576 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29577 }
578}
579
[email protected]9e0c83a2009-05-06 19:44:37580// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41581IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30582 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11583 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37584
585 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11586 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37587 ui_test_utils::NavigateToURL(browser(), url);
588
[email protected]9e0c83a2009-05-06 19:44:37589 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06590 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37591
592 // Let's show an interstitial.
593 TestInterstitialPage* interstitial_page =
594 new TestInterstitialPage(browser()->GetSelectedTabContents(),
595 true, GURL("http://interstitial.com"));
596 interstitial_page->Show();
597 // Give some time for the interstitial to show.
598 MessageLoop::current()->PostDelayedTask(FROM_HERE,
599 new MessageLoop::QuitTask(),
600 1000);
601 ui_test_utils::RunMessageLoop();
602
[email protected]fc2e0872009-08-21 22:14:41603 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37604
605 const char* kExpElementIDs[] = {
606 "", // Initially no element in the page should be focused
607 // (the location bar is focused).
608 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
609 "gmapLink"
610 };
611
612 // Test forward focus traversal.
613 for (int i = 0; i < 2; ++i) {
614 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06615 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37616
[email protected]911696b2011-01-28 02:36:49617 // Move the caret to the end, otherwise the next Tab key may not move focus.
618 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
619 browser(), ui::VKEY_END, false, false, false, false));
620
[email protected]9e0c83a2009-05-06 19:44:37621 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35622 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37623 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30624 std::string actual;
625 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
626 interstitial_page->render_view_host(), L"",
627 L"window.domAutomationController.send(getFocusedElement());",
628 &actual));
[email protected]9e0c83a2009-05-06 19:44:37629 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
630
[email protected]432115822011-07-10 15:52:27631 int notification_type;
[email protected]a6e602f2010-09-28 22:28:30632 NotificationSource notification_source =
633 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35634 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27635 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21636 notification_source = Source<TabContents>(
637 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35638 } else {
639 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27640 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]a6e602f2010-09-28 22:28:30641 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35642 }
[email protected]a6e602f2010-09-28 22:28:30643
644 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09645 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30646 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37647 }
648
649 // At this point the renderer has sent us a message asking to advance the
650 // focus (as the end of the focus loop was reached in the renderer).
651 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42652 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37653 }
654
655 // Now let's try reverse focus traversal.
656 for (int i = 0; i < 2; ++i) {
657 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06658 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37659
[email protected]911696b2011-01-28 02:36:49660 // Move the caret to the end, otherwise the next Tab key may not move focus.
661 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
662 browser(), ui::VKEY_END, false, false, false, false));
663
[email protected]9e0c83a2009-05-06 19:44:37664 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35665 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27666 int notification_type;
[email protected]a6e602f2010-09-28 22:28:30667 NotificationSource notification_source =
668 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35669 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27670 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21671 notification_source = Source<TabContents>(
672 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35673 } else {
674 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27675 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]a6e602f2010-09-28 22:28:30676 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35677 }
[email protected]9e0c83a2009-05-06 19:44:37678
[email protected]a6e602f2010-09-28 22:28:30679 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09680 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30681 notification_type, notification_source));
682
[email protected]9e0c83a2009-05-06 19:44:37683 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30684 std::string actual;
685 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
686 interstitial_page->render_view_host(), L"",
687 L"window.domAutomationController.send(getFocusedElement());",
688 &actual));
[email protected]9e0c83a2009-05-06 19:44:37689 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
690 }
691
692 // At this point the renderer has sent us a message asking to advance the
693 // focus (as the end of the focus loop was reached in the renderer).
694 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42695 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37696 }
697}
698
699// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33700// http://crbug.com/81451
701#if defined(OS_MACOSX)
702IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
703#else
[email protected]e4f4e0b2009-10-13 19:58:21704IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33705#endif
[email protected]a6e602f2010-09-28 22:28:30706 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11707 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37708
709 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11710 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37711 ui_test_utils::NavigateToURL(browser(), url);
712
[email protected]9e0c83a2009-05-06 19:44:37713 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06714 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37715 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
716 HasFocus());
717
[email protected]9d8a4642009-07-29 17:25:30718 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37719 TestInterstitialPage* interstitial_page =
720 new TestInterstitialPage(browser()->GetSelectedTabContents(),
721 true, GURL("http://interstitial.com"));
722 interstitial_page->Show();
723 // Give some time for the interstitial to show.
724 MessageLoop::current()->PostDelayedTask(FROM_HERE,
725 new MessageLoop::QuitTask(),
726 1000);
727 ui_test_utils::RunMessageLoop();
728
729 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06730 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37731 EXPECT_TRUE(interstitial_page->HasFocus());
732
733 // Hide the interstitial.
734 interstitial_page->DontProceed();
735
736 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06737 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37738}
739
[email protected]9bd491ee2008-12-10 22:31:07740// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20741// Flaky on mac and valgrind. http://crbug.com/67301.
742#if defined(OS_MACOSX)
743#define MAYBE_FindFocusTest FLAKY_FindFocusTest
744#else
745#define MAYBE_FindFocusTest FindFocusTest
746#endif
[email protected]a711df102010-12-14 09:47:03747IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30748 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11749 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55750
[email protected]9bd491ee2008-12-10 22:31:07751 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11752 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18753 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07754
[email protected]a711df102010-12-14 09:47:03755 EXPECT_TRUE(ChromeInForeground());
756
[email protected]853300a82010-07-27 21:17:57757#if defined(OS_MACOSX)
758 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28759 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09760 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57761#else
[email protected]9bd491ee2008-12-10 22:31:07762 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28763 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09764 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57765#endif
[email protected]8bcdec92009-02-25 16:15:18766
767 // Ideally, we wouldn't sleep here and instead would intercept the
768 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
769 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
770 // - creating a factory used to create the delegate
771 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41772 MessageLoop::current()->PostDelayedTask(
773 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18774 ui_test_utils::RunMessageLoop();
775
[email protected]21abcc742009-10-23 02:52:06776 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07777
[email protected]fc2e0872009-08-21 22:14:41778 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06779 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07780
781 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57782#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28783 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09784 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57785#else
[email protected]1d000682010-08-23 16:21:28786 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09787 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57788#endif
[email protected]21abcc742009-10-23 02:52:06789 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07790
791 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41792 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06793 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07794
795 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57796#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28797 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09798 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57799#else
[email protected]1d000682010-08-23 16:21:28800 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09801 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57802#endif
[email protected]8bcdec92009-02-25 16:15:18803
804 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41805 MessageLoop::current()->PostDelayedTask(
806 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18807 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06808 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07809}
[email protected]401513c2009-03-12 00:21:28810
811// Makes sure the focus is in the right location when opening the different
812// types of tabs.
[email protected]c92928672010-11-09 18:31:07813// Flaky, http://crbug.com/62539.
814IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30815 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07816
[email protected]401513c2009-03-12 00:21:28817 // Open the history tab, focus should be on the tab contents.
818 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42819 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54820 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42821 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28822
823 // Open the new tab, focus should be on the location bar.
824 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42825 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54826 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42827 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28828
829 // Open the download tab, focus should be on the tab contents.
830 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42831 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54832 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42833 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43834
835 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55836 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
837 PageTransition::LINK);
[email protected]a3f343f2010-10-06 23:39:42838 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54839 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42840 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28841}
[email protected]9d8a4642009-07-29 17:25:30842
843// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21844IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30845 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11846 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30847
[email protected]9d8a4642009-07-29 17:25:30848 // Open the new tab, reload.
849 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42850 ui_test_utils::RunAllPendingInMessageLoop();
851
[email protected]cb84d642010-06-10 00:56:28852 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40853 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30854 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06855 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30856
857 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11858 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41859 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06860 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28861 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40862 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07863
[email protected]9d8a4642009-07-29 17:25:30864 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41865 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06866 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30867}
868
869// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27870IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30871 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11872 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30873
[email protected]9d8a4642009-07-29 17:25:30874 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11875 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30876 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28877 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40878 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07879
[email protected]9d8a4642009-07-29 17:25:30880 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41881 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06882 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30883}
[email protected]629e0342010-07-27 23:30:13884
885} // namespace