blob: e83c347929379821ba25b5305d8299e46f116d7b [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]3ea1b182013-02-08 22:38:4112#include "base/strings/string_number_conversions.h"
[email protected]11521182013-06-11 04:06:3613#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1214#include "base/strings/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]af44e7fb2011-07-29 18:32:3229#include "chrome/test/base/in_process_browser_test.h"
[email protected]3ea1b182013-02-08 22:38:4130#include "chrome/test/base/interactive_test_utils.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]89b32522013-05-07 20:04:2140#include "net/test/spawned_test_server/spawned_test_server.h"
[email protected]853300a82010-07-27 21:17:5741
[email protected]134c47b92009-08-19 03:33:4442#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5443#include "chrome/browser/ui/views/frame/browser_view.h"
[email protected]5c4e4482013-03-03 00:09:1844#include "ui/views/focus/focus_manager.h"
45#include "ui/views/view.h"
[email protected]134c47b92009-08-19 03:33:4446#endif
47
[email protected]a711df102010-12-14 09:47:0348#if defined(OS_WIN)
[email protected]93270d002011-01-19 22:32:5949#include <windows.h>
[email protected]3ea1b182013-02-08 22:38:4150#include <Psapi.h>
[email protected]11521182013-06-11 04:06:3651#include "base/strings/string_util.h"
[email protected]a711df102010-12-14 09:47:0352#endif
53
[email protected]cadaec52012-02-08 21:53:1354using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5055using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1156using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2557using content::WebContents;
58
[email protected]3a3cf4d2011-12-09 00:39:3559#if defined(OS_MACOSX)
60// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4861#define MAYBE_FocusTraversal DISABLED_FocusTraversal
62#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]5b6d0432013-01-24 20:30:1663#elif defined(OS_WIN) || defined(OS_CHROMEOS)
64// http://crbug.com/109770 and http://crbug.com/62544
[email protected]2f2bf1172012-01-10 22:17:1865#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4866#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3567#else
68#define MAYBE_FocusTraversal FocusTraversal
69#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
70#endif
71
72#if defined(OS_LINUX) || defined(OS_MACOSX)
73// TODO(jcampan): http://crbug.com/23683 for linux.
74// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3075#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5776#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0777// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2678#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4179#endif
80
initial.commit09911bf2008-07-26 23:55:2981namespace {
82
[email protected]8bcdec92009-02-25 16:15:1883// The delay waited in some cases where we don't have a notifications for an
84// action we take.
initial.commit09911bf2008-07-26 23:55:2985const int kActionDelayMs = 500;
86
[email protected]a9c060ca2012-01-05 20:43:4187// Maxiumum time to wait until the focus is moved to expected view.
88const int kFocusChangeTimeoutMs = 500;
89
[email protected]f72a1cc2010-04-30 07:17:3090const char kSimplePage[] = "files/focus/page_with_focus.html";
91const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
92const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3193const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2994
[email protected]a711df102010-12-14 09:47:0395// Test to make sure Chrome is in the foreground as we start testing. This is
96// required for tests that synthesize input to the Chrome window.
97bool ChromeInForeground() {
98#if defined(OS_WIN)
99 HWND window = ::GetForegroundWindow();
100 std::wstring caption;
101 std::wstring filename;
102 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18103 if (len > 1)
104 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03105 bool chrome_window_in_foreground =
106 EndsWith(caption, L" - Google Chrome", true) ||
107 EndsWith(caption, L" - Chromium", true);
108 if (!chrome_window_in_foreground) {
109 DWORD process_id;
110 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
111
112 base::ProcessHandle process;
113 if (base::OpenProcessHandleWithAccess(process_id,
114 PROCESS_QUERY_LIMITED_INFORMATION,
115 &process)) {
[email protected]fdce4782011-11-29 20:06:18116 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
117 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03118 int error = GetLastError();
119 filename = std::wstring(L"Unable to read filename for process id '" +
120 base::IntToString16(process_id) +
121 L"' (error ") +
122 base::IntToString16(error) + L")";
123 }
124 base::CloseProcessHandle(process);
125 }
126 }
127 EXPECT_TRUE(chrome_window_in_foreground)
128 << "Chrome must be in the foreground when running interactive tests\n"
129 << "Process in foreground: " << filename.c_str() << "\n"
130 << "Window: " << window << "\n"
131 << "Caption: " << caption.c_str();
132 return chrome_window_in_foreground;
133#else
134 // Windows only at the moment.
135 return true;
136#endif
137}
138
[email protected]a9c060ca2012-01-05 20:43:41139// Wait the focus change in message loop.
140void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
141 if (ui_test_utils::IsViewFocused(browser, id) ||
142 base::Time::Now() > timeout) {
[email protected]3264d162013-05-08 22:02:11143 base::MessageLoop::current()->PostTask(FROM_HERE,
144 base::MessageLoop::QuitClosure());
[email protected]a9c060ca2012-01-05 20:43:41145 } else {
[email protected]3264d162013-05-08 22:02:11146 base::MessageLoop::current()->PostDelayedTask(
[email protected]a9c060ca2012-01-05 20:43:41147 FROM_HERE,
148 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42149 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41150 }
151};
152
[email protected]8bcdec92009-02-25 16:15:18153class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29154 public:
[email protected]21abcc742009-10-23 02:52:06155 bool IsViewFocused(ViewID vid) {
156 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17157 }
158
[email protected]fc2e0872009-08-21 22:14:41159 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06160 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41161 }
[email protected]0b8fa8b2011-12-07 00:54:52162
[email protected]a9c060ca2012-01-05 20:43:41163 bool WaitForFocusChange(ViewID vid) {
164 const base::Time timeout = base::Time::Now() +
165 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
[email protected]3264d162013-05-08 22:02:11166 base::MessageLoop::current()->PostDelayedTask(
[email protected]a9c060ca2012-01-05 20:43:41167 FROM_HERE,
168 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42169 base::TimeDelta::FromMilliseconds(100));
[email protected]729eb632012-07-26 04:45:26170 content::RunMessageLoop();
[email protected]a9c060ca2012-01-05 20:43:41171 return IsViewFocused(vid);
172 }
initial.commit09911bf2008-07-26 23:55:29173};
174
[email protected]85f0a572012-02-07 22:20:13175class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37176 public:
[email protected]85f0a572012-02-07 22:20:13177 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]650b2d52013-02-10 03:41:45178 base::FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37179 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
180 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31181 file_path = file_path.AppendASCII("focus");
182 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37183 r = file_util::ReadFileToString(file_path, &html_contents_);
184 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13185 interstitial_page_ = InterstitialPage::Create(
186 tab, new_navigation, url , this);
187 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37188 }
189
[email protected]626cc902013-02-06 18:30:53190 virtual std::string GetHTMLContents() OVERRIDE {
[email protected]9e0c83a2009-05-06 19:44:37191 return html_contents_;
192 }
193
[email protected]85f0a572012-02-07 22:20:13194 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13195 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37196 }
197
[email protected]85f0a572012-02-07 22:20:13198 void DontProceed() {
199 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21200 }
201
[email protected]9e0c83a2009-05-06 19:44:37202 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58203 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37204 }
205
206 private:
207 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13208 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37209};
[email protected]b9821882009-08-17 22:25:17210
[email protected]c39e233d2012-05-18 13:24:12211// Flaky on mac. http://crbug.com/67301.
212#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23213#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12214#else
[email protected]839e0802012-05-18 14:45:23215#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12216#endif
217IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30218 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42219#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41220 // It seems we have to wait a little bit for the widgets to spin up before
221 // we can start clicking on them.
[email protected]3264d162013-05-08 22:02:11222 base::MessageLoop::current()->PostDelayedTask(
[email protected]7e560102012-03-08 20:58:42223 FROM_HERE,
[email protected]3264d162013-05-08 22:02:11224 base::MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42225 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26226 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42227#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41228
[email protected]5c4e4482013-03-03 00:09:18229 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00230
[email protected]fc2e0872009-08-21 22:14:41231 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21232 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00233
[email protected]5c4e4482013-03-03 00:09:18234 ClickOnView(VIEW_ID_OMNIBOX);
235 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00236}
[email protected]186f13f2009-08-19 20:34:00237
[email protected]320948e02011-01-10 08:21:12238// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26239IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30240 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11241 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29242
243 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11244 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18245 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29246
[email protected]90556dd2012-06-07 20:26:18247 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00248
initial.commit09911bf2008-07-26 23:55:29249 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21250 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29251 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57252 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29253 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21254 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29255
[email protected]a37d4b02012-06-25 21:56:10256 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18257 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29258 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57259 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29260 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18261 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29262
[email protected]186f13f2009-08-19 20:34:00263 // The rest of this test does not make sense on Linux because the behavior
264 // of Activate() is not well defined and can vary by window manager.
265#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29266 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21267 Browser* browser2 =
268 new Browser(Browser::CreateParams(browser()->profile(),
269 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18270 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10271 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18272 browser2->window()->Show();
273 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29274
[email protected]90556dd2012-06-07 20:26:18275 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow();
[email protected]4a507a62009-05-28 00:10:00276 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16277 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18278 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50279 const views::Widget* widget2 =
280 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10281 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50282 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18283 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54284 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38285 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29286
287 // Switch to the 1st browser window, focus should still be on the location
288 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18289 browser()->window()->Activate();
[email protected]5c4e4482013-03-03 00:09:18290 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]8bcdec92009-02-25 16:15:18291 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29292
293 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18294 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10295 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
296 ASSERT_TRUE(widget);
297 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54298 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38299 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18300
301 // Close the 2nd browser to avoid a DCHECK().
302 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00303#endif
initial.commit09911bf2008-07-26 23:55:29304}
305
306// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39307// Disabled, http://crbug.com/62542.
308IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30309 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11310 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29311
312 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11313 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18314 ui_test_utils::NavigateToURL(browser(), url);
315
initial.commit09911bf2008-07-26 23:55:29316 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03317 for (int i = 0; i < 4; ++i) {
318 chrome::AddSelectedTabWithURL(browser(), url,
319 content::PAGE_TRANSITION_TYPED);
320 }
initial.commit09911bf2008-07-26 23:55:29321
322 // Alternate focus for the tab.
323 const bool kFocusPage[3][5] = {
324 { true, true, true, true, false },
325 { false, false, false, false, false },
326 { false, true, false, true, false }
327 };
328
329 for (int i = 1; i < 3; i++) {
330 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18331 // Activate the tab.
[email protected]57892182012-12-03 19:15:39332 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29333
334 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54335 if (kFocusPage[i][j]) {
[email protected]57892182012-12-03 19:15:39336 browser()->tab_strip_model()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54337 } else {
[email protected]a37d4b02012-06-25 21:56:10338 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54339 }
initial.commit09911bf2008-07-26 23:55:29340 }
341
342 // Now come back to the tab and check the right view is focused.
343 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18344 // Activate the tab.
[email protected]57892182012-12-03 19:15:39345 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29346
[email protected]5c4e4482013-03-03 00:09:18347 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06348 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29349 }
[email protected]cb7e2542009-12-14 22:02:35350
[email protected]57892182012-12-03 19:15:39351 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35352 // Try the above, but with ctrl+tab. Since tab normally changes focus,
353 // this has regressed in the past. Loop through several times to be sure.
354 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21355 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18356 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35357 ASSERT_TRUE(IsViewFocused(vid));
358
[email protected]1d000682010-08-23 16:21:28359 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09360 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35361 }
362
363 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39364 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35365 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21366 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18367 VIEW_ID_OMNIBOX;
[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, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35372 }
initial.commit09911bf2008-07-26 23:55:29373 }
374}
375
[email protected]ae40b572009-10-02 21:17:45376// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35377IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[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());
[email protected]ae40b572009-10-02 21:17:45380
381 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11382 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45383 ui_test_utils::NavigateToURL(browser(), url);
384
[email protected]a37d4b02012-06-25 21:56:10385 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07386 ui_test_utils::FindInPage(
387 browser()->tab_strip_model()->GetActiveWebContents(),
388 ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06389 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45390
391 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10392 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45393
394 // Create a 2nd tab.
[email protected]52877dbc62012-06-29 22:22:03395 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45396
397 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21398 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45399
400 // Select 1st tab, focus should still be on the location-bar.
401 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39402 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18403 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45404
405 // Now open the find box again, switch to another tab and come back, the focus
406 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10407 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06408 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39409 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21410 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39411 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06412 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45413}
414
initial.commit09911bf2008-07-26 23:55:29415// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07416// Flaky, http://crbug.com/62538.
417IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26418 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30419 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11420 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29421
initial.commit09911bf2008-07-26 23:55:29422 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21423 Browser* browser2 =
424 new Browser(Browser::CreateParams(browser()->profile(),
425 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18426 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10427 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18428 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00429
[email protected]ed179ee2009-10-03 21:02:51430 Browser* focused_browser = NULL;
431 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16432#if defined(USE_X11)
433 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00434 // to figure out which browser does have focus.
435 if (browser2->window()->IsActive()) {
436 focused_browser = browser2;
437 unfocused_browser = browser();
438 } else if (browser()->window()->IsActive()) {
439 focused_browser = browser();
440 unfocused_browser = browser2;
441 } else {
[email protected]579c3d82010-10-06 03:53:51442 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00443 }
444#elif defined(OS_WIN)
445 focused_browser = browser();
446 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57447#elif defined(OS_MACOSX)
448 // On Mac, the newly created window always gets the focus.
449 focused_browser = browser2;
450 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00451#endif
452
[email protected]95409e12010-08-17 20:07:11453 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00454 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46455
[email protected]8bcdec92009-02-25 16:15:18456 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00457 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29458
[email protected]b6987e02013-01-04 18:30:43459 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05460 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13461 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29462
[email protected]8bcdec92009-02-25 16:15:18463 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00464 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29465}
466
467// Page cannot steal focus when focus is on location bar.
[email protected]7ef7c6e2012-10-30 12:32:44468IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30469 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11470 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29471
472 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11473 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18474 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29475
[email protected]a37d4b02012-06-25 21:56:10476 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29477
[email protected]b6987e02013-01-04 18:30:43478 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07479 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13480 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29481
482 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18483 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29484}
485
[email protected]9e0c83a2009-05-06 19:44:37486// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35487// Note that this test relies on a notification from the renderer that the
488// focus has changed in the page. The notification in the renderer may change
489// at which point this test would fail (see comment in
490// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57491IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30492 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11493 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29494
[email protected]8bcdec92009-02-25 16:15:18495 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11496 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18497 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29498
[email protected]a37d4b02012-06-25 21:56:10499 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29500
[email protected]546ae4e02010-12-08 14:57:19501 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18502 const char* kExpElementIDs[] = {
503 "", // Initially no element in the page should be focused
504 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19505 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18506 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29507 };
508
509 // Test forward focus traversal.
510 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40511 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29512 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18513 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29514
[email protected]911696b2011-01-28 02:36:49515 // Move the caret to the end, otherwise the next Tab key may not move focus.
516 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
517 browser(), ui::VKEY_END, false, false, false, false));
518
initial.commit09911bf2008-07-26 23:55:29519 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35520 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40521 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29522 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01523 std::string actual;
[email protected]b6987e02013-01-04 18:30:43524 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]cc872372013-01-28 21:57:07525 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13526 "window.domAutomationController.send(getFocusedElement());",
[email protected]45671612009-04-29 22:24:01527 &actual));
initial.commit09911bf2008-07-26 23:55:29528 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
529
[email protected]130efb02009-09-18 18:54:35530 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19531 // If the next element is the kTextElementID, we expect to be
532 // notified we have switched to an editable node.
533 bool is_editable_node =
534 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53535 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19536
537 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09538 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27539 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56540 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]cc872372013-01-28 21:57:07541 browser()->tab_strip_model()->GetActiveWebContents()->
542 GetRenderViewHost())),
[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, false, false, false,
[email protected]432115822011-07-10 15:52:27548 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53549 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35550 }
initial.commit09911bf2008-07-26 23:55:29551 }
[email protected]8bcdec92009-02-25 16:15:18552
553 // At this point the renderer has sent us a message asking to advance the
554 // focus (as the end of the focus loop was reached in the renderer).
555 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44556 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29557 }
558
559 // Now let's try reverse focus traversal.
560 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40561 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29562 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18563 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29564
[email protected]911696b2011-01-28 02:36:49565 // Move the caret to the end, otherwise the next Tab key may not move focus.
566 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
567 browser(), ui::VKEY_END, false, false, false, false));
568
[email protected]8bcdec92009-02-25 16:15:18569 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30570 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40571 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19572 const char* next_element =
573 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35574
575 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19576 // If the next element is the kTextElementID, we expect to be
577 // notified we have switched to an editable node.
578 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53579 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19580
581 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09582 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27583 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56584 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]cc872372013-01-28 21:57:07585 browser()->tab_strip_model()->GetActiveWebContents()->
586 GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19587 details));
[email protected]130efb02009-09-18 18:54:35588 } else {
589 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19590 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09591 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27592 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53593 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35594 }
initial.commit09911bf2008-07-26 23:55:29595
596 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01597 std::string actual;
[email protected]b6987e02013-01-04 18:30:43598 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]cc872372013-01-28 21:57:07599 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13600 "window.domAutomationController.send(getFocusedElement());",
[email protected]45671612009-04-29 22:24:01601 &actual));
[email protected]546ae4e02010-12-08 14:57:19602 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29603 }
[email protected]8bcdec92009-02-25 16:15:18604
605 // At this point the renderer has sent us a message asking to advance the
606 // focus (as the end of the focus loop was reached in the renderer).
607 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44608 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29609 }
610}
611
[email protected]9e0c83a2009-05-06 19:44:37612// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41613IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30614 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11615 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37616
617 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11618 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37619 ui_test_utils::NavigateToURL(browser(), url);
620
[email protected]9e0c83a2009-05-06 19:44:37621 // Focus should be on the page.
[email protected]f2159ba2012-04-17 19:13:21622 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37623
624 // Let's show an interstitial.
[email protected]cc872372013-01-28 21:57:07625 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
626 browser()->tab_strip_model()->GetActiveWebContents(),
627 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37628 // Give some time for the interstitial to show.
[email protected]3264d162013-05-08 22:02:11629 base::MessageLoop::current()->PostDelayedTask(
630 FROM_HERE,
631 base::MessageLoop::QuitClosure(),
632 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26633 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37634
[email protected]a37d4b02012-06-25 21:56:10635 chrome::FocusLocationBar(browser());
[email protected]9e0c83a2009-05-06 19:44:37636
637 const char* kExpElementIDs[] = {
638 "", // Initially no element in the page should be focused
639 // (the location bar is focused).
640 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
641 "gmapLink"
642 };
643
644 // Test forward focus traversal.
645 for (int i = 0; i < 2; ++i) {
646 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18647 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[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 tab to move the focus.
[email protected]130efb02009-09-18 18:54:35654 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37655 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30656 std::string actual;
[email protected]b6987e02013-01-04 18:30:43657 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]06bc5d92013-01-02 22:44:13658 interstitial_page->render_view_host(),
[email protected]06bc5d92013-01-02 22:44:13659 "window.domAutomationController.send(getFocusedElement());",
[email protected]a6e602f2010-09-28 22:28:30660 &actual));
[email protected]9e0c83a2009-05-06 19:44:37661 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
662
[email protected]432115822011-07-10 15:52:27663 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53664 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07665 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35666 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27667 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56668 notification_source = content::Source<RenderViewHost>(
669 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35670 } else {
671 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27672 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53673 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35674 }
[email protected]a6e602f2010-09-28 22:28:30675
676 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09677 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30678 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37679 }
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]b8deecd2012-07-30 21:09:44684 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37685 }
686
687 // Now let's try reverse focus traversal.
688 for (int i = 0; i < 2; ++i) {
689 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18690 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9e0c83a2009-05-06 19:44:37691
[email protected]911696b2011-01-28 02:36:49692 // Move the caret to the end, otherwise the next Tab key may not move focus.
693 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
694 browser(), ui::VKEY_END, false, false, false, false));
695
[email protected]9e0c83a2009-05-06 19:44:37696 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35697 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27698 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53699 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07700 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35701 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27702 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56703 notification_source = content::Source<RenderViewHost>(
704 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35705 } else {
706 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27707 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53708 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35709 }
[email protected]9e0c83a2009-05-06 19:44:37710
[email protected]a6e602f2010-09-28 22:28:30711 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09712 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30713 notification_type, notification_source));
714
[email protected]9e0c83a2009-05-06 19:44:37715 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30716 std::string actual;
[email protected]b6987e02013-01-04 18:30:43717 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]06bc5d92013-01-02 22:44:13718 interstitial_page->render_view_host(),
[email protected]06bc5d92013-01-02 22:44:13719 "window.domAutomationController.send(getFocusedElement());",
[email protected]a6e602f2010-09-28 22:28:30720 &actual));
[email protected]9e0c83a2009-05-06 19:44:37721 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
722 }
723
724 // At this point the renderer has sent us a message asking to advance the
725 // focus (as the end of the focus loop was reached in the renderer).
726 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44727 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37728 }
729}
730
731// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33732// http://crbug.com/81451
[email protected]4d2451652012-02-14 23:54:26733IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30734 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11735 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37736
737 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11738 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37739 ui_test_utils::NavigateToURL(browser(), url);
740
[email protected]9e0c83a2009-05-06 19:44:37741 // Page should have focus.
[email protected]f2159ba2012-04-17 19:13:21742 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]cc872372013-01-28 21:57:07743 EXPECT_TRUE(browser()->tab_strip_model()->GetActiveWebContents()->
744 GetRenderViewHost()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37745
[email protected]9d8a4642009-07-29 17:25:30746 // Let's show an interstitial.
[email protected]cc872372013-01-28 21:57:07747 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
748 browser()->tab_strip_model()->GetActiveWebContents(),
749 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37750 // Give some time for the interstitial to show.
[email protected]3264d162013-05-08 22:02:11751 base::MessageLoop::current()->PostDelayedTask(
752 FROM_HERE,
753 base::MessageLoop::QuitClosure(),
754 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26755 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37756
757 // The interstitial should have focus now.
[email protected]f2159ba2012-04-17 19:13:21758 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37759 EXPECT_TRUE(interstitial_page->HasFocus());
760
761 // Hide the interstitial.
762 interstitial_page->DontProceed();
763
764 // Focus should be back on the original page.
[email protected]f2159ba2012-04-17 19:13:21765 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37766}
767
[email protected]9bd491ee2008-12-10 22:31:07768// Make sure Find box can request focus, even when it is already open.
[email protected]06e85dfd2012-10-31 01:32:35769// Disabled due to flakiness. http://crbug.com/67301.
770IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30771 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11772 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55773
[email protected]9bd491ee2008-12-10 22:31:07774 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11775 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18776 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07777
[email protected]a711df102010-12-14 09:47:03778 EXPECT_TRUE(ChromeInForeground());
779
[email protected]853300a82010-07-27 21:17:57780#if defined(OS_MACOSX)
781 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28782 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09783 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57784#else
[email protected]9bd491ee2008-12-10 22:31:07785 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28786 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09787 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57788#endif
[email protected]8bcdec92009-02-25 16:15:18789
[email protected]a9c060ca2012-01-05 20:43:41790 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07791
[email protected]a37d4b02012-06-25 21:56:10792 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18793 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07794
795 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57796#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28797 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09798 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57799#else
[email protected]1d000682010-08-23 16:21:28800 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09801 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57802#endif
[email protected]21abcc742009-10-23 02:52:06803 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07804
805 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41806 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21807 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07808
809 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57810#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28811 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09812 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57813#else
[email protected]1d000682010-08-23 16:21:28814 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09815 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57816#endif
[email protected]8bcdec92009-02-25 16:15:18817
[email protected]a9c060ca2012-01-05 20:43:41818 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07819}
[email protected]401513c2009-03-12 00:21:28820
821// Makes sure the focus is in the right location when opening the different
822// types of tabs.
[email protected]c92928672010-11-09 18:31:07823// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26824IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30825 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07826
[email protected]401513c2009-03-12 00:21:28827 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12828 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35829 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07830 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21831 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28832
833 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10834 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35835 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07836 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18837 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28838
839 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12840 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35841 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07842 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21843 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43844
845 // Open about:blank, focus should be on the location bar.
[email protected]081dc522013-05-15 04:59:20846 chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL),
[email protected]52877dbc62012-06-29 22:22:03847 content::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35848 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07849 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18850 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28851}
[email protected]9d8a4642009-07-29 17:25:30852
853// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21854IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30855 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11856 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30857
[email protected]9d8a4642009-07-29 17:25:30858 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06859 {
[email protected]a7fe9112012-07-20 02:34:45860 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06861 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07862 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10863 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06864 observer.Wait();
865 }
[email protected]b8deecd2012-07-30 21:09:44866 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42867
[email protected]6b4e8e42011-08-17 19:36:06868 {
[email protected]a7fe9112012-07-20 02:34:45869 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06870 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50871 content::Source<NavigationController>(
[email protected]cc872372013-01-28 21:57:07872 &browser()->tab_strip_model()->GetActiveWebContents()->
873 GetController()));
[email protected]a37d4b02012-06-25 21:56:10874 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06875 observer.Wait();
876 }
[email protected]9d8a4642009-07-29 17:25:30877 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18878 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30879
880 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11881 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10882 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18883 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06884 {
[email protected]a7fe9112012-07-20 02:34:45885 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06886 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50887 content::Source<NavigationController>(
[email protected]cc872372013-01-28 21:57:07888 &browser()->tab_strip_model()->GetActiveWebContents()->
889 GetController()));
[email protected]a37d4b02012-06-25 21:56:10890 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06891 observer.Wait();
892 }
[email protected]9ba21ede2010-07-30 01:11:07893
[email protected]9d8a4642009-07-29 17:25:30894 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12895 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21896 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30897}
898
899// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27900IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30901 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11902 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30903
[email protected]9d8a4642009-07-29 17:25:30904 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11905 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07906 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06907 {
[email protected]a7fe9112012-07-20 02:34:45908 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06909 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50910 content::Source<NavigationController>(
[email protected]cc872372013-01-28 21:57:07911 &browser()->tab_strip_model()->GetActiveWebContents()->
912 GetController()));
[email protected]a37d4b02012-06-25 21:56:10913 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06914 observer.Wait();
915 }
[email protected]9ba21ede2010-07-30 01:11:07916
[email protected]9d8a4642009-07-29 17:25:30917 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12918 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21919 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30920}
[email protected]629e0342010-07-27 23:30:13921
[email protected]895a84722012-11-29 18:45:44922// Tests that when a new tab is opened from the omnibox, the focus is moved from
923// the omnibox for the current tab.
924IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
925 NavigateFromOmniboxIntoNewTab) {
926 GURL url("http://www.google.com/");
927 GURL url2("http://maps.google.com/");
928
929 // Navigate to url.
930 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK);
931 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
932 p.disposition = CURRENT_TAB;
933 chrome::Navigate(&p);
934
935 // Focus the omnibox.
936 chrome::FocusLocationBar(browser());
937
938 OmniboxEditController* controller =
939 browser()->window()->GetLocationBar()->GetLocationEntry()->model()->
940 controller();
941
942 // Simulate an alt-enter.
943 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
944 content::PAGE_TRANSITION_TYPED, GURL());
945
946 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20947 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44948
949 // The tab contents should have the focus in the second tab.
950 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
951
952 // Go back to the first tab. The focus should not be in the omnibox.
953 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20954 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]5c4e4482013-03-03 00:09:18955 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44956}
957
[email protected]c744f7d22013-04-12 20:29:04958IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnNavigate) {
959 // Needed on Mac.
960 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
961 // Load the NTP.
962 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
963 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
964
965 // Navigate to another page.
966 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
967 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
968 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
969 ui_test_utils::NavigateToURL(browser(), file_url);
970
971 ClickOnView(VIEW_ID_TAB_CONTAINER);
972
973 // Navigate back. Should focus the location bar.
974 {
975 content::WindowedNotificationObserver back_nav_observer(
976 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
977 content::NotificationService::AllSources());
978 chrome::GoBack(browser(), CURRENT_TAB);
979 back_nav_observer.Wait();
980 }
981
982 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
983
984 // Navigate forward. Shouldn't focus the location bar.
985 ClickOnView(VIEW_ID_TAB_CONTAINER);
986 {
987 content::WindowedNotificationObserver forward_nav_observer(
988 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
989 content::NotificationService::AllSources());
990 chrome::GoForward(browser(), CURRENT_TAB);
991 forward_nav_observer.Wait();
992 }
993
994 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
995}
996
[email protected]629e0342010-07-27 23:30:13997} // namespace