blob: 52153efa5773d1cc796cd59dd752759eafed4654 [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]a9c060ca2012-01-05 20:43:417#include "base/bind.h"
[email protected]3985ba82010-07-29 21:44:128#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:429#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:1810#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1211#include "base/path_service.h"
[email protected]a711df102010-12-14 09:47:0312#include "base/string_number_conversions.h"
[email protected]477ae052011-11-18 23:53:5713#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2214#include "base/utf_string_conversions.h"
[email protected]7b5dc002010-11-16 23:08:1015#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1016#include "chrome/browser/ui/browser_commands.h"
[email protected]52877dbc62012-06-29 22:22:0317#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3318#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1219#include "chrome/browser/ui/chrome_pages.h"
[email protected]895a84722012-11-29 18:45:4420#include "chrome/browser/ui/omnibox/location_bar.h"
21#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
22#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
23#include "chrome/browser/ui/omnibox/omnibox_view.h"
[email protected]871dc682012-06-11 19:35:3324#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]b56e2e32012-05-11 21:18:0425#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1926#include "chrome/browser/ui/view_ids.h"
[email protected]432115822011-07-10 15:52:2727#include "chrome/common/chrome_notification_types.h"
[email protected]9e0c83a2009-05-06 19:44:3728#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5529#include "chrome/common/url_constants.h"
[email protected]895a84722012-11-29 18:45:4430#include "chrome/test/base/interactive_test_utils.h"
[email protected]af44e7fb2011-07-29 18:32:3231#include "chrome/test/base/in_process_browser_test.h"
[email protected]477ae052011-11-18 23:53:5732#include "chrome/test/base/ui_test_utils.h"
[email protected]cadaec52012-02-08 21:53:1333#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1334#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5735#include "content/public/browser/notification_service.h"
[email protected]9c1662b2012-03-06 15:44:3336#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5837#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2038#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1039#include "content/public/browser/web_contents_view.h"
[email protected]7d478cb2012-07-24 17:19:4240#include "content/public/test/browser_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1241#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5742
43#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]477ae052011-11-18 23:53:5744#include "ui/views/focus/focus_manager.h"
[email protected]5025f862011-11-30 23:35:2045#include "ui/views/view.h"
[email protected]853300a82010-07-27 21:17:5746#endif
initial.commit09911bf2008-07-26 23:55:2947
[email protected]134c47b92009-08-19 03:33:4448#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5449#include "chrome/browser/ui/views/frame/browser_view.h"
[email protected]134c47b92009-08-19 03:33:4450#endif
51
[email protected]a711df102010-12-14 09:47:0352#if defined(OS_WIN)
[email protected]a711df102010-12-14 09:47:0353#include <Psapi.h>
[email protected]93270d002011-01-19 22:32:5954#include <windows.h>
[email protected]1870d5cf2011-05-12 01:55:4055#include "base/string_util.h"
[email protected]a711df102010-12-14 09:47:0356#endif
57
[email protected]cadaec52012-02-08 21:53:1358using content::InterstitialPage;
[email protected]c5eed492012-01-04 17:07:5059using content::NavigationController;
[email protected]eaabba22012-03-07 15:02:1160using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2561using content::WebContents;
62
[email protected]3a3cf4d2011-12-09 00:39:3563#if defined(OS_MACOSX)
64// TODO(suzhe): http://crbug.com/60973
[email protected]a5eb2d672010-10-22 07:24:4865#define MAYBE_FocusTraversal DISABLED_FocusTraversal
66#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]871153922012-01-10 20:13:4867#elif defined(OS_WIN)
68// http://crbug.com/109770
[email protected]2f2bf1172012-01-10 22:17:1869#define MAYBE_FocusTraversal FocusTraversal
[email protected]871153922012-01-10 20:13:4870#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]3a3cf4d2011-12-09 00:39:3571#else
72#define MAYBE_FocusTraversal FocusTraversal
73#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
74#endif
75
76#if defined(OS_LINUX) || defined(OS_MACOSX)
77// TODO(jcampan): http://crbug.com/23683 for linux.
78// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3079#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5780#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0781// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2682#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4183#endif
84
initial.commit09911bf2008-07-26 23:55:2985namespace {
86
[email protected]8bcdec92009-02-25 16:15:1887// The delay waited in some cases where we don't have a notifications for an
88// action we take.
initial.commit09911bf2008-07-26 23:55:2989const int kActionDelayMs = 500;
90
[email protected]a9c060ca2012-01-05 20:43:4191// Maxiumum time to wait until the focus is moved to expected view.
92const int kFocusChangeTimeoutMs = 500;
93
[email protected]f72a1cc2010-04-30 07:17:3094const char kSimplePage[] = "files/focus/page_with_focus.html";
95const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
96const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3197const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2998
[email protected]a711df102010-12-14 09:47:0399// Test to make sure Chrome is in the foreground as we start testing. This is
100// required for tests that synthesize input to the Chrome window.
101bool ChromeInForeground() {
102#if defined(OS_WIN)
103 HWND window = ::GetForegroundWindow();
104 std::wstring caption;
105 std::wstring filename;
106 int len = ::GetWindowTextLength(window) + 1;
[email protected]fdce4782011-11-29 20:06:18107 if (len > 1)
108 ::GetWindowText(window, WriteInto(&caption, len), len);
[email protected]a711df102010-12-14 09:47:03109 bool chrome_window_in_foreground =
110 EndsWith(caption, L" - Google Chrome", true) ||
111 EndsWith(caption, L" - Chromium", true);
112 if (!chrome_window_in_foreground) {
113 DWORD process_id;
114 int thread_id = ::GetWindowThreadProcessId(window, &process_id);
115
116 base::ProcessHandle process;
117 if (base::OpenProcessHandleWithAccess(process_id,
118 PROCESS_QUERY_LIMITED_INFORMATION,
119 &process)) {
[email protected]fdce4782011-11-29 20:06:18120 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH),
121 MAX_PATH)) {
[email protected]a711df102010-12-14 09:47:03122 int error = GetLastError();
123 filename = std::wstring(L"Unable to read filename for process id '" +
124 base::IntToString16(process_id) +
125 L"' (error ") +
126 base::IntToString16(error) + L")";
127 }
128 base::CloseProcessHandle(process);
129 }
130 }
131 EXPECT_TRUE(chrome_window_in_foreground)
132 << "Chrome must be in the foreground when running interactive tests\n"
133 << "Process in foreground: " << filename.c_str() << "\n"
134 << "Window: " << window << "\n"
135 << "Caption: " << caption.c_str();
136 return chrome_window_in_foreground;
137#else
138 // Windows only at the moment.
139 return true;
140#endif
141}
142
[email protected]a9c060ca2012-01-05 20:43:41143// Wait the focus change in message loop.
144void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) {
145 if (ui_test_utils::IsViewFocused(browser, id) ||
146 base::Time::Now() > timeout) {
147 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
148 } else {
149 MessageLoop::current()->PostDelayedTask(
150 FROM_HERE,
151 base::Bind(&CheckFocus, browser, id, timeout),
[email protected]7e560102012-03-08 20:58:42152 base::TimeDelta::FromMilliseconds(10));
[email protected]a9c060ca2012-01-05 20:43:41153 }
154};
155
[email protected]8bcdec92009-02-25 16:15:18156class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:29157 public:
[email protected]0b8fa8b2011-12-07 00:54:52158 BrowserFocusTest() :
159#if defined(USE_AURA)
160 location_bar_focus_view_id_(VIEW_ID_OMNIBOX)
161#else
162 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR)
163#endif
[email protected]90ca44272012-07-18 18:15:48164 {}
[email protected]b9821882009-08-17 22:25:17165
[email protected]21abcc742009-10-23 02:52:06166 bool IsViewFocused(ViewID vid) {
167 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:17168 }
169
[email protected]fc2e0872009-08-21 22:14:41170 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:06171 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:41172 }
[email protected]0b8fa8b2011-12-07 00:54:52173
[email protected]a9c060ca2012-01-05 20:43:41174 bool WaitForFocusChange(ViewID vid) {
175 const base::Time timeout = base::Time::Now() +
176 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs);
177 MessageLoop::current()->PostDelayedTask(
178 FROM_HERE,
179 base::Bind(&CheckFocus, browser(), vid, timeout),
[email protected]7e560102012-03-08 20:58:42180 base::TimeDelta::FromMilliseconds(100));
[email protected]729eb632012-07-26 04:45:26181 content::RunMessageLoop();
[email protected]a9c060ca2012-01-05 20:43:41182 return IsViewFocused(vid);
183 }
184
[email protected]0b8fa8b2011-12-07 00:54:52185 ViewID location_bar_focus_view_id_;
initial.commit09911bf2008-07-26 23:55:29186};
187
[email protected]85f0a572012-02-07 22:20:13188class TestInterstitialPage : public content::InterstitialPageDelegate {
[email protected]9e0c83a2009-05-06 19:44:37189 public:
[email protected]85f0a572012-02-07 22:20:13190 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) {
[email protected]b65de8b92009-09-14 19:36:31191 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37192 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
193 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31194 file_path = file_path.AppendASCII("focus");
195 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37196 r = file_util::ReadFileToString(file_path, &html_contents_);
197 EXPECT_TRUE(r);
[email protected]85f0a572012-02-07 22:20:13198 interstitial_page_ = InterstitialPage::Create(
199 tab, new_navigation, url , this);
200 interstitial_page_->Show();
[email protected]9e0c83a2009-05-06 19:44:37201 }
202
203 virtual std::string GetHTMLContents() {
204 return html_contents_;
205 }
206
[email protected]85f0a572012-02-07 22:20:13207 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13208 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37209 }
210
[email protected]85f0a572012-02-07 22:20:13211 void DontProceed() {
212 interstitial_page_->DontProceed();
[email protected]4e85c112011-01-26 22:27:21213 }
214
[email protected]9e0c83a2009-05-06 19:44:37215 bool HasFocus() {
[email protected]9f76c1e2012-03-05 15:15:58216 return render_view_host()->GetView()->HasFocus();
[email protected]9e0c83a2009-05-06 19:44:37217 }
218
219 private:
220 std::string html_contents_;
[email protected]85f0a572012-02-07 22:20:13221 InterstitialPage* interstitial_page_; // Owns us.
[email protected]9e0c83a2009-05-06 19:44:37222};
[email protected]b9821882009-08-17 22:25:17223
[email protected]c39e233d2012-05-18 13:24:12224// Flaky on mac. http://crbug.com/67301.
225#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23226#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12227#else
[email protected]839e0802012-05-18 14:45:23228#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12229#endif
230IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30231 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42232#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41233 // It seems we have to wait a little bit for the widgets to spin up before
234 // we can start clicking on them.
[email protected]7e560102012-03-08 20:58:42235 MessageLoop::current()->PostDelayedTask(
236 FROM_HERE,
237 MessageLoop::QuitClosure(),
238 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26239 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42240#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41241
[email protected]0b8fa8b2011-12-07 00:54:52242 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00243
[email protected]fc2e0872009-08-21 22:14:41244 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21245 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00246
[email protected]fc2e0872009-08-21 22:14:41247 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]0b8fa8b2011-12-07 00:54:52248 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]186f13f2009-08-19 20:34:00249}
[email protected]186f13f2009-08-19 20:34:00250
[email protected]320948e02011-01-10 08:21:12251// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26252IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30253 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11254 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29255
256 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11257 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18258 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29259
[email protected]90556dd2012-06-07 20:26:18260 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00261
initial.commit09911bf2008-07-26 23:55:29262 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21263 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29264 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57265 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29266 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21267 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29268
[email protected]a37d4b02012-06-25 21:56:10269 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52270 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29271 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57272 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29273 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]0b8fa8b2011-12-07 00:54:52274 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29275
[email protected]186f13f2009-08-19 20:34:00276 // The rest of this test does not make sense on Linux because the behavior
277 // of Activate() is not well defined and can vary by window manager.
278#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29279 // Open a new browser window.
[email protected]a4fe67012012-07-25 20:14:29280 Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile()));
[email protected]8bcdec92009-02-25 16:15:18281 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10282 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18283 browser2->window()->Show();
284 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29285
[email protected]90556dd2012-06-07 20:26:18286 gfx::NativeWindow window2 = browser2->window()->GetNativeWindow();
[email protected]4a507a62009-05-28 00:10:00287 BrowserView* browser_view2 =
[email protected]075b204c2011-09-30 19:02:16288 BrowserView::GetBrowserViewForBrowser(browser2);
[email protected]8bcdec92009-02-25 16:15:18289 ASSERT_TRUE(browser_view2);
[email protected]0aaa5282011-10-26 18:54:50290 const views::Widget* widget2 =
291 views::Widget::GetWidgetForNativeWindow(window2);
[email protected]0c966682011-08-02 18:22:10292 ASSERT_TRUE(widget2);
[email protected]0aaa5282011-10-26 18:54:50293 const views::FocusManager* focus_manager2 = widget2->GetFocusManager();
[email protected]8bcdec92009-02-25 16:15:18294 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54295 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38296 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29297
298 // Switch to the 1st browser window, focus should still be on the location
299 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18300 browser()->window()->Activate();
[email protected]0b8fa8b2011-12-07 00:54:52301 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]8bcdec92009-02-25 16:15:18302 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29303
304 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18305 browser2->window()->Activate();
[email protected]0c966682011-08-02 18:22:10306 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
307 ASSERT_TRUE(widget);
308 EXPECT_EQ(NULL, widget->GetFocusManager()->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54309 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38310 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18311
312 // Close the 2nd browser to avoid a DCHECK().
313 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00314#endif
initial.commit09911bf2008-07-26 23:55:29315}
316
317// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39318// Disabled, http://crbug.com/62542.
319IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30320 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11321 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29322
323 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11324 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18325 ui_test_utils::NavigateToURL(browser(), url);
326
initial.commit09911bf2008-07-26 23:55:29327 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03328 for (int i = 0; i < 4; ++i) {
329 chrome::AddSelectedTabWithURL(browser(), url,
330 content::PAGE_TRANSITION_TYPED);
331 }
initial.commit09911bf2008-07-26 23:55:29332
333 // Alternate focus for the tab.
334 const bool kFocusPage[3][5] = {
335 { true, true, true, true, false },
336 { false, false, false, false, false },
337 { false, true, false, true, false }
338 };
339
340 for (int i = 1; i < 3; i++) {
341 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18342 // Activate the tab.
[email protected]57892182012-12-03 19:15:39343 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29344
345 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54346 if (kFocusPage[i][j]) {
[email protected]57892182012-12-03 19:15:39347 browser()->tab_strip_model()->GetWebContentsAt(j)->GetView()->Focus();
[email protected]7e383692009-06-12 19:14:54348 } else {
[email protected]a37d4b02012-06-25 21:56:10349 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54350 }
initial.commit09911bf2008-07-26 23:55:29351 }
352
353 // Now come back to the tab and check the right view is focused.
354 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18355 // Activate the tab.
[email protected]57892182012-12-03 19:15:39356 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29357
[email protected]f2159ba2012-04-17 19:13:21358 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52359 location_bar_focus_view_id_;
[email protected]21abcc742009-10-23 02:52:06360 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29361 }
[email protected]cb7e2542009-12-14 22:02:35362
[email protected]57892182012-12-03 19:15:39363 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35364 // Try the above, but with ctrl+tab. Since tab normally changes focus,
365 // this has regressed in the past. Loop through several times to be sure.
366 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21367 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52368 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35369 ASSERT_TRUE(IsViewFocused(vid));
370
[email protected]1d000682010-08-23 16:21:28371 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09372 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35373 }
374
375 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39376 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35377 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21378 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]0b8fa8b2011-12-07 00:54:52379 location_bar_focus_view_id_;
[email protected]cb7e2542009-12-14 22:02:35380 ASSERT_TRUE(IsViewFocused(vid));
381
[email protected]1d000682010-08-23 16:21:28382 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09383 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35384 }
initial.commit09911bf2008-07-26 23:55:29385 }
386}
387
[email protected]ae40b572009-10-02 21:17:45388// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35389IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30390 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11391 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45392
393 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11394 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45395 ui_test_utils::NavigateToURL(browser(), url);
396
[email protected]a37d4b02012-06-25 21:56:10397 chrome::Find(browser());
[email protected]ba3d09a12012-11-07 12:13:52398 ui_test_utils::FindInPage(chrome::GetActiveWebContents(browser()),
[email protected]864ebb52012-08-17 17:53:02399 ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06400 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45401
402 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10403 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45404
405 // Create a 2nd tab.
[email protected]52877dbc62012-06-29 22:22:03406 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45407
408 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21409 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45410
411 // Select 1st tab, focus should still be on the location-bar.
412 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39413 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]0b8fa8b2011-12-07 00:54:52414 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]ae40b572009-10-02 21:17:45415
416 // Now open the find box again, switch to another tab and come back, the focus
417 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10418 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06419 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39420 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21421 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39422 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06423 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45424}
425
initial.commit09911bf2008-07-26 23:55:29426// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07427// Flaky, http://crbug.com/62538.
428IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26429 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30430 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11431 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29432
initial.commit09911bf2008-07-26 23:55:29433 // Open a new browser window.
[email protected]a4fe67012012-07-25 20:14:29434 Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile()));
[email protected]8bcdec92009-02-25 16:15:18435 ASSERT_TRUE(browser2);
[email protected]686221c2012-11-08 07:30:10436 chrome::AddBlankTabAt(browser2, -1, true);
[email protected]8bcdec92009-02-25 16:15:18437 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00438
[email protected]ed179ee2009-10-03 21:02:51439 Browser* focused_browser = NULL;
440 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16441#if defined(USE_X11)
442 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00443 // to figure out which browser does have focus.
444 if (browser2->window()->IsActive()) {
445 focused_browser = browser2;
446 unfocused_browser = browser();
447 } else if (browser()->window()->IsActive()) {
448 focused_browser = browser();
449 unfocused_browser = browser2;
450 } else {
[email protected]579c3d82010-10-06 03:53:51451 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00452 }
453#elif defined(OS_WIN)
454 focused_browser = browser();
455 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57456#elif defined(OS_MACOSX)
457 // On Mac, the newly created window always gets the focus.
458 focused_browser = browser2;
459 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00460#endif
461
[email protected]95409e12010-08-17 20:07:11462 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00463 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46464
[email protected]8bcdec92009-02-25 16:15:18465 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00466 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29467
[email protected]7d478cb2012-07-24 17:19:42468 ASSERT_TRUE(content::ExecuteJavaScript(
[email protected]52877dbc62012-06-29 22:22:03469 chrome::GetActiveWebContents(unfocused_browser)->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51470 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29471
[email protected]8bcdec92009-02-25 16:15:18472 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00473 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29474}
475
476// Page cannot steal focus when focus is on location bar.
[email protected]7ef7c6e2012-10-30 12:32:44477IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30478 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11479 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29480
481 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11482 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18483 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29484
[email protected]a37d4b02012-06-25 21:56:10485 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29486
[email protected]7d478cb2012-07-24 17:19:42487 ASSERT_TRUE(content::ExecuteJavaScript(
[email protected]52877dbc62012-06-29 22:22:03488 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"",
[email protected]579c3d82010-10-06 03:53:51489 L"stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29490
491 // Make sure the location bar is still focused.
[email protected]0b8fa8b2011-12-07 00:54:52492 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29493}
494
[email protected]9e0c83a2009-05-06 19:44:37495// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35496// Note that this test relies on a notification from the renderer that the
497// focus has changed in the page. The notification in the renderer may change
498// at which point this test would fail (see comment in
499// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57500IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30501 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11502 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29503
[email protected]8bcdec92009-02-25 16:15:18504 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11505 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18506 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29507
[email protected]a37d4b02012-06-25 21:56:10508 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29509
[email protected]546ae4e02010-12-08 14:57:19510 const char* kTextElementID = "textEdit";
[email protected]8bcdec92009-02-25 16:15:18511 const char* kExpElementIDs[] = {
512 "", // Initially no element in the page should be focused
513 // (the location bar is focused).
[email protected]546ae4e02010-12-08 14:57:19514 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink",
[email protected]8bcdec92009-02-25 16:15:18515 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29516 };
517
518 // Test forward focus traversal.
519 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40520 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29521 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52522 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29523
[email protected]911696b2011-01-28 02:36:49524 // Move the caret to the end, otherwise the next Tab key may not move focus.
525 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
526 browser(), ui::VKEY_END, false, false, false, false));
527
initial.commit09911bf2008-07-26 23:55:29528 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35529 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40530 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29531 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01532 std::string actual;
[email protected]7d478cb2012-07-24 17:19:42533 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString(
[email protected]52877dbc62012-06-29 22:22:03534 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18535 L"",
[email protected]45671612009-04-29 22:24:01536 L"window.domAutomationController.send(getFocusedElement());",
537 &actual));
initial.commit09911bf2008-07-26 23:55:29538 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
539
[email protected]130efb02009-09-18 18:54:35540 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19541 // If the next element is the kTextElementID, we expect to be
542 // notified we have switched to an editable node.
543 bool is_editable_node =
544 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0);
[email protected]6c2381d2011-10-19 02:52:53545 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19546
547 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09548 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27549 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56550 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]52877dbc62012-06-29 22:22:03551 chrome::GetActiveWebContents(browser())->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19552 details));
[email protected]130efb02009-09-18 18:54:35553 } else {
554 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19555 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09556 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]432115822011-07-10 15:52:27557 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53558 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35559 }
initial.commit09911bf2008-07-26 23:55:29560 }
[email protected]8bcdec92009-02-25 16:15:18561
562 // At this point the renderer has sent us a message asking to advance the
563 // focus (as the end of the focus loop was reached in the renderer).
564 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44565 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29566 }
567
568 // Now let's try reverse focus traversal.
569 for (int i = 0; i < 3; ++i) {
[email protected]1870d5cf2011-05-12 01:55:40570 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29571 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52572 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
initial.commit09911bf2008-07-26 23:55:29573
[email protected]911696b2011-01-28 02:36:49574 // Move the caret to the end, otherwise the next Tab key may not move focus.
575 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
576 browser(), ui::VKEY_END, false, false, false, false));
577
[email protected]8bcdec92009-02-25 16:15:18578 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30579 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]1870d5cf2011-05-12 01:55:40580 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j));
[email protected]546ae4e02010-12-08 14:57:19581 const char* next_element =
582 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j];
[email protected]130efb02009-09-18 18:54:35583
584 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]546ae4e02010-12-08 14:57:19585 // If the next element is the kTextElementID, we expect to be
586 // notified we have switched to an editable node.
587 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0);
[email protected]6c2381d2011-10-19 02:52:53588 content::Details<bool> details(&is_editable_node);
[email protected]546ae4e02010-12-08 14:57:19589
590 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
[email protected]b6d81262011-01-13 17:36:09591 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27592 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]d53092652011-12-06 06:12:56593 content::NotificationSource(content::Source<RenderViewHost>(
[email protected]52877dbc62012-06-29 22:22:03594 chrome::GetActiveWebContents(browser())->GetRenderViewHost())),
[email protected]546ae4e02010-12-08 14:57:19595 details));
[email protected]130efb02009-09-18 18:54:35596 } else {
597 // On the last tab key press, the focus returns to the browser.
[email protected]546ae4e02010-12-08 14:57:19598 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09599 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]432115822011-07-10 15:52:27600 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:53601 content::NotificationSource(content::Source<Browser>(browser()))));
[email protected]130efb02009-09-18 18:54:35602 }
initial.commit09911bf2008-07-26 23:55:29603
604 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01605 std::string actual;
[email protected]7d478cb2012-07-24 17:19:42606 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString(
[email protected]52877dbc62012-06-29 22:22:03607 chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
[email protected]8bcdec92009-02-25 16:15:18608 L"",
[email protected]45671612009-04-29 22:24:01609 L"window.domAutomationController.send(getFocusedElement());",
610 &actual));
[email protected]546ae4e02010-12-08 14:57:19611 ASSERT_STREQ(next_element, actual.c_str());
initial.commit09911bf2008-07-26 23:55:29612 }
[email protected]8bcdec92009-02-25 16:15:18613
614 // At this point the renderer has sent us a message asking to advance the
615 // focus (as the end of the focus loop was reached in the renderer).
616 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44617 content::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29618 }
619}
620
[email protected]9e0c83a2009-05-06 19:44:37621// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41622IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30623 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11624 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37625
626 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11627 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37628 ui_test_utils::NavigateToURL(browser(), url);
629
[email protected]9e0c83a2009-05-06 19:44:37630 // Focus should be on the page.
[email protected]f2159ba2012-04-17 19:13:21631 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37632
633 // Let's show an interstitial.
634 TestInterstitialPage* interstitial_page =
[email protected]52877dbc62012-06-29 22:22:03635 new TestInterstitialPage(chrome::GetActiveWebContents(browser()),
[email protected]9e0c83a2009-05-06 19:44:37636 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37637 // Give some time for the interstitial to show.
638 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17639 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42640 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26641 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37642
[email protected]a37d4b02012-06-25 21:56:10643 chrome::FocusLocationBar(browser());
[email protected]9e0c83a2009-05-06 19:44:37644
645 const char* kExpElementIDs[] = {
646 "", // Initially no element in the page should be focused
647 // (the location bar is focused).
648 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
649 "gmapLink"
650 };
651
652 // Test forward focus traversal.
653 for (int i = 0; i < 2; ++i) {
654 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52655 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37656
[email protected]911696b2011-01-28 02:36:49657 // Move the caret to the end, otherwise the next Tab key may not move focus.
658 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
659 browser(), ui::VKEY_END, false, false, false, false));
660
[email protected]9e0c83a2009-05-06 19:44:37661 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35662 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37663 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30664 std::string actual;
[email protected]7d478cb2012-07-24 17:19:42665 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString(
[email protected]a6e602f2010-09-28 22:28:30666 interstitial_page->render_view_host(), L"",
667 L"window.domAutomationController.send(getFocusedElement());",
668 &actual));
[email protected]9e0c83a2009-05-06 19:44:37669 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
670
[email protected]432115822011-07-10 15:52:27671 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53672 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07673 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35674 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27675 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56676 notification_source = content::Source<RenderViewHost>(
677 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35678 } else {
679 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27680 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53681 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35682 }
[email protected]a6e602f2010-09-28 22:28:30683
684 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09685 browser(), ui::VKEY_TAB, false, false, false, false,
[email protected]a6e602f2010-09-28 22:28:30686 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37687 }
688
689 // At this point the renderer has sent us a message asking to advance the
690 // focus (as the end of the focus loop was reached in the renderer).
691 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44692 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37693 }
694
695 // Now let's try reverse focus traversal.
696 for (int i = 0; i < 2; ++i) {
697 // Location bar should be focused.
[email protected]0b8fa8b2011-12-07 00:54:52698 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9e0c83a2009-05-06 19:44:37699
[email protected]911696b2011-01-28 02:36:49700 // Move the caret to the end, otherwise the next Tab key may not move focus.
701 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
702 browser(), ui::VKEY_END, false, false, false, false));
703
[email protected]9e0c83a2009-05-06 19:44:37704 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35705 for (size_t j = 0; j < 7; ++j) {
[email protected]432115822011-07-10 15:52:27706 int notification_type;
[email protected]6c2381d2011-10-19 02:52:53707 content::NotificationSource notification_source =
[email protected]ad50def52011-10-19 23:17:07708 content::NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35709 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]432115822011-07-10 15:52:27710 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE;
[email protected]d53092652011-12-06 06:12:56711 notification_source = content::Source<RenderViewHost>(
712 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35713 } else {
714 // On the last tab key press, the focus returns to the browser.
[email protected]432115822011-07-10 15:52:27715 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER;
[email protected]6c2381d2011-10-19 02:52:53716 notification_source = content::Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35717 }
[email protected]9e0c83a2009-05-06 19:44:37718
[email protected]a6e602f2010-09-28 22:28:30719 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
[email protected]b6d81262011-01-13 17:36:09720 browser(), ui::VKEY_TAB, false, true, false, false,
[email protected]a6e602f2010-09-28 22:28:30721 notification_type, notification_source));
722
[email protected]9e0c83a2009-05-06 19:44:37723 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30724 std::string actual;
[email protected]7d478cb2012-07-24 17:19:42725 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString(
[email protected]a6e602f2010-09-28 22:28:30726 interstitial_page->render_view_host(), L"",
727 L"window.domAutomationController.send(getFocusedElement());",
728 &actual));
[email protected]9e0c83a2009-05-06 19:44:37729 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
730 }
731
732 // At this point the renderer has sent us a message asking to advance the
733 // focus (as the end of the focus loop was reached in the renderer).
734 // We need to run the message loop to process it.
[email protected]b8deecd2012-07-30 21:09:44735 content::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37736 }
737}
738
739// Focus stays on page with interstitials.
[email protected]cb931932011-05-03 19:57:33740// http://crbug.com/81451
[email protected]fd5a53e2011-09-15 14:09:05741#if defined(OS_MACOSX) || defined(OS_WIN)
[email protected]4d2451652012-02-14 23:54:26742IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33743#else
[email protected]e4f4e0b2009-10-13 19:58:21744IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]cb931932011-05-03 19:57:33745#endif
[email protected]a6e602f2010-09-28 22:28:30746 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11747 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37748
749 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11750 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37751 ui_test_utils::NavigateToURL(browser(), url);
752
[email protected]9e0c83a2009-05-06 19:44:37753 // Page should have focus.
[email protected]f2159ba2012-04-17 19:13:21754 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]52877dbc62012-06-29 22:22:03755 EXPECT_TRUE(chrome::GetActiveWebContents(browser())->GetRenderViewHost()->
[email protected]9f76c1e2012-03-05 15:15:58756 GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37757
[email protected]9d8a4642009-07-29 17:25:30758 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37759 TestInterstitialPage* interstitial_page =
[email protected]52877dbc62012-06-29 22:22:03760 new TestInterstitialPage(chrome::GetActiveWebContents(browser()),
[email protected]9e0c83a2009-05-06 19:44:37761 true, GURL("http://interstitial.com"));
[email protected]9e0c83a2009-05-06 19:44:37762 // Give some time for the interstitial to show.
763 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17764 MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42765 base::TimeDelta::FromSeconds(1));
[email protected]729eb632012-07-26 04:45:26766 content::RunMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37767
768 // The interstitial should have focus now.
[email protected]f2159ba2012-04-17 19:13:21769 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37770 EXPECT_TRUE(interstitial_page->HasFocus());
771
772 // Hide the interstitial.
773 interstitial_page->DontProceed();
774
775 // Focus should be back on the original page.
[email protected]f2159ba2012-04-17 19:13:21776 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37777}
778
[email protected]9bd491ee2008-12-10 22:31:07779// Make sure Find box can request focus, even when it is already open.
[email protected]06e85dfd2012-10-31 01:32:35780// Disabled due to flakiness. http://crbug.com/67301.
781IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30782 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11783 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55784
[email protected]9bd491ee2008-12-10 22:31:07785 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11786 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18787 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07788
[email protected]a711df102010-12-14 09:47:03789 EXPECT_TRUE(ChromeInForeground());
790
[email protected]853300a82010-07-27 21:17:57791#if defined(OS_MACOSX)
792 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28793 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09794 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57795#else
[email protected]9bd491ee2008-12-10 22:31:07796 // Press Ctrl+F, which will make the Find box open and request focus.
[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, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57799#endif
[email protected]8bcdec92009-02-25 16:15:18800
[email protected]a9c060ca2012-01-05 20:43:41801 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07802
[email protected]a37d4b02012-06-25 21:56:10803 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52804 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9bd491ee2008-12-10 22:31:07805
806 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57807#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28808 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09809 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57810#else
[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, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57813#endif
[email protected]21abcc742009-10-23 02:52:06814 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07815
816 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41817 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21818 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07819
820 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57821#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28822 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09823 browser(), ui::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57824#else
[email protected]1d000682010-08-23 16:21:28825 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09826 browser(), ui::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57827#endif
[email protected]8bcdec92009-02-25 16:15:18828
[email protected]a9c060ca2012-01-05 20:43:41829 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07830}
[email protected]401513c2009-03-12 00:21:28831
832// Makes sure the focus is in the right location when opening the different
833// types of tabs.
[email protected]c92928672010-11-09 18:31:07834// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26835IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30836 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07837
[email protected]401513c2009-03-12 00:21:28838 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12839 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35840 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03841 chrome::GetActiveWebContents(browser())));
[email protected]f2159ba2012-04-17 19:13:21842 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28843
844 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10845 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35846 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03847 chrome::GetActiveWebContents(browser())));
[email protected]0b8fa8b2011-12-07 00:54:52848 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28849
850 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12851 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35852 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03853 chrome::GetActiveWebContents(browser())));
[email protected]f2159ba2012-04-17 19:13:21854 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43855
856 // Open about:blank, focus should be on the location bar.
[email protected]52877dbc62012-06-29 22:22:03857 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL),
858 content::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35859 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]52877dbc62012-06-29 22:22:03860 chrome::GetActiveWebContents(browser())));
[email protected]0b8fa8b2011-12-07 00:54:52861 EXPECT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]401513c2009-03-12 00:21:28862}
[email protected]9d8a4642009-07-29 17:25:30863
864// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21865IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30866 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11867 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30868
[email protected]9d8a4642009-07-29 17:25:30869 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06870 {
[email protected]a7fe9112012-07-20 02:34:45871 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06872 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07873 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10874 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06875 observer.Wait();
876 }
[email protected]b8deecd2012-07-30 21:09:44877 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42878
[email protected]6b4e8e42011-08-17 19:36:06879 {
[email protected]a7fe9112012-07-20 02:34:45880 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06881 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50882 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03883 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10884 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06885 observer.Wait();
886 }
[email protected]9d8a4642009-07-29 17:25:30887 // Focus should stay on the location bar.
[email protected]0b8fa8b2011-12-07 00:54:52888 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]9d8a4642009-07-29 17:25:30889
890 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11891 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10892 chrome::FocusLocationBar(browser());
[email protected]0b8fa8b2011-12-07 00:54:52893 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_));
[email protected]6b4e8e42011-08-17 19:36:06894 {
[email protected]a7fe9112012-07-20 02:34:45895 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06896 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50897 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03898 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10899 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06900 observer.Wait();
901 }
[email protected]9ba21ede2010-07-30 01:11:07902
[email protected]9d8a4642009-07-29 17:25:30903 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12904 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21905 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30906}
907
908// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27909IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30910 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11911 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30912
[email protected]9d8a4642009-07-29 17:25:30913 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11914 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]e0f3e142012-07-26 03:31:34915 content::CrashTab(chrome::GetActiveWebContents(browser()));
[email protected]6b4e8e42011-08-17 19:36:06916 {
[email protected]a7fe9112012-07-20 02:34:45917 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06918 content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50919 content::Source<NavigationController>(
[email protected]52877dbc62012-06-29 22:22:03920 &chrome::GetActiveWebContents(browser())->GetController()));
[email protected]a37d4b02012-06-25 21:56:10921 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06922 observer.Wait();
923 }
[email protected]9ba21ede2010-07-30 01:11:07924
[email protected]9d8a4642009-07-29 17:25:30925 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12926 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21927 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30928}
[email protected]629e0342010-07-27 23:30:13929
[email protected]895a84722012-11-29 18:45:44930// Tests that when a new tab is opened from the omnibox, the focus is moved from
931// the omnibox for the current tab.
932IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
933 NavigateFromOmniboxIntoNewTab) {
934 GURL url("http://www.google.com/");
935 GURL url2("http://maps.google.com/");
936
937 // Navigate to url.
938 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK);
939 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
940 p.disposition = CURRENT_TAB;
941 chrome::Navigate(&p);
942
943 // Focus the omnibox.
944 chrome::FocusLocationBar(browser());
945
946 OmniboxEditController* controller =
947 browser()->window()->GetLocationBar()->GetLocationEntry()->model()->
948 controller();
949
950 // Simulate an alt-enter.
951 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
952 content::PAGE_TRANSITION_TYPED, GURL());
953
954 // Make sure the second tab is selected.
955 EXPECT_EQ(1, browser()->active_index());
956
957 // The tab contents should have the focus in the second tab.
958 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
959
960 // Go back to the first tab. The focus should not be in the omnibox.
961 chrome::SelectPreviousTab(browser());
962 EXPECT_EQ(0, browser()->active_index());
963 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(),
964 VIEW_ID_LOCATION_BAR));
965}
966
[email protected]629e0342010-07-27 23:30:13967} // namespace