Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / browser_focus_uitest.cc
blob9421bed46b90843725760bf897757322f3477b46
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 #if defined(OS_MACOSX)
482 #define MAYBE_FindFocusTest DISABLED_FindFocusTest
483 #else
484 #define MAYBE_FindFocusTest FindFocusTest
485 #endif
486 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
487 chrome::DisableFindBarAnimationsDuringTesting(true);
488 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
489 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
490 ui_test_utils::NavigateToURL(browser(), url);
491 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
493 chrome::ShowFindBar(browser());
494 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
496 chrome::FocusLocationBar(browser());
497 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
499 chrome::ShowFindBar(browser());
500 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
502 ClickOnView(VIEW_ID_TAB_CONTAINER);
503 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
505 chrome::ShowFindBar(browser());
506 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
509 // Makes sure the focus is in the right location when opening the different
510 // types of tabs.
511 // Flaky, http://crbug.com/62539.
512 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
513 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
515 // Open the history tab, focus should be on the tab contents.
516 chrome::ShowHistory(browser());
517 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
518 browser()->tab_strip_model()->GetActiveWebContents()));
519 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
521 // Open the new tab, focus should be on the location bar.
522 chrome::NewTab(browser());
523 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
524 browser()->tab_strip_model()->GetActiveWebContents()));
525 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
527 // Open the download tab, focus should be on the tab contents.
528 chrome::ShowDownloads(browser());
529 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
530 browser()->tab_strip_model()->GetActiveWebContents()));
531 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
533 // Open about:blank, focus should be on the location bar.
534 chrome::AddSelectedTabWithURL(
535 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
536 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
537 browser()->tab_strip_model()->GetActiveWebContents()));
538 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
541 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
542 // TODO(erg): http://crbug.com/163931
543 #define MAYBE_FocusOnReload DISABLED_FocusOnReload
544 #else
545 #define MAYBE_FocusOnReload FocusOnReload
546 #endif
548 // Tests that focus goes where expected when using reload.
549 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
550 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
552 // Open the new tab, reload.
554 content::WindowedNotificationObserver observer(
555 content::NOTIFICATION_LOAD_STOP,
556 content::NotificationService::AllSources());
557 chrome::NewTab(browser());
558 observer.Wait();
560 content::RunAllPendingInMessageLoop();
563 content::WindowedNotificationObserver observer(
564 content::NOTIFICATION_LOAD_STOP,
565 content::Source<content::NavigationController>(
566 &browser()->tab_strip_model()->GetActiveWebContents()->
567 GetController()));
568 chrome::Reload(browser(), CURRENT_TAB);
569 observer.Wait();
571 // Focus should stay on the location bar.
572 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
574 // Open a regular page, focus the location bar, reload.
575 ui_test_utils::NavigateToURL(browser(),
576 embedded_test_server()->GetURL(kSimplePage));
577 chrome::FocusLocationBar(browser());
578 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
580 content::WindowedNotificationObserver observer(
581 content::NOTIFICATION_LOAD_STOP,
582 content::Source<content::NavigationController>(
583 &browser()->tab_strip_model()->GetActiveWebContents()->
584 GetController()));
585 chrome::Reload(browser(), CURRENT_TAB);
586 observer.Wait();
589 // Focus should now be on the tab contents.
590 chrome::ShowDownloads(browser());
591 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
594 // Tests that focus goes where expected when using reload on a crashed tab.
595 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
596 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
598 // Open a regular page, crash, reload.
599 ui_test_utils::NavigateToURL(browser(),
600 embedded_test_server()->GetURL(kSimplePage));
601 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
603 content::WindowedNotificationObserver observer(
604 content::NOTIFICATION_LOAD_STOP,
605 content::Source<content::NavigationController>(
606 &browser()->tab_strip_model()->GetActiveWebContents()->
607 GetController()));
608 chrome::Reload(browser(), CURRENT_TAB);
609 observer.Wait();
612 // Focus should now be on the tab contents.
613 chrome::ShowDownloads(browser());
614 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
617 // Tests that focus goes to frame after crashed tab.
618 // TODO(shrikant): Find out where the focus should be deterministically.
619 // Currently focused_view after crash seem to be non null in debug mode
620 // (invalidated pointer 0xcccccc).
621 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
622 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
624 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
626 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
629 // Tests that when a new tab is opened from the omnibox, the focus is moved from
630 // the omnibox for the current tab.
631 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
632 GURL url("http://www.google.com/");
633 GURL url2("http://maps.google.com/");
635 // Navigate to url.
636 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
637 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
638 p.disposition = CURRENT_TAB;
639 chrome::Navigate(&p);
641 // Focus the omnibox.
642 chrome::FocusLocationBar(browser());
644 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
645 GetOmniboxView()->model()->controller();
647 // Simulate an alt-enter.
648 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
649 ui::PAGE_TRANSITION_TYPED);
651 // Make sure the second tab is selected.
652 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
654 // The tab contents should have the focus in the second tab.
655 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
657 // Go back to the first tab. The focus should not be in the omnibox.
658 chrome::SelectPreviousTab(browser());
659 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
660 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
663 // This functionality is currently broken. http://crbug.com/304865.
665 //#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
666 //// TODO(erg): http://crbug.com/163931
667 //#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
668 //#else
669 //#define MAYBE_FocusOnNavigate FocusOnNavigate
670 //#endif
672 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
673 // Needed on Mac.
674 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
675 // Load the NTP.
676 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
677 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
679 // Navigate to another page.
680 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
681 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
682 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
683 ui_test_utils::NavigateToURL(browser(), file_url);
685 ClickOnView(VIEW_ID_TAB_CONTAINER);
687 // Navigate back. Should focus the location bar.
689 content::WindowedNotificationObserver back_nav_observer(
690 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
691 content::NotificationService::AllSources());
692 chrome::GoBack(browser(), CURRENT_TAB);
693 back_nav_observer.Wait();
696 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
698 // Navigate forward. Shouldn't focus the location bar.
699 ClickOnView(VIEW_ID_TAB_CONTAINER);
701 content::WindowedNotificationObserver forward_nav_observer(
702 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
703 content::NotificationService::AllSources());
704 chrome::GoForward(browser(), CURRENT_TAB);
705 forward_nav_observer.Wait();
708 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
711 } // namespace