blob: 58b56cd58cf8c04d57be3aecc1fcd26113cca4af [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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]8bcdec92009-02-25 16:15:185#include "base/message_loop.h"
[email protected]ece3c8b2009-03-27 16:55:396#include "base/ref_counted.h"
[email protected]8bcdec92009-02-25 16:15:187#include "chrome/browser/automation/ui_controls.h"
8#include "chrome/browser/browser.h"
9#include "chrome/browser/dom_operation_notification_details.h"
10#include "chrome/browser/tab_contents/web_contents.h"
initial.commit09911bf2008-07-26 23:55:2911#include "chrome/browser/view_ids.h"
[email protected]8bcdec92009-02-25 16:15:1812#include "chrome/browser/views/frame/browser_view.h"
13#include "chrome/browser/views/location_bar_view.h"
14#include "chrome/common/notification_details.h"
15#include "chrome/common/notification_observer.h"
16#include "chrome/common/notification_service.h"
17#include "chrome/common/notification_source.h"
18#include "chrome/common/notification_type.h"
[email protected]08682a92009-03-17 02:54:0219#include "chrome/views/focus/focus_manager.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/views/view.h"
[email protected]0e8588c12009-03-17 01:44:3621#include "chrome/views/window/window.h"
[email protected]8bcdec92009-02-25 16:15:1822#include "chrome/test/in_process_browser_test.h"
23#include "chrome/test/ui_test_utils.h"
[email protected]ece3c8b2009-03-27 16:55:3924#include "net/base/host_resolver_unittest.h"
initial.commit09911bf2008-07-26 23:55:2925
26namespace {
27
[email protected]8bcdec92009-02-25 16:15:1828// The delay waited in some cases where we don't have a notifications for an
29// action we take.
initial.commit09911bf2008-07-26 23:55:2930const int kActionDelayMs = 500;
31
initial.commit09911bf2008-07-26 23:55:2932const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html";
33const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html";
34const wchar_t kTypicalPage[] = L"files/focus/typical_page.html";
35
[email protected]8bcdec92009-02-25 16:15:1836class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2937 public:
38 BrowserFocusTest() {
[email protected]ece3c8b2009-03-27 16:55:3939 host_mapper_ = new net::RuleBasedHostMapper();
40 // TODO(phajdan.jr): Don't make a real dns lookup here.
41 // page_with_focus.html has a reference to google.com.
42 host_mapper_->AllowDirectLookup("*.google.com");
43 scoped_host_mapper_.Init(host_mapper_.get());
44
[email protected]8bcdec92009-02-25 16:15:1845 set_show_window(true);
46 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2947 }
[email protected]ece3c8b2009-03-27 16:55:3948
49private:
50 scoped_refptr<net::RuleBasedHostMapper> host_mapper_;
51 net::ScopedHostMapper scoped_host_mapper_;
initial.commit09911bf2008-07-26 23:55:2952};
53
[email protected]8bcdec92009-02-25 16:15:1854class JavaScriptRunner : public NotificationObserver {
55 public:
56 JavaScriptRunner(WebContents* web_contents,
57 const std::wstring& frame_xpath,
58 const std::wstring& jscript)
59 : web_contents_(web_contents),
60 frame_xpath_(frame_xpath),
61 jscript_(jscript) {
62 NotificationService::current()->
63 AddObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
64 Source<WebContents>(web_contents));
65 }
initial.commit09911bf2008-07-26 23:55:2966
[email protected]8bcdec92009-02-25 16:15:1867 virtual void Observe(NotificationType type,
68 const NotificationSource& source,
69 const NotificationDetails& details) {
70 Details<DomOperationNotificationDetails> dom_op_details(details);
71 result_ = dom_op_details->json();
72 // The Jasonified response has quotes, remove them.
73 if (result_.length() > 1 && result_[0] == '"')
74 result_ = result_.substr(1, result_.length() - 2);
initial.commit09911bf2008-07-26 23:55:2975
[email protected]8bcdec92009-02-25 16:15:1876 NotificationService::current()->
77 RemoveObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
78 Source<WebContents>(web_contents_));
79 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
80 }
initial.commit09911bf2008-07-26 23:55:2981
[email protected]8bcdec92009-02-25 16:15:1882 std::string Run() {
83 // The DOMAutomationController requires an automation ID, eventhough we are
84 // not using it in this case.
85 web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(
86 frame_xpath_, L"window.domAutomationController.setAutomationId(0);");
initial.commit09911bf2008-07-26 23:55:2987
[email protected]8bcdec92009-02-25 16:15:1888 web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath_,
89 jscript_);
90 ui_test_utils::RunMessageLoop();
91 return result_;
92 }
93
94 private:
95 WebContents* web_contents_;
96 std::wstring frame_xpath_;
97 std::wstring jscript_;
98 std::string result_;
99
100 DISALLOW_COPY_AND_ASSIGN(JavaScriptRunner);
101};
initial.commit09911bf2008-07-26 23:55:29102
103} // namespace
104
[email protected]8bcdec92009-02-25 16:15:18105IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
106 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29107
108 // First we navigate to our test page.
[email protected]dd265012009-01-08 20:45:27109 GURL url = server->TestServerPageW(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18110 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29111
112 // The focus should be on the Tab contents.
[email protected]8bcdec92009-02-25 16:15:18113 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
114 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
115 ASSERT_TRUE(browser_view);
116 views::FocusManager* focus_manager =
117 views::FocusManager::GetFocusManager(hwnd);
118 ASSERT_TRUE(focus_manager);
initial.commit09911bf2008-07-26 23:55:29119
[email protected]8bcdec92009-02-25 16:15:18120 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29121
122 // Now hide the window, show it again, the focus should not have changed.
[email protected]8bcdec92009-02-25 16:15:18123 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of
124 // using Windows API.
125 ::ShowWindow(hwnd, SW_HIDE);
126 ::ShowWindow(hwnd, SW_SHOW);
127 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29128
129 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18130 LocationBarView* location_bar = browser_view->GetLocationBarView();
131 ui_controls::MoveMouseToCenterAndPress(location_bar,
132 ui_controls::LEFT,
133 ui_controls::DOWN | ui_controls::UP,
134 new MessageLoop::QuitTask());
135 ui_test_utils::RunMessageLoop();
136 // Location bar should have focus.
137 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29138
139 // Hide the window, show it again, the focus should not have changed.
[email protected]8bcdec92009-02-25 16:15:18140 ::ShowWindow(hwnd, SW_HIDE);
141 ::ShowWindow(hwnd, SW_SHOW);
142 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29143
144 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18145 Browser* browser2 = Browser::Create(browser()->profile());
146 ASSERT_TRUE(browser2);
147 browser2->AddBlankTab(true);
148 browser2->window()->Show();
149 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29150
[email protected]8bcdec92009-02-25 16:15:18151 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
152 BrowserView* browser_view2 = BrowserView::GetBrowserViewForHWND(hwnd2);
153 ASSERT_TRUE(browser_view2);
154 views::FocusManager* focus_manager2 =
155 views::FocusManager::GetFocusManager(hwnd2);
156 ASSERT_TRUE(focus_manager2);
157 EXPECT_EQ(browser_view2->GetContentsView(), focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29158
159 // Switch to the 1st browser window, focus should still be on the location
160 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18161 browser()->window()->Activate();
162 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
163 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29164
165 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18166 browser2->window()->Activate();
167 EXPECT_EQ(NULL, focus_manager->GetFocusedView());
168 EXPECT_EQ(browser_view2->GetContentsView(), focus_manager2->GetFocusedView());
169
170 // Close the 2nd browser to avoid a DCHECK().
171 browser_view2->Close();
initial.commit09911bf2008-07-26 23:55:29172}
173
174// Tabs remember focus.
[email protected]8bcdec92009-02-25 16:15:18175IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
176 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29177
178 // First we navigate to our test page.
[email protected]dd265012009-01-08 20:45:27179 GURL url = server->TestServerPageW(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18180 ui_test_utils::NavigateToURL(browser(), url);
181
182 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
183 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
184 ASSERT_TRUE(browser_view);
185
186 views::FocusManager* focus_manager =
187 views::FocusManager::GetFocusManager(hwnd);
188 ASSERT_TRUE(focus_manager);
initial.commit09911bf2008-07-26 23:55:29189
190 // Create several tabs.
[email protected]8bcdec92009-02-25 16:15:18191 for (int i = 0; i < 4; ++i)
192 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, NULL);
initial.commit09911bf2008-07-26 23:55:29193
194 // Alternate focus for the tab.
195 const bool kFocusPage[3][5] = {
196 { true, true, true, true, false },
197 { false, false, false, false, false },
198 { false, true, false, true, false }
199 };
200
201 for (int i = 1; i < 3; i++) {
202 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18203 // Activate the tab.
204 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29205
206 // Activate the location bar or the page.
[email protected]8bcdec92009-02-25 16:15:18207 views::View* view_to_focus = kFocusPage[i][j] ?
208 browser_view->GetContentsView() :
209 browser_view->GetLocationBarView();
initial.commit09911bf2008-07-26 23:55:29210
[email protected]8bcdec92009-02-25 16:15:18211 ui_controls::MoveMouseToCenterAndPress(view_to_focus,
212 ui_controls::LEFT,
213 ui_controls::DOWN |
214 ui_controls::UP,
215 new MessageLoop::QuitTask());
216 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29217 }
218
219 // Now come back to the tab and check the right view is focused.
220 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18221 // Activate the tab.
222 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29223
224 // Activate the location bar or the page.
[email protected]8bcdec92009-02-25 16:15:18225 views::View* view = kFocusPage[i][j] ?
226 browser_view->GetContentsView() :
227 browser_view->GetLocationBarView();
228 EXPECT_EQ(view, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29229 }
230 }
231}
232
233// Background window does not steal focus.
[email protected]8bcdec92009-02-25 16:15:18234IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
235 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29236
237 // First we navigate to our test page.
[email protected]8bcdec92009-02-25 16:15:18238 GURL url = server->TestServerPageW(kSimplePage);
239 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29240
241 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18242 Browser* browser2 = Browser::Create(browser()->profile());
243 ASSERT_TRUE(browser2);
244 browser2->AddBlankTab(true);
245 browser2->window()->Show();
[email protected]1e187af2009-02-25 02:02:46246 GURL steal_focus_url = server->TestServerPageW(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18247 ui_test_utils::NavigateToURL(browser2, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46248
[email protected]8bcdec92009-02-25 16:15:18249 // Activate the first browser.
250 browser()->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29251
252 // Wait for the focus to be stolen by the other browser.
253 ::Sleep(2000);
254
[email protected]8bcdec92009-02-25 16:15:18255 // Make sure the first browser is still active.
256 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
257 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
258 ASSERT_TRUE(browser_view);
[email protected]32670b02009-03-03 00:28:00259 EXPECT_TRUE(browser_view->frame()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18260
261 // Close the 2nd browser to avoid a DCHECK().
262 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
263 BrowserView* browser_view2 = BrowserView::GetBrowserViewForHWND(hwnd2);
264 browser_view2->Close();
initial.commit09911bf2008-07-26 23:55:29265}
266
267// Page cannot steal focus when focus is on location bar.
[email protected]8bcdec92009-02-25 16:15:18268IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
269 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29270
271 // Open the page that steals focus.
[email protected]dd265012009-01-08 20:45:27272 GURL url = server->TestServerPageW(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18273 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29274
[email protected]8bcdec92009-02-25 16:15:18275 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
276 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
277 views::FocusManager* focus_manager =
278 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:29279
[email protected]9bd491ee2008-12-10 22:31:07280 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18281 LocationBarView* location_bar = browser_view->GetLocationBarView();
282 ui_controls::MoveMouseToCenterAndPress(location_bar,
283 ui_controls::LEFT,
284 ui_controls::DOWN | ui_controls::UP,
285 new MessageLoop::QuitTask());
286 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29287
288 // Wait for the page to steal focus.
289 ::Sleep(2000);
290
291 // Make sure the location bar is still focused.
[email protected]8bcdec92009-02-25 16:15:18292 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29293}
294
295// Focus traversal
[email protected]8bcdec92009-02-25 16:15:18296IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
297 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29298
[email protected]8bcdec92009-02-25 16:15:18299 // First we navigate to our test page.
[email protected]dd265012009-01-08 20:45:27300 GURL url = server->TestServerPageW(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18301 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29302
[email protected]8bcdec92009-02-25 16:15:18303 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
304 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
305 views::FocusManager* focus_manager =
306 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:29307
308 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18309 LocationBarView* location_bar = browser_view->GetLocationBarView();
310 ui_controls::MoveMouseToCenterAndPress(location_bar,
311 ui_controls::LEFT,
312 ui_controls::DOWN | ui_controls::UP,
313 new MessageLoop::QuitTask());
314 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29315
[email protected]8bcdec92009-02-25 16:15:18316 const char* kExpElementIDs[] = {
317 "", // Initially no element in the page should be focused
318 // (the location bar is focused).
319 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
320 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29321 };
322
323 // Test forward focus traversal.
324 for (int i = 0; i < 3; ++i) {
325 // Location bar should be focused.
[email protected]8bcdec92009-02-25 16:15:18326 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29327
328 // Now let's press tab to move the focus.
329 for (int j = 0; j < 7; ++j) {
330 // Let's make sure the focus is on the expected element in the page.
[email protected]8bcdec92009-02-25 16:15:18331 JavaScriptRunner js_runner(
332 browser()->GetSelectedTabContents()->AsWebContents(),
333 L"",
334 L"window.domAutomationController.send(getFocusedElement());");
335 std::string actual = js_runner.Run();
initial.commit09911bf2008-07-26 23:55:29336 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
337
[email protected]8bcdec92009-02-25 16:15:18338 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false,
339 new MessageLoop::QuitTask());
340 ui_test_utils::RunMessageLoop();
341 // Ideally, we wouldn't sleep here and instead would use the event
342 // processed ack nofification from the renderer. I am reluctant to create
343 // a new notification/callback for that purpose just for this test.
initial.commit09911bf2008-07-26 23:55:29344 ::Sleep(kActionDelayMs);
345 }
[email protected]8bcdec92009-02-25 16:15:18346
347 // At this point the renderer has sent us a message asking to advance the
348 // focus (as the end of the focus loop was reached in the renderer).
349 // We need to run the message loop to process it.
350 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
351 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29352 }
353
354 // Now let's try reverse focus traversal.
355 for (int i = 0; i < 3; ++i) {
356 // Location bar should be focused.
[email protected]8bcdec92009-02-25 16:15:18357 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29358
[email protected]8bcdec92009-02-25 16:15:18359 // Now let's press shift-tab to move the focus in reverse.
initial.commit09911bf2008-07-26 23:55:29360 for (int j = 0; j < 7; ++j) {
[email protected]8bcdec92009-02-25 16:15:18361 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false,
362 new MessageLoop::QuitTask());
363 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29364 ::Sleep(kActionDelayMs);
365
366 // Let's make sure the focus is on the expected element in the page.
[email protected]8bcdec92009-02-25 16:15:18367 JavaScriptRunner js_runner(
368 browser()->GetSelectedTabContents()->AsWebContents(),
369 L"",
370 L"window.domAutomationController.send(getFocusedElement());");
371 std::string actual = js_runner.Run();
initial.commit09911bf2008-07-26 23:55:29372 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
373 }
[email protected]8bcdec92009-02-25 16:15:18374
375 // At this point the renderer has sent us a message asking to advance the
376 // focus (as the end of the focus loop was reached in the renderer).
377 // We need to run the message loop to process it.
378 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
379 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29380 }
381}
382
[email protected]9bd491ee2008-12-10 22:31:07383// Make sure Find box can request focus, even when it is already open.
[email protected]8bcdec92009-02-25 16:15:18384IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
385 HTTPTestServer* server = StartHTTPServer();
license.botbf09a502008-08-24 00:55:55386
[email protected]9bd491ee2008-12-10 22:31:07387 // Open some page (any page that doesn't steal focus).
[email protected]dd265012009-01-08 20:45:27388 GURL url = server->TestServerPageW(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18389 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07390
[email protected]8bcdec92009-02-25 16:15:18391 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
392 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
393 views::FocusManager* focus_manager =
394 views::FocusManager::GetFocusManager(hwnd);
395 LocationBarView* location_bar = browser_view->GetLocationBarView();
[email protected]9bd491ee2008-12-10 22:31:07396
397 // Press Ctrl+F, which will make the Find box open and request focus.
398 static const int VK_F = 0x46;
[email protected]8bcdec92009-02-25 16:15:18399 ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false,
400 new MessageLoop::QuitTask());
401 ui_test_utils::RunMessageLoop();
402
403 // Ideally, we wouldn't sleep here and instead would intercept the
404 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
405 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
406 // - creating a factory used to create the delegate
407 // - making the test a private and overwriting the delegate member directly.
[email protected]9bd491ee2008-12-10 22:31:07408 ::Sleep(kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18409 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
410 ui_test_utils::RunMessageLoop();
411
412 views::View* focused_view = focus_manager->GetFocusedView();
413 ASSERT_TRUE(focused_view != NULL);
414 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
[email protected]9bd491ee2008-12-10 22:31:07415
416 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18417 ui_controls::MoveMouseToCenterAndPress(location_bar,
418 ui_controls::LEFT,
419 ui_controls::DOWN | ui_controls::UP,
420 new MessageLoop::QuitTask());
421 ui_test_utils::RunMessageLoop();
422
[email protected]9bd491ee2008-12-10 22:31:07423 // Make sure the location bar is focused.
[email protected]8bcdec92009-02-25 16:15:18424 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
[email protected]9bd491ee2008-12-10 22:31:07425
426 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]8bcdec92009-02-25 16:15:18427 ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false,
428 new MessageLoop::QuitTask());
429 ui_test_utils::RunMessageLoop();
430 focused_view = focus_manager->GetFocusedView();
431 ASSERT_TRUE(focused_view != NULL);
432 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
[email protected]9bd491ee2008-12-10 22:31:07433
434 // Set focus to the page.
[email protected]8bcdec92009-02-25 16:15:18435 ui_controls::MoveMouseToCenterAndPress(browser_view->GetContentsView(),
436 ui_controls::LEFT,
437 ui_controls::DOWN | ui_controls::UP,
438 new MessageLoop::QuitTask());
439 ui_test_utils::RunMessageLoop();
440 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView());
[email protected]9bd491ee2008-12-10 22:31:07441
442 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]8bcdec92009-02-25 16:15:18443 ui_controls::SendKeyPressNotifyWhenDone(VK_F, true, false, false,
444 new MessageLoop::QuitTask());
445 ui_test_utils::RunMessageLoop();
446
447 // See remark above on why we wait.
[email protected]9bd491ee2008-12-10 22:31:07448 ::Sleep(kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18449 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
450 ui_test_utils::RunMessageLoop();
451
452 focused_view = focus_manager->GetFocusedView();
453 ASSERT_TRUE(focused_view != NULL);
454 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
[email protected]9bd491ee2008-12-10 22:31:07455}
[email protected]401513c2009-03-12 00:21:28456
457// Makes sure the focus is in the right location when opening the different
458// types of tabs.
459IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
460 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
461 BrowserView* browser_view = BrowserView::GetBrowserViewForHWND(hwnd);
462 ASSERT_TRUE(browser_view);
463 views::FocusManager* focus_manager =
464 views::FocusManager::GetFocusManager(hwnd);
465 ASSERT_TRUE(focus_manager);
466
467 // Open the history tab, focus should be on the tab contents.
468 browser()->ShowHistoryTab();
469 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView());
470
471 // Open the new tab, focus should be on the location bar.
472 browser()->NewTab();
473 EXPECT_EQ(browser_view->GetLocationBarView(),
474 focus_manager->GetFocusedView());
475
476 // Open the download tab, focus should be on the tab contents.
477 browser()->ShowDownloadsTab();
478 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView());
479}