blob: 84828224a39c7a9d10b4ceaac84e368d70e7eb65 [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]3985ba82010-07-29 21:44:127#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:428#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:189#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1210#include "base/path_service.h"
[email protected]1870d5cf2011-05-12 01:55:4011#include "base/stringprintf.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]be1ce6a72010-08-03 14:35:2213#include "base/utf_string_conversions.h"
[email protected]c848d3d92010-09-16 21:57:4514#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3316#include "chrome/browser/ui/browser_window.h"
[email protected]6b4e8e42011-08-17 19:36:0617#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]6a3ec2312010-12-02 19:30:1918#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2719#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3720#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5521#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3222#include "chrome/test/base/ui_test_utils.h"
23#include "chrome/test/base/in_process_browser_test.h"
[email protected]5de634712011-03-02 00:20:1924#include "content/browser/renderer_host/render_view_host.h"
25#include "content/browser/renderer_host/render_widget_host_view.h"
26#include "content/browser/tab_contents/interstitial_page.h"
27#include "content/browser/tab_contents/tab_contents.h"
28#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]216813952011-05-19 22:21:2629#include "content/common/view_messages.h"
[email protected]3985ba82010-07-29 21:44:1230#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5731
32#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0533#include "views/focus/focus_manager.h"
34#include "views/view.h"
[email protected]853300a82010-07-27 21:17:5735#endif
initial.commit09911bf2008-07-26 23:55:2936
[email protected]134c47b92009-08-19 03:33:4437#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5438#include "chrome/browser/ui/views/frame/browser_view.h"
39#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
40#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4441#endif
42
[email protected]753efc42010-03-09 19:52:1643#if defined(TOOLKIT_USES_GTK)
[email protected]93270d002011-01-19 22:32:5944#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1745#endif
46
[email protected]a711df102010-12-14 09:47:0347#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0348#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5949#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4050#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0351#endif
52
[email protected]fc2e0872009-08-21 22:14:4153#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5754#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1855#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3556// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5957#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5758#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1559// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4860#define MAYBE_FocusTraversal DISABLED_FocusTraversal
61#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[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]216813952011-05-19 22:21:26175 bool OnMessageReceived(const IPC::Message& message) {
176 bool handled = true;
177 IPC_BEGIN_MESSAGE_MAP(TestInterstitialPage, message)
178 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
179 IPC_MESSAGE_UNHANDLED(handled = false)
180 IPC_END_MESSAGE_MAP()
181 return handled;
182 }
183
184 void OnFocusedNodeChanged(bool is_editable_node) {
[email protected]a6e602f2010-09-28 22:28:30185 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27186 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21187 Source<TabContents>(tab()),
[email protected]546ae4e02010-12-08 14:57:19188 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:35189 }
190
[email protected]9e0c83a2009-05-06 19:44:37191 private:
192 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37193};
[email protected]b9821882009-08-17 22:25:17194
[email protected]e4f4e0b2009-10-13 19:58:21195IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30196 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42197#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41198 // It seems we have to wait a little bit for the widgets to spin up before
199 // we can start clicking on them.
200 MessageLoop::current()->PostDelayedTask(FROM_HERE,
201 new MessageLoop::QuitTask(),
202 kActionDelayMs);
203 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42204#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41205
[email protected]21abcc742009-10-23 02:52:06206 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00207
[email protected]fc2e0872009-08-21 22:14:41208 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06209 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00210
[email protected]fc2e0872009-08-21 22:14:41211 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06212 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00213}
[email protected]186f13f2009-08-19 20:34:00214
[email protected]320948e02011-01-10 08:21:12215// Flaky, http://crbug.com/69034.
216IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30217 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11218 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29219
220 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11221 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18222 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29223
[email protected]186f13f2009-08-19 20:34:00224 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
225
initial.commit09911bf2008-07-26 23:55:29226 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06227 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29228 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57229 ui_test_utils::HideNativeWindow(window);
230 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06231 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29232
[email protected]186f13f2009-08-19 20:34:00233 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06234 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29235 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57236 ui_test_utils::HideNativeWindow(window);
237 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06238 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29239
[email protected]186f13f2009-08-19 20:34:00240 // The rest of this test does not make sense on Linux because the behavior
241 // of Activate() is not well defined and can vary by window manager.
242#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29243 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18244 Browser* browser2 = Browser::Create(browser()->profile());
245 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43246 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18247 browser2->window()->Show();
248 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29249
[email protected]0c966682011-08-02 18:22:10250 gfx::NativeWindow window2 = browser2->window()->GetNativeHandle();
[email protected]4a507a62009-05-28 00:10:00251 BrowserView* browser_view2 =
[email protected]0c966682011-08-02 18:22:10252 BrowserView::GetBrowserViewForNativeWindow(window2);
[email protected]8bcdec92009-02-25 16:15:18253 ASSERT_TRUE(browser_view2);
[email protected]0c966682011-08-02 18:22:10254 views::Widget* widget2 = views::Widget::GetWidgetForNativeWindow(window2);
255 ASSERT_TRUE(widget2);
256 views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18257 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54258 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38259 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29260
261 // Switch to the 1st browser window, focus should still be on the location
262 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18263 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06264 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18265 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29266
267 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18268 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10269 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
270 ASSERT_TRUE(widget);
271 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54272 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38273 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18274
275 // Close the 2nd browser to avoid a DCHECK().
276 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00277#endif
initial.commit09911bf2008-07-26 23:55:29278}
279
280// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39281// Disabled, http://crbug.com/62542.
282IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30283 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11284 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29285
286 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11287 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18288 ui_test_utils::NavigateToURL(browser(), url);
289
initial.commit09911bf2008-07-26 23:55:29290 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33291 for (int i = 0; i < 4; ++i)
292 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29293
294 // Alternate focus for the tab.
295 const bool kFocusPage[3][5] = {
296 { true, true, true, true, false },
297 { false, false, false, false, false },
298 { false, true, false, true, false }
299 };
300
301 for (int i = 1; i < 3; i++) {
302 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18303 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41304 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29305
306 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54307 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00308 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54309 } else {
[email protected]186f13f2009-08-19 20:34:00310 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54311 }
initial.commit09911bf2008-07-26 23:55:29312 }
313
314 // Now come back to the tab and check the right view is focused.
315 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18316 // Activate the tab.
[email protected]eaca0ad12011-04-18 15:53:41317 browser()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29318
[email protected]186f13f2009-08-19 20:34:00319 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
320 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06321 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29322 }
[email protected]cb7e2542009-12-14 22:02:35323
[email protected]eaca0ad12011-04-18 15:53:41324 browser()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35325 // Try the above, but with ctrl+tab. Since tab normally changes focus,
326 // this has regressed in the past. Loop through several times to be sure.
327 for (int j = 0; j < 15; j++) {
328 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
329 VIEW_ID_LOCATION_BAR;
330 ASSERT_TRUE(IsViewFocused(vid));
331
[email protected]1d000682010-08-23 16:21:28332 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09333 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35334 }
335
336 // As above, but with ctrl+shift+tab.
[email protected]eaca0ad12011-04-18 15:53:41337 browser()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35338 for (int j = 14; j >= 0; --j) {
339 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
340 VIEW_ID_LOCATION_BAR;
341 ASSERT_TRUE(IsViewFocused(vid));
342
[email protected]1d000682010-08-23 16:21:28343 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09344 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35345 }
initial.commit09911bf2008-07-26 23:55:29346 }
347}
348
[email protected]ae40b572009-10-02 21:17:45349// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35350IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30351 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11352 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45353
354 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11355 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45356 ui_test_utils::NavigateToURL(browser(), url);
357
358 browser()->Find();
[email protected]9c318862011-02-01 22:27:24359 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45360 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06361 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45362
363 // Focus the location bar.
364 browser()->FocusLocationBar();
365
366 // Create a 2nd tab.
[email protected]616381f02010-11-02 15:15:33367 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
[email protected]ae40b572009-10-02 21:17:45368
369 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06370 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45371
372 // Select 1st tab, focus should still be on the location-bar.
373 // (bug http://crbug.com/23296)
[email protected]eaca0ad12011-04-18 15:53:41374 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06375 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45376
377 // Now open the find box again, switch to another tab and come back, the focus
378 // should return to the find box.
379 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06380 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]eaca0ad12011-04-18 15:53:41381 browser()->ActivateTabAt(1, true);
[email protected]21abcc742009-10-23 02:52:06382 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]eaca0ad12011-04-18 15:53:41383 browser()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06384 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45385}
386
initial.commit09911bf2008-07-26 23:55:29387// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07388// Flaky, http://crbug.com/62538.
389IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
390 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30391 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11392 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29393
initial.commit09911bf2008-07-26 23:55:29394 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18395 Browser* browser2 = Browser::Create(browser()->profile());
396 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43397 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18398 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00399
[email protected]ed179ee2009-10-03 21:02:51400 Browser* focused_browser = NULL;
401 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16402#if defined(USE_X11)
403 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00404 // to figure out which browser does have focus.
405 if (browser2->window()->IsActive()) {
406 focused_browser = browser2;
407 unfocused_browser = browser();
408 } else if (browser()->window()->IsActive()) {
409 focused_browser = browser();
410 unfocused_browser = browser2;
411 } else {
[email protected]579c3d82010-10-06 03:53:51412 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00413 }
414#elif defined(OS_WIN)
415 focused_browser = browser();
416 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57417#elif defined(OS_MACOSX)
418 // On Mac, the newly created window always gets the focus.
419 focused_browser = browser2;
420 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00421#endif
422
[email protected]95409e12010-08-17 20:07:11423 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00424 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46425
[email protected]8bcdec92009-02-25 16:15:18426 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00427 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29428
[email protected]579c3d82010-10-06 03:53:51429 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
430 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
431 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29432
[email protected]8bcdec92009-02-25 16:15:18433 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00434 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29435}
436
437// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21438IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30439 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11440 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29441
442 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11443 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18444 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29445
[email protected]186f13f2009-08-19 20:34:00446 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29447
[email protected]579c3d82010-10-06 03:53:51448 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
449 browser()->GetSelectedTabContents()->render_view_host(), L"",
450 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29451
452 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06453 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29454}
455
[email protected]9e0c83a2009-05-06 19:44:37456// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35457// Note that this test relies on a notification from the renderer that the
458// focus has changed in the page. The notification in the renderer may change
459// at which point this test would fail (see comment in
460// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57461IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30462 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11463 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29464
[email protected]8bcdec92009-02-25 16:15:18465 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11466 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18467 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29468
[email protected]186f13f2009-08-19 20:34:00469 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29470
[email protected]546ae4e02010-12-08 14:57:19471 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18472 const char* kExpElementIDs[] = {
473 "", // Initially no element in the page should be focused
474 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19475 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18476 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29477 };
478
479 // Test forward focus traversal.
480 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40481 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29482 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06483 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29484
[email protected]911696b2011-01-28 02:36:49485 // Move the caret to the end, otherwise the next Tab key may not move focus.
486 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
487 browser(), ui::VKEY_END, false, false, false, false));
488
initial.commit09911bf2008-07-26 23:55:29489 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35490 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40491 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29492 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01493 std::string actual;
494 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25495 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18496 L"",
[email protected]45671612009-04-29 22:24:01497 L"window.domAutomationController.send(getFocusedElement());",
498 &actual));
initial.commit09911bf2008-07-26 23:55:29499 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
500
[email protected]130efb02009-09-18 18:54:35501 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19502 // If the next element is the kTextElementID, we expect to be
503 // notified we have switched to an editable node.
504 bool is_editable_node =
505 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
506 Details<bool> details(&is_editable_node);
507
508 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09509 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27510 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21511 NotificationSource(Source<TabContents>(
512 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19513 details));
[email protected]130efb02009-09-18 18:54:35514 } else {
515 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19516 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09517 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27518 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]546ae4e02010-12-08 14:57:19519 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35520 }
initial.commit09911bf2008-07-26 23:55:29521 }
[email protected]8bcdec92009-02-25 16:15:18522
523 // At this point the renderer has sent us a message asking to advance the
524 // focus (as the end of the focus loop was reached in the renderer).
525 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42526 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29527 }
528
529 // Now let's try reverse focus traversal.
530 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40531 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29532 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06533 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29534
[email protected]911696b2011-01-28 02:36:49535 // Move the caret to the end, otherwise the next Tab key may not move focus.
536 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
537 browser(), ui::VKEY_END, false, false, false, false));
538
[email protected]8bcdec92009-02-25 16:15:18539 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30540 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40541 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19542 const char* next_element =
543 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35544
545 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19546 // If the next element is the kTextElementID, we expect to be
547 // notified we have switched to an editable node.
548 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
549 Details<bool> details(&is_editable_node);
550
551 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09552 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27553 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21554 NotificationSource(Source<TabContents>(
555 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19556 details));
[email protected]130efb02009-09-18 18:54:35557 } else {
558 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19559 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09560 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27561 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]546ae4e02010-12-08 14:57:19562 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35563 }
initial.commit09911bf2008-07-26 23:55:29564
565 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01566 std::string actual;
567 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25568 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18569 L"",
[email protected]45671612009-04-29 22:24:01570 L"window.domAutomationController.send(getFocusedElement());",
571 &actual));
[email protected]546ae4e02010-12-08 14:57:19572 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29573 }
[email protected]8bcdec92009-02-25 16:15:18574
575 // At this point the renderer has sent us a message asking to advance the
576 // focus (as the end of the focus loop was reached in the renderer).
577 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42578 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29579 }
580}
581
[email protected]9e0c83a2009-05-06 19:44:37582// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41583IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30584 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11585 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37586
587 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11588 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37589 ui_test_utils::NavigateToURL(browser(), url);
590
[email protected]9e0c83a2009-05-06 19:44:37591 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06592 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37593
594 // Let's show an interstitial.
595 TestInterstitialPage* interstitial_page =
596 new TestInterstitialPage(browser()->GetSelectedTabContents(),
597 true, GURL("http://interstitial.com"));
598 interstitial_page->Show();
599 // Give some time for the interstitial to show.
600 MessageLoop::current()->PostDelayedTask(FROM_HERE,
601 new MessageLoop::QuitTask(),
602 1000);
603 ui_test_utils::RunMessageLoop();
604
[email protected]fc2e0872009-08-21 22:14:41605 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37606
607 const char* kExpElementIDs[] = {
608 "", // Initially no element in the page should be focused
609 // (the location bar is focused).
610 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
611 "gmapLink"
612 };
613
614 // Test forward focus traversal.
615 for (int i = 0; i < 2; ++i) {
616 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06617 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37618
[email protected]911696b2011-01-28 02:36:49619 // Move the caret to the end, otherwise the next Tab key may not move focus.
620 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
621 browser(), ui::VKEY_END, false, false, false, false));
622
[email protected]9e0c83a2009-05-06 19:44:37623 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35624 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37625 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30626 std::string actual;
627 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
628 interstitial_page->render_view_host(), L"",
629 L"window.domAutomationController.send(getFocusedElement());",
630 &actual));
[email protected]9e0c83a2009-05-06 19:44:37631 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
632
[email protected]432115822011-07-10 15:52:27633 int notification_type;
[email protected]a6e602f2010-09-28 22:28:30634 NotificationSource notification_source =
635 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35636 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27637 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21638 notification_source = Source<TabContents>(
639 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35640 } else {
641 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27642 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]a6e602f2010-09-28 22:28:30643 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35644 }
[email protected]a6e602f2010-09-28 22:28:30645
646 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09647 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30648 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37649 }
650
651 // At this point the renderer has sent us a message asking to advance the
652 // focus (as the end of the focus loop was reached in the renderer).
653 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42654 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37655 }
656
657 // Now let's try reverse focus traversal.
658 for (int i = 0; i < 2; ++i) {
659 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06660 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37661
[email protected]911696b2011-01-28 02:36:49662 // Move the caret to the end, otherwise the next Tab key may not move focus.
663 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
664 browser(), ui::VKEY_END, false, false, false, false));
665
[email protected]9e0c83a2009-05-06 19:44:37666 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35667 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27668 int notification_type;
[email protected]a6e602f2010-09-28 22:28:30669 NotificationSource notification_source =
670 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35671 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27672 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21673 notification_source = Source<TabContents>(
674 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35675 } else {
676 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27677 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]a6e602f2010-09-28 22:28:30678 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35679 }
[email protected]9e0c83a2009-05-06 19:44:37680
[email protected]a6e602f2010-09-28 22:28:30681 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09682 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30683 notification_type, notification_source));
684
[email protected]9e0c83a2009-05-06 19:44:37685 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30686 std::string actual;
687 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
688 interstitial_page->render_view_host(), L"",
689 L"window.domAutomationController.send(getFocusedElement());",
690 &actual));
[email protected]9e0c83a2009-05-06 19:44:37691 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
692 }
693
694 // At this point the renderer has sent us a message asking to advance the
695 // focus (as the end of the focus loop was reached in the renderer).
696 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42697 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37698 }
699}
700
701// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33702// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05703#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]cb931932011-05-03 19:57:33704IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_InterstitialFocus) {
705#else
[email protected]e4f4e0b2009-10-13 19:58:21706IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33707#endif
[email protected]a6e602f2010-09-28 22:28:30708 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11709 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37710
711 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11712 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37713 ui_test_utils::NavigateToURL(browser(), url);
714
[email protected]9e0c83a2009-05-06 19:44:37715 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06716 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37717 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
718 HasFocus());
719
[email protected]9d8a4642009-07-29 17:25:30720 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37721 TestInterstitialPage* interstitial_page =
722 new TestInterstitialPage(browser()->GetSelectedTabContents(),
723 true, GURL("http://interstitial.com"));
724 interstitial_page->Show();
725 // Give some time for the interstitial to show.
726 MessageLoop::current()->PostDelayedTask(FROM_HERE,
727 new MessageLoop::QuitTask(),
728 1000);
729 ui_test_utils::RunMessageLoop();
730
731 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06732 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37733 EXPECT_TRUE(interstitial_page->HasFocus());
734
735 // Hide the interstitial.
736 interstitial_page->DontProceed();
737
738 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06739 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37740}
741
[email protected]9bd491ee2008-12-10 22:31:07742// Make sure Find box can request focus, even when it is already open.
[email protected]2004b762011-05-05 22:43:20743// Flaky on mac and valgrind. http://crbug.com/67301.
744#if defined(OS_MACOSX)
745#define MAYBE_FindFocusTest FLAKY_FindFocusTest
746#else
747#define MAYBE_FindFocusTest FindFocusTest
748#endif
[email protected]a711df102010-12-14 09:47:03749IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30750 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11751 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55752
[email protected]9bd491ee2008-12-10 22:31:07753 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11754 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18755 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07756
[email protected]a711df102010-12-14 09:47:03757 EXPECT_TRUE(ChromeInForeground());
758
[email protected]853300a82010-07-27 21:17:57759#if defined(OS_MACOSX)
760 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28761 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09762 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57763#else
[email protected]9bd491ee2008-12-10 22:31:07764 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28765 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09766 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57767#endif
[email protected]8bcdec92009-02-25 16:15:18768
769 // Ideally, we wouldn't sleep here and instead would intercept the
770 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
771 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
772 // - creating a factory used to create the delegate
773 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41774 MessageLoop::current()->PostDelayedTask(
775 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18776 ui_test_utils::RunMessageLoop();
777
[email protected]21abcc742009-10-23 02:52:06778 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07779
[email protected]fc2e0872009-08-21 22:14:41780 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06781 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07782
783 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57784#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28785 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09786 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57787#else
[email protected]1d000682010-08-23 16:21:28788 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09789 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57790#endif
[email protected]21abcc742009-10-23 02:52:06791 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07792
793 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41794 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06795 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07796
797 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57798#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28799 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09800 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57801#else
[email protected]1d000682010-08-23 16:21:28802 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09803 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57804#endif
[email protected]8bcdec92009-02-25 16:15:18805
806 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41807 MessageLoop::current()->PostDelayedTask(
808 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18809 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06810 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07811}
[email protected]401513c2009-03-12 00:21:28812
813// Makes sure the focus is in the right location when opening the different
814// types of tabs.
[email protected]c92928672010-11-09 18:31:07815// Flaky, http://crbug.com/62539.
816IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30817 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07818
[email protected]401513c2009-03-12 00:21:28819 // Open the history tab, focus should be on the tab contents.
820 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42821 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54822 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42823 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28824
825 // Open the new tab, focus should be on the location bar.
826 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42827 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54828 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42829 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28830
831 // Open the download tab, focus should be on the tab contents.
832 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42833 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54834 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42835 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43836
837 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55838 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
839 PageTransition::LINK);
[email protected]a3f343f2010-10-06 23:39:42840 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54841 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42842 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28843}
[email protected]9d8a4642009-07-29 17:25:30844
845// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21846IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30847 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11848 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30849
[email protected]9d8a4642009-07-29 17:25:30850 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06851 {
852 ui_test_utils::WindowedNotificationObserver observer(
853 content::NOTIFICATION_LOAD_STOP,
854 NotificationService::AllSources());
855 browser()->NewTab();
856 observer.Wait();
857 }
[email protected]f07d7bf2010-04-06 08:02:42858 ui_test_utils::RunAllPendingInMessageLoop();
859
[email protected]6b4e8e42011-08-17 19:36:06860 {
861 ui_test_utils::WindowedNotificationObserver observer(
862 content::NOTIFICATION_LOAD_STOP,
863 Source<NavigationController>(
864 &browser()->GetSelectedTabContentsWrapper()->controller()));
865 browser()->Reload(CURRENT_TAB);
866 observer.Wait();
867 }
[email protected]9d8a4642009-07-29 17:25:30868 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06869 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30870
871 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11872 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41873 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06874 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]6b4e8e42011-08-17 19:36:06875 {
876 ui_test_utils::WindowedNotificationObserver observer(
877 content::NOTIFICATION_LOAD_STOP,
878 Source<NavigationController>(
879 &browser()->GetSelectedTabContentsWrapper()->controller()));
880 browser()->Reload(CURRENT_TAB);
881 observer.Wait();
882 }
[email protected]9ba21ede2010-07-30 01:11:07883
[email protected]9d8a4642009-07-29 17:25:30884 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41885 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06886 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30887}
888
889// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27890IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30891 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11892 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30893
[email protected]9d8a4642009-07-29 17:25:30894 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11895 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30896 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]6b4e8e42011-08-17 19:36:06897 {
898 ui_test_utils::WindowedNotificationObserver observer(
899 content::NOTIFICATION_LOAD_STOP,
900 Source<NavigationController>(
901 &browser()->GetSelectedTabContentsWrapper()->controller()));
902 browser()->Reload(CURRENT_TAB);
903 observer.Wait();
904 }
[email protected]9ba21ede2010-07-30 01:11:07905
[email protected]9d8a4642009-07-29 17:25:30906 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41907 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06908 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30909}
[email protected]629e0342010-07-27 23:30:13910
911} // namespace