blob: aa793853dcad91d20eb2d50b3406fcc0c3e0b492 [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]8bcdec92009-02-25 16:15:1813#include "chrome/browser/automation/ui_controls.h"
14#include "chrome/browser/browser.h"
[email protected]134c47b92009-08-19 03:33:4415#include "chrome/browser/browser_window.h"
16#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3717#include "chrome/browser/renderer_host/render_widget_host_view.h"
18#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0019#include "chrome/browser/tab_contents/tab_contents.h"
20#include "chrome/browser/tab_contents/tab_contents_view.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3722#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1823#include "chrome/test/in_process_browser_test.h"
24#include "chrome/test/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1225#include "net/test/test_server.h"
[email protected]853300a82010-07-27 21:17:5726
27#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0528#include "views/focus/focus_manager.h"
29#include "views/view.h"
30#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5731#endif
initial.commit09911bf2008-07-26 23:55:2932
[email protected]134c47b92009-08-19 03:33:4433#if defined(TOOLKIT_VIEWS)
34#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1735#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4436#include "chrome/browser/views/tab_contents/tab_contents_container.h"
37#endif
38
[email protected]753efc42010-03-09 19:52:1639#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1740#include "chrome/browser/gtk/view_id_util.h"
41#endif
42
[email protected]fc2e0872009-08-21 22:14:4143#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5744#define MAYBE_FocusTraversal FocusTraversal
[email protected]fc2e0872009-08-21 22:14:4145// For some reason we hit an external DNS lookup in this test in Linux but not
46// on Windows. TODO(estade): investigate.
47#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3548// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5949#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5750#elif defined(OS_MACOSX)
51// TODO(suzhe): http://crbug.com/49738 (following two tests)
52#define MAYBE_FocusTraversal FAILS_FocusTraversal
53#define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial
54// TODO(suzhe): http://crbug.com/49737
55#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
56#elif defined(OS_WIN)
57#define MAYBE_FocusTraversal FocusTraversal
[email protected]e4f4e0b2009-10-13 19:58:2158#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3559#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4160#endif
61
initial.commit09911bf2008-07-26 23:55:2962namespace {
63
[email protected]8bcdec92009-02-25 16:15:1864// The delay waited in some cases where we don't have a notifications for an
65// action we take.
initial.commit09911bf2008-07-26 23:55:2966const int kActionDelayMs = 500;
67
[email protected]f72a1cc2010-04-30 07:17:3068const char kSimplePage[] = "files/focus/page_with_focus.html";
69const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
70const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3171const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2972
[email protected]8bcdec92009-02-25 16:15:1873class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2974 public:
75 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1876 set_show_window(true);
77 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2978 }
[email protected]b9821882009-08-17 22:25:1779
[email protected]21abcc742009-10-23 02:52:0680 bool IsViewFocused(ViewID vid) {
81 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1782 }
83
[email protected]fc2e0872009-08-21 22:14:4184 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0685 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4186 }
87
[email protected]853300a82010-07-27 21:17:5788 void BringBrowserWindowToFront() {
89 ui_test_utils::ShowAndFocusNativeWindow(
90 browser()->window()->GetNativeHandle());
[email protected]186f13f2009-08-19 20:34:0091 }
initial.commit09911bf2008-07-26 23:55:2992};
93
[email protected]9e0c83a2009-05-06 19:44:3794class TestInterstitialPage : public InterstitialPage {
95 public:
96 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
97 : InterstitialPage(tab, new_navigation, url),
[email protected]130efb02009-09-18 18:54:3598 waiting_for_dom_response_(false),
99 waiting_for_focus_change_(false) {
[email protected]b65de8b92009-09-14 19:36:31100 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37101 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
102 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31103 file_path = file_path.AppendASCII("focus");
104 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37105 r = file_util::ReadFileToString(file_path, &html_contents_);
106 EXPECT_TRUE(r);
107 }
108
109 virtual std::string GetHTMLContents() {
110 return html_contents_;
111 }
112
113 virtual void DomOperationResponse(const std::string& json_string,
114 int automation_id) {
115 if (waiting_for_dom_response_) {
116 dom_response_ = json_string;
117 waiting_for_dom_response_ = false;
118 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
119 return;
120 }
121 InterstitialPage::DomOperationResponse(json_string, automation_id);
122 }
123
124 std::string GetFocusedElement() {
125 std::wstring script = L"window.domAutomationController.setAutomationId(0);"
126 L"window.domAutomationController.send(getFocusedElement());";
127
128 render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
129 DCHECK(!waiting_for_dom_response_);
130 waiting_for_dom_response_ = true;
131 ui_test_utils::RunMessageLoop();
132 // Remove the JSON extra quotes.
133 if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
134 dom_response_[dom_response_.size() - 1] == '"') {
135 dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
136 }
137 return dom_response_;
138 }
139
140 bool HasFocus() {
141 return render_view_host()->view()->HasFocus();
142 }
143
[email protected]130efb02009-09-18 18:54:35144 void WaitForFocusChange() {
145 waiting_for_focus_change_ = true;
146 ui_test_utils::RunMessageLoop();
147 }
148
149 protected:
150 virtual void FocusedNodeChanged() {
151 if (!waiting_for_focus_change_)
152 return;
153
154 waiting_for_focus_change_= false;
155 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
156 }
157
[email protected]9e0c83a2009-05-06 19:44:37158 private:
159 std::string html_contents_;
160
161 bool waiting_for_dom_response_;
[email protected]130efb02009-09-18 18:54:35162 bool waiting_for_focus_change_;
[email protected]9e0c83a2009-05-06 19:44:37163 std::string dom_response_;
[email protected]9e0c83a2009-05-06 19:44:37164};
[email protected]b9821882009-08-17 22:25:17165
[email protected]e4f4e0b2009-10-13 19:58:21166IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]853300a82010-07-27 21:17:57167 BringBrowserWindowToFront();
168#if defined(USE_X11) || defined(OS_MACOSX)
[email protected]fc2e0872009-08-21 22:14:41169 // It seems we have to wait a little bit for the widgets to spin up before
170 // we can start clicking on them.
171 MessageLoop::current()->PostDelayedTask(FROM_HERE,
172 new MessageLoop::QuitTask(),
173 kActionDelayMs);
174 ui_test_utils::RunMessageLoop();
175#endif
176
[email protected]21abcc742009-10-23 02:52:06177 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00178
[email protected]fc2e0872009-08-21 22:14:41179 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06180 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00181
[email protected]fc2e0872009-08-21 22:14:41182 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06183 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00184}
[email protected]186f13f2009-08-19 20:34:00185
[email protected]e4f4e0b2009-10-13 19:58:21186IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]853300a82010-07-27 21:17:57187 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12188 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57189 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29190
191 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30192 GURL url = server->TestServerPage(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]3985ba82010-07-29 21:44:12253 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57254 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29255
256 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30257 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18258 ui_test_utils::NavigateToURL(browser(), url);
259
initial.commit09911bf2008-07-26 23:55:29260 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58261 for (int i = 0; i < 4; ++i) {
[email protected]715af7e2010-04-29 01:55:38262 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]4a1665442010-06-28 16:09:39263 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]22735af62009-04-07 21:09:58264 }
initial.commit09911bf2008-07-26 23:55:29265
266 // Alternate focus for the tab.
267 const bool kFocusPage[3][5] = {
268 { true, true, true, true, false },
269 { false, false, false, false, false },
270 { false, true, false, true, false }
271 };
272
273 for (int i = 1; i < 3; i++) {
274 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18275 // Activate the tab.
276 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29277
278 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54279 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00280 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54281 } else {
[email protected]186f13f2009-08-19 20:34:00282 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54283 }
initial.commit09911bf2008-07-26 23:55:29284 }
285
286 // Now come back to the tab and check the right view is focused.
287 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18288 // Activate the tab.
289 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29290
[email protected]186f13f2009-08-19 20:34:00291 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
292 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06293 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29294 }
[email protected]cb7e2542009-12-14 22:02:35295
296 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
297 browser()->SelectTabContentsAt(0, true);
298 // Try the above, but with ctrl+tab. Since tab normally changes focus,
299 // this has regressed in the past. Loop through several times to be sure.
300 for (int j = 0; j < 15; j++) {
301 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
302 VIEW_ID_LOCATION_BAR;
303 ASSERT_TRUE(IsViewFocused(vid));
304
305 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30306 false, false, false,
[email protected]cb7e2542009-12-14 22:02:35307 new MessageLoop::QuitTask());
308 ui_test_utils::RunMessageLoop();
309 }
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
318 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30319 true, false, false,
[email protected]cb7e2542009-12-14 22:02:35320 new MessageLoop::QuitTask());
321 ui_test_utils::RunMessageLoop();
322 }
initial.commit09911bf2008-07-26 23:55:29323 }
324}
325
[email protected]ae40b572009-10-02 21:17:45326// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35327IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]853300a82010-07-27 21:17:57328 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12329 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57330 ASSERT_TRUE(server);
[email protected]ae40b572009-10-02 21:17:45331
332 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30333 GURL url = server->TestServerPage(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45334 ui_test_utils::NavigateToURL(browser(), url);
335
336 browser()->Find();
337 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
338 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06339 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45340
341 // Focus the location bar.
342 browser()->FocusLocationBar();
343
344 // Create a 2nd tab.
[email protected]4a1665442010-06-28 16:09:39345 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
346 TabStripModel::ADD_SELECTED, NULL, std::string());
[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]3985ba82010-07-29 21:44:12369 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57370 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29371
372 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30373 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18374 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29375
376 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18377 Browser* browser2 = Browser::Create(browser()->profile());
378 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43379 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18380 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00381
[email protected]ed179ee2009-10-03 21:02:51382 Browser* focused_browser = NULL;
383 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16384#if defined(USE_X11)
385 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00386 // to figure out which browser does have focus.
387 if (browser2->window()->IsActive()) {
388 focused_browser = browser2;
389 unfocused_browser = browser();
390 } else if (browser()->window()->IsActive()) {
391 focused_browser = browser();
392 unfocused_browser = browser2;
393 } else {
394 ASSERT_TRUE(false);
395 }
396#elif defined(OS_WIN)
397 focused_browser = browser();
398 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57399#elif defined(OS_MACOSX)
400 // On Mac, the newly created window always gets the focus.
401 focused_browser = browser2;
402 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00403#endif
404
[email protected]f72a1cc2010-04-30 07:17:30405 GURL steal_focus_url = server->TestServerPage(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00406 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46407
[email protected]8bcdec92009-02-25 16:15:18408 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00409 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29410
411 // Wait for the focus to be stolen by the other browser.
[email protected]f07d7bf2010-04-06 08:02:42412 MessageLoop::current()->PostDelayedTask(
413 FROM_HERE, new MessageLoop::QuitTask(), 2000);
414 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29415
[email protected]8bcdec92009-02-25 16:15:18416 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00417 EXPECT_TRUE(focused_browser->window()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18418
419 // Close the 2nd browser to avoid a DCHECK().
[email protected]186f13f2009-08-19 20:34:00420 browser2->window()->Close();
initial.commit09911bf2008-07-26 23:55:29421}
422
423// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21424IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]853300a82010-07-27 21:17:57425 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12426 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57427 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29428
429 // Open the page that steals focus.
[email protected]f72a1cc2010-04-30 07:17:30430 GURL url = server->TestServerPage(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18431 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29432
[email protected]186f13f2009-08-19 20:34:00433 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29434
435 // Wait for the page to steal focus.
[email protected]186f13f2009-08-19 20:34:00436 PlatformThread::Sleep(2000);
initial.commit09911bf2008-07-26 23:55:29437
438 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06439 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29440}
441
[email protected]9e0c83a2009-05-06 19:44:37442// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35443// Note that this test relies on a notification from the renderer that the
444// focus has changed in the page. The notification in the renderer may change
445// at which point this test would fail (see comment in
446// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57447IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
448 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12449 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57450 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29451
[email protected]8bcdec92009-02-25 16:15:18452 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30453 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18454 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29455
[email protected]186f13f2009-08-19 20:34:00456 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29457
[email protected]8bcdec92009-02-25 16:15:18458 const char* kExpElementIDs[] = {
459 "", // Initially no element in the page should be focused
460 // (the location bar is focused).
461 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
462 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29463 };
464
[email protected]186f13f2009-08-19 20:34:00465 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
466
initial.commit09911bf2008-07-26 23:55:29467 // Test forward focus traversal.
468 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42469 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29470 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06471 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29472
473 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35474 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42475 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29476 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01477 std::string actual;
478 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25479 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18480 L"",
[email protected]45671612009-04-29 22:24:01481 L"window.domAutomationController.send(getFocusedElement());",
482 &actual));
initial.commit09911bf2008-07-26 23:55:29483 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
484
[email protected]130efb02009-09-18 18:54:35485 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30486 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35487
488 if (j < arraysize(kExpElementIDs) - 1) {
489 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
490 render_view_host());
491 } else {
492 // On the last tab key press, the focus returns to the browser.
493 ui_test_utils::WaitForFocusInBrowser(browser());
494 }
initial.commit09911bf2008-07-26 23:55:29495 }
[email protected]8bcdec92009-02-25 16:15:18496
497 // At this point the renderer has sent us a message asking to advance the
498 // focus (as the end of the focus loop was reached in the renderer).
499 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42500 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29501 }
502
503 // Now let's try reverse focus traversal.
504 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42505 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29506 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06507 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29508
[email protected]8bcdec92009-02-25 16:15:18509 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35510 for (size_t j = 0; j < 7; ++j) {
[email protected]f07d7bf2010-04-06 08:02:42511 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35512 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30513 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35514
515 if (j < arraysize(kExpElementIDs) - 1) {
516 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
517 render_view_host());
518 } else {
519 // On the last tab key press, the focus returns to the browser.
520 ui_test_utils::WaitForFocusInBrowser(browser());
521 }
initial.commit09911bf2008-07-26 23:55:29522
523 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01524 std::string actual;
525 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25526 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18527 L"",
[email protected]45671612009-04-29 22:24:01528 L"window.domAutomationController.send(getFocusedElement());",
529 &actual));
initial.commit09911bf2008-07-26 23:55:29530 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
531 }
[email protected]8bcdec92009-02-25 16:15:18532
533 // At this point the renderer has sent us a message asking to advance the
534 // focus (as the end of the focus loop was reached in the renderer).
535 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42536 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29537 }
538}
539
[email protected]9e0c83a2009-05-06 19:44:37540// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41541IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]853300a82010-07-27 21:17:57542 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12543 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57544 ASSERT_TRUE(server);
[email protected]9e0c83a2009-05-06 19:44:37545
546 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30547 GURL url = server->TestServerPage(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]130efb02009-09-18 18:54:35586 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::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) {
590 interstitial_page->WaitForFocusChange();
591 } else {
592 // On the last tab key press, the focus returns to the browser.
593 ui_test_utils::WaitForFocusInBrowser(browser());
594 }
[email protected]9e0c83a2009-05-06 19:44:37595 }
596
597 // At this point the renderer has sent us a message asking to advance the
598 // focus (as the end of the focus loop was reached in the renderer).
599 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42600 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37601 }
602
603 // Now let's try reverse focus traversal.
604 for (int i = 0; i < 2; ++i) {
605 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06606 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37607
608 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35609 for (size_t j = 0; j < 7; ++j) {
610 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30611 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35612
613 if (j < arraysize(kExpElementIDs) - 1) {
614 interstitial_page->WaitForFocusChange();
615 } else {
616 // On the last tab key press, the focus returns to the browser.
617 ui_test_utils::WaitForFocusInBrowser(browser());
618 }
[email protected]9e0c83a2009-05-06 19:44:37619
620 // Let's make sure the focus is on the expected element in the page.
621 std::string actual = interstitial_page->GetFocusedElement();
622 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
623 }
624
625 // At this point the renderer has sent us a message asking to advance the
626 // focus (as the end of the focus loop was reached in the renderer).
627 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42628 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37629 }
630}
631
632// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21633IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]853300a82010-07-27 21:17:57634 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12635 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57636 ASSERT_TRUE(server);
[email protected]9e0c83a2009-05-06 19:44:37637
638 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30639 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37640 ui_test_utils::NavigateToURL(browser(), url);
641
[email protected]9e0c83a2009-05-06 19:44:37642 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06643 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37644 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
645 HasFocus());
646
[email protected]9d8a4642009-07-29 17:25:30647 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37648 TestInterstitialPage* interstitial_page =
649 new TestInterstitialPage(browser()->GetSelectedTabContents(),
650 true, GURL("http://interstitial.com"));
651 interstitial_page->Show();
652 // Give some time for the interstitial to show.
653 MessageLoop::current()->PostDelayedTask(FROM_HERE,
654 new MessageLoop::QuitTask(),
655 1000);
656 ui_test_utils::RunMessageLoop();
657
658 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06659 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37660 EXPECT_TRUE(interstitial_page->HasFocus());
661
662 // Hide the interstitial.
663 interstitial_page->DontProceed();
664
665 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06666 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37667}
668
[email protected]9bd491ee2008-12-10 22:31:07669// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21670IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]853300a82010-07-27 21:17:57671 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12672 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57673 ASSERT_TRUE(server);
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]f72a1cc2010-04-30 07:17:30676 GURL url = server->TestServerPage(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.
683 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
684 false, false, true,
685 new MessageLoop::QuitTask());
686#else
[email protected]9bd491ee2008-12-10 22:31:07687 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]fc2e0872009-08-21 22:14:41688 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30689 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18690 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57691#endif
[email protected]8bcdec92009-02-25 16:15:18692 ui_test_utils::RunMessageLoop();
693
694 // Ideally, we wouldn't sleep here and instead would intercept the
695 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
696 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
697 // - creating a factory used to create the delegate
698 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41699 MessageLoop::current()->PostDelayedTask(
700 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18701 ui_test_utils::RunMessageLoop();
702
[email protected]21abcc742009-10-23 02:52:06703 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07704
[email protected]fc2e0872009-08-21 22:14:41705 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06706 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07707
708 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57709#if defined(OS_MACOSX)
710 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
711 false, false, true,
712 new MessageLoop::QuitTask());
713#else
[email protected]fc2e0872009-08-21 22:14:41714 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30715 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18716 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57717#endif
[email protected]8bcdec92009-02-25 16:15:18718 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06719 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07720
721 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41722 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06723 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07724
725 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57726#if defined(OS_MACOSX)
727 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
728 false, false, true,
[email protected]1b5a48c2010-04-29 23:08:30729 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57730#else
731 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
732 false, false, false,
733 new MessageLoop::QuitTask());
734#endif
[email protected]8bcdec92009-02-25 16:15:18735 ui_test_utils::RunMessageLoop();
736
737 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41738 MessageLoop::current()->PostDelayedTask(
739 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18740 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06741 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07742}
[email protected]401513c2009-03-12 00:21:28743
744// Makes sure the focus is in the right location when opening the different
745// types of tabs.
[email protected]e4f4e0b2009-10-13 19:58:21746IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
[email protected]853300a82010-07-27 21:17:57747 BringBrowserWindowToFront();
[email protected]401513c2009-03-12 00:21:28748 // Open the history tab, focus should be on the tab contents.
749 browser()->ShowHistoryTab();
[email protected]f07d7bf2010-04-06 08:02:42750
751 ui_test_utils::RunAllPendingInMessageLoop();
752
[email protected]21abcc742009-10-23 02:52:06753 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28754
755 // Open the new tab, focus should be on the location bar.
756 browser()->NewTab();
[email protected]21abcc742009-10-23 02:52:06757 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28758
759 // Open the download tab, focus should be on the tab contents.
760 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06761 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43762
763 // Open about:blank, focus should be on the location bar.
764 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK,
[email protected]4a1665442010-06-28 16:09:39765 -1, TabStripModel::ADD_SELECTED, NULL,
766 std::string());
[email protected]21abcc742009-10-23 02:52:06767 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28768}
[email protected]9d8a4642009-07-29 17:25:30769
770// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21771IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]853300a82010-07-27 21:17:57772 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12773 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57774 ASSERT_TRUE(server);
[email protected]9d8a4642009-07-29 17:25:30775
[email protected]9d8a4642009-07-29 17:25:30776 // Open the new tab, reload.
777 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42778
779 ui_test_utils::RunAllPendingInMessageLoop();
780
[email protected]cb84d642010-06-10 00:56:28781 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40782 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30783 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06784 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30785
786 // Open a regular page, focus the location bar, reload.
[email protected]f72a1cc2010-04-30 07:17:30787 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41788 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06789 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28790 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40791 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30792 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41793 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06794 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30795}
796
[email protected]c38a1902010-07-23 20:55:15797// FocusOnReloadCrashedTab times out often on chromium os debug build.
[email protected]517d2fe72010-07-23 15:40:41798// http://crbug.com/50025
799#if defined(OS_CHROMEOS) && !defined(NDEBUG)
[email protected]c38a1902010-07-23 20:55:15800#define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab
[email protected]517d2fe72010-07-23 15:40:41801#else
802#define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab
803#endif
804
[email protected]9d8a4642009-07-29 17:25:30805// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]517d2fe72010-07-23 15:40:41806IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) {
[email protected]853300a82010-07-27 21:17:57807 BringBrowserWindowToFront();
[email protected]3985ba82010-07-29 21:44:12808 net::HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57809 ASSERT_TRUE(server);
[email protected]9d8a4642009-07-29 17:25:30810
[email protected]9d8a4642009-07-29 17:25:30811 // Open a regular page, crash, reload.
[email protected]f72a1cc2010-04-30 07:17:30812 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30813 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28814 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40815 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30816 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41817 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06818 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30819}
[email protected]629e0342010-07-27 23:30:13820
821} // namespace