blob: ecf2a3596464efae7a2e7cd121d2dd4976a3d767 [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]a9c060ca2012-01-05 20:43:415#include "base/bind.h"
[email protected]3985ba82010-07-29 21:44:126#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:427#include "base/format_macros.h"
[email protected]3cc55ad2013-07-17 22:17:418#include "base/message_loop/message_loop.h"
[email protected]3985ba82010-07-29 21:44:129#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4110#include "base/strings/string_number_conversions.h"
[email protected]11521182013-06-11 04:06:3611#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1212#include "base/strings/utf_string_conversions.h"
[email protected]dcc8fbc2013-07-12 00:54:0913#include "chrome/browser/chrome_notification_types.h"
[email protected]7b5dc002010-11-16 23:08:1014#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1015#include "chrome/browser/ui/browser_commands.h"
[email protected]52877dbc62012-06-29 22:22:0316#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3317#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1218#include "chrome/browser/ui/chrome_pages.h"
[email protected]895a84722012-11-29 18:45:4419#include "chrome/browser/ui/omnibox/location_bar.h"
20#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
21#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
22#include "chrome/browser/ui/omnibox/omnibox_view.h"
[email protected]b56e2e32012-05-11 21:18:0423#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1924#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3725#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5526#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3227#include "chrome/test/base/in_process_browser_test.h"
[email protected]3ea1b182013-02-08 22:38:4128#include "chrome/test/base/interactive_test_utils.h"
[email protected]477ae052011-11-18 23:53:5729#include "chrome/test/base/ui_test_utils.h"
[email protected]cadaec52012-02-08 21:53:1330#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1331#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5732#include "content/public/browser/notification_service.h"
[email protected]9c1662b2012-03-06 15:44:3333#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5834#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2035#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1036#include "content/public/browser/web_contents_view.h"
[email protected]7d478cb2012-07-24 17:19:4237#include "content/public/test/browser_test_utils.h"
[email protected]c1dffe82013-06-26 20:59:0538#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5739
[email protected]a711df102010-12-14 09:47:0340#if defined(OS_WIN)
[email protected]93270d002011-01-19 22:32:5941#include <windows.h>
[email protected]3ea1b182013-02-08 22:38:4142#include <Psapi.h>
[email protected]11521182013-06-11 04:06:3643#include "base/strings/string_util.h"
[email protected]a711df102010-12-14 09:47:0344#endif
45
[email protected]cadaec52012-02-08 21:53:1346using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5047using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1148using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2549using content::WebContents;
50
[email protected]3a3cf4d2011-12-09 00:39:3551#if defined(OS_MACOSX)
52// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4853#define MAYBE_FocusTraversal DISABLED_FocusTraversal
54#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]ddabf402013-10-22 22:43:0855#elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
56// TODO(erg): http://crbug.com/163931
57#define MAYBE_FocusTraversal DISABLED_FocusTraversal
58#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]5b6d0432013-01-24 20:30:1659#elif defined(OS_WIN) || defined(OS_CHROMEOS)
60// http://crbug.com/109770 and http://crbug.com/62544
[email protected]2f2bf1172012-01-10 22:17:1861#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4862#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3563#else
64#define MAYBE_FocusTraversal FocusTraversal
65#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
66#endif
67
68#if defined(OS_LINUX) || defined(OS_MACOSX)
69// TODO(jcampan): http://crbug.com/23683 for linux.
70// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3071#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5772#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0773// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2674#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4175#endif
76
initial.commit09911bf2008-07-26 23:55:2977namespace {
78
[email protected]8bcdec92009-02-25 16:15:1879// The delay waited in some cases where we don't have a notifications for an
80// action we take.
initial.commit09911bf2008-07-26 23:55:2981const int kActionDelayMs = 500;
82
[email protected]a9c060ca2012-01-05 20:43:4183// Maxiumum time to wait until the focus is moved to expected view.
84const int kFocusChangeTimeoutMs = 500;
85
[email protected]c1dffe82013-06-26 20:59:0586const char kSimplePage[] = "/focus/page_with_focus.html";
87const char kStealFocusPage[] = "/focus/page_steals_focus.html";
88const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3189const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2990
[email protected]a711df102010-12-14 09:47:0391// Test to make sure Chrome is in the foreground as we start testing. This is
92// required for tests that synthesize input to the Chrome window.
93bool ChromeInForeground() {
94#if defined(OS_WIN)
95 HWND window = ::GetForegroundWindow();
96 std::wstring caption;
97 std::wstring filename;
98 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:1899 if (len > 1)
100 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03101 bool chrome_window_in_foreground =
102 EndsWith(caption, L" - Google Chrome", true) ||
103 EndsWith(caption, L" - Chromium", true);
104 if (!chrome_window_in_foreground) {
105 DWORD process_id;
106 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
107
108 base::ProcessHandle process;
109 if (base::OpenProcessHandleWithAccess(process_id,
110 PROCESS_QUERY_LIMITED_INFORMATION,
111 &process)) {
[email protected]fdce4782011-11-29 20:06:18112 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
113 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03114 int error = GetLastError();
115 filename = std::wstring(L"Unable to read filename for process id '" +
116 base::IntToString16(process_id) +
117 L"' (error ") +
118 base::IntToString16(error) + L")";
119 }
120 base::CloseProcessHandle(process);
121 }
122 }
123 EXPECT_TRUE(chrome_window_in_foreground)
124 << "Chrome must be in the foreground when running interactive tests\n"
125 << "Process in foreground: " << filename.c_str() << "\n"
126 << "Window: " << window << "\n"
127 << "Caption: " << caption.c_str();
128 return chrome_window_in_foreground;
129#else
130 // Windows only at the moment.
131 return true;
132#endif
133}
134
[email protected]a9c060ca2012-01-05 20:43:41135// Wait the focus change in message loop.
136void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
137 if (ui_test_utils::IsViewFocused(browser, id) ||
138 base::Time::Now() > timeout) {
[email protected]3264d162013-05-08 22:02:11139 base::MessageLoop::current()->PostTask(FROM_HERE,
140 base::MessageLoop::QuitClosure());
[email protected]a9c060ca2012-01-05 20:43:41141 } else {
[email protected]3264d162013-05-08 22:02:11142 base::MessageLoop::current()->PostDelayedTask(
[email protected]a9c060ca2012-01-05 20:43:41143 FROM_HERE,
144 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42145 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41146 }
147};
148
[email protected]8bcdec92009-02-25 16:15:18149class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29150 public:
[email protected]21abcc742009-10-23 02:52:06151 bool IsViewFocused(ViewID vid) {
152 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17153 }
154
[email protected]fc2e0872009-08-21 22:14:41155 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06156 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41157 }
[email protected]0b8fa8b2011-12-07 00:54:52158
[email protected]a9c060ca2012-01-05 20:43:41159 bool WaitForFocusChange(ViewID vid) {
160 const base::Time timeout = base::Time::Now() +
161 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
[email protected]3264d162013-05-08 22:02:11162 base::MessageLoop::current()->PostDelayedTask(
[email protected]a9c060ca2012-01-05 20:43:41163 FROM_HERE,
164 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42165 base::TimeDelta::FromMilliseconds(100));
[email protected]729eb632012-07-26 04:45:26166 content::RunMessageLoop();
[email protected]a9c060ca2012-01-05 20:43:41167 return IsViewFocused(vid);
168 }
initial.commit09911bf2008-07-26 23:55:29169};
170
[email protected]85f0a572012-02-07 22:20:13171class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37172 public:
[email protected]85f0a572012-02-07 22:20:13173 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]650b2d52013-02-10 03:41:45174 base::FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37175 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
176 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31177 file_path = file_path.AppendASCII("focus");
178 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]82f84b92013-08-30 18:23:50179 r = base::ReadFileToString(file_path, &html_contents_);
[email protected]9e0c83a2009-05-06 19:44:37180 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13181 interstitial_page_ = InterstitialPage::Create(
182 tab, new_navigation, url , this);
183 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37184 }
185
[email protected]626cc902013-02-06 18:30:53186 virtual std::string GetHTMLContents() OVERRIDE {
[email protected]9e0c83a2009-05-06 19:44:37187 return html_contents_;
188 }
189
[email protected]85f0a572012-02-07 22:20:13190 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13191 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37192 }
193
[email protected]85f0a572012-02-07 22:20:13194 void DontProceed() {
195 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21196 }
197
[email protected]9e0c83a2009-05-06 19:44:37198 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58199 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37200 }
201
202 private:
203 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13204 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37205};
[email protected]b9821882009-08-17 22:25:17206
[email protected]c39e233d2012-05-18 13:24:12207// Flaky on mac. http://crbug.com/67301.
208#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23209#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12210#else
[email protected]839e0802012-05-18 14:45:23211#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12212#endif
213IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30214 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42215#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41216 // It seems we have to wait a little bit for the widgets to spin up before
217 // we can start clicking on them.
[email protected]3264d162013-05-08 22:02:11218 base::MessageLoop::current()->PostDelayedTask(
[email protected]7e560102012-03-08 20:58:42219 FROM_HERE,
[email protected]3264d162013-05-08 22:02:11220 base::MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42221 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26222 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42223#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41224
[email protected]5c4e4482013-03-03 00:09:18225 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00226
[email protected]fc2e0872009-08-21 22:14:41227 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21228 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00229
[email protected]5c4e4482013-03-03 00:09:18230 ClickOnView(VIEW_ID_OMNIBOX);
231 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00232}
[email protected]186f13f2009-08-19 20:34:00233
[email protected]320948e02011-01-10 08:21:12234// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26235IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30236 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05237 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
initial.commit09911bf2008-07-26 23:55:29238
239 // First we navigate to our test page.
[email protected]c1dffe82013-06-26 20:59:05240 GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18241 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29242
[email protected]90556dd2012-06-07 20:26:18243 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00244
initial.commit09911bf2008-07-26 23:55:29245 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21246 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29247 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57248 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29249 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21250 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29251
[email protected]a37d4b02012-06-25 21:56:10252 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18253 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29254 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57255 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29256 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18257 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29258}
259
260// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39261// Disabled, http://crbug.com/62542.
262IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30263 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05264 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
initial.commit09911bf2008-07-26 23:55:29265
266 // First we navigate to our test page.
[email protected]c1dffe82013-06-26 20:59:05267 GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18268 ui_test_utils::NavigateToURL(browser(), url);
269
initial.commit09911bf2008-07-26 23:55:29270 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03271 for (int i = 0; i < 4; ++i) {
272 chrome::AddSelectedTabWithURL(browser(), url,
273 content::PAGE_TRANSITION_TYPED);
274 }
initial.commit09911bf2008-07-26 23:55:29275
276 // Alternate focus for the tab.
277 const bool kFocusPage[3][5] = {
278 { true, true, true, true, false },
279 { false, false, false, false, false },
280 { false, true, false, true, false }
281 };
282
283 for (int i = 1; i < 3; i++) {
284 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18285 // Activate the tab.
[email protected]57892182012-12-03 19:15:39286 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29287
288 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54289 if (kFocusPage[i][j]) {
[email protected]57892182012-12-03 19:15:39290 browser()->tab_strip_model()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54291 } else {
[email protected]a37d4b02012-06-25 21:56:10292 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54293 }
initial.commit09911bf2008-07-26 23:55:29294 }
295
296 // Now come back to the tab and check the right view is focused.
297 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18298 // Activate the tab.
[email protected]57892182012-12-03 19:15:39299 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29300
[email protected]5c4e4482013-03-03 00:09:18301 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06302 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29303 }
[email protected]cb7e2542009-12-14 22:02:35304
[email protected]57892182012-12-03 19:15:39305 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35306 // Try the above, but with ctrl+tab. Since tab normally changes focus,
307 // this has regressed in the past. Loop through several times to be sure.
308 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21309 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18310 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35311 ASSERT_TRUE(IsViewFocused(vid));
312
[email protected]1d000682010-08-23 16:21:28313 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09314 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35315 }
316
317 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39318 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35319 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21320 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18321 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35322 ASSERT_TRUE(IsViewFocused(vid));
323
[email protected]1d000682010-08-23 16:21:28324 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09325 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35326 }
initial.commit09911bf2008-07-26 23:55:29327 }
328}
329
[email protected]ae40b572009-10-02 21:17:45330// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35331IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30332 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05333 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]ae40b572009-10-02 21:17:45334
335 // First we navigate to our test page.
[email protected]c1dffe82013-06-26 20:59:05336 GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45337 ui_test_utils::NavigateToURL(browser(), url);
338
[email protected]a37d4b02012-06-25 21:56:10339 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07340 ui_test_utils::FindInPage(
341 browser()->tab_strip_model()->GetActiveWebContents(),
342 ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06343 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45344
345 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10346 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45347
348 // Create a 2nd tab.
[email protected]52877dbc62012-06-29 22:22:03349 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45350
351 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21352 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45353
354 // Select 1st tab, focus should still be on the location-bar.
355 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39356 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18357 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45358
359 // Now open the find box again, switch to another tab and come back, the focus
360 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10361 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06362 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39363 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21364 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39365 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06366 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45367}
368
initial.commit09911bf2008-07-26 23:55:29369// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07370// Flaky, http://crbug.com/62538.
371IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26372 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30373 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05374 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
initial.commit09911bf2008-07-26 23:55:29375
initial.commit09911bf2008-07-26 23:55:29376 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21377 Browser* browser2 =
378 new Browser(Browser::CreateParams(browser()->profile(),
379 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18380 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10381 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18382 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00383
[email protected]ed179ee2009-10-03 21:02:51384 Browser* focused_browser = NULL;
385 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16386#if defined(USE_X11)
387 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00388 // to figure out which browser does have focus.
389 if (browser2->window()->IsActive()) {
390 focused_browser = browser2;
391 unfocused_browser = browser();
392 } else if (browser()->window()->IsActive()) {
393 focused_browser = browser();
394 unfocused_browser = browser2;
395 } else {
[email protected]579c3d82010-10-06 03:53:51396 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00397 }
398#elif defined(OS_WIN)
399 focused_browser = browser();
400 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57401#elif defined(OS_MACOSX)
402 // On Mac, the newly created window always gets the focus.
403 focused_browser = browser2;
404 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00405#endif
406
[email protected]c1dffe82013-06-26 20:59:05407 GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00408 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46409
[email protected]8bcdec92009-02-25 16:15:18410 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00411 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29412
[email protected]b6987e02013-01-04 18:30:43413 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05414 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13415 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29416
[email protected]8bcdec92009-02-25 16:15:18417 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00418 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29419}
420
[email protected]ddabf402013-10-22 22:43:08421#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
422// TODO(erg): http://crbug.com/163931
423#define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
424#else
425#define MAYBE_LocationBarLockFocus LocationBarLockFocus
426#endif
427
initial.commit09911bf2008-07-26 23:55:29428// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08429IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30430 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05431 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
initial.commit09911bf2008-07-26 23:55:29432
433 // Open the page that steals focus.
[email protected]c1dffe82013-06-26 20:59:05434 GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18435 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29436
[email protected]a37d4b02012-06-25 21:56:10437 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29438
[email protected]b6987e02013-01-04 18:30:43439 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07440 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13441 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29442
443 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18444 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29445}
446
[email protected]9e0c83a2009-05-06 19:44:37447// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35448// Note that this test relies on a notification from the renderer that the
449// focus has changed in the page. The notification in the renderer may change
450// at which point this test would fail (see comment in
451// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57452IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30453 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05454 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
initial.commit09911bf2008-07-26 23:55:29455
[email protected]8bcdec92009-02-25 16:15:18456 // First we navigate to our test page.
[email protected]c1dffe82013-06-26 20:59:05457 GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18458 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29459
[email protected]a37d4b02012-06-25 21:56:10460 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29461
[email protected]546ae4e02010-12-08 14:57:19462 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18463 const char* kExpElementIDs[] = {
464 "", // Initially no element in the page should be focused
465 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19466 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18467 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29468 };
469
470 // Test forward focus traversal.
471 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40472 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29473 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18474 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29475
[email protected]911696b2011-01-28 02:36:49476 // Move the caret to the end, otherwise the next Tab key may not move focus.
477 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
478 browser(), ui::VKEY_END, false, false, false, false));
479
initial.commit09911bf2008-07-26 23:55:29480 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35481 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40482 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29483 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01484 std::string actual;
[email protected]b6987e02013-01-04 18:30:43485 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]cc872372013-01-28 21:57:07486 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13487 "window.domAutomationController.send(getFocusedElement());",
[email protected]45671612009-04-29 22:24:01488 &actual));
initial.commit09911bf2008-07-26 23:55:29489 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
490
[email protected]130efb02009-09-18 18:54:35491 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19492 // If the next element is the kTextElementID, we expect to be
493 // notified we have switched to an editable node.
494 bool is_editable_node =
495 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53496 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19497
498 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09499 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27500 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56501 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]cc872372013-01-28 21:57:07502 browser()->tab_strip_model()->GetActiveWebContents()->
503 GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19504 details));
[email protected]130efb02009-09-18 18:54:35505 } else {
506 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19507 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09508 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27509 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53510 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35511 }
initial.commit09911bf2008-07-26 23:55:29512 }
[email protected]8bcdec92009-02-25 16:15:18513
514 // At this point the renderer has sent us a message asking to advance the
515 // focus (as the end of the focus loop was reached in the renderer).
516 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44517 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29518 }
519
520 // Now let's try reverse focus traversal.
521 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40522 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29523 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18524 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29525
[email protected]911696b2011-01-28 02:36:49526 // Move the caret to the end, otherwise the next Tab key may not move focus.
527 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
528 browser(), ui::VKEY_END, false, false, false, false));
529
[email protected]8bcdec92009-02-25 16:15:18530 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30531 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40532 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19533 const char* next_element =
534 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35535
536 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19537 // If the next element is the kTextElementID, we expect to be
538 // notified we have switched to an editable node.
539 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53540 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19541
542 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09543 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27544 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56545 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]cc872372013-01-28 21:57:07546 browser()->tab_strip_model()->GetActiveWebContents()->
547 GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19548 details));
[email protected]130efb02009-09-18 18:54:35549 } else {
550 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19551 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09552 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27553 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53554 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35555 }
initial.commit09911bf2008-07-26 23:55:29556
557 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01558 std::string actual;
[email protected]b6987e02013-01-04 18:30:43559 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]cc872372013-01-28 21:57:07560 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13561 "window.domAutomationController.send(getFocusedElement());",
[email protected]45671612009-04-29 22:24:01562 &actual));
[email protected]546ae4e02010-12-08 14:57:19563 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29564 }
[email protected]8bcdec92009-02-25 16:15:18565
566 // At this point the renderer has sent us a message asking to advance the
567 // focus (as the end of the focus loop was reached in the renderer).
568 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44569 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29570 }
571}
572
[email protected]9e0c83a2009-05-06 19:44:37573// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41574IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30575 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05576 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]9e0c83a2009-05-06 19:44:37577
578 // First we navigate to our test page.
[email protected]c1dffe82013-06-26 20:59:05579 GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37580 ui_test_utils::NavigateToURL(browser(), url);
581
[email protected]9e0c83a2009-05-06 19:44:37582 // Focus should be on the page.
[email protected]f2159ba2012-04-17 19:13:21583 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37584
585 // Let's show an interstitial.
[email protected]cc872372013-01-28 21:57:07586 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
587 browser()->tab_strip_model()->GetActiveWebContents(),
588 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37589 // Give some time for the interstitial to show.
[email protected]3264d162013-05-08 22:02:11590 base::MessageLoop::current()->PostDelayedTask(
591 FROM_HERE,
592 base::MessageLoop::QuitClosure(),
593 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26594 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37595
[email protected]a37d4b02012-06-25 21:56:10596 chrome::FocusLocationBar(browser());
[email protected]9e0c83a2009-05-06 19:44:37597
598 const char* kExpElementIDs[] = {
599 "", // Initially no element in the page should be focused
600 // (the location bar is focused).
601 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
602 "gmapLink"
603 };
604
605 // Test forward focus traversal.
606 for (int i = 0; i < 2; ++i) {
607 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18608 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9e0c83a2009-05-06 19:44:37609
[email protected]911696b2011-01-28 02:36:49610 // Move the caret to the end, otherwise the next Tab key may not move focus.
611 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
612 browser(), ui::VKEY_END, false, false, false, false));
613
[email protected]9e0c83a2009-05-06 19:44:37614 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35615 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37616 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30617 std::string actual;
[email protected]b6987e02013-01-04 18:30:43618 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]06bc5d92013-01-02 22:44:13619 interstitial_page->render_view_host(),
[email protected]06bc5d92013-01-02 22:44:13620 "window.domAutomationController.send(getFocusedElement());",
[email protected]a6e602f2010-09-28 22:28:30621 &actual));
[email protected]9e0c83a2009-05-06 19:44:37622 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
623
[email protected]432115822011-07-10 15:52:27624 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53625 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07626 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35627 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27628 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56629 notification_source = content::Source<RenderViewHost>(
630 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35631 } else {
632 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27633 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53634 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35635 }
[email protected]a6e602f2010-09-28 22:28:30636
637 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09638 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30639 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37640 }
641
642 // At this point the renderer has sent us a message asking to advance the
643 // focus (as the end of the focus loop was reached in the renderer).
644 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44645 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37646 }
647
648 // Now let's try reverse focus traversal.
649 for (int i = 0; i < 2; ++i) {
650 // Location bar should be focused.
[email protected]5c4e4482013-03-03 00:09:18651 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9e0c83a2009-05-06 19:44:37652
[email protected]911696b2011-01-28 02:36:49653 // Move the caret to the end, otherwise the next Tab key may not move focus.
654 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
655 browser(), ui::VKEY_END, false, false, false, false));
656
[email protected]9e0c83a2009-05-06 19:44:37657 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35658 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27659 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53660 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07661 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35662 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27663 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56664 notification_source = content::Source<RenderViewHost>(
665 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35666 } else {
667 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27668 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53669 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35670 }
[email protected]9e0c83a2009-05-06 19:44:37671
[email protected]a6e602f2010-09-28 22:28:30672 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09673 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30674 notification_type, notification_source));
675
[email protected]9e0c83a2009-05-06 19:44:37676 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30677 std::string actual;
[email protected]b6987e02013-01-04 18:30:43678 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]06bc5d92013-01-02 22:44:13679 interstitial_page->render_view_host(),
[email protected]06bc5d92013-01-02 22:44:13680 "window.domAutomationController.send(getFocusedElement());",
[email protected]a6e602f2010-09-28 22:28:30681 &actual));
[email protected]9e0c83a2009-05-06 19:44:37682 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
683 }
684
685 // At this point the renderer has sent us a message asking to advance the
686 // focus (as the end of the focus loop was reached in the renderer).
687 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44688 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37689 }
690}
691
692// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33693// http://crbug.com/81451
[email protected]4d2451652012-02-14 23:54:26694IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30695 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05696 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]9e0c83a2009-05-06 19:44:37697
698 // First we navigate to our test page.
[email protected]c1dffe82013-06-26 20:59:05699 GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37700 ui_test_utils::NavigateToURL(browser(), url);
701
[email protected]9e0c83a2009-05-06 19:44:37702 // Page should have focus.
[email protected]f2159ba2012-04-17 19:13:21703 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]cc872372013-01-28 21:57:07704 EXPECT_TRUE(browser()->tab_strip_model()->GetActiveWebContents()->
705 GetRenderViewHost()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37706
[email protected]9d8a4642009-07-29 17:25:30707 // Let's show an interstitial.
[email protected]cc872372013-01-28 21:57:07708 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
709 browser()->tab_strip_model()->GetActiveWebContents(),
710 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37711 // Give some time for the interstitial to show.
[email protected]3264d162013-05-08 22:02:11712 base::MessageLoop::current()->PostDelayedTask(
713 FROM_HERE,
714 base::MessageLoop::QuitClosure(),
715 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26716 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37717
718 // The interstitial should have focus now.
[email protected]f2159ba2012-04-17 19:13:21719 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37720 EXPECT_TRUE(interstitial_page->HasFocus());
721
722 // Hide the interstitial.
723 interstitial_page->DontProceed();
724
725 // Focus should be back on the original page.
[email protected]f2159ba2012-04-17 19:13:21726 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37727}
728
[email protected]9bd491ee2008-12-10 22:31:07729// Make sure Find box can request focus, even when it is already open.
[email protected]06e85dfd2012-10-31 01:32:35730// Disabled due to flakiness. http://crbug.com/67301.
731IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30732 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05733 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
license.botbf09a502008-08-24 00:55:55734
[email protected]9bd491ee2008-12-10 22:31:07735 // Open some page (any page that doesn't steal focus).
[email protected]c1dffe82013-06-26 20:59:05736 GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18737 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07738
[email protected]a711df102010-12-14 09:47:03739 EXPECT_TRUE(ChromeInForeground());
740
[email protected]853300a82010-07-27 21:17:57741#if defined(OS_MACOSX)
742 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28743 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09744 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57745#else
[email protected]9bd491ee2008-12-10 22:31:07746 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28747 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09748 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57749#endif
[email protected]8bcdec92009-02-25 16:15:18750
[email protected]a9c060ca2012-01-05 20:43:41751 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07752
[email protected]a37d4b02012-06-25 21:56:10753 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18754 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07755
756 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57757#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28758 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09759 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57760#else
[email protected]1d000682010-08-23 16:21:28761 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09762 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57763#endif
[email protected]21abcc742009-10-23 02:52:06764 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07765
766 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41767 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21768 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07769
770 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57771#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28772 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09773 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57774#else
[email protected]1d000682010-08-23 16:21:28775 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09776 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57777#endif
[email protected]8bcdec92009-02-25 16:15:18778
[email protected]a9c060ca2012-01-05 20:43:41779 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07780}
[email protected]401513c2009-03-12 00:21:28781
782// Makes sure the focus is in the right location when opening the different
783// types of tabs.
[email protected]c92928672010-11-09 18:31:07784// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26785IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30786 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07787
[email protected]401513c2009-03-12 00:21:28788 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12789 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35790 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07791 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21792 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28793
794 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10795 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35796 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07797 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18798 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28799
800 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12801 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35802 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07803 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21804 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43805
806 // Open about:blank, focus should be on the location bar.
[email protected]081dc522013-05-15 04:59:20807 chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL),
[email protected]52877dbc62012-06-29 22:22:03808 content::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35809 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07810 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18811 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28812}
[email protected]9d8a4642009-07-29 17:25:30813
[email protected]ddabf402013-10-22 22:43:08814#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
815// TODO(erg): http://crbug.com/163931
816#define MAYBE_FocusOnReload DISABLED_FocusOnReload
817#else
818#define MAYBE_FocusOnReload FocusOnReload
819#endif
820
[email protected]9d8a4642009-07-29 17:25:30821// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08822IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30823 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05824 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]9d8a4642009-07-29 17:25:30825
[email protected]9d8a4642009-07-29 17:25:30826 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06827 {
[email protected]a7fe9112012-07-20 02:34:45828 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06829 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07830 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10831 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06832 observer.Wait();
833 }
[email protected]b8deecd2012-07-30 21:09:44834 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42835
[email protected]6b4e8e42011-08-17 19:36:06836 {
[email protected]a7fe9112012-07-20 02:34:45837 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06838 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50839 content::Source<NavigationController>(
[email protected]cc872372013-01-28 21:57:07840 &browser()->tab_strip_model()->GetActiveWebContents()->
841 GetController()));
[email protected]a37d4b02012-06-25 21:56:10842 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06843 observer.Wait();
844 }
[email protected]9d8a4642009-07-29 17:25:30845 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18846 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30847
848 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05849 ui_test_utils::NavigateToURL(browser(),
850 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10851 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18852 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06853 {
[email protected]a7fe9112012-07-20 02:34:45854 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06855 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50856 content::Source<NavigationController>(
[email protected]cc872372013-01-28 21:57:07857 &browser()->tab_strip_model()->GetActiveWebContents()->
858 GetController()));
[email protected]a37d4b02012-06-25 21:56:10859 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06860 observer.Wait();
861 }
[email protected]9ba21ede2010-07-30 01:11:07862
[email protected]9d8a4642009-07-29 17:25:30863 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12864 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21865 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30866}
867
868// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27869IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30870 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]c1dffe82013-06-26 20:59:05871 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]9d8a4642009-07-29 17:25:30872
[email protected]9d8a4642009-07-29 17:25:30873 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05874 ui_test_utils::NavigateToURL(browser(),
875 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07876 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06877 {
[email protected]a7fe9112012-07-20 02:34:45878 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06879 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50880 content::Source<NavigationController>(
[email protected]cc872372013-01-28 21:57:07881 &browser()->tab_strip_model()->GetActiveWebContents()->
882 GetController()));
[email protected]a37d4b02012-06-25 21:56:10883 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06884 observer.Wait();
885 }
[email protected]9ba21ede2010-07-30 01:11:07886
[email protected]9d8a4642009-07-29 17:25:30887 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12888 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21889 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30890}
[email protected]629e0342010-07-27 23:30:13891
[email protected]09475062013-10-21 21:29:56892// Tests that focus goes to frame after crashed tab.
893// TODO(shrikant): Find out where the focus should be deterministically.
894// Currently focused_view after crash seem to be non null in debug mode
895// (invalidated pointer 0xcccccc).
896IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
897 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
898 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
899
900 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
901
902 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
903}
904
[email protected]895a84722012-11-29 18:45:44905// Tests that when a new tab is opened from the omnibox, the focus is moved from
906// the omnibox for the current tab.
907IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
908 NavigateFromOmniboxIntoNewTab) {
909 GURL url("http://www.google.com/");
910 GURL url2("http://maps.google.com/");
911
912 // Navigate to url.
913 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK);
914 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
915 p.disposition = CURRENT_TAB;
916 chrome::Navigate(&p);
917
918 // Focus the omnibox.
919 chrome::FocusLocationBar(browser());
920
921 OmniboxEditController* controller =
922 browser()->window()->GetLocationBar()->GetLocationEntry()->model()->
923 controller();
924
925 // Simulate an alt-enter.
926 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
[email protected]04ef14d2013-10-04 01:01:49927 content::PAGE_TRANSITION_TYPED);
[email protected]895a84722012-11-29 18:45:44928
929 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20930 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44931
932 // The tab contents should have the focus in the second tab.
933 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
934
935 // Go back to the first tab. The focus should not be in the omnibox.
936 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20937 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]5c4e4482013-03-03 00:09:18938 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44939}
940
[email protected]ddabf402013-10-22 22:43:08941#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
942// TODO(erg): http://crbug.com/163931
943#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
944#else
945#define MAYBE_FocusOnNavigate FocusOnNavigate
946#endif
947
948IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04949 // Needed on Mac.
950 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
951 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06952 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04953 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
954
955 // Navigate to another page.
956 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
957 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
958 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
959 ui_test_utils::NavigateToURL(browser(), file_url);
960
961 ClickOnView(VIEW_ID_TAB_CONTAINER);
962
963 // Navigate back. Should focus the location bar.
964 {
965 content::WindowedNotificationObserver back_nav_observer(
966 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
967 content::NotificationService::AllSources());
968 chrome::GoBack(browser(), CURRENT_TAB);
969 back_nav_observer.Wait();
970 }
971
972 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
973
974 // Navigate forward. Shouldn't focus the location bar.
975 ClickOnView(VIEW_ID_TAB_CONTAINER);
976 {
977 content::WindowedNotificationObserver forward_nav_observer(
978 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
979 content::NotificationService::AllSources());
980 chrome::GoForward(browser(), CURRENT_TAB);
981 forward_nav_observer.Wait();
982 }
983
984 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
985}
986
[email protected]629e0342010-07-27 23:30:13987} // namespace