blob: 1e0f2fc1dd4e67901db74b81f0aab283190a3b31 [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]f07d7bf2010-04-06 08:02:427#include "base/format_macros.h"
[email protected]853300a82010-07-27 21:17:578#include "base/logging.h"
[email protected]8bcdec92009-02-25 16:15:189#include "base/message_loop.h"
[email protected]ece3c8b2009-03-27 16:55:3910#include "base/ref_counted.h"
[email protected]8bcdec92009-02-25 16:15:1811#include "chrome/browser/automation/ui_controls.h"
12#include "chrome/browser/browser.h"
[email protected]134c47b92009-08-19 03:33:4413#include "chrome/browser/browser_window.h"
14#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3715#include "chrome/browser/renderer_host/render_widget_host_view.h"
16#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0017#include "chrome/browser/tab_contents/tab_contents.h"
18#include "chrome/browser/tab_contents/tab_contents_view.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3720#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1821#include "chrome/test/in_process_browser_test.h"
22#include "chrome/test/ui_test_utils.h"
[email protected]853300a82010-07-27 21:17:5723
24#if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0525#include "views/focus/focus_manager.h"
26#include "views/view.h"
27#include "views/window/window.h"
[email protected]853300a82010-07-27 21:17:5728#endif
initial.commit09911bf2008-07-26 23:55:2929
[email protected]134c47b92009-08-19 03:33:4430#if defined(TOOLKIT_VIEWS)
31#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1732#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4433#include "chrome/browser/views/tab_contents/tab_contents_container.h"
34#endif
35
[email protected]753efc42010-03-09 19:52:1636#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1737#include "chrome/browser/gtk/view_id_util.h"
38#endif
39
[email protected]fc2e0872009-08-21 22:14:4140#if defined(OS_LINUX)
[email protected]853300a82010-07-27 21:17:5741#define MAYBE_FocusTraversal FocusTraversal
[email protected]fc2e0872009-08-21 22:14:4142// For some reason we hit an external DNS lookup in this test in Linux but not
43// on Windows. TODO(estade): investigate.
44#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3545// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5946#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5747#elif defined(OS_MACOSX)
48// TODO(suzhe): http://crbug.com/49738 (following two tests)
49#define MAYBE_FocusTraversal FAILS_FocusTraversal
50#define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial
51// TODO(suzhe): http://crbug.com/49737
52#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
53#elif defined(OS_WIN)
54#define MAYBE_FocusTraversal FocusTraversal
[email protected]e4f4e0b2009-10-13 19:58:2155#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3556#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4157#endif
58
initial.commit09911bf2008-07-26 23:55:2959namespace {
60
[email protected]8bcdec92009-02-25 16:15:1861// The delay waited in some cases where we don't have a notifications for an
62// action we take.
initial.commit09911bf2008-07-26 23:55:2963const int kActionDelayMs = 500;
64
[email protected]f72a1cc2010-04-30 07:17:3065const char kSimplePage[] = "files/focus/page_with_focus.html";
66const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
67const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3168const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2969
[email protected]8bcdec92009-02-25 16:15:1870class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2971 public:
72 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1873 set_show_window(true);
74 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2975 }
[email protected]b9821882009-08-17 22:25:1776
[email protected]21abcc742009-10-23 02:52:0677 bool IsViewFocused(ViewID vid) {
78 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1779 }
80
[email protected]fc2e0872009-08-21 22:14:4181 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0682 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4183 }
84
[email protected]853300a82010-07-27 21:17:5785 void BringBrowserWindowToFront() {
86 ui_test_utils::ShowAndFocusNativeWindow(
87 browser()->window()->GetNativeHandle());
[email protected]186f13f2009-08-19 20:34:0088 }
initial.commit09911bf2008-07-26 23:55:2989};
90
[email protected]9e0c83a2009-05-06 19:44:3791class TestInterstitialPage : public InterstitialPage {
92 public:
93 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
94 : InterstitialPage(tab, new_navigation, url),
[email protected]130efb02009-09-18 18:54:3595 waiting_for_dom_response_(false),
96 waiting_for_focus_change_(false) {
[email protected]b65de8b92009-09-14 19:36:3197 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:3798 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
99 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31100 file_path = file_path.AppendASCII("focus");
101 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37102 r = file_util::ReadFileToString(file_path, &html_contents_);
103 EXPECT_TRUE(r);
104 }
105
106 virtual std::string GetHTMLContents() {
107 return html_contents_;
108 }
109
110 virtual void DomOperationResponse(const std::string& json_string,
111 int automation_id) {
112 if (waiting_for_dom_response_) {
113 dom_response_ = json_string;
114 waiting_for_dom_response_ = false;
115 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
116 return;
117 }
118 InterstitialPage::DomOperationResponse(json_string, automation_id);
119 }
120
121 std::string GetFocusedElement() {
122 std::wstring script = L"window.domAutomationController.setAutomationId(0);"
123 L"window.domAutomationController.send(getFocusedElement());";
124
125 render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
126 DCHECK(!waiting_for_dom_response_);
127 waiting_for_dom_response_ = true;
128 ui_test_utils::RunMessageLoop();
129 // Remove the JSON extra quotes.
130 if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
131 dom_response_[dom_response_.size() - 1] == '"') {
132 dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
133 }
134 return dom_response_;
135 }
136
137 bool HasFocus() {
138 return render_view_host()->view()->HasFocus();
139 }
140
[email protected]130efb02009-09-18 18:54:35141 void WaitForFocusChange() {
142 waiting_for_focus_change_ = true;
143 ui_test_utils::RunMessageLoop();
144 }
145
146 protected:
147 virtual void FocusedNodeChanged() {
148 if (!waiting_for_focus_change_)
149 return;
150
151 waiting_for_focus_change_= false;
152 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
153 }
154
[email protected]9e0c83a2009-05-06 19:44:37155 private:
156 std::string html_contents_;
157
158 bool waiting_for_dom_response_;
[email protected]130efb02009-09-18 18:54:35159 bool waiting_for_focus_change_;
[email protected]9e0c83a2009-05-06 19:44:37160 std::string dom_response_;
[email protected]9e0c83a2009-05-06 19:44:37161};
[email protected]b9821882009-08-17 22:25:17162
[email protected]e4f4e0b2009-10-13 19:58:21163IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]853300a82010-07-27 21:17:57164 BringBrowserWindowToFront();
165#if defined(USE_X11) || defined(OS_MACOSX)
[email protected]fc2e0872009-08-21 22:14:41166 // It seems we have to wait a little bit for the widgets to spin up before
167 // we can start clicking on them.
168 MessageLoop::current()->PostDelayedTask(FROM_HERE,
169 new MessageLoop::QuitTask(),
170 kActionDelayMs);
171 ui_test_utils::RunMessageLoop();
172#endif
173
[email protected]21abcc742009-10-23 02:52:06174 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00175
[email protected]fc2e0872009-08-21 22:14:41176 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06177 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00178
[email protected]fc2e0872009-08-21 22:14:41179 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06180 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00181}
[email protected]186f13f2009-08-19 20:34:00182
[email protected]e4f4e0b2009-10-13 19:58:21183IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]853300a82010-07-27 21:17:57184 BringBrowserWindowToFront();
[email protected]8bcdec92009-02-25 16:15:18185 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57186 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29187
188 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30189 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18190 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29191
[email protected]186f13f2009-08-19 20:34:00192 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
193
initial.commit09911bf2008-07-26 23:55:29194 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06195 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29196 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57197 ui_test_utils::HideNativeWindow(window);
198 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06199 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29200
[email protected]186f13f2009-08-19 20:34:00201 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06202 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29203 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57204 ui_test_utils::HideNativeWindow(window);
205 ui_test_utils::ShowAndFocusNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06206 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29207
[email protected]186f13f2009-08-19 20:34:00208 // The rest of this test does not make sense on Linux because the behavior
209 // of Activate() is not well defined and can vary by window manager.
210#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29211 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18212 Browser* browser2 = Browser::Create(browser()->profile());
213 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43214 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18215 browser2->window()->Show();
216 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29217
[email protected]8bcdec92009-02-25 16:15:18218 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00219 BrowserView* browser_view2 =
220 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18221 ASSERT_TRUE(browser_view2);
222 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00223 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18224 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54225 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38226 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29227
228 // Switch to the 1st browser window, focus should still be on the location
229 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18230 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06231 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18232 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29233
234 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18235 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00236 EXPECT_EQ(NULL,
237 views::FocusManager::GetFocusManagerForNativeView(
238 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54239 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38240 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18241
242 // Close the 2nd browser to avoid a DCHECK().
243 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00244#endif
initial.commit09911bf2008-07-26 23:55:29245}
246
247// Tabs remember focus.
[email protected]e4f4e0b2009-10-13 19:58:21248IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
[email protected]853300a82010-07-27 21:17:57249 BringBrowserWindowToFront();
[email protected]8bcdec92009-02-25 16:15:18250 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57251 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29252
253 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30254 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18255 ui_test_utils::NavigateToURL(browser(), url);
256
initial.commit09911bf2008-07-26 23:55:29257 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58258 for (int i = 0; i < 4; ++i) {
[email protected]715af7e2010-04-29 01:55:38259 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]4a1665442010-06-28 16:09:39260 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]22735af62009-04-07 21:09:58261 }
initial.commit09911bf2008-07-26 23:55:29262
263 // Alternate focus for the tab.
264 const bool kFocusPage[3][5] = {
265 { true, true, true, true, false },
266 { false, false, false, false, false },
267 { false, true, false, true, false }
268 };
269
270 for (int i = 1; i < 3; i++) {
271 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18272 // Activate the tab.
273 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29274
275 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54276 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00277 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54278 } else {
[email protected]186f13f2009-08-19 20:34:00279 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54280 }
initial.commit09911bf2008-07-26 23:55:29281 }
282
283 // Now come back to the tab and check the right view is focused.
284 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18285 // Activate the tab.
286 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29287
[email protected]186f13f2009-08-19 20:34:00288 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
289 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06290 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29291 }
[email protected]cb7e2542009-12-14 22:02:35292
293 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
294 browser()->SelectTabContentsAt(0, true);
295 // Try the above, but with ctrl+tab. Since tab normally changes focus,
296 // this has regressed in the past. Loop through several times to be sure.
297 for (int j = 0; j < 15; j++) {
298 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
299 VIEW_ID_LOCATION_BAR;
300 ASSERT_TRUE(IsViewFocused(vid));
301
302 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30303 false, false, false,
[email protected]cb7e2542009-12-14 22:02:35304 new MessageLoop::QuitTask());
305 ui_test_utils::RunMessageLoop();
306 }
307
308 // As above, but with ctrl+shift+tab.
309 browser()->SelectTabContentsAt(4, true);
310 for (int j = 14; j >= 0; --j) {
311 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
312 VIEW_ID_LOCATION_BAR;
313 ASSERT_TRUE(IsViewFocused(vid));
314
315 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30316 true, false, false,
[email protected]cb7e2542009-12-14 22:02:35317 new MessageLoop::QuitTask());
318 ui_test_utils::RunMessageLoop();
319 }
initial.commit09911bf2008-07-26 23:55:29320 }
321}
322
[email protected]ae40b572009-10-02 21:17:45323// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35324IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]853300a82010-07-27 21:17:57325 BringBrowserWindowToFront();
[email protected]ae40b572009-10-02 21:17:45326 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57327 ASSERT_TRUE(server);
[email protected]ae40b572009-10-02 21:17:45328
329 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30330 GURL url = server->TestServerPage(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45331 ui_test_utils::NavigateToURL(browser(), url);
332
333 browser()->Find();
334 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
335 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06336 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45337
338 // Focus the location bar.
339 browser()->FocusLocationBar();
340
341 // Create a 2nd tab.
[email protected]4a1665442010-06-28 16:09:39342 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
343 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]ae40b572009-10-02 21:17:45344
345 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06346 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45347
348 // Select 1st tab, focus should still be on the location-bar.
349 // (bug http://crbug.com/23296)
350 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06351 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45352
353 // Now open the find box again, switch to another tab and come back, the focus
354 // should return to the find box.
355 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06356 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45357 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06358 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45359 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06360 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45361}
362
initial.commit09911bf2008-07-26 23:55:29363// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21364IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]853300a82010-07-27 21:17:57365 BringBrowserWindowToFront();
[email protected]8bcdec92009-02-25 16:15:18366 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57367 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29368
369 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30370 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18371 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29372
373 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18374 Browser* browser2 = Browser::Create(browser()->profile());
375 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43376 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18377 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00378
[email protected]ed179ee2009-10-03 21:02:51379 Browser* focused_browser = NULL;
380 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16381#if defined(USE_X11)
382 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00383 // to figure out which browser does have focus.
384 if (browser2->window()->IsActive()) {
385 focused_browser = browser2;
386 unfocused_browser = browser();
387 } else if (browser()->window()->IsActive()) {
388 focused_browser = browser();
389 unfocused_browser = browser2;
390 } else {
391 ASSERT_TRUE(false);
392 }
393#elif defined(OS_WIN)
394 focused_browser = browser();
395 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57396#elif defined(OS_MACOSX)
397 // On Mac, the newly created window always gets the focus.
398 focused_browser = browser2;
399 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00400#endif
401
[email protected]f72a1cc2010-04-30 07:17:30402 GURL steal_focus_url = server->TestServerPage(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00403 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46404
[email protected]8bcdec92009-02-25 16:15:18405 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00406 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29407
408 // Wait for the focus to be stolen by the other browser.
[email protected]f07d7bf2010-04-06 08:02:42409 MessageLoop::current()->PostDelayedTask(
410 FROM_HERE, new MessageLoop::QuitTask(), 2000);
411 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29412
[email protected]8bcdec92009-02-25 16:15:18413 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00414 EXPECT_TRUE(focused_browser->window()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18415
416 // Close the 2nd browser to avoid a DCHECK().
[email protected]186f13f2009-08-19 20:34:00417 browser2->window()->Close();
initial.commit09911bf2008-07-26 23:55:29418}
419
420// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21421IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]853300a82010-07-27 21:17:57422 BringBrowserWindowToFront();
[email protected]8bcdec92009-02-25 16:15:18423 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57424 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29425
426 // Open the page that steals focus.
[email protected]f72a1cc2010-04-30 07:17:30427 GURL url = server->TestServerPage(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18428 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29429
[email protected]186f13f2009-08-19 20:34:00430 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29431
432 // Wait for the page to steal focus.
[email protected]186f13f2009-08-19 20:34:00433 PlatformThread::Sleep(2000);
initial.commit09911bf2008-07-26 23:55:29434
435 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06436 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29437}
438
[email protected]9e0c83a2009-05-06 19:44:37439// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35440// Note that this test relies on a notification from the renderer that the
441// focus has changed in the page. The notification in the renderer may change
442// at which point this test would fail (see comment in
443// RenderWidget::didFocus()).
[email protected]853300a82010-07-27 21:17:57444IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
445 BringBrowserWindowToFront();
[email protected]8bcdec92009-02-25 16:15:18446 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57447 ASSERT_TRUE(server);
initial.commit09911bf2008-07-26 23:55:29448
[email protected]8bcdec92009-02-25 16:15:18449 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30450 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18451 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29452
[email protected]186f13f2009-08-19 20:34:00453 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29454
[email protected]8bcdec92009-02-25 16:15:18455 const char* kExpElementIDs[] = {
456 "", // Initially no element in the page should be focused
457 // (the location bar is focused).
458 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
459 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29460 };
461
[email protected]186f13f2009-08-19 20:34:00462 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
463
initial.commit09911bf2008-07-26 23:55:29464 // Test forward focus traversal.
465 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42466 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29467 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06468 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29469
470 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35471 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42472 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29473 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01474 std::string actual;
475 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25476 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18477 L"",
[email protected]45671612009-04-29 22:24:01478 L"window.domAutomationController.send(getFocusedElement());",
479 &actual));
initial.commit09911bf2008-07-26 23:55:29480 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
481
[email protected]130efb02009-09-18 18:54:35482 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30483 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35484
485 if (j < arraysize(kExpElementIDs) - 1) {
486 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
487 render_view_host());
488 } else {
489 // On the last tab key press, the focus returns to the browser.
490 ui_test_utils::WaitForFocusInBrowser(browser());
491 }
initial.commit09911bf2008-07-26 23:55:29492 }
[email protected]8bcdec92009-02-25 16:15:18493
494 // At this point the renderer has sent us a message asking to advance the
495 // focus (as the end of the focus loop was reached in the renderer).
496 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42497 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29498 }
499
500 // Now let's try reverse focus traversal.
501 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42502 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29503 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06504 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29505
[email protected]8bcdec92009-02-25 16:15:18506 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35507 for (size_t j = 0; j < 7; ++j) {
[email protected]f07d7bf2010-04-06 08:02:42508 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35509 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30510 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35511
512 if (j < arraysize(kExpElementIDs) - 1) {
513 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
514 render_view_host());
515 } else {
516 // On the last tab key press, the focus returns to the browser.
517 ui_test_utils::WaitForFocusInBrowser(browser());
518 }
initial.commit09911bf2008-07-26 23:55:29519
520 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01521 std::string actual;
522 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25523 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18524 L"",
[email protected]45671612009-04-29 22:24:01525 L"window.domAutomationController.send(getFocusedElement());",
526 &actual));
initial.commit09911bf2008-07-26 23:55:29527 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
528 }
[email protected]8bcdec92009-02-25 16:15:18529
530 // At this point the renderer has sent us a message asking to advance the
531 // focus (as the end of the focus loop was reached in the renderer).
532 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42533 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29534 }
535}
536
[email protected]9e0c83a2009-05-06 19:44:37537// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41538IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]853300a82010-07-27 21:17:57539 BringBrowserWindowToFront();
[email protected]9e0c83a2009-05-06 19:44:37540 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57541 ASSERT_TRUE(server);
[email protected]9e0c83a2009-05-06 19:44:37542
543 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30544 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37545 ui_test_utils::NavigateToURL(browser(), url);
546
[email protected]9e0c83a2009-05-06 19:44:37547 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06548 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37549
550 // Let's show an interstitial.
551 TestInterstitialPage* interstitial_page =
552 new TestInterstitialPage(browser()->GetSelectedTabContents(),
553 true, GURL("http://interstitial.com"));
554 interstitial_page->Show();
555 // Give some time for the interstitial to show.
556 MessageLoop::current()->PostDelayedTask(FROM_HERE,
557 new MessageLoop::QuitTask(),
558 1000);
559 ui_test_utils::RunMessageLoop();
560
[email protected]fc2e0872009-08-21 22:14:41561 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37562
563 const char* kExpElementIDs[] = {
564 "", // Initially no element in the page should be focused
565 // (the location bar is focused).
566 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
567 "gmapLink"
568 };
569
[email protected]fc2e0872009-08-21 22:14:41570 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
571
[email protected]9e0c83a2009-05-06 19:44:37572 // Test forward focus traversal.
573 for (int i = 0; i < 2; ++i) {
574 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06575 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37576
577 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35578 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37579 // Let's make sure the focus is on the expected element in the page.
580 std::string actual = interstitial_page->GetFocusedElement();
581 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
582
[email protected]130efb02009-09-18 18:54:35583 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30584 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35585
586 if (j < arraysize(kExpElementIDs) - 1) {
587 interstitial_page->WaitForFocusChange();
588 } else {
589 // On the last tab key press, the focus returns to the browser.
590 ui_test_utils::WaitForFocusInBrowser(browser());
591 }
[email protected]9e0c83a2009-05-06 19:44:37592 }
593
594 // At this point the renderer has sent us a message asking to advance the
595 // focus (as the end of the focus loop was reached in the renderer).
596 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42597 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37598 }
599
600 // Now let's try reverse focus traversal.
601 for (int i = 0; i < 2; ++i) {
602 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06603 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37604
605 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35606 for (size_t j = 0; j < 7; ++j) {
607 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30608 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35609
610 if (j < arraysize(kExpElementIDs) - 1) {
611 interstitial_page->WaitForFocusChange();
612 } else {
613 // On the last tab key press, the focus returns to the browser.
614 ui_test_utils::WaitForFocusInBrowser(browser());
615 }
[email protected]9e0c83a2009-05-06 19:44:37616
617 // Let's make sure the focus is on the expected element in the page.
618 std::string actual = interstitial_page->GetFocusedElement();
619 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
620 }
621
622 // At this point the renderer has sent us a message asking to advance the
623 // focus (as the end of the focus loop was reached in the renderer).
624 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42625 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37626 }
627}
628
629// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21630IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]853300a82010-07-27 21:17:57631 BringBrowserWindowToFront();
[email protected]9e0c83a2009-05-06 19:44:37632 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57633 ASSERT_TRUE(server);
[email protected]9e0c83a2009-05-06 19:44:37634
635 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30636 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37637 ui_test_utils::NavigateToURL(browser(), url);
638
[email protected]9e0c83a2009-05-06 19:44:37639 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06640 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37641 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
642 HasFocus());
643
[email protected]9d8a4642009-07-29 17:25:30644 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37645 TestInterstitialPage* interstitial_page =
646 new TestInterstitialPage(browser()->GetSelectedTabContents(),
647 true, GURL("http://interstitial.com"));
648 interstitial_page->Show();
649 // Give some time for the interstitial to show.
650 MessageLoop::current()->PostDelayedTask(FROM_HERE,
651 new MessageLoop::QuitTask(),
652 1000);
653 ui_test_utils::RunMessageLoop();
654
655 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06656 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37657 EXPECT_TRUE(interstitial_page->HasFocus());
658
659 // Hide the interstitial.
660 interstitial_page->DontProceed();
661
662 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06663 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37664}
665
[email protected]9bd491ee2008-12-10 22:31:07666// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21667IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]853300a82010-07-27 21:17:57668 BringBrowserWindowToFront();
[email protected]8bcdec92009-02-25 16:15:18669 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57670 ASSERT_TRUE(server);
license.botbf09a502008-08-24 00:55:55671
[email protected]9bd491ee2008-12-10 22:31:07672 // Open some page (any page that doesn't steal focus).
[email protected]f72a1cc2010-04-30 07:17:30673 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18674 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07675
[email protected]fc2e0872009-08-21 22:14:41676 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
[email protected]9bd491ee2008-12-10 22:31:07677
[email protected]853300a82010-07-27 21:17:57678#if defined(OS_MACOSX)
679 // Press Cmd+F, which will make the Find box open and request focus.
680 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
681 false, false, true,
682 new MessageLoop::QuitTask());
683#else
[email protected]9bd491ee2008-12-10 22:31:07684 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]fc2e0872009-08-21 22:14:41685 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30686 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18687 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57688#endif
[email protected]8bcdec92009-02-25 16:15:18689 ui_test_utils::RunMessageLoop();
690
691 // Ideally, we wouldn't sleep here and instead would intercept the
692 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
693 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
694 // - creating a factory used to create the delegate
695 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41696 MessageLoop::current()->PostDelayedTask(
697 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18698 ui_test_utils::RunMessageLoop();
699
[email protected]21abcc742009-10-23 02:52:06700 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07701
[email protected]fc2e0872009-08-21 22:14:41702 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06703 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07704
705 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57706#if defined(OS_MACOSX)
707 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
708 false, false, true,
709 new MessageLoop::QuitTask());
710#else
[email protected]fc2e0872009-08-21 22:14:41711 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30712 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18713 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57714#endif
[email protected]8bcdec92009-02-25 16:15:18715 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06716 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07717
718 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41719 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06720 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07721
722 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]853300a82010-07-27 21:17:57723#if defined(OS_MACOSX)
724 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false,
725 false, false, true,
[email protected]1b5a48c2010-04-29 23:08:30726 new MessageLoop::QuitTask());
[email protected]853300a82010-07-27 21:17:57727#else
728 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
729 false, false, false,
730 new MessageLoop::QuitTask());
731#endif
[email protected]8bcdec92009-02-25 16:15:18732 ui_test_utils::RunMessageLoop();
733
734 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41735 MessageLoop::current()->PostDelayedTask(
736 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18737 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06738 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07739}
[email protected]401513c2009-03-12 00:21:28740
741// Makes sure the focus is in the right location when opening the different
742// types of tabs.
[email protected]e4f4e0b2009-10-13 19:58:21743IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
[email protected]853300a82010-07-27 21:17:57744 BringBrowserWindowToFront();
[email protected]401513c2009-03-12 00:21:28745 // Open the history tab, focus should be on the tab contents.
746 browser()->ShowHistoryTab();
[email protected]f07d7bf2010-04-06 08:02:42747
748 ui_test_utils::RunAllPendingInMessageLoop();
749
[email protected]21abcc742009-10-23 02:52:06750 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28751
752 // Open the new tab, focus should be on the location bar.
753 browser()->NewTab();
[email protected]21abcc742009-10-23 02:52:06754 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28755
756 // Open the download tab, focus should be on the tab contents.
757 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06758 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43759
760 // Open about:blank, focus should be on the location bar.
761 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK,
[email protected]4a1665442010-06-28 16:09:39762 -1, TabStripModel::ADD_SELECTED, NULL,
763 std::string());
[email protected]21abcc742009-10-23 02:52:06764 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28765}
[email protected]9d8a4642009-07-29 17:25:30766
767// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21768IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]853300a82010-07-27 21:17:57769 BringBrowserWindowToFront();
[email protected]9d8a4642009-07-29 17:25:30770 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57771 ASSERT_TRUE(server);
[email protected]9d8a4642009-07-29 17:25:30772
[email protected]9d8a4642009-07-29 17:25:30773 // Open the new tab, reload.
774 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42775
776 ui_test_utils::RunAllPendingInMessageLoop();
777
[email protected]cb84d642010-06-10 00:56:28778 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40779 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30780 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06781 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30782
783 // Open a regular page, focus the location bar, reload.
[email protected]f72a1cc2010-04-30 07:17:30784 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41785 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06786 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28787 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40788 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30789 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41790 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06791 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30792}
793
[email protected]c38a1902010-07-23 20:55:15794// FocusOnReloadCrashedTab times out often on chromium os debug build.
[email protected]517d2fe72010-07-23 15:40:41795// http://crbug.com/50025
796#if defined(OS_CHROMEOS) && !defined(NDEBUG)
[email protected]c38a1902010-07-23 20:55:15797#define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab
[email protected]517d2fe72010-07-23 15:40:41798#else
799#define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab
800#endif
801
[email protected]9d8a4642009-07-29 17:25:30802// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]517d2fe72010-07-23 15:40:41803IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) {
[email protected]853300a82010-07-27 21:17:57804 BringBrowserWindowToFront();
[email protected]9d8a4642009-07-29 17:25:30805 HTTPTestServer* server = StartHTTPServer();
[email protected]853300a82010-07-27 21:17:57806 ASSERT_TRUE(server);
[email protected]9d8a4642009-07-29 17:25:30807
[email protected]9d8a4642009-07-29 17:25:30808 // Open a regular page, crash, reload.
[email protected]f72a1cc2010-04-30 07:17:30809 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30810 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28811 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40812 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30813 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41814 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06815 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30816}
[email protected]629e0342010-07-27 23:30:13817
818} // namespace