Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: chrome/browser/ui/browser_focus_uitest.cc

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

Powered by Google App Engine
This is Rietveld 408576698