blob: 3f9f6891060998034aaf6fb1cd4206dfe87e4441 [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 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]a9c060ca2012-01-05 20:43:417#include "base/bind.h"
[email protected]3985ba82010-07-29 21:44:128#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:429#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:1810#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1211#include "base/path_service.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]477ae052011-11-18 23:53:5713#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2214#include "base/utf_string_conversions.h"
[email protected]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1016#include "chrome/browser/ui/browser_commands.h"
[email protected]52877dbc62012-06-29 22:22:0317#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3318#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1219#include "chrome/browser/ui/chrome_pages.h"
[email protected]895a84722012-11-29 18:45:4420#include "chrome/browser/ui/omnibox/location_bar.h"
21#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
22#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
23#include "chrome/browser/ui/omnibox/omnibox_view.h"
[email protected]b56e2e32012-05-11 21:18:0424#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1925#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2726#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3727#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5528#include "chrome/common/url_constants.h"
[email protected]895a84722012-11-29 18:45:4429#include "chrome/test/base/interactive_test_utils.h"
[email protected]af44e7fb2011-07-29 18:32:3230#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5731#include "chrome/test/base/ui_test_utils.h"
[email protected]cadaec52012-02-08 21:53:1332#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1333#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5734#include "content/public/browser/notification_service.h"
[email protected]9c1662b2012-03-06 15:44:3335#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5836#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2037#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1038#include "content/public/browser/web_contents_view.h"
[email protected]7d478cb2012-07-24 17:19:4239#include "content/public/test/browser_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1240#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5741
42#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]477ae052011-11-18 23:53:5743#include "ui/views/focus/focus_manager.h"
[email protected]5025f862011-11-30 23:35:2044#include "ui/views/view.h"
[email protected]853300a82010-07-27 21:17:5745#endif
initial.commit09911bf2008-07-26 23:55:2946
[email protected]134c47b92009-08-19 03:33:4447#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5448#include "chrome/browser/ui/views/frame/browser_view.h"
[email protected]134c47b92009-08-19 03:33:4449#endif
50
[email protected]a711df102010-12-14 09:47:0351#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0352#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5953#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4054#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0355#endif
56
[email protected]cadaec52012-02-08 21:53:1357using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5058using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1159using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2560using content::WebContents;
61
[email protected]3a3cf4d2011-12-09 00:39:3562#if defined(OS_MACOSX)
63// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4864#define MAYBE_FocusTraversal DISABLED_FocusTraversal
65#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4866#elif defined(OS_WIN)
67// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1868#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4869#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3570#else
71#define MAYBE_FocusTraversal FocusTraversal
72#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
73#endif
74
75#if defined(OS_LINUX) || defined(OS_MACOSX)
76// TODO(jcampan): http://crbug.com/23683 for linux.
77// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3078#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5779#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0780// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2681#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4182#endif
83
initial.commit09911bf2008-07-26 23:55:2984namespace {
85
[email protected]8bcdec92009-02-25 16:15:1886// The delay waited in some cases where we don't have a notifications for an
87// action we take.
initial.commit09911bf2008-07-26 23:55:2988const int kActionDelayMs = 500;
89
[email protected]a9c060ca2012-01-05 20:43:4190// Maxiumum time to wait until the focus is moved to expected view.
91const int kFocusChangeTimeoutMs = 500;
92
[email protected]f72a1cc2010-04-30 07:17:3093const char kSimplePage[] = "files/focus/page_with_focus.html";
94const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
95const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3196const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2997
[email protected]a711df102010-12-14 09:47:0398// Test to make sure Chrome is in the foreground as we start testing. This is
99// required for tests that synthesize input to the Chrome window.
100bool ChromeInForeground() {
101#if defined(OS_WIN)
102 HWND window = ::GetForegroundWindow();
103 std::wstring caption;
104 std::wstring filename;
105 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18106 if (len > 1)
107 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03108 bool chrome_window_in_foreground =
109 EndsWith(caption, L" - Google Chrome", true) ||
110 EndsWith(caption, L" - Chromium", true);
111 if (!chrome_window_in_foreground) {
112 DWORD process_id;
113 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
114
115 base::ProcessHandle process;
116 if (base::OpenProcessHandleWithAccess(process_id,
117 PROCESS_QUERY_LIMITED_INFORMATION,
118 &process)) {
[email protected]fdce4782011-11-29 20:06:18119 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
120 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03121 int error = GetLastError();
122 filename = std::wstring(L"Unable to read filename for process id '" +
123 base::IntToString16(process_id) +
124 L"' (error ") +
125 base::IntToString16(error) + L")";
126 }
127 base::CloseProcessHandle(process);
128 }
129 }
130 EXPECT_TRUE(chrome_window_in_foreground)
131 << "Chrome must be in the foreground when running interactive tests\n"
132 << "Process in foreground: " << filename.c_str() << "\n"
133 << "Window: " << window << "\n"
134 << "Caption: " << caption.c_str();
135 return chrome_window_in_foreground;
136#else
137 // Windows only at the moment.
138 return true;
139#endif
140}
141
[email protected]a9c060ca2012-01-05 20:43:41142// Wait the focus change in message loop.
143void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
144 if (ui_test_utils::IsViewFocused(browser, id) ||
145 base::Time::Now() > timeout) {
146 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
147 } else {
148 MessageLoop::current()->PostDelayedTask(
149 FROM_HERE,
150 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42151 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41152 }
153};
154
[email protected]8bcdec92009-02-25 16:15:18155class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29156 public:
[email protected]0b8fa8b2011-12-07 00:54:52157 BrowserFocusTest() :
158#if defined(USE_AURA)
159 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
160#else
161 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
162#endif
[email protected]90ca44272012-07-18 18:15:48163 {}
[email protected]b9821882009-08-17 22:25:17164
[email protected]21abcc742009-10-23 02:52:06165 bool IsViewFocused(ViewID vid) {
166 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17167 }
168
[email protected]fc2e0872009-08-21 22:14:41169 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06170 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41171 }
[email protected]0b8fa8b2011-12-07 00:54:52172
[email protected]a9c060ca2012-01-05 20:43:41173 bool WaitForFocusChange(ViewID vid) {
174 const base::Time timeout = base::Time::Now() +
175 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
176 MessageLoop::current()->PostDelayedTask(
177 FROM_HERE,
178 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42179 base::TimeDelta::FromMilliseconds(100));
[email protected]729eb632012-07-26 04:45:26180 content::RunMessageLoop();
[email protected]a9c060ca2012-01-05 20:43:41181 return IsViewFocused(vid);
182 }
183
[email protected]0b8fa8b2011-12-07 00:54:52184 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29185};
186
[email protected]85f0a572012-02-07 22:20:13187class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37188 public:
[email protected]85f0a572012-02-07 22:20:13189 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]b65de8b92009-09-14 19:36:31190 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37191 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
192 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31193 file_path = file_path.AppendASCII("focus");
194 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37195 r = file_util::ReadFileToString(file_path, &html_contents_);
196 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13197 interstitial_page_ = InterstitialPage::Create(
198 tab, new_navigation, url , this);
199 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37200 }
201
202 virtual std::string GetHTMLContents() {
203 return html_contents_;
204 }
205
[email protected]85f0a572012-02-07 22:20:13206 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13207 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37208 }
209
[email protected]85f0a572012-02-07 22:20:13210 void DontProceed() {
211 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21212 }
213
[email protected]9e0c83a2009-05-06 19:44:37214 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58215 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37216 }
217
218 private:
219 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13220 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37221};
[email protected]b9821882009-08-17 22:25:17222
[email protected]c39e233d2012-05-18 13:24:12223// Flaky on mac. http://crbug.com/67301.
224#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23225#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12226#else
[email protected]839e0802012-05-18 14:45:23227#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12228#endif
229IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30230 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42231#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41232 // It seems we have to wait a little bit for the widgets to spin up before
233 // we can start clicking on them.
[email protected]7e560102012-03-08 20:58:42234 MessageLoop::current()->PostDelayedTask(
235 FROM_HERE,
236 MessageLoop::QuitClosure(),
237 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26238 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42239#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41240
[email protected]0b8fa8b2011-12-07 00:54:52241 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00242
[email protected]fc2e0872009-08-21 22:14:41243 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21244 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00245
[email protected]fc2e0872009-08-21 22:14:41246 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52247 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00248}
[email protected]186f13f2009-08-19 20:34:00249
[email protected]320948e02011-01-10 08:21:12250// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26251IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30252 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11253 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29254
255 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11256 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18257 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29258
[email protected]90556dd2012-06-07 20:26:18259 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00260
initial.commit09911bf2008-07-26 23:55:29261 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21262 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29263 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57264 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29265 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21266 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29267
[email protected]a37d4b02012-06-25 21:56:10268 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52269 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29270 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57271 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29272 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52273 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29274
[email protected]186f13f2009-08-19 20:34:00275 // The rest of this test does not make sense on Linux because the behavior
276 // of Activate() is not well defined and can vary by window manager.
277#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29278 // Open a new browser window.
[email protected]a4fe67012012-07-25 20:14:29279 Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile()));
[email protected]8bcdec92009-02-25 16:15:18280 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10281 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18282 browser2->window()->Show();
283 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29284
[email protected]90556dd2012-06-07 20:26:18285 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow();
[email protected]4a507a62009-05-28 00:10:00286 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16287 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18288 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50289 const views::Widget* widget2 =
290 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10291 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50292 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18293 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54294 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38295 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29296
297 // Switch to the 1st browser window, focus should still be on the location
298 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18299 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52300 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18301 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29302
303 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18304 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10305 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
306 ASSERT_TRUE(widget);
307 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54308 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38309 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18310
311 // Close the 2nd browser to avoid a DCHECK().
312 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00313#endif
initial.commit09911bf2008-07-26 23:55:29314}
315
316// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39317// Disabled, http://crbug.com/62542.
318IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30319 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11320 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29321
322 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11323 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18324 ui_test_utils::NavigateToURL(browser(), url);
325
initial.commit09911bf2008-07-26 23:55:29326 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03327 for (int i = 0; i < 4; ++i) {
328 chrome::AddSelectedTabWithURL(browser(), url,
329 content::PAGE_TRANSITION_TYPED);
330 }
initial.commit09911bf2008-07-26 23:55:29331
332 // Alternate focus for the tab.
333 const bool kFocusPage[3][5] = {
334 { true, true, true, true, false },
335 { false, false, false, false, false },
336 { false, true, false, true, false }
337 };
338
339 for (int i = 1; i < 3; i++) {
340 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18341 // Activate the tab.
[email protected]57892182012-12-03 19:15:39342 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29343
344 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54345 if (kFocusPage[i][j]) {
[email protected]57892182012-12-03 19:15:39346 browser()->tab_strip_model()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54347 } else {
[email protected]a37d4b02012-06-25 21:56:10348 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54349 }
initial.commit09911bf2008-07-26 23:55:29350 }
351
352 // Now come back to the tab and check the right view is focused.
353 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18354 // Activate the tab.
[email protected]57892182012-12-03 19:15:39355 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29356
[email protected]f2159ba2012-04-17 19:13:21357 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52358 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06359 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29360 }
[email protected]cb7e2542009-12-14 22:02:35361
[email protected]57892182012-12-03 19:15:39362 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35363 // Try the above, but with ctrl+tab. Since tab normally changes focus,
364 // this has regressed in the past. Loop through several times to be sure.
365 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21366 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52367 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35368 ASSERT_TRUE(IsViewFocused(vid));
369
[email protected]1d000682010-08-23 16:21:28370 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09371 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35372 }
373
374 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39375 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35376 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21377 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52378 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35379 ASSERT_TRUE(IsViewFocused(vid));
380
[email protected]1d000682010-08-23 16:21:28381 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09382 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35383 }
initial.commit09911bf2008-07-26 23:55:29384 }
385}
386
[email protected]ae40b572009-10-02 21:17:45387// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35388IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30389 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11390 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45391
392 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11393 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45394 ui_test_utils::NavigateToURL(browser(), url);
395
[email protected]a37d4b02012-06-25 21:56:10396 chrome::Find(browser());
[email protected]ba3d09a12012-11-07 12:13:52397 ui_test_utils::FindInPage(chrome::GetActiveWebContents(browser()),
[email protected]864ebb52012-08-17 17:53:02398 ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06399 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45400
401 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10402 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45403
404 // Create a 2nd tab.
[email protected]52877dbc62012-06-29 22:22:03405 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45406
407 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21408 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45409
410 // Select 1st tab, focus should still be on the location-bar.
411 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39412 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52413 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45414
415 // Now open the find box again, switch to another tab and come back, the focus
416 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10417 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06418 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39419 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21420 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39421 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06422 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45423}
424
initial.commit09911bf2008-07-26 23:55:29425// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07426// Flaky, http://crbug.com/62538.
427IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26428 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30429 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11430 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29431
initial.commit09911bf2008-07-26 23:55:29432 // Open a new browser window.
[email protected]a4fe67012012-07-25 20:14:29433 Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile()));
[email protected]8bcdec92009-02-25 16:15:18434 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10435 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18436 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00437
[email protected]ed179ee2009-10-03 21:02:51438 Browser* focused_browser = NULL;
439 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16440#if defined(USE_X11)
441 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00442 // to figure out which browser does have focus.
443 if (browser2->window()->IsActive()) {
444 focused_browser = browser2;
445 unfocused_browser = browser();
446 } else if (browser()->window()->IsActive()) {
447 focused_browser = browser();
448 unfocused_browser = browser2;
449 } else {
[email protected]579c3d82010-10-06 03:53:51450 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00451 }
452#elif defined(OS_WIN)
453 focused_browser = browser();
454 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57455#elif defined(OS_MACOSX)
456 // On Mac, the newly created window always gets the focus.
457 focused_browser = browser2;
458 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00459#endif
460
[email protected]95409e12010-08-17 20:07:11461 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00462 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46463
[email protected]8bcdec92009-02-25 16:15:18464 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00465 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29466
[email protected]b6987e02013-01-04 18:30:43467 ASSERT_TRUE(content::ExecuteScript(
468 chrome::GetActiveWebContents(unfocused_browser),
[email protected]06bc5d92013-01-02 22:44:13469 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29470
[email protected]8bcdec92009-02-25 16:15:18471 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00472 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29473}
474
475// Page cannot steal focus when focus is on location bar.
[email protected]7ef7c6e2012-10-30 12:32:44476IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30477 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11478 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29479
480 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11481 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18482 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29483
[email protected]a37d4b02012-06-25 21:56:10484 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29485
[email protected]b6987e02013-01-04 18:30:43486 ASSERT_TRUE(content::ExecuteScript(
487 chrome::GetActiveWebContents(browser()),
[email protected]06bc5d92013-01-02 22:44:13488 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29489
490 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52491 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29492}
493
[email protected]9e0c83a2009-05-06 19:44:37494// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35495// Note that this test relies on a notification from the renderer that the
496// focus has changed in the page. The notification in the renderer may change
497// at which point this test would fail (see comment in
498// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57499IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30500 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11501 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29502
[email protected]8bcdec92009-02-25 16:15:18503 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11504 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18505 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29506
[email protected]a37d4b02012-06-25 21:56:10507 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29508
[email protected]546ae4e02010-12-08 14:57:19509 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18510 const char* kExpElementIDs[] = {
511 "", // Initially no element in the page should be focused
512 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19513 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18514 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29515 };
516
517 // Test forward focus traversal.
518 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40519 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29520 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52521 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29522
[email protected]911696b2011-01-28 02:36:49523 // Move the caret to the end, otherwise the next Tab key may not move focus.
524 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
525 browser(), ui::VKEY_END, false, false, false, false));
526
initial.commit09911bf2008-07-26 23:55:29527 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35528 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40529 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29530 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01531 std::string actual;
[email protected]b6987e02013-01-04 18:30:43532 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
533 chrome::GetActiveWebContents(browser()),
[email protected]06bc5d92013-01-02 22:44:13534 "window.domAutomationController.send(getFocusedElement());",
[email protected]45671612009-04-29 22:24:01535 &actual));
initial.commit09911bf2008-07-26 23:55:29536 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
537
[email protected]130efb02009-09-18 18:54:35538 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19539 // If the next element is the kTextElementID, we expect to be
540 // notified we have switched to an editable node.
541 bool is_editable_node =
542 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53543 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19544
545 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09546 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27547 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56548 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]52877dbc62012-06-29 22:22:03549 chrome::GetActiveWebContents(browser())->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19550 details));
[email protected]130efb02009-09-18 18:54:35551 } else {
552 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19553 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09554 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27555 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53556 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35557 }
initial.commit09911bf2008-07-26 23:55:29558 }
[email protected]8bcdec92009-02-25 16:15:18559
560 // At this point the renderer has sent us a message asking to advance the
561 // focus (as the end of the focus loop was reached in the renderer).
562 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44563 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29564 }
565
566 // Now let's try reverse focus traversal.
567 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40568 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29569 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52570 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29571
[email protected]911696b2011-01-28 02:36:49572 // Move the caret to the end, otherwise the next Tab key may not move focus.
573 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
574 browser(), ui::VKEY_END, false, false, false, false));
575
[email protected]8bcdec92009-02-25 16:15:18576 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30577 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40578 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19579 const char* next_element =
580 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35581
582 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19583 // If the next element is the kTextElementID, we expect to be
584 // notified we have switched to an editable node.
585 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53586 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19587
588 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09589 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27590 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56591 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]52877dbc62012-06-29 22:22:03592 chrome::GetActiveWebContents(browser())->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19593 details));
[email protected]130efb02009-09-18 18:54:35594 } else {
595 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19596 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09597 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27598 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53599 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35600 }
initial.commit09911bf2008-07-26 23:55:29601
602 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01603 std::string actual;
[email protected]b6987e02013-01-04 18:30:43604 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
605 chrome::GetActiveWebContents(browser()),
[email protected]06bc5d92013-01-02 22:44:13606 "window.domAutomationController.send(getFocusedElement());",
[email protected]45671612009-04-29 22:24:01607 &actual));
[email protected]546ae4e02010-12-08 14:57:19608 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29609 }
[email protected]8bcdec92009-02-25 16:15:18610
611 // At this point the renderer has sent us a message asking to advance the
612 // focus (as the end of the focus loop was reached in the renderer).
613 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44614 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29615 }
616}
617
[email protected]9e0c83a2009-05-06 19:44:37618// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41619IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30620 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11621 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37622
623 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11624 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37625 ui_test_utils::NavigateToURL(browser(), url);
626
[email protected]9e0c83a2009-05-06 19:44:37627 // Focus should be on the page.
[email protected]f2159ba2012-04-17 19:13:21628 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37629
630 // Let's show an interstitial.
631 TestInterstitialPage* interstitial_page =
[email protected]52877dbc62012-06-29 22:22:03632 new TestInterstitialPage(chrome::GetActiveWebContents(browser()),
[email protected]9e0c83a2009-05-06 19:44:37633 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37634 // Give some time for the interstitial to show.
635 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17636 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42637 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26638 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37639
[email protected]a37d4b02012-06-25 21:56:10640 chrome::FocusLocationBar(browser());
[email protected]9e0c83a2009-05-06 19:44:37641
642 const char* kExpElementIDs[] = {
643 "", // Initially no element in the page should be focused
644 // (the location bar is focused).
645 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
646 "gmapLink"
647 };
648
649 // Test forward focus traversal.
650 for (int i = 0; i < 2; ++i) {
651 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52652 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37653
[email protected]911696b2011-01-28 02:36:49654 // Move the caret to the end, otherwise the next Tab key may not move focus.
655 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
656 browser(), ui::VKEY_END, false, false, false, false));
657
[email protected]9e0c83a2009-05-06 19:44:37658 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35659 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37660 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30661 std::string actual;
[email protected]b6987e02013-01-04 18:30:43662 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]06bc5d92013-01-02 22:44:13663 interstitial_page->render_view_host(),
[email protected]06bc5d92013-01-02 22:44:13664 "window.domAutomationController.send(getFocusedElement());",
[email protected]a6e602f2010-09-28 22:28:30665 &actual));
[email protected]9e0c83a2009-05-06 19:44:37666 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
667
[email protected]432115822011-07-10 15:52:27668 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53669 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07670 content::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]d53092652011-12-06 06:12:56673 notification_source = content::Source<RenderViewHost>(
674 interstitial_page->render_view_host());
[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]6c2381d2011-10-19 02:52:53678 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35679 }
[email protected]a6e602f2010-09-28 22:28:30680
681 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09682 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30683 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37684 }
685
686 // At this point the renderer has sent us a message asking to advance the
687 // focus (as the end of the focus loop was reached in the renderer).
688 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44689 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37690 }
691
692 // Now let's try reverse focus traversal.
693 for (int i = 0; i < 2; ++i) {
694 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52695 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37696
[email protected]911696b2011-01-28 02:36:49697 // Move the caret to the end, otherwise the next Tab key may not move focus.
698 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
699 browser(), ui::VKEY_END, false, false, false, false));
700
[email protected]9e0c83a2009-05-06 19:44:37701 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35702 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27703 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53704 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07705 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35706 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27707 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56708 notification_source = content::Source<RenderViewHost>(
709 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35710 } else {
711 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27712 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53713 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35714 }
[email protected]9e0c83a2009-05-06 19:44:37715
[email protected]a6e602f2010-09-28 22:28:30716 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09717 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30718 notification_type, notification_source));
719
[email protected]9e0c83a2009-05-06 19:44:37720 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30721 std::string actual;
[email protected]b6987e02013-01-04 18:30:43722 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]06bc5d92013-01-02 22:44:13723 interstitial_page->render_view_host(),
[email protected]06bc5d92013-01-02 22:44:13724 "window.domAutomationController.send(getFocusedElement());",
[email protected]a6e602f2010-09-28 22:28:30725 &actual));
[email protected]9e0c83a2009-05-06 19:44:37726 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
727 }
728
729 // At this point the renderer has sent us a message asking to advance the
730 // focus (as the end of the focus loop was reached in the renderer).
731 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44732 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37733 }
734}
735
736// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33737// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05738#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]4d2451652012-02-14 23:54:26739IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33740#else
[email protected]e4f4e0b2009-10-13 19:58:21741IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33742#endif
[email protected]a6e602f2010-09-28 22:28:30743 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11744 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37745
746 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11747 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37748 ui_test_utils::NavigateToURL(browser(), url);
749
[email protected]9e0c83a2009-05-06 19:44:37750 // Page should have focus.
[email protected]f2159ba2012-04-17 19:13:21751 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]52877dbc62012-06-29 22:22:03752 EXPECT_TRUE(chrome::GetActiveWebContents(browser())->GetRenderViewHost()->
[email protected]9f76c1e2012-03-05 15:15:58753 GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37754
[email protected]9d8a4642009-07-29 17:25:30755 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37756 TestInterstitialPage* interstitial_page =
[email protected]52877dbc62012-06-29 22:22:03757 new TestInterstitialPage(chrome::GetActiveWebContents(browser()),
[email protected]9e0c83a2009-05-06 19:44:37758 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37759 // Give some time for the interstitial to show.
760 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17761 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42762 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26763 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37764
765 // The interstitial should have focus now.
[email protected]f2159ba2012-04-17 19:13:21766 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37767 EXPECT_TRUE(interstitial_page->HasFocus());
768
769 // Hide the interstitial.
770 interstitial_page->DontProceed();
771
772 // Focus should be back on the original page.
[email protected]f2159ba2012-04-17 19:13:21773 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37774}
775
[email protected]9bd491ee2008-12-10 22:31:07776// Make sure Find box can request focus, even when it is already open.
[email protected]06e85dfd2012-10-31 01:32:35777// Disabled due to flakiness. http://crbug.com/67301.
778IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30779 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11780 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55781
[email protected]9bd491ee2008-12-10 22:31:07782 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11783 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18784 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07785
[email protected]a711df102010-12-14 09:47:03786 EXPECT_TRUE(ChromeInForeground());
787
[email protected]853300a82010-07-27 21:17:57788#if defined(OS_MACOSX)
789 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28790 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09791 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57792#else
[email protected]9bd491ee2008-12-10 22:31:07793 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28794 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09795 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57796#endif
[email protected]8bcdec92009-02-25 16:15:18797
[email protected]a9c060ca2012-01-05 20:43:41798 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07799
[email protected]a37d4b02012-06-25 21:56:10800 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52801 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07802
803 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57804#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28805 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09806 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57807#else
[email protected]1d000682010-08-23 16:21:28808 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09809 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57810#endif
[email protected]21abcc742009-10-23 02:52:06811 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07812
813 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41814 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21815 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07816
817 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57818#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28819 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09820 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57821#else
[email protected]1d000682010-08-23 16:21:28822 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09823 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57824#endif
[email protected]8bcdec92009-02-25 16:15:18825
[email protected]a9c060ca2012-01-05 20:43:41826 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07827}
[email protected]401513c2009-03-12 00:21:28828
829// Makes sure the focus is in the right location when opening the different
830// types of tabs.
[email protected]c92928672010-11-09 18:31:07831// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26832IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30833 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07834
[email protected]401513c2009-03-12 00:21:28835 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12836 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35837 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03838 chrome::GetActiveWebContents(browser())));
[email protected]f2159ba2012-04-17 19:13:21839 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28840
841 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10842 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35843 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03844 chrome::GetActiveWebContents(browser())));
[email protected]0b8fa8b2011-12-07 00:54:52845 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28846
847 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12848 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35849 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03850 chrome::GetActiveWebContents(browser())));
[email protected]f2159ba2012-04-17 19:13:21851 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43852
853 // Open about:blank, focus should be on the location bar.
[email protected]52877dbc62012-06-29 22:22:03854 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL),
855 content::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35856 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03857 chrome::GetActiveWebContents(browser())));
[email protected]0b8fa8b2011-12-07 00:54:52858 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28859}
[email protected]9d8a4642009-07-29 17:25:30860
861// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21862IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30863 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11864 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30865
[email protected]9d8a4642009-07-29 17:25:30866 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06867 {
[email protected]a7fe9112012-07-20 02:34:45868 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06869 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07870 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10871 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06872 observer.Wait();
873 }
[email protected]b8deecd2012-07-30 21:09:44874 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42875
[email protected]6b4e8e42011-08-17 19:36:06876 {
[email protected]a7fe9112012-07-20 02:34:45877 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06878 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50879 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03880 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10881 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06882 observer.Wait();
883 }
[email protected]9d8a4642009-07-29 17:25:30884 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52885 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30886
887 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11888 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10889 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52890 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06891 {
[email protected]a7fe9112012-07-20 02:34:45892 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06893 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50894 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03895 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10896 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06897 observer.Wait();
898 }
[email protected]9ba21ede2010-07-30 01:11:07899
[email protected]9d8a4642009-07-29 17:25:30900 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12901 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21902 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30903}
904
905// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27906IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30907 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11908 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30909
[email protected]9d8a4642009-07-29 17:25:30910 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11911 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]e0f3e142012-07-26 03:31:34912 content::CrashTab(chrome::GetActiveWebContents(browser()));
[email protected]6b4e8e42011-08-17 19:36:06913 {
[email protected]a7fe9112012-07-20 02:34:45914 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06915 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50916 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03917 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10918 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06919 observer.Wait();
920 }
[email protected]9ba21ede2010-07-30 01:11:07921
[email protected]9d8a4642009-07-29 17:25:30922 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12923 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21924 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30925}
[email protected]629e0342010-07-27 23:30:13926
[email protected]895a84722012-11-29 18:45:44927// Tests that when a new tab is opened from the omnibox, the focus is moved from
928// the omnibox for the current tab.
929IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
930 NavigateFromOmniboxIntoNewTab) {
931 GURL url("http://www.google.com/");
932 GURL url2("http://maps.google.com/");
933
934 // Navigate to url.
935 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK);
936 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
937 p.disposition = CURRENT_TAB;
938 chrome::Navigate(&p);
939
940 // Focus the omnibox.
941 chrome::FocusLocationBar(browser());
942
943 OmniboxEditController* controller =
944 browser()->window()->GetLocationBar()->GetLocationEntry()->model()->
945 controller();
946
947 // Simulate an alt-enter.
948 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
949 content::PAGE_TRANSITION_TYPED, GURL());
950
951 // Make sure the second tab is selected.
952 EXPECT_EQ(1, browser()->active_index());
953
954 // The tab contents should have the focus in the second tab.
955 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
956
957 // Go back to the first tab. The focus should not be in the omnibox.
958 chrome::SelectPreviousTab(browser());
959 EXPECT_EQ(0, browser()->active_index());
960 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(),
961 VIEW_ID_LOCATION_BAR));
962}
963
[email protected]629e0342010-07-27 23:30:13964} // namespace