blob: 03824b951f3ae66f414ae451ecf242cca6e1ceab [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]853300a82010-07-27 21:17:579#include "base/logging.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]ece3c8b2009-03-27 16:55:3912#include "base/ref_counted.h"
[email protected]34877b32010-07-31 17:47:0513#include "base/string_util.h"
[email protected]be1ce6a72010-08-03 14:35:2214#include "base/utf_string_conversions.h"
[email protected]8bcdec92009-02-25 16:15:1815#include "chrome/browser/automation/ui_controls.h"
16#include "chrome/browser/browser.h"
[email protected]134c47b92009-08-19 03:33:4417#include "chrome/browser/browser_window.h"
18#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3719#include "chrome/browser/renderer_host/render_widget_host_view.h"
20#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0021#include "chrome/browser/tab_contents/tab_contents.h"
22#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]c848d3d92010-09-16 21:57:4523#include "chrome/browser/tabs/tab_strip_model.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3725#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1826#include "chrome/test/in_process_browser_test.h"
27#include "chrome/test/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1228#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5729
30#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0531#include "views/focus/focus_manager.h"
32#include "views/view.h"
33#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5734#endif
initial.commit09911bf2008-07-26 23:55:2935
[email protected]134c47b92009-08-19 03:33:4436#if defined(TOOLKIT_VIEWS)
37#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1738#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4439#include "chrome/browser/views/tab_contents/tab_contents_container.h"
40#endif
41
[email protected]753efc42010-03-09 19:52:1642#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1743#include "chrome/browser/gtk/view_id_util.h"
44#endif
45
[email protected]fc2e0872009-08-21 22:14:4146#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5747#define MAYBE_FocusTraversal FocusTraversal
[email protected]252250d2010-09-27 18:36:1848#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3549// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5950#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5751#elif defined(OS_MACOSX)
52// TODO(suzhe): http://crbug.com/49738 (following two tests)
53#define MAYBE_FocusTraversal FAILS_FocusTraversal
54#define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial
55// TODO(suzhe): http://crbug.com/49737
56#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
57#elif defined(OS_WIN)
58#define MAYBE_FocusTraversal FocusTraversal
[email protected]e4f4e0b2009-10-13 19:58:2159#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3560#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4161#endif
62
initial.commit09911bf2008-07-26 23:55:2963namespace {
64
[email protected]8bcdec92009-02-25 16:15:1865// The delay waited in some cases where we don't have a notifications for an
66// action we take.
initial.commit09911bf2008-07-26 23:55:2967const int kActionDelayMs = 500;
68
[email protected]f72a1cc2010-04-30 07:17:3069const char kSimplePage[] = "files/focus/page_with_focus.html";
70const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
71const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3172const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2973
[email protected]8bcdec92009-02-25 16:15:1874class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2975 public:
76 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1877 set_show_window(true);
78 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2979 }
[email protected]b9821882009-08-17 22:25:1780
[email protected]21abcc742009-10-23 02:52:0681 bool IsViewFocused(ViewID vid) {
82 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1783 }
84
[email protected]fc2e0872009-08-21 22:14:4185 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0686 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4187 }
88
[email protected]853300a82010-07-27 21:17:5789 void BringBrowserWindowToFront() {
90 ui_test_utils::ShowAndFocusNativeWindow(
91 browser()->window()->GetNativeHandle());
[email protected]186f13f2009-08-19 20:34:0092 }
initial.commit09911bf2008-07-26 23:55:2993};
94
[email protected]9e0c83a2009-05-06 19:44:3795class TestInterstitialPage : public InterstitialPage {
96 public:
97 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
98 : InterstitialPage(tab, new_navigation, url),
[email protected]130efb02009-09-18 18:54:3599 waiting_for_dom_response_(false),
100 waiting_for_focus_change_(false) {
[email protected]b65de8b92009-09-14 19:36:31101 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37102 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
103 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31104 file_path = file_path.AppendASCII("focus");
105 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37106 r = file_util::ReadFileToString(file_path, &html_contents_);
107 EXPECT_TRUE(r);
108 }
109
110 virtual std::string GetHTMLContents() {
111 return html_contents_;
112 }
113
114 virtual void DomOperationResponse(const std::string& json_string,
115 int automation_id) {
116 if (waiting_for_dom_response_) {
117 dom_response_ = json_string;
118 waiting_for_dom_response_ = false;
119 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
120 return;
121 }
122 InterstitialPage::DomOperationResponse(json_string, automation_id);
123 }
124
125 std::string GetFocusedElement() {
126 std::wstring script = L"window.domAutomationController.setAutomationId(0);"
127 L"window.domAutomationController.send(getFocusedElement());";
128
129 render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
130 DCHECK(!waiting_for_dom_response_);
131 waiting_for_dom_response_ = true;
132 ui_test_utils::RunMessageLoop();
133 // Remove the JSON extra quotes.
134 if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
135 dom_response_[dom_response_.size() - 1] == '"') {
136 dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
137 }
138 return dom_response_;
139 }
140
141 bool HasFocus() {
142 return render_view_host()->view()->HasFocus();
143 }
144
[email protected]130efb02009-09-18 18:54:35145 void WaitForFocusChange() {
146 waiting_for_focus_change_ = true;
147 ui_test_utils::RunMessageLoop();
148 }
149
150 protected:
151 virtual void FocusedNodeChanged() {
152 if (!waiting_for_focus_change_)
153 return;
154
155 waiting_for_focus_change_= false;
156 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
157 }
158
[email protected]9e0c83a2009-05-06 19:44:37159 private:
160 std::string html_contents_;
161
162 bool waiting_for_dom_response_;
[email protected]130efb02009-09-18 18:54:35163 bool waiting_for_focus_change_;
[email protected]9e0c83a2009-05-06 19:44:37164 std::string dom_response_;
[email protected]9e0c83a2009-05-06 19:44:37165};
[email protected]b9821882009-08-17 22:25:17166
[email protected]e4f4e0b2009-10-13 19:58:21167IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]853300a82010-07-27 21:17:57168 BringBrowserWindowToFront();
169#if defined(USE_X11) || defined(OS_MACOSX)
[email protected]fc2e0872009-08-21 22:14:41170 // It seems we have to wait a little bit for the widgets to spin up before
171 // we can start clicking on them.
172 MessageLoop::current()->PostDelayedTask(FROM_HERE,
173 new MessageLoop::QuitTask(),
174 kActionDelayMs);
175 ui_test_utils::RunMessageLoop();
176#endif
177
[email protected]21abcc742009-10-23 02:52:06178 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00179
[email protected]fc2e0872009-08-21 22:14:41180 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06181 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00182
[email protected]fc2e0872009-08-21 22:14:41183 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06184 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00185}
[email protected]186f13f2009-08-19 20:34:00186
[email protected]e4f4e0b2009-10-13 19:58:21187IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]853300a82010-07-27 21:17:57188 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11189 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29190
191 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11192 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18193 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29194
[email protected]186f13f2009-08-19 20:34:00195 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
196
initial.commit09911bf2008-07-26 23:55:29197 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06198 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29199 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57200 ui_test_utils::HideNativeWindow(window);
201 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06202 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29203
[email protected]186f13f2009-08-19 20:34:00204 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06205 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29206 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57207 ui_test_utils::HideNativeWindow(window);
208 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06209 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29210
[email protected]186f13f2009-08-19 20:34:00211 // The rest of this test does not make sense on Linux because the behavior
212 // of Activate() is not well defined and can vary by window manager.
213#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29214 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18215 Browser* browser2 = Browser::Create(browser()->profile());
216 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43217 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18218 browser2->window()->Show();
219 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29220
[email protected]8bcdec92009-02-25 16:15:18221 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00222 BrowserView* browser_view2 =
223 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18224 ASSERT_TRUE(browser_view2);
225 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00226 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18227 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54228 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38229 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29230
231 // Switch to the 1st browser window, focus should still be on the location
232 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18233 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06234 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18235 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29236
237 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18238 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00239 EXPECT_EQ(NULL,
240 views::FocusManager::GetFocusManagerForNativeView(
241 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54242 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38243 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18244
245 // Close the 2nd browser to avoid a DCHECK().
246 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00247#endif
initial.commit09911bf2008-07-26 23:55:29248}
249
250// Tabs remember focus.
[email protected]e4f4e0b2009-10-13 19:58:21251IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
[email protected]853300a82010-07-27 21:17:57252 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11253 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29254
255 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11256 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18257 ui_test_utils::NavigateToURL(browser(), url);
258
initial.commit09911bf2008-07-26 23:55:29259 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58260 for (int i = 0; i < 4; ++i) {
[email protected]b283a7532010-08-12 21:24:59261 Browser* browser_used = NULL;
[email protected]715af7e2010-04-29 01:55:38262 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]b283a7532010-08-12 21:24:59263 TabStripModel::ADD_SELECTED, NULL, std::string(),
264 &browser_used);
265 EXPECT_EQ(browser(), browser_used);
[email protected]22735af62009-04-07 21:09:58266 }
initial.commit09911bf2008-07-26 23:55:29267
268 // Alternate focus for the tab.
269 const bool kFocusPage[3][5] = {
270 { true, true, true, true, false },
271 { false, false, false, false, false },
272 { false, true, false, true, false }
273 };
274
275 for (int i = 1; i < 3; i++) {
276 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18277 // Activate the tab.
278 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29279
280 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54281 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00282 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54283 } else {
[email protected]186f13f2009-08-19 20:34:00284 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54285 }
initial.commit09911bf2008-07-26 23:55:29286 }
287
288 // Now come back to the tab and check the right view is focused.
289 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18290 // Activate the tab.
291 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29292
[email protected]186f13f2009-08-19 20:34:00293 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
294 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06295 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29296 }
[email protected]cb7e2542009-12-14 22:02:35297
298 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
299 browser()->SelectTabContentsAt(0, true);
300 // Try the above, but with ctrl+tab. Since tab normally changes focus,
301 // this has regressed in the past. Loop through several times to be sure.
302 for (int j = 0; j < 15; j++) {
303 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
304 VIEW_ID_LOCATION_BAR;
305 ASSERT_TRUE(IsViewFocused(vid));
306
[email protected]1d000682010-08-23 16:21:28307 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27308 window, app::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35309 }
310
311 // As above, but with ctrl+shift+tab.
312 browser()->SelectTabContentsAt(4, true);
313 for (int j = 14; j >= 0; --j) {
314 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
315 VIEW_ID_LOCATION_BAR;
316 ASSERT_TRUE(IsViewFocused(vid));
317
[email protected]1d000682010-08-23 16:21:28318 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27319 window, app::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35320 }
initial.commit09911bf2008-07-26 23:55:29321 }
322}
323
[email protected]ae40b572009-10-02 21:17:45324// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35325IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]853300a82010-07-27 21:17:57326 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11327 ASSERT_TRUE(test_server()->Start());
[email protected]ae40b572009-10-02 21:17:45328
329 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11330 GURL url = test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45331 ui_test_utils::NavigateToURL(browser(), url);
332
333 browser()->Find();
334 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
335 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06336 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45337
338 // Focus the location bar.
339 browser()->FocusLocationBar();
340
341 // Create a 2nd tab.
[email protected]b283a7532010-08-12 21:24:59342 Browser* browser_used = NULL;
[email protected]4a1665442010-06-28 16:09:39343 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]b283a7532010-08-12 21:24:59344 TabStripModel::ADD_SELECTED, NULL, std::string(),
345 &browser_used);
346 EXPECT_EQ(browser(), browser_used);
[email protected]ae40b572009-10-02 21:17:45347
348 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06349 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45350
351 // Select 1st tab, focus should still be on the location-bar.
352 // (bug http://crbug.com/23296)
353 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06354 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45355
356 // Now open the find box again, switch to another tab and come back, the focus
357 // should return to the find box.
358 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06359 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45360 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06361 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45362 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06363 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45364}
365
initial.commit09911bf2008-07-26 23:55:29366// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21367IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]853300a82010-07-27 21:17:57368 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11369 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29370
371 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11372 GURL url = test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18373 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29374
375 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18376 Browser* browser2 = Browser::Create(browser()->profile());
377 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43378 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18379 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00380
[email protected]ed179ee2009-10-03 21:02:51381 Browser* focused_browser = NULL;
382 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16383#if defined(USE_X11)
384 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00385 // to figure out which browser does have focus.
386 if (browser2->window()->IsActive()) {
387 focused_browser = browser2;
388 unfocused_browser = browser();
389 } else if (browser()->window()->IsActive()) {
390 focused_browser = browser();
391 unfocused_browser = browser2;
392 } else {
393 ASSERT_TRUE(false);
394 }
395#elif defined(OS_WIN)
396 focused_browser = browser();
397 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57398#elif defined(OS_MACOSX)
399 // On Mac, the newly created window always gets the focus.
400 focused_browser = browser2;
401 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00402#endif
403
[email protected]95409e12010-08-17 20:07:11404 GURL steal_focus_url = test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00405 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46406
[email protected]8bcdec92009-02-25 16:15:18407 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00408 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29409
410 // Wait for the focus to be stolen by the other browser.
[email protected]f07d7bf2010-04-06 08:02:42411 MessageLoop::current()->PostDelayedTask(
412 FROM_HERE, new MessageLoop::QuitTask(), 2000);
413 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29414
[email protected]8bcdec92009-02-25 16:15:18415 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00416 EXPECT_TRUE(focused_browser->window()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18417
418 // Close the 2nd browser to avoid a DCHECK().
[email protected]186f13f2009-08-19 20:34:00419 browser2->window()->Close();
initial.commit09911bf2008-07-26 23:55:29420}
421
422// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21423IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]853300a82010-07-27 21:17:57424 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11425 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29426
427 // Open the page that steals focus.
[email protected]95409e12010-08-17 20:07:11428 GURL url = test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18429 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29430
[email protected]186f13f2009-08-19 20:34:00431 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29432
433 // Wait for the page to steal focus.
[email protected]186f13f2009-08-19 20:34:00434 PlatformThread::Sleep(2000);
initial.commit09911bf2008-07-26 23:55:29435
436 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06437 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29438}
439
[email protected]9e0c83a2009-05-06 19:44:37440// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35441// Note that this test relies on a notification from the renderer that the
442// focus has changed in the page. The notification in the renderer may change
443// at which point this test would fail (see comment in
444// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57445IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
446 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11447 ASSERT_TRUE(test_server()->Start());
initial.commit09911bf2008-07-26 23:55:29448
[email protected]8bcdec92009-02-25 16:15:18449 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11450 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18451 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29452
[email protected]186f13f2009-08-19 20:34:00453 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29454
[email protected]8bcdec92009-02-25 16:15:18455 const char* kExpElementIDs[] = {
456 "", // Initially no element in the page should be focused
457 // (the location bar is focused).
458 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
459 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29460 };
461
[email protected]186f13f2009-08-19 20:34:00462 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
463
initial.commit09911bf2008-07-26 23:55:29464 // Test forward focus traversal.
465 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42466 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29467 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06468 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29469
470 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35471 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42472 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29473 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01474 std::string actual;
475 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25476 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18477 L"",
[email protected]45671612009-04-29 22:24:01478 L"window.domAutomationController.send(getFocusedElement());",
479 &actual));
initial.commit09911bf2008-07-26 23:55:29480 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
481
[email protected]7f05552482010-09-03 00:38:27482 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30483 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35484
485 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]1d000682010-08-23 16:21:28486 ASSERT_NO_FATAL_FAILURE(
487 ui_test_utils::WaitForFocusChange(
488 browser()->GetSelectedTabContents()->render_view_host()));
[email protected]130efb02009-09-18 18:54:35489 } else {
490 // On the last tab key press, the focus returns to the browser.
[email protected]1d000682010-08-23 16:21:28491 ASSERT_NO_FATAL_FAILURE(
492 ui_test_utils::WaitForFocusInBrowser(browser()));
[email protected]130efb02009-09-18 18:54:35493 }
initial.commit09911bf2008-07-26 23:55:29494 }
[email protected]8bcdec92009-02-25 16:15:18495
496 // At this point the renderer has sent us a message asking to advance the
497 // focus (as the end of the focus loop was reached in the renderer).
498 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42499 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29500 }
501
502 // Now let's try reverse focus traversal.
503 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42504 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29505 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06506 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29507
[email protected]8bcdec92009-02-25 16:15:18508 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35509 for (size_t j = 0; j < 7; ++j) {
[email protected]f07d7bf2010-04-06 08:02:42510 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]7f05552482010-09-03 00:38:27511 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30512 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35513
514 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]1d000682010-08-23 16:21:28515 ASSERT_NO_FATAL_FAILURE(
516 ui_test_utils::WaitForFocusChange(
517 browser()->GetSelectedTabContents()->render_view_host()));
[email protected]130efb02009-09-18 18:54:35518 } else {
519 // On the last tab key press, the focus returns to the browser.
[email protected]1d000682010-08-23 16:21:28520 ASSERT_NO_FATAL_FAILURE(
521 ui_test_utils::WaitForFocusInBrowser(browser()));
[email protected]130efb02009-09-18 18:54:35522 }
initial.commit09911bf2008-07-26 23:55:29523
524 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01525 std::string actual;
526 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25527 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18528 L"",
[email protected]45671612009-04-29 22:24:01529 L"window.domAutomationController.send(getFocusedElement());",
530 &actual));
initial.commit09911bf2008-07-26 23:55:29531 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
532 }
[email protected]8bcdec92009-02-25 16:15:18533
534 // At this point the renderer has sent us a message asking to advance the
535 // focus (as the end of the focus loop was reached in the renderer).
536 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42537 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29538 }
539}
540
[email protected]9e0c83a2009-05-06 19:44:37541// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41542IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]853300a82010-07-27 21:17:57543 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11544 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37545
546 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11547 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37548 ui_test_utils::NavigateToURL(browser(), url);
549
[email protected]9e0c83a2009-05-06 19:44:37550 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06551 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37552
553 // Let's show an interstitial.
554 TestInterstitialPage* interstitial_page =
555 new TestInterstitialPage(browser()->GetSelectedTabContents(),
556 true, GURL("http://interstitial.com"));
557 interstitial_page->Show();
558 // Give some time for the interstitial to show.
559 MessageLoop::current()->PostDelayedTask(FROM_HERE,
560 new MessageLoop::QuitTask(),
561 1000);
562 ui_test_utils::RunMessageLoop();
563
[email protected]fc2e0872009-08-21 22:14:41564 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37565
566 const char* kExpElementIDs[] = {
567 "", // Initially no element in the page should be focused
568 // (the location bar is focused).
569 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
570 "gmapLink"
571 };
572
[email protected]fc2e0872009-08-21 22:14:41573 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
574
[email protected]9e0c83a2009-05-06 19:44:37575 // Test forward focus traversal.
576 for (int i = 0; i < 2; ++i) {
577 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06578 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37579
580 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35581 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37582 // Let's make sure the focus is on the expected element in the page.
583 std::string actual = interstitial_page->GetFocusedElement();
584 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
585
[email protected]7f05552482010-09-03 00:38:27586 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30587 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35588
589 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]1d000682010-08-23 16:21:28590 ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange());
[email protected]130efb02009-09-18 18:54:35591 } else {
592 // On the last tab key press, the focus returns to the browser.
[email protected]1d000682010-08-23 16:21:28593 ASSERT_NO_FATAL_FAILURE(
594 ui_test_utils::WaitForFocusInBrowser(browser()));
[email protected]130efb02009-09-18 18:54:35595 }
[email protected]9e0c83a2009-05-06 19:44:37596 }
597
598 // At this point the renderer has sent us a message asking to advance the
599 // focus (as the end of the focus loop was reached in the renderer).
600 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42601 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37602 }
603
604 // Now let's try reverse focus traversal.
605 for (int i = 0; i < 2; ++i) {
606 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06607 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37608
609 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35610 for (size_t j = 0; j < 7; ++j) {
[email protected]7f05552482010-09-03 00:38:27611 ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30612 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35613
614 if (j < arraysize(kExpElementIDs) - 1) {
[email protected]1d000682010-08-23 16:21:28615 ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange());
[email protected]130efb02009-09-18 18:54:35616 } else {
617 // On the last tab key press, the focus returns to the browser.
[email protected]1d000682010-08-23 16:21:28618 ASSERT_NO_FATAL_FAILURE(
619 ui_test_utils::WaitForFocusInBrowser(browser()));
[email protected]130efb02009-09-18 18:54:35620 }
[email protected]9e0c83a2009-05-06 19:44:37621
622 // Let's make sure the focus is on the expected element in the page.
623 std::string actual = interstitial_page->GetFocusedElement();
624 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
625 }
626
627 // At this point the renderer has sent us a message asking to advance the
628 // focus (as the end of the focus loop was reached in the renderer).
629 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42630 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37631 }
632}
633
634// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21635IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]853300a82010-07-27 21:17:57636 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11637 ASSERT_TRUE(test_server()->Start());
[email protected]9e0c83a2009-05-06 19:44:37638
639 // First we navigate to our test page.
[email protected]95409e12010-08-17 20:07:11640 GURL url = test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37641 ui_test_utils::NavigateToURL(browser(), url);
642
[email protected]9e0c83a2009-05-06 19:44:37643 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06644 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37645 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
646 HasFocus());
647
[email protected]9d8a4642009-07-29 17:25:30648 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37649 TestInterstitialPage* interstitial_page =
650 new TestInterstitialPage(browser()->GetSelectedTabContents(),
651 true, GURL("http://interstitial.com"));
652 interstitial_page->Show();
653 // Give some time for the interstitial to show.
654 MessageLoop::current()->PostDelayedTask(FROM_HERE,
655 new MessageLoop::QuitTask(),
656 1000);
657 ui_test_utils::RunMessageLoop();
658
659 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06660 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37661 EXPECT_TRUE(interstitial_page->HasFocus());
662
663 // Hide the interstitial.
664 interstitial_page->DontProceed();
665
666 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06667 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37668}
669
[email protected]9bd491ee2008-12-10 22:31:07670// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21671IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]853300a82010-07-27 21:17:57672 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11673 ASSERT_TRUE(test_server()->Start());
license.botbf09a502008-08-24 00:55:55674
[email protected]9bd491ee2008-12-10 22:31:07675 // Open some page (any page that doesn't steal focus).
[email protected]95409e12010-08-17 20:07:11676 GURL url = test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18677 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07678
[email protected]fc2e0872009-08-21 22:14:41679 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
[email protected]9bd491ee2008-12-10 22:31:07680
[email protected]853300a82010-07-27 21:17:57681#if defined(OS_MACOSX)
682 // Press Cmd+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28683 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27684 window, app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57685#else
[email protected]9bd491ee2008-12-10 22:31:07686 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]1d000682010-08-23 16:21:28687 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27688 window, app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57689#endif
[email protected]8bcdec92009-02-25 16:15:18690
691 // Ideally, we wouldn't sleep here and instead would intercept the
692 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
693 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
694 // - creating a factory used to create the delegate
695 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41696 MessageLoop::current()->PostDelayedTask(
697 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18698 ui_test_utils::RunMessageLoop();
699
[email protected]21abcc742009-10-23 02:52:06700 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07701
[email protected]fc2e0872009-08-21 22:14:41702 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06703 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07704
705 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57706#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28707 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27708 window, app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57709#else
[email protected]1d000682010-08-23 16:21:28710 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27711 window, app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57712#endif
[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
715 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41716 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06717 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07718
719 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57720#if defined(OS_MACOSX)
[email protected]1d000682010-08-23 16:21:28721 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27722 window, app::VKEY_F, false, false, false, true));
[email protected]853300a82010-07-27 21:17:57723#else
[email protected]1d000682010-08-23 16:21:28724 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]7f05552482010-09-03 00:38:27725 window, app::VKEY_F, true, false, false, false));
[email protected]853300a82010-07-27 21:17:57726#endif
[email protected]8bcdec92009-02-25 16:15:18727
728 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41729 MessageLoop::current()->PostDelayedTask(
730 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18731 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06732 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07733}
[email protected]401513c2009-03-12 00:21:28734
735// Makes sure the focus is in the right location when opening the different
736// types of tabs.
[email protected]30667d42010-07-30 17:59:16737// Flaky, http://crbug.com/50763.
738IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]853300a82010-07-27 21:17:57739 BringBrowserWindowToFront();
[email protected]9ba21ede2010-07-30 01:11:07740
[email protected]401513c2009-03-12 00:21:28741 // Open the history tab, focus should be on the tab contents.
742 browser()->ShowHistoryTab();
[email protected]f07d7bf2010-04-06 08:02:42743 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]21abcc742009-10-23 02:52:06744 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28745
746 // Open the new tab, focus should be on the location bar.
747 browser()->NewTab();
[email protected]21abcc742009-10-23 02:52:06748 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28749
750 // Open the download tab, focus should be on the tab contents.
751 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06752 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43753
754 // Open about:blank, focus should be on the location bar.
755 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK,
[email protected]4a1665442010-06-28 16:09:39756 -1, TabStripModel::ADD_SELECTED, NULL,
[email protected]b283a7532010-08-12 21:24:59757 std::string(),
758 NULL);
[email protected]21abcc742009-10-23 02:52:06759 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28760}
[email protected]9d8a4642009-07-29 17:25:30761
762// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21763IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]853300a82010-07-27 21:17:57764 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11765 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30766
[email protected]9d8a4642009-07-29 17:25:30767 // Open the new tab, reload.
768 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42769 ui_test_utils::RunAllPendingInMessageLoop();
770
[email protected]cb84d642010-06-10 00:56:28771 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40772 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30773 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06774 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30775
776 // Open a regular page, focus the location bar, reload.
[email protected]95409e12010-08-17 20:07:11777 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41778 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06779 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28780 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40781 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07782
[email protected]9d8a4642009-07-29 17:25:30783 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41784 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06785 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30786}
787
[email protected]0e7c82ed2010-08-31 20:39:19788#if (defined(OS_CHROMEOS) || defined(OS_LINUX)) && !defined(NDEBUG)
789// Hangy, http://crbug.com/50025.
[email protected]c38a1902010-07-23 20:55:15790#define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab
[email protected]517d2fe72010-07-23 15:40:41791#else
792#define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab
793#endif
794
[email protected]9d8a4642009-07-29 17:25:30795// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]517d2fe72010-07-23 15:40:41796IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) {
[email protected]853300a82010-07-27 21:17:57797 BringBrowserWindowToFront();
[email protected]95409e12010-08-17 20:07:11798 ASSERT_TRUE(test_server()->Start());
[email protected]9d8a4642009-07-29 17:25:30799
[email protected]9d8a4642009-07-29 17:25:30800 // Open a regular page, crash, reload.
[email protected]95409e12010-08-17 20:07:11801 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30802 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28803 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40804 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07805
[email protected]9d8a4642009-07-29 17:25:30806 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41807 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06808 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30809}
[email protected]629e0342010-07-27 23:30:13810
811} // namespace