blob: 0c86510c73ff5e78a043ded726e891a6fe5e3ea1 [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]3985ba82010-07-29 21:44:127#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:428#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:189#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1210#include "base/path_service.h"
[email protected]a711df102010-12-14 09:47:0311#include "base/string_number_conversions.h"
[email protected]34877b32010-07-31 17:47:0512#include "base/string_util.h"
[email protected]be1ce6a72010-08-03 14:35:2213#include "base/utf_string_conversions.h"
[email protected]134c47b92009-08-19 03:33:4414#include "chrome/browser/browser_window.h"
[email protected]c848d3d92010-09-16 21:57:4515#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]7b5dc002010-11-16 23:08:1016#include "chrome/browser/ui/browser.h"
[email protected]6a3ec2312010-12-02 19:30:1917#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3718#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5519#include "chrome/common/url_constants.h"
[email protected]8bcdec92009-02-25 16:15:1820#include "chrome/test/in_process_browser_test.h"
21#include "chrome/test/ui_test_utils.h"
[email protected]5de634712011-03-02 00:20:1922#include "content/browser/renderer_host/render_view_host.h"
23#include "content/browser/renderer_host/render_widget_host_view.h"
24#include "content/browser/tab_contents/interstitial_page.h"
25#include "content/browser/tab_contents/tab_contents.h"
26#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]3985ba82010-07-29 21:44:1227#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5728
29#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0530#include "views/focus/focus_manager.h"
31#include "views/view.h"
32#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5733#endif
initial.commit09911bf2008-07-26 23:55:2934
[email protected]134c47b92009-08-19 03:33:4435#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5436#include "chrome/browser/ui/views/frame/browser_view.h"
37#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
38#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
[email protected]134c47b92009-08-19 03:33:4439#endif
40
[email protected]753efc42010-03-09 19:52:1641#if defined(TOOLKIT_USES_GTK)
[email protected]93270d002011-01-19 22:32:5942#include "chrome/browser/ui/gtk/view_id_util.h"
[email protected]b9821882009-08-17 22:25:1743#endif
44
[email protected]a711df102010-12-14 09:47:0345#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0346#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5947#include <windows.h>
[email protected]a711df102010-12-14 09:47:0348#endif
49
[email protected]fc2e0872009-08-21 22:14:4150#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5751#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1852#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3553// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5954#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5755#elif defined(OS_MACOSX)
[email protected]43012222010-10-29 19:11:1556// TODO(suzhe): http://crbug.com/60973 (following two tests)
[email protected]a5eb2d672010-10-22 07:24:4857#define MAYBE_FocusTraversal DISABLED_FocusTraversal
58#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]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]4e85c112011-01-26 22:27:21157 // Exposing render_view_host() and tab() to be public; they are declared as
158 // protected in the superclass.
[email protected]a6e602f2010-09-28 22:28:30159 virtual RenderViewHost* render_view_host() {
160 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37161 }
162
[email protected]4e85c112011-01-26 22:27:21163 virtual TabContents* tab() {
164 return InterstitialPage::tab();
165 }
166
[email protected]9e0c83a2009-05-06 19:44:37167 bool HasFocus() {
168 return render_view_host()->view()->HasFocus();
169 }
170
[email protected]130efb02009-09-18 18:54:35171 protected:
[email protected]546ae4e02010-12-08 14:57:19172 virtual void FocusedNodeChanged(bool is_editable_node) {
[email protected]a6e602f2010-09-28 22:28:30173 NotificationService::current()->Notify(
174 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21175 Source<TabContents>(tab()),
[email protected]546ae4e02010-12-08 14:57:19176 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:35177 }
178
[email protected]9e0c83a2009-05-06 19:44:37179 private:
180 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37181};
[email protected]b9821882009-08-17 22:25:17182
[email protected]e4f4e0b2009-10-13 19:58:21183IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30184 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42185#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41186 // It seems we have to wait a little bit for the widgets to spin up before
187 // we can start clicking on them.
188 MessageLoop::current()->PostDelayedTask(FROM_HERE,
189 new MessageLoop::QuitTask(),
190 kActionDelayMs);
191 ui_test_utils::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42192#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41193
[email protected]21abcc742009-10-23 02:52:06194 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00195
[email protected]fc2e0872009-08-21 22:14:41196 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06197 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00198
[email protected]fc2e0872009-08-21 22:14:41199 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06200 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00201}
[email protected]186f13f2009-08-19 20:34:00202
[email protected]320948e02011-01-10 08:21:12203// Flaky, http://crbug.com/69034.
204IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30205 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11206 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29207
208 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11209 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18210 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29211
[email protected]186f13f2009-08-19 20:34:00212 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
213
initial.commit09911bf2008-07-26 23:55:29214 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06215 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29216 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57217 ui_test_utils::HideNativeWindow(window);
218 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06219 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29220
[email protected]186f13f2009-08-19 20:34:00221 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06222 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29223 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57224 ui_test_utils::HideNativeWindow(window);
225 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06226 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29227
[email protected]186f13f2009-08-19 20:34:00228 // The rest of this test does not make sense on Linux because the behavior
229 // of Activate() is not well defined and can vary by window manager.
230#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29231 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18232 Browser* browser2 = Browser::Create(browser()->profile());
233 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43234 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18235 browser2->window()->Show();
236 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29237
[email protected]8bcdec92009-02-25 16:15:18238 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00239 BrowserView* browser_view2 =
240 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18241 ASSERT_TRUE(browser_view2);
242 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00243 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18244 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54245 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38246 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29247
248 // Switch to the 1st browser window, focus should still be on the location
249 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18250 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06251 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18252 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29253
254 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18255 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00256 EXPECT_EQ(NULL,
257 views::FocusManager::GetFocusManagerForNativeView(
258 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54259 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38260 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18261
262 // Close the 2nd browser to avoid a DCHECK().
263 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00264#endif
initial.commit09911bf2008-07-26 23:55:29265}
266
267// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39268// Disabled, http://crbug.com/62542.
269IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30270 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11271 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29272
273 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11274 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18275 ui_test_utils::NavigateToURL(browser(), url);
276
initial.commit09911bf2008-07-26 23:55:29277 // Create several tabs.
[email protected]616381f02010-11-02 15:15:33278 for (int i = 0; i < 4; ++i)
279 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29280
281 // Alternate focus for the tab.
282 const bool kFocusPage[3][5] = {
283 { true, true, true, true, false },
284 { false, false, false, false, false },
285 { false, true, false, true, false }
286 };
287
288 for (int i = 1; i < 3; i++) {
289 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18290 // Activate the tab.
291 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29292
293 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54294 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00295 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54296 } else {
[email protected]186f13f2009-08-19 20:34:00297 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54298 }
initial.commit09911bf2008-07-26 23:55:29299 }
300
301 // Now come back to the tab and check the right view is focused.
302 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18303 // Activate the tab.
304 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29305
[email protected]186f13f2009-08-19 20:34:00306 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
307 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06308 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29309 }
[email protected]cb7e2542009-12-14 22:02:35310
[email protected]cb7e2542009-12-14 22:02:35311 browser()->SelectTabContentsAt(0, true);
312 // Try the above, but with ctrl+tab. Since tab normally changes focus,
313 // this has regressed in the past. Loop through several times to be sure.
314 for (int j = 0; j < 15; j++) {
315 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
316 VIEW_ID_LOCATION_BAR;
317 ASSERT_TRUE(IsViewFocused(vid));
318
[email protected]1d000682010-08-23 16:21:28319 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09320 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35321 }
322
323 // As above, but with ctrl+shift+tab.
324 browser()->SelectTabContentsAt(4, true);
325 for (int j = 14; j >= 0; --j) {
326 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
327 VIEW_ID_LOCATION_BAR;
328 ASSERT_TRUE(IsViewFocused(vid));
329
[email protected]1d000682010-08-23 16:21:28330 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09331 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35332 }
initial.commit09911bf2008-07-26 23:55:29333 }
334}
335
[email protected]ae40b572009-10-02 21:17:45336// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35337IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30338 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11339 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45340
341 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11342 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45343 ui_test_utils::NavigateToURL(browser(), url);
344
345 browser()->Find();
[email protected]9c318862011-02-01 22:27:24346 ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
[email protected]ae40b572009-10-02 21:17:45347 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06348 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45349
350 // Focus the location bar.
351 browser()->FocusLocationBar();
352
353 // Create a 2nd tab.
[email protected]616381f02010-11-02 15:15:33354 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
[email protected]ae40b572009-10-02 21:17:45355
356 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06357 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45358
359 // Select 1st tab, focus should still be on the location-bar.
360 // (bug http://crbug.com/23296)
361 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06362 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45363
364 // Now open the find box again, switch to another tab and come back, the focus
365 // should return to the find box.
366 browser()->Find();
[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 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06369 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45370 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06371 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45372}
373
initial.commit09911bf2008-07-26 23:55:29374// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07375// Flaky, http://crbug.com/62538.
376IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
377 FLAKY_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30378 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11379 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29380
initial.commit09911bf2008-07-26 23:55:29381 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18382 Browser* browser2 = Browser::Create(browser()->profile());
383 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43384 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18385 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00386
[email protected]ed179ee2009-10-03 21:02:51387 Browser* focused_browser = NULL;
388 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16389#if defined(USE_X11)
390 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00391 // to figure out which browser does have focus.
392 if (browser2->window()->IsActive()) {
393 focused_browser = browser2;
394 unfocused_browser = browser();
395 } else if (browser()->window()->IsActive()) {
396 focused_browser = browser();
397 unfocused_browser = browser2;
398 } else {
[email protected]579c3d82010-10-06 03:53:51399 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00400 }
401#elif defined(OS_WIN)
402 focused_browser = browser();
403 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57404#elif defined(OS_MACOSX)
405 // On Mac, the newly created window always gets the focus.
406 focused_browser = browser2;
407 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00408#endif
409
[email protected]95409e12010-08-17 20:07:11410 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00411 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46412
[email protected]8bcdec92009-02-25 16:15:18413 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00414 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29415
[email protected]579c3d82010-10-06 03:53:51416 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
417 unfocused_browser->GetSelectedTabContents()->render_view_host(), L"",
418 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29419
[email protected]8bcdec92009-02-25 16:15:18420 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00421 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29422}
423
424// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21425IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30426 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11427 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29428
429 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11430 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18431 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29432
[email protected]186f13f2009-08-19 20:34:00433 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29434
[email protected]579c3d82010-10-06 03:53:51435 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
436 browser()->GetSelectedTabContents()->render_view_host(), L"",
437 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29438
439 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06440 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29441}
442
[email protected]9e0c83a2009-05-06 19:44:37443// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35444// Note that this test relies on a notification from the renderer that the
445// focus has changed in the page. The notification in the renderer may change
446// at which point this test would fail (see comment in
447// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57448IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30449 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11450 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29451
[email protected]8bcdec92009-02-25 16:15:18452 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11453 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18454 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29455
[email protected]186f13f2009-08-19 20:34:00456 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29457
[email protected]546ae4e02010-12-08 14:57:19458 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18459 const char* kExpElementIDs[] = {
460 "", // Initially no element in the page should be focused
461 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19462 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18463 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29464 };
465
466 // Test forward focus traversal.
467 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42468 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29469 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06470 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29471
[email protected]911696b2011-01-28 02:36:49472 // Move the caret to the end, otherwise the next Tab key may not move focus.
473 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
474 browser(), ui::VKEY_END, false, false, false, false));
475
initial.commit09911bf2008-07-26 23:55:29476 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35477 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42478 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29479 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01480 std::string actual;
481 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25482 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18483 L"",
[email protected]45671612009-04-29 22:24:01484 L"window.domAutomationController.send(getFocusedElement());",
485 &actual));
initial.commit09911bf2008-07-26 23:55:29486 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
487
[email protected]130efb02009-09-18 18:54:35488 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19489 // If the next element is the kTextElementID, we expect to be
490 // notified we have switched to an editable node.
491 bool is_editable_node =
492 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
493 Details<bool> details(&is_editable_node);
494
495 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[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_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21498 NotificationSource(Source<TabContents>(
499 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19500 details));
[email protected]130efb02009-09-18 18:54:35501 } else {
502 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19503 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09504 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]546ae4e02010-12-08 14:57:19505 NotificationType::FOCUS_RETURNED_TO_BROWSER,
506 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35507 }
initial.commit09911bf2008-07-26 23:55:29508 }
[email protected]8bcdec92009-02-25 16:15:18509
510 // At this point the renderer has sent us a message asking to advance the
511 // focus (as the end of the focus loop was reached in the renderer).
512 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42513 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29514 }
515
516 // Now let's try reverse focus traversal.
517 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42518 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29519 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06520 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29521
[email protected]911696b2011-01-28 02:36:49522 // Move the caret to the end, otherwise the next Tab key may not move focus.
523 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
524 browser(), ui::VKEY_END, false, false, false, false));
525
[email protected]8bcdec92009-02-25 16:15:18526 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30527 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42528 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19529 const char* next_element =
530 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35531
[email protected]130efb02009-09-18 18:54:35532 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19533 // If the next element is the kTextElementID, we expect to be
534 // notified we have switched to an editable node.
535 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
536 Details<bool> details(&is_editable_node);
537
538 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09539 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]546ae4e02010-12-08 14:57:19540 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:21541 NotificationSource(Source<TabContents>(
542 browser()->GetSelectedTabContents())),
[email protected]546ae4e02010-12-08 14:57:19543 details));
[email protected]130efb02009-09-18 18:54:35544 } else {
545 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19546 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09547 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]546ae4e02010-12-08 14:57:19548 NotificationType::FOCUS_RETURNED_TO_BROWSER,
549 NotificationSource(Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35550 }
initial.commit09911bf2008-07-26 23:55:29551
initial.commit09911bf2008-07-26 23:55:29552 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01553 std::string actual;
554 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25555 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18556 L"",
[email protected]45671612009-04-29 22:24:01557 L"window.domAutomationController.send(getFocusedElement());",
558 &actual));
[email protected]546ae4e02010-12-08 14:57:19559 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29560 }
[email protected]8bcdec92009-02-25 16:15:18561
562 // At this point the renderer has sent us a message asking to advance the
563 // focus (as the end of the focus loop was reached in the renderer).
564 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42565 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29566 }
567}
568
[email protected]9e0c83a2009-05-06 19:44:37569// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41570IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30571 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11572 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37573
574 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11575 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37576 ui_test_utils::NavigateToURL(browser(), url);
577
[email protected]9e0c83a2009-05-06 19:44:37578 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06579 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37580
581 // Let's show an interstitial.
582 TestInterstitialPage* interstitial_page =
583 new TestInterstitialPage(browser()->GetSelectedTabContents(),
584 true, GURL("http://interstitial.com"));
585 interstitial_page->Show();
586 // Give some time for the interstitial to show.
587 MessageLoop::current()->PostDelayedTask(FROM_HERE,
588 new MessageLoop::QuitTask(),
589 1000);
590 ui_test_utils::RunMessageLoop();
591
[email protected]fc2e0872009-08-21 22:14:41592 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37593
594 const char* kExpElementIDs[] = {
595 "", // Initially no element in the page should be focused
596 // (the location bar is focused).
597 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
598 "gmapLink"
599 };
600
601 // Test forward focus traversal.
602 for (int i = 0; i < 2; ++i) {
603 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06604 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37605
[email protected]911696b2011-01-28 02:36:49606 // Move the caret to the end, otherwise the next Tab key may not move focus.
607 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
608 browser(), ui::VKEY_END, false, false, false, false));
609
[email protected]9e0c83a2009-05-06 19:44:37610 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35611 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37612 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30613 std::string actual;
614 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
615 interstitial_page->render_view_host(), L"",
616 L"window.domAutomationController.send(getFocusedElement());",
617 &actual));
[email protected]9e0c83a2009-05-06 19:44:37618 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
619
[email protected]a6e602f2010-09-28 22:28:30620 NotificationType::Type notification_type;
621 NotificationSource notification_source =
622 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35623 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30624 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21625 notification_source = Source<TabContents>(
626 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35627 } else {
628 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30629 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
630 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35631 }
[email protected]a6e602f2010-09-28 22:28:30632
633 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09634 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30635 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37636 }
637
638 // At this point the renderer has sent us a message asking to advance the
639 // focus (as the end of the focus loop was reached in the renderer).
640 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42641 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37642 }
643
644 // Now let's try reverse focus traversal.
645 for (int i = 0; i < 2; ++i) {
646 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06647 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37648
[email protected]911696b2011-01-28 02:36:49649 // Move the caret to the end, otherwise the next Tab key may not move focus.
650 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
651 browser(), ui::VKEY_END, false, false, false, false));
652
[email protected]9e0c83a2009-05-06 19:44:37653 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35654 for (size_t j = 0; j < 7; ++j) {
[email protected]a6e602f2010-09-28 22:28:30655 NotificationType::Type notification_type;
656 NotificationSource notification_source =
657 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35658 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30659 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
[email protected]4e85c112011-01-26 22:27:21660 notification_source = Source<TabContents>(
661 interstitial_page->tab());
[email protected]130efb02009-09-18 18:54:35662 } else {
663 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30664 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
665 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35666 }
[email protected]9e0c83a2009-05-06 19:44:37667
[email protected]a6e602f2010-09-28 22:28:30668 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09669 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30670 notification_type, notification_source));
671
[email protected]9e0c83a2009-05-06 19:44:37672 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30673 std::string actual;
674 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
675 interstitial_page->render_view_host(), L"",
676 L"window.domAutomationController.send(getFocusedElement());",
677 &actual));
[email protected]9e0c83a2009-05-06 19:44:37678 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
679 }
680
681 // At this point the renderer has sent us a message asking to advance the
682 // focus (as the end of the focus loop was reached in the renderer).
683 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42684 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37685 }
686}
687
688// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21689IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30690 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11691 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37692
693 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11694 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37695 ui_test_utils::NavigateToURL(browser(), url);
696
[email protected]9e0c83a2009-05-06 19:44:37697 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06698 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37699 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
700 HasFocus());
701
[email protected]9d8a4642009-07-29 17:25:30702 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37703 TestInterstitialPage* interstitial_page =
704 new TestInterstitialPage(browser()->GetSelectedTabContents(),
705 true, GURL("http://interstitial.com"));
706 interstitial_page->Show();
707 // Give some time for the interstitial to show.
708 MessageLoop::current()->PostDelayedTask(FROM_HERE,
709 new MessageLoop::QuitTask(),
710 1000);
711 ui_test_utils::RunMessageLoop();
712
713 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06714 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37715 EXPECT_TRUE(interstitial_page->HasFocus());
716
717 // Hide the interstitial.
718 interstitial_page->DontProceed();
719
720 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06721 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37722}
723
[email protected]9bd491ee2008-12-10 22:31:07724// Make sure Find box can request focus, even when it is already open.
[email protected]a711df102010-12-14 09:47:03725IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30726 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11727 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55728
[email protected]9bd491ee2008-12-10 22:31:07729 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11730 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18731 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07732
[email protected]a711df102010-12-14 09:47:03733 EXPECT_TRUE(ChromeInForeground());
734
[email protected]853300a82010-07-27 21:17:57735#if defined(OS_MACOSX)
736 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28737 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09738 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57739#else
[email protected]9bd491ee2008-12-10 22:31:07740 // Press Ctrl+F, which will make the Find box open and request focus.
[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, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57743#endif
[email protected]8bcdec92009-02-25 16:15:18744
745 // Ideally, we wouldn't sleep here and instead would intercept the
746 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
747 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
748 // - creating a factory used to create the delegate
749 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41750 MessageLoop::current()->PostDelayedTask(
751 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18752 ui_test_utils::RunMessageLoop();
753
[email protected]21abcc742009-10-23 02:52:06754 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07755
[email protected]fc2e0872009-08-21 22:14:41756 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06757 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07758
759 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57760#if defined(OS_MACOSX)
[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]1d000682010-08-23 16:21:28764 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09765 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57766#endif
[email protected]21abcc742009-10-23 02:52:06767 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07768
769 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41770 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06771 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07772
773 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57774#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28775 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09776 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57777#else
[email protected]1d000682010-08-23 16:21:28778 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09779 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57780#endif
[email protected]8bcdec92009-02-25 16:15:18781
782 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41783 MessageLoop::current()->PostDelayedTask(
784 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18785 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06786 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07787}
[email protected]401513c2009-03-12 00:21:28788
789// Makes sure the focus is in the right location when opening the different
790// types of tabs.
[email protected]c92928672010-11-09 18:31:07791// Flaky, http://crbug.com/62539.
792IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30793 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07794
[email protected]401513c2009-03-12 00:21:28795 // Open the history tab, focus should be on the tab contents.
796 browser()->ShowHistoryTab();
[email protected]a3f343f2010-10-06 23:39:42797 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54798 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42799 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28800
801 // Open the new tab, focus should be on the location bar.
802 browser()->NewTab();
[email protected]a3f343f2010-10-06 23:39:42803 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54804 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42805 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28806
807 // Open the download tab, focus should be on the tab contents.
808 browser()->ShowDownloadsTab();
[email protected]a3f343f2010-10-06 23:39:42809 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54810 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42811 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43812
813 // Open about:blank, focus should be on the location bar.
[email protected]cecc93a2010-10-05 15:58:55814 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
815 PageTransition::LINK);
[email protected]a3f343f2010-10-06 23:39:42816 ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop(
[email protected]317e5022011-03-25 17:03:54817 browser()->GetSelectedTabContents()));
[email protected]a3f343f2010-10-06 23:39:42818 EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28819}
[email protected]9d8a4642009-07-29 17:25:30820
821// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21822IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30823 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11824 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30825
[email protected]9d8a4642009-07-29 17:25:30826 // Open the new tab, reload.
827 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42828 ui_test_utils::RunAllPendingInMessageLoop();
829
[email protected]cb84d642010-06-10 00:56:28830 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40831 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30832 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06833 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30834
835 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11836 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41837 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06838 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28839 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40840 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07841
[email protected]9d8a4642009-07-29 17:25:30842 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41843 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06844 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30845}
846
847// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27848IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30849 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11850 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30851
[email protected]9d8a4642009-07-29 17:25:30852 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11853 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30854 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28855 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40856 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07857
[email protected]9d8a4642009-07-29 17:25:30858 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41859 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06860 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30861}
[email protected]629e0342010-07-27 23:30:13862
863} // namespace