ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / ui / browser_focus_uitest.cc
blob6ae9e605c61faafd4d83bcb195f1b1cac0eef302
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.
192 #if defined(OS_MACOSX)
193 #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
194 #else
195 #define MAYBE_ClickingMovesFocus ClickingMovesFocus
196 #endif
197 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
198 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
199 #if defined(OS_POSIX)
200 // It seems we have to wait a little bit for the widgets to spin up before
201 // we can start clicking on them.
202 base::MessageLoop::current()->task_runner()->PostDelayedTask(
203 FROM_HERE, base::MessageLoop::QuitClosure(),
204 base::TimeDelta::FromMilliseconds(kActionDelayMs));
205 content::RunMessageLoop();
206 #endif // defined(OS_POSIX)
208 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
210 ClickOnView(VIEW_ID_TAB_CONTAINER);
211 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
213 ClickOnView(VIEW_ID_OMNIBOX);
214 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
217 // Flaky, http://crbug.com/69034.
218 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
219 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
220 const GURL url = embedded_test_server()->GetURL(kSimplePage);
221 ui_test_utils::NavigateToURL(browser(), url);
223 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
225 // The focus should be on the Tab contents.
226 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
227 // Now hide the window, show it again, the focus should not have changed.
228 ui_test_utils::HideNativeWindow(window);
229 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
230 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
232 chrome::FocusLocationBar(browser());
233 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
234 // Hide the window, show it again, the focus should not have changed.
235 ui_test_utils::HideNativeWindow(window);
236 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
237 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
240 // Tabs remember focus.
241 // Disabled, http://crbug.com/62542.
242 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
243 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
244 const GURL url = embedded_test_server()->GetURL(kSimplePage);
245 ui_test_utils::NavigateToURL(browser(), url);
247 // Create several tabs.
248 for (int i = 0; i < 4; ++i) {
249 chrome::AddSelectedTabWithURL(browser(), url,
250 ui::PAGE_TRANSITION_TYPED);
253 // Alternate focus for the tab.
254 const bool kFocusPage[3][5] = {
255 { true, true, true, true, false },
256 { false, false, false, false, false },
257 { false, true, false, true, false }
260 for (int i = 1; i < 3; i++) {
261 for (int j = 0; j < 5; j++) {
262 // Activate the tab.
263 browser()->tab_strip_model()->ActivateTabAt(j, true);
265 // Activate the location bar or the page.
266 if (kFocusPage[i][j]) {
267 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
268 } else {
269 chrome::FocusLocationBar(browser());
273 // Now come back to the tab and check the right view is focused.
274 for (int j = 0; j < 5; j++) {
275 // Activate the tab.
276 browser()->tab_strip_model()->ActivateTabAt(j, true);
278 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
279 ASSERT_TRUE(IsViewFocused(vid));
282 browser()->tab_strip_model()->ActivateTabAt(0, true);
283 // Try the above, but with ctrl+tab. Since tab normally changes focus,
284 // this has regressed in the past. Loop through several times to be sure.
285 for (int j = 0; j < 15; j++) {
286 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
287 VIEW_ID_OMNIBOX;
288 ASSERT_TRUE(IsViewFocused(vid));
290 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
291 browser(), ui::VKEY_TAB, true, false, false, false));
294 // As above, but with ctrl+shift+tab.
295 browser()->tab_strip_model()->ActivateTabAt(4, true);
296 for (int j = 14; j >= 0; --j) {
297 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
298 VIEW_ID_OMNIBOX;
299 ASSERT_TRUE(IsViewFocused(vid));
301 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
302 browser(), ui::VKEY_TAB, true, true, false, false));
307 // Tabs remember focus with find-in-page box.
308 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
309 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
310 const GURL url = embedded_test_server()->GetURL(kSimplePage);
311 ui_test_utils::NavigateToURL(browser(), url);
313 chrome::Find(browser());
314 ui_test_utils::FindInPage(
315 browser()->tab_strip_model()->GetActiveWebContents(),
316 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
317 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
319 // Focus the location bar.
320 chrome::FocusLocationBar(browser());
322 // Create a 2nd tab.
323 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
325 // Focus should be on the recently opened tab page.
326 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
328 // Select 1st tab, focus should still be on the location-bar.
329 // (bug http://crbug.com/23296)
330 browser()->tab_strip_model()->ActivateTabAt(0, true);
331 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
333 // Now open the find box again, switch to another tab and come back, the focus
334 // should return to the find box.
335 chrome::Find(browser());
336 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
337 browser()->tab_strip_model()->ActivateTabAt(1, true);
338 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
339 browser()->tab_strip_model()->ActivateTabAt(0, true);
340 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
343 // Background window does not steal focus.
344 // Flaky, http://crbug.com/62538.
345 IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
346 DISABLED_BackgroundBrowserDontStealFocus) {
347 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
349 // Open a new browser window.
350 Browser* browser2 =
351 new Browser(Browser::CreateParams(browser()->profile(),
352 browser()->host_desktop_type()));
353 ASSERT_TRUE(browser2);
354 chrome::AddTabAt(browser2, GURL(), -1, true);
355 browser2->window()->Show();
357 Browser* focused_browser = NULL;
358 Browser* unfocused_browser = NULL;
359 #if defined(USE_X11)
360 // On X11, calling Activate() is not guaranteed to move focus, so we have
361 // to figure out which browser does have focus.
362 if (browser2->window()->IsActive()) {
363 focused_browser = browser2;
364 unfocused_browser = browser();
365 } else if (browser()->window()->IsActive()) {
366 focused_browser = browser();
367 unfocused_browser = browser2;
368 } else {
369 FAIL() << "Could not determine which browser has focus";
371 #elif defined(OS_WIN)
372 focused_browser = browser();
373 unfocused_browser = browser2;
374 #elif defined(OS_MACOSX)
375 // On Mac, the newly created window always gets the focus.
376 focused_browser = browser2;
377 unfocused_browser = browser();
378 #endif
380 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
381 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
383 // Activate the first browser.
384 focused_browser->window()->Activate();
386 ASSERT_TRUE(content::ExecuteScript(
387 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
388 "stealFocus();"));
390 // Make sure the first browser is still active.
391 EXPECT_TRUE(focused_browser->window()->IsActive());
394 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
395 // TODO(erg): http://crbug.com/163931
396 #define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
397 #else
398 #define MAYBE_LocationBarLockFocus LocationBarLockFocus
399 #endif
401 // Page cannot steal focus when focus is on location bar.
402 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
403 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
405 // Open the page that steals focus.
406 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
407 ui_test_utils::NavigateToURL(browser(), url);
409 chrome::FocusLocationBar(browser());
411 ASSERT_TRUE(content::ExecuteScript(
412 browser()->tab_strip_model()->GetActiveWebContents(),
413 "stealFocus();"));
415 // Make sure the location bar is still focused.
416 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
419 // Test forward and reverse focus traversal on a typical page.
420 // Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
421 // see https://crbug.com/60973.
422 #if defined(OS_MACOSX)
423 #define MAYBE_FocusTraversal DISABLED_FocusTraversal
424 #else
425 #define MAYBE_FocusTraversal FocusTraversal
426 #endif
427 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
428 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
429 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
430 ui_test_utils::NavigateToURL(browser(), url);
431 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
432 chrome::FocusLocationBar(browser());
434 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
435 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
436 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
439 // Test forward and reverse focus traversal while an interstitial is showing.
440 // Disabled, see http://crbug.com/60973
441 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusTraversalOnInterstitial) {
442 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
443 const GURL url = embedded_test_server()->GetURL(kSimplePage);
444 ui_test_utils::NavigateToURL(browser(), url);
445 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
447 // Create and show a test interstitial page.
448 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
449 browser()->tab_strip_model()->GetActiveWebContents());
450 content::RenderViewHost* host = interstitial_page->render_view_host();
452 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
453 chrome::FocusLocationBar(browser());
454 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
455 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
458 // Test the transfer of focus when an interstitial is shown and hidden.
459 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
460 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
461 const GURL url = embedded_test_server()->GetURL(kSimplePage);
462 ui_test_utils::NavigateToURL(browser(), url);
463 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
464 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
465 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
467 // Create and show a test interstitial page; it should gain focus.
468 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
469 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
470 EXPECT_TRUE(interstitial_page->HasFocus());
472 // Hide the interstitial; the original page should gain focus.
473 interstitial_page->DontProceed();
474 content::RunAllPendingInMessageLoop();
475 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
476 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
479 // Test that find-in-page UI can request focus, even when it is already open.
480 #if defined(OS_MACOSX)
481 #define MAYBE_FindFocusTest DISABLED_FindFocusTest
482 #else
483 #define MAYBE_FindFocusTest FindFocusTest
484 #endif
485 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
486 chrome::DisableFindBarAnimationsDuringTesting(true);
487 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
488 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
489 ui_test_utils::NavigateToURL(browser(), url);
490 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
492 chrome::ShowFindBar(browser());
493 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
495 chrome::FocusLocationBar(browser());
496 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
498 chrome::ShowFindBar(browser());
499 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
501 ClickOnView(VIEW_ID_TAB_CONTAINER);
502 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
504 chrome::ShowFindBar(browser());
505 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
508 // Makes sure the focus is in the right location when opening the different
509 // types of tabs.
510 // Flaky, http://crbug.com/62539.
511 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
512 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
514 // Open the history tab, focus should be on the tab contents.
515 chrome::ShowHistory(browser());
516 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
517 browser()->tab_strip_model()->GetActiveWebContents()));
518 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
520 // Open the new tab, focus should be on the location bar.
521 chrome::NewTab(browser());
522 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
523 browser()->tab_strip_model()->GetActiveWebContents()));
524 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
526 // Open the download tab, focus should be on the tab contents.
527 chrome::ShowDownloads(browser());
528 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
529 browser()->tab_strip_model()->GetActiveWebContents()));
530 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
532 // Open about:blank, focus should be on the location bar.
533 chrome::AddSelectedTabWithURL(
534 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
535 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
536 browser()->tab_strip_model()->GetActiveWebContents()));
537 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
540 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
541 // TODO(erg): http://crbug.com/163931
542 #define MAYBE_FocusOnReload DISABLED_FocusOnReload
543 #else
544 #define MAYBE_FocusOnReload FocusOnReload
545 #endif
547 // Tests that focus goes where expected when using reload.
548 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
549 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
551 // Open the new tab, reload.
553 content::WindowedNotificationObserver observer(
554 content::NOTIFICATION_LOAD_STOP,
555 content::NotificationService::AllSources());
556 chrome::NewTab(browser());
557 observer.Wait();
559 content::RunAllPendingInMessageLoop();
562 content::WindowedNotificationObserver observer(
563 content::NOTIFICATION_LOAD_STOP,
564 content::Source<content::NavigationController>(
565 &browser()->tab_strip_model()->GetActiveWebContents()->
566 GetController()));
567 chrome::Reload(browser(), CURRENT_TAB);
568 observer.Wait();
570 // Focus should stay on the location bar.
571 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
573 // Open a regular page, focus the location bar, reload.
574 ui_test_utils::NavigateToURL(browser(),
575 embedded_test_server()->GetURL(kSimplePage));
576 chrome::FocusLocationBar(browser());
577 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
579 content::WindowedNotificationObserver observer(
580 content::NOTIFICATION_LOAD_STOP,
581 content::Source<content::NavigationController>(
582 &browser()->tab_strip_model()->GetActiveWebContents()->
583 GetController()));
584 chrome::Reload(browser(), CURRENT_TAB);
585 observer.Wait();
588 // Focus should now be on the tab contents.
589 chrome::ShowDownloads(browser());
590 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
593 // Tests that focus goes where expected when using reload on a crashed tab.
594 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
595 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
597 // Open a regular page, crash, reload.
598 ui_test_utils::NavigateToURL(browser(),
599 embedded_test_server()->GetURL(kSimplePage));
600 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
602 content::WindowedNotificationObserver observer(
603 content::NOTIFICATION_LOAD_STOP,
604 content::Source<content::NavigationController>(
605 &browser()->tab_strip_model()->GetActiveWebContents()->
606 GetController()));
607 chrome::Reload(browser(), CURRENT_TAB);
608 observer.Wait();
611 // Focus should now be on the tab contents.
612 chrome::ShowDownloads(browser());
613 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
616 // Tests that focus goes to frame after crashed tab.
617 // TODO(shrikant): Find out where the focus should be deterministically.
618 // Currently focused_view after crash seem to be non null in debug mode
619 // (invalidated pointer 0xcccccc).
620 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
621 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
623 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
625 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
628 // Tests that when a new tab is opened from the omnibox, the focus is moved from
629 // the omnibox for the current tab.
630 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
631 GURL url("http://www.google.com/");
632 GURL url2("http://maps.google.com/");
634 // Navigate to url.
635 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
636 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
637 p.disposition = CURRENT_TAB;
638 chrome::Navigate(&p);
640 // Focus the omnibox.
641 chrome::FocusLocationBar(browser());
643 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
644 GetOmniboxView()->model()->controller();
646 // Simulate an alt-enter.
647 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
648 ui::PAGE_TRANSITION_TYPED);
650 // Make sure the second tab is selected.
651 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
653 // The tab contents should have the focus in the second tab.
654 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
656 // Go back to the first tab. The focus should not be in the omnibox.
657 chrome::SelectPreviousTab(browser());
658 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
659 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
662 // This functionality is currently broken. http://crbug.com/304865.
664 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
665 //// TODO(erg): http://crbug.com/163931
666 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
667 //#else
668 //#define MAYBE_FocusOnNavigate FocusOnNavigate
669 //#endif
671 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
672 // Needed on Mac.
673 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
674 // Load the NTP.
675 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
676 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
678 // Navigate to another page.
679 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
680 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
681 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
682 ui_test_utils::NavigateToURL(browser(), file_url);
684 ClickOnView(VIEW_ID_TAB_CONTAINER);
686 // Navigate back. Should focus the location bar.
688 content::WindowedNotificationObserver back_nav_observer(
689 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
690 content::NotificationService::AllSources());
691 chrome::GoBack(browser(), CURRENT_TAB);
692 back_nav_observer.Wait();
695 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
697 // Navigate forward. Shouldn't focus the location bar.
698 ClickOnView(VIEW_ID_TAB_CONTAINER);
700 content::WindowedNotificationObserver forward_nav_observer(
701 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
702 content::NotificationService::AllSources());
703 chrome::GoForward(browser(), CURRENT_TAB);
704 forward_nav_observer.Wait();
707 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
710 } // namespace