blob: 32df6856294ba13883e0016a17b99944a1306fb2 [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]a711df102010-12-14 09:47:0311#include "base/string_number_conversions.h"
[email protected]34877b32010-07-31 17:47:0512#include "base/string_util.h"
[email protected]be1ce6a72010-08-03 14:35:2213#include "base/utf_string_conversions.h"
[email protected]134c47b92009-08-19 03:33:4414#include "chrome/browser/browser_window.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]6a3ec2312010-12-02 19:30:1917#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3718#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5519#include "chrome/common/url_constants.h"
[email protected]8bcdec92009-02-25 16:15:1820#include "chrome/test/in_process_browser_test.h"
21#include "chrome/test/ui_test_utils.h"
[email protected]5de634712011-03-02 00:20:1922#include "content/browser/renderer_host/render_view_host.h"
23#include "content/browser/renderer_host/render_widget_host_view.h"
24#include "content/browser/tab_contents/interstitial_page.h"
25#include "content/browser/tab_contents/tab_contents.h"
26#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]3985ba82010-07-29 21:44:1227#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5728
29#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0530#include "views/focus/focus_manager.h"
31#include "views/view.h"
32#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5733#endif
initial.commit09911bf2008-07-26 23:55:2934
[email protected]134c47b92009-08-19 03:33:4435#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5436#include "chrome/browser/ui/views/frame/browser_view.h"
37#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
38#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4439#endif
40
[email protected]753efc42010-03-09 19:52:1641#if defined(TOOLKIT_USES_GTK)
[email protected]93270d002011-01-19 22:32:5942#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1743#endif
44
[email protected]a711df102010-12-14 09:47:0345#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0346#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5947#include <windows.h>
[email protected]a711df102010-12-14 09:47:0348#endif
49
[email protected]fc2e0872009-08-21 22:14:4150#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5751#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1852#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3553// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5954#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5755#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1556// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4857#define MAYBE_FocusTraversal DISABLED_FocusTraversal
58#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]5e3c1f52011-04-08 16:32:5959// Fails on MacOS X 10.6, http://crbug.com/78825
60#define ClickingMovesFocus FAILS_ClickingMovesFocus
61#define FindFocusTest FAILS_FindFocusTest
[email protected]853300a82010-07-27 21:17:5762// TODO(suzhe): http://crbug.com/49737
63#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
64#elif defined(OS_WIN)
[email protected]26153732010-11-09 18:47:3965// Disabled, http://crbug.com/62543.
66#define MAYBE_FocusTraversal DISABLED_FocusTraversal
67// Disabled, http://crbug.com/62544.
68#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]c92928672010-11-09 18:31:0769// Flaky, http://crbug.com/62537.
70#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4171#endif
72
initial.commit09911bf2008-07-26 23:55:2973namespace {
74
[email protected]8bcdec92009-02-25 16:15:1875// The delay waited in some cases where we don't have a notifications for an
76// action we take.
initial.commit09911bf2008-07-26 23:55:2977const int kActionDelayMs = 500;
78
[email protected]f72a1cc2010-04-30 07:17:3079const char kSimplePage[] = "files/focus/page_with_focus.html";
80const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
81const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3182const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2983
[email protected]a711df102010-12-14 09:47:0384// Test to make sure Chrome is in the foreground as we start testing. This is
85// required for tests that synthesize input to the Chrome window.
86bool ChromeInForeground() {
87#if defined(OS_WIN)
88 HWND window = ::GetForegroundWindow();
89 std::wstring caption;
90 std::wstring filename;
91 int len = ::GetWindowTextLength(window) + 1;
92 ::GetWindowText(window, WriteInto(&caption, len), len);
93 bool chrome_window_in_foreground =
94 EndsWith(caption, L" - Google Chrome", true) ||
95 EndsWith(caption, L" - Chromium", true);
96 if (!chrome_window_in_foreground) {
97 DWORD process_id;
98 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
99
100 base::ProcessHandle process;
101 if (base::OpenProcessHandleWithAccess(process_id,
102 PROCESS_QUERY_LIMITED_INFORMATION,
103 &process)) {
104 len = MAX_PATH;
105 if (!GetProcessImageFileName(process, WriteInto(&filename, len), len)) {
106 int error = GetLastError();
107 filename = std::wstring(L"Unable to read filename for process id '" +
108 base::IntToString16(process_id) +
109 L"' (error ") +
110 base::IntToString16(error) + L")";
111 }
112 base::CloseProcessHandle(process);
113 }
114 }
115 EXPECT_TRUE(chrome_window_in_foreground)
116 << "Chrome must be in the foreground when running interactive tests\n"
117 << "Process in foreground: " << filename.c_str() << "\n"
118 << "Window: " << window << "\n"
119 << "Caption: " << caption.c_str();
120 return chrome_window_in_foreground;
121#else
122 // Windows only at the moment.
123 return true;
124#endif
125}
126
[email protected]8bcdec92009-02-25 16:15:18127class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29128 public:
129 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:18130 set_show_window(true);
131 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29132 }
[email protected]b9821882009-08-17 22:25:17133
[email protected]21abcc742009-10-23 02:52:06134 bool IsViewFocused(ViewID vid) {
135 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17136 }
137
[email protected]fc2e0872009-08-21 22:14:41138 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06139 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41140 }
initial.commit09911bf2008-07-26 23:55:29141};
142
[email protected]9e0c83a2009-05-06 19:44:37143class TestInterstitialPage : public InterstitialPage {
144 public:
145 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30146 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31147 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37148 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
149 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31150 file_path = file_path.AppendASCII("focus");
151 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37152 r = file_util::ReadFileToString(file_path, &html_contents_);
153 EXPECT_TRUE(r);
154 }
155
156 virtual std::string GetHTMLContents() {
157 return html_contents_;
158 }
159
[email protected]4e85c112011-01-26 22:27:21160 // Exposing render_view_host() and tab() to be public; they are declared as
161 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30162 virtual RenderViewHost* render_view_host() {
163 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37164 }
165
[email protected]4e85c112011-01-26 22:27:21166 virtual TabContents* tab() {
167 return InterstitialPage::tab();
168 }
169
[email protected]9e0c83a2009-05-06 19:44:37170 bool HasFocus() {
171 return render_view_host()->view()->HasFocus();
172 }
173
[email protected]130efb02009-09-18 18:54:35174 protected:
[email protected]546ae4e02010-12-08 14:57:19175 virtual void FocusedNodeChanged(bool is_editable_node) {
[email protected]a6e602f2010-09-28 22:28:30176 NotificationService::current()->Notify(
177 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21178 Source<TabContents>(tab()),
[email protected]546ae4e02010-12-08 14:57:19179 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:35180 }
181
[email protected]9e0c83a2009-05-06 19:44:37182 private:
183 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37184};
[email protected]b9821882009-08-17 22:25:17185
[email protected]e4f4e0b2009-10-13 19:58:21186IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30187 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42188#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41189 // It seems we have to wait a little bit for the widgets to spin up before
190 // we can start clicking on them.
191 MessageLoop::current()->PostDelayedTask(FROM_HERE,
192 new MessageLoop::QuitTask(),
193 kActionDelayMs);
194 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42195#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41196
[email protected]21abcc742009-10-23 02:52:06197 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00198
[email protected]fc2e0872009-08-21 22:14:41199 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06200 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00201
[email protected]fc2e0872009-08-21 22:14:41202 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06203 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00204}
[email protected]186f13f2009-08-19 20:34:00205
[email protected]320948e02011-01-10 08:21:12206// Flaky, http://crbug.com/69034.
207IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30208 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11209 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29210
211 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11212 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18213 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29214
[email protected]186f13f2009-08-19 20:34:00215 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
216
initial.commit09911bf2008-07-26 23:55:29217 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06218 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29219 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57220 ui_test_utils::HideNativeWindow(window);
221 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06222 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29223
[email protected]186f13f2009-08-19 20:34:00224 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06225 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29226 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57227 ui_test_utils::HideNativeWindow(window);
228 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06229 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29230
[email protected]186f13f2009-08-19 20:34:00231 // The rest of this test does not make sense on Linux because the behavior
232 // of Activate() is not well defined and can vary by window manager.
233#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29234 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18235 Browser* browser2 = Browser::Create(browser()->profile());
236 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43237 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18238 browser2->window()->Show();
239 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29240
[email protected]8bcdec92009-02-25 16:15:18241 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00242 BrowserView* browser_view2 =
243 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18244 ASSERT_TRUE(browser_view2);
245 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00246 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18247 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54248 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38249 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29250
251 // Switch to the 1st browser window, focus should still be on the location
252 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18253 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06254 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18255 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29256
257 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18258 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00259 EXPECT_EQ(NULL,
260 views::FocusManager::GetFocusManagerForNativeView(
261 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54262 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38263 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18264
265 // Close the 2nd browser to avoid a DCHECK().
266 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00267#endif
initial.commit09911bf2008-07-26 23:55:29268}
269
270// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39271// Disabled, http://crbug.com/62542.
272IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30273 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11274 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29275
276 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11277 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18278 ui_test_utils::NavigateToURL(browser(), url);
279
initial.commit09911bf2008-07-26 23:55:29280 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33281 for (int i = 0; i < 4; ++i)
282 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29283
284 // Alternate focus for the tab.
285 const bool kFocusPage[3][5] = {
286 { true, true, true, true, false },
287 { false, false, false, false, false },
288 { false, true, false, true, false }
289 };
290
291 for (int i = 1; i < 3; i++) {
292 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18293 // Activate the tab.
294 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29295
296 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54297 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00298 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54299 } else {
[email protected]186f13f2009-08-19 20:34:00300 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54301 }
initial.commit09911bf2008-07-26 23:55:29302 }
303
304 // Now come back to the tab and check the right view is focused.
305 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18306 // Activate the tab.
307 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29308
[email protected]186f13f2009-08-19 20:34:00309 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
310 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06311 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29312 }
[email protected]cb7e2542009-12-14 22:02:35313
[email protected]cb7e2542009-12-14 22:02:35314 browser()->SelectTabContentsAt(0, true);
315 // Try the above, but with ctrl+tab. Since tab normally changes focus,
316 // this has regressed in the past. Loop through several times to be sure.
317 for (int j = 0; j < 15; j++) {
318 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
319 VIEW_ID_LOCATION_BAR;
320 ASSERT_TRUE(IsViewFocused(vid));
321
[email protected]1d000682010-08-23 16:21:28322 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09323 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35324 }
325
326 // As above, but with ctrl+shift+tab.
327 browser()->SelectTabContentsAt(4, true);
328 for (int j = 14; j >= 0; --j) {
329 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
330 VIEW_ID_LOCATION_BAR;
331 ASSERT_TRUE(IsViewFocused(vid));
332
[email protected]1d000682010-08-23 16:21:28333 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09334 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35335 }
initial.commit09911bf2008-07-26 23:55:29336 }
337}
338
[email protected]ae40b572009-10-02 21:17:45339// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35340IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30341 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11342 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45343
344 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11345 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45346 ui_test_utils::NavigateToURL(browser(), url);
347
348 browser()->Find();
[email protected]9c318862011-02-01 22:27:24349 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45350 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06351 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45352
353 // Focus the location bar.
354 browser()->FocusLocationBar();
355
356 // Create a 2nd tab.
[email protected]616381f02010-11-02 15:15:33357 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
[email protected]ae40b572009-10-02 21:17:45358
359 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06360 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45361
362 // Select 1st tab, focus should still be on the location-bar.
363 // (bug http://crbug.com/23296)
364 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06365 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45366
367 // Now open the find box again, switch to another tab and come back, the focus
368 // should return to the find box.
369 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06370 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45371 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06372 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45373 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06374 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45375}
376
initial.commit09911bf2008-07-26 23:55:29377// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07378// Flaky, http://crbug.com/62538.
379IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
380 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30381 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11382 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29383
initial.commit09911bf2008-07-26 23:55:29384 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18385 Browser* browser2 = Browser::Create(browser()->profile());
386 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43387 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18388 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00389
[email protected]ed179ee2009-10-03 21:02:51390 Browser* focused_browser = NULL;
391 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16392#if defined(USE_X11)
393 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00394 // to figure out which browser does have focus.
395 if (browser2->window()->IsActive()) {
396 focused_browser = browser2;
397 unfocused_browser = browser();
398 } else if (browser()->window()->IsActive()) {
399 focused_browser = browser();
400 unfocused_browser = browser2;
401 } else {
[email protected]579c3d82010-10-06 03:53:51402 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00403 }
404#elif defined(OS_WIN)
405 focused_browser = browser();
406 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57407#elif defined(OS_MACOSX)
408 // On Mac, the newly created window always gets the focus.
409 focused_browser = browser2;
410 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00411#endif
412
[email protected]95409e12010-08-17 20:07:11413 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00414 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46415
[email protected]8bcdec92009-02-25 16:15:18416 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00417 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29418
[email protected]579c3d82010-10-06 03:53:51419 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
420 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
421 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29422
[email protected]8bcdec92009-02-25 16:15:18423 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00424 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29425}
426
427// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21428IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30429 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11430 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29431
432 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11433 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18434 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29435
[email protected]186f13f2009-08-19 20:34:00436 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29437
[email protected]579c3d82010-10-06 03:53:51438 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
439 browser()->GetSelectedTabContents()->render_view_host(), L"",
440 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29441
442 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06443 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29444}
445
[email protected]9e0c83a2009-05-06 19:44:37446// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35447// Note that this test relies on a notification from the renderer that the
448// focus has changed in the page. The notification in the renderer may change
449// at which point this test would fail (see comment in
450// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57451IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30452 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11453 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29454
[email protected]8bcdec92009-02-25 16:15:18455 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11456 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18457 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29458
[email protected]186f13f2009-08-19 20:34:00459 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29460
[email protected]546ae4e02010-12-08 14:57:19461 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18462 const char* kExpElementIDs[] = {
463 "", // Initially no element in the page should be focused
464 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19465 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18466 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29467 };
468
469 // Test forward focus traversal.
470 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42471 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29472 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06473 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29474
[email protected]911696b2011-01-28 02:36:49475 // Move the caret to the end, otherwise the next Tab key may not move focus.
476 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
477 browser(), ui::VKEY_END, false, false, false, false));
478
initial.commit09911bf2008-07-26 23:55:29479 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35480 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42481 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29482 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01483 std::string actual;
484 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25485 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18486 L"",
[email protected]45671612009-04-29 22:24:01487 L"window.domAutomationController.send(getFocusedElement());",
488 &actual));
initial.commit09911bf2008-07-26 23:55:29489 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
490
[email protected]130efb02009-09-18 18:54:35491 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19492 // If the next element is the kTextElementID, we expect to be
493 // notified we have switched to an editable node.
494 bool is_editable_node =
495 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
496 Details<bool> details(&is_editable_node);
497
498 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09499 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]546ae4e02010-12-08 14:57:19500 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21501 NotificationSource(Source<TabContents>(
502 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19503 details));
[email protected]130efb02009-09-18 18:54:35504 } else {
505 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19506 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09507 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]546ae4e02010-12-08 14:57:19508 NotificationType::FOCUS_RETURNED_TO_BROWSER,
509 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35510 }
initial.commit09911bf2008-07-26 23:55:29511 }
[email protected]8bcdec92009-02-25 16:15:18512
513 // At this point the renderer has sent us a message asking to advance the
514 // focus (as the end of the focus loop was reached in the renderer).
515 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42516 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29517 }
518
519 // Now let's try reverse focus traversal.
520 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42521 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29522 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06523 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29524
[email protected]911696b2011-01-28 02:36:49525 // Move the caret to the end, otherwise the next Tab key may not move focus.
526 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
527 browser(), ui::VKEY_END, false, false, false, false));
528
[email protected]8bcdec92009-02-25 16:15:18529 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30530 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42531 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19532 const char* next_element =
533 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35534
[email protected]130efb02009-09-18 18:54:35535 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19536 // If the next element is the kTextElementID, we expect to be
537 // notified we have switched to an editable node.
538 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
539 Details<bool> details(&is_editable_node);
540
541 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09542 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]546ae4e02010-12-08 14:57:19543 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21544 NotificationSource(Source<TabContents>(
545 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19546 details));
[email protected]130efb02009-09-18 18:54:35547 } else {
548 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19549 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09550 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]546ae4e02010-12-08 14:57:19551 NotificationType::FOCUS_RETURNED_TO_BROWSER,
552 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35553 }
initial.commit09911bf2008-07-26 23:55:29554
initial.commit09911bf2008-07-26 23:55:29555 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01556 std::string actual;
557 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25558 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18559 L"",
[email protected]45671612009-04-29 22:24:01560 L"window.domAutomationController.send(getFocusedElement());",
561 &actual));
[email protected]546ae4e02010-12-08 14:57:19562 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29563 }
[email protected]8bcdec92009-02-25 16:15:18564
565 // At this point the renderer has sent us a message asking to advance the
566 // focus (as the end of the focus loop was reached in the renderer).
567 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42568 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29569 }
570}
571
[email protected]9e0c83a2009-05-06 19:44:37572// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41573IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30574 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11575 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37576
577 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11578 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37579 ui_test_utils::NavigateToURL(browser(), url);
580
[email protected]9e0c83a2009-05-06 19:44:37581 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06582 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37583
584 // Let's show an interstitial.
585 TestInterstitialPage* interstitial_page =
586 new TestInterstitialPage(browser()->GetSelectedTabContents(),
587 true, GURL("http://interstitial.com"));
588 interstitial_page->Show();
589 // Give some time for the interstitial to show.
590 MessageLoop::current()->PostDelayedTask(FROM_HERE,
591 new MessageLoop::QuitTask(),
592 1000);
593 ui_test_utils::RunMessageLoop();
594
[email protected]fc2e0872009-08-21 22:14:41595 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37596
597 const char* kExpElementIDs[] = {
598 "", // Initially no element in the page should be focused
599 // (the location bar is focused).
600 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
601 "gmapLink"
602 };
603
604 // Test forward focus traversal.
605 for (int i = 0; i < 2; ++i) {
606 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06607 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37608
[email protected]911696b2011-01-28 02:36:49609 // Move the caret to the end, otherwise the next Tab key may not move focus.
610 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
611 browser(), ui::VKEY_END, false, false, false, false));
612
[email protected]9e0c83a2009-05-06 19:44:37613 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35614 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37615 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30616 std::string actual;
617 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
618 interstitial_page->render_view_host(), L"",
619 L"window.domAutomationController.send(getFocusedElement());",
620 &actual));
[email protected]9e0c83a2009-05-06 19:44:37621 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
622
[email protected]a6e602f2010-09-28 22:28:30623 NotificationType::Type notification_type;
624 NotificationSource notification_source =
625 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35626 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30627 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21628 notification_source = Source<TabContents>(
629 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35630 } else {
631 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30632 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
633 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35634 }
[email protected]a6e602f2010-09-28 22:28:30635
636 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09637 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30638 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37639 }
640
641 // At this point the renderer has sent us a message asking to advance the
642 // focus (as the end of the focus loop was reached in the renderer).
643 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42644 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37645 }
646
647 // Now let's try reverse focus traversal.
648 for (int i = 0; i < 2; ++i) {
649 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06650 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37651
[email protected]911696b2011-01-28 02:36:49652 // Move the caret to the end, otherwise the next Tab key may not move focus.
653 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
654 browser(), ui::VKEY_END, false, false, false, false));
655
[email protected]9e0c83a2009-05-06 19:44:37656 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35657 for (size_t j = 0; j < 7; ++j) {
[email protected]a6e602f2010-09-28 22:28:30658 NotificationType::Type notification_type;
659 NotificationSource notification_source =
660 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35661 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30662 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21663 notification_source = Source<TabContents>(
664 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35665 } else {
666 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30667 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
668 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35669 }
[email protected]9e0c83a2009-05-06 19:44:37670
[email protected]a6e602f2010-09-28 22:28:30671 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09672 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30673 notification_type, notification_source));
674
[email protected]9e0c83a2009-05-06 19:44:37675 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30676 std::string actual;
677 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
678 interstitial_page->render_view_host(), L"",
679 L"window.domAutomationController.send(getFocusedElement());",
680 &actual));
[email protected]9e0c83a2009-05-06 19:44:37681 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
682 }
683
684 // At this point the renderer has sent us a message asking to advance the
685 // focus (as the end of the focus loop was reached in the renderer).
686 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42687 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37688 }
689}
690
691// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21692IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30693 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11694 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37695
696 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11697 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37698 ui_test_utils::NavigateToURL(browser(), url);
699
[email protected]9e0c83a2009-05-06 19:44:37700 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06701 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37702 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
703 HasFocus());
704
[email protected]9d8a4642009-07-29 17:25:30705 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37706 TestInterstitialPage* interstitial_page =
707 new TestInterstitialPage(browser()->GetSelectedTabContents(),
708 true, GURL("http://interstitial.com"));
709 interstitial_page->Show();
710 // Give some time for the interstitial to show.
711 MessageLoop::current()->PostDelayedTask(FROM_HERE,
712 new MessageLoop::QuitTask(),
713 1000);
714 ui_test_utils::RunMessageLoop();
715
716 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06717 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37718 EXPECT_TRUE(interstitial_page->HasFocus());
719
720 // Hide the interstitial.
721 interstitial_page->DontProceed();
722
723 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06724 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37725}
726
[email protected]9bd491ee2008-12-10 22:31:07727// Make sure Find box can request focus, even when it is already open.
[email protected]a711df102010-12-14 09:47:03728IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30729 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11730 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55731
[email protected]9bd491ee2008-12-10 22:31:07732 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11733 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18734 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07735
[email protected]a711df102010-12-14 09:47:03736 EXPECT_TRUE(ChromeInForeground());
737
[email protected]853300a82010-07-27 21:17:57738#if defined(OS_MACOSX)
739 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28740 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09741 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57742#else
[email protected]9bd491ee2008-12-10 22:31:07743 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28744 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09745 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57746#endif
[email protected]8bcdec92009-02-25 16:15:18747
748 // Ideally, we wouldn't sleep here and instead would intercept the
749 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
750 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
751 // - creating a factory used to create the delegate
752 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41753 MessageLoop::current()->PostDelayedTask(
754 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18755 ui_test_utils::RunMessageLoop();
756
[email protected]21abcc742009-10-23 02:52:06757 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07758
[email protected]fc2e0872009-08-21 22:14:41759 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06760 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07761
762 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57763#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28764 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09765 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57766#else
[email protected]1d000682010-08-23 16:21:28767 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09768 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57769#endif
[email protected]21abcc742009-10-23 02:52:06770 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07771
772 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41773 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06774 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07775
776 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57777#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28778 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09779 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57780#else
[email protected]1d000682010-08-23 16:21:28781 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09782 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57783#endif
[email protected]8bcdec92009-02-25 16:15:18784
785 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41786 MessageLoop::current()->PostDelayedTask(
787 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18788 ui_test_utils::RunMessageLoop();
[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}
[email protected]401513c2009-03-12 00:21:28791
792// Makes sure the focus is in the right location when opening the different
793// types of tabs.
[email protected]c92928672010-11-09 18:31:07794// Flaky, http://crbug.com/62539.
795IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30796 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07797
[email protected]401513c2009-03-12 00:21:28798 // Open the history tab, focus should be on the tab contents.
799 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42800 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54801 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42802 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28803
804 // Open the new tab, focus should be on the location bar.
805 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42806 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54807 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42808 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28809
810 // Open the download tab, focus should be on the tab contents.
811 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42812 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54813 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42814 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43815
816 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55817 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
818 PageTransition::LINK);
[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_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28822}
[email protected]9d8a4642009-07-29 17:25:30823
824// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21825IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30826 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11827 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30828
[email protected]9d8a4642009-07-29 17:25:30829 // Open the new tab, reload.
830 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42831 ui_test_utils::RunAllPendingInMessageLoop();
832
[email protected]cb84d642010-06-10 00:56:28833 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40834 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30835 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06836 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30837
838 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11839 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41840 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06841 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28842 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40843 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07844
[email protected]9d8a4642009-07-29 17:25:30845 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41846 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06847 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30848}
849
850// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27851IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30852 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11853 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30854
[email protected]9d8a4642009-07-29 17:25:30855 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11856 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30857 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28858 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40859 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07860
[email protected]9d8a4642009-07-29 17:25:30861 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41862 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06863 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30864}
[email protected]629e0342010-07-27 23:30:13865
866} // namespace