blob: e5ea8904f3659eaad4265be2dcc8cfc1f14b9781 [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
[email protected]8bcdec92009-02-25 16:15:1858// The delay waited in some cases where we don't have a notifications for an
59// action we take.
initial.commit09911bf2008-07-26 23:55:2960const int kActionDelayMs = 500;
61
[email protected]c1dffe82013-06-26 20:59:0562const char kSimplePage[] = "/focus/page_with_focus.html";
63const char kStealFocusPage[] = "/focus/page_steals_focus.html";
64const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4165
[email protected]8bcdec92009-02-25 16:15:1866class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2967 public:
[email protected]cfd33792014-05-23 20:39:3468 // InProcessBrowserTest overrides:
dcheng5dd5ff62014-10-21 12:42:3869 void SetUpOnMainThread() override {
[email protected]cfd33792014-05-23 20:39:3470 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
71 }
72
[email protected]21abcc742009-10-23 02:52:0673 bool IsViewFocused(ViewID vid) {
74 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1775 }
76
[email protected]fc2e0872009-08-21 22:14:4177 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0678 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4179 }
[email protected]0b8fa8b2011-12-07 00:54:5280
[email protected]cfd33792014-05-23 20:39:3481 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) {
82 const char kGetFocusedElementJS[] =
83 "window.domAutomationController.send(getFocusedElement());";
84 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton",
85 "googleLink", "gmailLink", "gmapLink" };
86 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse));
87 ui::KeyboardCode key = ui::VKEY_TAB;
88#if defined(OS_MACOSX)
89 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
90 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB;
91#elif defined(OS_WIN)
92 // This loop times out on Windows XP with no output. http://crbug.com/376635
93 if (base::win::GetVersion() < base::win::VERSION_VISTA)
94 return;
95#endif
96
97 // Loop through the focus chain twice for good measure.
98 for (size_t i = 0; i < 2; ++i) {
99 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i));
100 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
miu5d0cfcac2014-10-20 20:46:42101
[email protected]cfd33792014-05-23 20:39:34102 // Mac requires an extra Tab key press to traverse the app menu button
miu5d0cfcac2014-10-20 20:46:42103 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
104 // are required to traverse the back/forward buttons and the tab strip.
105#if defined(OS_MACOSX)
106 if (ui_controls::IsFullKeyboardAccessEnabled()) {
107 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
108 browser(), key, false, reverse, false, false));
109 if (reverse) {
110 for (int j = 0; j < 3; ++j) {
111 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
112 browser(), key, false, reverse, false, false));
113 }
114 }
115 }
116#endif
[email protected]cfd33792014-05-23 20:39:34117
118 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
119 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
120 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
121 // The details are the node's editable state, i.e. true for "textEdit".
122 bool is_editable_node = index == 0;
123
124 // Press Tab (or Shift+Tab) and check the focused element id.
125 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
126 browser(), key, false, reverse, false, false,
127 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
128 content::Source<RenderViewHost>(render_view_host),
129 content::Details<bool>(&is_editable_node)));
130 std::string focused_id;
131 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
132 render_view_host, kGetFocusedElementJS, &focused_id));
133 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
134 }
135
136#if defined(OS_MACOSX)
137 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
138 // would also apparently require extra Tab key presses here. Sigh...
139 chrome::FocusLocationBar(browser());
140#else
141 // On the last Tab key press, focus returns to the browser.
142 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
143 browser(), key, false, reverse, false, false,
144 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
145 content::Source<Browser>(browser())));
146#endif
147 content::RunAllPendingInMessageLoop();
148 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
149 }
[email protected]a9c060ca2012-01-05 20:43:41150 }
initial.commit09911bf2008-07-26 23:55:29151};
152
[email protected]cfd33792014-05-23 20:39:34153// A test interstitial page with typical HTML contents.
154class TestInterstitialPage : public content::InterstitialPageDelegate {
155 public:
156 explicit TestInterstitialPage(WebContents* tab) {
157 base::FilePath file_path;
158 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
159 EXPECT_TRUE(success);
160 file_path = file_path.AppendASCII("focus/typical_page.html");
161 success = base::ReadFileToString(file_path, &html_contents_);
162 EXPECT_TRUE(success);
163 interstitial_page_ = content::InterstitialPage::Create(
164 tab, true, GURL("http://interstitial.com"), this);
165
166 // Show the interstitial and delay return until it has attached.
167 interstitial_page_->Show();
meacerb47bc8782014-10-07 03:40:01168 content::WaitForInterstitialAttach(tab);
169
[email protected]cfd33792014-05-23 20:39:34170 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13171 }
[email protected]ca9077f2014-05-22 19:00:21172
dcheng5dd5ff62014-10-21 12:42:38173 std::string GetHTMLContents() override { return html_contents_; }
[email protected]cfd33792014-05-23 20:39:34174
[email protected]85f0a572012-02-07 22:20:13175 RenderViewHost* render_view_host() {
khannansf3b27192015-01-09 17:28:17176 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
[email protected]9e0c83a2009-05-06 19:44:37177 }
178
[email protected]cfd33792014-05-23 20:39:34179 void DontProceed() { interstitial_page_->DontProceed(); }
[email protected]4e85c112011-01-26 22:27:21180
[email protected]cfd33792014-05-23 20:39:34181 bool HasFocus() { return render_view_host()->GetView()->HasFocus(); }
[email protected]9e0c83a2009-05-06 19:44:37182
183 private:
184 std::string html_contents_;
[email protected]cfd33792014-05-23 20:39:34185 content::InterstitialPage* interstitial_page_; // Owns this.
186 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
[email protected]9e0c83a2009-05-06 19:44:37187};
[email protected]b9821882009-08-17 22:25:17188
[email protected]c39e233d2012-05-18 13:24:12189// Flaky on mac. http://crbug.com/67301.
190#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23191#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12192#else
[email protected]839e0802012-05-18 14:45:23193#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12194#endif
195IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30196 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42197#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41198 // It seems we have to wait a little bit for the widgets to spin up before
199 // we can start clicking on them.
skyostil380bb2222015-06-12 12:07:05200 base::MessageLoop::current()->task_runner()->PostDelayedTask(
201 FROM_HERE, base::MessageLoop::QuitClosure(),
[email protected]7e560102012-03-08 20:58:42202 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26203 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42204#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41205
[email protected]5c4e4482013-03-03 00:09:18206 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00207
[email protected]fc2e0872009-08-21 22:14:41208 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21209 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00210
[email protected]5c4e4482013-03-03 00:09:18211 ClickOnView(VIEW_ID_OMNIBOX);
212 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00213}
[email protected]186f13f2009-08-19 20:34:00214
[email protected]320948e02011-01-10 08:21:12215// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26216IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30217 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34218 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18219 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29220
[email protected]90556dd2012-06-07 20:26:18221 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00222
initial.commit09911bf2008-07-26 23:55:29223 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21224 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29225 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57226 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29227 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21228 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29229
[email protected]a37d4b02012-06-25 21:56:10230 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18231 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29232 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57233 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29234 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18235 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29236}
237
238// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39239// Disabled, http://crbug.com/62542.
240IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30241 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34242 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18243 ui_test_utils::NavigateToURL(browser(), url);
244
initial.commit09911bf2008-07-26 23:55:29245 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03246 for (int i = 0; i < 4; ++i) {
247 chrome::AddSelectedTabWithURL(browser(), url,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35248 ui::PAGE_TRANSITION_TYPED);
[email protected]52877dbc62012-06-29 22:22:03249 }
initial.commit09911bf2008-07-26 23:55:29250
251 // Alternate focus for the tab.
252 const bool kFocusPage[3][5] = {
253 { true, true, true, true, false },
254 { false, false, false, false, false },
255 { false, true, false, true, false }
256 };
257
258 for (int i = 1; i < 3; i++) {
259 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18260 // Activate the tab.
[email protected]57892182012-12-03 19:15:39261 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29262
263 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54264 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45265 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54266 } else {
[email protected]a37d4b02012-06-25 21:56:10267 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54268 }
initial.commit09911bf2008-07-26 23:55:29269 }
270
271 // Now come back to the tab and check the right view is focused.
272 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18273 // Activate the tab.
[email protected]57892182012-12-03 19:15:39274 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29275
[email protected]5c4e4482013-03-03 00:09:18276 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06277 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29278 }
[email protected]cb7e2542009-12-14 22:02:35279
[email protected]57892182012-12-03 19:15:39280 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35281 // Try the above, but with ctrl+tab. Since tab normally changes focus,
282 // this has regressed in the past. Loop through several times to be sure.
283 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21284 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18285 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35286 ASSERT_TRUE(IsViewFocused(vid));
287
[email protected]1d000682010-08-23 16:21:28288 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09289 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35290 }
291
292 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39293 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35294 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21295 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18296 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35297 ASSERT_TRUE(IsViewFocused(vid));
298
[email protected]1d000682010-08-23 16:21:28299 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09300 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35301 }
initial.commit09911bf2008-07-26 23:55:29302 }
303}
304
[email protected]ae40b572009-10-02 21:17:45305// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35306IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30307 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34308 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45309 ui_test_utils::NavigateToURL(browser(), url);
310
[email protected]a37d4b02012-06-25 21:56:10311 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07312 ui_test_utils::FindInPage(
313 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45314 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06315 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45316
317 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10318 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45319
320 // Create a 2nd tab.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35321 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45322
323 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21324 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45325
326 // Select 1st tab, focus should still be on the location-bar.
327 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39328 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18329 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45330
331 // Now open the find box again, switch to another tab and come back, the focus
332 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10333 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06334 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39335 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21336 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39337 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06338 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45339}
340
initial.commit09911bf2008-07-26 23:55:29341// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07342// Flaky, http://crbug.com/62538.
343IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26344 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30345 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29346
initial.commit09911bf2008-07-26 23:55:29347 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21348 Browser* browser2 =
349 new Browser(Browser::CreateParams(browser()->profile(),
350 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18351 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37352 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18353 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00354
[email protected]ed179ee2009-10-03 21:02:51355 Browser* focused_browser = NULL;
356 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16357#if defined(USE_X11)
358 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00359 // to figure out which browser does have focus.
360 if (browser2->window()->IsActive()) {
361 focused_browser = browser2;
362 unfocused_browser = browser();
363 } else if (browser()->window()->IsActive()) {
364 focused_browser = browser();
365 unfocused_browser = browser2;
366 } else {
[email protected]579c3d82010-10-06 03:53:51367 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00368 }
369#elif defined(OS_WIN)
370 focused_browser = browser();
371 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57372#elif defined(OS_MACOSX)
373 // On Mac, the newly created window always gets the focus.
374 focused_browser = browser2;
375 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00376#endif
377
[email protected]cfd33792014-05-23 20:39:34378 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00379 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46380
[email protected]8bcdec92009-02-25 16:15:18381 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00382 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29383
[email protected]b6987e02013-01-04 18:30:43384 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05385 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13386 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29387
[email protected]8bcdec92009-02-25 16:15:18388 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00389 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29390}
391
[email protected]ddabf402013-10-22 22:43:08392#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
393// TODO(erg): http://crbug.com/163931
394#define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
395#else
396#define MAYBE_LocationBarLockFocus LocationBarLockFocus
397#endif
398
initial.commit09911bf2008-07-26 23:55:29399// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08400IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30401 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29402
403 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34404 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18405 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29406
[email protected]a37d4b02012-06-25 21:56:10407 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29408
[email protected]b6987e02013-01-04 18:30:43409 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07410 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13411 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29412
413 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18414 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29415}
416
[email protected]cfd33792014-05-23 20:39:34417// Test forward and reverse focus traversal on a typical page.
bnc0f8fe372014-11-24 19:42:51418// Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
419// see https://crbug.com/60973.
420#if defined(OS_MACOSX)
421#define MAYBE_FocusTraversal DISABLED_FocusTraversal
422#else
423#define MAYBE_FocusTraversal FocusTraversal
424#endif
425IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30426 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34427 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18428 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34429 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10430 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29431
[email protected]cfd33792014-05-23 20:39:34432 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
433 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
434 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
initial.commit09911bf2008-07-26 23:55:29435}
436
[email protected]cfd33792014-05-23 20:39:34437// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29438// Disabled, see http://crbug.com/60973
439IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30440 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34441 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37442 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34443 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37444
[email protected]cfd33792014-05-23 20:39:34445 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07446 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34447 browser()->tab_strip_model()->GetActiveWebContents());
448 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37449
[email protected]cfd33792014-05-23 20:39:34450 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10451 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34452 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
453 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37454}
455
[email protected]cfd33792014-05-23 20:39:34456// Test the transfer of focus when an interstitial is shown and hidden.
457IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30458 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34459 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]e0ee4b42014-05-23 06:16:13460 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34461 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
462 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
463 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13464
[email protected]cfd33792014-05-23 20:39:34465 // Create and show a test interstitial page; it should gain focus.
466 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
467 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37468 EXPECT_TRUE(interstitial_page->HasFocus());
469
[email protected]cfd33792014-05-23 20:39:34470 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37471 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34472 content::RunAllPendingInMessageLoop();
473 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
474 EXPECT_TRUE(tab->GetRenderViewHost()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37475}
476
[email protected]cfd33792014-05-23 20:39:34477// Test that find-in-page UI can request focus, even when it is already open.
John Abd-El-Malekf3fdd622014-09-09 04:04:23478#if defined(OS_MACOSX)
479#define MAYBE_FindFocusTest DISABLED_FindFocusTest
480#else
481#define MAYBE_FindFocusTest FindFocusTest
482#endif
483IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]cfd33792014-05-23 20:39:34484 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30485 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34486 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13487 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34488 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13489
[email protected]cfd33792014-05-23 20:39:34490 chrome::ShowFindBar(browser());
491 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07492
[email protected]a37d4b02012-06-25 21:56:10493 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34494 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07495
[email protected]cfd33792014-05-23 20:39:34496 chrome::ShowFindBar(browser());
497 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07498
[email protected]fc2e0872009-08-21 22:14:41499 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34500 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07501
[email protected]cfd33792014-05-23 20:39:34502 chrome::ShowFindBar(browser());
503 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07504}
[email protected]401513c2009-03-12 00:21:28505
506// Makes sure the focus is in the right location when opening the different
507// types of tabs.
[email protected]c92928672010-11-09 18:31:07508// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26509IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30510 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07511
[email protected]401513c2009-03-12 00:21:28512 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12513 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35514 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07515 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21516 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28517
518 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10519 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35520 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07521 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18522 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28523
524 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12525 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35526 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07527 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21528 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43529
530 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17531 chrome::AddSelectedTabWithURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35532 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35533 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07534 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18535 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28536}
[email protected]9d8a4642009-07-29 17:25:30537
[email protected]ddabf402013-10-22 22:43:08538#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
539// TODO(erg): http://crbug.com/163931
540#define MAYBE_FocusOnReload DISABLED_FocusOnReload
541#else
542#define MAYBE_FocusOnReload FocusOnReload
543#endif
544
[email protected]9d8a4642009-07-29 17:25:30545// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08546IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30547 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30548
[email protected]9d8a4642009-07-29 17:25:30549 // Open the new tab, reload.
[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]ad50def52011-10-19 23:17:07553 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10554 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06555 observer.Wait();
556 }
[email protected]b8deecd2012-07-30 21:09:44557 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42558
[email protected]6b4e8e42011-08-17 19:36:06559 {
[email protected]a7fe9112012-07-20 02:34:45560 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06561 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34562 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07563 &browser()->tab_strip_model()->GetActiveWebContents()->
564 GetController()));
[email protected]a37d4b02012-06-25 21:56:10565 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06566 observer.Wait();
567 }
[email protected]9d8a4642009-07-29 17:25:30568 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18569 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30570
571 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05572 ui_test_utils::NavigateToURL(browser(),
573 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10574 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18575 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06576 {
[email protected]a7fe9112012-07-20 02:34:45577 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06578 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34579 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07580 &browser()->tab_strip_model()->GetActiveWebContents()->
581 GetController()));
[email protected]a37d4b02012-06-25 21:56:10582 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06583 observer.Wait();
584 }
[email protected]9ba21ede2010-07-30 01:11:07585
[email protected]9d8a4642009-07-29 17:25:30586 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12587 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21588 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30589}
590
591// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27592IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30593 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30594
[email protected]9d8a4642009-07-29 17:25:30595 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05596 ui_test_utils::NavigateToURL(browser(),
597 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07598 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06599 {
[email protected]a7fe9112012-07-20 02:34:45600 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06601 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34602 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07603 &browser()->tab_strip_model()->GetActiveWebContents()->
604 GetController()));
[email protected]a37d4b02012-06-25 21:56:10605 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06606 observer.Wait();
607 }
[email protected]9ba21ede2010-07-30 01:11:07608
[email protected]9d8a4642009-07-29 17:25:30609 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12610 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21611 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30612}
[email protected]629e0342010-07-27 23:30:13613
[email protected]09475062013-10-21 21:29:56614// Tests that focus goes to frame after crashed tab.
615// TODO(shrikant): Find out where the focus should be deterministically.
616// Currently focused_view after crash seem to be non null in debug mode
617// (invalidated pointer 0xcccccc).
618IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
619 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56620
621 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
622
623 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
624}
625
[email protected]895a84722012-11-29 18:45:44626// Tests that when a new tab is opened from the omnibox, the focus is moved from
627// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34628IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44629 GURL url("http://www.google.com/");
630 GURL url2("http://maps.google.com/");
631
632 // Navigate to url.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35633 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
[email protected]895a84722012-11-29 18:45:44634 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
635 p.disposition = CURRENT_TAB;
636 chrome::Navigate(&p);
637
638 // Focus the omnibox.
639 chrome::FocusLocationBar(browser());
640
[email protected]cfd33792014-05-23 20:39:34641 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
642 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44643
644 // Simulate an alt-enter.
645 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35646 ui::PAGE_TRANSITION_TYPED);
[email protected]895a84722012-11-29 18:45:44647
648 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20649 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44650
651 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34652 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44653
654 // Go back to the first tab. The focus should not be in the omnibox.
655 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20656 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34657 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44658}
659
[email protected]19efce52013-11-14 00:42:28660// This functionality is currently broken. http://crbug.com/304865.
661//
662//#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
663//// TODO(erg): http://crbug.com/163931
664//#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
665//#else
666//#define MAYBE_FocusOnNavigate FocusOnNavigate
667//#endif
[email protected]ddabf402013-10-22 22:43:08668
[email protected]19efce52013-11-14 00:42:28669IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04670 // Needed on Mac.
671 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
672 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06673 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04674 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
675
676 // Navigate to another page.
677 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
678 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
679 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
680 ui_test_utils::NavigateToURL(browser(), file_url);
681
682 ClickOnView(VIEW_ID_TAB_CONTAINER);
683
684 // Navigate back. Should focus the location bar.
685 {
686 content::WindowedNotificationObserver back_nav_observer(
687 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
688 content::NotificationService::AllSources());
689 chrome::GoBack(browser(), CURRENT_TAB);
690 back_nav_observer.Wait();
691 }
692
693 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
694
695 // Navigate forward. Shouldn't focus the location bar.
696 ClickOnView(VIEW_ID_TAB_CONTAINER);
697 {
698 content::WindowedNotificationObserver forward_nav_observer(
699 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
700 content::NotificationService::AllSources());
701 chrome::GoForward(browser(), CURRENT_TAB);
702 forward_nav_observer.Wait();
703 }
704
705 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
706}
707
[email protected]629e0342010-07-27 23:30:13708} // namespace