[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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 11 | #include "base/string_number_conversions.h" |
[email protected] | 34877b3 | 2010-07-31 17:47:05 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 14 | #include "chrome/browser/browser_window.h" |
| 15 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 16 | #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 17 | #include "chrome/browser/tab_contents/interstitial_page.h" |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 18 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 19 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 20 | #include "chrome/browser/tabs/tab_strip_model.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 21 | #include "chrome/browser/ui/browser.h" |
[email protected] | 6a3ec231 | 2010-12-02 19:30:19 | [diff] [blame] | 22 | #include "chrome/browser/ui/view_ids.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 23 | #include "chrome/common/chrome_paths.h" |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 25 | #include "chrome/test/in_process_browser_test.h" |
| 26 | #include "chrome/test/ui_test_utils.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 27 | #include "net/test/test_server.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 28 | |
| 29 | #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 30 | #include "views/focus/focus_manager.h" |
| 31 | #include "views/view.h" |
| 32 | #include "views/window/window.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 33 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 35 | #if defined(TOOLKIT_VIEWS) |
| 36 | #include "chrome/browser/views/frame/browser_view.h" |
[email protected] | 265ccd9 | 2010-04-29 17:57:17 | [diff] [blame] | 37 | #include "chrome/browser/views/location_bar/location_bar_view.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 38 | #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 39 | #endif |
| 40 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 41 | #if defined(TOOLKIT_USES_GTK) |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 42 | #include "chrome/browser/gtk/view_id_util.h" |
| 43 | #endif |
| 44 | |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 45 | #if defined(OS_WIN) |
| 46 | #include <windows.h> |
| 47 | #include <Psapi.h> |
| 48 | #endif |
| 49 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 50 | #if defined(OS_LINUX) |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 51 | #define MAYBE_FocusTraversal FocusTraversal |
[email protected] | 252250d | 2010-09-27 18:36:18 | [diff] [blame] | 52 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 53 | // TODO(jcampan): http://crbug.com/23683 |
[email protected] | bbe383d | 2010-07-13 21:49:59 | [diff] [blame] | 54 | #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 55 | #elif defined(OS_MACOSX) |
[email protected] | 4301222 | 2010-10-29 19:11:15 | [diff] [blame] | 56 | // TODO(suzhe): http://crbug.com/60973 (following two tests) |
[email protected] | a5eb2d67 | 2010-10-22 07:24:48 | [diff] [blame] | 57 | #define MAYBE_FocusTraversal DISABLED_FocusTraversal |
| 58 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 59 | // TODO(suzhe): http://crbug.com/49737 |
| 60 | #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
| 61 | #elif defined(OS_WIN) |
[email protected] | 2615373 | 2010-11-09 18:47:39 | [diff] [blame] | 62 | // Disabled, http://crbug.com/62543. |
| 63 | #define MAYBE_FocusTraversal DISABLED_FocusTraversal |
| 64 | // Disabled, http://crbug.com/62544. |
| 65 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 66 | // Flaky, http://crbug.com/62537. |
| 67 | #define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 68 | #endif |
| 69 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | namespace { |
| 71 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 72 | // The delay waited in some cases where we don't have a notifications for an |
| 73 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | const int kActionDelayMs = 500; |
| 75 | |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 76 | const char kSimplePage[] = "files/focus/page_with_focus.html"; |
| 77 | const char kStealFocusPage[] = "files/focus/page_steals_focus.html"; |
| 78 | const char kTypicalPage[] = "files/focus/typical_page.html"; |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 79 | const char kTypicalPageName[] = "typical_page.html"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 81 | // Test to make sure Chrome is in the foreground as we start testing. This is |
| 82 | // required for tests that synthesize input to the Chrome window. |
| 83 | bool ChromeInForeground() { |
| 84 | #if defined(OS_WIN) |
| 85 | HWND window = ::GetForegroundWindow(); |
| 86 | std::wstring caption; |
| 87 | std::wstring filename; |
| 88 | int len = ::GetWindowTextLength(window) + 1; |
| 89 | ::GetWindowText(window, WriteInto(&caption, len), len); |
| 90 | bool chrome_window_in_foreground = |
| 91 | EndsWith(caption, L" - Google Chrome", true) || |
| 92 | EndsWith(caption, L" - Chromium", true); |
| 93 | if (!chrome_window_in_foreground) { |
| 94 | DWORD process_id; |
| 95 | int thread_id = ::GetWindowThreadProcessId(window, &process_id); |
| 96 | |
| 97 | base::ProcessHandle process; |
| 98 | if (base::OpenProcessHandleWithAccess(process_id, |
| 99 | PROCESS_QUERY_LIMITED_INFORMATION, |
| 100 | &process)) { |
| 101 | len = MAX_PATH; |
| 102 | if (!GetProcessImageFileName(process, WriteInto(&filename, len), len)) { |
| 103 | int error = GetLastError(); |
| 104 | filename = std::wstring(L"Unable to read filename for process id '" + |
| 105 | base::IntToString16(process_id) + |
| 106 | L"' (error ") + |
| 107 | base::IntToString16(error) + L")"; |
| 108 | } |
| 109 | base::CloseProcessHandle(process); |
| 110 | } |
| 111 | } |
| 112 | EXPECT_TRUE(chrome_window_in_foreground) |
| 113 | << "Chrome must be in the foreground when running interactive tests\n" |
| 114 | << "Process in foreground: " << filename.c_str() << "\n" |
| 115 | << "Window: " << window << "\n" |
| 116 | << "Caption: " << caption.c_str(); |
| 117 | return chrome_window_in_foreground; |
| 118 | #else |
| 119 | // Windows only at the moment. |
| 120 | return true; |
| 121 | #endif |
| 122 | } |
| 123 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 124 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | public: |
| 126 | BrowserFocusTest() { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 127 | set_show_window(true); |
| 128 | EnableDOMAutomation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | } |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 130 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 131 | bool IsViewFocused(ViewID vid) { |
| 132 | return ui_test_utils::IsViewFocused(browser(), vid); |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 135 | void ClickOnView(ViewID vid) { |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 136 | ui_test_utils::ClickOnView(browser(), vid); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 137 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | }; |
| 139 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 140 | class TestInterstitialPage : public InterstitialPage { |
| 141 | public: |
| 142 | TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 143 | : InterstitialPage(tab, new_navigation, url) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 144 | FilePath file_path; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 145 | bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 146 | EXPECT_TRUE(r); |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 147 | file_path = file_path.AppendASCII("focus"); |
| 148 | file_path = file_path.AppendASCII(kTypicalPageName); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 149 | r = file_util::ReadFileToString(file_path, &html_contents_); |
| 150 | EXPECT_TRUE(r); |
| 151 | } |
| 152 | |
| 153 | virtual std::string GetHTMLContents() { |
| 154 | return html_contents_; |
| 155 | } |
| 156 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 157 | // Exposing render_view_host() to be public; it is declared as protected in |
| 158 | // the superclass. |
| 159 | virtual RenderViewHost* render_view_host() { |
| 160 | return InterstitialPage::render_view_host(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | bool HasFocus() { |
| 164 | return render_view_host()->view()->HasFocus(); |
| 165 | } |
| 166 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 167 | protected: |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 168 | virtual void FocusedNodeChanged(bool is_editable_node) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 169 | NotificationService::current()->Notify( |
| 170 | NotificationType::FOCUS_CHANGED_IN_PAGE, |
| 171 | Source<RenderViewHost>(render_view_host()), |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 172 | Details<const bool>(&is_editable_node)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 175 | private: |
| 176 | std::string html_contents_; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 177 | }; |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 178 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 179 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 180 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 181 | #if defined(USE_X11) || defined(OS_MACOSX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 182 | // It seems we have to wait a little bit for the widgets to spin up before |
| 183 | // we can start clicking on them. |
| 184 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 185 | new MessageLoop::QuitTask(), |
| 186 | kActionDelayMs); |
| 187 | ui_test_utils::RunMessageLoop(); |
| 188 | #endif |
| 189 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 190 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 191 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 192 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 193 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 194 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 195 | ClickOnView(VIEW_ID_LOCATION_BAR); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 196 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 197 | } |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 198 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 199 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 200 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 201 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | |
| 203 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 204 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 205 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 207 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 208 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | // The focus should be on the Tab contents. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 210 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 212 | ui_test_utils::HideNativeWindow(window); |
| 213 | ui_test_utils::ShowAndFocusNativeWindow(window); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 214 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 215 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 216 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 217 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 219 | ui_test_utils::HideNativeWindow(window); |
| 220 | ui_test_utils::ShowAndFocusNativeWindow(window); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 221 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 223 | // The rest of this test does not make sense on Linux because the behavior |
| 224 | // of Activate() is not well defined and can vary by window manager. |
| 225 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 227 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 228 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 229 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 230 | browser2->window()->Show(); |
| 231 | ui_test_utils::NavigateToURL(browser2, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 233 | HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 234 | BrowserView* browser_view2 = |
| 235 | BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 236 | ASSERT_TRUE(browser_view2); |
| 237 | views::FocusManager* focus_manager2 = |
[email protected] | 82166b6 | 2009-06-30 18:48:00 | [diff] [blame] | 238 | views::FocusManager::GetFocusManagerForNativeView(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 239 | ASSERT_TRUE(focus_manager2); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 240 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 241 | focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | |
| 243 | // Switch to the 1st browser window, focus should still be on the location |
| 244 | // bar and the second browser should have nothing focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 245 | browser()->window()->Activate(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 246 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 247 | EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | |
| 249 | // Switch back to the second browser, focus should still be on the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 250 | browser2->window()->Activate(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 251 | EXPECT_EQ(NULL, |
| 252 | views::FocusManager::GetFocusManagerForNativeView( |
| 253 | browser()->window()->GetNativeHandle())->GetFocusedView()); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 254 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 255 | focus_manager2->GetFocusedView()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 256 | |
| 257 | // Close the 2nd browser to avoid a DCHECK(). |
| 258 | browser_view2->Close(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 259 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Tabs remember focus. |
[email protected] | 2615373 | 2010-11-09 18:47:39 | [diff] [blame] | 263 | // Disabled, http://crbug.com/62542. |
| 264 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 265 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 266 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | |
| 268 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 269 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 270 | ui_test_utils::NavigateToURL(browser(), url); |
| 271 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | // Create several tabs. |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 273 | for (int i = 0; i < 4; ++i) |
| 274 | browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | |
| 276 | // Alternate focus for the tab. |
| 277 | const bool kFocusPage[3][5] = { |
| 278 | { true, true, true, true, false }, |
| 279 | { false, false, false, false, false }, |
| 280 | { false, true, false, true, false } |
| 281 | }; |
| 282 | |
| 283 | for (int i = 1; i < 3; i++) { |
| 284 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 285 | // Activate the tab. |
| 286 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 287 | |
| 288 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 289 | if (kFocusPage[i][j]) { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 290 | browser()->GetTabContentsAt(j)->view()->Focus(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 291 | } else { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 292 | browser()->FocusLocationBar(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 293 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Now come back to the tab and check the right view is focused. |
| 297 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 298 | // Activate the tab. |
| 299 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 301 | ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 302 | VIEW_ID_LOCATION_BAR; |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 303 | ASSERT_TRUE(IsViewFocused(vid)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | } |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 305 | |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 306 | browser()->SelectTabContentsAt(0, true); |
| 307 | // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 308 | // this has regressed in the past. Loop through several times to be sure. |
| 309 | for (int j = 0; j < 15; j++) { |
| 310 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 311 | VIEW_ID_LOCATION_BAR; |
| 312 | ASSERT_TRUE(IsViewFocused(vid)); |
| 313 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 314 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 315 | browser(), app::VKEY_TAB, true, false, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // As above, but with ctrl+shift+tab. |
| 319 | browser()->SelectTabContentsAt(4, true); |
| 320 | for (int j = 14; j >= 0; --j) { |
| 321 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 322 | VIEW_ID_LOCATION_BAR; |
| 323 | ASSERT_TRUE(IsViewFocused(vid)); |
| 324 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 325 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 326 | browser(), app::VKEY_TAB, true, true, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 327 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 331 | // Tabs remember focus with find-in-page box. |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 332 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 333 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 334 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 335 | |
| 336 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 337 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 338 | ui_test_utils::NavigateToURL(browser(), url); |
| 339 | |
| 340 | browser()->Find(); |
| 341 | ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
| 342 | ASCIIToUTF16("a"), true, false, NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 343 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 344 | |
| 345 | // Focus the location bar. |
| 346 | browser()->FocusLocationBar(); |
| 347 | |
| 348 | // Create a 2nd tab. |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 349 | browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 350 | |
| 351 | // Focus should be on the recently opened tab page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 352 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 353 | |
| 354 | // Select 1st tab, focus should still be on the location-bar. |
| 355 | // (bug http://crbug.com/23296) |
| 356 | browser()->SelectTabContentsAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 357 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 358 | |
| 359 | // Now open the find box again, switch to another tab and come back, the focus |
| 360 | // should return to the find box. |
| 361 | browser()->Find(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 362 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 363 | browser()->SelectTabContentsAt(1, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 364 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 365 | browser()->SelectTabContentsAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 366 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 367 | } |
| 368 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 369 | // Background window does not steal focus. |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 370 | // Flaky, http://crbug.com/62538. |
| 371 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, |
| 372 | FLAKY_BackgroundBrowserDontStealFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 373 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 374 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 { |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 394 | FAIL() << "Could not determine which browser has focus"; |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 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] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 405 | GURL steal_focus_url = test_server()->GetURL(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 | |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 411 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
| 412 | unfocused_browser->GetSelectedTabContents()->render_view_host(), L"", |
| 413 | L"stealFocus();")); |
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()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // Page cannot steal focus when focus is on location bar. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 420 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 421 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 422 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | |
| 424 | // Open the page that steals focus. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 425 | GURL url = test_server()->GetURL(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 426 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 428 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 430 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
| 431 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 432 | L"stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | |
| 434 | // Make sure the location bar is still focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 435 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 438 | // Focus traversal on a regular page. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 439 | // Note that this test relies on a notification from the renderer that the |
| 440 | // focus has changed in the page. The notification in the renderer may change |
| 441 | // at which point this test would fail (see comment in |
| 442 | // RenderWidget::didFocus()). |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 443 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 444 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 445 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 446 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 447 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 448 | GURL url = test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 449 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 451 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 453 | const char* kTextElementID = "textEdit"; |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 454 | const char* kExpElementIDs[] = { |
| 455 | "", // Initially no element in the page should be focused |
| 456 | // (the location bar is focused). |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 457 | kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink", |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 458 | "gmapLink" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | // Test forward focus traversal. |
| 462 | for (int i = 0; i < 3; ++i) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 463 | SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 464 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 465 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 466 | |
| 467 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 468 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 469 | SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | // 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] | 471 | std::string actual; |
| 472 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 473 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 474 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 475 | L"window.domAutomationController.send(getFocusedElement());", |
| 476 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 477 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 478 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 479 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 480 | // If the next element is the kTextElementID, we expect to be |
| 481 | // notified we have switched to an editable node. |
| 482 | bool is_editable_node = |
| 483 | (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); |
| 484 | Details<bool> details(&is_editable_node); |
| 485 | |
| 486 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( |
| 487 | browser(), app::VKEY_TAB, false, false, false, false, |
| 488 | NotificationType::FOCUS_CHANGED_IN_PAGE, |
| 489 | NotificationSource(Source<RenderViewHost>( |
| 490 | browser()->GetSelectedTabContents()->render_view_host())), |
| 491 | details)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 492 | } else { |
| 493 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 494 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 495 | browser(), app::VKEY_TAB, false, false, false, false, |
| 496 | NotificationType::FOCUS_RETURNED_TO_BROWSER, |
| 497 | NotificationSource(Source<Browser>(browser())))); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 498 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 499 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 500 | |
| 501 | // At this point the renderer has sent us a message asking to advance the |
| 502 | // focus (as the end of the focus loop was reached in the renderer). |
| 503 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 504 | ui_test_utils::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Now let's try reverse focus traversal. |
| 508 | for (int i = 0; i < 3; ++i) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 509 | SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 511 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 513 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 514 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 515 | SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 516 | const char* next_element = |
| 517 | kExpElementIDs[arraysize(kExpElementIDs) - 1 - j]; |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 518 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 519 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 520 | // If the next element is the kTextElementID, we expect to be |
| 521 | // notified we have switched to an editable node. |
| 522 | bool is_editable_node = (strcmp(kTextElementID, next_element) == 0); |
| 523 | Details<bool> details(&is_editable_node); |
| 524 | |
| 525 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( |
| 526 | browser(), app::VKEY_TAB, false, true, false, false, |
| 527 | NotificationType::FOCUS_CHANGED_IN_PAGE, |
| 528 | NotificationSource(Source<RenderViewHost>( |
| 529 | browser()->GetSelectedTabContents()->render_view_host())), |
| 530 | details)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 531 | } else { |
| 532 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 533 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 534 | browser(), app::VKEY_TAB, false, true, false, false, |
| 535 | NotificationType::FOCUS_RETURNED_TO_BROWSER, |
| 536 | NotificationSource(Source<Browser>(browser())))); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 537 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 538 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | // 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] | 540 | std::string actual; |
| 541 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 542 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 543 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 544 | L"window.domAutomationController.send(getFocusedElement());", |
| 545 | &actual)); |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 546 | ASSERT_STREQ(next_element, actual.c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 547 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 548 | |
| 549 | // At this point the renderer has sent us a message asking to advance the |
| 550 | // focus (as the end of the focus loop was reached in the renderer). |
| 551 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 552 | ui_test_utils::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 556 | // Focus traversal while an interstitial is showing. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 557 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 558 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 559 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 560 | |
| 561 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 562 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 563 | ui_test_utils::NavigateToURL(browser(), url); |
| 564 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 565 | // Focus should be on the page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 566 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 567 | |
| 568 | // Let's show an interstitial. |
| 569 | TestInterstitialPage* interstitial_page = |
| 570 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 571 | true, GURL("http://interstitial.com")); |
| 572 | interstitial_page->Show(); |
| 573 | // Give some time for the interstitial to show. |
| 574 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 575 | new MessageLoop::QuitTask(), |
| 576 | 1000); |
| 577 | ui_test_utils::RunMessageLoop(); |
| 578 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 579 | browser()->FocusLocationBar(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 580 | |
| 581 | const char* kExpElementIDs[] = { |
| 582 | "", // Initially no element in the page should be focused |
| 583 | // (the location bar is focused). |
| 584 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 585 | "gmapLink" |
| 586 | }; |
| 587 | |
| 588 | // Test forward focus traversal. |
| 589 | for (int i = 0; i < 2; ++i) { |
| 590 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 591 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 592 | |
| 593 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 594 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 595 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 596 | std::string actual; |
| 597 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 598 | interstitial_page->render_view_host(), L"", |
| 599 | L"window.domAutomationController.send(getFocusedElement());", |
| 600 | &actual)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 601 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 602 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 603 | NotificationType::Type notification_type; |
| 604 | NotificationSource notification_source = |
| 605 | NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 606 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 607 | notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; |
| 608 | notification_source = Source<RenderViewHost>( |
| 609 | interstitial_page->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 610 | } else { |
| 611 | // On the last tab key press, the focus returns to the browser. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 612 | notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; |
| 613 | notification_source = Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 614 | } |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 615 | |
| 616 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 617 | browser(), app::VKEY_TAB, false, false, false, false, |
| 618 | notification_type, notification_source)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // At this point the renderer has sent us a message asking to advance the |
| 622 | // focus (as the end of the focus loop was reached in the renderer). |
| 623 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 624 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Now let's try reverse focus traversal. |
| 628 | for (int i = 0; i < 2; ++i) { |
| 629 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 630 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 631 | |
| 632 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 633 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 634 | NotificationType::Type notification_type; |
| 635 | NotificationSource notification_source = |
| 636 | NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 637 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 638 | notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; |
| 639 | notification_source = Source<RenderViewHost>( |
| 640 | interstitial_page->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 641 | } else { |
| 642 | // On the last tab key press, the focus returns to the browser. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 643 | notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; |
| 644 | notification_source = Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 645 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 646 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 647 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 648 | browser(), app::VKEY_TAB, false, true, false, false, |
| 649 | notification_type, notification_source)); |
| 650 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 651 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 652 | std::string actual; |
| 653 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 654 | interstitial_page->render_view_host(), L"", |
| 655 | L"window.domAutomationController.send(getFocusedElement());", |
| 656 | &actual)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 657 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 658 | } |
| 659 | |
| 660 | // At this point the renderer has sent us a message asking to advance the |
| 661 | // focus (as the end of the focus loop was reached in the renderer). |
| 662 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 663 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | // Focus stays on page with interstitials. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 668 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 669 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 670 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 671 | |
| 672 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 673 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 674 | ui_test_utils::NavigateToURL(browser(), url); |
| 675 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 676 | // Page should have focus. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 677 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 678 | EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 679 | HasFocus()); |
| 680 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 681 | // Let's show an interstitial. |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 682 | TestInterstitialPage* interstitial_page = |
| 683 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 684 | true, GURL("http://interstitial.com")); |
| 685 | interstitial_page->Show(); |
| 686 | // Give some time for the interstitial to show. |
| 687 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 688 | new MessageLoop::QuitTask(), |
| 689 | 1000); |
| 690 | ui_test_utils::RunMessageLoop(); |
| 691 | |
| 692 | // The interstitial should have focus now. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 693 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 694 | EXPECT_TRUE(interstitial_page->HasFocus()); |
| 695 | |
| 696 | // Hide the interstitial. |
| 697 | interstitial_page->DontProceed(); |
| 698 | |
| 699 | // Focus should be back on the original page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 700 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 701 | } |
| 702 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 703 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 704 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 705 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 706 | ASSERT_TRUE(test_server()->Start()); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 707 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 708 | // Open some page (any page that doesn't steal focus). |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 709 | GURL url = test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 710 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 711 | |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 712 | EXPECT_TRUE(ChromeInForeground()); |
| 713 | |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 714 | #if defined(OS_MACOSX) |
| 715 | // Press Cmd+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 716 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 717 | browser(), app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 718 | #else |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 719 | // Press Ctrl+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 720 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 721 | browser(), app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 722 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 723 | |
| 724 | // Ideally, we wouldn't sleep here and instead would intercept the |
| 725 | // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 726 | // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 727 | // - creating a factory used to create the delegate |
| 728 | // - making the test a private and overwriting the delegate member directly. |
[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(); |
| 732 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 733 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 734 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 735 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 736 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 737 | |
| 738 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 739 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 740 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 741 | browser(), app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 742 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 743 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 744 | browser(), app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 745 | #endif |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 746 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 747 | |
| 748 | // Set focus to the page. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 749 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 750 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 751 | |
| 752 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 753 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 754 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 755 | browser(), app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 756 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 757 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 758 | browser(), app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 759 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 760 | |
| 761 | // See remark above on why we wait. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 762 | MessageLoop::current()->PostDelayedTask( |
| 763 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 764 | ui_test_utils::RunMessageLoop(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 765 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 766 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 767 | |
| 768 | // Makes sure the focus is in the right location when opening the different |
| 769 | // types of tabs. |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 770 | // Flaky, http://crbug.com/62539. |
| 771 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 772 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 773 | |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 774 | // Open the history tab, focus should be on the tab contents. |
| 775 | browser()->ShowHistoryTab(); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 776 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 777 | &browser()->GetSelectedTabContents()->controller())); |
| 778 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 779 | |
| 780 | // Open the new tab, focus should be on the location bar. |
| 781 | browser()->NewTab(); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 782 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 783 | &browser()->GetSelectedTabContents()->controller())); |
| 784 | EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 785 | |
| 786 | // Open the download tab, focus should be on the tab contents. |
| 787 | browser()->ShowDownloadsTab(); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 788 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 789 | &browser()->GetSelectedTabContents()->controller())); |
| 790 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 3e3f0eb | 2009-06-22 18:33:43 | [diff] [blame] | 791 | |
| 792 | // Open about:blank, focus should be on the location bar. |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 793 | browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), |
| 794 | PageTransition::LINK); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 795 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 796 | &browser()->GetSelectedTabContents()->controller())); |
| 797 | EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 798 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 799 | |
| 800 | // Tests that focus goes where expected when using reload. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 801 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 802 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 803 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 804 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 805 | // Open the new tab, reload. |
| 806 | browser()->NewTab(); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 807 | ui_test_utils::RunAllPendingInMessageLoop(); |
| 808 | |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 809 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 810 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 811 | // Focus should stay on the location bar. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 812 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 813 | |
| 814 | // Open a regular page, focus the location bar, reload. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 815 | ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 816 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 817 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 818 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 819 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 820 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 821 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 822 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 823 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // Tests that focus goes where expected when using reload on a crashed tab. |
[email protected] | 160f29ad | 2010-10-28 15:43:27 | [diff] [blame] | 827 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 828 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 829 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 830 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 831 | // Open a regular page, crash, reload. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 832 | ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 833 | ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 834 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 835 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 836 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 837 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 838 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 839 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 840 | } |
[email protected] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 841 | |
| 842 | } // namespace |