[email protected] | b5464901 | 2009-04-17 17:00:12 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 9 | #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 10 | #include "chrome/browser/tab_contents/interstitial_page.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" |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 14 | #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 15 | #include "chrome/common/chrome_paths.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 16 | #include "chrome/test/in_process_browser_test.h" |
| 17 | #include "chrome/test/ui_test_utils.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 18 | #include "views/focus/focus_manager.h" |
| 19 | #include "views/view.h" |
| 20 | #include "views/window/window.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 24 | // The delay waited in some cases where we don't have a notifications for an |
| 25 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | const int kActionDelayMs = 500; |
| 27 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; |
| 29 | const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; |
| 30 | const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 31 | const wchar_t kTypicalPageName[] = L"typical_page.html"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 33 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | public: |
| 35 | BrowserFocusTest() { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 36 | set_show_window(true); |
| 37 | EnableDOMAutomation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | } |
| 39 | }; |
| 40 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 41 | class 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | } // namespace |
| 98 | |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 99 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 100 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | |
| 102 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 103 | GURL url = server->TestServerPageW(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 104 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | |
| 106 | // The focus should be on the Tab contents. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 107 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 108 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 109 | ASSERT_TRUE(browser_view); |
| 110 | views::FocusManager* focus_manager = |
| 111 | views::FocusManager::GetFocusManager(hwnd); |
| 112 | ASSERT_TRUE(focus_manager); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 114 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 115 | focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
| 117 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 118 | // 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] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 122 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 123 | focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | |
| 125 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 126 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | |
| 135 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 136 | ::ShowWindow(hwnd, SW_HIDE); |
| 137 | ::ShowWindow(hwnd, SW_SHOW); |
| 138 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | |
| 140 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 141 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 142 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 143 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 144 | browser2->window()->Show(); |
| 145 | ui_test_utils::NavigateToURL(browser2, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 147 | HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 148 | BrowserView* browser_view2 = |
| 149 | BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 150 | ASSERT_TRUE(browser_view2); |
| 151 | views::FocusManager* focus_manager2 = |
| 152 | views::FocusManager::GetFocusManager(hwnd2); |
| 153 | ASSERT_TRUE(focus_manager2); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 154 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 155 | focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 159 | browser()->window()->Activate(); |
| 160 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
| 161 | EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
| 163 | // Switch back to the second browser, focus should still be on the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 164 | browser2->window()->Activate(); |
| 165 | EXPECT_EQ(NULL, focus_manager->GetFocusedView()); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 166 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 167 | focus_manager2->GetFocusedView()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 168 | |
| 169 | // Close the 2nd browser to avoid a DCHECK(). |
| 170 | browser_view2->Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Tabs remember focus. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 174 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { |
| 175 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | |
| 177 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 178 | GURL url = server->TestServerPageW(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 179 | ui_test_utils::NavigateToURL(browser(), url); |
| 180 | |
| 181 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 182 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 183 | ASSERT_TRUE(browser_view); |
| 184 | |
| 185 | views::FocusManager* focus_manager = |
| 186 | views::FocusManager::GetFocusManager(hwnd); |
| 187 | ASSERT_TRUE(focus_manager); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | |
| 189 | // Create several tabs. |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 190 | for (int i = 0; i < 4; ++i) { |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 191 | browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, false, |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 192 | NULL); |
| 193 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 204 | // Activate the tab. |
| 205 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | |
| 207 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 208 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 215 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | } |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 225 | // Activate the tab. |
| 226 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | |
| 228 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 229 | views::View* view; |
| 230 | if (kFocusPage[i][j]) { |
| 231 | view = browser_view->GetTabContentsContainerView(); |
| 232 | } else { |
| 233 | view = browser_view->GetLocationBarView(); |
| 234 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 235 | EXPECT_EQ(view, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Background window does not steal focus. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 241 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { |
| 242 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | |
| 244 | // First we navigate to our test page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 245 | GURL url = server->TestServerPageW(kSimplePage); |
| 246 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | |
| 248 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 249 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 250 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 251 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 252 | browser2->window()->Show(); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 253 | GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 254 | ui_test_utils::NavigateToURL(browser2, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 255 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 256 | // Activate the first browser. |
| 257 | browser()->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | |
| 259 | // Wait for the focus to be stolen by the other browser. |
| 260 | ::Sleep(2000); |
| 261 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 262 | // Make sure the first browser is still active. |
| 263 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 264 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 265 | ASSERT_TRUE(browser_view); |
[email protected] | 6c8c80e | 2009-05-19 14:51:36 | [diff] [blame] | 266 | EXPECT_TRUE(browser_view->frame()->GetWindow()->IsActive()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 267 | |
| 268 | // Close the 2nd browser to avoid a DCHECK(). |
| 269 | HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 270 | BrowserView* browser_view2 = |
| 271 | BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 272 | browser_view2->Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Page cannot steal focus when focus is on location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 276 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
| 277 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | |
| 279 | // Open the page that steals focus. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 280 | GURL url = server->TestServerPageW(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 281 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 283 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 284 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 285 | views::FocusManager* focus_manager = |
| 286 | views::FocusManager::GetFocusManager(hwnd); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 287 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 288 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 289 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | |
| 296 | // Wait for the page to steal focus. |
| 297 | ::Sleep(2000); |
| 298 | |
| 299 | // Make sure the location bar is still focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 300 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 303 | // Focus traversal on a regular page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 304 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { |
| 305 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 307 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 308 | GURL url = server->TestServerPageW(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 309 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 311 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 312 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 313 | views::FocusManager* focus_manager = |
| 314 | views::FocusManager::GetFocusManager(hwnd); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | |
| 316 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 317 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 324 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | // Test forward focus traversal. |
| 332 | for (int i = 0; i < 3; ++i) { |
| 333 | // Location bar should be focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 334 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | |
| 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] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 339 | std::string actual; |
| 340 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 341 | browser()->GetSelectedTabContents(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 342 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 343 | L"window.domAutomationController.send(getFocusedElement());", |
| 344 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 346 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 347 | 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] | b5464901 | 2009-04-17 17:00:12 | [diff] [blame] | 351 | // processed ack notification from the renderer. I am reluctant to create |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 352 | // a new notification/callback for that purpose just for this test. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | ::Sleep(kActionDelayMs); |
| 354 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 355 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | } |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 366 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 368 | // Now let's press shift-tab to move the focus in reverse. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 369 | for (int j = 0; j < 7; ++j) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 370 | ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false, |
| 371 | new MessageLoop::QuitTask()); |
| 372 | ui_test_utils::RunMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | ::Sleep(kActionDelayMs); |
| 374 | |
| 375 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 376 | std::string actual; |
| 377 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 378 | browser()->GetSelectedTabContents(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 379 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 380 | L"window.domAutomationController.send(getFocusedElement());", |
| 381 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 382 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 383 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 384 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 393 | // Focus traversal while an interstitial is showing. |
| 394 | IN_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] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 402 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 403 | views::FocusManager* focus_manager = |
| 404 | views::FocusManager::GetFocusManager(hwnd); |
| 405 | |
| 406 | // Focus should be on the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 407 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 408 | focus_manager->GetFocusedView()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 409 | |
| 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. |
| 489 | IN_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] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 497 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 498 | views::FocusManager* focus_manager = |
| 499 | views::FocusManager::GetFocusManager(hwnd); |
| 500 | |
| 501 | // Page should have focus. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 502 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 503 | focus_manager->GetFocusedView()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 504 | EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 505 | HasFocus()); |
| 506 | |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 507 | // Let's show an interstitial.erstitial |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 508 | 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] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 519 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 520 | focus_manager->GetFocusedView()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 521 | 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] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 527 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 528 | focus_manager->GetFocusedView()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 529 | EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 530 | HasFocus()); |
| 531 | } |
| 532 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 533 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 534 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
| 535 | HTTPTestServer* server = StartHTTPServer(); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 536 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 537 | // Open some page (any page that doesn't steal focus). |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 538 | GURL url = server->TestServerPageW(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 539 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 540 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 541 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 542 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 543 | views::FocusManager* focus_manager = |
| 544 | views::FocusManager::GetFocusManager(hwnd); |
| 545 | LocationBarView* location_bar = browser_view->GetLocationBarView(); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 546 | |
| 547 | // Press Ctrl+F, which will make the Find box open and request focus. |
| 548 | static const int VK_F = 0x46; |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 549 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 558 | ::Sleep(kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 559 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 565 | |
| 566 | // Click on the location bar. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 567 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 573 | // Make sure the location bar is focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 574 | EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 575 | |
| 576 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 577 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 583 | |
| 584 | // Set focus to the page. |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 585 | ui_controls::MoveMouseToCenterAndPress( |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 586 | browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 587 | ui_controls::LEFT, |
| 588 | ui_controls::DOWN | ui_controls::UP, |
| 589 | new MessageLoop::QuitTask()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 590 | ui_test_utils::RunMessageLoop(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 591 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 592 | focus_manager->GetFocusedView()); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 593 | |
| 594 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 595 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 600 | ::Sleep(kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 601 | 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] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 607 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 608 | |
| 609 | // Makes sure the focus is in the right location when opening the different |
| 610 | // types of tabs. |
| 611 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
| 612 | HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 613 | BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 614 | 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] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 621 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 622 | focus_manager->GetFocusedView()); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 623 | |
| 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] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame^] | 631 | EXPECT_EQ(browser_view->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 632 | focus_manager->GetFocusedView()); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 633 | } |