OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_commands.h" | 14 #include "chrome/browser/ui/browser_commands.h" |
16 #include "chrome/browser/ui/browser_tabstrip.h" | 15 #include "chrome/browser/ui/browser_tabstrip.h" |
17 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
18 #include "chrome/browser/ui/chrome_pages.h" | 17 #include "chrome/browser/ui/chrome_pages.h" |
| 18 #include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h" |
19 #include "chrome/browser/ui/omnibox/location_bar.h" | 19 #include "chrome/browser/ui/omnibox/location_bar.h" |
20 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" | 20 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" |
21 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" | 21 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
22 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 22 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
24 #include "chrome/browser/ui/view_ids.h" | 24 #include "chrome/browser/ui/view_ids.h" |
25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
27 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
28 #include "chrome/test/base/interactive_test_utils.h" | 28 #include "chrome/test/base/interactive_test_utils.h" |
29 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
30 #include "content/public/browser/interstitial_page.h" | 30 #include "content/public/browser/interstitial_page.h" |
31 #include "content/public/browser/interstitial_page_delegate.h" | 31 #include "content/public/browser/interstitial_page_delegate.h" |
32 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
33 #include "content/public/browser/render_view_host.h" | 33 #include "content/public/browser/render_view_host.h" |
34 #include "content/public/browser/render_widget_host_view.h" | 34 #include "content/public/browser/render_widget_host_view.h" |
35 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
36 #include "content/public/test/browser_test_utils.h" | 36 #include "content/public/test/browser_test_utils.h" |
37 #include "net/test/embedded_test_server/embedded_test_server.h" | 37 #include "net/test/embedded_test_server/embedded_test_server.h" |
38 | 38 |
39 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
40 #include <windows.h> | 40 #include "base/win/windows_version.h" |
41 #include <Psapi.h> | |
42 #include "base/strings/string_util.h" | |
43 #endif | 41 #endif |
44 | 42 |
45 using content::InterstitialPage; | |
46 using content::NavigationController; | |
47 using content::RenderViewHost; | 43 using content::RenderViewHost; |
48 using content::WebContents; | 44 using content::WebContents; |
49 | 45 |
50 #if defined(OS_MACOSX) | |
51 // TODO(suzhe): http://crbug.com/60973 | |
52 #define MAYBE_FocusTraversal DISABLED_FocusTraversal | |
53 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial | |
54 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) | |
55 // TODO(erg): http://crbug.com/163931 | |
56 #define MAYBE_FocusTraversal DISABLED_FocusTraversal | |
57 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial | |
58 #elif defined(OS_WIN) || defined(OS_CHROMEOS) | |
59 // http://crbug.com/109770 and http://crbug.com/62544 | |
60 #define MAYBE_FocusTraversal FocusTraversal | |
61 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial | |
62 #else | |
63 #define MAYBE_FocusTraversal FocusTraversal | |
64 #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial | |
65 #endif | |
66 | |
67 #if defined(OS_LINUX) || defined(OS_MACOSX) | 46 #if defined(OS_LINUX) || defined(OS_MACOSX) |
68 // TODO(jcampan): http://crbug.com/23683 for linux. | 47 // TODO(jcampan): http://crbug.com/23683 for linux. |
69 // TODO(suzhe): http://crbug.com/49737 for mac. | 48 // TODO(suzhe): http://crbug.com/49737 for mac. |
70 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage | 49 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage |
71 #elif defined(OS_WIN) | 50 #elif defined(OS_WIN) |
72 // Flaky, http://crbug.com/62537. | 51 // Flaky, http://crbug.com/62537. |
73 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage | 52 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage |
74 #endif | 53 #endif |
75 | 54 |
76 namespace { | 55 namespace { |
77 | 56 |
78 // The delay waited in some cases where we don't have a notifications for an | 57 // The delay waited in some cases where we don't have a notifications for an |
79 // action we take. | 58 // action we take. |
80 const int kActionDelayMs = 500; | 59 const int kActionDelayMs = 500; |
81 | 60 |
82 // Maxiumum time to wait until the focus is moved to expected view. | |
83 const int kFocusChangeTimeoutMs = 500; | |
84 | |
85 const char kSimplePage[] = "/focus/page_with_focus.html"; | 61 const char kSimplePage[] = "/focus/page_with_focus.html"; |
86 const char kStealFocusPage[] = "/focus/page_steals_focus.html"; | 62 const char kStealFocusPage[] = "/focus/page_steals_focus.html"; |
87 const char kTypicalPage[] = "/focus/typical_page.html"; | 63 const char kTypicalPage[] = "/focus/typical_page.html"; |
88 const char kTypicalPageName[] = "typical_page.html"; | |
89 | |
90 // Test to make sure Chrome is in the foreground as we start testing. This is | |
91 // required for tests that synthesize input to the Chrome window. | |
92 bool ChromeInForeground() { | |
93 #if defined(OS_WIN) | |
94 HWND window = ::GetForegroundWindow(); | |
95 std::wstring caption; | |
96 std::wstring filename; | |
97 int len = ::GetWindowTextLength(window) + 1; | |
98 if (len > 1) | |
99 ::GetWindowText(window, WriteInto(&caption, len), len); | |
100 bool chrome_window_in_foreground = | |
101 EndsWith(caption, L" - Google Chrome", true) || | |
102 EndsWith(caption, L" - Chromium", true); | |
103 if (!chrome_window_in_foreground) { | |
104 DWORD process_id; | |
105 int thread_id = ::GetWindowThreadProcessId(window, &process_id); | |
106 | |
107 base::ProcessHandle process; | |
108 if (base::OpenProcessHandleWithAccess(process_id, | |
109 PROCESS_QUERY_LIMITED_INFORMATION, | |
110 &process)) { | |
111 if (!GetProcessImageFileName(process, WriteInto(&filename, MAX_PATH), | |
112 MAX_PATH)) { | |
113 int error = GetLastError(); | |
114 filename = std::wstring(L"Unable to read filename for process id '" + | |
115 base::IntToString16(process_id) + | |
116 L"' (error ") + | |
117 base::IntToString16(error) + L")"; | |
118 } | |
119 base::CloseProcessHandle(process); | |
120 } | |
121 } | |
122 EXPECT_TRUE(chrome_window_in_foreground) | |
123 << "Chrome must be in the foreground when running interactive tests\n" | |
124 << "Process in foreground: " << filename.c_str() << "\n" | |
125 << "Window: " << window << "\n" | |
126 << "Caption: " << caption.c_str(); | |
127 return chrome_window_in_foreground; | |
128 #else | |
129 // Windows only at the moment. | |
130 return true; | |
131 #endif | |
132 } | |
133 | |
134 // Wait the focus change in message loop. | |
135 void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) { | |
136 if (ui_test_utils::IsViewFocused(browser, id) || | |
137 base::Time::Now() > timeout) { | |
138 base::MessageLoop::current()->PostTask(FROM_HERE, | |
139 base::MessageLoop::QuitClosure()); | |
140 } else { | |
141 base::MessageLoop::current()->PostDelayedTask( | |
142 FROM_HERE, | |
143 base::Bind(&CheckFocus, browser, id, timeout), | |
144 base::TimeDelta::FromMilliseconds(10)); | |
145 } | |
146 }; | |
147 | 64 |
148 class BrowserFocusTest : public InProcessBrowserTest { | 65 class BrowserFocusTest : public InProcessBrowserTest { |
149 public: | 66 public: |
| 67 // InProcessBrowserTest overrides: |
| 68 virtual void SetUpOnMainThread() OVERRIDE { |
| 69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 70 } |
| 71 |
150 bool IsViewFocused(ViewID vid) { | 72 bool IsViewFocused(ViewID vid) { |
151 return ui_test_utils::IsViewFocused(browser(), vid); | 73 return ui_test_utils::IsViewFocused(browser(), vid); |
152 } | 74 } |
153 | 75 |
154 void ClickOnView(ViewID vid) { | 76 void ClickOnView(ViewID vid) { |
155 ui_test_utils::ClickOnView(browser(), vid); | 77 ui_test_utils::ClickOnView(browser(), vid); |
156 } | 78 } |
157 | 79 |
158 bool WaitForFocusChange(ViewID vid) { | 80 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) { |
159 const base::Time timeout = base::Time::Now() + | 81 const char kGetFocusedElementJS[] = |
160 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs); | 82 "window.domAutomationController.send(getFocusedElement());"; |
161 base::MessageLoop::current()->PostDelayedTask( | 83 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton", |
162 FROM_HERE, | 84 "googleLink", "gmailLink", "gmapLink" }; |
163 base::Bind(&CheckFocus, browser(), vid, timeout), | 85 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse)); |
164 base::TimeDelta::FromMilliseconds(100)); | 86 ui::KeyboardCode key = ui::VKEY_TAB; |
165 content::RunMessageLoop(); | 87 #if defined(OS_MACOSX) |
166 return IsViewFocused(vid); | 88 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh... |
| 89 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB; |
| 90 #elif defined(OS_WIN) |
| 91 // This loop times out on Windows XP with no output. http://crbug.com/376635 |
| 92 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 93 return; |
| 94 #endif |
| 95 |
| 96 // Loop through the focus chain twice for good measure. |
| 97 for (size_t i = 0; i < 2; ++i) { |
| 98 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i)); |
| 99 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 100 // Mac requires an extra Tab key press to traverse the app menu button |
| 101 // iff "Full Keyboard Access" is enabled. This test code should probably |
| 102 // check the setting via NSApplication's isFullKeyboardAccessEnabled. |
| 103 |
| 104 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) { |
| 105 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j)); |
| 106 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j; |
| 107 // The details are the node's editable state, i.e. true for "textEdit". |
| 108 bool is_editable_node = index == 0; |
| 109 |
| 110 // Press Tab (or Shift+Tab) and check the focused element id. |
| 111 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( |
| 112 browser(), key, false, reverse, false, false, |
| 113 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, |
| 114 content::Source<RenderViewHost>(render_view_host), |
| 115 content::Details<bool>(&is_editable_node))); |
| 116 std::string focused_id; |
| 117 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 118 render_view_host, kGetFocusedElementJS, &focused_id)); |
| 119 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str()); |
| 120 } |
| 121 |
| 122 #if defined(OS_MACOSX) |
| 123 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and |
| 124 // would also apparently require extra Tab key presses here. Sigh... |
| 125 chrome::FocusLocationBar(browser()); |
| 126 #else |
| 127 // On the last Tab key press, focus returns to the browser. |
| 128 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 129 browser(), key, false, reverse, false, false, |
| 130 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, |
| 131 content::Source<Browser>(browser()))); |
| 132 #endif |
| 133 content::RunAllPendingInMessageLoop(); |
| 134 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 135 } |
167 } | 136 } |
168 }; | 137 }; |
169 | 138 |
| 139 // A helper class that waits for an interstitial page to attach. |
| 140 class WaitForInterstitial : public content::WebContentsObserver { |
| 141 public: |
| 142 explicit WaitForInterstitial(content::WebContents* tab) |
| 143 : WebContentsObserver(tab), |
| 144 runner_(new content::MessageLoopRunner) { |
| 145 runner_->Run(); |
| 146 } |
| 147 |
| 148 virtual void DidAttachInterstitialPage() OVERRIDE { runner_->Quit(); } |
| 149 virtual void DidDetachInterstitialPage() OVERRIDE { NOTREACHED(); } |
| 150 |
| 151 private: |
| 152 scoped_refptr<content::MessageLoopRunner> runner_; |
| 153 DISALLOW_COPY_AND_ASSIGN(WaitForInterstitial); |
| 154 }; |
| 155 |
| 156 // A test interstitial page with typical HTML contents. |
170 class TestInterstitialPage : public content::InterstitialPageDelegate { | 157 class TestInterstitialPage : public content::InterstitialPageDelegate { |
171 public: | 158 public: |
172 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) { | 159 explicit TestInterstitialPage(WebContents* tab) { |
173 base::FilePath file_path; | 160 base::FilePath file_path; |
174 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); | 161 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
175 EXPECT_TRUE(r); | 162 EXPECT_TRUE(success); |
176 file_path = file_path.AppendASCII("focus"); | 163 file_path = file_path.AppendASCII("focus/typical_page.html"); |
177 file_path = file_path.AppendASCII(kTypicalPageName); | 164 success = base::ReadFileToString(file_path, &html_contents_); |
178 r = base::ReadFileToString(file_path, &html_contents_); | 165 EXPECT_TRUE(success); |
179 EXPECT_TRUE(r); | 166 interstitial_page_ = content::InterstitialPage::Create( |
180 interstitial_page_ = InterstitialPage::Create( | 167 tab, true, GURL("http://interstitial.com"), this); |
181 tab, new_navigation, url , this); | 168 |
| 169 // Show the interstitial and delay return until it has attached. |
182 interstitial_page_->Show(); | 170 interstitial_page_->Show(); |
| 171 WaitForInterstitial wait(tab); |
| 172 EXPECT_TRUE(tab->ShowingInterstitialPage()); |
183 } | 173 } |
184 | 174 |
185 virtual std::string GetHTMLContents() OVERRIDE { | 175 virtual std::string GetHTMLContents() OVERRIDE { return html_contents_; } |
186 return html_contents_; | |
187 } | |
188 | 176 |
189 RenderViewHost* render_view_host() { | 177 RenderViewHost* render_view_host() { |
190 return interstitial_page_->GetRenderViewHostForTesting(); | 178 return interstitial_page_->GetRenderViewHostForTesting(); |
191 } | 179 } |
192 | 180 |
193 void DontProceed() { | 181 void DontProceed() { interstitial_page_->DontProceed(); } |
194 interstitial_page_->DontProceed(); | |
195 } | |
196 | 182 |
197 bool HasFocus() { | 183 bool HasFocus() { return render_view_host()->GetView()->HasFocus(); } |
198 return render_view_host()->GetView()->HasFocus(); | |
199 } | |
200 | 184 |
201 private: | 185 private: |
202 std::string html_contents_; | 186 std::string html_contents_; |
203 InterstitialPage* interstitial_page_; // Owns us. | 187 content::InterstitialPage* interstitial_page_; // Owns this. |
| 188 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage); |
204 }; | 189 }; |
205 | 190 |
206 // Flaky on mac. http://crbug.com/67301. | 191 // Flaky on mac. http://crbug.com/67301. |
207 #if defined(OS_MACOSX) | 192 #if defined(OS_MACOSX) |
208 #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus | 193 #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus |
209 #else | 194 #else |
210 #define MAYBE_ClickingMovesFocus ClickingMovesFocus | 195 #define MAYBE_ClickingMovesFocus ClickingMovesFocus |
211 #endif | 196 #endif |
212 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) { | 197 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) { |
213 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 198 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
(...skipping 12 matching lines...) Expand all Loading... |
226 ClickOnView(VIEW_ID_TAB_CONTAINER); | 211 ClickOnView(VIEW_ID_TAB_CONTAINER); |
227 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 212 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
228 | 213 |
229 ClickOnView(VIEW_ID_OMNIBOX); | 214 ClickOnView(VIEW_ID_OMNIBOX); |
230 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | 215 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
231 } | 216 } |
232 | 217 |
233 // Flaky, http://crbug.com/69034. | 218 // Flaky, http://crbug.com/69034. |
234 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { | 219 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { |
235 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 220 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
236 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 221 const GURL url = embedded_test_server()->GetURL(kSimplePage); |
237 | |
238 // First we navigate to our test page. | |
239 GURL url = embedded_test_server()->GetURL(kSimplePage); | |
240 ui_test_utils::NavigateToURL(browser(), url); | 222 ui_test_utils::NavigateToURL(browser(), url); |
241 | 223 |
242 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); | 224 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); |
243 | 225 |
244 // The focus should be on the Tab contents. | 226 // The focus should be on the Tab contents. |
245 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 227 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
246 // Now hide the window, show it again, the focus should not have changed. | 228 // Now hide the window, show it again, the focus should not have changed. |
247 ui_test_utils::HideNativeWindow(window); | 229 ui_test_utils::HideNativeWindow(window); |
248 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); | 230 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
249 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 231 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
250 | 232 |
251 chrome::FocusLocationBar(browser()); | 233 chrome::FocusLocationBar(browser()); |
252 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | 234 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
253 // Hide the window, show it again, the focus should not have changed. | 235 // Hide the window, show it again, the focus should not have changed. |
254 ui_test_utils::HideNativeWindow(window); | 236 ui_test_utils::HideNativeWindow(window); |
255 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); | 237 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
256 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | 238 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
257 } | 239 } |
258 | 240 |
259 // Tabs remember focus. | 241 // Tabs remember focus. |
260 // Disabled, http://crbug.com/62542. | 242 // Disabled, http://crbug.com/62542. |
261 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) { | 243 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) { |
262 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 244 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
263 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 245 const GURL url = embedded_test_server()->GetURL(kSimplePage); |
264 | |
265 // First we navigate to our test page. | |
266 GURL url = embedded_test_server()->GetURL(kSimplePage); | |
267 ui_test_utils::NavigateToURL(browser(), url); | 246 ui_test_utils::NavigateToURL(browser(), url); |
268 | 247 |
269 // Create several tabs. | 248 // Create several tabs. |
270 for (int i = 0; i < 4; ++i) { | 249 for (int i = 0; i < 4; ++i) { |
271 chrome::AddSelectedTabWithURL(browser(), url, | 250 chrome::AddSelectedTabWithURL(browser(), url, |
272 content::PAGE_TRANSITION_TYPED); | 251 content::PAGE_TRANSITION_TYPED); |
273 } | 252 } |
274 | 253 |
275 // Alternate focus for the tab. | 254 // Alternate focus for the tab. |
276 const bool kFocusPage[3][5] = { | 255 const bool kFocusPage[3][5] = { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 | 301 |
323 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 302 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
324 browser(), ui::VKEY_TAB, true, true, false, false)); | 303 browser(), ui::VKEY_TAB, true, true, false, false)); |
325 } | 304 } |
326 } | 305 } |
327 } | 306 } |
328 | 307 |
329 // Tabs remember focus with find-in-page box. | 308 // Tabs remember focus with find-in-page box. |
330 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { | 309 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
331 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 310 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
332 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 311 const GURL url = embedded_test_server()->GetURL(kSimplePage); |
333 | |
334 // First we navigate to our test page. | |
335 GURL url = embedded_test_server()->GetURL(kSimplePage); | |
336 ui_test_utils::NavigateToURL(browser(), url); | 312 ui_test_utils::NavigateToURL(browser(), url); |
337 | 313 |
338 chrome::Find(browser()); | 314 chrome::Find(browser()); |
339 ui_test_utils::FindInPage( | 315 ui_test_utils::FindInPage( |
340 browser()->tab_strip_model()->GetActiveWebContents(), | 316 browser()->tab_strip_model()->GetActiveWebContents(), |
341 base::ASCIIToUTF16("a"), true, false, NULL, NULL); | 317 base::ASCIIToUTF16("a"), true, false, NULL, NULL); |
342 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 318 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
343 | 319 |
344 // Focus the location bar. | 320 // Focus the location bar. |
345 chrome::FocusLocationBar(browser()); | 321 chrome::FocusLocationBar(browser()); |
(...skipping 17 matching lines...) Expand all Loading... |
363 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 339 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
364 browser()->tab_strip_model()->ActivateTabAt(0, true); | 340 browser()->tab_strip_model()->ActivateTabAt(0, true); |
365 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 341 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
366 } | 342 } |
367 | 343 |
368 // Background window does not steal focus. | 344 // Background window does not steal focus. |
369 // Flaky, http://crbug.com/62538. | 345 // Flaky, http://crbug.com/62538. |
370 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, | 346 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, |
371 DISABLED_BackgroundBrowserDontStealFocus) { | 347 DISABLED_BackgroundBrowserDontStealFocus) { |
372 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 348 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
373 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
374 | 349 |
375 // Open a new browser window. | 350 // Open a new browser window. |
376 Browser* browser2 = | 351 Browser* browser2 = |
377 new Browser(Browser::CreateParams(browser()->profile(), | 352 new Browser(Browser::CreateParams(browser()->profile(), |
378 browser()->host_desktop_type())); | 353 browser()->host_desktop_type())); |
379 ASSERT_TRUE(browser2); | 354 ASSERT_TRUE(browser2); |
380 chrome::AddTabAt(browser2, GURL(), -1, true); | 355 chrome::AddTabAt(browser2, GURL(), -1, true); |
381 browser2->window()->Show(); | 356 browser2->window()->Show(); |
382 | 357 |
383 Browser* focused_browser = NULL; | 358 Browser* focused_browser = NULL; |
(...skipping 12 matching lines...) Expand all Loading... |
396 } | 371 } |
397 #elif defined(OS_WIN) | 372 #elif defined(OS_WIN) |
398 focused_browser = browser(); | 373 focused_browser = browser(); |
399 unfocused_browser = browser2; | 374 unfocused_browser = browser2; |
400 #elif defined(OS_MACOSX) | 375 #elif defined(OS_MACOSX) |
401 // On Mac, the newly created window always gets the focus. | 376 // On Mac, the newly created window always gets the focus. |
402 focused_browser = browser2; | 377 focused_browser = browser2; |
403 unfocused_browser = browser(); | 378 unfocused_browser = browser(); |
404 #endif | 379 #endif |
405 | 380 |
406 GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage); | 381 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage); |
407 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); | 382 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
408 | 383 |
409 // Activate the first browser. | 384 // Activate the first browser. |
410 focused_browser->window()->Activate(); | 385 focused_browser->window()->Activate(); |
411 | 386 |
412 ASSERT_TRUE(content::ExecuteScript( | 387 ASSERT_TRUE(content::ExecuteScript( |
413 unfocused_browser->tab_strip_model()->GetActiveWebContents(), | 388 unfocused_browser->tab_strip_model()->GetActiveWebContents(), |
414 "stealFocus();")); | 389 "stealFocus();")); |
415 | 390 |
416 // Make sure the first browser is still active. | 391 // Make sure the first browser is still active. |
417 EXPECT_TRUE(focused_browser->window()->IsActive()); | 392 EXPECT_TRUE(focused_browser->window()->IsActive()); |
418 } | 393 } |
419 | 394 |
420 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) | 395 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
421 // TODO(erg): http://crbug.com/163931 | 396 // TODO(erg): http://crbug.com/163931 |
422 #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus | 397 #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus |
423 #else | 398 #else |
424 #define MAYBE_LocationBarLockFocus LocationBarLockFocus | 399 #define MAYBE_LocationBarLockFocus LocationBarLockFocus |
425 #endif | 400 #endif |
426 | 401 |
427 // Page cannot steal focus when focus is on location bar. | 402 // Page cannot steal focus when focus is on location bar. |
428 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) { | 403 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) { |
429 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 404 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
430 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
431 | 405 |
432 // Open the page that steals focus. | 406 // Open the page that steals focus. |
433 GURL url = embedded_test_server()->GetURL(kStealFocusPage); | 407 const GURL url = embedded_test_server()->GetURL(kStealFocusPage); |
434 ui_test_utils::NavigateToURL(browser(), url); | 408 ui_test_utils::NavigateToURL(browser(), url); |
435 | 409 |
436 chrome::FocusLocationBar(browser()); | 410 chrome::FocusLocationBar(browser()); |
437 | 411 |
438 ASSERT_TRUE(content::ExecuteScript( | 412 ASSERT_TRUE(content::ExecuteScript( |
439 browser()->tab_strip_model()->GetActiveWebContents(), | 413 browser()->tab_strip_model()->GetActiveWebContents(), |
440 "stealFocus();")); | 414 "stealFocus();")); |
441 | 415 |
442 // Make sure the location bar is still focused. | 416 // Make sure the location bar is still focused. |
443 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | 417 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
444 } | 418 } |
445 | 419 |
446 // Focus traversal on a regular page. | 420 // Test forward and reverse focus traversal on a typical page. |
447 // Note that this test relies on a notification from the renderer that the | 421 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { |
448 // focus has changed in the page. The notification in the renderer may change | |
449 // at which point this test would fail (see comment in | |
450 // RenderWidget::didFocus()). | |
451 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { | |
452 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 422 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
453 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 423 const GURL url = embedded_test_server()->GetURL(kTypicalPage); |
| 424 ui_test_utils::NavigateToURL(browser(), url); |
| 425 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 426 chrome::FocusLocationBar(browser()); |
454 | 427 |
455 // First we navigate to our test page. | 428 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); |
456 GURL url = embedded_test_server()->GetURL(kTypicalPage); | 429 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false)); |
| 430 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true)); |
| 431 } |
| 432 |
| 433 #if defined(OS_MACOSX) |
| 434 // TODO(msw): Mac's details wrongly claim that the "textEdit" is not editable on |
| 435 // the 2nd forwards traveral loop (but is correct in the 1st loop). Mac also |
| 436 // doesn't advance focus on the 2nd reverse Tab press (gmapLink->gmailLink), but |
| 437 // the 1st and 3rd+ Tab key presses seem to work. http://crbug.com/60973 |
| 438 #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
| 439 #else |
| 440 #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
| 441 #endif |
| 442 |
| 443 // Test forward and reverse focus traversal while an interstitial is showing. |
| 444 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
| 445 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 446 const GURL url = embedded_test_server()->GetURL(kSimplePage); |
457 ui_test_utils::NavigateToURL(browser(), url); | 447 ui_test_utils::NavigateToURL(browser(), url); |
| 448 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 449 |
| 450 // Create and show a test interstitial page. |
| 451 TestInterstitialPage* interstitial_page = new TestInterstitialPage( |
| 452 browser()->tab_strip_model()->GetActiveWebContents()); |
| 453 content::RenderViewHost* host = interstitial_page->render_view_host(); |
| 454 |
| 455 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 456 chrome::FocusLocationBar(browser()); |
| 457 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false)); |
| 458 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true)); |
| 459 } |
| 460 |
| 461 // Test the transfer of focus when an interstitial is shown and hidden. |
| 462 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
| 463 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 464 const GURL url = embedded_test_server()->GetURL(kSimplePage); |
| 465 ui_test_utils::NavigateToURL(browser(), url); |
| 466 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); |
| 467 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 468 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus()); |
| 469 |
| 470 // Create and show a test interstitial page; it should gain focus. |
| 471 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab); |
| 472 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 473 EXPECT_TRUE(interstitial_page->HasFocus()); |
| 474 |
| 475 // Hide the interstitial; the original page should gain focus. |
| 476 interstitial_page->DontProceed(); |
| 477 content::RunAllPendingInMessageLoop(); |
| 478 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 479 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus()); |
| 480 } |
| 481 |
| 482 // Test that find-in-page UI can request focus, even when it is already open. |
| 483 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
| 484 chrome::DisableFindBarAnimationsDuringTesting(true); |
| 485 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 486 const GURL url = embedded_test_server()->GetURL(kTypicalPage); |
| 487 ui_test_utils::NavigateToURL(browser(), url); |
| 488 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 489 |
| 490 chrome::ShowFindBar(browser()); |
| 491 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
458 | 492 |
459 chrome::FocusLocationBar(browser()); | 493 chrome::FocusLocationBar(browser()); |
| 494 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
460 | 495 |
461 const char* kTextElementID = "textEdit"; | 496 chrome::ShowFindBar(browser()); |
462 const char* kExpElementIDs[] = { | 497 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
463 "", // Initially no element in the page should be focused | |
464 // (the location bar is focused). | |
465 kTextElementID, "searchButton", "luckyButton", "googleLink", "gmailLink", | |
466 "gmapLink" | |
467 }; | |
468 | 498 |
469 // Test forward focus traversal. | 499 ClickOnView(VIEW_ID_TAB_CONTAINER); |
470 for (int i = 0; i < 3; ++i) { | 500 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
471 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); | |
472 // Location bar should be focused. | |
473 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | |
474 | 501 |
475 // Move the caret to the end, otherwise the next Tab key may not move focus. | 502 chrome::ShowFindBar(browser()); |
476 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 503 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
477 browser(), ui::VKEY_END, false, false, false, false)); | |
478 | |
479 // Now let's press tab to move the focus. | |
480 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { | |
481 SCOPED_TRACE(base::StringPrintf("inner loop %" PRIuS, j)); | |
482 // Let's make sure the focus is on the expected element in the page. | |
483 std::string actual; | |
484 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
485 browser()->tab_strip_model()->GetActiveWebContents(), | |
486 "window.domAutomationController.send(getFocusedElement());", | |
487 &actual)); | |
488 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | |
489 | |
490 if (j < arraysize(kExpElementIDs) - 1) { | |
491 // If the next element is the kTextElementID, we expect to be | |
492 // notified we have switched to an editable node. | |
493 bool is_editable_node = | |
494 (strcmp(kTextElementID, kExpElementIDs[j + 1]) == 0); | |
495 content::Details<bool> details(&is_editable_node); | |
496 | |
497 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( | |
498 browser(), ui::VKEY_TAB, false, false, false, false, | |
499 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, | |
500 content::NotificationSource(content::Source<RenderViewHost>( | |
501 browser()->tab_strip_model()->GetActiveWebContents()-> | |
502 GetRenderViewHost())), | |
503 details)); | |
504 } else { | |
505 // On the last tab key press, the focus returns to the browser. | |
506 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
507 browser(), ui::VKEY_TAB, false, false, false, false, | |
508 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, | |
509 content::NotificationSource(content::Source<Browser>(browser())))); | |
510 } | |
511 } | |
512 | |
513 // At this point the renderer has sent us a message asking to advance the | |
514 // focus (as the end of the focus loop was reached in the renderer). | |
515 // We need to run the message loop to process it. | |
516 content::RunAllPendingInMessageLoop(); | |
517 } | |
518 | |
519 // Now let's try reverse focus traversal. | |
520 for (int i = 0; i < 3; ++i) { | |
521 SCOPED_TRACE(base::StringPrintf("outer loop: %d", i)); | |
522 // Location bar should be focused. | |
523 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | |
524 | |
525 // Move the caret to the end, otherwise the next Tab key may not move focus. | |
526 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
527 browser(), ui::VKEY_END, false, false, false, false)); | |
528 | |
529 // Now let's press shift-tab to move the focus in reverse. | |
530 for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { | |
531 SCOPED_TRACE(base::StringPrintf("inner loop: %" PRIuS, j)); | |
532 const char* next_element = | |
533 kExpElementIDs[arraysize(kExpElementIDs) - 1 - j]; | |
534 | |
535 if (j < arraysize(kExpElementIDs) - 1) { | |
536 // If the next element is the kTextElementID, we expect to be | |
537 // notified we have switched to an editable node. | |
538 bool is_editable_node = (strcmp(kTextElementID, next_element) == 0); | |
539 content::Details<bool> details(&is_editable_node); | |
540 | |
541 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails( | |
542 browser(), ui::VKEY_TAB, false, true, false, false, | |
543 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, | |
544 content::NotificationSource(content::Source<RenderViewHost>( | |
545 browser()->tab_strip_model()->GetActiveWebContents()-> | |
546 GetRenderViewHost())), | |
547 details)); | |
548 } else { | |
549 // On the last tab key press, the focus returns to the browser. | |
550 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
551 browser(), ui::VKEY_TAB, false, true, false, false, | |
552 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER, | |
553 content::NotificationSource(content::Source<Browser>(browser())))); | |
554 } | |
555 | |
556 // Let's make sure the focus is on the expected element in the page. | |
557 std::string actual; | |
558 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
559 browser()->tab_strip_model()->GetActiveWebContents(), | |
560 "window.domAutomationController.send(getFocusedElement());", | |
561 &actual)); | |
562 ASSERT_STREQ(next_element, actual.c_str()); | |
563 } | |
564 | |
565 // At this point the renderer has sent us a message asking to advance the | |
566 // focus (as the end of the focus loop was reached in the renderer). | |
567 // We need to run the message loop to process it. | |
568 content::RunAllPendingInMessageLoop(); | |
569 } | |
570 } | |
571 | |
572 // Focus traversal while an interstitial is showing. | |
573 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { | |
574 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
575 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
576 | |
577 // First we navigate to our test page. | |
578 GURL url = embedded_test_server()->GetURL(kSimplePage); | |
579 ui_test_utils::NavigateToURL(browser(), url); | |
580 | |
581 // Focus should be on the page. | |
582 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | |
583 | |
584 // Let's show an interstitial. | |
585 TestInterstitialPage* interstitial_page = new TestInterstitialPage( | |
586 browser()->tab_strip_model()->GetActiveWebContents(), | |
587 true, GURL("http://interstitial.com")); | |
588 // Give some time for the interstitial to show. | |
589 base::MessageLoop::current()->PostDelayedTask( | |
590 FROM_HERE, | |
591 base::MessageLoop::QuitClosure(), | |
592 base::TimeDelta::FromSeconds(1)); | |
593 content::RunMessageLoop(); | |
594 | |
595 chrome::FocusLocationBar(browser()); | |
596 | |
597 const char* kExpElementIDs[] = { | |
598 "", // Initially no element in the page should be focused | |
599 // (the location bar is focused). | |
600 "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", | |
601 "gmapLink" | |
602 }; | |
603 | |
604 // Test forward focus traversal. | |
605 for (int i = 0; i < 2; ++i) { | |
606 // Location bar should be focused. | |
607 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | |
608 | |
609 // Move the caret to the end, otherwise the next Tab key may not move focus. | |
610 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
611 browser(), ui::VKEY_END, false, false, false, false)); | |
612 | |
613 // Now let's press tab to move the focus. | |
614 for (size_t j = 0; j < 7; ++j) { | |
615 // Let's make sure the focus is on the expected element in the page. | |
616 std::string actual; | |
617 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
618 interstitial_page->render_view_host(), | |
619 "window.domAutomationController.send(getFocusedElement());", | |
620 &actual)); | |
621 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | |
622 | |
623 int notification_type; | |
624 content::NotificationSource notification_source = | |
625 content::NotificationService::AllSources(); | |
626 if (j < arraysize(kExpElementIDs) - 1) { | |
627 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; | |
628 notification_source = content::Source<RenderViewHost>( | |
629 interstitial_page->render_view_host()); | |
630 } else { | |
631 // On the last tab key press, the focus returns to the browser. | |
632 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER; | |
633 notification_source = content::Source<Browser>(browser()); | |
634 } | |
635 | |
636 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
637 browser(), ui::VKEY_TAB, false, false, false, false, | |
638 notification_type, notification_source)); | |
639 } | |
640 | |
641 // At this point the renderer has sent us a message asking to advance the | |
642 // focus (as the end of the focus loop was reached in the renderer). | |
643 // We need to run the message loop to process it. | |
644 content::RunAllPendingInMessageLoop(); | |
645 } | |
646 | |
647 // Now let's try reverse focus traversal. | |
648 for (int i = 0; i < 2; ++i) { | |
649 // Location bar should be focused. | |
650 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | |
651 | |
652 // Move the caret to the end, otherwise the next Tab key may not move focus. | |
653 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
654 browser(), ui::VKEY_END, false, false, false, false)); | |
655 | |
656 // Now let's press shift-tab to move the focus in reverse. | |
657 for (size_t j = 0; j < 7; ++j) { | |
658 int notification_type; | |
659 content::NotificationSource notification_source = | |
660 content::NotificationService::AllSources(); | |
661 if (j < arraysize(kExpElementIDs) - 1) { | |
662 notification_type = content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE; | |
663 notification_source = content::Source<RenderViewHost>( | |
664 interstitial_page->render_view_host()); | |
665 } else { | |
666 // On the last tab key press, the focus returns to the browser. | |
667 notification_type = chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER; | |
668 notification_source = content::Source<Browser>(browser()); | |
669 } | |
670 | |
671 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
672 browser(), ui::VKEY_TAB, false, true, false, false, | |
673 notification_type, notification_source)); | |
674 | |
675 // Let's make sure the focus is on the expected element in the page. | |
676 std::string actual; | |
677 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
678 interstitial_page->render_view_host(), | |
679 "window.domAutomationController.send(getFocusedElement());", | |
680 &actual)); | |
681 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | |
682 } | |
683 | |
684 // At this point the renderer has sent us a message asking to advance the | |
685 // focus (as the end of the focus loop was reached in the renderer). | |
686 // We need to run the message loop to process it. | |
687 content::RunAllPendingInMessageLoop(); | |
688 } | |
689 } | |
690 | |
691 // Focus stays on page with interstitials. | |
692 // http://crbug.com/81451 | |
693 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_InterstitialFocus) { | |
694 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
695 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
696 | |
697 // First we navigate to our test page. | |
698 GURL url = embedded_test_server()->GetURL(kSimplePage); | |
699 ui_test_utils::NavigateToURL(browser(), url); | |
700 | |
701 // Page should have focus. | |
702 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | |
703 EXPECT_TRUE(browser()->tab_strip_model()->GetActiveWebContents()-> | |
704 GetRenderViewHost()->GetView()->HasFocus()); | |
705 | |
706 // Let's show an interstitial. | |
707 TestInterstitialPage* interstitial_page = new TestInterstitialPage( | |
708 browser()->tab_strip_model()->GetActiveWebContents(), | |
709 true, GURL("http://interstitial.com")); | |
710 // Give some time for the interstitial to show. | |
711 base::MessageLoop::current()->PostDelayedTask( | |
712 FROM_HERE, | |
713 base::MessageLoop::QuitClosure(), | |
714 base::TimeDelta::FromSeconds(1)); | |
715 content::RunMessageLoop(); | |
716 | |
717 // The interstitial should have focus now. | |
718 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | |
719 EXPECT_TRUE(interstitial_page->HasFocus()); | |
720 | |
721 // Hide the interstitial. | |
722 interstitial_page->DontProceed(); | |
723 | |
724 // Focus should be back on the original page. | |
725 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | |
726 } | |
727 | |
728 // Make sure Find box can request focus, even when it is already open. | |
729 // Disabled due to flakiness. http://crbug.com/67301. | |
730 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FindFocusTest) { | |
731 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
732 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
733 | |
734 // Open some page (any page that doesn't steal focus). | |
735 GURL url = embedded_test_server()->GetURL(kTypicalPage); | |
736 ui_test_utils::NavigateToURL(browser(), url); | |
737 | |
738 EXPECT_TRUE(ChromeInForeground()); | |
739 | |
740 #if defined(OS_MACOSX) | |
741 // Press Cmd+F, which will make the Find box open and request focus. | |
742 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
743 browser(), ui::VKEY_F, false, false, false, true)); | |
744 #else | |
745 // Press Ctrl+F, which will make the Find box open and request focus. | |
746 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
747 browser(), ui::VKEY_F, true, false, false, false)); | |
748 #endif | |
749 | |
750 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | |
751 | |
752 chrome::FocusLocationBar(browser()); | |
753 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | |
754 | |
755 // Now press Ctrl+F again and focus should move to the Find box. | |
756 #if defined(OS_MACOSX) | |
757 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
758 browser(), ui::VKEY_F, false, false, false, true)); | |
759 #else | |
760 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
761 browser(), ui::VKEY_F, true, false, false, false)); | |
762 #endif | |
763 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | |
764 | |
765 // Set focus to the page. | |
766 ClickOnView(VIEW_ID_TAB_CONTAINER); | |
767 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | |
768 | |
769 // Now press Ctrl+F again and focus should move to the Find box. | |
770 #if defined(OS_MACOSX) | |
771 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
772 browser(), ui::VKEY_F, false, false, false, true)); | |
773 #else | |
774 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | |
775 browser(), ui::VKEY_F, true, false, false, false)); | |
776 #endif | |
777 | |
778 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | |
779 } | 504 } |
780 | 505 |
781 // Makes sure the focus is in the right location when opening the different | 506 // Makes sure the focus is in the right location when opening the different |
782 // types of tabs. | 507 // types of tabs. |
783 // Flaky, http://crbug.com/62539. | 508 // Flaky, http://crbug.com/62539. |
784 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) { | 509 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) { |
785 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 510 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
786 | 511 |
787 // Open the history tab, focus should be on the tab contents. | 512 // Open the history tab, focus should be on the tab contents. |
788 chrome::ShowHistory(browser()); | 513 chrome::ShowHistory(browser()); |
(...skipping 24 matching lines...) Expand all Loading... |
813 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) | 538 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
814 // TODO(erg): http://crbug.com/163931 | 539 // TODO(erg): http://crbug.com/163931 |
815 #define MAYBE_FocusOnReload DISABLED_FocusOnReload | 540 #define MAYBE_FocusOnReload DISABLED_FocusOnReload |
816 #else | 541 #else |
817 #define MAYBE_FocusOnReload FocusOnReload | 542 #define MAYBE_FocusOnReload FocusOnReload |
818 #endif | 543 #endif |
819 | 544 |
820 // Tests that focus goes where expected when using reload. | 545 // Tests that focus goes where expected when using reload. |
821 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) { | 546 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) { |
822 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 547 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
823 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
824 | 548 |
825 // Open the new tab, reload. | 549 // Open the new tab, reload. |
826 { | 550 { |
827 content::WindowedNotificationObserver observer( | 551 content::WindowedNotificationObserver observer( |
828 content::NOTIFICATION_LOAD_STOP, | 552 content::NOTIFICATION_LOAD_STOP, |
829 content::NotificationService::AllSources()); | 553 content::NotificationService::AllSources()); |
830 chrome::NewTab(browser()); | 554 chrome::NewTab(browser()); |
831 observer.Wait(); | 555 observer.Wait(); |
832 } | 556 } |
833 content::RunAllPendingInMessageLoop(); | 557 content::RunAllPendingInMessageLoop(); |
834 | 558 |
835 { | 559 { |
836 content::WindowedNotificationObserver observer( | 560 content::WindowedNotificationObserver observer( |
837 content::NOTIFICATION_LOAD_STOP, | 561 content::NOTIFICATION_LOAD_STOP, |
838 content::Source<NavigationController>( | 562 content::Source<content::NavigationController>( |
839 &browser()->tab_strip_model()->GetActiveWebContents()-> | 563 &browser()->tab_strip_model()->GetActiveWebContents()-> |
840 GetController())); | 564 GetController())); |
841 chrome::Reload(browser(), CURRENT_TAB); | 565 chrome::Reload(browser(), CURRENT_TAB); |
842 observer.Wait(); | 566 observer.Wait(); |
843 } | 567 } |
844 // Focus should stay on the location bar. | 568 // Focus should stay on the location bar. |
845 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | 569 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
846 | 570 |
847 // Open a regular page, focus the location bar, reload. | 571 // Open a regular page, focus the location bar, reload. |
848 ui_test_utils::NavigateToURL(browser(), | 572 ui_test_utils::NavigateToURL(browser(), |
849 embedded_test_server()->GetURL(kSimplePage)); | 573 embedded_test_server()->GetURL(kSimplePage)); |
850 chrome::FocusLocationBar(browser()); | 574 chrome::FocusLocationBar(browser()); |
851 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); | 575 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
852 { | 576 { |
853 content::WindowedNotificationObserver observer( | 577 content::WindowedNotificationObserver observer( |
854 content::NOTIFICATION_LOAD_STOP, | 578 content::NOTIFICATION_LOAD_STOP, |
855 content::Source<NavigationController>( | 579 content::Source<content::NavigationController>( |
856 &browser()->tab_strip_model()->GetActiveWebContents()-> | 580 &browser()->tab_strip_model()->GetActiveWebContents()-> |
857 GetController())); | 581 GetController())); |
858 chrome::Reload(browser(), CURRENT_TAB); | 582 chrome::Reload(browser(), CURRENT_TAB); |
859 observer.Wait(); | 583 observer.Wait(); |
860 } | 584 } |
861 | 585 |
862 // Focus should now be on the tab contents. | 586 // Focus should now be on the tab contents. |
863 chrome::ShowDownloads(browser()); | 587 chrome::ShowDownloads(browser()); |
864 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 588 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
865 } | 589 } |
866 | 590 |
867 // Tests that focus goes where expected when using reload on a crashed tab. | 591 // Tests that focus goes where expected when using reload on a crashed tab. |
868 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) { | 592 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) { |
869 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 593 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
870 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
871 | 594 |
872 // Open a regular page, crash, reload. | 595 // Open a regular page, crash, reload. |
873 ui_test_utils::NavigateToURL(browser(), | 596 ui_test_utils::NavigateToURL(browser(), |
874 embedded_test_server()->GetURL(kSimplePage)); | 597 embedded_test_server()->GetURL(kSimplePage)); |
875 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); | 598 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); |
876 { | 599 { |
877 content::WindowedNotificationObserver observer( | 600 content::WindowedNotificationObserver observer( |
878 content::NOTIFICATION_LOAD_STOP, | 601 content::NOTIFICATION_LOAD_STOP, |
879 content::Source<NavigationController>( | 602 content::Source<content::NavigationController>( |
880 &browser()->tab_strip_model()->GetActiveWebContents()-> | 603 &browser()->tab_strip_model()->GetActiveWebContents()-> |
881 GetController())); | 604 GetController())); |
882 chrome::Reload(browser(), CURRENT_TAB); | 605 chrome::Reload(browser(), CURRENT_TAB); |
883 observer.Wait(); | 606 observer.Wait(); |
884 } | 607 } |
885 | 608 |
886 // Focus should now be on the tab contents. | 609 // Focus should now be on the tab contents. |
887 chrome::ShowDownloads(browser()); | 610 chrome::ShowDownloads(browser()); |
888 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 611 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
889 } | 612 } |
890 | 613 |
891 // Tests that focus goes to frame after crashed tab. | 614 // Tests that focus goes to frame after crashed tab. |
892 // TODO(shrikant): Find out where the focus should be deterministically. | 615 // TODO(shrikant): Find out where the focus should be deterministically. |
893 // Currently focused_view after crash seem to be non null in debug mode | 616 // Currently focused_view after crash seem to be non null in debug mode |
894 // (invalidated pointer 0xcccccc). | 617 // (invalidated pointer 0xcccccc). |
895 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) { | 618 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) { |
896 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 619 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
897 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
898 | 620 |
899 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); | 621 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); |
900 | 622 |
901 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 623 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
902 } | 624 } |
903 | 625 |
904 // Tests that when a new tab is opened from the omnibox, the focus is moved from | 626 // Tests that when a new tab is opened from the omnibox, the focus is moved from |
905 // the omnibox for the current tab. | 627 // the omnibox for the current tab. |
906 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, | 628 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) { |
907 NavigateFromOmniboxIntoNewTab) { | |
908 GURL url("http://www.google.com/"); | 629 GURL url("http://www.google.com/"); |
909 GURL url2("http://maps.google.com/"); | 630 GURL url2("http://maps.google.com/"); |
910 | 631 |
911 // Navigate to url. | 632 // Navigate to url. |
912 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK); | 633 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK); |
913 p.window_action = chrome::NavigateParams::SHOW_WINDOW; | 634 p.window_action = chrome::NavigateParams::SHOW_WINDOW; |
914 p.disposition = CURRENT_TAB; | 635 p.disposition = CURRENT_TAB; |
915 chrome::Navigate(&p); | 636 chrome::Navigate(&p); |
916 | 637 |
917 // Focus the omnibox. | 638 // Focus the omnibox. |
918 chrome::FocusLocationBar(browser()); | 639 chrome::FocusLocationBar(browser()); |
919 | 640 |
920 OmniboxEditController* controller = | 641 OmniboxEditController* controller = browser()->window()->GetLocationBar()-> |
921 browser()->window()->GetLocationBar()->GetOmniboxView()->model()-> | 642 GetOmniboxView()->model()->controller(); |
922 controller(); | |
923 | 643 |
924 // Simulate an alt-enter. | 644 // Simulate an alt-enter. |
925 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB, | 645 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB, |
926 content::PAGE_TRANSITION_TYPED); | 646 content::PAGE_TRANSITION_TYPED); |
927 | 647 |
928 // Make sure the second tab is selected. | 648 // Make sure the second tab is selected. |
929 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); | 649 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
930 | 650 |
931 // The tab contents should have the focus in the second tab. | 651 // The tab contents should have the focus in the second tab. |
932 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | 652 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
933 | 653 |
934 // Go back to the first tab. The focus should not be in the omnibox. | 654 // Go back to the first tab. The focus should not be in the omnibox. |
935 chrome::SelectPreviousTab(browser()); | 655 chrome::SelectPreviousTab(browser()); |
936 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); | 656 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
937 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 657 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX)); |
938 } | 658 } |
939 | 659 |
940 // This functionality is currently broken. http://crbug.com/304865. | 660 // This functionality is currently broken. http://crbug.com/304865. |
941 // | 661 // |
942 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) | 662 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) |
943 //// TODO(erg): http://crbug.com/163931 | 663 //// TODO(erg): http://crbug.com/163931 |
944 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate | 664 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate |
945 //#else | 665 //#else |
946 //#define MAYBE_FocusOnNavigate FocusOnNavigate | 666 //#define MAYBE_FocusOnNavigate FocusOnNavigate |
947 //#endif | 667 //#endif |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
979 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 699 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
980 content::NotificationService::AllSources()); | 700 content::NotificationService::AllSources()); |
981 chrome::GoForward(browser(), CURRENT_TAB); | 701 chrome::GoForward(browser(), CURRENT_TAB); |
982 forward_nav_observer.Wait(); | 702 forward_nav_observer.Wait(); |
983 } | 703 } |
984 | 704 |
985 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX)); | 705 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX)); |
986 } | 706 } |
987 | 707 |
988 } // namespace | 708 } // namespace |
OLD | NEW |