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

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

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