blob: 2396dc6df3eb87155e37501253cba97fe549f9e5 [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"
15#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3716#include "chrome/browser/renderer_host/render_widget_host_view.h"
17#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0018#include "chrome/browser/tab_contents/tab_contents.h"
19#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]c848d3d92010-09-16 21:57:4520#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1021#include "chrome/browser/ui/browser.h"
[email protected]6a3ec2312010-12-02 19:30:1922#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3723#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5524#include "chrome/common/url_constants.h"
[email protected]8bcdec92009-02-25 16:15:1825#include "chrome/test/in_process_browser_test.h"
26#include "chrome/test/ui_test_utils.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]b9821882009-08-17 22:25:1742#include "chrome/browser/gtk/view_id_util.h"
43#endif
44
[email protected]a711df102010-12-14 09:47:0345#if defined(OS_WIN)
46#include <windows.h>
47#include <Psapi.h>
48#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]853300a82010-07-27 21:17:5759// TODO(suzhe): http://crbug.com/49737
60#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
61#elif defined(OS_WIN)
[email protected]26153732010-11-09 18:47:3962// Disabled, http://crbug.com/62543.
63#define MAYBE_FocusTraversal DISABLED_FocusTraversal
64// Disabled, http://crbug.com/62544.
65#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]c92928672010-11-09 18:31:0766// Flaky, http://crbug.com/62537.
67#define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4168#endif
69
initial.commit09911bf2008-07-26 23:55:2970namespace {
71
[email protected]8bcdec92009-02-25 16:15:1872// The delay waited in some cases where we don't have a notifications for an
73// action we take.
initial.commit09911bf2008-07-26 23:55:2974const int kActionDelayMs = 500;
75
[email protected]f72a1cc2010-04-30 07:17:3076const char kSimplePage[] = "files/focus/page_with_focus.html";
77const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
78const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3179const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2980
[email protected]a711df102010-12-14 09:47:0381// Test to make sure Chrome is in the foreground as we start testing. This is
82// required for tests that synthesize input to the Chrome window.
83bool ChromeInForeground() {
84#if defined(OS_WIN)
85 HWND window = ::GetForegroundWindow();
86 std::wstring caption;
87 std::wstring filename;
88 int len = ::GetWindowTextLength(window) + 1;
89 ::GetWindowText(window, WriteInto(&caption, len), len);
90 bool chrome_window_in_foreground =
91 EndsWith(caption, L" - Google Chrome", true) ||
92 EndsWith(caption, L" - Chromium", true);
93 if (!chrome_window_in_foreground) {
94 DWORD process_id;
95 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
96
97 base::ProcessHandle process;
98 if (base::OpenProcessHandleWithAccess(process_id,
99 PROCESS_QUERY_LIMITED_INFORMATION,
100 &process)) {
101 len = MAX_PATH;
102 if (!GetProcessImageFileName(process, WriteInto(&filename, len), len)) {
103 int error = GetLastError();
104 filename = std::wstring(L"Unable to read filename for process id '" +
105 base::IntToString16(process_id) +
106 L"' (error ") +
107 base::IntToString16(error) + L")";
108 }
109 base::CloseProcessHandle(process);
110 }
111 }
112 EXPECT_TRUE(chrome_window_in_foreground)
113 << "Chrome must be in the foreground when running interactive tests\n"
114 << "Process in foreground: " << filename.c_str() << "\n"
115 << "Window: " << window << "\n"
116 << "Caption: " << caption.c_str();
117 return chrome_window_in_foreground;
118#else
119 // Windows only at the moment.
120 return true;
121#endif
122}
123
[email protected]8bcdec92009-02-25 16:15:18124class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29125 public:
126 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:18127 set_show_window(true);
128 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:29129 }
[email protected]b9821882009-08-17 22:25:17130
[email protected]21abcc742009-10-23 02:52:06131 bool IsViewFocused(ViewID vid) {
132 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17133 }
134
[email protected]fc2e0872009-08-21 22:14:41135 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06136 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41137 }
initial.commit09911bf2008-07-26 23:55:29138};
139
[email protected]9e0c83a2009-05-06 19:44:37140class TestInterstitialPage : public InterstitialPage {
141 public:
142 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:30143 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:31144 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37145 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
146 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31147 file_path = file_path.AppendASCII("focus");
148 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37149 r = file_util::ReadFileToString(file_path, &html_contents_);
150 EXPECT_TRUE(r);
151 }
152
153 virtual std::string GetHTMLContents() {
154 return html_contents_;
155 }
156
[email protected]a6e602f2010-09-28 22:28:30157 // Exposing render_view_host() to be public; it is declared as protected in
158 // the superclass.
159 virtual RenderViewHost* render_view_host() {
160 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37161 }
162
163 bool HasFocus() {
164 return render_view_host()->view()->HasFocus();
165 }
166
[email protected]130efb02009-09-18 18:54:35167 protected:
[email protected]546ae4e02010-12-08 14:57:19168 virtual void FocusedNodeChanged(bool is_editable_node) {
[email protected]a6e602f2010-09-28 22:28:30169 NotificationService::current()->Notify(
170 NotificationType::FOCUS_CHANGED_IN_PAGE,
171 Source<RenderViewHost>(render_view_host()),
[email protected]546ae4e02010-12-08 14:57:19172 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:35173 }
174
[email protected]9e0c83a2009-05-06 19:44:37175 private:
176 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37177};
[email protected]b9821882009-08-17 22:25:17178
[email protected]e4f4e0b2009-10-13 19:58:21179IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30180 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42181#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41182 // It seems we have to wait a little bit for the widgets to spin up before
183 // we can start clicking on them.
184 MessageLoop::current()->PostDelayedTask(FROM_HERE,
185 new MessageLoop::QuitTask(),
186 kActionDelayMs);
187 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42188#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41189
[email protected]21abcc742009-10-23 02:52:06190 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00191
[email protected]fc2e0872009-08-21 22:14:41192 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06193 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00194
[email protected]fc2e0872009-08-21 22:14:41195 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06196 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00197}
[email protected]186f13f2009-08-19 20:34:00198
[email protected]320948e02011-01-10 08:21:12199// Flaky, http://crbug.com/69034.
200IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30201 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11202 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29203
204 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11205 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18206 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29207
[email protected]186f13f2009-08-19 20:34:00208 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
209
initial.commit09911bf2008-07-26 23:55:29210 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06211 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29212 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57213 ui_test_utils::HideNativeWindow(window);
214 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06215 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29216
[email protected]186f13f2009-08-19 20:34:00217 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06218 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29219 // 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_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29223
[email protected]186f13f2009-08-19 20:34:00224 // The rest of this test does not make sense on Linux because the behavior
225 // of Activate() is not well defined and can vary by window manager.
226#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29227 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18228 Browser* browser2 = Browser::Create(browser()->profile());
229 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43230 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18231 browser2->window()->Show();
232 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29233
[email protected]8bcdec92009-02-25 16:15:18234 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00235 BrowserView* browser_view2 =
236 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18237 ASSERT_TRUE(browser_view2);
238 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00239 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18240 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54241 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38242 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29243
244 // Switch to the 1st browser window, focus should still be on the location
245 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18246 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06247 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18248 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29249
250 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18251 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00252 EXPECT_EQ(NULL,
253 views::FocusManager::GetFocusManagerForNativeView(
254 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54255 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38256 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18257
258 // Close the 2nd browser to avoid a DCHECK().
259 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00260#endif
initial.commit09911bf2008-07-26 23:55:29261}
262
263// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39264// Disabled, http://crbug.com/62542.
265IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30266 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11267 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29268
269 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11270 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18271 ui_test_utils::NavigateToURL(browser(), url);
272
initial.commit09911bf2008-07-26 23:55:29273 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33274 for (int i = 0; i < 4; ++i)
275 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29276
277 // Alternate focus for the tab.
278 const bool kFocusPage[3][5] = {
279 { true, true, true, true, false },
280 { false, false, false, false, false },
281 { false, true, false, true, false }
282 };
283
284 for (int i = 1; i < 3; i++) {
285 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18286 // Activate the tab.
287 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29288
289 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54290 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00291 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54292 } else {
[email protected]186f13f2009-08-19 20:34:00293 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54294 }
initial.commit09911bf2008-07-26 23:55:29295 }
296
297 // Now come back to the tab and check the right view is focused.
298 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18299 // Activate the tab.
300 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29301
[email protected]186f13f2009-08-19 20:34:00302 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
303 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06304 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29305 }
[email protected]cb7e2542009-12-14 22:02:35306
[email protected]cb7e2542009-12-14 22:02:35307 browser()->SelectTabContentsAt(0, true);
308 // Try the above, but with ctrl+tab. Since tab normally changes focus,
309 // this has regressed in the past. Loop through several times to be sure.
310 for (int j = 0; j < 15; j++) {
311 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
312 VIEW_ID_LOCATION_BAR;
313 ASSERT_TRUE(IsViewFocused(vid));
314
[email protected]1d000682010-08-23 16:21:28315 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09316 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35317 }
318
319 // As above, but with ctrl+shift+tab.
320 browser()->SelectTabContentsAt(4, true);
321 for (int j = 14; j >= 0; --j) {
322 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
323 VIEW_ID_LOCATION_BAR;
324 ASSERT_TRUE(IsViewFocused(vid));
325
[email protected]1d000682010-08-23 16:21:28326 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09327 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35328 }
initial.commit09911bf2008-07-26 23:55:29329 }
330}
331
[email protected]ae40b572009-10-02 21:17:45332// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35333IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30334 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11335 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45336
337 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11338 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45339 ui_test_utils::NavigateToURL(browser(), url);
340
341 browser()->Find();
342 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
343 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06344 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45345
346 // Focus the location bar.
347 browser()->FocusLocationBar();
348
349 // Create a 2nd tab.
[email protected]616381f02010-11-02 15:15:33350 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
[email protected]ae40b572009-10-02 21:17:45351
352 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06353 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45354
355 // Select 1st tab, focus should still be on the location-bar.
356 // (bug http://crbug.com/23296)
357 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06358 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45359
360 // Now open the find box again, switch to another tab and come back, the focus
361 // should return to the find box.
362 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06363 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45364 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06365 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45366 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06367 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45368}
369
initial.commit09911bf2008-07-26 23:55:29370// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07371// Flaky, http://crbug.com/62538.
372IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
373 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30374 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11375 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29376
initial.commit09911bf2008-07-26 23:55:29377 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18378 Browser* browser2 = Browser::Create(browser()->profile());
379 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43380 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18381 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00382
[email protected]ed179ee2009-10-03 21:02:51383 Browser* focused_browser = NULL;
384 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16385#if defined(USE_X11)
386 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00387 // to figure out which browser does have focus.
388 if (browser2->window()->IsActive()) {
389 focused_browser = browser2;
390 unfocused_browser = browser();
391 } else if (browser()->window()->IsActive()) {
392 focused_browser = browser();
393 unfocused_browser = browser2;
394 } else {
[email protected]579c3d82010-10-06 03:53:51395 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00396 }
397#elif defined(OS_WIN)
398 focused_browser = browser();
399 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57400#elif defined(OS_MACOSX)
401 // On Mac, the newly created window always gets the focus.
402 focused_browser = browser2;
403 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00404#endif
405
[email protected]95409e12010-08-17 20:07:11406 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00407 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46408
[email protected]8bcdec92009-02-25 16:15:18409 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00410 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29411
[email protected]579c3d82010-10-06 03:53:51412 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
413 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
414 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29415
[email protected]8bcdec92009-02-25 16:15:18416 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00417 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29418}
419
420// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21421IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30422 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11423 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29424
425 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11426 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18427 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29428
[email protected]186f13f2009-08-19 20:34:00429 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29430
[email protected]579c3d82010-10-06 03:53:51431 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
432 browser()->GetSelectedTabContents()->render_view_host(), L"",
433 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29434
435 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06436 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29437}
438
[email protected]9e0c83a2009-05-06 19:44:37439// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35440// Note that this test relies on a notification from the renderer that the
441// focus has changed in the page. The notification in the renderer may change
442// at which point this test would fail (see comment in
443// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57444IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30445 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11446 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29447
[email protected]8bcdec92009-02-25 16:15:18448 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11449 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18450 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29451
[email protected]186f13f2009-08-19 20:34:00452 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29453
[email protected]546ae4e02010-12-08 14:57:19454 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18455 const char* kExpElementIDs[] = {
456 "", // Initially no element in the page should be focused
457 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19458 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18459 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29460 };
461
462 // Test forward focus traversal.
463 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42464 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29465 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06466 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29467
468 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35469 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42470 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29471 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01472 std::string actual;
473 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25474 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18475 L"",
[email protected]45671612009-04-29 22:24:01476 L"window.domAutomationController.send(getFocusedElement());",
477 &actual));
initial.commit09911bf2008-07-26 23:55:29478 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
479
[email protected]130efb02009-09-18 18:54:35480 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19481 // If the next element is the kTextElementID, we expect to be
482 // notified we have switched to an editable node.
483 bool is_editable_node =
484 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
485 Details<bool> details(&is_editable_node);
486
487 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09488 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]546ae4e02010-12-08 14:57:19489 NotificationType::FOCUS_CHANGED_IN_PAGE,
490 NotificationSource(Source<RenderViewHost>(
491 browser()->GetSelectedTabContents()->render_view_host())),
492 details));
[email protected]130efb02009-09-18 18:54:35493 } else {
494 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19495 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09496 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]546ae4e02010-12-08 14:57:19497 NotificationType::FOCUS_RETURNED_TO_BROWSER,
498 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35499 }
initial.commit09911bf2008-07-26 23:55:29500 }
[email protected]8bcdec92009-02-25 16:15:18501
502 // At this point the renderer has sent us a message asking to advance the
503 // focus (as the end of the focus loop was reached in the renderer).
504 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42505 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29506 }
507
508 // Now let's try reverse focus traversal.
509 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42510 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29511 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06512 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29513
[email protected]8bcdec92009-02-25 16:15:18514 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30515 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42516 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19517 const char* next_element =
518 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35519
[email protected]130efb02009-09-18 18:54:35520 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19521 // If the next element is the kTextElementID, we expect to be
522 // notified we have switched to an editable node.
523 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
524 Details<bool> details(&is_editable_node);
525
526 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09527 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]546ae4e02010-12-08 14:57:19528 NotificationType::FOCUS_CHANGED_IN_PAGE,
529 NotificationSource(Source<RenderViewHost>(
530 browser()->GetSelectedTabContents()->render_view_host())),
531 details));
[email protected]130efb02009-09-18 18:54:35532 } else {
533 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19534 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09535 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]546ae4e02010-12-08 14:57:19536 NotificationType::FOCUS_RETURNED_TO_BROWSER,
537 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35538 }
initial.commit09911bf2008-07-26 23:55:29539
initial.commit09911bf2008-07-26 23:55:29540 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01541 std::string actual;
542 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25543 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18544 L"",
[email protected]45671612009-04-29 22:24:01545 L"window.domAutomationController.send(getFocusedElement());",
546 &actual));
[email protected]546ae4e02010-12-08 14:57:19547 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29548 }
[email protected]8bcdec92009-02-25 16:15:18549
550 // At this point the renderer has sent us a message asking to advance the
551 // focus (as the end of the focus loop was reached in the renderer).
552 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42553 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29554 }
555}
556
[email protected]9e0c83a2009-05-06 19:44:37557// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41558IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30559 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11560 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37561
562 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11563 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37564 ui_test_utils::NavigateToURL(browser(), url);
565
[email protected]9e0c83a2009-05-06 19:44:37566 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06567 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37568
569 // Let's show an interstitial.
570 TestInterstitialPage* interstitial_page =
571 new TestInterstitialPage(browser()->GetSelectedTabContents(),
572 true, GURL("http://interstitial.com"));
573 interstitial_page->Show();
574 // Give some time for the interstitial to show.
575 MessageLoop::current()->PostDelayedTask(FROM_HERE,
576 new MessageLoop::QuitTask(),
577 1000);
578 ui_test_utils::RunMessageLoop();
579
[email protected]fc2e0872009-08-21 22:14:41580 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37581
582 const char* kExpElementIDs[] = {
583 "", // Initially no element in the page should be focused
584 // (the location bar is focused).
585 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
586 "gmapLink"
587 };
588
589 // Test forward focus traversal.
590 for (int i = 0; i < 2; ++i) {
591 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06592 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37593
594 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35595 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37596 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30597 std::string actual;
598 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
599 interstitial_page->render_view_host(), L"",
600 L"window.domAutomationController.send(getFocusedElement());",
601 &actual));
[email protected]9e0c83a2009-05-06 19:44:37602 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
603
[email protected]a6e602f2010-09-28 22:28:30604 NotificationType::Type notification_type;
605 NotificationSource notification_source =
606 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35607 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30608 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
609 notification_source = Source<RenderViewHost>(
610 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35611 } else {
612 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30613 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
614 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35615 }
[email protected]a6e602f2010-09-28 22:28:30616
617 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09618 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30619 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37620 }
621
622 // At this point the renderer has sent us a message asking to advance the
623 // focus (as the end of the focus loop was reached in the renderer).
624 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42625 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37626 }
627
628 // Now let's try reverse focus traversal.
629 for (int i = 0; i < 2; ++i) {
630 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06631 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37632
633 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35634 for (size_t j = 0; j < 7; ++j) {
[email protected]a6e602f2010-09-28 22:28:30635 NotificationType::Type notification_type;
636 NotificationSource notification_source =
637 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35638 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30639 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
640 notification_source = Source<RenderViewHost>(
641 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35642 } else {
643 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30644 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
645 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35646 }
[email protected]9e0c83a2009-05-06 19:44:37647
[email protected]a6e602f2010-09-28 22:28:30648 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09649 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30650 notification_type, notification_source));
651
[email protected]9e0c83a2009-05-06 19:44:37652 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30653 std::string actual;
654 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
655 interstitial_page->render_view_host(), L"",
656 L"window.domAutomationController.send(getFocusedElement());",
657 &actual));
[email protected]9e0c83a2009-05-06 19:44:37658 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
659 }
660
661 // At this point the renderer has sent us a message asking to advance the
662 // focus (as the end of the focus loop was reached in the renderer).
663 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42664 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37665 }
666}
667
668// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21669IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30670 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11671 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37672
673 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11674 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37675 ui_test_utils::NavigateToURL(browser(), url);
676
[email protected]9e0c83a2009-05-06 19:44:37677 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06678 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37679 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
680 HasFocus());
681
[email protected]9d8a4642009-07-29 17:25:30682 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37683 TestInterstitialPage* interstitial_page =
684 new TestInterstitialPage(browser()->GetSelectedTabContents(),
685 true, GURL("http://interstitial.com"));
686 interstitial_page->Show();
687 // Give some time for the interstitial to show.
688 MessageLoop::current()->PostDelayedTask(FROM_HERE,
689 new MessageLoop::QuitTask(),
690 1000);
691 ui_test_utils::RunMessageLoop();
692
693 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06694 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37695 EXPECT_TRUE(interstitial_page->HasFocus());
696
697 // Hide the interstitial.
698 interstitial_page->DontProceed();
699
700 // Focus should be back on the original page.
[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}
703
[email protected]9bd491ee2008-12-10 22:31:07704// Make sure Find box can request focus, even when it is already open.
[email protected]a711df102010-12-14 09:47:03705IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[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());
license.botbf09a502008-08-24 00:55:55708
[email protected]9bd491ee2008-12-10 22:31:07709 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11710 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18711 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07712
[email protected]a711df102010-12-14 09:47:03713 EXPECT_TRUE(ChromeInForeground());
714
[email protected]853300a82010-07-27 21:17:57715#if defined(OS_MACOSX)
716 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28717 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09718 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57719#else
[email protected]9bd491ee2008-12-10 22:31:07720 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28721 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09722 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57723#endif
[email protected]8bcdec92009-02-25 16:15:18724
725 // Ideally, we wouldn't sleep here and instead would intercept the
726 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
727 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
728 // - creating a factory used to create the delegate
729 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41730 MessageLoop::current()->PostDelayedTask(
731 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18732 ui_test_utils::RunMessageLoop();
733
[email protected]21abcc742009-10-23 02:52:06734 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07735
[email protected]fc2e0872009-08-21 22:14:41736 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06737 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07738
739 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57740#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28741 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09742 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57743#else
[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]21abcc742009-10-23 02:52:06747 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07748
749 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41750 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06751 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07752
753 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57754#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28755 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09756 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57757#else
[email protected]1d000682010-08-23 16:21:28758 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09759 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57760#endif
[email protected]8bcdec92009-02-25 16:15:18761
762 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41763 MessageLoop::current()->PostDelayedTask(
764 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18765 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06766 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07767}
[email protected]401513c2009-03-12 00:21:28768
769// Makes sure the focus is in the right location when opening the different
770// types of tabs.
[email protected]c92928672010-11-09 18:31:07771// Flaky, http://crbug.com/62539.
772IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30773 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07774
[email protected]401513c2009-03-12 00:21:28775 // Open the history tab, focus should be on the tab contents.
776 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42777 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
778 &browser()->GetSelectedTabContents()->controller()));
779 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28780
781 // Open the new tab, focus should be on the location bar.
782 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42783 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
784 &browser()->GetSelectedTabContents()->controller()));
785 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28786
787 // Open the download tab, focus should be on the tab contents.
788 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42789 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
790 &browser()->GetSelectedTabContents()->controller()));
791 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43792
793 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55794 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
795 PageTransition::LINK);
[email protected]a3f343f2010-10-06 23:39:42796 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
797 &browser()->GetSelectedTabContents()->controller()));
798 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28799}
[email protected]9d8a4642009-07-29 17:25:30800
801// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21802IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30803 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11804 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30805
[email protected]9d8a4642009-07-29 17:25:30806 // Open the new tab, reload.
807 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42808 ui_test_utils::RunAllPendingInMessageLoop();
809
[email protected]cb84d642010-06-10 00:56:28810 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40811 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30812 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06813 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30814
815 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11816 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41817 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06818 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28819 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40820 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07821
[email protected]9d8a4642009-07-29 17:25:30822 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41823 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06824 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30825}
826
827// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27828IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30829 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11830 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30831
[email protected]9d8a4642009-07-29 17:25:30832 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11833 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30834 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28835 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40836 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07837
[email protected]9d8a4642009-07-29 17:25:30838 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41839 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06840 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30841}
[email protected]629e0342010-07-27 23:30:13842
843} // namespace