blob: 90a3b1937d631908699e20071131c4e356159010 [file] [log] [blame]
[email protected]265ccd92010-04-29 17:57:171// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]d9fde8d2009-10-08 19:59:305#include "build/build_config.h"
6
[email protected]3985ba82010-07-29 21:44:127#include "base/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:428#include "base/format_macros.h"
[email protected]8bcdec92009-02-25 16:15:189#include "base/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1210#include "base/path_service.h"
[email protected]34877b32010-07-31 17:47:0511#include "base/string_util.h"
[email protected]be1ce6a72010-08-03 14:35:2212#include "base/utf_string_conversions.h"
[email protected]8bcdec92009-02-25 16:15:1813#include "chrome/browser/browser.h"
[email protected]134c47b92009-08-19 03:33:4414#include "chrome/browser/browser_window.h"
15#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3716#include "chrome/browser/renderer_host/render_widget_host_view.h"
17#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0018#include "chrome/browser/tab_contents/tab_contents.h"
19#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]c848d3d92010-09-16 21:57:4520#include "chrome/browser/tabs/tab_strip_model.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3722#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1823#include "chrome/test/in_process_browser_test.h"
24#include "chrome/test/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1225#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5726
27#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0528#include "views/focus/focus_manager.h"
29#include "views/view.h"
30#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5731#endif
initial.commit09911bf2008-07-26 23:55:2932
[email protected]134c47b92009-08-19 03:33:4433#if defined(TOOLKIT_VIEWS)
34#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1735#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4436#include "chrome/browser/views/tab_contents/tab_contents_container.h"
37#endif
38
[email protected]753efc42010-03-09 19:52:1639#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1740#include "chrome/browser/gtk/view_id_util.h"
41#endif
42
[email protected]fc2e0872009-08-21 22:14:4143#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5744#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1845#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3546// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5947#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5748#elif defined(OS_MACOSX)
49// TODO(suzhe): http://crbug.com/49738 (following two tests)
50#define MAYBE_FocusTraversal FAILS_FocusTraversal
51#define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial
52// TODO(suzhe): http://crbug.com/49737
53#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
54#elif defined(OS_WIN)
55#define MAYBE_FocusTraversal FocusTraversal
[email protected]e4f4e0b2009-10-13 19:58:2156#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3557#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4158#endif
59
initial.commit09911bf2008-07-26 23:55:2960namespace {
61
[email protected]8bcdec92009-02-25 16:15:1862// The delay waited in some cases where we don't have a notifications for an
63// action we take.
initial.commit09911bf2008-07-26 23:55:2964const int kActionDelayMs = 500;
65
[email protected]f72a1cc2010-04-30 07:17:3066const char kSimplePage[] = "files/focus/page_with_focus.html";
67const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
68const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3169const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2970
[email protected]8bcdec92009-02-25 16:15:1871class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2972 public:
73 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1874 set_show_window(true);
75 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2976 }
[email protected]b9821882009-08-17 22:25:1777
[email protected]21abcc742009-10-23 02:52:0678 bool IsViewFocused(ViewID vid) {
79 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1780 }
81
[email protected]fc2e0872009-08-21 22:14:4182 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0683 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4184 }
initial.commit09911bf2008-07-26 23:55:2985};
86
[email protected]9e0c83a2009-05-06 19:44:3787class TestInterstitialPage : public InterstitialPage {
88 public:
89 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
[email protected]a6e602f2010-09-28 22:28:3090 : InterstitialPage(tab, new_navigation, url) {
[email protected]b65de8b92009-09-14 19:36:3191 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:3792 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
93 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:3194 file_path = file_path.AppendASCII("focus");
95 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:3796 r = file_util::ReadFileToString(file_path, &html_contents_);
97 EXPECT_TRUE(r);
98 }
99
100 virtual std::string GetHTMLContents() {
101 return html_contents_;
102 }
103
[email protected]a6e602f2010-09-28 22:28:30104 // Exposing render_view_host() to be public; it is declared as protected in
105 // the superclass.
106 virtual RenderViewHost* render_view_host() {
107 return InterstitialPage::render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37108 }
109
110 bool HasFocus() {
111 return render_view_host()->view()->HasFocus();
112 }
113
[email protected]130efb02009-09-18 18:54:35114 protected:
115 virtual void FocusedNodeChanged() {
[email protected]a6e602f2010-09-28 22:28:30116 NotificationService::current()->Notify(
117 NotificationType::FOCUS_CHANGED_IN_PAGE,
118 Source<RenderViewHost>(render_view_host()),
119 NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:35120 }
121
[email protected]9e0c83a2009-05-06 19:44:37122 private:
123 std::string html_contents_;
[email protected]9e0c83a2009-05-06 19:44:37124};
[email protected]b9821882009-08-17 22:25:17125
[email protected]e4f4e0b2009-10-13 19:58:21126IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30127 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]853300a82010-07-27 21:17:57128#if defined(USE_X11) || defined(OS_MACOSX)
[email protected]fc2e0872009-08-21 22:14:41129 // It seems we have to wait a little bit for the widgets to spin up before
130 // we can start clicking on them.
131 MessageLoop::current()->PostDelayedTask(FROM_HERE,
132 new MessageLoop::QuitTask(),
133 kActionDelayMs);
134 ui_test_utils::RunMessageLoop();
135#endif
136
[email protected]21abcc742009-10-23 02:52:06137 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00138
[email protected]fc2e0872009-08-21 22:14:41139 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06140 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00141
[email protected]fc2e0872009-08-21 22:14:41142 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06143 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00144}
[email protected]186f13f2009-08-19 20:34:00145
[email protected]e4f4e0b2009-10-13 19:58:21146IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30147 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11148 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29149
150 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11151 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18152 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29153
[email protected]186f13f2009-08-19 20:34:00154 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
155
initial.commit09911bf2008-07-26 23:55:29156 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06157 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29158 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57159 ui_test_utils::HideNativeWindow(window);
160 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06161 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29162
[email protected]186f13f2009-08-19 20:34:00163 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06164 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29165 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57166 ui_test_utils::HideNativeWindow(window);
167 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06168 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29169
[email protected]186f13f2009-08-19 20:34:00170 // The rest of this test does not make sense on Linux because the behavior
171 // of Activate() is not well defined and can vary by window manager.
172#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29173 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18174 Browser* browser2 = Browser::Create(browser()->profile());
175 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43176 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18177 browser2->window()->Show();
178 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29179
[email protected]8bcdec92009-02-25 16:15:18180 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00181 BrowserView* browser_view2 =
182 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18183 ASSERT_TRUE(browser_view2);
184 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00185 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18186 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54187 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38188 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29189
190 // Switch to the 1st browser window, focus should still be on the location
191 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18192 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06193 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18194 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29195
196 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18197 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00198 EXPECT_EQ(NULL,
199 views::FocusManager::GetFocusManagerForNativeView(
200 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54201 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38202 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18203
204 // Close the 2nd browser to avoid a DCHECK().
205 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00206#endif
initial.commit09911bf2008-07-26 23:55:29207}
208
209// Tabs remember focus.
[email protected]e4f4e0b2009-10-13 19:58:21210IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30211 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11212 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29213
214 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11215 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18216 ui_test_utils::NavigateToURL(browser(), url);
217
initial.commit09911bf2008-07-26 23:55:29218 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58219 for (int i = 0; i < 4; ++i) {
[email protected]b283a7532010-08-12 21:24:59220 Browser* browser_used = NULL;
[email protected]715af7e2010-04-29 01:55:38221 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]b283a7532010-08-12 21:24:59222 TabStripModel::ADD_SELECTED, NULL, std::string(),
223 &browser_used);
224 EXPECT_EQ(browser(), browser_used);
[email protected]22735af62009-04-07 21:09:58225 }
initial.commit09911bf2008-07-26 23:55:29226
227 // Alternate focus for the tab.
228 const bool kFocusPage[3][5] = {
229 { true, true, true, true, false },
230 { false, false, false, false, false },
231 { false, true, false, true, false }
232 };
233
234 for (int i = 1; i < 3; i++) {
235 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18236 // Activate the tab.
237 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29238
239 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54240 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00241 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54242 } else {
[email protected]186f13f2009-08-19 20:34:00243 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54244 }
initial.commit09911bf2008-07-26 23:55:29245 }
246
247 // Now come back to the tab and check the right view is focused.
248 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18249 // Activate the tab.
250 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29251
[email protected]186f13f2009-08-19 20:34:00252 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
253 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06254 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29255 }
[email protected]cb7e2542009-12-14 22:02:35256
[email protected]cb7e2542009-12-14 22:02:35257 browser()->SelectTabContentsAt(0, true);
258 // Try the above, but with ctrl+tab. Since tab normally changes focus,
259 // this has regressed in the past. Loop through several times to be sure.
260 for (int j = 0; j < 15; j++) {
261 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
262 VIEW_ID_LOCATION_BAR;
263 ASSERT_TRUE(IsViewFocused(vid));
264
[email protected]1d000682010-08-23 16:21:28265 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30266 browser(), app::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35267 }
268
269 // As above, but with ctrl+shift+tab.
270 browser()->SelectTabContentsAt(4, true);
271 for (int j = 14; j >= 0; --j) {
272 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
273 VIEW_ID_LOCATION_BAR;
274 ASSERT_TRUE(IsViewFocused(vid));
275
[email protected]1d000682010-08-23 16:21:28276 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30277 browser(), app::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35278 }
initial.commit09911bf2008-07-26 23:55:29279 }
280}
281
[email protected]ae40b572009-10-02 21:17:45282// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35283IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30284 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11285 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45286
287 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11288 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45289 ui_test_utils::NavigateToURL(browser(), url);
290
291 browser()->Find();
292 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
293 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06294 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45295
296 // Focus the location bar.
297 browser()->FocusLocationBar();
298
299 // Create a 2nd tab.
[email protected]b283a7532010-08-12 21:24:59300 Browser* browser_used = NULL;
[email protected]4a1665442010-06-28 16:09:39301 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]b283a7532010-08-12 21:24:59302 TabStripModel::ADD_SELECTED, NULL, std::string(),
303 &browser_used);
304 EXPECT_EQ(browser(), browser_used);
[email protected]ae40b572009-10-02 21:17:45305
306 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06307 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45308
309 // Select 1st tab, focus should still be on the location-bar.
310 // (bug http://crbug.com/23296)
311 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06312 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45313
314 // Now open the find box again, switch to another tab and come back, the focus
315 // should return to the find box.
316 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06317 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45318 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06319 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45320 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06321 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45322}
323
initial.commit09911bf2008-07-26 23:55:29324// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21325IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30326 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11327 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29328
329 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11330 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18331 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29332
333 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18334 Browser* browser2 = Browser::Create(browser()->profile());
335 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43336 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18337 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00338
[email protected]ed179ee2009-10-03 21:02:51339 Browser* focused_browser = NULL;
340 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16341#if defined(USE_X11)
342 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00343 // to figure out which browser does have focus.
344 if (browser2->window()->IsActive()) {
345 focused_browser = browser2;
346 unfocused_browser = browser();
347 } else if (browser()->window()->IsActive()) {
348 focused_browser = browser();
349 unfocused_browser = browser2;
350 } else {
351 ASSERT_TRUE(false);
352 }
353#elif defined(OS_WIN)
354 focused_browser = browser();
355 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57356#elif defined(OS_MACOSX)
357 // On Mac, the newly created window always gets the focus.
358 focused_browser = browser2;
359 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00360#endif
361
[email protected]95409e12010-08-17 20:07:11362 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00363 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46364
[email protected]8bcdec92009-02-25 16:15:18365 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00366 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29367
368 // Wait for the focus to be stolen by the other browser.
[email protected]f07d7bf2010-04-06 08:02:42369 MessageLoop::current()->PostDelayedTask(
370 FROM_HERE, new MessageLoop::QuitTask(), 2000);
371 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29372
[email protected]8bcdec92009-02-25 16:15:18373 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00374 EXPECT_TRUE(focused_browser->window()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18375
376 // Close the 2nd browser to avoid a DCHECK().
[email protected]186f13f2009-08-19 20:34:00377 browser2->window()->Close();
initial.commit09911bf2008-07-26 23:55:29378}
379
380// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21381IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30382 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11383 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29384
385 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11386 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18387 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29388
[email protected]186f13f2009-08-19 20:34:00389 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29390
391 // Wait for the page to steal focus.
[email protected]186f13f2009-08-19 20:34:00392 PlatformThread::Sleep(2000);
initial.commit09911bf2008-07-26 23:55:29393
394 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06395 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29396}
397
[email protected]9e0c83a2009-05-06 19:44:37398// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35399// Note that this test relies on a notification from the renderer that the
400// focus has changed in the page. The notification in the renderer may change
401// at which point this test would fail (see comment in
402// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57403IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30404 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11405 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29406
[email protected]8bcdec92009-02-25 16:15:18407 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11408 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18409 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29410
[email protected]186f13f2009-08-19 20:34:00411 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29412
[email protected]8bcdec92009-02-25 16:15:18413 const char* kExpElementIDs[] = {
414 "", // Initially no element in the page should be focused
415 // (the location bar is focused).
416 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
417 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29418 };
419
420 // Test forward focus traversal.
421 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42422 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29423 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06424 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29425
426 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35427 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42428 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29429 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01430 std::string actual;
431 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25432 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18433 L"",
[email protected]45671612009-04-29 22:24:01434 L"window.domAutomationController.send(getFocusedElement());",
435 &actual));
initial.commit09911bf2008-07-26 23:55:29436 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
437
[email protected]a6e602f2010-09-28 22:28:30438 NotificationType::Type notification_type;
439 NotificationSource notification_source =
440 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35441 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30442 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
443 notification_source = Source<RenderViewHost>(
444 browser()->GetSelectedTabContents()->render_view_host());
[email protected]130efb02009-09-18 18:54:35445 } else {
446 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30447 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
448 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35449 }
[email protected]a6e602f2010-09-28 22:28:30450
451 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
452 browser(), app::VKEY_TAB, false, false, false, false,
453 notification_type, notification_source));
initial.commit09911bf2008-07-26 23:55:29454 }
[email protected]8bcdec92009-02-25 16:15:18455
456 // At this point the renderer has sent us a message asking to advance the
457 // focus (as the end of the focus loop was reached in the renderer).
458 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42459 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29460 }
461
462 // Now let's try reverse focus traversal.
463 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42464 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29465 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06466 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29467
[email protected]8bcdec92009-02-25 16:15:18468 // Now let's press shift-tab to move the focus in reverse.
[email protected]a6e602f2010-09-28 22:28:30469 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42470 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35471
[email protected]a6e602f2010-09-28 22:28:30472 NotificationType::Type notification_type;
473 NotificationSource notification_source =
474 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35475 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30476 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
477 notification_source = Source<RenderViewHost>(
478 browser()->GetSelectedTabContents()->render_view_host());
[email protected]130efb02009-09-18 18:54:35479 } else {
480 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30481 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
482 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35483 }
initial.commit09911bf2008-07-26 23:55:29484
[email protected]a6e602f2010-09-28 22:28:30485 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
486 browser(), app::VKEY_TAB, false, true, false, false,
487 notification_type, notification_source));
488
initial.commit09911bf2008-07-26 23:55:29489 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01490 std::string actual;
491 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25492 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18493 L"",
[email protected]45671612009-04-29 22:24:01494 L"window.domAutomationController.send(getFocusedElement());",
495 &actual));
initial.commit09911bf2008-07-26 23:55:29496 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
497 }
[email protected]8bcdec92009-02-25 16:15:18498
499 // At this point the renderer has sent us a message asking to advance the
500 // focus (as the end of the focus loop was reached in the renderer).
501 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42502 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29503 }
504}
505
[email protected]9e0c83a2009-05-06 19:44:37506// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41507IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30508 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11509 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37510
511 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11512 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37513 ui_test_utils::NavigateToURL(browser(), url);
514
[email protected]9e0c83a2009-05-06 19:44:37515 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06516 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37517
518 // Let's show an interstitial.
519 TestInterstitialPage* interstitial_page =
520 new TestInterstitialPage(browser()->GetSelectedTabContents(),
521 true, GURL("http://interstitial.com"));
522 interstitial_page->Show();
523 // Give some time for the interstitial to show.
524 MessageLoop::current()->PostDelayedTask(FROM_HERE,
525 new MessageLoop::QuitTask(),
526 1000);
527 ui_test_utils::RunMessageLoop();
528
[email protected]fc2e0872009-08-21 22:14:41529 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37530
531 const char* kExpElementIDs[] = {
532 "", // Initially no element in the page should be focused
533 // (the location bar is focused).
534 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
535 "gmapLink"
536 };
537
538 // Test forward focus traversal.
539 for (int i = 0; i < 2; ++i) {
540 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06541 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37542
543 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35544 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37545 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30546 std::string actual;
547 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
548 interstitial_page->render_view_host(), L"",
549 L"window.domAutomationController.send(getFocusedElement());",
550 &actual));
[email protected]9e0c83a2009-05-06 19:44:37551 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
552
[email protected]a6e602f2010-09-28 22:28:30553 NotificationType::Type notification_type;
554 NotificationSource notification_source =
555 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35556 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30557 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
558 notification_source = Source<RenderViewHost>(
559 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35560 } else {
561 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30562 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
563 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35564 }
[email protected]a6e602f2010-09-28 22:28:30565
566 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
567 browser(), app::VKEY_TAB, false, false, false, false,
568 notification_type, notification_source));
[email protected]9e0c83a2009-05-06 19:44:37569 }
570
571 // At this point the renderer has sent us a message asking to advance the
572 // focus (as the end of the focus loop was reached in the renderer).
573 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42574 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37575 }
576
577 // Now let's try reverse focus traversal.
578 for (int i = 0; i < 2; ++i) {
579 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06580 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37581
582 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35583 for (size_t j = 0; j < 7; ++j) {
[email protected]a6e602f2010-09-28 22:28:30584 NotificationType::Type notification_type;
585 NotificationSource notification_source =
586 NotificationService::AllSources();
[email protected]130efb02009-09-18 18:54:35587 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]a6e602f2010-09-28 22:28:30588 notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE;
589 notification_source = Source<RenderViewHost>(
590 interstitial_page->render_view_host());
[email protected]130efb02009-09-18 18:54:35591 } else {
592 // On the last tab key press, the focus returns to the browser.
[email protected]a6e602f2010-09-28 22:28:30593 notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER;
594 notification_source = Source<Browser>(browser());
[email protected]130efb02009-09-18 18:54:35595 }
[email protected]9e0c83a2009-05-06 19:44:37596
[email protected]a6e602f2010-09-28 22:28:30597 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
598 browser(), app::VKEY_TAB, false, true, false, false,
599 notification_type, notification_source));
600
[email protected]9e0c83a2009-05-06 19:44:37601 // Let's make sure the focus is on the expected element in the page.
[email protected]a6e602f2010-09-28 22:28:30602 std::string actual;
603 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
604 interstitial_page->render_view_host(), L"",
605 L"window.domAutomationController.send(getFocusedElement());",
606 &actual));
[email protected]9e0c83a2009-05-06 19:44:37607 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
608 }
609
610 // At this point the renderer has sent us a message asking to advance the
611 // focus (as the end of the focus loop was reached in the renderer).
612 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42613 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37614 }
615}
616
617// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21618IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30619 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11620 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37621
622 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11623 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37624 ui_test_utils::NavigateToURL(browser(), url);
625
[email protected]9e0c83a2009-05-06 19:44:37626 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06627 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37628 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
629 HasFocus());
630
[email protected]9d8a4642009-07-29 17:25:30631 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37632 TestInterstitialPage* interstitial_page =
633 new TestInterstitialPage(browser()->GetSelectedTabContents(),
634 true, GURL("http://interstitial.com"));
635 interstitial_page->Show();
636 // Give some time for the interstitial to show.
637 MessageLoop::current()->PostDelayedTask(FROM_HERE,
638 new MessageLoop::QuitTask(),
639 1000);
640 ui_test_utils::RunMessageLoop();
641
642 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06643 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37644 EXPECT_TRUE(interstitial_page->HasFocus());
645
646 // Hide the interstitial.
647 interstitial_page->DontProceed();
648
649 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06650 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37651}
652
[email protected]9bd491ee2008-12-10 22:31:07653// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21654IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]a6e602f2010-09-28 22:28:30655 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11656 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55657
[email protected]9bd491ee2008-12-10 22:31:07658 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11659 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18660 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07661
[email protected]853300a82010-07-27 21:17:57662#if defined(OS_MACOSX)
663 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28664 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30665 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57666#else
[email protected]9bd491ee2008-12-10 22:31:07667 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28668 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30669 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57670#endif
[email protected]8bcdec92009-02-25 16:15:18671
672 // Ideally, we wouldn't sleep here and instead would intercept the
673 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
674 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
675 // - creating a factory used to create the delegate
676 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41677 MessageLoop::current()->PostDelayedTask(
678 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18679 ui_test_utils::RunMessageLoop();
680
[email protected]21abcc742009-10-23 02:52:06681 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07682
[email protected]fc2e0872009-08-21 22:14:41683 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06684 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07685
686 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57687#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28688 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30689 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57690#else
[email protected]1d000682010-08-23 16:21:28691 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30692 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57693#endif
[email protected]21abcc742009-10-23 02:52:06694 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07695
696 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41697 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06698 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07699
700 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57701#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28702 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30703 browser(), app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57704#else
[email protected]1d000682010-08-23 16:21:28705 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]a6e602f2010-09-28 22:28:30706 browser(), app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57707#endif
[email protected]8bcdec92009-02-25 16:15:18708
709 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41710 MessageLoop::current()->PostDelayedTask(
711 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18712 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06713 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07714}
[email protected]401513c2009-03-12 00:21:28715
716// Makes sure the focus is in the right location when opening the different
717// types of tabs.
[email protected]30667d42010-07-30 17:59:16718// Flaky, http://crbug.com/50763.
719IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30720 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07721
[email protected]401513c2009-03-12 00:21:28722 // Open the history tab, focus should be on the tab contents.
723 browser()->ShowHistoryTab();
[email protected]f07d7bf2010-04-06 08:02:42724 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]21abcc742009-10-23 02:52:06725 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28726
727 // Open the new tab, focus should be on the location bar.
728 browser()->NewTab();
[email protected]21abcc742009-10-23 02:52:06729 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28730
731 // Open the download tab, focus should be on the tab contents.
732 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06733 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43734
735 // Open about:blank, focus should be on the location bar.
736 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK,
[email protected]4a1665442010-06-28 16:09:39737 -1, TabStripModel::ADD_SELECTED, NULL,
[email protected]b283a7532010-08-12 21:24:59738 std::string(),
739 NULL);
[email protected]21abcc742009-10-23 02:52:06740 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28741}
[email protected]9d8a4642009-07-29 17:25:30742
743// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21744IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30745 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11746 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30747
[email protected]9d8a4642009-07-29 17:25:30748 // Open the new tab, reload.
749 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42750 ui_test_utils::RunAllPendingInMessageLoop();
751
[email protected]cb84d642010-06-10 00:56:28752 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40753 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30754 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06755 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30756
757 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11758 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41759 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06760 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28761 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40762 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07763
[email protected]9d8a4642009-07-29 17:25:30764 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41765 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06766 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30767}
768
[email protected]0e7c82ed2010-08-31 20:39:19769#if (defined(OS_CHROMEOS) || defined(OS_LINUX)) && !defined(NDEBUG)
770// Hangy, http://crbug.com/50025.
[email protected]c38a1902010-07-23 20:55:15771#define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab
[email protected]517d2fe72010-07-23 15:40:41772#else
773#define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab
774#endif
775
[email protected]9d8a4642009-07-29 17:25:30776// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]517d2fe72010-07-23 15:40:41777IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30778 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]95409e12010-08-17 20:07:11779 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30780
[email protected]9d8a4642009-07-29 17:25:30781 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11782 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30783 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28784 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40785 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07786
[email protected]9d8a4642009-07-29 17:25:30787 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41788 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06789 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30790}
[email protected]629e0342010-07-27 23:30:13791
792} // namespace