blob: 1dad0def14c997e93bba7e3f28971a62dc9893b8 [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]8bcdec92009-02-25 16:15:188#include "base/message_loop.h"
[email protected]ece3c8b2009-03-27 16:55:399#include "base/ref_counted.h"
[email protected]8bcdec92009-02-25 16:15:1810#include "chrome/browser/automation/ui_controls.h"
11#include "chrome/browser/browser.h"
[email protected]134c47b92009-08-19 03:33:4412#include "chrome/browser/browser_window.h"
13#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9e0c83a2009-05-06 19:44:3714#include "chrome/browser/renderer_host/render_widget_host_view.h"
15#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]186f13f2009-08-19 20:34:0016#include "chrome/browser/tab_contents/tab_contents.h"
17#include "chrome/browser/tab_contents/tab_contents_view.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3719#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1820#include "chrome/test/in_process_browser_test.h"
21#include "chrome/test/ui_test_utils.h"
[email protected]2362e4f2009-05-08 00:34:0522#include "views/focus/focus_manager.h"
23#include "views/view.h"
24#include "views/window/window.h"
initial.commit09911bf2008-07-26 23:55:2925
[email protected]134c47b92009-08-19 03:33:4426#if defined(TOOLKIT_VIEWS)
27#include "chrome/browser/views/frame/browser_view.h"
[email protected]265ccd92010-04-29 17:57:1728#include "chrome/browser/views/location_bar/location_bar_view.h"
[email protected]134c47b92009-08-19 03:33:4429#include "chrome/browser/views/tab_contents/tab_contents_container.h"
30#endif
31
[email protected]753efc42010-03-09 19:52:1632#if defined(TOOLKIT_USES_GTK)
[email protected]b9821882009-08-17 22:25:1733#include "chrome/browser/gtk/view_id_util.h"
34#endif
35
[email protected]fc2e0872009-08-21 22:14:4136#if defined(OS_LINUX)
37// For some reason we hit an external DNS lookup in this test in Linux but not
38// on Windows. TODO(estade): investigate.
39#define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3540// TODO(jcampan): http://crbug.com/23683
[email protected]bbe383d2010-07-13 21:49:5941#define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4142#else
[email protected]e4f4e0b2009-10-13 19:58:2143#define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial
[email protected]cb7e2542009-12-14 22:02:3544#define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4145#endif
46
initial.commit09911bf2008-07-26 23:55:2947namespace {
48
[email protected]8bcdec92009-02-25 16:15:1849// The delay waited in some cases where we don't have a notifications for an
50// action we take.
initial.commit09911bf2008-07-26 23:55:2951const int kActionDelayMs = 500;
52
[email protected]f72a1cc2010-04-30 07:17:3053const char kSimplePage[] = "files/focus/page_with_focus.html";
54const char kStealFocusPage[] = "files/focus/page_steals_focus.html";
55const char kTypicalPage[] = "files/focus/typical_page.html";
[email protected]b65de8b92009-09-14 19:36:3156const char kTypicalPageName[] = "typical_page.html";
initial.commit09911bf2008-07-26 23:55:2957
[email protected]8bcdec92009-02-25 16:15:1858class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2959 public:
60 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1861 set_show_window(true);
62 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2963 }
[email protected]b9821882009-08-17 22:25:1764
[email protected]21abcc742009-10-23 02:52:0665 bool IsViewFocused(ViewID vid) {
66 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1767 }
68
[email protected]fc2e0872009-08-21 22:14:4169 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0670 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4171 }
72
[email protected]186f13f2009-08-19 20:34:0073 static void HideNativeWindow(gfx::NativeWindow window) {
74#if defined(OS_WIN)
75 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of
76 // using Windows API.
77 ::ShowWindow(window, SW_HIDE);
[email protected]753efc42010-03-09 19:52:1678#elif defined(TOOLKIT_USES_GTK)
[email protected]186f13f2009-08-19 20:34:0079 gtk_widget_hide(GTK_WIDGET(window));
80#else
81 NOTIMPLEMENTED();
82#endif
83 }
84
85 static void ShowNativeWindow(gfx::NativeWindow window) {
86#if defined(OS_WIN)
87 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of
88 // using Windows API.
89 ::ShowWindow(window, SW_SHOW);
[email protected]753efc42010-03-09 19:52:1690#elif defined(TOOLKIT_USES_GTK)
[email protected]186f13f2009-08-19 20:34:0091 gtk_widget_hide(GTK_WIDGET(window));
92#else
93 NOTIMPLEMENTED();
94#endif
95 }
initial.commit09911bf2008-07-26 23:55:2996};
97
[email protected]9e0c83a2009-05-06 19:44:3798class TestInterstitialPage : public InterstitialPage {
99 public:
100 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
101 : InterstitialPage(tab, new_navigation, url),
[email protected]130efb02009-09-18 18:54:35102 waiting_for_dom_response_(false),
103 waiting_for_focus_change_(false) {
[email protected]b65de8b92009-09-14 19:36:31104 FilePath file_path;
[email protected]9e0c83a2009-05-06 19:44:37105 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
106 EXPECT_TRUE(r);
[email protected]b65de8b92009-09-14 19:36:31107 file_path = file_path.AppendASCII("focus");
108 file_path = file_path.AppendASCII(kTypicalPageName);
[email protected]9e0c83a2009-05-06 19:44:37109 r = file_util::ReadFileToString(file_path, &html_contents_);
110 EXPECT_TRUE(r);
111 }
112
113 virtual std::string GetHTMLContents() {
114 return html_contents_;
115 }
116
117 virtual void DomOperationResponse(const std::string& json_string,
118 int automation_id) {
119 if (waiting_for_dom_response_) {
120 dom_response_ = json_string;
121 waiting_for_dom_response_ = false;
122 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
123 return;
124 }
125 InterstitialPage::DomOperationResponse(json_string, automation_id);
126 }
127
128 std::string GetFocusedElement() {
129 std::wstring script = L"window.domAutomationController.setAutomationId(0);"
130 L"window.domAutomationController.send(getFocusedElement());";
131
132 render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
133 DCHECK(!waiting_for_dom_response_);
134 waiting_for_dom_response_ = true;
135 ui_test_utils::RunMessageLoop();
136 // Remove the JSON extra quotes.
137 if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
138 dom_response_[dom_response_.size() - 1] == '"') {
139 dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
140 }
141 return dom_response_;
142 }
143
144 bool HasFocus() {
145 return render_view_host()->view()->HasFocus();
146 }
147
[email protected]130efb02009-09-18 18:54:35148 void WaitForFocusChange() {
149 waiting_for_focus_change_ = true;
150 ui_test_utils::RunMessageLoop();
151 }
152
153 protected:
154 virtual void FocusedNodeChanged() {
155 if (!waiting_for_focus_change_)
156 return;
157
158 waiting_for_focus_change_= false;
159 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
160 }
161
[email protected]9e0c83a2009-05-06 19:44:37162 private:
163 std::string html_contents_;
164
165 bool waiting_for_dom_response_;
[email protected]130efb02009-09-18 18:54:35166 bool waiting_for_focus_change_;
[email protected]9e0c83a2009-05-06 19:44:37167 std::string dom_response_;
[email protected]9e0c83a2009-05-06 19:44:37168};
[email protected]b9821882009-08-17 22:25:17169
initial.commit09911bf2008-07-26 23:55:29170} // namespace
171
[email protected]e4f4e0b2009-10-13 19:58:21172IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) {
[email protected]753efc42010-03-09 19:52:16173#if defined(USE_X11)
[email protected]fc2e0872009-08-21 22:14:41174 // It seems we have to wait a little bit for the widgets to spin up before
175 // we can start clicking on them.
176 MessageLoop::current()->PostDelayedTask(FROM_HERE,
177 new MessageLoop::QuitTask(),
178 kActionDelayMs);
179 ui_test_utils::RunMessageLoop();
180#endif
181
[email protected]21abcc742009-10-23 02:52:06182 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00183
[email protected]fc2e0872009-08-21 22:14:41184 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06185 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]186f13f2009-08-19 20:34:00186
[email protected]fc2e0872009-08-21 22:14:41187 ClickOnView(VIEW_ID_LOCATION_BAR);
[email protected]21abcc742009-10-23 02:52:06188 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]186f13f2009-08-19 20:34:00189}
[email protected]186f13f2009-08-19 20:34:00190
[email protected]e4f4e0b2009-10-13 19:58:21191IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]8bcdec92009-02-25 16:15:18192 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29193
194 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30195 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18196 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29197
[email protected]186f13f2009-08-19 20:34:00198 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
199
initial.commit09911bf2008-07-26 23:55:29200 // The focus should be on the Tab contents.
[email protected]21abcc742009-10-23 02:52:06201 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29202 // Now hide the window, show it again, the focus should not have changed.
[email protected]186f13f2009-08-19 20:34:00203 HideNativeWindow(window);
204 ShowNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06205 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
initial.commit09911bf2008-07-26 23:55:29206
[email protected]186f13f2009-08-19 20:34:00207 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06208 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29209 // Hide the window, show it again, the focus should not have changed.
[email protected]186f13f2009-08-19 20:34:00210 HideNativeWindow(window);
211 ShowNativeWindow(window);
[email protected]21abcc742009-10-23 02:52:06212 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29213
[email protected]186f13f2009-08-19 20:34:00214 // The rest of this test does not make sense on Linux because the behavior
215 // of Activate() is not well defined and can vary by window manager.
216#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29217 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18218 Browser* browser2 = Browser::Create(browser()->profile());
219 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43220 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18221 browser2->window()->Show();
222 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29223
[email protected]8bcdec92009-02-25 16:15:18224 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00225 BrowserView* browser_view2 =
226 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18227 ASSERT_TRUE(browser_view2);
228 views::FocusManager* focus_manager2 =
[email protected]82166b62009-06-30 18:48:00229 views::FocusManager::GetFocusManagerForNativeView(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18230 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54231 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38232 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29233
234 // Switch to the 1st browser window, focus should still be on the location
235 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18236 browser()->window()->Activate();
[email protected]21abcc742009-10-23 02:52:06237 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]8bcdec92009-02-25 16:15:18238 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29239
240 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18241 browser2->window()->Activate();
[email protected]186f13f2009-08-19 20:34:00242 EXPECT_EQ(NULL,
243 views::FocusManager::GetFocusManagerForNativeView(
244 browser()->window()->GetNativeHandle())->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54245 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38246 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18247
248 // Close the 2nd browser to avoid a DCHECK().
249 browser_view2->Close();
[email protected]186f13f2009-08-19 20:34:00250#endif
initial.commit09911bf2008-07-26 23:55:29251}
252
253// Tabs remember focus.
[email protected]e4f4e0b2009-10-13 19:58:21254IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
[email protected]8bcdec92009-02-25 16:15:18255 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29256
257 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30258 GURL url = server->TestServerPage(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18259 ui_test_utils::NavigateToURL(browser(), url);
260
initial.commit09911bf2008-07-26 23:55:29261 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58262 for (int i = 0; i < 4; ++i) {
[email protected]715af7e2010-04-29 01:55:38263 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
[email protected]4a1665442010-06-28 16:09:39264 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]22735af62009-04-07 21:09:58265 }
initial.commit09911bf2008-07-26 23:55:29266
267 // Alternate focus for the tab.
268 const bool kFocusPage[3][5] = {
269 { true, true, true, true, false },
270 { false, false, false, false, false },
271 { false, true, false, true, false }
272 };
273
274 for (int i = 1; i < 3; i++) {
275 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18276 // Activate the tab.
277 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29278
279 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54280 if (kFocusPage[i][j]) {
[email protected]186f13f2009-08-19 20:34:00281 browser()->GetTabContentsAt(j)->view()->Focus();
[email protected]7e383692009-06-12 19:14:54282 } else {
[email protected]186f13f2009-08-19 20:34:00283 browser()->FocusLocationBar();
[email protected]7e383692009-06-12 19:14:54284 }
initial.commit09911bf2008-07-26 23:55:29285 }
286
287 // Now come back to the tab and check the right view is focused.
288 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18289 // Activate the tab.
290 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29291
[email protected]186f13f2009-08-19 20:34:00292 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
293 VIEW_ID_LOCATION_BAR;
[email protected]21abcc742009-10-23 02:52:06294 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29295 }
[email protected]cb7e2542009-12-14 22:02:35296
297 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
298 browser()->SelectTabContentsAt(0, true);
299 // Try the above, but with ctrl+tab. Since tab normally changes focus,
300 // this has regressed in the past. Loop through several times to be sure.
301 for (int j = 0; j < 15; j++) {
302 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
303 VIEW_ID_LOCATION_BAR;
304 ASSERT_TRUE(IsViewFocused(vid));
305
306 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30307 false, false, false,
[email protected]cb7e2542009-12-14 22:02:35308 new MessageLoop::QuitTask());
309 ui_test_utils::RunMessageLoop();
310 }
311
312 // As above, but with ctrl+shift+tab.
313 browser()->SelectTabContentsAt(4, true);
314 for (int j = 14; j >= 0; --j) {
315 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW :
316 VIEW_ID_LOCATION_BAR;
317 ASSERT_TRUE(IsViewFocused(vid));
318
319 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true,
[email protected]1b5a48c2010-04-29 23:08:30320 true, false, false,
[email protected]cb7e2542009-12-14 22:02:35321 new MessageLoop::QuitTask());
322 ui_test_utils::RunMessageLoop();
323 }
initial.commit09911bf2008-07-26 23:55:29324 }
325}
326
[email protected]ae40b572009-10-02 21:17:45327// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35328IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]ae40b572009-10-02 21:17:45329 HTTPTestServer* server = StartHTTPServer();
330
331 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30332 GURL url = server->TestServerPage(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45333 ui_test_utils::NavigateToURL(browser(), url);
334
335 browser()->Find();
336 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(),
337 ASCIIToUTF16("a"), true, false, NULL);
[email protected]21abcc742009-10-23 02:52:06338 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45339
340 // Focus the location bar.
341 browser()->FocusLocationBar();
342
343 // Create a 2nd tab.
[email protected]4a1665442010-06-28 16:09:39344 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1,
345 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]ae40b572009-10-02 21:17:45346
347 // Focus should be on the recently opened tab page.
[email protected]21abcc742009-10-23 02:52:06348 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45349
350 // Select 1st tab, focus should still be on the location-bar.
351 // (bug http://crbug.com/23296)
352 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06353 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]ae40b572009-10-02 21:17:45354
355 // Now open the find box again, switch to another tab and come back, the focus
356 // should return to the find box.
357 browser()->Find();
[email protected]21abcc742009-10-23 02:52:06358 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45359 browser()->SelectTabContentsAt(1, true);
[email protected]21abcc742009-10-23 02:52:06360 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]ae40b572009-10-02 21:17:45361 browser()->SelectTabContentsAt(0, true);
[email protected]21abcc742009-10-23 02:52:06362 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45363}
364
initial.commit09911bf2008-07-26 23:55:29365// Background window does not steal focus.
[email protected]e4f4e0b2009-10-13 19:58:21366IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
[email protected]8bcdec92009-02-25 16:15:18367 HTTPTestServer* server = StartHTTPServer();
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;
396#endif
397
[email protected]f72a1cc2010-04-30 07:17:30398 GURL steal_focus_url = server->TestServerPage(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00399 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46400
[email protected]8bcdec92009-02-25 16:15:18401 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00402 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29403
404 // Wait for the focus to be stolen by the other browser.
[email protected]f07d7bf2010-04-06 08:02:42405 MessageLoop::current()->PostDelayedTask(
406 FROM_HERE, new MessageLoop::QuitTask(), 2000);
407 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29408
[email protected]8bcdec92009-02-25 16:15:18409 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00410 EXPECT_TRUE(focused_browser->window()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18411
412 // Close the 2nd browser to avoid a DCHECK().
[email protected]186f13f2009-08-19 20:34:00413 browser2->window()->Close();
initial.commit09911bf2008-07-26 23:55:29414}
415
416// Page cannot steal focus when focus is on location bar.
[email protected]e4f4e0b2009-10-13 19:58:21417IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
[email protected]8bcdec92009-02-25 16:15:18418 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29419
420 // Open the page that steals focus.
[email protected]f72a1cc2010-04-30 07:17:30421 GURL url = server->TestServerPage(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18422 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29423
[email protected]186f13f2009-08-19 20:34:00424 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29425
426 // Wait for the page to steal focus.
[email protected]186f13f2009-08-19 20:34:00427 PlatformThread::Sleep(2000);
initial.commit09911bf2008-07-26 23:55:29428
429 // Make sure the location bar is still focused.
[email protected]21abcc742009-10-23 02:52:06430 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29431}
432
[email protected]9e0c83a2009-05-06 19:44:37433// Focus traversal on a regular page.
[email protected]130efb02009-09-18 18:54:35434// Note that this test relies on a notification from the renderer that the
435// focus has changed in the page. The notification in the renderer may change
436// at which point this test would fail (see comment in
437// RenderWidget::didFocus()).
[email protected]e4f4e0b2009-10-13 19:58:21438IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
[email protected]8bcdec92009-02-25 16:15:18439 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29440
[email protected]8bcdec92009-02-25 16:15:18441 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30442 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18443 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29444
[email protected]186f13f2009-08-19 20:34:00445 browser()->FocusLocationBar();
initial.commit09911bf2008-07-26 23:55:29446
[email protected]8bcdec92009-02-25 16:15:18447 const char* kExpElementIDs[] = {
448 "", // Initially no element in the page should be focused
449 // (the location bar is focused).
450 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
451 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29452 };
453
[email protected]186f13f2009-08-19 20:34:00454 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
455
initial.commit09911bf2008-07-26 23:55:29456 // Test forward focus traversal.
457 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42458 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29459 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06460 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29461
462 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35463 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) {
[email protected]f07d7bf2010-04-06 08:02:42464 SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j));
initial.commit09911bf2008-07-26 23:55:29465 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01466 std::string actual;
467 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25468 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18469 L"",
[email protected]45671612009-04-29 22:24:01470 L"window.domAutomationController.send(getFocusedElement());",
471 &actual));
initial.commit09911bf2008-07-26 23:55:29472 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
473
[email protected]130efb02009-09-18 18:54:35474 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30475 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35476
477 if (j < arraysize(kExpElementIDs) - 1) {
478 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
479 render_view_host());
480 } else {
481 // On the last tab key press, the focus returns to the browser.
482 ui_test_utils::WaitForFocusInBrowser(browser());
483 }
initial.commit09911bf2008-07-26 23:55:29484 }
[email protected]8bcdec92009-02-25 16:15:18485
486 // At this point the renderer has sent us a message asking to advance the
487 // focus (as the end of the focus loop was reached in the renderer).
488 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42489 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29490 }
491
492 // Now let's try reverse focus traversal.
493 for (int i = 0; i < 3; ++i) {
[email protected]f07d7bf2010-04-06 08:02:42494 SCOPED_TRACE(StringPrintf("outer loop: %d", i));
initial.commit09911bf2008-07-26 23:55:29495 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06496 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
initial.commit09911bf2008-07-26 23:55:29497
[email protected]8bcdec92009-02-25 16:15:18498 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35499 for (size_t j = 0; j < 7; ++j) {
[email protected]f07d7bf2010-04-06 08:02:42500 SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j));
[email protected]130efb02009-09-18 18:54:35501 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30502 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35503
504 if (j < arraysize(kExpElementIDs) - 1) {
505 ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()->
506 render_view_host());
507 } else {
508 // On the last tab key press, the focus returns to the browser.
509 ui_test_utils::WaitForFocusInBrowser(browser());
510 }
initial.commit09911bf2008-07-26 23:55:29511
512 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01513 std::string actual;
514 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]17c4f3c2009-07-04 16:36:25515 browser()->GetSelectedTabContents()->render_view_host(),
[email protected]8bcdec92009-02-25 16:15:18516 L"",
[email protected]45671612009-04-29 22:24:01517 L"window.domAutomationController.send(getFocusedElement());",
518 &actual));
initial.commit09911bf2008-07-26 23:55:29519 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
520 }
[email protected]8bcdec92009-02-25 16:15:18521
522 // At this point the renderer has sent us a message asking to advance the
523 // focus (as the end of the focus loop was reached in the renderer).
524 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42525 ui_test_utils::RunAllPendingInMessageLoop();
initial.commit09911bf2008-07-26 23:55:29526 }
527}
528
[email protected]9e0c83a2009-05-06 19:44:37529// Focus traversal while an interstitial is showing.
[email protected]fc2e0872009-08-21 22:14:41530IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) {
[email protected]9e0c83a2009-05-06 19:44:37531 HTTPTestServer* server = StartHTTPServer();
532
533 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30534 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37535 ui_test_utils::NavigateToURL(browser(), url);
536
[email protected]9e0c83a2009-05-06 19:44:37537 // Focus should be on the page.
[email protected]21abcc742009-10-23 02:52:06538 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37539
540 // Let's show an interstitial.
541 TestInterstitialPage* interstitial_page =
542 new TestInterstitialPage(browser()->GetSelectedTabContents(),
543 true, GURL("http://interstitial.com"));
544 interstitial_page->Show();
545 // Give some time for the interstitial to show.
546 MessageLoop::current()->PostDelayedTask(FROM_HERE,
547 new MessageLoop::QuitTask(),
548 1000);
549 ui_test_utils::RunMessageLoop();
550
[email protected]fc2e0872009-08-21 22:14:41551 browser()->FocusLocationBar();
[email protected]9e0c83a2009-05-06 19:44:37552
553 const char* kExpElementIDs[] = {
554 "", // Initially no element in the page should be focused
555 // (the location bar is focused).
556 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
557 "gmapLink"
558 };
559
[email protected]fc2e0872009-08-21 22:14:41560 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
561
[email protected]9e0c83a2009-05-06 19:44:37562 // Test forward focus traversal.
563 for (int i = 0; i < 2; ++i) {
564 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06565 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37566
567 // Now let's press tab to move the focus.
[email protected]130efb02009-09-18 18:54:35568 for (size_t j = 0; j < 7; ++j) {
[email protected]9e0c83a2009-05-06 19:44:37569 // Let's make sure the focus is on the expected element in the page.
570 std::string actual = interstitial_page->GetFocusedElement();
571 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
572
[email protected]130efb02009-09-18 18:54:35573 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30574 false, false, false, false));
[email protected]130efb02009-09-18 18:54:35575
576 if (j < arraysize(kExpElementIDs) - 1) {
577 interstitial_page->WaitForFocusChange();
578 } else {
579 // On the last tab key press, the focus returns to the browser.
580 ui_test_utils::WaitForFocusInBrowser(browser());
581 }
[email protected]9e0c83a2009-05-06 19:44:37582 }
583
584 // At this point the renderer has sent us a message asking to advance the
585 // focus (as the end of the focus loop was reached in the renderer).
586 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42587 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37588 }
589
590 // Now let's try reverse focus traversal.
591 for (int i = 0; i < 2; ++i) {
592 // Location bar should be focused.
[email protected]21abcc742009-10-23 02:52:06593 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9e0c83a2009-05-06 19:44:37594
595 // Now let's press shift-tab to move the focus in reverse.
[email protected]130efb02009-09-18 18:54:35596 for (size_t j = 0; j < 7; ++j) {
597 ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB,
[email protected]1b5a48c2010-04-29 23:08:30598 false, true, false, false));
[email protected]130efb02009-09-18 18:54:35599
600 if (j < arraysize(kExpElementIDs) - 1) {
601 interstitial_page->WaitForFocusChange();
602 } else {
603 // On the last tab key press, the focus returns to the browser.
604 ui_test_utils::WaitForFocusInBrowser(browser());
605 }
[email protected]9e0c83a2009-05-06 19:44:37606
607 // Let's make sure the focus is on the expected element in the page.
608 std::string actual = interstitial_page->GetFocusedElement();
609 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
610 }
611
612 // At this point the renderer has sent us a message asking to advance the
613 // focus (as the end of the focus loop was reached in the renderer).
614 // We need to run the message loop to process it.
[email protected]f07d7bf2010-04-06 08:02:42615 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]9e0c83a2009-05-06 19:44:37616 }
617}
618
619// Focus stays on page with interstitials.
[email protected]e4f4e0b2009-10-13 19:58:21620IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]9e0c83a2009-05-06 19:44:37621 HTTPTestServer* server = StartHTTPServer();
622
623 // First we navigate to our test page.
[email protected]f72a1cc2010-04-30 07:17:30624 GURL url = server->TestServerPage(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37625 ui_test_utils::NavigateToURL(browser(), url);
626
[email protected]9e0c83a2009-05-06 19:44:37627 // Page should have focus.
[email protected]21abcc742009-10-23 02:52:06628 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37629 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
630 HasFocus());
631
[email protected]9d8a4642009-07-29 17:25:30632 // Let's show an interstitial.
[email protected]9e0c83a2009-05-06 19:44:37633 TestInterstitialPage* interstitial_page =
634 new TestInterstitialPage(browser()->GetSelectedTabContents(),
635 true, GURL("http://interstitial.com"));
636 interstitial_page->Show();
637 // Give some time for the interstitial to show.
638 MessageLoop::current()->PostDelayedTask(FROM_HERE,
639 new MessageLoop::QuitTask(),
640 1000);
641 ui_test_utils::RunMessageLoop();
642
643 // The interstitial should have focus now.
[email protected]21abcc742009-10-23 02:52:06644 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37645 EXPECT_TRUE(interstitial_page->HasFocus());
646
647 // Hide the interstitial.
648 interstitial_page->DontProceed();
649
650 // Focus should be back on the original page.
[email protected]21abcc742009-10-23 02:52:06651 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9e0c83a2009-05-06 19:44:37652}
653
[email protected]9bd491ee2008-12-10 22:31:07654// Make sure Find box can request focus, even when it is already open.
[email protected]e4f4e0b2009-10-13 19:58:21655IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
[email protected]8bcdec92009-02-25 16:15:18656 HTTPTestServer* server = StartHTTPServer();
license.botbf09a502008-08-24 00:55:55657
[email protected]9bd491ee2008-12-10 22:31:07658 // Open some page (any page that doesn't steal focus).
[email protected]f72a1cc2010-04-30 07:17:30659 GURL url = server->TestServerPage(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18660 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07661
[email protected]fc2e0872009-08-21 22:14:41662 gfx::NativeWindow window = browser()->window()->GetNativeHandle();
[email protected]9bd491ee2008-12-10 22:31:07663
664 // Press Ctrl+F, which will make the Find box open and request focus.
[email protected]fc2e0872009-08-21 22:14:41665 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true,
[email protected]1b5a48c2010-04-29 23:08:30666 false, false, false,
[email protected]8bcdec92009-02-25 16:15:18667 new MessageLoop::QuitTask());
668 ui_test_utils::RunMessageLoop();
669
670 // Ideally, we wouldn't sleep here and instead would intercept the
671 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
672 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
673 // - creating a factory used to create the delegate
674 // - making the test a private and overwriting the delegate member directly.
[email protected]fc2e0872009-08-21 22:14:41675 MessageLoop::current()->PostDelayedTask(
676 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18677 ui_test_utils::RunMessageLoop();
678
[email protected]21abcc742009-10-23 02:52:06679 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07680
[email protected]fc2e0872009-08-21 22:14:41681 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06682 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9bd491ee2008-12-10 22:31:07683
684 // Now press Ctrl+F again and focus should move to the Find box.
[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());
688 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06689 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07690
691 // Set focus to the page.
[email protected]fc2e0872009-08-21 22:14:41692 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]21abcc742009-10-23 02:52:06693 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9bd491ee2008-12-10 22:31:07694
695 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]fc2e0872009-08-21 22:14:41696 ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, false,
[email protected]1b5a48c2010-04-29 23:08:30697 false, false,
698 new MessageLoop::QuitTask());
[email protected]8bcdec92009-02-25 16:15:18699 ui_test_utils::RunMessageLoop();
700
701 // See remark above on why we wait.
[email protected]fc2e0872009-08-21 22:14:41702 MessageLoop::current()->PostDelayedTask(
703 FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18704 ui_test_utils::RunMessageLoop();
[email protected]21abcc742009-10-23 02:52:06705 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07706}
[email protected]401513c2009-03-12 00:21:28707
708// Makes sure the focus is in the right location when opening the different
709// types of tabs.
[email protected]e4f4e0b2009-10-13 19:58:21710IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
[email protected]401513c2009-03-12 00:21:28711 // Open the history tab, focus should be on the tab contents.
712 browser()->ShowHistoryTab();
[email protected]f07d7bf2010-04-06 08:02:42713
714 ui_test_utils::RunAllPendingInMessageLoop();
715
[email protected]21abcc742009-10-23 02:52:06716 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]401513c2009-03-12 00:21:28717
718 // Open the new tab, focus should be on the location bar.
719 browser()->NewTab();
[email protected]21abcc742009-10-23 02:52:06720 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28721
722 // Open the download tab, focus should be on the tab contents.
723 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06724 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]3e3f0eb2009-06-22 18:33:43725
726 // Open about:blank, focus should be on the location bar.
727 browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK,
[email protected]4a1665442010-06-28 16:09:39728 -1, TabStripModel::ADD_SELECTED, NULL,
729 std::string());
[email protected]21abcc742009-10-23 02:52:06730 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]401513c2009-03-12 00:21:28731}
[email protected]9d8a4642009-07-29 17:25:30732
733// Tests that focus goes where expected when using reload.
[email protected]e4f4e0b2009-10-13 19:58:21734IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]9d8a4642009-07-29 17:25:30735 HTTPTestServer* server = StartHTTPServer();
736
[email protected]9d8a4642009-07-29 17:25:30737 // Open the new tab, reload.
738 browser()->NewTab();
[email protected]f07d7bf2010-04-06 08:02:42739
740 ui_test_utils::RunAllPendingInMessageLoop();
741
[email protected]cb84d642010-06-10 00:56:28742 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40743 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30744 // Focus should stay on the location bar.
[email protected]21abcc742009-10-23 02:52:06745 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]9d8a4642009-07-29 17:25:30746
747 // Open a regular page, focus the location bar, reload.
[email protected]f72a1cc2010-04-30 07:17:30748 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]fc2e0872009-08-21 22:14:41749 browser()->FocusLocationBar();
[email protected]21abcc742009-10-23 02:52:06750 ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR));
[email protected]cb84d642010-06-10 00:56:28751 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40752 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30753 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41754 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06755 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30756}
757
758// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]e4f4e0b2009-10-13 19:58:21759IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) {
[email protected]9d8a4642009-07-29 17:25:30760 HTTPTestServer* server = StartHTTPServer();
761
[email protected]9d8a4642009-07-29 17:25:30762 // Open a regular page, crash, reload.
[email protected]f72a1cc2010-04-30 07:17:30763 ui_test_utils::NavigateToURL(browser(), server->TestServerPage(kSimplePage));
[email protected]9d8a4642009-07-29 17:25:30764 ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
[email protected]cb84d642010-06-10 00:56:28765 browser()->Reload(CURRENT_TAB);
[email protected]b7a20d32009-08-15 00:02:40766 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
[email protected]9d8a4642009-07-29 17:25:30767 // Focus should now be on the tab contents.
[email protected]fc2e0872009-08-21 22:14:41768 browser()->ShowDownloadsTab();
[email protected]21abcc742009-10-23 02:52:06769 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
[email protected]9d8a4642009-07-29 17:25:30770}