blob: 8b421f61723363864e0ba291729a4dd3b493167d [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"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3724#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1825#include "chrome/test/in_process_browser_test.h"
26#include "chrome/test/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1227#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5728
29#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0530#include "views/focus/focus_manager.h"
31#include "views/view.h"
32#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5733#endif
initial.commit09911bf2008-07-26 23:55:2934
[email protected]134c47b92009-08-19 03:33:4435#if defined(TOOLKIT_VIEWS)
36#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1737#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4438#include "chrome/browser/views/tab_contents/tab_contents_container.h"
39#endif
40
[email protected]753efc42010-03-09 19:52:1641#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1742#include "chrome/browser/gtk/view_id_util.h"
43#endif
44
[email protected]fc2e0872009-08-21 22:14:4145#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5746#define MAYBE_FocusTraversal FocusTraversal
[email protected]fc2e0872009-08-21 22:14:4147// For some reason we hit an external DNS lookup in this test in Linux but not
48// on Windows. TODO(estade): investigate.
49#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3550// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5951#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5752#elif defined(OS_MACOSX)
53// TODO(suzhe): http://crbug.com/49738 (following two tests)
54#define MAYBE_FocusTraversal FAILS_FocusTraversal
55#define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial
56// TODO(suzhe): http://crbug.com/49737
57#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
58#elif defined(OS_WIN)
59#define MAYBE_FocusTraversal FocusTraversal
[email protected]e4f4e0b2009-10-13 19:58:2160#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3561#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4162#endif
63
initial.commit09911bf2008-07-26 23:55:2964namespace {
65
[email protected]8bcdec92009-02-25 16:15:1866// The delay waited in some cases where we don't have a notifications for an
67// action we take.
initial.commit09911bf2008-07-26 23:55:2968const int kActionDelayMs = 500;
69
[email protected]f72a1cc2010-04-30 07:17:3070const char kSimplePage[] = "files/focus/page_with_focus.html";
71const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
72const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3173const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2974
[email protected]8bcdec92009-02-25 16:15:1875class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2976 public:
77 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1878 set_show_window(true);
79 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2980 }
[email protected]b9821882009-08-17 22:25:1781
[email protected]21abcc742009-10-23 02:52:0682 bool IsViewFocused(ViewID vid) {
83 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1784 }
85
[email protected]fc2e0872009-08-21 22:14:4186 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0687 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4188 }
89
[email protected]853300a82010-07-27 21:17:5790 void BringBrowserWindowToFront() {
91 ui_test_utils::ShowAndFocusNativeWindow(
92 browser()->window()->GetNativeHandle());
[email protected]186f13f2009-08-19 20:34:0093 }
initial.commit09911bf2008-07-26 23:55:2994};
95
[email protected]9e0c83a2009-05-06 19:44:3796class TestInterstitialPage : public InterstitialPage {
97 public:
98 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
99 : InterstitialPage(tab, new_navigation, url),
[email protected]130efb02009-09-18 18:54:35100 waiting_for_dom_response_(false),
101 waiting_for_focus_change_(false) {
[email protected]b65de8b92009-09-14 19:36:31102 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37103 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
104 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31105 file_path = file_path.AppendASCII("focus");
106 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37107 r = file_util::ReadFileToString(file_path, &html_contents_);
108 EXPECT_TRUE(r);
109 }
110
111 virtual std::string GetHTMLContents() {
112 return html_contents_;
113 }
114
115 virtual void DomOperationResponse(const std::string& json_string,
116 int automation_id) {
117 if (waiting_for_dom_response_) {
118 dom_response_ = json_string;
119 waiting_for_dom_response_ = false;
120 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
121 return;
122 }
123 InterstitialPage::DomOperationResponse(json_string, automation_id);
124 }
125
126 std::string GetFocusedElement() {
127 std::wstring script = L"window.domAutomationController.setAutomationId(0);"
128 L"window.domAutomationController.send(getFocusedElement());";
129
130 render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
131 DCHECK(!waiting_for_dom_response_);
132 waiting_for_dom_response_ = true;
133 ui_test_utils::RunMessageLoop();
134 // Remove the JSON extra quotes.
135 if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
136 dom_response_[dom_response_.size() - 1] == '"') {
137 dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
138 }
139 return dom_response_;
140 }
141
142 bool HasFocus() {
143 return render_view_host()->view()->HasFocus();
144 }
145
[email protected]130efb02009-09-18 18:54:35146 void WaitForFocusChange() {
147 waiting_for_focus_change_ = true;
148 ui_test_utils::RunMessageLoop();
149 }
150
151 protected:
152 virtual void FocusedNodeChanged() {
153 if (!waiting_for_focus_change_)
154 return;
155
156 waiting_for_focus_change_= false;
157 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
158 }
159
[email protected]9e0c83a2009-05-06 19:44:37160 private:
161 std::string html_contents_;
162
163 bool waiting_for_dom_response_;
[email protected]130efb02009-09-18 18:54:35164 bool waiting_for_focus_change_;
[email protected]9e0c83a2009-05-06 19:44:37165 std::string dom_response_;
[email protected]9e0c83a2009-05-06 19:44:37166};
[email protected]b9821882009-08-17 22:25:17167
[email protected]e4f4e0b2009-10-13 19:58:21168IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]853300a82010-07-27 21:17:57169 BringBrowserWindowToFront();
170#if defined(USE_X11) || defined(OS_MACOSX)
[email protected]fc2e0872009-08-21 22:14:41171 // It seems we have to wait a little bit for the widgets to spin up before
172 // we can start clicking on them.
173 MessageLoop::current()->PostDelayedTask(FROM_HERE,
174 new MessageLoop::QuitTask(),
175 kActionDelayMs);
176 ui_test_utils::RunMessageLoop();
177#endif
178
[email protected]21abcc742009-10-23 02:52:06179 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00180
[email protected]fc2e0872009-08-21 22:14:41181 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06182 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00183
[email protected]fc2e0872009-08-21 22:14:41184 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06185 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00186}
[email protected]186f13f2009-08-19 20:34:00187
[email protected]e4f4e0b2009-10-13 19:58:21188IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]853300a82010-07-27 21:17:57189 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12190 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57191 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29192
193 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30194 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18195 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29196
[email protected]186f13f2009-08-19 20:34:00197 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
198
initial.commit09911bf2008-07-26 23:55:29199 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06200 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29201 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57202 ui_test_utils::HideNativeWindow(window);
203 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06204 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29205
[email protected]186f13f2009-08-19 20:34:00206 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06207 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29208 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57209 ui_test_utils::HideNativeWindow(window);
210 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06211 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29212
[email protected]186f13f2009-08-19 20:34:00213 // The rest of this test does not make sense on Linux because the behavior
214 // of Activate() is not well defined and can vary by window manager.
215#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29216 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18217 Browser* browser2 = Browser::Create(browser()->profile());
218 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43219 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18220 browser2->window()->Show();
221 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29222
[email protected]8bcdec92009-02-25 16:15:18223 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00224 BrowserView* browser_view2 =
225 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18226 ASSERT_TRUE(browser_view2);
227 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00228 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18229 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54230 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38231 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29232
233 // Switch to the 1st browser window, focus should still be on the location
234 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18235 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06236 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18237 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29238
239 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18240 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00241 EXPECT_EQ(NULL,
242 views::FocusManager::GetFocusManagerForNativeView(
243 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54244 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38245 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18246
247 // Close the 2nd browser to avoid a DCHECK().
248 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00249#endif
initial.commit09911bf2008-07-26 23:55:29250}
251
252// Tabs remember focus.
[email protected]e4f4e0b2009-10-13 19:58:21253IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
[email protected]853300a82010-07-27 21:17:57254 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12255 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57256 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29257
258 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30259 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18260 ui_test_utils::NavigateToURL(browser(), url);
261
initial.commit09911bf2008-07-26 23:55:29262 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58263 for (int i = 0; i < 4; ++i) {
[email protected]715af7e2010-04-29 01:55:38264 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]4a1665442010-06-28 16:09:39265 TabStripModel::ADD_SELECTED, NULL, std::string());
[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
307 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30308 false, false, false,
[email protected]cb7e2542009-12-14 22:02:35309 new MessageLoop::QuitTask());
310 ui_test_utils::RunMessageLoop();
311 }
312
313 // As above, but with ctrl+shift+tab.
314 browser()->SelectTabContentsAt(4, true);
315 for (int j = 14; j >= 0; --j) {
316 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
317 VIEW_ID_LOCATION_BAR;
318 ASSERT_TRUE(IsViewFocused(vid));
319
320 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30321 true, false, false,
[email protected]cb7e2542009-12-14 22:02:35322 new MessageLoop::QuitTask());
323 ui_test_utils::RunMessageLoop();
324 }
initial.commit09911bf2008-07-26 23:55:29325 }
326}
327
[email protected]ae40b572009-10-02 21:17:45328// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35329IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]853300a82010-07-27 21:17:57330 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12331 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57332 ASSERT_TRUE(server);
[email protected]ae40b572009-10-02 21:17:45333
334 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30335 GURL url = server->TestServerPage(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45336 ui_test_utils::NavigateToURL(browser(), url);
337
338 browser()->Find();
339 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
340 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06341 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45342
343 // Focus the location bar.
344 browser()->FocusLocationBar();
345
346 // Create a 2nd tab.
[email protected]4a1665442010-06-28 16:09:39347 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
348 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]ae40b572009-10-02 21:17:45349
350 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06351 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45352
353 // Select 1st tab, focus should still be on the location-bar.
354 // (bug http://crbug.com/23296)
355 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06356 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45357
358 // Now open the find box again, switch to another tab and come back, the focus
359 // should return to the find box.
360 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06361 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45362 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06363 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45364 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06365 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45366}
367
initial.commit09911bf2008-07-26 23:55:29368// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21369IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]853300a82010-07-27 21:17:57370 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12371 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57372 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29373
374 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30375 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18376 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29377
378 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18379 Browser* browser2 = Browser::Create(browser()->profile());
380 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43381 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18382 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00383
[email protected]ed179ee2009-10-03 21:02:51384 Browser* focused_browser = NULL;
385 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16386#if defined(USE_X11)
387 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00388 // to figure out which browser does have focus.
389 if (browser2->window()->IsActive()) {
390 focused_browser = browser2;
391 unfocused_browser = browser();
392 } else if (browser()->window()->IsActive()) {
393 focused_browser = browser();
394 unfocused_browser = browser2;
395 } else {
396 ASSERT_TRUE(false);
397 }
398#elif defined(OS_WIN)
399 focused_browser = browser();
400 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57401#elif defined(OS_MACOSX)
402 // On Mac, the newly created window always gets the focus.
403 focused_browser = browser2;
404 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00405#endif
406
[email protected]f72a1cc2010-04-30 07:17:30407 GURL steal_focus_url = server->TestServerPage(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00408 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46409
[email protected]8bcdec92009-02-25 16:15:18410 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00411 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29412
413 // Wait for the focus to be stolen by the other browser.
[email protected]f07d7bf2010-04-06 08:02:42414 MessageLoop::current()->PostDelayedTask(
415 FROM_HERE, new MessageLoop::QuitTask(), 2000);
416 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29417
[email protected]8bcdec92009-02-25 16:15:18418 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00419 EXPECT_TRUE(focused_browser->window()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18420
421 // Close the 2nd browser to avoid a DCHECK().
[email protected]186f13f2009-08-19 20:34:00422 browser2->window()->Close();
initial.commit09911bf2008-07-26 23:55:29423}
424
425// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21426IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]853300a82010-07-27 21:17:57427 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12428 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57429 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29430
431 // Open the page that steals focus.
[email protected]f72a1cc2010-04-30 07:17:30432 GURL url = server->TestServerPage(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18433 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29434
[email protected]186f13f2009-08-19 20:34:00435 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29436
437 // Wait for the page to steal focus.
[email protected]186f13f2009-08-19 20:34:00438 PlatformThread::Sleep(2000);
initial.commit09911bf2008-07-26 23:55:29439
440 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06441 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29442}
443
[email protected]9e0c83a2009-05-06 19:44:37444// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35445// Note that this test relies on a notification from the renderer that the
446// focus has changed in the page. The notification in the renderer may change
447// at which point this test would fail (see comment in
448// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57449IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
450 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12451 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57452 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29453
[email protected]8bcdec92009-02-25 16:15:18454 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30455 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18456 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29457
[email protected]186f13f2009-08-19 20:34:00458 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29459
[email protected]8bcdec92009-02-25 16:15:18460 const char* kExpElementIDs[] = {
461 "", // Initially no element in the page should be focused
462 // (the location bar is focused).
463 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
464 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29465 };
466
[email protected]186f13f2009-08-19 20:34:00467 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
468
initial.commit09911bf2008-07-26 23:55:29469 // Test forward focus traversal.
470 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42471 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29472 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06473 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29474
475 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35476 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42477 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29478 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01479 std::string actual;
480 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25481 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18482 L"",
[email protected]45671612009-04-29 22:24:01483 L"window.domAutomationController.send(getFocusedElement());",
484 &actual));
initial.commit09911bf2008-07-26 23:55:29485 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
486
[email protected]130efb02009-09-18 18:54:35487 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30488 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35489
490 if (j < arraysize(kExpElementIDs) - 1) {
491 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
492 render_view_host());
493 } else {
494 // On the last tab key press, the focus returns to the browser.
495 ui_test_utils::WaitForFocusInBrowser(browser());
496 }
initial.commit09911bf2008-07-26 23:55:29497 }
[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 // Now let's try reverse focus traversal.
506 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42507 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29508 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06509 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29510
[email protected]8bcdec92009-02-25 16:15:18511 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35512 for (size_t j = 0; j < 7; ++j) {
[email protected]f07d7bf2010-04-06 08:02:42513 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35514 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30515 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35516
517 if (j < arraysize(kExpElementIDs) - 1) {
518 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
519 render_view_host());
520 } else {
521 // On the last tab key press, the focus returns to the browser.
522 ui_test_utils::WaitForFocusInBrowser(browser());
523 }
initial.commit09911bf2008-07-26 23:55:29524
525 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01526 std::string actual;
527 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25528 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18529 L"",
[email protected]45671612009-04-29 22:24:01530 L"window.domAutomationController.send(getFocusedElement());",
531 &actual));
initial.commit09911bf2008-07-26 23:55:29532 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
533 }
[email protected]8bcdec92009-02-25 16:15:18534
535 // At this point the renderer has sent us a message asking to advance the
536 // focus (as the end of the focus loop was reached in the renderer).
537 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42538 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29539 }
540}
541
[email protected]9e0c83a2009-05-06 19:44:37542// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41543IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]853300a82010-07-27 21:17:57544 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12545 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57546 ASSERT_TRUE(server);
[email protected]9e0c83a2009-05-06 19:44:37547
548 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30549 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37550 ui_test_utils::NavigateToURL(browser(), url);
551
[email protected]9e0c83a2009-05-06 19:44:37552 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06553 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37554
555 // Let's show an interstitial.
556 TestInterstitialPage* interstitial_page =
557 new TestInterstitialPage(browser()->GetSelectedTabContents(),
558 true, GURL("http://interstitial.com"));
559 interstitial_page->Show();
560 // Give some time for the interstitial to show.
561 MessageLoop::current()->PostDelayedTask(FROM_HERE,
562 new MessageLoop::QuitTask(),
563 1000);
564 ui_test_utils::RunMessageLoop();
565
[email protected]fc2e0872009-08-21 22:14:41566 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37567
568 const char* kExpElementIDs[] = {
569 "", // Initially no element in the page should be focused
570 // (the location bar is focused).
571 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
572 "gmapLink"
573 };
574
[email protected]fc2e0872009-08-21 22:14:41575 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
576
[email protected]9e0c83a2009-05-06 19:44:37577 // Test forward 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 tab to move the focus.
[email protected]130efb02009-09-18 18:54:35583 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37584 // Let's make sure the focus is on the expected element in the page.
585 std::string actual = interstitial_page->GetFocusedElement();
586 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
587
[email protected]130efb02009-09-18 18:54:35588 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30589 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35590
591 if (j < arraysize(kExpElementIDs) - 1) {
592 interstitial_page->WaitForFocusChange();
593 } else {
594 // On the last tab key press, the focus returns to the browser.
595 ui_test_utils::WaitForFocusInBrowser(browser());
596 }
[email protected]9e0c83a2009-05-06 19:44:37597 }
598
599 // At this point the renderer has sent us a message asking to advance the
600 // focus (as the end of the focus loop was reached in the renderer).
601 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42602 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37603 }
604
605 // Now let's try reverse focus traversal.
606 for (int i = 0; i < 2; ++i) {
607 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06608 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37609
610 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35611 for (size_t j = 0; j < 7; ++j) {
612 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30613 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35614
615 if (j < arraysize(kExpElementIDs) - 1) {
616 interstitial_page->WaitForFocusChange();
617 } else {
618 // On the last tab key press, the focus returns to the browser.
619 ui_test_utils::WaitForFocusInBrowser(browser());
620 }
[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]3985ba82010-07-29 21:44:12637 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57638 ASSERT_TRUE(server);
[email protected]9e0c83a2009-05-06 19:44:37639
640 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30641 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37642 ui_test_utils::NavigateToURL(browser(), url);
643
[email protected]9e0c83a2009-05-06 19:44:37644 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06645 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37646 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
647 HasFocus());
648
[email protected]9d8a4642009-07-29 17:25:30649 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37650 TestInterstitialPage* interstitial_page =
651 new TestInterstitialPage(browser()->GetSelectedTabContents(),
652 true, GURL("http://interstitial.com"));
653 interstitial_page->Show();
654 // Give some time for the interstitial to show.
655 MessageLoop::current()->PostDelayedTask(FROM_HERE,
656 new MessageLoop::QuitTask(),
657 1000);
658 ui_test_utils::RunMessageLoop();
659
660 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06661 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37662 EXPECT_TRUE(interstitial_page->HasFocus());
663
664 // Hide the interstitial.
665 interstitial_page->DontProceed();
666
667 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06668 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37669}
670
[email protected]9bd491ee2008-12-10 22:31:07671// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21672IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]853300a82010-07-27 21:17:57673 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12674 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57675 ASSERT_TRUE(server);
license.botbf09a502008-08-24 00:55:55676
[email protected]9bd491ee2008-12-10 22:31:07677 // Open some page (any page that doesn't steal focus).
[email protected]f72a1cc2010-04-30 07:17:30678 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18679 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07680
[email protected]fc2e0872009-08-21 22:14:41681 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
[email protected]9bd491ee2008-12-10 22:31:07682
[email protected]853300a82010-07-27 21:17:57683#if defined(OS_MACOSX)
684 // Press Cmd+F, which will make the Find box open and request focus.
685 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
686 false, false, true,
687 new MessageLoop::QuitTask());
688#else
[email protected]9bd491ee2008-12-10 22:31:07689 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]fc2e0872009-08-21 22:14:41690 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30691 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18692 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57693#endif
[email protected]8bcdec92009-02-25 16:15:18694 ui_test_utils::RunMessageLoop();
695
696 // Ideally, we wouldn't sleep here and instead would intercept the
697 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
698 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
699 // - creating a factory used to create the delegate
700 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41701 MessageLoop::current()->PostDelayedTask(
702 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18703 ui_test_utils::RunMessageLoop();
704
[email protected]21abcc742009-10-23 02:52:06705 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07706
[email protected]fc2e0872009-08-21 22:14:41707 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06708 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07709
710 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57711#if defined(OS_MACOSX)
712 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
713 false, false, true,
714 new MessageLoop::QuitTask());
715#else
[email protected]fc2e0872009-08-21 22:14:41716 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30717 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18718 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57719#endif
[email protected]8bcdec92009-02-25 16:15:18720 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06721 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07722
723 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41724 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06725 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07726
727 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57728#if defined(OS_MACOSX)
729 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
730 false, false, true,
[email protected]1b5a48c2010-04-29 23:08:30731 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57732#else
733 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
734 false, false, false,
735 new MessageLoop::QuitTask());
736#endif
[email protected]8bcdec92009-02-25 16:15:18737 ui_test_utils::RunMessageLoop();
738
739 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41740 MessageLoop::current()->PostDelayedTask(
741 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18742 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06743 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07744}
[email protected]401513c2009-03-12 00:21:28745
746// Makes sure the focus is in the right location when opening the different
747// types of tabs.
[email protected]30667d42010-07-30 17:59:16748// Flaky, http://crbug.com/50763.
749IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) {
[email protected]853300a82010-07-27 21:17:57750 BringBrowserWindowToFront();
[email protected]9ba21ede2010-07-30 01:11:07751
[email protected]401513c2009-03-12 00:21:28752 // Open the history tab, focus should be on the tab contents.
753 browser()->ShowHistoryTab();
[email protected]f07d7bf2010-04-06 08:02:42754 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]21abcc742009-10-23 02:52:06755 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28756
757 // Open the new tab, focus should be on the location bar.
758 browser()->NewTab();
[email protected]21abcc742009-10-23 02:52:06759 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28760
761 // Open the download tab, focus should be on the tab contents.
762 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06763 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43764
765 // Open about:blank, focus should be on the location bar.
766 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK,
[email protected]4a1665442010-06-28 16:09:39767 -1, TabStripModel::ADD_SELECTED, NULL,
768 std::string());
[email protected]21abcc742009-10-23 02:52:06769 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28770}
[email protected]9d8a4642009-07-29 17:25:30771
772// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21773IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]853300a82010-07-27 21:17:57774 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12775 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57776 ASSERT_TRUE(server);
[email protected]9d8a4642009-07-29 17:25:30777
[email protected]9d8a4642009-07-29 17:25:30778 // Open the new tab, reload.
779 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42780 ui_test_utils::RunAllPendingInMessageLoop();
781
[email protected]cb84d642010-06-10 00:56:28782 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40783 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30784 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06785 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30786
787 // Open a regular page, focus the location bar, reload.
[email protected]f72a1cc2010-04-30 07:17:30788 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41789 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06790 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28791 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40792 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07793
[email protected]9d8a4642009-07-29 17:25:30794 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41795 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06796 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30797}
798
[email protected]c38a1902010-07-23 20:55:15799// FocusOnReloadCrashedTab times out often on chromium os debug build.
[email protected]517d2fe72010-07-23 15:40:41800// http://crbug.com/50025
801#if defined(OS_CHROMEOS) && !defined(NDEBUG)
[email protected]c38a1902010-07-23 20:55:15802#define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab
[email protected]517d2fe72010-07-23 15:40:41803#else
804#define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab
805#endif
806
[email protected]9d8a4642009-07-29 17:25:30807// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]517d2fe72010-07-23 15:40:41808IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) {
[email protected]853300a82010-07-27 21:17:57809 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12810 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57811 ASSERT_TRUE(server);
[email protected]9d8a4642009-07-29 17:25:30812
[email protected]9d8a4642009-07-29 17:25:30813 // Open a regular page, crash, reload.
[email protected]f72a1cc2010-04-30 07:17:30814 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30815 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28816 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40817 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9ba21ede2010-07-30 01:11:07818
[email protected]9d8a4642009-07-29 17:25:30819 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41820 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06821 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30822}
[email protected]629e0342010-07-27 23:30:13823
824} // namespace