license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 5 | #include "base/message_loop.h" |
[email protected] | ece3c8b | 2009-03-27 16:55:39 | [diff] [blame^] | 6 | #include "base/ref_counted.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 7 | #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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "chrome/browser/view_ids.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 12 | #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] | 08682a9 | 2009-03-17 02:54:02 | [diff] [blame] | 19 | #include "chrome/views/focus/focus_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "chrome/views/view.h" |
[email protected] | 0e8588c1 | 2009-03-17 01:44:36 | [diff] [blame] | 21 | #include "chrome/views/window/window.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 22 | #include "chrome/test/in_process_browser_test.h" |
| 23 | #include "chrome/test/ui_test_utils.h" |
[email protected] | ece3c8b | 2009-03-27 16:55:39 | [diff] [blame^] | 24 | #include "net/base/host_resolver_unittest.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 28 | // The delay waited in some cases where we don't have a notifications for an |
| 29 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | const int kActionDelayMs = 500; |
| 31 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; |
| 33 | const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; |
| 34 | const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; |
| 35 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 36 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | public: |
| 38 | BrowserFocusTest() { |
[email protected] | ece3c8b | 2009-03-27 16:55:39 | [diff] [blame^] | 39 | 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 45 | set_show_window(true); |
| 46 | EnableDOMAutomation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | } |
[email protected] | ece3c8b | 2009-03-27 16:55:39 | [diff] [blame^] | 48 | |
| 49 | private: |
| 50 | scoped_refptr<net::RuleBasedHostMapper> host_mapper_; |
| 51 | net::ScopedHostMapper scoped_host_mapper_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | }; |
| 53 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 54 | class 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 67 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 76 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 82 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 88 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | |
| 103 | } // namespace |
| 104 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 105 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
| 106 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | |
| 108 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 109 | GURL url = server->TestServerPageW(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 110 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
| 112 | // The focus should be on the Tab contents. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 113 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 120 | EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | |
| 122 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 123 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | |
| 129 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 130 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | |
| 139 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 140 | ::ShowWindow(hwnd, SW_HIDE); |
| 141 | ::ShowWindow(hwnd, SW_SHOW); |
| 142 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | |
| 144 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 145 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 151 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 161 | browser()->window()->Activate(); |
| 162 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
| 163 | EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | |
| 165 | // Switch back to the second browser, focus should still be on the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 166 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Tabs remember focus. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 175 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { |
| 176 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | |
| 178 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 179 | GURL url = server->TestServerPageW(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 180 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | |
| 190 | // Create several tabs. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 191 | for (int i = 0; i < 4; ++i) |
| 192 | browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 203 | // Activate the tab. |
| 204 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
| 206 | // Activate the location bar or the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 207 | views::View* view_to_focus = kFocusPage[i][j] ? |
| 208 | browser_view->GetContentsView() : |
| 209 | browser_view->GetLocationBarView(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 211 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | } |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 221 | // Activate the tab. |
| 222 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | |
| 224 | // Activate the location bar or the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 225 | views::View* view = kFocusPage[i][j] ? |
| 226 | browser_view->GetContentsView() : |
| 227 | browser_view->GetLocationBarView(); |
| 228 | EXPECT_EQ(view, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Background window does not steal focus. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 234 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { |
| 235 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | |
| 237 | // First we navigate to our test page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 238 | GURL url = server->TestServerPageW(kSimplePage); |
| 239 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | |
| 241 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 242 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 243 | ASSERT_TRUE(browser2); |
| 244 | browser2->AddBlankTab(true); |
| 245 | browser2->window()->Show(); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 246 | GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 247 | ui_test_utils::NavigateToURL(browser2, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 248 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 249 | // Activate the first browser. |
| 250 | browser()->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | |
| 252 | // Wait for the focus to be stolen by the other browser. |
| 253 | ::Sleep(2000); |
| 254 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 255 | // 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] | 32670b0 | 2009-03-03 00:28:00 | [diff] [blame] | 259 | EXPECT_TRUE(browser_view->frame()->IsActive()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 260 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Page cannot steal focus when focus is on location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 268 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
| 269 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | |
| 271 | // Open the page that steals focus. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 272 | GURL url = server->TestServerPageW(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 273 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 275 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 280 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 281 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 287 | |
| 288 | // Wait for the page to steal focus. |
| 289 | ::Sleep(2000); |
| 290 | |
| 291 | // Make sure the location bar is still focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 292 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Focus traversal |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 296 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { |
| 297 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 298 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 299 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 300 | GURL url = server->TestServerPageW(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 301 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 302 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 303 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 307 | |
| 308 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 309 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 316 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | // Test forward focus traversal. |
| 324 | for (int i = 0; i < 3; ++i) { |
| 325 | // Location bar should be focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 326 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 331 | JavaScriptRunner js_runner( |
| 332 | browser()->GetSelectedTabContents()->AsWebContents(), |
| 333 | L"", |
| 334 | L"window.domAutomationController.send(getFocusedElement());"); |
| 335 | std::string actual = js_runner.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 336 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 337 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 338 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | ::Sleep(kActionDelayMs); |
| 345 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 346 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 352 | } |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 357 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 359 | // Now let's press shift-tab to move the focus in reverse. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | for (int j = 0; j < 7; ++j) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 361 | ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false, |
| 362 | new MessageLoop::QuitTask()); |
| 363 | ui_test_utils::RunMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | ::Sleep(kActionDelayMs); |
| 365 | |
| 366 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 367 | JavaScriptRunner js_runner( |
| 368 | browser()->GetSelectedTabContents()->AsWebContents(), |
| 369 | L"", |
| 370 | L"window.domAutomationController.send(getFocusedElement());"); |
| 371 | std::string actual = js_runner.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 373 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 374 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 383 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 384 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
| 385 | HTTPTestServer* server = StartHTTPServer(); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 386 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 387 | // Open some page (any page that doesn't steal focus). |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 388 | GURL url = server->TestServerPageW(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 389 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 390 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 391 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 396 | |
| 397 | // Press Ctrl+F, which will make the Find box open and request focus. |
| 398 | static const int VK_F = 0x46; |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 399 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 408 | ::Sleep(kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 409 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 415 | |
| 416 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 417 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 423 | // Make sure the location bar is focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 424 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 425 | |
| 426 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 427 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 433 | |
| 434 | // Set focus to the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 435 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 441 | |
| 442 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 443 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 448 | ::Sleep(kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 449 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 455 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 456 | |
| 457 | // Makes sure the focus is in the right location when opening the different |
| 458 | // types of tabs. |
| 459 | IN_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 | } |