Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / browser_focus_uitest.cc
blobe415dae2dfafcb010e28ebd96f637a53e9e276b1
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.
5 #include "base/bind.h"
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/tabs/tab_strip_model.h"
21 #include "chrome/browser/ui/view_ids.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/interactive_test_utils.h"
26 #include "chrome/test/base/ui_test_utils.h"
27 #include "components/omnibox/browser/omnibox_edit_controller.h"
28 #include "components/omnibox/browser/omnibox_edit_model.h"
29 #include "components/omnibox/browser/omnibox_view.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"
40 #if defined(OS_WIN)
41 #include "base/win/windows_version.h"
42 #endif
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
51 #elif defined(OS_WIN)
52 // Flaky, http://crbug.com/62537.
53 #define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
54 #endif
56 namespace {
58 #if defined(OS_POSIX)
59 // The delay waited in some cases where we don't have a notifications for an
60 // action we take.
61 const int kActionDelayMs = 500;
62 #endif
64 const char kSimplePage[] = "/focus/page_with_focus.html";
65 const char kStealFocusPage[] = "/focus/page_steals_focus.html";
66 const char kTypicalPage[] = "/focus/typical_page.html";
68 class BrowserFocusTest : public InProcessBrowserTest {
69 public:
70 // InProcessBrowserTest overrides:
71 void SetUpOnMainThread() override {
72 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
75 bool IsViewFocused(ViewID vid) {
76 return ui_test_utils::IsViewFocused(browser(), vid);
79 void ClickOnView(ViewID vid) {
80 ui_test_utils::ClickOnView(browser(), vid);
83 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) {
84 const char kGetFocusedElementJS[] =
85 "window.domAutomationController.send(getFocusedElement());";
86 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton",
87 "googleLink", "gmailLink", "gmapLink" };
88 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse));
89 ui::KeyboardCode key = ui::VKEY_TAB;
90 #if defined(OS_MACOSX)
91 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
92 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB;
93 #elif defined(OS_WIN)
94 // This loop times out on Windows XP with no output. http://crbug.com/376635
95 if (base::win::GetVersion() < base::win::VERSION_VISTA)
96 return;
97 #endif
99 // Loop through the focus chain twice for good measure.
100 for (size_t i = 0; i < 2; ++i) {
101 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i));
102 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
104 // Mac requires an extra Tab key press to traverse the app menu button
105 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
106 // are required to traverse the back/forward buttons and the tab strip.
107 #if defined(OS_MACOSX)
108 if (ui_controls::IsFullKeyboardAccessEnabled()) {
109 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
110 browser(), key, false, reverse, false, false));
111 if (reverse) {
112 for (int j = 0; j < 3; ++j) {
113 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
114 browser(), key, false, reverse, false, false));
118 #endif
120 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
121 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
122 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
123 // The details are the node's editable state, i.e. true for "textEdit".
124 bool is_editable_node = index == 0;
126 // Press Tab (or Shift+Tab) and check the focused element id.
127 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
128 browser(), key, false, reverse, false, false,
129 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
130 content::Source<RenderViewHost>(render_view_host),
131 content::Details<bool>(&is_editable_node)));
132 std::string focused_id;
133 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
134 render_view_host, kGetFocusedElementJS, &focused_id));
135 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
138 #if defined(OS_MACOSX)
139 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
140 // would also apparently require extra Tab key presses here. Sigh...
141 chrome::FocusLocationBar(browser());
142 #else
143 // On the last Tab key press, focus returns to the browser.
144 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
145 browser(), key, false, reverse, false, false,
146 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
147 content::Source<Browser>(browser())));
148 #endif
149 content::RunAllPendingInMessageLoop();
150 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
155 // A test interstitial page with typical HTML contents.
156 class TestInterstitialPage : public content::InterstitialPageDelegate {
157 public:
158 explicit TestInterstitialPage(WebContents* tab) {
159 base::FilePath file_path;
160 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
161 EXPECT_TRUE(success);
162 file_path = file_path.AppendASCII("focus/typical_page.html");
163 success = base::ReadFileToString(file_path, &html_contents_);
164 EXPECT_TRUE(success);
165 interstitial_page_ = content::InterstitialPage::Create(
166 tab, true, GURL("http://interstitial.com"), this);
168 // Show the interstitial and delay return until it has attached.
169 interstitial_page_->Show();
170 content::WaitForInterstitialAttach(tab);
172 EXPECT_TRUE(tab->ShowingInterstitialPage());
175 std::string GetHTMLContents() override { return html_contents_; }
177 RenderViewHost* render_view_host() {
178 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
181 void DontProceed() { interstitial_page_->DontProceed(); }
183 bool HasFocus() { return render_view_host()->GetView()->HasFocus(); }
185 private:
186 std::string html_contents_;
187 content::InterstitialPage* interstitial_page_; // Owns this.
188 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
191 // Flaky on Mac (http://crbug.com/67301) and Windows
192 // (http://crbug.com/523255).
193 #if defined(OS_MACOSX) || defined(OS_WIN)
194 #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
195 #else
196 #define MAYBE_ClickingMovesFocus ClickingMovesFocus
197 #endif
198 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
199 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
200 #if defined(OS_POSIX)
201 // It seems we have to wait a little bit for the widgets to spin up before
202 // we can start clicking on them.
203 base::MessageLoop::current()->task_runner()->PostDelayedTask(
204 FROM_HERE, base::MessageLoop::QuitClosure(),
205 base::TimeDelta::FromMilliseconds(kActionDelayMs));
206 content::RunMessageLoop();
207 #endif // defined(OS_POSIX)
209 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
211 ClickOnView(VIEW_ID_TAB_CONTAINER);
212 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
214 ClickOnView(VIEW_ID_OMNIBOX);
215 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
218 // Flaky, http://crbug.com/69034.
219 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
220 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
221 const GURL url = embedded_test_server()->GetURL(kSimplePage);
222 ui_test_utils::NavigateToURL(browser(), url);
224 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
226 // The focus should be on the Tab contents.
227 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
228 // Now hide the window, show it again, the focus should not have changed.
229 ui_test_utils::HideNativeWindow(window);
230 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
231 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
233 chrome::FocusLocationBar(browser());
234 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
235 // Hide the window, show it again, the focus should not have changed.
236 ui_test_utils::HideNativeWindow(window);
237 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
238 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
241 // Tabs remember focus.
242 // Disabled, http://crbug.com/62542.
243 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
244 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
245 const GURL url = embedded_test_server()->GetURL(kSimplePage);
246 ui_test_utils::NavigateToURL(browser(), url);
248 // Create several tabs.
249 for (int i = 0; i < 4; ++i) {
250 chrome::AddSelectedTabWithURL(browser(), url,
251 ui::PAGE_TRANSITION_TYPED);
254 // Alternate focus for the tab.
255 const bool kFocusPage[3][5] = {
256 { true, true, true, true, false },
257 { false, false, false, false, false },
258 { false, true, false, true, false }
261 for (int i = 1; i < 3; i++) {
262 for (int j = 0; j < 5; j++) {
263 // Activate the tab.
264 browser()->tab_strip_model()->ActivateTabAt(j, true);
266 // Activate the location bar or the page.
267 if (kFocusPage[i][j]) {
268 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
269 } else {
270 chrome::FocusLocationBar(browser());
274 // Now come back to the tab and check the right view is focused.
275 for (int j = 0; j < 5; j++) {
276 // Activate the tab.
277 browser()->tab_strip_model()->ActivateTabAt(j, true);
279 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
280 ASSERT_TRUE(IsViewFocused(vid));
283 browser()->tab_strip_model()->ActivateTabAt(0, true);
284 // Try the above, but with ctrl+tab. Since tab normally changes focus,
285 // this has regressed in the past. Loop through several times to be sure.
286 for (int j = 0; j < 15; j++) {
287 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
288 VIEW_ID_OMNIBOX;
289 ASSERT_TRUE(IsViewFocused(vid));
291 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
292 browser(), ui::VKEY_TAB, true, false, false, false));
295 // As above, but with ctrl+shift+tab.
296 browser()->tab_strip_model()->ActivateTabAt(4, true);
297 for (int j = 14; j >= 0; --j) {
298 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
299 VIEW_ID_OMNIBOX;
300 ASSERT_TRUE(IsViewFocused(vid));
302 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
303 browser(), ui::VKEY_TAB, true, true, false, false));
308 // Tabs remember focus with find-in-page box.
309 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
310 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
311 const GURL url = embedded_test_server()->GetURL(kSimplePage);
312 ui_test_utils::NavigateToURL(browser(), url);
314 chrome::Find(browser());
315 ui_test_utils::FindInPage(
316 browser()->tab_strip_model()->GetActiveWebContents(),
317 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
318 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
320 // Focus the location bar.
321 chrome::FocusLocationBar(browser());
323 // Create a 2nd tab.
324 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
326 // Focus should be on the recently opened tab page.
327 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
329 // Select 1st tab, focus should still be on the location-bar.
330 // (bug http://crbug.com/23296)
331 browser()->tab_strip_model()->ActivateTabAt(0, true);
332 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
334 // Now open the find box again, switch to another tab and come back, the focus
335 // should return to the find box.
336 chrome::Find(browser());
337 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
338 browser()->tab_strip_model()->ActivateTabAt(1, true);
339 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
340 browser()->tab_strip_model()->ActivateTabAt(0, true);
341 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
344 // Background window does not steal focus.
345 // Flaky, http://crbug.com/62538.
346 IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
347 DISABLED_BackgroundBrowserDontStealFocus) {
348 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
350 // Open a new browser window.
351 Browser* browser2 =
352 new Browser(Browser::CreateParams(browser()->profile(),
353 browser()->host_desktop_type()));
354 ASSERT_TRUE(browser2);
355 chrome::AddTabAt(browser2, GURL(), -1, true);
356 browser2->window()->Show();
358 Browser* focused_browser = NULL;
359 Browser* unfocused_browser = NULL;
360 #if defined(USE_X11)
361 // On X11, calling Activate() is not guaranteed to move focus, so we have
362 // to figure out which browser does have focus.
363 if (browser2->window()->IsActive()) {
364 focused_browser = browser2;
365 unfocused_browser = browser();
366 } else if (browser()->window()->IsActive()) {
367 focused_browser = browser();
368 unfocused_browser = browser2;
369 } else {
370 FAIL() << "Could not determine which browser has focus";
372 #elif defined(OS_WIN)
373 focused_browser = browser();
374 unfocused_browser = browser2;
375 #elif defined(OS_MACOSX)
376 // On Mac, the newly created window always gets the focus.
377 focused_browser = browser2;
378 unfocused_browser = browser();
379 #endif
381 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
382 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
384 // Activate the first browser.
385 focused_browser->window()->Activate();
387 ASSERT_TRUE(content::ExecuteScript(
388 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
389 "stealFocus();"));
391 // Make sure the first browser is still active.
392 EXPECT_TRUE(focused_browser->window()->IsActive());
395 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
396 // TODO(erg): http://crbug.com/163931
397 #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
398 #else
399 #define MAYBE_LocationBarLockFocus LocationBarLockFocus
400 #endif
402 // Page cannot steal focus when focus is on location bar.
403 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
404 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
406 // Open the page that steals focus.
407 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
408 ui_test_utils::NavigateToURL(browser(), url);
410 chrome::FocusLocationBar(browser());
412 ASSERT_TRUE(content::ExecuteScript(
413 browser()->tab_strip_model()->GetActiveWebContents(),
414 "stealFocus();"));
416 // Make sure the location bar is still focused.
417 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
420 // Test forward and reverse focus traversal on a typical page.
421 // Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
422 // see https://crbug.com/60973.
423 #if defined(OS_MACOSX)
424 #define MAYBE_FocusTraversal DISABLED_FocusTraversal
425 #else
426 #define MAYBE_FocusTraversal FocusTraversal
427 #endif
428 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
429 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
430 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
431 ui_test_utils::NavigateToURL(browser(), url);
432 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
433 chrome::FocusLocationBar(browser());
435 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
436 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
437 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
440 // Test forward and reverse focus traversal while an interstitial is showing.
441 // Disabled, see http://crbug.com/60973
442 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusTraversalOnInterstitial) {
443 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
444 const GURL url = embedded_test_server()->GetURL(kSimplePage);
445 ui_test_utils::NavigateToURL(browser(), url);
446 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
448 // Create and show a test interstitial page.
449 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
450 browser()->tab_strip_model()->GetActiveWebContents());
451 content::RenderViewHost* host = interstitial_page->render_view_host();
453 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
454 chrome::FocusLocationBar(browser());
455 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
456 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
459 // Test the transfer of focus when an interstitial is shown and hidden.
460 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
461 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
462 const GURL url = embedded_test_server()->GetURL(kSimplePage);
463 ui_test_utils::NavigateToURL(browser(), url);
464 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
465 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
466 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
468 // Create and show a test interstitial page; it should gain focus.
469 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
470 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
471 EXPECT_TRUE(interstitial_page->HasFocus());
473 // Hide the interstitial; the original page should gain focus.
474 interstitial_page->DontProceed();
475 content::RunAllPendingInMessageLoop();
476 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
477 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
480 // Test that find-in-page UI can request focus, even when it is already open.
481 // flaky on Windows - http://crbug.com/523255
482 #if defined(OS_MACOSX) || defined(OS_WIN)
483 #define MAYBE_FindFocusTest DISABLED_FindFocusTest
484 #else
485 #define MAYBE_FindFocusTest FindFocusTest
486 #endif
487 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
488 chrome::DisableFindBarAnimationsDuringTesting(true);
489 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
490 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
491 ui_test_utils::NavigateToURL(browser(), url);
492 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
494 chrome::ShowFindBar(browser());
495 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
497 chrome::FocusLocationBar(browser());
498 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
500 chrome::ShowFindBar(browser());
501 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
503 ClickOnView(VIEW_ID_TAB_CONTAINER);
504 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
506 chrome::ShowFindBar(browser());
507 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
510 // Makes sure the focus is in the right location when opening the different
511 // types of tabs.
512 // Flaky, http://crbug.com/62539.
513 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
514 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
516 // Open the history tab, focus should be on the tab contents.
517 chrome::ShowHistory(browser());
518 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
519 browser()->tab_strip_model()->GetActiveWebContents()));
520 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
522 // Open the new tab, focus should be on the location bar.
523 chrome::NewTab(browser());
524 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
525 browser()->tab_strip_model()->GetActiveWebContents()));
526 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
528 // Open the download tab, focus should be on the tab contents.
529 chrome::ShowDownloads(browser());
530 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
531 browser()->tab_strip_model()->GetActiveWebContents()));
532 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
534 // Open about:blank, focus should be on the location bar.
535 chrome::AddSelectedTabWithURL(
536 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
537 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
538 browser()->tab_strip_model()->GetActiveWebContents()));
539 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
542 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
543 // TODO(erg): http://crbug.com/163931
544 #define MAYBE_FocusOnReload DISABLED_FocusOnReload
545 #else
546 #define MAYBE_FocusOnReload FocusOnReload
547 #endif
549 // Tests that focus goes where expected when using reload.
550 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
551 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
553 // Open the new tab, reload.
555 content::WindowedNotificationObserver observer(
556 content::NOTIFICATION_LOAD_STOP,
557 content::NotificationService::AllSources());
558 chrome::NewTab(browser());
559 observer.Wait();
561 content::RunAllPendingInMessageLoop();
564 content::WindowedNotificationObserver observer(
565 content::NOTIFICATION_LOAD_STOP,
566 content::Source<content::NavigationController>(
567 &browser()->tab_strip_model()->GetActiveWebContents()->
568 GetController()));
569 chrome::Reload(browser(), CURRENT_TAB);
570 observer.Wait();
572 // Focus should stay on the location bar.
573 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
575 // Open a regular page, focus the location bar, reload.
576 ui_test_utils::NavigateToURL(browser(),
577 embedded_test_server()->GetURL(kSimplePage));
578 chrome::FocusLocationBar(browser());
579 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
581 content::WindowedNotificationObserver observer(
582 content::NOTIFICATION_LOAD_STOP,
583 content::Source<content::NavigationController>(
584 &browser()->tab_strip_model()->GetActiveWebContents()->
585 GetController()));
586 chrome::Reload(browser(), CURRENT_TAB);
587 observer.Wait();
590 // Focus should now be on the tab contents.
591 chrome::ShowDownloads(browser());
592 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
595 // Tests that focus goes where expected when using reload on a crashed tab.
596 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
597 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
599 // Open a regular page, crash, reload.
600 ui_test_utils::NavigateToURL(browser(),
601 embedded_test_server()->GetURL(kSimplePage));
602 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
604 content::WindowedNotificationObserver observer(
605 content::NOTIFICATION_LOAD_STOP,
606 content::Source<content::NavigationController>(
607 &browser()->tab_strip_model()->GetActiveWebContents()->
608 GetController()));
609 chrome::Reload(browser(), CURRENT_TAB);
610 observer.Wait();
613 // Focus should now be on the tab contents.
614 chrome::ShowDownloads(browser());
615 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
618 // Tests that focus goes to frame after crashed tab.
619 // TODO(shrikant): Find out where the focus should be deterministically.
620 // Currently focused_view after crash seem to be non null in debug mode
621 // (invalidated pointer 0xcccccc).
622 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
623 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
625 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
627 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
630 // Tests that when a new tab is opened from the omnibox, the focus is moved from
631 // the omnibox for the current tab.
632 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
633 GURL url("http://www.google.com/");
634 GURL url2("http://maps.google.com/");
636 // Navigate to url.
637 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
638 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
639 p.disposition = CURRENT_TAB;
640 chrome::Navigate(&p);
642 // Focus the omnibox.
643 chrome::FocusLocationBar(browser());
645 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
646 GetOmniboxView()->model()->controller();
648 // Simulate an alt-enter.
649 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
650 ui::PAGE_TRANSITION_TYPED);
652 // Make sure the second tab is selected.
653 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
655 // The tab contents should have the focus in the second tab.
656 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
658 // Go back to the first tab. The focus should not be in the omnibox.
659 chrome::SelectPreviousTab(browser());
660 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
661 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
664 // This functionality is currently broken. http://crbug.com/304865.
666 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
667 //// TODO(erg): http://crbug.com/163931
668 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
669 //#else
670 //#define MAYBE_FocusOnNavigate FocusOnNavigate
671 //#endif
673 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
674 // Needed on Mac.
675 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
676 // Load the NTP.
677 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
678 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
680 // Navigate to another page.
681 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
682 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
683 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
684 ui_test_utils::NavigateToURL(browser(), file_url);
686 ClickOnView(VIEW_ID_TAB_CONTAINER);
688 // Navigate back. Should focus the location bar.
690 content::WindowedNotificationObserver back_nav_observer(
691 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
692 content::NotificationService::AllSources());
693 chrome::GoBack(browser(), CURRENT_TAB);
694 back_nav_observer.Wait();
697 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
699 // Navigate forward. Shouldn't focus the location bar.
700 ClickOnView(VIEW_ID_TAB_CONTAINER);
702 content::WindowedNotificationObserver forward_nav_observer(
703 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
704 content::NotificationService::AllSources());
705 chrome::GoForward(browser(), CURRENT_TAB);
706 forward_nav_observer.Wait();
709 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
712 } // namespace