[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] | d9fde8d | 2009-10-08 19:59:30 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 7 | #include "base/message_loop.h" |
[email protected] | ece3c8b | 2009-03-27 16:55:39 | [diff] [blame] | 8 | #include "base/ref_counted.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 9 | #include "chrome/browser/automation/ui_controls.h" |
| 10 | #include "chrome/browser/browser.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 11 | #include "chrome/browser/browser_window.h" |
| 12 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 13 | #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 14 | #include "chrome/browser/tab_contents/interstitial_page.h" |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 15 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "chrome/browser/view_ids.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 18 | #include "chrome/common/chrome_paths.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 19 | #include "chrome/test/in_process_browser_test.h" |
| 20 | #include "chrome/test/ui_test_utils.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 21 | #include "views/focus/focus_manager.h" |
| 22 | #include "views/view.h" |
| 23 | #include "views/window/window.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 25 | #if defined(TOOLKIT_VIEWS) |
| 26 | #include "chrome/browser/views/frame/browser_view.h" |
| 27 | #include "chrome/browser/views/location_bar_view.h" |
| 28 | #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 29 | #endif |
| 30 | |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 31 | #if defined(OS_LINUX) |
| 32 | #include "chrome/browser/gtk/view_id_util.h" |
| 33 | #endif |
| 34 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 35 | #if defined(OS_LINUX) |
| 36 | // For some reason we hit an external DNS lookup in this test in Linux but not |
| 37 | // on Windows. TODO(estade): investigate. |
| 38 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame^] | 39 | // TODO(jcampan): http://crbug.com/23683 |
| 40 | #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 41 | #else |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 42 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame^] | 43 | #define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 44 | #endif |
| 45 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | namespace { |
| 47 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 48 | // The delay waited in some cases where we don't have a notifications for an |
| 49 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | const int kActionDelayMs = 500; |
| 51 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; |
| 53 | const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; |
| 54 | const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 55 | const char kTypicalPageName[] = "typical_page.html"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 57 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | public: |
| 59 | BrowserFocusTest() { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 60 | set_show_window(true); |
| 61 | EnableDOMAutomation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 63 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 64 | bool IsViewFocused(ViewID vid) { |
| 65 | return ui_test_utils::IsViewFocused(browser(), vid); |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 68 | void ClickOnView(ViewID vid) { |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 69 | ui_test_utils::ClickOnView(browser(), vid); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 72 | static void HideNativeWindow(gfx::NativeWindow window) { |
| 73 | #if defined(OS_WIN) |
| 74 | // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
| 75 | // using Windows API. |
| 76 | ::ShowWindow(window, SW_HIDE); |
| 77 | #elif defined(OS_LINUX) |
| 78 | gtk_widget_hide(GTK_WIDGET(window)); |
| 79 | #else |
| 80 | NOTIMPLEMENTED(); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | static void ShowNativeWindow(gfx::NativeWindow window) { |
| 85 | #if defined(OS_WIN) |
| 86 | // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of |
| 87 | // using Windows API. |
| 88 | ::ShowWindow(window, SW_SHOW); |
| 89 | #elif defined(OS_LINUX) |
| 90 | gtk_widget_hide(GTK_WIDGET(window)); |
| 91 | #else |
| 92 | NOTIMPLEMENTED(); |
| 93 | #endif |
| 94 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | }; |
| 96 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 97 | class TestInterstitialPage : public InterstitialPage { |
| 98 | public: |
| 99 | TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) |
| 100 | : InterstitialPage(tab, new_navigation, url), |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 101 | waiting_for_dom_response_(false), |
| 102 | waiting_for_focus_change_(false) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 103 | FilePath file_path; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 104 | bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 105 | EXPECT_TRUE(r); |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 106 | file_path = file_path.AppendASCII("focus"); |
| 107 | file_path = file_path.AppendASCII(kTypicalPageName); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 108 | r = file_util::ReadFileToString(file_path, &html_contents_); |
| 109 | EXPECT_TRUE(r); |
| 110 | } |
| 111 | |
| 112 | virtual std::string GetHTMLContents() { |
| 113 | return html_contents_; |
| 114 | } |
| 115 | |
| 116 | virtual void DomOperationResponse(const std::string& json_string, |
| 117 | int automation_id) { |
| 118 | if (waiting_for_dom_response_) { |
| 119 | dom_response_ = json_string; |
| 120 | waiting_for_dom_response_ = false; |
| 121 | MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 122 | return; |
| 123 | } |
| 124 | InterstitialPage::DomOperationResponse(json_string, automation_id); |
| 125 | } |
| 126 | |
| 127 | std::string GetFocusedElement() { |
| 128 | std::wstring script = L"window.domAutomationController.setAutomationId(0);" |
| 129 | L"window.domAutomationController.send(getFocusedElement());"; |
| 130 | |
| 131 | render_view_host()->ExecuteJavascriptInWebFrame(L"", script); |
| 132 | DCHECK(!waiting_for_dom_response_); |
| 133 | waiting_for_dom_response_ = true; |
| 134 | ui_test_utils::RunMessageLoop(); |
| 135 | // Remove the JSON extra quotes. |
| 136 | if (dom_response_.size() >= 2 && dom_response_[0] == '"' && |
| 137 | dom_response_[dom_response_.size() - 1] == '"') { |
| 138 | dom_response_ = dom_response_.substr(1, dom_response_.size() - 2); |
| 139 | } |
| 140 | return dom_response_; |
| 141 | } |
| 142 | |
| 143 | bool HasFocus() { |
| 144 | return render_view_host()->view()->HasFocus(); |
| 145 | } |
| 146 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 147 | void WaitForFocusChange() { |
| 148 | waiting_for_focus_change_ = true; |
| 149 | ui_test_utils::RunMessageLoop(); |
| 150 | } |
| 151 | |
| 152 | protected: |
| 153 | virtual void FocusedNodeChanged() { |
| 154 | if (!waiting_for_focus_change_) |
| 155 | return; |
| 156 | |
| 157 | waiting_for_focus_change_= false; |
| 158 | MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 159 | } |
| 160 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 161 | private: |
| 162 | std::string html_contents_; |
| 163 | |
| 164 | bool waiting_for_dom_response_; |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 165 | bool waiting_for_focus_change_; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 166 | std::string dom_response_; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 167 | }; |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 168 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | } // namespace |
| 170 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 171 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 172 | #if defined(OS_LINUX) |
| 173 | // It seems we have to wait a little bit for the widgets to spin up before |
| 174 | // we can start clicking on them. |
| 175 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 176 | new MessageLoop::QuitTask(), |
| 177 | kActionDelayMs); |
| 178 | ui_test_utils::RunMessageLoop(); |
| 179 | #endif |
| 180 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 181 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 182 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 183 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 184 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 185 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 186 | ClickOnView(VIEW_ID_LOCATION_BAR); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 187 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 188 | } |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 189 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 190 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 191 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
| 193 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 194 | GURL url = server->TestServerPageW(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 195 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 197 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 198 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | // The focus should be on the Tab contents. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 200 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 201 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 202 | HideNativeWindow(window); |
| 203 | ShowNativeWindow(window); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 204 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 206 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 207 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 209 | HideNativeWindow(window); |
| 210 | ShowNativeWindow(window); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 211 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 213 | // The rest of this test does not make sense on Linux because the behavior |
| 214 | // of Activate() is not well defined and can vary by window manager. |
| 215 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 217 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 218 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 219 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 220 | browser2->window()->Show(); |
| 221 | ui_test_utils::NavigateToURL(browser2, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 223 | HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 224 | BrowserView* browser_view2 = |
| 225 | BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 226 | ASSERT_TRUE(browser_view2); |
| 227 | views::FocusManager* focus_manager2 = |
[email protected] | 82166b6 | 2009-06-30 18:48:00 | [diff] [blame] | 228 | views::FocusManager::GetFocusManagerForNativeView(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 229 | ASSERT_TRUE(focus_manager2); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 230 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 231 | focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | |
| 233 | // Switch to the 1st browser window, focus should still be on the location |
| 234 | // bar and the second browser should have nothing focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 235 | browser()->window()->Activate(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 236 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 237 | EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | |
| 239 | // Switch back to the second browser, focus should still be on the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 240 | browser2->window()->Activate(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 241 | EXPECT_EQ(NULL, |
| 242 | views::FocusManager::GetFocusManagerForNativeView( |
| 243 | browser()->window()->GetNativeHandle())->GetFocusedView()); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 244 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 245 | focus_manager2->GetFocusedView()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 246 | |
| 247 | // Close the 2nd browser to avoid a DCHECK(). |
| 248 | browser_view2->Close(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 249 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Tabs remember focus. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 253 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 254 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | |
| 256 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 257 | GURL url = server->TestServerPageW(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 258 | ui_test_utils::NavigateToURL(browser(), url); |
| 259 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | // Create several tabs. |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 261 | for (int i = 0; i < 4; ++i) { |
[email protected] | 82166b6 | 2009-06-30 18:48:00 | [diff] [blame] | 262 | browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, |
| 263 | false, NULL); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 264 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | |
| 266 | // Alternate focus for the tab. |
| 267 | const bool kFocusPage[3][5] = { |
| 268 | { true, true, true, true, false }, |
| 269 | { false, false, false, false, false }, |
| 270 | { false, true, false, true, false } |
| 271 | }; |
| 272 | |
| 273 | for (int i = 1; i < 3; i++) { |
| 274 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 275 | // Activate the tab. |
| 276 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 277 | |
| 278 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 279 | if (kFocusPage[i][j]) { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 280 | browser()->GetTabContentsAt(j)->view()->Focus(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 281 | } else { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 282 | browser()->FocusLocationBar(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 283 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Now come back to the tab and check the right view is focused. |
| 287 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 288 | // Activate the tab. |
| 289 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 291 | ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 292 | VIEW_ID_LOCATION_BAR; |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 293 | ASSERT_TRUE(IsViewFocused(vid)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | } |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame^] | 295 | |
| 296 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 297 | browser()->SelectTabContentsAt(0, true); |
| 298 | // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 299 | // this has regressed in the past. Loop through several times to be sure. |
| 300 | for (int j = 0; j < 15; j++) { |
| 301 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 302 | VIEW_ID_LOCATION_BAR; |
| 303 | ASSERT_TRUE(IsViewFocused(vid)); |
| 304 | |
| 305 | ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, |
| 306 | false, false, |
| 307 | new MessageLoop::QuitTask()); |
| 308 | ui_test_utils::RunMessageLoop(); |
| 309 | } |
| 310 | |
| 311 | // As above, but with ctrl+shift+tab. |
| 312 | browser()->SelectTabContentsAt(4, true); |
| 313 | for (int j = 14; j >= 0; --j) { |
| 314 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 315 | VIEW_ID_LOCATION_BAR; |
| 316 | ASSERT_TRUE(IsViewFocused(vid)); |
| 317 | |
| 318 | ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, |
| 319 | true, false, |
| 320 | new MessageLoop::QuitTask()); |
| 321 | ui_test_utils::RunMessageLoop(); |
| 322 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 326 | // Tabs remember focus with find-in-page box. |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame^] | 327 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 328 | HTTPTestServer* server = StartHTTPServer(); |
| 329 | |
| 330 | // First we navigate to our test page. |
| 331 | GURL url = server->TestServerPageW(kSimplePage); |
| 332 | ui_test_utils::NavigateToURL(browser(), url); |
| 333 | |
| 334 | browser()->Find(); |
| 335 | ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
| 336 | ASCIIToUTF16("a"), true, false, NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 337 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 338 | |
| 339 | // Focus the location bar. |
| 340 | browser()->FocusLocationBar(); |
| 341 | |
| 342 | // Create a 2nd tab. |
| 343 | browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, |
| 344 | false, NULL); |
| 345 | |
| 346 | // Focus should be on the recently opened tab page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 347 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 348 | |
| 349 | // Select 1st tab, focus should still be on the location-bar. |
| 350 | // (bug http://crbug.com/23296) |
| 351 | browser()->SelectTabContentsAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 352 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 353 | |
| 354 | // Now open the find box again, switch to another tab and come back, the focus |
| 355 | // should return to the find box. |
| 356 | browser()->Find(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 357 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 358 | browser()->SelectTabContentsAt(1, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 359 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 360 | browser()->SelectTabContentsAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 361 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 362 | } |
| 363 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | // Background window does not steal focus. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 365 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 366 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | |
| 368 | // First we navigate to our test page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 369 | GURL url = server->TestServerPageW(kSimplePage); |
| 370 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | |
| 372 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 373 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 374 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 375 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 376 | browser2->window()->Show(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 377 | |
[email protected] | ed179ee | 2009-10-03 21:02:51 | [diff] [blame] | 378 | Browser* focused_browser = NULL; |
| 379 | Browser* unfocused_browser = NULL; |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 380 | #if defined(OS_LINUX) |
| 381 | // On Linux, calling Activate() is not guaranteed to move focus, so we have |
| 382 | // to figure out which browser does have focus. |
| 383 | if (browser2->window()->IsActive()) { |
| 384 | focused_browser = browser2; |
| 385 | unfocused_browser = browser(); |
| 386 | } else if (browser()->window()->IsActive()) { |
| 387 | focused_browser = browser(); |
| 388 | unfocused_browser = browser2; |
| 389 | } else { |
| 390 | ASSERT_TRUE(false); |
| 391 | } |
| 392 | #elif defined(OS_WIN) |
| 393 | focused_browser = browser(); |
| 394 | unfocused_browser = browser2; |
| 395 | #endif |
| 396 | |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 397 | GURL steal_focus_url = server->TestServerPageW(kStealFocusPage); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 398 | ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 399 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 400 | // Activate the first browser. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 401 | focused_browser->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | |
| 403 | // Wait for the focus to be stolen by the other browser. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 404 | PlatformThread::Sleep(2000); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 406 | // Make sure the first browser is still active. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 407 | EXPECT_TRUE(focused_browser->window()->IsActive()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 408 | |
| 409 | // Close the 2nd browser to avoid a DCHECK(). |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 410 | browser2->window()->Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // Page cannot steal focus when focus is on location bar. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 414 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 415 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | |
| 417 | // Open the page that steals focus. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 418 | GURL url = server->TestServerPageW(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 419 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 421 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 422 | |
| 423 | // Wait for the page to steal focus. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 424 | PlatformThread::Sleep(2000); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 425 | |
| 426 | // Make sure the location bar is still focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 427 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | } |
| 429 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 430 | // Focus traversal on a regular page. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 431 | // Note that this test relies on a notification from the renderer that the |
| 432 | // focus has changed in the page. The notification in the renderer may change |
| 433 | // at which point this test would fail (see comment in |
| 434 | // RenderWidget::didFocus()). |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 435 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 436 | HTTPTestServer* server = StartHTTPServer(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 438 | // First we navigate to our test page. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 439 | GURL url = server->TestServerPageW(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 440 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 442 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 444 | const char* kExpElementIDs[] = { |
| 445 | "", // Initially no element in the page should be focused |
| 446 | // (the location bar is focused). |
| 447 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 448 | "gmapLink" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | }; |
| 450 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 451 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 452 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 453 | // Test forward focus traversal. |
| 454 | for (int i = 0; i < 3; ++i) { |
| 455 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 456 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | |
| 458 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 459 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | // 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] | 461 | std::string actual; |
| 462 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 463 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 464 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 465 | L"window.domAutomationController.send(getFocusedElement());", |
| 466 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 468 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 469 | ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 470 | false, false, false)); |
| 471 | |
| 472 | if (j < arraysize(kExpElementIDs) - 1) { |
| 473 | ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> |
| 474 | render_view_host()); |
| 475 | } else { |
| 476 | // On the last tab key press, the focus returns to the browser. |
| 477 | ui_test_utils::WaitForFocusInBrowser(browser()); |
| 478 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 480 | |
| 481 | // At this point the renderer has sent us a message asking to advance the |
| 482 | // focus (as the end of the focus loop was reached in the renderer). |
| 483 | // We need to run the message loop to process it. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 484 | MessageLoop::current()->RunAllPending(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Now let's try reverse focus traversal. |
| 488 | for (int i = 0; i < 3; ++i) { |
| 489 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 490 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 492 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 493 | for (size_t j = 0; j < 7; ++j) { |
| 494 | ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 495 | false, true, false)); |
| 496 | |
| 497 | if (j < arraysize(kExpElementIDs) - 1) { |
| 498 | ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> |
| 499 | render_view_host()); |
| 500 | } else { |
| 501 | // On the last tab key press, the focus returns to the browser. |
| 502 | ui_test_utils::WaitForFocusInBrowser(browser()); |
| 503 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | |
| 505 | // 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] | 506 | std::string actual; |
| 507 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 508 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 509 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 510 | L"window.domAutomationController.send(getFocusedElement());", |
| 511 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 513 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 514 | |
| 515 | // At this point the renderer has sent us a message asking to advance the |
| 516 | // focus (as the end of the focus loop was reached in the renderer). |
| 517 | // We need to run the message loop to process it. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 518 | MessageLoop::current()->RunAllPending(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 522 | // Focus traversal while an interstitial is showing. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 523 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 524 | HTTPTestServer* server = StartHTTPServer(); |
| 525 | |
| 526 | // First we navigate to our test page. |
| 527 | GURL url = server->TestServerPageW(kSimplePage); |
| 528 | ui_test_utils::NavigateToURL(browser(), url); |
| 529 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 530 | // Focus should be on the page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 531 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 532 | |
| 533 | // Let's show an interstitial. |
| 534 | TestInterstitialPage* interstitial_page = |
| 535 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 536 | true, GURL("http://interstitial.com")); |
| 537 | interstitial_page->Show(); |
| 538 | // Give some time for the interstitial to show. |
| 539 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 540 | new MessageLoop::QuitTask(), |
| 541 | 1000); |
| 542 | ui_test_utils::RunMessageLoop(); |
| 543 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 544 | browser()->FocusLocationBar(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 545 | |
| 546 | const char* kExpElementIDs[] = { |
| 547 | "", // Initially no element in the page should be focused |
| 548 | // (the location bar is focused). |
| 549 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 550 | "gmapLink" |
| 551 | }; |
| 552 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 553 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 554 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 555 | // Test forward focus traversal. |
| 556 | for (int i = 0; i < 2; ++i) { |
| 557 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 558 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 559 | |
| 560 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 561 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 562 | // Let's make sure the focus is on the expected element in the page. |
| 563 | std::string actual = interstitial_page->GetFocusedElement(); |
| 564 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 565 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 566 | ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 567 | false, false, false)); |
| 568 | |
| 569 | if (j < arraysize(kExpElementIDs) - 1) { |
| 570 | interstitial_page->WaitForFocusChange(); |
| 571 | } else { |
| 572 | // On the last tab key press, the focus returns to the browser. |
| 573 | ui_test_utils::WaitForFocusInBrowser(browser()); |
| 574 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // At this point the renderer has sent us a message asking to advance the |
| 578 | // focus (as the end of the focus loop was reached in the renderer). |
| 579 | // We need to run the message loop to process it. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 580 | MessageLoop::current()->RunAllPending(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // Now let's try reverse focus traversal. |
| 584 | for (int i = 0; i < 2; ++i) { |
| 585 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 586 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 587 | |
| 588 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 589 | for (size_t j = 0; j < 7; ++j) { |
| 590 | ASSERT_TRUE(ui_controls::SendKeyPress(window, base::VKEY_TAB, |
| 591 | false, true, false)); |
| 592 | |
| 593 | if (j < arraysize(kExpElementIDs) - 1) { |
| 594 | interstitial_page->WaitForFocusChange(); |
| 595 | } else { |
| 596 | // On the last tab key press, the focus returns to the browser. |
| 597 | ui_test_utils::WaitForFocusInBrowser(browser()); |
| 598 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 599 | |
| 600 | // Let's make sure the focus is on the expected element in the page. |
| 601 | std::string actual = interstitial_page->GetFocusedElement(); |
| 602 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 603 | } |
| 604 | |
| 605 | // At this point the renderer has sent us a message asking to advance the |
| 606 | // focus (as the end of the focus loop was reached in the renderer). |
| 607 | // We need to run the message loop to process it. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 608 | MessageLoop::current()->RunAllPending(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | // Focus stays on page with interstitials. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 613 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 614 | HTTPTestServer* server = StartHTTPServer(); |
| 615 | |
| 616 | // First we navigate to our test page. |
| 617 | GURL url = server->TestServerPageW(kSimplePage); |
| 618 | ui_test_utils::NavigateToURL(browser(), url); |
| 619 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 620 | // Page should have focus. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 621 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 622 | EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 623 | HasFocus()); |
| 624 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 625 | // Let's show an interstitial. |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 626 | TestInterstitialPage* interstitial_page = |
| 627 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 628 | true, GURL("http://interstitial.com")); |
| 629 | interstitial_page->Show(); |
| 630 | // Give some time for the interstitial to show. |
| 631 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 632 | new MessageLoop::QuitTask(), |
| 633 | 1000); |
| 634 | ui_test_utils::RunMessageLoop(); |
| 635 | |
| 636 | // The interstitial should have focus now. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 637 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 638 | EXPECT_TRUE(interstitial_page->HasFocus()); |
| 639 | |
| 640 | // Hide the interstitial. |
| 641 | interstitial_page->DontProceed(); |
| 642 | |
| 643 | // Focus should be back on the original page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 644 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 645 | } |
| 646 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 647 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 648 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 649 | HTTPTestServer* server = StartHTTPServer(); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 650 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 651 | // Open some page (any page that doesn't steal focus). |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 652 | GURL url = server->TestServerPageW(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 653 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 654 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 655 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 656 | |
| 657 | // Press Ctrl+F, which will make the Find box open and request focus. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 658 | ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 659 | false, false, |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 660 | new MessageLoop::QuitTask()); |
| 661 | ui_test_utils::RunMessageLoop(); |
| 662 | |
| 663 | // Ideally, we wouldn't sleep here and instead would intercept the |
| 664 | // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 665 | // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 666 | // - creating a factory used to create the delegate |
| 667 | // - making the test a private and overwriting the delegate member directly. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 668 | MessageLoop::current()->PostDelayedTask( |
| 669 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 670 | ui_test_utils::RunMessageLoop(); |
| 671 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 672 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 673 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 674 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 675 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 676 | |
| 677 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 678 | ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 679 | false, false, |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 680 | new MessageLoop::QuitTask()); |
| 681 | ui_test_utils::RunMessageLoop(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 682 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 683 | |
| 684 | // Set focus to the page. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 685 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 686 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 687 | |
| 688 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 689 | ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, false, |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 690 | false, new MessageLoop::QuitTask()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 691 | ui_test_utils::RunMessageLoop(); |
| 692 | |
| 693 | // See remark above on why we wait. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 694 | MessageLoop::current()->PostDelayedTask( |
| 695 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 696 | ui_test_utils::RunMessageLoop(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 697 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 698 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 699 | |
| 700 | // Makes sure the focus is in the right location when opening the different |
| 701 | // types of tabs. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 702 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 703 | // Open the history tab, focus should be on the tab contents. |
| 704 | browser()->ShowHistoryTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 705 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 706 | |
| 707 | // Open the new tab, focus should be on the location bar. |
| 708 | browser()->NewTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 709 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 710 | |
| 711 | // Open the download tab, focus should be on the tab contents. |
| 712 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 713 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 3e3f0eb | 2009-06-22 18:33:43 | [diff] [blame] | 714 | |
| 715 | // Open about:blank, focus should be on the location bar. |
| 716 | browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
| 717 | true, -1, false, NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 718 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 719 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 720 | |
| 721 | // Tests that focus goes where expected when using reload. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 722 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 723 | HTTPTestServer* server = StartHTTPServer(); |
| 724 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 725 | // Open the new tab, reload. |
| 726 | browser()->NewTab(); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 727 | browser()->Reload(); |
| 728 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 729 | // Focus should stay on the location bar. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 730 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 731 | |
| 732 | // Open a regular page, focus the location bar, reload. |
| 733 | ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 734 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 735 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 736 | browser()->Reload(); |
| 737 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 738 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 739 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 740 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // Tests that focus goes where expected when using reload on a crashed tab. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 744 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReloadCrashedTab) { |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 745 | HTTPTestServer* server = StartHTTPServer(); |
| 746 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 747 | // Open a regular page, crash, reload. |
| 748 | ui_test_utils::NavigateToURL(browser(), server->TestServerPageW(kSimplePage)); |
| 749 | ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 750 | browser()->Reload(); |
| 751 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 752 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 753 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 754 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 755 | } |