blob: 3681c1b1ef98496e6fa80f7b8aba25157e1c1f48 [file] [log] [blame]
[email protected]b54649012009-04-17 17:00:121// Copyright (c) 2009 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]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"
[email protected]9e0c83a2009-05-06 19:44:379#include "chrome/browser/renderer_host/render_widget_host_view.h"
10#include "chrome/browser/tab_contents/interstitial_page.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"
[email protected]610d36a2009-05-22 23:00:3814#include "chrome/browser/views/tab_contents/tab_contents_container.h"
[email protected]9e0c83a2009-05-06 19:44:3715#include "chrome/common/chrome_paths.h"
[email protected]8bcdec92009-02-25 16:15:1816#include "chrome/test/in_process_browser_test.h"
17#include "chrome/test/ui_test_utils.h"
[email protected]2362e4f2009-05-08 00:34:0518#include "views/focus/focus_manager.h"
19#include "views/view.h"
20#include "views/window/window.h"
initial.commit09911bf2008-07-26 23:55:2921
22namespace {
23
[email protected]8bcdec92009-02-25 16:15:1824// The delay waited in some cases where we don't have a notifications for an
25// action we take.
initial.commit09911bf2008-07-26 23:55:2926const int kActionDelayMs = 500;
27
initial.commit09911bf2008-07-26 23:55:2928const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html";
29const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html";
30const wchar_t kTypicalPage[] = L"files/focus/typical_page.html";
[email protected]9e0c83a2009-05-06 19:44:3731const wchar_t kTypicalPageName[] = L"typical_page.html";
initial.commit09911bf2008-07-26 23:55:2932
[email protected]8bcdec92009-02-25 16:15:1833class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2934 public:
35 BrowserFocusTest() {
[email protected]8bcdec92009-02-25 16:15:1836 set_show_window(true);
37 EnableDOMAutomation();
initial.commit09911bf2008-07-26 23:55:2938 }
39};
40
[email protected]9e0c83a2009-05-06 19:44:3741class TestInterstitialPage : public InterstitialPage {
42 public:
43 TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
44 : InterstitialPage(tab, new_navigation, url),
45 waiting_for_dom_response_(false) {
46 std::wstring file_path;
47 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
48 EXPECT_TRUE(r);
49 file_util::AppendToPath(&file_path, L"focus");
50 file_util::AppendToPath(&file_path, kTypicalPageName);
51 r = file_util::ReadFileToString(file_path, &html_contents_);
52 EXPECT_TRUE(r);
53 }
54
55 virtual std::string GetHTMLContents() {
56 return html_contents_;
57 }
58
59 virtual void DomOperationResponse(const std::string& json_string,
60 int automation_id) {
61 if (waiting_for_dom_response_) {
62 dom_response_ = json_string;
63 waiting_for_dom_response_ = false;
64 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
65 return;
66 }
67 InterstitialPage::DomOperationResponse(json_string, automation_id);
68 }
69
70 std::string GetFocusedElement() {
71 std::wstring script = L"window.domAutomationController.setAutomationId(0);"
72 L"window.domAutomationController.send(getFocusedElement());";
73
74 render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
75 DCHECK(!waiting_for_dom_response_);
76 waiting_for_dom_response_ = true;
77 ui_test_utils::RunMessageLoop();
78 // Remove the JSON extra quotes.
79 if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
80 dom_response_[dom_response_.size() - 1] == '"') {
81 dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
82 }
83 return dom_response_;
84 }
85
86 bool HasFocus() {
87 return render_view_host()->view()->HasFocus();
88 }
89
90 private:
91 std::string html_contents_;
92
93 bool waiting_for_dom_response_;
94 std::string dom_response_;
95
96};
initial.commit09911bf2008-07-26 23:55:2997} // namespace
98
[email protected]7e383692009-06-12 19:14:5499IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
[email protected]8bcdec92009-02-25 16:15:18100 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29101
102 // First we navigate to our test page.
[email protected]dd265012009-01-08 20:45:27103 GURL url = server->TestServerPageW(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18104 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29105
106 // The focus should be on the Tab contents.
[email protected]8bcdec92009-02-25 16:15:18107 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00108 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]8bcdec92009-02-25 16:15:18109 ASSERT_TRUE(browser_view);
110 views::FocusManager* focus_manager =
111 views::FocusManager::GetFocusManager(hwnd);
112 ASSERT_TRUE(focus_manager);
initial.commit09911bf2008-07-26 23:55:29113
[email protected]7e383692009-06-12 19:14:54114 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38115 focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29116
117 // Now hide the window, show it again, the focus should not have changed.
[email protected]8bcdec92009-02-25 16:15:18118 // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of
119 // using Windows API.
120 ::ShowWindow(hwnd, SW_HIDE);
121 ::ShowWindow(hwnd, SW_SHOW);
[email protected]7e383692009-06-12 19:14:54122 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38123 focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29124
125 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18126 LocationBarView* location_bar = browser_view->GetLocationBarView();
127 ui_controls::MoveMouseToCenterAndPress(location_bar,
128 ui_controls::LEFT,
129 ui_controls::DOWN | ui_controls::UP,
130 new MessageLoop::QuitTask());
131 ui_test_utils::RunMessageLoop();
132 // Location bar should have focus.
133 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29134
135 // Hide the window, show it again, the focus should not have changed.
[email protected]8bcdec92009-02-25 16:15:18136 ::ShowWindow(hwnd, SW_HIDE);
137 ::ShowWindow(hwnd, SW_SHOW);
138 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29139
140 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18141 Browser* browser2 = Browser::Create(browser()->profile());
142 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43143 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18144 browser2->window()->Show();
145 ui_test_utils::NavigateToURL(browser2, url);
initial.commit09911bf2008-07-26 23:55:29146
[email protected]8bcdec92009-02-25 16:15:18147 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00148 BrowserView* browser_view2 =
149 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18150 ASSERT_TRUE(browser_view2);
151 views::FocusManager* focus_manager2 =
152 views::FocusManager::GetFocusManager(hwnd2);
153 ASSERT_TRUE(focus_manager2);
[email protected]7e383692009-06-12 19:14:54154 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38155 focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29156
157 // Switch to the 1st browser window, focus should still be on the location
158 // bar and the second browser should have nothing focused.
[email protected]8bcdec92009-02-25 16:15:18159 browser()->window()->Activate();
160 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
161 EXPECT_EQ(NULL, focus_manager2->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29162
163 // Switch back to the second browser, focus should still be on the page.
[email protected]8bcdec92009-02-25 16:15:18164 browser2->window()->Activate();
165 EXPECT_EQ(NULL, focus_manager->GetFocusedView());
[email protected]7e383692009-06-12 19:14:54166 EXPECT_EQ(browser_view2->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38167 focus_manager2->GetFocusedView());
[email protected]8bcdec92009-02-25 16:15:18168
169 // Close the 2nd browser to avoid a DCHECK().
170 browser_view2->Close();
initial.commit09911bf2008-07-26 23:55:29171}
172
173// Tabs remember focus.
[email protected]8bcdec92009-02-25 16:15:18174IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
175 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29176
177 // First we navigate to our test page.
[email protected]dd265012009-01-08 20:45:27178 GURL url = server->TestServerPageW(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18179 ui_test_utils::NavigateToURL(browser(), url);
180
181 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00182 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]8bcdec92009-02-25 16:15:18183 ASSERT_TRUE(browser_view);
184
185 views::FocusManager* focus_manager =
186 views::FocusManager::GetFocusManager(hwnd);
187 ASSERT_TRUE(focus_manager);
initial.commit09911bf2008-07-26 23:55:29188
189 // Create several tabs.
[email protected]22735af62009-04-07 21:09:58190 for (int i = 0; i < 4; ++i) {
[email protected]5a4940be2009-05-06 06:44:39191 browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, false,
[email protected]22735af62009-04-07 21:09:58192 NULL);
193 }
initial.commit09911bf2008-07-26 23:55:29194
195 // Alternate focus for the tab.
196 const bool kFocusPage[3][5] = {
197 { true, true, true, true, false },
198 { false, false, false, false, false },
199 { false, true, false, true, false }
200 };
201
202 for (int i = 1; i < 3; i++) {
203 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18204 // Activate the tab.
205 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29206
207 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54208 views::View* view_to_focus;
209 if (kFocusPage[i][j]) {
210 view_to_focus = browser_view->GetTabContentsContainerView();
211 } else {
212 view_to_focus = browser_view->GetLocationBarView();
213 }
initial.commit09911bf2008-07-26 23:55:29214
[email protected]8bcdec92009-02-25 16:15:18215 ui_controls::MoveMouseToCenterAndPress(view_to_focus,
216 ui_controls::LEFT,
217 ui_controls::DOWN |
218 ui_controls::UP,
219 new MessageLoop::QuitTask());
220 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29221 }
222
223 // Now come back to the tab and check the right view is focused.
224 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18225 // Activate the tab.
226 browser()->SelectTabContentsAt(j, true);
initial.commit09911bf2008-07-26 23:55:29227
228 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54229 views::View* view;
230 if (kFocusPage[i][j]) {
231 view = browser_view->GetTabContentsContainerView();
232 } else {
233 view = browser_view->GetLocationBarView();
234 }
[email protected]8bcdec92009-02-25 16:15:18235 EXPECT_EQ(view, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29236 }
237 }
238}
239
240// Background window does not steal focus.
[email protected]8bcdec92009-02-25 16:15:18241IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
242 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29243
244 // First we navigate to our test page.
[email protected]8bcdec92009-02-25 16:15:18245 GURL url = server->TestServerPageW(kSimplePage);
246 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29247
248 // Open a new browser window.
[email protected]8bcdec92009-02-25 16:15:18249 Browser* browser2 = Browser::Create(browser()->profile());
250 ASSERT_TRUE(browser2);
[email protected]e0c7c262009-04-23 23:09:43251 browser2->tabstrip_model()->delegate()->AddBlankTab(true);
[email protected]8bcdec92009-02-25 16:15:18252 browser2->window()->Show();
[email protected]1e187af2009-02-25 02:02:46253 GURL steal_focus_url = server->TestServerPageW(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18254 ui_test_utils::NavigateToURL(browser2, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46255
[email protected]8bcdec92009-02-25 16:15:18256 // Activate the first browser.
257 browser()->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29258
259 // Wait for the focus to be stolen by the other browser.
260 ::Sleep(2000);
261
[email protected]8bcdec92009-02-25 16:15:18262 // Make sure the first browser is still active.
263 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00264 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]8bcdec92009-02-25 16:15:18265 ASSERT_TRUE(browser_view);
[email protected]6c8c80e2009-05-19 14:51:36266 EXPECT_TRUE(browser_view->frame()->GetWindow()->IsActive());
[email protected]8bcdec92009-02-25 16:15:18267
268 // Close the 2nd browser to avoid a DCHECK().
269 HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00270 BrowserView* browser_view2 =
271 BrowserView::GetBrowserViewForNativeWindow(hwnd2);
[email protected]8bcdec92009-02-25 16:15:18272 browser_view2->Close();
initial.commit09911bf2008-07-26 23:55:29273}
274
275// Page cannot steal focus when focus is on location bar.
[email protected]8bcdec92009-02-25 16:15:18276IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
277 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29278
279 // Open the page that steals focus.
[email protected]dd265012009-01-08 20:45:27280 GURL url = server->TestServerPageW(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18281 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29282
[email protected]8bcdec92009-02-25 16:15:18283 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00284 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]8bcdec92009-02-25 16:15:18285 views::FocusManager* focus_manager =
286 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:29287
[email protected]9bd491ee2008-12-10 22:31:07288 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18289 LocationBarView* location_bar = browser_view->GetLocationBarView();
290 ui_controls::MoveMouseToCenterAndPress(location_bar,
291 ui_controls::LEFT,
292 ui_controls::DOWN | ui_controls::UP,
293 new MessageLoop::QuitTask());
294 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29295
296 // Wait for the page to steal focus.
297 ::Sleep(2000);
298
299 // Make sure the location bar is still focused.
[email protected]8bcdec92009-02-25 16:15:18300 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29301}
302
[email protected]9e0c83a2009-05-06 19:44:37303// Focus traversal on a regular page.
[email protected]8bcdec92009-02-25 16:15:18304IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
305 HTTPTestServer* server = StartHTTPServer();
initial.commit09911bf2008-07-26 23:55:29306
[email protected]8bcdec92009-02-25 16:15:18307 // First we navigate to our test page.
[email protected]dd265012009-01-08 20:45:27308 GURL url = server->TestServerPageW(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18309 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29310
[email protected]8bcdec92009-02-25 16:15:18311 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00312 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]8bcdec92009-02-25 16:15:18313 views::FocusManager* focus_manager =
314 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:29315
316 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18317 LocationBarView* location_bar = browser_view->GetLocationBarView();
318 ui_controls::MoveMouseToCenterAndPress(location_bar,
319 ui_controls::LEFT,
320 ui_controls::DOWN | ui_controls::UP,
321 new MessageLoop::QuitTask());
322 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29323
[email protected]8bcdec92009-02-25 16:15:18324 const char* kExpElementIDs[] = {
325 "", // Initially no element in the page should be focused
326 // (the location bar is focused).
327 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
328 "gmapLink"
initial.commit09911bf2008-07-26 23:55:29329 };
330
331 // Test forward focus traversal.
332 for (int i = 0; i < 3; ++i) {
333 // Location bar should be focused.
[email protected]8bcdec92009-02-25 16:15:18334 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29335
336 // Now let's press tab to move the focus.
337 for (int j = 0; j < 7; ++j) {
338 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01339 std::string actual;
340 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]57c6a652009-05-04 07:58:34341 browser()->GetSelectedTabContents(),
[email protected]8bcdec92009-02-25 16:15:18342 L"",
[email protected]45671612009-04-29 22:24:01343 L"window.domAutomationController.send(getFocusedElement());",
344 &actual));
initial.commit09911bf2008-07-26 23:55:29345 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
346
[email protected]8bcdec92009-02-25 16:15:18347 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false,
348 new MessageLoop::QuitTask());
349 ui_test_utils::RunMessageLoop();
350 // Ideally, we wouldn't sleep here and instead would use the event
[email protected]b54649012009-04-17 17:00:12351 // processed ack notification from the renderer. I am reluctant to create
[email protected]8bcdec92009-02-25 16:15:18352 // a new notification/callback for that purpose just for this test.
initial.commit09911bf2008-07-26 23:55:29353 ::Sleep(kActionDelayMs);
354 }
[email protected]8bcdec92009-02-25 16:15:18355
356 // At this point the renderer has sent us a message asking to advance the
357 // focus (as the end of the focus loop was reached in the renderer).
358 // We need to run the message loop to process it.
359 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
360 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29361 }
362
363 // Now let's try reverse focus traversal.
364 for (int i = 0; i < 3; ++i) {
365 // Location bar should be focused.
[email protected]8bcdec92009-02-25 16:15:18366 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
initial.commit09911bf2008-07-26 23:55:29367
[email protected]8bcdec92009-02-25 16:15:18368 // Now let's press shift-tab to move the focus in reverse.
initial.commit09911bf2008-07-26 23:55:29369 for (int j = 0; j < 7; ++j) {
[email protected]8bcdec92009-02-25 16:15:18370 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false,
371 new MessageLoop::QuitTask());
372 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29373 ::Sleep(kActionDelayMs);
374
375 // Let's make sure the focus is on the expected element in the page.
[email protected]45671612009-04-29 22:24:01376 std::string actual;
377 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
[email protected]57c6a652009-05-04 07:58:34378 browser()->GetSelectedTabContents(),
[email protected]8bcdec92009-02-25 16:15:18379 L"",
[email protected]45671612009-04-29 22:24:01380 L"window.domAutomationController.send(getFocusedElement());",
381 &actual));
initial.commit09911bf2008-07-26 23:55:29382 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
383 }
[email protected]8bcdec92009-02-25 16:15:18384
385 // At this point the renderer has sent us a message asking to advance the
386 // focus (as the end of the focus loop was reached in the renderer).
387 // We need to run the message loop to process it.
388 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
389 ui_test_utils::RunMessageLoop();
initial.commit09911bf2008-07-26 23:55:29390 }
391}
392
[email protected]9e0c83a2009-05-06 19:44:37393// Focus traversal while an interstitial is showing.
394IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) {
395 HTTPTestServer* server = StartHTTPServer();
396
397 // First we navigate to our test page.
398 GURL url = server->TestServerPageW(kSimplePage);
399 ui_test_utils::NavigateToURL(browser(), url);
400
401 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00402 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]9e0c83a2009-05-06 19:44:37403 views::FocusManager* focus_manager =
404 views::FocusManager::GetFocusManager(hwnd);
405
406 // Focus should be on the page.
[email protected]7e383692009-06-12 19:14:54407 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38408 focus_manager->GetFocusedView());
[email protected]9e0c83a2009-05-06 19:44:37409
410 // Let's show an interstitial.
411 TestInterstitialPage* interstitial_page =
412 new TestInterstitialPage(browser()->GetSelectedTabContents(),
413 true, GURL("http://interstitial.com"));
414 interstitial_page->Show();
415 // Give some time for the interstitial to show.
416 MessageLoop::current()->PostDelayedTask(FROM_HERE,
417 new MessageLoop::QuitTask(),
418 1000);
419 ui_test_utils::RunMessageLoop();
420
421 // Click on the location bar.
422 LocationBarView* location_bar = browser_view->GetLocationBarView();
423 ui_controls::MoveMouseToCenterAndPress(location_bar,
424 ui_controls::LEFT,
425 ui_controls::DOWN | ui_controls::UP,
426 new MessageLoop::QuitTask());
427 ui_test_utils::RunMessageLoop();
428
429 const char* kExpElementIDs[] = {
430 "", // Initially no element in the page should be focused
431 // (the location bar is focused).
432 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
433 "gmapLink"
434 };
435
436 // Test forward focus traversal.
437 for (int i = 0; i < 2; ++i) {
438 // Location bar should be focused.
439 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
440
441 // Now let's press tab to move the focus.
442 for (int j = 0; j < 7; ++j) {
443 // Let's make sure the focus is on the expected element in the page.
444 std::string actual = interstitial_page->GetFocusedElement();
445 ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
446
447 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false,
448 new MessageLoop::QuitTask());
449 ui_test_utils::RunMessageLoop();
450 // Ideally, we wouldn't sleep here and instead would use the event
451 // processed ack notification from the renderer. I am reluctant to create
452 // a new notification/callback for that purpose just for this test.
453 ::Sleep(kActionDelayMs);
454 }
455
456 // At this point the renderer has sent us a message asking to advance the
457 // focus (as the end of the focus loop was reached in the renderer).
458 // We need to run the message loop to process it.
459 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
460 ui_test_utils::RunMessageLoop();
461 }
462
463 // Now let's try reverse focus traversal.
464 for (int i = 0; i < 2; ++i) {
465 // Location bar should be focused.
466 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
467
468 // Now let's press shift-tab to move the focus in reverse.
469 for (int j = 0; j < 7; ++j) {
470 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false,
471 new MessageLoop::QuitTask());
472 ui_test_utils::RunMessageLoop();
473 ::Sleep(kActionDelayMs);
474
475 // Let's make sure the focus is on the expected element in the page.
476 std::string actual = interstitial_page->GetFocusedElement();
477 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
478 }
479
480 // At this point the renderer has sent us a message asking to advance the
481 // focus (as the end of the focus loop was reached in the renderer).
482 // We need to run the message loop to process it.
483 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
484 ui_test_utils::RunMessageLoop();
485 }
486}
487
488// Focus stays on page with interstitials.
489IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
490 HTTPTestServer* server = StartHTTPServer();
491
492 // First we navigate to our test page.
493 GURL url = server->TestServerPageW(kSimplePage);
494 ui_test_utils::NavigateToURL(browser(), url);
495
496 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00497 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]9e0c83a2009-05-06 19:44:37498 views::FocusManager* focus_manager =
499 views::FocusManager::GetFocusManager(hwnd);
500
501 // Page should have focus.
[email protected]7e383692009-06-12 19:14:54502 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38503 focus_manager->GetFocusedView());
[email protected]9e0c83a2009-05-06 19:44:37504 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
505 HasFocus());
506
[email protected]7e383692009-06-12 19:14:54507 // Let's show an interstitial.erstitial
[email protected]9e0c83a2009-05-06 19:44:37508 TestInterstitialPage* interstitial_page =
509 new TestInterstitialPage(browser()->GetSelectedTabContents(),
510 true, GURL("http://interstitial.com"));
511 interstitial_page->Show();
512 // Give some time for the interstitial to show.
513 MessageLoop::current()->PostDelayedTask(FROM_HERE,
514 new MessageLoop::QuitTask(),
515 1000);
516 ui_test_utils::RunMessageLoop();
517
518 // The interstitial should have focus now.
[email protected]7e383692009-06-12 19:14:54519 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38520 focus_manager->GetFocusedView());
[email protected]9e0c83a2009-05-06 19:44:37521 EXPECT_TRUE(interstitial_page->HasFocus());
522
523 // Hide the interstitial.
524 interstitial_page->DontProceed();
525
526 // Focus should be back on the original page.
[email protected]7e383692009-06-12 19:14:54527 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38528 focus_manager->GetFocusedView());
[email protected]9e0c83a2009-05-06 19:44:37529 EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
530 HasFocus());
531}
532
[email protected]9bd491ee2008-12-10 22:31:07533// Make sure Find box can request focus, even when it is already open.
[email protected]8bcdec92009-02-25 16:15:18534IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
535 HTTPTestServer* server = StartHTTPServer();
license.botbf09a502008-08-24 00:55:55536
[email protected]9bd491ee2008-12-10 22:31:07537 // Open some page (any page that doesn't steal focus).
[email protected]dd265012009-01-08 20:45:27538 GURL url = server->TestServerPageW(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18539 ui_test_utils::NavigateToURL(browser(), url);
[email protected]9bd491ee2008-12-10 22:31:07540
[email protected]8bcdec92009-02-25 16:15:18541 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00542 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]8bcdec92009-02-25 16:15:18543 views::FocusManager* focus_manager =
544 views::FocusManager::GetFocusManager(hwnd);
545 LocationBarView* location_bar = browser_view->GetLocationBarView();
[email protected]9bd491ee2008-12-10 22:31:07546
547 // Press Ctrl+F, which will make the Find box open and request focus.
548 static const int VK_F = 0x46;
[email protected]8bcdec92009-02-25 16:15:18549 ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false,
550 new MessageLoop::QuitTask());
551 ui_test_utils::RunMessageLoop();
552
553 // Ideally, we wouldn't sleep here and instead would intercept the
554 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we
555 // could create a RenderViewHostDelegate wrapper and hook-it up by either:
556 // - creating a factory used to create the delegate
557 // - making the test a private and overwriting the delegate member directly.
[email protected]9bd491ee2008-12-10 22:31:07558 ::Sleep(kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18559 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
560 ui_test_utils::RunMessageLoop();
561
562 views::View* focused_view = focus_manager->GetFocusedView();
563 ASSERT_TRUE(focused_view != NULL);
564 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
[email protected]9bd491ee2008-12-10 22:31:07565
566 // Click on the location bar.
[email protected]8bcdec92009-02-25 16:15:18567 ui_controls::MoveMouseToCenterAndPress(location_bar,
568 ui_controls::LEFT,
569 ui_controls::DOWN | ui_controls::UP,
570 new MessageLoop::QuitTask());
571 ui_test_utils::RunMessageLoop();
572
[email protected]9bd491ee2008-12-10 22:31:07573 // Make sure the location bar is focused.
[email protected]8bcdec92009-02-25 16:15:18574 EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
[email protected]9bd491ee2008-12-10 22:31:07575
576 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]8bcdec92009-02-25 16:15:18577 ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false,
578 new MessageLoop::QuitTask());
579 ui_test_utils::RunMessageLoop();
580 focused_view = focus_manager->GetFocusedView();
581 ASSERT_TRUE(focused_view != NULL);
582 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
[email protected]9bd491ee2008-12-10 22:31:07583
584 // Set focus to the page.
[email protected]610d36a2009-05-22 23:00:38585 ui_controls::MoveMouseToCenterAndPress(
[email protected]7e383692009-06-12 19:14:54586 browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38587 ui_controls::LEFT,
588 ui_controls::DOWN | ui_controls::UP,
589 new MessageLoop::QuitTask());
[email protected]8bcdec92009-02-25 16:15:18590 ui_test_utils::RunMessageLoop();
[email protected]7e383692009-06-12 19:14:54591 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38592 focus_manager->GetFocusedView());
[email protected]9bd491ee2008-12-10 22:31:07593
594 // Now press Ctrl+F again and focus should move to the Find box.
[email protected]8bcdec92009-02-25 16:15:18595 ui_controls::SendKeyPressNotifyWhenDone(VK_F, true, false, false,
596 new MessageLoop::QuitTask());
597 ui_test_utils::RunMessageLoop();
598
599 // See remark above on why we wait.
[email protected]9bd491ee2008-12-10 22:31:07600 ::Sleep(kActionDelayMs);
[email protected]8bcdec92009-02-25 16:15:18601 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
602 ui_test_utils::RunMessageLoop();
603
604 focused_view = focus_manager->GetFocusedView();
605 ASSERT_TRUE(focused_view != NULL);
606 EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
[email protected]9bd491ee2008-12-10 22:31:07607}
[email protected]401513c2009-03-12 00:21:28608
609// Makes sure the focus is in the right location when opening the different
610// types of tabs.
611IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
612 HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
[email protected]4a507a62009-05-28 00:10:00613 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
[email protected]401513c2009-03-12 00:21:28614 ASSERT_TRUE(browser_view);
615 views::FocusManager* focus_manager =
616 views::FocusManager::GetFocusManager(hwnd);
617 ASSERT_TRUE(focus_manager);
618
619 // Open the history tab, focus should be on the tab contents.
620 browser()->ShowHistoryTab();
[email protected]7e383692009-06-12 19:14:54621 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38622 focus_manager->GetFocusedView());
[email protected]401513c2009-03-12 00:21:28623
624 // Open the new tab, focus should be on the location bar.
625 browser()->NewTab();
626 EXPECT_EQ(browser_view->GetLocationBarView(),
627 focus_manager->GetFocusedView());
628
629 // Open the download tab, focus should be on the tab contents.
630 browser()->ShowDownloadsTab();
[email protected]7e383692009-06-12 19:14:54631 EXPECT_EQ(browser_view->GetTabContentsContainerView(),
[email protected]610d36a2009-05-22 23:00:38632 focus_manager->GetFocusedView());
[email protected]401513c2009-03-12 00:21:28633}