[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 6 | #include "base/file_util.h" |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 7 | #include "base/format_macros.h" |
[email protected] | 3cc55ad | 2013-07-17 22:17:41 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 9 | #include "base/path_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
[email protected] | 1152118 | 2013-06-11 04:06:36 | [diff] [blame] | 11 | #include "base/strings/stringprintf.h" |
[email protected] | 5846d58 | 2013-06-08 16:02:12 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | dcc8fbc | 2013-07-12 00:54:09 | [diff] [blame] | 13 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_tabstrip.h" |
[email protected] | 00070c73 | 2011-04-09 15:31:33 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 18 | #include "chrome/browser/ui/chrome_pages.h" |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 19 | #include "chrome/browser/ui/omnibox/location_bar.h" |
| 20 | #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" |
| 21 | #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
| 22 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 23 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 6a3ec231 | 2010-12-02 19:30:19 | [diff] [blame] | 24 | #include "chrome/browser/ui/view_ids.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 25 | #include "chrome/common/chrome_paths.h" |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 26 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 27 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 28 | #include "chrome/test/base/interactive_test_utils.h" |
[email protected] | 477ae05 | 2011-11-18 23:53:57 | [diff] [blame] | 29 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | cadaec5 | 2012-02-08 21:53:13 | [diff] [blame] | 30 | #include "content/public/browser/interstitial_page.h" |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 31 | #include "content/public/browser/interstitial_page_delegate.h" |
[email protected] | 477ae05 | 2011-11-18 23:53:57 | [diff] [blame] | 32 | #include "content/public/browser/notification_service.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 33 | #include "content/public/browser/render_view_host.h" |
[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 34 | #include "content/public/browser/render_widget_host_view.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 35 | #include "content/public/browser/web_contents.h" |
[email protected] | 8643e6d | 2012-01-18 20:26:10 | [diff] [blame] | 36 | #include "content/public/browser/web_contents_view.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 37 | #include "content/public/test/browser_test_utils.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 38 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 39 | |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 40 | #if defined(OS_WIN) |
[email protected] | 93270d00 | 2011-01-19 22:32:59 | [diff] [blame] | 41 | #include <windows.h> |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 42 | #include <Psapi.h> |
[email protected] | 1152118 | 2013-06-11 04:06:36 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 44 | #endif |
| 45 | |
[email protected] | cadaec5 | 2012-02-08 21:53:13 | [diff] [blame] | 46 | using content::InterstitialPage; |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 47 | using content::NavigationController; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 48 | using content::RenderViewHost; |
[email protected] | 4b19ea5 | 2012-01-02 20:15:25 | [diff] [blame] | 49 | using content::WebContents; |
| 50 | |
[email protected] | 3a3cf4d | 2011-12-09 00:39:35 | [diff] [blame] | 51 | #if defined(OS_MACOSX) |
| 52 | // TODO(suzhe): http://crbug.com/60973 |
[email protected] | a5eb2d67 | 2010-10-22 07:24:48 | [diff] [blame] | 53 | #define MAYBE_FocusTraversal DISABLED_FocusTraversal |
| 54 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | ddabf40 | 2013-10-22 22:43:08 | [diff] [blame^] | 55 | #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
| 56 | // TODO(erg): http://crbug.com/163931 |
| 57 | #define MAYBE_FocusTraversal DISABLED_FocusTraversal |
| 58 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | 5b6d043 | 2013-01-24 20:30:16 | [diff] [blame] | 59 | #elif defined(OS_WIN) || defined(OS_CHROMEOS) |
| 60 | // http://crbug.com/109770 and http://crbug.com/62544 |
[email protected] | 2f2bf117 | 2012-01-10 22:17:18 | [diff] [blame] | 61 | #define MAYBE_FocusTraversal FocusTraversal |
[email protected] | 87115392 | 2012-01-10 20:13:48 | [diff] [blame] | 62 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | 3a3cf4d | 2011-12-09 00:39:35 | [diff] [blame] | 63 | #else |
| 64 | #define MAYBE_FocusTraversal FocusTraversal |
| 65 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
| 66 | #endif |
| 67 | |
| 68 | #if defined(OS_LINUX) || defined(OS_MACOSX) |
| 69 | // TODO(jcampan): http://crbug.com/23683 for linux. |
| 70 | // TODO(suzhe): http://crbug.com/49737 for mac. |
[email protected] | e00ccc9 | 2012-11-01 17:32:30 | [diff] [blame] | 71 | #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 72 | #elif defined(OS_WIN) |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 73 | // Flaky, http://crbug.com/62537. |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 74 | #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 75 | #endif |
| 76 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | namespace { |
| 78 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 79 | // The delay waited in some cases where we don't have a notifications for an |
| 80 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | const int kActionDelayMs = 500; |
| 82 | |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 83 | // Maxiumum time to wait until the focus is moved to expected view. |
| 84 | const int kFocusChangeTimeoutMs = 500; |
| 85 | |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 86 | const char kSimplePage[] = "/focus/page_with_focus.html"; |
| 87 | const char kStealFocusPage[] = "/focus/page_steals_focus.html"; |
| 88 | const char kTypicalPage[] = "/focus/typical_page.html"; |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 89 | const char kTypicalPageName[] = "typical_page.html"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 91 | // Test to make sure Chrome is in the foreground as we start testing. This is |
| 92 | // required for tests that synthesize input to the Chrome window. |
| 93 | bool ChromeInForeground() { |
| 94 | #if defined(OS_WIN) |
| 95 | HWND window = ::GetForegroundWindow(); |
| 96 | std::wstring caption; |
| 97 | std::wstring filename; |
| 98 | int len = ::GetWindowTextLength(window) + 1; |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 99 | if (len > 1) |
| 100 | ::GetWindowText(window, WriteInto(&caption, len), len); |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 101 | bool chrome_window_in_foreground = |
| 102 | EndsWith(caption, L" - Google Chrome", true) || |
| 103 | EndsWith(caption, L" - Chromium", true); |
| 104 | if (!chrome_window_in_foreground) { |
| 105 | DWORD process_id; |
| 106 | int thread_id = ::GetWindowThreadProcessId(window, &process_id); |
| 107 | |
| 108 | base::ProcessHandle process; |
| 109 | if (base::OpenProcessHandleWithAccess(process_id, |
| 110 | PROCESS_QUERY_LIMITED_INFORMATION, |
| 111 | &process)) { |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 112 | if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH), |
| 113 | MAX_PATH)) { |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 114 | int error = GetLastError(); |
| 115 | filename = std::wstring(L"Unable to read filename for process id '" + |
| 116 | base::IntToString16(process_id) + |
| 117 | L"' (error ") + |
| 118 | base::IntToString16(error) + L")"; |
| 119 | } |
| 120 | base::CloseProcessHandle(process); |
| 121 | } |
| 122 | } |
| 123 | EXPECT_TRUE(chrome_window_in_foreground) |
| 124 | << "Chrome must be in the foreground when running interactive tests\n" |
| 125 | << "Process in foreground: " << filename.c_str() << "\n" |
| 126 | << "Window: " << window << "\n" |
| 127 | << "Caption: " << caption.c_str(); |
| 128 | return chrome_window_in_foreground; |
| 129 | #else |
| 130 | // Windows only at the moment. |
| 131 | return true; |
| 132 | #endif |
| 133 | } |
| 134 | |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 135 | // Wait the focus change in message loop. |
| 136 | void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) { |
| 137 | if (ui_test_utils::IsViewFocused(browser, id) || |
| 138 | base::Time::Now() > timeout) { |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 139 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 140 | base::MessageLoop::QuitClosure()); |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 141 | } else { |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 142 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 143 | FROM_HERE, |
| 144 | base::Bind(&CheckFocus, browser, id, timeout), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 145 | base::TimeDelta::FromMilliseconds(10)); |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 149 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | public: |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 151 | bool IsViewFocused(ViewID vid) { |
| 152 | return ui_test_utils::IsViewFocused(browser(), vid); |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 155 | void ClickOnView(ViewID vid) { |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 156 | ui_test_utils::ClickOnView(browser(), vid); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 157 | } |
[email protected] | 0b8fa8b | 2011-12-07 00:54:52 | [diff] [blame] | 158 | |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 159 | bool WaitForFocusChange(ViewID vid) { |
| 160 | const base::Time timeout = base::Time::Now() + |
| 161 | base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs); |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 162 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 163 | FROM_HERE, |
| 164 | base::Bind(&CheckFocus, browser(), vid, timeout), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 165 | base::TimeDelta::FromMilliseconds(100)); |
[email protected] | 729eb63 | 2012-07-26 04:45:26 | [diff] [blame] | 166 | content::RunMessageLoop(); |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 167 | return IsViewFocused(vid); |
| 168 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | }; |
| 170 | |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 171 | class TestInterstitialPage : public content::InterstitialPageDelegate { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 172 | public: |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 173 | TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 174 | base::FilePath file_path; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 175 | bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 176 | EXPECT_TRUE(r); |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 177 | file_path = file_path.AppendASCII("focus"); |
| 178 | file_path = file_path.AppendASCII(kTypicalPageName); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 179 | r = base::ReadFileToString(file_path, &html_contents_); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 180 | EXPECT_TRUE(r); |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 181 | interstitial_page_ = InterstitialPage::Create( |
| 182 | tab, new_navigation, url , this); |
| 183 | interstitial_page_->Show(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 626cc90 | 2013-02-06 18:30:53 | [diff] [blame] | 186 | virtual std::string GetHTMLContents() OVERRIDE { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 187 | return html_contents_; |
| 188 | } |
| 189 | |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 190 | RenderViewHost* render_view_host() { |
[email protected] | cadaec5 | 2012-02-08 21:53:13 | [diff] [blame] | 191 | return interstitial_page_->GetRenderViewHostForTesting(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 194 | void DontProceed() { |
| 195 | interstitial_page_->DontProceed(); |
[email protected] | 4e85c11 | 2011-01-26 22:27:21 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 198 | bool HasFocus() { |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 199 | return render_view_host()->GetView()->HasFocus(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private: |
| 203 | std::string html_contents_; |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 204 | InterstitialPage* interstitial_page_; // Owns us. |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 205 | }; |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 206 | |
[email protected] | c39e233d | 2012-05-18 13:24:12 | [diff] [blame] | 207 | // Flaky on mac. http://crbug.com/67301. |
| 208 | #if defined(OS_MACOSX) |
[email protected] | 839e080 | 2012-05-18 14:45:23 | [diff] [blame] | 209 | #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus |
[email protected] | c39e233d | 2012-05-18 13:24:12 | [diff] [blame] | 210 | #else |
[email protected] | 839e080 | 2012-05-18 14:45:23 | [diff] [blame] | 211 | #define MAYBE_ClickingMovesFocus ClickingMovesFocus |
[email protected] | c39e233d | 2012-05-18 13:24:12 | [diff] [blame] | 212 | #endif |
| 213 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 214 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 0ff0ff3 | 2010-12-21 19:34:42 | [diff] [blame] | 215 | #if defined(OS_POSIX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 216 | // It seems we have to wait a little bit for the widgets to spin up before |
| 217 | // we can start clicking on them. |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 218 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 219 | FROM_HERE, |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 220 | base::MessageLoop::QuitClosure(), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 221 | base::TimeDelta::FromMilliseconds(kActionDelayMs)); |
[email protected] | 729eb63 | 2012-07-26 04:45:26 | [diff] [blame] | 222 | content::RunMessageLoop(); |
[email protected] | 0ff0ff3 | 2010-12-21 19:34:42 | [diff] [blame] | 223 | #endif // defined(OS_POSIX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 224 | |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 225 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 226 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 227 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 228 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 229 | |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 230 | ClickOnView(VIEW_ID_OMNIBOX); |
| 231 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 232 | } |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 233 | |
[email protected] | 320948e0 | 2011-01-10 08:21:12 | [diff] [blame] | 234 | // Flaky, http://crbug.com/69034. |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 235 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 236 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 237 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | |
| 239 | // First we navigate to our test page. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 240 | GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 241 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | |
[email protected] | 90556dd | 2012-06-07 20:26:18 | [diff] [blame] | 243 | gfx::NativeWindow window = browser()->window()->GetNativeWindow(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 244 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | // The focus should be on the Tab contents. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 246 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 248 | ui_test_utils::HideNativeWindow(window); |
[email protected] | 7807088c | 2011-11-09 15:16:29 | [diff] [blame] | 249 | ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 250 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 252 | chrome::FocusLocationBar(browser()); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 253 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 255 | ui_test_utils::HideNativeWindow(window); |
[email protected] | 7807088c | 2011-11-09 15:16:29 | [diff] [blame] | 256 | ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 257 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Tabs remember focus. |
[email protected] | 2615373 | 2010-11-09 18:47:39 | [diff] [blame] | 261 | // Disabled, http://crbug.com/62542. |
| 262 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 263 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 264 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | |
| 266 | // First we navigate to our test page. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 267 | GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 268 | ui_test_utils::NavigateToURL(browser(), url); |
| 269 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | // Create several tabs. |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 271 | for (int i = 0; i < 4; ++i) { |
| 272 | chrome::AddSelectedTabWithURL(browser(), url, |
| 273 | content::PAGE_TRANSITION_TYPED); |
| 274 | } |
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. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 286 | browser()->tab_strip_model()->ActivateTabAt(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] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 290 | browser()->tab_strip_model()->GetWebContentsAt(j)->GetView()->Focus(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 291 | } else { |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 292 | chrome::FocusLocationBar(browser()); |
[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. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 299 | browser()->tab_strip_model()->ActivateTabAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 301 | ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX; |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 302 | ASSERT_TRUE(IsViewFocused(vid)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | } |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 304 | |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 305 | browser()->tab_strip_model()->ActivateTabAt(0, true); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 306 | // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 307 | // this has regressed in the past. Loop through several times to be sure. |
| 308 | for (int j = 0; j < 15; j++) { |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 309 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 310 | VIEW_ID_OMNIBOX; |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 311 | ASSERT_TRUE(IsViewFocused(vid)); |
| 312 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 313 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 314 | browser(), ui::VKEY_TAB, true, false, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // As above, but with ctrl+shift+tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 318 | browser()->tab_strip_model()->ActivateTabAt(4, true); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 319 | for (int j = 14; j >= 0; --j) { |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 320 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 321 | VIEW_ID_OMNIBOX; |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 322 | ASSERT_TRUE(IsViewFocused(vid)); |
| 323 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 324 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 325 | browser(), ui::VKEY_TAB, true, true, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 326 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 330 | // Tabs remember focus with find-in-page box. |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 331 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 332 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 333 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 334 | |
| 335 | // First we navigate to our test page. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 336 | GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 337 | ui_test_utils::NavigateToURL(browser(), url); |
| 338 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 339 | chrome::Find(browser()); |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 340 | ui_test_utils::FindInPage( |
| 341 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 342 | ASCIIToUTF16("a"), true, false, NULL, 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. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 346 | chrome::FocusLocationBar(browser()); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 347 | |
| 348 | // Create a 2nd tab. |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 349 | chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_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] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 352 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[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) |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 356 | browser()->tab_strip_model()->ActivateTabAt(0, true); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 357 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[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. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 361 | chrome::Find(browser()); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 362 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 363 | browser()->tab_strip_model()->ActivateTabAt(1, true); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 364 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 365 | browser()->tab_strip_model()->ActivateTabAt(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, |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 372 | DISABLED_BackgroundBrowserDontStealFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 373 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 374 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
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] | c125cdc | 2013-02-25 19:10:21 | [diff] [blame] | 377 | Browser* browser2 = |
| 378 | new Browser(Browser::CreateParams(browser()->profile(), |
| 379 | browser()->host_desktop_type())); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 380 | ASSERT_TRUE(browser2); |
[email protected] | 686221c | 2012-11-08 07:30:10 | [diff] [blame] | 381 | chrome::AddBlankTabAt(browser2, -1, true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 382 | browser2->window()->Show(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 383 | |
[email protected] | ed179ee | 2009-10-03 21:02:51 | [diff] [blame] | 384 | Browser* focused_browser = NULL; |
| 385 | Browser* unfocused_browser = NULL; |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 386 | #if defined(USE_X11) |
| 387 | // On X11, calling Activate() is not guaranteed to move focus, so we have |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 388 | // to figure out which browser does have focus. |
| 389 | if (browser2->window()->IsActive()) { |
| 390 | focused_browser = browser2; |
| 391 | unfocused_browser = browser(); |
| 392 | } else if (browser()->window()->IsActive()) { |
| 393 | focused_browser = browser(); |
| 394 | unfocused_browser = browser2; |
| 395 | } else { |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 396 | FAIL() << "Could not determine which browser has focus"; |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 397 | } |
| 398 | #elif defined(OS_WIN) |
| 399 | focused_browser = browser(); |
| 400 | unfocused_browser = browser2; |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 401 | #elif defined(OS_MACOSX) |
| 402 | // On Mac, the newly created window always gets the focus. |
| 403 | focused_browser = browser2; |
| 404 | unfocused_browser = browser(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 405 | #endif |
| 406 | |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 407 | GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 408 | ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 409 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 410 | // Activate the first browser. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 411 | focused_browser->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 413 | ASSERT_TRUE(content::ExecuteScript( |
[email protected] | da2bfa4 | 2013-01-29 23:47:05 | [diff] [blame] | 414 | unfocused_browser->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 415 | "stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 417 | // Make sure the first browser is still active. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 418 | EXPECT_TRUE(focused_browser->window()->IsActive()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | ddabf40 | 2013-10-22 22:43:08 | [diff] [blame^] | 421 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
| 422 | // TODO(erg): http://crbug.com/163931 |
| 423 | #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus |
| 424 | #else |
| 425 | #define MAYBE_LocationBarLockFocus LocationBarLockFocus |
| 426 | #endif |
| 427 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | // Page cannot steal focus when focus is on location bar. |
[email protected] | ddabf40 | 2013-10-22 22:43:08 | [diff] [blame^] | 429 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 430 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 431 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 432 | |
| 433 | // Open the page that steals focus. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 434 | GURL url = embedded_test_server()->GetURL(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 435 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 437 | chrome::FocusLocationBar(browser()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 438 | |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 439 | ASSERT_TRUE(content::ExecuteScript( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 440 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 441 | "stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | |
| 443 | // Make sure the location bar is still focused. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 444 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | } |
| 446 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 447 | // Focus traversal on a regular page. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 448 | // Note that this test relies on a notification from the renderer that the |
| 449 | // focus has changed in the page. The notification in the renderer may change |
| 450 | // at which point this test would fail (see comment in |
| 451 | // RenderWidget::didFocus()). |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 452 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 453 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 454 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 456 | // First we navigate to our test page. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 457 | GURL url = embedded_test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 458 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 460 | chrome::FocusLocationBar(browser()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 462 | const char* kTextElementID = "textEdit"; |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 463 | const char* kExpElementIDs[] = { |
| 464 | "", // Initially no element in the page should be focused |
| 465 | // (the location bar is focused). |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 466 | kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink", |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 467 | "gmapLink" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | // Test forward focus traversal. |
| 471 | for (int i = 0; i < 3; ++i) { |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 472 | SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 473 | // Location bar should be focused. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 474 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | |
[email protected] | 911696b | 2011-01-28 02:36:49 | [diff] [blame] | 476 | // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 477 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 478 | browser(), ui::VKEY_END, false, false, false, false)); |
| 479 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 480 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 481 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 482 | SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | // 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] | 484 | std::string actual; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 485 | ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 486 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 487 | "window.domAutomationController.send(getFocusedElement());", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 488 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 490 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 491 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 492 | // If the next element is the kTextElementID, we expect to be |
| 493 | // notified we have switched to an editable node. |
| 494 | bool is_editable_node = |
| 495 | (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 496 | content::Details<bool> details(&is_editable_node); |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 497 | |
| 498 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 499 | browser(), ui::VKEY_TAB, false, false, false, false, |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 500 | content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, |
[email protected] | d5309265 | 2011-12-06 06:12:56 | [diff] [blame] | 501 | content::NotificationSource(content::Source<RenderViewHost>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 502 | browser()->tab_strip_model()->GetActiveWebContents()-> |
| 503 | GetRenderViewHost())), |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 504 | details)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 505 | } else { |
| 506 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 507 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 508 | browser(), ui::VKEY_TAB, false, false, false, false, |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 509 | chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 510 | content::NotificationSource(content::Source<Browser>(browser())))); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 511 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 513 | |
| 514 | // At this point the renderer has sent us a message asking to advance the |
| 515 | // focus (as the end of the focus loop was reached in the renderer). |
| 516 | // We need to run the message loop to process it. |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 517 | content::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // Now let's try reverse focus traversal. |
| 521 | for (int i = 0; i < 3; ++i) { |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 522 | SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 523 | // Location bar should be focused. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 524 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | |
[email protected] | 911696b | 2011-01-28 02:36:49 | [diff] [blame] | 526 | // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 527 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 528 | browser(), ui::VKEY_END, false, false, false, false)); |
| 529 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 530 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 531 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 532 | SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j)); |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 533 | const char* next_element = |
| 534 | kExpElementIDs[arraysize(kExpElementIDs) - 1 - j]; |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 535 | |
| 536 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 537 | // If the next element is the kTextElementID, we expect to be |
| 538 | // notified we have switched to an editable node. |
| 539 | bool is_editable_node = (strcmp(kTextElementID, next_element) == 0); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 540 | content::Details<bool> details(&is_editable_node); |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 541 | |
| 542 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 543 | browser(), ui::VKEY_TAB, false, true, false, false, |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 544 | content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, |
[email protected] | d5309265 | 2011-12-06 06:12:56 | [diff] [blame] | 545 | content::NotificationSource(content::Source<RenderViewHost>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 546 | browser()->tab_strip_model()->GetActiveWebContents()-> |
| 547 | GetRenderViewHost())), |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 548 | details)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 549 | } else { |
| 550 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 551 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 552 | browser(), ui::VKEY_TAB, false, true, false, false, |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 553 | chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 554 | content::NotificationSource(content::Source<Browser>(browser())))); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 555 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 556 | |
| 557 | // 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] | 558 | std::string actual; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 559 | ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 560 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 561 | "window.domAutomationController.send(getFocusedElement());", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 562 | &actual)); |
[email protected] | 546ae4e0 | 2010-12-08 14:57:19 | [diff] [blame] | 563 | ASSERT_STREQ(next_element, actual.c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 564 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 565 | |
| 566 | // At this point the renderer has sent us a message asking to advance the |
| 567 | // focus (as the end of the focus loop was reached in the renderer). |
| 568 | // We need to run the message loop to process it. |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 569 | content::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 573 | // Focus traversal while an interstitial is showing. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 574 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 575 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 576 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 577 | |
| 578 | // First we navigate to our test page. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 579 | GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 580 | ui_test_utils::NavigateToURL(browser(), url); |
| 581 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 582 | // Focus should be on the page. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 583 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 584 | |
| 585 | // Let's show an interstitial. |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 586 | TestInterstitialPage* interstitial_page = new TestInterstitialPage( |
| 587 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 588 | true, GURL("http://interstitial.com")); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 589 | // Give some time for the interstitial to show. |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 590 | base::MessageLoop::current()->PostDelayedTask( |
| 591 | FROM_HERE, |
| 592 | base::MessageLoop::QuitClosure(), |
| 593 | base::TimeDelta::FromSeconds(1)); |
[email protected] | 729eb63 | 2012-07-26 04:45:26 | [diff] [blame] | 594 | content::RunMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 595 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 596 | chrome::FocusLocationBar(browser()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 597 | |
| 598 | const char* kExpElementIDs[] = { |
| 599 | "", // Initially no element in the page should be focused |
| 600 | // (the location bar is focused). |
| 601 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 602 | "gmapLink" |
| 603 | }; |
| 604 | |
| 605 | // Test forward focus traversal. |
| 606 | for (int i = 0; i < 2; ++i) { |
| 607 | // Location bar should be focused. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 608 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 609 | |
[email protected] | 911696b | 2011-01-28 02:36:49 | [diff] [blame] | 610 | // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 611 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 612 | browser(), ui::VKEY_END, false, false, false, false)); |
| 613 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 614 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 615 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 616 | // 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] | 617 | std::string actual; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 618 | ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 619 | interstitial_page->render_view_host(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 620 | "window.domAutomationController.send(getFocusedElement());", |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 621 | &actual)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 622 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 623 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 624 | int notification_type; |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 625 | content::NotificationSource notification_source = |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 626 | content::NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 627 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 628 | notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; |
[email protected] | d5309265 | 2011-12-06 06:12:56 | [diff] [blame] | 629 | notification_source = content::Source<RenderViewHost>( |
| 630 | interstitial_page->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 631 | } else { |
| 632 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 633 | notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER; |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 634 | notification_source = content::Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 635 | } |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 636 | |
| 637 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 638 | browser(), ui::VKEY_TAB, false, false, false, false, |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 639 | notification_type, notification_source)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // At this point the renderer has sent us a message asking to advance the |
| 643 | // focus (as the end of the focus loop was reached in the renderer). |
| 644 | // We need to run the message loop to process it. |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 645 | content::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // Now let's try reverse focus traversal. |
| 649 | for (int i = 0; i < 2; ++i) { |
| 650 | // Location bar should be focused. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 651 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 652 | |
[email protected] | 911696b | 2011-01-28 02:36:49 | [diff] [blame] | 653 | // Move the caret to the end, otherwise the next Tab key may not move focus. |
| 654 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 655 | browser(), ui::VKEY_END, false, false, false, false)); |
| 656 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 657 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 658 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 659 | int notification_type; |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 660 | content::NotificationSource notification_source = |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 661 | content::NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 662 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 663 | notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; |
[email protected] | d5309265 | 2011-12-06 06:12:56 | [diff] [blame] | 664 | notification_source = content::Source<RenderViewHost>( |
| 665 | interstitial_page->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 666 | } else { |
| 667 | // On the last tab key press, the focus returns to the browser. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 668 | notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER; |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 669 | notification_source = content::Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 670 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 671 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 672 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 673 | browser(), ui::VKEY_TAB, false, true, false, false, |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 674 | notification_type, notification_source)); |
| 675 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 676 | // 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] | 677 | std::string actual; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 678 | ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 679 | interstitial_page->render_view_host(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 680 | "window.domAutomationController.send(getFocusedElement());", |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 681 | &actual)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 682 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 683 | } |
| 684 | |
| 685 | // At this point the renderer has sent us a message asking to advance the |
| 686 | // focus (as the end of the focus loop was reached in the renderer). |
| 687 | // We need to run the message loop to process it. |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 688 | content::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | // Focus stays on page with interstitials. |
[email protected] | cb93193 | 2011-05-03 19:57:33 | [diff] [blame] | 693 | // http://crbug.com/81451 |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 694 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 695 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 696 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 697 | |
| 698 | // First we navigate to our test page. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 699 | GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 700 | ui_test_utils::NavigateToURL(browser(), url); |
| 701 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 702 | // Page should have focus. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 703 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 704 | EXPECT_TRUE(browser()->tab_strip_model()->GetActiveWebContents()-> |
| 705 | GetRenderViewHost()->GetView()->HasFocus()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 706 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 707 | // Let's show an interstitial. |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 708 | TestInterstitialPage* interstitial_page = new TestInterstitialPage( |
| 709 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 710 | true, GURL("http://interstitial.com")); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 711 | // Give some time for the interstitial to show. |
[email protected] | 3264d16 | 2013-05-08 22:02:11 | [diff] [blame] | 712 | base::MessageLoop::current()->PostDelayedTask( |
| 713 | FROM_HERE, |
| 714 | base::MessageLoop::QuitClosure(), |
| 715 | base::TimeDelta::FromSeconds(1)); |
[email protected] | 729eb63 | 2012-07-26 04:45:26 | [diff] [blame] | 716 | content::RunMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 717 | |
| 718 | // The interstitial should have focus now. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 719 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 720 | EXPECT_TRUE(interstitial_page->HasFocus()); |
| 721 | |
| 722 | // Hide the interstitial. |
| 723 | interstitial_page->DontProceed(); |
| 724 | |
| 725 | // Focus should be back on the original page. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 726 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 727 | } |
| 728 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 729 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | 06e85dfd | 2012-10-31 01:32:35 | [diff] [blame] | 730 | // Disabled due to flakiness. http://crbug.com/67301. |
| 731 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FindFocusTest) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 732 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 733 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 734 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 735 | // Open some page (any page that doesn't steal focus). |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 736 | GURL url = embedded_test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 737 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 738 | |
[email protected] | a711df10 | 2010-12-14 09:47:03 | [diff] [blame] | 739 | EXPECT_TRUE(ChromeInForeground()); |
| 740 | |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 741 | #if defined(OS_MACOSX) |
| 742 | // Press Cmd+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 743 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 744 | browser(), ui::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 745 | #else |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 746 | // Press Ctrl+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 747 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 748 | browser(), ui::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 749 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 750 | |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 751 | ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 752 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 753 | chrome::FocusLocationBar(browser()); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 754 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 755 | |
| 756 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 757 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 758 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 759 | browser(), ui::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 760 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 761 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 762 | browser(), ui::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 763 | #endif |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 764 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 765 | |
| 766 | // Set focus to the page. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 767 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 768 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 769 | |
| 770 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 771 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 772 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 773 | browser(), ui::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 774 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 775 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 776 | browser(), ui::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 777 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 778 | |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 779 | ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 780 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 781 | |
| 782 | // Makes sure the focus is in the right location when opening the different |
| 783 | // types of tabs. |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 784 | // Flaky, http://crbug.com/62539. |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 785 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 786 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 787 | |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 788 | // Open the history tab, focus should be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 789 | chrome::ShowHistory(browser()); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 790 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 791 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 792 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 793 | |
| 794 | // Open the new tab, focus should be on the location bar. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 795 | chrome::NewTab(browser()); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 796 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 797 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 798 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 799 | |
| 800 | // Open the download tab, focus should be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 801 | chrome::ShowDownloads(browser()); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 802 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 803 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 804 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 3e3f0eb | 2009-06-22 18:33:43 | [diff] [blame] | 805 | |
| 806 | // Open about:blank, focus should be on the location bar. |
[email protected] | 081dc52 | 2013-05-15 04:59:20 | [diff] [blame] | 807 | chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL), |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 808 | content::PAGE_TRANSITION_LINK); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 809 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 810 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 811 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 812 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 813 | |
[email protected] | ddabf40 | 2013-10-22 22:43:08 | [diff] [blame^] | 814 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
| 815 | // TODO(erg): http://crbug.com/163931 |
| 816 | #define MAYBE_FocusOnReload DISABLED_FocusOnReload |
| 817 | #else |
| 818 | #define MAYBE_FocusOnReload FocusOnReload |
| 819 | #endif |
| 820 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 821 | // Tests that focus goes where expected when using reload. |
[email protected] | ddabf40 | 2013-10-22 22:43:08 | [diff] [blame^] | 822 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 823 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 824 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 825 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 826 | // Open the new tab, reload. |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 827 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 828 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 829 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 830 | content::NotificationService::AllSources()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 831 | chrome::NewTab(browser()); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 832 | observer.Wait(); |
| 833 | } |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 834 | content::RunAllPendingInMessageLoop(); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 835 | |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 836 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 837 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 838 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 839 | content::Source<NavigationController>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 840 | &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 841 | GetController())); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 842 | chrome::Reload(browser(), CURRENT_TAB); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 843 | observer.Wait(); |
| 844 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 845 | // Focus should stay on the location bar. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 846 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 847 | |
| 848 | // Open a regular page, focus the location bar, reload. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 849 | ui_test_utils::NavigateToURL(browser(), |
| 850 | embedded_test_server()->GetURL(kSimplePage)); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 851 | chrome::FocusLocationBar(browser()); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 852 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 853 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 854 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 855 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 856 | content::Source<NavigationController>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 857 | &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 858 | GetController())); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 859 | chrome::Reload(browser(), CURRENT_TAB); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 860 | observer.Wait(); |
| 861 | } |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 862 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 863 | // Focus should now be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 864 | chrome::ShowDownloads(browser()); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 865 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Tests that focus goes where expected when using reload on a crashed tab. |
[email protected] | 160f29ad | 2010-10-28 15:43:27 | [diff] [blame] | 869 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 870 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 871 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 872 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 873 | // Open a regular page, crash, reload. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 874 | ui_test_utils::NavigateToURL(browser(), |
| 875 | embedded_test_server()->GetURL(kSimplePage)); |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 876 | content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 877 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 878 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 879 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 880 | content::Source<NavigationController>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 881 | &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 882 | GetController())); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 883 | chrome::Reload(browser(), CURRENT_TAB); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 884 | observer.Wait(); |
| 885 | } |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 886 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 887 | // Focus should now be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 888 | chrome::ShowDownloads(browser()); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 889 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 890 | } |
[email protected] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 891 | |
[email protected] | 0947506 | 2013-10-21 21:29:56 | [diff] [blame] | 892 | // Tests that focus goes to frame after crashed tab. |
| 893 | // TODO(shrikant): Find out where the focus should be deterministically. |
| 894 | // Currently focused_view after crash seem to be non null in debug mode |
| 895 | // (invalidated pointer 0xcccccc). |
| 896 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) { |
| 897 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 898 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 899 | |
| 900 | content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); |
| 901 | |
| 902 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 903 | } |
| 904 | |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 905 | // Tests that when a new tab is opened from the omnibox, the focus is moved from |
| 906 | // the omnibox for the current tab. |
| 907 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, |
| 908 | NavigateFromOmniboxIntoNewTab) { |
| 909 | GURL url("http://www.google.com/"); |
| 910 | GURL url2("http://maps.google.com/"); |
| 911 | |
| 912 | // Navigate to url. |
| 913 | chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK); |
| 914 | p.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 915 | p.disposition = CURRENT_TAB; |
| 916 | chrome::Navigate(&p); |
| 917 | |
| 918 | // Focus the omnibox. |
| 919 | chrome::FocusLocationBar(browser()); |
| 920 | |
| 921 | OmniboxEditController* controller = |
| 922 | browser()->window()->GetLocationBar()->GetLocationEntry()->model()-> |
| 923 | controller(); |
| 924 | |
| 925 | // Simulate an alt-enter. |
| 926 | controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB, |
[email protected] | 04ef14d | 2013-10-04 01:01:49 | [diff] [blame] | 927 | content::PAGE_TRANSITION_TYPED); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 928 | |
| 929 | // Make sure the second tab is selected. |
[email protected] | 04e3ead5 | 2013-01-14 01:09:20 | [diff] [blame] | 930 | EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 931 | |
| 932 | // The tab contents should have the focus in the second tab. |
| 933 | EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
| 934 | |
| 935 | // Go back to the first tab. The focus should not be in the omnibox. |
| 936 | chrome::SelectPreviousTab(browser()); |
[email protected] | 04e3ead5 | 2013-01-14 01:09:20 | [diff] [blame] | 937 | EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 938 | EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 939 | } |
| 940 | |
[email protected] | ddabf40 | 2013-10-22 22:43:08 | [diff] [blame^] | 941 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
| 942 | // TODO(erg): http://crbug.com/163931 |
| 943 | #define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate |
| 944 | #else |
| 945 | #define MAYBE_FocusOnNavigate FocusOnNavigate |
| 946 | #endif |
| 947 | |
| 948 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnNavigate) { |
[email protected] | c744f7d2 | 2013-04-12 20:29:04 | [diff] [blame] | 949 | // Needed on Mac. |
| 950 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 951 | // Load the NTP. |
[email protected] | 19506d54 | 2013-10-15 23:11:06 | [diff] [blame] | 952 | ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL)); |
[email protected] | c744f7d2 | 2013-04-12 20:29:04 | [diff] [blame] | 953 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 954 | |
| 955 | // Navigate to another page. |
| 956 | const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html"); |
| 957 | GURL file_url(ui_test_utils::GetTestUrl(base::FilePath( |
| 958 | base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile))); |
| 959 | ui_test_utils::NavigateToURL(browser(), file_url); |
| 960 | |
| 961 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 962 | |
| 963 | // Navigate back. Should focus the location bar. |
| 964 | { |
| 965 | content::WindowedNotificationObserver back_nav_observer( |
| 966 | content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 967 | content::NotificationService::AllSources()); |
| 968 | chrome::GoBack(browser(), CURRENT_TAB); |
| 969 | back_nav_observer.Wait(); |
| 970 | } |
| 971 | |
| 972 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 973 | |
| 974 | // Navigate forward. Shouldn't focus the location bar. |
| 975 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 976 | { |
| 977 | content::WindowedNotificationObserver forward_nav_observer( |
| 978 | content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 979 | content::NotificationService::AllSources()); |
| 980 | chrome::GoForward(browser(), CURRENT_TAB); |
| 981 | forward_nav_observer.Wait(); |
| 982 | } |
| 983 | |
| 984 | EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 985 | } |
| 986 | |
[email protected] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 987 | } // namespace |