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_view_host.h"
34 #include "content/public/browser/render_widget_host_view.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/test/browser_test_utils.h"
37 #include "net/test/embedded_test_server/embedded_test_server.h"
40 #include "base/win/windows_version.h"
43 using content::RenderViewHost
;
44 using content::WebContents
;
46 #if defined(OS_LINUX) || defined(OS_MACOSX)
47 // TODO(jcampan): http://crbug.com/23683 for linux.
48 // TODO(suzhe): http://crbug.com/49737 for mac.
49 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
51 // Flaky, http://crbug.com/62537.
52 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
57 // The delay waited in some cases where we don't have a notifications for an
59 const int kActionDelayMs
= 500;
61 const char kSimplePage
[] = "/focus/page_with_focus.html";
62 const char kStealFocusPage
[] = "/focus/page_steals_focus.html";
63 const char kTypicalPage
[] = "/focus/typical_page.html";
65 class BrowserFocusTest
: public InProcessBrowserTest
{
67 // InProcessBrowserTest overrides:
68 virtual void SetUpOnMainThread() override
{
69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
72 bool IsViewFocused(ViewID vid
) {
73 return ui_test_utils::IsViewFocused(browser(), vid
);
76 void ClickOnView(ViewID vid
) {
77 ui_test_utils::ClickOnView(browser(), vid
);
80 void TestFocusTraversal(RenderViewHost
* render_view_host
, bool reverse
) {
81 const char kGetFocusedElementJS
[] =
82 "window.domAutomationController.send(getFocusedElement());";
83 const char* kExpectedIDs
[] = { "textEdit", "searchButton", "luckyButton",
84 "googleLink", "gmailLink", "gmapLink" };
85 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse
));
86 ui::KeyboardCode key
= ui::VKEY_TAB
;
87 #if defined(OS_MACOSX)
88 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
89 key
= reverse
? ui::VKEY_BACKTAB
: ui::VKEY_TAB
;
91 // This loop times out on Windows XP with no output. http://crbug.com/376635
92 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
96 // Loop through the focus chain twice for good measure.
97 for (size_t i
= 0; i
< 2; ++i
) {
98 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS
, i
));
99 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
101 // Mac requires an extra Tab key press to traverse the app menu button
102 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
103 // are required to traverse the back/forward buttons and the tab strip.
104 #if defined(OS_MACOSX)
105 if (ui_controls::IsFullKeyboardAccessEnabled()) {
106 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
107 browser(), key
, false, reverse
, false, false));
109 for (int j
= 0; j
< 3; ++j
) {
110 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
111 browser(), key
, false, reverse
, false, false));
117 for (size_t j
= 0; j
< arraysize(kExpectedIDs
); ++j
) {
118 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS
, j
));
119 const size_t index
= reverse
? arraysize(kExpectedIDs
) - 1 - j
: j
;
120 // The details are the node's editable state, i.e. true for "textEdit".
121 bool is_editable_node
= index
== 0;
123 // Press Tab (or Shift+Tab) and check the focused element id.
124 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
125 browser(), key
, false, reverse
, false, false,
126 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE
,
127 content::Source
<RenderViewHost
>(render_view_host
),
128 content::Details
<bool>(&is_editable_node
)));
129 std::string focused_id
;
130 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
131 render_view_host
, kGetFocusedElementJS
, &focused_id
));
132 EXPECT_STREQ(kExpectedIDs
[index
], focused_id
.c_str());
135 #if defined(OS_MACOSX)
136 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
137 // would also apparently require extra Tab key presses here. Sigh...
138 chrome::FocusLocationBar(browser());
140 // On the last Tab key press, focus returns to the browser.
141 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
142 browser(), key
, false, reverse
, false, false,
143 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER
,
144 content::Source
<Browser
>(browser())));
146 content::RunAllPendingInMessageLoop();
147 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
152 // A test interstitial page with typical HTML contents.
153 class TestInterstitialPage
: public content::InterstitialPageDelegate
{
155 explicit TestInterstitialPage(WebContents
* tab
) {
156 base::FilePath file_path
;
157 bool success
= PathService::Get(chrome::DIR_TEST_DATA
, &file_path
);
158 EXPECT_TRUE(success
);
159 file_path
= file_path
.AppendASCII("focus/typical_page.html");
160 success
= base::ReadFileToString(file_path
, &html_contents_
);
161 EXPECT_TRUE(success
);
162 interstitial_page_
= content::InterstitialPage::Create(
163 tab
, true, GURL("http://interstitial.com"), this);
165 // Show the interstitial and delay return until it has attached.
166 interstitial_page_
->Show();
167 content::WaitForInterstitialAttach(tab
);
169 EXPECT_TRUE(tab
->ShowingInterstitialPage());
172 virtual std::string
GetHTMLContents() override
{ return html_contents_
; }
174 RenderViewHost
* render_view_host() {
175 return interstitial_page_
->GetRenderViewHostForTesting();
178 void DontProceed() { interstitial_page_
->DontProceed(); }
180 bool HasFocus() { return render_view_host()->GetView()->HasFocus(); }
183 std::string html_contents_
;
184 content::InterstitialPage
* interstitial_page_
; // Owns this.
185 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage
);
188 // Flaky on mac. http://crbug.com/67301.
189 #if defined(OS_MACOSX)
190 #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
192 #define MAYBE_ClickingMovesFocus ClickingMovesFocus
194 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_ClickingMovesFocus
) {
195 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
196 #if defined(OS_POSIX)
197 // It seems we have to wait a little bit for the widgets to spin up before
198 // we can start clicking on them.
199 base::MessageLoop::current()->PostDelayedTask(
201 base::MessageLoop::QuitClosure(),
202 base::TimeDelta::FromMilliseconds(kActionDelayMs
));
203 content::RunMessageLoop();
204 #endif // defined(OS_POSIX)
206 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
208 ClickOnView(VIEW_ID_TAB_CONTAINER
);
209 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
211 ClickOnView(VIEW_ID_OMNIBOX
);
212 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
215 // Flaky, http://crbug.com/69034.
216 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_BrowsersRememberFocus
) {
217 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
218 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
219 ui_test_utils::NavigateToURL(browser(), url
);
221 gfx::NativeWindow window
= browser()->window()->GetNativeWindow();
223 // The focus should be on the Tab contents.
224 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
225 // Now hide the window, show it again, the focus should not have changed.
226 ui_test_utils::HideNativeWindow(window
);
227 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window
));
228 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
230 chrome::FocusLocationBar(browser());
231 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
232 // Hide the window, show it again, the focus should not have changed.
233 ui_test_utils::HideNativeWindow(window
);
234 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window
));
235 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
238 // Tabs remember focus.
239 // Disabled, http://crbug.com/62542.
240 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_TabsRememberFocus
) {
241 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
242 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
243 ui_test_utils::NavigateToURL(browser(), url
);
245 // Create several tabs.
246 for (int i
= 0; i
< 4; ++i
) {
247 chrome::AddSelectedTabWithURL(browser(), url
,
248 ui::PAGE_TRANSITION_TYPED
);
251 // Alternate focus for the tab.
252 const bool kFocusPage
[3][5] = {
253 { true, true, true, true, false },
254 { false, false, false, false, false },
255 { false, true, false, true, false }
258 for (int i
= 1; i
< 3; i
++) {
259 for (int j
= 0; j
< 5; j
++) {
261 browser()->tab_strip_model()->ActivateTabAt(j
, true);
263 // Activate the location bar or the page.
264 if (kFocusPage
[i
][j
]) {
265 browser()->tab_strip_model()->GetWebContentsAt(j
)->Focus();
267 chrome::FocusLocationBar(browser());
271 // Now come back to the tab and check the right view is focused.
272 for (int j
= 0; j
< 5; j
++) {
274 browser()->tab_strip_model()->ActivateTabAt(j
, true);
276 ViewID vid
= kFocusPage
[i
][j
] ? VIEW_ID_TAB_CONTAINER
: VIEW_ID_OMNIBOX
;
277 ASSERT_TRUE(IsViewFocused(vid
));
280 browser()->tab_strip_model()->ActivateTabAt(0, true);
281 // Try the above, but with ctrl+tab. Since tab normally changes focus,
282 // this has regressed in the past. Loop through several times to be sure.
283 for (int j
= 0; j
< 15; j
++) {
284 ViewID vid
= kFocusPage
[i
][j
% 5] ? VIEW_ID_TAB_CONTAINER
:
286 ASSERT_TRUE(IsViewFocused(vid
));
288 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
289 browser(), ui::VKEY_TAB
, true, false, false, false));
292 // As above, but with ctrl+shift+tab.
293 browser()->tab_strip_model()->ActivateTabAt(4, true);
294 for (int j
= 14; j
>= 0; --j
) {
295 ViewID vid
= kFocusPage
[i
][j
% 5] ? VIEW_ID_TAB_CONTAINER
:
297 ASSERT_TRUE(IsViewFocused(vid
));
299 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
300 browser(), ui::VKEY_TAB
, true, true, false, false));
305 // Tabs remember focus with find-in-page box.
306 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_TabsRememberFocusFindInPage
) {
307 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
308 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
309 ui_test_utils::NavigateToURL(browser(), url
);
311 chrome::Find(browser());
312 ui_test_utils::FindInPage(
313 browser()->tab_strip_model()->GetActiveWebContents(),
314 base::ASCIIToUTF16("a"), true, false, NULL
, NULL
);
315 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
317 // Focus the location bar.
318 chrome::FocusLocationBar(browser());
321 chrome::AddSelectedTabWithURL(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
323 // Focus should be on the recently opened tab page.
324 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
326 // Select 1st tab, focus should still be on the location-bar.
327 // (bug http://crbug.com/23296)
328 browser()->tab_strip_model()->ActivateTabAt(0, true);
329 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
331 // Now open the find box again, switch to another tab and come back, the focus
332 // should return to the find box.
333 chrome::Find(browser());
334 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
335 browser()->tab_strip_model()->ActivateTabAt(1, true);
336 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
337 browser()->tab_strip_model()->ActivateTabAt(0, true);
338 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
341 // Background window does not steal focus.
342 // Flaky, http://crbug.com/62538.
343 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
,
344 DISABLED_BackgroundBrowserDontStealFocus
) {
345 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
347 // Open a new browser window.
349 new Browser(Browser::CreateParams(browser()->profile(),
350 browser()->host_desktop_type()));
351 ASSERT_TRUE(browser2
);
352 chrome::AddTabAt(browser2
, GURL(), -1, true);
353 browser2
->window()->Show();
355 Browser
* focused_browser
= NULL
;
356 Browser
* unfocused_browser
= NULL
;
358 // On X11, calling Activate() is not guaranteed to move focus, so we have
359 // to figure out which browser does have focus.
360 if (browser2
->window()->IsActive()) {
361 focused_browser
= browser2
;
362 unfocused_browser
= browser();
363 } else if (browser()->window()->IsActive()) {
364 focused_browser
= browser();
365 unfocused_browser
= browser2
;
367 FAIL() << "Could not determine which browser has focus";
369 #elif defined(OS_WIN)
370 focused_browser
= browser();
371 unfocused_browser
= browser2
;
372 #elif defined(OS_MACOSX)
373 // On Mac, the newly created window always gets the focus.
374 focused_browser
= browser2
;
375 unfocused_browser
= browser();
378 const GURL steal_focus_url
= embedded_test_server()->GetURL(kStealFocusPage
);
379 ui_test_utils::NavigateToURL(unfocused_browser
, steal_focus_url
);
381 // Activate the first browser.
382 focused_browser
->window()->Activate();
384 ASSERT_TRUE(content::ExecuteScript(
385 unfocused_browser
->tab_strip_model()->GetActiveWebContents(),
388 // Make sure the first browser is still active.
389 EXPECT_TRUE(focused_browser
->window()->IsActive());
392 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
393 // TODO(erg): http://crbug.com/163931
394 #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
396 #define MAYBE_LocationBarLockFocus LocationBarLockFocus
399 // Page cannot steal focus when focus is on location bar.
400 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_LocationBarLockFocus
) {
401 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
403 // Open the page that steals focus.
404 const GURL url
= embedded_test_server()->GetURL(kStealFocusPage
);
405 ui_test_utils::NavigateToURL(browser(), url
);
407 chrome::FocusLocationBar(browser());
409 ASSERT_TRUE(content::ExecuteScript(
410 browser()->tab_strip_model()->GetActiveWebContents(),
413 // Make sure the location bar is still focused.
414 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
417 // Test forward and reverse focus traversal on a typical page.
418 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, FocusTraversal
) {
419 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
420 const GURL url
= embedded_test_server()->GetURL(kTypicalPage
);
421 ui_test_utils::NavigateToURL(browser(), url
);
422 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
423 chrome::FocusLocationBar(browser());
425 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
426 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab
->GetRenderViewHost(), false));
427 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab
->GetRenderViewHost(), true));
430 // Test forward and reverse focus traversal while an interstitial is showing.
431 // Disabled, see http://crbug.com/60973
432 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusTraversalOnInterstitial
) {
433 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
434 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
435 ui_test_utils::NavigateToURL(browser(), url
);
436 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
438 // Create and show a test interstitial page.
439 TestInterstitialPage
* interstitial_page
= new TestInterstitialPage(
440 browser()->tab_strip_model()->GetActiveWebContents());
441 content::RenderViewHost
* host
= interstitial_page
->render_view_host();
443 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
444 chrome::FocusLocationBar(browser());
445 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host
, false));
446 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host
, true));
449 // Test the transfer of focus when an interstitial is shown and hidden.
450 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, InterstitialFocus
) {
451 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
452 const GURL url
= embedded_test_server()->GetURL(kSimplePage
);
453 ui_test_utils::NavigateToURL(browser(), url
);
454 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
455 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
456 EXPECT_TRUE(tab
->GetRenderViewHost()->GetView()->HasFocus());
458 // Create and show a test interstitial page; it should gain focus.
459 TestInterstitialPage
* interstitial_page
= new TestInterstitialPage(tab
);
460 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
461 EXPECT_TRUE(interstitial_page
->HasFocus());
463 // Hide the interstitial; the original page should gain focus.
464 interstitial_page
->DontProceed();
465 content::RunAllPendingInMessageLoop();
466 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
467 EXPECT_TRUE(tab
->GetRenderViewHost()->GetView()->HasFocus());
470 // Test that find-in-page UI can request focus, even when it is already open.
471 #if defined(OS_MACOSX)
472 #define MAYBE_FindFocusTest DISABLED_FindFocusTest
474 #define MAYBE_FindFocusTest FindFocusTest
476 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_FindFocusTest
) {
477 chrome::DisableFindBarAnimationsDuringTesting(true);
478 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
479 const GURL url
= embedded_test_server()->GetURL(kTypicalPage
);
480 ui_test_utils::NavigateToURL(browser(), url
);
481 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
483 chrome::ShowFindBar(browser());
484 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
486 chrome::FocusLocationBar(browser());
487 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
489 chrome::ShowFindBar(browser());
490 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
492 ClickOnView(VIEW_ID_TAB_CONTAINER
);
493 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
495 chrome::ShowFindBar(browser());
496 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
499 // Makes sure the focus is in the right location when opening the different
501 // Flaky, http://crbug.com/62539.
502 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_TabInitialFocus
) {
503 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
505 // Open the history tab, focus should be on the tab contents.
506 chrome::ShowHistory(browser());
507 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
508 browser()->tab_strip_model()->GetActiveWebContents()));
509 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
511 // Open the new tab, focus should be on the location bar.
512 chrome::NewTab(browser());
513 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
514 browser()->tab_strip_model()->GetActiveWebContents()));
515 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
517 // Open the download tab, focus should be on the tab contents.
518 chrome::ShowDownloads(browser());
519 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
520 browser()->tab_strip_model()->GetActiveWebContents()));
521 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
523 // Open about:blank, focus should be on the location bar.
524 chrome::AddSelectedTabWithURL(
525 browser(), GURL(url::kAboutBlankURL
), ui::PAGE_TRANSITION_LINK
);
526 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
527 browser()->tab_strip_model()->GetActiveWebContents()));
528 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
531 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
532 // TODO(erg): http://crbug.com/163931
533 #define MAYBE_FocusOnReload DISABLED_FocusOnReload
535 #define MAYBE_FocusOnReload FocusOnReload
538 // Tests that focus goes where expected when using reload.
539 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, MAYBE_FocusOnReload
) {
540 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
542 // Open the new tab, reload.
544 content::WindowedNotificationObserver
observer(
545 content::NOTIFICATION_LOAD_STOP
,
546 content::NotificationService::AllSources());
547 chrome::NewTab(browser());
550 content::RunAllPendingInMessageLoop();
553 content::WindowedNotificationObserver
observer(
554 content::NOTIFICATION_LOAD_STOP
,
555 content::Source
<content::NavigationController
>(
556 &browser()->tab_strip_model()->GetActiveWebContents()->
558 chrome::Reload(browser(), CURRENT_TAB
);
561 // Focus should stay on the location bar.
562 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
564 // Open a regular page, focus the location bar, reload.
565 ui_test_utils::NavigateToURL(browser(),
566 embedded_test_server()->GetURL(kSimplePage
));
567 chrome::FocusLocationBar(browser());
568 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
570 content::WindowedNotificationObserver
observer(
571 content::NOTIFICATION_LOAD_STOP
,
572 content::Source
<content::NavigationController
>(
573 &browser()->tab_strip_model()->GetActiveWebContents()->
575 chrome::Reload(browser(), CURRENT_TAB
);
579 // Focus should now be on the tab contents.
580 chrome::ShowDownloads(browser());
581 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
584 // Tests that focus goes where expected when using reload on a crashed tab.
585 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusOnReloadCrashedTab
) {
586 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
588 // Open a regular page, crash, reload.
589 ui_test_utils::NavigateToURL(browser(),
590 embedded_test_server()->GetURL(kSimplePage
));
591 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
593 content::WindowedNotificationObserver
observer(
594 content::NOTIFICATION_LOAD_STOP
,
595 content::Source
<content::NavigationController
>(
596 &browser()->tab_strip_model()->GetActiveWebContents()->
598 chrome::Reload(browser(), CURRENT_TAB
);
602 // Focus should now be on the tab contents.
603 chrome::ShowDownloads(browser());
604 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
607 // Tests that focus goes to frame after crashed tab.
608 // TODO(shrikant): Find out where the focus should be deterministically.
609 // Currently focused_view after crash seem to be non null in debug mode
610 // (invalidated pointer 0xcccccc).
611 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusAfterCrashedTab
) {
612 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
614 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
616 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
619 // Tests that when a new tab is opened from the omnibox, the focus is moved from
620 // the omnibox for the current tab.
621 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, NavigateFromOmniboxIntoNewTab
) {
622 GURL
url("http://www.google.com/");
623 GURL
url2("http://maps.google.com/");
626 chrome::NavigateParams
p(browser(), url
, ui::PAGE_TRANSITION_LINK
);
627 p
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
628 p
.disposition
= CURRENT_TAB
;
629 chrome::Navigate(&p
);
631 // Focus the omnibox.
632 chrome::FocusLocationBar(browser());
634 OmniboxEditController
* controller
= browser()->window()->GetLocationBar()->
635 GetOmniboxView()->model()->controller();
637 // Simulate an alt-enter.
638 controller
->OnAutocompleteAccept(url2
, NEW_FOREGROUND_TAB
,
639 ui::PAGE_TRANSITION_TYPED
);
641 // Make sure the second tab is selected.
642 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
644 // The tab contents should have the focus in the second tab.
645 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER
));
647 // Go back to the first tab. The focus should not be in the omnibox.
648 chrome::SelectPreviousTab(browser());
649 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
650 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX
));
653 // This functionality is currently broken. http://crbug.com/304865.
655 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
656 //// TODO(erg): http://crbug.com/163931
657 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
659 //#define MAYBE_FocusOnNavigate FocusOnNavigate
662 IN_PROC_BROWSER_TEST_F(BrowserFocusTest
, DISABLED_FocusOnNavigate
) {
664 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
666 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
667 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
669 // Navigate to another page.
670 const base::FilePath::CharType
* kEmptyFile
= FILE_PATH_LITERAL("empty.html");
671 GURL
file_url(ui_test_utils::GetTestUrl(base::FilePath(
672 base::FilePath::kCurrentDirectory
), base::FilePath(kEmptyFile
)));
673 ui_test_utils::NavigateToURL(browser(), file_url
);
675 ClickOnView(VIEW_ID_TAB_CONTAINER
);
677 // Navigate back. Should focus the location bar.
679 content::WindowedNotificationObserver
back_nav_observer(
680 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
681 content::NotificationService::AllSources());
682 chrome::GoBack(browser(), CURRENT_TAB
);
683 back_nav_observer
.Wait();
686 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX
));
688 // Navigate forward. Shouldn't focus the location bar.
689 ClickOnView(VIEW_ID_TAB_CONTAINER
);
691 content::WindowedNotificationObserver
forward_nav_observer(
692 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
693 content::NotificationService::AllSources());
694 chrome::GoForward(browser(), CURRENT_TAB
);
695 forward_nav_observer
.Wait();
698 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX
));