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