[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] | 34877b3 | 2010-07-31 17:47:05 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 15 | #include "chrome/browser/automation/ui_controls.h" |
| 16 | #include "chrome/browser/browser.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 17 | #include "chrome/browser/browser_window.h" |
| 18 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 19 | #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 20 | #include "chrome/browser/tab_contents/interstitial_page.h" |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 21 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 22 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 23 | #include "chrome/browser/tabs/tab_strip_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "chrome/browser/view_ids.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 25 | #include "chrome/common/chrome_paths.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 26 | #include "chrome/test/in_process_browser_test.h" |
| 27 | #include "chrome/test/ui_test_utils.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 28 | #include "net/test/test_server.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 29 | |
| 30 | #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 31 | #include "views/focus/focus_manager.h" |
| 32 | #include "views/view.h" |
| 33 | #include "views/window/window.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 34 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 36 | #if defined(TOOLKIT_VIEWS) |
| 37 | #include "chrome/browser/views/frame/browser_view.h" |
[email protected] | 265ccd9 | 2010-04-29 17:57:17 | [diff] [blame] | 38 | #include "chrome/browser/views/location_bar/location_bar_view.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 39 | #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 40 | #endif |
| 41 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 42 | #if defined(TOOLKIT_USES_GTK) |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 43 | #include "chrome/browser/gtk/view_id_util.h" |
| 44 | #endif |
| 45 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 46 | #if defined(OS_LINUX) |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 47 | #define MAYBE_FocusTraversal FocusTraversal |
[email protected] | 252250d | 2010-09-27 18:36:18 | [diff] [blame^] | 48 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 49 | // TODO(jcampan): http://crbug.com/23683 |
[email protected] | bbe383d | 2010-07-13 21:49:59 | [diff] [blame] | 50 | #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 51 | #elif defined(OS_MACOSX) |
| 52 | // TODO(suzhe): http://crbug.com/49738 (following two tests) |
| 53 | #define MAYBE_FocusTraversal FAILS_FocusTraversal |
| 54 | #define MAYBE_FocusTraversalOnInterstitial FAILS_FocusTraversalOnInterstitial |
| 55 | // TODO(suzhe): http://crbug.com/49737 |
| 56 | #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
| 57 | #elif defined(OS_WIN) |
| 58 | #define MAYBE_FocusTraversal FocusTraversal |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 59 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 60 | #define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 61 | #endif |
| 62 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | namespace { |
| 64 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 65 | // The delay waited in some cases where we don't have a notifications for an |
| 66 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | const int kActionDelayMs = 500; |
| 68 | |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 69 | const char kSimplePage[] = "files/focus/page_with_focus.html"; |
| 70 | const char kStealFocusPage[] = "files/focus/page_steals_focus.html"; |
| 71 | const char kTypicalPage[] = "files/focus/typical_page.html"; |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 72 | const char kTypicalPageName[] = "typical_page.html"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 74 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | public: |
| 76 | BrowserFocusTest() { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 77 | set_show_window(true); |
| 78 | EnableDOMAutomation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 80 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 81 | bool IsViewFocused(ViewID vid) { |
| 82 | return ui_test_utils::IsViewFocused(browser(), vid); |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 85 | void ClickOnView(ViewID vid) { |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 86 | ui_test_utils::ClickOnView(browser(), vid); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 89 | void BringBrowserWindowToFront() { |
| 90 | ui_test_utils::ShowAndFocusNativeWindow( |
| 91 | browser()->window()->GetNativeHandle()); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 92 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | }; |
| 94 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 95 | class TestInterstitialPage : public InterstitialPage { |
| 96 | public: |
| 97 | TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) |
| 98 | : InterstitialPage(tab, new_navigation, url), |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 99 | waiting_for_dom_response_(false), |
| 100 | waiting_for_focus_change_(false) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 101 | FilePath file_path; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 102 | bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 103 | EXPECT_TRUE(r); |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 104 | file_path = file_path.AppendASCII("focus"); |
| 105 | file_path = file_path.AppendASCII(kTypicalPageName); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 106 | r = file_util::ReadFileToString(file_path, &html_contents_); |
| 107 | EXPECT_TRUE(r); |
| 108 | } |
| 109 | |
| 110 | virtual std::string GetHTMLContents() { |
| 111 | return html_contents_; |
| 112 | } |
| 113 | |
| 114 | virtual void DomOperationResponse(const std::string& json_string, |
| 115 | int automation_id) { |
| 116 | if (waiting_for_dom_response_) { |
| 117 | dom_response_ = json_string; |
| 118 | waiting_for_dom_response_ = false; |
| 119 | MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 120 | return; |
| 121 | } |
| 122 | InterstitialPage::DomOperationResponse(json_string, automation_id); |
| 123 | } |
| 124 | |
| 125 | std::string GetFocusedElement() { |
| 126 | std::wstring script = L"window.domAutomationController.setAutomationId(0);" |
| 127 | L"window.domAutomationController.send(getFocusedElement());"; |
| 128 | |
| 129 | render_view_host()->ExecuteJavascriptInWebFrame(L"", script); |
| 130 | DCHECK(!waiting_for_dom_response_); |
| 131 | waiting_for_dom_response_ = true; |
| 132 | ui_test_utils::RunMessageLoop(); |
| 133 | // Remove the JSON extra quotes. |
| 134 | if (dom_response_.size() >= 2 && dom_response_[0] == '"' && |
| 135 | dom_response_[dom_response_.size() - 1] == '"') { |
| 136 | dom_response_ = dom_response_.substr(1, dom_response_.size() - 2); |
| 137 | } |
| 138 | return dom_response_; |
| 139 | } |
| 140 | |
| 141 | bool HasFocus() { |
| 142 | return render_view_host()->view()->HasFocus(); |
| 143 | } |
| 144 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 145 | void WaitForFocusChange() { |
| 146 | waiting_for_focus_change_ = true; |
| 147 | ui_test_utils::RunMessageLoop(); |
| 148 | } |
| 149 | |
| 150 | protected: |
| 151 | virtual void FocusedNodeChanged() { |
| 152 | if (!waiting_for_focus_change_) |
| 153 | return; |
| 154 | |
| 155 | waiting_for_focus_change_= false; |
| 156 | MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 157 | } |
| 158 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 159 | private: |
| 160 | std::string html_contents_; |
| 161 | |
| 162 | bool waiting_for_dom_response_; |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 163 | bool waiting_for_focus_change_; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 164 | std::string dom_response_; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 165 | }; |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 166 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 167 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 168 | BringBrowserWindowToFront(); |
| 169 | #if defined(USE_X11) || defined(OS_MACOSX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 170 | // It seems we have to wait a little bit for the widgets to spin up before |
| 171 | // we can start clicking on them. |
| 172 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 173 | new MessageLoop::QuitTask(), |
| 174 | kActionDelayMs); |
| 175 | ui_test_utils::RunMessageLoop(); |
| 176 | #endif |
| 177 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 178 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 179 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 180 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 181 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[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_LOCATION_BAR); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 184 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 185 | } |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 186 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 187 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 188 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 189 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | |
| 191 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 192 | GURL url = test_server()->GetURL(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] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 253 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | |
| 255 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 256 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 257 | ui_test_utils::NavigateToURL(browser(), url); |
| 258 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | // Create several tabs. |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 260 | for (int i = 0; i < 4; ++i) { |
[email protected] | b283a753 | 2010-08-12 21:24:59 | [diff] [blame] | 261 | Browser* browser_used = NULL; |
[email protected] | 715af7e | 2010-04-29 01:55:38 | [diff] [blame] | 262 | browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1, |
[email protected] | b283a753 | 2010-08-12 21:24:59 | [diff] [blame] | 263 | TabStripModel::ADD_SELECTED, NULL, std::string(), |
| 264 | &browser_used); |
| 265 | EXPECT_EQ(browser(), browser_used); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 266 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | |
| 268 | // Alternate focus for the tab. |
| 269 | const bool kFocusPage[3][5] = { |
| 270 | { true, true, true, true, false }, |
| 271 | { false, false, false, false, false }, |
| 272 | { false, true, false, true, false } |
| 273 | }; |
| 274 | |
| 275 | for (int i = 1; i < 3; i++) { |
| 276 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 277 | // Activate the tab. |
| 278 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
| 280 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 281 | if (kFocusPage[i][j]) { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 282 | browser()->GetTabContentsAt(j)->view()->Focus(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 283 | } else { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 284 | browser()->FocusLocationBar(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 285 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // Now come back to the tab and check the right view is focused. |
| 289 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 290 | // Activate the tab. |
| 291 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 293 | ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 294 | VIEW_ID_LOCATION_BAR; |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 295 | ASSERT_TRUE(IsViewFocused(vid)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | } |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 297 | |
| 298 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 299 | browser()->SelectTabContentsAt(0, true); |
| 300 | // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 301 | // this has regressed in the past. Loop through several times to be sure. |
| 302 | for (int j = 0; j < 15; j++) { |
| 303 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 304 | VIEW_ID_LOCATION_BAR; |
| 305 | ASSERT_TRUE(IsViewFocused(vid)); |
| 306 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 307 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 308 | window, app::VKEY_TAB, true, false, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 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 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 318 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 319 | window, app::VKEY_TAB, true, true, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 320 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 324 | // Tabs remember focus with find-in-page box. |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 325 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 326 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 327 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 328 | |
| 329 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 330 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 331 | ui_test_utils::NavigateToURL(browser(), url); |
| 332 | |
| 333 | browser()->Find(); |
| 334 | ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
| 335 | ASCIIToUTF16("a"), true, false, NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 336 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 337 | |
| 338 | // Focus the location bar. |
| 339 | browser()->FocusLocationBar(); |
| 340 | |
| 341 | // Create a 2nd tab. |
[email protected] | b283a753 | 2010-08-12 21:24:59 | [diff] [blame] | 342 | Browser* browser_used = NULL; |
[email protected] | 4a166544 | 2010-06-28 16:09:39 | [diff] [blame] | 343 | browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, -1, |
[email protected] | b283a753 | 2010-08-12 21:24:59 | [diff] [blame] | 344 | TabStripModel::ADD_SELECTED, NULL, std::string(), |
| 345 | &browser_used); |
| 346 | EXPECT_EQ(browser(), browser_used); |
[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] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 369 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 370 | |
| 371 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 372 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 373 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | |
| 375 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 376 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 377 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 378 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 379 | browser2->window()->Show(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 380 | |
[email protected] | ed179ee | 2009-10-03 21:02:51 | [diff] [blame] | 381 | Browser* focused_browser = NULL; |
| 382 | Browser* unfocused_browser = NULL; |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 383 | #if defined(USE_X11) |
| 384 | // On X11, calling Activate() is not guaranteed to move focus, so we have |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 385 | // to figure out which browser does have focus. |
| 386 | if (browser2->window()->IsActive()) { |
| 387 | focused_browser = browser2; |
| 388 | unfocused_browser = browser(); |
| 389 | } else if (browser()->window()->IsActive()) { |
| 390 | focused_browser = browser(); |
| 391 | unfocused_browser = browser2; |
| 392 | } else { |
| 393 | ASSERT_TRUE(false); |
| 394 | } |
| 395 | #elif defined(OS_WIN) |
| 396 | focused_browser = browser(); |
| 397 | unfocused_browser = browser2; |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 398 | #elif defined(OS_MACOSX) |
| 399 | // On Mac, the newly created window always gets the focus. |
| 400 | focused_browser = browser2; |
| 401 | unfocused_browser = browser(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 402 | #endif |
| 403 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 404 | GURL steal_focus_url = test_server()->GetURL(kStealFocusPage); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 405 | ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 406 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 407 | // Activate the first browser. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 408 | focused_browser->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | |
| 410 | // Wait for the focus to be stolen by the other browser. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 411 | MessageLoop::current()->PostDelayedTask( |
| 412 | FROM_HERE, new MessageLoop::QuitTask(), 2000); |
| 413 | ui_test_utils::RunMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 414 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 415 | // Make sure the first browser is still active. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 416 | EXPECT_TRUE(focused_browser->window()->IsActive()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 417 | |
| 418 | // Close the 2nd browser to avoid a DCHECK(). |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 419 | browser2->window()->Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // Page cannot steal focus when focus is on location bar. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 423 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 424 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 425 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | |
| 427 | // Open the page that steals focus. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 428 | GURL url = test_server()->GetURL(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 429 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 431 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 432 | |
| 433 | // Wait for the page to steal focus. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 434 | PlatformThread::Sleep(2000); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 435 | |
| 436 | // Make sure the location bar is still focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 437 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 440 | // Focus traversal on a regular page. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 441 | // Note that this test relies on a notification from the renderer that the |
| 442 | // focus has changed in the page. The notification in the renderer may change |
| 443 | // at which point this test would fail (see comment in |
| 444 | // RenderWidget::didFocus()). |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 445 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
| 446 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 447 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 449 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 450 | GURL url = test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 451 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 453 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 455 | const char* kExpElementIDs[] = { |
| 456 | "", // Initially no element in the page should be focused |
| 457 | // (the location bar is focused). |
| 458 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 459 | "gmapLink" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | }; |
| 461 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 462 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 463 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 464 | // Test forward focus traversal. |
| 465 | for (int i = 0; i < 3; ++i) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 466 | SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 468 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | |
| 470 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 471 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 472 | SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 473 | // 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] | 474 | std::string actual; |
| 475 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 476 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 477 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 478 | L"window.domAutomationController.send(getFocusedElement());", |
| 479 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 480 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 481 | |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 482 | ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, |
[email protected] | 1b5a48c | 2010-04-29 23:08:30 | [diff] [blame] | 483 | false, false, false, false)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 484 | |
| 485 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 486 | ASSERT_NO_FATAL_FAILURE( |
| 487 | ui_test_utils::WaitForFocusChange( |
| 488 | browser()->GetSelectedTabContents()->render_view_host())); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 489 | } else { |
| 490 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 491 | ASSERT_NO_FATAL_FAILURE( |
| 492 | ui_test_utils::WaitForFocusInBrowser(browser())); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 493 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 495 | |
| 496 | // At this point the renderer has sent us a message asking to advance the |
| 497 | // focus (as the end of the focus loop was reached in the renderer). |
| 498 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 499 | ui_test_utils::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // Now let's try reverse focus traversal. |
| 503 | for (int i = 0; i < 3; ++i) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 504 | SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 506 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 508 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 509 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 510 | SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 511 | ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, |
[email protected] | 1b5a48c | 2010-04-29 23:08:30 | [diff] [blame] | 512 | false, true, false, false)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 513 | |
| 514 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 515 | ASSERT_NO_FATAL_FAILURE( |
| 516 | ui_test_utils::WaitForFocusChange( |
| 517 | browser()->GetSelectedTabContents()->render_view_host())); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 518 | } else { |
| 519 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 520 | ASSERT_NO_FATAL_FAILURE( |
| 521 | ui_test_utils::WaitForFocusInBrowser(browser())); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 522 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 523 | |
| 524 | // 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] | 525 | std::string actual; |
| 526 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 527 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 528 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 529 | L"window.domAutomationController.send(getFocusedElement());", |
| 530 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 532 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 533 | |
| 534 | // At this point the renderer has sent us a message asking to advance the |
| 535 | // focus (as the end of the focus loop was reached in the renderer). |
| 536 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 537 | ui_test_utils::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 541 | // Focus traversal while an interstitial is showing. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 542 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 543 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 544 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 545 | |
| 546 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 547 | GURL url = test_server()->GetURL(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] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 586 | ASSERT_TRUE(ui_controls::SendKeyPress(window, app::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) { |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 590 | ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 591 | } else { |
| 592 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 593 | ASSERT_NO_FATAL_FAILURE( |
| 594 | ui_test_utils::WaitForFocusInBrowser(browser())); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 595 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // At this point the renderer has sent us a message asking to advance the |
| 599 | // focus (as the end of the focus loop was reached in the renderer). |
| 600 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 601 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // Now let's try reverse focus traversal. |
| 605 | for (int i = 0; i < 2; ++i) { |
| 606 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 607 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 608 | |
| 609 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 610 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 611 | ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, |
[email protected] | 1b5a48c | 2010-04-29 23:08:30 | [diff] [blame] | 612 | false, true, false, false)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 613 | |
| 614 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 615 | ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 616 | } else { |
| 617 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 618 | ASSERT_NO_FATAL_FAILURE( |
| 619 | ui_test_utils::WaitForFocusInBrowser(browser())); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 620 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 621 | |
| 622 | // Let's make sure the focus is on the expected element in the page. |
| 623 | std::string actual = interstitial_page->GetFocusedElement(); |
| 624 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 625 | } |
| 626 | |
| 627 | // At this point the renderer has sent us a message asking to advance the |
| 628 | // focus (as the end of the focus loop was reached in the renderer). |
| 629 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 630 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | // Focus stays on page with interstitials. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 635 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 636 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 637 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 638 | |
| 639 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 640 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 641 | ui_test_utils::NavigateToURL(browser(), url); |
| 642 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 643 | // Page should have focus. |
[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 | EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 646 | HasFocus()); |
| 647 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 648 | // Let's show an interstitial. |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 649 | TestInterstitialPage* interstitial_page = |
| 650 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 651 | true, GURL("http://interstitial.com")); |
| 652 | interstitial_page->Show(); |
| 653 | // Give some time for the interstitial to show. |
| 654 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 655 | new MessageLoop::QuitTask(), |
| 656 | 1000); |
| 657 | ui_test_utils::RunMessageLoop(); |
| 658 | |
| 659 | // The interstitial should have focus now. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 660 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 661 | EXPECT_TRUE(interstitial_page->HasFocus()); |
| 662 | |
| 663 | // Hide the interstitial. |
| 664 | interstitial_page->DontProceed(); |
| 665 | |
| 666 | // Focus should be back on the original page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 667 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 668 | } |
| 669 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 670 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 671 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 672 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 673 | ASSERT_TRUE(test_server()->Start()); |
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] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 676 | GURL url = test_server()->GetURL(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. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 683 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 684 | window, app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 685 | #else |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 686 | // Press Ctrl+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 687 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 688 | window, app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 689 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 690 | |
| 691 | // Ideally, we wouldn't sleep here and instead would intercept the |
| 692 | // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 693 | // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 694 | // - creating a factory used to create the delegate |
| 695 | // - making the test a private and overwriting the delegate member directly. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 696 | MessageLoop::current()->PostDelayedTask( |
| 697 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 698 | ui_test_utils::RunMessageLoop(); |
| 699 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 700 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 701 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 702 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 703 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 704 | |
| 705 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 706 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 707 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 708 | window, app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 709 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 710 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 711 | window, app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 712 | #endif |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 713 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 714 | |
| 715 | // Set focus to the page. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 716 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 717 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 718 | |
| 719 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 720 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 721 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 722 | window, app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 723 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 724 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | 7f0555248 | 2010-09-03 00:38:27 | [diff] [blame] | 725 | window, app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 726 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 727 | |
| 728 | // See remark above on why we wait. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 729 | MessageLoop::current()->PostDelayedTask( |
| 730 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 731 | ui_test_utils::RunMessageLoop(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 732 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 733 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 734 | |
| 735 | // Makes sure the focus is in the right location when opening the different |
| 736 | // types of tabs. |
[email protected] | 30667d4 | 2010-07-30 17:59:16 | [diff] [blame] | 737 | // Flaky, http://crbug.com/50763. |
| 738 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 739 | BringBrowserWindowToFront(); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 740 | |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 741 | // Open the history tab, focus should be on the tab contents. |
| 742 | browser()->ShowHistoryTab(); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 743 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 744 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 745 | |
| 746 | // Open the new tab, focus should be on the location bar. |
| 747 | browser()->NewTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 748 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 749 | |
| 750 | // Open the download tab, focus should be on the tab contents. |
| 751 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 752 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 3e3f0eb | 2009-06-22 18:33:43 | [diff] [blame] | 753 | |
| 754 | // Open about:blank, focus should be on the location bar. |
| 755 | browser()->AddTabWithURL(GURL("about:blank"), GURL(), PageTransition::LINK, |
[email protected] | 4a166544 | 2010-06-28 16:09:39 | [diff] [blame] | 756 | -1, TabStripModel::ADD_SELECTED, NULL, |
[email protected] | b283a753 | 2010-08-12 21:24:59 | [diff] [blame] | 757 | std::string(), |
| 758 | NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 759 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 760 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 761 | |
| 762 | // Tests that focus goes where expected when using reload. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 763 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 764 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 765 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 766 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 767 | // Open the new tab, reload. |
| 768 | browser()->NewTab(); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 769 | ui_test_utils::RunAllPendingInMessageLoop(); |
| 770 | |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 771 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 772 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 773 | // Focus should stay on the location bar. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 774 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 775 | |
| 776 | // Open a regular page, focus the location bar, reload. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 777 | ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 778 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 779 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 780 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 781 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 782 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 783 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 784 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 785 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 786 | } |
| 787 | |
[email protected] | 0e7c82ed | 2010-08-31 20:39:19 | [diff] [blame] | 788 | #if (defined(OS_CHROMEOS) || defined(OS_LINUX)) && !defined(NDEBUG) |
| 789 | // Hangy, http://crbug.com/50025. |
[email protected] | c38a190 | 2010-07-23 20:55:15 | [diff] [blame] | 790 | #define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab |
[email protected] | 517d2fe7 | 2010-07-23 15:40:41 | [diff] [blame] | 791 | #else |
| 792 | #define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab |
| 793 | #endif |
| 794 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 795 | // Tests that focus goes where expected when using reload on a crashed tab. |
[email protected] | 517d2fe7 | 2010-07-23 15:40:41 | [diff] [blame] | 796 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) { |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 797 | BringBrowserWindowToFront(); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 798 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 799 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 800 | // Open a regular page, crash, reload. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 801 | ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 802 | ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 803 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 804 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 805 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 806 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 807 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 808 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 809 | } |
[email protected] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 810 | |
| 811 | } // namespace |