blob: 33fd1bd2a50c5da6e5443b3016d46c7fdc01cee0 [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]a9c060ca2012-01-05 20:43:415#include "base/bind.h"
thestig18dfb7a52014-08-26 10:44:046#include "base/files/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:427#include "base/format_macros.h"
[email protected]3cc55ad2013-07-17 22:17:418#include "base/message_loop/message_loop.h"
[email protected]3985ba82010-07-29 21:44:129#include "base/path_service.h"
[email protected]11521182013-06-11 04:06:3610#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1211#include "base/strings/utf_string_conversions.h"
[email protected]dcc8fbc2013-07-12 00:54:0912#include "chrome/browser/chrome_notification_types.h"
[email protected]7b5dc002010-11-16 23:08:1013#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1014#include "chrome/browser/ui/browser_commands.h"
[email protected]52877dbc62012-06-29 22:22:0315#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3316#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1217#include "chrome/browser/ui/chrome_pages.h"
[email protected]cfd33792014-05-23 20:39:3418#include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h"
[email protected]d57ce6a2014-07-03 15:39:2319#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]895a84722012-11-29 18:45:4420#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"
[email protected]b56e2e32012-05-11 21:18:0423#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1924#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3725#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5526#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3227#include "chrome/test/base/in_process_browser_test.h"
[email protected]3ea1b182013-02-08 22:38:4128#include "chrome/test/base/interactive_test_utils.h"
[email protected]477ae052011-11-18 23:53:5729#include "chrome/test/base/ui_test_utils.h"
[email protected]cadaec52012-02-08 21:53:1330#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1331#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5732#include "content/public/browser/notification_service.h"
[email protected]9c1662b2012-03-06 15:44:3333#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5834#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2035#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4236#include "content/public/test/browser_test_utils.h"
[email protected]c1dffe82013-06-26 20:59:0537#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5738
[email protected]e0ee4b42014-05-23 06:16:1339#if defined(OS_WIN)
[email protected]cfd33792014-05-23 20:39:3440#include "base/win/windows_version.h"
[email protected]e0ee4b42014-05-23 06:16:1341#endif
42
[email protected]eaabba22012-03-07 15:02:1143using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2544using content::WebContents;
45
[email protected]3a3cf4d2011-12-09 00:39:3546#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.
[email protected]e00ccc92012-11-01 17:32:3049#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5750#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0751// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2652#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4153#endif
54
initial.commit09911bf2008-07-26 23:55:2955namespace {
56
[email protected]8bcdec92009-02-25 16:15:1857// The delay waited in some cases where we don't have a notifications for an
58// action we take.
initial.commit09911bf2008-07-26 23:55:2959const int kActionDelayMs = 500;
60
[email protected]c1dffe82013-06-26 20:59:0561const char kSimplePage[] = "/focus/page_with_focus.html";
62const char kStealFocusPage[] = "/focus/page_steals_focus.html";
63const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4164
[email protected]8bcdec92009-02-25 16:15:1865class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2966 public:
[email protected]cfd33792014-05-23 20:39:3467 // InProcessBrowserTest overrides:
68 virtual void SetUpOnMainThread() OVERRIDE {
69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
70 }
71
[email protected]21abcc742009-10-23 02:52:0672 bool IsViewFocused(ViewID vid) {
73 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1774 }
75
[email protected]fc2e0872009-08-21 22:14:4176 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0677 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4178 }
[email protected]0b8fa8b2011-12-07 00:54:5279
[email protected]cfd33792014-05-23 20:39:3480 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;
90#elif defined(OS_WIN)
91 // This loop times out on Windows XP with no output. http://crbug.com/376635
92 if (base::win::GetVersion() < base::win::VERSION_VISTA)
93 return;
94#endif
95
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));
100 // Mac requires an extra Tab key press to traverse the app menu button
101 // iff "Full Keyboard Access" is enabled. This test code should probably
102 // check the setting via NSApplication's isFullKeyboardAccessEnabled.
103
104 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
105 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
106 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
107 // The details are the node's editable state, i.e. true for "textEdit".
108 bool is_editable_node = index == 0;
109
110 // Press Tab (or Shift+Tab) and check the focused element id.
111 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
112 browser(), key, false, reverse, false, false,
113 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
114 content::Source<RenderViewHost>(render_view_host),
115 content::Details<bool>(&is_editable_node)));
116 std::string focused_id;
117 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
118 render_view_host, kGetFocusedElementJS, &focused_id));
119 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
120 }
121
122#if defined(OS_MACOSX)
123 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
124 // would also apparently require extra Tab key presses here. Sigh...
125 chrome::FocusLocationBar(browser());
126#else
127 // On the last Tab key press, focus returns to the browser.
128 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
129 browser(), key, false, reverse, false, false,
130 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
131 content::Source<Browser>(browser())));
132#endif
133 content::RunAllPendingInMessageLoop();
134 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
135 }
[email protected]a9c060ca2012-01-05 20:43:41136 }
initial.commit09911bf2008-07-26 23:55:29137};
138
[email protected]cfd33792014-05-23 20:39:34139// A helper class that waits for an interstitial page to attach.
140class WaitForInterstitial : public content::WebContentsObserver {
[email protected]ca9077f2014-05-22 19:00:21141 public:
[email protected]cfd33792014-05-23 20:39:34142 explicit WaitForInterstitial(content::WebContents* tab)
143 : WebContentsObserver(tab),
144 runner_(new content::MessageLoopRunner) {
145 runner_->Run();
[email protected]9e0c83a2009-05-06 19:44:37146 }
147
[email protected]cfd33792014-05-23 20:39:34148 virtual void DidAttachInterstitialPage() OVERRIDE { runner_->Quit(); }
149 virtual void DidDetachInterstitialPage() OVERRIDE { NOTREACHED(); }
150
151 private:
152 scoped_refptr<content::MessageLoopRunner> runner_;
153 DISALLOW_COPY_AND_ASSIGN(WaitForInterstitial);
154};
155
156// A test interstitial page with typical HTML contents.
157class TestInterstitialPage : public content::InterstitialPageDelegate {
158 public:
159 explicit TestInterstitialPage(WebContents* tab) {
160 base::FilePath file_path;
161 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
162 EXPECT_TRUE(success);
163 file_path = file_path.AppendASCII("focus/typical_page.html");
164 success = base::ReadFileToString(file_path, &html_contents_);
165 EXPECT_TRUE(success);
166 interstitial_page_ = content::InterstitialPage::Create(
167 tab, true, GURL("http://interstitial.com"), this);
168
169 // Show the interstitial and delay return until it has attached.
170 interstitial_page_->Show();
171 WaitForInterstitial wait(tab);
172 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13173 }
[email protected]ca9077f2014-05-22 19:00:21174
[email protected]cfd33792014-05-23 20:39:34175 virtual std::string GetHTMLContents() OVERRIDE { return html_contents_; }
176
[email protected]85f0a572012-02-07 22:20:13177 RenderViewHost* render_view_host() {
[email protected]cadaec52012-02-08 21:53:13178 return interstitial_page_->GetRenderViewHostForTesting();
[email protected]9e0c83a2009-05-06 19:44:37179 }
180
[email protected]cfd33792014-05-23 20:39:34181 void DontProceed() { interstitial_page_->DontProceed(); }
[email protected]4e85c112011-01-26 22:27:21182
[email protected]cfd33792014-05-23 20:39:34183 bool HasFocus() { return render_view_host()->GetView()->HasFocus(); }
[email protected]9e0c83a2009-05-06 19:44:37184
185 private:
186 std::string html_contents_;
[email protected]cfd33792014-05-23 20:39:34187 content::InterstitialPage* interstitial_page_; // Owns this.
188 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
[email protected]9e0c83a2009-05-06 19:44:37189};
[email protected]b9821882009-08-17 22:25:17190
[email protected]c39e233d2012-05-18 13:24:12191// Flaky on mac. http://crbug.com/67301.
192#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23193#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12194#else
[email protected]839e0802012-05-18 14:45:23195#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12196#endif
197IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30198 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42199#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41200 // It seems we have to wait a little bit for the widgets to spin up before
201 // we can start clicking on them.
[email protected]3264d162013-05-08 22:02:11202 base::MessageLoop::current()->PostDelayedTask(
[email protected]7e560102012-03-08 20:58:42203 FROM_HERE,
[email protected]3264d162013-05-08 22:02:11204 base::MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42205 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26206 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42207#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41208
[email protected]5c4e4482013-03-03 00:09:18209 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00210
[email protected]fc2e0872009-08-21 22:14:41211 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21212 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00213
[email protected]5c4e4482013-03-03 00:09:18214 ClickOnView(VIEW_ID_OMNIBOX);
215 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00216}
[email protected]186f13f2009-08-19 20:34:00217
[email protected]320948e02011-01-10 08:21:12218// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26219IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30220 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34221 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18222 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29223
[email protected]90556dd2012-06-07 20:26:18224 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00225
initial.commit09911bf2008-07-26 23:55:29226 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21227 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29228 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57229 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29230 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21231 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29232
[email protected]a37d4b02012-06-25 21:56:10233 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18234 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29235 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57236 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29237 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18238 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29239}
240
241// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39242// Disabled, http://crbug.com/62542.
243IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30244 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34245 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18246 ui_test_utils::NavigateToURL(browser(), url);
247
initial.commit09911bf2008-07-26 23:55:29248 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03249 for (int i = 0; i < 4; ++i) {
250 chrome::AddSelectedTabWithURL(browser(), url,
251 content::PAGE_TRANSITION_TYPED);
252 }
initial.commit09911bf2008-07-26 23:55:29253
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 }
259 };
260
261 for (int i = 1; i < 3; i++) {
262 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18263 // Activate the tab.
[email protected]57892182012-12-03 19:15:39264 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29265
266 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54267 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45268 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54269 } else {
[email protected]a37d4b02012-06-25 21:56:10270 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54271 }
initial.commit09911bf2008-07-26 23:55:29272 }
273
274 // Now come back to the tab and check the right view is focused.
275 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18276 // Activate the tab.
[email protected]57892182012-12-03 19:15:39277 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29278
[email protected]5c4e4482013-03-03 00:09:18279 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06280 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29281 }
[email protected]cb7e2542009-12-14 22:02:35282
[email protected]57892182012-12-03 19:15:39283 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35284 // 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++) {
[email protected]f2159ba2012-04-17 19:13:21287 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18288 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35289 ASSERT_TRUE(IsViewFocused(vid));
290
[email protected]1d000682010-08-23 16:21:28291 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09292 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35293 }
294
295 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39296 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35297 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21298 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18299 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35300 ASSERT_TRUE(IsViewFocused(vid));
301
[email protected]1d000682010-08-23 16:21:28302 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09303 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35304 }
initial.commit09911bf2008-07-26 23:55:29305 }
306}
307
[email protected]ae40b572009-10-02 21:17:45308// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35309IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30310 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34311 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45312 ui_test_utils::NavigateToURL(browser(), url);
313
[email protected]a37d4b02012-06-25 21:56:10314 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07315 ui_test_utils::FindInPage(
316 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45317 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06318 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45319
320 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10321 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45322
323 // Create a 2nd tab.
[email protected]52877dbc62012-06-29 22:22:03324 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45325
326 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21327 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45328
329 // Select 1st tab, focus should still be on the location-bar.
330 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39331 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18332 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45333
334 // Now open the find box again, switch to another tab and come back, the focus
335 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10336 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06337 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39338 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21339 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39340 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06341 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45342}
343
initial.commit09911bf2008-07-26 23:55:29344// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07345// Flaky, http://crbug.com/62538.
346IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26347 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30348 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29349
initial.commit09911bf2008-07-26 23:55:29350 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21351 Browser* browser2 =
352 new Browser(Browser::CreateParams(browser()->profile(),
353 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18354 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37355 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18356 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00357
[email protected]ed179ee2009-10-03 21:02:51358 Browser* focused_browser = NULL;
359 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16360#if defined(USE_X11)
361 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00362 // 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 {
[email protected]579c3d82010-10-06 03:53:51370 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00371 }
372#elif defined(OS_WIN)
373 focused_browser = browser();
374 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57375#elif defined(OS_MACOSX)
376 // On Mac, the newly created window always gets the focus.
377 focused_browser = browser2;
378 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00379#endif
380
[email protected]cfd33792014-05-23 20:39:34381 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00382 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46383
[email protected]8bcdec92009-02-25 16:15:18384 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00385 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29386
[email protected]b6987e02013-01-04 18:30:43387 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05388 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13389 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29390
[email protected]8bcdec92009-02-25 16:15:18391 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00392 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29393}
394
[email protected]ddabf402013-10-22 22:43:08395#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
401
initial.commit09911bf2008-07-26 23:55:29402// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08403IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30404 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29405
406 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34407 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18408 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29409
[email protected]a37d4b02012-06-25 21:56:10410 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29411
[email protected]b6987e02013-01-04 18:30:43412 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07413 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13414 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29415
416 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18417 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29418}
419
[email protected]cfd33792014-05-23 20:39:34420// Test forward and reverse focus traversal on a typical page.
421IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30422 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34423 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18424 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34425 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10426 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29427
[email protected]cfd33792014-05-23 20:39:34428 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
429 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
430 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
initial.commit09911bf2008-07-26 23:55:29431}
432
[email protected]cfd33792014-05-23 20:39:34433// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29434// Disabled, see http://crbug.com/60973
435IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30436 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34437 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37438 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34439 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37440
[email protected]cfd33792014-05-23 20:39:34441 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07442 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34443 browser()->tab_strip_model()->GetActiveWebContents());
444 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37445
[email protected]cfd33792014-05-23 20:39:34446 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10447 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34448 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
449 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37450}
451
[email protected]cfd33792014-05-23 20:39:34452// Test the transfer of focus when an interstitial is shown and hidden.
453IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30454 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34455 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]e0ee4b42014-05-23 06:16:13456 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34457 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
458 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
459 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13460
[email protected]cfd33792014-05-23 20:39:34461 // Create and show a test interstitial page; it should gain focus.
462 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
463 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37464 EXPECT_TRUE(interstitial_page->HasFocus());
465
[email protected]cfd33792014-05-23 20:39:34466 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37467 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34468 content::RunAllPendingInMessageLoop();
469 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
470 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37471}
472
[email protected]cfd33792014-05-23 20:39:34473// Test that find-in-page UI can request focus, even when it is already open.
474IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
475 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30476 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34477 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13478 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34479 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13480
[email protected]cfd33792014-05-23 20:39:34481 chrome::ShowFindBar(browser());
482 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07483
[email protected]a37d4b02012-06-25 21:56:10484 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34485 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07486
[email protected]cfd33792014-05-23 20:39:34487 chrome::ShowFindBar(browser());
488 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07489
[email protected]fc2e0872009-08-21 22:14:41490 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34491 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07492
[email protected]cfd33792014-05-23 20:39:34493 chrome::ShowFindBar(browser());
494 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07495}
[email protected]401513c2009-03-12 00:21:28496
497// Makes sure the focus is in the right location when opening the different
498// types of tabs.
[email protected]c92928672010-11-09 18:31:07499// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26500IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30501 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07502
[email protected]401513c2009-03-12 00:21:28503 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12504 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35505 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07506 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21507 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28508
509 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10510 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35511 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07512 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18513 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28514
515 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12516 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35517 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07518 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21519 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43520
521 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17522 chrome::AddSelectedTabWithURL(
523 browser(), GURL(url::kAboutBlankURL), content::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35524 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07525 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18526 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28527}
[email protected]9d8a4642009-07-29 17:25:30528
[email protected]ddabf402013-10-22 22:43:08529#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
530// TODO(erg): http://crbug.com/163931
531#define MAYBE_FocusOnReload DISABLED_FocusOnReload
532#else
533#define MAYBE_FocusOnReload FocusOnReload
534#endif
535
[email protected]9d8a4642009-07-29 17:25:30536// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08537IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30538 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30539
[email protected]9d8a4642009-07-29 17:25:30540 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06541 {
[email protected]a7fe9112012-07-20 02:34:45542 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06543 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07544 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10545 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06546 observer.Wait();
547 }
[email protected]b8deecd2012-07-30 21:09:44548 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42549
[email protected]6b4e8e42011-08-17 19:36:06550 {
[email protected]a7fe9112012-07-20 02:34:45551 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06552 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34553 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07554 &browser()->tab_strip_model()->GetActiveWebContents()->
555 GetController()));
[email protected]a37d4b02012-06-25 21:56:10556 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06557 observer.Wait();
558 }
[email protected]9d8a4642009-07-29 17:25:30559 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18560 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30561
562 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05563 ui_test_utils::NavigateToURL(browser(),
564 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10565 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18566 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06567 {
[email protected]a7fe9112012-07-20 02:34:45568 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06569 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34570 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07571 &browser()->tab_strip_model()->GetActiveWebContents()->
572 GetController()));
[email protected]a37d4b02012-06-25 21:56:10573 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06574 observer.Wait();
575 }
[email protected]9ba21ede2010-07-30 01:11:07576
[email protected]9d8a4642009-07-29 17:25:30577 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12578 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21579 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30580}
581
582// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27583IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30584 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30585
[email protected]9d8a4642009-07-29 17:25:30586 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05587 ui_test_utils::NavigateToURL(browser(),
588 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07589 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06590 {
[email protected]a7fe9112012-07-20 02:34:45591 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06592 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34593 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07594 &browser()->tab_strip_model()->GetActiveWebContents()->
595 GetController()));
[email protected]a37d4b02012-06-25 21:56:10596 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06597 observer.Wait();
598 }
[email protected]9ba21ede2010-07-30 01:11:07599
[email protected]9d8a4642009-07-29 17:25:30600 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12601 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21602 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30603}
[email protected]629e0342010-07-27 23:30:13604
[email protected]09475062013-10-21 21:29:56605// Tests that focus goes to frame after crashed tab.
606// TODO(shrikant): Find out where the focus should be deterministically.
607// Currently focused_view after crash seem to be non null in debug mode
608// (invalidated pointer 0xcccccc).
609IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
610 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56611
612 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
613
614 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
615}
616
[email protected]895a84722012-11-29 18:45:44617// Tests that when a new tab is opened from the omnibox, the focus is moved from
618// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34619IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44620 GURL url("http://www.google.com/");
621 GURL url2("http://maps.google.com/");
622
623 // Navigate to url.
624 chrome::NavigateParams p(browser(), url, content::PAGE_TRANSITION_LINK);
625 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
626 p.disposition = CURRENT_TAB;
627 chrome::Navigate(&p);
628
629 // Focus the omnibox.
630 chrome::FocusLocationBar(browser());
631
[email protected]cfd33792014-05-23 20:39:34632 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
633 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44634
635 // Simulate an alt-enter.
636 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
[email protected]04ef14d2013-10-04 01:01:49637 content::PAGE_TRANSITION_TYPED);
[email protected]895a84722012-11-29 18:45:44638
639 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20640 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44641
642 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34643 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44644
645 // Go back to the first tab. The focus should not be in the omnibox.
646 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20647 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34648 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44649}
650
[email protected]19efce52013-11-14 00:42:28651// This functionality is currently broken. http://crbug.com/304865.
652//
653//#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
654//// TODO(erg): http://crbug.com/163931
655//#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
656//#else
657//#define MAYBE_FocusOnNavigate FocusOnNavigate
658//#endif
[email protected]ddabf402013-10-22 22:43:08659
[email protected]19efce52013-11-14 00:42:28660IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04661 // Needed on Mac.
662 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
663 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06664 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04665 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
666
667 // Navigate to another page.
668 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
669 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
670 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
671 ui_test_utils::NavigateToURL(browser(), file_url);
672
673 ClickOnView(VIEW_ID_TAB_CONTAINER);
674
675 // Navigate back. Should focus the location bar.
676 {
677 content::WindowedNotificationObserver back_nav_observer(
678 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
679 content::NotificationService::AllSources());
680 chrome::GoBack(browser(), CURRENT_TAB);
681 back_nav_observer.Wait();
682 }
683
684 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
685
686 // Navigate forward. Shouldn't focus the location bar.
687 ClickOnView(VIEW_ID_TAB_CONTAINER);
688 {
689 content::WindowedNotificationObserver forward_nav_observer(
690 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
691 content::NotificationService::AllSources());
692 chrome::GoForward(browser(), CURRENT_TAB);
693 forward_nav_observer.Wait();
694 }
695
696 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
697}
698
[email protected]629e0342010-07-27 23:30:13699} // namespace