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