blob: b063b87455af01a1b0c7c5077fe187d8042036b7 [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"
khannansf3b27192015-01-09 17:28:1733#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3334#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5835#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2036#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4237#include "content/public/test/browser_test_utils.h"
[email protected]c1dffe82013-06-26 20:59:0538#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5739
[email protected]e0ee4b42014-05-23 06:16:1340#if defined(OS_WIN)
[email protected]cfd33792014-05-23 20:39:3441#include "base/win/windows_version.h"
[email protected]e0ee4b42014-05-23 06:16:1342#endif
43
[email protected]eaabba22012-03-07 15:02:1144using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2545using content::WebContents;
46
[email protected]3a3cf4d2011-12-09 00:39:3547#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.
[email protected]e00ccc92012-11-01 17:32:3050#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5751#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0752// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2653#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4154#endif
55
initial.commit09911bf2008-07-26 23:55:2956namespace {
57
mgiuca88867d32015-07-08 04:08:2458#if defined(OS_POSIX)
[email protected]8bcdec92009-02-25 16:15:1859// The delay waited in some cases where we don't have a notifications for an
60// action we take.
initial.commit09911bf2008-07-26 23:55:2961const int kActionDelayMs = 500;
mgiuca88867d32015-07-08 04:08:2462#endif
initial.commit09911bf2008-07-26 23:55:2963
[email protected]c1dffe82013-06-26 20:59:0564const char kSimplePage[] = "/focus/page_with_focus.html";
65const char kStealFocusPage[] = "/focus/page_steals_focus.html";
66const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4167
[email protected]8bcdec92009-02-25 16:15:1868class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2969 public:
[email protected]cfd33792014-05-23 20:39:3470 // InProcessBrowserTest overrides:
dcheng5dd5ff62014-10-21 12:42:3871 void SetUpOnMainThread() override {
[email protected]cfd33792014-05-23 20:39:3472 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
73 }
74
[email protected]21abcc742009-10-23 02:52:0675 bool IsViewFocused(ViewID vid) {
76 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1777 }
78
[email protected]fc2e0872009-08-21 22:14:4179 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0680 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4181 }
[email protected]0b8fa8b2011-12-07 00:54:5282
[email protected]cfd33792014-05-23 20:39:3483 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
98
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));
miu5d0cfcac2014-10-20 20:46:42103
[email protected]cfd33792014-05-23 20:39:34104 // Mac requires an extra Tab key press to traverse the app menu button
miu5d0cfcac2014-10-20 20:46:42105 // 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));
115 }
116 }
117 }
118#endif
[email protected]cfd33792014-05-23 20:39:34119
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;
125
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());
136 }
137
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));
151 }
[email protected]a9c060ca2012-01-05 20:43:41152 }
initial.commit09911bf2008-07-26 23:55:29153};
154
[email protected]cfd33792014-05-23 20:39:34155// A test interstitial page with typical HTML contents.
156class 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);
167
168 // Show the interstitial and delay return until it has attached.
169 interstitial_page_->Show();
meacerb47bc8782014-10-07 03:40:01170 content::WaitForInterstitialAttach(tab);
171
[email protected]cfd33792014-05-23 20:39:34172 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13173 }
[email protected]ca9077f2014-05-22 19:00:21174
dcheng5dd5ff62014-10-21 12:42:38175 std::string GetHTMLContents() override { return html_contents_; }
[email protected]cfd33792014-05-23 20:39:34176
[email protected]85f0a572012-02-07 22:20:13177 RenderViewHost* render_view_host() {
khannansf3b27192015-01-09 17:28:17178 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
[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.
skyostil380bb2222015-06-12 12:07:05202 base::MessageLoop::current()->task_runner()->PostDelayedTask(
203 FROM_HERE, base::MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42204 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26205 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42206#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41207
[email protected]5c4e4482013-03-03 00:09:18208 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00209
[email protected]fc2e0872009-08-21 22:14:41210 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21211 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00212
[email protected]5c4e4482013-03-03 00:09:18213 ClickOnView(VIEW_ID_OMNIBOX);
214 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00215}
[email protected]186f13f2009-08-19 20:34:00216
[email protected]320948e02011-01-10 08:21:12217// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26218IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30219 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34220 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18221 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29222
[email protected]90556dd2012-06-07 20:26:18223 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00224
initial.commit09911bf2008-07-26 23:55:29225 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21226 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29227 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57228 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29229 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21230 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29231
[email protected]a37d4b02012-06-25 21:56:10232 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18233 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29234 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57235 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29236 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18237 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29238}
239
240// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39241// Disabled, http://crbug.com/62542.
242IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30243 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34244 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18245 ui_test_utils::NavigateToURL(browser(), url);
246
initial.commit09911bf2008-07-26 23:55:29247 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03248 for (int i = 0; i < 4; ++i) {
249 chrome::AddSelectedTabWithURL(browser(), url,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35250 ui::PAGE_TRANSITION_TYPED);
[email protected]52877dbc62012-06-29 22:22:03251 }
initial.commit09911bf2008-07-26 23:55:29252
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 }
258 };
259
260 for (int i = 1; i < 3; i++) {
261 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18262 // Activate the tab.
[email protected]57892182012-12-03 19:15:39263 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29264
265 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54266 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45267 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54268 } else {
[email protected]a37d4b02012-06-25 21:56:10269 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54270 }
initial.commit09911bf2008-07-26 23:55:29271 }
272
273 // Now come back to the tab and check the right view is focused.
274 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18275 // Activate the tab.
[email protected]57892182012-12-03 19:15:39276 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29277
[email protected]5c4e4482013-03-03 00:09:18278 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06279 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29280 }
[email protected]cb7e2542009-12-14 22:02:35281
[email protected]57892182012-12-03 19:15:39282 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35283 // 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++) {
[email protected]f2159ba2012-04-17 19:13:21286 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18287 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35288 ASSERT_TRUE(IsViewFocused(vid));
289
[email protected]1d000682010-08-23 16:21:28290 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09291 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35292 }
293
294 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39295 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35296 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21297 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18298 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35299 ASSERT_TRUE(IsViewFocused(vid));
300
[email protected]1d000682010-08-23 16:21:28301 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09302 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35303 }
initial.commit09911bf2008-07-26 23:55:29304 }
305}
306
[email protected]ae40b572009-10-02 21:17:45307// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35308IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30309 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34310 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45311 ui_test_utils::NavigateToURL(browser(), url);
312
[email protected]a37d4b02012-06-25 21:56:10313 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07314 ui_test_utils::FindInPage(
315 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45316 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06317 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45318
319 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10320 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45321
322 // Create a 2nd tab.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35323 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45324
325 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21326 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45327
328 // Select 1st tab, focus should still be on the location-bar.
329 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39330 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18331 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45332
333 // Now open the find box again, switch to another tab and come back, the focus
334 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10335 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06336 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39337 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21338 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39339 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06340 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45341}
342
initial.commit09911bf2008-07-26 23:55:29343// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07344// Flaky, http://crbug.com/62538.
345IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26346 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30347 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29348
initial.commit09911bf2008-07-26 23:55:29349 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21350 Browser* browser2 =
351 new Browser(Browser::CreateParams(browser()->profile(),
352 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18353 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37354 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18355 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00356
[email protected]ed179ee2009-10-03 21:02:51357 Browser* focused_browser = NULL;
358 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16359#if defined(USE_X11)
360 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00361 // 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 {
[email protected]579c3d82010-10-06 03:53:51369 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00370 }
371#elif defined(OS_WIN)
372 focused_browser = browser();
373 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57374#elif defined(OS_MACOSX)
375 // On Mac, the newly created window always gets the focus.
376 focused_browser = browser2;
377 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00378#endif
379
[email protected]cfd33792014-05-23 20:39:34380 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00381 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46382
[email protected]8bcdec92009-02-25 16:15:18383 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00384 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29385
[email protected]b6987e02013-01-04 18:30:43386 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05387 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13388 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29389
[email protected]8bcdec92009-02-25 16:15:18390 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00391 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29392}
393
[email protected]ddabf402013-10-22 22:43:08394#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
400
initial.commit09911bf2008-07-26 23:55:29401// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08402IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30403 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29404
405 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34406 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18407 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29408
[email protected]a37d4b02012-06-25 21:56:10409 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29410
[email protected]b6987e02013-01-04 18:30:43411 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07412 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13413 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29414
415 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18416 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29417}
418
[email protected]cfd33792014-05-23 20:39:34419// Test forward and reverse focus traversal on a typical page.
bnc0f8fe372014-11-24 19:42:51420// 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
427IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30428 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34429 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18430 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34431 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10432 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29433
[email protected]cfd33792014-05-23 20:39:34434 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));
initial.commit09911bf2008-07-26 23:55:29437}
438
[email protected]cfd33792014-05-23 20:39:34439// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29440// Disabled, see http://crbug.com/60973
441IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30442 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34443 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37444 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34445 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37446
[email protected]cfd33792014-05-23 20:39:34447 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07448 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34449 browser()->tab_strip_model()->GetActiveWebContents());
450 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37451
[email protected]cfd33792014-05-23 20:39:34452 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10453 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34454 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
455 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37456}
457
[email protected]cfd33792014-05-23 20:39:34458// Test the transfer of focus when an interstitial is shown and hidden.
459IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30460 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34461 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]e0ee4b42014-05-23 06:16:13462 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34463 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
464 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
465 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13466
[email protected]cfd33792014-05-23 20:39:34467 // 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));
[email protected]9e0c83a2009-05-06 19:44:37470 EXPECT_TRUE(interstitial_page->HasFocus());
471
[email protected]cfd33792014-05-23 20:39:34472 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37473 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34474 content::RunAllPendingInMessageLoop();
475 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
476 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37477}
478
[email protected]cfd33792014-05-23 20:39:34479// Test that find-in-page UI can request focus, even when it is already open.
John Abd-El-Malekf3fdd622014-09-09 04:04:23480#if defined(OS_MACOSX)
481#define MAYBE_FindFocusTest DISABLED_FindFocusTest
482#else
483#define MAYBE_FindFocusTest FindFocusTest
484#endif
485IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]cfd33792014-05-23 20:39:34486 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30487 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34488 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13489 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34490 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13491
[email protected]cfd33792014-05-23 20:39:34492 chrome::ShowFindBar(browser());
493 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07494
[email protected]a37d4b02012-06-25 21:56:10495 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34496 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07497
[email protected]cfd33792014-05-23 20:39:34498 chrome::ShowFindBar(browser());
499 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07500
[email protected]fc2e0872009-08-21 22:14:41501 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34502 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07503
[email protected]cfd33792014-05-23 20:39:34504 chrome::ShowFindBar(browser());
505 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07506}
[email protected]401513c2009-03-12 00:21:28507
508// Makes sure the focus is in the right location when opening the different
509// types of tabs.
[email protected]c92928672010-11-09 18:31:07510// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26511IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30512 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07513
[email protected]401513c2009-03-12 00:21:28514 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12515 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35516 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07517 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21518 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28519
520 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10521 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35522 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07523 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18524 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28525
526 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12527 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35528 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07529 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21530 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43531
532 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17533 chrome::AddSelectedTabWithURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35534 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35535 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07536 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18537 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28538}
[email protected]9d8a4642009-07-29 17:25:30539
[email protected]ddabf402013-10-22 22:43:08540#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
546
[email protected]9d8a4642009-07-29 17:25:30547// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08548IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30549 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30550
[email protected]9d8a4642009-07-29 17:25:30551 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06552 {
[email protected]a7fe9112012-07-20 02:34:45553 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06554 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07555 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10556 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06557 observer.Wait();
558 }
[email protected]b8deecd2012-07-30 21:09:44559 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42560
[email protected]6b4e8e42011-08-17 19:36:06561 {
[email protected]a7fe9112012-07-20 02:34:45562 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06563 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34564 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07565 &browser()->tab_strip_model()->GetActiveWebContents()->
566 GetController()));
[email protected]a37d4b02012-06-25 21:56:10567 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06568 observer.Wait();
569 }
[email protected]9d8a4642009-07-29 17:25:30570 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18571 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30572
573 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05574 ui_test_utils::NavigateToURL(browser(),
575 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10576 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18577 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06578 {
[email protected]a7fe9112012-07-20 02:34:45579 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06580 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34581 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07582 &browser()->tab_strip_model()->GetActiveWebContents()->
583 GetController()));
[email protected]a37d4b02012-06-25 21:56:10584 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06585 observer.Wait();
586 }
[email protected]9ba21ede2010-07-30 01:11:07587
[email protected]9d8a4642009-07-29 17:25:30588 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12589 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21590 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30591}
592
593// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27594IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30595 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30596
[email protected]9d8a4642009-07-29 17:25:30597 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05598 ui_test_utils::NavigateToURL(browser(),
599 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07600 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06601 {
[email protected]a7fe9112012-07-20 02:34:45602 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06603 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34604 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07605 &browser()->tab_strip_model()->GetActiveWebContents()->
606 GetController()));
[email protected]a37d4b02012-06-25 21:56:10607 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06608 observer.Wait();
609 }
[email protected]9ba21ede2010-07-30 01:11:07610
[email protected]9d8a4642009-07-29 17:25:30611 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12612 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21613 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30614}
[email protected]629e0342010-07-27 23:30:13615
[email protected]09475062013-10-21 21:29:56616// 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).
620IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
621 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56622
623 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
624
625 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
626}
627
[email protected]895a84722012-11-29 18:45:44628// Tests that when a new tab is opened from the omnibox, the focus is moved from
629// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34630IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44631 GURL url("http://www.google.com/");
632 GURL url2("http://maps.google.com/");
633
634 // Navigate to url.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35635 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
[email protected]895a84722012-11-29 18:45:44636 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
637 p.disposition = CURRENT_TAB;
638 chrome::Navigate(&p);
639
640 // Focus the omnibox.
641 chrome::FocusLocationBar(browser());
642
[email protected]cfd33792014-05-23 20:39:34643 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
644 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44645
646 // Simulate an alt-enter.
647 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35648 ui::PAGE_TRANSITION_TYPED);
[email protected]895a84722012-11-29 18:45:44649
650 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20651 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44652
653 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34654 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44655
656 // Go back to the first tab. The focus should not be in the omnibox.
657 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20658 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34659 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44660}
661
[email protected]19efce52013-11-14 00:42:28662// This functionality is currently broken. http://crbug.com/304865.
663//
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
[email protected]ddabf402013-10-22 22:43:08670
[email protected]19efce52013-11-14 00:42:28671IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04672 // Needed on Mac.
673 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
674 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06675 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04676 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
677
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);
683
684 ClickOnView(VIEW_ID_TAB_CONTAINER);
685
686 // Navigate back. Should focus the location bar.
687 {
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();
693 }
694
695 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
696
697 // Navigate forward. Shouldn't focus the location bar.
698 ClickOnView(VIEW_ID_TAB_CONTAINER);
699 {
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();
705 }
706
707 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
708}
709
[email protected]629e0342010-07-27 23:30:13710} // namespace