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
3 // found in the LICENSE file.
6 #include "base/files/file_util.h"
7 #include "base/format_macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/browser/ui/browser_window.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/location_bar/location_bar.h"
20 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
21 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
22 #include "chrome/browser/ui/omnibox/omnibox_view.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/view_ids.h"
25 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/in_process_browser_test.h"
28 #include "chrome/test/base/interactive_test_utils.h"
29 #include "chrome/test/base/ui_test_utils.h"
30 #include "content/public/browser/interstitial_page.h"
31 #include "content/public/browser/interstitial_page_delegate.h"
32 #include "content/public/browser/notification_service.h"
33 #include "content/public/browser/render_frame_host.h"
34 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/render_widget_host_view.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/test/browser_test_utils.h"
38 #include "net/test/embedded_test_server/embedded_test_server.h"
41 #include "base/win/windows_version.h"
44 using content::RenderViewHost
;
45 using content::WebContents
;
47 #if defined(OS_LINUX) || defined(OS_MACOSX)
48 // TODO(jcampan): http://crbug.com/23683 for linux.
49 // TODO(suzhe): http://crbug.com/49737 for mac.
50 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
52 // Flaky, http://crbug.com/62537.
53 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
58 // The delay waited in some cases where we don't have a notifications for an
60 const int kActionDelayMs
= 500;
62 const char kSimplePage
[] = "/focus/page_with_focus.html";
63 const char kStealFocusPage
[] = "/focus/page_steals_focus.html";
64 const char kTypicalPage
[] = "/focus/typical_page.html";
66 class BrowserFocusTest
: public InProcessBrowserTest
{
68 // InProcessBrowserTest overrides:
69 void SetUpOnMainThread() override
{
70 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
73 bool IsViewFocused(ViewID vid
) {
74 return ui_test_utils::IsViewFocused(browser(), vid
);
77 void ClickOnView(ViewID vid
) {
78 ui_test_utils::ClickOnView(browser(), vid
);
81 void TestFocusTraversal(RenderViewHost
* render_view_host
, bool reverse
) {
82 const char kGetFocusedElementJS
[] =
83 "window.domAutomationController.send(getFocusedElement());";
84 const char* kExpectedIDs
[] = { "textEdit", "searchButton", "luckyButton",
85 "googleLink", "gmailLink", "gmapLink" };
86 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse
));
87 ui::KeyboardCode key
= ui::VKEY_TAB
;
88 #if defined(OS_MACOSX)
89 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
90 key
= reverse
? ui::VKEY_BACKTAB
: ui::VKEY_TAB
;
92 // This loop times out on Windows XP with no output. http://crbug.com/376635
93 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
97 // Loop through the focus chain twice for good measure.
98 for (size_t i
= 0; i
< 2; ++i
) {
99 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS
, i
));
100 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
102 // Mac requires an extra Tab key press to traverse the app menu button
103 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
104 // are required to traverse the back/forward buttons and the tab strip.
105 #if defined(OS_MACOSX)
106 if (ui_controls::IsFullKeyboardAccessEnabled()) {
107 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
108 browser(), key
, false, reverse
, false, false));
110 for (int j
= 0; j
< 3; ++j
) {
111 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
112 browser(), key
, false, reverse
, false, false));
118 for (size_t j
= 0; j
< arraysize(kExpectedIDs
); ++j
) {
119 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS
, j
));
120 const size_t index
= reverse
? arraysize(kExpectedIDs
) - 1 - j
: j
;
121 // The details are the node's editable state, i.e. true for "textEdit".
122 bool is_editable_node
= index
== 0;
124 // Press Tab (or Shift+Tab) and check the focused element id.
125 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
126 browser(), key
, false, reverse
, false, false,
127 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE
,
128 content::Source
<RenderViewHost
>(render_view_host
),
129 content::Details
<bool>(&is_editable_node
)));
130 std::string focused_id
;
131 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
132 render_view_host
, kGetFocusedElementJS
, &focused_id
));
133 EXPECT_STREQ(kExpectedIDs
[index
], focused_id
.c_str());
136 #if defined(OS_MACOSX)
137 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
138 // would also apparently require extra Tab key presses here. Sigh...
139 chrome::FocusLocationBar(browser());
141 // On the last Tab key press, focus returns to the browser.
142 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
143 browser(), key
, false, reverse
, false, false,
144 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER
,
145 content::Source
<Browser
>(browser())));
147 content::RunAllPendingInMessageLoop();
148 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
153 // A test interstitial page with typical HTML contents.
154 class TestInterstitialPage
: public content::InterstitialPageDelegate
{
156 explicit TestInterstitialPage(WebContents
* tab
) {
157 base::FilePath file_path
;
158 bool success
= PathService::Get(chrome::DIR_TEST_DATA
, &file_path
);
159 EXPECT_TRUE(success
);
160 file_path
= file_path
.AppendASCII("focus/typical_page.html");
161 success
= base::ReadFileToString(file_path
, &html_contents_
);
162 EXPECT_TRUE(success
);
163 interstitial_page_
= content::InterstitialPage::Create(
164 tab
, true, GURL("http://interstitial.com"), this);
166 // Show the interstitial and delay return until it has attached.
167 interstitial_page_
->Show();
168 content::WaitForInterstitialAttach(tab
);
170 EXPECT_TRUE(tab
->ShowingInterstitialPage());
173 std::string
GetHTMLContents() override
{ return html_contents_
; }
175 RenderViewHost
* render_view_host() {
176 return interstitial_page_
->GetMainFrame()->GetRenderViewHost();
179 void DontProceed() { interstitial_page_
->DontProceed(); }
181 bool HasFocus() { return render_view_host()->GetView()->HasFocus(); }
184 std::string html_contents_
;
185 content::InterstitialPage
* interstitial_page_
; // Owns this.
186 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage
);
189 // Flaky on mac. http://crbug.com/67301.
190 #if defined(OS_MACOSX)
191 #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
193 #define MAYBE_ClickingMovesFocus ClickingMovesFocus
195 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_ClickingMovesFocus
) {
196 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
197 #if defined(OS_POSIX)
198 // It seems we have to wait a little bit for the widgets to spin up before
199 // we can start clicking on them.
200 base::MessageLoop::current()->PostDelayedTask(
202 base::MessageLoop::QuitClosure(),
203 base::TimeDelta::FromMilliseconds(kActionDelayMs
));
204 content::RunMessageLoop();
205 #endif // defined(OS_POSIX)
207 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
209 ClickOnView(VIEW_ID_TAB_CONTAINER
);
210 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
212 ClickOnView(VIEW_ID_OMNIBOX
);
213 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
216 // Flaky, http://crbug.com/69034.
217 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_BrowsersRememberFocus
) {
218 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
219 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
220 ui_test_utils::NavigateToURL(browser(), url
);
222 gfx::NativeWindow window
= browser()->window()->GetNativeWindow();
224 // The focus should be on the Tab contents.
225 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
226 // Now hide the window, show it again, the focus should not have changed.
227 ui_test_utils::HideNativeWindow(window
);
228 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window
));
229 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
231 chrome::FocusLocationBar(browser());
232 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
233 // Hide the window, show it again, the focus should not have changed.
234 ui_test_utils::HideNativeWindow(window
);
235 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window
));
236 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
239 // Tabs remember focus.
240 // Disabled, http://crbug.com/62542.
241 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_TabsRememberFocus
) {
242 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
243 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
244 ui_test_utils::NavigateToURL(browser(), url
);
246 // Create several tabs.
247 for (int i
= 0; i
< 4; ++i
) {
248 chrome::AddSelectedTabWithURL(browser(), url
,
249 ui::PAGE_TRANSITION_TYPED
);
252 // Alternate focus for the tab.
253 const bool kFocusPage
[3][5] = {
254 { true, true, true, true, false },
255 { false, false, false, false, false },
256 { false, true, false, true, false }
259 for (int i
= 1; i
< 3; i
++) {
260 for (int j
= 0; j
< 5; j
++) {
262 browser()->tab_strip_model()->ActivateTabAt(j
, true);
264 // Activate the location bar or the page.
265 if (kFocusPage
[i
][j
]) {
266 browser()->tab_strip_model()->GetWebContentsAt(j
)->Focus();
268 chrome::FocusLocationBar(browser());
272 // Now come back to the tab and check the right view is focused.
273 for (int j
= 0; j
< 5; j
++) {
275 browser()->tab_strip_model()->ActivateTabAt(j
, true);
277 ViewID vid
= kFocusPage
[i
][j
] ? VIEW_ID_TAB_CONTAINER
: VIEW_ID_OMNIBOX
;
278 ASSERT_TRUE(IsViewFocused(vid
));
281 browser()->tab_strip_model()->ActivateTabAt(0, true);
282 // Try the above, but with ctrl+tab. Since tab normally changes focus,
283 // this has regressed in the past. Loop through several times to be sure.
284 for (int j
= 0; j
< 15; j
++) {
285 ViewID vid
= kFocusPage
[i
][j
% 5] ? VIEW_ID_TAB_CONTAINER
:
287 ASSERT_TRUE(IsViewFocused(vid
));
289 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
290 browser(), ui::VKEY_TAB
, true, false, false, false));
293 // As above, but with ctrl+shift+tab.
294 browser()->tab_strip_model()->ActivateTabAt(4, true);
295 for (int j
= 14; j
>= 0; --j
) {
296 ViewID vid
= kFocusPage
[i
][j
% 5] ? VIEW_ID_TAB_CONTAINER
:
298 ASSERT_TRUE(IsViewFocused(vid
));
300 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
301 browser(), ui::VKEY_TAB
, true, true, false, false));
306 // Tabs remember focus with find-in-page box.
307 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_TabsRememberFocusFindInPage
) {
308 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
309 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
310 ui_test_utils::NavigateToURL(browser(), url
);
312 chrome::Find(browser());
313 ui_test_utils::FindInPage(
314 browser()->tab_strip_model()->GetActiveWebContents(),
315 base::ASCIIToUTF16("a"), true, false, NULL
, NULL
);
316 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
318 // Focus the location bar.
319 chrome::FocusLocationBar(browser());
322 chrome::AddSelectedTabWithURL(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
324 // Focus should be on the recently opened tab page.
325 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
327 // Select 1st tab, focus should still be on the location-bar.
328 // (bug http://crbug.com/23296)
329 browser()->tab_strip_model()->ActivateTabAt(0, true);
330 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
332 // Now open the find box again, switch to another tab and come back, the focus
333 // should return to the find box.
334 chrome::Find(browser());
335 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
336 browser()->tab_strip_model()->ActivateTabAt(1, true);
337 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
338 browser()->tab_strip_model()->ActivateTabAt(0, true);
339 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
342 // Background window does not steal focus.
343 // Flaky, http://crbug.com/62538.
344 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
,
345 DISABLED_BackgroundBrowserDontStealFocus
) {
346 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
348 // Open a new browser window.
350 new Browser(Browser::CreateParams(browser()->profile(),
351 browser()->host_desktop_type()));
352 ASSERT_TRUE(browser2
);
353 chrome::AddTabAt(browser2
, GURL(), -1, true);
354 browser2
->window()->Show();
356 Browser
* focused_browser
= NULL
;
357 Browser
* unfocused_browser
= NULL
;
359 // On X11, calling Activate() is not guaranteed to move focus, so we have
360 // to figure out which browser does have focus.
361 if (browser2
->window()->IsActive()) {
362 focused_browser
= browser2
;
363 unfocused_browser
= browser();
364 } else if (browser()->window()->IsActive()) {
365 focused_browser
= browser();
366 unfocused_browser
= browser2
;
368 FAIL() << "Could not determine which browser has focus";
370 #elif defined(OS_WIN)
371 focused_browser
= browser();
372 unfocused_browser
= browser2
;
373 #elif defined(OS_MACOSX)
374 // On Mac, the newly created window always gets the focus.
375 focused_browser
= browser2
;
376 unfocused_browser
= browser();
379 const GURL steal_focus_url
= embedded_test_server()->GetURL(kStealFocusPage
);
380 ui_test_utils::NavigateToURL(unfocused_browser
, steal_focus_url
);
382 // Activate the first browser.
383 focused_browser
->window()->Activate();
385 ASSERT_TRUE(content::ExecuteScript(
386 unfocused_browser
->tab_strip_model()->GetActiveWebContents(),
389 // Make sure the first browser is still active.
390 EXPECT_TRUE(focused_browser
->window()->IsActive());
393 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
394 // TODO(erg): http://crbug.com/163931
395 #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
397 #define MAYBE_LocationBarLockFocus LocationBarLockFocus
400 // Page cannot steal focus when focus is on location bar.
401 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_LocationBarLockFocus
) {
402 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
404 // Open the page that steals focus.
405 const GURL url
= embedded_test_server()->GetURL(kStealFocusPage
);
406 ui_test_utils::NavigateToURL(browser(), url
);
408 chrome::FocusLocationBar(browser());
410 ASSERT_TRUE(content::ExecuteScript(
411 browser()->tab_strip_model()->GetActiveWebContents(),
414 // Make sure the location bar is still focused.
415 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
418 // Test forward and reverse focus traversal on a typical page.
419 // Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
420 // see https://crbug.com/60973.
421 #if defined(OS_MACOSX)
422 #define MAYBE_FocusTraversal DISABLED_FocusTraversal
424 #define MAYBE_FocusTraversal FocusTraversal
426 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_FocusTraversal
) {
427 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
428 const GURL url
= embedded_test_server()->GetURL(kTypicalPage
);
429 ui_test_utils::NavigateToURL(browser(), url
);
430 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
431 chrome::FocusLocationBar(browser());
433 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
434 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab
->GetRenderViewHost(), false));
435 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab
->GetRenderViewHost(), true));
438 // Test forward and reverse focus traversal while an interstitial is showing.
439 // Disabled, see http://crbug.com/60973
440 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusTraversalOnInterstitial
) {
441 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
442 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
443 ui_test_utils::NavigateToURL(browser(), url
);
444 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
446 // Create and show a test interstitial page.
447 TestInterstitialPage
* interstitial_page
= new TestInterstitialPage(
448 browser()->tab_strip_model()->GetActiveWebContents());
449 content::RenderViewHost
* host
= interstitial_page
->render_view_host();
451 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
452 chrome::FocusLocationBar(browser());
453 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host
, false));
454 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host
, true));
457 // Test the transfer of focus when an interstitial is shown and hidden.
458 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, InterstitialFocus
) {
459 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
460 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
461 ui_test_utils::NavigateToURL(browser(), url
);
462 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
463 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
464 EXPECT_TRUE(tab
->GetRenderViewHost()->GetView()->HasFocus());
466 // Create and show a test interstitial page; it should gain focus.
467 TestInterstitialPage
* interstitial_page
= new TestInterstitialPage(tab
);
468 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
469 EXPECT_TRUE(interstitial_page
->HasFocus());
471 // Hide the interstitial; the original page should gain focus.
472 interstitial_page
->DontProceed();
473 content::RunAllPendingInMessageLoop();
474 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
475 EXPECT_TRUE(tab
->GetRenderViewHost()->GetView()->HasFocus());
478 // Test that find-in-page UI can request focus, even when it is already open.
479 #if defined(OS_MACOSX)
480 #define MAYBE_FindFocusTest DISABLED_FindFocusTest
482 #define MAYBE_FindFocusTest FindFocusTest
484 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_FindFocusTest
) {
485 chrome::DisableFindBarAnimationsDuringTesting(true);
486 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
487 const GURL url
= embedded_test_server()->GetURL(kTypicalPage
);
488 ui_test_utils::NavigateToURL(browser(), url
);
489 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
491 chrome::ShowFindBar(browser());
492 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
494 chrome::FocusLocationBar(browser());
495 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
497 chrome::ShowFindBar(browser());
498 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
500 ClickOnView(VIEW_ID_TAB_CONTAINER
);
501 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
503 chrome::ShowFindBar(browser());
504 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
507 // Makes sure the focus is in the right location when opening the different
509 // Flaky, http://crbug.com/62539.
510 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_TabInitialFocus
) {
511 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
513 // Open the history tab, focus should be on the tab contents.
514 chrome::ShowHistory(browser());
515 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
516 browser()->tab_strip_model()->GetActiveWebContents()));
517 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
519 // Open the new tab, focus should be on the location bar.
520 chrome::NewTab(browser());
521 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
522 browser()->tab_strip_model()->GetActiveWebContents()));
523 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
525 // Open the download tab, focus should be on the tab contents.
526 chrome::ShowDownloads(browser());
527 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
528 browser()->tab_strip_model()->GetActiveWebContents()));
529 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
531 // Open about:blank, focus should be on the location bar.
532 chrome::AddSelectedTabWithURL(
533 browser(), GURL(url::kAboutBlankURL
), ui::PAGE_TRANSITION_LINK
);
534 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
535 browser()->tab_strip_model()->GetActiveWebContents()));
536 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
539 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
540 // TODO(erg): http://crbug.com/163931
541 #define MAYBE_FocusOnReload DISABLED_FocusOnReload
543 #define MAYBE_FocusOnReload FocusOnReload
546 // Tests that focus goes where expected when using reload.
547 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_FocusOnReload
) {
548 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
550 // Open the new tab, reload.
552 content::WindowedNotificationObserver
observer(
553 content::NOTIFICATION_LOAD_STOP
,
554 content::NotificationService::AllSources());
555 chrome::NewTab(browser());
558 content::RunAllPendingInMessageLoop();
561 content::WindowedNotificationObserver
observer(
562 content::NOTIFICATION_LOAD_STOP
,
563 content::Source
<content::NavigationController
>(
564 &browser()->tab_strip_model()->GetActiveWebContents()->
566 chrome::Reload(browser(), CURRENT_TAB
);
569 // Focus should stay on the location bar.
570 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
572 // Open a regular page, focus the location bar, reload.
573 ui_test_utils::NavigateToURL(browser(),
574 embedded_test_server()->GetURL(kSimplePage
));
575 chrome::FocusLocationBar(browser());
576 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
578 content::WindowedNotificationObserver
observer(
579 content::NOTIFICATION_LOAD_STOP
,
580 content::Source
<content::NavigationController
>(
581 &browser()->tab_strip_model()->GetActiveWebContents()->
583 chrome::Reload(browser(), CURRENT_TAB
);
587 // Focus should now be on the tab contents.
588 chrome::ShowDownloads(browser());
589 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
592 // Tests that focus goes where expected when using reload on a crashed tab.
593 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusOnReloadCrashedTab
) {
594 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
596 // Open a regular page, crash, reload.
597 ui_test_utils::NavigateToURL(browser(),
598 embedded_test_server()->GetURL(kSimplePage
));
599 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
601 content::WindowedNotificationObserver
observer(
602 content::NOTIFICATION_LOAD_STOP
,
603 content::Source
<content::NavigationController
>(
604 &browser()->tab_strip_model()->GetActiveWebContents()->
606 chrome::Reload(browser(), CURRENT_TAB
);
610 // Focus should now be on the tab contents.
611 chrome::ShowDownloads(browser());
612 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
615 // Tests that focus goes to frame after crashed tab.
616 // TODO(shrikant): Find out where the focus should be deterministically.
617 // Currently focused_view after crash seem to be non null in debug mode
618 // (invalidated pointer 0xcccccc).
619 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusAfterCrashedTab
) {
620 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
622 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
624 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
627 // Tests that when a new tab is opened from the omnibox, the focus is moved from
628 // the omnibox for the current tab.
629 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, NavigateFromOmniboxIntoNewTab
) {
630 GURL
url("http://www.google.com/");
631 GURL
url2("http://maps.google.com/");
634 chrome::NavigateParams
p(browser(), url
, ui::PAGE_TRANSITION_LINK
);
635 p
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
636 p
.disposition
= CURRENT_TAB
;
637 chrome::Navigate(&p
);
639 // Focus the omnibox.
640 chrome::FocusLocationBar(browser());
642 OmniboxEditController
* controller
= browser()->window()->GetLocationBar()->
643 GetOmniboxView()->model()->controller();
645 // Simulate an alt-enter.
646 controller
->OnAutocompleteAccept(url2
, NEW_FOREGROUND_TAB
,
647 ui::PAGE_TRANSITION_TYPED
);
649 // Make sure the second tab is selected.
650 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
652 // The tab contents should have the focus in the second tab.
653 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
655 // Go back to the first tab. The focus should not be in the omnibox.
656 chrome::SelectPreviousTab(browser());
657 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
658 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX
));
661 // This functionality is currently broken. http://crbug.com/304865.
663 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
664 //// TODO(erg): http://crbug.com/163931
665 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
667 //#define MAYBE_FocusOnNavigate FocusOnNavigate
670 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusOnNavigate
) {
672 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
674 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
675 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
677 // Navigate to another page.
678 const base::FilePath::CharType
* kEmptyFile
= FILE_PATH_LITERAL("empty.html");
679 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
680 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
681 ui_test_utils::NavigateToURL(browser(), file_url
);
683 ClickOnView(VIEW_ID_TAB_CONTAINER
);
685 // Navigate back. Should focus the location bar.
687 content::WindowedNotificationObserver
back_nav_observer(
688 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
689 content::NotificationService::AllSources());
690 chrome::GoBack(browser(), CURRENT_TAB
);
691 back_nav_observer
.Wait();
694 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
696 // Navigate forward. Shouldn't focus the location bar.
697 ClickOnView(VIEW_ID_TAB_CONTAINER
);
699 content::WindowedNotificationObserver
forward_nav_observer(
700 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
701 content::NotificationService::AllSources());
702 chrome::GoForward(browser(), CURRENT_TAB
);
703 forward_nav_observer
.Wait();
706 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX
));